kernex-agent 0.2.0

CLI dev assistant powered by Kernex runtime
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
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "kx", version, about = "CLI dev assistant powered by Kernex")]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Command>,

    /// AI provider to use (claude-code, ollama, openai, anthropic, gemini, openrouter)
    #[arg(long, global = true, default_value = "claude-code")]
    pub provider: String,

    /// Model override (provider-specific, e.g. gpt-4o, llama3.2)
    #[arg(long, global = true)]
    pub model: Option<String>,

    /// API key for providers that require one
    #[arg(long, global = true)]
    pub api_key: Option<String>,

    /// Base URL override (e.g. http://localhost:11434 for Ollama)
    #[arg(long, global = true)]
    pub base_url: Option<String>,

    /// One-shot message when no subcommand is given (kx "fix the bug")
    pub message: Option<String>,
}

#[derive(Subcommand)]
pub enum Command {
    /// Interactive coding assistant with persistent memory
    Dev {
        /// One-shot message (skip interactive loop)
        message: Option<String>,
    },
    /// Repository health audit (deps, tests, docs, structure)
    Audit,
    /// Documentation audit (detect outdated docs, archive)
    Docs,
    /// Initialize kx for current project (installs builtin skills)
    Init,
    /// Run a multi-agent pipeline
    Pipeline {
        #[command(subcommand)]
        action: PipelineAction,
    },
    /// Manage installed skills
    Skills {
        #[command(subcommand)]
        action: SkillsAction,
    },
    /// Manage scheduled tasks (cron-style self-scheduling)
    Cron {
        #[command(subcommand)]
        action: CronAction,
    },
}

#[derive(Subcommand)]
pub enum PipelineAction {
    /// Run a named pipeline/topology
    Run {
        /// Pipeline name (matches topology directory name)
        name: String,
    },
    /// List available pipelines
    List,
}

#[derive(Subcommand)]
pub enum SkillsAction {
    /// List installed skills
    List,
    /// Add a skill from GitHub (owner/repo or owner/repo/path)
    Add {
        /// Skill source (e.g., acme/my-skill or acme/repo/skills/rust)
        source: String,
        /// Trust level to assign (sandboxed, standard, trusted)
        #[arg(short, long, default_value = "sandboxed")]
        trust: String,
    },
    /// Remove an installed skill
    Remove {
        /// Name of the skill to remove
        name: String,
    },
    /// Verify integrity of installed skills
    Verify,
}

