orch-cli 0.103.0

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
use std::fs;
use std::os::unix::fs::symlink;

use axum::body::{Body, to_bytes};
use axum::http::Request;
use tower::ServiceExt;

use super::*;

const TOKEN: &str = "secret-token";
const HOST: &str = "127.0.0.1:43917";

async fn app() -> Router {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let (events, _) = broadcast::channel(4);
    router(App {
        store,
        events,
        token: Arc::from(TOKEN),
        host: Arc::from(HOST),
        codes: Arc::default(),
    })
}

async fn get(path: &str, host: &str, cookie: Option<&str>) -> Response {
    let mut req = Request::get(path).header(header::HOST, host);
    if let Some(c) = cookie {
        req = req.header(header::COOKIE, c);
    }

    app()
        .await
        .oneshot(req.body(Body::empty()).unwrap())
        .await
        .unwrap()
}

#[tokio::test]
async fn an_api_call_without_the_cookie_is_refused() {
    let res = get("/api/tasks?repo=o/r", HOST, None).await;
    assert_eq!(
        res.status(),
        StatusCode::UNAUTHORIZED,
        "no cookie must not pass"
    );
}

#[tokio::test]
async fn a_wrong_cookie_is_refused() {
    let res = get("/api/tasks?repo=o/r", HOST, Some("orch_ui=nope")).await;
    assert_eq!(
        res.status(),
        StatusCode::UNAUTHORIZED,
        "a wrong token must not pass"
    );
}

#[tokio::test]
async fn a_rebound_host_is_refused_even_with_the_cookie() {
    let res = get(
        "/api/tasks?repo=o/r",
        "evil.example:43917",
        Some("orch_ui=secret-token"),
    )
    .await;
    assert_eq!(
        res.status(),
        StatusCode::UNAUTHORIZED,
        "a DNS rebinding page sends its own Host"
    );
}

#[tokio::test]
async fn the_right_cookie_and_host_read_the_board() {
    let res = get(
        "/api/tasks?repo=o/r",
        HOST,
        Some("other=1; orch_ui=secret-token"),
    )
    .await;
    assert_eq!(res.status(), StatusCode::OK, "the owner's cookie must pass");
}

async fn post_code(app: &Router, bearer: &str) -> Response {
    let req = Request::post("/login-code")
        .header(header::HOST, HOST)
        .header(header::AUTHORIZATION, format!("Bearer {bearer}"))
        .body(Body::empty())
        .unwrap();
    app.clone().oneshot(req).await.unwrap()
}

async fn visit(app: &Router, path: &str) -> Response {
    let req = Request::get(path)
        .header(header::HOST, HOST)
        .body(Body::empty())
        .unwrap();
    app.clone().oneshot(req).await.unwrap()
}

#[tokio::test]
async fn a_sign_in_code_works_once_and_sets_a_strict_cookie() {
    let app = app().await;
    let res = post_code(&app, TOKEN).await;
    assert_eq!(res.status(), StatusCode::OK, "orch ui gets a code");
    let body = to_bytes(res.into_body(), 1024).await.unwrap();
    let code: serde_json::Value = serde_json::from_slice(&body).unwrap();
    let code = code["code"].as_str().unwrap().to_string();

    let res = visit(&app, &format!("/?code={code}")).await;
    assert_eq!(
        res.status(),
        StatusCode::SEE_OTHER,
        "the code is traded for a cookie"
    );
    let cookie = res
        .headers()
        .get(header::SET_COOKIE)
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default();
    assert!(cookie.starts_with("orch_ui=secret-token"), "{cookie}");
    assert!(
        cookie.contains("HttpOnly") && cookie.contains("SameSite=Strict"),
        "{cookie}"
    );

    let again = visit(&app, &format!("/?code={code}")).await;
    assert_eq!(
        again.status(),
        StatusCode::UNAUTHORIZED,
        "a spent code from history is refused"
    );
}

