kernex-agent 0.4.4

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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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, anthropic, openai, ollama, gemini, openrouter, groq, mistral, deepseek, fireworks, xai)
    #[arg(
        short = 'p',
        long,
        global = true,
        default_value = "claude-code",
        env = "KERNEX_PROVIDER"
    )]
    pub provider: String,

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

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

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

    /// Override the active project scope (default: current directory name)
    #[arg(long, global = true)]
    pub project: Option<String>,

    /// Channel identifier for memory scoping (default: cli)
    #[arg(long, global = true)]
    pub channel: Option<String>,

    /// Max response tokens per provider request (default: provider-specific)
    #[arg(long, global = true)]
    pub max_tokens: Option<u32>,

    /// Disable persistent memory for this session
    #[arg(long, global = true)]
    pub no_memory: bool,

    /// Disable automatic conversation summarization on overflow.
    /// kx defaults to summarizing the oldest messages when a session
    /// exceeds the runtime's context cap; pass --no-auto-compact to
    /// fall back to the kernex-runtime default behavior (silent drop)
    /// for cost reasons or when running against a provider where the
    /// extra summarization round-trip is undesirable.
    #[arg(long, global = true)]
    pub no_auto_compact: bool,

    /// Print tool calls and hook events to stderr
    #[arg(long, global = true)]
    pub verbose: bool,

    /// 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,
    /// Diagnose the local install: provider availability, env vars, data dir
    Doctor,
    /// 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,
    },
    /// Run kx as a headless HTTP server for remote agent execution
    Serve {
        /// Host address to bind
        #[arg(long, default_value = "127.0.0.1")]
        host: String,
        /// Port to listen on
        #[arg(long, default_value_t = 8080)]
        port: u16,
        /// Bearer auth token (or set KERNEX_AUTH_TOKEN env var)
        #[arg(long, env = "KERNEX_AUTH_TOKEN")]
        auth_token: Option<String>,
        /// Max concurrent agent jobs
        #[arg(long, default_value_t = 4)]
        workers: usize,
    },
}

#[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,
        /// Replace an existing skill of the same name even if it came from
        /// a different source. Required to shadow a trusted builtin.
        #[arg(long)]
        force: bool,
    },
    /// Remove an installed skill
    Remove {
        /// Name of the skill to remove
        name: String,
    },
    /// Verify integrity of installed skills
    Verify,
    /// Validate a skill directory against the Agent Skills spec (content, format, anti-patterns)
    Lint {
        /// Path to skill directory to validate (defaults to current directory)
        #[arg(default_value = ".")]
        path: String,
    },
}

#[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,
                force: _,
            } = 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,
                force: _,
            } = 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_parses_serve_defaults() {
        let cli = Cli::try_parse_from(["kx", "serve", "--auth-token", "secret"]).unwrap();
        if let Some(Command::Serve {
            host,
            port,
            auth_token,
            workers,
        }) = cli.command
        {
            assert_eq!(host, "127.0.0.1");
            assert_eq!(port, 8080);
            assert_eq!(auth_token, Some("secret".to_string()));
            assert_eq!(workers, 4);
        } else {
            panic!("Expected Serve command");
        }
    }

    #[test]
    fn cli_parses_serve_custom_host_port() {
        let cli = Cli::try_parse_from([
            "kx",
            "serve",
            "--host",
            "0.0.0.0",
            "--port",
            "9000",
            "--workers",
            "8",
        ])
        .unwrap();
        if let Some(Command::Serve {
            host,
            port,
            workers,
            ..
        }) = cli.command
        {
            assert_eq!(host, "0.0.0.0");
            assert_eq!(port, 9000);
            assert_eq!(workers, 8);
        } else {
            panic!("Expected Serve command");
        }
    }

    #[test]
    fn cli_parses_serve_no_auth_token() {
        let cli = Cli::try_parse_from(["kx", "serve"]).unwrap();
        if let Some(Command::Serve { auth_token, .. }) = cli.command {
            assert!(auth_token.is_none());
        } else {
            panic!("Expected Serve 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());
    }

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

    #[test]
    fn cli_parses_model_short_flag() {
        let cli = Cli::try_parse_from(["kx", "-m", "llama3.2", "dev"]).unwrap();
        assert_eq!(cli.model, Some("llama3.2".to_string()));
    }

    #[test]
    fn cli_parses_project_flag() {
        let cli = Cli::try_parse_from(["kx", "--project", "my-app", "dev"]).unwrap();
        assert_eq!(cli.project, Some("my-app".to_string()));
    }

    #[test]
    fn cli_parses_channel_flag() {
        let cli = Cli::try_parse_from(["kx", "--channel", "ci", "dev"]).unwrap();
        assert_eq!(cli.channel, Some("ci".to_string()));
    }

    #[test]
    fn cli_parses_max_tokens_flag() {
        let cli = Cli::try_parse_from(["kx", "--max-tokens", "2048", "dev"]).unwrap();
        assert_eq!(cli.max_tokens, Some(2048));
    }

    #[test]
    fn cli_parses_no_memory_flag() {
        let cli = Cli::try_parse_from(["kx", "--no-memory", "dev"]).unwrap();
        assert!(cli.no_memory);
    }

    #[test]
    fn cli_parses_verbose_flag() {
        let cli = Cli::try_parse_from(["kx", "--verbose", "dev"]).unwrap();
        assert!(cli.verbose);
    }

    #[test]
    fn cli_flags_default_false() {
        let cli = Cli::try_parse_from(["kx", "dev"]).unwrap();
        assert!(!cli.no_memory);
        assert!(!cli.verbose);
        assert!(cli.project.is_none());
        assert!(cli.channel.is_none());
        assert!(cli.max_tokens.is_none());
    }
}