sid-isnt-done 0.6.0

sid is a UNIX-inspired coding agent for Anthropic-compatible APIs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Argument parsing for the `agent` and `judge` builtins and the `ralph`
//! command line.
//!
//! stdin is context, argv is instruction: both builtins accept a service name
//! and an optional instruction string.  The judge additionally accepts
//! lifecycle flags (`--jury N | --soak N`, `--goldfish`, `--pedantic`,
//! `--seed=...`).

/// How the judge's pinned transcript is seeded from the launching thread.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SeedMode {
    /// Full-if-fits, else compact (the default).
    #[default]
    Auto,
    /// Seed verbatim from the launch thread.
    Full,
    /// Seed from a compacted summary of the launch thread.
    Compact,
    /// Start the judge cold.
    None,
}

impl SeedMode {
    fn parse(value: &str) -> Result<SeedMode, String> {
        match value {
            "auto" => Ok(SeedMode::Auto),
            "full" => Ok(SeedMode::Full),
            "compact" => Ok(SeedMode::Compact),
            "none" => Ok(SeedMode::None),
            other => Err(format!(
                "invalid --seed value {other:?}; expected full|compact|none"
            )),
        }
    }
}

/// Verdict gating mode for a judge invocation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum JudgeMode {
    /// One sample, one verdict.
    Single,
    /// N independent goldfish samples; all must pass.
    Jury(u32),
    /// N consecutive passes across loop iterations; the counter persists in
    /// run state.
    Soak(u32),
}

/// A parsed `agent NAME [INSTR]` invocation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AgentArgs {
    /// Service name from agents.conf.
    pub service: String,
    /// Optional instruction (argv); stdin carries context.
    pub instruction: Option<String>,
}

impl AgentArgs {
    /// Parse `agent NAME [INSTR]` from argv (excluding argv[0]).
    pub fn parse(args: &[String]) -> Result<AgentArgs, String> {
        match args {
            [] => Err("usage: agent NAME [INSTR]".to_string()),
            [service] => Ok(AgentArgs {
                service: service.clone(),
                instruction: None,
            }),
            [service, instruction] => Ok(AgentArgs {
                service: service.clone(),
                instruction: Some(instruction.clone()),
            }),
            _ => Err("usage: agent NAME [INSTR] (too many arguments)".to_string()),
        }
    }
}

/// A parsed `judge NAME [flags] [INSTR]` invocation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct JudgeArgs {
    /// Service name from agents.conf.
    pub service: String,
    /// Optional instruction (argv); stdin carries context.
    pub instruction: Option<String>,
    /// Jury/soak gating.
    pub mode: JudgeMode,
    /// Truncate the judge's transcript to its seed before each call.
    pub goldfish: bool,
    /// Treat suggestion findings as required.
    pub pedantic: bool,
    /// Seeding policy for the pinned judge transcript.
    pub seed: SeedMode,
}

impl JudgeArgs {
    /// Parse `judge NAME [--jury N | --soak N] [--goldfish] [--pedantic]
    /// [--seed=MODE] [INSTR]` from argv (excluding argv[0]).
    pub fn parse(args: &[String]) -> Result<JudgeArgs, String> {
        const USAGE: &str = "usage: judge NAME [--jury N | --soak N] [--goldfish] [--pedantic] [--seed=MODE] [INSTR]";
        let mut service = None;
        let mut instruction = None;
        let mut jury = None;
        let mut soak = None;
        let mut goldfish = false;
        let mut pedantic = false;
        let mut seed = SeedMode::default();

        let mut iter = args.iter().peekable();
        while let Some(arg) = iter.next() {
            match arg.as_str() {
                "--goldfish" => goldfish = true,
                "--pedantic" => pedantic = true,
                "--jury" => {
                    let value = iter.next().ok_or("--jury requires a count")?;
                    jury = Some(parse_count("--jury", value)?);
                }
                "--soak" => {
                    let value = iter.next().ok_or("--soak requires a count")?;
                    soak = Some(parse_count("--soak", value)?);
                }
                other if other.starts_with("--jury=") => {
                    jury = Some(parse_count("--jury", &other["--jury=".len()..])?);
                }
                other if other.starts_with("--soak=") => {
                    soak = Some(parse_count("--soak", &other["--soak=".len()..])?);
                }
                other if other.starts_with("--seed=") => {
                    seed = SeedMode::parse(&other["--seed=".len()..])?;
                }
                other if other.starts_with("--") => {
                    return Err(format!("unknown judge flag {other:?}\n{USAGE}"));
                }
                _ => {
                    if service.is_none() {
                        service = Some(arg.clone());
                    } else if instruction.is_none() {
                        instruction = Some(arg.clone());
                    } else {
                        return Err(format!("too many arguments\n{USAGE}"));
                    }
                }
            }
        }

        let service = service.ok_or(USAGE)?;
        let mode = match (jury, soak) {
            (Some(_), Some(_)) => {
                return Err("--jury and --soak are mutually exclusive".to_string());
            }
            (Some(n), None) => JudgeMode::Jury(n),
            (None, Some(n)) => JudgeMode::Soak(n),
            (None, None) => JudgeMode::Single,
        };
        Ok(JudgeArgs {
            service,
            instruction,
            mode,
            goldfish,
            pedantic,
            seed,
        })
    }
}

