devtrace-cli 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- devtrace/__init__.py +4 -0
- devtrace/__main__.py +8 -0
- devtrace/cli.py +631 -0
- devtrace/collectors/__init__.py +0 -0
- devtrace/collectors/node_deps.py +110 -0
- devtrace/collectors/node_runtime.py +37 -0
- devtrace/collectors/python_deps.py +136 -0
- devtrace/collectors/python_runtime.py +18 -0
- devtrace/collectors/system.py +46 -0
- devtrace/core/__init__.py +0 -0
- devtrace/core/agent.py +297 -0
- devtrace/core/ai_explainer.py +96 -0
- devtrace/core/collector.py +62 -0
- devtrace/core/comparator.py +199 -0
- devtrace/core/diagnostics.py +216 -0
- devtrace/core/healer.py +218 -0
- devtrace/core/models.py +86 -0
- devtrace/core/reporter.py +85 -0
- devtrace/core/serializer.py +13 -0
- devtrace/mcp_server.py +175 -0
- devtrace/project/__init__.py +0 -0
- devtrace/project/detector.py +44 -0
- devtrace/storage/__init__.py +0 -0
- devtrace/storage/history_store.py +108 -0
- devtrace/storage/json_store.py +51 -0
- devtrace_cli-1.0.0.dist-info/METADATA +341 -0
- devtrace_cli-1.0.0.dist-info/RECORD +30 -0
- devtrace_cli-1.0.0.dist-info/WHEEL +4 -0
- devtrace_cli-1.0.0.dist-info/entry_points.txt +2 -0
- devtrace_cli-1.0.0.dist-info/licenses/LICENSE +21 -0
devtrace/__init__.py
ADDED
devtrace/__main__.py
ADDED
devtrace/cli.py
ADDED
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
"""Command-line interface entry point for DevTrace."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from devtrace import __version__
|
|
11
|
+
from devtrace.core.agent import run_devtrace_agent
|
|
12
|
+
from devtrace.core.ai_explainer import build_grounded_prompt, request_ai_explanation
|
|
13
|
+
from devtrace.core.collector import capture_environment
|
|
14
|
+
from devtrace.core.comparator import (
|
|
15
|
+
EcosystemDiff,
|
|
16
|
+
SnapshotComparison,
|
|
17
|
+
compare_snapshots,
|
|
18
|
+
load_snapshot_file,
|
|
19
|
+
)
|
|
20
|
+
from devtrace.core.diagnostics import DiagnosticReport, load_error_file, run_diagnostics
|
|
21
|
+
from devtrace.core.healer import build_heal_plan, execute_sandbox_heal
|
|
22
|
+
from devtrace.core.reporter import render_markdown_report
|
|
23
|
+
from devtrace.mcp_server import serve_stdio
|
|
24
|
+
from devtrace.storage.history_store import (
|
|
25
|
+
find_baseline_snapshot,
|
|
26
|
+
list_history,
|
|
27
|
+
save_to_history,
|
|
28
|
+
)
|
|
29
|
+
from devtrace.storage.json_store import resolve_output_path, write_snapshot_atomic
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
33
|
+
"""Build and configure the argument parser for DevTrace CLI."""
|
|
34
|
+
parser = argparse.ArgumentParser(
|
|
35
|
+
prog="devtrace",
|
|
36
|
+
description="Local-first, cross-language developer diagnostics CLI.",
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"--version",
|
|
40
|
+
action="version",
|
|
41
|
+
version=f"%(prog)s {__version__}",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
subparsers = parser.add_subparsers(dest="command", help="Available commands")
|
|
45
|
+
|
|
46
|
+
# 1. capture
|
|
47
|
+
capture_parser = subparsers.add_parser(
|
|
48
|
+
"capture",
|
|
49
|
+
help="Capture a structured snapshot of the current development environment.",
|
|
50
|
+
)
|
|
51
|
+
capture_parser.add_argument(
|
|
52
|
+
"--project",
|
|
53
|
+
type=str,
|
|
54
|
+
default=".",
|
|
55
|
+
help="Path to the project directory (default: current directory).",
|
|
56
|
+
)
|
|
57
|
+
capture_parser.add_argument(
|
|
58
|
+
"--output",
|
|
59
|
+
type=str,
|
|
60
|
+
default=None,
|
|
61
|
+
help="Output JSON file path (default: <project>/.devtrace/snapshot.json).",
|
|
62
|
+
)
|
|
63
|
+
capture_parser.add_argument(
|
|
64
|
+
"--force",
|
|
65
|
+
action="store_true",
|
|
66
|
+
help="Overwrite the output snapshot file if it already exists.",
|
|
67
|
+
)
|
|
68
|
+
capture_parser.add_argument(
|
|
69
|
+
"--tag",
|
|
70
|
+
type=str,
|
|
71
|
+
default=None,
|
|
72
|
+
help="Optional label for this snapshot (e.g., 'working', 'pre-upgrade').",
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
# 2. compare
|
|
76
|
+
compare_parser = subparsers.add_parser(
|
|
77
|
+
"compare",
|
|
78
|
+
help="Compare two DevTrace environment snapshots and report differences.",
|
|
79
|
+
)
|
|
80
|
+
compare_parser.add_argument("left", type=str, help="Path to the first snapshot.")
|
|
81
|
+
compare_parser.add_argument("right", type=str, help="Path to the second snapshot.")
|
|
82
|
+
compare_parser.add_argument(
|
|
83
|
+
"--json",
|
|
84
|
+
action="store_true",
|
|
85
|
+
dest="as_json",
|
|
86
|
+
help="Output comparison result as machine-readable JSON.",
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# 3. diagnose
|
|
90
|
+
diagnose_parser = subparsers.add_parser(
|
|
91
|
+
"diagnose",
|
|
92
|
+
help="Run deterministic diagnostic rules on a snapshot and optional error log.",
|
|
93
|
+
)
|
|
94
|
+
diagnose_parser.add_argument(
|
|
95
|
+
"--snapshot",
|
|
96
|
+
type=str,
|
|
97
|
+
default=".devtrace/snapshot.json",
|
|
98
|
+
help="Path to snapshot JSON (default: .devtrace/snapshot.json).",
|
|
99
|
+
)
|
|
100
|
+
diagnose_parser.add_argument(
|
|
101
|
+
"--error-file",
|
|
102
|
+
type=str,
|
|
103
|
+
default=None,
|
|
104
|
+
help="Optional path to a crash log or stack-trace text file.",
|
|
105
|
+
)
|
|
106
|
+
diagnose_parser.add_argument(
|
|
107
|
+
"--json",
|
|
108
|
+
action="store_true",
|
|
109
|
+
dest="as_json",
|
|
110
|
+
help="Output diagnostic report as machine-readable JSON.",
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
# 4. report
|
|
114
|
+
report_parser = subparsers.add_parser(
|
|
115
|
+
"report",
|
|
116
|
+
help="Generate a shareable Markdown bug/environment report.",
|
|
117
|
+
)
|
|
118
|
+
report_parser.add_argument(
|
|
119
|
+
"--snapshot",
|
|
120
|
+
type=str,
|
|
121
|
+
default=".devtrace/snapshot.json",
|
|
122
|
+
help="Path to primary snapshot JSON (default: .devtrace/snapshot.json).",
|
|
123
|
+
)
|
|
124
|
+
report_parser.add_argument(
|
|
125
|
+
"--compare-with",
|
|
126
|
+
type=str,
|
|
127
|
+
default=None,
|
|
128
|
+
help="Optional second snapshot JSON to include comparison diff.",
|
|
129
|
+
)
|
|
130
|
+
report_parser.add_argument(
|
|
131
|
+
"--error-file",
|
|
132
|
+
type=str,
|
|
133
|
+
default=None,
|
|
134
|
+
help="Optional crash log file to include diagnostic findings.",
|
|
135
|
+
)
|
|
136
|
+
report_parser.add_argument(
|
|
137
|
+
"--output",
|
|
138
|
+
type=str,
|
|
139
|
+
default=None,
|
|
140
|
+
help="Optional file path to save the Markdown report (prints to stdout if omitted).",
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
# 5. explain (AI layer grounded on deterministic facts)
|
|
144
|
+
explain_parser = subparsers.add_parser(
|
|
145
|
+
"explain",
|
|
146
|
+
help="Explain environment diagnostics using a local or BYOK LLM.",
|
|
147
|
+
)
|
|
148
|
+
explain_parser.add_argument(
|
|
149
|
+
"--snapshot",
|
|
150
|
+
type=str,
|
|
151
|
+
default=".devtrace/snapshot.json",
|
|
152
|
+
help="Path to snapshot JSON (default: .devtrace/snapshot.json).",
|
|
153
|
+
)
|
|
154
|
+
explain_parser.add_argument(
|
|
155
|
+
"--error-file",
|
|
156
|
+
type=str,
|
|
157
|
+
default=None,
|
|
158
|
+
help="Optional crash log file path.",
|
|
159
|
+
)
|
|
160
|
+
explain_parser.add_argument(
|
|
161
|
+
"--model",
|
|
162
|
+
type=str,
|
|
163
|
+
default=None,
|
|
164
|
+
help="LLM model name (default: DEVTRACE_AI_MODEL or llama3.2).",
|
|
165
|
+
)
|
|
166
|
+
explain_parser.add_argument(
|
|
167
|
+
"--dry-run",
|
|
168
|
+
action="store_true",
|
|
169
|
+
help="Print the evidence-grounded prompt without calling any LLM API.",
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
# 6. mcp (Agentic server mode for Cursor / Claude Code / Windsurf)
|
|
173
|
+
subparsers.add_parser(
|
|
174
|
+
"mcp",
|
|
175
|
+
help="Start DevTrace as a stdio Model Context Protocol (MCP) server.",
|
|
176
|
+
)
|
|
177
|
+
# 7. history (Time-travel snapshot timeline)
|
|
178
|
+
history_parser = subparsers.add_parser(
|
|
179
|
+
"history",
|
|
180
|
+
help="List recorded environment snapshots in chronological order.",
|
|
181
|
+
)
|
|
182
|
+
history_parser.add_argument(
|
|
183
|
+
"--project",
|
|
184
|
+
type=str,
|
|
185
|
+
default=".",
|
|
186
|
+
help="Path to the project directory (default: current directory).",
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
# 8. diff (Time-travel diff against latest or tagged baseline)
|
|
190
|
+
diff_parser = subparsers.add_parser(
|
|
191
|
+
"diff",
|
|
192
|
+
help="Compare current live environment against a saved baseline snapshot.",
|
|
193
|
+
)
|
|
194
|
+
diff_parser.add_argument(
|
|
195
|
+
"--project",
|
|
196
|
+
type=str,
|
|
197
|
+
default=".",
|
|
198
|
+
help="Path to the project directory (default: current directory).",
|
|
199
|
+
)
|
|
200
|
+
diff_parser.add_argument(
|
|
201
|
+
"--against-tag",
|
|
202
|
+
type=str,
|
|
203
|
+
default=None,
|
|
204
|
+
help="Compare against the latest snapshot with this tag (e.g., 'working').",
|
|
205
|
+
)
|
|
206
|
+
diff_parser.add_argument(
|
|
207
|
+
"--json",
|
|
208
|
+
action="store_true",
|
|
209
|
+
dest="as_json",
|
|
210
|
+
help="Output diff result as machine-readable JSON.",
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
# 9. ask (Multi-step agentic environment investigator)
|
|
214
|
+
ask_parser = subparsers.add_parser(
|
|
215
|
+
"ask",
|
|
216
|
+
help="Ask DevTrace Agent a question about your environment or bug.",
|
|
217
|
+
)
|
|
218
|
+
ask_parser.add_argument("question", type=str, help="Natural language question to investigate.")
|
|
219
|
+
ask_parser.add_argument(
|
|
220
|
+
"--project",
|
|
221
|
+
type=str,
|
|
222
|
+
default=".",
|
|
223
|
+
help="Path to the project directory (default: current directory).",
|
|
224
|
+
)
|
|
225
|
+
ask_parser.add_argument(
|
|
226
|
+
"--error-file",
|
|
227
|
+
type=str,
|
|
228
|
+
default=None,
|
|
229
|
+
help="Optional crash log file for the agent to analyze.",
|
|
230
|
+
)
|
|
231
|
+
ask_parser.add_argument(
|
|
232
|
+
"--model",
|
|
233
|
+
type=str,
|
|
234
|
+
default=None,
|
|
235
|
+
help="LLM model name (default: DEVTRACE_AI_MODEL or llama3.2).",
|
|
236
|
+
)
|
|
237
|
+
ask_parser.add_argument(
|
|
238
|
+
"--offline",
|
|
239
|
+
action="store_true",
|
|
240
|
+
help="Run deterministic offline tool investigation without calling an LLM.",
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
# 10. heal (Isolated sandbox verification & self-healing)
|
|
244
|
+
heal_parser = subparsers.add_parser(
|
|
245
|
+
"heal",
|
|
246
|
+
help="Verify missing dependency fixes inside an isolated temporary sandbox.",
|
|
247
|
+
)
|
|
248
|
+
heal_parser.add_argument(
|
|
249
|
+
"--project",
|
|
250
|
+
type=str,
|
|
251
|
+
default=".",
|
|
252
|
+
help="Path to the project directory (default: current directory).",
|
|
253
|
+
)
|
|
254
|
+
heal_parser.add_argument(
|
|
255
|
+
"--error-file",
|
|
256
|
+
type=str,
|
|
257
|
+
default=None,
|
|
258
|
+
help="Optional crash log file to infer missing packages from.",
|
|
259
|
+
)
|
|
260
|
+
heal_parser.add_argument(
|
|
261
|
+
"--verify-cmd",
|
|
262
|
+
type=str,
|
|
263
|
+
default=None,
|
|
264
|
+
help="Optional command to run inside the sandbox to verify the fix (e.g. 'pytest').",
|
|
265
|
+
)
|
|
266
|
+
heal_parser.add_argument(
|
|
267
|
+
"--dry-run",
|
|
268
|
+
action="store_true",
|
|
269
|
+
help="Preview the repair plan without creating a sandbox.",
|
|
270
|
+
)
|
|
271
|
+
heal_parser.add_argument(
|
|
272
|
+
"--apply",
|
|
273
|
+
action="store_true",
|
|
274
|
+
help="Apply the fix to the active host environment after sandbox verification passes.",
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
return parser
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _run_capture(
|
|
281
|
+
project_arg: str,
|
|
282
|
+
output_arg: str | None,
|
|
283
|
+
force: bool,
|
|
284
|
+
tag: str | None = None,
|
|
285
|
+
) -> int:
|
|
286
|
+
"""Execute the `capture` subcommand and print a concise summary."""
|
|
287
|
+
project_dir = Path(project_arg)
|
|
288
|
+
try:
|
|
289
|
+
snapshot = capture_environment(project_dir, tag=tag)
|
|
290
|
+
output_path = resolve_output_path(project_dir, output_arg)
|
|
291
|
+
saved_path = write_snapshot_atomic(snapshot, output_path, force=force)
|
|
292
|
+
history_path = save_to_history(project_dir, snapshot)
|
|
293
|
+
except (FileNotFoundError, NotADirectoryError, FileExistsError, OSError) as exc:
|
|
294
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
295
|
+
return 1
|
|
296
|
+
|
|
297
|
+
langs = ", ".join(snapshot.project.detected_languages) or "none"
|
|
298
|
+
node_ver = snapshot.runtimes.node.version or "not installed"
|
|
299
|
+
|
|
300
|
+
py_deps = snapshot.dependencies.python
|
|
301
|
+
node_deps = snapshot.dependencies.node
|
|
302
|
+
drift_count = (
|
|
303
|
+
len(py_deps.missing_from_env)
|
|
304
|
+
+ len(py_deps.undeclared_in_manifest)
|
|
305
|
+
+ len(node_deps.missing_from_env)
|
|
306
|
+
+ len(node_deps.undeclared_in_manifest)
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
print("DevTrace snapshot captured successfully.")
|
|
310
|
+
print(f" Snapshot ID : {snapshot.snapshot_id}")
|
|
311
|
+
if snapshot.tag:
|
|
312
|
+
print(f" Tag : {snapshot.tag}")
|
|
313
|
+
print(f" System : {snapshot.system.os_release} ({snapshot.system.architecture})")
|
|
314
|
+
print(f" Python : {snapshot.runtimes.python.version}")
|
|
315
|
+
print(f" Node.js : {node_ver}")
|
|
316
|
+
print(f" Languages : {langs}")
|
|
317
|
+
if "python" in snapshot.project.detected_languages:
|
|
318
|
+
print(
|
|
319
|
+
f" Python Deps : {len(py_deps.declared)} declared, {len(py_deps.installed)} installed"
|
|
320
|
+
)
|
|
321
|
+
if "node" in snapshot.project.detected_languages:
|
|
322
|
+
print(
|
|
323
|
+
f" Node Deps : {len(node_deps.declared)} declared, "
|
|
324
|
+
f"{len(node_deps.installed)} installed"
|
|
325
|
+
)
|
|
326
|
+
if drift_count > 0:
|
|
327
|
+
print(f" Drift Alert : {drift_count} dependency mismatch(es) detected")
|
|
328
|
+
print(f" Saved to : {saved_path}")
|
|
329
|
+
print(f" History : {history_path}")
|
|
330
|
+
return 0
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def _print_ecosystem_diff(label: str, diff: EcosystemDiff) -> None:
|
|
334
|
+
"""Print human-readable dependency differences for one language."""
|
|
335
|
+
if not diff.has_differences():
|
|
336
|
+
return
|
|
337
|
+
print(f"\n[{label} Dependencies]")
|
|
338
|
+
for pkg, ver in diff.added_installed.items():
|
|
339
|
+
print(f" + {pkg}: {ver} (added in right)")
|
|
340
|
+
for pkg, ver in diff.removed_installed.items():
|
|
341
|
+
print(f" - {pkg}: {ver} (removed in right)")
|
|
342
|
+
for pkg, vers in diff.changed_installed.items():
|
|
343
|
+
print(f" ~ {pkg}: {vers['left']} -> {vers['right']}")
|
|
344
|
+
for pkg, decl in diff.declared_changes.items():
|
|
345
|
+
print(f" * manifest {pkg}: {decl['left']} -> {decl['right']}")
|
|
346
|
+
if diff.missing_from_env_left != diff.missing_from_env_right:
|
|
347
|
+
print(
|
|
348
|
+
f" ! missing_from_env: {diff.missing_from_env_left} -> {diff.missing_from_env_right}"
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _print_comparison(comp: SnapshotComparison) -> None:
|
|
353
|
+
"""Print a structured, evidence-focused comparison report."""
|
|
354
|
+
print("DevTrace Snapshot Comparison")
|
|
355
|
+
print(f" Left : {comp.left_snapshot_id}")
|
|
356
|
+
print(f" Right : {comp.right_snapshot_id}")
|
|
357
|
+
|
|
358
|
+
if not comp.has_differences():
|
|
359
|
+
print("\nNo meaningful environment differences detected.")
|
|
360
|
+
return
|
|
361
|
+
|
|
362
|
+
for title, changes in (
|
|
363
|
+
("System", comp.system_changes),
|
|
364
|
+
("Runtimes", comp.runtime_changes),
|
|
365
|
+
("Project", comp.project_changes),
|
|
366
|
+
):
|
|
367
|
+
if changes:
|
|
368
|
+
print(f"\n[{title}]")
|
|
369
|
+
for ch in changes:
|
|
370
|
+
print(f" ~ {ch.field}: {ch.left!r} -> {ch.right!r}")
|
|
371
|
+
|
|
372
|
+
_print_ecosystem_diff("Python", comp.python_dependencies)
|
|
373
|
+
_print_ecosystem_diff("Node.js", comp.node_dependencies)
|
|
374
|
+
print(
|
|
375
|
+
"\nNote: Observed differences are environment evidence, "
|
|
376
|
+
"not automatic proof of bug causation."
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _run_compare(left_arg: str, right_arg: str, as_json: bool) -> int:
|
|
381
|
+
"""Execute the `compare` subcommand."""
|
|
382
|
+
try:
|
|
383
|
+
left_data = load_snapshot_file(Path(left_arg))
|
|
384
|
+
right_data = load_snapshot_file(Path(right_arg))
|
|
385
|
+
comparison = compare_snapshots(left_data, right_data)
|
|
386
|
+
except (FileNotFoundError, ValueError) as exc:
|
|
387
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
388
|
+
return 1
|
|
389
|
+
|
|
390
|
+
if as_json:
|
|
391
|
+
print(json.dumps(comparison.to_dict(), indent=2))
|
|
392
|
+
else:
|
|
393
|
+
_print_comparison(comparison)
|
|
394
|
+
return 0
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def _print_diagnostics(report: DiagnosticReport) -> None:
|
|
398
|
+
"""Print human-readable diagnostic findings."""
|
|
399
|
+
print("DevTrace Diagnostic Analysis")
|
|
400
|
+
print(f" Snapshot ID : {report.snapshot_id}")
|
|
401
|
+
print(f" Error Log : {'analyzed' if report.error_file_analyzed else 'not provided'}")
|
|
402
|
+
|
|
403
|
+
if not report.findings:
|
|
404
|
+
print("\nNo diagnostic findings triggered.")
|
|
405
|
+
return
|
|
406
|
+
|
|
407
|
+
for finding in report.findings:
|
|
408
|
+
print(f"\n[{finding.severity.upper()}] {finding.rule_id} — {finding.title}")
|
|
409
|
+
print(f" Explanation : {finding.explanation}")
|
|
410
|
+
for ev in finding.evidence:
|
|
411
|
+
print(f" Evidence : {ev}")
|
|
412
|
+
print(f" Next Check : {finding.next_check}")
|
|
413
|
+
print(f" Uncertainty : {finding.uncertainty}")
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def _run_diagnose(snapshot_arg: str, error_arg: str | None, as_json: bool) -> int:
|
|
417
|
+
"""Execute the `diagnose` subcommand."""
|
|
418
|
+
try:
|
|
419
|
+
snap_data = load_snapshot_file(Path(snapshot_arg))
|
|
420
|
+
err_text = load_error_file(Path(error_arg)) if error_arg else None
|
|
421
|
+
report = run_diagnostics(snap_data, error_text=err_text)
|
|
422
|
+
except (FileNotFoundError, ValueError) as exc:
|
|
423
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
424
|
+
return 1
|
|
425
|
+
|
|
426
|
+
if as_json:
|
|
427
|
+
print(json.dumps(report.to_dict(), indent=2))
|
|
428
|
+
else:
|
|
429
|
+
_print_diagnostics(report)
|
|
430
|
+
return 0
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
def _run_report(
|
|
434
|
+
snapshot_arg: str,
|
|
435
|
+
compare_arg: str | None,
|
|
436
|
+
error_arg: str | None,
|
|
437
|
+
output_arg: str | None,
|
|
438
|
+
) -> int:
|
|
439
|
+
"""Execute the `report` subcommand."""
|
|
440
|
+
try:
|
|
441
|
+
snap_data = load_snapshot_file(Path(snapshot_arg))
|
|
442
|
+
err_text = load_error_file(Path(error_arg)) if error_arg else None
|
|
443
|
+
diag = run_diagnostics(snap_data, error_text=err_text)
|
|
444
|
+
comp = None
|
|
445
|
+
if compare_arg:
|
|
446
|
+
other_data = load_snapshot_file(Path(compare_arg))
|
|
447
|
+
comp = compare_snapshots(snap_data, other_data)
|
|
448
|
+
markdown = render_markdown_report(snap_data, diag, comparison=comp)
|
|
449
|
+
except (FileNotFoundError, ValueError) as exc:
|
|
450
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
451
|
+
return 1
|
|
452
|
+
|
|
453
|
+
if output_arg:
|
|
454
|
+
out_path = Path(output_arg).expanduser().resolve()
|
|
455
|
+
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
456
|
+
out_path.write_text(markdown, encoding="utf-8")
|
|
457
|
+
print(f"Markdown diagnostic report saved to: {out_path}")
|
|
458
|
+
else:
|
|
459
|
+
print(markdown, end="")
|
|
460
|
+
return 0
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def _run_history(project_arg: str) -> int:
|
|
464
|
+
"""List stored historical snapshots for a project."""
|
|
465
|
+
entries = list_history(Path(project_arg))
|
|
466
|
+
if not entries:
|
|
467
|
+
print("No snapshot history recorded yet. Run `devtrace capture` first.")
|
|
468
|
+
return 0
|
|
469
|
+
|
|
470
|
+
print("DevTrace Environment History (oldest -> newest)")
|
|
471
|
+
for idx, entry in enumerate(entries, start=1):
|
|
472
|
+
tag_label = f" [tag: {entry.tag}]" if entry.tag else ""
|
|
473
|
+
short_id = entry.snapshot_id.split("-")[0]
|
|
474
|
+
print(
|
|
475
|
+
f" {idx}. {entry.captured_at} | {short_id}{tag_label} | "
|
|
476
|
+
f"Py {entry.python_version or 'n/a'} | "
|
|
477
|
+
f"Node {entry.node_version or 'n/a'} | drift={entry.drift_count}"
|
|
478
|
+
)
|
|
479
|
+
return 0
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def _run_diff(project_arg: str, against_tag: str | None, as_json: bool) -> int:
|
|
483
|
+
"""Compare current live environment against a stored historical baseline."""
|
|
484
|
+
project_dir = Path(project_arg)
|
|
485
|
+
try:
|
|
486
|
+
baseline_entry = find_baseline_snapshot(project_dir, tag=against_tag)
|
|
487
|
+
baseline_data = load_snapshot_file(baseline_entry.file_path)
|
|
488
|
+
current_snap = capture_environment(project_dir).to_dict()
|
|
489
|
+
comparison = compare_snapshots(baseline_data, current_snap)
|
|
490
|
+
except (FileNotFoundError, NotADirectoryError, ValueError, OSError) as exc:
|
|
491
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
492
|
+
return 1
|
|
493
|
+
|
|
494
|
+
if as_json:
|
|
495
|
+
print(json.dumps(comparison.to_dict(), indent=2))
|
|
496
|
+
else:
|
|
497
|
+
label = f"tag '{against_tag}'" if against_tag else "latest snapshot"
|
|
498
|
+
print(f"Time-Travel Diff (baseline: {label} @ {baseline_entry.captured_at})")
|
|
499
|
+
_print_comparison(comparison)
|
|
500
|
+
return 0
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
def main(argv: list[str] | None = None) -> int:
|
|
504
|
+
"""Main CLI execution entry point."""
|
|
505
|
+
parser = build_parser()
|
|
506
|
+
args = parser.parse_args(argv)
|
|
507
|
+
|
|
508
|
+
if args.command is None:
|
|
509
|
+
parser.print_help()
|
|
510
|
+
return 0
|
|
511
|
+
|
|
512
|
+
if args.command == "capture":
|
|
513
|
+
return _run_capture(
|
|
514
|
+
project_arg=args.project,
|
|
515
|
+
output_arg=args.output,
|
|
516
|
+
force=args.force,
|
|
517
|
+
tag=args.tag,
|
|
518
|
+
)
|
|
519
|
+
|
|
520
|
+
if args.command == "compare":
|
|
521
|
+
return _run_compare(
|
|
522
|
+
left_arg=args.left,
|
|
523
|
+
right_arg=args.right,
|
|
524
|
+
as_json=args.as_json,
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
if args.command == "diagnose":
|
|
528
|
+
return _run_diagnose(
|
|
529
|
+
snapshot_arg=args.snapshot,
|
|
530
|
+
error_arg=args.error_file,
|
|
531
|
+
as_json=args.as_json,
|
|
532
|
+
)
|
|
533
|
+
|
|
534
|
+
if args.command == "report":
|
|
535
|
+
return _run_report(
|
|
536
|
+
snapshot_arg=args.snapshot,
|
|
537
|
+
compare_arg=args.compare_with,
|
|
538
|
+
error_arg=args.error_file,
|
|
539
|
+
output_arg=args.output,
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
if args.command == "explain":
|
|
543
|
+
try:
|
|
544
|
+
snap_data = load_snapshot_file(Path(args.snapshot))
|
|
545
|
+
err_text = load_error_file(Path(args.error_file)) if args.error_file else None
|
|
546
|
+
diag = run_diagnostics(snap_data, error_text=err_text)
|
|
547
|
+
prompt = build_grounded_prompt(snap_data, diag, error_text=err_text)
|
|
548
|
+
if args.dry_run:
|
|
549
|
+
print(prompt)
|
|
550
|
+
return 0
|
|
551
|
+
answer = request_ai_explanation(prompt, model=args.model)
|
|
552
|
+
print(answer)
|
|
553
|
+
return 0
|
|
554
|
+
except (FileNotFoundError, ValueError, RuntimeError) as exc:
|
|
555
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
556
|
+
return 1
|
|
557
|
+
|
|
558
|
+
if args.command == "mcp":
|
|
559
|
+
return serve_stdio()
|
|
560
|
+
|
|
561
|
+
if args.command == "history":
|
|
562
|
+
return _run_history(project_arg=args.project)
|
|
563
|
+
|
|
564
|
+
if args.command == "diff":
|
|
565
|
+
return _run_diff(
|
|
566
|
+
project_arg=args.project,
|
|
567
|
+
against_tag=args.against_tag,
|
|
568
|
+
as_json=args.as_json,
|
|
569
|
+
)
|
|
570
|
+
|
|
571
|
+
if args.command == "ask":
|
|
572
|
+
try:
|
|
573
|
+
err_path = Path(args.error_file) if args.error_file else None
|
|
574
|
+
result = run_devtrace_agent(
|
|
575
|
+
question=args.question,
|
|
576
|
+
project_dir=Path(args.project),
|
|
577
|
+
error_file=err_path,
|
|
578
|
+
model=args.model,
|
|
579
|
+
offline_only=args.offline,
|
|
580
|
+
)
|
|
581
|
+
except (FileNotFoundError, NotADirectoryError, ValueError) as exc:
|
|
582
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
583
|
+
return 1
|
|
584
|
+
|
|
585
|
+
if result.tool_trace:
|
|
586
|
+
print("Agent Tool Execution Trace:")
|
|
587
|
+
for idx, step in enumerate(result.tool_trace, start=1):
|
|
588
|
+
print(f" {idx}. {step.tool_name} -> {step.summary}")
|
|
589
|
+
print()
|
|
590
|
+
print(result.answer)
|
|
591
|
+
return 0
|
|
592
|
+
|
|
593
|
+
if args.command == "heal":
|
|
594
|
+
try:
|
|
595
|
+
err_text = load_error_file(Path(args.error_file)) if args.error_file else None
|
|
596
|
+
plan = build_heal_plan(
|
|
597
|
+
project_dir=Path(args.project),
|
|
598
|
+
error_text=err_text,
|
|
599
|
+
verify_command=args.verify_cmd,
|
|
600
|
+
)
|
|
601
|
+
result = execute_sandbox_heal(
|
|
602
|
+
project_dir=Path(args.project),
|
|
603
|
+
plan=plan,
|
|
604
|
+
dry_run=args.dry_run,
|
|
605
|
+
apply_to_host=args.apply,
|
|
606
|
+
)
|
|
607
|
+
except (FileNotFoundError, NotADirectoryError, ValueError, OSError) as exc:
|
|
608
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
609
|
+
return 1
|
|
610
|
+
|
|
611
|
+
print("DevTrace Sandbox Heal Report")
|
|
612
|
+
print(f" Proposed Packages : {', '.join(plan.proposed_pip_args) or 'none'}")
|
|
613
|
+
print(f" Sandbox Tested : {result.sandbox_tested}")
|
|
614
|
+
if result.install_succeeded is not None:
|
|
615
|
+
print(f" Install Status : {'OK' if result.install_succeeded else 'FAILED'}")
|
|
616
|
+
if result.verify_succeeded is not None:
|
|
617
|
+
print(f" Verify Status : {'PASSED' if result.verify_succeeded else 'FAILED'}")
|
|
618
|
+
print(f" Applied to Host : {result.applied_to_host}")
|
|
619
|
+
for line in result.logs:
|
|
620
|
+
print(f" - {line}")
|
|
621
|
+
return (
|
|
622
|
+
0
|
|
623
|
+
if (result.install_succeeded is not False and result.verify_succeeded is not False)
|
|
624
|
+
else 1
|
|
625
|
+
)
|
|
626
|
+
|
|
627
|
+
return 1
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
if __name__ == "__main__":
|
|
631
|
+
sys.exit(main())
|
|
File without changes
|