-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscavenger.py
More file actions
1312 lines (1186 loc) · 48 KB
/
Copy pathscavenger.py
File metadata and controls
1312 lines (1186 loc) · 48 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
from __future__ import annotations
import argparse
import fnmatch
import hashlib
import json
import os
import shutil
import uuid
from collections import Counter
from datetime import datetime, timedelta, timezone
from importlib import metadata
from pathlib import Path
TZ = timezone(timedelta(hours=8))
TOOL_NAME = "OpenClearn"
VERSION_FILE = Path(__file__).resolve().parent / "VERSION"
def read_tool_version() -> str:
try:
return VERSION_FILE.read_text(encoding="utf-8").strip() or "v0.0.0"
except Exception:
pass
try:
return f"v{metadata.version('openclearn')}"
except Exception:
return "v0.0.0"
TOOL_VERSION = read_tool_version()
MODE_DEFAULTS = {
"safe": {
"stale_days": 14,
"trial_timeout_hours": 240,
"max_rollbacks": 20,
"media_enabled": False,
"media_delete_duplicates": False,
},
"balanced": {
"stale_days": 7,
"trial_timeout_hours": 168,
"max_rollbacks": 50,
"media_enabled": True,
"media_delete_duplicates": False,
},
"aggressive": {
"stale_days": 3,
"trial_timeout_hours": 96,
"max_rollbacks": 200,
"media_enabled": True,
"media_delete_duplicates": True,
},
}
DEFAULT_MEDIA_EXTENSIONS = [
".png",
".jpg",
".jpeg",
".webp",
".gif",
".bmp",
".mp4",
".mov",
".avi",
".mkv",
".webm",
]
DEFAULT_COLLECTOR_PATTERNS = [
"*.tmp",
"*.temp",
"*.bak",
"*.old",
"*.log",
"*.cache",
]
DEFAULT_DOC_EXTENSIONS = [
".txt",
".md",
".json",
".jsonl",
".csv",
".yaml",
".yml",
".log",
".ini",
".toml",
".pdf",
".docx",
".xlsx",
".pptx",
]
DEFAULT_CONFIG = {
"root": ".",
"mode": "safe",
"snapshot_file": "state/g5_skill_snapshots.json",
"external_specimen_file": "state/g5_external_specimens.json",
"structure_adjustment_file": "protocols/g5_structure_adjustments.json",
"state_file": "state/g5_scavenger_state.json",
"report_jsonl": "audit/g5_scavenger_reports.jsonl",
"stale_days": 14,
"trial_timeout_hours": 240,
"max_rollbacks": 20,
"protect_keywords": ["OPENSPACE-", "DO_NOT_TOUCH"],
"media_cleanup": {
"enabled": False,
"delete_duplicates": False,
"keep_strategy": "oldest",
"min_size_kb": 64,
"roots": ["scratch", "public"],
"extensions": DEFAULT_MEDIA_EXTENSIONS,
},
"collector_context": {
"persona": "careful_cleaner",
"principles": ["collect_first", "review_before_delete", "protect_core_memory"],
"allow_roots": ["scratch", "audit", "state", "public"],
"deny_roots": [".git", "protocols", "residents", "memory_store"],
"deny_patterns": ["*.key", "*.pem", "*.env", "*anchor*", "*identity*"],
"protected_files": [],
},
"collector": {
"candidate_file": "state/g5_scavenger_candidates.json",
"review_markdown": "audit/g5_scavenger_review.md",
"approve_file": "state/g5_scavenger_approve.json",
"use_trash": True,
"trash_dir": "trash/openclearn",
"stale_days": 21,
"roots": ["scratch", "audit", "state", "public"],
"include_patterns": DEFAULT_COLLECTOR_PATTERNS,
"exclude_patterns": ["state/*.json", "state/*.jsonl"],
},
"doc_cleanup": {
"enabled": False,
"roots": [],
"extensions": DEFAULT_DOC_EXTENSIONS,
"min_size_kb": 1,
"max_hash_mb": 32,
"max_text_scan_kb": 256,
},
}
BUILTIN_AGENT_PROFILES = {
"codex": {
"persona": "pragmatic_cleaner",
"extra_protect_keywords": ["OPENSPACE-", "DO_NOT_TOUCH"],
"collector_exclude_patterns": [".git/*", ".venv/*", "node_modules/*", "__pycache__/*"],
},
"claude": {
"persona": "careful_archivist",
"extra_protect_keywords": ["OPENSPACE-", "ANTHROPIC", "DO_NOT_TOUCH"],
"collector_exclude_patterns": [".git/*", ".venv/*", "node_modules/*", "__pycache__/*"],
},
"openclaw": {
"persona": "city_sanitation_guard",
"extra_protect_keywords": ["OPENSPACE-", "XIAOYU-ANCHOR", "DO_NOT_TOUCH"],
"collector_exclude_patterns": [".git/*", ".venv/*", "node_modules/*", "__pycache__/*"],
},
}
def now_dt() -> datetime:
return datetime.now(TZ)
def now_iso() -> str:
return now_dt().isoformat()
def parse_ts(value: str | None) -> datetime | None:
if not value:
return None
try:
return datetime.fromisoformat(value)
except Exception:
return None
def load_json(path: Path, default: dict | None = None) -> dict:
if not path.exists():
return default or {}
try:
return json.loads(path.read_text(encoding="utf-8-sig"))
except Exception:
return default or {}
def write_json(path: Path, payload: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def write_default_config(path: Path) -> dict:
if path.exists():
return {
"tool": TOOL_NAME,
"version": TOOL_VERSION,
"operation": "init_config",
"status": "skipped_exists",
"path": str(path.resolve()),
}
payload = dict(DEFAULT_CONFIG)
payload["root"] = str(Path.cwd().resolve())
write_json(path, payload)
return {
"tool": TOOL_NAME,
"version": TOOL_VERSION,
"operation": "init_config",
"status": "created",
"path": str(path.resolve()),
}
def append_jsonl(path: Path, payload: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a", encoding="utf-8") as fh:
fh.write(json.dumps(payload, ensure_ascii=False) + "\n")
def file_state(path: Path) -> dict:
return {
"path": str(path),
"exists": path.exists(),
"is_file": path.is_file(),
"is_dir": path.is_dir(),
}
def run_doctor(config_path: Path, config: dict, root: Path, mode: str, collector_context: dict, agent_profile: dict) -> dict:
checks: list[dict] = []
warnings: list[str] = []
errors: list[str] = []
def check(name: str, ok: bool, detail: str) -> None:
checks.append({"name": name, "ok": bool(ok), "detail": detail})
if not ok:
errors.append(f"{name}: {detail}")
check("config_file", config_path.exists(), str(config_path))
check("root_exists", root.exists(), str(root))
required_keys = [
"snapshot_file",
"external_specimen_file",
"structure_adjustment_file",
"state_file",
"report_jsonl",
]
missing = [key for key in required_keys if key not in config]
check("required_config_keys", not missing, ",".join(missing) if missing else "ok")
if mode not in MODE_DEFAULTS:
check("mode", False, f"unsupported:{mode}")
else:
check("mode", True, mode)
collector_cfg = dict(config.get("collector", {}))
candidate_file = resolve_path(root, str(collector_cfg.get("candidate_file", "state/g5_scavenger_candidates.json")))
review_markdown = resolve_path(root, str(collector_cfg.get("review_markdown", "audit/g5_scavenger_review.md")))
approve_file = resolve_path(root, str(collector_cfg.get("approve_file", "state/g5_scavenger_approve.json")))
trash_dir = resolve_path(root, str(collector_cfg.get("trash_dir", "trash/openclearn")))
for path_name, path in {
"candidate_parent": candidate_file.parent,
"review_parent": review_markdown.parent,
"approve_parent": approve_file.parent,
"trash_parent": trash_dir.parent,
}.items():
check(path_name, path.exists() or path.parent.exists(), str(path))
if not bool(collector_cfg.get("use_trash", True)):
warnings.append("collector.use_trash is false; delete mode becomes less recoverable")
allow_roots = list(collector_context.get("allow_roots", []))
deny_roots = list(collector_context.get("deny_roots", []))
if not allow_roots:
warnings.append("collector_context.allow_roots is empty; scanner scope is broad")
for allow_root in allow_roots:
if not Path(allow_root).exists():
warnings.append(f"allow_root_missing:{allow_root}")
for deny_root in deny_roots:
if Path(deny_root).exists() and any(is_subpath(Path(deny_root), Path(ar)) for ar in allow_roots):
warnings.append(f"deny_root_inside_allow_root:{deny_root}")
doc_cfg = dict(config.get("doc_cleanup", {}))
if bool(doc_cfg.get("enabled", False)):
for raw in doc_cfg.get("roots", []):
p = resolve_path(root, str(raw))
if not p.exists():
warnings.append(f"doc_root_missing:{p}")
report = {
"tool": TOOL_NAME,
"version": TOOL_VERSION,
"generated_at": now_iso(),
"operation": "doctor",
"status": "ok" if not errors else "error",
"config": file_state(config_path),
"root": file_state(root),
"mode": mode,
"agent_persona": str(agent_profile.get("persona", "cleaner")),
"checks": checks,
"warnings": warnings,
"errors": errors,
}
return report
def estimate_json_rows_bytes(rows: list[dict]) -> int:
total = 0
for row in rows:
try:
total += len(json.dumps(row, ensure_ascii=False).encode("utf-8"))
except Exception:
continue
return total
def choose_better_snapshot(a: dict, b: dict) -> dict:
a_key = (
int(a.get("refresh_count", 0)),
int(a.get("strength_score", 0)),
str(a.get("last_refreshed_at", "")),
)
b_key = (
int(b.get("refresh_count", 0)),
int(b.get("strength_score", 0)),
str(b.get("last_refreshed_at", "")),
)
return a if a_key >= b_key else b
def dedupe_snapshots(snapshots: list[dict]) -> tuple[list[dict], list[dict]]:
by_key: dict[tuple[str, str], dict] = {}
removed: list[dict] = []
for row in snapshots:
key = (str(row.get("line_id", "")), str(row.get("capability_signature", "")))
existing = by_key.get(key)
if not existing:
by_key[key] = row
continue
keep = choose_better_snapshot(existing, row)
drop = row if keep is existing else existing
by_key[key] = keep
removed.append(drop)
return list(by_key.values()), removed
def reap_misc_residue(snapshots: list[dict], stale_days: int) -> tuple[list[dict], list[dict]]:
cutoff = now_dt() - timedelta(days=stale_days)
kept: list[dict] = []
removed: list[dict] = []
for row in snapshots:
if row.get("line_id") != "misc_line":
kept.append(row)
continue
refreshed = parse_ts(str(row.get("last_refreshed_at", "")))
refresh_count = int(row.get("refresh_count", 0))
stale = refreshed is None or refreshed < cutoff
if stale and refresh_count <= 1:
removed.append(row)
else:
kept.append(row)
return kept, removed
def dedupe_external_samples(samples: list[dict]) -> tuple[list[dict], list[dict]]:
by_id: dict[str, dict] = {}
removed: list[dict] = []
for row in samples:
sid = str(row.get("sample_id", ""))
if sid not in by_id:
by_id[sid] = row
continue
prev = by_id[sid]
prev_ts = str(prev.get("discovered_at", ""))
cur_ts = str(row.get("discovered_at", ""))
if cur_ts > prev_ts:
by_id[sid] = row
removed.append(prev)
else:
if len(str(row.get("tool_purpose", ""))) > len(str(prev.get("tool_purpose", ""))):
by_id[sid] = row
removed.append(prev)
else:
removed.append(row)
merged = list(by_id.values())
merged.sort(key=lambda x: str(x.get("sample_id", "")))
return merged, removed
def hash_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as fh:
while True:
block = fh.read(1024 * 1024)
if not block:
break
digest.update(block)
return digest.hexdigest()
def looks_garbled_text(text: str) -> bool:
t = str(text or "").strip()
if not t:
return False
if "\ufffd" in t:
return True
if t.count("??") >= 2:
return True
allowed = 0
total = 0
for ch in t:
total += 1
if (
ch.isascii() and (ch.isalnum() or ch.isspace() or ch in ",.:;!?/()[]{}-_+'\"")
) or ("\u4e00" <= ch <= "\u9fff"):
allowed += 1
ratio = allowed / max(1, total)
return ratio < 0.8
def scan_document_candidates(
root: Path,
doc_cfg: dict,
exclude_patterns: list[str],
context: dict,
) -> tuple[list[dict], list[dict], int]:
if not bool(doc_cfg.get("enabled", False)):
return [], [], 0
roots = [resolve_path(root, str(x)) for x in doc_cfg.get("roots", ["Desktop", "Documents"])]
exts = [str(x).lower() for x in doc_cfg.get("extensions", DEFAULT_DOC_EXTENSIONS)]
ext_set = {e if e.startswith(".") else f".{e}" for e in exts}
min_size_bytes = int(doc_cfg.get("min_size_kb", 1)) * 1024
max_hash_bytes = int(doc_cfg.get("max_hash_mb", 32)) * 1024 * 1024
max_text_scan_bytes = int(doc_cfg.get("max_text_scan_kb", 256)) * 1024
text_exts = {".txt", ".md", ".json", ".jsonl", ".csv", ".yaml", ".yml", ".log", ".ini", ".toml"}
garbled_candidates: list[dict] = []
dup_candidates: list[dict] = []
by_size: dict[int, list[Path]] = {}
for scan_root in roots:
if not scan_root.exists():
continue
for dirpath, _, filenames in os.walk(scan_root):
for name in filenames:
p = Path(dirpath) / name
if p.suffix.lower() not in ext_set:
continue
rel = safe_relative(p, root)
if should_exclude_path(rel, exclude_patterns):
continue
allowed, reason = context_allows_file(p, root, context)
if not allowed:
continue
try:
st = p.stat()
except Exception:
continue
size = int(st.st_size)
if size < min_size_bytes:
continue
by_size.setdefault(size, []).append(p)
if p.suffix.lower() in text_exts:
try:
with p.open("rb") as fh:
raw = fh.read(max_text_scan_bytes)
text = raw.decode("utf-8", errors="replace")
except Exception:
continue
if looks_garbled_text(text):
garbled_candidates.append(
{
"candidate_id": f"garbled-{uuid.uuid4().hex[:12]}",
"kind": "garbled_document",
"path": str(p.resolve()),
"relative_path": rel,
"size_bytes": size,
"reason": "garbled_text_pattern",
"source": "doc_scan",
"context_reason": reason,
}
)
for size, files in by_size.items():
if len(files) < 2:
continue
# keep hashing bounded for very large files
hashable = [p for p in files if p.exists() and p.stat().st_size <= max_hash_bytes]
if len(hashable) < 2:
continue
by_hash: dict[str, list[Path]] = {}
for p in hashable:
try:
h = hash_file(p)
except Exception:
continue
by_hash.setdefault(h, []).append(p)
for _, same in by_hash.items():
if len(same) < 2:
continue
ordered = sorted(same, key=lambda x: x.stat().st_mtime)
keep = ordered[0]
group_id = f"dupdoc-{uuid.uuid4().hex[:10]}"
for p in ordered[1:]:
allowed, reason = context_allows_file(p, root, context)
if not allowed:
continue
dup_candidates.append(
{
"candidate_id": f"{group_id}-{uuid.uuid4().hex[:8]}",
"kind": "exact_duplicate_document",
"path": str(p.resolve()),
"relative_path": safe_relative(p, root),
"size_bytes": size,
"reason": f"duplicate_of:{keep}",
"source": "doc_scan",
"context_reason": reason,
}
)
reclaimable = sum(int(c.get("size_bytes", 0)) for c in dup_candidates)
return garbled_candidates, dup_candidates, reclaimable
def scan_media_duplicates(
roots: list[Path],
extensions: list[str],
min_size_bytes: int,
keep_strategy: str,
) -> tuple[list[dict], int]:
ext_set = {e.lower() if e.startswith(".") else f".{e.lower()}" for e in extensions}
by_size: dict[int, list[Path]] = {}
for root in roots:
if not root.exists():
continue
for dirpath, _, filenames in os.walk(root):
for name in filenames:
p = Path(dirpath) / name
if p.suffix.lower() not in ext_set:
continue
try:
size = p.stat().st_size
except Exception:
continue
if size < min_size_bytes:
continue
by_size.setdefault(size, []).append(p)
groups: list[dict] = []
reclaimable = 0
for size, files in by_size.items():
if len(files) < 2:
continue
by_hash: dict[str, list[Path]] = {}
for p in files:
try:
h = hash_file(p)
except Exception:
continue
by_hash.setdefault(h, []).append(p)
for h, same in by_hash.items():
if len(same) < 2:
continue
ordered = sorted(same, key=lambda x: x.stat().st_mtime)
if keep_strategy == "newest":
ordered = list(reversed(ordered))
keep = ordered[0]
delete = ordered[1:]
reclaim = size * len(delete)
reclaimable += reclaim
groups.append(
{
"hash": h,
"size_bytes": size,
"keep": str(keep),
"delete": [str(x) for x in delete],
"count": len(same),
"reclaimable_bytes": reclaim,
}
)
groups.sort(key=lambda g: int(g.get("reclaimable_bytes", 0)), reverse=True)
return groups, reclaimable
def delete_media_duplicates(groups: list[dict]) -> tuple[int, int]:
deleted_files = 0
deleted_bytes = 0
for g in groups:
size = int(g.get("size_bytes", 0))
for raw in g.get("delete", []):
p = Path(str(raw))
if not p.exists():
continue
try:
p.unlink()
deleted_files += 1
deleted_bytes += size
except Exception:
continue
return deleted_files, deleted_bytes
def rollback_stale_trials_guarded(
records: list[dict],
trial_timeout_hours: int,
max_rollbacks: int,
protect_keywords: list[str],
) -> tuple[list[dict], list[dict]]:
changed: list[dict] = []
now = now_dt()
cutoff = now - timedelta(hours=trial_timeout_hours)
for row in records:
if len(changed) >= max_rollbacks:
break
if str(row.get("status", "")) != "active_trial":
continue
marker = " ".join(
[
str(row.get("adjustment_id", "")),
str(row.get("source_round", "")),
str(row.get("reason", "")),
]
).lower()
if any(k.lower() in marker for k in protect_keywords):
continue
trial_end = parse_ts(str(row.get("trial_end", "")))
ts = parse_ts(str(row.get("timestamp", "")))
stale = False
if trial_end:
stale = trial_end <= now
elif ts:
stale = ts <= cutoff
if not stale:
continue
row["status"] = "rolled_back"
row["evaluation_result"] = "scavenger_timeout_rollback"
row["rollback_reason"] = "scavenger_stale_active_trial_timeout"
row["updated_at"] = now_iso()
changed.append(
{
"adjustment_id": row.get("adjustment_id"),
"source_round": row.get("source_round"),
"previous_status": "active_trial",
"new_status": "rolled_back",
}
)
return records, changed
def pick_value(cli_value: int | None, mode_value: int, config_value: int | None) -> int:
if cli_value is not None:
return cli_value
if config_value is not None:
return config_value
return mode_value
def resolve_path(root: Path, raw: str) -> Path:
if str(raw).startswith(("C:", "D:", "/", "\\")):
return Path(str(raw)).resolve()
return (root / str(raw)).resolve()
def safe_relative(path: Path, root: Path) -> str:
try:
return str(path.resolve().relative_to(root.resolve()))
except Exception:
return str(path.resolve())
def is_subpath(path: Path, root: Path) -> bool:
try:
path.resolve().relative_to(root.resolve())
return True
except Exception:
return False
def load_agent_profile(profile_name: str, profile_path: Path | None) -> dict:
profile = dict(BUILTIN_AGENT_PROFILES.get(profile_name.lower(), {}))
if profile_path and profile_path.exists():
external = load_json(profile_path, {})
if isinstance(external, dict):
profile.update(external)
return profile
def load_api_binding(config: dict, cli_provider: str | None, cli_key_env: str | None) -> dict:
llm_cfg = config.get("llm_binding", {}) if isinstance(config, dict) else {}
provider = str(cli_provider or llm_cfg.get("provider", "none")).lower()
key_env = str(cli_key_env or llm_cfg.get("api_key_env", "")).strip()
key_present = bool(key_env and os.getenv(key_env))
return {
"provider": provider,
"api_key_env": key_env or None,
"api_key_loaded": key_present,
}
def load_collector_context(config: dict, root: Path) -> dict:
raw = config.get("collector_context", {}) if isinstance(config, dict) else {}
allow_roots = [resolve_path(root, str(x)) for x in raw.get("allow_roots", ["scratch", "audit", "state", "trash"])]
deny_roots = [resolve_path(root, str(x)) for x in raw.get("deny_roots", [".git", "protocols", "residents", "memory_store"])]
deny_patterns = [str(x) for x in raw.get("deny_patterns", ["*.key", "*.pem", "*.env", "*anchor*", "*identity*"])]
protected_files = [str(resolve_path(root, str(x))) for x in raw.get("protected_files", [])]
persona = str(raw.get("persona", "cleaner"))
principles = [str(x) for x in raw.get("principles", ["collect_first", "review_before_delete", "protect_core_memory"])]
return {
"allow_roots": allow_roots,
"deny_roots": deny_roots,
"deny_patterns": deny_patterns,
"protected_files": protected_files,
"persona": persona,
"principles": principles,
}
def context_allows_file(path: Path, root: Path, context: dict) -> tuple[bool, str]:
rp = path.resolve()
rel = safe_relative(rp, root).replace("\\", "/")
if str(rp) in set(context.get("protected_files", [])):
return False, "protected_file"
for denied in context.get("deny_roots", []):
if is_subpath(rp, denied):
return False, f"deny_root:{safe_relative(denied, root)}"
if context.get("allow_roots"):
if not any(is_subpath(rp, ar) for ar in context.get("allow_roots", [])):
return False, "outside_allow_roots"
for pattern in context.get("deny_patterns", []):
p = str(pattern).replace("\\", "/")
if fnmatch.fnmatch(rel, p) or fnmatch.fnmatch(path.name.lower(), str(pattern).lower()):
return False, f"deny_pattern:{pattern}"
return True, "allowed"
def should_exclude_path(rel_path: str, exclude_patterns: list[str]) -> bool:
rel = rel_path.replace("\\", "/")
for pattern in exclude_patterns:
p = str(pattern).replace("\\", "/")
if fnmatch.fnmatch(rel, p):
return True
return False
def scan_stale_files(
root: Path,
collector_cfg: dict,
exclude_patterns: list[str],
context: dict,
) -> list[dict]:
candidates: list[dict] = []
stale_days = int(collector_cfg.get("stale_days", 21))
include_patterns = list(collector_cfg.get("include_patterns", DEFAULT_COLLECTOR_PATTERNS))
roots = [resolve_path(root, str(x)) for x in collector_cfg.get("roots", ["scratch", "audit", "state"])]
cutoff = now_dt() - timedelta(days=stale_days)
for scan_root in roots:
if not scan_root.exists():
continue
for dirpath, _, filenames in os.walk(scan_root):
for name in filenames:
file_path = Path(dirpath) / name
rel = safe_relative(file_path, root)
if should_exclude_path(rel, exclude_patterns):
continue
allowed, reason = context_allows_file(file_path, root, context)
if not allowed:
continue
if include_patterns and not any(fnmatch.fnmatch(name.lower(), p.lower()) for p in include_patterns):
continue
try:
st = file_path.stat()
except Exception:
continue
modified = datetime.fromtimestamp(st.st_mtime, tz=TZ)
if modified >= cutoff:
continue
age_days = (now_dt() - modified).days
candidates.append(
{
"candidate_id": f"stale-{uuid.uuid4().hex[:12]}",
"kind": "stale_artifact",
"path": str(file_path.resolve()),
"relative_path": rel,
"size_bytes": int(st.st_size),
"age_days": age_days,
"reason": f"older_than_{stale_days}d",
"source": "collector",
"context_reason": reason,
}
)
return candidates
def media_groups_to_candidates(groups: list[dict], root: Path, context: dict) -> list[dict]:
candidates: list[dict] = []
for g in groups:
group_id = f"dup-{uuid.uuid4().hex[:12]}"
size = int(g.get("size_bytes", 0))
keep = Path(str(g.get("keep", ""))).resolve()
for raw in g.get("delete", []):
p = Path(str(raw)).resolve()
allowed, reason = context_allows_file(p, root, context)
if not allowed:
continue
candidates.append(
{
"candidate_id": f"{group_id}-{uuid.uuid4().hex[:8]}",
"kind": "exact_duplicate_media",
"path": str(p),
"relative_path": safe_relative(p, root),
"size_bytes": size,
"reason": f"duplicate_of:{keep}",
"group_id": group_id,
"source": "media_duplicate_scan",
"context_reason": reason,
}
)
return candidates
def write_review_markdown(path: Path, bundle: dict, max_items: int = 200) -> None:
candidates = bundle.get("candidates", [])
lines: list[str] = []
lines.append("# OpenClearn Review Report")
lines.append("")
lines.append(f"- generated_at: `{bundle.get('generated_at')}`")
lines.append(f"- root: `{bundle.get('root')}`")
lines.append(f"- agent_profile: `{bundle.get('agent_profile')}` / persona: `{bundle.get('agent_persona')}`")
lines.append(f"- candidate_count: `{len(candidates)}`")
lines.append(f"- estimated_reclaim_mb: `{bundle.get('estimated_reclaim_bytes', 0) / 1024 / 1024:.2f}`")
lines.append("")
# Kind summary
kind_counts: Counter = Counter(str(c.get("kind", "unknown")) for c in candidates)
kind_bytes: dict[str, int] = {}
for c in candidates:
k = str(c.get("kind", "unknown"))
kind_bytes[k] = kind_bytes.get(k, 0) + int(c.get("size_bytes", 0))
lines.append("## Summary by Kind")
lines.append("")
lines.append("| Kind | Count | Reclaim |")
lines.append("|------|-------|---------|")
for kind, count in kind_counts.most_common():
mb = kind_bytes.get(kind, 0) / 1024 / 1024
lines.append(f"| `{kind}` | {count} | {mb:.2f} MB |")
lines.append("")
# Age distribution for stale artifacts
stale = [c for c in candidates if c.get("kind") == "stale_artifact" and "age_days" in c]
if stale:
buckets = {"<7d": 0, "7-30d": 0, "30-90d": 0, ">90d": 0}
for c in stale:
age = int(c.get("age_days", 0))
if age < 7:
buckets["<7d"] += 1
elif age < 30:
buckets["7-30d"] += 1
elif age < 90:
buckets["30-90d"] += 1
else:
buckets[">90d"] += 1
lines.append("## Stale File Age Distribution")
lines.append("")
for bucket, cnt in buckets.items():
if cnt:
lines.append(f"- `{bucket}`: {cnt} files")
lines.append("")
lines.append(f"## Top Candidates (showing {min(max_items, len(candidates))} of {len(candidates)})")
lines.append("")
for c in candidates[:max_items]:
age_note = f" | age={c['age_days']}d" if "age_days" in c else ""
lines.append(
f"- `{c.get('candidate_id')}` | `{c.get('kind')}`"
f" | `{c.get('size_bytes', 0):,}` bytes{age_note}"
f" | `{c.get('relative_path')}`"
)
if len(candidates) > max_items:
lines.append(f"\n_...{len(candidates) - max_items} more candidates not shown. See candidates JSON._")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
def load_approval_set(path: Path) -> tuple[set[str], set[str]]:
payload = load_json(path, {})
approved_ids = {str(x) for x in payload.get("approve_candidate_ids", []) if str(x)}
approved_paths = {str(Path(str(x)).resolve()) for x in payload.get("approve_paths", []) if str(x)}
return approved_ids, approved_paths
def move_to_trash(path: Path, trash_root: Path) -> Path:
trash_root.mkdir(parents=True, exist_ok=True)
target = trash_root / f"{path.name}.{uuid.uuid4().hex[:8]}.trash"
shutil.move(str(path), str(target))
return target
def apply_collector_deletion(
bundle: dict,
root: Path,
approve_file: Path,
trash_enabled: bool,
trash_dir: Path,
hard_delete: bool,
) -> dict:
approved_ids, approved_paths = load_approval_set(approve_file)
deleted: list[dict] = []
skipped: list[dict] = []
deleted_bytes = 0
for c in bundle.get("candidates", []):
cid = str(c.get("candidate_id", ""))
raw_path = str(c.get("path", ""))
p = Path(raw_path).resolve()
allowed = cid in approved_ids or str(p) in approved_paths
if not allowed:
skipped.append({"candidate_id": cid, "path": str(p), "reason": "not_approved"})
continue
if not is_subpath(p, root):
skipped.append({"candidate_id": cid, "path": str(p), "reason": "outside_root"})
continue
if not p.exists():
skipped.append({"candidate_id": cid, "path": str(p), "reason": "missing"})
continue
size = int(c.get("size_bytes", 0))
try:
if hard_delete and not trash_enabled:
p.unlink()
deleted.append({"candidate_id": cid, "path": str(p), "action": "hard_delete"})
else:
moved = move_to_trash(p, trash_dir)
deleted.append({"candidate_id": cid, "path": str(p), "action": "move_to_trash", "trash_path": str(moved)})
deleted_bytes += size
except Exception as exc:
skipped.append({"candidate_id": cid, "path": str(p), "reason": f"delete_error:{exc.__class__.__name__}"})
return {
"approved_candidate_ids": len(approved_ids),
"approved_paths": len(approved_paths),
"deleted_count": len(deleted),
"deleted_bytes": deleted_bytes,
"deleted": deleted,
"skipped_count": len(skipped),
"skipped": skipped[:200],
}
def build_cleanup_state(
mode: str,
dry_run: bool,
stale_days: int,
trial_timeout_hours: int,
max_rollbacks: int,
protect_keywords: list[str],
media_enabled: bool,
media_delete: bool,
media_keep: str,
media_roots: list[Path],
removed_dup_snap: list[dict],
removed_misc: list[dict],
removed_dup_samples: list[dict],
rolled_back: list[dict],
media_groups: list[dict],
media_reclaimable: int,
media_deleted_files: int,
media_deleted_bytes: int,
snapshots_2: list[dict],
api_binding: dict,
agent_profile_name: str,
persona: str,
) -> dict:
return {
"version": TOOL_VERSION,
"updated_at": now_iso(),
"status": "completed",
"operation": "cleanup",
"mode": mode,
"dry_run": dry_run,
"agent_profile": agent_profile_name,
"agent_persona": persona,
"llm_binding": api_binding,
"metrics": {
"removed_duplicate_snapshots": len(removed_dup_snap),
"removed_misc_residue": len(removed_misc),
"removed_duplicate_samples": len(removed_dup_samples),
"rolled_back_stale_trials": len(rolled_back),
"media_duplicate_groups": len(media_groups),
"media_reclaimable_bytes": media_reclaimable,
"media_deleted_files": media_deleted_files,
"media_deleted_bytes": media_deleted_bytes,
"estimated_reclaim_bytes": (
estimate_json_rows_bytes(removed_dup_snap)
+ estimate_json_rows_bytes(removed_misc)
+ estimate_json_rows_bytes(removed_dup_samples)
+ media_reclaimable
),
},