-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathagt.py
More file actions
467 lines (375 loc) · 14 KB
/
agt.py
File metadata and controls
467 lines (375 loc) · 14 KB
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
AGT — Unified CLI for the Agent Governance Toolkit.
Single entry point that namespaces all governance commands:
agt verify OWASP ASI compliance verification
agt integrity Module integrity checks
agt lint-policy Policy file linting
agt doctor Diagnose installation health
agt version Show installed package versions
Plugin subcommands from other AGT packages are discovered via the
``agt.commands`` entry-point group.
"""
from __future__ import annotations
import sys
from typing import Any, Dict, Optional
import click
try:
from rich import box as _rich_box
from rich.console import Console as _RichConsole
from rich.table import Table as _RichTable
_console = _RichConsole()
_console_err = _RichConsole(stderr=True)
_HAS_RICH = True
except ImportError: # pragma: no cover
_HAS_RICH = False
_console = None # type: ignore[assignment]
_console_err = None # type: ignore[assignment]
def _print(msg: str, *, style: str = "", err: bool = False) -> None:
"""Print with optional rich styling; falls back to plain print."""
if _HAS_RICH and style:
target = _console_err if err else _console
target.print(msg, style=style) # type: ignore[union-attr]
else:
print(msg, file=sys.stderr if err else sys.stdout)
def _get_package_version(package_name: str) -> Optional[str]:
"""Return installed version via importlib.metadata, or None."""
try:
from importlib.metadata import version
return version(package_name)
except Exception:
return None
def _discover_plugins() -> Dict[str, click.Command]:
"""Discover plugin commands from the ``agt.commands`` entry-point group."""
plugins: Dict[str, click.Command] = {}
try:
if sys.version_info >= (3, 10):
from importlib.metadata import entry_points
eps = entry_points(group="agt.commands")
else:
from importlib.metadata import entry_points
all_eps = entry_points()
eps = all_eps.get("agt.commands", [])
for ep in eps:
try:
obj = ep.load()
if isinstance(obj, click.Command):
plugins[ep.name] = obj
elif callable(obj):
result = obj()
if isinstance(result, click.Command):
plugins[ep.name] = result
except Exception:
pass
except Exception:
pass
return plugins
class AgtContext:
"""Shared context passed to all subcommands via ``click.Context.obj``."""
def __init__(
self,
output_json: bool = False,
verbose: bool = False,
quiet: bool = False,
no_color: bool = False,
) -> None:
self.output_json = output_json
self.verbose = verbose
self.quiet = quiet
self.no_color = no_color
class AgtGroup(click.Group):
"""Custom group that merges built-in and plugin commands."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._plugins_loaded = False
def _ensure_plugins(self) -> None:
if self._plugins_loaded:
return
self._plugins_loaded = True
for name, cmd in _discover_plugins().items():
if name not in self.commands:
self.add_command(cmd, name)
def list_commands(self, ctx: click.Context) -> list[str]:
self._ensure_plugins()
return sorted(super().list_commands(ctx))
def get_command(self, ctx: click.Context, cmd_name: str) -> Optional[click.Command]:
self._ensure_plugins()
return super().get_command(ctx, cmd_name)
@click.group(cls=AgtGroup)
@click.option("--json", "output_json", is_flag=True, default=False, help="Output in JSON format.")
@click.option("--verbose", "-v", is_flag=True, default=False, help="Increase output verbosity.")
@click.option("--quiet", "-q", is_flag=True, default=False, help="Suppress non-essential output.")
@click.option("--no-color", is_flag=True, default=False, help="Disable colored output.")
@click.version_option(
version=_get_package_version("agent_governance_toolkit") or "unknown",
prog_name="agt",
)
@click.pass_context
def cli(
ctx: click.Context,
output_json: bool,
verbose: bool,
quiet: bool,
no_color: bool,
) -> None:
"""
AGT — Agent Governance Toolkit CLI.
Unified command-line interface for governing AI agents.
\b
Quick start:
agt verify Check OWASP ASI compliance
agt doctor Diagnose installation health
agt lint-policy ./dir Lint policy files
agt integrity Verify module integrity
\b
Plugin commands from installed AGT packages are auto-discovered and appear
below when installed.
"""
ctx.ensure_object(dict)
ctx.obj = AgtContext(
output_json=output_json,
verbose=verbose,
quiet=quiet,
no_color=no_color,
)
@cli.command()
@click.option("--badge", is_flag=True, default=False, help="Output markdown badge only.")
@click.option(
"--evidence",
"evidence_path",
type=click.Path(exists=True, dir_okay=False, path_type=str),
default=None,
help="Path to runtime evidence JSON/YAML.",
)
@click.option(
"--strict",
is_flag=True,
default=False,
help="Fail if runtime evidence shows weak or missing governance setup.",
)
@click.pass_obj
def verify(
ctx_obj: AgtContext,
badge: bool,
evidence_path: str | None,
strict: bool,
) -> None:
"""Run OWASP ASI 2026 governance verification."""
try:
from agent_compliance.verify import GovernanceVerifier
verifier = GovernanceVerifier()
if evidence_path:
attestation = verifier.verify_evidence(evidence_path=evidence_path, strict=strict)
else:
attestation = verifier.verify()
if ctx_obj.output_json:
click.echo(attestation.to_json())
elif badge:
click.echo(attestation.badge_markdown())
else:
click.echo(attestation.summary())
if not attestation.passed:
raise SystemExit(1)
except SystemExit:
raise
except Exception as e:
_handle_error(e, ctx_obj.output_json)
raise SystemExit(1)
@cli.command()
@click.option("--manifest", type=click.Path(), default=None, help="Path to integrity.json manifest.")
@click.option(
"--generate",
type=click.Path(),
default=None,
metavar="OUTPUT_PATH",
help="Generate manifest at path.",
)
@click.pass_obj
def integrity(ctx_obj: AgtContext, manifest: Optional[str], generate: Optional[str]) -> None:
"""Verify or generate module integrity manifest."""
import json as json_mod
import os
try:
if generate and manifest:
_print("Error: --manifest and --generate are mutually exclusive", style="red", err=True)
raise SystemExit(1)
from agent_compliance.integrity import IntegrityVerifier
if generate:
verifier = IntegrityVerifier()
result = verifier.generate_manifest(generate)
if ctx_obj.output_json:
click.echo(
json_mod.dumps(
{
"status": "ok",
"path": generate,
"files": len(result["files"]),
"functions": len(result["functions"]),
},
indent=2,
)
)
else:
click.echo(f"Manifest written to {generate}")
click.echo(f" Files hashed: {len(result['files'])}")
click.echo(f" Functions hashed: {len(result['functions'])}")
return
if manifest and not os.path.exists(manifest):
_print(f"Error: manifest file not found: {manifest}", style="red", err=True)
raise SystemExit(1)
verifier = IntegrityVerifier(manifest_path=manifest)
report = verifier.verify()
if ctx_obj.output_json:
click.echo(json_mod.dumps(report.to_dict(), indent=2))
else:
click.echo(report.summary())
if not report.passed:
raise SystemExit(1)
except SystemExit:
raise
except Exception as e:
_handle_error(e, ctx_obj.output_json)
raise SystemExit(1)
@cli.command("lint-policy")
@click.argument("path", type=click.Path(exists=True))
@click.option("--strict", is_flag=True, default=False, help="Treat warnings as errors.")
@click.pass_obj
def lint_policy(ctx_obj: AgtContext, path: str, strict: bool) -> None:
"""Lint YAML policy files for common mistakes."""
import json as json_mod
try:
from agent_compliance.lint_policy import lint_path
result = lint_path(path)
if ctx_obj.output_json:
click.echo(json_mod.dumps(result.to_dict(), indent=2))
else:
for msg in result.messages:
click.echo(msg)
if result.messages:
click.echo()
click.echo(result.summary())
if strict and result.warnings:
raise SystemExit(1)
if not result.passed:
raise SystemExit(1)
except SystemExit:
raise
except Exception as e:
_handle_error(e, ctx_obj.output_json)
raise SystemExit(1)
_AGT_PACKAGES = [
("agent_governance_toolkit", "Agent Governance Toolkit", "Meta-package & compliance CLI"),
("agent_os_kernel", "Agent OS Kernel", "Policy engine & framework integrations"),
("agentmesh_platform", "AgentMesh Platform", "Zero-trust identity & trust scoring"),
("agentmesh_runtime", "AgentMesh Runtime", "Execution supervisor & privilege rings"),
("agent_sre", "Agent SRE", "SLOs, error budgets & chaos testing"),
("agentmesh_marketplace", "AgentMesh Marketplace", "Plugin lifecycle management"),
("agentmesh_lightning", "AgentMesh Lightning", "RL training governance"),
("agent_hypervisor", "Agent Hypervisor", "Session management & kill switch"),
]
@cli.command()
@click.pass_obj
def doctor(ctx_obj: AgtContext) -> None:
"""Diagnose AGT installation health."""
import json as json_mod
import platform
from pathlib import Path
py_version = platform.python_version()
results: list[Dict[str, Any]] = []
for pkg_name, display_name, description in _AGT_PACKAGES:
ver = _get_package_version(pkg_name)
results.append(
{
"package": pkg_name,
"name": display_name,
"description": description,
"installed": ver is not None,
"version": ver,
}
)
plugins = _discover_plugins()
config_locations = [
Path.cwd() / "agentmesh.yaml",
Path.cwd() / "policies",
Path.cwd() / "integrity.json",
]
config_found = {str(p): p.exists() for p in config_locations}
if ctx_obj.output_json:
report = {
"python_version": py_version,
"packages": results,
"plugins": list(plugins.keys()),
"config_files": config_found,
}
click.echo(json_mod.dumps(report, indent=2))
return
_print(f"\n🩺 AGT Doctor — Python {py_version}", style="bold blue")
_print("")
installed_count = sum(1 for r in results if r["installed"])
total_count = len(results)
if _HAS_RICH and _console is not None and not ctx_obj.no_color:
table = _RichTable(
title="Installed Packages",
box=_rich_box.ROUNDED,
show_lines=False,
)
table.add_column("Package", style="cyan", no_wrap=True)
table.add_column("Version", style="green")
table.add_column("Status")
table.add_column("Description", style="dim")
for r in results:
status = "[green]✓ installed[/green]" if r["installed"] else "[dim]· not installed[/dim]"
ver = r["version"] or "—"
table.add_row(r["package"], ver, status, r["description"])
_console.print(table)
else:
click.echo("Installed Packages:")
click.echo("-" * 70)
for r in results:
status = "✓" if r["installed"] else "·"
ver = r["version"] or "—"
click.echo(f" {status} {r['package']:30s} {ver:12s} {r['description']}")
_print(f"\n {installed_count}/{total_count} packages installed", style="bold")
if plugins:
_print(f"\n Plugin commands: {', '.join(sorted(plugins.keys()))}", style="green")
else:
_print("\n No plugin commands registered (install AGT packages with [full] extras)", style="dim")
_print("\n Config files:", style="bold")
for path_str, exists in config_found.items():
icon = "✓" if exists else "·"
_print(f" {icon} {path_str}")
_print("")
def _handle_error(e: Exception, output_json: bool = False) -> None:
"""Centralized error handler."""
import json as json_mod
import os
is_known = isinstance(
e,
(IOError, ValueError, KeyError, PermissionError, FileNotFoundError),
)
if output_json:
err_type = "ValidationError" if is_known else "InternalError"
err_msg = str(e) if is_known else "An internal error occurred"
click.echo(
json_mod.dumps(
{
"status": "error",
"message": err_msg,
"type": err_type,
},
indent=2,
)
)
return
if is_known:
_print(f"Error: {e}", style="red", err=True)
else:
_print("Error: An internal error occurred", style="red", err=True)
if os.environ.get("AGENTOS_DEBUG"):
_print(f" {e}", style="dim", err=True)
def main() -> None:
"""Console-script entry point."""
cli(standalone_mode=True)
if __name__ == "__main__":
main()