| 1 | use crate::config::CommandWorkflowSummary; |
| 2 | use serde::Serialize; |
| 3 | |
| 4 | pub const WORKFLOW_DRAFT_INSTRUCTION_PREFIX: &str = "[codewhale.workflow-draft.v1]"; |
| 5 | pub const WORKFLOW_CONFIRM_INSTRUCTION_PREFIX: &str = "[codewhale.workflow-confirm.v1]"; |
| 6 | pub const WORKFLOW_OBJECTIVE_MAX_CHARS: usize = 1_000; |
| 7 | pub const WORKFLOW_DISPLAY_MAX_CHARS: usize = 160; |
| 8 | |
| 9 | #[derive(serde::Serialize, serde::Deserialize)] |
| 10 | pub struct WorkflowDraftEnvelope { |
| 11 | pub id: String, |
| 12 | pub objective: Option<String>, |
| 13 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 14 | pub source_path: Option<String>, |
| 15 | } |
| 16 | |
| 17 | pub fn envelope_from_instruction(prefix: &str, instruction: &str) -> Option<WorkflowDraftEnvelope> { |
| 18 | let first_line = instruction.lines().next()?; |
| 19 | let encoded = first_line.strip_prefix(prefix)?; |
| 20 | serde_json::from_str(encoded).ok() |
| 21 | } |
| 22 | |
| 23 | pub enum PendingWorkflowDraft { |
| 24 | Ready(WorkflowDraftEnvelope), |
| 25 | NoDraft, |
| 26 | NotReviewed, |
| 27 | } |
| 28 | |
| 29 | /// One workflow run as the human-facing `/workflow` command reads it: a |
| 30 | /// bounded projection of the run record with no raw event payloads and no |
| 31 | /// filesystem paths beyond the source file name. |
| 32 | #[derive(Debug, Clone, PartialEq, Eq, Serialize)] |
| 33 | pub struct HostWorkflowRunLine { |
| 34 | pub run_id: String, |
| 35 | /// Human-facing stage derived from the durable owner record and its typed |
| 36 | /// event tail: queued | running | waiting | completed | degraded | failed | |
| 37 | /// cancelled. This is presentation state, not a second lifecycle owner. |
| 38 | pub status: &'static str, |
| 39 | /// Whether the canonical owner record is still nonterminal. Controls key |
| 40 | /// off this bit instead of reverse-parsing the display stage. |
| 41 | pub active: bool, |
| 42 | /// The run's goal, its workflow id, or the source file name. |
| 43 | pub label: String, |
| 44 | pub started_at_ms: u64, |
| 45 | pub completed_at_ms: Option<u64>, |
| 46 | pub child_count: usize, |
| 47 | pub last_progress: Option<String>, |
| 48 | pub error: Option<String>, |
| 49 | } |
| 50 | |
| 51 | pub trait CommandWorkflowReviewContext { |
| 52 | fn pending_draft(&self) -> PendingWorkflowDraft; |
| 53 | } |
| 54 | |
| 55 | pub trait CommandWorkflowContext { |
| 56 | fn runs(&self) -> Vec<HostWorkflowRunLine>; |
| 57 | fn cancel(&mut self, run_id: &str) -> Result<HostWorkflowRunLine, String>; |
| 58 | fn settings(&self) -> CommandWorkflowSummary; |
| 59 | } |
| 60 | |
| 61 | pub struct AutoReviewReceipt { |
| 62 | pub plan_mode: bool, |
| 63 | pub broad_allows_paused: bool, |
| 64 | } |
| 65 | |
| 66 | pub trait CommandAutoReviewContext { |
| 67 | fn enable_auto_review(&mut self) -> Result<AutoReviewReceipt, String>; |
| 68 | } |
| 69 | |
| 70 | /// Render the `/config workflow` report from the typed effective facts. |
| 71 | pub fn render_workflow_summary(summary: CommandWorkflowSummary) -> String { |
| 72 | let on = |value: bool| if value { "on" } else { "off" }; |
| 73 | [ |
| 74 | "[workflow] — config.toml".to_string(), |
| 75 | format!( |
| 76 | "auto_start_read_only = {} · read-only plans start without an approval card", |
| 77 | on(summary.auto_start_read_only) |
| 78 | ), |
| 79 | format!( |
| 80 | "require_approval_for_writes = {} · plans that write, use shell/network, or elevate show an approval card first", |
| 81 | on(summary.require_approval_for_writes) |
| 82 | ), |
| 83 | format!( |
| 84 | "max_children = {} · max_concurrent = {} · max_depth = {} · hard ceilings for one run", |
| 85 | summary.max_children, summary.max_concurrent, summary.max_depth |
| 86 | ), |
| 87 | String::new(), |
| 88 | "[goal] — config.toml".to_string(), |
| 89 | format!( |
| 90 | "max_continuations = {} · automatic continuation passes before a goal pauses; 0 = unlimited (completion, blocked, or you stop it)", |
| 91 | summary.goal_max_continuations |
| 92 | ), |
| 93 | format!( |
| 94 | "enforce_token_budget = {} · true = a goal's token budget is a hard stop; false = advisory telemetry", |
| 95 | on(summary.goal_enforce_token_budget) |
| 96 | ), |
| 97 | ] |
| 98 | .join("\n") |
| 99 | } |
| 100 |