nika-init 0.64.0

Nika project scaffolding — course generator, workflow templates, showcase
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
//! Exercise checks — validation assertions on raw YAML text
//!
//! Each check function examines the user's workflow YAML to verify
//! that specific patterns are present. Used by the course engine
//! to grade exercises automatically.

/// Verdict for a single check
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CheckVerdict {
    /// The check passed
    Pass,
    /// The check failed with a reason
    Fail(String),
    /// Bonus achievement unlocked
    Bonus(String),
}

impl CheckVerdict {
    pub fn is_pass(&self) -> bool {
        matches!(self, Self::Pass | Self::Bonus(_))
    }
}

/// Result of a single check
#[derive(Debug, Clone)]
pub struct CheckResult {
    /// Human-readable name of the check
    pub name: &'static str,
    /// The verdict
    pub verdict: CheckVerdict,
}

/// Full report for one exercise
#[derive(Debug, Clone)]
pub struct ExerciseReport {
    /// Exercise identifier (e.g., "01-03")
    pub exercise_id: String,
    /// All check results
    pub checks: Vec<CheckResult>,
}

impl ExerciseReport {
    /// Did all required checks pass?
    pub fn passed(&self) -> bool {
        self.checks.iter().all(|c| c.verdict.is_pass())
    }

    /// Count of bonus achievements
    pub fn bonus_count(&self) -> usize {
        self.checks
            .iter()
            .filter(|c| matches!(c.verdict, CheckVerdict::Bonus(_)))
            .count()
    }
}

/// Full report for one level
#[derive(Debug, Clone)]
pub struct LevelReport {
    /// Level number
    pub level: u8,
    /// Exercise reports
    pub exercises: Vec<ExerciseReport>,
}

impl LevelReport {
    /// Did all exercises in the level pass?
    pub fn all_passed(&self) -> bool {
        self.exercises.iter().all(|e| e.passed())
    }

    /// How many exercises passed?
    pub fn pass_count(&self) -> usize {
        self.exercises.iter().filter(|e| e.passed()).count()
    }
}

// ─── Assertion functions ────────────────────────────────────────────────────

/// Check that the YAML contains no TODO/FIXME/XXX placeholders
pub fn check_no_todos(yaml: &str) -> CheckResult {
    let has_todos = yaml.lines().any(|line| {
        let trimmed = line.trim_start();
        // Skip comment lines — templates use # TODO: as instructions
        if trimmed.starts_with('#') {
            return false;
        }
        trimmed.contains("TODO") || trimmed.contains("FIXME") || trimmed.contains("XXX")
    });
    CheckResult {
        name: "no_todos",
        verdict: if has_todos {
            CheckVerdict::Fail("Workflow still contains TODO/FIXME/XXX placeholders".into())
        } else {
            CheckVerdict::Pass
        },
    }
}

/// Check that the YAML contains a specific verb (e.g., "exec", "fetch", "infer", "invoke", "agent")
pub fn check_has_verb(yaml: &str, verb: &str) -> CheckResult {
    let pattern = format!("{}:", verb);
    CheckResult {
        name: "has_verb",
        verdict: if yaml.contains(&pattern) {
            CheckVerdict::Pass
        } else {
            CheckVerdict::Fail(format!("Expected verb '{}:' not found", verb))
        },
    }
}

/// Check that the YAML uses depends_on for task ordering
pub fn check_has_depends_on(yaml: &str) -> CheckResult {
    CheckResult {
        name: "has_depends_on",
        verdict: if yaml.contains("depends_on:") || yaml.contains("depends_on: [") {
            CheckVerdict::Pass
        } else {
            CheckVerdict::Fail("No depends_on: found — tasks should declare dependencies".into())
        },
    }
}

/// Check that the YAML uses for_each for parallel iteration
pub fn check_has_for_each(yaml: &str) -> CheckResult {
    CheckResult {
        name: "has_for_each",
        verdict: if yaml.contains("for_each:") {
            CheckVerdict::Pass
        } else {
            CheckVerdict::Fail("No for_each: found — expected parallel iteration".into())
        },
    }
}

