orch-cli 0.88.4

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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! GitHub Projects V2 integration — keeps project board columns in sync with task status.
//!
//! Uses GitHub's Projects V2 GraphQL API via the native `GhHttp` reqwest client.
//! All operations are best-effort: failures are logged but never block task execution.

use crate::backends::Status;
use crate::config;
use crate::github::http::GhHttp;
use std::collections::HashMap;

pub(crate) mod label_status;

/// Project board info returned by list queries.
#[derive(Debug, Clone)]
pub struct ProjectInfo {
    pub id: String,
    pub number: u64,
    pub title: String,
}

/// Keeps a GitHub Projects V2 board in sync with orch task statuses.
///
/// Config keys (in `~/.orch/config.yml`):
/// ```yaml
/// gh:
///   project_id: "PVT_kwHO..."
///   project_status_field_id: "PVTSSF_..."
///   project_status_map:
///     backlog: "option-id-1"
///     in_progress: "option-id-2"
///     review: "option-id-3"
///     done: "option-id-4"
/// ```
pub struct ProjectSync {
    project_id: String,
    status_field_id: String,
    status_map: HashMap<String, String>,
    /// GitHub Projects V2 "Estimate" number field ID (optional — not all projects have one).
    estimate_field_id: Option<String>,
    gh: GhHttp,
}

impl ProjectSync {
    /// Load from config. Returns `None` if project integration is not configured.
    pub fn from_config() -> Option<Self> {
        let project_id = config::get("gh.project_id").ok()?;
        if project_id.is_empty() {
            return None;
        }
        let status_field_id = config::get("gh.project_status_field_id").ok()?;

        let mut status_map = HashMap::new();
        for key in &["backlog", "in_progress", "review", "done"] {
            if let Ok(val) = config::get(&format!("gh.project_status_map.{key}")) {
                status_map.insert(key.to_string(), val);
            }
        }

        // Estimate field ID is optional — not all projects have an Estimate field.
        let estimate_field_id = config::get("gh.project_estimate_field_id")
            .ok()
            .filter(|v| !v.is_empty());

        let gh = match GhHttp::new() {
            Ok(gh) => gh,
            Err(e) => {
                tracing::warn!(error = %e, "project sync disabled: failed to build HTTP client");
                return None;
            }
        };

        Some(Self {
            project_id,
            status_field_id,
            status_map,
            estimate_field_id,
            gh,
        })
    }

    /// Discover the Status field ID and option IDs from a project.
    ///
    /// Queries the project's fields via GraphQL and finds the single-select
    /// "Status" field, returning a `ProjectSync` populated with field/option IDs.
    /// Also discovers the "Estimate" number field if present.
    pub async fn discover_fields(project_id: &str) -> anyhow::Result<Self> {
        let gh = GhHttp::new()?;
        // Fetch all field types so we can find both Status (single-select) and Estimate (number).
        let query = r#"query($projectId: ID!) { node(id: $projectId) { ... on ProjectV2 { fields(first: 100) { nodes { ... on ProjectV2SingleSelectField { id name options { id name } } ... on ProjectV2Field { id name } ... on ProjectV2FieldNumber { id name } } } } } }"#;

        let result = gh
            .graphql_with_vars(query, serde_json::json!({ "projectId": project_id }))
            .await?;

        let fields = result
            .pointer("/data/node/fields/nodes")
            .and_then(|n| n.as_array())
            .ok_or_else(|| anyhow::anyhow!("failed to parse project fields response"))?;

        // Find the "Status" field (case-insensitive single-select).
        let status_field = fields
            .iter()
            .find(|f| {
                f.get("name")
                    .and_then(|n| n.as_str())
                    .map(|n| n.eq_ignore_ascii_case("status"))
                    .unwrap_or(false)
            })
            .ok_or_else(|| anyhow::anyhow!("no 'Status' field found in project"))?;

        let field_id = status_field
            .get("id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Status field missing id"))?
            .to_string();

        let options = status_field
            .get("options")
            .and_then(|o| o.as_array())
            .ok_or_else(|| anyhow::anyhow!("Status field missing options"))?;

