Repository navigation
Expand file tree
/
Copy pathconfigure.py
More file actions
executable file
·2004 lines (1814 loc) · 84.2 KB
/
Copy pathconfigure.py
File metadata and controls
executable file
·2004 lines (1814 loc) · 84.2 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
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Mantis Configuration Manager.
Configures workflow.json with sandboxes (static-only, gvisor, microsandbox, gce),
model selection (Gemini, Claude, GLM, OpenAI-compatible), and fast preflight testing.
Supports local configuration overlays via workflow.local.json.
"""
import argparse
import asyncio
import json
import os
import platform
import re
import shutil
import subprocess
import sys
import tempfile
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union
# Ensure reference root is in sys.path when script is run directly
_REF_ROOT = str(Path(__file__).resolve().parent.parent)
if _REF_ROOT not in sys.path:
sys.path.insert(0, _REF_ROOT)
from core.config import (
DEFAULT_MODEL,
DEFAULT_VERTEX_FAST_MODEL,
PLACEHOLDER_STRINGS,
RECOMMENDED_MODELS,
SUPPORTED_SANDBOXES,
_strip_empty_injection_points,
get_llm_kwargs,
is_placeholder,
normalize_model_id,
resolve_model_id,
)
# Guest images the microsandbox tier can run. The first is the operator-built
# image from ./install.sh (sandbox/Dockerfile); the fallbacks are the pinned
# base images install.sh pulls directly when no container builder is present
# (mirror.gcr.io first because some networks block Docker Hub). Keep this list
# in sync with the FALLBACK_IMAGES list in install.sh.
MICROSANDBOX_DEFAULT_IMAGE = "mantis-sandbox:latest"
MICROSANDBOX_FALLBACK_IMAGES = (
"mirror.gcr.io/library/python:3-slim",
"docker.io/library/python:3-slim",
)
def get_local_workflow_path(base_workflow_path: str) -> str:
"""Returns path to local workflow overlay adjacent to base workflow path."""
base_dir = os.path.dirname(os.path.abspath(base_workflow_path))
base_name = os.path.basename(base_workflow_path)
stem = base_name[:-5] if base_name.endswith(".json") else base_name
return os.path.join(base_dir, f"{stem}.local.json")
def merge_dicts(base: dict, overlay: dict) -> dict:
"""Deeply merges overlay dictionary into base dictionary.
Cleanly replaces sandbox configurations when switching sandbox mechanisms
or resetting options to avoid inheriting incompatible base options.
"""
merged = dict(base)
for k, v in overlay.items():
if k == "sandbox" and isinstance(v, dict):
base_sb = merged.get("sandbox", {}) if isinstance(merged.get("sandbox"), dict) else {}
base_type = base_sb.get("type")
new_type = v.get("type", base_type)
if new_type in ("static-only", "static"):
merged["sandbox"] = {"type": new_type, "options": {}}
sb_proj = v.get("options", {}).get("project") if isinstance(v.get("options"), dict) else None
if sb_proj and not merged.get("project"):
merged["project"] = sb_proj
elif new_type != base_type or ("options" in v and v["options"] == {}):
merged["sandbox"] = dict(v)
if "options" not in merged["sandbox"] or not isinstance(merged["sandbox"]["options"], dict):
merged["sandbox"]["options"] = {}
else:
merged["sandbox"] = {
"type": new_type,
"options": merge_dicts(
base_sb.get("options", {}) if isinstance(base_sb.get("options"), dict) else {},
v.get("options", {}) if isinstance(v.get("options"), dict) else {},
),
}
elif k in merged and isinstance(merged[k], dict) and isinstance(v, dict):
merged[k] = merge_dicts(merged[k], v)
else:
merged[k] = v
return merged
def find_workflow_json(custom_path: str = "") -> str:
"""Discovers workflow.json across standard reference package locations.
Never probes untrusted $CWD/workflow.json by default to prevent repository
graph hijacking. Explicit custom paths must be supplied via CLI/argument.
"""
if custom_path:
return os.path.abspath(custom_path)
# SECURITY (INV-4): every candidate below is derived from __file__ (the installed
# package location). $CWD is never probed: launch.py resolves the operator's
# configured sandbox through this function, so a workflow.json planted in an
# untrusted checkout would otherwise dictate the execution graph and sandbox policy.
package_wf = os.path.join(Path(__file__).resolve().parent.parent, "workflow.json")
parent_ref_wf = os.path.join(Path(__file__).resolve().parent.parent.parent, "reference", "workflow.json")
for candidate in [package_wf, parent_ref_wf]:
if os.path.exists(candidate):
return os.path.abspath(candidate)
return os.path.abspath(package_wf)
def load_workflow_dict(workflow_path: str, load_local: bool = True) -> dict:
"""Loads workflow JSON dictionary from path or returns a default template.
If load_local is True, checks for workflow.local.json (or .workflow.local.json)
adjacent to workflow_path and merges its configuration on top.
"""
data = None
if os.path.exists(workflow_path):
try:
with open(workflow_path, "r", encoding="utf-8") as f:
loaded = json.load(f)
if isinstance(loaded, dict):
data = loaded
except Exception:
pass
if data is None:
data = {
"name": "mantis_vulnerability_pipeline",
"config": {
"db_path": "knowledge.db",
"retry_attempts": 3,
"default_model": DEFAULT_MODEL,
"reasoning_effort": "medium",
"seed_prompt": "Initial Task Input: Evaluate {filepath}",
"sandbox": {"type": "static-only", "options": {}},
},
"nodes": [],
"edges": [],
}
if load_local:
base_dir = os.path.dirname(os.path.abspath(workflow_path))
base_name = os.path.basename(workflow_path)
stem = base_name[:-5] if base_name.endswith(".json") else base_name
local_candidates = [
os.path.join(base_dir, f"{stem}.local.json"),
os.path.join(base_dir, f".{stem}.local.json"),
]
abs_wf = os.path.abspath(workflow_path)
for cand in local_candidates:
if os.path.exists(cand) and os.path.abspath(cand) != abs_wf:
try:
with open(cand, "r", encoding="utf-8") as lf:
local_data = json.load(lf)
if isinstance(local_data, dict):
if "config" in local_data and isinstance(local_data["config"], dict):
data["config"] = merge_dicts(data.get("config", {}), local_data["config"])
for top_k in (
"sandbox",
"default_model",
"fast_model",
"api_base",
"timeout",
"reasoning_effort",
"db_path",
"retry_attempts",
"seed_prompt",
):
if top_k in local_data and (
"config" not in local_data
or top_k not in local_data.get("config", {})
):
if top_k == "sandbox" and isinstance(local_data[top_k], dict):
data.setdefault("config", {})["sandbox"] = merge_dicts(
{"sandbox": data.get("config", {}).get("sandbox", {})},
{"sandbox": local_data["sandbox"]},
)["sandbox"]
elif isinstance(local_data[top_k], dict) and isinstance(data.get("config", {}).get(top_k), dict):
data.setdefault("config", {})[top_k] = merge_dicts(
data.get("config", {}).get(top_k, {}), local_data[top_k]
)
else:
data.setdefault("config", {})[top_k] = local_data[top_k]
for k in ("name", "nodes", "edges"):
if k in local_data:
data[k] = local_data[k]
break
except Exception as e:
print(f"[CONFIG WARNING] Could not load local overlay {cand}: {e}")
return data
def check_microsandbox_virtualization() -> Tuple[bool, str]:
"""Platform-aware check that the microsandbox tier can boot microVMs here.
Linux: requires a readable/writable /dev/kvm. macOS (Apple Silicon):
libkrun rides Hypervisor.framework -- no /dev/kvm exists, the bundled
libkrunfw in the microsandbox wheel is the requirement. Mirrors the
platform guard in MicrosandboxEnvironment.__init__.
"""
if sys.platform.startswith("linux"):
if not os.path.exists("/dev/kvm"):
return False, "/dev/kvm device does not exist."
if not os.access("/dev/kvm", os.R_OK | os.W_OK):
return False, "Current user lacks read/write permissions on /dev/kvm."
return True, "KVM virtualization available (/dev/kvm accessible)."
if sys.platform == "darwin":
if platform.machine() != "arm64":
return False, "microsandbox on macOS requires Apple Silicon (arm64)."
try:
import microsandbox as _msb # noqa: F401
except Exception as e:
return False, f"microsandbox package not importable: {e}"
return True, "Hypervisor.framework virtualization available (Apple Silicon + microsandbox wheel)."
return False, f"microsandbox is not supported on platform '{sys.platform}'."
def verify_microsandbox_isolation_primitives() -> Tuple[bool, str]:
"""Verifies the installed microsandbox SDK enforces Network.none() and PullPolicy.NEVER."""
try:
from microsandbox import PullPolicy
from microsandbox.types import Action, Network
except Exception as e:
return False, f"microsandbox SDK not importable: {e}"
try:
net = Network.none()
policy = getattr(net, "policy", None)
if (
policy is None
or getattr(policy, "default_egress", None) != Action.DENY
or getattr(policy, "default_ingress", None) != Action.DENY
or tuple(getattr(policy, "rules", ())) != ()
or dict(getattr(net, "ports", {})) != {}
):
return (
False,
"microsandbox Network.none() does not enforce Action.DENY on default egress/ingress with empty rules and ports.",
)
if getattr(PullPolicy, "NEVER", None) is None:
return False, "microsandbox PullPolicy.NEVER is unavailable."
except Exception as e:
return False, f"microsandbox isolation check failed: {e}"
return True, "Microsandbox Network.none() egress/ingress DENY policy verified."
def find_cached_microsandbox_image(preferred: str = "") -> Optional[str]:
"""Returns the first locally cached guest image usable by the microsandbox tier.
The runtime boots with PullPolicy.NEVER, so an image missing from the local
cache means every reproducer campaign fails at first execute. Checks the
operator's configured image first, then the install.sh-built image, then
the pinned fallback base images. Returns None when nothing is cached.
"""
candidates = []
if preferred:
candidates.append(preferred)
candidates.append(MICROSANDBOX_DEFAULT_IMAGE)
candidates.extend(MICROSANDBOX_FALLBACK_IMAGES)
async def _probe() -> Optional[str]:
from microsandbox import Image, ImageNotFoundError
for cand in candidates:
try:
await Image.get(cand)
return cand
except ImageNotFoundError:
continue
except Exception:
# Cache DB unavailable (locked/permissions): treat as no image
# rather than crashing configuration.
return None
return None
try:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if loop and loop.is_running():
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
return executor.submit(asyncio.run, _probe()).result()
return asyncio.run(_probe())
except Exception:
return None
def _adc_credentials_candidates() -> List[str]:
"""Returns candidate Application Default Credentials file paths in
`google.auth.default()` precedence order without invoking `gcloud` or
probing the GCE metadata server."""
env_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
if env_path:
return [env_path]
try:
from google.auth import _cloud_sdk
well_known = _cloud_sdk.get_application_default_credentials_path()
if well_known:
return [well_known]
except Exception:
pass
cfg_dir = os.environ.get("CLOUDSDK_CONFIG")
if not cfg_dir:
if os.name == "nt" and os.environ.get("APPDATA"):
cfg_dir = os.path.join(os.environ["APPDATA"], "gcloud")
else:
cfg_dir = os.path.join(os.path.expanduser("~"), ".config", "gcloud")
return [os.path.join(cfg_dir, "application_default_credentials.json")]
def _detect_adc_project() -> Optional[str]:
"""Safely extracts a non-placeholder GCP project from an existing local ADC
credentials file using `google.auth.load_credentials_from_file()`.
Avoids `google.auth.default()` so startup never probes the GCE metadata
server (`169.254.169.254`) when no ADC file exists and never launches an
unbounded `gcloud config get project` subprocess. Skips `external_account`
files (which can trigger HTTP or pluggable-command calls) and intentionally
does NOT fall back to `quota_project_id` so a billing project is never
mistaken for (or persisted as) a Vertex/GCE sandbox project. Never raises.
"""
adc_path = next(
(p for p in _adc_credentials_candidates() if p and os.path.isfile(p)),
None,
)
if not adc_path:
return None
try:
with open(adc_path, "r", encoding="utf-8") as fh:
info = json.load(fh)
if not isinstance(info, dict):
return None
if info.get("type") in ("external_account", "external_account_authorized_user"):
return None
import warnings
import google.auth
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
warnings.simplefilter("ignore", UserWarning)
creds, proj = google.auth.load_credentials_from_file(adc_path)
candidate = proj or getattr(creds, "project_id", None)
if candidate and not is_placeholder(str(candidate)):
return str(candidate).strip()
except Exception:
pass
return None
def detect_capabilities() -> dict:
"""Inspects the local host environment to detect available sandboxes, tools, and credentials."""
caps: dict[str, Any] = {
"kvm": False,
"docker": False,
"podman": False,
"container_tool": None,
"runsc": False,
"gcloud": False,
"gcp_auth": False,
"gcp_account": None,
"gcp_project": None,
"gcp_project_probe_failed": False,
"vertex_project": os.environ.get("VERTEXAI_PROJECT")
or os.environ.get("GOOGLE_CLOUD_PROJECT"),
"gemini_api_key": bool(
os.environ.get("GEMINI_API_KEY") or os.environ.get("GOOGLE_API_KEY")
),
"anthropic_api_key": bool(os.environ.get("ANTHROPIC_API_KEY")),
"openai_api_key": bool(os.environ.get("OPENAI_API_KEY")),
"llm_api_base": os.environ.get("LLM_API_BASE"),
"microsandbox_image": None,
"recommended_sandbox": "static-only",
"available_sandboxes": ["static-only"],
}
# 1. Check virtualization and SDK isolation primitives for microsandbox
# (Linux KVM or macOS Hypervisor.framework -- see check_microsandbox_virtualization).
virt_ok, _ = check_microsandbox_virtualization()
if virt_ok:
caps["kvm"] = os.path.exists("/dev/kvm") and os.access("/dev/kvm", os.R_OK | os.W_OK)
iso_ok, _ = verify_microsandbox_isolation_primitives()
if iso_ok:
caps["available_sandboxes"].append("microsandbox")
caps["microsandbox_image"] = find_cached_microsandbox_image()
# 2. Check container engines and gVisor
for tool in ("docker", "podman"):
if shutil.which(tool):
caps[tool] = True
if not caps["container_tool"]:
caps["container_tool"] = tool
# Check runsc runtime
try:
out = subprocess.run(
[tool, "info", "--format", "{{json .Runtimes}}"],
capture_output=True,
text=True,
timeout=3,
)
if out.returncode == 0 and "runsc" in out.stdout:
caps["runsc"] = True
if "gvisor" not in caps["available_sandboxes"]:
caps["available_sandboxes"].append("gvisor")
except Exception:
pass
# 3. Check gcloud CLI and GCP Auth
gcloud_bin = shutil.which("gcloud")
if gcloud_bin:
caps["gcloud"] = True
try:
p_auth = subprocess.run(
[
gcloud_bin,
"auth",
"list",
"--filter=status:ACTIVE",
"--format=value(account)",
],
capture_output=True,
text=True,
timeout=4,
)
if p_auth.returncode == 0 and p_auth.stdout.strip():
caps["gcp_auth"] = True
caps["gcp_account"] = p_auth.stdout.strip().splitlines()[0].strip()
p_proj = subprocess.run(
[gcloud_bin, "config", "get-value", "project"],
capture_output=True,
text=True,
timeout=3,
)
if p_proj.returncode == 0:
val = p_proj.stdout.strip()
if val and "unset" not in val and not is_placeholder(val):
caps["gcp_project"] = val
except subprocess.TimeoutExpired:
caps["gcp_project_probe_failed"] = True
except Exception:
pass
if (
not caps["gcp_project"]
and not caps["vertex_project"]
and not caps["gcp_project_probe_failed"]
):
adc_proj = _detect_adc_project()
if adc_proj:
caps["vertex_project"] = adc_proj
if caps["gcloud"] and caps["gcp_auth"] and (caps["gcp_project"] or caps["vertex_project"]):
caps["available_sandboxes"].append("gce")
# Select recommendation hierarchy. Microsandbox ranks first when a guest
# image is cached: it is the only tier whose runnability is fully verified
# client-side (hardware microVM + image present), whereas 'gce' merely
# having gcloud auth says nothing about the pre-provisioned VPC/subnet/VM
# image it needs, and gvisor shares the host kernel. Without a cached
# image microsandbox is NOT recommended: the runtime never pulls
# (PullPolicy.NEVER), so every campaign would fail at first execute.
if "microsandbox" in caps["available_sandboxes"] and caps["microsandbox_image"]:
caps["recommended_sandbox"] = "microsandbox"
elif "gce" in caps["available_sandboxes"] and caps["gcp_project"]:
caps["recommended_sandbox"] = "gce"
elif "gvisor" in caps["available_sandboxes"]:
caps["recommended_sandbox"] = "gvisor"
else:
caps["recommended_sandbox"] = "static-only"
return caps
def _distinct_configured_models(config: dict, nodes: Optional[List[dict]] = None) -> List[str]:
"""Returns deduplicated model IDs active across default_model, fast_model, and agent nodes."""
default_m = config.get("default_model", DEFAULT_MODEL)
candidates: List[str] = []
if default_m is not None:
candidates.append(str(default_m))
fast_m = config.get("fast_model")
if fast_m:
candidates.append(str(fast_m))
resolved_list = config.get("resolved_node_models")
if isinstance(resolved_list, list):
for rm in resolved_list:
if rm:
candidates.append(str(rm))
if isinstance(nodes, list):
has_unpinned_fast_tier = False
for node in nodes:
if not isinstance(node, dict) or node.get("type") != "agent" or node.get("id") == "structural_index":
continue
if node.get("model"):
candidates.append(str(node["model"]))
elif node.get("id") in ("history", "deduplicator"):
has_unpinned_fast_tier = True
if (
has_unpinned_fast_tier
and not fast_m
and not config.get("api_base")
and not (os.environ.get("LLM_API_BASE") or "").strip()
and not (os.environ.get("MANTIS_MODEL") or os.environ.get("MODEL_ID") or "").strip()
and not is_placeholder(default_m)
and resolve_model_id(None, str(default_m or DEFAULT_MODEL)).startswith("vertex_ai/gemini-")
):
candidates.append(DEFAULT_VERTEX_FAST_MODEL)
if not candidates:
candidates.append(DEFAULT_MODEL)
seen_resolved: set[str] = set()
ordered: List[str] = []
for raw_m in candidates:
norm_key = raw_m if is_placeholder(raw_m) else resolve_model_id(raw_m, str(default_m or DEFAULT_MODEL))
if norm_key not in seen_resolved:
seen_resolved.add(norm_key)
ordered.append(raw_m)
return ordered
def _non_vertex_model_hint(
config: Optional[dict] = None,
caps: Optional[dict] = None,
) -> str:
"""Returns an actionable CLI hint when non-Vertex credentials or --api-base
are present while the model is still pinned to a vertex_ai/* default."""
cfg_api_base = (config.get("api_base") if isinstance(config, dict) else None) or (
caps.get("llm_api_base") if isinstance(caps, dict) else None
) or os.environ.get("LLM_API_BASE") or os.environ.get("OPENAI_API_BASE")
if cfg_api_base:
return (
f"Detected custom API base ('{cfg_api_base}') with a Vertex AI model; "
f"select a matching provider/model explicitly, e.g.: "
f".venv/bin/python3 scripts/configure.py --model openai/<model-id> --api-base {cfg_api_base}"
)
has_anthropic = (
bool(caps.get("anthropic_api_key"))
if isinstance(caps, dict) and "anthropic_api_key" in caps
else bool(os.environ.get("ANTHROPIC_API_KEY"))
)
if has_anthropic:
return (
"Detected ANTHROPIC_API_KEY; select an Anthropic model explicitly, e.g.: "
".venv/bin/python3 scripts/configure.py --model anthropic/claude-sonnet-4-5"
)
has_openai = (
bool(caps.get("openai_api_key"))
if isinstance(caps, dict) and "openai_api_key" in caps
else bool(os.environ.get("OPENAI_API_KEY"))
)
if has_openai:
return (
"Detected OPENAI_API_KEY; select an OpenAI model explicitly, e.g.: "
".venv/bin/python3 scripts/configure.py --model openai/gpt-4o"
)
has_gemini = (
bool(caps.get("gemini_api_key"))
if isinstance(caps, dict) and "gemini_api_key" in caps
else bool(os.environ.get("GEMINI_API_KEY") or os.environ.get("GOOGLE_API_KEY"))
)
if has_gemini:
return (
"Detected GEMINI_API_KEY; run '.venv/bin/python3 scripts/configure.py --auto' "
"or '.venv/bin/python3 scripts/configure.py --model gemini/gemini-3.8-flash'."
)
return ""
def is_default_or_unconfigured(config: dict, nodes: Optional[List[dict]] = None) -> Tuple[bool, List[str]]:
"""Evaluates if workflow config contains default placeholders or unconfigured settings."""
issues = []
if not isinstance(config, dict):
return True, ["Config is not a valid dictionary."]
sb = config.get("sandbox", {})
sb_type = sb.get("type", "static-only") if isinstance(sb, dict) else "static-only"
sb_opts = sb.get("options", {}) if isinstance(sb, dict) else {}
# Sandbox checks
if sb_type == "gce":
proj = sb_opts.get("project")
if is_placeholder(proj):
env_proj = os.environ.get("GOOGLE_CLOUD_PROJECT") or os.environ.get("VERTEXAI_PROJECT")
if is_placeholder(env_proj):
issues.append(
f"GCE Sandbox 'options.project' contains default placeholder ('{proj}')."
)
if not shutil.which("gcloud"):
issues.append("GCE Sandbox requires 'gcloud' CLI on PATH.")
elif sb_type == "gvisor":
if not shutil.which("docker") and not shutil.which("podman"):
issues.append("gVisor sandbox requires 'docker' or 'podman' on PATH.")
elif sb_type == "microsandbox":
virt_ok, virt_msg = check_microsandbox_virtualization()
if not virt_ok:
issues.append(f"Microsandbox virtualization unavailable: {virt_msg}")
else:
iso_ok, iso_msg = verify_microsandbox_isolation_primitives()
if not iso_ok:
issues.append(f"Microsandbox isolation policy unverified: {iso_msg}")
elif sb_type == "remote":
has_target = (
(sb_opts.get("container") and not is_placeholder(sb_opts.get("container")))
or (sb_opts.get("host") and not is_placeholder(sb_opts.get("host")))
or (sb_opts.get("ssh_host") and not is_placeholder(sb_opts.get("ssh_host")))
or (isinstance(sb_opts.get("exec_argv"), list) and len(sb_opts.get("exec_argv")) > 0)
)
if not has_target:
issues.append(
"Remote sandbox requires 'options.container', 'options.host', or 'options.exec_argv' in workflow.local.json."
)
# Model checks across all active stage models
caps_cache: Optional[dict] = None
for model in _distinct_configured_models(config, nodes=nodes):
if is_placeholder(model):
issues.append(f"Model '{model}' contains placeholder string.")
continue
resolved_m = resolve_model_id(model, config.get("default_model", DEFAULT_MODEL))
if str(resolved_m).startswith("vertex_ai/"):
proj = (
sb_opts.get("project")
or os.environ.get("VERTEXAI_PROJECT")
or os.environ.get("GOOGLE_CLOUD_PROJECT")
or config.get("project")
)
if is_placeholder(proj):
if caps_cache is None:
caps_cache = detect_capabilities()
if not caps_cache.get("gcp_project") and not caps_cache.get("vertex_project"):
hint = _non_vertex_model_hint(config, caps_cache)
msg = (
f"Vertex AI Model '{resolved_m}' requires VERTEXAI_PROJECT / GOOGLE_CLOUD_PROJECT or active gcloud project."
)
if hint:
msg = f"{msg} {hint}"
issues.append(msg)
return bool(issues), issues
async def _check_sandbox_preflight(sandbox_cfg: dict, target_path: str = "") -> Tuple[bool, str]:
"""Runs the asynchronous preflight check on a sandbox configuration."""
sb_type = sandbox_cfg.get("type", "static-only")
if sb_type in ("static-only", "static"):
return True, "Static-only sandbox ready (dynamic execution disabled)."
if sb_type == "gce":
opts = sandbox_cfg.get("options", {})
proj = opts.get("project") or os.environ.get("GOOGLE_CLOUD_PROJECT") or os.environ.get("VERTEXAI_PROJECT")
if not proj or is_placeholder(proj):
return False, f"GCE Project is unconfigured placeholder '{proj}'."
if not shutil.which("gcloud"):
return False, "'gcloud' CLI tool not found on PATH."
# Fast gcloud auth test
try:
p = subprocess.run(
["gcloud", "auth", "list", "--filter=status:ACTIVE", "--format=value(account)"],
capture_output=True,
text=True,
timeout=5,
cwd=tempfile.gettempdir(),
)
if p.returncode != 0 or not p.stdout.strip():
return False, "No active GCP credentials found in gcloud auth list."
return (
True,
f"GCE credentials & project verified (Project: {proj}). "
f"(Note: Ephemeral VM creation requires pre-provisioned VPC/Subnet/Image per docs/gce_sandbox_setup.md).",
)
except Exception as e:
return False, f"GCE gcloud check failed: {e}"
if sb_type == "gvisor":
raw_tool = sandbox_cfg.get("options", {}).get("container_tool")
if raw_tool and raw_tool not in ("docker", "podman"):
return False, f"Invalid container_tool '{raw_tool}'. Only 'docker' and 'podman' are allowed."
tool = raw_tool or ("docker" if shutil.which("docker") else "podman")
if not tool or not shutil.which(tool):
return False, "Docker or Podman not installed for gVisor sandbox."
try:
p = subprocess.run(
[tool, "info", "--format", "{{json .Runtimes}}"],
capture_output=True,
text=True,
timeout=5,
cwd=tempfile.gettempdir(),
)
if p.returncode != 0:
return False, f"Cannot connect to {tool} daemon."
if "runsc" not in p.stdout:
return False, f"Runtime 'runsc' (gVisor) is not registered in {tool} runtimes."
return True, f"gVisor Sandbox ready ({tool} + runsc)."
except Exception as e:
return False, f"gVisor check failed: {e}"
if sb_type == "microsandbox":
virt_ok, virt_msg = check_microsandbox_virtualization()
if not virt_ok:
return False, virt_msg
iso_ok, iso_msg = verify_microsandbox_isolation_primitives()
if not iso_ok:
return False, iso_msg
configured_image = str(sandbox_cfg.get("options", {}).get("image", "") or "")
cached = find_cached_microsandbox_image(preferred=configured_image)
if configured_image and cached != configured_image:
return False, (
f"Configured guest image '{configured_image}' is not in the local cache "
f"(the sandbox never pulls at run time). Run ./install.sh to provision it."
)
if not cached:
return False, (
"No microsandbox guest image found in the local cache "
"(the sandbox never pulls at run time). Run ./install.sh to provision one."
)
return True, f"Microsandbox ready ({virt_msg} Guest image: {cached})."
if sb_type == "remote":
try:
from core.environments.remote_env import RemoteTargetEnvironment
opts = dict(sandbox_cfg.get("options", {}) or {})
for k, v in sandbox_cfg.items():
if k not in ("type", "options") and v is not None:
opts.setdefault(k, v)
env = RemoteTargetEnvironment(target_path=target_path, **opts)
await env.preflight()
return True, f"Remote split-plane sandbox ready (transport={env.transport}, workdir={env.working_dir})."
except Exception as e:
return False, f"Remote sandbox preflight failed: {e}"
return False, f"Unknown sandbox type '{sb_type}'."
def _probe_llm_reachability(
resolved_model: str,
kwargs: dict,
prompt: str = "test",
max_tokens: int = 256,
timeout: float = 15.0,
) -> Tuple[bool, str]:
"""Actively probes LLM endpoint reachability, credentials, and dependencies with a minimal test prompt."""
try:
import litellm
except ImportError:
return False, "LiteLLM is not installed in the current environment."
# For Vertex AI partner models (e.g. vertex_ai/claude-*, vertex_ai/zai_org/*),
# verify that required client dependencies are installed
if resolved_model.startswith("vertex_ai/"):
if "claude" in resolved_model:
try:
import anthropic # noqa: F401
import vertexai # noqa: F401
except ImportError as ie:
return (
False,
f"Missing dependency for Vertex AI partner model '{resolved_model}': {ie}. "
f"Run 'pip install google-cloud-aiplatform anthropic' or re-run './install.sh'."
)
from core.config import (
is_rate_limit_error,
extract_retry_after,
extract_rate_limit_detail,
compute_full_jitter_delay,
is_auth_error,
format_auth_error_message,
)
call_kwargs = dict(kwargs)
call_kwargs["max_tokens"] = max_tokens
call_kwargs["timeout"] = timeout
call_kwargs["model"] = resolved_model
_strip_empty_injection_points(call_kwargs)
max_probe_attempts = 3
for attempt in range(max_probe_attempts):
try:
response = litellm.completion(
messages=[{"role": "user", "content": prompt}],
**call_kwargs,
)
if response and getattr(response, "choices", None) and len(response.choices) > 0:
return True, f"LLM reachability verified for '{resolved_model}'."
return True, f"LLM probe received response for '{resolved_model}'."
except Exception as e:
err_msg = str(e)
err_type = type(e).__name__
if is_auth_error(e):
return False, format_auth_error_message(e, model=resolved_model)
if "No module named 'vertexai'" in err_msg or "No module named 'anthropic'" in err_msg:
return (
False,
f"Missing dependency for Vertex AI partner models: {err_msg}. "
f"Run 'pip install google-cloud-aiplatform anthropic' or re-run './install.sh'."
)
if is_rate_limit_error(e):
if attempt + 1 < max_probe_attempts:
retry_after = extract_retry_after(e)
delay = compute_full_jitter_delay(
attempt=attempt,
initial_delay=5.0,
max_delay=30.0,
min_offset=5.0,
retry_after=retry_after,
)
time.sleep(delay)
continue
detail = extract_rate_limit_detail(e)
return (
True,
f"LLM reachability verified for '{resolved_model}' (Endpoint & credentials verified; currently rate-limited: {detail}).",
)
return False, f"LLM reachability probe failed ({err_type}): {err_msg}"
return False, f"LLM reachability probe failed after {max_probe_attempts} attempts."
def _check_single_llm_preflight(
model: str,
config: dict,
*,
probe: bool = False,
) -> Tuple[bool, str]:
api_base = config.get("api_base")
timeout = config.get("timeout")
effort = config.get("reasoning_effort")
try:
resolved_model, kwargs = get_llm_kwargs(
model_id=model,
default_model=config.get("default_model", DEFAULT_MODEL),
api_base=api_base,
timeout=timeout,
reasoning_effort=effort,
config=config,
)
except Exception as e:
return False, f"LLM Configuration Error: {e}"
if resolved_model.startswith("vertex_ai/openai/"):
proj = kwargs.get("vertex_project")
if not proj or is_placeholder(proj):
if not api_base:
return False, "Vertex AI OpenAI model requires a valid GCP Project ID or --api-base endpoint."
endpoint_info = f" @ {api_base}" if api_base else ""
static_msg = f"Vertex AI OpenAI LLM configured (Model: {resolved_model}{endpoint_info})."
elif resolved_model.startswith("vertex_ai/"):
proj = kwargs.get("vertex_project")
if not proj or is_placeholder(proj):
hint = _non_vertex_model_hint(config)
base_err = "Vertex AI requires a valid GCP Project ID."
return False, f"{base_err} {hint}" if hint else base_err
static_msg = f"Vertex AI LLM configured (Model: {resolved_model}, Project: {proj})."
elif resolved_model.startswith("anthropic/"):
if not os.environ.get("ANTHROPIC_API_KEY"):
return False, "Anthropic model requires ANTHROPIC_API_KEY environment variable."
static_msg = f"Anthropic LLM configured (Model: {resolved_model})."
elif resolved_model.startswith("openai/") or api_base:
if not os.environ.get("OPENAI_API_KEY") and not api_base:
return False, "OpenAI model requires OPENAI_API_KEY or --api-base endpoint."
endpoint_info = f" @ {api_base}" if api_base else ""
static_msg = f"OpenAI-compatible LLM configured (Model: {resolved_model}{endpoint_info})."
elif resolved_model.startswith("gemini/"):
if not os.environ.get("GEMINI_API_KEY") and not os.environ.get("GOOGLE_API_KEY"):
return (
False,
"Gemini API model ('gemini/...') requires GEMINI_API_KEY or GOOGLE_API_KEY "
"(use 'vertex_ai/...' to authenticate with a GCP Project ID).",
)
static_msg = f"Gemini LLM configured (Model: {resolved_model})."
elif resolved_model.startswith("gemini-"):
if not os.environ.get("GEMINI_API_KEY") and not os.environ.get("GOOGLE_API_KEY"):
proj_candidates = (
os.environ.get("GOOGLE_CLOUD_PROJECT"),
os.environ.get("VERTEXAI_PROJECT"),
kwargs.get("vertex_project"),
)
if not any(p and not is_placeholder(str(p)) for p in proj_candidates):
return False, "Gemini model requires GEMINI_API_KEY or GCP Project ID for Vertex AI."
static_msg = f"Gemini LLM configured (Model: {resolved_model})."
else:
static_msg = f"LLM configured (Model: {resolved_model})."
should_probe = probe or os.environ.get("MANTIS_PROBE_LLM") in ("1", "true", "True")
if should_probe:
probe_timeout = timeout if timeout is not None else 15.0
probe_ok, probe_msg = _probe_llm_reachability(
resolved_model, kwargs, prompt="test", max_tokens=256, timeout=probe_timeout
)
if not probe_ok:
return False, probe_msg
return True, f"{static_msg} [Live probe: OK]"
return True, static_msg
def _check_llm_preflight(
config: dict,
probe: bool = False,
nodes: Optional[List[dict]] = None,
) -> Tuple[bool, str]:
"""Fast validation of LLM configuration and credentials across all distinct stage models.
If probe is True (or MANTIS_PROBE_LLM=1), actively tests model reachability
and client dependencies by sending a test prompt with max_tokens=256.
"""
distinct_models = _distinct_configured_models(config, nodes=nodes)
checked_msgs: List[str] = []
for cand_model in distinct_models:
ok, msg = _check_single_llm_preflight(cand_model, config, probe=probe)
if not ok:
return False, msg
checked_msgs.append(msg)
return True, "; ".join(checked_msgs)
_PINNED_RUNTIME_PACKAGES = ("google-adk", "litellm")
def check_pinned_dependencies(requirements_path: Optional[str] = None) -> Tuple[bool, str]:
"""Compares the installed runtime packages against requirements.txt's pins.
Reported in the field: a fresh `git pull` on a venv that still carried the
previous google-adk failed inside ADK's workflow replay with a traceback
that no longer exists in the pinned release, and the report read as a
Mantis regression. The check is a warning, not a gate -- an operator may
run a newer ADK on purpose -- but it names the package, both versions and
the command, so the next report says which it was.
"""
try:
from importlib import metadata
except Exception: # pragma: no cover
return True, "dependency pins not checked (importlib.metadata unavailable)"
req = Path(requirements_path) if requirements_path else Path(__file__).resolve().parent.parent / "requirements.txt"
try:
text = req.read_text(encoding="utf-8")
except OSError:
return True, f"dependency pins not checked ({req.name} not found)"
pins: Dict[str, str] = {}
for line in text.splitlines():
m = re.match(r"^([A-Za-z0-9][A-Za-z0-9._-]*)==([^\s\\;]+)", line.strip())
if m:
pins[m.group(1).lower().replace("_", "-")] = m.group(2)
stale = []
for name in _PINNED_RUNTIME_PACKAGES:
pinned = pins.get(name)
if not pinned:
continue
try:
installed = metadata.version(name)
except metadata.PackageNotFoundError:
stale.append(f"{name}: not installed (pinned {pinned})")
continue
if installed != pinned:
stale.append(f"{name}: installed {installed}, pinned {pinned}")
if stale:
return False, (
"installed packages differ from requirements.txt -- " + "; ".join(stale)
+ f". Reinstall with: pip install --require-hashes -r {req} "
"(failures inside google.adk on a stale install are not Mantis bugs)."