/// Check that the YAML uses with: bindings
pub fn check_has_with_bindings(yaml: &str) -> CheckResult {
    // Look for `with:` followed by binding patterns (key: $task_id)
    let has_with = yaml.contains("with:")
        && (yaml.contains(": $") || yaml.contains(": \"$") || yaml.contains(": '$"));
    CheckResult {
        name: "has_with_bindings",
        verdict: if has_with {
            CheckVerdict::Pass
        } else {
            CheckVerdict::Fail("No with: bindings found — use with: { alias: $task_id }".into())
        },
    }
}

/// Check that the YAML declares the workflow schema
pub fn check_has_schema(yaml: &str) -> CheckResult {
    CheckResult {
        name: "has_schema",
        verdict: if yaml.contains("schema:") && yaml.contains("nika/workflow@0.12") {
            CheckVerdict::Pass
        } else {
            CheckVerdict::Fail("Missing schema declaration: schema: \"nika/workflow@0.12\"".into())
        },
    }
}

/// Check that the YAML has at least N tasks
pub fn check_min_tasks(yaml: &str, min: usize) -> CheckResult {
    // Count occurrences of `- id:` which marks task definitions
    let count = yaml.matches("- id:").count();
    CheckResult {
        name: "min_tasks",
        verdict: if count >= min {
            CheckVerdict::Pass
        } else {
            CheckVerdict::Fail(format!(
                "Expected at least {} tasks but found {}",
                min, count
            ))
        },
    }
}

// ─── Bonus checks ───────────────────────────────────────────────────────────

/// Bonus: completed without using any hints
pub fn bonus_no_hints_used(hints_used: u32) -> CheckResult {
    CheckResult {
        name: "bonus_no_hints",
        verdict: if hints_used == 0 {
            CheckVerdict::Bonus("Completed without hints!".into())
        } else {
            CheckVerdict::Pass // Not a failure, just no bonus
        },
    }
}

/// Bonus: passed on the first attempt (no prior Attempted status)
pub fn bonus_first_try(is_first_try: bool) -> CheckResult {
    CheckResult {
        name: "bonus_first_try",
        verdict: if is_first_try {
            CheckVerdict::Bonus("First try!".into())
        } else {
            CheckVerdict::Pass // Not a failure, just no bonus
        },
    }
}

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

    const SAMPLE_YAML: &str = r#"
schema: "nika/workflow@0.12"
workflow: test
tasks:
  - id: step1
    exec:
      run: echo "hello"
  - id: step2
    depends_on: [step1]
    with:
      result: $step1
    infer:
      prompt: "{{with.result}}"
