-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·1418 lines (1221 loc) · 50.5 KB
/
Copy pathcli.py
File metadata and controls
executable file
·1418 lines (1221 loc) · 50.5 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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Morpheus CLI - morpheus <command>
Agent State Compiler with verifiable provenance.
"""
import json
import os
import re
import socket
from fnmatch import fnmatch
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread
from types import SimpleNamespace
from urllib.parse import urlencode
import typer
from pathlib import Path
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from morpheus.core.config import MorpheusConfig
from morpheus.core.compiler import DEFAULT_EXCLUDE_PATTERNS, compile_project
from morpheus.core.wake import generate_wake_md
from morpheus.core.provenance import (
compute_sha256_file,
compute_sha256_bytes,
build_receipt,
evidence_jsonl_bytes,
latest_receipt_file,
new_receipt_id,
receipt_file_name,
)
from morpheus.core.safe_io import reject_symlink_components, reject_symlink_paths
from morpheus.core.semantic.review import (
ReviewStore,
apply_accepted_candidates,
run_semantic_review,
)
from morpheus.core.verify import verify_receipt_chain
from morpheus.core.providers.fake import FakeProvider
from morpheus.core.providers.local import LocalProvider
from morpheus.core.providers.null import NullProvider
from morpheus.core.providers.ollama import OllamaProvider
from morpheus.integrations.manifest import integration_cache_path_error, integration_manifest
from morpheus.training.consolidate import consolidate_sessions
from morpheus.training.train import check_dependencies
app = typer.Typer(
help="Morpheus AI — Agent State Compiler with verifiable provenance",
add_completion=False
)
review_app = typer.Typer(help="Review semantic candidates before they become active state.")
app.add_typer(review_app, name="review")
console = Console()
WILDCARD_HOSTS = {"0.0.0.0", "::", ""}
DEFAULT_MODEL_SMOKE_MODEL = "qwen2.5:0.5b"
DEFAULT_MODEL_SMOKE_PROMPT = (
"Reply with one short sentence confirming Morpheus model smoke test is working."
)
STALE_TEXT_SUFFIXES = {
".md",
".mdx",
".txt",
".rst",
".toml",
".yaml",
".yml",
".json",
".py",
".js",
".ts",
".tsx",
".html",
".css",
}
STALE_SCAN_ROOT_FILES = {
"AGENTS.md",
"CHANGELOG.md",
"CONTRIBUTING.md",
"README.md",
"README.ru.md",
"SECURITY.md",
"SPEC.md",
"pyproject.toml",
}
STALE_POSITIONING_RULES = [
{
"rule_id": "personal_ai_agent",
"pattern": re.compile(r"\bpersonal AI agent\b", re.IGNORECASE),
"suggested_replacement": (
"Morpheus is an Agent State Compiler that generates WAKE.md."
),
},
{
"rule_id": "daily_lora_core",
"pattern": re.compile(
r"\b(daily training|daily memory consolidation|weights-as-memory)\b",
re.IGNORECASE,
),
"suggested_replacement": (
"LoRA is experimental; compile, retrieve, cite evidence, and verify receipts "
"are the core path."
),
},
{
"rule_id": "eu_ai_act_claim",
"pattern": re.compile(r"\bEU AI Act compliant by design\b", re.IGNORECASE),
"suggested_replacement": (
"Designed for provenance, local-first operation, source attribution, "
"and user-controlled state export."
),
},
{
"rule_id": "memory_compiler_pitch",
"pattern": re.compile(r"\bLocal-first memory compiler for AI agents\b", re.IGNORECASE),
"suggested_replacement": (
"WAKE.md for AI agents — compile project state so agents stop starting cold."
),
},
]
class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
"""Static file handler that keeps CLI output focused on Morpheus URLs."""
def log_message(self, format: str, *args) -> None:
return
class ReusableThreadingHTTPServer(ThreadingHTTPServer):
allow_reuse_address = True
def server_bind(self):
if self.allow_reuse_address and hasattr(socket, "SO_REUSEADDR"):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if (
self.allow_reuse_port
and hasattr(socket, "SO_REUSEPORT")
and self.address_family in (socket.AF_INET, socket.AF_INET6)
):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
self.socket.bind(self.server_address)
self.server_address = self.socket.getsockname()
host, port = self.server_address[:2]
self.server_name = host
self.server_port = port
def display_url(host: str, port: int, path: str = "") -> str:
"""Return a URL humans can open when a service binds to host."""
visible_host = "127.0.0.1" if host in WILDCARD_HOSTS else host
if ":" in visible_host and not visible_host.startswith("["):
visible_host = f"[{visible_host}]"
visible_path = path if not path or path.startswith("/") else f"/{path}"
return f"http://{visible_host}:{port}{visible_path}"
def ui_entrypoint_path(api_base: str) -> str:
"""Return the static UI entrypoint with an explicit backend API hint."""
return f"/ui/index.html?{urlencode({'api': api_base})}"
def primary_lan_ip() -> str | None:
"""Best-effort LAN IP for cross-device URLs; returns None offline."""
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.connect(("8.8.8.8", 80))
ip_address = sock.getsockname()[0]
except OSError:
return None
if ip_address.startswith("127."):
return None
return ip_address
def default_ui_root() -> Path:
"""Find the source tree that contains ui/index.html."""
candidates = [Path.cwd(), Path(__file__).resolve().parents[1]]
for candidate in candidates:
if (candidate / "ui" / "index.html").is_file():
return candidate
return Path.cwd()
def resolve_ui_root_or_exit(ui_root: Path | None) -> Path:
"""Validate the directory served by `morpheus serve --ui`."""
root = ui_root.expanduser() if ui_root else default_ui_root()
try:
reject_symlink_components(root, "UI root")
except ValueError as exc:
console.print(f"[red]UI root invalid:[/red] {exc}")
raise typer.Exit(1) from exc
if not root.is_dir():
console.print(f"[red]UI root not found:[/red] {root}")
raise typer.Exit(1)
entrypoint = root / "ui" / "index.html"
try:
reject_symlink_components(entrypoint, "UI entrypoint")
reject_symlink_paths([entrypoint], "UI entrypoint")
except ValueError as exc:
console.print(f"[red]UI entrypoint invalid:[/red] {exc}")
raise typer.Exit(1) from exc
if not entrypoint.is_file():
console.print("[red]UI entrypoint not found[/red]")
console.print("Expected UI file: ui/index.html")
console.print(f"Expected path: {entrypoint}")
raise typer.Exit(1)
return root
def start_static_ui_server(*, directory: Path, host: str, port: int):
"""Start the static UI server in a daemon thread and return the server."""
handler = partial(QuietHTTPRequestHandler, directory=str(directory))
server = ReusableThreadingHTTPServer((host, port), handler)
thread = Thread(target=server.serve_forever, daemon=True)
thread.start()
return server
def serve_summary_lines(host: str, port: int, ui_host: str | None, ui_port: int) -> list[str]:
local_api = display_url(host, port)
lines = [f"API: {local_api}"]
needs_lan_ip = host in WILDCARD_HOSTS or ui_host in WILDCARD_HOSTS
lan_ip = primary_lan_ip() if needs_lan_ip else None
if lan_ip and host in WILDCARD_HOSTS:
lines.append(f"LAN API: {display_url(lan_ip, port)}")
if ui_host is not None:
lines.append(f"UI: {display_url(ui_host, ui_port, ui_entrypoint_path(local_api))}")
if lan_ip and ui_host in WILDCARD_HOSTS:
lan_api = display_url(lan_ip if host in WILDCARD_HOSTS else host, port)
lines.append(f"LAN UI: {display_url(lan_ip, ui_port, ui_entrypoint_path(lan_api))}")
return lines
def ensure_initialized():
"""Check if morpheus is initialized in current directory."""
morpheus_dir = Path.cwd() / ".morpheus"
if morpheus_dir.is_symlink() or not morpheus_dir.is_dir():
console.print("[red]Not initialized. Run 'morpheus init' first.[/red]")
raise typer.Exit(1)
return morpheus_dir
def latest_receipt_or_exit(receipts_dir: Path) -> Path | None:
"""Return the receipt chain tail or exit with a user-facing error."""
try:
return latest_receipt_file(receipts_dir)
except (json.JSONDecodeError, ValueError) as exc:
console.print(f"[red]Receipt chain invalid:[/red] {exc}")
raise typer.Exit(1) from exc
def load_json_or_exit(path: Path, label: str) -> dict:
"""Load a JSON object or exit with a user-facing error."""
try:
reject_symlink_paths([path], label)
data = json.loads(path.read_text())
except (OSError, ValueError, json.JSONDecodeError) as exc:
console.print(f"[red]{label} invalid:[/red] {exc}")
raise typer.Exit(1) from exc
if not isinstance(data, dict):
console.print(f"[red]{label} invalid:[/red] expected JSON object")
raise typer.Exit(1)
return data
def list_count(value) -> int:
"""Return the length only for JSON arrays."""
return len(value) if isinstance(value, list) else 0
def receipt_claim_total(value) -> int:
"""Return total receipt claims only for numeric claim-count mappings."""
if not isinstance(value, dict):
return 0
return sum(count for count in value.values() if isinstance(count, int))
def integration_token_path_error(token_path: Path, service_label: str) -> str | None:
token_dir = token_path.parent
if token_dir.is_symlink():
return f"{service_label} token directory must not be a symlink: {token_dir}"
if token_dir.exists() and not token_dir.is_dir():
return f"{service_label} token directory is not a directory: {token_dir}"
if token_path.is_symlink():
return f"{service_label} token path must not be a symlink: {token_path}"
if token_path.exists() and not token_path.is_file():
return f"{service_label} token path is not a file: {token_path}"
return None
def github_token_path_error(token_path: Path) -> str | None:
return integration_token_path_error(token_path, "GitHub")
def integration_status(token_path: Path, service_label: str) -> str:
if integration_token_path_error(token_path, service_label):
return "[red]invalid[/red]"
if token_path.is_file():
return "[green]configured[/green]"
return "[yellow]not configured[/yellow]"
def rich_integration_status(status: str) -> str:
labels = {
"configured": "[green]configured[/green]",
"cache_ready": "[green]cache ready[/green]",
"not_configured": "[yellow]not configured[/yellow]",
"invalid": "[red]invalid[/red]",
}
return labels.get(status, status)
def request_context(api_base: str):
"""Build the small request shape shared API helpers need."""
clean_api_base = api_base.rstrip("/")
return SimpleNamespace(
base_url=clean_api_base + "/",
embedded_agent_api_base=clean_api_base,
)
def ensure_project_initialized(project_root: Path) -> tuple[Path, bool]:
"""Initialize .morpheus for a chosen project root when needed."""
if project_root.is_symlink():
raise ValueError(f"Project root must not be a symlink: {project_root}")
reject_symlink_components(project_root, "Project root")
project_root = project_root.resolve()
if not project_root.is_dir():
raise ValueError(f"Project root is not a directory: {project_root}")
morpheus_dir = project_root / ".morpheus"
if morpheus_dir.is_symlink():
raise ValueError(".morpheus path must not be a symlink")
if morpheus_dir.exists() and not morpheus_dir.is_dir():
raise ValueError(".morpheus path is not a directory")
initialized = not morpheus_dir.exists()
MorpheusConfig(project_root=project_root).init_default()
return morpheus_dir, initialized
def copy_public_wake(project_root: Path, morpheus_dir: Path) -> Path:
"""Copy the compiled private WAKE.md to the project root for public handoff."""
private_wake = morpheus_dir / "WAKE.md"
public_wake = project_root / "WAKE.md"
reject_symlink_paths([private_wake, public_wake], "WAKE.md")
if public_wake.exists() and not public_wake.is_file():
raise ValueError(f"WAKE.md path is not a file: {public_wake}")
public_wake.write_text(private_wake.read_text())
return public_wake
def wake_handoff_prompt() -> str:
"""Return the short prompt printed by the one-command wake flow."""
return (
"Read WAKE.md before editing. Treat it as current project state, then run "
"`morpheus compile` and `morpheus verify --all` after meaningful changes."
)
def semantic_provider_from_env():
"""Resolve the explicit semantic provider without making cloud calls."""
provider_name = os.getenv("MORPHEUS_SEMANTIC_PROVIDER", "local").strip().lower()
if provider_name in {"", "local"}:
return LocalProvider()
if provider_name == "fake":
return FakeProvider()
if provider_name == "null":
return NullProvider()
if provider_name == "ollama":
provider = OllamaProvider()
model = os.getenv("MORPHEUS_SEMANTIC_MODEL")
if model:
provider.model = model
return provider
raise ValueError(f"Unsupported semantic provider: {provider_name}")
def run_semantic_review_or_exit(project_root: Path) -> dict:
try:
provider = semantic_provider_from_env()
report = run_semantic_review(project_root, provider=provider)
except (OSError, ValueError) as exc:
console.print(f"[red]Semantic review failed:[/red] {exc}")
raise typer.Exit(1) from exc
console.print(
"[green]✓ Semantic review:[/green] "
f"{report['candidates_total']} candidates, "
f"{report['source_backed_total']} source-backed"
)
return report
def find_stale_positioning_claims(project_root: Path) -> list[dict[str, object]]:
"""Find launch-positioning claims that conflict with the WAKE.md framing."""
if project_root.is_symlink():
raise ValueError(f"Project root must not be a symlink: {project_root}")
reject_symlink_components(project_root, "Project root")
project_root = project_root.resolve()
if not project_root.is_dir():
raise ValueError(f"Project root is not a directory: {project_root}")
findings = []
for path in sorted(project_root.rglob("*"), key=lambda item: item.as_posix()):
if path.is_symlink() or not path.is_file():
continue
if path.suffix.lower() not in STALE_TEXT_SUFFIXES:
continue
if stale_scan_path_excluded(path, project_root):
continue
if not stale_scan_path_is_launch_surface(path, project_root):
continue
try:
lines = path.read_text(errors="ignore").splitlines()
except OSError:
continue
for line_number, line in enumerate(lines, 1):
if stale_line_is_negated_or_safe(line):
continue
for rule in STALE_POSITIONING_RULES:
match = rule["pattern"].search(line)
if not match:
continue
findings.append(
{
"rule_id": rule["rule_id"],
"path": path.relative_to(project_root).as_posix(),
"line": line_number,
"excerpt": line.strip(),
"matched": match.group(0),
"suggested_replacement": rule["suggested_replacement"],
}
)
return findings
def stale_scan_path_excluded(path: Path, project_root: Path) -> bool:
"""Return true when a file should not be scanned by `morpheus stale`."""
try:
rel_path = path.relative_to(project_root)
except ValueError:
return True
rel_text = rel_path.as_posix()
for pattern in DEFAULT_EXCLUDE_PATTERNS:
if any(part == pattern for part in rel_path.parts):
return True
if fnmatch(rel_text, pattern) or fnmatch(rel_path.name, pattern):
return True
return False
def stale_scan_path_is_launch_surface(path: Path, project_root: Path) -> bool:
"""Limit default stale scans to public positioning surfaces, not tests/code."""
try:
rel_path = path.relative_to(project_root)
except ValueError:
return False
if len(rel_path.parts) == 1:
return rel_path.name in STALE_SCAN_ROOT_FILES
return rel_path.parts[0] == "docs" and path.suffix.lower() in {".md", ".mdx"}
def stale_line_is_negated_or_safe(line: str) -> bool:
"""Avoid reporting lines that intentionally reject the stale claim."""
folded = line.casefold()
safe_phrases = [
"not a personal ai agent",
"not a memory layer",
"not a lora trainer",
"lora is experimental",
"lora/training is experimental",
"not the core product path",
"not the core launch path",
]
return any(phrase in folded for phrase in safe_phrases)
@app.command()
def init(
force: bool = typer.Option(False, "--force", "-f", help="Reinitialize even if already initialized")
):
"""Initialize morpheus in current directory.
Creates .morpheus/ with morpheus.toml and ed25519 keys.
"""
morpheus_dir = Path.cwd() / ".morpheus"
if morpheus_dir.is_symlink():
console.print("[red].morpheus path must not be a symlink[/red]")
raise typer.Exit(1)
if morpheus_dir.exists() and not morpheus_dir.is_dir():
console.print("[red].morpheus path is not a directory[/red]")
raise typer.Exit(1)
if morpheus_dir.exists() and not force:
console.print("[yellow].morpheus/ already exists. Use --force to reinitialize.[/yellow]")
raise typer.Exit(1)
config = MorpheusConfig(project_root=Path.cwd())
try:
config.init_default()
except (OSError, ValueError) as exc:
console.print(f"[red]Initialization failed:[/red] {exc}")
raise typer.Exit(1) from exc
console.print(Panel.fit(
"[green]✓ Morpheus initialized[/green]\n"
f"Project: [bold]{Path.cwd().name}[/bold]\n"
"Run [bold]morpheus compile[/bold] to generate WAKE.md",
title="Morpheus AI",
border_style="green"
))
@app.command()
def compile(
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed output"),
semantic: bool = typer.Option(False, "--semantic", help="Extract semantic review candidates"),
review: bool = typer.Option(False, "--review", help="Write semantic candidates for review"),
):
"""Compile sources → state.json + WAKE.md + signed receipt.
Extracts claims (TODO:, DECISION:, FIXME:, NOTE:, HACK:, XXX:) from
project files, builds evidence chain, and generates cryptographic receipt.
"""
morpheus_dir = ensure_initialized()
project_root = Path.cwd()
console.print("[blue]Compiling project state...[/blue]")
# Compile
try:
state = compile_project(project_root)
except ValueError as exc:
console.print(f"[red]{exc}[/red]")
raise typer.Exit(1) from exc
# Get previous receipt hash
receipts_dir = morpheus_dir / "receipts"
prev_hash = None
if receipts_dir.exists():
latest = latest_receipt_or_exit(receipts_dir)
if latest:
try:
prev_hash = compute_sha256_file(latest)
except OSError as exc:
console.print(
f"[red]Receipt chain invalid:[/red] {latest.name}: "
f"unreadable receipt ({exc})"
)
raise typer.Exit(1) from exc
# Build sources list
sources_data = [{
"id": s.id,
"path": s.path,
"sha256": s.sha256,
"size_bytes": s.size_bytes,
"line_count": s.line_count
} for s in state.sources]
# Generate final WAKE.md before signing so the receipt hashes the artifact on disk.
receipt_id = new_receipt_id()
state.receipt_id = receipt_id
state_dump = state.model_dump()
state_json = json.dumps(state_dump, indent=2, default=str)
state_json_sha = compute_sha256_bytes(state_json.encode())
evidence_jsonl = evidence_jsonl_bytes(state_dump.get("evidence", []))
evidence_jsonl_sha = compute_sha256_bytes(evidence_jsonl)
wake_md = generate_wake_md(state, receipt_id)
wake_md_sha = compute_sha256_bytes(wake_md.encode())
# Build receipt
private_key_path = morpheus_dir / "keys" / "local.key"
try:
receipt = build_receipt(
state_dump,
wake_md_sha,
sources_data,
private_key_path,
prev_hash,
receipt_id=receipt_id,
state_json_sha=state_json_sha,
evidence_jsonl_sha=evidence_jsonl_sha,
)
except (OSError, ValueError) as exc:
console.print(f"[red]Signing failed:[/red] {exc}")
raise typer.Exit(1) from exc
# Write artifacts only after all target paths are known to be safe.
wake_path = morpheus_dir / "WAKE.md"
state_path = morpheus_dir / "state.json"
evidence_path = morpheus_dir / "evidence.jsonl"
receipt_path = receipts_dir / receipt_file_name(receipt["receipt_id"])
audit_log = receipts_dir / "audit.log"
try:
receipt_path.parent.mkdir(parents=True, exist_ok=True)
reject_symlink_paths(
[wake_path, state_path, evidence_path, receipt_path, audit_log],
"Output path",
)
wake_path.write_text(wake_md)
# Save state
state_path.write_text(state_json)
# Save evidence
evidence_path.write_bytes(evidence_jsonl)
# Save receipt
receipt_path.write_text(json.dumps(receipt, indent=2, default=str))
# Update audit log
with open(audit_log, "a") as f:
f.write(f"{receipt['issued_at']} {receipt['receipt_id']}\n")
except (OSError, ValueError) as exc:
console.print(f"[red]Output write failed:[/red] {exc}")
raise typer.Exit(1) from exc
# Output
if verbose:
table = Table(title="Compilation Results")
table.add_column("Metric", style="cyan")
table.add_column("Value", style="green")
table.add_row("Sources", str(len(state.sources)))
table.add_row("Claims", str(len(state.claims)))
table.add_row("Evidence", str(len(state.evidence)))
table.add_row("Receipt", receipt["receipt_id"])
table.add_row("Signed", "✓" if receipt["signature"]["signature_b64"] else "✗")
console.print(table)
else:
console.print(f"[green]✓ Compiled:[/green] {len(state.claims)} claims from {len(state.sources)} sources")
console.print(f"[green]✓ Receipt:[/green] {receipt['receipt_id']}")
if semantic:
if not review:
console.print("[red]Semantic compile is review-gated. Pass --review.[/red]")
raise typer.Exit(2)
run_semantic_review_or_exit(project_root)
@app.command()
def verify(
all: bool = typer.Option(False, "--all", "-a", help="Full provenance verification"),
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed output")
):
"""Verify receipt chain integrity.
Without --all: checks latest receipt exists.
With --all: validates entire chain + signatures.
"""
morpheus_dir = ensure_initialized()
receipts_dir = morpheus_dir / "receipts"
if not receipts_dir.exists():
console.print("[yellow]No receipts found[/yellow]")
raise typer.Exit(1)
if not receipts_dir.is_dir():
console.print("[red]Receipt chain invalid:[/red] receipts path is not a directory")
raise typer.Exit(1)
if not list(receipts_dir.glob("receipt_*.json")):
console.print("[yellow]No receipts found[/yellow]")
raise typer.Exit(1)
existing = sorted(receipts_dir.glob("receipt_*.json"))
if all:
valid, errors = verify_receipt_chain(morpheus_dir)
if valid:
console.print(Panel.fit(
"[green]✓ Receipt chain valid[/green]\n"
f"Total receipts: {len(existing)}\n"
"All signatures verified",
title="Verification Passed",
border_style="green"
))
else:
console.print(Panel.fit(
"[red]✗ Verification failed[/red]\n" + "\n".join(f" • {e}" for e in errors),
title="Verification Failed",
border_style="red"
))
raise typer.Exit(1)
else:
# Quick check
latest = latest_receipt_or_exit(receipts_dir)
receipt = load_json_or_exit(latest, "Receipt file")
if verbose:
table = Table(title="Latest Receipt")
table.add_column("Field", style="cyan")
table.add_column("Value", style="green")
table.add_row("ID", receipt.get("receipt_id", "unknown"))
table.add_row("Issued", receipt.get("issued_at", "unknown"))
table.add_row("Claims", str(receipt_claim_total(receipt.get("claim_count"))))
table.add_row("Sources", str(list_count(receipt.get("sources"))))
console.print(table)
else:
console.print(f"[green]✓ Latest:[/green] {receipt.get('receipt_id', 'unknown')}")
@app.command()
def status():
"""Show current project state summary."""
morpheus_dir = Path.cwd() / ".morpheus"
if morpheus_dir.is_symlink() or not morpheus_dir.is_dir():
console.print("[yellow]Not initialized[/yellow]")
return
state_path = morpheus_dir / "state.json"
if not state_path.exists():
console.print("[yellow]No compilation yet. Run 'morpheus compile'[/yellow]")
return
state = load_json_or_exit(state_path, "State file")
receipts_dir = morpheus_dir / "receipts"
receipt_path = latest_receipt_or_exit(receipts_dir) if receipts_dir.exists() else None
table = Table(title="Project Status")
table.add_column("Metric", style="cyan")
table.add_column("Value", style="green")
compiled_at = state.get("compiled_at")
compiled_at_display = str(compiled_at)[:19] if compiled_at else "unknown"
table.add_row("Sources", str(list_count(state.get("sources"))))
table.add_row("Claims", str(list_count(state.get("claims"))))
table.add_row("Evidence", str(list_count(state.get("evidence"))))
table.add_row("Last Compiled", compiled_at_display)
table.add_row("Latest Receipt", receipt_path.name.replace("receipt_", "").replace(".json", "") if receipt_path else "none")
console.print(table)
@app.command("diagnostics")
def diagnostics_command(
json_output: bool = typer.Option(False, "--json", help="Print machine-readable JSON"),
api_base: str = typer.Option(
"http://127.0.0.1:8000",
"--api-base",
help="API base URL to embed in agent connect links",
),
):
"""Inspect backend-style readiness for the current project without starting a server."""
from morpheus.api.server import diagnostics_payload
payload = diagnostics_payload(request_context(api_base), Path.cwd())
if json_output:
console.out(json.dumps(payload, indent=2))
return
table = Table(title="Morpheus Diagnostics")
table.add_column("Check", style="cyan")
table.add_column("Status", style="green")
table.add_column("Detail", style="yellow")
for check in payload["checks"]:
table.add_row(
check["label"],
"OK" if check["ok"] else "Needs action",
check["detail"],
)
console.print(table)
next_action = payload["next_action"]
console.print(f"Next action: {next_action['label']}")
if next_action.get("command"):
console.print(f"Command: {next_action['command']}")
else:
console.print(f"Detail: {next_action['detail']}")
console.print(f"Agent connect: {payload['agent_connect_url']}")
@app.command("agent-connect")
def agent_connect_command(
json_output: bool = typer.Option(False, "--json", help="Print machine-readable JSON"),
api_base: str = typer.Option(
"http://127.0.0.1:8000",
"--api-base",
help="API base URL to embed in HTTP agent links",
),
):
"""Print the full self-connect manifest for agents without starting a server."""
from morpheus.api.server import agent_connect_payload
payload = agent_connect_payload(request_context(api_base), Path.cwd())
if json_output:
console.out(json.dumps(payload, indent=2))
return
state = payload["state"]
next_action = payload["next_action"]
next_action_command = next_action.get("command") or next_action["detail"]
console.print(Panel.fit(
f"Project: [bold]{payload['project_root']}[/bold]\n"
f"Initialized: [bold]{state['initialized']}[/bold]\n"
f"Compiled: [bold]{state['compiled']}[/bold]\n"
f"Next action: [bold]{next_action['label']}[/bold]\n"
f"Command: [bold]{next_action_command}[/bold]\n"
"Machine JSON: [bold]morpheus agent-connect --json[/bold]\n"
f"Prompt: {payload['agent_prompt']}",
title="Morpheus Agent Connect",
border_style="green",
))
@app.command("handoff")
def handoff_command(
json_output: bool = typer.Option(False, "--json", help="Print machine-readable JSON"),
api_base: str = typer.Option(
"http://127.0.0.1:8000",
"--api-base",
help="API base URL to embed in HTTP agent links",
),
):
"""Print a complete copyable bundle for handing the project to another agent."""
from morpheus.api.server import HTTPException, agent_handoff_payload
try:
payload = agent_handoff_payload(request_context(api_base), Path.cwd())
except HTTPException as exc:
console.print(f"[red]Handoff failed:[/red] {exc.detail}")
raise typer.Exit(1) from exc
if json_output:
console.out(json.dumps(payload, indent=2))
return
console.out(payload["markdown"])
@app.command("prepare-agent")
def prepare_agent_command(
json_output: bool = typer.Option(False, "--json", help="Print machine-readable JSON"),
api_base: str = typer.Option(
"http://127.0.0.1:8000",
"--api-base",
help="API base URL to embed in HTTP agent links",
),
):
"""Initialize, compile, bootstrap AGENTS.md, verify, and print handoff."""
from morpheus.api.server import HTTPException, agent_prepare_payload
try:
payload = agent_prepare_payload(request_context(api_base), Path.cwd())
except HTTPException as exc:
console.print(f"[red]Prepare failed:[/red] {exc.detail}")
raise typer.Exit(1) from exc
if json_output:
console.out(json.dumps(payload, indent=2))
return
console.out(payload["handoff"]["markdown"])
@app.command("bootstrap-agent")
def bootstrap_agent(
api_base: str = typer.Option(
"http://127.0.0.1:8000",
"--api-base",
help="API base URL to embed in AGENTS.md",
),
dry_run: bool = typer.Option(
False,
"--dry-run",
help="Print the AGENTS.md preview without writing it",
),
):
"""Create, refresh, or preview the Morpheus-managed AGENTS.md section."""
from morpheus.api.server import preview_agent_bootstrap, write_agent_bootstrap
from morpheus.api.server import HTTPException
try:
handler = preview_agent_bootstrap if dry_run else write_agent_bootstrap
response = handler(request_context(api_base), Path.cwd())
except HTTPException as exc:
console.print(f"[red]Bootstrap failed:[/red] {exc.detail}")
raise typer.Exit(1) from exc
if dry_run:
console.out(response.content)
return
if response.created:
action = "Created AGENTS.md"
elif response.updated:
action = "Updated AGENTS.md"
else:
action = "AGENTS.md already current"
console.print(Panel.fit(
f"[green]{action}[/green]\n"
f"Path: [bold]{response.path}[/bold]\n"
f"Agent connect: {response.agent_connect_url}",
title="Morpheus Agent Bootstrap",
border_style="green",
))
@app.command()
def wake(
project: Path | None = typer.Argument(
None,
help="Optional project path to initialize, compile, verify, and write root WAKE.md",
),
private: bool = typer.Option(
False,
"--private",
help="Keep the compiled WAKE.md inside .morpheus/ instead of writing root WAKE.md",
),
semantic: bool = typer.Option(False, "--semantic", help="Extract semantic review candidates"),
review: bool = typer.Option(False, "--review", help="Write semantic candidates for review"),
):
"""Print WAKE.md, or run the one-command project wake flow."""
if project is not None:
original_cwd = Path.cwd()
try:
project_root = project.expanduser()
if not project_root.is_absolute():
project_root = original_cwd / project_root
morpheus_dir, initialized = ensure_project_initialized(project_root)
os.chdir(project_root)
if initialized:
console.print("[green]✓ Initialized .morpheus/[/green]")
compile(verbose=False, semantic=semantic, review=review)
verify(all=True)
if private:
wake_path = morpheus_dir / "WAKE.md"
console.print(f"[green]✓ Private WAKE.md:[/green] {wake_path}")
else:
wake_path = copy_public_wake(project_root, morpheus_dir)
console.print(f"[green]✓ Public WAKE.md:[/green] {wake_path}")
console.print(Panel.fit(
f"Agent handoff prompt:\n{wake_handoff_prompt()}",
title="Morpheus Wake",
border_style="green",
))
except (OSError, ValueError) as exc:
console.print(f"[red]Wake failed:[/red] {exc}")
raise typer.Exit(1) from exc
finally:
os.chdir(original_cwd)
return
morpheus_dir = ensure_initialized()
wake_path = morpheus_dir / "WAKE.md"
if not wake_path.exists():
console.print("[red]No WAKE.md found. Run 'morpheus compile'[/red]")
raise typer.Exit(1)
try:
reject_symlink_paths([wake_path], "WAKE.md")
content = wake_path.read_text()
except (OSError, ValueError) as exc:
console.print(f"[red]WAKE.md unreadable:[/red] {exc}")
raise typer.Exit(1) from exc
console.out(content, end="")
@review_app.command("list")
def review_list(
kind: str | None = typer.Option(None, "--kind", help="Filter by candidate kind"),
label: str | None = typer.Option(None, "--label", help="Filter by candidate label"),
json_output: bool = typer.Option(False, "--json", help="Print machine-readable JSON"),
):
"""List semantic candidates waiting in the review store."""
candidates = ReviewStore(Path.cwd()).load_candidates()
if kind:
candidates = [candidate for candidate in candidates if candidate.kind == kind]
if label:
candidates = [candidate for candidate in candidates if candidate.label == label]
if json_output:
console.out(json.dumps([candidate.model_dump(mode="json") for candidate in candidates], indent=2))
return
table = Table(title="Semantic Review Candidates")
table.add_column("ID", style="cyan")
table.add_column("Status", style="green")
table.add_column("Label", style="yellow")
table.add_column("Source")
table.add_column("Claim")
for candidate in candidates:
table.add_row(
candidate.id,
candidate.status,
candidate.label,