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
72 changes: 49 additions & 23 deletions crates/cyber-cli/src/commands/exec/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use crate::error::CliError;
const LEASE_RENEWAL: Duration = Duration::from_secs(15);
/// A fatal protocol error.
const EXIT_PROTOCOL: u8 = 2;
/// At most this many owned requests wait for a reply at once (two per pending input);
/// a further one is declined at once with a `capacity` error line.
const MAX_ASKS: usize = 64;

/// What the stdin task sends.
enum Line {
Expand Down Expand Up @@ -116,7 +119,9 @@ pub async fn execute(
.map_err(api)?,
);
let hooks = HookLines::start(client, args, &id, &out).await;
let (sender, mut lines) = mpsc::channel(64);
// At most this many parsed lines (each at most 1 MiB) wait for the control loop;
// a writer that runs further ahead blocks on its stdin pipe.
let (sender, mut lines) = mpsc::channel(16);
tokio::spawn(read_stdin(sender));
let mut driver = Driver::new(client, &id, args, &mut out, run);
driver.hooks = hooks;
Expand Down Expand Up @@ -465,19 +470,25 @@ impl<'a> Driver<'a> {
self.release().await;
let asks = std::mem::take(&mut self.asks);
for (request_id, ask) in asks {
let (route, body) = if ask.question {
("questions", json!({ "answers": null }))
} else {
(
"permissions",
json!({ "reply": "reject", "message": NO_APPROVER }),
)
};
let path = format!("/sessions/{}/{route}/{request_id}/reply", ask.session_id);
let _ = self.client.post(&path, body).await;
self.decline(&request_id, &ask).await;
}
}

/// Decline a request nobody here will answer: `reject` a permission, dismiss a
/// question.
async fn decline(&self, request_id: &str, ask: &Ask) {
let (route, body) = if ask.question {
("questions", json!({ "answers": null }))
} else {
(
"permissions",
json!({ "reply": "reject", "message": NO_APPROVER }),
)
};
let path = format!("/sessions/{}/{route}/{request_id}/reply", ask.session_id);
let _ = self.client.post(&path, body).await;
}

async fn event(&mut self, event: Event) {
// Receipt summaries (opt-in) are live and only ever for this Session.
self.hooks.live(&event, self.out);
Expand Down Expand Up @@ -614,7 +625,7 @@ impl<'a> Driver<'a> {
}
}
self.stage_answers();
self.step = None;
self.set_step(None);
self.refresh_background().await;
}

Expand Down Expand Up @@ -671,7 +682,10 @@ impl<'a> Driver<'a> {
/// A failed Turn fails the inputs it was answering.
fn failed(&mut self, data: &Value) {
let message = data["message"].as_str().unwrap_or("error").to_owned();
self.out.error(&message);
self.out.stream_line(
"error",
json!({ "message": message, "input_message_ids": self.answering }),
);
for message_id in &self.answering {
if let Some(input) = self.inputs.get_mut(message_id) {
input.error.get_or_insert_with(|| message.clone());
Expand All @@ -693,14 +707,20 @@ impl<'a> Driver<'a> {
if !safe_id(request_id, prefix) || !safe_id(session_id, "ses") {
return;
}
self.asks.insert(
request_id.to_owned(),
Ask {
session_id: session_id.to_owned(),
question,
inputs,
},
);
let ask = Ask {
session_id: session_id.to_owned(),
question,
inputs,
};
if self.asks.len() >= MAX_ASKS && !self.asks.contains_key(request_id) {
self.out.stream_line(
"error",
json!({ "code": "capacity", "request_id": request_id,
"message": format!("more than {MAX_ASKS} requests wait; {request_id} was declined") }),
);
return self.decline(request_id, &ask).await;
}
self.asks.insert(request_id.to_owned(), ask);
let line = if question {
"question"
} else {
Expand Down Expand Up @@ -759,6 +779,12 @@ impl<'a> Driver<'a> {
|| (self.closed && self.inputs.is_empty() && self.interruptions.is_empty())
}

/// The driven step being followed, whose lines carry its input cohort.
fn set_step(&mut self, step: Option<(&str, &[String])>) {
self.step = step.map(|(message, _)| message.to_owned());
self.out.correlate(step.map(|(_, roots)| roots));
}

fn owns_step(&self, data: &Value) -> bool {
self.step
.as_deref()
Expand All @@ -770,7 +796,7 @@ impl<'a> Driver<'a> {
if self.answer_done {
self.stage_answers();
}
self.step = None;
self.set_step(None);
let Some(message) = data["message_id"].as_str().filter(|id| safe_id(id, "msg")) else {
return;
};
Expand All @@ -792,7 +818,7 @@ impl<'a> Driver<'a> {
if !self.inputs.owns_cohort(&roots) {
return;
}
self.step = Some(message.to_owned());
self.set_step(Some((message, &roots)));
self.answer_done = false;
self.run.text.clear();
for root in roots {
Expand Down
2 changes: 1 addition & 1 deletion crates/cyber-cli/src/commands/exec/driver/interruption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Driver<'_> {
}
self.answering.retain(|id| self.inputs.contains(id));
if self.answering.is_empty() {
self.step = None;
self.set_step(None);
self.run.retire_driver_answer();
self.answer_done = false;
}
Expand Down
47 changes: 47 additions & 0 deletions crates/cyber-cli/src/commands/exec/driver/request_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,50 @@ async fn a_request_answered_elsewhere_leaves_the_drivers_asks() {
driver.close().await;
f.app.runtime.shutdown().await;
}

/// More owned requests than the bound: the next one is declined at once, so it
/// neither waits unseen nor grows the driver's tracked requests.
#[tokio::test]
async fn an_owned_request_beyond_the_bound_is_declined_at_once() {
let f = Fixture::new().await;
let args = ExecArgs::default();
let session = json!({"id":f.session});
let mut out = Out::new(Format::Text, &args, &session);
let mut driver = Driver::new(
&f.client,
&f.session,
&args,
&mut out,
Run::new(&args, Instant::now()),
);
driver.attach().await;
driver
.admit(
None,
vec![json!({"type":"text","text":"PERMISSION_CASE"})],
"queue",
)
.await;
let request = f.request().await;
for index in 0..MAX_ASKS {
driver.asks.insert(
format!("per_placeholder{index}"),
Ask {
session_id: f.session.clone(),
question: false,
inputs: request.input_message_ids.clone(),
},
);
}
let (base, data) = event(&request);
driver.asked(base, &data).await;
assert_eq!(driver.asks.len(), MAX_ASKS);
assert!(!driver.asks.contains_key(&request.id));
tokio::time::timeout(Duration::from_secs(30), f.app.runtime.wait_idle(&f.session))
.await
.expect("the declined request kept its Turn waiting");
assert!(f.app.runtime.pending_requests(Some(&f.session)).is_empty());
driver.asks.clear();
driver.close().await;
f.app.runtime.shutdown().await;
}
5 changes: 5 additions & 0 deletions crates/cyber-cli/src/commands/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ fn launch(
client
};
let mut out = Out::new(format, &args, &session);
let config = ctx.config().ok().map(|resolved| resolved.value);
out.redact_errors(report::error_redactor(
config.as_ref(),
args.password.as_deref(),
));
let code = match (review, &args.workflow) {
(_, Some(target)) => {
workflow::execute(&client, ctx, &session, &args, target, out).await
Expand Down
Loading
Loading