#[tokio::test]
async fn a_code_needs_the_token_and_the_token_no_longer_signs_in() {
    let app = app().await;
    assert_eq!(
        post_code(&app, "nope").await.status(),
        StatusCode::UNAUTHORIZED,
        "no token, no code"
    );
    let res = visit(&app, "/?token=secret-token").await;
    assert!(
        res.headers().get(header::SET_COOKIE).is_none(),
        "the token in a URL signs nobody in"
    );
}

#[tokio::test]
async fn the_page_carries_a_strict_content_security_policy() {
    let res = get("/", HOST, Some("orch_ui=secret-token")).await;
    assert_eq!(
        res.status(),
        StatusCode::OK,
        "the page loads with the cookie"
    );
    let csp = res
        .headers()
        .get(header::CONTENT_SECURITY_POLICY)
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default();
    assert!(csp.contains("default-src 'none'"), "{csp}");
}

#[test]
fn config_that_is_not_yaml_is_refused() {
    let problem = config_problem("", "projects: [unclosed");
    assert!(
        problem.is_some_and(|p| p.contains("not valid YAML")),
        "broken YAML must not replace the config"
    );
}

#[test]
fn a_project_config_must_keep_its_repo() {
    assert!(
        config_problem("o/r", "gh:\n  repo: o/other\n").is_some(),
        "a changed gh.repo would drop the project"
    );
    assert!(
        config_problem("o/r", "workflow: {}\n").is_some(),
        "a missing gh.repo would drop the project"
    );
    assert!(
        config_problem("o/r", "gh:\n  repo: o/r\n# a comment\n").is_none(),
        "a valid project config passes"
    );
}

#[test]
fn the_global_config_only_needs_to_parse() {
    assert!(
        config_problem("", "projects:\n  - ~/code/app\n").is_none(),
        "valid YAML passes"
    );
}

#[test]
fn the_version_follows_the_text() {
    assert_eq!(version_of("a"), version_of("a"), "same text, same version");
    assert_ne!(
        version_of("a"),
        version_of("b"),
        "an edit changes the version"
    );
}

async fn task_through(store: &TaskStore, path: &[TaskStatus]) -> i64 {
    let id = store
        .create_internal("o/r", "a task", "", "test", "ui", None)
        .await
        .unwrap();
    for status in path {
        store.update_status(id, *status).await.unwrap();
    }
    id
}

#[tokio::test]
async fn a_merged_task_shows_in_the_day_and_the_stats() {
    let store = TaskStore::open_memory().await.unwrap();
    let id = task_through(
        &store,
        &[TaskStatus::Routed, TaskStatus::InProgress, TaskStatus::Done],
    )
    .await;
    let done = store
        .entered_between(
            TaskStatus::Done,
            "2000-01-01T00:00:00Z",
            "2999-01-01T00:00:00Z",
            None,
        )
        .await
        .unwrap();
    assert_eq!(done.len(), 1, "the merge is in the range");
    assert_eq!(done[0].1.id, id, "it is the task that merged");

    let stats = store.stats_since("2000-01-01T00:00:00Z").await.unwrap();
    assert_eq!(stats.merged.len(), 1, "the stats count the merge once");

    let since = store.entered_status(&[id]).await.unwrap();
    assert!(
        since.contains_key(&id),
        "a done task knows when it got there"
    );
}

#[tokio::test]
async fn a_range_outside_the_merge_is_empty() {
    let store = TaskStore::open_memory().await.unwrap();
    task_through(&store, &[TaskStatus::Done]).await;
    let done = store
        .entered_between(
            TaskStatus::Done,
            "2000-01-01T00:00:00Z",
            "2000-01-02T00:00:00Z",
            None,
        )
        .await
        .unwrap();
    assert!(done.is_empty(), "nothing merged in 2000");
}

#[tokio::test]
async fn the_inbox_holds_blocked_and_human_tasks_only() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    task_through(&store, &[TaskStatus::Blocked]).await;
    task_through(&store, &[TaskStatus::Routed]).await;
    let open = store.list_open(None).await.unwrap();
    let waiting = cards_waiting(&store, open).await;
    assert_eq!(waiting.len(), 1, "only the blocked task waits on a person");
    assert_eq!(waiting[0].status, "blocked", "and it is the blocked one");
}

