Skip to content

Commit e0db252

Browse files
author
Larry
committed
fix(acp): admit steers before the prompt is formatted
The native-steer guard waited for the running prompt's DM classification, which was recorded only at prompt formatting. Until then every follow-up cancelled the turn. That regressed same-thread channel follow-ups during session setup, which main steered, and left top-level DM follow-ups restarting during setup. Record the classification as soon as channel metadata resolves. When an initial_message setup prompt will run first, record it after that turn instead, because the setup prompt reads the same steer mailbox. While the classification is unset, use the listener's own channel classification: a channel it knows is not a DM always renders channel-style, so the channel rule applies at once, as on main. A DM or a channel of unresolved type still fails closed. Signed-off-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent 020a765 commit e0db252

3 files changed

Lines changed: 198 additions & 70 deletions

File tree

‎crates/buzz-acp/src/lib.rs‎

Lines changed: 158 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ struct QueuedNormalListenerEvent {
635635
/// The admitted event as a native steer would render it, including an
636636
/// edit's resolved original-message routing.
637637
steer_event: queue::BatchEvent,
638+
/// The listener's DM classification of the channel (unresolved counts as
639+
/// a DM); see [`EventQueue::in_flight_accepts_steer`].
640+
channel_is_dm: bool,
638641
}
639642

