room: steer mid-turn chat to the next step boundary #1
No files matched your search
@@ -82,7 +82,10 @@ web/app/ Nuxt 4 SPA (no SSR)
|
||||
before its last message. `message` is `ReplaySafety::Unsafe`.
|
||||
- Turns are serialized per room. A new mention while a turn runs drops
|
||||
the turn future (turn-level steering) and starts a new turn over the
|
||||
committed session. Do not drop the room task.
|
||||
committed session. A message without a mention never drops anything:
|
||||
`runner::RoomSteering` hands it to the framework's steering hook, which
|
||||
appends it at the next step boundary and advances `absorbed_seq` so the
|
||||
next boundary pass cannot repeat it. Do not drop the room task.
|
||||
- The preview host label is `r<room>-p<port>-<expiry>-<hmac>.<preview
|
||||
host>` for a port, or `r<room>-s<hash>-<expiry>-<hmac>.<preview host>`
|
||||
for a static target. The HMAC covers the target and expiry with a key
|
||||
|
||||
+14
-1
@@ -190,6 +190,16 @@ an `absorbed_seq` cursor, so nothing is appended twice. The cursor
|
||||
advances on every append, whatever the path, so the boundary pass and
|
||||
the interrupt path cannot both write the same line.
|
||||
|
||||
The mid-turn half of that is the framework's steering hook
|
||||
(`HostAgentConfig::steering`), not a step-level drop: the turn's
|
||||
`RoomSteering` source hands the driver one User-role line per drain at a
|
||||
step boundary, so a member can correct the agent while it works without
|
||||
the step in flight dying. It stops at the first line it cannot carry —
|
||||
a mention, which the drop path owns, or a message with images, which a
|
||||
line cannot — and leaves the cursor there, so the boundary pass appends
|
||||
them in the log's order. What steering still cannot do is stop a running
|
||||
command: only the mention drop does that, and only between steps.
|
||||
|
||||
A burst of mentions that lands before the first drop completes all goes
|
||||
into the same restart, in order, one message each. Nothing is folded.
|
||||
|
||||
@@ -663,7 +673,10 @@ Backups: the SQLite file (attachments included) and the secret key.
|
||||
Small and additive, in the framework's own terms:
|
||||
|
||||
1. `HostAgent::step` plus the turn setup around it, so a frontend can own
|
||||
the loop and drop one step. `rhema-code` steers at turn level: it
|
||||
the loop and drop one step. The steering hook covers the common case
|
||||
today: chat lands at a step boundary without a drop. A drop is still
|
||||
what stops a long command, and it still costs the whole turn.
|
||||
`rhema-code` steers at turn level: it
|
||||
drops the whole turn future and starts a new turn with the nudge on
|
||||
the same agent and session (`turn.rs`). That is enough for a
|
||||
single-user TUI, where a turn boundary is a ledger row. A room wants
|
||||
|
||||
+10
-4
@@ -9,7 +9,7 @@ use base64::Engine;
|
||||
use rhema_bash::{AllowAll, ShellTools};
|
||||
use rhema_core::request::ThinkingEffort;
|
||||
use rhema_core::{Overlay, Session, ToolResolver};
|
||||
use rhema_host_agent::{HostAgent, HostAgentConfig};
|
||||
use rhema_host_agent::{HostAgent, HostAgentConfig, Steering};
|
||||
use rhema_sandbox::WorkspaceSeed;
|
||||
use rhema_sandbox::{Sandbox, ShellSandbox};
|
||||
|
||||
@@ -302,8 +302,14 @@ fn room_section(config: &TurnConfig) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
/// Assembles the host agent over `session`.
|
||||
pub fn build_agent(state: &Shared, config: &TurnConfig, session: Session) -> AppResult<HostAgent> {
|
||||
/// Assembles the host agent over `session`, with `steering` as the turn
|
||||
/// loop's step-boundary source (see `runner::RoomSteering`).
|
||||
pub fn build_agent(
|
||||
state: &Shared,
|
||||
config: &TurnConfig,
|
||||
session: Session,
|
||||
steering: Option<Arc<dyn Steering>>,
|
||||
) -> AppResult<HostAgent> {
|
||||
let sandbox = build_sandbox(state, config)?;
|
||||
let executor = Arc::new(ShellSandbox::new(Arc::new(sandbox)));
|
||||
let shell = Arc::new(ShellTools::with_executor(
|
||||
@@ -353,7 +359,7 @@ pub fn build_agent(state: &Shared, config: &TurnConfig, session: Session) -> App
|
||||
shell_env: Default::default(),
|
||||
transcript: Some(Default::default()),
|
||||
summary_worker: None,
|
||||
steering: None,
|
||||
steering,
|
||||
home: None,
|
||||
environment: Some(rhema_host_agent::remote_notice(
|
||||
"ssh",
|
||||
|
||||
+95
-6
@@ -1,16 +1,22 @@
|
||||
//! The per-room turn loop. One task per room, one turn at a time.
|
||||
//!
|
||||
//! Steering is turn level for now: a new mention drops the running turn
|
||||
//! future and starts a fresh turn over the committed session, the shape
|
||||
//! `rhema-code` uses. The step-level hook (`HostAgent::step`) is
|
||||
//! deferred; see the spec's framework section.
|
||||
//! A mention is turn level: it drops the running turn future and starts a
|
||||
//! fresh turn over the committed session, the shape `rhema-code` uses. The
|
||||
//! step-level hook (`HostAgent::step`) is deferred; see the spec's framework
|
||||
//! section.
|
||||
//!
|
||||
//! Chat is step level, through the framework's steering hook: a message
|
||||
//! posted without a mention while the agent works lands in the history at
|
||||
//! the next step boundary instead of waiting for the next turn. See
|
||||
//! [`RoomSteering`].
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
|
||||
|
||||
use rhema_core::event::Event;
|
||||
use rhema_core::{EventSink, Message, Role, Session, Usage};
|
||||
use rhema_host_agent::Steering;
|
||||
use serde_json::json;
|
||||
use tokio::sync::mpsc::UnboundedReceiver;
|
||||
|
||||
@@ -118,7 +124,8 @@ async fn run_turn(
|
||||
rx: &mut UnboundedReceiver<Wake>,
|
||||
pending_mention: &mut bool,
|
||||
) {
|
||||
let mut agent = match agent::build_agent(state, config, session) {
|
||||
let steering = steering_for(state, room, config);
|
||||
let mut agent = match agent::build_agent(state, config, session, Some(steering)) {
|
||||
Ok(agent) => agent,
|
||||
Err(err) => {
|
||||
notice(state, room, "error", err.message);
|
||||
@@ -198,6 +205,88 @@ async fn run_turn(
|
||||
}
|
||||
}
|
||||
|
||||
/// The steering source of one turn: chat that arrives while the agent
|
||||
/// works, appended at the next step boundary.
|
||||
///
|
||||
/// Without it a message posted mid-turn waits for the next turn, so the
|
||||
/// model never reads "no, on the other branch" until the work the line
|
||||
/// is about is finished. The framework drains a [`Steering`] between two
|
||||
/// steps and appends one User-role message per drain, so the line lands
|
||||
/// in the history without destroying the step in flight (the step is
|
||||
/// what the mention path drops, and only a mention does).
|
||||
///
|
||||
/// Two messages deliberately stay out of the drain, and the drain stops
|
||||
/// at the first one it meets so the room log is appended in order:
|
||||
///
|
||||
/// - a mention, which the turn loop answers by dropping the turn. Two
|
||||
/// handlers for one line would append it twice.
|
||||
/// - a message with images, which a line of text cannot carry.
|
||||
///
|
||||
/// Neither is lost: the turn loop's boundary pass reads from the same
|
||||
/// `absorbed_seq` cursor and appends whatever the drain left behind.
|
||||
struct RoomSteering {
|
||||
state: Shared,
|
||||
room: String,
|
||||
/// The room's `absorbed_seq`, as the history and the log agree on it.
|
||||
/// Shared with the turn loop through the row: a line drained here is
|
||||
/// already appended, so the next boundary pass must not append it
|
||||
/// again.
|
||||
absorbed: AtomicI64,
|
||||
}
|
||||
|
||||
impl RoomSteering {
|
||||
fn new(state: Shared, room: String, absorbed: i64) -> Self {
|
||||
Self {
|
||||
state,
|
||||
room,
|
||||
absorbed: AtomicI64::new(absorbed),
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the cursor in the row, and only then in memory, so a failed
|
||||
/// write re-announces the line next turn instead of dropping it.
|
||||
fn advance(&self, seq: i64) {
|
||||
let written = self.state.db.conn().execute(
|
||||
"UPDATE rooms SET absorbed_seq = ?1 WHERE id = ?2",
|
||||
rusqlite::params![seq, self.room],
|
||||
);
|
||||
if written.is_ok() {
|
||||
self.absorbed.store(seq, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Steering for RoomSteering {
|
||||
fn drain(&self) -> Vec<String> {
|
||||
let absorbed = self.absorbed.load(Ordering::SeqCst);
|
||||
let Ok(messages) = pending_messages(&self.state, &self.room, absorbed) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut lines = Vec::new();
|
||||
let mut last = absorbed;
|
||||
for message in messages {
|
||||
if message.mentions || !message.attachments.is_empty() {
|
||||
break;
|
||||
}
|
||||
lines.push(format!("{}: {}", message.author_name, message.text));
|
||||
last = message.seq as i64;
|
||||
}
|
||||
if last != absorbed {
|
||||
self.advance(last);
|
||||
}
|
||||
lines
|
||||
}
|
||||
}
|
||||
|
||||
/// The turn's steering source, over the cursor the turn started from.
|
||||
fn steering_for(state: &Shared, room: &str, config: &TurnConfig) -> Arc<dyn Steering> {
|
||||
Arc::new(RoomSteering::new(
|
||||
state.clone(),
|
||||
room.to_owned(),
|
||||
config.absorbed_seq,
|
||||
))
|
||||
}
|
||||
|
||||
fn pending_messages(state: &Shared, room: &str, absorbed: i64) -> AppResult<Vec<RoomMessage>> {
|
||||
let conn = state.db.conn();
|
||||
let mut stmt = conn.prepare(
|
||||
|
||||
Reference in new issue
Block a user