Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 106 additions & 84 deletions core/continuum-core/src/airc/inbound_attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::cognition::conversation::room_event::chat_transcript_message;
use crate::routing::airc_realtime_wire::{bus_event_from_envelope, envelope_from_event};
use crate::ipc::positron_kanban_source::KANBAN_CHANGED;
use crate::ipc::positron_source::CHAT_POSTED;
use continuum_mind::chat::import_watermark::ImportWatermark;
use continuum_mind::chat::line_store::{LineImporter, PostedLine};
use crate::ipc::positron_wall_source::WALL_CHANGED;
use crate::runtime::MessageBus;

Expand Down Expand Up @@ -132,61 +134,50 @@ pub fn spawn_daemon_attach(
});
}

/// Where the per-channel attach watermark persists — the CONSUMER CURSOR
/// (task #242). One tiny JSON file per channel under the user's continuum
/// state dir: the [`IpcCursor`] this process has caught up to, threaded back
/// as [`AttachStart::After`] on the next attach so a core reboot resumes at
/// the gap instead of replaying the room's entire history into the UI and
/// every persona's perception (glass-boxed three times on 2026-07-30 — each
/// reboot re-fed days of transcript as fresh inbox to every mind).
///
/// Under the canonical continuum home, never a cwd-relative `.`: on the 5090
/// (2026-10-06) the Windows service had no `HOME`, the old `unwrap_or(".")`
/// resolved `.\.continuum\state` against an unwritable working directory, and
/// 592 persists failed "Access is denied", so every core restart re-fed the
/// room into the desktop, the human views and every persona as if it were new.
fn cursor_path(channel: &RoomId) -> Result<PathBuf, String> {
Ok(crate::paths::continuum_home()?
.join("state")
.join(format!("airc-attach-cursor-{}.json", channel.as_uuid())))
/// The per-channel attach watermark — the CONSUMER CURSOR (task #242): the [`IpcCursor`] this
/// core has caught up to, threaded back as [`AttachStart::After`] on the next attach so a reboot
/// resumes at the gap instead of replaying the room's history into the UI and every persona's
/// perception (glass-boxed three times on 2026-07-30). It lives in the chat store beside the lines
/// it vouches for (`chat::import_watermark`, Slice P), written through the ORM; every line up to
/// it has already been imported, because the seam awaits each import before the next frame.
async fn load_cursor(lines: &dyn LineImporter, channel: &RoomId) -> Result<Option<IpcCursor>, String> {
if let Some(imported) = lines.watermark(*channel).await? {
let mark = imported.position;
return Ok(Some(IpcCursor { epoch: mark.epoch, counter: mark.counter, event_id: airc_core::EventId::from_uuid(mark.event_id) }));
}
migrate_legacy_cursor(lines, channel).await
}

fn load_cursor(channel: &RoomId) -> Option<IpcCursor> {
let path = match cursor_path(channel) {
Ok(path) => path,
Err(error) => {
warn!("airc attach cursor has no home ({error}): this attach starts as a first attach");
return None;
}
/// One-time migration: a node that persisted its watermark as a JSON file before the store owned
/// it (`state/airc-attach-cursor-<room>.json`) hands it to the store and removes the file, so the
/// first attach after the upgrade resumes where the last one stopped. That file advanced
/// independently of the old lossy writer, so it does NOT vouch that the lines before it were
/// stored: the store records it as unvouched through that position (`ImportHistory`), and a reader
/// of that history perceives a gap (Codex on #5694). A missing or corrupt file is a first attach.
async fn migrate_legacy_cursor(lines: &dyn LineImporter, channel: &RoomId) -> Result<Option<IpcCursor>, String> {
let Ok(home) = crate::paths::continuum_home() else {
return Ok(None);
};
let raw = std::fs::read_to_string(path).ok()?;
// A corrupt watermark degrades to first-attach semantics (one full seed) —
// annoying, never wrong. It is presentation-adjacent state, not truth: the
// durable transcript is the storage of record either way.
serde_json::from_str(&raw).ok()
}

fn persist_cursor(channel: &RoomId, cursor: &IpcCursor) {
let path = match cursor_path(channel) {
Ok(path) => path,
Err(error) => {
warn!("failed to persist airc attach cursor: no continuum home ({error})");
return;
}
let path = home.join("state").join(format!("airc-attach-cursor-{}.json", channel.as_uuid()));
let Some(cursor) = std::fs::read_to_string(&path).ok().and_then(|raw| serde_json::from_str::<IpcCursor>(&raw).ok()) else { // storage boundary: the legacy cursor file's JSON
return Ok(None);
};
if let Some(dir) = path.parent() {
let _ = std::fs::create_dir_all(dir);
lines.migrate_watermark(*channel, mark_of(&cursor)).await?;
if let Err(error) = std::fs::remove_file(&path) {
warn!("migrated the airc attach cursor into the store but could not remove {}: {error}", path.display());
}
match serde_json::to_string(cursor) {
Ok(json) => {
if let Err(error) = std::fs::write(&path, json) {
warn!(
"failed to persist airc attach cursor to {}: {error}",
path.display()
);
}
}
Err(error) => warn!("failed to serialize airc attach cursor: {error}"),
Ok(Some(cursor))
}

fn mark_of(cursor: &IpcCursor) -> ImportWatermark {
ImportWatermark { epoch: cursor.epoch, counter: cursor.counter, event_id: cursor.event_id.as_uuid() }
}

/// Record the daemon's advance (always an already-delivered, already-imported event). A failure
/// costs a larger replay on the next attach, which the idempotent import absorbs: said, not fatal.
async fn persist_cursor(lines: &dyn LineImporter, channel: &RoomId, cursor: &IpcCursor) {
if let Err(error) = lines.advance_watermark(*channel, mark_of(cursor)).await {
warn!("failed to persist the airc attach watermark for {channel}: {error}");
}
}

Expand Down Expand Up @@ -264,7 +255,8 @@ pub async fn run_daemon_attach(
// The room starts at join for a fresh node — personas perceive the
// room AS IT IS NOW (#131), and deep history is #249's bounded
// durable-transcript hydration, never a perception replay.
let request = attach_request_for_cursor(channel, load_cursor(&channel));
let lines = continuum_mind::chat::line_store::ChatStore;
let request = attach_request_for_cursor(channel, load_cursor(&lines, &channel).await?);
let mut stream = client
.attach(request)
.await
Expand Down Expand Up @@ -303,13 +295,17 @@ pub async fn run_daemon_attach(
probe_class = "airc.cursor.advanced",
"attach watermark advanced (daemon heartbeat) — persisting"
);
persist_cursor(&channel, advanced_to);
persist_cursor(&lines, &channel, advanced_to).await;
}
handle_attach_response(response, &bus).await?;
handle_attach_response(response, &bus, &lines).await?;
}
}

pub async fn handle_attach_response(response: Response, bus: &MessageBus) -> Result<(), String> {
pub async fn handle_attach_response(
response: Response,
bus: &MessageBus,
lines: &dyn LineImporter,
) -> Result<(), String> {
match response {
Response::Ok => Ok(()),
// v5 owner-core schema (task #82): the daemon now streams raw
Expand All @@ -318,7 +314,7 @@ pub async fn handle_attach_response(response: Response, bus: &MessageBus) -> Res
// TranscriptEvent. A malformed buffer is logged + skipped (the
// live stream shouldn't die because one event failed to parse).
Response::Event { envelope } => match decode_wire_event(envelope) {
Ok(event) => publish_transcript_event(&Arc::new(event), bus).await,
Ok(event) => publish_transcript_event(&Arc::new(event), bus, lines).await,
Err(error) => {
warn!("Skipping malformed airc daemon event: {error}");
Ok(())
Expand All @@ -338,6 +334,7 @@ pub async fn handle_attach_response(response: Response, bus: &MessageBus) -> Res
pub async fn publish_transcript_event(
event: &Arc<airc_core::TranscriptEvent>,
bus: &MessageBus,
lines: &dyn LineImporter,
) -> Result<(), String> {
// THE ONE INBOUND DECODE POINT for room events: the transport kind and headers
// decide protocol frame or conversation, before anything reads the payload. The
Expand Down Expand Up @@ -394,7 +391,13 @@ pub async fn publish_transcript_event(
// content). An event is at most one of these; anything else is not
// ours here → skip. Classification, never a fallback — a
// non-matching kind fabricates nothing.
if let Some((name, payload)) = chat_posted_from_message(event) {
if let Some(line) = posted_from_message(event) {
// The durable transcript FIRST, awaited (#140, Slice P): an error ends this
// attach before the watermark can pass the line, and the re-attach replays it;
// the import is idempotent by id, so the replay never doubles it.
lines.import(line.clone()).await.map_err(|e| {
format!("inbound line not stored; re-attaching to replay it: {e}")
})?;
// task #84: the persona-turn / plain-airc-message stream into
// the room. This is the seam a client's live read surface
// (positron ChatViewState) is fed from — probe it so the turn
Expand All @@ -412,9 +415,9 @@ pub async fn publish_transcript_event(
crate::cognition::deliberation_budget::record_room_speech(
event.room_id.as_uuid(),
Some(event.peer_id.as_uuid()),
payload["content"].as_str().unwrap_or_default(),
&line.text,
);
bus.publish_async_only(name, payload);
bus.publish_async_only(CHAT_POSTED, chat_posted_payload(&line));
} else if let Some((name, payload)) = wall_changed_from_event(event) {
bus.publish_async_only(name, payload);
} else if let Some((name, payload)) = kanban_changed_from_event(event) {
Expand Down Expand Up @@ -456,10 +459,16 @@ pub async fn publish_transcript_event(
"first capacity offer heard from a grid peer",
);
}
} else if let Some((name, payload)) = chat_posted_from_envelope(&envelope, event) {
} else if let Some(line) = posted_from_envelope(&envelope, event) {
// The same awaited, idempotent import as the plain arm (a human line was stored by
// chat/send under this id already; the import reuses its row and arrival).
lines
.import(line.clone())
.await
.map_err(|e| format!("inbound line not stored; re-attaching to replay it: {e}"))?;
continuum_base::probe!(
class = "airc.chat.projected",
sender_id = %payload["senderId"],
sender_id = %line.author.as_uuid(),
room_id = %event.room_id.as_uuid(),
event_id = %event.event_id.as_uuid(),
"chat_transcript envelope projected to chat:posted for positron"
Expand All @@ -468,17 +477,17 @@ pub async fn publish_transcript_event(
crate::cognition::deliberation_budget::record_room_speech(
event.room_id.as_uuid(),
Some(event.peer_id.as_uuid()),
payload["content"].as_str().unwrap_or_default(),
&line.text,
);
bus.publish_async_only(name, payload);
bus.publish_async_only(CHAT_POSTED, chat_posted_payload(&line));
}
Ok(())
}

/// Project a `chat_transcript` envelope (a human `chat/send`, the web
/// client — the `Body::Json` room-message shape) into the SAME thin
/// `chat:posted` payload the plain-text sibling
/// [`chat_posted_from_message`] emits. Returns `None` for any other
/// [`posted_from_message`] emits. Returns `None` for any other
/// envelope (EventBridge, presence, media-control) — it is not a chat line.
///
/// Identity-free like its sibling: `senderId` is the envelope's logical
Expand All @@ -492,10 +501,10 @@ pub async fn publish_transcript_event(
/// the inline id means the projection, the durable chat store, and the
/// original `chat/send` all agree on ONE identity per human message, so
/// the store-side projector (#140) dedups instead of double-writing.
fn chat_posted_from_envelope(
fn posted_from_envelope(
envelope: &AircRealtimeEnvelope,
event: &airc_core::TranscriptEvent,
) -> Option<(&'static str, serde_json::Value)> {
) -> Option<PostedLine> {
let (sender_id, content) = chat_transcript_message(envelope, event.peer_id.as_uuid())?;
let message_id = match &envelope.payload {
crate::routing::airc_realtime::AircRealtimePayload::ExistingSchema { payload } => payload
Expand All @@ -507,14 +516,36 @@ fn chat_posted_from_envelope(
_ => None,
}
.unwrap_or_else(|| event.event_id.as_uuid());
let payload = serde_json::json!({
"messageId": message_id,
"roomId": event.room_id.as_uuid(),
"senderId": sender_id,
"content": content,
"timestamp": event.occurred_at_ms,
});
Some((CHAT_POSTED, payload))
let mut line = posted_line(event, content)?;
line.message_id = message_id;
line.author = airc_core::PeerId::from_uuid(sender_id);
Some(line)
}

/// The typed line of a room event, from its header (kind, ids, addressee) and its decoded text;
/// `None` when the event is not a room line.
fn posted_line(event: &airc_core::TranscriptEvent, text: String) -> Option<PostedLine> {
let arrival = continuum_mind::chat::arrival::Arrival::of_event(event)?;
Some(PostedLine {
message_id: arrival.message_id,
room: arrival.room,
author: arrival.author,
kind: arrival.kind,
direct_to: arrival.direct_to,
occurred_at_ms: event.occurred_at_ms,
text,
})
}

/// The `chat:posted` projection cue the positron renderer reads, derived from the typed line.
fn chat_posted_payload(line: &PostedLine) -> serde_json::Value {
serde_json::json!({
"messageId": line.message_id,
"roomId": line.room.as_uuid(),
"senderId": line.author.as_uuid(),
"content": line.text,
"timestamp": line.occurred_at_ms,
})
}

/// Decode a `grid_capacity` envelope's inline payload into a [`CapacityOffer`].
Expand Down Expand Up @@ -547,21 +578,12 @@ fn capacity_offer_from_envelope(
/// is a presence fact, never a message fact — see
/// `airc-to-positron-chat-projection`. airc's `event_id` IS the stable
/// `message_id` (same event → same id across replay + peers).
fn chat_posted_from_message(
event: &airc_core::TranscriptEvent,
) -> Option<(&'static str, serde_json::Value)> {
fn posted_from_message(event: &airc_core::TranscriptEvent) -> Option<PostedLine> {
if event.kind != TranscriptKind::Message {
return None;
}
let content = event.body.as_ref()?.as_text()?;
let payload = serde_json::json!({
"messageId": event.event_id.as_uuid(),
"roomId": event.room_id.as_uuid(),
"senderId": event.peer_id.as_uuid(),
"content": content,
"timestamp": event.occurred_at_ms,
});
Some((CHAT_POSTED, payload))
posted_line(event, content.to_string())
}

/// Project a `WallPostPublished` transcript event into the `wall:changed`
Expand Down
Loading
Loading