argonauts-learning 0.1.0

Argonauts Code learning: the outcome log and track records, routing and predictions, learned workflows and the evolution loop, replays, skills, and memory safety.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! A learned workflow as step-by-step instructions for one agent. The body is built from the
//! learned workflow's shape: what it's for, its phases and steps, its checklist, and its checks by
//! name. It never reads a setting's value, the environment, or a check's command, so none can leak
//! into a skill every later session reads.

use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::sync::LazyLock;

use regex::Regex;
use serde_json::Value;

use argonauts_core::workflow::{Phase, Step, StepKind};

use super::document::{self, MAX_DESCRIPTION};
use super::{Frontmatter, META_LEARNED_WORKFLOW, META_SHA256, META_VERSION, Skill};
use crate::error::{LearningError, Result};
use crate::learned_workflows::{LearnedWorkflow, LoadedLearnedWorkflow, Match};
use crate::scan::{Finding, scan};

pub fn export(loaded: &LoadedLearnedWorkflow) -> Result<Skill> {
    let learned_workflow = &loaded.learned_workflow;
    let name = learned_workflow.name.clone();
    let body = document::written_body(&body(learned_workflow));
    let mut metadata = BTreeMap::new();
    metadata.insert(META_LEARNED_WORKFLOW.to_owned(), name.clone());
    metadata.insert(
        META_VERSION.to_owned(),
        learned_workflow.version.to_string(),
    );
    metadata.insert(META_SHA256.to_owned(), document::body_hash(&body));
    let skill = Skill {
        frontmatter: Frontmatter {
            name: name.clone(),
            description: description(learned_workflow),
            license: None,
            compatibility: None,
            metadata,
            allowed_tools: None,
        },
        body,
    };
    let text = document::render(&skill);
    refuse_unsafe(&text)?;
    let problems = document::validate(&text, &name);
    if !problems.is_empty() {
        return Err(LearningError::Refused(format!(
            "The {name} workflow can't be exported as a skill: {}.",
            problems.join("; ")
        )));
    }
    Ok(skill)
}

/// Refuses a secret by its kind only, hidden text, and paths under a home folder.
fn refuse_unsafe(text: &str) -> Result<()> {
    let findings = scan(text);
    if let Some(Finding::Secret { kind }) = findings
        .iter()
        .find(|finding| matches!(finding, Finding::Secret { .. }))
    {
        return Err(LearningError::Refused(format!(
            "The skill would carry a {kind}, so the export is refused. Take it out of the \
             learned workflow first."
        )));
    }
    if let Some(hidden) = findings.iter().find(|finding| finding.refuses()) {
        return Err(LearningError::Refused(format!(
            "The skill would carry {hidden}, so the export is refused."
        )));
    }
    if holds_home_path(text) {
        return Err(LearningError::Refused(
            "The skill would carry a path under a home folder, so the export is refused. Use a \
             path inside the repository instead."
                .to_owned(),
        ));
    }
    Ok(())
}

