forked from fmagent-project/FM-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
778 lines (730 loc) · 29.9 KB
/
Copy pathmain.py
File metadata and controls
778 lines (730 loc) · 29.9 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
from src.call_graph_edges import load_call_edges
from src.entry_reasoning_pipeline import run_entry_pipeline
from src.file_utils import (
collect_file_names,
_has_source_code,
_get_all_phase_files,
_write_file_names,
_json_file_is_valid,
_is_under_submodules,
_ensure_resume_mode_compatible,
)
from src.verification import _generate_all_bugs_validation_summary
from src.extract import run_extraction, EXT_TO_LANG
from src.generate_topdown_layers import generate_topdown_layers
from src.spec_generation_and_verification import run_spec_generation_and_verification
from src.incremental_reasoner import run_incremental_pipeline
from src.git import (
frozen_worktree,
_is_git_repo,
_get_head_commit,
_record_version,
)
from src.languages.codegraph import try_codegraph_init
from src.plugin import run_plugin_hook
from src.pipeline_setup import (
_run_setup_extract,
_run_generate_phases,
_post_process_phases,
_run_generate_domain_context,
)
from src.domain_knowledge import (
collect_domain_knowledge_paths,
stage_domain_knowledge_files,
)
import os
import sys
import argparse
import json
import time
import shutil
import logging
import contextlib
def _clean_previous_run(work_dir):
"""Remove the fm_agent working directory from the previous pipeline run."""
if os.path.isdir(work_dir):
shutil.rmtree(work_dir)
def _resolve_bug_validator_path(raw_path):
"""Resolve a custom bug-validator prompt path from the launch directory."""
if not raw_path:
return None
path = os.path.abspath(os.path.expanduser(raw_path))
if not os.path.isfile(path):
raise ValueError(
"--bug-validator must point to a file: "
f"{raw_path}"
)
try:
with open(path, "r"):
pass
except OSError as exc:
raise ValueError(
f"--bug-validator file is not readable: {exc}"
) from exc
return path
def _normalize_submodules(proj_dir, submodules):
"""Return validated project-relative submodule directories."""
if not submodules:
return []
proj_dir = os.path.abspath(proj_dir)
normalized = []
seen = set()
for raw in submodules:
value = (raw or "").strip()
if not value:
continue
candidate = value if os.path.isabs(value) else os.path.join(proj_dir, value)
candidate = os.path.abspath(candidate)
try:
inside_project = os.path.commonpath([proj_dir, candidate]) == proj_dir
except ValueError:
inside_project = False
if not inside_project or candidate == proj_dir:
raise ValueError(
f"--submodule must name subdirectories inside proj_dir, got: {raw}"
)
if not os.path.isdir(candidate):
raise ValueError(f"--submodule path is not a directory: {raw}")
rel = os.path.relpath(candidate, proj_dir).replace(os.sep, "/")
if rel not in seen:
normalized.append(rel)
seen.add(rel)
collapsed = []
for rel in sorted(normalized, key=lambda path: (path.count("/"), path)):
if not collapsed or not _is_under_submodules(rel, collapsed):
collapsed.append(rel)
return collapsed
def run_pipeline(
proj_dir,
resume=False,
required_source_files=None,
domain_knowledge_files=None,
submodules=None,
one_phase=False,
extra_call_edges_path=None,
only_spec=False,
bug_validator_path=None,
plugin_config=None,
plugin_context=None,
all_bugs=False,
):
if not os.path.isdir(proj_dir):
print(f"[Pipeline] ERROR: proj_dir does not exist or is not a directory: {proj_dir}")
sys.exit(1)
if not _has_source_code(proj_dir, submodules):
scope = f" selected submodule(s): {', '.join(submodules)}" if submodules else f" {proj_dir}"
print(f"[Pipeline] ERROR: No source code files found in{scope}. "
f"Supported extensions: {', '.join(sorted(EXT_TO_LANG.keys()))}")
sys.exit(1)
work_dir = os.path.join(proj_dir, "fm_agent")
input_dir = os.path.join(work_dir, "extracted_functions")
output_dir = os.path.join(work_dir, "logic_verification_results")
script_dir = os.path.dirname(os.path.abspath(__file__))
extra_call_edges = load_call_edges(extra_call_edges_path)
# Clean files from the previous run — unless resuming, where we keep all
# prior progress (phases.json, generated specs, verification results) and
# only do the remaining work.
if resume:
if os.path.isdir(work_dir):
print(f"[Pipeline] RESUME: keeping existing {os.path.relpath(work_dir, proj_dir)}/ — only remaining work will run.")
else:
print("[Pipeline] RESUME requested but no previous fm_agent/ found — starting fresh.")
resume = False
else:
_clean_previous_run(work_dir)
if resume and not only_spec:
_ensure_resume_mode_compatible(output_dir, all_bugs)
os.makedirs(work_dir, exist_ok=True)
if plugin_config is not None:
plugin_context_path = os.path.join(work_dir, "plugin_context.json")
with open(plugin_context_path, "w", encoding="utf-8") as file:
json.dump(plugin_context or {}, file, indent=2)
if plugin_config.configure_hook is not None:
run_plugin_hook(
plugin_config.name,
"configure",
plugin_config.configure_function,
plugin_config.configure_hook,
proj_dir,
)
domain_knowledge_relpaths = stage_domain_knowledge_files(
proj_dir, work_dir, domain_knowledge_files
)
if domain_knowledge_relpaths:
print(
"[Pipeline] User domain knowledge: "
f"{len(domain_knowledge_relpaths)} markdown file(s)."
)
# Stage 1: generate phase.json (input: target code → phases.json)
# Stage 2: generate domain context (input: phases.json → domain context files)
phase_stage = plugin_config.get_stage("generate_phase_plan") if plugin_config else None
context_stage = plugin_config.get_stage("generate_domain_context") if plugin_config else None
extraction_stage = plugin_config.get_stage("extract_functions") if plugin_config else None
file_list_stage = plugin_config.get_stage("collect_file_list") if plugin_config else None
topdown_stage = plugin_config.get_stage("generate_topdown_layers") if plugin_config else None
spec_stage = plugin_config.get_stage("generate_specs_and_verification") if plugin_config else None
print("[Pipeline] Stage 1/6: Generating phase plan...")
if phase_stage is not None and phase_stage.type == "pass":
print(
"[Pipeline] Stage 1/6: Plugin stage "
"'generate_phase_plan' type=pass, skipping."
)
elif phase_stage is not None and phase_stage.type == "replace":
run_plugin_hook(
plugin_config.name,
"generate_phase_plan",
phase_stage.replace_function,
phase_stage.replace_hook,
proj_dir,
)
else:
if phase_stage is not None and phase_stage.input_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_phase_plan",
phase_stage.input_function,
phase_stage.input_hook,
proj_dir,
)
_run_generate_phases(
proj_dir, work_dir, script_dir, resume=resume,
submodules=submodules,
)
if phase_stage is not None and phase_stage.output_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_phase_plan",
phase_stage.output_function,
phase_stage.output_hook,
proj_dir,
)
phases_modified = _post_process_phases(
proj_dir, work_dir,
required_source_files=required_source_files,
submodules=submodules,
one_phase=one_phase,
)
print("[Pipeline] Stage 2/6: Generating domain context...")
if context_stage is not None and context_stage.type == "pass":
print(
"[Pipeline] Stage 2/6: Plugin stage "
"'generate_domain_context' type=pass, skipping."
)
elif context_stage is not None and context_stage.type == "replace":
run_plugin_hook(
plugin_config.name,
"generate_domain_context",
context_stage.replace_function,
context_stage.replace_hook,
proj_dir,
)
else:
if context_stage is not None and context_stage.input_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_domain_context",
context_stage.input_function,
context_stage.input_hook,
proj_dir,
)
_run_generate_domain_context(
proj_dir,
work_dir,
script_dir,
resume=resume and not phases_modified,
)
if context_stage is not None and context_stage.output_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_domain_context",
context_stage.output_function,
context_stage.output_hook,
proj_dir,
)
# Build (or rebuild) the codegraph index if codegraph is installed. Both
# run_extraction (Stage 3) and generate_topdown_layers (Stage 5) read from it.
# force=not resume mirrors run_extraction below: a fresh run rebuilds so the
# index matches the current tree, while a resume reuses the existing index
# (same tree as the interrupted run — rebuilding would just be wasted work).
try_codegraph_init(proj_dir, force=not resume)
# Run function extraction using extract.py
# force=False on resume preserves already-specced extracted files; on a fresh
# run fm_agent/ was just wiped so it is equivalent to force=True.
print("[Pipeline] Stage 3/6: Extracting functions from source files...")
if extraction_stage is not None and extraction_stage.type == "pass":
print(
"[Pipeline] Stage 3/6: Plugin stage "
"'extract_functions' type=pass, skipping."
)
elif extraction_stage is not None and extraction_stage.type == "replace":
run_plugin_hook(
plugin_config.name,
"extract_functions",
extraction_stage.replace_function,
extraction_stage.replace_hook,
proj_dir,
)
else:
if extraction_stage is not None and extraction_stage.input_hook is not None:
run_plugin_hook(
plugin_config.name,
"extract_functions",
extraction_stage.input_function,
extraction_stage.input_hook,
proj_dir,
)
run_extraction(proj_dir, work_dir=work_dir, force=not resume, verbose=True)
if extraction_stage is not None and extraction_stage.output_hook is not None:
run_plugin_hook(
plugin_config.name,
"extract_functions",
extraction_stage.output_function,
extraction_stage.output_hook,
proj_dir,
)
# Copy system_prompt.md to spec_prompts/system_prompt.md
spec_prompts_dir = os.path.join(work_dir, "spec_prompts")
os.makedirs(spec_prompts_dir, exist_ok=True)
shutil.copy2(
os.path.join(script_dir, "md", "system_prompt.md"),
os.path.join(spec_prompts_dir, "system_prompt.md"),
)
shutil.copy2(
os.path.join(script_dir, "src", "generate_batch_prompts.py"),
os.path.join(spec_prompts_dir, "generate_batch_prompts.py"),
)
# generate_batch_prompts.py imports is_file_ready from this module at runtime.
shutil.copy2(
os.path.join(script_dir, "src", "file_utils.py"),
os.path.join(spec_prompts_dir, "file_utils.py"),
)
phases_path = os.path.join(work_dir, "phases.json")
with open(phases_path, "r") as f:
phases_data = json.load(f)
print("[Pipeline] Stage 4/6: Collecting file list...")
file_list_path = os.path.join(work_dir, "fm_agent_file_list.json")
if file_list_stage is not None and file_list_stage.type == "pass":
print(
"[Pipeline] Stage 4/6: Plugin stage "
"'collect_file_list' type=pass, skipping."
)
with open(file_list_path, "r", encoding="utf-8") as file:
file_list = json.load(file)
elif file_list_stage is not None and file_list_stage.type == "replace":
run_plugin_hook(
plugin_config.name,
"collect_file_list",
file_list_stage.replace_function,
file_list_stage.replace_hook,
proj_dir,
)
with open(file_list_path, "r", encoding="utf-8") as file:
file_list = json.load(file)
else:
if file_list_stage is not None and file_list_stage.input_hook is not None:
run_plugin_hook(
plugin_config.name,
"collect_file_list",
file_list_stage.input_function,
file_list_stage.input_hook,
proj_dir,
)
file_list = collect_file_names(input_dir, file_list_path)
if submodules:
file_list = _write_file_names(
_get_all_phase_files(phases_data, input_dir), file_list_path
)
if file_list_stage is not None and file_list_stage.output_hook is not None:
run_plugin_hook(
plugin_config.name,
"collect_file_list",
file_list_stage.output_function,
file_list_stage.output_hook,
proj_dir,
)
with open(file_list_path, "r", encoding="utf-8") as file:
file_list = json.load(file)
if not file_list:
print("[Pipeline] No functions found to verify. Skipping spec generation.")
return
# --- Stage 5: Generate topdown layers ---
print("[Pipeline] Stage 5/6: Generating topdown layers...")
if topdown_stage is not None and topdown_stage.type == "pass":
print(
"[Pipeline] Stage 5/6: Plugin stage "
"'generate_topdown_layers' type=pass, skipping."
)
elif topdown_stage is not None and topdown_stage.type == "replace":
run_plugin_hook(
plugin_config.name,
"generate_topdown_layers",
topdown_stage.replace_function,
topdown_stage.replace_hook,
proj_dir,
)
else:
if topdown_stage is not None and topdown_stage.input_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_topdown_layers",
topdown_stage.input_function,
topdown_stage.input_hook,
proj_dir,
)
generate_topdown_layers(work_dir, extra_call_edges=extra_call_edges)
if topdown_stage is not None and topdown_stage.output_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_topdown_layers",
topdown_stage.output_function,
topdown_stage.output_hook,
proj_dir,
)
# --- Stage 6: Execute spec generation workflow (per phase, per layer) ---
if only_spec:
print("[Pipeline] Stage 6/6: Generating specs (reasoning & bug validation disabled)...")
else:
print("[Pipeline] Stage 6/6: Generating specs & verification...")
if spec_stage is not None and spec_stage.type == "pass":
print(
"[Pipeline] Stage 6/6: Plugin stage "
"'generate_specs_and_verification' type=pass, skipping."
)
elif spec_stage is not None and spec_stage.type == "replace":
run_plugin_hook(
plugin_config.name,
"generate_specs_and_verification",
spec_stage.replace_function,
spec_stage.replace_hook,
proj_dir,
)
else:
if spec_stage is not None and spec_stage.input_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_specs_and_verification",
spec_stage.input_function,
spec_stage.input_hook,
proj_dir,
)
run_spec_generation_and_verification(
proj_dir,
work_dir,
input_dir,
output_dir,
script_dir,
spec_prompts_dir,
phases_data,
resume=resume,
extra_call_edges=extra_call_edges,
only_spec=only_spec,
bug_validator_path=bug_validator_path,
all_bugs=all_bugs,
)
if spec_stage is not None and spec_stage.output_hook is not None:
run_plugin_hook(
plugin_config.name,
"generate_specs_and_verification",
spec_stage.output_function,
spec_stage.output_hook,
proj_dir,
)
# Print confirmed bug count (skipped in only-spec mode, which runs no
# reasoning or bug validation).
if not only_spec:
if all_bugs:
# A resumed run may find every function and candidate validation
# already complete, so no watcher runs to refresh the persistent
# summary. Rebuild it from the current terminal records here.
_generate_all_bugs_validation_summary(work_dir)
summary_path = os.path.join(work_dir, "bug_validation", "summary.json")
if os.path.exists(summary_path):
with open(summary_path, "r") as f:
summary = json.load(f)
confirmed = summary.get("total_confirmed", 0)
print(f"[Pipeline] Confirmed bugs: {confirmed}")
if only_spec:
print("[Pipeline] Done (specs only; reasoning & bug validation skipped).")
else:
print("[Pipeline] Done.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
usage="python3 main.py <proj_dir> [--resume] [--incremental INTENT_FILE] "
"[--domain-knowledge FILE ...] [--one-phase] [--isolate] "
"[--all-bugs] "
"[--submodule PATH [PATH ...]] [--entry-func PATH] "
"[--end-func PATH ...] [--extra-edge FILE] "
"[--bug-validator FILE] [--only-spec] "
"[--list-plugin] [--plugin NAME]",
description="Run the FM agent pipeline on a project directory.",
)
parser.add_argument("proj_dir", nargs="?", help="path to the project directory")
parser.add_argument(
"--resume",
action="store_true",
help="continue a previous run in <proj_dir>/fm_agent instead of wiping it: "
"keeps phases.json, generated specs, and existing verification results; "
"only does the remaining work. Repeat --all-bugs when resuming an "
"all-bugs run.",
)
parser.add_argument(
"--incremental",
metavar="INTENT_FILE",
help="Run in incremental mode. Value is the path to the intent file "
"defining the goal of modification.",
)
parser.add_argument(
"--isolate",
action="store_true",
help="Run the pipeline against an isolated git worktree snapshot of "
"the project instead of the project directory itself.",
)
parser.add_argument(
"--one-phase",
action="store_true",
help="Put all planned source files into a single analysis phase.",
)
parser.add_argument(
"--all-bugs",
action="store_true",
help="continue reasoning after mismatches and report every candidate; "
"full and incremental modes validate each candidate.",
)
parser.add_argument(
"--only-spec",
action="store_true",
help="Only generate behavioral specs; skip the reasoning and bug "
"validation stages.",
)
parser.add_argument(
"--domain-knowledge",
"--knowledge",
metavar="FILE",
action="append",
nargs="+",
default=[],
help="additional Markdown domain-knowledge file(s) to copy into "
"fm_agent/spec_prompts/domain_context/user_knowledge/ and provide to "
"setup, spec generation, and validation agents. May be repeated. "
"FM_AGENT_DOMAIN_KNOWLEDGE can also provide os.pathsep-separated files.",
)
parser.add_argument(
"--submodule",
metavar="PATH",
nargs="+",
default=None,
help="Only process source code under one or more subdirectories of proj_dir.",
)
parser.add_argument(
"--entry-func",
metavar="PATH",
default=None,
help="function path of the entry point to start reasoning from.",
)
parser.add_argument(
"--end-func",
metavar="PATH",
nargs="+",
default=None,
help="one or more function paths at which to stop (space-separated list); "
"if omitted, the whole call graph reachable from --entry-func is analyzed.",
)
parser.add_argument(
"--extra-edge",
dest="extra_edge",
metavar="FILE",
default=None,
help="optional JSON file, or directory of JSON files, containing "
"supplemental caller->callee edges.",
)
parser.add_argument(
"--bug-validator",
metavar="FILE",
default=None,
help="use a custom Markdown prompt for bug validation instead of "
"the built-in md/bug_validator.md",
)
parser.add_argument(
"--list-plugin",
action="store_true",
help="list all valid plugins found under the plugins/ directory and exit.",
)
parser.add_argument(
"--plugin",
metavar="NAME",
default=None,
help="load and activate the named plugin from the plugins/ directory.",
)
args = parser.parse_args()
if args.list_plugin:
from pathlib import Path
from src.plugin import load_plugins
plugins_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "plugins")
plugins = load_plugins(Path(plugins_dir))
if not plugins:
print("No valid plugins found.")
else:
print(f"{'Plugin':<30} {'Version':<12} Stages")
print("-" * 65)
for name, plugin in plugins.items():
stage_names = ", ".join(plugin.stages.keys()) if plugin.stages else "(none)"
print(f"{name:<30} {plugin.version:<12} {stage_names}")
sys.exit(0)
selected_plugin = args.plugin
if selected_plugin and args.entry_func is not None:
parser.error("--plugin cannot be combined with --entry-func.")
plugin_config = None
if selected_plugin:
if not args.proj_dir:
parser.error("the following arguments are required when --plugin is used: proj_dir")
from pathlib import Path
from src.plugin import validate_plugin
plugins_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "plugins")
plugin_config = validate_plugin(Path(plugins_dir) / selected_plugin)
if plugin_config is None:
print(f"[Pipeline] ERROR: Plugin '{selected_plugin}' not found or invalid.")
sys.exit(1)
print(f"[Pipeline] Loaded plugin '{plugin_config.name}' v{plugin_config.version}")
if not args.proj_dir:
parser.error("the following arguments are required: proj_dir")
resume = args.resume or os.environ.get("FM_AGENT_RESUME") == "1"
proj_dir = os.path.abspath(args.proj_dir)
extra_call_edges_path = args.extra_edge
if extra_call_edges_path:
extra_call_edges_path = os.path.abspath(extra_call_edges_path)
plugin_context = {
"extra_edge": extra_call_edges_path,
}
try:
bug_validator_path = _resolve_bug_validator_path(args.bug_validator)
except ValueError as exc:
parser.error(str(exc))
try:
submodules = _normalize_submodules(proj_dir, args.submodule)
except ValueError as exc:
parser.error(str(exc))
try:
domain_knowledge_files = collect_domain_knowledge_paths(
args.domain_knowledge,
base_dir=proj_dir,
fallback_base_dir=os.getcwd(),
)
except ValueError as exc:
parser.error(str(exc))
if submodules and args.entry_func is not None:
parser.error("--submodule cannot be combined with --entry-func.")
if args.only_spec and args.incremental:
parser.error(
"--only-spec cannot be combined with --incremental "
"(incremental mode is inherently a reasoning/bug-validation flow)."
)
# ---- pre-flight environment check (shared by all pipeline modes) ----
import config
from src.env_check import run as env_check_run
if not env_check_run(proj_dir, config):
sys.exit(0)
start_time = time.time()
# Entry-point mode uses its dedicated copy-and-trim pipeline.
if args.entry_func is not None:
run_entry_pipeline(
proj_dir,
entry_func=args.entry_func,
end_funcs=args.end_func,
resume=resume,
domain_knowledge_files=domain_knowledge_files,
one_phase=args.one_phase,
extra_call_edges_path=extra_call_edges_path,
only_spec=args.only_spec,
bug_validator_path=bug_validator_path,
all_bugs=args.all_bugs,
)
end_time = time.time()
logging.info(f"Total time: {end_time - start_time:.2f} seconds")
sys.exit(0)
# Incremental mode diffs against the commit recorded by a previous run, and
# --isolate snapshots the repo via a git worktree, so both require a git repo.
# A non-git project can only run the full pipeline against the project directory
# itself.
if not _is_git_repo(proj_dir):
parser.error(
f"FM-Agent requires a git repository, but {proj_dir} is not."
)
# Resolve the intent path before snapshotting, since cwd-relative paths must
# resolve against the real project, not the frozen worktree copy.
intent_path = os.path.abspath(args.incremental) if args.incremental else None
# In incremental mode the commit to diff against is the most recent one recorded
# in version.log (the last line, since each run appends its commit). Read it from
# the real project before snapshotting.
old_commit = None
if args.incremental:
version_path = os.path.join(proj_dir, "fm_agent", "version.log")
if os.path.exists(version_path):
with open(version_path, "r") as f:
commits = [line.strip() for line in f if line.strip()]
old_commit = commits[-1] if commits else None
# Capture the project's latest commit id before running. With --isolate the
# pipeline runs against a throwaway worktree snapshot whose HEAD is a synthetic
# snapshot commit, so the version to record must come from the real project.
new_commit = _get_head_commit(proj_dir)
# With --isolate, the pipeline runs against the snapshot's fm_agent/. Resuming
# needs the previous run's fm_agent/ (phases.json, specs, verification results)
# to be present in the snapshot, so copy the excluded workspace in for resume
# too — not just incremental mode.
run_ctx = (
frozen_worktree(
proj_dir, copy_excluded=bool(args.incremental) or resume
)
if args.isolate
else contextlib.nullcontext(proj_dir)
)
with run_ctx as run_dir:
try:
# Incremental mode requires a recorded commit to diff against; without a
# version.log from a previous run, fall back to the full pipeline.
if args.incremental and old_commit:
run_incremental_pipeline(
run_dir,
intent_path,
old_commit,
domain_knowledge_files=domain_knowledge_files,
submodules=submodules,
one_phase=args.one_phase,
extra_call_edges_path=extra_call_edges_path,
bug_validator_path=bug_validator_path,
all_bugs=args.all_bugs,
)
else:
run_pipeline(
run_dir,
resume=resume,
domain_knowledge_files=domain_knowledge_files,
submodules=submodules,
one_phase=args.one_phase,
extra_call_edges_path=extra_call_edges_path,
only_spec=args.only_spec,
bug_validator_path=bug_validator_path,
plugin_config=plugin_config,
plugin_context=plugin_context,
all_bugs=args.all_bugs,
)
# Record the commit that was processed. Written after the pipeline since
# it recreates fm_agent/; with --isolate it lives in the snapshot and is
# copied back to the real project below. Only recorded on success so a
# partial run does not advance the version baseline.
_record_version(new_commit, os.path.join(run_dir, "fm_agent"))
finally:
# With --isolate the pipeline ran against a throwaway snapshot, so its
# fm_agent/ results live in the snapshot. Copy them back into the real
# project so they are not lost when the snapshot is discarded — this runs
# even when the pipeline crashes or is interrupted mid-run, so partial
# progress survives and can be resumed with --resume.
if args.isolate:
src_fm = os.path.join(run_dir, "fm_agent")
dst_fm = os.path.join(proj_dir, "fm_agent")
if os.path.isdir(src_fm):
if os.path.isdir(dst_fm):
shutil.rmtree(dst_fm)
shutil.copytree(src_fm, dst_fm, symlinks=True)
print(f"[Pipeline] Copied results back to {dst_fm}")
end_time = time.time()
logging.info(f"Total time: {end_time - start_time:.2f} seconds")