Files
lc-mcp/tests/test_controller_runtime.py
ywsandClaude Opus 5.5 a993210760 合并 codex/mcp-runtime-reliability: 运行可靠性、局状态与决策交接
与 main 上的审查修复及 2131f89 (story-test) 合并, 冲突处理:
- mcp_session: 保留超时/取消客户端, 补回 raise_on_error
- 执行器: 局状态改用 run_state (run_id 绑定), 取代完成后归档 context;
  Jev 请求统一经 jev_client.request_answer (支持 baseUrl/curl, 返回置信度与概率);
  保留截图解码缓存、地图模板缓存、按坐标点按钮、use_last_error 单实例互斥;
  刷新计数与开战标记只在动作成功后更新
- battle_batch: 取消/预算框架 + 只在看到胜率按钮时点胜率再回车;
  回合标签改为包含匹配 (OCR 常输出 'wave1')
- 文档采用 reliability 版本 (已是仓库相对路径), 注明 jev_client
- 测试: 互斥量桩改为 WinDLL/get_last_error, 战斗批次测试按胜率语义重写,
  Jev 测试改为对 jev_client 打桩, 生命周期测试改为轮询; 145 项通过

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 03:12:48 +00:00

221 lines
12 KiB
Python

"""Controller lifecycle and battle regressions using mocked MCP only."""
import contextlib
import ctypes
import io
import json
import os
from pathlib import Path
import sys
import tempfile
import unittest
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import battle_batch
import jev_mirror_agent as agent
from mcp_session import SessionCancelled, SessionTimeout
from run_state import save_json
class BattleBatchTest(unittest.TestCase):
def setUp(self):
self.folder = tempfile.TemporaryDirectory()
self.addCleanup(self.folder.cleanup)
self.stop = Path(self.folder.name) / 'JEV_STOP'
def run_helper(self, client):
session = mock.MagicMock()
session.__enter__.return_value = client
output = io.StringIO()
with mock.patch.object(battle_batch, 'Session', return_value=session), \
mock.patch.object(battle_batch, 'STOP_FILE', self.stop), \
mock.patch.object(sys, 'argv', ['battle_batch.py', '--seconds', '1', '--battle-confirmed']), \
mock.patch.object(battle_batch.time, 'sleep'), contextlib.redirect_stdout(output):
battle_batch.main()
return json.loads(output.getvalue())
def test_terminal_screens_never_receive_more_keys(self):
for text in ('进行判定', '选择遭遇战奖励卡', '参加战斗的全体罪人已经死亡。'):
with self.subTest(text=text):
client = mock.Mock()
client.call.return_value = {'ok': True, 'items': [{'text': text}]}
result = self.run_helper(client)
self.assertEqual(result['pairs'], 0)
self.assertEqual(client.call.call_args_list, [mock.call('get_state')])
def test_stop_after_win_rate_prevents_enter(self):
client = mock.Mock()
def call(tool, args=None):
if tool == 'get_state':
return {'ok': True, 'items': [{'text': 'WAVE 1'}, {'text': 'Win Rate'}]}
self.stop.touch()
return {'ok': True}
client.call.side_effect = call
result = self.run_helper(client)
self.assertTrue(result['stopped'])
self.assertFalse(result['outcome_unknown'])
self.assertEqual(client.call.call_args_list,
[mock.call('get_state'), mock.call('click_text', {'text': 'Win Rate'})])
def test_judgement_after_one_turn_ends_without_more_input(self):
client = mock.Mock()
frames = iter([[{'text': 'WAVE 1'}, {'text': '胜率'}], [{'text': '选择一位罪人来进行判定'}]])
client.call.side_effect = lambda tool, args=None: (
{'ok': True, 'items': next(frames)} if tool == 'get_state' else {'ok': True})
result = self.run_helper(client)
self.assertEqual(result['pairs'], 1)
self.assertEqual([c.args[0] for c in client.call.call_args_list],
['get_state', 'click_text', 'keypress', 'get_state'])
def test_cinematic_without_win_rate_sends_no_input(self):
# P 会在胜率/伤害之间切换, 看不到胜率按钮(特写)时只重新观察
client = mock.Mock()
client.call.return_value = {'ok': True, 'items': [{'text': 'TURN 2'}]}
result = self.run_helper(client)
self.assertEqual(result['pairs'], 0)
self.assertTrue(all(c.args[0] == 'get_state' for c in client.call.call_args_list))
class AgentRuntimeTest(unittest.TestCase):
def setUp(self):
self.folder = tempfile.TemporaryDirectory()
self.addCleanup(self.folder.cleanup)
self.home = Path(self.folder.name)
self.config = self.home / 'config.json'
self.config.write_text('{}')
self.kernel = mock.Mock()
self.kernel.CreateMutexW.return_value = 12345
self.kernel.GetLastError.return_value = 0
def run_agent(self, bridge, extra_args=(), constructor_error=None, fixed=True, executor_decider=None):
output = io.StringIO()
environment = dict(os.environ, LC_FIXED_FALLBACK='1' if fixed else '0')
environment.pop('OPENCODE_API_KEY', None)
with mock.patch.object(agent, 'RUNTIME_HOME', self.home), \
mock.patch.object(agent, 'STOP_FILE', self.home / 'JEV_STOP'), \
mock.patch.object(agent, 'Bridge', return_value=bridge, side_effect=constructor_error) as factory, \
mock.patch.object(ctypes, 'WinDLL', return_value=self.kernel, create=True), \
mock.patch.object(ctypes, 'get_last_error', side_effect=lambda: self.kernel.GetLastError(), create=True), \
mock.patch.object(sys, 'argv', ['jev_mirror_agent.py', str(self.config), '--steps', '1', *extra_args]), \
mock.patch.dict(os.environ, environment, clear=True), \
mock.patch.object(agent.time, 'sleep'), \
mock.patch.object(agent, 'button_enabled', return_value=False), \
contextlib.redirect_stdout(output):
agent.main(executor_decider=executor_decider)
status = json.loads((self.home / 'jev_mirror_status.json').read_text(encoding='utf-8'))
return status, factory
def test_old_claim_context_does_not_complete_new_run_at_lobby(self):
save_json(self.home / 'jev_shop_context.json', {'floor': 5, 'claim_requested': True})
save_json(self.home / 'mirror_current_run.json', {
'start_utc': '2026-10-02T00:00:00+00:00', 'status': 'running',
'dungeon_completed': False, 'final_reward_claimed': False})
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [
{'text': text, 'score': 1} for text in ('探索状态', '进入', '名与蛛之镜')]}
status, _ = self.run_agent(bridge)
run = json.loads((self.home / 'mirror_current_run.json').read_text())
self.assertEqual(status['state'], 'stopped')
self.assertFalse(run['dungeon_completed'])
self.assertFalse(run['final_reward_claimed'])
bridge.close.assert_called_once()
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_bridge_initialization_failure_records_error_and_releases_mutex(self):
status, _ = self.run_agent(mock.Mock(), constructor_error=SessionTimeout('initialization hung'))
self.assertEqual(status['state'], 'requires_analysis')
self.assertTrue(status['outcome_unknown'])
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_stop_file_prevents_bridge_startup(self):
(self.home / 'JEV_STOP').touch()
status, factory = self.run_agent(mock.Mock())
self.assertEqual(status['state'], 'stopped')
factory.assert_not_called()
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_pending_request_cancellation_marks_run_stopped_and_logs_uncertainty(self):
bridge = mock.Mock()
bridge.call.side_effect = SessionCancelled('cancelled pending get_state', True)
status, _ = self.run_agent(bridge)
self.assertEqual(status['state'], 'stopped')
self.assertTrue(status['outcome_unknown'])
run = json.loads((self.home / 'mirror_current_run.json').read_text())
self.assertEqual(run['status'], 'stopped')
rows = (self.home / 'jev_mirror_log.jsonl').read_text().splitlines()
self.assertEqual(json.loads(rows[-1])['state'], 'stopped')
bridge.close.assert_called_once()
def test_expired_budget_prevents_execute_from_sending_any_key(self):
bridge = mock.Mock()
with mock.patch.object(agent, 'STOP_FILE') as stop:
stop.exists.return_value = False
result = agent.execute(bridge, {'tool': 'keypress', 'args': {'key': 'enter'}}, deadline=0)
self.assertTrue(result['stopped'])
bridge.call.assert_not_called()
def test_close_error_still_releases_controller_mutex(self):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': []}
bridge.close.side_effect = RuntimeError('close failed')
with self.assertRaisesRegex(RuntimeError, 'close failed'):
self.run_agent(bridge)
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
def test_no_jev_config_publishes_execution_model_request_without_input(self):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [{'text': '路线', 'score': 1}]}
options = {
'wait': {'description': 'wait', 'action': {'tool': 'wait', 'args': {'seconds': .7}}},
'requires_analysis': {'description': 'inspect', 'action': {'tool': 'requires_analysis', 'args': {}}},
'route_w': {'description': 'event above', 'action': {'tool': 'route', 'args': {'key': 'w'}}},
}
with mock.patch.object(agent, 'candidates', return_value=options), mock.patch.object(agent, 'decide') as decide:
status, _ = self.run_agent(bridge, fixed=False)
self.assertEqual(status['state'], 'requires_decision')
self.assertEqual(status['decision_request']['reason'], 'no_jev_model')
self.assertEqual(bridge.call.call_args_list, [mock.call('get_state')])
decide.assert_not_called()
bridge.close.assert_called_once()
self.assertEqual(self.kernel.CloseHandle.call_count, 1)
pending = json.loads((self.home / 'decision_request.json').read_text(encoding='utf-8'))
run = json.loads((self.home / 'mirror_current_run.json').read_text())
self.assertEqual(pending['run_id'], run['run_id'])
self.assertEqual(run['status'], 'requires_decision')
def test_pending_execution_model_decision_can_resume_same_run(self):
save_json(self.home / 'mirror_current_run.json', {
'run_id': 'pending-run', 'start_utc': '2026-10-02T00:00:00+00:00',
'status': 'requires_decision', 'dungeon_completed': False, 'final_reward_claimed': False})
save_json(self.home / 'jev_shop_context.json', {'run_id': 'pending-run', 'floor': 3})
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': []}
status, _ = self.run_agent(bridge, extra_args=['--resume'])
self.assertEqual(status['state'], 'stopped')
self.assertEqual(status['run_id'], 'pending-run')
def test_embedded_execution_model_selects_and_executes_without_jev(self):
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [{'text': '路线', 'score': 1}]}
options = {
'wait': {'description': 'wait', 'action': {'tool': 'wait', 'args': {'seconds': .7}}},
'requires_analysis': {'description': 'inspect', 'action': {'tool': 'requires_analysis', 'args': {}}},
'route_w': {'description': 'event above', 'action': {'tool': 'route', 'args': {'key': 'w'}}},
}
executor = mock.Mock(side_effect=lambda request: {'request_id': request['request_id'], 'choice': 'route_w'})
with mock.patch.object(agent, 'candidates', return_value=options), mock.patch.object(agent, 'decide') as decide:
status, _ = self.run_agent(bridge, fixed=False, executor_decider=executor)
self.assertEqual(status['state'], 'stopped')
executor.assert_called_once()
decide.assert_not_called()
self.assertEqual(bridge.call.call_args_list, [mock.call('get_state'),
mock.call('keypress', {'key': 'w'}), mock.call('keypress', {'key': 'enter'}),
mock.call('keypress', {'key': 'enter'})])
rows = [json.loads(line) for line in (self.home / 'jev_mirror_log.jsonl').read_text(encoding='utf-8').splitlines()]
self.assertEqual(rows[-1]['decision_engine'], 'executor')
self.assertEqual(rows[-1]['jev_ms'], 0)
if __name__ == '__main__':
unittest.main()