rhei-cli 0.3.0

Command-line driver for the Rhei agent runtime.
Documentation
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
    // The sections that orient one invocation: where it stands, what already
    // happened to it, and how to reach the rest of the project. The history
    // sections live next door. §FS-rhei-memory.3.1 §FS-rhei-memory.3.3

    /// A machine with one working state, one revisitable state, and two
    /// terminal ones — enough for every condition the memory sections branch on.
    fn memory_machine() -> rhei_validator::StateMachine {
        rhei_validator::StateMachine::from_yaml_str(
            r#"
name: memory-test
version: 1
states:
  pending:
    initial: true
    description: Ready for work
    instructions: Do the work for Task {task_id}.
  review:
    description: Review
    instructions: Review Task {task_id}.
  completed:
    description: Done
    final: true
  cancelled:
    description: Dropped
    final: true
transitions:
  - { from: pending, to: review }
  - { from: review, to: review }
  - { from: review, to: completed }
  - { from: "*", to: cancelled }
"#,
        )
        .expect("machine should parse")
    }

    /// `<label> 1`..`<label> n`, one per line — a body long enough to trip a cap.
    fn repeated_lines(label: &str, count: usize) -> String {
        let mut out = String::new();
        for n in 1..=count {
            out.push_str(label);
            out.push(' ');
            out.push_str(&n.to_string());
            out.push('\n');
        }
        out
    }

    /// Write a fixture tree and load it exactly as a command would.
    fn memory_dir(files: &[(&str, &str)]) -> tempfile::TempDir {
        let dir = tempfile::tempdir().expect("tmpdir");
        for (relative, body) in files {
            write_under(dir.path(), relative, body);
        }
        dir
    }

    /// The render context every memory test composes from, with the fixture
    /// directory standing in for both the execution root and the checkout.
    #[allow(clippy::too_many_arguments)]
    fn memory_context<'a>(
        root: &'a Path,
        plan_path: &'a Path,
        loaded: &'a LoadedPlan,
        memory: &'a PromptMemory,
        machine: &'a rhei_validator::StateMachine,
        task: &'a rhei_core::ast::Task,
        state_name: &'a str,
    ) -> RuntimeTemplateContext<'a> {
        RuntimeTemplateContext {
            workspace_root: root,
            task_roots: Some(&loaded.task_roots),
            plan_tasks: Some(&loaded.rhei.tasks),
            checkout_root: root,
            plan_path,
            state_machine_path: None,
            plan_title: &loaded.rhei.title,
            task,
            state_name,
            current_state_raw: task.state.as_str(),
            machine,
            metadata: loaded.rhei.metadata.as_ref(),
            target: None,
            model: None,
            model_provider: None,
            model_name: None,
            agent: Some("mock"),
            agent_mode: None,
            tooling: None,
            memory: Some(memory),
        }
    }

    /// The single-file rhei most memory tests run against: a parent with three
    /// children, a prior chain, an export, and a claimed sibling.
    const MEMORY_PLAN: &str = r#"# Rhei: Memory Plan

---
structure:
  maxLevels: 3
---

## Notes

Standing note from the plan writer.

## Tasks

### Task 1: Harden the parser
**State:** pending

The decomposition for the whole subtree lives here.
Acceptance: every child lands its own result.

#### Task 1.1: Implement
**State:** completed
**Provides:** findings

#### Task 1.2: Review round 1
**State:** completed
**Prior:** 1.1

#### Task 1.3: Fix round 1
**State:** review
**Prior:** 1.2
**Consumes:** 1.1:findings