640643
impl QueuedNormalListenerEvent {
@@ -669,8 +672,11 @@ impl QueuedNormalListenerEvent {
669672
// takes the cancel+merge path, whose re-prompt carries its own
670673
// `<context>`. The DM rule applies only when the running turn's
671674
// prompt itself was rendered as a DM.
672-
let same_reply_route =
673-
queue.in_flight_accepts_steer(&self.scope, &self.steer_event.reply_route());
675+
let same_reply_route = queue.in_flight_accepts_steer(
676+
&self.scope,
677+
&self.steer_event.reply_route(),
678+
self.channel_is_dm,
679+
);
674680
let native_attempted = matches!(signal, ControlSignal::Steer)
675681
&& same_reply_route
676682
&& try_native_steer(
@@ -716,6 +722,7 @@ impl NormalListenerIngress {
716722
self,
717723
queue: &mut EventQueue,
718724
session_scope: scope::SessionScope,
725+
channel_is_dm: bool,
719726
) -> QueuedNormalListenerEvent {
720727
let Self {
721728
buzz_event,
@@ -746,6 +753,7 @@ impl NormalListenerIngress {
746753
effective_author,
747754
reaction_target_id,
748755
steer_event,
756+
channel_is_dm,
749757
}
750758
}
751759
}
@@ -3523,14 +3531,13 @@ async fn run_harness(
35233531
// channel-keyed routing. Telemetry only for now —
35243532
// queue/pool partitioning by scope lands in a
35253533
// follow-up (see ticket outline steps 2–4).
3526-
let session_scope = ingress.session_scope(
3527-
config.session_policy,
3528-
is_dm_channel(
3529-
ingress.buzz_event.channel_id,
3530-
&ctx.channel_info,
3531-
)
3532-
.await,
3533-
);
3534+
let channel_is_dm = is_dm_channel(
3535+
ingress.buzz_event.channel_id,
3536+
&ctx.channel_info,
3537+
)
3538+
.await;
3539+
let session_scope =
3540+
ingress.session_scope(config.session_policy, channel_is_dm);
35343541
tracing::debug!(
35353542
channel_id = %session_scope.channel_id(),
35363543
scope = %session_scope.telemetry_label(),
@@ -3539,7 +3546,7 @@ async fn run_harness(
35393546
policy = %config.session_policy,
35403547
"admitted event — resolved session scope"
35413548
);
3542-
let queued = ingress.push(&mut queue, session_scope);
3549+
let queued = ingress.push(&mut queue, session_scope, channel_is_dm);
35433550
// 👀 — immediate "seen" reaction, only if the event
35443551
// was actually queued (not dropped by DedupMode::Drop).
35453552
// Fire-and-forget: on rare fast-failure paths the
@@ -10024,7 +10031,7 @@ mod edit_native_steer_tests {
1002410031
};
1002510032
let scope = ingress.session_scope(scope::SessionPolicy::Channel, false);
1002610033
let mut queue = EventQueue::new(config::DedupMode::Queue);
10027-
let queued = ingress.push(&mut queue, scope);
10034+
let queued = ingress.push(&mut queue, scope, false);
1002810035
assert_eq!(queued.steer_event.edit, Some(resolved));
1002910036
assert_eq!(queued.reaction_target_id, original.id.to_hex());
1003010037
}
@@ -10073,7 +10080,7 @@ mod edit_native_steer_tests {
1007310080
let mut queue = EventQueue::new(config::DedupMode::Queue);
1007410081
let running = ingress(running_event, None);
1007510082
let scope = running.session_scope(scope::SessionPolicy::Channel, is_dm);
10076-
running.push(&mut queue, scope.clone());
10083+
running.push(&mut queue, scope.clone(), is_dm);
1007710084
queue.flush_next().expect("running turn");
1007810085
assert!(queue.is_scope_in_flight(&scope));
1007910086
if let Some(prompt_is_dm) = prompt_is_dm {
@@ -10109,7 +10116,7 @@ mod edit_native_steer_tests {
1010910116
);
1011010117
let (ack_tx, _ack_rx) = mpsc::unbounded_channel();
1011110118
incoming_ingress
10112-
.push(&mut queue, scope.clone())
10119+
.push(&mut queue, scope.clone(), is_dm)
1011310120
.steer_or_interrupt(
1011410121
MultipleEventHandling::Steer,
1011510122
None,
@@ -10196,20 +10203,41 @@ mod edit_native_steer_tests {
1019610203
);
1019710204
}
1019810205

10199-
/// The DM steer rule depends on the running prompt's own classification,
10200-
/// which travels from the queue turn through `dispatch_pending` into
10201-
/// `run_prompt_task`, where formatting the prompt records it. Drive that
10202-
/// production handoff with DM channel metadata, then the real listener
10203-
/// admission: a top-level follow-up must be steered natively.
10204-
#[tokio::test]
10205-
async fn dispatched_dm_turn_records_classification_for_native_steer() {
10206+
/// Where a dispatched turn is held when the follow-up arrives: the agent
10207+
/// never answers the first request with this method.
10208+
enum HoldAt {
10209+
/// Session setup, before the turn's own prompt is formatted.
10210+
SessionNew,
10211+
/// The turn's own prompt is running.
10212+
Prompt,
10213+
}
10214+
10215+
/// Dispatch a turn for `running_event` through the production
10216+
/// `dispatch_pending` → `run_prompt_task` path to an agent that holds at
10217+
/// `hold_at`, then drive `incoming` through the real listener admission.
10218+
/// `channel_type` is the channel's metadata (`"dm"` or `"stream"`), known
10219+
/// to the listener and the prompt alike. Returns the native steer request,
10220+
/// if any, and the control signal sent to the running turn, if any.
10221+
async fn steer_into_dispatched_turn(
10222+
channel_type: &str,
10223+
initial_message: Option<&str>,
10224+
hold_at: HoldAt,
10225+
running_event: nostr::Event,
10226+
incoming: nostr::Event,
10227+
) -> (Option<pool::SteerRequest>, Option<ControlSignal>) {
1020610228
let channel_id = Uuid::new_v4();
10229+
let is_dm = channel_type == "dm";
1020710230
let capture = std::env::temp_dir().join(format!(
10208-
"buzz-acp-dm-steer-handoff-{}.ndjson",
10231+
"buzz-acp-dispatched-steer-{}.ndjson",
1020910232
Uuid::new_v4()
1021010233
));
1021110234
let quoted_capture = capture.to_string_lossy().replace('\'', "'\\''");
10212-
// Record each request and never answer: the prompt stays in flight.
10235+
let held_method = match hold_at {
10236+
HoldAt::SessionNew => "session/new",
10237+
HoldAt::Prompt => "session/prompt",
10238+
};
10239+
// Record each request and never answer: the turn stays in flight at
10240+
// its first request, which is `held_method`.
1021310241
let script = format!(
1021410242
r#"while IFS= read -r line; do printf '%s\n' "$line" >> '{quoted_capture}'; done"#
1021510243
);
@@ -10226,8 +10254,8 @@ mod edit_native_steer_tests {
1022610254
prompt_tag: "@mention".into(),
1022710255
edit: None,
1022810256
};
10229-
let running = ingress(message(None));
10230-
let scope = running.session_scope(scope::SessionPolicy::Channel, true);
10257+
let running = ingress(running_event);
10258+
let scope = running.session_scope(scope::SessionPolicy::Channel, is_dm);
1023110259
let mut agent = pool::OwnedAgent {
1023210260
index: 0,
1023310261
acp,
@@ -10238,16 +10266,18 @@ mod edit_native_steer_tests {
1023810266
desired_model_request_id: None,
1023910267
desired_model_pending_ack: false,
1024010268
startup_effort: None,
10241-
agent_name: "dm-steer-test-agent".into(),
10269+
agent_name: "dispatched-steer-test-agent".into(),
1024210270
goose_system_prompt_supported: None,
1024310271
protocol_version: 1,
1024410272
};
10245-
agent
10246-
.state
10247-
.sessions
10248-
.insert(scope.clone(), "live-session".into());
10273+
if matches!(hold_at, HoldAt::Prompt) {
10274+
agent
10275+
.state
10276+
.sessions
10277+
.insert(scope.clone(), "live-session".into());
10278+
}
1024910279
// The relay has no newer metadata, project, or history: every query
10250-
// returns no events, so the prompt keeps the startup DM metadata.
10280+
// returns no events, so the turn keeps the startup channel metadata.
1025110281
let relay = tokio::net::TcpListener::bind("127.0.0.1:0")
1025210282
.await
1025310283
.expect("bind relay fixture");
@@ -10269,12 +10299,13 @@ mod edit_native_steer_tests {
1026910299
});
1027010300
let mut ctx = pool::tests::make_prompt_context_no_owner();
1027110301
ctx.rest_client.base_url = base_url;
10302+
ctx.initial_message = initial_message.map(str::to_string);
1027210303
ctx.channel_info = pool::ChannelInfoResolver::new(
1027310304
HashMap::from([(
1027410305
channel_id,
1027510306
relay::ChannelInfo {
10276-
name: "dm".into(),
10277-
channel_type: "dm".into(),
10307+
name: "test-channel".into(),
10308+
channel_type: channel_type.into(),
1027810309
description: None,
1027910310
},
1028010311
)]),
@@ -10283,26 +10314,24 @@ mod edit_native_steer_tests {
1028310314
let ctx = Arc::new(ctx);
1028410315
let mut pool = AgentPool::from_slots(vec![Some(agent)]);
1028510316
let mut queue = EventQueue::new(config::DedupMode::Queue);
10286-
running.push(&mut queue, scope.clone());
10317+
running.push(&mut queue, scope.clone(), is_dm);
1028710318
let mut last_activity = tokio::time::Instant::now();
1028810319
let dispatched = dispatch_pending(&mut pool, &mut queue, &ctx, &mut last_activity, None);
10289-
assert_eq!(dispatched.len(), 1, "the DM turn is dispatched");
10320+
assert_eq!(dispatched.len(), 1, "the running turn is dispatched");
1029010321

10291-
// The prompt reaches the agent only after `run_prompt_task` has
10292-
// formatted it, so its classification is settled by then.
10322+
// The turn has passed every step before `held_method` once that
10323+
// request reaches the agent.
10324+
let held_request = format!("\"{held_method}\"");
1029310325
tokio::time::timeout(Duration::from_secs(10), async {
10294-
loop {
10295-
let prompted = std::fs::read_to_string(&capture)
10296-
.unwrap_or_default()
10297-
.contains("\"session/prompt\"");
10298-
if prompted {
10299-
break;
10300-
}
10326+
while !std::fs::read_to_string(&capture)
10327+
.unwrap_or_default()
10328+
.contains(&held_request)
10329+
{
1030110330
tokio::time::sleep(Duration::from_millis(10)).await;
1030210331
}
1030310332
})
1030410333
.await
10305-
.expect("dispatched DM prompt reaches the agent");
10334+
.expect("dispatched turn reaches its held request");
1030610335

1030710336
// Observe the listener's decision through the running task's own
1030810337
// steer and control channels.
@@ -10312,13 +10341,13 @@ mod edit_native_steer_tests {
1031210341
.task_map_mut()
1031310342
.values_mut()
1031410343
.find(|meta| meta.scope.as_ref() == Some(&scope))
10315-
.expect("running DM task");
10344+
.expect("running task");
1031610345
meta.control_tx = Some(control_tx);
1031710346
meta.steer_tx = Some(steer_tx);
1031810347

1031910348
let (ack_tx, _ack_rx) = mpsc::unbounded_channel();
10320-
ingress(message(None))
10321-
.push(&mut queue, scope.clone())
10349+
ingress(incoming)
10350+
.push(&mut queue, scope.clone(), is_dm)
1032210351
.steer_or_interrupt(
1032310352
MultipleEventHandling::Steer,
1032410353
None,
@@ -10333,6 +10362,19 @@ mod edit_native_steer_tests {
1033310362
while pool.join_set.join_next().await.is_some() {}
1033410363
relay.abort();
1033510364
let _ = std::fs::remove_file(&capture);
10365+
(steer, control)
10366+
}
10367+
10368+
/// The DM steer rule depends on the running prompt's own classification,
10369+
/// which travels from the queue turn through `dispatch_pending` into
10370+
/// `run_prompt_task`, which records it. Drive that production handoff
10371+
/// with DM channel metadata, then the real listener admission: a
10372+
/// top-level follow-up must be steered natively.
10373+
#[tokio::test]
10374+
async fn dispatched_dm_turn_records_classification_for_native_steer() {
10375+
let (steer, control) =
10376+
steer_into_dispatched_turn("dm", None, HoldAt::Prompt, message(None), message(None))
10377+
.await;
1033610378

1033710379
assert!(steer.is_some(), "DM follow-up is sent as a native steer");
1033810380
assert_eq!(
@@ -10341,6 +10383,71 @@ mod edit_native_steer_tests {
1034110383
);
1034210384
}
1034310385

10386+
/// The prompt task records its classification as soon as channel
10387+
/// metadata resolves, so a top-level DM follow-up during a slow
10388+
/// `session/new` is steered natively too.
10389+
#[tokio::test]
10390+
async fn dm_follow_up_during_session_setup_steers_natively() {
10391+
let (steer, control) = steer_into_dispatched_turn(
10392+
"dm",
10393+
None,
10394+
HoldAt::SessionNew,
10395+
message(None),
10396+
message(None),
10397+
)
10398+
.await;
10399+
10400+
assert!(steer.is_some(), "DM follow-up is sent as a native steer");
10401+
assert_eq!(
10402+
control, None,
10403+
"native steer must not cancel the running turn"
10404+
);
10405+
}
10406+
10407+
/// An `initial_message` setup prompt reads the same steer mailbox, so a
10408+
/// DM follow-up must not be admitted for native steering before that
10409+
/// setup turn finishes; it takes the cancel+merge path.
10410+
#[tokio::test]
10411+
async fn dm_follow_up_before_initial_message_cancels_and_merges() {
10412+
let (steer, control) = steer_into_dispatched_turn(
10413+
"dm",
10414+
Some("set up the session"),
10415+
HoldAt::SessionNew,
10416+
message(None),
10417+
message(None),
10418+
)
10419+
.await;
10420+
10421+
assert!(
10422+
steer.is_none(),
10423+
"no native steer into the initial_message setup turn"
10424+
);
10425+
assert_eq!(control, Some(ControlSignal::Steer));
10426+
}
10427+
10428+
/// A known channel's prompt is never rendered as a DM, so a same-thread
10429+
/// follow-up is steered natively before the turn's classification is
10430+
/// recorded, as before DM steering existed: here the prompt task is
10431+
/// still waiting on `session/new` ahead of an `initial_message`.
10432+
#[tokio::test]
10433+
async fn channel_same_thread_follow_up_during_setup_steers_natively() {
10434+
let root = "ab".repeat(32);
10435+
let (steer, control) = steer_into_dispatched_turn(
10436+
"stream",
10437+
Some("set up the session"),
10438+
HoldAt::SessionNew,
10439+
message(Some(&root)),
10440+
message(Some(&root)),
10441+
)
10442+
.await;
10443+
10444+
assert!(steer.is_some(), "same-thread follow-up is a native steer");
10445+
assert_eq!(
10446+
control, None,
10447+
"native steer must not cancel the running turn"
10448+
);
10449+
}
10450+
1034410451
/// A DM thread reply needs a `--reply-to` the running top-level turn's
1034510452
/// `<context>` does not carry, so it takes the cancel+merge path.
1034610453
#[tokio::test]
@@ -10373,10 +10480,10 @@ mod edit_native_steer_tests {
1037310480
}
1037410481

1037510482
/// Before the prompt task records its classification, nothing proves
10376-
/// which `<context>` the turn will carry, so the follow-up takes the
10377-
/// cancel+merge path.
10483+
/// which `<context>` a turn in a DM (or a channel of unresolved type)
10484+
/// will carry, so the follow-up takes the cancel+merge path.
1037810485
#[tokio::test]
10379-
async fn follow_up_before_prompt_classification_cancels_and_merges() {
10486+
async fn dm_follow_up_before_prompt_classification_cancels_and_merges() {
1038010487
let (steer, control) =
1038110488
steer_into_running_turn(true, None, message(None), message(None), None);
1038210489

0 commit comments

Comments
 (0)