-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (131 loc) · 4.8 KB
/
main.py
File metadata and controls
157 lines (131 loc) · 4.8 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
"""
AutoAlign — CLI Entry Point
Usage:
python main.py align examples/sample_brd.md
python main.py align examples/sample_brd.md --output aligned_brd.md
python main.py align examples/sample_brd.md --max-iterations 3
python main.py query "What are the PII logging rules?"
python main.py rebuild-kb
"""
import sys
from pathlib import Path
from typing import Optional
import typer
from rich.console import Console
from rich.panel import Panel
from rich.markdown import Markdown
from rich.table import Table
from rich import print as rprint
app = typer.Typer(
name="autoalign",
help="AutoAlign — Autonomous Governance: Converting Static Docs into Living Policy",
add_completion=False,
)
console = Console()
def _print_banner():
console.print(
Panel.fit(
"[bold cyan]AutoAlign[/bold cyan] [dim]v1.0.0[/dim]\n"
"[dim]Autonomous Governance · HackFest 2.0 · Team Ninja Turtles[/dim]",
border_style="cyan",
)
)
@app.command()
def align(
brd_path: str = typer.Argument(..., help="Path to the BRD Markdown file"),
output: Optional[str] = typer.Option(
None, "--output", "-o", help="Save aligned BRD to this file"
),
max_iterations: int = typer.Option(
5, "--max-iterations", "-n", help="Maximum debate iterations"
),
rebuild_kb: bool = typer.Option(
False, "--rebuild-kb", help="Force rebuild the policy knowledge base"
),
show_diff: bool = typer.Option(
False, "--diff", help="Show a side-by-side diff (original vs aligned)"
),
):
"""
Align a Business Requirement Document with internal governance policies.
"""
_print_banner()
from turgon import TurgonClient
brd_file = Path(brd_path)
if not brd_file.exists():
console.print(f"[red]Error:[/red] BRD file not found: {brd_path}")
raise typer.Exit(1)
console.print(f"\nAligning [cyan]{brd_file.name}[/cyan] with internal policies...\n")
client = TurgonClient(force_rebuild_kb=rebuild_kb, max_iterations=max_iterations)
result = client.align_file(str(brd_file))
# Print the compliance report
console.print("\n")
console.print(Panel(result.compliance_report, title="Compliance Report", border_style="green" if result.is_compliant else "yellow"))
# Violations table
if result.violations:
table = Table(title="Remaining Violations", show_lines=True)
table.add_column("Severity", style="bold", width=10)
table.add_column("Policy Section", width=18)
table.add_column("Description")
for v in result.violations:
color = {
"CRITICAL": "red",
"HIGH": "yellow",
"MEDIUM": "blue",
"LOW": "dim",
}.get(v.severity, "white")
table.add_row(
f"[{color}]{v.severity}[/{color}]",
v.policy_section,
v.description[:100] + ("..." if len(v.description) > 100 else ""),
)
console.print(table)
# Show aligned BRD preview
console.print("\n[bold]Aligned BRD Preview (first 60 lines):[/bold]")
preview_lines = result.aligned_brd.splitlines()[:60]
console.print(
Panel(
Markdown("\n".join(preview_lines)),
title="Aligned BRD",
border_style="cyan",
)
)
# Save output if requested
if output:
out_path = Path(output)
out_path.write_text(result.aligned_brd, encoding="utf-8")
console.print(f"\n[green]Aligned BRD saved to:[/green] {out_path}")
# Summary
console.print(
f"\n[bold]Summary:[/bold] {result.summary()}"
)
raise typer.Exit(0 if result.is_compliant else 1)
@app.command()
def query(
question: str = typer.Argument(..., help="A natural language policy question"),
rebuild_kb: bool = typer.Option(
False, "--rebuild-kb", help="Force rebuild the policy knowledge base"
),
):
"""
Query the policy knowledge base with a natural language question.
"""
_print_banner()
from turgon import TurgonClient
console.print(f"\nQuerying knowledge base: [italic cyan]{question}[/italic cyan]\n")
client = TurgonClient(force_rebuild_kb=rebuild_kb)
answer = client.query_policy(question)
console.print(Panel(answer, title="Relevant Policy Sections", border_style="blue"))
@app.command(name="rebuild-kb")
def rebuild_kb():
"""
Force rebuild the policy knowledge base from the docs/ directory.
"""
_print_banner()
from src.knowledge_base import PolicyDocumentLoader
console.print("\nRebuilding policy knowledge base from [cyan]docs/[/cyan]...\n")
loader = PolicyDocumentLoader()
loader.build_vector_store(force_rebuild=True)
console.print("[green]Knowledge base rebuilt successfully.[/green]")
if __name__ == "__main__":
app()