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
pub mod amberify;
pub mod complexity;
pub mod dev;
pub mod doctor;
pub mod guide;
pub mod init;
pub mod install;
pub mod learn;
pub mod setup;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
#[derive(Parser, Debug, Clone)]
#[command(
name = "portail",
about = "Unified proxy/gateway: AI Gateway + MCP Gateway + CDN cache",
version,
long_about = "Portail is a unified proxy and gateway for AI services, MCP tools, and CDN caching.\n\nRun 'portail serve' to start the server or 'portail --help' for commands."
)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
/// Path to config file
#[arg(short, long, default_value = "portail.toml")]
pub config: PathBuf,
/// Enable verbose logging
#[arg(short, long)]
pub verbose: bool,
/// Output format for non-interactive commands
#[arg(long, default_value = "text")]
pub format: OutputFormat,
}
#[derive(Debug, Clone, clap::ValueEnum)]
pub enum OutputFormat {
Text,
Json,
}
#[derive(Subcommand, Debug, Clone)]
pub enum Commands {
/// Start the proxy server
Serve,
/// Developer/contributor entry point — dev dashboard, check, test, lint, build, audit
Dev {
#[command(subcommand)]
action: DevAction,
},
/// Show current status and health
Status,
/// Stream agent lifecycle events
Events {
/// Number of recent events to show
#[arg(short, long)]
count: Option<usize>,
/// Stream live events
#[arg(short, long)]
stream: bool,
},
/// Manage prompt injection hooks
Hooks {
#[command(subcommand)]
action: HookAction,
},
/// Show or manage configuration
Config {
#[command(subcommand)]
action: Option<ConfigAction>,
},
/// Show CDN cache statistics
Cache {
#[command(subcommand)]
action: Option<CacheAction>,
},
/// Run health check
Health,
/// Analyze time complexity (Big O) across the codebase
Complexity {
/// Output format (text or json)
#[arg(long, default_value = "text")]
format: String,
/// Save report to file
#[arg(short, long)]
output: Option<PathBuf>,
/// CI mode: write report to file, always exit 0, never fail
#[arg(long)]
ci: bool,
/// CI report path (default: complexity-report.toml)
#[arg(long, default_value = "complexity-report.toml")]
ci_report_path: PathBuf,
},
/// Capture/replay production traffic for regression detection
DriftDetect {
#[command(subcommand)]
command: crate::drift::DriftCommand,
/// CI mode: write report to file, always exit 0
#[arg(long)]
ci: bool,
},
/// Compare route table against golden spec
SpecVerify {
#[command(subcommand)]
command: crate::spec_verify::SpecCommand,
/// CI mode: write report, always exit 0
#[arg(long)]
ci: bool,
},
/// Install portail (binary, cargo, or nix)
Install {
/// Installation method
#[arg(long, default_value = "auto")]
method: InstallMethod,
/// Installation directory (for binary install)
#[arg(long)]
dir: Option<PathBuf>,
},
/// Generate documentation
Docs {
/// Open docs in browser after generation
#[arg(long)]
open: bool,
},
/// Setup portail: domain, DNS, certificates
Setup {
/// Skip interactive prompts and use defaults
#[arg(long)]
non_interactive: bool,
/// Domain name (e.g., portail.example.com)
#[arg(long)]
domain: Option<String>,
/// Use self-signed certificates instead of Let's Encrypt
#[arg(long)]
self_signed: bool,
/// Setup Headscale for mesh networking
#[arg(long)]
headscale: bool,
},
/// Learn about networking and security concepts
Learn {
/// Topic to learn about (dns, tcp, tls, doh, vpn, proxy, firewall, dnssec, zero-trust, headscale)
topic: Option<String>,
},
/// Interactive guide for branch protection setup
Guide,
/// Convert shell scripts to Amber language
Amberify {
/// Input file or directory
#[arg(short, long)]
input: PathBuf,
/// Output directory
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Interactive configuration generator (wizard)
Init,
/// System compatibility check
Doctor,
/// Manage upstream target templates (list, export, share)
Target {
#[command(subcommand)]
action: TargetAction,
},
/// Manage built-in MCP servers (list, install, config)
Mcp {
#[command(subcommand)]
action: McpAction,
},
/// Manage .vaked plugins (list, load, lower, build)
Vaked {
#[command(subcommand)]
action: VakedAction,
},
/// Loop state — query, update, HITL decisions
Loop {
#[command(subcommand)]
action: LoopAction,
},
/// Process Interception Tracker — watch /proc, log all processes
Pit {
/// One-shot scan instead of continuous watch
#[arg(long)]
scan: bool,
},
/// Generate shell completions
Completions {
/// Shell type (bash, zsh, fish, powershell, elvish)
shell: clap_complete::Shell,
},
/// Manage documentation packages (pkg-ctx)
PkgCtx {
#[command(subcommand)]
action: PkgCtxAction,
},
/// Release audit: verify binaries, generate SBOM + report
ReleaseAudit {
/// Directory containing release artifacts
#[arg(short, long, default_value = "dist")]
dir: PathBuf,
/// Release version string
#[arg(short, long)]
version: String,
/// Output directory for reports
#[arg(short, long)]
out: Option<PathBuf>,
},
}
#[derive(Debug, Clone, clap::ValueEnum)]
pub enum InstallMethod {
Auto,
Cargo,
Nix,
Binary,
}
#[derive(Subcommand, Debug, Clone)]
pub enum HookAction {
/// List all hooks
List,
/// Add a new hook (JSON inline or @file)
Add {
#[arg(short, long)]
hook: String,
},
/// Delete a hook by ID
Delete { id: String },
/// Show hook details
Show { id: String },
}
#[derive(Subcommand, Debug, Clone)]
pub enum ConfigAction {
/// Show current configuration
Show,
/// Validate configuration file
Validate,
/// Reload configuration (SIGHUP)
Reload,
/// Rollback to a previous config version
Rollback {
/// Version number (1-indexed, from 'config history')
version: u64,
},
/// Show config version history
History,
}
#[derive(Subcommand, Debug, Clone)]
pub enum CacheAction {
/// Show cache statistics
Stats,
/// Purge cache entries by prefix
Purge { prefix: String },
/// Show cache hit/miss ratios
Ratio,
}
impl Cli {
pub fn is_server(&self) -> bool {
matches!(self.command, Some(Commands::Serve))
}
}
#[derive(Subcommand, Debug, Clone)]
pub enum TargetAction {
/// List all available target templates (built-in + configured)
List,
/// Export a target as a shareable JSON snippet
Export { name: String },
/// Show default built-in targets
Builtins,
}
#[derive(Subcommand, Debug, Clone)]
pub enum DevAction {
/// Cargo check across all allocator variants
Check,
/// Run tests (all suites)
Test,
/// Lint: clippy + fmt check
Lint,
/// Build release binary
Build {
/// Use fat LTO instead of thin (slower, smaller binary)
#[arg(long)]
max: bool,
},
/// Full release audit: verify, SBOM, report, stamp
Audit {
/// Release version string
#[arg(short, long)]
version: String,
},
/// Run the full CI pipeline (check → lint → test → audit)
Ci {
/// Release version for audit step
#[arg(short, long)]
version: Option<String>,
},
}
#[derive(Subcommand, Debug, Clone)]
pub enum McpAction {
/// List all MCP servers (built-in + registry)
List,
/// Show info about a specific MCP server
Info { name: String },
/// Generate configuration block for a built-in MCP server
Config { name: String },
/// List built-in MCP server templates
Builtins,
}
#[derive(Subcommand, Debug, Clone)]
pub enum LoopAction {
/// Show current loop state
Status,
/// List backlog tasks
Backlog,
/// Add a task to the backlog
Add { phase: String, description: String },
/// Approve a HITL-pending task
Approve {
task_id: String,
reason: Option<String>,
},
/// Reject a HITL-pending task
Reject { task_id: String, reason: String },
/// Get next task for a phase
Next { phase: Option<String> },
/// Get the oneshot prompt for HITL
Prompt,
/// Show task history
History { phase: Option<String> },
/// Run a loop iteration for a schedule
Run {
/// Schedule name to run
schedule: String,
/// Number of iterations to run (default: 1)
#[arg(short = 'n', long, default_value = "1")]
count: usize,
},
/// Override a loop run's decision (council vote)
Council {
/// Run ID to override
run_id: String,
/// Decision: ship, iterate, or escalate
decision: String,
/// Reason for the decision
#[arg(short, long)]
reason: Option<String>,
},
/// Reset the circuit breaker
ResetCircuit,
/// Show loop engine config and building blocks
Config,
/// List registered schedules
Schedules,
}
#[derive(Subcommand, Debug, Clone)]
pub enum VakedAction {
/// List loaded .vaked plugins
List,
/// Load a .vaked file
Load { path: PathBuf },
/// Show the lowered Nix/NixOS output
Lower { path: PathBuf },
/// Build a .vaked plugin to WASM
Build { path: PathBuf },
}
#[derive(Subcommand, Debug, Clone)]
pub enum PkgCtxAction {
/// Add a documentation package from a git repo
Add {
/// Git repository URL
repo: String,
/// Package name (default: inferred from URL)
#[arg(short, long)]
name: Option<String>,
/// Version tag (default: latest)
#[arg(short, long)]
version: Option<String>,
},
/// Search installed documentation packages
Search {
/// Package name (with optional @version)
library: String,
/// Search query
topic: String,
},
/// List installed documentation packages
List,
/// Start the MCP stdio server for AI agent integration
Serve,
}