orch-cli 0.100.14

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use super::{
    ALLOWED_FIELDS, INCREMENTABLE_FIELDS, MemoryEntry, NewTask, Row, StoreRouteCmd, TaskStatus,
    UpsertExternalCmd, sanitize_activity_details, sql_value_for_task_field,
};
use sqlx::SqliteConnection;

async fn previous(
    conn: &mut SqliteConnection,
    id: i64,
) -> anyhow::Result<(String, Option<String>, Option<String>)> {
    let row = sqlx::query("SELECT status, agent, model FROM tasks WHERE id = ?")
        .bind(id)
        .fetch_one(&mut *conn)
        .await?;
    let status: String = row
        .try_get("status")
        .map_err(|e| anyhow::anyhow!("failed to read status column before update: {e}"))?;
    let agent: Option<String> = row
        .try_get("agent")
        .map_err(|e| anyhow::anyhow!("failed to read agent column before update: {e}"))?;
    let model: Option<String> = row
        .try_get("model")
        .map_err(|e| anyhow::anyhow!("failed to read model column before update: {e}"))?;
    Ok((status, agent, model))
}

#[expect(
    clippy::too_many_arguments,
    reason = "one argument per column of the activity row"
)]
pub(crate) async fn activity(
    conn: &mut SqliteConnection,
    task_id: i64,
    event_type: &str,
    from_status: Option<&str>,
    to_status: Option<&str>,
    agent: Option<&str>,
    model: Option<&str>,
    details: Option<&serde_json::Value>,
) -> anyhow::Result<()> {
    let details_json = match details {
        Some(v) => serde_json::to_string(&sanitize_activity_details(v))?,
        None => "{}".to_string(),
    };
    sqlx::query(
        "INSERT INTO task_activity
         (task_id, event_type, from_status, to_status, agent, model, details)
         VALUES (?, ?, ?, ?, ?, ?, ?)",
    )
    .bind(task_id)
    .bind(event_type)
    .bind(from_status)
    .bind(to_status)
    .bind(agent)
    .bind(model)
    .bind(details_json)
    .execute(conn)
    .await?;
    Ok(())
}

async fn status_change(
    conn: &mut SqliteConnection,
    id: i64,
    previous: &(String, Option<String>, Option<String>),
    to: TaskStatus,
    details: Option<&serde_json::Value>,
) -> anyhow::Result<()> {
    activity(
        conn,
        id,
        "status_change",
        Some(previous.0.as_str()),
        Some(to.as_str()),
        previous.1.as_deref(),
        previous.2.as_deref(),
        details,
    )
    .await
}

