Skip to content

Commit 7c78810

Browse files
author
Larry
committed
fix(acp): steer by the running prompt's DM classification
The native-steer guard classified the incoming event's channel again. If a turn started while channel metadata was unavailable, its prompt used channel-style <context>; once metadata resolved, a top-level DM follow-up looked route-compatible and was steered into a turn that replies in a thread. The prompt task now records the classification it rendered into the in-flight turn's queue record. The guard reads that value and takes the cancel+merge path until it is recorded. Signed-off-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent e4653e0 commit 7c78810

3 files changed

Lines changed: 133 additions & 55 deletions

File tree

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

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,8 @@ impl QueuedNormalListenerEvent {
649649
});
650650
}
651651

652-
/// `prompt_is_dm` must match how the running turn's prompt classified the
653-
/// channel (see [`resolved_dm_channel`]): the DM steer rule is only right
654-
/// for a turn whose `<context>` was rendered as a DM.
655652
fn steer_or_interrupt(
656653
self,
657-
prompt_is_dm: bool,
658654
handling: MultipleEventHandling,
659655
owner: Option<&str>,
660656
pool: &mut AgentPool,
@@ -671,12 +667,10 @@ impl QueuedNormalListenerEvent {
671667
// carry a message that replies in the same place. A channel-policy or
672668
// DM session spans several destinations; a message for another one
673669
// takes the cancel+merge path, whose re-prompt carries its own
674-
// `<context>`.
675-
let same_reply_route = queue
676-
.in_flight_reply_route(&self.scope)
677-
.is_some_and(|running| {
678-
running.accepts_steer(&self.steer_event.reply_route(), prompt_is_dm)
679-
});
670+
// `<context>`. The DM rule applies only when the running turn's
671+
// 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());
680674
let native_attempted = matches!(signal, ControlSignal::Steer)
681675
&& same_reply_route
682676
&& try_native_steer(
@@ -832,33 +826,8 @@ pub(crate) async fn is_dm_channel(
832826
channel_id: Uuid,
833827
channel_info: &pool::ChannelInfoResolver,
834828
) -> bool {
835-
fail_closed_dm(
836-
channel_id,
837-
resolved_dm_channel(channel_id, channel_info).await,
838-
)
839-
}
840-
841-
/// Whether `channel_id` is a DM according to its metadata, or `None` when the
842-
/// metadata cannot be resolved.
843-
///
844-
/// Prompt formatting treats unresolved metadata as a non-DM channel, while
845-
/// the author gate fails closed as a DM ([`fail_closed_dm`]). Decisions that
846-
/// must agree with the rendered prompt, such as native steering, use this
847-
/// value with the prompt's fallback.
848-
pub(crate) async fn resolved_dm_channel(
849-
channel_id: Uuid,
850-
channel_info: &pool::ChannelInfoResolver,
851-
) -> Option<bool> {
852-
channel_info
853-
.resolve_channel_metadata(channel_id)
854-
.await
855-
.map(|info| info.channel_type == "dm")
856-
}
857-
858-
/// The author gate's DM classification: unresolved metadata is a DM.
859-
fn fail_closed_dm(channel_id: Uuid, resolved: Option<bool>) -> bool {
860-
match resolved {
861-
Some(is_dm) => is_dm,
829+
match channel_info.resolve_channel_metadata(channel_id).await {
830+
Some(info) => info.channel_type == "dm",
862831
None => {
863832
tracing::warn!(
864833
channel_id = %channel_id,
@@ -3554,12 +3523,13 @@ async fn run_harness(
35543523
// channel-keyed routing. Telemetry only for now —
35553524
// queue/pool partitioning by scope lands in a
35563525
// follow-up (see ticket outline steps 2–4).
3557-
let channel_id = ingress.buzz_event.channel_id;
3558-
let resolved_dm =
3559-
resolved_dm_channel(channel_id, &ctx.channel_info).await;
35603526
let session_scope = ingress.session_scope(
35613527
config.session_policy,
3562-
fail_closed_dm(channel_id, resolved_dm),
3528+
is_dm_channel(
3529+
ingress.buzz_event.channel_id,
3530+
&ctx.channel_info,
3531+
)
3532+
.await,
35633533
);
35643534
tracing::debug!(
35653535
channel_id = %session_scope.channel_id(),
@@ -3581,8 +3551,6 @@ async fn run_harness(
35813551
// event data through the optional steer/interrupt
35823552
// decision.
35833553
queued.steer_or_interrupt(
3584-
// The prompt's fallback: unresolved is not a DM.
3585-
resolved_dm.unwrap_or(false),
35863554
config.multiple_event_handling,
35873555
owner_cache.get(),
35883556
&mut pool,
@@ -4908,6 +4876,9 @@ fn dispatch_pending(
49084876
.state
49094877
.set_scope_owner_generation(scope.clone(), owner_generation);
49104878

4879+
// The prompt task records how it classified the channel, for the
4880+
// native-steer guard (`EventQueue::in_flight_accepts_steer`).
4881+
let prompt_dm = queue.in_flight_prompt_dm(&scope).unwrap_or_default();
49114882
let abort_handle = pool.join_set.spawn(async move {
49124883
pool::run_prompt_task(
49134884
agent,
@@ -4917,6 +4888,7 @@ fn dispatch_pending(
49174888
result_tx,
49184889
Some(control_rx),
49194890
task_turn_id,
4891+
prompt_dm,
49204892
)
49214893
.await;
49224894
});
@@ -5638,6 +5610,7 @@ fn dispatch_heartbeat(
56385610
result_tx,
56395611
None,
56405612
task_turn_id,
5613+
Default::default(),
56415614
)
56425615
.await;
56435616
});
@@ -10069,14 +10042,17 @@ mod edit_native_steer_tests {
1006910042
target_event_id: original.id.to_hex(),
1007010043
target_thread_tags: queue::parse_thread_tags(original),
1007110044
};
10072-
steer_into_running_turn(false, running_event, edit, Some(resolved))
10045+
steer_into_running_turn(false, Some(false), running_event, edit, Some(resolved))
1007310046
}
1007410047

1007510048
/// Drive `incoming` through the listener's steer decision while a turn
1007610049
/// for `running_event` is in flight in the same conversation session
10077-
/// (the channel policy, or any DM).
10050+
/// (the channel policy, or any DM). `prompt_is_dm` is how the running
10051+
/// turn's prompt classified the channel; `None` means the prompt has not
10052+
/// been formatted yet.
1007810053
fn steer_into_running_turn(
1007910054
is_dm: bool,
10055+
prompt_is_dm: Option<bool>,
1008010056
running_event: nostr::Event,
1008110057
incoming: nostr::Event,
1008210058
incoming_edit: Option<queue::ResolvedEdit>,
@@ -10100,6 +10076,12 @@ mod edit_native_steer_tests {
1010010076
running.push(&mut queue, scope.clone());
1010110077
queue.flush_next().expect("running turn");
1010210078
assert!(queue.is_scope_in_flight(&scope));
10079+
if let Some(prompt_is_dm) = prompt_is_dm {
10080+
queue
10081+
.in_flight_prompt_dm(&scope)
10082+
.expect("in-flight turn records its prompt classification")
10083+
.record(prompt_is_dm);
10084+
}
1010310085

1010410086
let mut pool = AgentPool::from_slots(vec![]);
1010510087
let (control_tx, mut control_rx) = tokio::sync::oneshot::channel();
@@ -10129,7 +10111,6 @@ mod edit_native_steer_tests {
1012910111
incoming_ingress
1013010112
.push(&mut queue, scope.clone())
1013110113
.steer_or_interrupt(
10132-
is_dm,
1013310114
MultipleEventHandling::Steer,
1013410115
None,
1013510116
&mut pool,
@@ -10205,7 +10186,8 @@ mod edit_native_steer_tests {
1020510186
/// natively into the running turn rather than cancelling it.
1020610187
#[tokio::test]
1020710188
async fn dm_top_level_follow_up_steers_natively() {
10208-
let (steer, control) = steer_into_running_turn(true, message(None), message(None), None);
10189+
let (steer, control) =
10190+
steer_into_running_turn(true, Some(true), message(None), message(None), None);
1020910191

1021010192
assert!(steer.is_some(), "DM follow-up is sent as a native steer");
1021110193
assert_eq!(
@@ -10220,14 +10202,42 @@ mod edit_native_steer_tests {
1022010202
async fn dm_thread_reply_during_top_level_turn_cancels_and_merges() {
1022110203
let root = "ab".repeat(32);
1022210204
let (steer, control) =
10223-
steer_into_running_turn(true, message(None), message(Some(&root)), None);
10205+
steer_into_running_turn(true, Some(true), message(None), message(Some(&root)), None);
1022410206

1022510207
assert!(
1022610208
steer.is_none(),
1022710209
"no native steer into a different DM thread"
1022810210
);
1022910211
assert_eq!(control, Some(ControlSignal::Steer));
1023010212
}
10213+
10214+
/// A DM turn whose prompt was rendered while channel metadata was
10215+
/// unavailable got channel-style `<context>`: its replies go to a thread
10216+
/// rooted at its trigger. The DM rule must not apply once metadata
10217+
/// resolves, so a top-level follow-up takes the cancel+merge path.
10218+
#[tokio::test]
10219+
async fn dm_follow_up_into_channel_formatted_turn_cancels_and_merges() {
10220+
let (steer, control) =
10221+
steer_into_running_turn(true, Some(false), message(None), message(None), None);
10222+
10223+
assert!(
10224+
steer.is_none(),
10225+
"steer guard follows the running prompt's classification"
10226+
);
10227+
assert_eq!(control, Some(ControlSignal::Steer));
10228+
}
10229+
10230+
/// Before the prompt task records its classification, nothing proves
10231+
/// which `<context>` the turn will carry, so the follow-up takes the
10232+
/// cancel+merge path.
10233+
#[tokio::test]
10234+
async fn follow_up_before_prompt_classification_cancels_and_merges() {
10235+
let (steer, control) =
10236+
steer_into_running_turn(true, None, message(None), message(None), None);
10237+
10238+
assert!(steer.is_none(), "no native steer without a recorded prompt");
10239+
assert_eq!(control, Some(ControlSignal::Steer));
10240+
}
1023110241
}
1023210242

1023310243
#[cfg(test)]

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,7 @@ fn send_prompt_result(
24112411
///
24122412
/// The agent is ALWAYS returned — even on panic the `JoinSet` detects the
24132413
/// abort and the caller uses `task_map` to recover the agent index.
2414+
#[allow(clippy::too_many_arguments)]
24142415
pub async fn run_prompt_task(
24152416
mut agent: OwnedAgent,
24162417
batch: Option<FlushBatch>,
@@ -2419,6 +2420,7 @@ pub async fn run_prompt_task(
24192420
result_tx: mpsc::UnboundedSender<PromptResult>,
24202421
control_rx: Option<tokio::sync::oneshot::Receiver<ControlSignal>>,
24212422
turn_id: String,
2423+
prompt_dm: crate::queue::PromptDmClassification,
24222424
) {
24232425
// Is this a channel prompt or a heartbeat?
24242426
let source = match &batch {
@@ -3031,6 +3033,9 @@ pub async fn run_prompt_task(
30313033
.as_ref()
30323034
.map(|info| info.channel_type == "dm")
30333035
.unwrap_or(false);
3036+
// `format_prompt` derives the same value from `channel_info`; the
3037+
// main loop's native-steer guard reads it from here.
3038+
prompt_dm.record(is_dm);
30343039
let context_target = resolve_context_target(b, is_dm);
30353040
let hydrated_thread_root = match &context_target {
30363041
ContextTarget::Thread(root) => Some(root),
@@ -7134,6 +7139,7 @@ mod tests {
71347139
result_tx,
71357140
None,
71367141
"observer-test-turn".into(),
7142+
Default::default(),
71377143
));
71387144
let liveness = tokio::time::timeout(Duration::from_secs(5), async {
71397145
request_rx.await.expect("channel context lookup started");
@@ -7227,6 +7233,7 @@ done"#
72277233
result_tx.clone(),
72287234
None,
72297235
format!("turn-{turn}"),
7236+
Default::default(),
72307237
)
72317238
.await;
72327239
let result = result_rx.recv().await.expect("prompt result");
@@ -7350,6 +7357,7 @@ done"#
73507357
result_tx.clone(),
73517358
None,
73527359
format!("turn-{turn}"),
7360+
Default::default(),
73537361
)
73547362
.await;
73557363
let result = result_rx.recv().await.expect("prompt result");
@@ -7563,6 +7571,7 @@ done"#
75637571
result_tx.clone(),
75647572
None,
75657573
"first-turn".into(),
7574+
Default::default(),
75667575
)
75677576
.await;
75687577
let first_result = result_rx.recv().await.expect("first prompt result");
@@ -7582,6 +7591,7 @@ done"#
75827591
result_tx,
75837592
None,
75847593
"follow-up-turn".into(),
7594+
Default::default(),
75857595
)
75867596
.await;
75877597
let mut result = result_rx.recv().await.expect("prompt result");
@@ -7752,6 +7762,7 @@ done"#
77527762
result_tx.clone(),
77537763
None,
77547764
turn_id.into(),
7765+
Default::default(),
77557766
)
77567767
.await;
77577768
let result = result_rx.recv().await.expect("prompt result");
@@ -7916,6 +7927,7 @@ printf '%s\n' '{{"jsonrpc":"2.0","id":0,"result":{{"stopReason":"end_turn"}}}}'"
79167927
result_tx,
79177928
None,
79187929
"next-turn".into(),
7930+
Default::default(),
79197931
)
79207932
.await;
79217933
let mut result = result_rx.recv().await.expect("next prompt result");
@@ -11140,6 +11152,7 @@ done"#
1114011152
result_tx,
1114111153
None,
1114211154
"indeterminate-project-turn".into(),
11155+
Default::default(),
1114311156
)
1114411157
.await;
1114511158

0 commit comments

Comments
 (0)