Open
fix(workflow): retain incomplete log records for the next poll #240
tianjunAdd on Oct 30
fix(workflow): retain incomplete log records for the next poll #240
Open
Total: 2 files changed+446-167
| @@ -15,7 +15,7 @@ stdout 即本轮要展示给用户的文本(调用方原样转播,不做任 | |||
| 15 | 以空行分隔,全空则无输出: | 15 | 以空行分隔,全空则无输出: |
| 16 | 1. 关键事件行(仅增量:自上次调用以来 log.jsonl 的新增事件中筛选 update/ | 16 | 1. 关键事件行(仅增量:自上次调用以来 log.jsonl 的新增事件中筛选 update/ |
| 17 | rollback/approval/approval_ignored 渲染;init/verdict 等其余类型与解析失败 | 17 | rollback/approval/approval_ignored 渲染;init/verdict 等其余类型与解析失败 |
| 18 | - 的行除外——解析失败的行原文展示不丢信息): | 18 | + 的行除外——已换行但解析失败的行原文展示不丢信息): |
| 19 | [HH:MM:SS] <task_id>: <from> → <to>(→ awaiting_approval 时附"(进入待审批)") | 19 | [HH:MM:SS] <task_id>: <from> → <to>(→ awaiting_approval 时附"(进入待审批)") |
| 20 | [HH:MM:SS] 回滚 <from> → <to> | 20 | [HH:MM:SS] 回滚 <from> → <to> |
| 21 | [HH:MM:SS] <task_id>: 审批 <decision>(:comment 截断 80 字) | 21 | [HH:MM:SS] <task_id>: 审批 <decision>(:comment 截断 80 字) |
| @@ -36,7 +36,8 @@ stdout 即本轮要展示给用户的文本(调用方原样转播,不做任 | |||
| 36 | title/acceptance/require_approval 从 workflow yaml(含子图文件)解析 | 36 | title/acceptance/require_approval 从 workflow yaml(含子图文件)解析 |
| 37 | 37 | ||
| 38 | 已读事件偏移持久化在 .workflow/skill_poll.state,调用方无需记账。log.jsonl | 38 | 已读事件偏移持久化在 .workflow/skill_poll.state,调用方无需记账。log.jsonl |
| 39 | -只增不删(回滚也是追加事件),偏移量单调安全。status.json 不存在(编排器 | 39 | +只增不删(回滚也是追加事件)。末尾未换行的有效 JSON 仍立即展示;尚未写完的 |
| 40 | +JSON 或 UTF-8 尾字节留待下轮补全,不推进该行偏移。status.json 不存在(编排器 | ||
| 40 | 尚未初始化)→ 仅可能有事件段;log/status 都不存在 → 无输出,exit 0; | 41 | 尚未初始化)→ 仅可能有事件段;log/status 都不存在 → 无输出,exit 0; |
| 41 | 读取/解析失败 → stderr 报错 exit 2(状态不落盘,下轮重试不丢事件)。 | 42 | 读取/解析失败 → stderr 报错 exit 2(状态不落盘,下轮重试不丢事件)。 |
| 42 | 先写 stdout 再落状态:中途崩溃宁可下轮重播。无待审批任务时不加载 yaml | 43 | 先写 stdout 再落状态:中途崩溃宁可下轮重播。无待审批任务时不加载 yaml |
| @@ -44,6 +45,8 @@ stdout 即本轮要展示给用户的文本(调用方原样转播,不做任 | |||
| 44 | """ | 45 | """ |
| 45 | 46 | ||
| 46 | import argparse | 47 | import argparse |
| 48 | +import codecs | ||
| 49 | +import io | ||
| 47 | import json | 50 | import json |
| 48 | import logging | 51 | import logging |
| 49 | import os | 52 | import os |
| @@ -282,7 +285,7 @@ def write_state(wf_dir, state): | |||
| 282 | 285 | ||
| 283 | 286 | ||
| 284 | def read_events(wf_dir, state): | 287 | def read_events(wf_dir, state): |
| 285 | - """读取偏移之后的事件;读取失败不推进持久化偏移。""" | 288 | + """读取完整事件;有效 JSON 可无末尾换行,未写完的尾记录留待下轮。""" |
| 286 | offset = state.get("events_offset") | 289 | offset = state.get("events_offset") |
| 287 | if not isinstance(offset, int) or isinstance(offset, bool) or offset < 0: | 290 | if not isinstance(offset, int) or isinstance(offset, bool) or offset < 0: |
| 288 | offset = 0 | 291 | offset = 0 |
| @@ -290,16 +293,27 @@ def read_events(wf_dir, state): | |||
| 290 | if not os.path.isfile(log_path): | 293 | if not os.path.isfile(log_path): |
| 291 | return [], offset, None | 294 | return [], offset, None |
| 292 | try: | 295 | try: |
| 293 | - with open(log_path, encoding="utf-8") as f: | 296 | + with open(log_path, "rb") as f: |
| 294 | - lines = f.readlines() | 297 | + decoder = codecs.getincrementaldecoder("utf-8")() |
| 295 | - except OSError as e: | 298 | + content = decoder.decode(f.read(), final=False) |
| 299 | + lines = io.StringIO(content, newline=None).readlines() | ||
| 300 | + except (OSError, UnicodeDecodeError) as e: | ||
| 296 | return [], offset, "log.jsonl 读取失败: %s" % e | 301 | return [], offset, "log.jsonl 读取失败: %s" % e |
| 302 | + consumed = len(lines) | ||
| 303 | + if lines and not lines[-1].endswith("\n"): | ||
| 304 | + try: | ||
| 305 | + json.loads(lines[-1]) | ||
| 306 | + except ValueError: | ||
| 307 | + consumed -= 1 | ||
| 308 | + else: | ||
| 309 | + if decoder.getstate()[0]: | ||
| 310 | + consumed -= 1 | ||
| 297 | event_lines = [] | 311 | event_lines = [] |
| 298 | - for raw in lines[offset:]: | 312 | + for raw in lines[offset:consumed]: |
| 299 | text = render_event(raw.rstrip("\n")) | 313 | text = render_event(raw.rstrip("\n")) |
| 300 | if text: | 314 | if text: |
| 301 | event_lines.append(text) | 315 | event_lines.append(text) |
| 302 | - return event_lines, len(lines), None | 316 | + return event_lines, consumed, None |
| 303 | 317 | ||
| 304 | 318 | ||
| 305 | def pending_approval_ids(tasks, work_dir): | 319 | def pending_approval_ids(tasks, work_dir): |