codebee 0.1.71 → 0.1.73

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 (118) hide show
  1. package/CHANGELOG.md +601 -589
  2. package/LICENSE +21 -21
  3. package/README.md +508 -509
  4. package/app/core/acceptance.py +100 -100
  5. package/app/core/aiflavor.py +197 -197
  6. package/app/core/artifacts.py +148 -148
  7. package/app/core/assets.py +110 -110
  8. package/app/core/attachments.py +623 -623
  9. package/app/core/audit.py +30 -30
  10. package/app/core/backup.py +470 -470
  11. package/app/core/beekeeper.py +183 -183
  12. package/app/core/benchstore.py +95 -95
  13. package/app/core/board.py +209 -209
  14. package/app/core/bookmeta.py +935 -935
  15. package/app/core/bookmeta_catalog.py +180 -180
  16. package/app/core/branching.py +137 -137
  17. package/app/core/builtin_agent.py +1680 -1673
  18. package/app/core/capability.py +144 -144
  19. package/app/core/chaptersafe.py +32 -32
  20. package/app/core/cleanup.py +303 -303
  21. package/app/core/compaction.py +201 -201
  22. package/app/core/covergen.py +356 -356
  23. package/app/core/defectretro.py +43 -43
  24. package/app/core/diagnostics.py +114 -114
  25. package/app/core/dispatch.py +190 -190
  26. package/app/core/dispatch_log.py +128 -128
  27. package/app/core/env_scrub.py +85 -85
  28. package/app/core/errorlog.py +137 -137
  29. package/app/core/evalbench.py +836 -720
  30. package/app/core/evaluation.py +174 -174
  31. package/app/core/failure_notify.py +290 -290
  32. package/app/core/flows.py +8 -8
  33. package/app/core/goal_service.py +167 -167
  34. package/app/core/health.py +374 -374
  35. package/app/core/history.py +38 -38
  36. package/app/core/hooks.py +162 -162
  37. package/app/core/jobs.py +25 -1
  38. package/app/core/manager.py +2208 -2143
  39. package/app/core/market.py +90 -4
  40. package/app/core/market_remote.py +16 -0
  41. package/app/core/mocks.py +64 -64
  42. package/app/core/modelhub.py +3 -2
  43. package/app/core/notify.py +201 -201
  44. package/app/core/novel_quality.py +189 -189
  45. package/app/core/operations.py +228 -228
  46. package/app/core/paihang.py +145 -138
  47. package/app/core/pipeline.py +102 -30
  48. package/app/core/planner.py +2 -0
  49. package/app/core/plugins.py +34 -0
  50. package/app/core/portguard.py +111 -111
  51. package/app/core/preview.py +293 -293
  52. package/app/core/prompt_guard.py +78 -0
  53. package/app/core/publish/__init__.py +7 -7
  54. package/app/core/publish/auto.py +396 -367
  55. package/app/core/publish/browser.py +655 -655
  56. package/app/core/publish/fanqie.py +165 -165
  57. package/app/core/publish/flow.py +640 -589
  58. package/app/core/publish/flows-fanqie-calibrated.json +336 -336
  59. package/app/core/publish/ledger.py +307 -229
  60. package/app/core/publish/manager.py +1118 -824
  61. package/app/core/publish/qimao.py +176 -146
  62. package/app/core/publish/ws.py +139 -139
  63. package/app/core/quality_gate.py +194 -0
  64. package/app/core/redact.py +38 -38
  65. package/app/core/repeat_guard.py +129 -129
  66. package/app/core/router.py +398 -398
  67. package/app/core/runner.py +2142 -2142
  68. package/app/core/selfupdate.py +384 -384
  69. package/app/core/session_log.py +161 -161
  70. package/app/core/sessions.py +312 -312
  71. package/app/core/settings_schema.py +270 -270
  72. package/app/core/share_page.py +114 -114
  73. package/app/core/skill_scan.py +139 -122
  74. package/app/core/skillpacks/fanqie-novel.md +98 -80
  75. package/app/core/skillpacks/market/character-bible.md +66 -66
  76. package/app/core/skillpacks/market/code-risk-checklist.md +58 -58
  77. package/app/core/skillpacks/market/git-workflow.md +57 -57
  78. package/app/core/skillpacks/market/release-notes.md +72 -72
  79. package/app/core/skillpacks/market/weekly-report.md +71 -71
  80. package/app/core/skillpacks/market/worldview-consistency.md +70 -70
  81. package/app/core/skillpacks/qimao-signing.md +70 -59
  82. package/app/core/skills.py +58 -8
  83. package/app/core/step_runner.py +132 -132
  84. package/app/core/store.py +51 -0
  85. package/app/core/task_compile.py +199 -199
  86. package/app/core/telemetry.py +292 -292
  87. package/app/core/tlsctx.py +64 -64
  88. package/app/core/token_meter.py +147 -147
  89. package/app/core/tracing.py +144 -144
  90. package/app/core/upstream.py +41 -41
  91. package/app/core/usage.py +1035 -1035
  92. package/app/core/volumes.py +389 -389
  93. package/app/core/wxdigest.py +858 -858
  94. package/app/core/zentao.py +2692 -2614
  95. package/app/main.py +110 -39
  96. package/app/mcp_server.py +175 -175
  97. package/app/pick_dialog.py +110 -110
  98. package/app/ui/app.js +509 -114
  99. package/app/ui/artdiff.js +74 -74
  100. package/app/ui/board.html +162 -162
  101. package/app/ui/board.js +305 -305
  102. package/app/ui/i18n.js +41 -2
  103. package/app/ui/icons/bee.svg +79 -79
  104. package/app/ui/index.html +8 -3
  105. package/app/ui/manifest.json +16 -16
  106. package/app/ui/qrcode.js +2297 -2297
  107. package/app/ui/style.css +74 -26
  108. package/bin/tutti.js +147 -147
  109. package/package.json +48 -47
  110. package/tools/wx_reader.py +261 -261
  111. package/app/.playwright-cli/console-2026-09-23T08-26-03-635Z.log +0 -88
  112. package/app/.playwright-cli/page-2026-09-23T08-26-03-845Z.yml +0 -126
  113. package/app/.playwright-cli/page-2026-09-23T08-29-44-203Z.png +0 -0
  114. package/app/.playwright-cli/page-2026-09-23T08-35-42-046Z.yml +0 -210
  115. package/app/.playwright-cli/page-2026-09-23T08-36-52-858Z.png +0 -0
  116. package/app/.playwright-cli/page-2026-09-23T08-37-33-938Z.png +0 -0
  117. package/app/.playwright-cli/run-detail-after.png +0 -0
  118. package/app//344/275/234/345/223/201/344/277/241/346/201/257-/344/270/203/347/214/253.md +0 -18
