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
14 changes: 14 additions & 0 deletions cli/src/bridge/v2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ impl Renderer {

/// The answers being sent to an open question, so its closed card keeps
/// what was asked and what was answered. Secret answers are dropped here.
/// Whether `interaction` is open in this session (its card was rendered
/// and it hasn't closed). False for one from before a restart.
pub(crate) fn is_open(&self, interaction: &InteractionId) -> bool {
self.interactions.contains_key(interaction)
}

/// Drops an interaction whose card Inline never showed, so closing it
/// renders nothing (there is no card to update).
pub(crate) fn forget(&mut self, interaction: &InteractionId) {
self.interactions.remove(interaction);
self.card_diffs.remove(interaction);
self.card_answers.remove(interaction);
}

pub(crate) fn note_answers(&mut self, interaction: &InteractionId, answers: &[FieldAnswer]) {
let Some(Interaction {
kind: InteractionKind::Question { form },
Expand Down
79 changes: 68 additions & 11 deletions cli/src/bridge/v2/work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,14 @@ struct Outgoing {
plan: bool,
}

/// What cancelling a request whose card Inline refused came to.
enum Cancelled {
Yes,
/// It ended (answered, interrupted) while its card was retried.
AlreadyEnded,
No,
}

/// An input the harness may or may not have.
#[derive(Clone, Copy, Debug)]
struct Uncertain {
Expand Down Expand Up @@ -1236,7 +1244,10 @@ impl Actor {
self.pick(message_id, actor, &i, f, label).await
}
ActionData::Decision { i, d } => {
let card = self.cards.get(&message_id).cloned()?;
let Some(card) = self.cards.get(&message_id).cloned() else {
// A card from before a restart: its request ended with the old session.
return Some(NOT_PENDING.to_owned());
};
if card.interaction.as_str() != i {
return Some(NOT_PENDING.to_owned());
}
Expand Down Expand Up @@ -3041,7 +3052,11 @@ impl Actor {
return;
}
crate::telemetry::report_bridge_output_refused(kind);
if !outgoing.fallback {
// Plain text keeps the buttons (an approval's, a single choice's) but
// not a form drawn as fields, which could then never be answered.
let unanswerable_as_text =
matches!(&outgoing.card, Some((_, Some(form))) if !render::pickable(form));
if !outgoing.fallback && !unanswerable_as_text {
let random_id = outgoing.random_id.wrapping_add(1).max(1);
self.outbox.push_front(Outgoing {
doc: plain_fallback(&outgoing.doc),
Expand All @@ -3051,16 +3066,58 @@ impl Actor {
});
return;
}
let what = if kind == "card" {
"a request for you"
} else {
"an answer"
let agent = self.agent.agent_name.clone();
let Some((interaction, _)) = outgoing.card else {
self.notice(&format!(
"{agent} had an answer Inline couldn't show. Ask {agent} to send it again, shorter."
))
.await;
return;
};
self.notice(&format!(
"{} had {what} Inline couldn't show. Continue in the terminal to see it.",
self.agent.agent_name
))
.await;
// Only a request this session still has open: one from before a
// restart, or that ended while the card was retried, needs nothing,
// and its ID could name another request of the new session.
if !self.renderer.is_open(&interaction) {
return;
}
// Its card was never shown, so closing it must not post one.
self.renderer.forget(&interaction);
// A request nobody can see can't be answered: cancel it, so the agent
// goes on instead of waiting for an answer that never comes. There is
// no terminal to answer it in.
let text = match self.cancel_unshowable(interaction).await {
Cancelled::Yes => format!(
"{agent} asked for something Inline couldn't show, so it was declined and {agent} goes on. Ask again in a simpler way if you need it."
),
Cancelled::AlreadyEnded => return,
Cancelled::No => format!(
"{agent} asked for something Inline couldn't show. Stop the turn with /stop to go on."
),
};
self.notice(&text).await;
}

/// Cancels a request whose card Inline refused, as the owner, the way a
/// released session cancels its open requests.
async fn cancel_unshowable(&mut self, interaction: InteractionId) -> Cancelled {
let Some(session) = self.held_session() else {
return Cancelled::No;
};
match session
.control(Op::Answer {
interaction,
response: InteractionResponse::Cancel,
by: UserId(self.agent.owner_user_id),
})
.await
{
Ok(_) => Cancelled::Yes,
Err(OpError::InteractionNotPending) => Cancelled::AlreadyEnded,
Err(error) => {
log::warn!(target: "inline::bridge::v2", "chat={} cannot cancel a refused request: {error:?}", self.chat_id);
Cancelled::No
}
}
}

async fn flush_outbox(&mut self) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
"form": {
"fields": [
{
"id": "How should the empty state look?",
"id": "q0",
"header": "Empty state",
"prompt": "How should the empty state look?",
"input": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
"form": {
"fields": [
{
"id": "Which package manager should the fix use?",
"id": "q0",
"header": "Package manager",
"prompt": "Which package manager should the fix use?",
"input": {
Expand Down Expand Up @@ -357,7 +357,7 @@
{
"name": "answer the question form",
"interaction": "req-ask-1",
"response": {"kind": "answers", "answers": [{"field": "Which package manager should the fix use?", "values": ["pnpm"]}]},
"response": {"kind": "answers", "answers": [{"field": "q0", "values": ["pnpm"]}]},
"native": {
"type": "control_response",
"response": {
Expand Down
Loading
Loading