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
140 changes: 111 additions & 29 deletions crates/fctl-control/src/do_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ const HOOKS_READY_AT: &str = "hooks_ready_at";
/// its webhooks (`FINISHED_SETTLE_MS` on), unless it works again first.
const HOOKS_FINISHED_AT: &str = "hooks_finished_at";

/// The pull requests the session lists that D1 may not record as its yet
/// (`fctl_domain::pulls::Unrecorded`, JSON), which its alarm records
/// ([`SessionObject::record_pull_requests`]).
const UNRECORDED: &str = "unrecorded_pull_requests";

/// Whether `s` is one of the sessions an autopilot started.
fn crew(s: &Session) -> bool {
s.autopilot
Expand Down Expand Up @@ -630,6 +635,7 @@ impl DurableObject for SessionObject {
self.write_through().await;
}
self.flush_follow_ups(now_ms()).await;
self.record_pull_requests(now_ms()).await;
self.ask_again(now_ms()).await;
self.index_now(now_ms()).await;
self.expire_images(now_ms()).await;
Expand Down Expand Up @@ -3242,7 +3248,7 @@ impl SessionObject {
self.notify_fleet(&s);
}
if !found.is_empty() {
self.track_pull_requests(&s, found);
self.track_pull_requests(&s, found, now).await;
}
let merged_more = merged(&s.pull_requests).len() > merged_before;
self.want_done(&s, progress_before, merged_more).await;
Expand Down Expand Up @@ -3588,37 +3594,113 @@ impl SessionObject {
}

/// Records the pull requests the session lists from now on, so that
/// GitHub's webhooks about them reach it, passes on the findings their
/// reviews kept since they went to no session (`crate::reviews::listed`),
/// and looks them up where the instance takes the webhook
/// (`crate::pull_requests`), which shows where those findings went.
fn track_pull_requests(&self, s: &Session, prs: Vec<PullRequestRef>) {
let env = self.env.clone();
/// GitHub's webhooks about them, their reviews' findings and follow-ups
/// reach it: its alarm does, at once, and again until it has
/// ([`Self::record_pull_requests`]). Linear hears of them too, where
/// the session answers it.
async fn track_pull_requests(&self, s: &Session, prs: Vec<PullRequestRef>, now: i64) {
if Self::answers_linear(s) {
let env = self.env.clone();
let id = s.id.as_str().to_owned();
let prs = prs.clone();
self.state.wait_until(async move {
if let Err(e) = crate::delegations::pull_requests(&env, &id, &prs).await {
worker::console_error!(
"telling Linear of the pull requests of {id}: {}",
e.0.message
);
}
});
}
let mut unrecorded = self.unrecorded().unwrap_or_default();
unrecorded.add(prs.into_iter().map(|p| p.url), now);
self.save_unrecorded(&unrecorded);
self.arm(now).await;
}

/// The pull requests the session lists that D1 may not record yet.
fn unrecorded(&self) -> Option<fctl_domain::pulls::Unrecorded> {
serde_json::from_str(&self.meta(UNRECORDED)?).ok()
}

fn save_unrecorded(&self, u: &fctl_domain::pulls::Unrecorded) {
if let Ok(text) = serde_json::to_string(u) {
self.set_meta(UNRECORDED, &text);
}
}

/// Records the pull requests the session lists that D1 may not record
/// as its yet ([`UNRECORDED`]), once their time came: those its tokens
/// reach (`crate::pull_requests::track`), and passes on the findings
/// their reviews kept since they went to no session
/// (`crate::reviews::listed`). A try counts once both are done. Then
/// it looks them up where the instance takes the webhook
/// (`crate::pull_requests::refresh`), which shows where those findings
/// went. Sets the alarm for the next try first: a try can fail, or a
/// deploy cut it short. After a day of tries, the hourly job records
/// them (`crate::pull_requests::heal`).
async fn record_pull_requests(&self, now: i64) {
let Some(mut unrecorded) = self.unrecorded() else {
return;
};
if unrecorded.due > now {
self.arm(unrecorded.due).await;
return;
}
let Some(s) = self.summary() else { return };
let id = s.id.as_str().to_owned();
let linear = Self::answers_linear(s);
// Review agents' sessions take no findings.
let reviewer = s.review.is_some();
self.state.wait_until(async move {
if linear && let Err(e) = crate::delegations::pull_requests(&env, &id, &prs).await {
worker::console_error!(
"telling Linear of the pull requests of {id}: {}",
e.0.message
);
let tried = unrecorded.urls.clone();
let prs: Vec<PullRequestRef> = s
.pull_requests
.iter()
.filter(|p| tried.iter().any(|u| u.eq_ignore_ascii_case(&p.url)))
.cloned()
.collect();
if unrecorded.trying(now) {
self.save_unrecorded(&unrecorded);
self.arm(unrecorded.due).await;
} else {
worker::console_error!(
"leaving the pull requests of {id} to the hourly job: recording them failed for a day"
);
self.delete_meta(UNRECORDED);
}
let Ok(db) = self.env.d1("DB") else { return };
let tracked = match crate::pull_requests::track(&db, &id, &prs).await {
Ok(t) => t,
Err(e) => {
worker::console_error!("recording the pull requests of {id}: {}", e.0.message);
return;
}
let Ok(db) = env.d1("DB") else { return };
let tracked = match crate::pull_requests::track(&db, &id, &prs).await {
Ok(t) => t,
Err(e) => {
worker::console_error!("tracking the pull requests of {id}: {}", e.0.message);
return;
}
};
if !reviewer && let Err(e) = crate::reviews::listed(&env, &db, &id, &tracked).await {
worker::console_error!(
"passing on the unsent review findings of the pull requests of {id}: {}",
e.0.message
);
};
// Their reviews' findings before they count as recorded: once
// their rows are in D1, nothing else passes those on. Review
// agents' sessions take none.
if s.review.is_none()
&& let Err(e) = crate::reviews::listed(&self.env, &db, &id, &tracked).await
Comment thread
fctl-review[bot] marked this conversation as resolved.
Resolved
{
worker::console_error!(
"passing on the unsent review findings of the pull requests of {id}: {}",
e.0.message
);
return;
}
// Those it found meanwhile wait for their own try.
Comment thread
fctl-review[bot] marked this conversation as resolved.
Resolved
if let Some(mut left) = self.unrecorded() {
if left.recorded(&tried) {
self.delete_meta(UNRECORDED);
} else {
self.save_unrecorded(&left);
}
}
if tracked.is_empty() {
return;
}
// A lookup that fails leaves them to the hourly job, which looks up
// first those nobody has.
let env = self.env.clone();
self.state.wait_until(async move {
let Ok(db) = env.d1("DB") else { return };
let cfg = crate::config::Config::from_env(&env);
if crate::pull_requests::enabled(&cfg)
&& let Err(e) =
Expand Down
Loading
Loading