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
8 changes: 5 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ fake-claude's behaviors are listed in [docs/development.md](docs/development.md)
default sandbox image, `ghcr.io/hyperstructure/sandbox`, published for
every release. Hosts pull it, and `fctl host image build` builds its
`main`. The host relies on its layout (the `agent` user, the PATH,
`/opt/fctl/bin`) and on `Dockerfile` and `provision.sh` at its root:
change those there, together with `docs/sandbox-images.md` here. Its Rust
is the one in `rust-toolchain.toml`; bump the two together.
`/opt/fctl/bin`), on `Dockerfile` and `provision.sh` at its root, and on
the `org.opencontainers.image.revision` label that only its Image
workflow sets (hosts pull again over, and remove, only images that have
it): change those there, together with `docs/sandbox-images.md` here.
Its Rust is the one in `rust-toolchain.toml`; bump the two together.

design and fctl-site are private for now: `npm ci` reaches the design
package over git, so it needs GitHub access (CI uses a read-only deploy
Expand Down
2 changes: 1 addition & 1 deletion crates/fctl-host/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ mod tests {

use crate::driver::Driver;
use crate::driver::container::Flavor;
use crate::driver::container::fake::FakeCli;
use crate::driver::fake::FakeCli;
use crate::session::HostCtx;
use std::sync::{Arc, Mutex, PoisonError};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
Expand Down
54 changes: 29 additions & 25 deletions crates/fctl-host/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::config::HostConfig;
use crate::driver::{Driver, SandboxInfo, sandbox_name, session_of, timed_out};
use crate::expiry;
use crate::login::{self, LoginCmd};
use crate::refresh;
use crate::session::{self, HostCtx, SessionCmd, SessionEvent, SessionHandle, Start};
use crate::sysinfo;
use anyhow::Result;
Expand Down Expand Up @@ -116,34 +117,23 @@ impl Daemon {
})
}

/// Pulls the host's sandbox image in the background when it isn't here
/// yet, so that the first session after an upgrade doesn't wait for it.
/// When it can't be pulled, as before a release's image is published,
/// it pulls the image sessions start from instead.
fn prefetch_image(&self) {
/// Pulls the host's sandbox image in the background, now and every
/// [`refresh::REFRESH_EVERY`]: when it isn't here yet, so that the first
/// session after an upgrade doesn't wait for it, and on the default
/// image again, so that new sessions start from its newest build. Then
/// it removes the images sessions no longer start from.
fn refresh_image(&self) {
if !self.ctx.driver.uses_images() {
return;
}
let ctx = self.ctx.clone();
tokio::spawn(async move {
let image = ctx.cfg.image();
if ctx.driver.has_image(&image).await {
return;
}
// Never over a fallback that is here: sessions start from it.
let fallback = match ctx.cfg.fallback_image() {
Some(f) if !ctx.driver.has_image(&f).await => Some(f),
_ => None,
};
tracing::info!("pulling {image}");
let log = |line: String| tracing::info!("{line}");
match ctx
.driver
.pull_or_fallback(&image, fallback.as_deref(), false, &log)
.await
{
Ok(pulled) => tracing::info!("pulled {pulled}"),
Err(e) => tracing::warn!("pulling {image}: {e:#} (sessions try again)"),
let mut every = tokio::time::interval(refresh::REFRESH_EVERY);
every.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
every.tick().await;
refresh::refresh(&ctx.driver, &ctx.cfg).await;
clean_up_images(&ctx).await;
}
});
}
Expand Down Expand Up @@ -253,7 +243,12 @@ impl Daemon {
}
self.release(&name);
}
Chore::Swept => self.sweeping = false,
Chore::Swept => {
self.sweeping = false;
// Images that only the removed sandboxes used can go too.
let ctx = self.ctx.clone();
tokio::spawn(async move { clean_up_images(&ctx).await });
}
Chore::Stopped { name, ended } => {
self.outbox.push_back(ended);
self.release(&name);
Expand Down Expand Up @@ -360,7 +355,7 @@ impl Daemon {
wait = (wait * 2).min(Duration::from_secs(60));
}
// Once the runtime answers.
self.prefetch_image();
self.refresh_image();
self.start_sweep();
refresh_facts_every(self.ctx.clone(), FACTS_EVERY);
let url = format!(
Expand Down Expand Up @@ -658,6 +653,15 @@ impl Daemon {
}
}

/// Removes the sandbox images that sessions no longer start from, saying
/// which in the log.
async fn clean_up_images(ctx: &HostCtx) {
for name in refresh::clean_up(&ctx.driver, &ctx.cfg).await {
let what = refresh::describe(&name);
tracing::info!("removed {what}: sessions no longer start from it");
}
}

/// Probes the drivers and measures what sandboxes run on every `period`, in
/// the background.
fn refresh_facts_every(ctx: Arc<HostCtx>, period: Duration) {
Expand Down
Loading
Loading