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
120 changes: 96 additions & 24 deletions crates/fctl-control/src/api/autopilots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ pub async fn pause(ctx: &mut Ctx, req: &mut Request, id: &str) -> AppResult<Resp
/// subscription from now on when it ran on someone else's.
pub async fn resume(ctx: &mut Ctx, id: &str) -> AppResult<Response> {
if let Some(r) = resuming(ctx, id).await?
&& let Err(why) = resumed(ctx, &r, false).await?
&& let Err(why) = resumed(ctx, &r, With::Nothing).await?
{
return Err(conflict(why.message()));
}
Expand Down Expand Up @@ -1502,11 +1502,24 @@ pub async fn resuming(ctx: &Ctx, id: &str) -> AppResult<Option<Resuming>> {
}))
}

/// Resumes the autopilot [`resuming`] checked: it wakes at once, with
/// `message` for one a person's message to its own session resumed
/// (`SendMessageRequest::wake_autopilot`), which went to it first. Not
/// when it changed meanwhile, and why.
pub async fn resumed(ctx: &Ctx, r: &Resuming, message: bool) -> AppResult<Result<(), NotResumed>> {
/// What resumed the autopilot, besides the resume: what went to it first,
/// which its log and audit say.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum With {
/// A resume alone.
Nothing,
/// A person's message to its own session
/// (`SendMessageRequest::wake_autopilot`).
Message,
/// A person's answer to what it asked, or their no
/// (`AnswerAutopilotAskRequest::wake_autopilot`).
Answer,
}

