-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC02_recover_task_workflow.py
More file actions
460 lines (398 loc) · 15.9 KB
/
C02_recover_task_workflow.py
File metadata and controls
460 lines (398 loc) · 15.9 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
# -*- encoding: utf-8 -*-
"""
@File: C03_recover_task_workflow.py
@Modify Time: 2026/2/25
@Author: Kevin-Chen
@Descriptions: 恢复被中断的任务拆分工作流(A02)
"""
import argparse
import json
import os
import re
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timedelta
from A02_task_workflow import DIRECTOR_SUCCESS_TEXT, base_director_prompt, prepare_agent_prompt
from B00_agent_config import agent_names_list, run_agent, today_str, working_path
from B02_log_tools import Colors, log_message
from B03_init_function_agents import init_agent
DIRECTOR_NAME = "调度器"
STATE_FILE_NAME = "task_workflow_state.json"
_TOKEN_RE = re.compile(r"^--(.+?)--$")
def _is_separator_line(line):
stripped = line.strip()
if not stripped:
return False
if len(stripped) < 50:
return False
return all(ch == "=" for ch in stripped)
def _split_log_entries(text):
entries = []
buf = []
for raw_line in text.splitlines():
if _is_separator_line(raw_line):
entry = "\n".join(buf).strip("\n")
if entry:
entries.append(entry)
buf = []
else:
buf.append(raw_line)
entry = "\n".join(buf).strip("\n")
if entry:
entries.append(entry)
return entries
def _extract_token(line):
match = _TOKEN_RE.match(line.strip())
if match:
return match.group(1)
return None
def _parse_output_entry(entry, expected_agent_name=None):
lines = entry.splitlines()
for idx in range(len(lines) - 1):
session_id = _extract_token(lines[idx])
agent_name = _extract_token(lines[idx + 1])
if session_id and agent_name:
if expected_agent_name and agent_name != expected_agent_name:
continue
message = "\n".join(lines[idx + 2:]).strip()
return {
"session_id": session_id,
"agent_name": agent_name,
"message": message,
}
return None
def _find_last_output(entries, agent_name):
for entry in reversed(entries):
parsed = _parse_output_entry(entry, expected_agent_name=agent_name)
if parsed:
return parsed
return None
def _find_latest_json_output(entries, agent_name, strict_json):
for entry in reversed(entries):
parsed = _parse_output_entry(entry, expected_agent_name=agent_name)
if not parsed:
continue
msg_dict = _try_parse_json(parsed["message"], strict_json=strict_json)
if not msg_dict:
continue
try:
msg_dict = normalize_director_payload(msg_dict, allowed_success_values={DIRECTOR_SUCCESS_TEXT})
except ValueError:
continue
return parsed, msg_dict
return None, None
def _find_prompt_index(entries, prompt_text):
if not prompt_text:
return None
for idx in range(len(entries) - 1, -1, -1):
if prompt_text in entries[idx]:
return idx
return None
def _find_next_output_after(entries, start_index, agent_name):
for idx in range(start_index + 1, len(entries)):
parsed = _parse_output_entry(entries[idx], expected_agent_name=agent_name)
if parsed:
return parsed
return None
def _read_log_entries(log_path):
if not log_path or not os.path.exists(log_path):
return []
with open(log_path, "r", encoding="utf-8") as f:
return _split_log_entries(f.read())
def _find_latest_log_file(log_dir, agent_name, max_log_days):
if not log_dir or not os.path.isdir(log_dir):
return None
prefix = f"agent_{agent_name}_"
candidates = []
for name in os.listdir(log_dir):
if not name.startswith(prefix) or not name.endswith(".log"):
continue
path = os.path.join(log_dir, name)
if not os.path.isfile(path):
continue
candidates.append(path)
if not candidates:
return None
candidates.sort(key=lambda p: os.path.getmtime(p), reverse=True)
if max_log_days is None:
return candidates[0]
deadline = datetime.now() - timedelta(days=max_log_days)
for path in candidates:
if datetime.fromtimestamp(os.path.getmtime(path)) >= deadline:
return path
return candidates[0]
def _try_parse_json(text, strict_json):
try:
return json.loads(text)
except json.JSONDecodeError:
if strict_json:
return None
fenced = str(text or "").strip()
if fenced.startswith("```") and fenced.endswith("```"):
fenced = fenced.strip("`").strip()
try:
return json.loads(fenced)
except json.JSONDecodeError:
pass
start = str(text).find("{")
end = str(text).rfind("}")
if start != -1 and end != -1 and end > start:
try:
return json.loads(str(text)[start:end + 1])
except json.JSONDecodeError:
return None
return None
def _load_state(state_path):
if not state_path or not os.path.exists(state_path):
return None
with open(state_path, "r", encoding="utf-8") as f:
return json.load(f)
def _normalize_loaded_state(state):
if not state:
return None
msg_dict = state.get("msg_dict")
if not msg_dict:
return state
state["msg_dict"] = normalize_director_payload(msg_dict, allowed_success_values={DIRECTOR_SUCCESS_TEXT})
return state
def _save_state(state_path, state):
if not state_path:
return
with open(state_path, "w", encoding="utf-8") as f:
json.dump(state, f, ensure_ascii=False, indent=2)
def _log(log_file_path, message, color=Colors.YELLOW):
log_message(log_file_path=log_file_path, message=message, color=color)
def _rebuild_state_from_logs(log_dir, max_log_days, strict_json):
director_log = _find_latest_log_file(log_dir, DIRECTOR_NAME, max_log_days)
if not director_log:
raise RuntimeError("未找到调度器日志,无法恢复。")
director_entries = _read_log_entries(director_log)
director_output, msg_dict = _find_latest_json_output(
director_entries,
DIRECTOR_NAME,
strict_json=strict_json,
)
if not director_output:
if strict_json:
raise RuntimeError("调度器日志中未找到可解析JSON输出,建议关闭 --strict-json 后重试。")
raise RuntimeError("调度器日志中未找到可解析JSON输出,无法恢复。")
director_session_id = director_output["session_id"]
state = {
"phase": "director_ready",
"iteration": 0,
"director_session_id": director_session_id,
"agent_session_id_dict": {},
"pending_agents": [],
"agent_prompts": {},
"agent_responses": {},
"last_director_prompt": "",
"last_director_response": director_output["message"],
"msg_dict": msg_dict,
"updated_at": datetime.now().isoformat(timespec="seconds"),
}
for agent_name in agent_names_list:
agent_log = _find_latest_log_file(log_dir, agent_name, max_log_days)
entries = _read_log_entries(agent_log)
last_output = _find_last_output(entries, agent_name)
if last_output:
state["agent_session_id_dict"][agent_name] = last_output["session_id"]
if "success" in msg_dict:
state["phase"] = "completed"
return state
for agent_name, prompt in msg_dict.items():
if agent_name not in agent_names_list:
raise RuntimeError(f"调度器返回了未知的智能体名称: {agent_name}")
agent_log = _find_latest_log_file(log_dir, agent_name, max_log_days)
entries = _read_log_entries(agent_log)
state["agent_prompts"][agent_name] = prompt
prompt_index = _find_prompt_index(entries, prompt)
if prompt_index is not None:
output = _find_next_output_after(entries, prompt_index, agent_name)
if output:
state["agent_responses"][agent_name] = output["message"]
continue
state["pending_agents"].append(agent_name)
if state["pending_agents"]:
state["phase"] = "agents_pending"
else:
state["phase"] = "director_pending"
return state
def _backfill_agent_sessions_from_logs(state, log_dir, max_log_days):
agent_session_id_dict = state.setdefault("agent_session_id_dict", {})
for agent_name in agent_names_list:
if agent_session_id_dict.get(agent_name):
continue
agent_log = _find_latest_log_file(log_dir, agent_name, max_log_days)
entries = _read_log_entries(agent_log)
last_output = _find_last_output(entries, agent_name)
if last_output:
agent_session_id_dict[agent_name] = last_output["session_id"]
return state
def recover_task_workflow(
state_path=None,
log_dir=None,
prefer_checkpoint=True,
max_log_days=3,
strict_json=False,
allow_reinit_on_missing_session=False,
dry_run=False,
):
if state_path is None:
state_path = os.path.join(working_path, STATE_FILE_NAME)
if log_dir is None:
log_dir = working_path
state = None
if prefer_checkpoint:
state = _load_state(state_path)
try:
state = _normalize_loaded_state(state)
except ValueError:
state = None
if not state:
state = _rebuild_state_from_logs(log_dir, max_log_days, strict_json)
state = _backfill_agent_sessions_from_logs(state, log_dir, max_log_days)
if dry_run:
state["updated_at"] = datetime.now().isoformat(timespec="seconds")
_save_state(state_path, state)
return {
"status": "dry_run",
"phase": state.get("phase"),
"iteration": int(state.get("iteration", 0)),
"pending_agents": state.get("pending_agents", []),
"director_session_id": state.get("director_session_id"),
"agent_session_id_dict": state.get("agent_session_id_dict", {}),
"state_path": state_path,
}
director_log_path = os.path.join(log_dir, f"agent_{DIRECTOR_NAME}_{today_str}.log")
director_session_id = state.get("director_session_id")
if not director_session_id:
raise RuntimeError("调度器 session_id 缺失,无法恢复。")
msg_dict = state.get("msg_dict", {})
iteration = int(state.get("iteration", 0))
if "success" in msg_dict:
return {"status": "completed", "phase": "completed", "iteration": iteration}
agent_session_id_dict = state.get("agent_session_id_dict", {})
agent_responses = state.get("agent_responses", {})
while True:
if not msg_dict:
raise RuntimeError("调度器输出为空,无法继续恢复。")
if "success" in msg_dict:
state.update({
"phase": "completed",
"msg_dict": msg_dict,
"updated_at": datetime.now().isoformat(timespec="seconds"),
})
_save_state(state_path, state)
return {"status": "completed", "phase": "completed", "iteration": iteration}
pending_agent_calls = []
for agent_name, agent_prompt in msg_dict.items():
if agent_name not in agent_names_list:
raise RuntimeError(f"调度器返回了未知的智能体名称: {agent_name}")
if agent_name in agent_responses:
continue
session_id = agent_session_id_dict.get(agent_name)
if not session_id:
if allow_reinit_on_missing_session:
_, session_id = init_agent(agent_name)
agent_session_id_dict[agent_name] = session_id
else:
raise RuntimeError(f"{agent_name} 缺失 session_id,无法恢复。")
agent_log_file_path = os.path.join(log_dir, f"agent_{agent_name}_{today_str}.log")
pending_agent_calls.append({
"agent_name": agent_name,
"agent_prompt": agent_prompt,
"session_id": session_id,
"agent_log_file_path": agent_log_file_path,
})
pending_agents = [item["agent_name"] for item in pending_agent_calls]
with ThreadPoolExecutor(max_workers=max(1, len(pending_agent_calls))) as executor:
futures = {
executor.submit(
run_agent,
item["agent_name"],
item["agent_log_file_path"],
prepare_agent_prompt(item["agent_name"], item["agent_prompt"]),
False,
item["session_id"],
): item
for item in pending_agent_calls
}
for future in as_completed(futures):
item = futures[future]
msg, _ = future.result()
agent_responses[item["agent_name"]] = msg
what_agent_just_use = list(msg_dict.keys())
what_agent_replay = ""
for agent_name in what_agent_just_use:
msg = agent_responses.get(agent_name, "")
what_agent_replay += f"""
{agent_name}:
```
{msg}
```
"""
doing_director_prompt = f"""
---
你刚刚调用的智能体为 {what_agent_just_use} 返回内容如下:
{what_agent_replay}
---
继续按照上述流程进行调度.
"""
doing_director_prompt = base_director_prompt + doing_director_prompt
msg, _ = run_agent(
DIRECTOR_NAME,
director_log_path,
doing_director_prompt,
init_yn=False,
session_id=director_session_id,
)
msg_dict = _try_parse_json(msg, strict_json=strict_json)
if msg_dict:
try:
msg_dict = normalize_director_payload(msg_dict, allowed_success_values={DIRECTOR_SUCCESS_TEXT})
except ValueError:
msg_dict = None
if not msg_dict:
_log(director_log_path, f"调度器返回非JSON,无法解析:\n{msg}", color=Colors.RED)
raise RuntimeError("调度器输出无法解析为JSON,恢复中断。")
iteration += 1
agent_responses = {}
state.update({
"phase": "director_ready",
"iteration": iteration,
"director_session_id": director_session_id,
"agent_session_id_dict": agent_session_id_dict,
"pending_agents": pending_agents,
"agent_prompts": msg_dict,
"agent_responses": {},
"last_director_prompt": doing_director_prompt,
"last_director_response": msg,
"msg_dict": msg_dict,
"updated_at": datetime.now().isoformat(timespec="seconds"),
})
_save_state(state_path, state)
def _build_arg_parser():
parser = argparse.ArgumentParser(description="恢复 A02 任务拆分工作流")
parser.add_argument("--state-path", default=None, help="状态文件路径")
parser.add_argument("--log-dir", default=None, help="日志目录,默认使用 working_path")
parser.add_argument("--max-log-days", default=3, type=int, help="日志回溯天数")
parser.add_argument("--no-prefer-checkpoint", action="store_true", help="不优先使用 checkpoint")
parser.add_argument("--strict-json", action="store_true", help="启用严格调度器 JSON 解析")
parser.add_argument(
"--allow-reinit-on-missing-session",
action="store_true",
help="缺失 session_id 时允许重建智能体会话",
)
parser.add_argument("--dry-run", action="store_true", help="仅重建状态,不执行恢复")
return parser
if __name__ == "__main__":
args = _build_arg_parser().parse_args()
result = recover_task_workflow(
state_path=args.state_path,
log_dir=args.log_dir,
prefer_checkpoint=not args.no_prefer_checkpoint,
max_log_days=args.max_log_days,
strict_json=args.strict_json,
allow_reinit_on_missing_session=args.allow_reinit_on_missing_session,
dry_run=args.dry_run,
)
print(json.dumps(result, ensure_ascii=False, indent=2))