orchestratectl 0.1.0

Rust CLI for orchestrating AI-agent workflows on a developer's machine.
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
//! `discussion resolve` — domain verb (design.md §2.4).
//!
//! Emits a `discussion.resolved` event under the run's `flock` and updates
//! `discussions/<id>.json` via the reducer in the same locked window.
//!
//! Outcome contract (single `outcome` discriminator in the success payload):
//! - `appended` — first resolution; new event written.
//! - `idempotent-replay` — `--idempotency-key` matched a prior event with
//!   identical payload; original `seq` returned.
//! - `no-op` — discussion already resolved with the same `--choice` (no
//!   key, or no prior key match).
//! - `dry-run` — preflight; the would-be outcome of the real call is
//!   reported in `would_be`.
//!
//! Conflict matrix:
//! - same `--idempotency-key`, different payload → `idempotency_conflict`.
//! - already-resolved, no key, different `--choice` → `discussion_already_resolved`.

use serde::Serialize;
use serde_json::{json, Value};

use octl_core::{ensure_root, read_discussion_opt, Discussion, DiscussionStatus, RunLock};

use crate::error::CliError;
use crate::output::{self, OutputFormat, OutputSpec};
use crate::run::{from_core, parse_discussion_id, require_nonempty, run_paths};

/// Hard caps on free-form CLI strings written into the event log.
/// `event create --from-file` enforces a 1 MiB cap on its JSON payload;
/// matching bounds here prevent a misconfigured agent from bloating the
/// canonical log via the domain-verb path.
const MAX_CHOICE_BYTES: usize = 1024;
const MAX_NOTE_BYTES: usize = 16 * 1024;

pub struct Args<'a> {
    pub run_id: String,
    pub discussion_id: String,
    pub choice: String,
    pub note: Option<String>,
    pub idempotency_key: Option<String>,
    pub dry_run: bool,
    pub spec: &'a OutputSpec,
    pub warnings: &'a [String],
}

#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
enum Outcome {
    Appended,
    IdempotentReplay,
    NoOp,
    DryRun,
}

#[derive(Serialize)]
struct ResolvePayload<'a> {
    run_id: &'a str,
    discussion_id: &'a str,
    node_id: &'a str,
    choice: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    note: Option<&'a str>,
    outcome: Outcome,
    /// `seq` of the appended (or replayed) event; `None` for no-op and
    /// dry-run.
    #[serde(skip_serializing_if = "Option::is_none")]
    seq: Option<u64>,
    /// On `dry-run`, the outcome the real call would produce (`appended`,
    /// `no-op`, or `idempotent-replay`). Conflict cases still surface as
    /// errors even in dry-run, never as a success envelope.
    #[serde(skip_serializing_if = "Option::is_none")]
    would_be: Option<Outcome>,
    /// Files the reducer (would) touch. Empty on no-op since no
    /// projection changes.
    projections: Vec<String>,
}