fn parse_count(flag: &str, value: &str) -> Result<u32, String> {
    let count: u32 = value
        .parse()
        .map_err(|_| format!("{flag} requires a positive integer, got {value:?}"))?;
    if count == 0 {
        return Err(format!("{flag} requires a positive integer, got 0"));
    }
    Ok(count)
}

/// Parsed `ralph SCRIPT [--max-iters N] [--budget TOKENS] [--resume RUN_ID] [-- ARGS...]`.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct RunArgs {
    /// Path to the script, resolved against the workspace then the
    /// configuration root.
    pub script: String,
    /// Cap on agent invocations across the run.
    pub max_iters: Option<u64>,
    /// Cap on total tokens across all child sessions.
    pub budget: Option<u64>,
    /// Resume an interrupted run by id.
    pub resume: Option<String>,
    /// Arguments for the script, exposed as positional parameters `$1`, `$2`, ….
    pub script_args: Vec<String>,
}

impl RunArgs {
    /// Parse the `ralph` command line (excluding argv[0]).  Flags may appear
    /// anywhere; the first positional is the script and later positionals are
    /// passed through to it.  `--` stops flag parsing.
    pub fn parse(args: &[String]) -> Result<RunArgs, String> {
        const USAGE: &str = "usage: ralph SCRIPT [--max-iters N] [--budget TOKENS] [--resume RUN_ID] [--] [ARGS...]";
        let mut script = None;
        let mut max_iters = None;
        let mut budget = None;
        let mut resume = None;
        let mut script_args = Vec::new();
        let mut flags_done = false;
        let mut iter = args.iter();
        while let Some(token) = iter.next() {
            if flags_done {
                if script.is_none() {
                    script = Some(token.clone());
                } else {
                    script_args.push(token.clone());
                }
                continue;
            }
            match token.as_str() {
                "--" => flags_done = true,
                "--max-iters" => {
                    let value = iter.next().ok_or("--max-iters requires a count")?;
                    max_iters = Some(parse_u64("--max-iters", value)?);
                }
                "--budget" => {
                    let value = iter.next().ok_or("--budget requires a token count")?;
                    budget = Some(parse_u64("--budget", value)?);
                }
                "--resume" => {
                    let value = iter.next().ok_or("--resume requires a run id")?;
                    resume = Some(value.to_string());
                }
                other if other.starts_with("--max-iters=") => {
                    max_iters = Some(parse_u64("--max-iters", &other["--max-iters=".len()..])?);
                }
                other if other.starts_with("--budget=") => {
                    budget = Some(parse_u64("--budget", &other["--budget=".len()..])?);
                }
                other if other.starts_with("--resume=") => {
                    resume = Some(other["--resume=".len()..].to_string());
                }
                other if other.starts_with("--") => {
                    return Err(format!("unknown ralph flag {other:?}\n{USAGE}"));
                }
                _ => {
                    if script.is_none() {
                        script = Some(token.clone());
                    } else {
                        script_args.push(token.clone());
                    }
                }
            }
        }
        let script = script.ok_or(USAGE)?;
        Ok(RunArgs {
            script,
            max_iters,
            budget,
            resume,
            script_args,
        })
    }
}

