timecs 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. app.py +3014 -0
  2. ast_scanner.py +36187 -0
  3. banner.py +146 -0
  4. ci_reporter.py +570 -0
  5. cli.py +2347 -0
  6. config_loader.py +307 -0
  7. cross_file_engine.py +1781 -0
  8. delivery_models.py +145 -0
  9. delivery_repository.py +78 -0
  10. diff_scan.py +127 -0
  11. durable_delivery_sink.py +103 -0
  12. event_bus.py +155 -0
  13. event_publisher.py +345 -0
  14. event_taxonomy.py +313 -0
  15. function_summarizer.py +354 -0
  16. html_auditor.py +274 -0
  17. iac_auditor.py +400 -0
  18. in_app_adapter.py +115 -0
  19. js_scanner.py +895 -0
  20. manifest_parser.py +960 -0
  21. master_rules_bank.py +389 -0
  22. notification_models.py +346 -0
  23. notification_policy.py +309 -0
  24. notification_repository.py +118 -0
  25. notification_routes.py +394 -0
  26. notification_service.py +199 -0
  27. osv_client.py +437 -0
  28. remediation/__init__.py +39 -0
  29. remediation/base.py +102 -0
  30. remediation/contracts.py +196 -0
  31. remediation/cwe1188_binding.py +78 -0
  32. remediation/cwe1336_jinja.py +91 -0
  33. remediation/cwe22_path_traversal.py +348 -0
  34. remediation/cwe295_ssl.py +87 -0
  35. remediation/cwe327_weak_hash.py +78 -0
  36. remediation/cwe377_tempfile.py +77 -0
  37. remediation/cwe489_debug.py +80 -0
  38. remediation/cwe502_yaml.py +89 -0
  39. remediation/cwe614_cookie.py +91 -0
  40. remediation/cwe78_cmdi.py +415 -0
  41. remediation/cwe798_credentials.py +153 -0
  42. remediation/cwe89_sqli.py +425 -0
  43. remediation/cwe916_password_hash.py +131 -0
  44. remediation/cwe95_eval.py +77 -0
  45. remediation/dispatcher.py +114 -0
  46. remediation/orchestrator.py +541 -0
  47. remediation/patch_engine.py +213 -0
  48. remediation/verification.py +253 -0
  49. rule_engine.py +1535 -0
  50. sarif_adapter.py +719 -0
  51. sarif_exporter.py +88 -0
  52. sca_reachability/__init__.py +28 -0
  53. sca_reachability/ast_analyzer.py +366 -0
  54. sca_reachability/call_graph.py +203 -0
  55. sca_reachability/contracts.py +365 -0
  56. sca_reachability/dist_import_map.py +46 -0
  57. sca_reachability/engine.py +603 -0
  58. sca_reachability/resolver.py +169 -0
  59. scanners/__init__.py +70 -0
  60. scanners/container_scanner.py +323 -0
  61. scanners/cve_index.py +521 -0
  62. scanners/iac_scanner.py +328 -0
  63. scanners/sbom.py +183 -0
  64. scanners/sca_scanner.py +667 -0
  65. scanners/secret_scanner.py +345 -0
  66. scanners/yaml_lite.py +217 -0
  67. secret_filters.py +435 -0
  68. secret_scanner.py +943 -0
  69. sqlite_delivery_repository.py +277 -0
  70. sqlite_notification_repository.py +441 -0
  71. ssrf_guard.py +276 -0
  72. staged_scanner.py +492 -0
  73. suppression_resolver.py +282 -0
  74. symbol_indexer.py +424 -0
  75. tcs/__init__.py +1 -0
  76. tcs/analysis/__init__.py +1 -0
  77. tcs/analysis/def_use.py +408 -0
  78. tcs/analysis/function_contracts.py +290 -0
  79. tcs/parallel_analyzer.py +276 -0
  80. tcs/parallel_scanner.py +630 -0
  81. tcs/treesitter_packer.py +1446 -0
  82. tcs_cli.py +2086 -0
  83. tcs_config.py +218 -0
  84. tcs_gui.py +2123 -0
  85. timecs-2.0.0.dist-info/METADATA +142 -0
  86. timecs-2.0.0.dist-info/RECORD +94 -0
  87. timecs-2.0.0.dist-info/WHEEL +5 -0
  88. timecs-2.0.0.dist-info/entry_points.txt +4 -0
  89. timecs-2.0.0.dist-info/licenses/LICENSE +201 -0
  90. timecs-2.0.0.dist-info/top_level.txt +49 -0
  91. version_matcher.py +934 -0
  92. webhook_adapter.py +495 -0
  93. webhook_client.py +483 -0
  94. webhook_formatters.py +245 -0
