//! In-process integration tests: mock OpenRouter + mock runner (real temp
//! git repos, real bundles) + the real agentd HTTP server + scheduler.
//!
//! Covers: full success flow (read → edit → build → finish, branch pushed
//! to the temp remote, token absent from logs/events), unauthenticated POST
//! rejection, wrong-Origin rejection, and release-signal build preemption.
#[cfg(test)]
mod integration {
use std::collections::{HashMap, VecDeque};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::{Arc, Mutex};
use axum::extract::{Path as AxPath, State};
use axum::http::{header, HeaderMap, StatusCode};
use axum::response::IntoResponse;
use axum::routing::{get, post};
use tokio::process::Command as TokioCommand;
use crate::state::AppState;
use crate::store::Store;
// ---------- tiny git helper (argv only, no shell) ----------
async fn git(dir: &Path, args: &[&str]) -> String {
let out = TokioCommand::new("git")
.arg("-C")
.arg(dir)
.args(args)
.env("GIT_PAGER", "cat")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.stdin(Stdio::null())
.output()
.await
.expect("spawn git");
assert!(
out.status.success(),
"git {} failed: {}",
args.join(" "),
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8(out.stdout).unwrap()
}
async fn git_no_cwd(args: &[&str], workdir: &Path) -> String {
let out = TokioCommand::new("git")
.args(args)
.current_dir(workdir)
.env("GIT_PAGER", "cat")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.stdin(Stdio::null())
.output()
.await
.expect("spawn git");
assert!(
out.status.success(),
"git {} failed: {}",
args.join(" "),
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8(out.stdout).unwrap()
}
/// Build a temp bare "remote" with one commit (`main` + AGENTS.md files).
async fn init_remote(tmp: &Path) -> PathBuf {
let remote = tmp.join("remote.git");
git_no_cwd(
&[
"init",
"--bare",
"-b",
"main",
remote.to_str().unwrap(),
],
tmp,
)
.await;
let work = tmp.join("seed");
git_no_cwd(
&[
"clone",
remote.to_str().unwrap(),
work.to_str().unwrap(),
],
tmp,
)
.await;
git_no_cwd(&["config", "user.email", "t@t"], &work).await;
git_no_cwd(&["config", "user.name", "t"], &work).await;
std::fs::create_dir_all(work.join("notes")).unwrap();
std::fs::write(work.join("AGENTS.md"), "ROOT RULES FOR TEST\n").unwrap();
std::fs::write(work.join("notes/AGENTS.md"), "NOTES MODULE RULES\n").unwrap();
std::fs::write(work.join("notes/file.txt"), "hello\n").unwrap();
// A committed JUnit report with one failure, so `run_tests` has XML
// to parse in every slot checkout. Committed → never pollutes diffs.
let xml_dir = work.join("notes/build/test-results/testDevUnitTest");
std::fs::create_dir_all(&xml_dir).unwrap();
std::fs::write(
xml_dir.join("TEST-com.example.GreetingTest.xml"),
r#"
AssertionFailedError: expected:<HELLO> but was:<hello>
at com.example.GreetingTest.shoutsGreeting(GreetingTest.kt:42)
"#,
)
.unwrap();
git_no_cwd(&["add", "-A"], &work).await;
git_no_cwd(&["commit", "-m", "seed commit"], &work).await;
git_no_cwd(&["push", "origin", "main"], &work).await;
remote
}
// ---------- mock OpenRouter (scripted responses) ----------
#[derive(Clone)]
struct MockOr {
script: Arc>>,
seen: Arc>>,
}
async fn mock_or_chat(
State(st): State,
headers: HeaderMap,
body: axum::Json,
) -> impl IntoResponse {
// `usage: {include: true}` must be requested.
let usage_ok = body.get("usage").and_then(|u| u.get("include")).and_then(|v| v.as_bool()).unwrap_or(false);
assert!(usage_ok, "agentd must send usage:{{include:true}}");
assert!(
headers.contains_key(header::AUTHORIZATION),
"auth header present"
);
st.seen.lock().unwrap().push(body.0.clone());
let next = st.script.lock().unwrap().pop_front().unwrap_or_else(|| {
serde_json::json!({
"choices": [{"message": {"role": "assistant", "content": "stuck"}}],
"usage": {"prompt_tokens": 1, "completion_tokens": 1}
})
});
axum::Json(next)
}
fn tool_resp(id: &str, name: &str, args: serde_json::Value, text: &str) -> serde_json::Value {
serde_json::json!({
"choices": [{"message": {
"role": "assistant", "content": text,
"tool_calls": [{
"id": id, "type": "function",
"function": {"name": name, "arguments": serde_json::to_string(&args).unwrap()}
}]
}}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "cost": 0.0001}
})
}
fn success_script() -> Vec {
vec![
tool_resp("c1", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
tool_resp(
"c2",
"edit_file",
serde_json::json!({"path": "notes/file.txt", "old": "hello\n", "new": "hello world\n"}),
"editing",
),
tool_resp(
"c3",
"build",
serde_json::json!({"command": "./gradlew :notes:compileDevKotlin"}),
"building",
),
tool_resp(
"c4",
"finish",
serde_json::json!({
"summary": "Changed the greeting.",
"commit_message": "notes: change greeting\n\nSwitched the greeting text per the task."
}),
"finishing",
),
]
}
async fn serve_mock_or(script: Vec) -> (String, MockOr) {
let st = MockOr {
script: Arc::new(Mutex::new(script.into())),
seen: Arc::new(Mutex::new(Vec::new())),
};
let app = axum::Router::new()
.route("/chat/completions", post(mock_or_chat))
.with_state(st.clone());
let lis = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = lis.local_addr().unwrap();
tokio::spawn(async move { axum::serve(lis, app).await.unwrap() });
(format!("http://{addr}"), st)
}
// ---------- mock runner (real git checkout per slot) ----------
/// One canned exec response: commands containing `pattern` get
/// (`exit_code`, `stdout`, `stderr`).
type CannedExec = (String, i32, String, String);
struct SlotState {
dir: PathBuf,
branch: String,
base_sha: String,
}
#[derive(Clone)]
struct MockRunner {
slots: Arc>>,
/// Repo slots clone from (agentd's mirror in the harness, so WIP refs
/// imported by agentd are fetchable) and record of every prepare as
/// `(slot, branch)` — resume may migrate a task to another slot.
clone_source: PathBuf,
prepares: Arc>>,
build_seen: Arc>>,
build_should_preempt: bool,
/// Canned exec responses, first-match wins: `(substring, exit_code,
/// stdout, stderr)`. Checked before the default success response so
/// tests can simulate build/test failures per command pattern.
canned_exec: Arc>>,
/// Unique per-harness scratch dir. Slot checkouts live under it so
/// parallel tests (same process, distinct harnesses) never share a
/// slot directory. (An earlier revision used a fixed
/// `$TMPDIR/mockslot-{pid}-{slot}` path; two harnesses preparing the
/// same slot concurrently deleted each other's checkout mid-task.)
scratch: PathBuf,
}
impl MockRunner {
/// Add a canned exec response for commands containing `pattern`.
fn canned(&self, pattern: &str, exit_code: i32, stdout: &str, stderr: &str) {
self.canned_exec.lock().unwrap().push((
pattern.to_string(),
exit_code,
stdout.to_string(),
stderr.to_string(),
));
}
}
#[allow(dead_code)]
fn parse_slot(path: &str, suffix: &str) -> Option {
// "/v1/slots/{slot}/{suffix}"
let rest = path.strip_prefix("/v1/slots/")?;
let (slot, tail) = rest.split_once('/')?;
if !tail.starts_with(suffix) {
return None;
}
slot.parse().ok()
}
async fn mock_prepare(
State(st): State,
AxPath(slot): AxPath,
body: axum::Json,
) -> impl IntoResponse {
let branch = body.get("branch").and_then(|v| v.as_str()).unwrap_or("").to_string();
assert!(branch.starts_with("agent/"), "branch prefix");
// Record which slot each prepare lands on (resume may migrate slots).
st.prepares.lock().unwrap().push((slot, branch.clone()));
// Per-harness scratch dir: parallel tests (same process, distinct
// harnesses) must never share a slot checkout. A fixed
// `$TMPDIR/mockslot-{pid}-{slot}` path let two harnesses preparing
// the same slot concurrently delete each other's checkout mid-task.
let dir = st.scratch.join(format!("slot-{slot}"));
std::fs::create_dir_all(&st.scratch).unwrap();
let _ = std::fs::remove_dir_all(&dir);
git_no_cwd(
&["clone", st.clone_source.to_str().unwrap(), dir.to_str().unwrap()],
&st.scratch,
)
.await;
git(&dir, &["config", "user.email", "t@t"]).await;
git(&dir, &["config", "user.name", "t"]).await;
// Resume branch: fetch the WIP ref from the clone source (agentd's
// mirror) into the slot, check out the WIP sha, then reset --soft
// the base so the work reappears as uncommitted changes. Fresh
// prepare: new branch from main.
let base = if let Some(resume) = body.get("resume") {
let base_sha = resume.get("base_sha").and_then(|v| v.as_str()).unwrap_or("").to_string();
assert!(!base_sha.is_empty(), "resume needs base_sha");
if let (Some(wip_ref), Some(wip_sha)) = (
resume.get("wip_ref").and_then(|v| v.as_str()),
resume.get("wip_sha").and_then(|v| v.as_str()),
) {
// Explicit fetch of the exact ref (plain `fetch origin` would
// not pick up `refs/agent-wip/*`).
git(&dir, &["fetch", "origin", &format!("+{wip_ref}:{wip_ref}")]).await;
let got = git(&dir, &["rev-parse", wip_ref]).await;
assert_eq!(got.trim(), wip_sha, "WIP ref sha must match");
git(&dir, &["checkout", "-B", &branch, wip_sha]).await;
git(&dir, &["reset", "--soft", &base_sha]).await;
} else {
git(&dir, &["checkout", "-B", &branch, &base_sha]).await;
}
// `.git/agent-base` records the original base, like the runner.
std::fs::write(dir.join(".git/agent-base"), &base_sha).unwrap();
base_sha
} else {
git(&dir, &["checkout", "-b", &branch]).await;
let base = git(&dir, &["rev-parse", "HEAD"]).await;
base.trim().to_string()
};
st.slots.lock().unwrap().insert(
slot,
SlotState { dir, branch, base_sha: base.clone() },
);
(
StatusCode::OK,
axum::Json(serde_json::json!({"base_sha": base})),
)
}
async fn mock_read(
State(st): State,
AxPath(slot): AxPath,
body: axum::Json,
) -> impl IntoResponse {
let dir = st.slots.lock().unwrap().get(&slot).unwrap().dir.clone();
let rel = body.get("path").and_then(|v| v.as_str()).unwrap_or("");
let content = std::fs::read_to_string(dir.join(rel)).unwrap_or_else(|_| "MISSING".into());
let total = content.lines().count();
axum::Json(serde_json::json!({"content": content, "total_lines": total, "truncated": false}))
}
async fn mock_write(
State(st): State,
AxPath(slot): AxPath,
body: axum::Json,
) -> impl IntoResponse {
let dir = st.slots.lock().unwrap().get(&slot).unwrap().dir.clone();
let rel = body.get("path").and_then(|v| v.as_str()).unwrap_or("");
let content = body.get("content").and_then(|v| v.as_str()).unwrap_or("");
let p = dir.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, content).unwrap();
(StatusCode::OK, axum::Json(serde_json::json!({})))
}
async fn mock_edit(
State(st): State,
AxPath(slot): AxPath,
body: axum::Json,
) -> impl IntoResponse {
let dir = st.slots.lock().unwrap().get(&slot).unwrap().dir.clone();
let rel = body.get("path").and_then(|v| v.as_str()).unwrap_or("");
let old = body.get("old").and_then(|v| v.as_str()).unwrap_or("");
let new = body.get("new").and_then(|v| v.as_str()).unwrap_or("");
let p = dir.join(rel);
let content = std::fs::read_to_string(&p).unwrap();
if content.matches(old).count() != 1 {
return (
StatusCode::CONFLICT,
axum::Json(serde_json::json!({"error": "old must occur exactly once"})),
);
}
std::fs::write(p, content.replacen(old, new, 1)).unwrap();
(StatusCode::OK, axum::Json(serde_json::json!({})))
}
async fn mock_exec(
State(st): State,
AxPath(slot): AxPath,
body: axum::Json,
) -> impl IntoResponse {
let cmd = body.get("command").and_then(|v| v.as_str()).unwrap_or("").to_string();
let kind = body.get("kind").and_then(|v| v.as_str()).unwrap_or("").to_string();
st.build_seen.lock().unwrap().push(cmd.clone());
if st.build_should_preempt && kind == "build" {
// Simulate a release kill: killed=true like the real runner.
return axum::Json(serde_json::json!({
"exit_code": null, "stdout": "", "stderr": "killed",
"truncated": false, "timed_out": false, "killed": true,
"queued_ms": 0, "duration_ms": 5
}));
}
{
let canned = st.canned_exec.lock().unwrap().clone();
for (pattern, exit_code, stdout, stderr) in &canned {
if cmd.contains(pattern.as_str()) {
return axum::Json(serde_json::json!({
"exit_code": exit_code, "stdout": stdout, "stderr": stderr,
"truncated": false, "timed_out": false, "killed": false,
"queued_ms": 0, "duration_ms": 5
}));
}
}
}
if kind == "shell" {
// Bind the dir in its own statement: holding the std MutexGuard
// across the `.await` below would make the future !Send and break
// axum's Handler bound.
let dir = st.slots.lock().unwrap().get(&slot).map(|s| s.dir.clone());
if let Some(dir) = dir {
// Only allow the read-only commands agentd's structured tools
// build (defense in depth: the harness never runs arbitrary
// shell from these tests).
let allowed = cmd.starts_with("git log ")
|| cmd.starts_with("git show ")
|| cmd.starts_with("git blame ")
|| cmd.starts_with("rg --files ");
if allowed {
let out = TokioCommand::new("bash")
.arg("-lc")
.arg(&cmd)
.current_dir(&dir)
.env("GIT_PAGER", "cat")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.stdin(Stdio::null())
.output()
.await
.unwrap();
return axum::Json(serde_json::json!({
"exit_code": out.status.code().unwrap_or(1),
"stdout": String::from_utf8_lossy(&out.stdout),
"stderr": String::from_utf8_lossy(&out.stderr),
"truncated": false, "timed_out": false, "killed": false,
"queued_ms": 0, "duration_ms": 5
}));
}
}
}
// Fake build success; run `true`-equivalent by touching nothing.
axum::Json(serde_json::json!({
"exit_code": 0, "stdout": format!("ok: {cmd}"),
"stderr": "", "truncated": false, "timed_out": false,
"killed": false, "queued_ms": 0, "duration_ms": 5
}))
}
/// `list` backed by the real slot checkout (mirrors the runner: one
/// level, dirs end with `/`, sorted). Needed by `run_tests`, which lists
/// the JUnit XML dir.
async fn mock_list(
State(st): State,
AxPath(slot): AxPath,
body: axum::Json,
) -> impl IntoResponse {
let dir = st.slots.lock().unwrap().get(&slot).unwrap().dir.clone();
let rel = body.get("path").and_then(|v| v.as_str()).unwrap_or("");
let target = dir.join(rel);
let mut entries: Vec = Vec::new();
if let Ok(rd) = std::fs::read_dir(&target) {
for entry in rd.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
entries.push(format!("{name}/"));
} else {
entries.push(name);
}
}
}
entries.sort();
axum::Json(serde_json::json!({"entries": entries, "truncated": false}))
}
async fn mock_diff(
State(st): State,
AxPath(slot): AxPath,
_body: axum::Json,
) -> impl IntoResponse {
let dir = st.slots.lock().unwrap().get(&slot).unwrap().dir.clone();
git(&dir, &["add", "-A"]).await;
let diff = git(&dir, &["diff", "--cached"]).await;
let stat = git(&dir, &["diff", "--cached", "--stat"]).await;
axum::Json(serde_json::json!({"stat": stat, "diff": diff, "truncated": false}))
}
async fn mock_commit(
State(st): State,
AxPath(slot): AxPath,
body: axum::Json,
) -> impl IntoResponse {
let dir = st.slots.lock().unwrap().get(&slot).unwrap().dir.clone();
let msg = body.get("message").and_then(|v| v.as_str()).unwrap_or("").to_string();
git(&dir, &["add", "-A"]).await;
// Identity comes from the slot repo's local config (set at prepare),
// mirroring the real runner where the sandbox user has its own git
// identity — agentd never injects one.
let out = TokioCommand::new("git")
.arg("-C")
.arg(&dir)
.arg("commit")
.arg("-m")
.arg(&msg)
.env("GIT_PAGER", "cat")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.stdin(Stdio::null())
.output()
.await
.unwrap();
if !out.status.success() {
return (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(serde_json::json!({"error": format!("commit failed: {}", String::from_utf8_lossy(&out.stderr))})),
);
}
let sha = git(&dir, &["rev-parse", "HEAD"]).await;
(StatusCode::OK, axum::Json(serde_json::json!({"sha": sha.trim()})))
}
async fn mock_bundle(
State(st): State,
AxPath(slot): AxPath,
) -> impl IntoResponse {
let (dir, branch, base) = {
let slots = st.slots.lock().unwrap();
let s = slots.get(&slot).unwrap();
(s.dir.clone(), s.branch.clone(), s.base_sha.clone())
};
let out = TokioCommand::new("git")
.arg("-C")
.arg(&dir)
.arg("bundle")
.arg("create")
.arg("-")
.arg(format!("{base}..{branch}"))
.env("GIT_PAGER", "cat")
.stdin(Stdio::null())
.output()
.await
.unwrap();
assert!(out.status.success());
(
StatusCode::OK,
[(header::CONTENT_TYPE, "application/octet-stream")],
out.stdout,
)
}
async fn serve_mock_runner(clone_source: PathBuf, preempt: bool, scratch: PathBuf) -> (String, MockRunner) {
std::fs::create_dir_all(&scratch).unwrap();
let st = MockRunner {
slots: Arc::new(Mutex::new(Default::default())),
clone_source,
prepares: Arc::new(Mutex::new(Vec::new())),
build_seen: Arc::new(Mutex::new(Vec::new())),
build_should_preempt: preempt,
canned_exec: Arc::new(Mutex::new(Vec::new())),
scratch,
};
let app: axum::Router = axum::Router::new()
.route("/v1/slots/:slot/prepare", post(mock_prepare))
.route("/v1/slots/:slot/read", post(mock_read))
.route("/v1/slots/:slot/write", post(mock_write))
.route("/v1/slots/:slot/edit", post(mock_edit))
.route("/v1/slots/:slot/exec", post(mock_exec))
.route("/v1/slots/:slot/list", post(mock_list))
.route("/v1/slots/:slot/diff", post(mock_diff))
.route("/v1/slots/:slot/commit", post(mock_commit))
.route("/v1/slots/:slot/bundle", get(mock_bundle))
.route(
"/v1/slots/:slot/cancel",
post(|_: AxPath| async {
axum::Json(serde_json::json!({"killed": false}))
}),
)
.route(
"/v1/builds/kill",
post(|| async { axum::Json(serde_json::json!({"killed": true})) }),
)
.with_state(st.clone());
let lis = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = lis.local_addr().unwrap();
tokio::spawn(async move { axum::serve(lis, app).await.unwrap() });
(format!("http://{addr}"), st)
}
// ---------- mock GitHub (recorded fixtures + recorded POSTs) ----------
#[derive(Debug, Clone)]
struct RecordedGhRequest {
method: String,
path: String,
}
#[derive(Clone, Default)]
struct MockGitHub {
recorded: Arc>>,
responses: Arc>>,
}
/// A recorded fixture served at one mock-GitHub path: HTTP status + body.
type GhFixture = (u16, Vec);
impl MockGitHub {
/// Serve `body` at `path` with HTTP 200.
fn respond(&self, path: &str, body: impl Into>) {
self.responses
.lock()
.unwrap()
.insert(path.to_string(), (200, body.into()));
}
fn requests(&self) -> Vec {
self.recorded.lock().unwrap().clone()
}
}
async fn mock_github_handler(
State(st): State,
req: axum::http::Request,
) -> impl IntoResponse {
let (parts, _body) = req.into_parts();
st.recorded.lock().unwrap().push(RecordedGhRequest {
method: parts.method.to_string(),
path: parts.uri.path().to_string(),
});
// Write endpoints take precedence over recorded GET fixtures sharing
// the same path (e.g. the comments list).
let path = parts.uri.path().to_string();
if parts.method == axum::http::Method::POST && path.ends_with("/comments") {
return (
StatusCode::CREATED,
[(header::CONTENT_TYPE, "application/json")],
br#"{"html_url":"https://github.com/vayun-mathur/Modern-Apps/issues/903#issuecomment-9"}"#.to_vec(),
)
.into_response();
}
if parts.method == axum::http::Method::POST && path.ends_with("/labels") {
return (
StatusCode::OK,
[(header::CONTENT_TYPE, "application/json")],
br#"[{"name":"solved for next release"}]"#.to_vec(),
)
.into_response();
}
let found = st
.responses
.lock()
.unwrap()
.get(parts.uri.path())
.cloned();
match found {
Some((status, body)) => (
StatusCode::from_u16(status).unwrap_or(StatusCode::OK),
[(header::CONTENT_TYPE, "application/json")],
body,
)
.into_response(),
None => (StatusCode::NOT_FOUND, "no mock for path").into_response(),
}
}
async fn serve_mock_github() -> (String, MockGitHub) {
let st = MockGitHub::default();
// Recorded fixtures for issue #903 (weather/Celsius) + search.
st.respond(
"/repos/vayun-mathur/Modern-Apps/issues/903",
include_str!("testdata/github/issue_903.json"),
);
st.respond(
"/repos/vayun-mathur/Modern-Apps/issues/903/comments",
include_str!("testdata/github/comments_903.json"),
);
st.respond("/search/issues", include_str!("testdata/github/search_weather.json"));
st.respond(
"/repos/vayun-mathur/Modern-Apps/issues",
include_str!("testdata/github/list_weather.json"),
);
let app = axum::Router::new()
.fallback(mock_github_handler)
.with_state(st.clone());
let lis = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = lis.local_addr().unwrap();
tokio::spawn(async move { axum::serve(lis, app).await.unwrap() });
(format!("http://{addr}"), st)
}
// ---------- harness: real agentd server pointed at the mocks ----------
struct Harness {
addr: SocketAddr,
client: reqwest::Client,
origin: String,
_tmp: tempfile::TempDir,
_remote: PathBuf,
runner_seen: MockRunner,
or_seen: MockOr,
github_seen: MockGitHub,
cookie: String,
store: Store,
}
async fn spawn_harness(script: Vec) -> Harness {
spawn_harness_opts(script, false).await
}
async fn spawn_harness_opts(script: Vec, preempt: bool) -> Harness {
spawn_harness_full(script, preempt, false).await
}
/// `skip_wait`: when true, the release-signal wait is skipped and the
/// build is preempted immediately (fast deterministic test).
async fn spawn_harness_full(
script: Vec,
preempt: bool,
skip_wait: bool,
) -> Harness {
let tmp = tempfile::tempdir().unwrap();
let remote = init_remote(tmp.path()).await;
// agentd's own mirror: a real bare clone of the temp remote. The
// mock runner clones/fetches from THIS mirror (not the seed remote),
// so WIP refs agentd imports at park time are fetchable at resume.
let mirror = tmp.path().join("mirror.git");
git_no_cwd(
&["clone", "--bare", remote.to_str().unwrap(), mirror.to_str().unwrap()],
tmp.path(),
)
.await;
let (or_url, or_seen) = serve_mock_or(script).await;
let (runner_url, runner_seen) = serve_mock_runner(mirror.clone(), preempt, tmp.path().join("slots")).await;
let (github_url, github_seen) = serve_mock_github().await;
let state_dir = tmp.path().join("state");
std::fs::create_dir_all(&state_dir).unwrap();
let store = Store::open(&state_dir).unwrap();
// Password hash for "s3cret".
let hash = {
use argon2::{Argon2, PasswordHasher, password_hash::SaltString};
let salt = SaltString::from_b64("AAAAAAAAAAAAAAAAAAAAAA").unwrap();
Argon2::default().hash_password(b"s3cret", &salt).unwrap().to_string()
};
let origin = "https://agents.example.test".to_string();
let cfg = crate::config::Config {
openrouter_api_key: "test-key".into(),
openrouter_base_url: or_url,
password_hash: hash,
git_push_token: "PUSH-TOKEN-SHOULD-NEVER-LEAK".into(),
repo_url: remote.to_str().unwrap().to_string(),
public_origin: origin.clone(),
default_model: "test/model".into(),
runner_url,
state_dir: state_dir.clone(),
mirror_dir: mirror.clone(),
release_signal: tmp.path().join("signal-release"),
listen: "127.0.0.1:0".into(),
slots: 2,
github_repo: "vayun-mathur/Modern-Apps".into(),
github_api_base: github_url.clone(),
github_token: Some("test-github-token".into()),
test: crate::config::TestTuning {
poll_ms: Some(20),
build_wait_secs: Some(2),
context_chars: None,
},
test_skip_release_wait: skip_wait,
triage_enabled: false,
triage_interval_secs: 120,
};
let state = AppState {
config: Arc::new(cfg.clone()),
store: store.clone(),
auth: crate::auth::Auth::new(store.clone(), cfg.password_hash.clone()),
runner: crate::runner::Runner::new(cfg.runner_url.clone()),
openrouter: crate::openrouter::OpenRouter::new(
cfg.openrouter_base_url.clone(),
cfg.openrouter_api_key.clone(),
),
events_tx: {
let (tx, _) = tokio::sync::broadcast::channel(64);
tx
},
};
// Scheduler workers (2 slots).
let _ = crate::scheduler::spawn_workers(state.driver(), 2);
let app = crate::ui::router(state);
let lis = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = lis.local_addr().unwrap();
tokio::spawn(async move { axum::serve(lis, app).await.unwrap() });
let client = reqwest::Client::builder().redirect(reqwest::redirect::Policy::none()).build().unwrap();
// Log in.
let resp = client
.post(format!("http://{addr}/login"))
.form(&[("password", "s3cret")])
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let cookie = resp
.headers()
.get(header::SET_COOKIE)
.unwrap()
.to_str()
.unwrap()
.split(';')
.next()
.unwrap()
.to_string();
assert!(cookie.starts_with("agentd_session="));
Harness {
addr,
client,
origin,
_tmp: tmp,
_remote: remote,
runner_seen,
or_seen,
github_seen,
cookie,
store,
}
}
fn url(h: &Harness, path: &str) -> String {
format!("http://{}{}", h.addr, path)
}
async fn create_task(h: &Harness, title: &str, prompt: &str) -> i64 {
create_task_opts(h, title, prompt, false).await
}
/// Create a task, optionally with the "Plan first" checkbox checked
/// (`require_plan=1` in the form body).
async fn create_task_opts(h: &Harness, title: &str, prompt: &str, require_plan: bool) -> i64 {
let mut form = vec![
("title", title),
("module_scope", "notes"),
("model", "test/model"),
("prompt", prompt),
("budget_steps", "60"),
("budget_usd", "2.0"),
];
if require_plan {
form.push(("require_plan", "1"));
}
let resp = h
.client
.post(url(h, "/tasks"))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.form(&form)
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND, "task created");
let loc = resp.headers().get(header::LOCATION).unwrap().to_str().unwrap().to_string();
loc.rsplit('/').next().unwrap().parse().unwrap()
}
/// Create a task from the start-from-issue form: blank title/prompt plus
/// an issue number (prefilled from the mock GitHub). Returns the raw
/// response so tests can assert non-FOUND statuses too.
async fn create_task_from_issue_raw(
h: &Harness,
title: &str,
prompt: &str,
module_scope: &str,
issue_number: &str,
) -> reqwest::Response {
h.client
.post(url(h, "/tasks"))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.form(&[
("title", title),
("module_scope", module_scope),
("model", "test/model"),
("prompt", prompt),
("budget_steps", "60"),
("budget_usd", "2.0"),
("issue_number", issue_number),
])
.send()
.await
.unwrap()
}
async fn create_task_from_issue(h: &Harness, title: &str, prompt: &str, issue: u64) -> i64 {
let resp = create_task_from_issue_raw(h, title, prompt, "notes", &issue.to_string()).await;
assert_eq!(resp.status(), StatusCode::FOUND, "task created from issue");
let loc = resp.headers().get(header::LOCATION).unwrap().to_str().unwrap().to_string();
loc.rsplit('/').next().unwrap().parse().unwrap()
}
async fn post_message(h: &Harness, id: i64, text: &str) -> StatusCode {
h.client
.post(url(h, &format!("/tasks/{id}/messages")))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.form(&[("text", text)])
.send()
.await
.unwrap()
.status()
}
/// POST to an empty-body action route (plan approve, review approve,
/// stop, discard). Axum `Form` extractors on sibling routes need a body;
/// these routes take none, so send none.
async fn post_action(h: &Harness, id: i64, action: &str) -> StatusCode {
h.client
.post(url(h, &format!("/tasks/{id}/{action}")))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.send()
.await
.unwrap()
.status()
}
async fn post_plan_changes(h: &Harness, id: i64, text: &str) -> StatusCode {
h.client
.post(url(h, &format!("/tasks/{id}/plan/changes")))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.form(&[("text", text)])
.send()
.await
.unwrap()
.status()
}
/// Does `refs/heads/` exist on the temp remote?
async fn remote_has_branch(h: &Harness, branch: &str) -> bool {
TokioCommand::new("git")
.arg("--git-dir")
.arg(h._remote.clone())
.arg("rev-parse")
.arg("--verify")
.arg(format!("refs/heads/{branch}"))
.output()
.await
.map(|o| o.status.success())
.unwrap_or(false)
}
/// Show a file at a remote branch (`None` when missing).
async fn remote_show(h: &Harness, branch: &str, path: &str) -> Option {
let out = TokioCommand::new("git")
.arg("--git-dir")
.arg(h._remote.clone())
.arg("show")
.arg(format!("refs/heads/{branch}:{path}"))
.output()
.await
.unwrap();
if out.status.success() {
Some(String::from_utf8(out.stdout).unwrap())
} else {
None
}
}
/// One-line log subjects on the remote branch after `main`.
async fn remote_log(h: &Harness, branch: &str) -> String {
let out = TokioCommand::new("git")
.arg("--git-dir")
.arg(h._remote.clone())
.arg("log")
.arg("--format=%s")
.arg(format!("main..refs/heads/{branch}"))
.output()
.await
.unwrap();
assert!(out.status.success(), "branch {branch} on remote");
String::from_utf8(out.stdout).unwrap()
}
async fn task_html(h: &Harness, id: i64) -> String {
h.client
.get(url(h, &format!("/tasks/{id}")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap()
}
/// Ask-answer-resume: `ask_user` parks as needs_input, the POSTed answer
/// becomes the tool result in the next model request (right position),
/// edits survive, and `finish` parks as review (no push) → approve pushes
/// → succeeded.
#[tokio::test]
async fn ask_user_answer_resume_finish() {
let h = spawn_harness(vec![
tool_resp("c1", "edit_file", serde_json::json!({"path": "notes/file.txt", "old": "hello\n", "new": "hello world\n"}), "editing"),
tool_resp(
"c2",
"ask_user",
serde_json::json!({"question": "Which greeting?", "options": ["hello world", "hi"]}),
"asking",
),
// After resume: the tool result for c2 (the answer) must be in
// this request, then the model finishes.
tool_resp(
"c3",
"finish",
serde_json::json!({
"summary": "Changed the greeting per answer.",
"commit_message": "notes: change greeting\n\nSwitched the greeting text per the task."
}),
"finishing",
),
])
.await;
let id = create_task(&h, "Ask flow", "Edit the file, ask which greeting, then finish.").await;
// Park as needs_input(question); the page shows the question + options.
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
assert!(html.contains("pill needs_input\">needs_input<"), "parked; tail: {}", &html[html.len().saturating_sub(800)..]);
assert!(html.contains("Which greeting?"), "question shown");
assert!(html.contains("hello world"), "option shown");
// Answer via the option-button shape (form posts option text).
assert_eq!(post_message(&h, id, "hello world").await, StatusCode::FOUND);
// `finish` parks as review (no push yet).
wait_review(&h, id, std::time::Duration::from_secs(60)).await;
let branch = format!("agent/{id}-ask-flow");
assert!(!remote_has_branch(&h, &branch).await, "no push before approve");
let html = task_html(&h, id).await;
assert!(html.contains("pill review\">review<"), "in review");
// Approve → push → succeeded.
assert_eq!(post_action(&h, id, "approve").await, StatusCode::FOUND);
wait_status(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
assert!(html.contains("pill succeeded\">succeeded<"), "approved to success");
// The request AFTER the park carries the tool result with the answer
// in the right position: last message, role=tool, id=c2.
let seen = h.or_seen.seen.lock().unwrap().clone();
assert!(seen.len() >= 3, "model called again after resume: {}", seen.len());
let resumed_req = &seen[2];
let msgs = resumed_req.get("messages").and_then(|v| v.as_array()).unwrap();
let last = msgs.last().unwrap();
assert_eq!(last.get("role").and_then(|v| v.as_str()), Some("tool"));
assert_eq!(last.get("tool_call_id").and_then(|v| v.as_str()), Some("c2"));
assert!(
last.get("content").and_then(|v| v.as_str()).unwrap_or("").contains("hello world"),
"answer in tool result: {last}"
);
// The branch was pushed with the edit intact.
let branch = format!("agent/{id}-ask-flow");
let out = TokioCommand::new("git")
.arg("--git-dir")
.arg(h._remote.clone())
.arg("show")
.arg(format!("refs/heads/{branch}:notes/file.txt"))
.output()
.await
.unwrap();
assert!(out.status.success(), "branch pushed");
assert!(String::from_utf8(out.stdout).unwrap().contains("hello world"));
}
/// Steering while running: a message posted mid-run appears as a user
/// message in a subsequent model request.
#[tokio::test]
async fn steering_while_running() {
let h = spawn_harness(vec![
tool_resp("c1", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
tool_resp("c2", "edit_file", serde_json::json!({"path": "notes/file.txt", "old": "hello\n", "new": "hello steered\n"}), "editing"),
tool_resp(
"c3",
"finish",
serde_json::json!({
"summary": "Steered edit.",
"commit_message": "notes: steered edit\n\nBody here."
}),
"finishing",
),
])
.await;
let id = create_task(&h, "Steer me", "Read, edit, finish.").await;
// Post immediately: the driver's first inbox drain happens after its
// first turn's tool results (claim poll + git clone + reads come
// first), so this steering is guaranteed to be picked up for a later
// request — deterministically, with no sleep.
assert_eq!(post_message(&h, id, "steer: prefer exclamation").await, StatusCode::FOUND);
// `finish` parks as review; approve to push.
wait_review(&h, id, std::time::Duration::from_secs(60)).await;
assert_eq!(post_action(&h, id, "approve").await, StatusCode::FOUND);
wait_status(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
assert!(html.contains("pill succeeded\">succeeded<"));
let seen = h.or_seen.seen.lock().unwrap().clone();
let mut found = false;
for req in &seen {
let msgs = req.get("messages").and_then(|v| v.as_array()).unwrap();
for m in msgs {
if m.get("role").and_then(|v| v.as_str()) == Some("user")
&& m.get("content").and_then(|v| v.as_str()).unwrap_or("").contains("steer: prefer exclamation")
{
found = true;
}
}
}
assert!(found, "steering message reached the model");
}
/// Stop → needs_input(stopped) → message → resumes with edits kept.
///
/// Deterministic by stopping immediately after create: the flag persists
/// in the DB, so whenever the driver first observes it (queued or
/// mid-run) it parks as stopped; the resume then replays the script to
/// success. (Stopping after N turns is inherently racy — mock
/// round-trips take milliseconds.)
#[tokio::test]
async fn stop_then_resume_keeps_edits() {
let h = spawn_harness(vec![
tool_resp("c1", "edit_file", serde_json::json!({"path": "notes/file.txt", "old": "hello\n", "new": "hello stopped\n"}), "editing"),
tool_resp("c2", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
tool_resp("c3", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
tool_resp(
"c4",
"finish",
serde_json::json!({
"summary": "Done after stop.",
"commit_message": "notes: resume after stop\n\nBody here."
}),
"finishing",
),
])
.await;
let id = create_task(&h, "Stop flow", "Edit, then keep reading, then finish.").await;
// Stop immediately (flag persists; the driver parks as stopped
// whenever it first observes it — queued or mid-run).
let resp = h
.client
.post(url(&h, &format!("/tasks/{id}/stop")))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
assert!(html.contains("stopped"), "stopped park visible; tail: {}", &html[html.len().saturating_sub(1500)..]);
// Resume with a message; the edit must survive the park/resume.
assert_eq!(post_message(&h, id, "continue please").await, StatusCode::FOUND);
// Script ends in `finish` → review; approve to push.
wait_review(&h, id, std::time::Duration::from_secs(60)).await;
assert_eq!(post_action(&h, id, "approve").await, StatusCode::FOUND);
wait_status(&h, id, std::time::Duration::from_secs(60)).await;
let branch = format!("agent/{id}-stop-flow");
let out = TokioCommand::new("git")
.arg("--git-dir")
.arg(h._remote.clone())
.arg("show")
.arg(format!("refs/heads/{branch}:notes/file.txt"))
.output()
.await
.unwrap();
assert!(out.status.success(), "branch pushed");
assert!(String::from_utf8(out.stdout).unwrap().contains("hello stopped"), "edit survived stop/resume");
}
/// Multi-call turn with ask_user → error result, no park.
#[tokio::test]
async fn mixed_ask_user_turn_rejected_without_park() {
let h = spawn_harness(vec![
serde_json::json!({
"choices": [{"message": {
"role": "assistant", "content": "two calls",
"tool_calls": [
{"id": "m1", "type": "function", "function": {"name": "read_file", "arguments": "{\"path\": \"notes/file.txt\"}"}},
{"id": "m2", "type": "function", "function": {"name": "ask_user", "arguments": "{\"question\": \"Q?\"}"}}
]
}}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5}
}),
// After the rejection the model text-replies → parks as chat.
serde_json::json!({
"choices": [{"message": {"role": "assistant", "content": "understood, continuing alone"}}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5}
}),
])
.await;
let id = create_task(&h, "Mixed turn", "Do things.").await;
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
// Parked as chat (text-only), NOT as a question.
assert!(html.contains("pill needs_input\">needs_input<"));
assert!(!html.contains("Waiting for you (question)"), "no question park; tail: {}", &html[html.len().saturating_sub(1500)..]);
assert!(html.contains("must be the only call in its turn"), "error surfaced");
}
/// Text-only turn parks as chat (nudge-then-fail retired).
#[tokio::test]
async fn text_only_parks_as_chat() {
let h = spawn_harness(vec![serde_json::json!({
"choices": [{"message": {"role": "assistant", "content": "I need guidance"}}],
"usage": {"prompt_tokens": 1, "completion_tokens": 1}
})])
.await;
let id = create_task(&h, "Chat park", "Say hi.").await;
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
assert!(html.contains("pill needs_input\">needs_input<"));
assert!(html.contains("Waiting for you (chat)"), "chat park; tail: {}", &html[html.len().saturating_sub(1500)..]);
}
/// Tiny budget → budget park; answering extends +20 steps / +$1.00.
#[tokio::test]
async fn budget_park_and_extension() {
let h = spawn_harness(vec![
tool_resp("c1", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
tool_resp("c2", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
tool_resp("c3", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
tool_resp("c4", "read_file", serde_json::json!({"path": "notes/file.txt"}), "reading"),
serde_json::json!({
"choices": [{"message": {"role": "assistant", "content": "still going"}}],
"usage": {"prompt_tokens": 1, "completion_tokens": 1}
}),
])
.await;
// Create with budget_steps=2 via the raw form.
let resp = h
.client
.post(url(&h, "/tasks"))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.form(&[
("title", "Budget flow"),
("module_scope", "notes"),
("model", "test/model"),
("prompt", "Read a lot."),
("budget_steps", "2"),
("budget_usd", "2.0"),
])
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let loc = resp.headers().get(header::LOCATION).unwrap().to_str().unwrap().to_string();
let id: i64 = loc.rsplit('/').next().unwrap().parse().unwrap();
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
assert!(html.contains("Waiting for you (budget)"), "budget park; tail: {}", &html[html.len().saturating_sub(1500)..]);
// Answer → resume grants +20 steps / +$1.00 (2 → 22 steps).
assert_eq!(post_message(&h, id, "keep going").await, StatusCode::FOUND);
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let html = task_html(&h, id).await;
assert!(html.contains("22"), "budget extended to 22 steps; tail: {}", &html[html.len().saturating_sub(1500)..]);
}
/// Discard is terminal and removes the WIP ref.
#[tokio::test]
async fn discard_task_terminal() {
let h = spawn_harness(vec![serde_json::json!({
"choices": [{"message": {"role": "assistant", "content": "waiting"}}],
"usage": {"prompt_tokens": 1, "completion_tokens": 1}
})])
.await;
let id = create_task(&h, "Discard me", "Say hi.").await;
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let resp = h
.client
.post(url(&h, &format!("/tasks/{id}/discard")))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let html = task_html(&h, id).await;
assert!(html.contains("pill discarded\">discarded<"), "discarded; tail: {}", &html[html.len().saturating_sub(800)..]);
}
async fn wait_for(h: &Harness, id: i64, statuses: &[&str], timeout: std::time::Duration) {
let start = std::time::Instant::now();
loop {
// Poll the task page for a status pill. NOTE: the page's CSS
// block contains `.pill.failed` etc. selectors, so match the full
// rendered span `pill {s}">{s}` — never the bare class.
let html = h
.client
.get(url(h, &format!("/tasks/{id}")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
for s in statuses {
let span = format!("pill {s}\">{s}");
if html.contains(&span) {
return;
}
}
if start.elapsed() > timeout {
panic!("task {id} did not reach {statuses:?} in {timeout:?}; html tail: {}", &html[html.len().saturating_sub(500)..]);
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
}
async fn wait_status(h: &Harness, id: i64, timeout: std::time::Duration) {
wait_for(h, id, &["succeeded", "failed", "discarded", "cancelled"], timeout).await;
}
async fn wait_parked(h: &Harness, id: i64, timeout: std::time::Duration) {
wait_for(h, id, &["needs_input", "review"], timeout).await;
}
async fn wait_review(h: &Harness, id: i64, timeout: std::time::Duration) {
wait_for(h, id, &["review"], timeout).await;
}
#[tokio::test]
async fn full_success_flow_pushes_branch() {
let h = spawn_harness(success_script()).await;
let resp = h
.client
.post(url(&h, "/tasks"))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.form(&[
("title", "Change greeting"),
("module_scope", "notes"),
("model", "test/model"),
("prompt", "Change the greeting text."),
("budget_steps", "60"),
("budget_usd", "2.0"),
])
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND, "task created");
let loc = resp.headers().get(header::LOCATION).unwrap().to_str().unwrap().to_string();
let id: i64 = loc.rsplit('/').next().unwrap().parse().unwrap();
// `finish` parks as review WITHOUT pushing; the branch must not exist
// on the remote yet.
let branch = format!("agent/{id}-change-greeting");
wait_review(&h, id, std::time::Duration::from_secs(60)).await;
assert!(!remote_has_branch(&h, &branch).await, "no push before approve");
// Review card shows summary, commit message, and the diff.
let html = task_html(&h, id).await;
assert!(html.contains("pill review\">review<"), "in review");
assert!(html.contains("Changed the greeting."), "summary shown");
assert!(html.contains("notes: change greeting"), "commit message shown");
assert!(html.contains("hello world"), "diff shown");
assert!(html.contains("Approve & push") || html.contains("Approve & push"), "approve button");
// Approve → push → succeeded with the agent's message as the single commit.
assert_eq!(post_action(&h, id, "approve").await, StatusCode::FOUND);
wait_status(&h, id, std::time::Duration::from_secs(60)).await;
// Task page shows success.
let html = h
.client
.get(url(&h, &format!("/tasks/{id}")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
assert!(html.contains("pill succeeded\">succeeded<"), "task succeeded; html tail: {}", &html[html.len().saturating_sub(800)..]);
assert!(html.contains("agent/"), "branch shown");
// The branch exists in the temp remote with the expected commit.
let log = remote_log(&h, &branch).await;
assert!(log.contains("notes: change greeting"), "commit msg: {log}");
assert_eq!(log.lines().count(), 1, "single squashed commit: {log}");
// Token never appears in events (SSE backlog) or any page.
// NOTE: `/tasks/{id}/events` is an infinite SSE stream (15s
// keep-alives), so `.text()` on it would hang forever. Read it with
// `?after=` bounded ... instead: assert on the DB-backed backlog via
// the HTML page's rendered events (already covered) plus a direct
// events query below with a timeout-guarded raw socket read.
for path in [format!("/tasks/{id}"), "/".to_string()] {
let body = h
.client
.get(url(&h, &path))
.header(header::COOKIE, &h.cookie)
.header(header::ACCEPT, "text/html")
.send()
.await
.unwrap()
.text()
.await
.unwrap();
assert!(!body.contains("PUSH-TOKEN-SHOULD-NEVER-LEAK"), "token leaked in {path}");
}
{
// Bounded SSE read: the backlog frames arrive immediately; the
// stream then idles on keep-alives, so cap the read at 10s.
use tokio::io::AsyncReadExt;
let stream = tokio::net::TcpStream::connect(h.addr).await.unwrap();
let (mut reader, mut writer) = stream.into_split();
let req = format!(
"GET /tasks/{id}/events HTTP/1.1\r\nHost: {}\r\nCookie: {}\r\nAccept: text/event-stream\r\nConnection: close\r\n\r\n",
h.addr, h.cookie
);
use tokio::io::AsyncWriteExt;
writer.write_all(req.as_bytes()).await.unwrap();
let mut buf = vec![0u8; 1_000_000];
let mut total = 0usize;
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
loop {
let remaining = deadline.saturating_duration_since(std::time::Instant::now());
if remaining.is_zero() {
break;
}
match tokio::time::timeout(
remaining,
reader.read(&mut buf[total..]),
)
.await
{
Ok(Ok(0)) => break,
Ok(Ok(n)) => {
total += n;
if total >= buf.len() {
break;
}
}
_ => break,
}
}
let body = String::from_utf8_lossy(&buf[..total]);
assert!(!body.contains("PUSH-TOKEN-SHOULD-NEVER-LEAK"), "token leaked in events stream");
assert!(body.contains("data:"), "expected SSE data frames, got {body:?}");
}
// Runner saw the build command.
let seen = h.runner_seen.build_seen.lock().unwrap();
assert!(seen.iter().any(|c| c.contains(":notes:compileDevKotlin")), "build forwarded: {seen:?}");
}
#[tokio::test]
async fn unauthenticated_post_rejected() {
let h = spawn_harness(vec![]).await;
let resp = h
.client
.post(url(&h, "/tasks"))
.header(header::ORIGIN, &h.origin)
.form(&[
("title", "x"),
("module_scope", "notes"),
("model", "m"),
("prompt", "y"),
("budget_steps", "60"),
("budget_usd", "2.0"),
])
.send()
.await
.unwrap();
// Redirect to /login (not queued).
assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/login");
}
#[tokio::test]
async fn wrong_origin_rejected() {
let h = spawn_harness(vec![]).await;
let resp = h
.client
.post(url(&h, "/tasks"))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, "https://evil.example.test")
.form(&[
("title", "x"),
("module_scope", "notes"),
("model", "m"),
("prompt", "y"),
("budget_steps", "60"),
("budget_usd", "2.0"),
])
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
// Missing Origin also rejected.
let resp = h
.client
.post(url(&h, "/tasks"))
.header(header::COOKIE, &h.cookie)
.form(&[
("title", "x"),
("module_scope", "notes"),
("model", "m"),
("prompt", "y"),
("budget_steps", "60"),
("budget_usd", "2.0"),
])
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
}
#[tokio::test]
async fn release_signal_preempts_build() {
// The harness holds the release-signal file present and skips the
// 60-minute wait; agentd must surface "preempted by a production
// release" to the model (and call the runner's builds/kill). The
// trailing text-only turn now parks as needs_input(chat) instead of
// failing (nudge-then-fail is retired).
let h = spawn_harness_full(
vec![
tool_resp(
"c1",
"build",
serde_json::json!({"command": "./gradlew :notes:lint"}),
"try build",
),
serde_json::json!({
"choices": [{"message": {"role": "assistant", "content": "retrying"}}],
"usage": {"prompt_tokens": 1, "completion_tokens": 1}
}),
],
false,
true,
)
.await;
// Hold the release signal for the whole task (harness points
// AGENTD_RELEASE_SIGNAL at tmp/signal-release).
std::fs::write(h._tmp.path().join("signal-release"), "1").unwrap();
let resp = h
.client
.post(url(&h, "/tasks"))
.header(header::COOKIE, &h.cookie)
.header(header::ORIGIN, &h.origin)
.form(&[
("title", "Build during release"),
("module_scope", "notes"),
("model", "test/model"),
("prompt", "Run a build."),
("budget_steps", "60"),
("budget_usd", "2.0"),
])
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let loc = resp.headers().get(header::LOCATION).unwrap().to_str().unwrap().to_string();
let id: i64 = loc.rsplit('/').next().unwrap().parse().unwrap();
let _ = wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let html = h
.client
.get(url(&h, &format!("/tasks/{id}")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
assert!(
html.contains("preempted by a production release"),
"preemption surfaced; tail: {}",
&html[html.len().saturating_sub(2000)..]
);
assert!(
html.contains("pill needs_input\">needs_input<"),
"text-only turn parked as chat; tail: {}",
&html[html.len().saturating_sub(2000)..]
);
}
/// Chat UI over HTTP: static assets carry JS/CSS content types, the four
/// fragment routes 401 without auth, a parked (chat) task shows up under
/// "Needs you" with a `(1)` title count, and the task page has no inline
/// script/style plus a CSP without 'unsafe-inline'.
#[tokio::test]
async fn ui_fragments_dashboard_and_static() {
let h = spawn_harness(vec![serde_json::json!({
"choices": [{"message": {"role": "assistant", "content": "hello, need guidance"}}],
"usage": {"prompt_tokens": 1, "completion_tokens": 1}
})])
.await;
// Static assets: correct content types, real bodies.
let resp = h.client.get(url(&h, "/static/app.js")).send().await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let ct = resp.headers().get(header::CONTENT_TYPE).unwrap().to_str().unwrap().to_string();
assert!(ct.starts_with("application/javascript"), "js type: {ct}");
let js = resp.text().await.unwrap();
assert!(js.contains("EventSource"), "live-update script served");
let resp = h.client.get(url(&h, "/static/app.css")).send().await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let ct = resp.headers().get(header::CONTENT_TYPE).unwrap().to_str().unwrap().to_string();
assert!(ct.starts_with("text/css"), "css type: {ct}");
// Fragments require auth: no cookie → 401 (never a login redirect
// the script would swap into the page).
for path in [
"/tasks/1/timeline",
"/tasks/1/panel",
"/tasks/1/cards",
"/fragments/dashboard",
] {
let r = h.client.get(url(&h, path)).send().await.unwrap();
assert_eq!(r.status(), StatusCode::UNAUTHORIZED, "{path} needs auth");
}
// Park a task as chat (needs_input) → dashboard counts it.
let id = create_task(&h, "Chat UI", "Say hi.").await;
wait_parked(&h, id, std::time::Duration::from_secs(60)).await;
let dash = h
.client
.get(url(&h, "/"))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
assert!(dash.contains("(1) agentd"), "title count; head: {}", &dash[..dash.len().min(600)]);
assert!(dash.contains("Needs you (1)"), "needs-you section");
assert!(dash.contains("replied — waiting for you"), "chat reason");
// Authed fragments render server markup.
let body = h
.client
.get(url(&h, &format!("/tasks/{id}/timeline")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
assert!(body.contains("bubble assistant"), "timeline fragment; body: {body}");
let body = h
.client
.get(url(&h, &format!("/tasks/{id}/panel")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
assert!(body.contains("Todos"), "panel fragment; body: {body}");
let body = h
.client
.get(url(&h, &format!("/tasks/{id}/cards")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
assert!(body.contains("Waiting for you (chat)"), "cards fragment; body: {body}");
// Full task page: bundled script only, no inline script/style, and
// the CSP no longer allows 'unsafe-inline'.
let resp = h
.client
.get(url(&h, &format!("/tasks/{id}")))
.header(header::COOKIE, &h.cookie)
.send()
.await
.unwrap();
let csp = resp.headers().get(header::CONTENT_SECURITY_POLICY).unwrap().to_str().unwrap().to_string();
assert!(!csp.contains("unsafe-inline"), "no unsafe-inline: {csp}");
assert!(csp.contains("script-src 'self'"), "script self: {csp}");
let html = resp.text().await.unwrap();
assert!(html.contains("