pub fn run(args: Args<'_>) -> Result<(), CliError> {
    let run_id = args.run_id.clone();
    let discussion_id = parse_discussion_id(&args.discussion_id)?;
    let choice = require_nonempty(&args.choice, "choice")?;
    if choice.len() > MAX_CHOICE_BYTES {
        return Err(CliError::user(
            "invalid_value",
            format!(
                "--choice is {} bytes; max is {} bytes",
                choice.len(),
                MAX_CHOICE_BYTES
            ),
        )
        .with_invalid_value(format!("{} bytes", choice.len())));
    }
    let note = match args.note.as_deref() {
        Some(n) => {
            let n = require_nonempty(n, "note")?;
            if n.len() > MAX_NOTE_BYTES {
                return Err(CliError::user(
                    "invalid_value",
                    format!(
                        "--note is {} bytes; max is {} bytes",
                        n.len(),
                        MAX_NOTE_BYTES
                    ),
                )
                .with_invalid_value(format!("{} bytes", n.len())));
            }
            Some(n)
        }
        None => None,
    };

    let root = crate::home::root_dir()?;
    let paths = run_paths(&root, &run_id)?;

    if !paths.root.is_dir() {
        return Err(
            CliError::user("run_not_found", format!("no run with id {run_id}"))
                .with_invalid_value(&run_id),
        );
    }

    let projections_for = |id: &str| {
        vec![
            format!("discussions/{id}.json"),
            "manifest.json".to_string(),
        ]
    };

    ensure_root(&root).map_err(from_core)?;

    // All authoritative reads happen inside the lock so a concurrent
    // writer can't race the idempotency / domain-state decisions.
    enum LockResult {
        Missing,
        Conflict {
            existing: Discussion,
        },
        IdempotencyConflict {
            prior_seq: u64,
            prior_choice: Option<String>,
            prior_note: Option<String>,
            prior_discussion_id: Option<String>,
        },
        IdempotentReplay {
            seq: u64,
            existing: Discussion,
        },
        NoOp {
            existing: Discussion,
        },
        WouldAppend {
            existing: Discussion,
        },
        Appended {
            seq: u64,
            node_id: String,
        },
    }

    let result = RunLock::with_lock(&paths, |lock| {
        // Idempotency-key scan must run BEFORE the domain status check so
        // that mismatched payloads return `idempotency_conflict`, not
        // `discussion_already_resolved`. A keyed replay then short-circuits
        // even if a concurrent writer raced ahead with the same key.
        if let Some(key) = args.idempotency_key.as_deref() {
            if let Some(prior) =
                octl_core::find_prior_with_key(lock, &paths, "discussion.resolved", key)?
            {
                let prior_discussion_id = prior
                    .data
                    .get("discussion_id")
                    .and_then(Value::as_str)
                    .map(str::to_string);
                let prior_choice = prior
                    .data
                    .get("resolution")
                    .and_then(Value::as_str)
                    .map(str::to_string);
                let prior_note = prior
                    .data
                    .get("note")
                    .and_then(Value::as_str)
                    .map(str::to_string);

                let matches = prior_discussion_id.as_deref() == Some(discussion_id.as_str())
                    && prior_choice.as_deref() == Some(&choice)
                    && prior_note.as_deref() == note.as_deref();

                if !matches {
                    return Ok(LockResult::IdempotencyConflict {
                        prior_seq: prior.seq,
                        prior_choice,
                        prior_note,
                        prior_discussion_id,
                    });
                }

                // Idempotent replay — recover the projection state for
                // the success envelope.
                match read_discussion_opt(&paths, &discussion_id)? {
                    Some(d) => {
                        return Ok(LockResult::IdempotentReplay {
                            seq: prior.seq,
                            existing: d,
                        })
                    }
                    None => return Ok(LockResult::Missing),
                }
            }
        }

        let disc = match read_discussion_opt(&paths, &discussion_id)? {
            Some(d) => d,
            None => return Ok(LockResult::Missing),
        };

        if matches!(disc.status, DiscussionStatus::Resolved) {
            if disc.resolution.as_deref() == Some(&choice) {
                return Ok(LockResult::NoOp { existing: disc });
            }
            return Ok(LockResult::Conflict { existing: disc });
        }

        // Dry-run stops here — we know the operation would append, but
        // the locked critical section is the right place to confirm it.
        if args.dry_run {
            return Ok(LockResult::WouldAppend { existing: disc });
        }

        let mut data = json!({
            "discussion_id": discussion_id.as_str(),
            "resolution": choice,
        });
        if let Some(n) = &note {
            data["note"] = Value::String(n.clone());
        }

        let node_id = disc.node_id.clone();
        let seq = octl_core::append_and_apply_unlocked(
            lock,
            &paths,
            "discussion.resolved",
            Some(&node_id),
            args.idempotency_key.as_deref(),
            data,
        )?;
        Ok(LockResult::Appended {
            seq,
            node_id: node_id.to_string(),
        })
    })
    .map_err(from_core)?;

    match result {
        LockResult::Missing => Err(CliError::user(
            "discussion_not_found",
            format!("no discussion {discussion_id} in run {run_id}"),
        )
        .with_invalid_value(discussion_id.as_str())),

        LockResult::IdempotencyConflict {
            prior_seq,
            prior_choice,
            prior_note,
            prior_discussion_id,
        } => {
            let mut details = json!({
                "prior_seq": prior_seq,
            });
            if let Some(id) = prior_discussion_id {
                details["prior_discussion_id"] = Value::String(id);
            }
            if let Some(c) = prior_choice {
                details["prior_resolution"] = Value::String(c);
            }
            if let Some(n) = prior_note {
                details["prior_note"] = Value::String(n);
            }
            Err(CliError::user(
                "idempotency_conflict",
                "idempotency-key was previously used with a different payload",
            )
            .with_invalid_value(args.idempotency_key.as_deref().unwrap_or(""))
            .with_expected(details))
        }

        LockResult::Conflict { existing } => {
            let existing_choice = existing.resolution.clone().unwrap_or_default();
            let mut details = json!({
                "existing_resolution": existing_choice,
            });
            if let Some(n) = &existing.note {
                details["existing_note"] = Value::String(n.clone());
            }
            if let Some(t) = existing.resolved_at {
                details["resolved_at"] = Value::String(t.to_rfc3339());
            }
            Err(CliError::user(
                "discussion_already_resolved",
                format!("discussion {discussion_id} is already resolved with a different choice"),
            )
            .with_invalid_value(&choice)
            .with_expected(details))
        }

        LockResult::IdempotentReplay { seq, existing } => {
            let payload = ResolvePayload {
                run_id: &run_id,
                discussion_id: discussion_id.as_str(),
                node_id: existing.node_id.as_str(),
                choice: &choice,
                note: note.as_deref(),
                outcome: Outcome::IdempotentReplay,
                seq: Some(seq),
                would_be: None,
                projections: Vec::new(),
            };
            emit(&payload, args.spec, args.warnings)
        }

        LockResult::NoOp { existing } => {
            let payload = ResolvePayload {
                run_id: &run_id,
                discussion_id: discussion_id.as_str(),
                node_id: existing.node_id.as_str(),
                choice: &choice,
                note: existing.note.as_deref(),
                outcome: Outcome::NoOp,
                seq: None,
                would_be: None,
                projections: Vec::new(),
            };
            emit(&payload, args.spec, args.warnings)
        }

        LockResult::WouldAppend { existing } => {
            let payload = ResolvePayload {
                run_id: &run_id,
                discussion_id: discussion_id.as_str(),
                node_id: existing.node_id.as_str(),
                choice: &choice,
                note: note.as_deref(),
                outcome: Outcome::DryRun,
                seq: None,
                would_be: Some(Outcome::Appended),
                projections: projections_for(discussion_id.as_str()),
            };
            emit(&payload, args.spec, args.warnings)
        }

        LockResult::Appended { seq, node_id } => {
            let payload = ResolvePayload {
                run_id: &run_id,
                discussion_id: discussion_id.as_str(),
                node_id: &node_id,
                choice: &choice,
                note: note.as_deref(),
                outcome: Outcome::Appended,
                seq: Some(seq),
                would_be: None,
                projections: projections_for(discussion_id.as_str()),
            };
            emit(&payload, args.spec, args.warnings)
        }
    }
}