/// Resumes the autopilot [`resuming`] checked: it wakes at once, after
/// what `with` says went to it first. Not when it changed meanwhile, and
/// why.
pub async fn resumed(ctx: &Ctx, r: &Resuming, with: With) -> AppResult<Result<(), NotResumed>> {
let Resuming {
p,
row,
Expand Down Expand Up @@ -1544,10 +1557,11 @@ pub async fn resumed(ctx: &Ctx, r: &Resuming, message: bool) -> AppResult<Result
},
));
}
let with = if message {
" with a message to its session"
} else {
""
let (message, answer) = (with == With::Message, with == With::Answer);
let with = match with {
With::Nothing => "",
With::Message => " with a message to its session",
With::Answer => " with their answer",
};
engine::log(
&ctx.db,
Expand Down Expand Up @@ -1576,7 +1590,7 @@ pub async fn resumed(ctx: &Ctx, r: &Resuming, message: bool) -> AppResult<Result
Some(p.workspace_id()),
"autopilot.resumed",
Some(&row.project_id),
Some(serde_json::json!({ "taken_over": take_over, "message": message })),
Some(serde_json::json!({ "taken_over": take_over, "message": message, "answer": answer })),
);
crate::schedules::wake(&ctx.env).await;
Ok(Ok(()))
Expand Down Expand Up @@ -2045,7 +2059,9 @@ pub async fn remove_note(ctx: &mut Ctx, id: &str, note: &str) -> AppResult<Respo
/// the answer as its text, before the answer is theirs: when they can't,
/// the ask stays open. For a question about no session, the autopilot's
/// own session that asked keeps them, without a message: it hears of them
/// with the answer, and downloads them from there.
/// with the answer, and downloads them from there. While it's paused, it
/// hears the answer once resumed, or at once with `wake_autopilot`, which
/// resumes it ([`waking`]).
pub async fn answer(ctx: &mut Ctx, req: &mut Request, id: &str, ask: &str) -> AppResult<Response> {
let body: AnswerAutopilotAskRequest = http::body(req).await?;
let l = load(ctx, id).await?;
Expand All @@ -2066,6 +2082,7 @@ pub async fn answer(ctx: &mut Ctx, req: &mut Request, id: &str, ask: &str) -> Ap
}
let me = ctx.user()?.clone();
let files = fctl_domain::validate::attachment_ids(&body.attachments)?;
let waking = waking(ctx, id, &row, body.wake_autopilot).await?;
// A secret, and where it goes; else the answer.
let (answer, secret) = match (found.kind(), &found.secret_name, &found.secret_project_id) {
(AutopilotAskKind::Secret, Some(name), Some(project)) => {
Expand Down Expand Up @@ -2191,7 +2208,71 @@ pub async fn answer(ctx: &mut Ctx, req: &mut Request, id: &str, ask: &str) -> Ap
.about(about.as_deref());
entry.other_project = elsewhere.as_deref();
engine::log(&ctx.db, &row.project_id, entry, ctx.now).await;
if row.state() == AutopilotState::On {
heard(ctx, id, &row, waking).await
}

/// The resume an answer to an ask, or a no, asks for with
/// `wake_autopilot` while the autopilot is paused: checked before anything
/// goes, as `POST …/autopilot/resume` checks it, so that an answer it
/// refuses stays unsent. `None` when none was asked, or it is on already
/// and hears the answer as it is.
async fn waking(
ctx: &Ctx,
id: &str,
row: &AutopilotRow,
wake: Option<bool>,
) -> AppResult<Option<Resuming>> {
if wake != Some(true) {
return Ok(None);
}
if row.state() == AutopilotState::Off {
return Err(conflict(
"This project is off autopilot: answer without waking it, or put the project on autopilot again.",
));
}
let Some(r) = resuming(ctx, id).await? else {
return Ok(None);
};
// One that couldn't run would pause again at its wake, the answer
// unheard.
r.runs_on(ctx).await?.map_err(|why| conflict(&why))?;
Ok(Some(r))
}

/// After an answer or a no went: resumes the paused autopilot when
/// `waking`, then wakes it with what people answered while it's on, and
/// answers with it as it now stands. The answer went whatever happens
/// here: a failed resume is no refusal, which people would answer again
/// (they can't: it's closed), so the autopilot comes back paused, and
/// people resume it.
async fn heard(
ctx: &mut Ctx,
id: &str,
row: &AutopilotRow,
waking: Option<Resuming>,
) -> AppResult<Response> {
let mut on = row.state() == AutopilotState::On;
if let Some(r) = &waking {
match resumed(ctx, r, With::Answer).await {
Ok(Ok(())) => on = true,
// Someone resumed it meanwhile, which the nudge finds on, or
// took it off.
Ok(Err(NotResumed::NotPaused)) => on = true,
Ok(Err(why)) => worker::console_log!(
"autopilot {}: not resumed with an answer: {}",
row.project_id,
why.message()
),
Err(e) => worker::console_error!(
"autopilot {}: resuming it with an answer: {}",
row.project_id,
e.0.message
),
}
}
// Only one that is on moves: the resume woke it at once, and the
// reason joins it.
if on {
engine::nudge(
&ctx.env,
&ctx.db,
Expand Down Expand Up @@ -2380,6 +2461,7 @@ pub async fn decline(ctx: &mut Ctx, req: &mut Request, id: &str, ask: &str) -> A
require_steer(&l)?;
let reason = ap::reason(body.reason.as_deref()).map_err(AppError)?;
let me = ctx.user()?.clone();
let waking = waking(ctx, id, &row, body.wake_autopilot).await?;
let done = q(
&ctx.db,
"UPDATE autopilot_asks SET status = 'declined', answer = ?, answered_by = ?, answered_at = ? WHERE id = ? \
Expand Down Expand Up @@ -2407,17 +2489,7 @@ pub async fn decline(ctx: &mut Ctx, req: &mut Request, id: &str, ask: &str) -> A
ctx.now,
)
.await;
if row.state() == AutopilotState::On {
engine::nudge(
&ctx.env,
&ctx.db,
&row.project_id,
AutopilotWakeReason::Answered,
ctx.now + ap::PEOPLE_GATHER_MS,
)
.await?;
}
get(ctx, id).await
heard(ctx, id, &row, waking).await
}

/// At most a line of what someone wrote, for the log.
Expand Down
41 changes: 23 additions & 18 deletions crates/fctl-control/src/api/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,25 +697,30 @@ pub async fn send_message(ctx: &mut Ctx, req: &mut Request, id: &str) -> AppResu
// here: a failure is no refusal, which people would send again.
let (woke_autopilot, not_woken) = match &waking {
None => (false, None),
Some((r, _)) => match crate::api::autopilots::resumed(ctx, r, true).await {
Ok(Ok(())) => (true, None),
// Resumed, or taken off, by someone else meanwhile: the
// message went as it is.
Ok(Err(crate::api::autopilots::NotResumed::NotPaused)) => (false, None),
Ok(Err(why)) => (false, Some(why.message().to_owned())),
Err(e) => {
worker::console_error!(
"session {id}: resuming its autopilot after a message: {}",
e.0.message
);
(
false,
Some(
"Resuming it failed: resume it on its project's Autopilot tab.".to_owned(),
),
)
Some((r, _)) => {
match crate::api::autopilots::resumed(ctx, r, crate::api::autopilots::With::Message)
.await
{
Ok(Ok(())) => (true, None),
// Resumed, or taken off, by someone else meanwhile: the
// message went as it is.
Ok(Err(crate::api::autopilots::NotResumed::NotPaused)) => (false, None),
Ok(Err(why)) => (false, Some(why.message().to_owned())),
Err(e) => {
worker::console_error!(
"session {id}: resuming its autopilot after a message: {}",
e.0.message
);
(
false,
Some(
"Resuming it failed: resume it on its project's Autopilot tab."
.to_owned(),
),
)
}
}
},
}
};
http::ok(&SendMessageResponse {
delivery_id,
Expand Down
2 changes: 1 addition & 1 deletion crates/fctl-domain/src/autopilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ pub fn briefing(place: Place<'_>, rules: Rules<'_>, direction: &str) -> String {
"- fctl wakes you at least every {}, and sooner when something needs you. Each wake says the time (UTC): later in a turn, run `date` before you write a time rather than work it out. Before you end your turn, say when to wake you if not then: `fctl autopilot wake --in 30m` (from 5 minutes to 24 hours), say when a session should be done, or in an hour or two while you wait only for people.\n",
duration(rules.check_in_minutes)
));
s.push_str("- When the direction is done and nothing is left to do, write it down and pause yourself with `fctl autopilot pause --reason \"…\"`, the reason saying what you'd need to go on, if anything: people read it as they write to you, and resume you when they have more, often with a message to this session, which then comes before fctl's wake.\n");
s.push_str("- When the direction is done and nothing is left to do, write it down and pause yourself with `fctl autopilot pause --reason \"…\"`, the reason saying what you'd need to go on, if anything: people read it as they write to you, and resume you when they have more, often with a message to this session, which then comes before fctl's wake, or with their answer to what you asked.\n");
s.push_str("\n## Getting good work from your sessions\n\n");
for line in work_lines(rules.powers) {
s.push_str(&format!("- {line}\n"));
Expand Down
11 changes: 11 additions & 0 deletions crates/fctl-proto/src/autopilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,14 @@ pub struct AnswerAutopilotAskRequest {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[cfg_attr(feature = "ts", ts(as = "Option<Vec<crate::AttachmentId>>", optional))]
pub attachments: Vec<crate::AttachmentId>,
/// While the autopilot is paused: resume it with the answer, for those
/// who steer it, as `POST …/autopilot/resume` does (on the caller's
/// subscription from then on, unless its owner keeps it). Checked
/// before anything goes; the answer goes first, and the autopilot
/// wakes with it. Without it, a paused autopilot hears the answer once
/// someone resumes it. One that is on hears it as it is. Control planes
/// from before it ignore it.
pub wake_autopilot: Option<bool>,
}

/// `POST …/autopilot/asks/{ask}/decline`.
Expand All @@ -1001,6 +1009,9 @@ pub struct AnswerAutopilotAskRequest {
#[cfg_attr(feature = "ts", derive(ts_rs::TS), ts(optional_fields))]
pub struct DeclineAutopilotAskRequest {
pub reason: Option<String>,
/// While the autopilot is paused: resume it as it hears the no, as
/// `AnswerAutopilotAskRequest::wake_autopilot` does with an answer.
pub wake_autopilot: Option<bool>,
}

/// `POST …/autopilot/log`: the autopilot writes down what it did, decided
Expand Down
Loading
Loading