static HOME_PATH: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?:^|[\s`'\x22(=:])(?:~/|/Users/[^/\s]+/|/home/[^/\s]+/|[A-Za-z]:\\Users\\)")
        .unwrap_or_else(|err| panic!("home path pattern: {err}"))
});

fn holds_home_path(text: &str) -> bool {
    if HOME_PATH.is_match(text) {
        return true;
    }
    std::env::var("HOME")
        .ok()
        .filter(|home| home.len() > 1)
        .is_some_and(|home| text.contains(home.trim_end_matches('/')))
}

fn description(learned_workflow: &LearnedWorkflow) -> String {
    let mut text = one_line(&learned_workflow.description);
    if text.is_empty() {
        text = format!(
            "The {} workflow, as steps one agent can follow.",
            learned_workflow.name
        );
    }
    if !text.ends_with(['.', '!', '?']) {
        text.push('.');
    }
    if let Some(when) = when_to_use(&learned_workflow.matches) {
        text.push(' ');
        text.push_str(&when);
    }
    document::cut(&text, MAX_DESCRIPTION)
}

fn one_line(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn when_to_use(matches: &Match) -> Option<String> {
    let types = listed(&matches.task_types);
    let labels = listed(&matches.labels_any);
    match (types, labels) {
        (Some(types), Some(labels)) => Some(format!(
            "Use it for {types} tasks, or tasks labeled {labels}."
        )),
        (Some(types), None) => Some(format!("Use it for {types} tasks.")),
        (None, Some(labels)) => Some(format!("Use it for tasks labeled {labels}.")),
        (None, None) => None,
    }
}

/// "a", "a or b", "a, b, or c".
fn listed(items: &[String]) -> Option<String> {
    let items: Vec<String> = items
        .iter()
        .map(|item| one_line(item))
        .filter(|item| !item.is_empty())
        .collect();
    match items.as_slice() {
        [] => None,
        [one] => Some(one.clone()),
        [first, second] => Some(format!("{first} or {second}")),
        [rest @ .., last] => Some(format!("{}, or {last}", rest.join(", "))),
    }
}

fn body(learned_workflow: &LearnedWorkflow) -> String {
    let name = &learned_workflow.name;
    let mut text = format!("# {name}\n\n");
    let summary = one_line(&learned_workflow.description);
    if !summary.is_empty() {
        let _ = writeln!(text, "{summary}\n");
    }
    if let Some(when) = when_to_use(&learned_workflow.matches) {
        let _ = writeln!(text, "{when}\n");
    }
    let _ = writeln!(
        text,
        "These steps are version {} of `{name}`, the learned workflow that worked best for \
         this kind of task when a crew of agents ran it in this repository, written out for one \
         agent working alone.\n",
        learned_workflow.version
    );

    let checklist = checklist(learned_workflow);
    let checks = checks(learned_workflow);
    text.push_str("## Steps\n\n");
    let phases = inline_phases(learned_workflow)
        .unwrap_or_else(|| default_phases(learned_workflow, &checklist, &checks));
    for (index, (title, steps)) in phases.iter().enumerate() {
        let _ = writeln!(text, "### {}. {title}\n", index + 1);
        for (number, step) in steps.iter().enumerate() {
            let _ = writeln!(text, "{}. {step}", number + 1);
        }
        text.push('\n');
    }

    if !checklist.is_empty() {
        text.push_str("## Checklist\n\nCheck the plan against each item before you build.\n\n");
        for item in &checklist {
            let _ = writeln!(text, "- {item}");
        }
        text.push('\n');
    }
    if !checks.is_empty() {
        text.push_str("## Checks\n\nThe work is done only when each of these holds.\n\n");
        for check in &checks {
            let _ = writeln!(text, "- {check}");
        }
        text.push('\n');
    }
    text.push_str(
        "## Limits\n\nLeave approvals and merging to the person you work for. Never weaken, skip, \
         or delete a test to make it pass, and never commit a secret.\n",
    );
    text
}

type Phases = Vec<(String, Vec<String>)>;

fn section<'a>(learned_workflow: &'a LearnedWorkflow, key: &str) -> Option<&'a Value> {
    learned_workflow
        .settings
        .get(key)
        .filter(|value| !value.is_null())
}

fn enabled(learned_workflow: &LearnedWorkflow, key: &str) -> bool {
    section(learned_workflow, key)
        .is_some_and(|value| value.get("enabled").and_then(Value::as_bool) != Some(false))
}

/// The protocol's phases, with a step for each part the learned workflow turns on.
fn default_phases(
    learned_workflow: &LearnedWorkflow,
    checklist: &[String],
    checks: &[String],
) -> Phases {
    let mut plan = vec![
        "Draft a plan on your own first: the change, the files it touches, and how you'll test \
         it."
        .to_owned(),
    ];
    if enabled(learned_workflow, "experiments") {
        plan.push(
            "Try the riskiest assumption in a small throwaway experiment, and keep only what you \
             learn."
                .to_owned(),
        );
    }
    if !checklist.is_empty() || enabled(learned_workflow, "critique") {
        plan.push(
            "Critique the plan as a skeptic from another team would, against the checklist \
             below, and fix what it finds."
                .to_owned(),
        );
    }
    if enabled(learned_workflow, "judging") {
        plan.push(
            "If you drafted more than one option, compare them on the evidence and keep the \
             strongest."
                .to_owned(),
        );
    }

    let mut build = Vec::new();
    if let Some(divide) = section(learned_workflow, "divide") {
        let mut step = "Split the work into small pieces with clear interfaces.".to_owned();
        if divide.get("contracts_first").and_then(Value::as_bool) == Some(true) {
            step.push_str(" Write the interfaces and their acceptance tests first.");
        }
        build.push(step);
    }
    build.push("Build in small steps, running the tests as you go.".to_owned());

    let mut check = vec![if checks.is_empty() {
        "Run the project's tests and linters, and fix what fails.".to_owned()
    } else {
        "Check each check below, and fix what fails.".to_owned()
    }];
    check.push(
        "Review your own diff as a reviewer from another team would: correctness, tests, \
         interfaces, and anything risky."
            .to_owned(),
    );

    vec![
        (
            "Understand".to_owned(),
            vec![
                "Read the request back: the goal, the acceptance criteria, and what's out of \
                 scope, in your own words."
                    .to_owned(),
                "Read the code the task touches before you change anything.".to_owned(),
            ],
        ),
        ("Plan".to_owned(), plan),
        ("Build".to_owned(), build),
        ("Check".to_owned(), check),
        (
            "Finish".to_owned(),
            vec![
                "Summarize what changed, how you tested it, and anything left for a person to \
                 decide."
                    .to_owned(),
            ],
        ),
    ]
}

/// A learned workflow that writes its workflow's phases out in full.
fn inline_phases(learned_workflow: &LearnedWorkflow) -> Option<Phases> {
    let phases: Vec<Phase> =
        serde_json::from_value(section(learned_workflow, "phases")?.clone()).ok()?;
    let phases: Phases = phases
        .iter()
        .map(|phase| {
            let title = one_line(&phase.name);
            let steps = phase
                .steps
                .iter()
                .map(|step| instruction(step, 0))
                .collect();
            (title, steps)
        })
        .filter(|(title, steps): &(String, Vec<String>)| !title.is_empty() && !steps.is_empty())
        .collect();
    (!phases.is_empty()).then_some(phases)
}

/// One step as a sentence for one agent. Loops and per-item steps carry their inner steps as
/// an indented list.
fn instruction(step: &Step, depth: usize) -> String {
    let text = match step.kind {
        StepKind::Read => {
            "Read the request back: the goal, the acceptance criteria, and what's out of scope, in your own words."
                .to_owned()
        }
        StepKind::Compare => "Write down where the options disagree, quoting each.".to_owned(),
        StepKind::Draft if step.blind => {
            "Draft your own answer before you look at anyone else's.".to_owned()
        }
        StepKind::Draft => "Draft an answer.".to_owned(),
        StepKind::Map => "Compare the drafts point by point, quoting each.".to_owned(),
        StepKind::FanOut => "Work through the task from each angle in turn, separately.".to_owned(),
        StepKind::ForEach => "For each item of the earlier output:".to_owned(),
        StepKind::Review => {
            "Review the work as a skeptical reviewer from another team would.".to_owned()
        }
        StepKind::Judge => {
            "Score the candidates against the criteria, and keep the strongest on the evidence."
                .to_owned()
        }
        StepKind::Critique => {
            "Try to disprove each finding or claim, and drop what doesn't survive.".to_owned()
        }
        StepKind::Synthesize => "Combine what survived into one answer.".to_owned(),
        StepKind::Build => {
            "Make the change in small steps, running the tests as you go.".to_owned()
        }
        StepKind::Checks => "Run the checks below, and fix what fails.".to_owned(),
        StepKind::GoalCheck => {
            "Check each done-when criterion against evidence: a passing test, or a file and line."
                .to_owned()
        }
        StepKind::LoopUntil => match step.max {
            Some(max) => format!("Repeat these steps until they succeed, at most {max} times:"),
            None => "Repeat these steps until they succeed:".to_owned(),
        },
        StepKind::Approval => {
            "Stop and check with the person you work for before you go on.".to_owned()
        }
        StepKind::Merge => "Hand the change over for review. Never merge it yourself.".to_owned(),
        StepKind::Workflow => "Follow the nested workflow's steps.".to_owned(),
        StepKind::Script => "Follow the steps of the workflow in use.".to_owned(),
    };
    if step.steps.is_empty() || depth > 2 {
        return text;
    }
    let indent = "   ".repeat(depth + 1);
    let mut text = text;
    for inner in &step.steps {
        let _ = write!(text, "\n{indent}- {}", instruction(inner, depth + 1));
    }
    text
}

/// What a critic checks, with a question for each item the protocol knows.
fn checklist(learned_workflow: &LearnedWorkflow) -> Vec<String> {
    let Some(items) =
        section(learned_workflow, "critique").and_then(|critique| critique.get("checklist"))
    else {
        return Vec::new();
    };
    strings(items)
        .into_iter()
        .map(|item| match item.as_str() {
            "criteria" => "Does it meet every acceptance criterion?".to_owned(),
            "risky_assumption" => {
                "What's the riskiest assumption, and how will you check it?".to_owned()
            }
            "missing_steps" => {
                "Is a step missing, such as a migration, a config change, or docs?".to_owned()
            }
            "testability" => "Can each part be tested, and how?".to_owned(),
            "interfaces" => "Are the interfaces clear, and do they fit their callers?".to_owned(),
            "premortem" => {
                "If this failed in a month, what would most likely be the cause?".to_owned()
            }
            "simpler_plan" => "Is there a simpler plan that does the job?".to_owned(),
            "evidence" => "What evidence shows each claim holds?".to_owned(),
            _ => item,
        })
        .collect()
}

/// The checks by name, never their commands.
fn checks(learned_workflow: &LearnedWorkflow) -> Vec<String> {
    let Some(checks) = section(learned_workflow, "checks") else {
        return Vec::new();
    };
    let names = match checks {
        Value::Array(_) => strings(checks),
        other => other.get("run").map(strings).unwrap_or_default(),
    };
    names
        .into_iter()
        .filter(|name| {
            name.len() <= 64
                && name
                    .chars()
                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '_' | '-'))
        })
        .map(|name| {
            let meaning = match name.as_str() {
                "protected_paths" => Some("leave protected paths alone"),
                "test_tampering" => Some("no test is weakened, skipped, or deleted"),
                "secret_scan" => Some("no secret, key, or token in the diff"),
                "dependency_policy" => Some("no dependency the project's policy doesn't allow"),
                "lint" => Some("the linters pass"),
                "types" => Some("the type checker passes"),
                "unit_tests" => Some("the unit tests pass"),
                "acceptance_tests" => Some("the acceptance tests pass"),
                _ => None,
            };
            match meaning {
                Some(meaning) => format!("`{name}`: {meaning}"),
                None => format!("`{name}`"),
            }
        })
        .collect()
}

/// Strings from a list, each on one line, or from one string.
fn strings(value: &Value) -> Vec<String> {
    let items = match value {
        Value::Array(items) => items.iter().filter_map(Value::as_str).collect(),
        Value::String(text) => vec![text.as_str()],
        _ => Vec::new(),
    };
    items
        .into_iter()
        .map(one_line)
        .filter(|item| !item.is_empty())
        .map(|item| document::cut(&item, 200))
        .collect()
}