#[test]
fn a_local_day_becomes_store_instants() {
    assert_eq!(
        store_instant("2026-11-11T03:00:00.000Z").as_deref(),
        Some("2026-11-11T03:00:00Z"),
        "milliseconds drop so text comparison orders it"
    );
    assert_eq!(
        store_instant("2026-11-11T00:00:00-03:00").as_deref(),
        Some("2026-11-11T03:00:00Z"),
        "an offset becomes UTC"
    );
    assert!(
        store_instant("yesterday").is_none(),
        "anything else is refused"
    );
}

async fn get_with(path: &str, extra: &[(&str, &str)]) -> Response {
    let mut req = Request::get(path)
        .header(header::HOST, HOST)
        .header(header::COOKIE, "orch_ui=secret-token");
    for (k, v) in extra {
        req = req.header(*k, *v);
    }

    app()
        .await
        .oneshot(req.body(Body::empty()).unwrap())
        .await
        .unwrap()
}

#[tokio::test]
async fn a_page_on_another_local_port_is_refused() {
    let res = get_with("/api/tasks?repo=o/r", &[("sec-fetch-site", "same-site")]).await;
    assert_eq!(
        res.status(),
        StatusCode::UNAUTHORIZED,
        "another port of 127.0.0.1"
    );
    let res = get_with(
        "/api/tasks?repo=o/r",
        &[("origin", "http://127.0.0.1:3000")],
    )
    .await;
    assert_eq!(res.status(), StatusCode::UNAUTHORIZED, "a foreign origin");
}

#[tokio::test]
async fn the_ui_page_itself_passes() {
    let res = get_with(
        "/api/tasks?repo=o/r",
        &[
            ("sec-fetch-site", "same-origin"),
            ("origin", "http://127.0.0.1:43917"),
        ],
    )
    .await;
    assert_eq!(res.status(), StatusCode::OK, "its own page");
}

#[tokio::test]
async fn every_api_route_refuses_a_request_without_the_cookie() {
    let routes = [
        "/app.js",
        "/app.css",
        "/api/projects",
        "/api/tasks?repo=o/r",
        "/api/running",
        "/api/inbox",
        "/api/day?from=2026-01-01T00:00:00Z&to=2026-01-02T00:00:00Z",
        "/api/task?id=1",
        "/api/pr?id=1",
        "/api/usage",
        "/api/stats?days=7",
        "/api/config",
        "/api/chat?repo=o/r",
        "/api/plan?repo=o/r",
        "/api/agents",
        "/api/setup",
        "/api/setup/login",
        "/api/setup/repos",
        "/api/setup/owners",
        "/api/setup/dirs",
        "/api/events?repo=o/r",
        "/api/output?id=1",
        "/api/logs",
        "/api/activity",
    ];
    for route in routes {
        let res = get(route, HOST, None).await;
        assert_eq!(res.status(), StatusCode::UNAUTHORIZED, "{route}");
    }
}

#[test]
fn saving_a_symlinked_config_keeps_the_link() {
    let dir = tempfile::tempdir().unwrap();
    let real = dir.path().join("dotfiles-config.yml");
    let link = dir.path().join("config.yml");
    fs::write(&real, "a: 1\n").unwrap();
    symlink(&real, &link).unwrap();
    let backups = tempfile::tempdir().unwrap();
    save_config(&link, "a: 1\n", "a: 2\n", backups.path()).unwrap();
    assert!(
        fs::symlink_metadata(&link)
            .unwrap()
            .file_type()
            .is_symlink(),
        "the link stays"
    );
    assert_eq!(
        fs::read_to_string(&real).unwrap(),
        "a: 2\n",
        "the target has the edit"
    );
    assert!(
        !dir.path().join("config.yml.bak").exists(),
        "no backup beside the file"
    );
    assert_eq!(
        fs::read_dir(backups.path()).unwrap().count(),
        1,
        "the old text is kept in the state dir"
    );
}