        // Map option names to their IDs, normalizing to our column keys.
        let mut status_map = HashMap::new();
        for opt in options {
            let opt_id = opt.get("id").and_then(|v| v.as_str()).unwrap_or("");
            let opt_name = opt.get("name").and_then(|v| v.as_str()).unwrap_or("");
            let lower = opt_name.to_lowercase();

            let key = if lower.contains("backlog") || lower.contains("todo") || lower == "new" {
                "backlog"
            } else if lower.contains("progress") || lower.contains("doing") {
                "in_progress"
            } else if lower.contains("review") {
                "review"
            } else if lower.contains("done") || lower.contains("complete") {
                "done"
            } else {
                continue;
            };

            status_map.insert(key.to_string(), opt_id.to_string());
        }

        // Find the "Estimate" number field (case-insensitive).
        let estimate_field_id = fields
            .iter()
            .find(|f| {
                f.get("name")
                    .and_then(|n| n.as_str())
                    .map(|n| n.eq_ignore_ascii_case("estimate"))
                    .unwrap_or(false)
            })
            .and_then(|f| f.get("id").and_then(|v| v.as_str()))
            .filter(|id| !id.is_empty())
            .map(String::from);

        Ok(Self {
            project_id: project_id.to_string(),
            status_field_id: field_id,
            status_map,
            estimate_field_id,
            gh,
        })
    }

    /// List all accessible projects for the authenticated user and their orgs.
    pub async fn list_projects() -> anyhow::Result<Vec<ProjectInfo>> {
        let gh = GhHttp::new()?;
        let mut projects = Vec::new();

        // Get current user login
        let user = gh.get_whoami().await?;

        // User projects
        let query = r#"query($login: String!) { user(login: $login) { projectsV2(first: 100) { nodes { id number title } } } }"#;
        if let Ok(result) = gh
            .graphql_with_vars(query, serde_json::json!({ "login": user }))
            .await
        {
            if let Some(nodes) = result
                .pointer("/data/user/projectsV2/nodes")
                .and_then(|n| n.as_array())
            {
                for node in nodes {
                    if let Some(info) = parse_project_node(node) {
                        projects.push(info);
                    }
                }
            }
        }

        // Try org projects for the repo owner (if different from user)
        if let Ok(repo) = config::get_current_repo() {
            if let Some(owner) = repo.split('/').next() {
                if owner != user {
                    let query = r#"query($login: String!) { organization(login: $login) { projectsV2(first: 100) { nodes { id number title } } } }"#;
                    if let Ok(result) = gh
                        .graphql_with_vars(query, serde_json::json!({ "login": owner }))
                        .await
                    {
                        if let Some(nodes) = result
                            .pointer("/data/organization/projectsV2/nodes")
                            .and_then(|n| n.as_array())
                        {
                            for node in nodes {
                                if let Some(info) = parse_project_node(node) {
                                    projects.push(info);
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(projects)
    }

    /// Add an issue to the project board. Returns the project item ID.
    pub async fn add_item(&self, issue_node_id: &str) -> anyhow::Result<String> {
        let query = r#"mutation($projectId: ID!, $contentId: ID!) { addProjectV2ItemById(input: { projectId: $projectId, contentId: $contentId }) { item { id } } }"#;

        let result = self
            .gh
            .graphql_with_vars(
                query,
                serde_json::json!({
                    "projectId": self.project_id,
                    "contentId": issue_node_id,
                }),
            )
            .await?;
        result
            .pointer("/data/addProjectV2ItemById/item/id")
            .and_then(|v| v.as_str())
            .map(String::from)
            .ok_or_else(|| anyhow::anyhow!("failed to add item to project"))
    }

    /// Update an item's status column on the project board.
    pub async fn update_item_status(&self, item_id: &str, status: &Status) -> anyhow::Result<()> {
        let column = Self::status_to_column(status);
        let option_id = self
            .status_map
            .get(column)
            .ok_or_else(|| anyhow::anyhow!("no option ID for column: {column}"))?;

        let query = r#"mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) { updateProjectV2ItemFieldValue(input: { projectId: $projectId, itemId: $itemId, fieldId: $fieldId, value: { singleSelectOptionId: $optionId } }) { projectV2Item { id } } }"#;

        self.gh
            .graphql_with_vars(
                query,
                serde_json::json!({
                    "projectId": self.project_id,
                    "itemId": item_id,
                    "fieldId": self.status_field_id,
                    "optionId": option_id,
                }),
            )
            .await?;
        Ok(())
    }

    /// Sync an issue's project board status: add to project if needed, then update column.
    ///
    /// This is the main entry point called from `update_status()`.
    pub async fn sync_item_status(
        &self,
        issue_node_id: &str,
        status: &Status,
    ) -> anyhow::Result<()> {
        // Add item to project (idempotent — returns existing item if already added)
        let item_id = self.add_item(issue_node_id).await?;

        // Update the status column
        self.update_item_status(&item_id, status).await
    }

    /// Map orch `Status` to a project board column key.
    pub fn status_to_column(status: &Status) -> &'static str {
        match status {
            Status::New | Status::Routed => "backlog",
            Status::InProgress | Status::Blocked => "in_progress",
            Status::InReview | Status::NeedsReview => "review",
            Status::Done => "done",
        }
    }

    /// Get the status field ID.
    pub fn status_field_id(&self) -> &str {
        &self.status_field_id
    }

    /// Get the status map (column key → option ID).
    pub fn status_map(&self) -> &HashMap<String, String> {
        &self.status_map
    }

    /// Get the estimate field ID, if configured.
    pub fn estimate_field_id(&self) -> Option<&str> {
        self.estimate_field_id.as_deref()
    }

    /// Get the project ID.
    pub fn project_id(&self) -> &str {
        &self.project_id
    }

    /// Update an item's Estimate number field on the project board.
    ///
    /// Returns the project item ID (either found or newly added).
    pub async fn sync_item_estimate(
        &self,
        issue_node_id: &str,
        estimate: u8,
    ) -> anyhow::Result<String> {
        let Some(field_id) = &self.estimate_field_id else {
            return Err(anyhow::anyhow!("estimate field not configured"));
        };

        // Add item to project (idempotent — returns existing item if already added).
        let item_id = self.add_item(issue_node_id).await?;

        // Update the estimate number field.
        let query = r#"mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: Float!) { updateProjectV2ItemFieldValue(input: { projectId: $projectId, itemId: $itemId, fieldId: $fieldId, value: { number: $value } }) { projectV2Item { id } } }"#;

        self.gh
            .graphql_with_vars(
                query,
                serde_json::json!({
                    "projectId": self.project_id,
                    "itemId": item_id,
                    "fieldId": field_id,
                    "value": f64::from(estimate),
                }),
            )
            .await?;

        Ok(item_id)
    }

    /// Batch-fetch estimate values for multiple project items by their content (issue) node IDs.
    ///
    /// Returns a map from content node ID → estimate value (0 if not set or field absent).
    /// Only queries the estimate field — the result is used to populate orch's estimate
    /// during task ingestion when `tasks.estimate` is still 0.
    ///
    /// GitHub's GraphQL API doesn't support querying items by arbitrary node IDs directly,
    /// so we fetch all project items and their field values, then match by content ID.
    /// This is acceptable for projects with reasonable item counts (< 1000).
    pub async fn get_estimates_for_issues(
        &self,
        issue_node_ids: &[String],
    ) -> anyhow::Result<std::collections::HashMap<String, u8>> {
        let Some(field_id) = &self.estimate_field_id else {
            return Ok(std::collections::HashMap::new());
        };

        let issue_set: std::collections::HashSet<&str> =
            issue_node_ids.iter().map(|s| s.as_str()).collect();

        // Fetch all items in the project with their fieldValues filtered to the estimate field.
        let query = r#"query($projectId: ID!) { node(id: $projectId) { ... on ProjectV2 { items(first: 100) { nodes { id content { ... on Issue { id } ... on PullRequest { id } } fieldValues(first: 10) { nodes { ... on ProjectV2ItemFieldNumberValue { field { id } number } } } } } } } }"#;

        let result = self
            .gh
            .graphql_with_vars(query, serde_json::json!({ "projectId": self.project_id }))
            .await?;

        let items = result
            .pointer("/data/node/items/nodes")
            .and_then(|n| n.as_array())
            .ok_or_else(|| anyhow::anyhow!("failed to parse project items"))?;

        let mut estimates = std::collections::HashMap::new();

        for item in items {
            // Extract the content node ID (issue or PR).
            let content = match item.get("content") {
                Some(c) => c,
                None => continue, // Item has no content (e.g. draft).
            };
            let content_id = match content.get("id").and_then(|v| v.as_str()) {
                Some(id) => id,
                None => continue,
            };

            // Skip if this content ID is not in our target set.
            if !issue_set.contains(content_id) {
                continue;
            }

            // Find the estimate field value.
            let field_values = match item
                .get("fieldValues")
                .and_then(|fv| fv.get("nodes"))
                .and_then(|n| n.as_array())
            {
                Some(fv) => fv,
                None => continue,
            };
            let estimate = field_values
                .iter()
                .find(|fv| {
                    fv.get("field")
                        .and_then(|f| f.get("id"))
                        .and_then(|id| id.as_str())
                        .is_some_and(|id| id == field_id)
                })
                .and_then(|fv| fv.get("number"))
                .and_then(|n| n.as_f64())
                .unwrap_or(0.0) as u8;

            // Only accept Fibonacci values (0 = not set, or 1/2/3/5/8/13/21).
            const FIBONACCI: &[u8] = &[0, 1, 2, 3, 5, 8, 13, 21];
            if FIBONACCI.contains(&estimate) {
                estimates.insert(content_id.to_string(), estimate);
            }
        }

        Ok(estimates)
    }
}

/// Parse a project node from GraphQL response.
fn parse_project_node(node: &serde_json::Value) -> Option<ProjectInfo> {
    Some(ProjectInfo {
        id: node.get("id")?.as_str()?.to_string(),
        number: node.get("number")?.as_u64()?,
        title: node.get("title")?.as_str()?.to_string(),
    })
}

/// Write project config fields to `~/.orch/config.yml`.
pub async fn write_project_config(sync: &ProjectSync) -> anyhow::Result<()> {
    let config_path = crate::home::config_path()?;
    let content = if config_path.exists() {
        tokio::fs::read_to_string(&config_path).await?
    } else {
        String::new()
    };

    // Parse existing YAML or start fresh
    let mut doc: serde_norway::Value = if content.is_empty() {
        serde_norway::Value::Mapping(serde_norway::Mapping::new())
    } else {
        serde_norway::from_str(&content)?
    };

    let root = doc
        .as_mapping_mut()
        .ok_or_else(|| anyhow::anyhow!("config is not a YAML mapping"))?;

    // Ensure gh section exists
    let gh_key = serde_norway::Value::String("gh".to_string());
    if !root.contains_key(&gh_key) {
        root.insert(
            gh_key.clone(),
            serde_norway::Value::Mapping(serde_norway::Mapping::new()),
        );
    }
    let gh = root
        .get_mut(&gh_key)
        .and_then(|v| v.as_mapping_mut())
        .ok_or_else(|| anyhow::anyhow!("gh config is not a mapping"))?;

    // Set project fields
    gh.insert(
        serde_norway::Value::String("project_id".to_string()),
        serde_norway::Value::String(sync.project_id.clone()),
    );
    gh.insert(
        serde_norway::Value::String("project_status_field_id".to_string()),
        serde_norway::Value::String(sync.status_field_id.clone()),
    );

    // Build status map
    let mut map = serde_norway::Mapping::new();
    for (key, val) in &sync.status_map {
        map.insert(
            serde_norway::Value::String(key.clone()),
            serde_norway::Value::String(val.clone()),
        );
    }
    gh.insert(
        serde_norway::Value::String("project_status_map".to_string()),
        serde_norway::Value::Mapping(map),
    );

    // Persist the estimate field ID (optional — not all projects have one).
    if let Some(ref field_id) = sync.estimate_field_id {
        gh.insert(
            serde_norway::Value::String("project_estimate_field_id".to_string()),
            serde_norway::Value::String(field_id.clone()),
        );
    }

    tokio::fs::write(&config_path, serde_norway::to_string(&doc)?).await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backends::Status;

    #[test]
    fn status_to_column_mapping() {
        assert_eq!(ProjectSync::status_to_column(&Status::New), "backlog");
        assert_eq!(ProjectSync::status_to_column(&Status::Routed), "backlog");
        assert_eq!(
            ProjectSync::status_to_column(&Status::InProgress),
            "in_progress"
        );
        assert_eq!(
            ProjectSync::status_to_column(&Status::Blocked),
            "in_progress"
        );
        assert_eq!(ProjectSync::status_to_column(&Status::InReview), "review");
        assert_eq!(
            ProjectSync::status_to_column(&Status::NeedsReview),
            "review"
        );
        assert_eq!(ProjectSync::status_to_column(&Status::Done), "done");
    }

    #[test]
    fn parse_project_node_valid() {
        let node = serde_json::json!({
            "id": "PVT_kwHOA123",
            "number": 42,
            "title": "My Project"
        });
        let info = parse_project_node(&node).unwrap();
        assert_eq!(info.id, "PVT_kwHOA123");
        assert_eq!(info.number, 42);
        assert_eq!(info.title, "My Project");
    }

    #[test]
    fn parse_project_node_missing_field() {
        let node = serde_json::json!({ "id": "PVT_kwHOA123" });
        assert!(parse_project_node(&node).is_none());
    }

    #[test]
    fn from_config_returns_none_when_not_configured() {
        // Isolate from both the repo's .orch.yml and the global ~/.orch/config.yml:
        // 1. Change to a temp dir so project config lookup finds no .orch.yml
        // 2. Point ORCH_HOME at a separate temp dir so the global config is not found
        // 3. Clear the config cache so no previously-cached values are reused
        let original_cwd = std::env::current_dir().unwrap();
        let prev_orch_home = std::env::var("ORCH_HOME").ok();

        let temp_cwd = tempfile::tempdir().unwrap();
        let temp_orch_home = tempfile::tempdir().unwrap();
        let orch_dir = temp_orch_home.path().join(".orch");
        std::fs::create_dir_all(&orch_dir).unwrap();

        std::env::set_current_dir(temp_cwd.path()).unwrap();
        std::env::set_var("ORCH_HOME", &orch_dir);
        config::clear_test_cache();

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            assert!(ProjectSync::from_config().is_none());
        }));

        std::env::set_current_dir(&original_cwd).unwrap();
        if let Some(prev) = prev_orch_home {
            std::env::set_var("ORCH_HOME", prev);
        } else {
            std::env::remove_var("ORCH_HOME");
        }
        result.unwrap();
    }

    #[test]
    fn get_estimates_filters_non_fibonacci_values() {
        // The get_estimates_for_issues function should only accept Fibonacci values.
        // Non-Fibonacci values should be silently dropped.
        // We test this by verifying the parsing logic directly.
        const FIBONACCI: &[u8] = &[0, 1, 2, 3, 5, 8, 13, 21];

        // Fibonacci values should be accepted
        for &v in FIBONACCI {
            assert!(
                FIBONACCI.contains(&v),
                "Fibonacci value {v} should be accepted"
            );
        }

        // Non-Fibonacci values should NOT be in the list
        let non_fib = [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 22];
        for &v in &non_fib {
            assert!(
                !FIBONACCI.contains(&v),
                "Non-Fibonacci value {v} should NOT be accepted"
            );
        }
    }

    #[test]
    fn estimate_field_id_is_optional() {
        // ProjectSync should be constructable with no estimate field.
        // estimate_field_id() should return None when not configured.
        let original_cwd = std::env::current_dir().unwrap();
        let prev_orch_home = std::env::var("ORCH_HOME").ok();

        let temp_cwd = tempfile::tempdir().unwrap();
        let temp_orch_home = tempfile::tempdir().unwrap();
        let orch_dir = temp_orch_home.path().join(".orch");
        std::fs::create_dir_all(&orch_dir).unwrap();

        std::env::set_current_dir(temp_cwd.path()).unwrap();
        std::env::set_var("ORCH_HOME", &orch_dir);
        config::clear_test_cache();

        // Write a config with project ID and status field but NO estimate field.
        let config_path = orch_dir.join("config.yml");
        std::fs::write(
            &config_path,
            r#"
gh:
  project_id: "PVT_kwHOAB123"
  project_status_field_id: "PVTSSF_123"
  project_status_map:
    backlog: "opt1"
"#,
        )
        .unwrap();

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let sync = ProjectSync::from_config();
            assert!(
                sync.is_some(),
                "should be Some when project_id is configured"
            );
            assert_eq!(
                sync.unwrap().estimate_field_id(),
                None,
                "estimate_field_id should be None when not configured"
            );
        }));

        std::env::set_current_dir(&original_cwd).unwrap();
        if let Some(prev) = prev_orch_home {
            std::env::set_var("ORCH_HOME", prev);
        } else {
            std::env::remove_var("ORCH_HOME");
        }
        result.unwrap();
    }
}