-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc.py
More file actions
2215 lines (1999 loc) · 87.1 KB
/
Copy pathcc.py
File metadata and controls
2215 lines (1999 loc) · 87.1 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
"""Thin wrapper around Claude Code SDK. One session, streaming.
Channel-agnostic: caller passes the channel-specific state file, source prompt,
and MCP servers. This lets the TG bot and the WeChat bot share one class while
keeping session state and exposed tools isolated per channel.
"""
import asyncio
import copy
import hashlib
import json
import logging
import os
import subprocess
import time
from contextlib import suppress
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, AsyncIterator, Callable, Coroutine, Literal
from claude_agent_sdk import (
AssistantMessage,
ClaudeAgentOptions,
ClaudeSDKClient,
ResultMessage,
TextBlock,
ToolResultBlock,
ToolUseBlock,
UserMessage,
)
from blocking_review import (
blocking_review_max_rounds,
build_repair_prompt,
run_blocking_review,
unresolved_review_message,
)
from claude_agent_sdk.types import (
PermissionResultAllow,
StreamEvent,
ToolPermissionContext,
)
try:
from claude_agent_sdk.types import PermissionResultDeny
except ImportError: # pragma: no cover - old SDK compatibility
PermissionResultDeny = None
log = logging.getLogger(__name__)
# (tool_name, tool_input, text_chunk, tool_result) — exactly one non-None.
# tool_result = {"is_error": bool, "text": str} so bot can surface real errors
# instead of letting CC hallucinate high-level reasons ("系统限制了...").
# text_chunk is a *delta* (not a snapshot) — bot must accumulate it. Driven by
# StreamEvent.content_block_delta.text_delta (CLI --include-partial-messages).
StreamCB = Callable[
[str | None, dict | None, str | None, dict | None],
Coroutine[Any, Any, None],
]
from constants import (
HOOKS_DIR as _HOOKS_DIR,
INSTANCE,
INSTANCE_LABELS,
PROJECT,
SKILL_HOOKS_DIR as _SKILL_HOOKS_DIR,
STATE_DIR as _STATE_DIR,
)
from skill_evolve_nudge import notify_skill_evolve_turn
from turn_audit import (
TurnAudit,
begin_turn,
finish_turn,
should_block_for_permission,
summarize_tool_use,
)
VENV_PYTHON = str(Path(__file__).parent / ".venv" / "bin" / "python")
_CC_PROJECTS = Path.home() / ".claude" / "projects" / str(Path.home()).replace("/", "-")
def _find_jsonl_any_bucket(sid: str) -> Path | None:
"""Locate <sid>.jsonl across all ~/.claude/projects/<cwd-hash>/ buckets.
babata 默认 cwd=$HOME, 单 bucket 看到的只是 V 在 $HOME 跑 claude 那批 session.
V 在 ~/code/foo 跑 claude 的 session 落在 -Users-admin-code-foo/ 别的 bucket,
list_recent_sessions(scan_all_buckets=True) 会列出来; 但实际 resume / 读 turn
时还得跨 bucket 找文件 — 这里就做这个查找.
Read-only — 不动文件. resume 路径要把文件 copy 到 babata bucket 才能让 SDK
--resume 找到 (CLI cwd-bound), 走 _import_jsonl_to_bucket.
"""
target = _CC_PROJECTS / f"{sid}.jsonl"
if target.is_file():
return target
projects_root = _CC_PROJECTS.parent
try:
for bucket in projects_root.iterdir():
if not bucket.is_dir() or bucket == _CC_PROJECTS:
continue
candidate = bucket / f"{sid}.jsonl"
if candidate.is_file():
return candidate
except Exception:
pass
return None
def _import_jsonl_to_bucket(source_sid: str) -> str | None:
"""Resolve <source_sid>.jsonl to a sid that lives in babata's bucket.
源文件已在 babata bucket → 直接返回 source_sid (no copy, no fork).
源文件在别的 cwd-bucket (V 在终端别处开的原生 CC) → fork: 用 *新 uuid*
复制到 babata bucket, 返回新 sid. 关键设计:
- 新 uuid 避免 sid 冲突: 原 sid 在源 bucket 不动, V 在原 cwd 终端继续
resume 同一个 sid 仍能续上, 跟 babata 这边的 fork 物理隔离 不会
silent diverge (Codex A-FORK).
- SDK --resume <new_sid> 能找到 (CLI cwd-bound, 文件在 babata bucket).
并发安全:
- per-source-sid flock 防多 babata 实例同时 import 写花文件 (Codex C-CONCURRENCY)
- 写 .tmp + os.replace 原子化, 中途 crash 不留半文件
- 末行 JSON 校验 — 源文件可能正被终端 session 追加 (Codex H-RACE),
最后一行不完整就 trim 掉
"""
own = _CC_PROJECTS / f"{source_sid}.jsonl"
if own.is_file():
return source_sid
src = _find_jsonl_any_bucket(source_sid)
if src is None:
return None
import fcntl
import shutil
import uuid
new_sid = str(uuid.uuid4())
new_target = _CC_PROJECTS / f"{new_sid}.jsonl"
tmp_path = _CC_PROJECTS / f".{new_sid}.jsonl.tmp"
lock_path = _CC_PROJECTS / f".import-{source_sid}.lock"
lock_f = None
try:
_CC_PROJECTS.mkdir(parents=True, exist_ok=True)
lock_f = open(lock_path, "w")
fcntl.flock(lock_f.fileno(), fcntl.LOCK_EX)
# Re-check after acquiring lock (another babata instance may have just
# finished an unrelated import — irrelevant since we use a fresh uuid,
# but cheap belt-and-suspenders).
if own.is_file():
return source_sid
shutil.copy2(src, tmp_path)
# 末行 JSON 校验. shutil.copy2 是单次 read+write — 若源 bucket 终端
# session 正 append, 末行可能截断. 不能 parse 就 trim.
try:
content = tmp_path.read_bytes()
if content and not content.endswith(b"\n"):
# Last line not newline-terminated; treat as in-progress write.
last_nl = content.rfind(b"\n")
if last_nl >= 0:
tmp_path.write_bytes(content[: last_nl + 1])
else:
# Single incomplete line — drop to empty rather than corrupt.
tmp_path.write_bytes(b"")
else:
# Validate last line parses as JSON; if not, trim it.
lines = content.splitlines()
if lines:
try:
json.loads(lines[-1])
except Exception:
tmp_path.write_bytes(b"\n".join(lines[:-1]) + (b"\n" if lines[:-1] else b""))
except Exception:
pass
with open(tmp_path, "rb") as t:
os.fsync(t.fileno())
os.replace(tmp_path, new_target)
return new_sid
except Exception as e:
log.warning("import jsonl %s from %s failed: %s", source_sid, src.parent.name, e)
with suppress(Exception):
tmp_path.unlink()
return None
finally:
if lock_f is not None:
with suppress(Exception):
fcntl.flock(lock_f.fileno(), fcntl.LOCK_UN)
with suppress(Exception):
lock_f.close()
with suppress(Exception):
lock_path.unlink()
# 默认隔离 — 不读用户 ~/.claude/{settings.json, CLAUDE.md, skills/}, 不动 OAuth keychain.
# 用户日常 CC 完全无感. 开源用户走这条 (.env 必须有 ANTHROPIC_API_KEY).
# V 私人 .env 设 BABATA_SHARED_CC=1 → 共享用户 CC 全套 (skill / settings / OAuth), 跟之前体验一致.
_SETTING_SOURCES: list[str] = ["user"] if os.environ.get("BABATA_SHARED_CC") == "1" else []
# 默认信任边界 — babata 容器化 default: cwd 限定 repo 自身 (不让 babata access 用户整个家),
# permission_mode 走 "default" (重要 tool call 提示授权, 不静默 bypass).
# V 私人 .env 设 BABATA_FULL_TRUST=1 → cwd=~/ + auto mode (官方支持, status "auto mode on").
_FULL_TRUST = os.environ.get("BABATA_FULL_TRUST") == "1"
_DEFAULT_CWD = str(Path.home()) if _FULL_TRUST else str(Path(__file__).parent)
_PERMISSION_MODE = "auto" if _FULL_TRUST else "default"
# _STATE_DIR: all channel state files land here. Cross-channel session picker
# scans this dir, so it's a soft coupling — cc.py isn't fully channel-agnostic
# anymore, but /resume can list sessions from every channel (TG / WX / terminal
# bb) in one picker, sorted by bucket mtime with non-local channels tagged.
# _SKILL_HOOKS_DIR: SDK doesn't fire settings.json SessionEnd/SessionStart
# hooks (those are CC CLI-native), so babata fires them explicitly on
# reset()/init so TG/WeChat channels can plug into skill evolution (v3).
def _channel_label_from_state_file(fp: Path) -> str:
"""state file name → human-readable channel label for the /resume picker.
Labels come from constants.INSTANCE_LABELS (single source of truth). Stem
format: ``<PROJECT>-session.json`` for main instance, ``<PROJECT>-<inst>-
session.json`` for named instances. Unknown instances fall back to the raw
suffix so new bots show up with a best-effort label instead of disappearing.
Example (PROJECT=babata, default map):
babata-session.json → "巴巴塔"
babata-vvv-session.json → "巴巴塔2"
babata-vvvv-session.json → "巴巴塔3"
babata-weixin-session.json → "wx"
"""
core = fp.stem # strip .json
if core.endswith("-session"):
core = core[: -len("-session")]
if core == PROJECT:
return INSTANCE_LABELS.get("", PROJECT)
if core.startswith(f"{PROJECT}-"):
suffix = core[len(f"{PROJECT}-"):]
return INSTANCE_LABELS.get(suffix, suffix)
return core or "unknown"
_SUMMARY_CACHE_FILE = _STATE_DIR / "session-summaries.json"
# Summary subprocess 的专用 CWD. claude -p 会按 CWD 把 session jsonl 写到对应
# bucket (~/.claude/projects/<cwd-hash>/); 如果 summary 进程跟 babata 日常用同
# 一个 CWD ($HOME), 它产生的 "用一句话总结..." session 会污染主 bucket, 反过来
# 被 list_recent_sessions 扫到显示给 V. 用专用 sandbox CWD 物理隔离.
_SUMMARY_SANDBOX = _STATE_DIR / "summary-sandbox"
def _load_summary_cache() -> dict:
try:
return json.loads(_SUMMARY_CACHE_FILE.read_text())
except Exception:
return {}
def _save_summary_cache(cache: dict) -> None:
"""原子写 (tmp + rename) 避免跟并发 generator 互踩."""
try:
_SUMMARY_CACHE_FILE.parent.mkdir(parents=True, exist_ok=True)
tmp = _SUMMARY_CACHE_FILE.with_suffix(".json.tmp")
tmp.write_text(json.dumps(cache, ensure_ascii=False))
tmp.replace(_SUMMARY_CACHE_FILE)
except Exception as e:
log.warning("Failed to persist summary cache: %s", e)
def _extract_session_text_for_summary(jsonl_path: Path, max_chars: int = 6000) -> str:
"""把 session jsonl 里 user + assistant 真实文本拼成给 claude -p 总结的输入.
max_chars 控制上限, 避免长会话一次塞爆 (haiku 便宜但仍是成本). 优先拿开头
+ 结尾 —— 开头锚定主题, 结尾反映当前状态. 中间大量 tool-call / context dump
对"一句话总结"没增量.
"""
lines = []
try:
for raw in jsonl_path.read_text().splitlines():
try:
d = json.loads(raw)
except Exception:
continue
if d.get("type") not in ("user", "assistant"):
continue
msg = d.get("message")
if not isinstance(msg, dict):
continue
text = _extract_text(msg.get("content"))
if not text or _is_synthetic_user_text(text):
continue
role = d.get("type")
lines.append(f"[{role}] {text[:500]}")
except Exception:
return ""
if not lines:
return ""
joined = "\n".join(lines)
if len(joined) <= max_chars:
return joined
# 头 60% + 尾 40%
head_n = int(max_chars * 0.6)
tail_n = max_chars - head_n
return joined[:head_n] + "\n...[省略中段]...\n" + joined[-tail_n:]
def _spawn_summary_generation(sid: str, source_mtime: float) -> None:
"""Fire-and-forget: 后台线程跑 claude -p haiku 生成 session 一句话总结, 写缓存.
调用方不等结果 — 本次 /resume 仍用 first_user fallback, 下次 /resume 命中缓存.
Haiku 一句话总结典型 1-3 秒, 10 session 并发大约 3-5 秒写完缓存.
"""
jsonl = _find_jsonl_any_bucket(sid)
if jsonl is None:
return
def worker() -> None:
import subprocess
try:
text = _extract_session_text_for_summary(jsonl)
if not text:
return
prompt = (
"用一句话 (不超过 20 个中文字) 总结下面这段 CC session 的核心主题, "
"让用户一眼认出是哪个对话. 只输出一句话, 不加任何前缀 / 引号 / 解释.\n\n"
+ text
)
cli = os.environ.get("CLAUDE_CLI_PATH") or "claude"
# Model 不写死, 跟随 ~/.claude/settings.json 全局默认 (babata 哲学:
# 模型会随 CC 升级, 代码里写死 'haiku' 将来可能指向弃用 tier).
# 若 V 觉得总结任务用 opus 太慢, 改 settings.json 或加 env override.
#
# CWD 用 _SUMMARY_SANDBOX 而不是 $HOME, 避免 subprocess session jsonl
# 污染主 bucket (2026-04-20 踩过的坑).
_SUMMARY_SANDBOX.mkdir(parents=True, exist_ok=True)
result = subprocess.run(
[cli, "-p", prompt,
"--output-format", "text",
"--permission-mode", "auto"],
capture_output=True, text=True, timeout=60,
cwd=str(_SUMMARY_SANDBOX),
)
summary = (result.stdout or "").strip().strip('"').strip("「」")
if not summary or len(summary) > 60:
# haiku 偶尔抗命给长输出 / 空输出 — 丢弃不写缓存, 下次再试
return
cache = _load_summary_cache()
cache[sid] = {"summary": summary, "source_mtime": source_mtime}
_save_summary_cache(cache)
except Exception as e:
log.debug("summary gen failed for %s: %s", sid[:8], e)
import threading
threading.Thread(target=worker, daemon=True).start()
def _scan_peer_sids() -> dict[str, list[str]]:
"""扫 _STATE_DIR 所有 *-session.json, 返回 {sid: [channel_label, ...]}.
一个 sid 如果被多个 channel 的 recent_sids 收录 (例 V 从 TG /resume 了 bb 开的
session → 两个 state 都会有它), 列表里会有多个来源.
"""
out: dict[str, list[str]] = {}
if not _STATE_DIR.is_dir():
return out
for fp in sorted(_STATE_DIR.glob("*-session.json")):
label = _channel_label_from_state_file(fp)
try:
data = json.loads(fp.read_text())
except Exception:
continue
for sid in data.get("recent_sids") or []:
out.setdefault(sid, []).append(label)
return out
async def _always_allow(
tool_name: str,
tool_input: dict[str, Any],
ctx: ToolPermissionContext,
) -> PermissionResultAllow | Any:
"""Auto-approve every SDK permission prompt.
bypassPermissions mode alone doesn't cover CC's "protected paths"
(~/.claude, .git, .ssh, .zshrc, .mcp.json, ...). Those still prompt in
every mode except `auto` and `dontAsk`. Bot is non-interactive, so any
prompt that reaches SDK = hung tool call.
Personal Mac full-trust — blanket allow."""
block, reason = should_block_for_permission(tool_name, tool_input)
if block:
message = reason or "Blocked by babata deterministic guard"
if PermissionResultDeny is not None:
return PermissionResultDeny(message=message)
raise PermissionError(message)
return PermissionResultAllow()
# Tunables. These are storage / token-budget params, not "importance judgments" —
# CC still decides meaning from the data we expose. Kept explicit so a future
# reader sees the cost model instead of magic numbers.
_MAX_RECENT_SIDS = 200 # ring buffer of past session_ids (~1y at 5/day, ~15KB state file)
_RESUME_INJECT_PAIRS = 3 # last N user+assistant pairs to inject on resume failure
_RESUME_INJECT_CHARS = 300 # per-turn char cap (3 pairs × 300 × 2 ≈ 1.8KB, fits any system_prompt)
_IDLE_RESET_MINUTES_DEFAULT = 1440 # 24h, parity with hermes session_reset.idle_minutes (gateway/config.py:114)
_DEFAULT_MEMORY_INJECT_SCRIPT = Path.home() / "cc-workspace/scripts/memory-inject.sh"
_DEFAULT_MEMORY_REFLEX_SCRIPT = Path.home() / "cc-workspace/bin/babata-memory-reflex"
_DEFAULT_MEMORY_REFLEX_LOG = Path.home() / "cc-workspace/state/memory-reflex/events.jsonl"
def _cc_memory_inject_enabled() -> bool:
if os.environ.get("BABATA_CRON_AGENT") == "1":
return False
return os.environ.get("BABATA_CC_MEMORY_INJECT", "1") != "0"
def _memory_inject_script() -> Path:
configured = os.environ.get("BABATA_MEMORY_INJECT_SCRIPT")
return Path(configured).expanduser() if configured else _DEFAULT_MEMORY_INJECT_SCRIPT
def _memory_inject_timeout() -> float:
raw = os.environ.get("BABATA_CC_MEMORY_INJECT_TIMEOUT", "5")
try:
return max(0.1, float(raw))
except ValueError:
return 5.0
def _memory_reflex_enabled() -> bool:
return os.environ.get("BABATA_MEMORY_REFLEX", "1") != "0"
def _memory_reflex_mode() -> str:
if not _memory_reflex_enabled():
return "off"
mode = os.environ.get("BABATA_MEMORY_REFLEX_MODE", "dry-run").strip().lower()
return mode if mode in {"dry-run", "enforce"} else "dry-run"
def _memory_reflex_script() -> Path:
configured = os.environ.get("BABATA_MEMORY_REFLEX_SCRIPT")
return Path(configured).expanduser() if configured else _DEFAULT_MEMORY_REFLEX_SCRIPT
def _memory_reflex_timeout() -> float:
raw = os.environ.get("BABATA_MEMORY_REFLEX_TIMEOUT", "0.8")
try:
return max(0.1, float(raw))
except ValueError:
return 0.8
def _memory_source_from_prompt(source_prompt: str) -> str:
lower = source_prompt.lower()
if "source: telegram" in lower:
return "tg"
if "source: wechat" in lower:
return "wechat"
if "source: sidebar" in lower:
return "sidebar"
return os.environ.get("BABATA_MEMORY_SOURCE") or "unknown"
def _memory_reflex_for_prompt(source_prompt: str, user_prompt: str | None) -> dict[str, Any]:
if not _memory_reflex_enabled() or not user_prompt:
return {}
script = _memory_reflex_script()
if not script.is_file():
log.warning("babata memory reflex script missing: %s", script)
return {}
source = _memory_source_from_prompt(source_prompt)
try:
result = subprocess.run(
[
str(script),
"--message", "-",
"--source", source,
"--cpu", "claude",
"--cwd", _DEFAULT_CWD,
],
input=user_prompt,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=_memory_reflex_timeout(),
check=False,
)
except Exception as exc:
log.warning("babata memory reflex failed: %s", exc)
return {}
if result.returncode != 0:
log.warning("babata memory reflex exited %s: %s", result.returncode, result.stderr.strip()[:500])
return {}
try:
parsed = json.loads(result.stdout)
except Exception as exc:
log.warning("babata memory reflex returned invalid json: %s", exc)
return {}
return parsed if isinstance(parsed, dict) else {}
def _format_memory_reflex_hint(reflex: dict[str, Any]) -> str:
routes = [str(r) for r in reflex.get("routes", []) if str(r)]
profile = str(reflex.get("profile") or "lite")
if not routes or (profile == "lite" and all(r in {"none", "lite"} for r in routes)):
return ""
reasons = reflex.get("reasons")
reason_text = "; ".join(str(r) for r in reasons[:3]) if isinstance(reasons, list) else ""
return "\n".join([
"<memory-reflex>",
f"routes: {', '.join(routes)}",
f"profile: {profile}",
"note: router signal only; retrieve deeper evidence only when useful.",
f"why: {reason_text}" if reason_text else "why: unspecified",
"</memory-reflex>",
])
def _memory_reflex_log_path() -> Path:
configured = os.environ.get("BABATA_MEMORY_REFLEX_LOG")
return Path(configured).expanduser() if configured else _DEFAULT_MEMORY_REFLEX_LOG
def _message_summary(text: str | None, limit: int = 180) -> str:
compact = " ".join((text or "").split())
return compact[:limit].rstrip()
def _append_memory_reflex_event(payload: dict[str, Any]) -> None:
try:
path = _memory_reflex_log_path()
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a", encoding="utf-8") as f:
f.write(json.dumps(payload, ensure_ascii=False, sort_keys=True) + "\n")
except Exception as exc:
log.warning("babata memory reflex log failed: %s", exc)
def _log_memory_reflex_preflight(
*,
reflex: dict[str, Any],
user_prompt: str | None,
source: str,
cpu: str,
mode: str,
actual_profile: str,
memory_injected: bool,
hint_injected: bool,
) -> str | None:
if not reflex:
return None
now = time.time()
digest = hashlib.sha256((user_prompt or "").encode("utf-8")).hexdigest()
event_id = hashlib.sha256(f"{now}:{cpu}:{source}:{digest}".encode("utf-8")).hexdigest()[:16]
_append_memory_reflex_event({
"event": "preflight",
"id": event_id,
"ts": now,
"source": source,
"cpu": cpu,
"mode": mode,
"message_sha256": digest,
"message_summary": _message_summary(user_prompt),
"router": reflex,
"actual_profile": actual_profile,
"memory_injected": memory_injected,
"hint_injected": hint_injected,
"post_answer_observation": "pending",
})
return event_id
def _answer_memory_observation(content: str) -> dict[str, Any]:
markers = ("不记得", "没记住", "没有记忆", "没有记录", "查不到", "没查到", "无法确认", "没有找到")
return {
"heuristic_only": True,
"memory_miss_marker": any(marker in content for marker in markers),
"wrong_recall": None,
"missed_required_lookup": None,
}
def _log_memory_reflex_post_answer(event_id: str | None, content: str) -> None:
if not event_id:
return
_append_memory_reflex_event({
"event": "post_answer",
"id": event_id,
"ts": time.time(),
"answer_sha256": hashlib.sha256((content or "").encode("utf-8")).hexdigest(),
"answer_summary": _message_summary(content),
"observation": _answer_memory_observation(content or ""),
})
def _render_babata_memory_context_event(
source_prompt: str,
user_prompt: str | None = None,
) -> tuple[str, str | None]:
if not _cc_memory_inject_enabled():
return "", None
script = _memory_inject_script()
if not script.is_file():
log.warning("babata memory inject script missing: %s", script)
return "", None
reflex = _memory_reflex_for_prompt(source_prompt, user_prompt)
mode = _memory_reflex_mode()
enforce = mode == "enforce"
source = _memory_source_from_prompt(source_prompt)
actual_profile = os.environ.get("BABATA_MEMORY_PROFILE") or (
str(reflex.get("profile") or "lite") if enforce else "lite"
)
env = os.environ.copy()
env["BABATA_MEMORY_PROFILE"] = actual_profile
env.setdefault("BABATA_MEMORY_CPU", "claude")
env.setdefault("BABATA_MEMORY_SOURCE", source)
env.setdefault("BABATA_MEMORY_INCLUDE_TOP", "force")
try:
result = subprocess.run(
[str(script)],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=_memory_inject_timeout(),
check=False,
)
except Exception as exc:
log.warning("babata memory inject failed: %s", exc)
return "", None
if result.returncode != 0:
log.warning(
"babata memory inject exited %s: %s",
result.returncode,
result.stderr.strip()[:500],
)
return "", None
parts = [result.stdout.strip()]
hint = _format_memory_reflex_hint(reflex) if enforce else ""
if hint:
parts.append(hint)
context = "\n\n".join(part for part in parts if part)
event_id = _log_memory_reflex_preflight(
reflex=reflex,
user_prompt=user_prompt,
source=source,
cpu="claude",
mode=mode,
actual_profile=actual_profile,
memory_injected=bool(context),
hint_injected=bool(hint),
)
return context, event_id
def _render_babata_memory_context(source_prompt: str, user_prompt: str | None = None) -> str:
return _render_babata_memory_context_event(source_prompt, user_prompt)[0]
def _idle_reset_seconds() -> int:
"""Idle threshold in seconds. 0 = disabled. Override via BABATA_IDLE_RESET_MINUTES."""
raw = os.environ.get("BABATA_IDLE_RESET_MINUTES")
if raw is None:
m = _IDLE_RESET_MINUTES_DEFAULT
else:
try:
m = int(raw)
except ValueError:
log.warning(
"Bad BABATA_IDLE_RESET_MINUTES=%r, using default %d",
raw, _IDLE_RESET_MINUTES_DEFAULT,
)
m = _IDLE_RESET_MINUTES_DEFAULT
return max(0, m) * 60
# CC CLI writes synthetic `type:"user"` entries for its own housekeeping:
# <local-command-caveat>…</local-command-caveat> — inserted before slash commands
# <command-name>/foo</command-name> — the slash command itself
# <command-message>foo</command-message> — plain-language label
# <command-args></command-args> — args after the slash
# <bash-input>…</bash-input> — CC's Bash tool calls
# <local-command-stdout>… — command captures
# Any of these as the first "user" message of a session makes the /resume
# preview useless. Match tag-prefixed content so V sees her real first prompt.
_SYNTHETIC_USER_PREFIXES = (
"<local-command-",
"<command-name>",
"<command-message>",
"<command-args>",
"<command-stdout>",
"<command-stderr>",
"<bash-input>",
"<bash-stdout>",
"<bash-stderr>",
)
def _is_synthetic_user_text(text: str) -> bool:
"""True if `text` is CC-injected scaffolding rather than a V-authored turn."""
stripped = text.lstrip()
return stripped.startswith(_SYNTHETIC_USER_PREFIXES)
def _extract_text(content: Any) -> str:
if isinstance(content, str):
return content.strip()
if isinstance(content, list):
parts = [b.get("text", "") for b in content
if isinstance(b, dict) and b.get("type") == "text"]
return " ".join(parts).strip()
return ""
def _tool_result_text(content: Any) -> str:
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):
parts: list[str] = []
for b in content:
if isinstance(b, dict):
t = b.get("text") or b.get("content")
if t:
parts.append(str(t))
else:
parts.append(str(b))
return "".join(parts)
return str(content)
def _extract_tool_uses(messages: list[Any]) -> list[dict[str, Any]]:
out: list[dict[str, Any]] = []
for msg in messages:
if not isinstance(msg, AssistantMessage):
continue
for block in getattr(msg, "content", []) or []:
if not isinstance(block, ToolUseBlock):
continue
out.append(summarize_tool_use(
getattr(block, "name", "") or "",
getattr(block, "input", {}) or {},
))
return out
def _ordered_tool_names(existing: list[str], tool_uses: list[dict[str, Any]]) -> list[str]:
out: list[str] = []
seen: set[str] = set()
for name in existing + [str(t.get("name") or "") for t in tool_uses]:
if not name or name in seen:
continue
seen.add(name)
out.append(name)
return out
@dataclass
class Response:
content: str
session_id: str
cost: float
tools: list[str] = field(default_factory=list)
resume_note: str | None = None # populated when SDK resume failed + we recovered
stopped: bool = False
# Model + token accounting, from ResultMessage.model_usage (first key = actual
# model CC used this turn). None when SDK didn't report (e.g. /new shortcut).
model: str | None = None
context_window: int | None = None
max_output_tokens: int | None = None
input_tokens: int = 0
output_tokens: int = 0
cache_read_tokens: int = 0
cache_creation_tokens: int = 0
audit: dict[str, Any] | None = None
# True when the SDK ResultMessage reported an error result (rate-limit /
# max_turns / API 5xx). Surfaced so the bot can show the real failure
# instead of an empty bubble.
is_error: bool = False
def _result_error_text(subtype: str | None, result: Any) -> str:
"""Human-facing message for an error ResultMessage so V sees the real
failure (limit / max_turns / API error) rather than an empty answer."""
labels = {
"error_max_turns": "本轮触及最大工具调用轮次上限 (max_turns)",
"error_during_execution": "执行期间出错",
"error_max_tokens": "本轮触及输出 token 上限",
}
label = labels.get(subtype or "", subtype or "未知错误")
detail = str(result).strip() if result else ""
base = f"⚠️ 本轮未正常完成:{label}"
return f"{base}\n{detail}" if detail else base
@dataclass
class Event:
kind: Literal[
"tool_use",
"tool_result",
"text_delta",
"turn_end",
"session_changed",
"error",
]
name: str | None = None
input_dict: dict[str, Any] | None = None
is_error: bool = False
text: str | None = None
chunk: str | None = None
response: Response | None = None
old_sid: str | None = None
new_sid: str | None = None
exception: Exception | None = None
class CC:
"""Single-session Claude Code interface for one channel.
Each channel (TG / WeChat) owns its own CC instance: separate state file,
separate resume history, separate MCP tool surface.
"""
supports_hot_input = False
def __init__(
self,
*,
state_file: Path,
source_prompt: str,
mcp_servers: dict[str, Any] | None = None,
model: str | None = None,
) -> None:
self._state_file = state_file
self._source_prompt = source_prompt
self._mcp_servers = mcp_servers or {}
self._model = model
self._session_id: str | None = self._load_state().get("session_id")
self._memory_reflex_event_id: str | None = None
def _source_prompt_with_memory(
self,
extra_context: str | None = None,
*,
user_prompt: str | None = None,
) -> str:
parts = [self._source_prompt]
memory_context, event_id = _render_babata_memory_context_event(self._source_prompt, user_prompt)
self._memory_reflex_event_id = event_id
if memory_context:
parts.append(memory_context)
if extra_context:
parts.append(extra_context)
return "\n\n".join(parts)
def _record_memory_reflex_answer(self, content: str) -> None:
_log_memory_reflex_post_answer(self._memory_reflex_event_id, content)
self._memory_reflex_event_id = None
# ── state persistence (per-channel) ──────────────────────────────
def _load_state(self) -> dict:
try:
return json.loads(self._state_file.read_text())
except Exception:
return {}
def _save_state(self, state: dict) -> None:
try:
self._state_file.parent.mkdir(parents=True, exist_ok=True)
tmp = self._state_file.with_suffix(self._state_file.suffix + ".tmp")
tmp.write_text(json.dumps(state))
tmp.replace(self._state_file)
except Exception as e:
log.warning("Failed to persist session state: %s", e)
def _remember_engine_sid(self, state: dict, sid: str | None) -> None:
engine_name = getattr(self, "_babata_engine_name", None)
if not isinstance(engine_name, str) or not engine_name:
return
engine_sids = state.get("engine_session_ids")
if not isinstance(engine_sids, dict):
engine_sids = {}
engine_sids[engine_name] = sid or ""
state["engine_session_ids"] = engine_sids
def _record_sid(self, sid: str | None) -> None:
state = self._load_state()
state["session_id"] = sid
self._remember_engine_sid(state, sid)
# Touch activity timestamp on every sid write — _run() calls this after
# each successful turn, so it doubles as the idle-reset clock.
state["last_activity_at"] = time.time()
if sid:
hist = [s for s in state.get("recent_sids", []) if s != sid]
hist.insert(0, sid)
state["recent_sids"] = hist[:_MAX_RECENT_SIDS]
self._save_state(state)
def _check_idle_reset(self) -> bool:
"""Silently reset session if idle exceeds threshold. Returns True if reset.
Idle reset ≠ /new: fires only the babata-local session-end hook for the
old sid. Skips skill-evolve session-start (V didn't actively reset) and
skips the "会话已重置" reply. Next turn starts fresh and picks up the
standard SessionStart memory inject.
Default 24h, parity with hermes idle_minutes. Migration: state files
without last_activity_at fall back to file mtime so an already-stale
sid doesn't get one free turn before reset kicks in.
"""
threshold = _idle_reset_seconds()
if threshold <= 0 or not self._session_id:
return False
last = self._load_state().get("last_activity_at")
if not isinstance(last, (int, float)):
try:
last = self._state_file.stat().st_mtime
except OSError:
return False
elapsed = time.time() - last
if elapsed < 0:
# Future timestamp (clock rollback / NTP / corruption). Don't
# reset — _record_sid overwrites with current time on next turn.
log.warning(
"idle check: future timestamp on sid=%s (last=%s now=%s); skipping",
self._session_id[:8], last, time.time(),
)
return False
if elapsed <= threshold:
return False
log.info(
"idle reset: sid=%s elapsed=%.0fs threshold=%ds",
self._session_id[:8], elapsed, threshold,
)
old_sid = self._session_id
self._fire_hook(_HOOKS_DIR, "session-end.sh", old_sid)
self._session_id = None
self._record_sid(None)
return True
def _recent_turns_summary(self) -> str:
"""Take the most recent session (tracked in state.recent_sids) and
extract last _RESUME_INJECT_PAIRS user+assistant pairs. Returns '' if
state empty or no session file usable."""
sids = self._load_state().get("recent_sids") or []
for sid in sids:
target = _CC_PROJECTS / f"{sid}.jsonl"
if not target.is_file():
continue
turns: list[tuple[str, str]] = []
try:
for line in target.read_text().splitlines():
try:
d = json.loads(line)
except Exception:
continue
msg = d.get("message")
if not isinstance(msg, dict):
continue
role = msg.get("role")
if role not in ("user", "assistant"):
continue
text = _extract_text(msg.get("content"))
if text:
turns.append((role, text))
except Exception:
continue
if not turns:
continue
recent = turns[-(2 * _RESUME_INJECT_PAIRS):]
lines = [f"{'V' if r == 'user' else 'CC'}: {t[:_RESUME_INJECT_CHARS]}"
for r, t in recent]
return "会话从历史归档恢复, 最近几轮:\n" + "\n".join(lines)
return ""
def reset(self) -> None:
old_sid = self._session_id
if old_sid:
self._fire_hook(_SKILL_HOOKS_DIR, "session-end.sh", old_sid)
self._fire_hook(_HOOKS_DIR, "session-end.sh", old_sid)
self._session_id = None
self._record_sid(None)
# skill-evolve SessionStart: 处理 pending + surface 上次 evolve 给 V (空
# sid, 它不关心新 sid 是啥). babata-local session-start 不在这里 fire —
# 新 sid 要等下一次 query 的 ResultMessage 才拿得到, 见 _run().
self._fire_hook(_SKILL_HOOKS_DIR, "session-start.sh", "")
def list_recent_sessions(
self, limit: int = 10, channel_filter: list[str] | None = None,
scan_all_buckets: bool = False,
) -> list[dict]:
"""Return recent sessions for `/resume` picker — 跨渠道可见.
channel_filter: 白名单 channel label 列表. None = 不过滤 (默认, 扫全部).
传 ['巴巴塔', '巴巴塔2', '巴巴塔3'] (见 constants.INSTANCE_LABELS) = 只列 TG 类 session.
'term' / 'oneshot' 是特殊 label = 所有 channel state 都没收录过的 orphan
session, 按 JSONL 的 entrypoint 字段细分:
- entrypoint=sdk-cli → 'oneshot' (claude -p 一次性, cron / 手敲 -p)
- 其他 (cli / claude-desktop / sdk-py orphan / 未知) → 'term' (交互或异常)
这样 /resume 里 "终端" 和 "一次性" 能分开, V 找 bb 交互 session 不再被
cron 一次性塞满列表.
行为变更 (2026-04-20): 从"扫本 channel state.recent_sids"改成"扫整个
_CC_PROJECTS bucket 的 *.jsonl 按 mtime 排序". 原因: V 的设计是多渠道
共享一个 CC 内核, TG / WX / 终端 bb 开的 session 应互相可见. 旧实现