"#;

    #[test]
    fn test_check_no_todos_pass() {
        let result = check_no_todos(SAMPLE_YAML);
        assert!(result.verdict.is_pass());
    }

    #[test]
    fn test_check_no_todos_fail() {
        let yaml = "tasks:\n  - id: a\n    exec: \"TODO: fix this\"";
        let result = check_no_todos(yaml);
        assert!(!result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_verb_found() {
        let result = check_has_verb(SAMPLE_YAML, "exec");
        assert!(result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_verb_missing() {
        let result = check_has_verb(SAMPLE_YAML, "fetch");
        assert!(!result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_depends_on() {
        let result = check_has_depends_on(SAMPLE_YAML);
        assert!(result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_depends_on_missing() {
        let yaml = "tasks:\n  - id: a\n    exec:\n      run: echo hi";
        let result = check_has_depends_on(yaml);
        assert!(!result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_for_each() {
        let yaml = "tasks:\n  - id: a\n    for_each: [1, 2]\n    exec:\n      run: echo hi";
        let result = check_has_for_each(yaml);
        assert!(result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_for_each_missing() {
        let result = check_has_for_each(SAMPLE_YAML);
        assert!(!result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_with_bindings() {
        let result = check_has_with_bindings(SAMPLE_YAML);
        assert!(result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_schema() {
        let result = check_has_schema(SAMPLE_YAML);
        assert!(result.verdict.is_pass());
    }

    #[test]
    fn test_check_has_schema_missing() {
        let yaml = "workflow: test\ntasks:\n  - id: a\n    exec:\n      run: echo hi";
        let result = check_has_schema(yaml);
        assert!(!result.verdict.is_pass());
    }

    #[test]
    fn test_check_min_tasks_pass() {
        let result = check_min_tasks(SAMPLE_YAML, 2);
        assert!(result.verdict.is_pass());
    }

    #[test]
    fn test_check_min_tasks_fail() {
        let result = check_min_tasks(SAMPLE_YAML, 5);
        assert!(!result.verdict.is_pass());
    }

    #[test]
    fn test_bonus_no_hints() {
        let result = bonus_no_hints_used(0);
        assert!(matches!(result.verdict, CheckVerdict::Bonus(_)));
    }

    #[test]
    fn test_bonus_no_hints_used_some() {
        let result = bonus_no_hints_used(3);
        assert_eq!(result.verdict, CheckVerdict::Pass);
    }

    #[test]
    fn test_bonus_first_try() {
        let result = bonus_first_try(true);
        assert!(matches!(result.verdict, CheckVerdict::Bonus(_)));
    }

    #[test]
    fn test_bonus_first_try_not() {
        let result = bonus_first_try(false);
        assert_eq!(result.verdict, CheckVerdict::Pass);
    }

    #[test]
    fn test_exercise_report_passed() {
        let report = ExerciseReport {
            exercise_id: "01-01".into(),
            checks: vec![
                check_has_schema(SAMPLE_YAML),
                check_has_verb(SAMPLE_YAML, "exec"),
                check_min_tasks(SAMPLE_YAML, 2),
            ],
        };
        assert!(report.passed());
    }

    #[test]
    fn test_exercise_report_failed() {
        let report = ExerciseReport {
            exercise_id: "01-01".into(),
            checks: vec![
                check_has_schema(SAMPLE_YAML),
                check_has_verb(SAMPLE_YAML, "agent"), // missing
            ],
        };
        assert!(!report.passed());
    }

    #[test]
    fn test_exercise_report_bonus_count() {
        let report = ExerciseReport {
            exercise_id: "01-01".into(),
            checks: vec![
                check_has_schema(SAMPLE_YAML),
                bonus_no_hints_used(0),
                bonus_first_try(true),
            ],
        };
        assert_eq!(report.bonus_count(), 2);
    }

    #[test]
    fn test_level_report() {
        let report = LevelReport {
            level: 1,
            exercises: vec![
                ExerciseReport {
                    exercise_id: "01-01".into(),
                    checks: vec![check_has_schema(SAMPLE_YAML)],
                },
                ExerciseReport {
                    exercise_id: "01-02".into(),
                    checks: vec![check_has_schema(SAMPLE_YAML)],
                },
            ],
        };
        assert!(report.all_passed());
        assert_eq!(report.pass_count(), 2);
    }

    #[test]
    fn test_check_verdict_is_pass() {
        assert!(CheckVerdict::Pass.is_pass());
        assert!(CheckVerdict::Bonus("nice".into()).is_pass());
        assert!(!CheckVerdict::Fail("bad".into()).is_pass());
    }

    #[test]
    fn test_check_no_todos_ignores_comments() {
        let yaml = "schema: \"nika/workflow@0.12\"\nworkflow: test\n# TODO: This is a guide comment\ntasks:\n  - id: hello\n    exec: \"echo hello\"\n";
        let result = check_no_todos(yaml);
        assert!(result.verdict.is_pass(), "TODO in comments should not fail");
    }

    #[test]
    fn test_check_no_todos_catches_inline_todos() {
        let yaml = "schema: \"nika/workflow@0.12\"\ntasks:\n  - id: hello\n    exec: \"TODO implement this\"\n";
        let result = check_no_todos(yaml);
        assert!(!result.verdict.is_pass(), "TODO in values should fail");
    }
}