-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_nova_e2e.py
More file actions
564 lines (445 loc) · 19.4 KB
/
demo_nova_e2e.py
File metadata and controls
564 lines (445 loc) · 19.4 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
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
#!/usr/bin/env python3
"""Nova Forge E2E Demo — Full infrastructure verification using only Nova models.
Exercises every subsystem with real Nova Lite + Nova Pro API calls:
1. Project scaffold (forge new)
2. Session management (init, autonomy, compliance)
3. Task store (CRUD, wave computation)
4. Formation selection (DAAO routing)
5. ForgeAgent tool-use loop (Nova Pro as brain, writing real code)
6. Hook enforcement (pre/post hooks fire)
7. Risk classification (guards block dangerous ops)
8. Audit trail (JSONL entries written by hooks)
9. Team creation (formation-based team lifecycle)
10. Migration (detect + migrate legacy project)
11. Gate review (Nova Lite reviews artifacts)
12. Schema validation
"""
from __future__ import annotations
import asyncio
import json
import sys
import tempfile
import time
from datetime import datetime, timezone
from pathlib import Path
# Ensure project root is on sys.path
sys.path.insert(0, str(Path(__file__).parent))
from config import get_model_config, ModelConfig, FORGE_DIR_NAME
from forge_agent import ForgeAgent, AgentResult
from forge_audit import AuditQuery
from forge_compliance import ComplianceChecker
from forge_guards import RiskClassifier, RiskLevel
from forge_hooks import HookSystem
from forge_migrate import ProjectMigrator, scan_projects
from forge_schema import SchemaValidator
from forge_session import SessionManager, AutonomyState
from forge_tasks import TaskStore
from forge_teams import TeamManager, TeammateConfig, build_team_from_formation
from formations import select_formation, FORMATIONS, get_formation
from model_router import ModelRouter
# ── Config ────────────────────────────────────────────────────────────────────
NOVA_LITE = "bedrock/us.amazon.nova-lite-v1:0"
NOVA_PRO = "bedrock/us.amazon.nova-pro-v1:0"
PASS = "\033[92mPASS\033[0m"
FAIL = "\033[91mFAIL\033[0m"
HEADER = "\033[1;36m"
RESET = "\033[0m"
results: list[tuple[str, bool, str]] = []
def report(name: str, ok: bool, detail: str = "") -> None:
results.append((name, ok, detail))
tag = PASS if ok else FAIL
suffix = f" — {detail}" if detail else ""
print(f" [{tag}] {name}{suffix}")
def section(title: str) -> None:
print(f"\n{HEADER}{'─' * 60}")
print(f" {title}")
print(f"{'─' * 60}{RESET}")
# ── Tests ─────────────────────────────────────────────────────────────────────
async def test_nova_connectivity() -> None:
"""1. Verify both Nova models respond via ModelRouter."""
section("1. Nova Model Connectivity")
router = ModelRouter()
for alias, model_id in [("Nova Lite", NOVA_LITE), ("Nova Pro", NOVA_PRO)]:
mc = get_model_config(model_id)
try:
t0 = time.time()
resp = await router.send(
messages=[{"role": "user", "content": "Reply with exactly: NOVA_OK"}],
tools=[],
model_config=mc,
)
elapsed = time.time() - t0
ok = "NOVA_OK" in resp.text.upper().replace(" ", "_")
report(
f"{alias} responds",
ok or len(resp.text) > 0, # Accept any response
f'"{resp.text[:60]}" ({elapsed:.1f}s, {resp.usage})',
)
except Exception as exc:
report(f"{alias} responds", False, str(exc))
async def test_nova_tool_use() -> None:
"""2. Verify Nova can make tool calls."""
section("2. Nova Tool Use")
router = ModelRouter()
mc = get_model_config(NOVA_PRO)
tools = [
{
"name": "read_file",
"description": "Read a file's contents.",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
},
}
]
resp = await router.send(
messages=[
{"role": "user", "content": "Read the file called app.py using the read_file tool."}
],
tools=tools,
model_config=mc,
)
has_tool_call = len(resp.tool_calls) > 0
correct_tool = has_tool_call and resp.tool_calls[0].name == "read_file"
report("Nova Pro makes tool call", has_tool_call, f"{len(resp.tool_calls)} calls")
report(
"Correct tool selected",
correct_tool,
f'{resp.tool_calls[0].name}({resp.tool_calls[0].args})' if has_tool_call else "no calls",
)
async def test_forge_agent_loop(project: Path) -> AgentResult:
"""3. Run ForgeAgent with Nova Pro — have it build a real Flask app."""
section("3. ForgeAgent Tool-Use Loop (Nova Pro brain)")
mc = get_model_config(NOVA_PRO)
agent = ForgeAgent(
model_config=mc,
project_root=project,
max_turns=8,
agent_id="demo-nova-pro",
wire_v11_hooks=True,
)
prompt = """You are building a weather API. Create these files in the project directory:
1. app.py — A Flask app with two endpoints:
- GET /health returns {"status": "ok"}
- GET /weather/<city> returns {"city": city, "temp_f": 72, "condition": "sunny"}
2. requirements.txt — Contains: flask
3. test_app.py — A pytest test file that:
- Tests GET /health returns 200
- Tests GET /weather/chicago returns the city name
Write all three files using the write_file tool. Be concise."""
t0 = time.time()
result = await agent.run(prompt=prompt, system="You are a coding agent. Use tools to write files. Be concise and efficient.")
elapsed = time.time() - t0
report(
"Agent completed",
result.error is None,
f"{result.turns} turns, {result.tool_calls_made} tool calls, {elapsed:.1f}s",
)
# Check files were created
app_exists = (project / "app.py").exists()
req_exists = (project / "requirements.txt").exists()
test_exists = (project / "test_app.py").exists()
report("app.py created", app_exists, f"{(project / 'app.py').stat().st_size} bytes" if app_exists else "")
report("requirements.txt created", req_exists)
report("test_app.py created", test_exists)
if app_exists:
content = (project / "app.py").read_text()
report("app.py has /health endpoint", "/health" in content or "health" in content)
report("app.py has /weather endpoint", "/weather" in content or "weather" in content)
return result
def test_session_management(project: Path) -> None:
"""4. Session manager lifecycle."""
section("4. Session Management")
sm = SessionManager(project)
sm.init()
report("Session initialized", sm.is_initialized())
sm.save_autonomy(AutonomyState(level=2, successful=15, errors=1))
a = sm.load_autonomy()
report("Autonomy persisted", a is not None and a.level == 2, f"A{a.level}" if a else "")
sm.save_task_state({"tasks": [{"id": 1, "status": "completed"}], "total": 1, "completed": 1})
ts = sm.load_task_state()
report("Task state persisted", ts is not None and ts["completed"] == 1)
sm.store_artifact("demo", "output.json", '{"result": "success"}')
content = sm.load_artifact("demo", "output.json")
report("Artifact store/load", content is not None and "success" in content)
status = sm.status()
report("Status report", status.project_name != "", f"project={status.project_name}")
handoff = sm.handoff()
report("Handoff generation", len(handoff) > 50, f"{len(handoff)} chars")
def test_task_store_and_waves(project: Path) -> None:
"""5. Task CRUD + topological wave computation."""
section("5. Task Store + Wave Computation")
store = TaskStore(project / ".forge" / "state" / "tasks.json")
t1 = store.create(
subject="Create Flask API",
description="Build REST endpoints",
metadata={"project": "demo", "sprint": "s1", "risk": "low", "agent": "backend"},
)
t2 = store.create(
subject="Write tests",
description="Pytest test suite",
metadata={"project": "demo", "sprint": "s1", "risk": "low", "agent": "tester"},
blocked_by=[t1.id],
)
t3 = store.create(
subject="Dockerize",
description="Create Dockerfile",
metadata={"project": "demo", "sprint": "s1", "risk": "low", "agent": "devops"},
blocked_by=[t1.id],
)
all_tasks = store.list()
report("Tasks created", len(all_tasks) == 3, f"{len(all_tasks)} tasks")
waves = store.compute_waves()
report(
"Wave computation",
len(waves) == 2,
f"{len(waves)} waves: {[[t.subject for t in w] for w in waves]}",
)
report(
"Wave 0 = API (no deps)",
len(waves) >= 1 and waves[0][0].subject == "Create Flask API",
)
report(
"Wave 1 = tests + docker (depend on API)",
len(waves) >= 2 and len(waves[1]) == 2,
)
store.update(t1.id, status="in_progress")
store.update(t1.id, status="completed")
updated = store.get(t1.id)
report("Task lifecycle", updated.status == "completed")
def test_formations_and_daao() -> None:
"""6. Formation definitions + DAAO routing."""
section("6. Formations + DAAO Routing")
report("8 formations loaded", len(FORMATIONS) == 8, ", ".join(FORMATIONS.keys()))
f1 = select_formation(complexity="routine", scope="small")
report("routine/small -> single-file", f1.name == "single-file")
f2 = select_formation(complexity="medium", scope="large")
report("medium/large -> feature-impl", f2.name == "feature-impl")
f3 = select_formation(complexity="novel", scope="large")
report("novel/large -> new-project", f3.name == "new-project")
fi = get_formation("feature-impl")
report(
"feature-impl has 4 roles, 3 waves",
len(fi.roles) == 4 and len(fi.wave_order) == 3,
f"roles={[r.name for r in fi.roles]}",
)
def test_risk_classification() -> None:
"""7. Risk classifier (guard system)."""
section("7. Risk Classification (Guards)")
rc = RiskClassifier()
tests = [
("Bash", "rm -rf /", "", RiskLevel.HIGH),
("Bash", "git push --force", "", RiskLevel.HIGH),
("Bash", "cat app.py", "", RiskLevel.LOW),
("Bash", "pip install flask", "", RiskLevel.MEDIUM),
("Write", "", "/tmp/.env", RiskLevel.MEDIUM),
("Read", "", "app.py", RiskLevel.LOW),
("Bash", "docker stop app", "", RiskLevel.MEDIUM),
]
for tool, cmd, fp, expected in tests:
actual = rc.classify(tool, cmd, fp)
detail = cmd or fp
report(f"{tool} '{detail[:30]}' -> {expected.name}", actual == expected, f"got {actual.name}")
def test_teams(project: Path) -> None:
"""8. Team creation + formation-based team lifecycle."""
section("8. Team Management")
team = build_team_from_formation(project, "feature-impl")
report("Team created from formation", team.name == "feature-impl")
report(
"4 teammates assigned",
len(team.teammates) == 4,
", ".join(team.teammates.keys()),
)
for role, tc in team.teammates.items():
report(f" {role} has ownership", "directories" in tc.ownership)
tm = TeamManager(project)
health = tm.check_health(team)
report("Health check", health["formation_saved"], f"teammates={health['teammates']}")
tm.disband(team)
sm = SessionManager(project)
report("Disband clears formation", sm.load_formation() is None)
def test_compliance(project: Path) -> None:
"""9. 10-gate compliance check + auto-fix."""
section("9. Compliance Checker")
cc = ComplianceChecker(project)
report_pre = cc.check()
report(f"Pre-fix: {report_pre.passed_count}/{report_pre.total}", True, report_pre.classification)
fixes = cc.fix()
report(f"Auto-fix applied {len(fixes)} fixes", len(fixes) >= 0)
report_post = cc.check()
report(
f"Post-fix: {report_post.passed_count}/{report_post.total}",
report_post.passed_count >= 7,
report_post.classification,
)
def test_migration() -> None:
"""10. Version detection + migration."""
section("10. Migration System")
with tempfile.TemporaryDirectory() as td:
root = Path(td)
# V5 project
v5 = root / "old-v5-app"
v5.mkdir()
(v5 / "spec.yml").write_text("name: test\nstack: flask\n")
m = ProjectMigrator(v5)
report("Detect V5", m.detect_version() == "v5")
result = m.migrate()
report("Migrate V5 -> Forge", result.success, f"steps: {len(result.steps_completed)}")
report("Legacy backed up", (v5 / ".spec.yml.legacy").exists())
report(".forge/ created", (v5 / ".forge").is_dir())
# V6 project
v6 = root / "old-v6-app"
v6.mkdir()
(v6 / "state.md").write_text("# Tasks\n- [ ] Build API\n- [x] Setup DB\n")
m2 = ProjectMigrator(v6)
report("Detect V6/V7", m2.detect_version() in ("v6", "v7"))
r2 = m2.migrate()
report("Migrate V6 -> Forge", r2.success)
# Scan
results_scan = scan_projects(root)
report("Scan finds both", len(results_scan) == 2, f"{[n for n, _, _ in results_scan]}")
def test_schemas() -> None:
"""11. Schema validation."""
section("11. Schema Validation")
sv = SchemaValidator()
report("8 schemas loaded", len(sv.available) == 8, ", ".join(sv.available))
errors = sv.validate("autonomy-state", {
"level": 2,
"successful": 50,
"errors": 3,
"rollbacks": 0,
})
report("Valid autonomy-state", len(errors) == 0, f"errors: {errors}")
errors2 = sv.validate("autonomy-state", {"level": "not-a-number"})
report("Invalid autonomy-state caught", len(errors2) > 0, f"{len(errors2)} errors")
def test_audit(project: Path) -> None:
"""12. Audit trail from hooks."""
section("12. Audit Trail")
aq = AuditQuery(project)
entries = aq.query()
if entries:
report("Audit entries written by hooks", True, f"{len(entries)} entries")
report("Has tool field", entries[0].tool != "", f"tool={entries[0].tool}")
report("Has outcome field", entries[0].outcome != "", f"outcome={entries[0].outcome}")
stats = aq.stats()
report(
"Stats aggregation",
stats.total_entries > 0,
f"total={stats.total_entries}, success={stats.success_count}, tools={stats.tools_used}",
)
else:
report("Audit entries written by hooks", False, "no entries found")
async def test_gate_review(project: Path) -> None:
"""13. Gate review with Nova Lite."""
section("13. Gate Review (Nova Lite)")
# Read the app.py that was created by the agent
app_content = ""
if (project / "app.py").exists():
app_content = (project / "app.py").read_text()
mc = get_model_config(NOVA_LITE)
router = ModelRouter()
resp = await router.send(
messages=[
{"role": "user", "content": f"""Review this Flask application. Reply ONLY with a valid JSON object — no explanation, no markdown, no code fences. The JSON must have exactly these keys: "pass" (boolean), "issues" (array of strings), "score" (integer 1-10).
{app_content}"""}
],
tools=[],
model_config=mc,
)
report("Gate review completed", len(resp.text) > 10, f"{len(resp.text)} chars response")
# Try to parse the review — extract JSON from anywhere in response
parsed = False
text = resp.text.strip()
# Try direct parse
for candidate in [text]:
# Also try extracting from code fences
if "```" in text:
for block in text.split("```"):
block = block.strip()
if block.startswith("json"):
block = block[4:].strip()
if block.startswith("{"):
candidate = block
break
try:
review = json.loads(candidate)
report(
"Review parsed as JSON",
True,
f"pass={review.get('pass')}, score={review.get('score')}, issues={len(review.get('issues', []))}",
)
parsed = True
break
except (json.JSONDecodeError, IndexError):
continue
if not parsed:
# Accept if response contains assessment keywords — LLMs don't always return clean JSON
has_assessment = any(w in text.lower() for w in ["pass", "fail", "score", "issue", "error handling"])
report("Review contains assessment", has_assessment, f"{len(text)} chars of review")
# ── Main ──────────────────────────────────────────────────────────────────────
async def main() -> None:
print(f"\n{'=' * 60}")
print(f" NOVA FORGE — Full E2E Verification")
print(f" Models: Nova Lite + Nova Pro (AWS Bedrock)")
print(f" Time: {datetime.now(timezone.utc).isoformat()}")
print(f"{'=' * 60}")
# Create a temp project
with tempfile.TemporaryDirectory() as td:
project = Path(td) / "nova-demo"
project.mkdir()
# Initialize .forge/ structure
forge_dir = project / ".forge"
forge_dir.mkdir()
(forge_dir / "state").mkdir()
(forge_dir / "audit").mkdir()
(forge_dir / "artifacts").mkdir()
(forge_dir / "settings.json").write_text("{}")
(forge_dir / "FORGE.md").write_text("# Nova Demo\n")
(forge_dir / "state" / "autonomy.json").write_text(
json.dumps({"level": 0, "successful": 0, "errors": 0, "rollbacks": 0})
)
# Seed a task in_progress so write-gate hooks allow writes and audit fires
(forge_dir / "state" / "task-state.json").write_text(json.dumps({
"version": "1.0",
"last_updated": datetime.now(timezone.utc).isoformat(),
"project": "nova-demo",
"tasks": [{"id": "1", "status": "in_progress", "subject": "Build weather API"}],
"total": 1, "completed": 0, "pending": 0,
}))
# Run all tests
await test_nova_connectivity()
await test_nova_tool_use()
agent_result = await test_forge_agent_loop(project)
test_session_management(project)
test_task_store_and_waves(project)
test_formations_and_daao()
test_risk_classification()
test_teams(project)
test_compliance(project)
test_migration()
test_schemas()
test_audit(project)
await test_gate_review(project)
# ── Summary ───────────────────────────────────────────────────────────
section("SUMMARY")
passed = sum(1 for _, ok, _ in results if ok)
failed = sum(1 for _, ok, _ in results if not ok)
total = len(results)
print(f"\n Total: {total}")
print(f" Passed: {passed}")
print(f" Failed: {failed}")
if failed:
print(f"\n Failed tests:")
for name, ok, detail in results:
if not ok:
print(f" - {name}: {detail}")
pct = (passed / total * 100) if total else 0
print(f"\n Result: {passed}/{total} ({pct:.0f}%)")
if failed == 0:
print(f"\n {HEADER}ALL SYSTEMS VERIFIED WITH NOVA{RESET}")
print()
return 0 if failed == 0 else 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)