Repository navigation
Expand file tree
/
Copy pathmemory_gc_workflow.py
More file actions
314 lines (275 loc) · 11.1 KB
/
Copy pathmemory_gc_workflow.py
File metadata and controls
314 lines (275 loc) · 11.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
"""MemoryGcWorkflow — bridge-owned /memory-gc --dry-run (Phase 6.5).
The MVP is intentionally read-only. It gathers deterministic memory stats,
loads bounded lesson excerpts, asks the runner for classification JSON, and
renders a dry-run report. It never calls route/archive/maintain scripts.
"""
from __future__ import annotations
import json
import logging
import shlex
import subprocess
import time
from pathlib import Path
from typing import Any
from feishu_bridge.paths import agents_home, claude_home
from feishu_bridge.workflows.runtime import (
STATE_COMPLETED,
STATE_FAILED,
JsonPolicyError,
WorkflowContext,
WorkflowResult,
parse_ttl_seconds,
request_json_with_policy,
)
log = logging.getLogger("feishu-bridge")
_DEFAULT_TTL_SECONDS = 24 * 3600
_CLASSIFY_REQUIRED = ["summary", "daily", "curated", "recommendations"]
_MAX_DAILY_FILES = 8
_MAX_DAILY_CHARS_PER_FILE = 2500
_MAX_CURATED_LINES = 120
_MAX_REPORT_ITEMS = 20
class MemoryGcWorkflow:
"""Read-only memory maintenance dry-run workflow."""
name = "memory-gc"
version = 1
def __init__(self, skill_dir: Path, ttl_string: str | None = "24h") -> None:
self.skill_dir = Path(skill_dir)
self.ttl_seconds = parse_ttl_seconds(ttl_string, _DEFAULT_TTL_SECONDS)
def start(self, ctx: WorkflowContext, arg: str) -> WorkflowResult:
try:
args = shlex.split(arg or "")
except ValueError as exc:
return WorkflowResult(
state=STATE_FAILED,
user_message=f"`/memory-gc` 参数解析失败: {exc}",
error=str(exc),
)
if "--dry-run" not in args:
return WorkflowResult(
state=STATE_FAILED,
user_message=(
"`/memory-gc` 的 bridge MVP 目前只支持只读模式。"
"请使用 `/memory-gc --dry-run`。"
),
error="write mode not implemented",
)
try:
stats = self._run_stats()
except _MemoryGcError as exc:
return WorkflowResult(
state=STATE_FAILED,
user_message=f"`/memory-gc --dry-run` 统计失败: {exc}",
error=str(exc),
)
if self._nothing_to_do(stats):
return WorkflowResult(
state=STATE_COMPLETED,
user_message=self._render_healthy_report(stats),
payload={"stats": stats, "classification": None},
)
prompt = self._build_classify_prompt(ctx, stats)
started = time.time()
try:
classification = request_json_with_policy(
runner_call=lambda p: ctx.runner.run(
p, session_id=ctx.session_id, resume=False,
tag=None, on_output=None,
),
base_prompt=prompt,
schema_required=_CLASSIFY_REQUIRED,
log_prefix=f"memory-gc[{ctx.scope_key}]",
)
except JsonPolicyError as exc:
log.warning(
"memory-gc: JSON policy exhausted in scope %s: %s",
ctx.scope_key, exc,
)
return WorkflowResult(
state=STATE_FAILED,
user_message=(
"`/memory-gc --dry-run` 分类失败:本轮未能生成有效 JSON"
"(已重试 3 次),未修改任何文件。"
),
error=str(exc),
payload={"stats": stats},
)
log.info(
"memory-gc: dry-run ok in scope %s (duration=%.1fs daily=%s curated=%s)",
ctx.scope_key,
time.time() - started,
stats.get("daily_count"),
stats.get("curated_count"),
)
return WorkflowResult(
state=STATE_COMPLETED,
user_message=self._render_dry_run_report(stats, classification),
payload={"stats": stats, "classification": classification},
)
def resume_confirm(
self, ctx: WorkflowContext, payload: dict,
) -> WorkflowResult:
return WorkflowResult(
state=STATE_FAILED,
user_message="`/memory-gc` 写入模式尚未实现;请继续使用 `--dry-run`。",
error="apply not implemented",
payload=payload,
)
def resume_cancel(
self, ctx: WorkflowContext, payload: dict,
) -> WorkflowResult:
return WorkflowResult(
state=STATE_COMPLETED,
user_message="已关闭 `/memory-gc` dry-run 结果;未修改任何文件。",
payload=payload,
)
# ---- deterministic readers --------------------------------------
def _run_stats(self) -> dict[str, Any]:
script = self._script_path("memory-gc-stats.sh")
if script is None:
raise _MemoryGcError("memory-gc-stats.sh not found")
try:
proc = subprocess.run(
["bash", str(script)],
capture_output=True,
text=True,
timeout=15,
)
except subprocess.SubprocessError as exc:
raise _MemoryGcError(f"stats script failed: {exc}") from exc
if proc.returncode != 0:
raise _MemoryGcError(
(proc.stderr or proc.stdout or "").strip()[:500]
or f"stats script exited {proc.returncode}"
)
try:
data = json.loads((proc.stdout or "").strip())
except json.JSONDecodeError as exc:
raise _MemoryGcError(f"stats script returned bad JSON: {exc}") from exc
if not isinstance(data, dict):
raise _MemoryGcError("stats script did not return an object")
return data
def _script_path(self, name: str) -> Path | None:
candidates = [
self.skill_dir / "scripts" / name,
claude_home() / "skills" / "memory-gc" / "scripts" / name,
]
for path in candidates:
if path.is_file():
return path
return None
def _nothing_to_do(self, stats: dict[str, Any]) -> bool:
daily_count = int(stats.get("daily_count") or 0)
curated_count = int(stats.get("curated_count") or 0)
return daily_count == 0 and curated_count <= 80
def _build_classify_prompt(
self, ctx: WorkflowContext, stats: dict[str, Any],
) -> str:
daily_block = self._daily_excerpts(stats)
curated_block = self._curated_excerpt()
return f"""Classify memory maintenance candidates for a dry run.
Return ONLY a JSON object with these keys:
- summary: short human-readable summary
- daily: array of objects with file, action, target, reason
- curated: array of objects with match, action, reason
- recommendations: array of short strings
Allowed daily actions: KEEP, ROUTE, ABSORBED, DUPLICATE, OUTDATED, DISCARD.
Allowed curated actions: KEEP, DELETE, MERGE, UPDATE.
Do not propose file writes. This is a read-only dry-run report.
## Stats
```json
{json.dumps(stats, ensure_ascii=False, indent=2)}
```
## Daily lesson excerpts
{daily_block}
## Curated lesson excerpt
{curated_block}
"""
def _daily_excerpts(self, stats: dict[str, Any]) -> str:
files = stats.get("daily_files") or []
if not isinstance(files, list) or not files:
return "(no daily lesson files)"
blocks: list[str] = []
for raw in files[:_MAX_DAILY_FILES]:
path = Path(str(raw)).expanduser()
try:
text = path.read_text(encoding="utf-8")
except OSError as exc:
blocks.append(f"### {path}\n(read failed: {exc})")
continue
if len(text) > _MAX_DAILY_CHARS_PER_FILE:
text = text[:_MAX_DAILY_CHARS_PER_FILE].rstrip() + "\n...[truncated]"
blocks.append(f"### {path}\n\n{text}")
if len(files) > _MAX_DAILY_FILES:
blocks.append(f"... {len(files) - _MAX_DAILY_FILES} more files omitted")
return "\n\n".join(blocks)
def _curated_excerpt(self) -> str:
path = agents_home() / "rules" / "lessons.md"
if not path.is_file():
path = claude_home() / "rules" / "lessons.md"
if not path.is_file():
return "(no curated lessons file found)"
lines: list[str] = []
try:
for line in path.read_text(encoding="utf-8").splitlines():
if line.lstrip().startswith("- "):
lines.append(line)
if len(lines) >= _MAX_CURATED_LINES:
break
except OSError as exc:
return f"(read failed: {exc})"
return "\n".join(lines) if lines else "(no lesson entries found)"
# ---- rendering ---------------------------------------------------
def _render_healthy_report(self, stats: dict[str, Any]) -> str:
return (
"🧹 `/memory-gc --dry-run` 完成:无需清理。\n\n"
f"- daily lessons: `{int(stats.get('daily_count') or 0)}`\n"
f"- curated lessons: `{int(stats.get('curated_count') or 0)}/80`\n"
f"- sessions: `{int(stats.get('session_count') or 0)}`\n\n"
"未修改任何文件。"
)
def _render_dry_run_report(
self, stats: dict[str, Any], classification: dict[str, Any],
) -> str:
lines = [
"🧹 `/memory-gc --dry-run` 报告(未修改任何文件)",
"",
f"- daily lessons: `{int(stats.get('daily_count') or 0)}`",
f"- curated lessons: `{int(stats.get('curated_count') or 0)}/80`",
f"- sessions: `{int(stats.get('session_count') or 0)}`",
"",
f"Summary: {classification.get('summary') or '(empty)'}",
]
daily = classification.get("daily") or []
if isinstance(daily, list) and daily:
lines.extend(["", "**Daily candidates**"])
for item in daily[:_MAX_REPORT_ITEMS]:
if not isinstance(item, dict):
continue
lines.append(
"- "
f"`{item.get('action', '?')}` "
f"{item.get('file', '(unknown)')} -> "
f"{item.get('target', 'none')}: "
f"{item.get('reason', '')}"
)
curated = classification.get("curated") or []
if isinstance(curated, list) and curated:
lines.extend(["", "**Curated candidates**"])
for item in curated[:_MAX_REPORT_ITEMS]:
if not isinstance(item, dict):
continue
lines.append(
"- "
f"`{item.get('action', '?')}` "
f"{item.get('match', '(unknown)')}: "
f"{item.get('reason', '')}"
)
recommendations = classification.get("recommendations") or []
if isinstance(recommendations, list) and recommendations:
lines.extend(["", "**Recommendations**"])
for rec in recommendations[:_MAX_REPORT_ITEMS]:
lines.append(f"- {rec}")
return "\n".join(lines)
class _MemoryGcError(Exception):
pass
__all__ = ["MemoryGcWorkflow"]