task_status 在退出码为 0 但日志有报错时附上 errors
取自 codex/story-test-20261003 的 66418dc (DSH) 中 tasks.py 部分及其两项测试:
AALC 每日流程单次战斗失败("未能进入战斗")后仍 exit 0, 只看 succeeded 会误判。
同一提交里的编队滑动补丁未合入: 后台模式分段移动时长 <0.01s 时 AALC 的
_mouse_move_to 步数为 0 不发移动事件, 前台每段受 pyautogui 默认 0.1s 停顿
影响单次滑动约 4.4s, 且行程比 AALC 的 Windows 标定多 60px; 另行重写后实测。
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -85,6 +85,10 @@ codex mcp add limbus --env PYTHONUTF8=1 -- D:\lc-mcp\vendor\aalc\.venv\Scripts\p
|
||||
所以任务启动时先把原值写到 `~/lc_mcp/runs/*.restore.json`;`stop_task` 强停或任务进程异常退出后,
|
||||
服务会据此写回。写回失败时 `task_status` 显示 `pending_restore`,并在写回成功前拒绝启动新任务。
|
||||
|
||||
`state` 只反映任务子进程的退出码。AALC 在单次战斗失败(例如日志里的
|
||||
`超出最大尝试次数,未能进入战斗`)后仍会继续跑完并正常退出,所以 `task_status`
|
||||
会把日志尾部的 `[ERROR]`/异常行放进 `errors` 字段:**看到 `succeeded` 也要先看 `errors`。**
|
||||
|
||||
## 环境变量
|
||||
|
||||
| 变量 | 默认 | 说明 |
|
||||
|
||||
@@ -35,6 +35,22 @@ def tail(path, n):
|
||||
return []
|
||||
|
||||
|
||||
ERROR_MARKERS = ('[ERROR]', 'Traceback (most recent call last)')
|
||||
ERROR_SCAN_LINES = 400
|
||||
|
||||
|
||||
def log_errors(path, limit=3):
|
||||
"""挑出日志尾部的报错行。
|
||||
|
||||
AALC 的每日流程在单次战斗失败后会继续跑完并正常退出(exit 0), 只看退出码
|
||||
会把"超出最大尝试次数,未能进入战斗"这类失败当成成功, 所以状态里额外附上
|
||||
日志里的报错行。
|
||||
"""
|
||||
hits = [line for line in tail(path, ERROR_SCAN_LINES)
|
||||
if any(marker in line for marker in ERROR_MARKERS)]
|
||||
return hits[-limit:]
|
||||
|
||||
|
||||
class TaskRunner:
|
||||
|
||||
def __init__(self, run_dir, argv_factory=default_argv, keep_logs=20, restore_argv=default_restore_argv):
|
||||
@@ -112,6 +128,9 @@ class TaskRunner:
|
||||
'log': self.info['log'], 'log_tail': tail(self.info['log'], max(1, min(int(lines), 200)))}
|
||||
if code is not None:
|
||||
r['exit_code'] = code
|
||||
errors = log_errors(self.info['log'])
|
||||
if errors:
|
||||
r['errors'] = errors
|
||||
pending = self._pending_restores()
|
||||
if pending:
|
||||
r['pending_restore'] = [os.path.basename(p) for p in pending]
|
||||
|
||||
@@ -635,5 +635,37 @@ class TransportTest(unittest.TestCase):
|
||||
self.assertEqual((state['ok'], state['items']), (True, []))
|
||||
|
||||
|
||||
class TaskErrorSurfaceTest(unittest.TestCase):
|
||||
"""AALC 每日流程单次战斗失败后仍 exit 0; 状态里必须带出日志报错。"""
|
||||
|
||||
CHILD = ('import sys; '
|
||||
'sys.stdout.write("[ERROR] [AALC] tasks\\\\battle\\\\battle.py:83: '
|
||||
'超出最大尝试次数,未能进入战斗\\n"); '
|
||||
'sys.stdout.write("[lc_mcp] task daily done\\n"); '
|
||||
'sys.stdout.flush()')
|
||||
|
||||
def test_exit_zero_with_battle_error_is_reported(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', self.CHILD])
|
||||
self.assertTrue(runner.start('daily', {'exp': 1})['ok'])
|
||||
deadline = time.time() + 15
|
||||
while time.time() < deadline and runner.running():
|
||||
time.sleep(0.05)
|
||||
status = runner.status()
|
||||
self.assertEqual((status['state'], status['exit_code']), ('succeeded', 0), status)
|
||||
self.assertIn('未能进入战斗', status['errors'][-1])
|
||||
|
||||
def test_clean_log_has_no_errors_field(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', 'print("[lc_mcp] ok")'])
|
||||
self.assertTrue(runner.start('daily', {})['ok'])
|
||||
deadline = time.time() + 15
|
||||
while time.time() < deadline and runner.running():
|
||||
time.sleep(0.05)
|
||||
status = runner.status()
|
||||
self.assertEqual(status['state'], 'succeeded')
|
||||
self.assertNotIn('errors', status)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user