@@ -1,201 +1,201 @@
1
- # -*- coding: utf-8 -*-
2
- """三段式上下文压缩:工具结果剪枝 → LLM 摘要 → surface replace。
3
-
4
- 设计稿:docs/migration/02-context-compaction.md §1B。
5
- 参考 dsh:
6
- packages/compaction/compaction-basic/src/region.ts:117-155(selectCompactableRange:
7
- 跳过 system 头、保留尾预算、不切断 tool call/result 配对)
8
- packages/compaction/compaction-tool-result-pruner/src/index.ts:83-122(头尾剪枝)
9
- packages/compaction/compaction-basic/src/region.ts:173-275(压缩事务四件事件:
10
- compaction/start → summary → user/message(replace) → end;失败仍写 end 保证日志可见)
11
-
12
- 触发:maybe_compact() 检查 token_meter 压力比 ≥ 阈值才动手。
13
- """
14
- from __future__ import annotations
15
-
16
- import logging
17
-
18
- from .token_meter import token_meter, DEFAULT_PRESSURE_THRESHOLD
19
-
20
- log = logging.getLogger(__name__)
21
-
22
- # 工具结果剪枝参数(字符数;dsh 默认 8192/4096,单行 marker)
23
- PRUNE_HEAD = 8192
24
- PRUNE_TAIL = 4096
25
- PRUNE_MARKER = "\n…[中间内容已剪枝,原文见会话日志]…\n"
26
-
27
- # 尾部保留预算(估算 token)
28
- RETAIN_TAIL_TOKENS = 8000
29
- # 单次压缩区域上限(事件数,防一次吞太多)
30
- MAX_REGION_EVENTS = 50
31
-
32
-
33
- def estimate_tokens(text: str) -> int:
34
- """极简估算:4 字符/token(中文偏低估,偏保守触发)。"""
35
- return max(1, len(text or "") // 4)
36
-
37
-
38
- def _ev_text(ev) -> str:
39
- return str(ev.data.get("content") or ev.data.get("stdout") or "")
40
-
41
-
42
- def prune_text(text: str, *, head=PRUNE_HEAD, tail=PRUNE_TAIL, marker=PRUNE_MARKER) -> str:
43
- """超长文本头尾剪枝(纯无 LLM 调用)。"""
44
- if text is None:
45
- return ""
46
- if len(text) <= head + tail + len(marker):
47
- return text
48
- return text[:head] + marker + text[-tail:]
49
-
50
-
51
- def select_range(session, *, retain_tail_tokens=RETAIN_TAIL_TOKENS,
52
- max_region_events=MAX_REGION_EVENTS):
53
- """选可压缩区域 [start_seq, end_seq](闭区间)。
54
-
55
- 规则(仿 dsh selectCompactableRange):
56
- 1. 跳过 system 头节点(从第一条 user_message 起);
57
- 2. 尾部保留 retain_tail_tokens 预算不参与压缩(retain<=0 表示尾部不保留);
58
- 3. 全部内容都在尾预算内 → 无可压缩区域,返回 (0, 0);
59
- 4. 区域末端不能落在 tool 配对中间(回退到配对完成处);
60
- 5. 区域不超过 max_region_events 个事件。
61
- 返回 (start_seq, end_seq);无可压缩区域返回 (0, 0)。
62
- """
63
- events = session.events()
64
- surface_events = [e for e in events if e.type in
65
- ("system_message", "user_message", "assistant_message")]
66
- if not surface_events:
67
- return 0, 0
68
- first_user_seq = next((e.seq for e in surface_events
69
- if e.type == "user_message"), 0)
70
- if not first_user_seq:
71
- return 0, 0
72
- # 尾部保留:倒推预算。tail_start_seq = 保留尾段的第一个 seq。
73
- last_seq = surface_events[-1].seq
74
- if retain_tail_tokens <= 0:
75
- tail_start_seq = last_seq + 1 # 尾部不保留:全部可压
76
- else:
77
- tail_budget = 0
78
- tail_start_seq = 0 # 0 表示"全部都在预算内"(无区域)
79
- for ev in reversed(surface_events):
80
- tail_budget += estimate_tokens(_ev_text(ev))
81
- if tail_budget >= retain_tail_tokens:
82
- tail_start_seq = ev.seq
83
- break
84
- if not tail_start_seq:
85
- return 0, 0 # 预算从未被突破 → 无可压缩区域
86
- # 候选区域 = [first_user_seq, tail_start_seq)
87
- all_events = session.events()
88
- by_seq = {e.seq: e for e in all_events}
89
- end_seq = tail_start_seq - 1
90
- # 不切断 tool 配对:末端若是 tool_call/tool_result,回退到最近的非 tool 事件
91
- while end_seq >= first_user_seq:
92
- ev = by_seq.get(end_seq)
93
- if ev is None or ev.type not in ("tool_call", "tool_result"):
94
- break
95
- end_seq -= 1
96
- # 区域事件数上限
97
- region_events = [e for e in all_events
98
- if first_user_seq <= e.seq <= end_seq]
99
- if len(region_events) > max_region_events:
100
- end_seq = region_events[max_region_events - 1].seq
101
- # 再做一次配对回退
102
- while end_seq >= first_user_seq:
103
- ev = by_seq.get(end_seq)
104
- if ev is None or ev.type not in ("tool_call", "tool_result"):
105
- break
106
- end_seq -= 1
107
- if end_seq < first_user_seq:
108
- return 0, 0
109
- return first_user_seq, end_seq
110
-
111
-
112
- _SUMMARY_SYSTEM = (
113
- "你是会话压缩器。把以下对话历史压缩成简洁摘要,必须保留:\n"
114
- "1. 关键决策与结论;2. 涉及的文件路径与命令;3. 未完成事项;\n"
115
- "4. 错误信息原文。直接输出摘要正文,不要客套。"
116
- )
117
-
118
-
119
- def compact_region(session, start_seq: int, end_seq: int, llm_caller,
120
- *, reason: str = "pressure"):
121
- """执行一次压缩事务(四件事件,失败仍写 compaction_end 保证日志可见)。
122
-
123
- llm_caller(messages: list[dict]) -> str:一次 LLM 调用,返回摘要文本。
124
- """
125
- tx = session.append("compaction_start",
126
- {"start_seq": start_seq, "end_seq": end_seq, "reason": reason},
127
- surface_op="shadow")
128
- summary = ""
129
- error = ""
130
- try:
131
- region_events = [e for e in session.events()
132
- if start_seq <= e.seq <= end_seq]
133
- # 第一段:工具结果先剪枝(省摘要调用的输入 token)
134
- parts = []
135
- for ev in region_events:
136
- text = _ev_text(ev)
137
- if ev.type == "tool_result":
138
- text = prune_text(text)
139
- if text.strip():
140
- parts.append(f"[{ev.type}#{ev.seq}] {text}")
141
- region_text = "\n\n".join(parts)
142
- raw_tokens = estimate_tokens(region_text)
143
- summary = llm_caller([
144
- {"role": "system", "content": _SUMMARY_SYSTEM},
145
- {"role": "user", "content": region_text},
146
- ])
147
- summary_tokens = estimate_tokens(summary)
148
- session.append("compaction_summary",
149
- {"raw_tokens": raw_tokens,
150
- "summary_tokens": summary_tokens,
151
- "start_seq": start_seq, "end_seq": end_seq},
152
- surface_op="shadow")
153
- # 台账入账(A 专项:省了多少不再是黑箱)——摘要调用本身的真实消耗
154
- # (读 region、写 summary)此前完全隐形;saved=净省量与消耗并列成账。
155
- # record 自吞一切异常,统计绝不拖垮压缩事务。
156
- try:
157
- from . import usage as _usage
158
- _usage.record(source="compaction",
159
- run_id=str(getattr(session, "run_id", "") or ""),
160
- tool="compaction",
161
- usage={"input": raw_tokens,
162
- "output": summary_tokens,
163
- "saved": max(0, raw_tokens - summary_tokens)},
164
- ok=True)
165
- except Exception:
166
- pass
167
- # surface replace:把区域折叠为一条 user 摘要消息
168
- session.append("user_message", {
169
- "content": f"[系统压缩摘要 {start_seq}-{end_seq},原因 {reason}]\n{summary}",
170
- }, surface_op={"op": "replace", "start_seq": start_seq, "end_seq": end_seq})
171
- except Exception as e:
172
- error = repr(e)
173
- log.warning("compact_region failed: %s", error)
174
- session.append("compaction_summary", {"error": error}, surface_op="shadow")
175
- finally:
176
- session.append("compaction_end",
177
- {"tx_seq": tx.seq, "error": error},
178
- surface_op="shadow")
179
- return not error
180
-
181
-
182
- def maybe_compact(session, *, model: str = "", llm_caller=None,
183
- threshold: float = DEFAULT_PRESSURE_THRESHOLD,
184
- run_id: str = None,
185
- retain_tail_tokens: int = RETAIN_TAIL_TOKENS) -> bool:
186
- """压力比 ≥ 阈值时触发一次压缩。返回是否执行了压缩。
187
-
188
- llm_caller 缺省直接跳过(没有摘要能力就不压)。
189
- """
190
- if llm_caller is None:
191
- return False
192
- rid = run_id or session.run_id
193
- ratio = token_meter.pressure_ratio(rid, model=model)
194
- if ratio < threshold:
195
- return False
196
- start, end = select_range(session, retain_tail_tokens=retain_tail_tokens)
197
- if not start:
198
- return False
199
- log.info("compaction triggered run=%s ratio=%.2f region=[%d,%d]",
200
- rid, ratio, start, end)
201
- return compact_region(session, start, end, llm_caller, reason=f"pressure={ratio:.2f}")
1
+ # -*- coding: utf-8 -*-
2
+ """三段式上下文压缩:工具结果剪枝 → LLM 摘要 → surface replace。
3
+
4
+ 设计稿:docs/migration/02-context-compaction.md §1B。
5
+ 参考 dsh:
6
+ packages/compaction/compaction-basic/src/region.ts:117-155(selectCompactableRange:
7
+ 跳过 system 头、保留尾预算、不切断 tool call/result 配对)
8
+ packages/compaction/compaction-tool-result-pruner/src/index.ts:83-122(头尾剪枝)
9
+ packages/compaction/compaction-basic/src/region.ts:173-275(压缩事务四件事件:
10
+ compaction/start → summary → user/message(replace) → end;失败仍写 end 保证日志可见)
11
+
12
+ 触发:maybe_compact() 检查 token_meter 压力比 ≥ 阈值才动手。
13
+ """
14
+ from __future__ import annotations
15
+
16
+ import logging
17
+
18
+ from .token_meter import token_meter, DEFAULT_PRESSURE_THRESHOLD
19
+
20
+ log = logging.getLogger(__name__)
21
+
22
+ # 工具结果剪枝参数(字符数;dsh 默认 8192/4096,单行 marker)
23
+ PRUNE_HEAD = 8192
24
+ PRUNE_TAIL = 4096
25
+ PRUNE_MARKER = "\n…[中间内容已剪枝,原文见会话日志]…\n"
26
+
27
+ # 尾部保留预算(估算 token)
28
+ RETAIN_TAIL_TOKENS = 8000
29
+ # 单次压缩区域上限(事件数,防一次吞太多)
30
+ MAX_REGION_EVENTS = 50
31
+
32
+
33
+ def estimate_tokens(text: str) -> int:
34
+ """极简估算:4 字符/token(中文偏低估,偏保守触发)。"""
35
+ return max(1, len(text or "") // 4)
36
+
37
+
38
+ def _ev_text(ev) -> str:
39
+ return str(ev.data.get("content") or ev.data.get("stdout") or "")
40
+
41
+
42
+ def prune_text(text: str, *, head=PRUNE_HEAD, tail=PRUNE_TAIL, marker=PRUNE_MARKER) -> str:
43
+ """超长文本头尾剪枝(纯无 LLM 调用)。"""
44
+ if text is None:
45
+ return ""
46
+ if len(text) <= head + tail + len(marker):
47
+ return text
48
+ return text[:head] + marker + text[-tail:]
49
+
50
+
51
+ def select_range(session, *, retain_tail_tokens=RETAIN_TAIL_TOKENS,
52
+ max_region_events=MAX_REGION_EVENTS):
53
+ """选可压缩区域 [start_seq, end_seq](闭区间)。
54
+
55
+ 规则(仿 dsh selectCompactableRange):
56
+ 1. 跳过 system 头节点(从第一条 user_message 起);
57
+ 2. 尾部保留 retain_tail_tokens 预算不参与压缩(retain<=0 表示尾部不保留);
58
+ 3. 全部内容都在尾预算内 → 无可压缩区域,返回 (0, 0);
59
+ 4. 区域末端不能落在 tool 配对中间(回退到配对完成处);
60
+ 5. 区域不超过 max_region_events 个事件。
61
+ 返回 (start_seq, end_seq);无可压缩区域返回 (0, 0)。
62
+ """
63
+ events = session.events()
64
+ surface_events = [e for e in events if e.type in
65
+ ("system_message", "user_message", "assistant_message")]
66
+ if not surface_events:
67
+ return 0, 0
68
+ first_user_seq = next((e.seq for e in surface_events
69
+ if e.type == "user_message"), 0)
70
+ if not first_user_seq:
71
+ return 0, 0
72
+ # 尾部保留:倒推预算。tail_start_seq = 保留尾段的第一个 seq。
73
+ last_seq = surface_events[-1].seq
74
+ if retain_tail_tokens <= 0:
75
+ tail_start_seq = last_seq + 1 # 尾部不保留:全部可压
76
+ else:
77
+ tail_budget = 0
78
+ tail_start_seq = 0 # 0 表示"全部都在预算内"(无区域)
79
+ for ev in reversed(surface_events):
80
+ tail_budget += estimate_tokens(_ev_text(ev))
81
+ if tail_budget >= retain_tail_tokens:
82
+ tail_start_seq = ev.seq
83
+ break
84
+ if not tail_start_seq:
85
+ return 0, 0 # 预算从未被突破 → 无可压缩区域
86
+ # 候选区域 = [first_user_seq, tail_start_seq)
87
+ all_events = session.events()
88
+ by_seq = {e.seq: e for e in all_events}
89
+ end_seq = tail_start_seq - 1
90
+ # 不切断 tool 配对:末端若是 tool_call/tool_result,回退到最近的非 tool 事件
91
+ while end_seq >= first_user_seq:
92
+ ev = by_seq.get(end_seq)
93
+ if ev is None or ev.type not in ("tool_call", "tool_result"):
94
+ break
95
+ end_seq -= 1
96
+ # 区域事件数上限
97
+ region_events = [e for e in all_events
98
+ if first_user_seq <= e.seq <= end_seq]
99
+ if len(region_events) > max_region_events:
100
+ end_seq = region_events[max_region_events - 1].seq
101
+ # 再做一次配对回退
102
+ while end_seq >= first_user_seq:
103
+ ev = by_seq.get(end_seq)
104
+ if ev is None or ev.type not in ("tool_call", "tool_result"):
105
+ break
106
+ end_seq -= 1
107
+ if end_seq < first_user_seq:
108
+ return 0, 0
109
+ return first_user_seq, end_seq
110
+
111
+
112
+ _SUMMARY_SYSTEM = (
113
+ "你是会话压缩器。把以下对话历史压缩成简洁摘要,必须保留:\n"
114
+ "1. 关键决策与结论;2. 涉及的文件路径与命令;3. 未完成事项;\n"
115
+ "4. 错误信息原文。直接输出摘要正文,不要客套。"
116
+ )
117
+
118
+
119
+ def compact_region(session, start_seq: int, end_seq: int, llm_caller,
120
+ *, reason: str = "pressure"):
121
+ """执行一次压缩事务(四件事件,失败仍写 compaction_end 保证日志可见)。
122
+
123
+ llm_caller(messages: list[dict]) -> str:一次 LLM 调用,返回摘要文本。
124
+ """
125
+ tx = session.append("compaction_start",
126
+ {"start_seq": start_seq, "end_seq": end_seq, "reason": reason},
127
+ surface_op="shadow")
128
+ summary = ""
129
+ error = ""
130
+ try:
131
+ region_events = [e for e in session.events()
132
+ if start_seq <= e.seq <= end_seq]
133
+ # 第一段:工具结果先剪枝(省摘要调用的输入 token)
134
+ parts = []
135
+ for ev in region_events:
136
+ text = _ev_text(ev)
137
+ if ev.type == "tool_result":
138
+ text = prune_text(text)
139
+ if text.strip():
140
+ parts.append(f"[{ev.type}#{ev.seq}] {text}")
141
+ region_text = "\n\n".join(parts)
142
+ raw_tokens = estimate_tokens(region_text)
143
+ summary = llm_caller([
144
+ {"role": "system", "content": _SUMMARY_SYSTEM},
145
+ {"role": "user", "content": region_text},
146
+ ])
147
+ summary_tokens = estimate_tokens(summary)
148
+ session.append("compaction_summary",
149
+ {"raw_tokens": raw_tokens,
150
+ "summary_tokens": summary_tokens,
151
+ "start_seq": start_seq, "end_seq": end_seq},
152
+ surface_op="shadow")
153
+ # 台账入账(A 专项:省了多少不再是黑箱)——摘要调用本身的真实消耗
154
+ # (读 region、写 summary)此前完全隐形;saved=净省量与消耗并列成账。
155
+ # record 自吞一切异常,统计绝不拖垮压缩事务。
156
+ try:
157
+ from . import usage as _usage
158
+ _usage.record(source="compaction",
159
+ run_id=str(getattr(session, "run_id", "") or ""),
160
+ tool="compaction",
161
+ usage={"input": raw_tokens,
162
+ "output": summary_tokens,
163
+ "saved": max(0, raw_tokens - summary_tokens)},
164
+ ok=True)
165
+ except Exception:
166
+ pass
167
+ # surface replace:把区域折叠为一条 user 摘要消息
168
+ session.append("user_message", {
169
+ "content": f"[系统压缩摘要 {start_seq}-{end_seq},原因 {reason}]\n{summary}",
170
+ }, surface_op={"op": "replace", "start_seq": start_seq, "end_seq": end_seq})
171
+ except Exception as e:
172
+ error = repr(e)
173
+ log.warning("compact_region failed: %s", error)
174
+ session.append("compaction_summary", {"error": error}, surface_op="shadow")
175
+ finally:
176
+ session.append("compaction_end",
177
+ {"tx_seq": tx.seq, "error": error},
178
+ surface_op="shadow")
179
+ return not error
180
+
181
+
182
+ def maybe_compact(session, *, model: str = "", llm_caller=None,
183
+ threshold: float = DEFAULT_PRESSURE_THRESHOLD,
184
+ run_id: str = None,
185
+ retain_tail_tokens: int = RETAIN_TAIL_TOKENS) -> bool:
186
+ """压力比 ≥ 阈值时触发一次压缩。返回是否执行了压缩。
187
+
188
+ llm_caller 缺省直接跳过(没有摘要能力就不压)。
189
+ """
190
+ if llm_caller is None:
191
+ return False
192
+ rid = run_id or session.run_id
193
+ ratio = token_meter.pressure_ratio(rid, model=model)
194
+ if ratio < threshold:
195
+ return False
196
+ start, end = select_range(session, retain_tail_tokens=retain_tail_tokens)
197
+ if not start:
198
+ return False
199
+ log.info("compaction triggered run=%s ratio=%.2f region=[%d,%d]",
200
+ rid, ratio, start, end)
201
+ return compact_region(session, start, end, llm_caller, reason=f"pressure={ratio:.2f}")