fn parse_u64(flag: &str, value: &str) -> Result<u64, String> {
    value
        .parse()
        .map_err(|_| format!("{flag} requires a non-negative integer, got {value:?}"))
}

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

    fn strings(args: &[&str]) -> Vec<String> {
        args.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn agent_args_parse() {
        assert_eq!(
            AgentArgs::parse(&strings(&["fix"])).unwrap(),
            AgentArgs {
                service: "fix".to_string(),
                instruction: None,
            }
        );
        assert_eq!(
            AgentArgs::parse(&strings(&["fix", "Make CI pass."])).unwrap(),
            AgentArgs {
                service: "fix".to_string(),
                instruction: Some("Make CI pass.".to_string()),
            }
        );
        assert!(AgentArgs::parse(&[]).is_err());
        assert!(AgentArgs::parse(&strings(&["fix", "a", "b"])).is_err());
    }

    #[test]
    fn judge_args_default_mode() {
        let parsed = JudgeArgs::parse(&strings(&["judge", "Is it done?"])).unwrap();
        assert_eq!(parsed.service, "judge");
        assert_eq!(parsed.instruction.as_deref(), Some("Is it done?"));
        assert_eq!(parsed.mode, JudgeMode::Single);
        assert!(!parsed.goldfish);
        assert!(!parsed.pedantic);
        assert_eq!(parsed.seed, SeedMode::Auto);
    }

    #[test]
    fn judge_args_soak_and_flags() {
        let parsed = JudgeArgs::parse(&strings(&[
            "judge",
            "--soak",
            "3",
            "--goldfish",
            "--pedantic",
            "--seed=none",
            "CI passes. Done?",
        ]))
        .unwrap();
        assert_eq!(parsed.mode, JudgeMode::Soak(3));
        assert!(parsed.goldfish);
        assert!(parsed.pedantic);
        assert_eq!(parsed.seed, SeedMode::None);
        assert_eq!(parsed.instruction.as_deref(), Some("CI passes. Done?"));
    }

    #[test]
    fn judge_args_jury_equals_form() {
        let parsed = JudgeArgs::parse(&strings(&["judge", "--jury=3"])).unwrap();
        assert_eq!(parsed.mode, JudgeMode::Jury(3));
    }

    #[test]
    fn judge_args_jury_soak_mutually_exclusive() {
        let err = JudgeArgs::parse(&strings(&["judge", "--jury", "2", "--soak", "3"])).unwrap_err();
        assert!(err.contains("mutually exclusive"));
    }

    #[test]
    fn judge_args_reject_zero_and_garbage_counts() {
        assert!(JudgeArgs::parse(&strings(&["judge", "--soak", "0"])).is_err());
        assert!(JudgeArgs::parse(&strings(&["judge", "--jury", "lots"])).is_err());
        assert!(JudgeArgs::parse(&strings(&["judge", "--soak"])).is_err());
    }

    #[test]
    fn judge_args_unknown_flag() {
        assert!(JudgeArgs::parse(&strings(&["judge", "--parallel"])).is_err());
    }

    #[test]
    fn judge_args_missing_service() {
        assert!(JudgeArgs::parse(&strings(&["--goldfish"])).is_err());
    }

    #[test]
    fn judge_seed_modes() {
        for (text, mode) in [
            ("--seed=auto", SeedMode::Auto),
            ("--seed=full", SeedMode::Full),
            ("--seed=compact", SeedMode::Compact),
            ("--seed=none", SeedMode::None),
        ] {
            let parsed = JudgeArgs::parse(&strings(&["judge", text])).unwrap();
            assert_eq!(parsed.seed, mode);
        }
        assert!(JudgeArgs::parse(&strings(&["judge", "--seed=verbose"])).is_err());
    }

    #[test]
    fn run_args_parse() {
        let parsed = RunArgs::parse(&strings(&[
            "ralph.sid",
            "--max-iters",
            "25",
            "--budget",
            "1000000",
        ]))
        .unwrap();
        assert_eq!(parsed.script, "ralph.sid");
        assert_eq!(parsed.max_iters, Some(25));
        assert_eq!(parsed.budget, Some(1_000_000));
        assert_eq!(parsed.resume, None);
        assert!(parsed.script_args.is_empty());
    }

    #[test]
    fn run_args_resume_equals_form() {
        let parsed =
            RunArgs::parse(&strings(&["ralph.sid", "--resume=2026-06-10T14-22-07"])).unwrap();
        assert_eq!(parsed.resume.as_deref(), Some("2026-06-10T14-22-07"));
    }

    #[test]
    fn run_args_collects_script_args() {
        let parsed =
            RunArgs::parse(&strings(&["ralph.sid", "PLAN.md", "--max-iters", "5"])).unwrap();
        assert_eq!(parsed.script, "ralph.sid");
        assert_eq!(parsed.max_iters, Some(5));
        assert_eq!(parsed.script_args, vec!["PLAN.md".to_string()]);
    }

    #[test]
    fn run_args_double_dash_stops_flag_parsing() {
        let parsed = RunArgs::parse(&strings(&["ralph.sid", "--", "--max-iters", "5"])).unwrap();
        assert_eq!(parsed.script, "ralph.sid");
        assert_eq!(parsed.max_iters, None);
        assert_eq!(
            parsed.script_args,
            vec!["--max-iters".to_string(), "5".to_string()]
        );
    }

    #[test]
    fn run_args_double_dash_still_yields_script() {
        let parsed = RunArgs::parse(&strings(&["--", "ralph.sid", "arg"])).unwrap();
        assert_eq!(parsed.script, "ralph.sid");
        assert_eq!(parsed.script_args, vec!["arg".to_string()]);
    }

    #[test]
    fn run_args_require_script() {
        assert!(RunArgs::parse(&strings(&[])).is_err());
        assert!(RunArgs::parse(&strings(&["--max-iters", "3"])).is_err());
        assert!(RunArgs::parse(&strings(&["a.sid", "--frobnicate"])).is_err());
    }
}