#### Task 1.4: Review round 2
**State:** pending
**Prior:** 1.3
**Assignee:** codex
"#;

    /// Load `MEMORY_PLAN` plus any extra fixture files, then hand back the
    /// pieces every test needs.
    fn memory_plan_dir(extra: &[(&str, &str)]) -> tempfile::TempDir {
        let mut files = vec![("plan.rhei.md", MEMORY_PLAN)];
        files.extend_from_slice(extra);
        memory_dir(&files)
    }

    /// §FS-rhei-memory.3.1: the chain names the Panta, the rhei, and every
    /// ancestor root-first, and ends in the bold line for this invocation.
    #[test]
    fn position_names_the_chain_down_to_this_invocation() {
        let dir = memory_plan_dir(&[]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = memory_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1.3").expect("task 1.3");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "review");

        let position = render_position(&context);
        assert!(
            position.starts_with(
                "\n## Position\n\nPanta: Memory Plan \u{203a} rhei `plan`: Memory Plan \
                 \u{203a} Task plan.1: Harden the parser [pending]\n"
            ),
            "got:\n{position}"
        );
        assert!(
            position.contains(
                "\u{203a} **Task plan.1.3: Fix round 1 [review]** \u{2190} this invocation \
                 (visit 1)"
            ),
            "got:\n{position}"
        );
    }

    /// §FS-rhei-memory.3.1: a root task's chain is the Panta and the rhei
    /// alone, and it gets no sibling list and no parent body.
    #[test]
    fn a_root_task_has_no_siblings_and_no_parent() {
        let dir = memory_plan_dir(&[]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = memory_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1").expect("task 1");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "pending");

        let position = render_position(&context);
        assert!(
            position.contains(
                "Panta: Memory Plan \u{203a} rhei `plan`: Memory Plan\n\u{203a} **Task plan.1:"
            ),
            "got:\n{position}"
        );
        assert!(!position.contains("### Siblings"), "got:\n{position}");
        assert!(!position.contains("### Parent"), "got:\n{position}");
    }

    /// §FS-rhei-memory.4.2: siblings render in plan order, and one that lists
    /// this task as a prior — or consumes one of its exports — says so.
    #[test]
    fn siblings_mark_the_ones_that_wait_on_this_task() {
        let dir = memory_plan_dir(&[]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = memory_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1.3").expect("task 1.3");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "review");

        let position = render_position(&context);
        assert!(
            position.contains(
                "### Siblings\n\n\
                 - Task plan.1.1: Implement [completed]\n\
                 - Task plan.1.2: Review round 1 [completed]\n\
                 - Task plan.1.4: Review round 2 [pending] \u{2014} waits on this task\n"
            ),
            "got:\n{position}"
        );

        // The export consumer of 1.1 waits on it too, by `**Consumes:**` alone.
        let producer = find_task_by_id_str(&loaded.rhei.tasks, "plan.1.1").expect("task 1.1");
        let producer_context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, producer, "review");
        let produced = render_position(&producer_context);
        assert!(
            produced.contains(
                "- Task plan.1.3: Fix round 1 [review] \u{2014} waits on this task\n"
            ),
            "got:\n{produced}"
        );
    }

    /// §FS-rhei-memory.3.1: the nearest ancestor's body is pasted in full and
    /// fenced; higher ancestors contribute one chain line and nothing more.
    #[test]
    fn the_parent_body_is_pasted_and_fenced() {
        let dir = memory_plan_dir(&[]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = memory_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1.3").expect("task 1.3");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "review");

        let position = render_position(&context);
        assert!(
            position.contains("### Parent: Task plan.1: Harden the parser\n\n```markdown\n"),
            "got:\n{position}"
        );
        assert!(
            position.contains("Acceptance: every child lands its own result."),
            "got:\n{position}"
        );
    }

    /// §FS-rhei-memory.3.1: a bare rhei's own content sections are its Rhei
    /// Context, and its implicit Panta contributes no Project Context.
    #[test]
    fn a_bare_rhei_has_rhei_context_and_no_project_context() {
        let dir = memory_plan_dir(&[]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = memory_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1.3").expect("task 1.3");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "review");

        let position = render_position(&context);
        assert!(position.contains("### Rhei Context"), "got:\n{position}");
        assert!(position.contains("## Notes"), "got:\n{position}");
        assert!(position.contains("Standing note from the plan writer."), "got:\n{position}");
        assert!(!position.contains("### Project Context"), "got:\n{position}");
    }

    /// §FS-rhei-memory.4.2: `### Rhei Context` is verbatim — the pasted block
    /// is the bytes between the section heading and the next one, paragraph
    /// breaks, lists, and fences included.
    #[test]
    fn rhei_context_pastes_the_authored_block_byte_for_byte() {
        let authored = "First paragraph.\n\nSecond paragraph.\n\n- item one\n- item two\n\n\
                        ```sh\necho hi\n\necho bye\n```\n\nTail paragraph.";
        let plan = format!(
            "# Rhei: Verbatim\n\n## Notes\n\n{authored}\n\n## Tasks\n\n\
             ### Task 1: Only\n**State:** pending\n"
        );
        let dir = memory_dir(&[("plan.rhei.md", plan.as_str())]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = memory_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1").expect("task 1");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "pending");

        let position = render_position(&context);
        // The body holds a triple-backtick run, so the fence is four.
        assert!(
            position.contains(&format!("````markdown\n## Notes\n\n{authored}\n````")),
            "got:\n{position}"
        );
    }

    /// A machine with two counted loops and a supervising state, so a
    /// `**State:**` line can carry the `-<visit>` suffix the engine writes.
    fn counted_loop_machine() -> rhei_validator::StateMachine {
        rhei_validator::StateMachine::from_yaml_str(
            r#"
name: counted-loops
version: 1
states:
  pending:
    initial: true
    description: Ready for work
    instructions: Do the work for Task {task_id}.
  work:
    description: Work
    visits: 3
    instructions: Work on Task {task_id}.
  fix:
    description: Fix
    visits: 3
    instructions: Fix Task {task_id}.
  supervising:
    description: Supervise
    instructions: Supervise Task {task_id}.
  completed:
    description: Done
    final: true
transitions:
  - { from: pending, to: work }
  - { from: work, to: fix }
  - { from: fix, to: work }
  - { from: supervising, to: supervising }
  - { from: "*", to: completed }
"#,
        )
        .expect("machine should parse")
    }

    /// A parent mid-supervision, a child mid-loop, a `bug` sibling mid-loop,
    /// and a finished `bug` for the history to summarize.
    const COUNTED_LOOP_PLAN: &str = r#"# Rhei: Loops

---
structure:
  maxLevels: 3
  nodeKinds: [task, bug]
---

## Tasks

### Task 1: Parent
**State:** supervising-3

The decomposition for the whole subtree lives here.

#### Task 1.1: This one
**State:** work-3

#### Bug 1.2: Flaky teardown
**State:** fix-2
**Assignee:** codex
**Prior:** 1.1

#### Bug 1.3: Fixed already
**State:** completed
"#;

    /// One form for a state name across the whole prompt. A counted loop writes
    /// `work-3` into `**State:**`; the invocation's own state is already
    /// normalized, so a neighbour's raw value read as a different machine.
    // §FS-rhei-memory.4.5
    #[test]
    fn counted_loop_suffixes_are_normalized_in_every_section() {
        let dir = memory_dir(&[
            ("plan.rhei.md", COUNTED_LOOP_PLAN),
            ("runtime/state-transitions.log", "plan.1.3 fix@completed\n"),
            ("runtime/results/plan.1.3.md", "## Result\n\nTeardown no longer races.\n"),
        ]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = counted_loop_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1.1").expect("task 1.1");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "work");

        let position = render_position(&context);
        assert!(
            position.contains("\u{203a} Task plan.1: Parent [supervising]\n"),
            "the chain normalizes an ancestor's state; got:\n{position}"
        );
        assert!(
            position.contains("- Bug plan.1.2: Flaky teardown [fix] \u{2014} waits on this task\n"),
            "a sibling's state is normalized; got:\n{position}"
        );

        let history = render_plan_history(&context).expect("history");
        assert!(
            history.contains(
                "- Bug plan.1.3: Fixed already \u{2014} completed \u{2014} Teardown no longer \
                 races.\n"
            ),
            "got:\n{history}"
        );
        assert!(
            history.contains("- Bug plan.1.2: Flaky teardown [fix] \u{2014} codex\n"),
            "`### In Flight` normalizes too; got:\n{history}"
        );

        assert!(
            history.contains(
                "### Dependents\n\n- Bug plan.1.2: Flaky teardown [fix] \u{2014} prior\n"
            ),
            "`### Dependents` normalizes too; got:\n{history}"
        );

        // §FS-rhei-supervision.5.1: the pre-existing `## Child Tasks` map, which
        // printed the raw value beside the normalized one.
        let parent = find_task_by_id_str(&loaded.rhei.tasks, "plan.1").expect("task 1");
        let parent_context = memory_context(
            dir.path(),
            &plan_path,
            &loaded,
            &memory,
            &machine,
            parent,
            "supervising",
        );
        let prompt = compose_agent_prompt(&parent_context).expect("prompt");
        assert!(
            prompt.contains(
                "## Child Tasks\n\n\
                 - Task plan.1.1: This one [work]\n\
                 - Bug plan.1.2: Flaky teardown [fix]\n\
                 - Bug plan.1.3: Fixed already [completed]\n"
            ),
            "got:\n{prompt}"
        );
    }

    /// §FS-rhei-memory.3.1 §FS-rhei-memory.3.2: a node is named by its own kind,
    /// title-cased — the form `## Child Tasks` and `rhei list` already print.
    #[test]
    fn a_node_is_named_by_its_own_kind() {
        let dir = memory_dir(&[
            ("plan.rhei.md", COUNTED_LOOP_PLAN),
            ("runtime/state-transitions.log", "plan.1.3 fix@completed\n"),
        ]);
        let plan_path = dir.path().join("plan.rhei.md");
        let loaded = load_plan(&plan_path).expect("plan loads");
        let memory =
            prompt_memory(&loaded, &plan_path, &dir.path().join("runtime"), BTreeSet::new());
        let machine = counted_loop_machine();
        let task = find_task_by_id_str(&loaded.rhei.tasks, "plan.1.1").expect("task 1.1");
        let context =
            memory_context(dir.path(), &plan_path, &loaded, &memory, &machine, task, "work");

        let position = render_position(&context);
        assert!(position.contains("- Bug plan.1.2: Flaky teardown"), "got:\n{position}");
        assert!(
            position.contains("### Parent: Task plan.1: Parent\n"),
            "the parent heading carries its kind too; got:\n{position}"
        );
        assert!(
            position.contains("\u{203a} **Task plan.1.1: This one [work]**"),
            "got:\n{position}"
        );

        let history = render_plan_history(&context).expect("history");
        assert!(history.contains("- Bug plan.1.3: Fixed already \u{2014} completed"), "got:\n{history}");
    }