返回 CodeWhale
operate.rs
根目录 / crates / engine / src / operate.rs
1 //! Operate: always-on fleet operation matching landed CWC `OperateRecord`
2 //! (`Hmbown/cwc` `20de981`, PR #284).
3 //!
4 //! One schema for `cw · operate` and CWC `/operate`. Burn rate is optional
5 //! (`null` = unbounded). The lead plans before workers. Pace throttles or
6 //! widens; it never stops the operation. Supervisor over nested instances —
7 //! no second `Engine::run_turn`.
8
9 use std::fs;
10 use std::path::{Component, Path, PathBuf};
11 use std::process::{Command, Stdio};
12
13 use anyhow::{Context, Result, bail};
14 use chrono::Utc;
15 use serde::{Deserialize, Serialize};
16 use uuid::Uuid;
17
18 use crate::automation_manager::{AutomationManager, AutomationRecord, AutomationStatus};
19
20 pub const CWC_OPERATE_SCHEMA_VERSION: u32 = 1;
21 pub const OPERATE_MAX_WRITERS: usize = 3;
22 /// `hold` admits no new writers past this budget (the 8% band is met; hold
23 /// the current width instead of widening).
24 pub const OPERATE_HOLD_WRITERS: usize = 2;
25 /// `throttle` (observed more than 8% over target) cuts worker concurrency to
26 /// one writer. Pace throttles; it never stops the operation.
27 pub const OPERATE_THROTTLE_WRITERS: usize = 1;
28 pub const OPERATE_KEEPALIVE_ID: &str = "cw-operate";
29 /// Follow-up lead runs recur hourly; the first lead-plan step is kicked to
30 /// the next scheduler tick instead of waiting for the first recurrence.
31 pub const OPERATE_KEEPALIVE_RRULE: &str = "FREQ=HOURLY;INTERVAL=1";
32 pub const AUTO_MERGE_CHECKER_ENV: &str = "CODEWHALE_AUTO_MERGE_CHECKER";
33 pub const DIRECTION_PATH_ENV: &str = "CODEWHALE_DIRECTION_PATH";
34 pub const CHECK_AUTO_MERGE_SCRIPT: &str = "scripts/check-auto-merge.py";
35 pub const AUTO_MERGE_SCRIPT: &str = "scripts/auto_merge.py";
36 pub const AUTO_MERGE_PR_SCRIPT: &str = "scripts/auto-merge-pr.py";
37
38 const PACE_BAND: f64 = 0.08;
39
40 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
41 #[serde(rename_all = "camelCase")]
42 pub struct OperateBurnRate {
43 pub kind: String,
44 pub amount_usd_per_hour: f64,
45 }
46
47 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
48 #[serde(rename_all = "snake_case")]
49 pub enum OperateStatus {
50 Planning,
51 Running,
52 #[serde(rename = "idle_blocked")]
53 IdleBlocked,
54 Cancelled,
55 }
56
57 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
58 #[serde(rename_all = "snake_case")]
59 pub enum OperateIdleReason {
60 MissingCredentials,
61 AwaitingLeadPlan,
62 DirectionEmpty,
63 HumanGated,
64 }
65
66 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
67 #[serde(rename_all = "snake_case")]
68 pub enum OperatePace {
69 Unbounded,
70 Hold,
71 Throttle,
72 Widen,
73 }
74
75 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
76 #[serde(rename_all = "camelCase")]
77 pub struct OperateRosterMember {
78 pub id: String,
79 pub display_name: String,
80 pub role: String,
81 /// Saved selection for the lead; empty when no model has been assigned.
82 pub model: String,
83 pub state: String,
84 }
85
86 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87 #[serde(rename_all = "camelCase")]
88 pub struct OperatePlanSlice {
89 pub id: String,
90 pub title: String,
91 pub owner_id: String,
92 pub depends_on: Vec<String>,
93 pub est_cost_usd: f64,
94 pub start_offset_sec: u32,
95 pub duration_sec: u32,
96 }
97
98 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
99 pub struct OperateLeadPlan {
100 pub slices: Vec<OperatePlanSlice>,
101 }
102
103 /// Landed CWC `OperateRecord` (packages/contracts/src/operate.js @ 20de981).
104 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
105 #[serde(rename_all = "camelCase")]
106 pub struct Operation {
107 pub id: String,
108 pub schema_version: u32,
109 pub direction: String,
110 pub burn_rate: Option<OperateBurnRate>,
111 pub lead_operator: OperateRosterMember,
112 pub roster: Vec<OperateRosterMember>,
113 pub lead_plan: Option<OperateLeadPlan>,
114 pub status: OperateStatus,
115 pub idle_blocked_reason: Option<OperateIdleReason>,
116 pub pace: OperatePace,
117 pub writers_in_flight: usize,
118 pub workers_admitted: bool,
119 pub spent_usd: f64,
120 pub observed_burn_usd_per_hour: Option<f64>,
121 pub credentials_present: bool,
122 pub human_gated: bool,
123 pub human_gate: String,
124 pub created_at: String,
125 pub updated_at: String,
126 pub last_keep_alive_at: String,
127 #[serde(default)]
128 pub cancelled_at: String,
129 }
130
131 impl Operation {
132 /// Update display metadata from the saved selection without assigning a
133 /// route to planned workers or claiming that an executor is running.
134 pub fn set_lead_model(&mut self, model: &str) {
135 self.lead_operator.model = model.to_string();
136 for member in &mut self.roster {
137 if member.id == self.lead_operator.id {
138 member.model = model.to_string();
139 }
140 }
141 }
142
143 #[must_use]
144 pub fn new(direction: impl Into<String>, burn_usd_per_hour: Option<f64>) -> Self {
145 let now = Utc::now().to_rfc3339();
146 let lead = OperateRosterMember {
147 id: "lead".to_string(),
148 display_name: "Lead operator".to_string(),
149 role: "lead".to_string(),
150 model: String::new(),
151 state: "planning".to_string(),
152 };
153 let mut op = Self {
154 id: format!("op_{}", Uuid::new_v4()),
155 schema_version: CWC_OPERATE_SCHEMA_VERSION,
156 direction: normalize_direction(direction.into()),
157 burn_rate: normalize_burn_rate(burn_usd_per_hour),
158 lead_operator: lead.clone(),
159 roster: vec![lead],
160 lead_plan: None,
161 status: OperateStatus::Planning,
162 idle_blocked_reason: None,
163 pace: OperatePace::Unbounded,
164 writers_in_flight: 0,
165 workers_admitted: false,
166 spent_usd: 0.0,
167 observed_burn_usd_per_hour: None,
168 credentials_present: false,
169 human_gated: false,
170 human_gate: String::new(),
171 created_at: now.clone(),
172 updated_at: now.clone(),
173 last_keep_alive_at: now,
174 cancelled_at: String::new(),
175 };
176 op.project();
177 op
178 }
179
180 pub fn plan_from_direction(&mut self) {
181 self.lead_plan = slices_from_direction(&self.direction);
182 sync_plan_owners(self);
183 self.project();
184 }
185
186 pub fn project(&mut self) {
187 if self.status == OperateStatus::Cancelled {
188 self.idle_blocked_reason = None;
189 self.workers_admitted = false;
190 self.writers_in_flight = 0;
191 for member in &mut self.roster {
192 member.state = "idle".to_string();
193 }
194 return;
195 }
196 let (status, reason) = derive_status(self);
197 self.status = status;
198 self.idle_blocked_reason = reason;
199 self.workers_admitted = workers_admitted(self);
200 self.pace = derive_pace(self);
201 // Pace is a dispatch budget, not a label: the roster and the
202 // `writersInFlight` count the keepalive lead actually dispatches at
203 // come from `worker_dispatch_budget`, so over-target burn reduces
204 // real concurrency instead of only renaming it.
205 let budget = worker_dispatch_budget(self);
206 live_roster(self, budget);
207 self.writers_in_flight = if self.workers_admitted {
208 self.roster
209 .iter()
210 .filter(|member| member.role == "worker" && member.state == "in_flight")
211 .count()
212 } else {
213 0
214 };
215 if let Some(lead) = self.roster.iter().find(|member| member.role == "lead") {
216 self.lead_operator = lead.clone();
217 }
218 }
219 }
220
221 fn normalize_direction(value: String) -> String {
222 value.trim().chars().take(4000).collect()
223 }
224
225 fn normalize_burn_rate(amount: Option<f64>) -> Option<OperateBurnRate> {
226 parse_burn_amount(amount).ok().flatten()
227 }
228
229 /// CWC `normalizeOperateBurnRate`: number, `$/hr` object, or null.
230 pub fn parse_burn_rate(value: Option<&serde_json::Value>) -> Result<Option<OperateBurnRate>> {
231 let Some(value) = value else {
232 return Ok(None);
233 };
234 if value.is_null()
235 || value == &serde_json::Value::Bool(false)
236 || value.as_str().is_some_and(str::is_empty)
237 {
238 return Ok(None);
239 }
240 if let Some("unbounded") = value.get("kind").and_then(|kind| kind.as_str()) {
241 return Ok(None);
242 }
243 let amount = if value.is_number() || value.is_string() {
244 json_number(value)
245 } else {
246 json_number(
247 value
248 .get("amountUsdPerHour")
249 .or_else(|| value.get("usdPerHour"))
250 .or_else(|| value.get("amount"))
251 .unwrap_or(&serde_json::Value::Null),
252 )
253 };
254 if amount.is_none() {
255 anyhow::bail!("Burn rate is optional. When set, it must be a positive $/hr.");
256 }
257 parse_burn_amount(amount)
258 }
259
260 fn json_number(value: &serde_json::Value) -> Option<f64> {
261 value
262 .as_f64()
263 .or_else(|| value.as_i64().map(|n| n as f64))
264 .or_else(|| value.as_str().and_then(|s| s.parse().ok()))
265 }
266
267 fn parse_burn_amount(amount: Option<f64>) -> Result<Option<OperateBurnRate>> {
268 let Some(amount) = amount else {
269 return Ok(None);
270 };
271 if !amount.is_finite() || amount <= 0.0 {
272 anyhow::bail!("Burn rate is optional. When set, it must be a positive $/hr.");
273 }
274 if amount > 10_000.0 {
275 anyhow::bail!("Burn rate must be 10000 $/hr or less.");
276 }
277 let rounded = (amount * 100.0).round() / 100.0;
278 if rounded <= 0.0 {
279 // A sub-cent rate rounds to a $0/hr target, which the pace governor
280 // would treat as unbounded — reject it instead of silently dropping
281 // the requested cap.
282 anyhow::bail!("Burn rate must be at least $0.01/hr.");
283 }
284 Ok(Some(OperateBurnRate {
285 kind: "usd_per_hour".to_string(),
286 amount_usd_per_hour: rounded,
287 }))
288 }
289
290 fn derive_status(op: &Operation) -> (OperateStatus, Option<OperateIdleReason>) {
291 if op.status == OperateStatus::Cancelled {
292 return (OperateStatus::Cancelled, None);
293 }
294 if !op.credentials_present {
295 return (
296 OperateStatus::IdleBlocked,
297 Some(OperateIdleReason::MissingCredentials),
298 );
299 }
300 if op.direction.is_empty() {
301 return (
302 OperateStatus::IdleBlocked,
303 Some(OperateIdleReason::DirectionEmpty),
304 );
305 }
306 if op.human_gated {
307 return (
308 OperateStatus::IdleBlocked,
309 Some(OperateIdleReason::HumanGated),
310 );
311 }
312 if op
313 .lead_plan
314 .as_ref()
315 .is_none_or(|plan| plan.slices.is_empty())
316 {
317 return (
318 OperateStatus::IdleBlocked,
319 Some(OperateIdleReason::AwaitingLeadPlan),
320 );
321 }
322 (OperateStatus::Running, None)
323 }
324
325 fn workers_admitted(op: &Operation) -> bool {
326 op.status != OperateStatus::Cancelled
327 && op.credentials_present
328 && !op.direction.is_empty()
329 && op
330 .lead_plan
331 .as_ref()
332 .is_some_and(|plan| !plan.slices.is_empty())
333 && !op.human_gated
334 }
335
336 fn derive_pace(op: &Operation) -> OperatePace {
337 let Some(rate) = &op.burn_rate else {
338 return OperatePace::Unbounded;
339 };
340 let Some(observed) = op.observed_burn_usd_per_hour.filter(|value| *value > 0.0) else {
341 return OperatePace::Widen;
342 };
343 let target = rate.amount_usd_per_hour;
344 if target <= 0.0 {
345 return OperatePace::Unbounded;
346 }
347 let delta = (observed - target) / target;
348 if delta > PACE_BAND {
349 OperatePace::Throttle
350 } else if delta < -PACE_BAND {
351 OperatePace::Widen
352 } else {
353 OperatePace::Hold
354 }
355 }
356
357 /// Pace-driven worker-dispatch budget within the 8% band semantics.
358 ///
359 /// `widen`/`unbounded` opens the full writer width, `hold` admits no new
360 /// writers past the hold width, and `throttle` cuts concurrency to one
361 /// writer. While workers are admitted the budget is never zero — pace
362 /// throttles spend, it never stops the operation.
363 #[must_use]
364 pub fn worker_dispatch_budget(op: &Operation) -> usize {
365 if !op.workers_admitted {
366 return 0;
367 }
368 match op.pace {
369 OperatePace::Unbounded | OperatePace::Widen => OPERATE_MAX_WRITERS,
370 OperatePace::Hold => OPERATE_HOLD_WRITERS,
371 OperatePace::Throttle => OPERATE_THROTTLE_WRITERS,
372 }
373 }
374
375 fn live_roster(op: &mut Operation, budget: usize) {
376 let admitted = op.workers_admitted;
377 let cancelled = op.status == OperateStatus::Cancelled;
378 let has_plan = op
379 .lead_plan
380 .as_ref()
381 .is_some_and(|plan| !plan.slices.is_empty());
382 // Only the first `budget` workers in roster (plan) order dispatch; the
383 // rest stay idle until a slot frees or pace widens.
384 let mut worker_slot = 0usize;
385 for member in &mut op.roster {
386 if cancelled {
387 member.state = "idle".to_string();
388 } else if !op.credentials_present {
389 member.state = "blocked".to_string();
390 } else if member.role == "lead" && !has_plan {
391 member.state = "planning".to_string();
392 } else if !admitted {
393 member.state = if member.role == "lead" {
394 "planning".to_string()
395 } else {
396 "idle".to_string()
397 };
398 } else if member.role == "worker" {
399 worker_slot += 1;
400 member.state = if worker_slot <= budget {
401 "in_flight".to_string()
402 } else {
403 "idle".to_string()
404 };
405 } else {
406 member.state = "planning".to_string();
407 }
408 }
409 }
410
411 fn slices_from_direction(direction: &str) -> Option<OperateLeadPlan> {
412 let items = direction_items(direction);
413 if items.is_empty() {
414 return None;
415 }
416 let mut cursor = 0u32;
417 let slices = items
418 .into_iter()
419 .enumerate()
420 .map(|(index, title)| {
421 let duration_sec = 1800;
422 let slice = OperatePlanSlice {
423 id: format!("slice-{}", index + 1),
424 title,
425 owner_id: if index == 0 {
426 "lead".to_string()
427 } else {
428 format!("worker-{index}")
429 },
430 depends_on: if index == 0 {
431 Vec::new()
432 } else {
433 vec![format!("slice-{index}")]
434 },
435 est_cost_usd: 0.25,
436 start_offset_sec: cursor,
437 duration_sec,
438 };
439 cursor = cursor.saturating_add(duration_sec);
440 slice
441 })
442 .collect();
443 Some(OperateLeadPlan { slices })
444 }
445
446 fn direction_items(direction: &str) -> Vec<String> {
447 let mut items = Vec::new();
448 for raw in direction.lines() {
449 let line = raw.trim();
450 if line.is_empty() {
451 continue;
452 }
453 let item = line
454 .trim_start_matches(|c: char| {
455 c.is_ascii_digit() || c == '.' || c == ')' || c == '-' || c == '*' || c == '#'
456 })
457 .trim();
458 if !item.is_empty() {
459 items.push(item.to_string());
460 }
461 }
462 if items.is_empty() && !direction.trim().is_empty() {
463 items.push(direction.trim().to_string());
464 }
465 items
466 }
467
468 #[must_use]
469 pub fn render_plan_board(op: &Operation) -> String {
470 render_plan_board_locale(op, codewhale_localization::Locale::En)
471 }
472
473 /// Plan board with locale-aware chrome. Contract tokens (status / pace enum
474 /// values, slice ids, owner ids) stay verbatim; the surrounding prose comes
475 /// from the TUI locale packs.
476 #[must_use]
477 pub fn render_plan_board_locale(op: &Operation, locale: codewhale_localization::Locale) -> String {
478 use codewhale_localization::{MessageId, tr};
479 let tr_line = |id: MessageId| tr(locale, id).into_owned();
480
481 let mut out = String::new();
482 out.push_str(
483 &tr_line(MessageId::OperateBoardHeader)
484 .replace("{id}", &op.id)
485 .replace("{status}", &status_label(op))
486 .replace("{pace}", pace_label(op.pace))
487 .replace("{writers}", &op.writers_in_flight.to_string()),
488 );
489 out.push('\n');
490 match &op.burn_rate {
491 Some(rate) => out.push_str(
492 &tr_line(MessageId::OperateBoardBurnObserved)
493 .replace(
494 "{actual}",
495 &format!("{}", op.observed_burn_usd_per_hour.unwrap_or(0.0)),
496 )
497 .replace("{target}", &format!("{}", rate.amount_usd_per_hour)),
498 ),
499 None => out.push_str(&tr_line(MessageId::OperateBoardBurnNoCap)),
500 }
501 out.push('\n');
502 if op.direction.is_empty() {
503 out.push_str(&tr_line(MessageId::OperateBoardDirectionEmpty));
504 } else {
505 out.push_str(
506 &tr_line(MessageId::OperateBoardDirectionLine)
507 .replace("{line}", op.direction.lines().next().unwrap_or("")),
508 );
509 }
510 out.push('\n');
511 let Some(plan) = &op.lead_plan else {
512 out.push_str(&tr_line(MessageId::OperateBoardPlanMissing));
513 out.push('\n');
514 return out;
515 };
516 out.push_str(&tr_line(MessageId::OperateBoardPlanHeader));
517 out.push('\n');
518 for slice in &plan.slices {
519 out.push_str(&format!(
520 " {:<9} {:<8} {:>5} {:>5} {:>6.2} {:<7} {}\n",
521 slice.id,
522 slice.owner_id,
523 slice.start_offset_sec,
524 slice.duration_sec,
525 slice.est_cost_usd,
526 if slice.depends_on.is_empty() {
527 "-".to_string()
528 } else {
529 slice.depends_on.join(",")
530 },
531 slice.title
532 ));
533 }
534 out.push_str(&render_timeline(&plan.slices, locale));
535 out
536 }
537
538 fn render_timeline(slices: &[OperatePlanSlice], locale: codewhale_localization::Locale) -> String {
539 use codewhale_localization::{MessageId, tr};
540 let max_end = slices
541 .iter()
542 .map(|slice| slice.start_offset_sec.saturating_add(slice.duration_sec))
543 .max()
544 .unwrap_or(0)
545 .max(1);
546 let width = 24u32;
547 let mut out = format!("{}\n", tr(locale, MessageId::OperateBoardGantt));
548 for slice in slices {
549 let start = (slice.start_offset_sec.saturating_mul(width)) / max_end;
550 let end = (slice
551 .start_offset_sec
552 .saturating_add(slice.duration_sec)
553 .saturating_mul(width))
554 / max_end;
555 let end = end.max(start.saturating_add(1)).min(width);
556 let mut bar = vec!['.'; width as usize];
557 for idx in start..end {
558 if let Some(cell) = bar.get_mut(idx as usize) {
559 *cell = '#';
560 }
561 }
562 out.push_str(&format!(
563 " {:<9} {}\n",
564 slice.id,
565 bar.into_iter().collect::<String>()
566 ));
567 }
568 out
569 }
570
571 fn status_label(op: &Operation) -> String {
572 match (op.status, op.idle_blocked_reason) {
573 (OperateStatus::Cancelled, _) => "cancelled".to_string(),
574 (OperateStatus::Running, _) => "running".to_string(),
575 (OperateStatus::Planning, _) => "planning".to_string(),
576 (_, Some(OperateIdleReason::DirectionEmpty)) => "idle_blocked: direction_empty".to_string(),
577 (_, Some(OperateIdleReason::AwaitingLeadPlan)) => {
578 "idle_blocked: awaiting_lead_plan".to_string()
579 }
580 (_, Some(OperateIdleReason::MissingCredentials)) => {
581 "idle_blocked: missing_credentials".to_string()
582 }
583 (_, Some(OperateIdleReason::HumanGated)) => {
584 format!("idle_blocked: human_gated {}", op.human_gate)
585 }
586 (OperateStatus::IdleBlocked, None) => "idle_blocked".to_string(),
587 }
588 }
589
590 fn pace_label(pace: OperatePace) -> &'static str {
591 match pace {
592 OperatePace::Unbounded => "unbounded",
593 OperatePace::Hold => "hold",
594 OperatePace::Throttle => "throttle",
595 OperatePace::Widen => "widen",
596 }
597 }
598
599 /// Honor an explicit operator-provided path (env override) only when it is a
600 /// non-empty value without NUL bytes or `..` traversal segments that actually
601 /// names one regular file. Keeps env-provided values out of raw path
602 /// expressions (CodeQL "uncontrolled data in path" class).
603 fn explicit_file_path(raw: &str) -> Option<PathBuf> {
604 let trimmed = raw.trim();
605 if trimmed.is_empty() || trimmed.contains('\0') {
606 return None;
607 }
608 let path = PathBuf::from(trimmed);
609 if path
610 .components()
611 .any(|component| matches!(component, Component::ParentDir))
612 {
613 return None;
614 }
615 path.is_file().then_some(path)
616 }
617
618 /// Same hardening for an explicit directory (env override): no NUL bytes, no
619 /// `..` traversal segments. Existence is probed by the caller.
620 fn explicit_dir_path(raw: &str) -> Option<PathBuf> {
621 let trimmed = raw.trim();
622 if trimmed.is_empty() || trimmed.contains('\0') {
623 return None;
624 }
625 let path = PathBuf::from(trimmed);
626 if path
627 .components()
628 .any(|component| matches!(component, Component::ParentDir))
629 {
630 return None;
631 }
632 Some(path)
633 }
634
635 #[must_use]
636 pub fn discover_direction_path(workspace: &Path) -> Option<PathBuf> {
637 if let Ok(explicit) = std::env::var(DIRECTION_PATH_ENV)
638 && let Some(path) = explicit_file_path(&explicit)
639 {
640 return Some(path);
641 }
642 let local = workspace.join("DIRECTION.md");
643 if local.is_file() {
644 return Some(local);
645 }
646 materialize_ops_origin_main()
647 .ok()
648 .map(|root| root.join("DIRECTION.md"))
649 .filter(|path| path.is_file())
650 }
651
652 pub fn read_direction(workspace: &Path) -> Result<String> {
653 match discover_direction_path(workspace) {
654 Some(path) => fs::read_to_string(&path)
655 .with_context(|| format!("Failed to read direction {}", path.display())),
656 None => Ok(String::new()),
657 }
658 }
659
660 /// Resolve the saved keepalive pin, or the caller's effective session route
661 /// for a fresh/unpinned record. Auto stays a policy until Runtime admits a turn;
662 /// resolving its inventory here could run a paid classifier.
663 fn keepalive_route(
664 config: &codewhale_runtime::config::Config,
665 current: Option<&AutomationRecord>,
666 selection: Option<(&codewhale_runtime::config::ProviderIdentity, &str)>,
667 ) -> Result<(codewhale_runtime::config::ProviderIdentity, String, bool)> {
668 let (identity, model) = if let Some(record) = current
669 .filter(|record| record.model_provider.is_some() || record.model_provider_id.is_some())
670 {
671 let identity = config
672 .resolve_persisted_provider_identity(
673 record.model_provider.as_deref(),
674 record.model_provider_id.as_deref(),
675 )
676 .map_err(anyhow::Error::msg)?;
677 let model = record
678 .model
679 .as_deref()
680 .map(str::trim)
681 .filter(|model| !model.is_empty())
682 .context("Pinned Operate keepalive has no model; repair its saved route")?;
683 (identity, model.to_string())
684 } else if let Some((identity, model)) = selection {
685 let identity = config
686 .resolve_persisted_provider_identity(
687 Some(identity.provider.as_str()),
688 identity.persisted_id(),
689 )
690 .map_err(anyhow::Error::msg)?;
691 (identity, model.to_string())
692 } else {
693 (
694 config
695 .active_provider_identity()
696 .map_err(anyhow::Error::msg)?,
697 config.default_model(),
698 )
699 };
700 if model.trim().eq_ignore_ascii_case("auto") {
701 let mut scoped = config.clone();
702 scoped
703 .scope_to_provider_identity(&identity)
704 .map_err(anyhow::Error::msg)?;
705 let credentials = crate::provider_readiness::has_api_key_for(&scoped, &identity);
706 return Ok((identity, "auto".to_string(), credentials));
707 }
708 let route =
709 crate::route_runtime::resolve_runtime_route_for_identity(config, &identity, Some(&model))
710 .map_err(anyhow::Error::msg)?;
711 let credentials = crate::provider_readiness::has_api_key_for(&route.config, &route.identity);
712 Ok((route.identity, route.model, credentials))
713 }
714
715 /// Inspect the same saved route used by the scheduler. Credential presence is
716 /// a local readiness observation, not proof of provider execution.
717 pub fn keepalive_readiness(
718 manager: &AutomationManager,
719 config: &codewhale_runtime::config::Config,
720 selection: Option<(&codewhale_runtime::config::ProviderIdentity, &str)>,
721 ) -> Result<(String, bool)> {
722 let mut readiness = (String::new(), false);
723 manager.edit_automation(OPERATE_KEEPALIVE_ID, |current| {
724 if current
725 .as_ref()
726 .and_then(|record| record.execution_scope.as_deref())
727 .is_some_and(|scope| Some(scope) != manager.execution_scope())
728 {
729 bail!("Operate keepalive belongs to another Runtime execution scope");
730 }
731 let (_, model, credentials) = keepalive_route(config, current.as_ref(), selection)?;
732 readiness = (model, credentials);
733 Ok(None)
734 })?;
735 Ok(readiness)
736 }
737
738 #[derive(Debug, Clone, PartialEq, Eq)]
739 pub struct AutoMergeRequest<'a> {
740 pub pr: &'a str,
741 pub role: &'a str,
742 pub repo: &'a str,
743 }
744
745 #[derive(Debug, Clone, PartialEq, Eq)]
746 pub enum AutoMergeDecision {
747 Allow,
748 Deny { reason: String },
749 }
750
751 #[must_use]
752 pub fn check_auto_merge_args(repo: &str, pr: &str, agent: &str) -> Vec<String> {
753 vec![
754 CHECK_AUTO_MERGE_SCRIPT.to_string(),
755 "--repo".to_string(),
756 repo.to_string(),
757 "--pr".to_string(),
758 pr.to_string(),
759 "--agent".to_string(),
760 agent.to_string(),
761 ]
762 }
763
764 #[must_use]
765 pub fn auto_merge_pr_args(repo: &str, pr: &str, agent: &str) -> Vec<String> {
766 vec![
767 AUTO_MERGE_PR_SCRIPT.to_string(),
768 "--repo".to_string(),
769 repo.to_string(),
770 "--pr".to_string(),
771 pr.to_string(),
772 "--agent".to_string(),
773 agent.to_string(),
774 ]
775 }
776
777 /// Strictly validate an auto-merge request before any of it reaches argv.
778 ///
779 /// The checker is spawned without a shell, but these values still become
780 /// arguments to `python3` and then to `gh`, so they are held to the shapes
781 /// GitHub itself allows: `repo` is `owner/name`, `pr` is a positive decimal
782 /// number, and `agent` is a short role token. No value may start with `-`.
783 pub fn validate_auto_merge_request(request: &AutoMergeRequest<'_>) -> Result<(), String> {
784 fn is_owner(owner: &str) -> bool {
785 (1..=39).contains(&owner.len())
786 && !owner.starts_with('-')
787 && owner
788 .bytes()
789 .all(|b| b.is_ascii_alphanumeric() || b == b'-')
790 }
791 fn is_repo_name(name: &str) -> bool {
792 (1..=100).contains(&name.len())
793 && name != "."
794 && name != ".."
795 && !name.starts_with('-')
796 && name
797 .bytes()
798 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
799 }
800 let repo_ok = request
801 .repo
802 .split_once('/')
803 .is_some_and(|(owner, name)| is_owner(owner) && is_repo_name(name));
804 if !repo_ok {
805 return Err("repo must be `owner/name` using GitHub name characters".to_string());
806 }
807 let pr_ok = (1..=10).contains(&request.pr.len())
808 && request.pr.bytes().all(|b| b.is_ascii_digit())
809 && request.pr.parse::<u64>().is_ok_and(|n| n > 0);
810 if !pr_ok {
811 return Err("pr must be a positive pull request number".to_string());
812 }
813 let agent_ok = (1..=64).contains(&request.role.len())
814 && !request.role.starts_with('-')
815 && request
816 .role
817 .bytes()
818 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_'));
819 if !agent_ok {
820 return Err("agent must be 1-64 letters, digits, `-` or `_`".to_string());
821 }
822 Ok(())
823 }
824
825 pub fn evaluate_auto_merge(
826 request: AutoMergeRequest<'_>,
827 checker: Option<&Path>,
828 ) -> AutoMergeDecision {
829 if let Err(reason) = validate_auto_merge_request(&request) {
830 return AutoMergeDecision::Deny { reason };
831 }
832 let Some(checker) = checker else {
833 return AutoMergeDecision::Deny {
834 reason: "auto-merge checker missing; fail-closed".to_string(),
835 };
836 };
837 if !checker.exists() {
838 return AutoMergeDecision::Deny {
839 reason: "auto-merge checker missing; fail-closed".to_string(),
840 };
841 }
842 match Command::new("python3")
843 .arg(checker)
844 .arg("--repo")
845 .arg(request.repo)
846 .arg("--pr")
847 .arg(request.pr)
848 .arg("--agent")
849 .arg(request.role)
850 .stdin(Stdio::null())
851 .stdout(Stdio::null())
852 .stderr(Stdio::null())
853 .status()
854 {
855 Ok(status) if status.success() => AutoMergeDecision::Allow,
856 Ok(_) => AutoMergeDecision::Deny {
857 reason: "auto-merge checker refused".to_string(),
858 },
859 Err(error) => AutoMergeDecision::Deny {
860 reason: format!("auto-merge checker failed to start: {error}"),
861 },
862 }
863 }
864
865 #[must_use]
866 pub fn discover_auto_merge_checker(_workspace: &Path) -> Option<PathBuf> {
867 if let Ok(explicit) = std::env::var(AUTO_MERGE_CHECKER_ENV)
868 && let Some(path) = explicit_file_path(&explicit)
869 {
870 return Some(path);
871 }
872 materialize_ops_origin_main()
873 .ok()
874 .map(|root| root.join(CHECK_AUTO_MERGE_SCRIPT))
875 .filter(|path| path.is_file())
876 }
877
878 fn ops_git_candidates() -> Vec<PathBuf> {
879 let mut out = Vec::new();
880 for key in ["CODEWHALE_OPS_GIT", "CODEWHALE_OPS_ROOT"] {
881 if let Ok(path) = std::env::var(key)
882 && let Some(path) = explicit_dir_path(&path)
883 {
884 out.push(path);
885 }
886 }
887 out
888 }
889
890 fn git_origin_main_sha(repo: &Path) -> Option<String> {
891 let output = codewhale_dependencies::Git::review_base(repo)
892 .ok()?
893 .args(["rev-parse", "origin/main"])
894 .output()
895 .ok()?;
896 if !output.status.success() {
897 return None;
898 }
899 let sha = String::from_utf8_lossy(&output.stdout).trim().to_string();
900 // The sha becomes a path segment below; only plain hex of a plausible
901 // length may flow into it.
902 if !(7..=64).contains(&sha.len()) || !sha.chars().all(|c| c.is_ascii_hexdigit()) {
903 return None;
904 }
905 Some(sha)
906 }
907
908 pub fn materialize_ops_origin_main() -> Result<PathBuf> {
909 let repo = ops_git_candidates()
910 .into_iter()
911 .find(|path| git_origin_main_sha(path).is_some())
912 .context("no codewhale-ops git checkout with origin/main")?;
913 let sha = git_origin_main_sha(&repo).context("origin/main sha")?;
914 let dest = default_operate_dir()
915 .parent()
916 .unwrap_or(Path::new("."))
917 .join("ops-main")
918 .join(&sha[..12.min(sha.len())]);
919 let marker = dest.join(CHECK_AUTO_MERGE_SCRIPT);
920 if marker.is_file()
921 && dest.join("DIRECTION.md").is_file()
922 && dest.join(AUTO_MERGE_SCRIPT).is_file()
923 {
924 return Ok(dest);
925 }
926 fs::create_dir_all(&dest).with_context(|| format!("Failed to create {}", dest.display()))?;
927 let archive = codewhale_dependencies::Git::workspace_command(&repo)?
928 .args([
929 "archive",
930 "origin/main",
931 "--",
932 "DIRECTION.md",
933 CHECK_AUTO_MERGE_SCRIPT,
934 AUTO_MERGE_SCRIPT,
935 AUTO_MERGE_PR_SCRIPT,
936 "agent-workstreams/AUTO_MERGE.toml",
937 ])
938 .output()
939 .context("git archive origin/main")?;
940 if !archive.status.success() {
941 anyhow::bail!(
942 "git archive origin/main failed: {}",
943 String::from_utf8_lossy(&archive.stderr).trim()
944 );
945 }
946 let mut child = Command::new("tar")
947 .arg("-x")
948 .arg("-C")
949 .arg(&dest)
950 .stdin(Stdio::piped())
951 .stdout(Stdio::null())
952 .stderr(Stdio::piped())
953 .spawn()
954 .context("tar extract ops origin/main")?;
955 if let Some(mut stdin) = child.stdin.take() {
956 use std::io::Write;
957 stdin.write_all(&archive.stdout)?;
958 }
959 let status = child.wait()?;
960 if !status.success() {
961 anyhow::bail!("failed to extract ops origin/main archive");
962 }
963 if !marker.is_file() {
964 anyhow::bail!("ops origin/main archive missing {CHECK_AUTO_MERGE_SCRIPT}");
965 }
966 Ok(dest)
967 }
968
969 pub fn default_operate_dir() -> PathBuf {
970 if let Ok(path) = std::env::var("CODEWHALE_OPERATE_DIR") {
971 let trimmed = path.trim();
972 if !trimmed.is_empty() {
973 return PathBuf::from(trimmed);
974 }
975 }
976 crate::automation_manager::default_automations_dir()
977 .parent()
978 .map(|parent| parent.join("operate"))
979 .unwrap_or_else(|| PathBuf::from("operate"))
980 }
981
982 pub struct OperationStore {
983 path: PathBuf,
984 lock_path: PathBuf,
985 }
986
987 impl OperationStore {
988 pub fn open(dir: impl Into<PathBuf>) -> Result<Self> {
989 let dir = dir.into();
990 fs::create_dir_all(&dir).with_context(|| format!("Failed to create {}", dir.display()))?;
991 let path = dir.join("current.json");
992 let lock_path = dir.join("current.json.lock");
993 Ok(Self { path, lock_path })
994 }
995
996 pub fn load(&self) -> Result<Option<Operation>> {
997 // Match the skill-state discipline: pure readers take the shared
998 // cross-process read lock only when one exists, so a read never
999 // fabricates a lock file.
1000 if self.lock_path.exists() {
1001 let file = fs::File::open(&self.lock_path)
1002 .with_context(|| format!("Failed to open {}", self.lock_path.display()))?;
1003 let lock = fd_lock::RwLock::new(file);
1004 let _guard = lock
1005 .read()
1006 .with_context(|| format!("read-lock {}", self.path.display()))?;
1007 return self.load_unlocked();
1008 }
1009 self.load_unlocked()
1010 }
1011
1012 fn load_unlocked(&self) -> Result<Option<Operation>> {
1013 if !self.path.exists() {
1014 return Ok(None);
1015 }
1016 let raw = fs::read_to_string(&self.path)
1017 .with_context(|| format!("Failed to read {}", self.path.display()))?;
1018 let op: Operation = serde_json::from_str(&raw)
1019 .with_context(|| format!("Failed to parse {}", self.path.display()))?;
1020 Ok(Some(op))
1021 }
1022
1023 /// Save under the cross-process writer lock with an atomic temp+rename
1024 /// write, so concurrent Codewhale processes never interleave partial
1025 /// records.
1026 pub fn save(&self, op: &Operation) -> Result<()> {
1027 let file = self.open_lock_file()?;
1028 let mut lock = fd_lock::RwLock::new(file);
1029 let _guard = lock
1030 .write()
1031 .with_context(|| format!("write-lock {}", self.path.display()))?;
1032 self.save_unlocked(op)
1033 }
1034
1035 /// Read-merge-write under the cross-process writer lock: the latest
1036 /// on-disk record is reloaded *inside* the lock before `edit` runs, so a
1037 /// concurrent PATCH/keepalive/plan save can no longer be silently lost by
1038 /// a stale read. Returns `None` when no operation is recorded yet.
1039 pub fn mutate(
1040 &self,
1041 edit: impl FnOnce(&mut Operation) -> Result<()>,
1042 ) -> Result<Option<Operation>> {
1043 let file = self.open_lock_file()?;
1044 let mut lock = fd_lock::RwLock::new(file);
1045 let _guard = lock
1046 .write()
1047 .with_context(|| format!("write-lock {}", self.path.display()))?;
1048 let Some(mut op) = self.load_unlocked()? else {
1049 return Ok(None);
1050 };
1051 edit(&mut op)?;
1052 self.save_unlocked(&op)?;
1053 Ok(Some(op))
1054 }
1055
1056 fn open_lock_file(&self) -> Result<fs::File> {
1057 crate::session_manager::open_private_lock_file(&self.lock_path)
1058 .with_context(|| format!("Failed to open {}", self.lock_path.display()))
1059 }
1060
1061 fn save_unlocked(&self, op: &Operation) -> Result<()> {
1062 codewhale_config::persistence::atomic_write_json(&self.path, op)
1063 .with_context(|| format!("Failed to write {}", self.path.display()))
1064 }
1065 }
1066
1067 pub fn start_operation(
1068 store: &OperationStore,
1069 workspace: &Path,
1070 direction: Option<String>,
1071 burn_usd_per_hour: Option<f64>,
1072 credentials_present: bool,
1073 lead_model: &str,
1074 ) -> Result<Operation> {
1075 let mut direction = match direction {
1076 Some(text) if !text.trim().is_empty() => text,
1077 _ => read_direction(workspace)?,
1078 };
1079 if direction.trim().is_empty()
1080 && let Some(existing) = store.load()?
1081 {
1082 direction = existing.direction;
1083 }
1084 let mut op = Operation::new(direction, burn_usd_per_hour);
1085 op.set_lead_model(lead_model);
1086 op.credentials_present = credentials_present;
1087 op.project();
1088 store.save(&op)?;
1089 Ok(op)
1090 }
1091
1092 /// Re-entering Operate attaches to the recorded operation — same id, spend,
1093 /// lead plan, and roster — instead of minting a fresh record that silently
1094 /// resets progress. A cancelled record (or an empty store) starts a new
1095 /// operation. An explicitly provided direction is applied through the normal
1096 /// patch rules (which invalidate a superseded lead plan).
1097 pub fn attach_or_start_operation(
1098 store: &OperationStore,
1099 workspace: &Path,
1100 direction: Option<String>,
1101 burn_usd_per_hour: Option<f64>,
1102 credentials_present: bool,
1103 lead_model: &str,
1104 ) -> Result<Operation> {
1105 if store
1106 .load()?
1107 .is_some_and(|op| op.status != OperateStatus::Cancelled)
1108 {
1109 let attached = store.mutate(|op| {
1110 if let Some(text) = direction.as_ref().filter(|text| !text.trim().is_empty()) {
1111 apply_operate_patch(op, &serde_json::json!({ "direction": text }))?;
1112 }
1113 op.set_lead_model(lead_model);
1114 op.credentials_present = credentials_present;
1115 op.project();
1116 Ok(())
1117 })?;
1118 if let Some(op) = attached {
1119 return Ok(op);
1120 }
1121 }
1122 start_operation(
1123 store,
1124 workspace,
1125 direction,
1126 burn_usd_per_hour,
1127 credentials_present,
1128 lead_model,
1129 )
1130 }
1131
1132 pub fn apply_operate_patch(op: &mut Operation, patch: &serde_json::Value) -> Result<()> {
1133 if op.status == OperateStatus::Cancelled {
1134 anyhow::bail!("A cancelled Operation cannot be edited.");
1135 }
1136 if let Some(direction) = patch.get("direction") {
1137 let next = normalize_direction(direction.as_str().map(str::to_string).unwrap_or_default());
1138 if patch.get("leadPlan").is_none() && next != op.direction {
1139 // A changed direction supersedes the recorded lead plan: workers
1140 // must stop executing slices derived from the old direction. The
1141 // operation idles `awaiting_lead_plan` until the lead re-plans
1142 // (same patch may instead carry an explicit replacement plan).
1143 op.lead_plan = None;
1144 }
1145 op.direction = next;
1146 }
1147 if patch.get("burnRate").is_some() {
1148 op.burn_rate = parse_burn_rate(patch.get("burnRate"))?;
1149 }
1150 if let Some(plan) = patch.get("leadPlan") {
1151 op.lead_plan = if plan.is_null() {
1152 None
1153 } else {
1154 Some(serde_json::from_value(plan.clone()).context("leadPlan is invalid")?)
1155 };
1156 // Plan owners are the worker roster: PUT /v1/operate/plan and a
1157 // PATCHed plan must admit their owners or workers never dispatch.
1158 sync_plan_owners(op);
1159 }
1160 if let Some(flag) = patch.get("humanGated").and_then(serde_json::Value::as_bool) {
1161 op.human_gated = flag;
1162 }
1163 if let Some(gate) = patch.get("humanGate").and_then(serde_json::Value::as_str) {
1164 op.human_gate = gate.trim().chars().take(160).collect();
1165 if human_gate_for(&op.human_gate) {
1166 op.human_gated = true;
1167 }
1168 }
1169 if let Some(flag) = patch
1170 .get("credentialsPresent")
1171 .and_then(serde_json::Value::as_bool)
1172 {
1173 op.credentials_present = flag;
1174 }
1175 op.updated_at = Utc::now().to_rfc3339();
1176 op.project();
1177 Ok(())
1178 }
1179
1180 /// Add every non-lead plan owner to the roster (idempotent) so admitted
1181 /// slices have a worker to dispatch to.
1182 fn sync_plan_owners(op: &mut Operation) {
1183 if let Some(plan) = &op.lead_plan {
1184 for slice in &plan.slices {
1185 if slice.owner_id != "lead"
1186 && !slice.owner_id.is_empty()
1187 && !op.roster.iter().any(|member| member.id == slice.owner_id)
1188 {
1189 op.roster.push(OperateRosterMember {
1190 id: slice.owner_id.clone(),
1191 display_name: slice.owner_id.clone(),
1192 role: "worker".to_string(),
1193 model: String::new(),
1194 state: "idle".to_string(),
1195 });
1196 }
1197 }
1198 }
1199 }
1200
1201 pub fn cancel_operation(store: &OperationStore) -> Result<Option<Operation>> {
1202 store.mutate(|op| {
1203 let now = Utc::now().to_rfc3339();
1204 op.status = OperateStatus::Cancelled;
1205 op.cancelled_at = now.clone();
1206 op.updated_at = now.clone();
1207 op.last_keep_alive_at = now;
1208 op.project();
1209 Ok(())
1210 })
1211 }
1212
1213 pub fn keep_alive_observation(
1214 op: &mut Operation,
1215 observed_burn_usd_per_hour: Option<f64>,
1216 spent_usd: Option<f64>,
1217 credentials_present: Option<bool>,
1218 human_gated: Option<bool>,
1219 ) {
1220 let now = Utc::now().to_rfc3339();
1221 op.last_keep_alive_at = now.clone();
1222 op.updated_at = now;
1223 if let Some(burn) = observed_burn_usd_per_hour {
1224 op.observed_burn_usd_per_hour = Some(burn.max(0.0));
1225 }
1226 if let Some(spent) = spent_usd {
1227 op.spent_usd = spent.max(0.0);
1228 }
1229 if let Some(credentials) = credentials_present {
1230 op.credentials_present = credentials;
1231 }
1232 if let Some(gated) = human_gated {
1233 op.human_gated = gated;
1234 }
1235 op.project();
1236 }
1237
1238 /// Install (or refresh) the `cw-operate` keepalive bound to `workspace`.
1239 ///
1240 /// The record is built directly under the fixed id — no create-then-delete
1241 /// id swap that could orphan an active UUID-named automation. Reuse
1242 /// refreshes the prompt and workspace while preserving an explicit saved
1243 /// model/provider pair. Fresh and legacy unpinned records capture the caller's
1244 /// effective route together, including Auto intent and exact custom identity.
1245 ///
1246 /// `kick_now` schedules the first lead-plan step for the next scheduler tick
1247 /// (a fresh operation otherwise idles up to an hour awaiting its plan); the
1248 /// hourly recurrence covers follow-ups.
1249 pub fn upsert_keepalive(
1250 manager: &AutomationManager,
1251 workspace: &Path,
1252 kick_now: bool,
1253 config: &codewhale_runtime::config::Config,
1254 selection: Option<(&codewhale_runtime::config::ProviderIdentity, &str)>,
1255 ) -> Result<(String, bool)> {
1256 let now = Utc::now();
1257 let prompt = format!(
1258 "Keep Operate alive. Read the Operate record (current.json) and its direction, refresh the lead plan, and dispatch ready slices with at most `writersInFlight` concurrent workers — that budget already encodes pace (hold/throttle/widen), so honor it instead of widening on your own. Burn rate paces spend; it never stops the operation. Workspace: {}",
1259 workspace.display()
1260 );
1261 let mut readiness = (String::new(), false);
1262 manager.edit_automation(OPERATE_KEEPALIVE_ID, |current| {
1263 let scope = manager
1264 .execution_scope()
1265 .context("Operate execution ownership is unverified")?;
1266 if current
1267 .as_ref()
1268 .and_then(|record| record.execution_scope.as_deref())
1269 .is_some_and(|bound| bound != scope)
1270 {
1271 bail!("Operate keepalive belongs to another Runtime execution scope");
1272 }
1273 let (identity, model, ready) = keepalive_route(config, current.as_ref(), selection)?;
1274 readiness = (model.clone(), ready);
1275 let mut record = current.unwrap_or_else(|| AutomationRecord {
1276 destination_thread_id: None,
1277 schema_version: crate::automation_manager::CURRENT_AUTOMATION_SCHEMA_VERSION,
1278 execution_scope: Some(scope.to_string()),
1279 id: OPERATE_KEEPALIVE_ID.to_string(),
1280 name: "Operate keep-alive".to_string(),
1281 prompt: prompt.clone(),
1282 rrule: OPERATE_KEEPALIVE_RRULE.to_string(),
1283 cwds: Vec::new(),
1284 model: None,
1285 model_provider: None,
1286 model_provider_id: None,
1287 mode: Some("operate".to_string()),
1288 allow_shell: Some(false),
1289 trust_mode: Some(false),
1290 auto_approve: Some(false),
1291 delivery_mode: None,
1292 status: AutomationStatus::Active,
1293 created_at: now,
1294 updated_at: now,
1295 next_run_at: None,
1296 last_run_at: None,
1297 revision: 0,
1298 disallowed_tools: Vec::new(),
1299 });
1300 record.schema_version = record.schema_version.max(
1301 crate::automation_manager::required_automation_schema_version(&record.disallowed_tools),
1302 );
1303 if record.execution_scope.is_none() {
1304 record.execution_scope = Some(scope.to_string());
1305 }
1306 record.name = "Operate keep-alive".to_string();
1307 record.prompt = prompt;
1308 record.rrule = OPERATE_KEEPALIVE_RRULE.to_string();
1309 record.cwds = vec![workspace.to_path_buf()];
1310 record.model = Some(model);
1311 record.model_provider = Some(identity.provider.as_str().to_string());
1312 record.model_provider_id = identity.persisted_id().map(str::to_string);
1313 record.mode = Some("operate".to_string());
1314 record.allow_shell = Some(false);
1315 record.trust_mode = Some(false);
1316 record.auto_approve = Some(false);
1317 record.delivery_mode = None;
1318 record.status = AutomationStatus::Active;
1319 record.updated_at = now;
1320 if kick_now {
1321 record.next_run_at = Some(now);
1322 }
1323 Ok(Some(record))
1324 })?;
1325 Ok(readiness)
1326 }
1327
1328 /// Cancel tears the operation down *including* its keepalive: an unattended
1329 /// hourly lead run after cancel is pure cost. The automation is paused
1330 /// (never deleted) so its run history survives and a later start reactivates
1331 /// it. A missing keepalive is not an error; any other failure to establish
1332 /// its state is. A requested spending stop that cannot confirm the record is
1333 /// paused must never look like one that happened — an unreadable record may
1334 /// still be Active and scheduled.
1335 pub fn pause_keepalive(manager: &AutomationManager) -> Result<()> {
1336 let record = match manager.get_automation(OPERATE_KEEPALIVE_ID) {
1337 Ok(record) => record,
1338 Err(error) if is_not_found(&error) => return Ok(()),
1339 Err(error) => {
1340 return Err(error.context(
1341 "Operate keepalive state could not be established; it may still be scheduled",
1342 ));
1343 }
1344 };
1345 if matches!(record.status, AutomationStatus::Active) {
1346 manager.pause_automation(OPERATE_KEEPALIVE_ID)?;
1347 }
1348 Ok(())
1349 }
1350
1351 fn is_not_found(error: &anyhow::Error) -> bool {
1352 error.chain().any(|cause| {
1353 cause
1354 .downcast_ref::<std::io::Error>()
1355 .is_some_and(|io| io.kind() == std::io::ErrorKind::NotFound)
1356 })
1357 }
1358
1359 /// Pull the next keepalive lead run to the next scheduler tick (for example
1360 /// after a direction PATCH invalidated the plan). No-op when the keepalive is
1361 /// absent or paused (a paused keepalive belongs to a cancelled operation).
1362 pub fn kick_keepalive(manager: &AutomationManager) -> Result<bool> {
1363 let mut kicked = false;
1364 manager.edit_automation(OPERATE_KEEPALIVE_ID, |current| {
1365 let Some(mut record) = current else {
1366 return Ok(None);
1367 };
1368 if record.status == AutomationStatus::Active {
1369 let now = Utc::now();
1370 record.next_run_at = Some(now);
1371 record.updated_at = now;
1372 kicked = true;
1373 }
1374 Ok(Some(record))
1375 })?;
1376 Ok(kicked)
1377 }
1378
1379 #[must_use]
1380 pub fn human_gate_for(action: &str) -> bool {
1381 matches!(
1382 action,
1383 "deploy" | "billing" | "force-push" | "forbidden-pr" | "red-ci"
1384 )
1385 }
1386
1387 #[cfg(test)]
1388 mod tests {
1389 use super::*;
1390 use tempfile::TempDir;
1391
1392 fn route_fixture_config() -> codewhale_runtime::config::Config {
1393 toml::from_str(
1394 r#"
1395 provider = "route-a"
1396 [providers.route-a]
1397 kind = "openai-compatible"
1398 base_url = "https://route-a.example.test/v1"
1399 model = "same-model"
1400 auth_mode = "none"
1401 [providers.route-b]
1402 kind = "openai-compatible"
1403 base_url = "https://route-b.example.test/v1"
1404 model = "same-model"
1405 auth_mode = "api-key"
1406 api_key_env = "CW_OPERATE_MISSING_TEST_KEY"
1407 "#,
1408 )
1409 .expect("route fixture")
1410 }
1411
1412 #[test]
1413 fn keepalive_saved_route_controls_readiness_refresh_and_auto_intent() -> Result<()> {
1414 let _env = crate::test_support::lock_test_env();
1415 let _cli = crate::test_support::EnvVarGuard::remove("CODEWHALE_CLI_API_KEY");
1416 let _missing = crate::test_support::EnvVarGuard::remove("CW_OPERATE_MISSING_TEST_KEY");
1417 let root = TempDir::new()?;
1418 let manager = AutomationManager::open_for_test(root.path().join("automations"))?;
1419 let mut config = route_fixture_config();
1420 assert!(upsert_keepalive(&manager, root.path(), true, &config, None)?.1);
1421 let first = manager.get_automation(OPERATE_KEEPALIVE_ID)?;
1422 assert_eq!(first.model.as_deref(), Some("same-model"));
1423 assert_eq!(first.model_provider.as_deref(), Some("custom"));
1424 assert_eq!(first.model_provider_id.as_deref(), Some("route-a"));
1425 assert_eq!(first.auto_approve, Some(false));
1426
1427 // Explicitly edit the saved route. Parent route-a is credential-ready;
1428 // route-b is not, even though both expose the same model spelling.
1429 let mut pinned = first.clone();
1430 pinned.model_provider_id = Some("route-b".into());
1431 manager.save_automation(&pinned)?;
1432 assert!(!keepalive_readiness(&manager, &config, None)?.1);
1433 assert!(!upsert_keepalive(&manager, root.path(), false, &config, None)?.1);
1434 assert_eq!(
1435 manager
1436 .get_automation(OPERATE_KEEPALIVE_ID)?
1437 .model_provider_id,
1438 Some("route-b".into())
1439 );
1440
1441 pinned.model = Some("auto".into());
1442 manager.save_automation(&pinned)?;
1443 assert!(!upsert_keepalive(&manager, root.path(), false, &config, None)?.1);
1444 let auto = manager.get_automation(OPERATE_KEEPALIVE_ID)?;
1445 assert_eq!(auto.model.as_deref(), Some("auto"));
1446 assert_eq!(auto.model_provider_id.as_deref(), Some("route-b"));
1447 assert_eq!(auto.created_at, first.created_at);
1448
1449 config.providers.as_mut().unwrap().custom.remove("route-b");
1450 let before = serde_json::to_value(&auto)?;
1451 assert!(keepalive_readiness(&manager, &config, None).is_err());
1452 assert!(upsert_keepalive(&manager, root.path(), true, &config, None).is_err());
1453 assert_eq!(
1454 serde_json::to_value(manager.get_automation(OPERATE_KEEPALIVE_ID)?)?,
1455 before
1456 );
1457
1458 // Legacy unpinned GLM is a previous scheduler default, not an exact
1459 // provider choice. Migration replaces all route fields together.
1460 pinned.model = Some("GLM-5.3".into());
1461 pinned.model_provider = None;
1462 pinned.model_provider_id = None;
1463 manager.save_automation(&pinned)?;
1464 assert!(upsert_keepalive(&manager, root.path(), true, &config, None)?.1);
1465 let migrated = manager.get_automation(OPERATE_KEEPALIVE_ID)?;
1466 assert_eq!(migrated.model.as_deref(), Some("same-model"));
1467 assert_eq!(migrated.model_provider.as_deref(), Some("custom"));
1468 assert_eq!(migrated.model_provider_id.as_deref(), Some("route-a"));
1469 Ok(())
1470 }
1471
1472 #[test]
1473 fn keepalive_legacy_custom_records_the_table_id() -> Result<()> {
1474 let _env = crate::test_support::lock_test_env();
1475 let _cli = crate::test_support::EnvVarGuard::remove("CODEWHALE_CLI_API_KEY");
1476 let root = TempDir::new()?;
1477 let manager = AutomationManager::open_for_test(root.path().join("automations"))?;
1478 let config = codewhale_runtime::config::parse_config_base(
1479 r#"provider = "custom"
1480 [providers.custom]
1481 kind = "openai-compatible"
1482 base_url = "https://legacy.example.test/v1"
1483 model = "legacy-model"
1484 auth_mode = "none"
1485 "#,
1486 )?;
1487 upsert_keepalive(&manager, root.path(), false, &config, None)?;
1488 let record = manager.get_automation(OPERATE_KEEPALIVE_ID)?;
1489 assert_eq!(record.model.as_deref(), Some("legacy-model"));
1490 // The literal route is the `[providers.custom]` table since #6394.
1491 assert_eq!(record.model_provider.as_deref(), Some("custom"));
1492 assert_eq!(record.model_provider_id.as_deref(), Some("custom"));
1493 upsert_keepalive(&manager, root.path(), false, &config, None)?;
1494 assert_eq!(
1495 manager
1496 .get_automation(OPERATE_KEEPALIVE_ID)?
1497 .model_provider_id
1498 .as_deref(),
1499 Some("custom")
1500 );
1501 Ok(())
1502 }
1503
1504 struct RouteRecordingExecutor(
1505 std::sync::Arc<std::sync::Mutex<Vec<crate::runtime_threads::manager::CreateThreadRequest>>>,
1506 );
1507
1508 #[async_trait::async_trait]
1509 impl crate::task_manager::TaskExecutor for RouteRecordingExecutor {
1510 async fn execute(
1511 &self,
1512 task: crate::task_manager::ExecutionTask,
1513 _events: tokio::sync::mpsc::Sender<crate::task_manager::TaskExecutionEvent>,
1514 _cancel: tokio_util::sync::CancellationToken,
1515 ) -> crate::task_manager::TaskExecutionResult {
1516 self.0.lock().unwrap().push(task.thread_request());
1517 crate::task_manager::TaskExecutionResult {
1518 status: crate::task_manager::TaskStatus::Completed,
1519 result_text: Some("route fixture completed".into()),
1520 error: None,
1521 terminal_reason: crate::task_manager::TaskTerminalReason::Completed,
1522 }
1523 }
1524 }
1525
1526 #[tokio::test]
1527 async fn keepalive_pin_reaches_task_and_survives_parent_change() -> Result<()> {
1528 let root = TempDir::new()?;
1529 let manager = AutomationManager::open_for_test(root.path().join("automations"))?;
1530 let mut config = route_fixture_config();
1531 upsert_keepalive(&manager, root.path(), false, &config, None)?;
1532 pause_keepalive(&manager)?;
1533 config.provider = Some("route-b".into());
1534 assert!(upsert_keepalive(&manager, root.path(), true, &config, None)?.1);
1535 let observations = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
1536 let tasks = crate::task_manager::TaskManager::start_with_executor(
1537 crate::task_manager::TaskManagerConfig {
1538 data_dir: root.path().join("tasks"),
1539 worker_count: 1,
1540 default_workspace: root.path().to_path_buf(),
1541 default_model: "changed-default".into(),
1542 default_mode: "agent".into(),
1543 allow_shell: false,
1544 trust_mode: false,
1545 execution_limits: Default::default(),
1546 human_waits_answerable: false,
1547 },
1548 std::sync::Arc::new(RouteRecordingExecutor(observations.clone())),
1549 )
1550 .await?;
1551 let shared = std::sync::Arc::new(tokio::sync::Mutex::new(manager));
1552 let run = crate::automation_manager::run_now_shared(&shared, OPERATE_KEEPALIVE_ID, &tasks)
1553 .await?;
1554 let id = run.task_id.as_deref().context("bound task")?;
1555 let task = crate::task_manager::wait_for_terminal_state(
1556 &tasks,
1557 id,
1558 std::time::Duration::from_secs(5),
1559 )
1560 .await?;
1561 assert_eq!(task.status, crate::task_manager::TaskStatus::Completed);
1562 assert_eq!(task.model, "same-model");
1563 assert_eq!(task.model_provider.as_deref(), Some("custom"));
1564 assert_eq!(task.model_provider_id.as_deref(), Some("route-a"));
1565 let observed = observations.lock().unwrap();
1566 assert_eq!(observed.len(), 1);
1567 assert_eq!(observed[0].model, Some(task.model.clone()));
1568 assert_eq!(observed[0].model_provider_id, task.model_provider_id);
1569 assert_eq!(observed[0].auto_approve, Some(false));
1570 drop(observed);
1571 let reopened = AutomationManager::open_for_test(root.path().join("automations"))?;
1572 assert!(keepalive_readiness(&reopened, &config, None)?.1);
1573 assert_eq!(
1574 reopened.list_runs(OPERATE_KEEPALIVE_ID, None)?[0]
1575 .task_id
1576 .as_deref(),
1577 Some(id)
1578 );
1579 // A subsequent explicit edit cannot rewrite an accepted task binding.
1580 let mut edited = reopened.get_automation(OPERATE_KEEPALIVE_ID)?;
1581 edited.model = Some("replacement-model".into());
1582 edited.model_provider_id = Some("route-b".into());
1583 reopened.save_automation(&edited)?;
1584 assert_eq!(
1585 tasks
1586 .read_bound_task(id)?
1587 .context("accepted task")?
1588 .model_provider_id,
1589 Some("route-a".into())
1590 );
1591 tasks.shutdown();
1592 Ok(())
1593 }
1594
1595 fn with_credentials(mut op: Operation) -> Operation {
1596 op.credentials_present = true;
1597 op.project();
1598 op
1599 }
1600
1601 #[test]
1602 fn unbounded_start_matches_cwc_contract() {
1603 let op = with_credentials(Operation::new("Keep shipping honest slices", None));
1604 assert_eq!(op.schema_version, 1);
1605 assert!(op.burn_rate.is_none());
1606 assert_eq!(op.pace, OperatePace::Unbounded);
1607 assert_eq!(op.status, OperateStatus::IdleBlocked);
1608 assert_eq!(
1609 op.idle_blocked_reason,
1610 Some(OperateIdleReason::AwaitingLeadPlan)
1611 );
1612 assert!(!op.workers_admitted);
1613 assert!(op.lead_operator.model.is_empty(), "no model was assigned");
1614 let json = serde_json::to_value(&op).expect("json");
1615 assert!(json.get("burnRate").unwrap().is_null());
1616 assert_eq!(json["leadOperator"]["model"], "");
1617 assert_eq!(json["schemaVersion"], 1);
1618 assert!(json.get("leadPlan").unwrap().is_null());
1619 assert_eq!(json["idleBlockedReason"], "awaiting_lead_plan");
1620 assert_eq!(json["workersAdmitted"], false);
1621 assert!(json["id"].as_str().unwrap().starts_with("op_"));
1622 }
1623
1624 #[test]
1625 fn operation_lead_label_follows_selection_and_planned_workers_stay_unassigned() -> Result<()> {
1626 let root = TempDir::new()?;
1627 let store = OperationStore::open(root.path())?;
1628 let operation = start_operation(
1629 &store,
1630 root.path(),
1631 Some("First bounded task\nSecond bounded task".into()),
1632 None,
1633 true,
1634 "selected-model",
1635 )?;
1636 assert_eq!(operation.lead_operator.model, "selected-model");
1637 let mut attached =
1638 attach_or_start_operation(&store, root.path(), None, None, true, "auto")?;
1639 assert_eq!(attached.id, operation.id);
1640 assert_eq!(attached.lead_operator.model, "auto");
1641 assert!(
1642 attached
1643 .roster
1644 .iter()
1645 .filter(|member| member.id == "lead")
1646 .all(|member| member.model == "auto")
1647 );
1648 attached.plan_from_direction();
1649 assert!(attached.roster.iter().any(|member| member.id != "lead"));
1650 assert!(
1651 attached
1652 .roster
1653 .iter()
1654 .filter(|member| member.id != "lead")
1655 .all(|member| member.model.is_empty())
1656 );
1657 assert!(
1658 store
1659 .load()?
1660 .context("saved operation")?
1661 .roster
1662 .iter()
1663 .filter(|member| member.id == "lead")
1664 .all(|member| member.model == "auto")
1665 );
1666 Ok(())
1667 }
1668
1669 /// The cross-process lock sidecar is owner-only like the operation file.
1670 #[cfg(unix)]
1671 #[test]
1672 fn operation_lock_file_is_owner_only() -> Result<()> {
1673 use std::os::unix::fs::PermissionsExt as _;
1674 let root = TempDir::new()?;
1675 let store = OperationStore::open(root.path())?;
1676 start_operation(&store, root.path(), None, None, true, "auto")?;
1677 let lock = root.path().join("current.json.lock");
1678 assert_eq!(fs::metadata(&lock)?.permissions().mode() & 0o777, 0o600);
1679 Ok(())
1680 }
1681
1682 #[test]
1683 fn create_without_credentials_fails_closed() {
1684 let op = Operation::new("Keep shipping honest slices", None);
1685 assert_eq!(op.status, OperateStatus::IdleBlocked);
1686 assert_eq!(
1687 op.idle_blocked_reason,
1688 Some(OperateIdleReason::MissingCredentials)
1689 );
1690 assert!(!op.workers_admitted);
1691 }
1692
1693 #[test]
1694 fn burn_rate_paces_and_never_stops() {
1695 let mut op = with_credentials(Operation::new(
1696 "Hold a $12/hr burn\nSecond slice\nThird slice",
1697 Some(12.0),
1698 ));
1699 op.plan_from_direction();
1700 assert_eq!(op.status, OperateStatus::Running);
1701 assert!(op.workers_admitted);
1702 keep_alive_observation(&mut op, Some(20.0), Some(80.0), None, None);
1703 assert_eq!(op.status, OperateStatus::Running);
1704 assert_eq!(op.pace, OperatePace::Throttle);
1705 // Throttle is a real dispatch cut, not a label: of the two planned
1706 // workers only one stays in flight while the operation keeps running.
1707 assert_eq!(op.writers_in_flight, OPERATE_THROTTLE_WRITERS);
1708 assert_eq!(
1709 op.roster
1710 .iter()
1711 .filter(|member| member.role == "worker" && member.state == "in_flight")
1712 .count(),
1713 OPERATE_THROTTLE_WRITERS
1714 );
1715 assert_eq!(
1716 op.roster
1717 .iter()
1718 .filter(|member| member.role == "worker" && member.state == "idle")
1719 .count(),
1720 1,
1721 "the worker past the throttle budget idles"
1722 );
1723 assert!(op.idle_blocked_reason.is_none());
1724 assert!(op.workers_admitted);
1725 let board = render_plan_board(&op);
1726 assert!(!board.contains("exhausted"));
1727 assert!(!board.contains("wallet"));
1728 assert_eq!(op.burn_rate.as_ref().unwrap().kind, "usd_per_hour");
1729 assert!((op.burn_rate.as_ref().unwrap().amount_usd_per_hour - 12.0).abs() < f64::EPSILON);
1730 }
1731
1732 #[test]
1733 fn hold_band_freezes_writer_width() {
1734 let mut op = with_credentials(Operation::new("one\ntwo\nthree", Some(12.0)));
1735 op.plan_from_direction();
1736 keep_alive_observation(&mut op, Some(12.0), None, None, None);
1737 assert_eq!(op.pace, OperatePace::Hold);
1738 assert_eq!(op.status, OperateStatus::Running);
1739 // Hold admits no new writers past the hold width; this plan has two
1740 // workers, so both stay in flight but the budget stops at two.
1741 assert_eq!(op.writers_in_flight, 2);
1742 let mut four = with_credentials(Operation::new("one\ntwo\nthree\nfour\nfive", Some(12.0)));
1743 four.plan_from_direction();
1744 keep_alive_observation(&mut four, Some(12.3), None, None, None);
1745 assert_eq!(four.pace, OperatePace::Hold);
1746 assert_eq!(
1747 four.writers_in_flight, OPERATE_HOLD_WRITERS,
1748 "hold never widens to the full writer width"
1749 );
1750 }
1751
1752 #[test]
1753 fn under_rate_widens() {
1754 let mut op = with_credentials(Operation::new("one\ntwo\nthree\nfour", Some(12.0)));
1755 op.plan_from_direction();
1756 keep_alive_observation(&mut op, Some(1.0), None, None, None);
1757 assert_eq!(op.pace, OperatePace::Widen);
1758 assert_eq!(op.status, OperateStatus::Running);
1759 assert_eq!(
1760 op.writers_in_flight, OPERATE_MAX_WRITERS,
1761 "widen opens the full writer width"
1762 );
1763 }
1764
1765 #[test]
1766 fn cancelled_field_matches_landed_cwc_contract() {
1767 // CWC `packages/contracts/src/operate.js` (`20de981`, PR #284) names
1768 // the field `cancelledAt` — `publicOperateRecord` always emits it,
1769 // as `""` before cancellation. There is no bare `cancelled` field.
1770 let dir = TempDir::new().expect("temp");
1771 let store = OperationStore::open(dir.path()).expect("store");
1772 let op = start_operation(
1773 &store,
1774 dir.path(),
1775 Some("Contract shape".into()),
1776 None,
1777 true,
1778 "selected-model",
1779 )
1780 .expect("start");
1781 let json = serde_json::to_value(&op).expect("json");
1782 assert_eq!(json["cancelledAt"], serde_json::json!(""));
1783 assert!(json.get("cancelled").is_none());
1784
1785 let cancelled = cancel_operation(&store).expect("cancel").expect("present");
1786 let json = serde_json::to_value(&cancelled).expect("json");
1787 assert!(!json["cancelledAt"].as_str().unwrap().is_empty());
1788 assert!(json.get("cancelled").is_none());
1789 }
1790
1791 #[test]
1792 fn direction_change_invalidates_stale_lead_plan() {
1793 let mut op = with_credentials(Operation::new("Old direction", None));
1794 op.plan_from_direction();
1795 assert_eq!(op.status, OperateStatus::Running);
1796
1797 apply_operate_patch(
1798 &mut op,
1799 &serde_json::json!({ "direction": "Brand new direction" }),
1800 )
1801 .expect("patch");
1802 assert_eq!(op.direction, "Brand new direction");
1803 assert!(
1804 op.lead_plan.is_none(),
1805 "a changed direction must not leave superseded slices executing"
1806 );
1807 assert_eq!(op.status, OperateStatus::IdleBlocked);
1808 assert_eq!(
1809 op.idle_blocked_reason,
1810 Some(OperateIdleReason::AwaitingLeadPlan)
1811 );
1812 assert!(!op.workers_admitted);
1813 assert_eq!(op.writers_in_flight, 0);
1814 }
1815
1816 #[test]
1817 fn same_direction_patch_keeps_plan() {
1818 let mut op = with_credentials(Operation::new("Steady", None));
1819 op.plan_from_direction();
1820 apply_operate_patch(&mut op, &serde_json::json!({ "direction": "Steady" })).expect("patch");
1821 assert!(op.lead_plan.is_some());
1822 assert_eq!(op.status, OperateStatus::Running);
1823 }
1824
1825 #[test]
1826 fn put_plan_admits_worker_owners() {
1827 let mut op = with_credentials(Operation::new("Slice it", None));
1828 let plan = serde_json::json!({
1829 "slices": [
1830 { "id": "slice-1", "title": "Scout", "ownerId": "lead",
1831 "dependsOn": [], "estCostUsd": 0.1, "startOffsetSec": 0, "durationSec": 600 },
1832 { "id": "slice-2", "title": "Build", "ownerId": "worker-7",
1833 "dependsOn": ["slice-1"], "estCostUsd": 0.2, "startOffsetSec": 600, "durationSec": 1200 }
1834 ]
1835 });
1836 apply_operate_patch(&mut op, &serde_json::json!({ "leadPlan": plan })).expect("patch");
1837 assert!(
1838 op.roster.iter().any(|member| member.id == "worker-7"),
1839 "plan owners must join the roster or workers never dispatch"
1840 );
1841 assert_eq!(op.status, OperateStatus::Running);
1842 assert!(op.workers_admitted);
1843 // Idempotent: re-applying the same plan must not duplicate the owner.
1844 let before = op.roster.len();
1845 apply_operate_patch(&mut op, &serde_json::json!({ "leadPlan": plan })).expect("patch");
1846 assert_eq!(op.roster.len(), before);
1847 }
1848
1849 #[test]
1850 fn attach_preserves_operation_record() {
1851 let dir = TempDir::new().expect("temp");
1852 let store = OperationStore::open(dir.path()).expect("store");
1853 let first = start_operation(
1854 &store,
1855 dir.path(),
1856 Some("Keep the lineage".into()),
1857 None,
1858 true,
1859 "selected-model",
1860 )
1861 .expect("start");
1862 let mut spent = first.clone();
1863 keep_alive_observation(&mut spent, Some(3.0), Some(42.0), None, None);
1864 store.save(&spent).expect("save spend");
1865
1866 let reentered =
1867 attach_or_start_operation(&store, dir.path(), None, None, true, "selected-model")
1868 .expect("attach");
1869 assert_eq!(reentered.id, first.id, "re-entry must attach, not reset");
1870 assert_eq!(reentered.spent_usd, 42.0);
1871 assert_eq!(reentered.observed_burn_usd_per_hour, Some(3.0));
1872
1873 // A cancelled record is terminal: re-entry starts a new operation.
1874 cancel_operation(&store).expect("cancel");
1875 let fresh =
1876 attach_or_start_operation(&store, dir.path(), None, None, true, "selected-model")
1877 .expect("restart");
1878 assert_ne!(fresh.id, first.id);
1879 // The fresh operation reuses the recorded direction and awaits its
1880 // own lead plan (CWC projects a plan-less record to idle_blocked).
1881 assert_eq!(fresh.direction, "Keep the lineage");
1882 assert_eq!(fresh.status, OperateStatus::IdleBlocked);
1883 assert_eq!(
1884 fresh.idle_blocked_reason,
1885 Some(OperateIdleReason::AwaitingLeadPlan)
1886 );
1887 assert_eq!(fresh.spent_usd, 0.0);
1888 }
1889
1890 #[test]
1891 fn mutate_reloads_latest_record_under_lock() {
1892 let dir = TempDir::new().expect("temp");
1893 let writer = OperationStore::open(dir.path()).expect("store a");
1894 let reader = OperationStore::open(dir.path()).expect("store b");
1895 start_operation(
1896 &writer,
1897 dir.path(),
1898 Some("First".into()),
1899 None,
1900 true,
1901 "selected-model",
1902 )
1903 .expect("start");
1904
1905 writer
1906 .mutate(|op| {
1907 apply_operate_patch(op, &serde_json::json!({ "direction": "Second" }))?;
1908 Ok(())
1909 })
1910 .expect("mutate a")
1911 .expect("present");
1912 reader
1913 .mutate(|op| {
1914 keep_alive_observation(op, Some(9.0), Some(5.0), None, None);
1915 Ok(())
1916 })
1917 .expect("mutate b")
1918 .expect("present");
1919
1920 // The second store reloaded under the lock, so the first store's
1921 // direction write survives alongside the keepalive observation.
1922 let merged = writer.load().expect("load").expect("present");
1923 assert_eq!(merged.direction, "Second");
1924 assert_eq!(merged.spent_usd, 5.0);
1925 assert_eq!(merged.observed_burn_usd_per_hour, Some(9.0));
1926 }
1927
1928 #[test]
1929 fn burn_rate_below_a_cent_is_rejected() {
1930 let err = parse_burn_rate(Some(&serde_json::json!(0.001))).expect_err("rejects");
1931 assert!(err.to_string().contains("at least $0.01/hr"));
1932 let zero_target = parse_burn_rate(Some(&serde_json::json!(0.004))).expect_err("rejects");
1933 assert!(zero_target.to_string().contains("at least $0.01/hr"));
1934 }
1935
1936 #[test]
1937 fn keepalive_reuse_refreshes_cwds_and_kicks_first_lead_run() {
1938 let dir = TempDir::new().expect("temp");
1939 let manager = AutomationManager::open_for_test(dir.path().to_path_buf()).expect("manager");
1940 let workspace_a = dir.path().join("workspace-a");
1941 let workspace_b = dir.path().join("workspace-b");
1942 fs::create_dir_all(&workspace_a).expect("dir a");
1943 fs::create_dir_all(&workspace_b).expect("dir b");
1944
1945 upsert_keepalive(&manager, &workspace_a, false, &route_fixture_config(), None)
1946 .expect("upsert a");
1947 let first = manager
1948 .get_automation(OPERATE_KEEPALIVE_ID)
1949 .expect("keepalive a");
1950 assert_eq!(first.cwds, vec![workspace_a.clone()]);
1951
1952 upsert_keepalive(&manager, &workspace_b, true, &route_fixture_config(), None)
1953 .expect("upsert b");
1954 let second = manager
1955 .get_automation(OPERATE_KEEPALIVE_ID)
1956 .expect("keepalive b");
1957 assert_eq!(
1958 second.cwds,
1959 vec![workspace_b],
1960 "reuse must retarget the workspace scheduled runs execute in"
1961 );
1962 assert_eq!(
1963 second.next_run_at.map(|at| at <= Utc::now()),
1964 Some(true),
1965 "kick schedules the first lead run for the next scheduler tick"
1966 );
1967 assert_eq!(second.model.as_deref(), Some("same-model"));
1968 assert_eq!(second.mode.as_deref(), Some("operate"));
1969 assert_eq!(second.rrule, OPERATE_KEEPALIVE_RRULE);
1970
1971 // Only one automation exists — no orphaned UUID-named twin.
1972 assert_eq!(
1973 manager
1974 .list_automations()
1975 .expect("list")
1976 .iter()
1977 .filter(|record| record.mode.as_deref() == Some("operate"))
1978 .count(),
1979 1
1980 );
1981 }
1982
1983 #[test]
1984 fn cancel_pauses_keepalive_so_no_cost_accrues() {
1985 let dir = TempDir::new().expect("temp");
1986 let manager = AutomationManager::open_for_test(dir.path().to_path_buf()).expect("manager");
1987 upsert_keepalive(&manager, dir.path(), false, &route_fixture_config(), None)
1988 .expect("upsert");
1989 pause_keepalive(&manager).expect("pause");
1990 let paused = manager
1991 .get_automation(OPERATE_KEEPALIVE_ID)
1992 .expect("keepalive");
1993 assert_eq!(paused.status, AutomationStatus::Paused);
1994 assert_eq!(paused.next_run_at, None, "nothing fires after cancel");
1995
1996 // Pausing is idempotent and a missing keepalive is not an error.
1997 pause_keepalive(&manager).expect("pause again");
1998 let empty =
1999 AutomationManager::open_for_test(dir.path().join("empty")).expect("empty manager");
2000 pause_keepalive(&empty).expect("missing keepalive is a no-op");
2001 assert!(!kick_keepalive(&empty).expect("kick missing"));
2002
2003 // A fresh start reactivates the keepalive.
2004 upsert_keepalive(&manager, dir.path(), false, &route_fixture_config(), None)
2005 .expect("reactivate");
2006 let active = manager
2007 .get_automation(OPERATE_KEEPALIVE_ID)
2008 .expect("keepalive");
2009 assert_eq!(active.status, AutomationStatus::Active);
2010
2011 // Kicks only touch an active keepalive.
2012 assert!(kick_keepalive(&manager).expect("kick"));
2013 pause_keepalive(&manager).expect("pause");
2014 assert!(!kick_keepalive(&manager).expect("kick paused"));
2015 }
2016
2017 #[test]
2018 fn pause_keepalive_refuses_to_report_a_stop_it_cannot_confirm() {
2019 let dir = TempDir::new().expect("temp");
2020 let manager = AutomationManager::open_for_test(dir.path().to_path_buf()).expect("manager");
2021 upsert_keepalive(&manager, dir.path(), false, &route_fixture_config(), None)
2022 .expect("upsert");
2023 // Fault fixture: the keepalive record exists but cannot be parsed, so
2024 // whether it is still Active (and spending) is unknown.
2025 let record = dir
2026 .path()
2027 .join("automations")
2028 .join(format!("{OPERATE_KEEPALIVE_ID}.json"));
2029 assert!(record.exists(), "fixture targets the real record path");
2030 fs::write(&record, b"{\"status\": \"active\", truncated").expect("corrupt record");
2031
2032 let error = pause_keepalive(&manager).expect_err("an unconfirmed stop must fail");
2033 assert!(
2034 format!("{error:#}").contains("could not be established"),
2035 "{error:#}"
2036 );
2037 }
2038
2039 #[test]
2040 fn explicit_env_paths_reject_traversal() {
2041 assert!(explicit_file_path(" /tmp/does-not-exist.md ").is_none());
2042 assert!(explicit_file_path("").is_none());
2043 assert!(explicit_file_path("/tmp/../etc/passwd").is_none());
2044 // Keep the temp dir alive for the whole assertion: dropping it first
2045 // would delete the file under the path.
2046 let dir = TempDir::new().expect("temp");
2047 let checker = dir.path().join("check.py");
2048 fs::write(&checker, "# marker").expect("write");
2049 let found =
2050 explicit_file_path(checker.to_str().expect("utf8")).expect("regular file accepted");
2051 assert_eq!(found, checker);
2052 assert!(explicit_dir_path("../escape").is_none());
2053 assert!(explicit_dir_path("ops/inside").is_some());
2054 }
2055
2056 #[test]
2057 fn missing_credentials_fail_closed() {
2058 let dir = TempDir::new().expect("temp");
2059 let store = OperationStore::open(dir.path()).expect("store");
2060 let op = start_operation(
2061 &store,
2062 dir.path(),
2063 Some("Do not spend silently".into()),
2064 None,
2065 false,
2066 "selected-model",
2067 )
2068 .expect("start");
2069 assert_eq!(op.status, OperateStatus::IdleBlocked);
2070 assert_eq!(
2071 op.idle_blocked_reason,
2072 Some(OperateIdleReason::MissingCredentials)
2073 );
2074 assert!(!op.workers_admitted);
2075 assert_eq!(op.writers_in_flight, 0);
2076 }
2077
2078 #[test]
2079 fn cancel_stays_cancelled_through_keep_alive() {
2080 let dir = TempDir::new().expect("temp");
2081 let store = OperationStore::open(dir.path()).expect("store");
2082 start_operation(
2083 &store,
2084 dir.path(),
2085 Some("Stop".into()),
2086 None,
2087 true,
2088 "selected-model",
2089 )
2090 .expect("start");
2091 let cancelled = cancel_operation(&store).expect("cancel").expect("present");
2092 let mut kept = cancelled;
2093 keep_alive_observation(&mut kept, Some(40.0), Some(999.0), None, None);
2094 assert_eq!(kept.status, OperateStatus::Cancelled);
2095 assert!(!kept.workers_admitted);
2096 }
2097
2098 #[test]
2099 fn lead_plan_is_the_gantt_model() {
2100 let mut op = with_credentials(Operation::new("Scout\nWrite", None));
2101 op.plan_from_direction();
2102 let plan = op.lead_plan.as_ref().expect("plan");
2103 assert_eq!(plan.slices.len(), 2);
2104 assert_eq!(plan.slices[0].owner_id, "lead");
2105 assert_eq!(plan.slices[1].depends_on, vec!["slice-1".to_string()]);
2106 assert_eq!(plan.slices[0].start_offset_sec, 0);
2107 assert!(plan.slices[0].duration_sec >= 1);
2108 let board = render_plan_board(&op);
2109 assert!(board.contains("gantt time →"), "{board}");
2110 assert!(board.contains("leadPlan"), "{board}");
2111 assert!(board.contains("No cap"), "{board}");
2112 }
2113
2114 #[test]
2115 fn empty_direction_is_idle_blocked() {
2116 let op = with_credentials(Operation::new("", None));
2117 assert_eq!(op.status, OperateStatus::IdleBlocked);
2118 assert_eq!(
2119 op.idle_blocked_reason,
2120 Some(OperateIdleReason::DirectionEmpty)
2121 );
2122 }
2123
2124 #[test]
2125 fn human_gates_do_not_include_merge() {
2126 assert!(human_gate_for("deploy"));
2127 assert!(human_gate_for("billing"));
2128 assert!(!human_gate_for("merge"));
2129 }
2130
2131 #[test]
2132 fn calls_landed_checker_flags() {
2133 assert_eq!(
2134 check_auto_merge_args("codewhale-hq/CodeWhale", "1234", "keel"),
2135 vec![
2136 "scripts/check-auto-merge.py",
2137 "--repo",
2138 "codewhale-hq/CodeWhale",
2139 "--pr",
2140 "1234",
2141 "--agent",
2142 "keel"
2143 ]
2144 );
2145 assert_eq!(
2146 auto_merge_pr_args("codewhale-hq/CodeWhale", "1234", "keel")[0],
2147 "scripts/auto-merge-pr.py"
2148 );
2149 let deny = evaluate_auto_merge(
2150 AutoMergeRequest {
2151 pr: "12",
2152 role: "keel",
2153 repo: "codewhale-hq/CodeWhale",
2154 },
2155 None,
2156 );
2157 assert!(matches!(deny, AutoMergeDecision::Deny { .. }));
2158 let _ = AUTO_MERGE_CHECKER_ENV;
2159 let _ = discover_auto_merge_checker(Path::new("/no-ops-here"));
2160 }
2161
2162 #[test]
2163 fn auto_merge_request_fields_are_validated_before_spawn() {
2164 let ok = |repo, pr, role| {
2165 validate_auto_merge_request(&AutoMergeRequest { pr, role, repo }).is_ok()
2166 };
2167 assert!(ok("codewhale-hq/CodeWhale", "1234", "keel"));
2168 assert!(ok("a-b/c.d_e-f", "1", "scout_2"));
2169 for (repo, pr, role) in [
2170 ("Hmbown", "1", "keel"),
2171 ("a/b/../../x", "1", "keel"),
2172 ("-x/y", "1", "keel"),
2173 ("x/-y", "1", "keel"),
2174 ("x/..", "1", "keel"),
2175 ("x y/z", "1", "keel"),
2176 ("x/y", "0", "keel"),
2177 ("x/y", "-1", "keel"),
2178 ("x/y", "1 2", "keel"),
2179 ("x/y", "12345678901", "keel"),
2180 ("x/y", "", "keel"),
2181 ("x/y", "1", ""),
2182 ("x/y", "1", "--fixture=/x"),
2183 ("x/y", "1", "keel ops"),
2184 ] {
2185 assert!(
2186 !ok(repo, pr, role),
2187 "{repo:?} {pr:?} {role:?} must be rejected"
2188 );
2189 }
2190 // A malformed request is denied even when a checker exists, so the
2191 // checker is never spawned with it.
2192 let dir = TempDir::new().expect("temp");
2193 let checker = dir.path().join("check-auto-merge.py");
2194 fs::write(&checker, "import sys\nsys.exit(0)\n").expect("write");
2195 assert!(matches!(
2196 evaluate_auto_merge(
2197 AutoMergeRequest {
2198 pr: "1",
2199 role: "--policy=x",
2200 repo: "x/y",
2201 },
2202 Some(&checker),
2203 ),
2204 AutoMergeDecision::Deny { .. }
2205 ));
2206 }
2207
2208 #[test]
2209 fn checker_exit_zero_allows() {
2210 let dir = TempDir::new().expect("temp");
2211 let checker = dir.path().join("check-auto-merge.py");
2212 fs::write(
2213 &checker,
2214 "#!/usr/bin/env python3\nimport argparse, sys\np=argparse.ArgumentParser()\np.add_argument('--repo')\np.add_argument('--pr')\np.add_argument('--agent', required=True)\np.parse_args()\nsys.exit(0)\n",
2215 )
2216 .expect("write");
2217 assert_eq!(
2218 evaluate_auto_merge(
2219 AutoMergeRequest {
2220 pr: "42",
2221 role: "keel",
2222 repo: "codewhale-hq/CodeWhale",
2223 },
2224 Some(&checker),
2225 ),
2226 AutoMergeDecision::Allow
2227 );
2228 }
2229
2230 #[test]
2231 fn plan_board_localizes_chrome_but_not_contract_tokens() {
2232 let mut op = with_credentials(Operation::new("Scout\nWrite", None));
2233 op.plan_from_direction();
2234 let english = render_plan_board_locale(&op, codewhale_localization::Locale::En);
2235 assert!(english.contains("gantt time →"), "{english}");
2236 assert!(english.contains("burn No cap"), "{english}");
2237 let japanese = render_plan_board_locale(&op, codewhale_localization::Locale::Ja);
2238 assert!(japanese.contains("ガント"), "{japanese}");
2239 // Contract tokens stay verbatim in every locale.
2240 assert!(japanese.contains("slice-1"), "{japanese}");
2241 assert!(japanese.contains(&op.id), "{japanese}");
2242 }
2243
2244 #[test]
2245 fn keepalive_automation_and_defaults() {
2246 let dir = TempDir::new().expect("temp");
2247 let manager = AutomationManager::open_for_test(dir.path().to_path_buf()).expect("manager");
2248 upsert_keepalive(&manager, dir.path(), false, &route_fixture_config(), None)
2249 .expect("upsert");
2250 let record = manager
2251 .get_automation(OPERATE_KEEPALIVE_ID)
2252 .expect("keepalive");
2253 assert_eq!(record.model.as_deref(), Some("same-model"));
2254 assert_eq!(record.mode.as_deref(), Some("operate"));
2255 assert_eq!(record.cwds, vec![dir.path().to_path_buf()]);
2256 assert_eq!(
2257 record.next_run_at, None,
2258 "without a kick the hourly recurrence owns the next run"
2259 );
2260 assert_eq!(OPERATE_MAX_WRITERS, 3);
2261 }
2262 }
2263
2263 lines RUST