#[derive(Subcommand)]
pub enum CronAction {
    /// Schedule a new task for autonomous execution
    Create {
        /// What the agent should do when the task runs
        description: String,
        /// When to run (ISO 8601 datetime, e.g. "2026-04-03T09:00:00")
        #[arg(long)]
        at: String,
        /// Repeat interval: daily, weekly, monthly, weekdays
        #[arg(long)]
        repeat: Option<String>,
    },
    /// List all pending scheduled tasks
    List,
    /// Cancel a scheduled task by ID prefix
    Delete {
        /// Task ID prefix (first 8+ characters shown by cron list)
        id: String,
    },
}

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

    #[test]
    fn cli_parses_no_args() {
        let cli = Cli::try_parse_from(["kx"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert!(cli.command.is_none());
        assert!(cli.message.is_none());
        assert_eq!(cli.provider, "claude-code");
    }

    #[test]
    fn cli_parses_oneshot_message() {
        let cli = Cli::try_parse_from(["kx", "fix the bug"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert!(cli.command.is_none());
        assert_eq!(cli.message, Some("fix the bug".to_string()));
    }

    #[test]
    fn cli_parses_dev_subcommand() {
        let cli = Cli::try_parse_from(["kx", "dev"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert!(matches!(cli.command, Some(Command::Dev { message: None })));
    }

    #[test]
    fn cli_parses_dev_with_message() {
        let cli = Cli::try_parse_from(["kx", "dev", "write tests"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Dev { message }) = cli.command {
            assert_eq!(message, Some("write tests".to_string()));
        } else {
            panic!("Expected Dev command");
        }
    }

    #[test]
    fn cli_parses_audit() {
        let cli = Cli::try_parse_from(["kx", "audit"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert!(matches!(cli.command, Some(Command::Audit)));
    }

    #[test]
    fn cli_parses_docs() {
        let cli = Cli::try_parse_from(["kx", "docs"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert!(matches!(cli.command, Some(Command::Docs)));
    }

    #[test]
    fn cli_parses_init() {
        let cli = Cli::try_parse_from(["kx", "init"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert!(matches!(cli.command, Some(Command::Init)));
    }

    #[test]
    fn cli_parses_pipeline_run() {
        let cli = Cli::try_parse_from(["kx", "pipeline", "run", "code-review"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Pipeline { action }) = cli.command {
            if let PipelineAction::Run { name } = action {
                assert_eq!(name, "code-review");
            } else {
                panic!("Expected Run action");
            }
        } else {
            panic!("Expected Pipeline command");
        }
    }

    #[test]
    fn cli_parses_pipeline_list() {
        let cli = Cli::try_parse_from(["kx", "pipeline", "list"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Pipeline { action }) = cli.command {
            assert!(matches!(action, PipelineAction::List));
        } else {
            panic!("Expected Pipeline command");
        }
    }

    #[test]
    fn cli_parses_provider_flag() {
        let cli = Cli::try_parse_from(["kx", "--provider", "ollama", "dev"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert_eq!(cli.provider, "ollama");
    }

    #[test]
    fn cli_parses_model_flag() {
        let cli = Cli::try_parse_from(["kx", "--model", "gpt-4o", "dev"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert_eq!(cli.model, Some("gpt-4o".to_string()));
    }

    #[test]
    fn cli_parses_api_key_flag() {
        let cli = Cli::try_parse_from(["kx", "--api-key", "sk-test", "dev"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert_eq!(cli.api_key, Some("sk-test".to_string()));
    }

    #[test]
    fn cli_parses_base_url_flag() {
        let cli = Cli::try_parse_from(["kx", "--base-url", "http://localhost:11434", "dev"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        assert_eq!(cli.base_url, Some("http://localhost:11434".to_string()));
    }

    #[test]
    fn cli_provider_default_is_claude_code() {
        let cli = Cli::try_parse_from(["kx", "dev"]);
        assert!(cli.is_ok());
        assert_eq!(cli.unwrap().provider, "claude-code");
    }

    #[test]
    fn cli_parses_skills_list() {
        let cli = Cli::try_parse_from(["kx", "skills", "list"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Skills { action }) = cli.command {
            assert!(matches!(action, SkillsAction::List));
        } else {
            panic!("Expected Skills command");
        }
    }

    #[test]
    fn cli_parses_skills_add() {
        let cli = Cli::try_parse_from(["kx", "skills", "add", "acme/repo"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Skills { action }) = cli.command {
            if let SkillsAction::Add { source, trust } = action {
                assert_eq!(source, "acme/repo");
                assert_eq!(trust, "sandboxed");
            } else {
                panic!("Expected Add action");
            }
        } else {
            panic!("Expected Skills command");
        }
    }

    #[test]
    fn cli_parses_skills_add_with_trust() {
        let cli = Cli::try_parse_from(["kx", "skills", "add", "acme/repo", "-t", "trusted"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Skills { action }) = cli.command {
            if let SkillsAction::Add { source, trust } = action {
                assert_eq!(source, "acme/repo");
                assert_eq!(trust, "trusted");
            } else {
                panic!("Expected Add action");
            }
        } else {
            panic!("Expected Skills command");
        }
    }

    #[test]
    fn cli_parses_skills_remove() {
        let cli = Cli::try_parse_from(["kx", "skills", "remove", "my-skill"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Skills { action }) = cli.command {
            if let SkillsAction::Remove { name } = action {
                assert_eq!(name, "my-skill");
            } else {
                panic!("Expected Remove action");
            }
        } else {
            panic!("Expected Skills command");
        }
    }

    #[test]
    fn cli_parses_skills_verify() {
        let cli = Cli::try_parse_from(["kx", "skills", "verify"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Skills { action }) = cli.command {
            assert!(matches!(action, SkillsAction::Verify));
        } else {
            panic!("Expected Skills command");
        }
    }

    #[test]
    fn cli_parses_cron_list() {
        let cli = Cli::try_parse_from(["kx", "cron", "list"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Cron { action }) = cli.command {
            assert!(matches!(action, CronAction::List));
        } else {
            panic!("Expected Cron command");
        }
    }

    #[test]
    fn cli_parses_cron_create() {
        let cli = Cli::try_parse_from([
            "kx",
            "cron",
            "create",
            "run the test suite",
            "--at",
            "2026-04-03T09:00:00",
        ]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Cron { action }) = cli.command {
            if let CronAction::Create {
                description,
                at,
                repeat,
            } = action
            {
                assert_eq!(description, "run the test suite");
                assert_eq!(at, "2026-04-03T09:00:00");
                assert!(repeat.is_none());
            } else {
                panic!("Expected Create action");
            }
        } else {
            panic!("Expected Cron command");
        }
    }

    #[test]
    fn cli_parses_cron_create_with_repeat() {
        let cli = Cli::try_parse_from([
            "kx",
            "cron",
            "create",
            "run lints",
            "--at",
            "2026-04-03T08:00:00",
            "--repeat",
            "daily",
        ]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Cron { action }) = cli.command {
            if let CronAction::Create { repeat, .. } = action {
                assert_eq!(repeat, Some("daily".to_string()));
            } else {
                panic!("Expected Create action");
            }
        } else {
            panic!("Expected Cron command");
        }
    }

    #[test]
    fn cli_parses_cron_delete() {
        let cli = Cli::try_parse_from(["kx", "cron", "delete", "abc12345"]);
        assert!(cli.is_ok());
        let cli = cli.unwrap();
        if let Some(Command::Cron { action }) = cli.command {
            if let CronAction::Delete { id } = action {
                assert_eq!(id, "abc12345");
            } else {
                panic!("Expected Delete action");
            }
        } else {
            panic!("Expected Cron command");
        }
    }

    #[test]
    fn cli_has_valid_structure() {
        // Verifies the CLI definition doesn't have conflicts
        Cli::command().debug_assert();
    }

    #[test]
    fn cli_version_flag() {
        let result = Cli::try_parse_from(["kx", "--version"]);
        // --version causes early exit, so it's an error from clap's perspective
        assert!(result.is_err());
    }

    #[test]
    fn cli_help_flag() {
        let result = Cli::try_parse_from(["kx", "--help"]);
        // --help causes early exit
        assert!(result.is_err());
    }
}