/// Through Raft, ids come from the replicated rows so every node assigns the same one.
/// Otherwise NULL lets SQLite's AUTOINCREMENT pick it, as before
pub(crate) async fn create(
    conn: &mut SqliteConnection,
    now: &str,
    new: &NewTask,
    internal: bool,
    replicated: bool,
) -> anyhow::Result<i64> {
    let labels_json = serde_json::to_string(&new.labels)?;
    let row = sqlx::query(
        "INSERT INTO tasks (id, external_id, repo, origin, title, body, source, source_id, author, url, labels, parent_id, created_at, updated_at)
         VALUES ((SELECT CASE WHEN ? THEN COALESCE(MAX(id), 0) + 1 END FROM tasks), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
         RETURNING id",
    )
    .bind(replicated)
    .bind(&new.external_id)
    .bind(&new.repo)
    .bind(&new.origin)
    .bind(&new.title)
    .bind(&new.body)
    .bind(&new.source)
    .bind(&new.source_id)
    .bind(&new.author)
    .bind(&new.url)
    .bind(&labels_json)
    .bind(new.parent_id)
    .bind(now)
    .bind(now)
    .fetch_one(&mut *conn)
    .await?;
    let id: i64 = row
        .try_get("id")
        .map_err(|e| anyhow::anyhow!("INSERT RETURNING id returned NULL or invalid: {e}"))?;
    if internal {
        // resolve_task_id finds internal tasks by this external id
        sqlx::query("UPDATE tasks SET external_id = ? WHERE id = ?")
            .bind(format!("internal:{id}"))
            .bind(id)
            .execute(conn)
            .await?;
    }

    Ok(id)
}

pub(crate) async fn upsert_external(
    conn: &mut SqliteConnection,
    now: &str,
    ext: &UpsertExternalCmd,
    replicated: bool,
) -> anyhow::Result<i64> {
    let labels_json = serde_json::to_string(&ext.labels)?;
    let row = sqlx::query(
        "INSERT INTO tasks (id, external_id, repo, origin, title, body, author, url, labels, parent_id, created_at, updated_at)
         VALUES ((SELECT CASE WHEN ? THEN COALESCE(MAX(id), 0) + 1 END FROM tasks), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
         ON CONFLICT(repo, external_id) DO UPDATE SET
            title = excluded.title,
            body = excluded.body,
            labels = excluded.labels,
            url = excluded.url,
            parent_id = COALESCE(excluded.parent_id, tasks.parent_id)
         RETURNING id",
    )
    .bind(replicated)
    .bind(&ext.ext_id)
    .bind(&ext.repo)
    .bind(&ext.origin)
    .bind(&ext.title)
    .bind(&ext.body)
    .bind(&ext.author)
    .bind(&ext.url)
    .bind(&labels_json)
    .bind(ext.parent_id)
    .bind(now)
    .bind(now)
    .fetch_one(conn)
    .await?;
    row.try_get("id")
        .map_err(|e| anyhow::anyhow!("INSERT RETURNING id returned NULL or invalid: {e}"))
}

fn ensure_allowed(updates: &[(String, serde_json::Value)]) -> anyhow::Result<()> {
    for (col, _) in updates {
        anyhow::ensure!(
            ALLOWED_FIELDS.contains(&col.as_str()),
            "column {col} is not in the update allowlist"
        );
    }

    Ok(())
}

pub(crate) async fn update_status_and_fields(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    status: TaskStatus,
    updates: &[(String, serde_json::Value)],
) -> anyhow::Result<()> {
    ensure_allowed(updates)?;
    let prev = previous(conn, id).await?;

    let mut set_parts = vec!["status = ?".to_string()];
    let mut values: Vec<Option<String>> = vec![Some(status.as_str().to_string())];
    let mut block_reason_in_updates = false;
    for (col, val) in updates {
        if col == "block_reason" {
            block_reason_in_updates = true;
        }

        set_parts.push(format!("{col} = ?"));
        match val {
            serde_json::Value::String(s) => values.push(Some(s.clone())),
            serde_json::Value::Number(n) => values.push(Some(n.to_string())),
            serde_json::Value::Bool(b) => {
                values.push(Some(if *b { "1" } else { "0" }.to_string()));
            }
            serde_json::Value::Null => values.push(None),
            other => values.push(Some(serde_json::to_string(other)?)),
        }
    }

    if !block_reason_in_updates && status != TaskStatus::Blocked {
        set_parts.push("block_reason = NULL".to_string());
    }

    set_parts.push("updated_at = ?".to_string());
    values.push(Some(now.to_string()));
    let sql = format!("UPDATE tasks SET {} WHERE id = ?", set_parts.join(", "));
    let mut query = sqlx::query(sqlx::AssertSqlSafe(sql.as_str()));
    for v in &values {
        query = query.bind(v.as_deref());
    }
    query.bind(id).execute(&mut *conn).await?;
    status_change(conn, id, &prev, status, None).await
}

/// Moves the task to `status`, and when `expected` is set only if it still holds that status
pub(crate) async fn update_status(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    status: TaskStatus,
    expected: Option<TaskStatus>,
) -> anyhow::Result<bool> {
    let prev = previous(conn, id).await?;
    let clear_block = if status == TaskStatus::Blocked {
        ""
    } else {
        ", block_reason = NULL"
    };
    let guard = if expected.is_some() {
        " AND status = ?"
    } else {
        ""
    };
    let sql =
        format!("UPDATE tasks SET status = ?{clear_block}, updated_at = ? WHERE id = ?{guard}");
    let mut query = sqlx::query(sqlx::AssertSqlSafe(sql.as_str()))
        .bind(status.as_str())
        .bind(now)
        .bind(id);
    if let Some(expected) = expected {
        query = query.bind(expected.as_str());
    }

    if query.execute(&mut *conn).await?.rows_affected() == 0 {
        return Ok(false);
    }

    status_change(conn, id, &prev, status, None).await?;
    Ok(true)
}

pub(crate) async fn update_external_id(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    external_id: &str,
) -> anyhow::Result<()> {
    sqlx::query("UPDATE tasks SET external_id = ?, updated_at = ? WHERE id = ?")
        .bind(external_id)
        .bind(now)
        .bind(id)
        .execute(conn)
        .await?;
    Ok(())
}

pub(crate) async fn touch(conn: &mut SqliteConnection, now: &str, id: i64) -> anyhow::Result<()> {
    sqlx::query("UPDATE tasks SET updated_at = ? WHERE id = ?")
        .bind(now)
        .bind(id)
        .execute(conn)
        .await?;
    Ok(())
}

pub(crate) async fn reset_to_new(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
) -> anyhow::Result<()> {
    let prev = previous(conn, id).await?;
    sqlx::query(
        "UPDATE tasks SET status = 'new', branch = '', worktree = '', worktree_cleaned = 0, block_reason = NULL, updated_at = ? WHERE id = ?",
    )
    .bind(now)
    .bind(id)
    .execute(&mut *conn)
    .await?;
    let details = serde_json::json!({ "op": "reset_to_new" });
    status_change(conn, id, &prev, TaskStatus::New, Some(&details)).await
}

/// `silent` leaves `updated_at` alone, for bookkeeping that must not look like activity
pub(crate) async fn set_fields(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    updates: &[(String, serde_json::Value)],
    silent: bool,
) -> anyhow::Result<()> {
    if updates.is_empty() {
        return Ok(());
    }

    ensure_allowed(updates)?;
    let mut set_parts = Vec::new();
    let mut values: Vec<Option<String>> = Vec::new();
    for (col, val) in updates {
        set_parts.push(format!("{col} = ?"));
        values.push(sql_value_for_task_field(col, val)?);
    }

    if !silent {
        set_parts.push("updated_at = ?".to_string());
        values.push(Some(now.to_string()));
    }

    let sql = format!("UPDATE tasks SET {} WHERE id = ?", set_parts.join(", "));
    let mut query = sqlx::query(sqlx::AssertSqlSafe(sql.as_str()));
    for v in &values {
        query = query.bind(v.as_deref());
    }
    query.bind(id).execute(conn).await?;
    Ok(())
}

pub(crate) async fn increment(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    field: &str,
) -> anyhow::Result<i64> {
    anyhow::ensure!(
        INCREMENTABLE_FIELDS.contains(&field),
        "field {field} is not incrementable"
    );
    let sql = format!(
        "UPDATE tasks SET {field} = {field} + 1, updated_at = ? WHERE id = ? RETURNING {field} AS val"
    );
    let row = sqlx::query(sqlx::AssertSqlSafe(sql.as_str()))
        .bind(now)
        .bind(id)
        .fetch_one(conn)
        .await?;
    let val: i32 = row.try_get("val")?;
    Ok(val.into())
}

/// `failure_only` keeps `attempts`, `review_invocations`, `review_cycles`,
/// `ci_merge_failures` and `merge_conflict_retries`, see `reset_failure_counters`
pub(crate) async fn reset_counters(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    failure_only: bool,
) -> anyhow::Result<()> {
    let all = if failure_only {
        ""
    } else {
        "attempts = 0, merge_conflict_retries = 0, ci_merge_failures = 0, review_cycles = 0, review_invocations = 0,"
    };
    let sql = format!(
        "UPDATE tasks SET {all}
        route_attempts = 0,
        review_agent_failures = 0,
        review_resets = 0,
        pr_create_failures = 0,
        push_failures = 0,
        network_retries = 0,
        timeout_retries = 0,
        auto_unblock_count = 0,
        auto_unblock_last_at = '',
        auto_unblock_last_reason = '',
        ci_recovery_count = 0,
        no_code_reroutes = 0,
        no_code_last_agent = '',
        needs_review_refires = 0,
        updated_at = ?
     WHERE id = ?"
    );
    sqlx::query(sqlx::AssertSqlSafe(sql.as_str()))
        .bind(now)
        .bind(id)
        .execute(conn)
        .await?;
    Ok(())
}

pub(crate) async fn store_route(
    conn: &mut SqliteConnection,
    now: &str,
    route: &StoreRouteCmd,
) -> anyhow::Result<()> {
    sqlx::query(
        "UPDATE tasks SET
        agent = ?, model = ?, complexity = ?, estimate = ?, route_reason = ?,
        agent_profile = ?, selected_skills = ?,
        updated_at = ?
     WHERE id = ?",
    )
    .bind(&route.agent)
    .bind(&route.model)
    .bind(&route.complexity)
    .bind(i64::from(route.estimate))
    .bind(&route.reason)
    .bind(&route.profile)
    .bind(&route.skills)
    .bind(now)
    .bind(route.id)
    .execute(&mut *conn)
    .await?;
    let details = serde_json::json!({
        "complexity": route.complexity,
        "estimate": route.estimate,
        "reason": route.reason,
        "skills": route.skills,
    });
    activity(
        conn,
        route.id,
        "dispatch",
        None,
        None,
        Some(&route.agent),
        route.model.as_deref(),
        Some(&details),
    )
    .await
}

pub(crate) async fn set_estimate_if_zero(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    estimate: u8,
) -> anyhow::Result<bool> {
    let result =
        sqlx::query("UPDATE tasks SET estimate = ?, updated_at = ? WHERE id = ? AND estimate = 0")
            .bind(i64::from(estimate))
            .bind(now)
            .bind(id)
            .execute(conn)
            .await?;
    Ok(result.rows_affected() > 0)
}

pub(crate) async fn append_memory(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
    entry: &MemoryEntry,
) -> anyhow::Result<()> {
    let row = sqlx::query("SELECT memory FROM tasks WHERE id = ?")
        .bind(id)
        .fetch_one(&mut *conn)
        .await?;
    // A corrupt value must fail, an empty array in its place would lose the old entries
    let memory_opt: Option<String> = row
        .try_get::<Option<String>, _>("memory")
        .map_err(|e| anyhow::anyhow!("failed to read memory column for task {id}: {e}"))?;
    let memory_str = memory_opt.unwrap_or_default();
    let mut memory: Vec<MemoryEntry> = if memory_str.trim().is_empty() {
        Vec::new()
    } else {
        serde_json::from_str(&memory_str)
            .map_err(|e| anyhow::anyhow!("failed to parse memory JSON for task {id}: {e}"))?
    };
    memory.push(entry.clone());
    sqlx::query("UPDATE tasks SET memory = ?, updated_at = ? WHERE id = ?")
        .bind(serde_json::to_string(&memory)?)
        .bind(now)
        .bind(id)
        .execute(conn)
        .await?;
    Ok(())
}

pub(crate) async fn mark_cleaned(
    conn: &mut SqliteConnection,
    now: &str,
    id: i64,
) -> anyhow::Result<()> {
    sqlx::query("UPDATE tasks SET worktree_cleaned = 1, updated_at = ? WHERE id = ?")
        .bind(now)
        .bind(id)
        .execute(&mut *conn)
        .await?;
    activity(
        conn,
        id,
        "branch_delete",
        None,
        None,
        None,
        None,
        Some(&serde_json::json!({ "worktree_cleaned": true })),
    )
    .await
}