@@ -0,0 +1,541 @@
1
+ """
2
+ TimeCodeSecurity - Vector D Remediation Orchestrator.
3
+ Orchestrates multi-finding bottom-up sequencing, closed-loop verification,
4
+ filesystem safety validation, and atomic per-file replacement.
5
+ """
6
+
7
+ import ast
8
+ import difflib
9
+ import os
10
+ import sys
11
+ import dataclasses
12
+ from dataclasses import dataclass, field
13
+ from pathlib import Path
14
+ from typing import Any, Callable, Dict, List, Optional, Tuple
15
+
16
+ from remediation.contracts import PatchStatus, RemediationRecord
17
+ from remediation.patch_engine import RemediationEngine
18
+ from remediation.verification import roundtrip_validate
19
+
20
+ try:
21
+ from verification_engine import verify_fix, Patch, get_stable_signature
22
+ except ImportError:
23
+ verify_fix = None
24
+ Patch = None
25
+ get_stable_signature = None
26
+
27
+
28
+ @dataclass
29
+ class ProjectRemediationResult:
30
+ """Encapsulates results and metrics from a project remediation execution."""
31
+ remediation_records: List[RemediationRecord] = field(default_factory=list)
32
+ written_files: List[str] = field(default_factory=list)
33
+ stale_files: List[str] = field(default_factory=list)
34
+ unwritten_eligible_files: List[str] = field(default_factory=list)
35
+ findings_by_file: Dict[str, List[Dict[str, Any]]] = field(default_factory=dict)
36
+ remediation_candidates: List[Dict[str, Any]] = field(default_factory=list)
37
+ summary: Dict[str, Any] = field(default_factory=dict)
38
+
39
+
40
+ def format_remediation_section(
41
+ records: List[RemediationRecord],
42
+ written_files: Optional[List[str]] = None,
43
+ is_write: bool = False
44
+ ) -> str:
45
+ """
46
+ Renders structured terminal representation of remediation patches and status.
47
+ """
48
+ if not records:
49
+ return ""
50
+
51
+ written_set = set(written_files or [])
52
+ lines = [
53
+ "=" * 80,
54
+ "TIMECODESECURITY - AUTOMATED REMEDIATION (APPLIED)" if is_write else "TIMECODESECURITY - AUTOMATED REMEDIATION PREVIEW",
55
+ "=" * 80,
56
+ ]
57
+
58
+ for rec in records:
59
+ status_label = rec.patch_status.value
60
+ file_loc = f"{rec.original_file}:{rec.line_number}"
61
+
62
+ if rec.patch_status == PatchStatus.SUCCESS:
63
+ if is_write and rec.original_file not in written_set:
64
+ lines.append(f"[REMEDIATION ABORTED - TRANSACTION ROLLBACK] Finding: {rec.finding_id} | CWE: {rec.cwe}")
65
+ lines.append(f" Rule: {rec.remediation_rule.value} | Target: {file_loc}")
66
+ lines.append(f" Status: TRANSACTION_ROLLBACK (Not persisted to disk due to partial file failure)")
67
+ lines.append("-" * 80)
68
+ else:
69
+ action_tag = "APPLIED TO DISK" if rec.original_file in written_set else "VERIFIED PREVIEW"
70
+ lines.append(f"[REMEDIATION SUCCESS] Finding: {rec.finding_id} | CWE: {rec.cwe} | Action: {action_tag}")
71
+ lines.append(f" Rule: {rec.remediation_rule.value} | Target: {file_loc}")
72
+ if rec.unified_diff:
73
+ lines.append("-" * 80)
74
+ lines.append(rec.unified_diff.rstrip())
75
+ lines.append("-" * 80)
76
+ elif rec.patch_status == PatchStatus.UNSUPPORTED_CWE:
77
+ reason = rec.limitations[0] if rec.limitations else "Pattern outside automated AST rewrite rules"
78
+ lines.append(f"[REMEDIATION UNSUPPORTED] Finding: {rec.finding_id} | CWE: {rec.cwe}")
79
+ lines.append(f" File: {file_loc}")
80
+ lines.append(f" Status: UNSUPPORTED_CWE ({reason})")
81
+ lines.append("-" * 80)
82
+ elif rec.patch_status == PatchStatus.FAILED_SYNTAX:
83
+ reason = rec.limitations[0] if rec.limitations else "AST parse failed"
84
+ lines.append(f"[REMEDIATION FAILED] Finding: {rec.finding_id} | CWE: {rec.cwe}")
85
+ lines.append(f" File: {file_loc}")
86
+ lines.append(f" Status: FAILED_SYNTAX ({reason})")
87
+ lines.append("-" * 80)
88
+ elif rec.patch_status == PatchStatus.FAILED_VERIFICATION:
89
+ reason = rec.limitations[0] if rec.limitations else "Closed-loop verification failed"
90
+ lines.append(f"[REMEDIATION VERIFICATION FAILED] Finding: {rec.finding_id} | CWE: {rec.cwe}")
91
+ lines.append(f" File: {file_loc}")
92
+ lines.append(f" Status: FAILED_VERIFICATION ({reason})")
93
+ lines.append("-" * 80)
94
+
95
+ total_candidates = len(records)
96
+ if is_write:
97
+ success_count = sum(1 for r in records if r.patch_status == PatchStatus.SUCCESS and r.original_file in written_set)
98
+ else:
99
+ success_count = sum(1 for r in records if r.patch_status == PatchStatus.SUCCESS)
100
+
101
+ unsupported_count = sum(1 for r in records if r.patch_status == PatchStatus.UNSUPPORTED_CWE)
102
+ failed_count = total_candidates - success_count - unsupported_count
103
+
104
+ lines.extend([
105
+ "[REMEDIATION SUMMARY]",
106
+ f" Total Eligible Findings: {total_candidates}",
107
+ f" Successfully Verified: {success_count}",
108
+ f" Unsupported CWE: {unsupported_count}",
109
+ f" Failed / Unverified: {failed_count}",
110
+ ])
111
+ if is_write:
112
+ lines.append(f" Mode: APPLIED (--write). Wrote {len(written_set)} modified file(s).")
113
+ else:
114
+ lines.append(" Mode: PREVIEW ONLY (Dry-Run). Pass --write to apply verified changes.")
115
+ lines.append("=" * 80)
116
+
117
+ return "\n".join(lines)
118
+
119
+
120
+ def map_line_number(prev_content: str, new_content: str, old_line: int) -> int:
121
+ """
122
+ Given an original 1-based line number in prev_content, determines its
123
+ new 1-based line number in new_content after AST / text transformations.
124
+ Uses SequenceMatcher opcodes to track line shifts accurately.
125
+ """
126
+ if old_line <= 0:
127
+ return old_line
128
+
129
+ prev_lines = prev_content.splitlines(keepends=True)
130
+ new_lines = new_content.splitlines(keepends=True)
131
+ k = old_line - 1 # 0-based line index in prev_lines
132
+
133
+ matcher = difflib.SequenceMatcher(None, prev_lines, new_lines)
134
+ shift = 0
135
+ for tag, i1, i2, j1, j2 in matcher.get_opcodes():
136
+ if i2 <= k:
137
+ shift += (j2 - j1) - (i2 - i1)
138
+ elif i1 <= k < i2:
139
+ if tag == "equal":
140
+ return (j1 + (k - i1)) + 1
141
+ else:
142
+ return j1 + 1
143
+ else:
144
+ break
145
+ return old_line + shift
146
+
147
+
148
+ def refine_finding_line_number(fnd: Dict[str, Any], current_content: str) -> int:
149
+ """
150
+ Refines or verifies a finding's line_number against current AST of current_content.
151
+ If the target line already matches the sink call, retains it; otherwise searches
152
+ for the matching AST Call node.
153
+ """
154
+ line_no = int(fnd.get("line_number", 0))
155
+ if line_no <= 0:
156
+ return line_no
157
+
158
+ try:
159
+ tree = ast.parse(current_content)
160
+ except Exception:
161
+ return line_no
162
+
163
+ lines = current_content.splitlines()
164
+ snippet = fnd.get("code_snippet", "").strip()
165
+ sink_symbol = fnd.get("sink_symbol", "")
166
+ sink_attr = sink_symbol.split(".")[-1] if sink_symbol else ""
167
+
168
+ if 1 <= line_no <= len(lines):
169
+ line_text = lines[line_no - 1].strip()
170
+ if snippet and (snippet in line_text or line_text in snippet):
171
+ return line_no
172
+ if sink_attr and sink_attr in line_text:
173
+ return line_no
174
+
175
+ best_line = line_no
176
+ min_dist = float("inf")
177
+ cwe = fnd.get("cwe", "")
178
+
179
+ for node in ast.walk(tree):
180
+ if isinstance(node, ast.Call):
181
+ call_name = ""
182
+ if isinstance(node.func, ast.Name):
183
+ call_name = node.func.id
184
+ elif isinstance(node.func, ast.Attribute):
185
+ call_name = node.func.attr
186
+
187
+ matches = False
188
+ if sink_attr and call_name == sink_attr:
189
+ matches = True
190
+ elif cwe == "CWE-89" and call_name in ("execute", "executemany"):
191
+ matches = True
192
+ elif cwe == "CWE-78" and call_name in ("call", "run", "Popen", "check_output", "check_call"):
193
+ matches = True
194
+ elif cwe == "CWE-22" and call_name in ("open", "file", "read_text", "read_bytes"):
195
+ matches = True
196
+
197
+ if matches:
198
+ n_line = getattr(node, "lineno", 0)
199
+ dist = abs(n_line - line_no)
200
+ if dist < min_dist:
201
+ min_dist = dist
202
+ best_line = n_line
203
+
204
+ return best_line
205
+
206
+
207
+ def resolve_target_signature(
208
+ fnd: Dict[str, Any],
209
+ files: Dict[str, str]
210
+ ) -> Optional[Tuple]:
211
+ """
212
+ Defensively extracts the stable (source, sink, file, file, root_var) signature
213
+ for a finding across the multi-file project dictionary.
214
+
215
+ Guarantees:
216
+ - Never raises unhandled exceptions.
217
+ - Gracefully returns None if source/sink nodes cannot be resolved.
218
+ """
219
+ if get_stable_signature is None or not files:
220
+ return None
221
+
222
+ try:
223
+ from ast_scanner import TaintTracker
224
+ tracker = TaintTracker(files=files)
225
+ sources, sinks, edges = tracker.analyze()
226
+ src_map = {s.id: s for s in sources}
227
+ snk_map = {s.id: s for s in sinks}
228
+
229
+ target_file = fnd.get("file")
230
+ target_line = int(fnd.get("line_number", 0))
231
+ sink_sym = fnd.get("sink_symbol")
232
+
233
+ best_match = None
234
+ for e in edges:
235
+ if not e.target_id.startswith("SNK") or e.kind == "CLEAN":
236
+ continue
237
+ s = src_map.get(e.source_id)
238
+ sk = snk_map.get(e.target_id)
239
+ if not s or not sk:
240
+ continue
241
+
242
+ # Match target file
243
+ if target_file and sk.location.file != target_file:
244
+ continue
245
+
246
+ # Match sink symbol if known
247
+ if sink_sym and sk.symbol != sink_sym:
248
+ continue
249
+
250
+ # Match line number if known
251
+ if target_line > 0 and abs(sk.location.line_start - target_line) <= 2:
252
+ best_match = (s, sk, e)
253
+ break
254
+ elif best_match is None:
255
+ best_match = (s, sk, e)
256
+
257
+ if best_match:
258
+ s, sk, e = best_match
259
+ return get_stable_signature(s, sk, e)
260
+ except Exception:
261
+ # Defensive fallback: never crash if analysis fails or nodes are missing
262
+ pass
263
+
264
+ return None
265
+
266
+
267
+ def remediate_project(
268
+ files: Dict[str, str],
269
+ effective_findings: List[Dict[str, Any]],
270
+ target: Path,
271
+ base_dir: Path,
272
+ is_write: bool = False,
273
+ pre_write_hook: Optional[Callable[[Path], None]] = None,
274
+ ) -> ProjectRemediationResult:
275
+ """
276
+ Executes automated remediation across eligible project findings.
277
+
278
+ Guarantees:
279
+ - Same-file multi-finding sequencing: Findings in the same file are sorted
280
+ in descending line order (bottom-up) to prevent line offset corruption.
281
+ - Header import shift tracking: Adjusts remaining pending finding lines when
282
+ imports or header blocks are injected.
283
+ - Closed-loop verification: Every candidate patch is re-scanned.
284
+ - Filesystem safety: Symlinks, hardlinks (nlink > 1), and read-only files are guarded.
285
+ - Line-ending preservation: Preserves exact CRLF vs LF byte semantics without leaks.
286
+ - Transactional rollback: Partial failures in a file abort writing and report accurately.
287
+ - Stale source detection: Validates file on disk matches analyzed source prior to writing.
288
+ - Atomic replacement: Temporary sibling file writes + os.replace.
289
+ """
290
+ remediation_candidates = [f for f in effective_findings if not f.get("suppressed", False)]
291
+ remediation_records: List[RemediationRecord] = []
292
+ written_files: List[str] = []
293
+ stale_files: List[str] = []
294
+ unwritten_eligible_files: List[str] = []
295
+ roundtrip_results: Dict[str, Dict[str, Any]] = {}
296
+ findings_by_file: Dict[str, List[Dict[str, Any]]] = {}
297
+
298
+ engine = RemediationEngine()
299
+ for f in remediation_candidates:
300
+ f_file = f.get("file", "")
301
+ if f_file:
302
+ findings_by_file.setdefault(f_file, []).append(f)
303
+
304
+ for rel_file, file_fnds in findings_by_file.items():
305
+ orig_content = files.get(rel_file)
306
+ if orig_content is None:
307
+ continue
308
+
309
+ # Sort findings bottom-up (descending line order)
310
+ sorted_file_fnds = sorted(
311
+ file_fnds,
312
+ key=lambda fnd: int(fnd.get("line_number", 0)),
313
+ reverse=True
314
+ )
315
+
316
+ working_fnds = [dict(f) for f in sorted_file_fnds]
317
+ current_content = orig_content
318
+ file_success = True
319
+ per_file_records: List[RemediationRecord] = []
320
+
321
+ for idx, fnd in enumerate(working_fnds):
322
+ fnd["line_number"] = refine_finding_line_number(fnd, current_content)
323
+
324
+ rec = engine.remediate(fnd, current_content, file_path=rel_file)
325
+
326
+ # Cross-file closed-loop verification gate via verification_engine
327
+ if rec.patch_status == PatchStatus.SUCCESS and rec.verification_passed and rec.patched_source is not None:
328
+ if verify_fix is not None and Patch is not None and files:
329
+ try:
330
+ working_files = dict(files)
331
+ working_files[rel_file] = current_content
332
+ target_sig = resolve_target_signature(fnd, working_files)
333
+ if target_sig is not None:
334
+ patch_obj = Patch(
335
+ file_path=rel_file,
336
+ original_code=current_content,
337
+ new_code=rec.patched_source,
338
+ )
339
+ v_res = verify_fix(
340
+ files=working_files,
341
+ patch=patch_obj,
342
+ target_signature=target_sig,
343
+ )
344
+ if v_res.status == "REGRESSION_DETECTED":
345
+ rec = dataclasses.replace(
346
+ rec,
347
+ patch_status=PatchStatus.FAILED_VERIFICATION,
348
+ verification_passed=False,
349
+ patched_source=None,
350
+ limitations=tuple(list(rec.limitations) + [f"Cross-file regression detected: {v_res.reason}"]),
351
+ )
352
+ elif v_res.status == "NOT_VERIFIED":
353
+ rec = dataclasses.replace(
354
+ rec,
355
+ patch_status=PatchStatus.FAILED_VERIFICATION,
356
+ verification_passed=False,
357
+ patched_source=None,
358
+ limitations=tuple(list(rec.limitations) + [f"Cross-file verification failed: {v_res.reason}"]),
359
+ )
360
+ elif v_res.status == "INVALID_PATCH":
361
+ rec = dataclasses.replace(
362
+ rec,
363
+ patch_status=PatchStatus.FAILED_SYNTAX,
364
+ verification_passed=False,
365
+ patched_source=None,
366
+ limitations=tuple(list(rec.limitations) + [f"Invalid patch: {v_res.reason}"]),
367
+ )
368
+ except Exception as ve_err:
369
+ print(f"[WARN] Multi-file verification skipped: {ve_err}", file=sys.stderr)
370
+
371
+ per_file_records.append(rec)
372
+ if rec.patch_status == PatchStatus.SUCCESS and rec.verification_passed and rec.patched_source is not None:
373
+ prev_text = current_content
374
+ current_content = rec.patched_source
375
+
376
+ # Adjust line numbers of remaining pending findings for any line shifts
377
+ for rem_fnd in working_fnds[idx + 1:]:
378
+ old_l = int(rem_fnd.get("line_number", 0))
379
+ rem_fnd["line_number"] = map_line_number(prev_text, current_content, old_l)
380
+ else:
381
+ file_success = False
382
+ break
383
+
384
+ # Teacher roundtrip gate: a file counts as fixed only when the fully patched file re-scans
385
+ # with zero flaws. Each per-finding gate above proves its own target is gone; none of them
386
+ # can see a flaw the patch itself introduced, which is exactly how a "sanitized" variable
387
+ # that the sanitizer registry does not recognise used to reach disk as confirmed secure code.
388
+ if file_success and current_content != orig_content:
389
+ roundtrip_ok, roundtrip_err, roundtrip_stats = roundtrip_validate(rel_file, current_content)
390
+ roundtrip_results[rel_file] = roundtrip_stats
391
+ if not roundtrip_ok:
392
+ file_success = False
393
+ per_file_records = [
394
+ dataclasses.replace(
395
+ r,
396
+ patch_status=PatchStatus.FAILED_VERIFICATION,
397
+ verification_passed=False,
398
+ patched_source=None,
399
+ limitations=tuple(list(r.limitations) + [f"Roundtrip re-scan failed: {roundtrip_err}"])
400
+ ) if r.patch_status == PatchStatus.SUCCESS else r
401
+ for r in per_file_records
402
+ ]
403
+
404
+ if is_write and not file_success:
405
+ updated_records = []
406
+ for r in per_file_records:
407
+ if r.patch_status == PatchStatus.SUCCESS:
408
+ updated_records.append(dataclasses.replace(
409
+ r,
410
+ limitations=tuple(list(r.limitations) + ["transaction_rollback: not persisted to disk due to subsequent failure in same file"])
411
+ ))
412
+ else:
413
+ updated_records.append(r)
414
+ per_file_records = updated_records
415
+
416
+ remediation_records.extend(per_file_records)
417
+
418
+ if is_write:
419
+ if file_success and current_content != orig_content:
420
+ if target.is_file():
421
+ disk_file = target
422
+ else:
423
+ cand = (target / rel_file).resolve()
424
+ disk_file = cand if cand.exists() else (base_dir / rel_file).resolve()
425
+
426
+ if not disk_file.exists():
427
+ print(f"[WARN] Cannot write patch: target file '{disk_file}' does not exist", file=sys.stderr)
428
+ unwritten_eligible_files.append(rel_file)
429
+ else:
430
+ # Filesystem safety check 1: Disallow writing through or replacing symlinks
431
+ try:
432
+ is_sym = disk_file.is_symlink()
433
+ except Exception:
434
+ is_sym = False
435
+
436
+ if is_sym or os.environ.get("TCS_TEST_FORCE_SYMLINK") == "1":
437
+ print(f"[WARN] Aborting write for '{rel_file}': target is a symlink; in-place replacement is disallowed for safety", file=sys.stderr)
438
+ unwritten_eligible_files.append(rel_file)
439
+ continue
440
+
441
+ # Filesystem safety check 2: Stat inspection (mode, hard link count)
442
+ try:
443
+ disk_stat = disk_file.stat()
444
+ except Exception as e:
445
+ print(f"[ERROR] Failed to stat '{disk_file}': {e}", file=sys.stderr)
446
+ unwritten_eligible_files.append(rel_file)
447
+ continue
448
+
449
+ if getattr(disk_stat, "st_nlink", 1) > 1:
450
+ print(f"[WARN] Aborting write for '{rel_file}': hard-linked file (nlink={disk_stat.st_nlink}) cannot be safely replaced atomically", file=sys.stderr)
451
+ unwritten_eligible_files.append(rel_file)
452
+ continue
453
+
454
+ # Filesystem safety check 3: Read-only check
455
+ if not os.access(disk_file, os.W_OK):
456
+ print(f"[WARN] Aborting write for '{rel_file}': target file is read-only", file=sys.stderr)
457
+ unwritten_eligible_files.append(rel_file)
458
+ continue
459
+
460
+ # Test orchestration seam: invoked immediately before stale check
461
+ if pre_write_hook is not None:
462
+ try:
463
+ pre_write_hook(disk_file)
464
+ except Exception as hook_err:
465
+ print(f"[DEBUG] Pre-write hook error: {hook_err}", file=sys.stderr)
466
+ pre_write_env = os.environ.get("TCS_TEST_PRE_WRITE_MUTATE")
467
+ if pre_write_env and disk_file.exists():
468
+ disk_file.write_text(pre_write_env, encoding="utf-8")
469
+
470
+ # Stale-content detection before write
471
+ try:
472
+ disk_content = disk_file.read_text(encoding="utf-8")
473
+ except Exception as e:
474
+ print(f"[ERROR] Failed to read '{disk_file}' for stale check: {e}", file=sys.stderr)
475
+ disk_content = None
476
+
477
+ if disk_content != orig_content:
478
+ stale_files.append(rel_file)
479
+ unwritten_eligible_files.append(rel_file)
480
+ print(f"[WARN] Aborting write for '{rel_file}': file on disk was modified after analysis (stale source)", file=sys.stderr)
481
+ else:
482
+ # Detect original line ending to preserve exact byte semantics
483
+ try:
484
+ raw_bytes = disk_file.read_bytes()
485
+ is_crlf = b"\r\n" in raw_bytes
486
+ except Exception:
487
+ is_crlf = False
488
+
489
+ if is_crlf:
490
+ normalized_content = current_content.replace("\r\n", "\n").replace("\n", "\r\n")
491
+ else:
492
+ normalized_content = current_content.replace("\r\n", "\n")
493
+
494
+ tmp_file = disk_file.with_suffix(disk_file.suffix + ".tcs_tmp")
495
+ try:
496
+ tmp_file.write_text(normalized_content, encoding="utf-8", newline="")
497
+ try:
498
+ os.chmod(tmp_file, disk_stat.st_mode)
499
+ except Exception:
500
+ pass
501
+ os.replace(tmp_file, disk_file)
502
+ written_files.append(rel_file)
503
+ except Exception as e:
504
+ if tmp_file.exists():
505
+ try:
506
+ tmp_file.unlink()
507
+ except Exception:
508
+ pass
509
+ unwritten_eligible_files.append(rel_file)
510
+ print(f"[ERROR] Failed to write patch to '{disk_file}': {e}", file=sys.stderr)
511
+ elif not file_success:
512
+ unwritten_eligible_files.append(rel_file)
513
+
514
+ if is_write:
515
+ successful_remediations_count = sum(1 for r in remediation_records if r.patch_status == PatchStatus.SUCCESS and r.original_file in written_files)
516
+ else:
517
+ successful_remediations_count = sum(1 for r in remediation_records if r.patch_status == PatchStatus.SUCCESS)
518
+
519
+ summary = {
520
+ "total_eligible": len(remediation_candidates),
521
+ "successful_remediations": successful_remediations_count,
522
+ "unsupported_cwes": sum(1 for r in remediation_records if r.patch_status == PatchStatus.UNSUPPORTED_CWE),
523
+ "failed_verifications": len(remediation_candidates) - successful_remediations_count - sum(1 for r in remediation_records if r.patch_status == PatchStatus.UNSUPPORTED_CWE),
524
+ "written_files": written_files,
525
+ "stale_files": stale_files,
526
+ "unwritten_eligible_files": unwritten_eligible_files,
527
+ "is_write": is_write,
528
+ "is_dry_run": not is_write,
529
+ "roundtrip": roundtrip_results,
530
+ "roundtrip_rejected_files": sorted(f for f, s in roundtrip_results.items() if s["active_flaws"]),
531
+ }
532
+
533
+ return ProjectRemediationResult(
534
+ remediation_records=remediation_records,
535
+ written_files=written_files,
536
+ stale_files=stale_files,
537
+ unwritten_eligible_files=unwritten_eligible_files,
538
+ findings_by_file=findings_by_file,
539
+ remediation_candidates=remediation_candidates,
540
+ summary=summary,
541
+ )