jarvis-ai-assistant 0.5.0__py3-none-any.whl → 0.6.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.
Files changed (41) hide show
  1. jarvis/__init__.py +1 -1
  2. jarvis/jarvis_agent/__init__.py +114 -6
  3. jarvis/jarvis_agent/agent_manager.py +3 -0
  4. jarvis/jarvis_agent/jarvis.py +45 -9
  5. jarvis/jarvis_agent/run_loop.py +6 -1
  6. jarvis/jarvis_agent/task_planner.py +219 -0
  7. jarvis/jarvis_c2rust/__init__.py +13 -0
  8. jarvis/jarvis_c2rust/cli.py +405 -0
  9. jarvis/jarvis_c2rust/collector.py +209 -0
  10. jarvis/jarvis_c2rust/library_replacer.py +933 -0
  11. jarvis/jarvis_c2rust/llm_module_agent.py +1265 -0
  12. jarvis/jarvis_c2rust/scanner.py +1671 -0
  13. jarvis/jarvis_c2rust/transpiler.py +1236 -0
  14. jarvis/jarvis_code_agent/code_agent.py +151 -18
  15. jarvis/jarvis_data/config_schema.json +13 -3
  16. jarvis/jarvis_sec/README.md +180 -0
  17. jarvis/jarvis_sec/__init__.py +674 -0
  18. jarvis/jarvis_sec/checkers/__init__.py +33 -0
  19. jarvis/jarvis_sec/checkers/c_checker.py +1269 -0
  20. jarvis/jarvis_sec/checkers/rust_checker.py +367 -0
  21. jarvis/jarvis_sec/cli.py +110 -0
  22. jarvis/jarvis_sec/prompts.py +324 -0
  23. jarvis/jarvis_sec/report.py +260 -0
  24. jarvis/jarvis_sec/types.py +20 -0
  25. jarvis/jarvis_sec/workflow.py +513 -0
  26. jarvis/jarvis_tools/cli/main.py +1 -0
  27. jarvis/jarvis_tools/execute_script.py +1 -1
  28. jarvis/jarvis_tools/read_code.py +11 -1
  29. jarvis/jarvis_tools/read_symbols.py +129 -0
  30. jarvis/jarvis_tools/registry.py +9 -1
  31. jarvis/jarvis_tools/sub_agent.py +4 -3
  32. jarvis/jarvis_tools/sub_code_agent.py +3 -3
  33. jarvis/jarvis_utils/config.py +28 -6
  34. jarvis/jarvis_utils/git_utils.py +39 -0
  35. jarvis/jarvis_utils/utils.py +150 -7
  36. {jarvis_ai_assistant-0.5.0.dist-info → jarvis_ai_assistant-0.6.0.dist-info}/METADATA +13 -1
  37. {jarvis_ai_assistant-0.5.0.dist-info → jarvis_ai_assistant-0.6.0.dist-info}/RECORD +41 -22
  38. {jarvis_ai_assistant-0.5.0.dist-info → jarvis_ai_assistant-0.6.0.dist-info}/entry_points.txt +4 -0
  39. {jarvis_ai_assistant-0.5.0.dist-info → jarvis_ai_assistant-0.6.0.dist-info}/WHEEL +0 -0
  40. {jarvis_ai_assistant-0.5.0.dist-info → jarvis_ai_assistant-0.6.0.dist-info}/licenses/LICENSE +0 -0
  41. {jarvis_ai_assistant-0.5.0.dist-info → jarvis_ai_assistant-0.6.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,933 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Library-based dependency replacer for C→Rust migration (LLM-only subtree evaluation).
4
+
5
+ 要点:
6
+ - 不依赖 pruner,仅复用 scanner 的通用工具函数
7
+ - 将“依赖子树(根函数及其可达的函数集合)”的摘要与局部源码片段提供给 LLM,由 LLM 评估该子树是否可由“指定标准库/第三方 crate 的单个 API”整体替代
8
+ - 若可替代:将根函数的 ref 替换为该库 API,并删除其所有子孙函数节点(类型不受影响)
9
+ - 生成剪枝后的符号表与替代映射,并计算新的转译顺序(兼容输出别名)
10
+
11
+ 输入数据:
12
+ - symbols.jsonl(或传入的 .jsonl 路径):由 scanner 生成的统一符号表,字段参见 scanner.py
13
+
14
+ 输出:
15
+ - symbols_library_pruned.jsonl:剪枝后的符号表(默认名,可通过参数自定义)
16
+ - library_replacements.jsonl:替代根到库 API 的映射(JSONL,每行一个 {id,name,qualified_name,library,function,confidence,mode})
17
+ - 兼容输出:
18
+ - symbols_prune.jsonl:与主输出等价
19
+ - symbols.jsonl:通用别名(用于后续流程统一读取)
20
+ - translation_order_prune.jsonl:剪枝阶段的转译顺序
21
+ - translation_order.jsonl:通用别名(与剪枝阶段一致)
22
+ """
23
+
24
+ from __future__ import annotations
25
+
26
+ import json
27
+ import shutil
28
+ import time
29
+ from pathlib import Path
30
+ from typing import Any, Dict, List, Optional, Set, Tuple
31
+
32
+ import typer
33
+
34
+ # 依赖:仅使用 scanner 的工具函数,避免循环导入
35
+ from jarvis.jarvis_c2rust.scanner import (
36
+ compute_translation_order_jsonl,
37
+ find_root_function_ids,
38
+ )
39
+
40
+
41
+ def apply_library_replacement(
42
+ db_path: Path,
43
+ library_name: str,
44
+ llm_group: Optional[str] = None,
45
+ candidates: Optional[List[str]] = None,
46
+ out_symbols_path: Optional[Path] = None,
47
+ out_mapping_path: Optional[Path] = None,
48
+ max_funcs: Optional[int] = None,
49
+ disabled_libraries: Optional[List[str]] = None,
50
+ resume: bool = True,
51
+ checkpoint_path: Optional[Path] = None,
52
+ checkpoint_interval: int = 1,
53
+ clear_checkpoint_on_done: bool = True,
54
+ ) -> Dict[str, Path]:
55
+ """
56
+ 基于依赖图由 LLM 判定,对满足“整子树可由指定库单个 API 替代”的函数根进行替换并剪枝。
57
+
58
+ 参数:
59
+ - db_path: 指向 symbols.jsonl 的路径或其所在目录
60
+ - library_name: 指定库名(如 'std'、'regex'),要求 LLM 仅在该库中选择 API
61
+ - llm_group: 可选,评估时使用的模型组
62
+ - candidates: 仅评估这些函数作为根(名称或限定名);缺省评估所有根函数(无入边)
63
+ - out_symbols_path/out_mapping_path: 输出文件路径(若省略使用默认)
64
+ - max_funcs: LLM 评估的最大根数量(限流/调试)
65
+ - disabled_libraries: 禁用的开源库名称列表(不允许在评估/建议中使用;在提示词中明确说明)
66
+ 返回:
67
+ Dict[str, Path]: {"symbols": 新符号表路径, "mapping": 替代映射路径, "symbols_prune": 兼容符号表路径, "order": 通用顺序路径, "order_prune": 剪枝阶段顺序路径}
68
+ """
69
+
70
+ def _resolve_symbols_jsonl_path(hint: Path) -> Path:
71
+ p = Path(hint)
72
+ if p.is_file() and p.suffix.lower() == ".jsonl":
73
+ return p
74
+ if p.is_dir():
75
+ return p / ".jarvis" / "c2rust" / "symbols.jsonl"
76
+ return Path(".") / ".jarvis" / "c2rust" / "symbols.jsonl"
77
+
78
+ sjsonl = _resolve_symbols_jsonl_path(db_path)
79
+ if not sjsonl.exists():
80
+ raise FileNotFoundError(f"未找到 symbols.jsonl: {sjsonl}")
81
+
82
+ data_dir = sjsonl.parent
83
+ if out_symbols_path is None:
84
+ out_symbols_path = data_dir / "symbols_library_pruned.jsonl"
85
+ else:
86
+ out_symbols_path = Path(out_symbols_path)
87
+ if out_mapping_path is None:
88
+ out_mapping_path = data_dir / "library_replacements.jsonl"
89
+ else:
90
+ out_mapping_path = Path(out_mapping_path)
91
+
92
+ # 兼容输出
93
+ out_symbols_prune_path = data_dir / "symbols_prune.jsonl"
94
+ alias_symbols_path = data_dir / "symbols.jsonl" # 通用别名
95
+ order_prune_path = data_dir / "translation_order_prune.jsonl"
96
+ alias_order_path = data_dir / "translation_order.jsonl"
97
+
98
+ # Checkpoint 默认路径
99
+ if checkpoint_path is None:
100
+ checkpoint_path = data_dir / "library_replacer_checkpoint.json"
101
+
102
+ # 读取符号
103
+ all_records: List[Dict[str, Any]] = []
104
+ by_id: Dict[int, Dict[str, Any]] = {}
105
+ name_to_id: Dict[str, int] = {}
106
+ func_ids: Set[int] = set()
107
+ id_refs_names: Dict[int, List[str]] = {}
108
+
109
+ with open(sjsonl, "r", encoding="utf-8") as f:
110
+ idx = 0
111
+ for line in f:
112
+ line = line.strip()
113
+ if not line:
114
+ continue
115
+ try:
116
+ obj = json.loads(line)
117
+ except Exception:
118
+ continue
119
+ idx += 1
120
+ fid = int(obj.get("id") or idx)
121
+ obj["id"] = fid
122
+ nm = obj.get("name") or ""
123
+ qn = obj.get("qualified_name") or ""
124
+ cat = obj.get("category") or "" # "function" | "type"
125
+ refs = obj.get("ref")
126
+ if not isinstance(refs, list):
127
+ refs = []
128
+ refs = [r for r in refs if isinstance(r, str) and r]
129
+
130
+ all_records.append(obj)
131
+ by_id[fid] = obj
132
+ id_refs_names[fid] = refs
133
+ if nm:
134
+ name_to_id.setdefault(nm, fid)
135
+ if qn:
136
+ name_to_id.setdefault(qn, fid)
137
+ if cat == "function":
138
+ func_ids.add(fid)
139
+
140
+ # 构造函数内边(id→id)
141
+ adj_func: Dict[int, List[int]] = {}
142
+ for fid in func_ids:
143
+ internal: List[int] = []
144
+ for target in id_refs_names.get(fid, []):
145
+ tid = name_to_id.get(target)
146
+ if tid is not None and tid in func_ids and tid != fid:
147
+ internal.append(tid)
148
+ try:
149
+ internal = list(dict.fromkeys(internal))
150
+ except Exception:
151
+ internal = sorted(list(set(internal)))
152
+ adj_func[fid] = internal
153
+
154
+ # 评估队列:从所有无入边函数作为种子开始,按层次遍历整个图,使“父先于子”被评估;
155
+ # 若不存在无入边节点(如强连通环),则回退为全量函数集合。
156
+ try:
157
+ roots_all = find_root_function_ids(sjsonl)
158
+ except Exception:
159
+ roots_all = []
160
+ seeds = [rid for rid in roots_all if rid in func_ids]
161
+ if not seeds:
162
+ seeds = sorted(list(func_ids))
163
+
164
+ visited: Set[int] = set()
165
+ order: List[int] = []
166
+ q: List[int] = list(seeds)
167
+ qi = 0
168
+ while qi < len(q):
169
+ u = q[qi]
170
+ qi += 1
171
+ if u in visited or u not in func_ids:
172
+ continue
173
+ visited.add(u)
174
+ order.append(u)
175
+ for v in adj_func.get(u, []):
176
+ if v not in visited and v in func_ids:
177
+ q.append(v)
178
+ # 若存在未覆盖的孤立/循环组件,补充其节点(确保每个函数节点都将被作为“候选根”参与评估)
179
+ if len(visited) < len(func_ids):
180
+ leftovers = [fid for fid in sorted(func_ids) if fid not in visited]
181
+ order.extend(leftovers)
182
+
183
+ # 评估顺序:包含根节点与所有子节点(广度优先近似的父先子后),被覆盖/剪枝的节点将自动跳过
184
+ root_funcs = order
185
+
186
+ # 可达缓存(需在 candidates 使用前定义,避免前向引用)
187
+ desc_cache: Dict[int, Set[int]] = {}
188
+
189
+ def _collect_descendants(start: int) -> Set[int]:
190
+ if start in desc_cache:
191
+ return desc_cache[start]
192
+ visited: Set[int] = set()
193
+ stack: List[int] = [start]
194
+ visited.add(start)
195
+ while stack:
196
+ u = stack.pop()
197
+ for v in adj_func.get(u, []):
198
+ if v not in visited:
199
+ visited.add(v)
200
+ stack.append(v)
201
+ desc_cache[start] = visited
202
+ return visited
203
+
204
+ # 如果传入 candidates,则仅评估这些节点(按上面的顺序过滤),并限定作用域:
205
+ # - 仅保留从这些根可达的函数;对不可达函数直接删除(类型记录保留)
206
+ scope_unreachable_funcs: Set[int] = set()
207
+ if candidates:
208
+ cand_ids: Set[int] = set()
209
+ # 支持重载:同名/同限定名可能对应多个函数ID,需全部纳入候选
210
+ key_set = set(candidates)
211
+ for rec in all_records:
212
+ if (rec.get("category") or "") != "function":
213
+ continue
214
+ nm = rec.get("name") or ""
215
+ qn = rec.get("qualified_name") or ""
216
+ if nm in key_set or qn in key_set:
217
+ try:
218
+ cand_ids.add(int(rec.get("id")))
219
+ except Exception:
220
+ continue
221
+ if cand_ids:
222
+ root_funcs = [rid for rid in root_funcs if rid in cand_ids]
223
+ # 计算从候选根出发的可达函数集合(含根)
224
+ reachable_all: Set[int] = set()
225
+ for rid in root_funcs:
226
+ reachable_all.update(_collect_descendants(rid))
227
+ # 不可达函数(仅限函数类别)将被直接删除
228
+ scope_unreachable_funcs = {fid for fid in func_ids if fid not in reachable_all}
229
+ if scope_unreachable_funcs:
230
+ typer.secho(
231
+ f"[c2rust-library] 根据根列表,标记不可达函数删除: {len(scope_unreachable_funcs)} 个",
232
+ fg=typer.colors.YELLOW,
233
+ err=True,
234
+ )
235
+
236
+ # 读取源码片段(辅助 LLM)
237
+ def _read_source_snippet(rec: Dict[str, Any], max_lines: int = 200) -> str:
238
+ path = rec.get("file") or ""
239
+ try:
240
+ if not path:
241
+ return ""
242
+ p = Path(path)
243
+ if not p.exists():
244
+ return ""
245
+ sl = int(rec.get("start_line") or 1)
246
+ el = int(rec.get("end_line") or sl)
247
+ if el < sl:
248
+ el = sl
249
+ lines = p.read_text(encoding="utf-8", errors="replace").splitlines()
250
+ start_idx = max(sl - 1, 0)
251
+ end_idx = min(el, len(lines))
252
+ snippet_lines = lines[start_idx:end_idx]
253
+ if len(snippet_lines) > max_lines:
254
+ snippet_lines = snippet_lines[:max_lines]
255
+ return "\n".join(snippet_lines)
256
+ except Exception:
257
+ return ""
258
+
259
+ # LLM 可用性
260
+ try:
261
+ from jarvis.jarvis_platform.registry import PlatformRegistry # type: ignore
262
+ from jarvis.jarvis_utils.config import get_normal_platform_name, get_normal_model_name # type: ignore
263
+ _model_available = True
264
+ except Exception:
265
+ PlatformRegistry = None # type: ignore
266
+ get_normal_platform_name = None # type: ignore
267
+ get_normal_model_name = None # type: ignore
268
+ _model_available = False
269
+
270
+ # 预处理禁用库
271
+ disabled_norm: List[str] = []
272
+ disabled_display: str = ""
273
+ if isinstance(disabled_libraries, list):
274
+ disabled_norm = [str(x).strip().lower() for x in disabled_libraries if str(x).strip()]
275
+ disabled_display = ", ".join([str(x).strip() for x in disabled_libraries if str(x).strip()])
276
+
277
+ # 断点恢复支持:工具函数与关键键构造
278
+ def _norm_list(items: Optional[List[str]]) -> List[str]:
279
+ if not isinstance(items, list):
280
+ return []
281
+ vals: List[str] = []
282
+ for x in items:
283
+ try:
284
+ s = str(x).strip()
285
+ except Exception:
286
+ continue
287
+ if s:
288
+ vals.append(s)
289
+ try:
290
+ vals = list(dict.fromkeys(vals))
291
+ except Exception:
292
+ vals = sorted(set(vals))
293
+ return vals
294
+
295
+ def _norm_list_lower(items: Optional[List[str]]) -> List[str]:
296
+ return [s.lower() for s in _norm_list(items)]
297
+
298
+ ckpt_path: Path = Path(checkpoint_path) if checkpoint_path is not None else (data_dir / "library_replacer_checkpoint.json")
299
+
300
+ def _make_ckpt_key() -> Dict[str, Any]:
301
+ try:
302
+ abs_sym = str(Path(sjsonl).resolve())
303
+ except Exception:
304
+ abs_sym = str(sjsonl)
305
+ key: Dict[str, Any] = {
306
+ "symbols": abs_sym,
307
+ "library_name": str(library_name),
308
+ "llm_group": str(llm_group or ""),
309
+ "candidates": _norm_list(candidates),
310
+ "disabled_libraries": _norm_list_lower(disabled_libraries),
311
+ "max_funcs": (int(max_funcs) if isinstance(max_funcs, int) or (isinstance(max_funcs, float) and float(max_funcs).is_integer()) else None),
312
+ }
313
+ return key
314
+
315
+ def _load_checkpoint_if_match() -> Optional[Dict[str, Any]]:
316
+ try:
317
+ if not resume:
318
+ return None
319
+ if not ckpt_path.exists():
320
+ return None
321
+ obj = json.loads(ckpt_path.read_text(encoding="utf-8"))
322
+ if not isinstance(obj, dict):
323
+ return None
324
+ if obj.get("key") != _make_ckpt_key():
325
+ return None
326
+ return obj
327
+ except Exception:
328
+ return None
329
+
330
+ def _atomic_write(path: Path, content: str) -> None:
331
+ try:
332
+ tmp = path.with_suffix(path.suffix + ".tmp")
333
+ tmp.write_text(content, encoding="utf-8")
334
+ tmp.replace(path)
335
+ except Exception:
336
+ try:
337
+ path.write_text(content, encoding="utf-8")
338
+ except Exception:
339
+ pass
340
+
341
+ def _new_model() -> Optional[Any]:
342
+ if not _model_available:
343
+ return None
344
+ try:
345
+ registry = PlatformRegistry.get_global_platform_registry() # type: ignore
346
+ model = None
347
+ if llm_group:
348
+ try:
349
+ platform_name = get_normal_platform_name(llm_group) # type: ignore
350
+ if platform_name:
351
+ model = registry.create_platform(platform_name) # type: ignore
352
+ except Exception:
353
+ model = None
354
+ if model is None:
355
+ model = registry.get_normal_platform() # type: ignore
356
+ try:
357
+ model.set_model_group(llm_group) # type: ignore
358
+ except Exception:
359
+ pass
360
+ if llm_group:
361
+ try:
362
+ mn = get_normal_model_name(llm_group) # type: ignore
363
+ if mn:
364
+ model.set_model_name(mn) # type: ignore
365
+ except Exception:
366
+ pass
367
+ model.set_system_prompt( # type: ignore
368
+ "你是资深 C→Rust 迁移专家。任务:给定一个函数及其调用子树(依赖图摘要、函数签名、源码片段),"
369
+ "判断是否可以使用一个或多个成熟的 Rust 库整体替代该子树的功能(允许库内多个 API 协同,允许多个库组合;不允许使用不成熟/不常见库)。"
370
+ "如可替代,请给出 libraries 列表(库名),可选给出代表性 API/模块与实现备注 notes(如何用这些库协作实现)。"
371
+ "输出格式:仅输出一个 <yaml> 块,字段: replaceable(bool), libraries(list[str]), confidence(float 0..1),可选 library(str,首选主库), api(str) 或 apis(list),notes(str)。"
372
+ )
373
+ return model
374
+ except Exception as e:
375
+ typer.secho(
376
+ f"[c2rust-library] 初始化 LLM 平台失败,将回退为保守策略: {e}",
377
+ fg=typer.colors.YELLOW,
378
+ err=True,
379
+ )
380
+ return None
381
+
382
+ # 解析 YAML/JSON 结果
383
+ def _parse_agent_yaml_summary(text: str) -> Optional[Dict[str, Any]]:
384
+ if not isinstance(text, str) or not text.strip():
385
+ return None
386
+ import re as _re
387
+ import json as _json
388
+ try:
389
+ import yaml # type: ignore
390
+ except Exception:
391
+ yaml = None # type: ignore
392
+
393
+ m_sum = _re.search(r"<SUMMARY>([\s\S]*?)</SUMMARY>", text, flags=_re.IGNORECASE)
394
+ block = (m_sum.group(1) if m_sum else text).strip()
395
+
396
+ m_yaml = _re.search(r"<yaml>([\s\S]*?)</yaml>", block, flags=_re.IGNORECASE)
397
+ if m_yaml:
398
+ raw = m_yaml.group(1).strip()
399
+ if raw and yaml:
400
+ try:
401
+ data = yaml.safe_load(raw)
402
+ if isinstance(data, dict):
403
+ return data
404
+ except Exception:
405
+ pass
406
+
407
+ m_code = _re.search(r"```(?:yaml|yml)\s*([\s\S]*?)```", block, flags=_re.IGNORECASE)
408
+ if m_code:
409
+ raw = m_code.group(1).strip()
410
+ if raw and yaml:
411
+ try:
412
+ data = yaml.safe_load(raw)
413
+ if isinstance(data, dict):
414
+ return data
415
+ except Exception:
416
+ pass
417
+
418
+ m_json = _re.search(r"\{[\s\S]*\}", block)
419
+ if m_json:
420
+ raw = m_json.group(0).strip()
421
+ try:
422
+ data = _json.loads(raw)
423
+ if isinstance(data, dict):
424
+ return data
425
+ except Exception:
426
+ pass
427
+
428
+ # 宽松键值
429
+ def _kv(pattern: str) -> Optional[str]:
430
+ m = _re.search(pattern, block, flags=_re.IGNORECASE)
431
+ return m.group(1).strip() if m else None
432
+
433
+ rep_raw = _kv(r"replaceable\s*:\s*(.+)")
434
+ lib_raw = _kv(r"library\s*:\s*(.+)")
435
+ api_raw = _kv(r"(?:api|function)\s*:\s*(.+)")
436
+ conf_raw = _kv(r"confidence\s*:\s*([0-9\.\-eE]+)")
437
+ if any([rep_raw, lib_raw, api_raw, conf_raw]):
438
+ result: Dict[str, Any] = {}
439
+ if rep_raw is not None:
440
+ rep_s = rep_raw.strip().strip("\"'")
441
+ result["replaceable"] = rep_s.lower() in ("true", "yes", "y", "1")
442
+ if lib_raw is not None:
443
+ result["library"] = lib_raw.strip().strip("\"'")
444
+ if api_raw is not None:
445
+ result["api"] = api_raw.strip().strip("\"'")
446
+ if conf_raw is not None:
447
+ try:
448
+ result["confidence"] = float(conf_raw)
449
+ except Exception:
450
+ pass
451
+ return result if result else None
452
+ return None
453
+
454
+ def _llm_evaluate_subtree(fid: int, desc: Set[int]) -> Dict[str, Any]:
455
+ if not _model_available:
456
+ return {"replaceable": False}
457
+ model = _new_model()
458
+ if not model:
459
+ return {"replaceable": False}
460
+
461
+ root_rec = by_id.get(fid, {})
462
+ root_name = root_rec.get("qualified_name") or root_rec.get("name") or f"sym_{fid}"
463
+ root_sig = root_rec.get("signature") or ""
464
+ root_lang = root_rec.get("language") or ""
465
+ root_src = _read_source_snippet(root_rec)
466
+
467
+ # 子树摘要(限制长度,避免超长)
468
+ nodes_meta: List[str] = []
469
+ for nid in sorted(desc):
470
+ r = by_id.get(nid, {})
471
+ nm = r.get("qualified_name") or r.get("name") or f"sym_{nid}"
472
+ sg = r.get("signature") or ""
473
+ if sg and sg != nm:
474
+ nodes_meta.append(f"- {nm} | {sg}")
475
+ else:
476
+ nodes_meta.append(f"- {nm}")
477
+ if len(nodes_meta) > 200:
478
+ nodes_meta = nodes_meta[:200] + [f"...({len(desc)-200} more)"]
479
+
480
+ # 选取部分代表性叶子/内部节点源码(最多 3 个)
481
+ samples: List[str] = []
482
+ sample_ids: List[int] = [fid]
483
+ for ch in adj_func.get(fid, [])[:2]:
484
+ sample_ids.append(ch)
485
+ for sid in sample_ids:
486
+ rec = by_id.get(sid, {})
487
+ nm = rec.get("qualified_name") or rec.get("name") or f"sym_{sid}"
488
+ sg = rec.get("signature") or ""
489
+ src = _read_source_snippet(rec, max_lines=120)
490
+ samples.append(f"--- BEGIN {nm} ---\n{sg}\n{src}\n--- END {nm} ---")
491
+
492
+ # 构建依赖图(子树内的调用有向边)
493
+ def _label(nid: int) -> str:
494
+ r = by_id.get(nid, {})
495
+ return r.get("qualified_name") or r.get("name") or f"sym_{nid}"
496
+
497
+ edges_list: List[str] = []
498
+ for u in sorted(desc):
499
+ for v in adj_func.get(u, []):
500
+ if v in desc:
501
+ edges_list.append(f"{_label(u)} -> {_label(v)}")
502
+ edges_text: str
503
+ if len(edges_list) > 400:
504
+ edges_text = "\n".join(edges_list[:400] + [f"...({len(edges_list) - 400} more edges)"])
505
+ else:
506
+ edges_text = "\n".join(edges_list)
507
+
508
+ # 适度提供 DOT(边数不大时),便于大模型直观看图
509
+ dot_text = ""
510
+ if len(edges_list) <= 200:
511
+ dot_lines: List[str] = ["digraph subtree {", " rankdir=LR;"]
512
+ for u in sorted(desc):
513
+ for v in adj_func.get(u, []):
514
+ if v in desc:
515
+ dot_lines.append(f' "{_label(u)}" -> "{_label(v)}";')
516
+ dot_lines.append("}")
517
+ dot_text = "\n".join(dot_lines)
518
+
519
+ disabled_hint = (
520
+ f"重要约束:禁止使用以下库(若这些库为唯一可行选项则判定为不可替代):{disabled_display}\n"
521
+ if disabled_display else ""
522
+ )
523
+
524
+ prompt = (
525
+ "请评估以下 C/C++ 函数子树是否可以由一个或多个成熟的 Rust 库整体替代(语义等价或更强)。"
526
+ "允许库内多个 API 协同,允许多个库组合;如果必须依赖尚不成熟/冷门库或非 Rust 库,则判定为不可替代。\n"
527
+ f"{disabled_hint}"
528
+ "输出格式:仅输出一个 <yaml> 块,字段: replaceable(bool), libraries(list[str]), confidence(float 0..1),"
529
+ "可选字段: library(str,首选主库), api(str) 或 apis(list), notes(str: 简述如何由这些库协作实现的思路)。\n\n"
530
+ f"根函数(被评估子树的根): {root_name}\n"
531
+ f"签名: {root_sig}\n"
532
+ f"语言: {root_lang}\n"
533
+ "根函数源码片段(可能截断):\n"
534
+ f"{root_src}\n\n"
535
+ f"子树规模: {len(desc)} 个函数\n"
536
+ "子树函数列表(名称|签名):\n"
537
+ + "\n".join(nodes_meta)
538
+ + "\n\n"
539
+ "依赖图(调用边,caller -> callee):\n"
540
+ f"{edges_text}\n\n"
541
+ + (f"DOT 表示(边数较少时提供):\n```dot\n{dot_text}\n```\n\n" if dot_text else "")
542
+ + "代表性源码样本(部分节点,可能截断,仅供辅助判断):\n"
543
+ + "\n".join(samples)
544
+ + "\n"
545
+ )
546
+
547
+ try:
548
+ result = model.chat_until_success(prompt) # type: ignore
549
+ parsed = _parse_agent_yaml_summary(result or "")
550
+ if isinstance(parsed, dict):
551
+ rep = bool(parsed.get("replaceable") is True)
552
+ lib = str(parsed.get("library") or "").strip()
553
+ api = str(parsed.get("api") or parsed.get("function") or "").strip()
554
+ apis = parsed.get("apis")
555
+ libs_raw = parsed.get("libraries")
556
+ notes = str(parsed.get("notes") or "").strip()
557
+ # 归一化 libraries
558
+ libraries: List[str] = []
559
+ if isinstance(libs_raw, list):
560
+ libraries = [str(x).strip() for x in libs_raw if str(x).strip()]
561
+ elif isinstance(libs_raw, str):
562
+ libraries = [s.strip() for s in libs_raw.split(",") if s.strip()]
563
+ conf = parsed.get("confidence")
564
+ try:
565
+ conf = float(conf)
566
+ except Exception:
567
+ conf = 0.0
568
+ # 不强制要求具体 API 或特定库名;若缺省且存在 library 字段,则纳入 libraries
569
+ if not libraries and lib:
570
+ libraries = [lib]
571
+
572
+ # 禁用库命中时,强制视为不可替代
573
+ if disabled_norm:
574
+ libs_lower = [l.lower() for l in libraries]
575
+ lib_single_lower = lib.lower() if lib else ""
576
+ banned_hit = any(l in disabled_norm for l in libs_lower) or (lib_single_lower and lib_single_lower in disabled_norm)
577
+ if banned_hit:
578
+ rep = False
579
+ warn_libs = ", ".join(sorted(set([lib] + libraries))) if (libraries or lib) else "(未提供库名)"
580
+ typer.secho(
581
+ f"[c2rust-library] 评估结果包含禁用库,强制判定为不可替代: {root_name} | 命中库: {warn_libs}",
582
+ fg=typer.colors.YELLOW,
583
+ err=True,
584
+ )
585
+ if notes:
586
+ notes = notes + f" | 禁用库命中: {warn_libs}"
587
+ else:
588
+ notes = f"禁用库命中: {warn_libs}"
589
+
590
+ result_obj: Dict[str, Any] = {
591
+ "replaceable": rep,
592
+ "library": lib,
593
+ "libraries": libraries,
594
+ "api": api,
595
+ "confidence": conf,
596
+ }
597
+ if isinstance(apis, list):
598
+ result_obj["apis"] = apis
599
+ if notes:
600
+ result_obj["notes"] = notes
601
+ return result_obj
602
+ typer.secho("[c2rust-library] LLM 结果解析失败,视为不可替代。", fg=typer.colors.YELLOW, err=True)
603
+ return {"replaceable": False}
604
+ except Exception as e:
605
+ typer.secho(f"[c2rust-library] LLM 评估失败,视为不可替代: {e}", fg=typer.colors.YELLOW, err=True)
606
+ return {"replaceable": False}
607
+
608
+ # 评估阶段:若某节点评估不可替代,则继续评估其子节点(递归/深度优先)
609
+ eval_counter = 0
610
+ total_roots = len(root_funcs)
611
+ pruned_dynamic: Set[int] = set() # 动态累计的“将被剪除”的函数集合(不含选中根)
612
+ selected_roots: List[Tuple[int, Dict[str, Any]]] = [] # 实时选中的可替代根(fid, LLM结果)
613
+ processed_roots: Set[int] = set() # 已处理(评估或跳过)的根集合
614
+ last_ckpt_saved = 0 # 上次保存的计数
615
+
616
+ # 若存在匹配的断点文件,则加载恢复
617
+ _loaded_ckpt = _load_checkpoint_if_match()
618
+ if resume and _loaded_ckpt:
619
+ try:
620
+ eval_counter = int(_loaded_ckpt.get("eval_counter") or 0)
621
+ except Exception:
622
+ pass
623
+ try:
624
+ processed_roots = set(int(x) for x in (_loaded_ckpt.get("processed_roots") or []))
625
+ except Exception:
626
+ processed_roots = set()
627
+ try:
628
+ pruned_dynamic = set(int(x) for x in (_loaded_ckpt.get("pruned_dynamic") or []))
629
+ except Exception:
630
+ pruned_dynamic = set()
631
+ try:
632
+ sr_list = []
633
+ for it in (_loaded_ckpt.get("selected_roots") or []):
634
+ if isinstance(it, dict) and "fid" in it and "res" in it:
635
+ sr_list.append((int(it["fid"]), it["res"]))
636
+ selected_roots = sr_list
637
+ except Exception:
638
+ selected_roots = []
639
+ typer.secho(
640
+ f"[c2rust-library] 已从断点恢复: 已评估={eval_counter}, 已处理根={len(processed_roots)}, 已剪除={len(pruned_dynamic)}, 已选中替代根={len(selected_roots)}",
641
+ fg=typer.colors.YELLOW,
642
+ err=True,
643
+ )
644
+
645
+ def _current_checkpoint_state() -> Dict[str, Any]:
646
+ try:
647
+ ts = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime())
648
+ except Exception:
649
+ ts = ""
650
+ return {
651
+ "key": _make_ckpt_key(),
652
+ "eval_counter": eval_counter,
653
+ "processed_roots": sorted(list(processed_roots)),
654
+ "pruned_dynamic": sorted(list(pruned_dynamic)),
655
+ "selected_roots": [{"fid": fid, "res": res} for fid, res in selected_roots],
656
+ "timestamp": ts,
657
+ }
658
+
659
+ def _periodic_checkpoint_save(force: bool = False) -> None:
660
+ nonlocal last_ckpt_saved
661
+ if not resume:
662
+ return
663
+ try:
664
+ interval = int(checkpoint_interval)
665
+ except Exception:
666
+ interval = 1
667
+ need_save = force or (interval <= 0) or ((eval_counter - last_ckpt_saved) >= interval)
668
+ if not need_save:
669
+ return
670
+ try:
671
+ _atomic_write(ckpt_path, json.dumps(_current_checkpoint_state(), ensure_ascii=False, indent=2))
672
+ last_ckpt_saved = eval_counter
673
+ except Exception:
674
+ pass
675
+
676
+ def _evaluate_node(fid: int) -> None:
677
+ nonlocal eval_counter
678
+ # 限流
679
+ if max_funcs is not None and eval_counter >= max_funcs:
680
+ return
681
+ # 若该节点已被标记剪除或已处理,跳过
682
+ if fid in pruned_dynamic or fid in processed_roots or fid not in func_ids:
683
+ return
684
+
685
+ # 构造子树并打印进度
686
+ desc = _collect_descendants(fid)
687
+ rec_meta = by_id.get(fid, {})
688
+ label = rec_meta.get("qualified_name") or rec_meta.get("name") or f"sym_{fid}"
689
+ typer.secho(
690
+ f"[c2rust-library] 正在评估: {label} (ID: {fid}), 子树函数数={len(desc)}",
691
+ fg=typer.colors.CYAN,
692
+ err=True,
693
+ )
694
+
695
+ # 执行 LLM 评估
696
+ res = _llm_evaluate_subtree(fid, desc)
697
+ eval_counter += 1
698
+ processed_roots.add(fid)
699
+ res["mode"] = "llm"
700
+ _periodic_checkpoint_save()
701
+
702
+ # 若可替代,打印评估结果摘要(库/参考API/置信度/备注),并即时标记子孙剪除与后续跳过
703
+ try:
704
+ if res.get("replaceable") is True:
705
+ libs = res.get("libraries") or ([res.get("library")] if res.get("library") else [])
706
+ libs = [str(x) for x in libs if str(x)]
707
+ api = str(res.get("api") or "")
708
+ apis = res.get("apis")
709
+ notes = str(res.get("notes") or "")
710
+ conf = res.get("confidence")
711
+ try:
712
+ conf = float(conf)
713
+ except Exception:
714
+ conf = 0.0
715
+ libs_str = ", ".join(libs) if libs else "(未指定库)"
716
+ apis_str = ", ".join([str(a) for a in apis]) if isinstance(apis, list) else (api if api else "")
717
+ msg = f"[c2rust-library] 可替换: {label} -> 库: {libs_str}"
718
+ if apis_str:
719
+ msg += f"; 参考API: {apis_str}"
720
+ msg += f"; 置信度: {conf:.2f}"
721
+ if notes:
722
+ msg += f"; 备注: {notes[:200]}"
723
+ typer.secho(msg, fg=typer.colors.GREEN, err=True)
724
+
725
+ # 入口函数保护:不替代 main(保留进行转译),改为深入评估其子节点
726
+ nm = str(rec_meta.get("name") or "")
727
+ qn = str(rec_meta.get("qualified_name") or "")
728
+ # Configurable entry detection (avoid hard-coding 'main'):
729
+ # Honor env vars: JARVIS_C2RUST_DELAY_ENTRY_SYMBOLS / JARVIS_C2RUST_DELAY_ENTRIES / C2RUST_DELAY_ENTRIES
730
+ import os
731
+ entries_env = os.environ.get("JARVIS_C2RUST_DELAY_ENTRY_SYMBOLS") or \
732
+ os.environ.get("JARVIS_C2RUST_DELAY_ENTRIES") or \
733
+ os.environ.get("C2RUST_DELAY_ENTRIES") or ""
734
+ entries_set = set()
735
+ if entries_env:
736
+ try:
737
+ import re as _re
738
+ parts = _re.split(r"[,\s;]+", entries_env.strip())
739
+ except Exception:
740
+ parts = [p.strip() for p in entries_env.replace(";", ",").split(",")]
741
+ entries_set = {p.strip().lower() for p in parts if p and p.strip()}
742
+ if entries_set:
743
+ is_entry = (nm.lower() in entries_set) or (qn.lower() in entries_set) or any(qn.lower().endswith(f"::{e}") for e in entries_set)
744
+ else:
745
+ is_entry = (nm.lower() == "main") or (qn.lower() == "main") or qn.lower().endswith("::main")
746
+ if is_entry:
747
+ typer.secho(
748
+ "[c2rust-library] 入口函数保护:跳过对 main 的库替代,继续评估其子节点。",
749
+ fg=typer.colors.YELLOW,
750
+ err=True,
751
+ )
752
+ for ch in adj_func.get(fid, []):
753
+ _evaluate_node(ch)
754
+ else:
755
+ # 即时剪枝(不含根)
756
+ to_prune = set(desc)
757
+ to_prune.discard(fid)
758
+
759
+ newly = len(to_prune - pruned_dynamic)
760
+ pruned_dynamic.update(to_prune)
761
+ selected_roots.append((fid, res))
762
+ _periodic_checkpoint_save()
763
+ typer.secho(
764
+ f"[c2rust-library] 即时标记剪除子节点(本次新增): +{newly} 个 (累计={len(pruned_dynamic)})",
765
+ fg=typer.colors.MAGENTA,
766
+ err=True,
767
+ )
768
+ else:
769
+ # 若不可替代,继续评估其子节点(深度优先)
770
+ for ch in adj_func.get(fid, []):
771
+ _evaluate_node(ch)
772
+ except Exception:
773
+ pass
774
+
775
+ # 对每个候选根进行评估;若根不可替代将递归评估其子节点
776
+ for fid in root_funcs:
777
+ _evaluate_node(fid)
778
+
779
+ # 剪枝集合来自动态评估阶段的累计结果
780
+ pruned_funcs: Set[int] = set(pruned_dynamic)
781
+ # 若限定候选根(candidates)已指定,则将不可达函数一并删除
782
+ try:
783
+ pruned_funcs.update(scope_unreachable_funcs)
784
+ except Exception:
785
+ pass
786
+
787
+ # 写出新符号表
788
+ now_ts = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime())
789
+ kept_ids: Set[int] = set()
790
+ for rec in all_records:
791
+ fid = int(rec.get("id"))
792
+ cat = rec.get("category") or ""
793
+ if cat == "function":
794
+ if fid in pruned_funcs:
795
+ continue
796
+ kept_ids.add(fid)
797
+ else:
798
+ kept_ids.add(fid)
799
+
800
+ sel_root_ids = set(fid for fid, _ in selected_roots)
801
+ replacements: List[Dict[str, Any]] = []
802
+
803
+ with open(out_symbols_path, "w", encoding="utf-8") as fo, \
804
+ open(out_symbols_prune_path, "w", encoding="utf-8") as fo2:
805
+
806
+ for rec in all_records:
807
+ fid = int(rec.get("id"))
808
+ if fid not in kept_ids:
809
+ continue
810
+
811
+ rec_out = dict(rec)
812
+ if (rec.get("category") or "") == "function" and fid in sel_root_ids:
813
+ # 以库级替代为语义:不要求具体 API;将根 ref 设置为库占位符(支持多库组合)
814
+ conf = 0.0
815
+ api = ""
816
+ apis = None
817
+ libraries_out: List[str] = []
818
+ notes_out: str = ""
819
+ lib_single: str = ""
820
+ for rf, rres in selected_roots:
821
+ if rf == fid:
822
+ api = str(rres.get("api") or rres.get("function") or "")
823
+ apis = rres.get("apis")
824
+ libs_val = rres.get("libraries")
825
+ if isinstance(libs_val, list):
826
+ libraries_out = [str(x) for x in libs_val if str(x)]
827
+ lib_single = str(rres.get("library") or "").strip()
828
+ try:
829
+ conf = float(rres.get("confidence") or 0.0)
830
+ except Exception:
831
+ conf = 0.0
832
+ notes_val = rres.get("notes")
833
+ if isinstance(notes_val, str):
834
+ notes_out = notes_val
835
+ break
836
+ # 若 libraries 存在则使用多库占位;否则若存在单个 library 字段则使用之;否则置空
837
+ if libraries_out:
838
+ lib_markers = [f"lib::{lb}" for lb in libraries_out]
839
+ elif lib_single:
840
+ lib_markers = [f"lib::{lib_single}"]
841
+ else:
842
+ lib_markers = []
843
+ rec_out["ref"] = lib_markers
844
+ try:
845
+ rec_out["updated_at"] = now_ts
846
+ except Exception:
847
+ pass
848
+ # 保存库替代元数据到符号表,供后续转译阶段作为上下文使用
849
+ try:
850
+ meta_apis = apis if isinstance(apis, list) else ([api] if api else [])
851
+ lib_primary = libraries_out[0] if libraries_out else lib_single
852
+ rec_out["lib_replacement"] = {
853
+ "libraries": libraries_out,
854
+ "library": lib_primary or "",
855
+ "apis": meta_apis,
856
+ "api": api,
857
+ "confidence": float(conf) if isinstance(conf, (int, float)) else 0.0,
858
+ "notes": notes_out,
859
+ "mode": "llm",
860
+ "updated_at": now_ts,
861
+ }
862
+ except Exception:
863
+ # 忽略写入元数据失败,不阻塞主流程
864
+ pass
865
+ rep_obj: Dict[str, Any] = {
866
+ "id": fid,
867
+ "name": rec.get("name") or "",
868
+ "qualified_name": rec.get("qualified_name") or "",
869
+ "library": (libraries_out[0] if libraries_out else ""),
870
+ "libraries": libraries_out,
871
+ "function": api,
872
+ "confidence": conf,
873
+ "mode": "llm",
874
+ }
875
+ if isinstance(apis, list):
876
+ rep_obj["apis"] = apis
877
+ if notes_out:
878
+ rep_obj["notes"] = notes_out
879
+ replacements.append(rep_obj)
880
+
881
+ line = json.dumps(rec_out, ensure_ascii=False) + "\n"
882
+ fo.write(line)
883
+ fo2.write(line)
884
+ # 不覆写 symbols.jsonl(保留原始扫描/整理结果作为基线)
885
+
886
+ # 写出替代映射
887
+ with open(out_mapping_path, "w", encoding="utf-8") as fm:
888
+ for m in replacements:
889
+ fm.write(json.dumps(m, ensure_ascii=False) + "\n")
890
+
891
+ # 生成转译顺序(剪枝阶段与别名)
892
+ order_path = None
893
+ try:
894
+ compute_translation_order_jsonl(Path(out_symbols_path), out_path=order_prune_path)
895
+ shutil.copy2(order_prune_path, alias_order_path)
896
+ order_path = alias_order_path
897
+ except Exception as e:
898
+ typer.secho(f"[c2rust-library] 基于剪枝符号表生成翻译顺序失败: {e}", fg=typer.colors.YELLOW, err=True)
899
+
900
+ # 完成后清理断点(可选)
901
+ try:
902
+ if resume and clear_checkpoint_on_done and ckpt_path.exists():
903
+ ckpt_path.unlink()
904
+ typer.secho(f"[c2rust-library] 已清理断点文件: {ckpt_path}", fg=typer.colors.BLUE, err=True)
905
+ except Exception:
906
+ pass
907
+
908
+ typer.secho(
909
+ "[c2rust-library] 库替代剪枝完成(LLM 子树评估):\n"
910
+ f"- 选中替代根: {len(selected_roots)} 个\n"
911
+ f"- 剪除函数: {len(pruned_funcs)} 个\n"
912
+ f"- 新符号表: {out_symbols_path}\n"
913
+ f"- 替代映射: {out_mapping_path}\n"
914
+ f"- 兼容符号表输出: {out_symbols_prune_path}\n"
915
+ + (f"- 转译顺序: {order_path}\n" if order_path else "")
916
+ + f"- 兼容顺序输出: {order_prune_path}",
917
+ fg=typer.colors.GREEN,
918
+ )
919
+
920
+ result: Dict[str, Path] = {
921
+ "symbols": Path(out_symbols_path),
922
+ "mapping": Path(out_mapping_path),
923
+ "symbols_prune": Path(out_symbols_prune_path),
924
+ }
925
+ if order_path:
926
+ result["order"] = Path(order_path)
927
+ if order_prune_path:
928
+ result["order_prune"] = Path(order_prune_path)
929
+ return result
930
+
931
+
932
+
933
+ __all__ = ["apply_library_replacement"]