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
2 changes: 1 addition & 1 deletion src/agent/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl ChannelState {
};

self.process_run_logger
.log_worker_completed(worker_id, &result, false);
.log_worker_cancelled(worker_id, &result);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to persist cancelled explicitly. One thing to keep an eye on: the synthetic WorkerComplete { success: false } is still indistinguishable from a real failure for any downstream consumers that don't consult worker_runs.status (or parse the result text). If anything still maps success=false => failed, it may be worth threading a terminal status through the event shape in a follow-up.

if let Err(error) = self.deps.event_tx.send(ProcessEvent::WorkerComplete {
agent_id: self.deps.agent_id.clone(),
worker_id,
Expand Down
141 changes: 136 additions & 5 deletions src/conversation/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,24 @@ impl ProcessRunLogger {

/// Record a worker completing with its result. Fire-and-forget.
pub fn log_worker_completed(&self, worker_id: WorkerId, result: &str, success: bool) {
let status = if success { "done" } else { "failed" };
self.log_worker_completed_with_status(worker_id, result, status);
}

/// Record a worker as cancelled. Fire-and-forget.
pub fn log_worker_cancelled(&self, worker_id: WorkerId, result: &str) {
self.log_worker_completed_with_status(worker_id, result, "cancelled");
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

fn log_worker_completed_with_status(&self, worker_id: WorkerId, result: &str, status: &str) {
let pool = self.pool.clone();
let id = worker_id.to_string();
let result = result.to_string();
let status = if success { "done" } else { "failed" };
let status = status.to_string();

tokio::spawn(async move {
if let Err(error) = sqlx::query(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since log_worker_started() is also fire-and-forget, it seems possible for this UPDATE to race ahead of the INSERT on very fast cancellations and silently affect 0 rows. You already handle this pattern in log_worker_project_link() / log_opencode_metadata().

Might be worth adding a small retry on rows_affected() == 0 here so we don't lose the terminal state, and guarding against overwriting an already-terminal row.

Suggested change
if let Err(error) = sqlx::query(
"UPDATE worker_runs SET result = ?, status = ?, completed_at = CURRENT_TIMESTAMP WHERE id = ? AND completed_at IS NULL"

"UPDATE worker_runs SET result = ?, status = ?, completed_at = CURRENT_TIMESTAMP WHERE id = ?"
"UPDATE worker_runs SET result = ?, status = ?, completed_at = CURRENT_TIMESTAMP WHERE id = ? AND completed_at IS NULL"
)
.bind(&result)
.bind(status)
Expand Down Expand Up @@ -671,7 +681,7 @@ impl ProcessRunLogger {
WHEN result IS NULL OR result = '' THEN 'Worker cancelled' \
ELSE result \
END, \
status = 'failed', \
status = 'cancelled', \
completed_at = COALESCE(completed_at, CURRENT_TIMESTAMP) \
WHERE id = ? AND channel_id = ? AND status = 'running'",
)
Expand All @@ -697,7 +707,7 @@ impl ProcessRunLogger {
WHEN result IS NULL OR result = '' THEN 'Worker cancelled' \
ELSE result \
END, \
status = 'failed', \
status = 'cancelled', \
completed_at = COALESCE(completed_at, CURRENT_TIMESTAMP) \
WHERE id = ? AND channel_id IS NULL AND status = 'running'",
)
Expand Down Expand Up @@ -1050,10 +1060,131 @@ mod tests {

let status: String = sqlx::Row::try_get(&row, "status").expect("missing status");
let result: String = sqlx::Row::try_get(&row, "result").expect("missing result");
assert_eq!(status, "failed");
assert_eq!(status, "cancelled");
assert_eq!(result, "Worker cancelled");
}

#[tokio::test]
async fn cancel_running_worker_sets_cancelled_status() {
let pool = setup_worker_runs_table().await;
let logger = ProcessRunLogger::new(pool.clone());
let worker_id = uuid::Uuid::new_v4();

sqlx::query("INSERT INTO worker_runs (id, channel_id, status, result) VALUES (?, 'ch-1', 'running', '')")
.bind(worker_id.to_string())
.execute(&pool)
.await
.expect("insert");

let cancelled = logger
.cancel_running_worker("ch-1", worker_id)
.await
.expect("cancel should succeed");
assert!(cancelled);

let row = sqlx::query("SELECT status, result FROM worker_runs WHERE id = ?")
.bind(worker_id.to_string())
.fetch_one(&pool)
.await
.expect("fetch");
let status: String = sqlx::Row::try_get(&row, "status").expect("status");
let result: String = sqlx::Row::try_get(&row, "result").expect("result");
assert_eq!(status, "cancelled");
assert_eq!(result, "Worker cancelled");
}

/// Poll until a worker's status changes from "running", with a timeout.
async fn poll_worker_status(
pool: &sqlx::SqlitePool,
worker_id: uuid::Uuid,
timeout_ms: u64,
) -> String {
let deadline = tokio::time::Instant::now() + std::time::Duration::from_millis(timeout_ms);
loop {
let row = sqlx::query("SELECT status FROM worker_runs WHERE id = ?")
.bind(worker_id.to_string())
.fetch_one(pool)
.await
.expect("fetch");
let status: String = sqlx::Row::try_get(&row, "status").expect("status");
if status != "running" {
return status;
}
if tokio::time::Instant::now() >= deadline {
panic!("worker status did not transition within {timeout_ms}ms");
}
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
}
}

#[tokio::test]
async fn log_worker_completed_success_sets_done_status() {
let pool = setup_worker_runs_table().await;
let logger = ProcessRunLogger::new(pool.clone());
let worker_id = uuid::Uuid::new_v4();

sqlx::query("INSERT INTO worker_runs (id, channel_id, status, result) VALUES (?, 'ch-1', 'running', '')")
.bind(worker_id.to_string())
.execute(&pool)
.await
.expect("insert");

logger.log_worker_completed(worker_id, "task finished", true);
let status = poll_worker_status(&pool, worker_id, 2000).await;

assert_eq!(status, "done");
let row = sqlx::query("SELECT result FROM worker_runs WHERE id = ?")
.bind(worker_id.to_string())
.fetch_one(&pool)
.await
.expect("fetch");
let result: String = sqlx::Row::try_get(&row, "result").expect("result");
assert_eq!(result, "task finished");
}

#[tokio::test]
async fn log_worker_completed_failure_sets_failed_status() {
let pool = setup_worker_runs_table().await;
let logger = ProcessRunLogger::new(pool.clone());
let worker_id = uuid::Uuid::new_v4();

sqlx::query("INSERT INTO worker_runs (id, channel_id, status, result) VALUES (?, 'ch-1', 'running', '')")
.bind(worker_id.to_string())
.execute(&pool)
.await
.expect("insert");

logger.log_worker_completed(worker_id, "something broke", false);
let status = poll_worker_status(&pool, worker_id, 2000).await;

assert_eq!(status, "failed");
}

#[tokio::test]
async fn log_worker_cancelled_sets_cancelled_status() {
let pool = setup_worker_runs_table().await;
let logger = ProcessRunLogger::new(pool.clone());
let worker_id = uuid::Uuid::new_v4();

sqlx::query("INSERT INTO worker_runs (id, channel_id, status, result) VALUES (?, 'ch-1', 'running', '')")
.bind(worker_id.to_string())
.execute(&pool)
.await
.expect("insert");

logger.log_worker_cancelled(worker_id, "Worker cancelled: user requested");
let status = poll_worker_status(&pool, worker_id, 2000).await;

assert_eq!(status, "cancelled");
let row = sqlx::query("SELECT result FROM worker_runs WHERE id = ?")
.bind(worker_id.to_string())
.fetch_one(&pool)
.await
.expect("fetch");
let result: String = sqlx::Row::try_get(&row, "result").expect("result");
assert_eq!(result, "Worker cancelled: user requested");
}

#[tokio::test]
async fn cancel_running_detached_worker_does_not_touch_channel_bound_rows() {
let pool = setup_worker_runs_table().await;
Expand Down
1 change: 1 addition & 0 deletions src/tools/worker_inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl WorkerInspectTool {
"running" => "[running]",
"done" => "[done]",
"failed" => "[failed]",
"cancelled" => "[cancelled]",
_ => "[-]",
};
summary.push_str(&format!(
Expand Down
Loading