fn emit(
    payload: &ResolvePayload<'_>,
    spec: &OutputSpec,
    warnings: &[String],
) -> Result<(), CliError> {
    match spec.format {
        OutputFormat::Json | OutputFormat::Jsonl => {
            return output::emit_envelope(payload, spec, warnings);
        }
        OutputFormat::Text => {}
    }
    {
        println!("run-id:        {}", payload.run_id);
        println!("discussion-id: {}", payload.discussion_id);
        println!("node-id:       {}", payload.node_id);
        println!("choice:        {}", output::escape_one_line(payload.choice));
        if let Some(n) = payload.note {
            println!("note:          {}", output::escape_one_line(n));
        }
        let outcome_label = match payload.outcome {
            Outcome::Appended => "appended",
            Outcome::IdempotentReplay => "idempotent-replay",
            Outcome::NoOp => "no-op (already resolved with same choice)",
            Outcome::DryRun => "dry-run (no filesystem changes)",
        };
        println!("outcome:       {outcome_label}");
        if let Some(s) = payload.seq {
            println!("seq:           {s}");
        }
        if let Some(w) = payload.would_be {
            let w_label = match w {
                Outcome::Appended => "appended",
                Outcome::IdempotentReplay => "idempotent-replay",
                Outcome::NoOp => "no-op",
                Outcome::DryRun => "dry-run",
            };
            println!("would-be:      {w_label}");
        }
        if !payload.projections.is_empty() {
            println!("projections:");
            for p in &payload.projections {
                println!("  - {p}");
            }
        }
        output::emit_text_warnings(warnings);
    }
    Ok(())
}