合并 codex/mcp-runtime-reliability: 运行可靠性、局状态与决策交接

Browse Source
与 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>
This commit is contained in:
yws
2026-10-03 03:12:48 +00:00
co-authored by Claude Opus 5.5
24 changed files with 2014 additions and 194 deletions
+220
View File
@@ -0,0 +1,220 @@
"""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()
+125
View File
@@ -0,0 +1,125 @@
"""Provider routing regressions; no game or network calls."""
import json
from pathlib import Path
import sys
import unittest
from unittest import mock
import urllib.error
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
from decision_router import DecisionRequired, DecisionRouter
class DecisionRouterTest(unittest.TestCase):
def setUp(self):
self.options = {
'route_up': {'description': 'Reachable event', 'action': {'tool': 'route', 'args': {'key': 'w'}}},
'wait': {'description': 'Observe again', 'action': {'tool': 'wait', 'args': {'seconds': .7}}},
}
self.state = {'scene': 'map', 'items': [{'text': '事件'}]}
self.jev = mock.Mock(return_value={'choice': 'route_up', 'latency_ms': 10,
'confidence': .8, 'probabilities': {'route_up': .9, 'wait': .1}})
self.config = {'decisionEngine': 'jev', 'model': 'jev-test', 'apiKey': 'private-key'}
def router(self, config=None, **kwargs):
return DecisionRouter(self.config if config is None else config, self.jev, **kwargs)
def test_configured_jev_keeps_model_and_probabilities(self):
result = self.router().choose(self.state, self.options)
self.assertEqual((result.engine, result.choice, result.model, result.jev_ms), ('jev', 'route_up', 'jev-test', 10))
self.assertEqual(result.confidence, .8)
self.assertEqual(result.probabilities, {'route_up': .9, 'wait': .1})
def test_no_model_hands_off_without_using_a_key_from_environment(self):
with self.assertRaises(DecisionRequired) as raised:
self.router({'apiKey': 'private-key'}).choose(self.state, self.options, run_id='run-1')
self.jev.assert_not_called()
request = raised.exception.request
self.assertEqual((request['reason'], request['run_id']), ('no_jev_model', 'run-1'))
self.assertEqual(request['candidates'], self.options)
self.assertNotIn('private-key', json.dumps(request))
def test_model_without_credentials_hands_off(self):
with self.assertRaises(DecisionRequired) as raised:
self.router({'model': 'jev-test'}).choose(self.state, self.options)
self.assertEqual(raised.exception.request['reason'], 'missing_jev_credentials')
self.jev.assert_not_called()
def test_executor_mode_never_calls_jev_even_when_configured(self):
result = self.router(dict(self.config, decisionEngine='executor'), executor_decider=lambda request: 'wait').choose(self.state, self.options)
self.assertEqual((result.engine, result.choice, result.jev_ms), ('executor', 'wait', 0))
self.jev.assert_not_called()
def test_auto_uses_jev_when_model_and_key_are_both_present(self):
executor = mock.Mock()
result = self.router({'model': 'jev-test', 'apiKey': 'private-key'}, executor_decider=executor).choose(self.state, self.options)
self.assertEqual(result.engine, 'jev')
executor.assert_not_called()
def test_callback_receives_request_and_returns_matching_id(self):
def executor(request):
self.assertEqual(request['page_state']['scene'], 'map')
self.assertNotIn('apiKey', request)
return {'request_id': request['request_id'], 'choice': 'route_up', 'model': 'active-executor'}
result = self.router({}, executor_decider=executor).choose(self.state, self.options)
self.assertEqual((result.engine, result.model), ('executor', 'active-executor'))
def test_http_limit_hands_off_with_failed_attempt_timing(self):
self.jev.side_effect = urllib.error.HTTPError('https://example.invalid', 429, 'rate limit', {}, None)
with self.assertRaises(DecisionRequired) as raised:
self.router().choose(self.state, self.options)
request = raised.exception.request
self.assertEqual(request['reason'], 'jev_http_429')
self.assertGreaterEqual(request['failed_jev_ms'], 0)
self.assertNotIn('private-key', json.dumps(request))
def test_timeout_uses_available_execution_model(self):
self.jev.side_effect = TimeoutError('timed out')
result = self.router(executor_decider=lambda request: 'route_up').choose(self.state, self.options)
self.assertEqual((result.engine, result.choice), ('executor', 'route_up'))
self.assertEqual(result.fallback_reason, 'jev_unavailable_TimeoutError')
def test_jev_unknown_candidate_hands_off_instead_of_sending_input(self):
self.jev.return_value = {'choice': 'invented_click'}
result = self.router(executor_decider=lambda request: 'wait').choose(self.state, self.options)
self.assertEqual(result.fallback_reason, 'jev_unavailable_ValueError')
def test_executor_cannot_return_unlisted_action(self):
with self.assertRaisesRegex(ValueError, 'unsupported candidate'):
self.router({}, executor_decider=lambda request: 'invented_click').choose(self.state, self.options)
def test_executor_response_from_another_request_is_rejected(self):
with self.assertRaisesRegex(ValueError, 'different decision request'):
self.router({}, executor_decider=lambda request: {'request_id': 'old-request', 'choice': 'wait'}).choose(self.state, self.options)
def test_callback_cannot_change_request_id_or_executable_actions(self):
def executor(request):
request['request_id'] = 'changed'
request['candidates']['route_up']['action']['args']['key'] = 'enter'
return {'request_id': 'changed', 'choice': 'route_up'}
with self.assertRaisesRegex(ValueError, 'different decision request'):
self.router({}, executor_decider=executor).choose(self.state, self.options)
self.assertEqual(self.options['route_up']['action']['args']['key'], 'w')
def test_forced_fixed_mode_preserves_explicit_legacy_choice(self):
result = self.router(force_fixed=True, fixed_decider=lambda options: 'wait').choose(self.state, self.options)
self.assertEqual((result.engine, result.choice), ('fixed', 'wait'))
self.jev.assert_not_called()
def test_empty_candidates_and_invalid_mode_are_errors(self):
with self.assertRaisesRegex(ValueError, 'at least one candidate'):
self.router().choose(self.state, {})
with self.assertRaisesRegex(ValueError, 'decisionEngine'):
self.router({'decisionEngine': 'unknown'})
def test_non_finite_confidence_or_invalid_probabilities_hands_off(self):
for invalid in ({'confidence': float('nan')}, {'probabilities': {'route_up': 1}},
{'probabilities': {'route_up': .8, 'wait': .8}}):
with self.subTest(invalid=invalid):
self.jev.return_value = dict(choice='route_up', **invalid)
result = self.router(executor_decider=lambda request: 'wait').choose(self.state, self.options)
self.assertEqual(result.engine, 'executor')
if __name__ == '__main__':
unittest.main()
+28 -22
View File
@@ -7,6 +7,7 @@ from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import jev_mirror_agent as agent
import jev_client # noqa: E402 Jev 请求经由 jev_client 发出
class SceneRegressionTest(unittest.TestCase):
@@ -92,13 +93,29 @@ class SceneRegressionTest(unittest.TestCase):
self.assertEqual(agent.fixed_fallback(options),'prepare_thunderbranch')
def test_jev_rate_limit_fails_without_sleep_or_second_request(self):
error=agent.urllib.error.HTTPError('https://example.test',429,'rate limit',{},None)
with mock.patch.object(agent.urllib.request,'urlopen',side_effect=error) as request, mock.patch.object(agent.time,'sleep') as sleep:
with self.assertRaises(agent.urllib.error.HTTPError):
agent.decide({'apiKey':'test'}, {'items':[]},{'wait':{'description':'wait'}},[])
error=jev_client.urllib.error.HTTPError('https://example.test',429,'rate limit',{},None)
with mock.patch.object(jev_client.urllib.request,'urlopen',side_effect=error) as request, mock.patch.object(jev_client.time,'sleep') as sleep:
with self.assertRaises(jev_client.urllib.error.HTTPError):
agent.decide({'apiKey':'test','model':'jev-test'}, {'items':[]},{'wait':{'description':'wait'}},[])
self.assertEqual(request.call_count,1)
sleep.assert_not_called()
def test_jev_uses_configured_model_and_returns_probabilities_without_images(self):
response = mock.MagicMock()
response.__enter__.return_value = response
response.read.return_value = b'{"model":"jev-test","answers":{"action":{"type":"choice","choice":"wait","confidence":0.5,"probabilities":{"wait":0.75,"inspect":0.25}}}}'
state = {'screen': {'path': 'LOCAL_SCREENSHOT_ONLY.png'},
'items': [{'text': 'EVENT', 'x': 10, 'y': 20}]}
options = {'wait': {'description': 'wait'}, 'inspect': {'description': 'inspect'}}
with mock.patch.object(jev_client.urllib.request, 'urlopen', return_value=response) as request:
result = agent.decide({'apiKey': 'test', 'model': 'jev-test'}, state, options, [])
payload = agent.json.loads(request.call_args.args[0].data.decode('utf-8'))
self.assertEqual(payload['model'], 'jev-test')
self.assertIsInstance(payload['state'], str)
self.assertNotIn('LOCAL_SCREENSHOT_ONLY', payload['state'])
self.assertEqual(result['confidence'], .5)
self.assertEqual(result['probabilities'], {'wait': .75, 'inspect': .25})
def test_single_extra_gift_decline_preserves_core_and_multi_cards(self):
state = {'items': [{'text':t,'x':100,'y':100,'w':100,'h':40} for t in
('获得E.G.O饰品','石版字符','拒绝饰品','选择')]}
@@ -512,30 +529,19 @@ class MainLoopTest(unittest.TestCase):
agent.main()
return make_bridge
def test_completed_run_archives_context_for_next_run(self):
with tempfile.TemporaryDirectory() as folder:
home = Path(folder)
(home / 'jev_shop_context.json').write_text(json.dumps(
{'refresh_used': 0, 'floor': 5, 'claim_requested': True, 'battery_obtained': True}))
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [
{'text': t, 'x': 0, 'y': 0, 'w': 1, 'h': 1, 'score': 1.0} for t in ('探索状态', '进入')]}
self.run_main(home, bridge)
self.assertEqual(json.loads((home / 'jev_mirror_status.json').read_text())['state'], 'completed')
# 否则下一局一启动就因 claim_requested 被判定为“已完成”
self.assertFalse((home / 'jev_shop_context.json').exists())
self.assertEqual(len(list(home.glob('jev_shop_context.*.json'))), 1)
def test_tool_errors_are_observed_instead_of_aborting(self):
with tempfile.TemporaryDirectory() as folder:
home = Path(folder)
bridge = mock.Mock()
bridge.call.return_value = {'ok': False, 'error': '截图失败'}
make_bridge = self.run_main(home, bridge, steps=2)
make_bridge.assert_called_once_with(raise_on_error=False)
make_bridge.assert_called_once()
self.assertIs(make_bridge.call_args.kwargs['raise_on_error'], False)
status = json.loads((home / 'jev_mirror_status.json').read_text())
self.assertNotIn('error', status) # 没有因异常退出, 而是按“截图暂不可用”继续等待
self.assertEqual(status['choice'], 'wait')
# 没有因异常退出, 而是按“截图暂不可用”等待到步数用完
self.assertEqual((status['state'], status.get('reason')), ('stopped', 'step_limit'))
records = [json.loads(line) for line in (home / 'jev_mirror_log.jsonl').read_text().splitlines()]
self.assertEqual([r['choice'] for r in records], ['wait', 'wait'])
class SessionTest(unittest.TestCase):
@@ -545,7 +551,7 @@ class SessionTest(unittest.TestCase):
session = mcp_session.Session.__new__(mcp_session.Session)
session.request = mock.Mock(return_value={'isError': True, 'content': [
{'type': 'text', 'text': json.dumps({'ok': False, 'error': 'text not found'})}]})
session.raise_on_error = False
session.raise_on_error, session.timeout = False, 30
self.assertEqual(session.call('click_text', {'text': 'x'}), {'ok': False, 'error': 'text not found'})
session.raise_on_error = True
with self.assertRaises(RuntimeError):
+57 -2
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
"""不需要 Windows/AALC 的测试: python -m unittest discover -s tests"""
import json
import io
import os
import socket
import subprocess
@@ -84,6 +85,55 @@ class ProtocolTest(unittest.TestCase):
self.assertIn('bad arguments', r['error'])
self.assertFalse(call(self.s, 'click')['ok'])
def test_invalid_request_shapes_do_not_escape_or_send_input(self):
for params in (['invalid'], 'invalid', 1, True, None):
response = self.s.handle({'jsonrpc': '2.0', 'id': 7, 'method': 'tools/call', 'params': params})
self.assertEqual(response['error']['code'], -32602)
for arguments in ([], 'invalid', 1, True, None):
response = self.s.handle({'jsonrpc': '2.0', 'id': 8, 'method': 'tools/call',
'params': {'name': 'click', 'arguments': arguments}})
self.assertEqual(response['error']['code'], -32602)
self.assertEqual(self.engine.actions, [])
def test_notifications_never_reply_or_perform_request_only_actions(self):
message = {'jsonrpc': '2.0', 'method': 'tools/call',
'params': {'name': 'click', 'arguments': {'x': 1, 'y': 2}}}
self.assertIsNone(self.s.handle(message))
self.assertEqual(self.engine.actions, [])
self.assertIsNotNone(self.s.handle(dict(message, id=None)))
def test_malformed_messages_allow_following_valid_request(self):
messages = ['null', '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":[1]}',
'{"jsonrpc":"2.0","id":2,"method":"ping"}']
stdin, stdout = io.StringIO('\n'.join(messages) + '\n'), io.StringIO()
srv_mod.serve_lines(self.s, stdin.readline, stdout.write)
responses = [json.loads(line) for line in stdout.getvalue().splitlines()]
self.assertEqual([r.get('error', {}).get('code') for r in responses[:2]], [-32600, -32602])
self.assertEqual(responses[2], {'jsonrpc': '2.0', 'id': 2, 'result': {}})
def test_invalid_envelope_returns_request_error(self):
for updates in ({'jsonrpc': '1.0'}, {'method': []}, {'id': True}, {'id': []}):
message = dict({'jsonrpc': '2.0', 'id': 9, 'method': 'ping'}, **updates)
self.assertEqual(self.s.handle(message)['error']['code'], -32600)
def test_task_start_cannot_race_an_input_already_in_progress(self):
entered, release = threading.Event(), threading.Event()
self.engine.click = lambda *args: (entered.set(), release.wait(3))
worker = threading.Thread(target=lambda: call(self.s, 'click', x=1, y=2))
worker.start()
try:
self.assertTrue(entered.wait(2))
started = time.monotonic()
response = call(self.s, 'run_task', task='mirror')
self.assertFalse(response['ok'])
self.assertIsNone(self.tasks.proc)
self.assertLess(time.monotonic() - started, 1)
self.assertEqual(call(self.s, 'task_status')['state'], 'idle')
self.assertTrue(call(self.s, 'stop_task')['ok'])
finally:
release.set()
worker.join(3)
def test_drag_duration_and_rejected_arguments(self):
r = call(self.s, 'drag', from_x=200, from_y=300, to_x=200, to_y=600, duration=1.5)
self.assertTrue(r['ok'])
@@ -123,8 +173,13 @@ class ProtocolTest(unittest.TestCase):
self.assertIn('stop_task', call(self.s, 'drag', from_x=1, from_y=2,
to_x=3, to_y=4)['error'])
self.assertTrue(call(self.s, 'get_state')['ok']) # 只读工具仍可用
time.sleep(0.5)
st = call(self.s, 'task_status')
# 轮询等子进程输出, 不用固定等待 (机器负载高时 0.5s 不够)
deadline = time.time() + 10
while True:
st = call(self.s, 'task_status')
if st['log_tail'] or time.time() > deadline:
break
time.sleep(0.05)
self.assertEqual(st['state'], 'running')
self.assertEqual(st['params'], {'count': 2})
self.assertIn("child ['mirror', '{\"count\": 2}']", '\n'.join(st['log_tail']))
+156
View File
@@ -0,0 +1,156 @@
"""Real pipe regressions with a dummy worker; never connects to a game service."""
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import threading
import time
import unittest
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
from mcp_session import Session, SessionCancelled, SessionTimeout
WORKER = '''import json,os,sys,time
mode=os.environ['WORKER_MODE']
for line in sys.stdin:
message=json.loads(line)
with open(os.environ['WORKER_LOG'],'a',encoding='utf-8') as log:
log.write(json.dumps(message)+'\\n')
mid=message.get('id')
if mode=='initialize-failure':
sys.exit(1)
if mid is None:
continue
if message['method']=='initialize':
if mode=='stderr-burst':
sys.stderr.write('diagnostic\\n'*50000)
sys.stderr.flush()
result={}
elif mode=='silent':
continue
elif mode=='malformed':
print('invalid json',flush=True)
continue
else:
result={'content':[{'type':'text','text':json.dumps({'ok':True,'text':'中文'})}]}
print(json.dumps({'jsonrpc':'2.0','id':mid,'result':result}),flush=True)
if mode=='no-read':
time.sleep(60)
'''
class SessionTest(unittest.TestCase):
def setUp(self):
self.folder = tempfile.TemporaryDirectory()
self.addCleanup(self.folder.cleanup)
self.log = Path(self.folder.name) / 'requests.jsonl'
self.children = []
def worker(self, mode, **options):
real_popen = subprocess.Popen
def launch(*args, **kwargs):
kwargs['env'] = dict(kwargs['env'], WORKER_MODE=mode, WORKER_LOG=str(self.log))
child = real_popen([sys.executable, '-u', '-c', WORKER], **kwargs)
self.children.append(child)
return child
with mock.patch('mcp_session.subprocess.Popen', side_effect=launch):
return Session(**options)
def test_silent_response_times_out_without_retry_and_closes_child(self):
client = self.worker('silent', timeout=.5)
self.addCleanup(client.close)
started = time.monotonic()
with self.assertRaises(SessionTimeout) as caught:
client.call('click', {'x': 1, 'y': 2})
self.assertTrue(caught.exception.outcome_unknown)
self.assertEqual(caught.exception.tool, 'click')
self.assertLess(time.monotonic() - started, 3)
self.assertIsNotNone(client.process.poll())
rows = [json.loads(line) for line in self.log.read_text().splitlines()]
self.assertEqual(sum(row['method'] == 'tools/call' for row in rows), 1)
with self.assertRaises(ConnectionError):
client.call('get_state')
def test_stop_interrupts_pending_response(self):
cancelled = threading.Event()
client = self.worker('silent', timeout=5, cancelled=cancelled.is_set)
self.addCleanup(client.close)
timer = threading.Timer(.2, cancelled.set)
timer.start()
self.addCleanup(timer.cancel)
started = time.monotonic()
with self.assertRaises(SessionCancelled) as caught:
client.call('keypress', {'key': 'enter'})
self.assertTrue(caught.exception.outcome_unknown)
self.assertLess(time.monotonic() - started, 3)
self.assertIsNotNone(client.process.poll())
def test_stderr_burst_does_not_block_handshake_and_chinese_response(self):
with self.worker('stderr-burst', timeout=5) as client:
self.assertEqual(client.call('get_state')['text'], '中文')
self.assertLessEqual(len(client.stderr_tail), 20)
def test_initialization_failure_closes_every_pipe(self):
with self.assertRaises(ConnectionError):
self.worker('initialize-failure', timeout=2)
child = self.children[0]
self.assertIsNotNone(child.poll())
self.assertTrue(all(stream.closed for stream in (child.stdin, child.stdout, child.stderr)))
def test_invalid_response_closes_session(self):
client = self.worker('malformed', timeout=2)
self.addCleanup(client.close)
with self.assertRaises(ConnectionError):
client.call('get_state')
self.assertIsNotNone(client.process.poll())
def test_blocked_stdin_write_is_also_bounded(self):
client = self.worker('no-read', timeout=.5)
self.addCleanup(client.close)
started = time.monotonic()
with self.assertRaises(SessionTimeout):
client.call('type_text', {'text': 'x' * 500000})
self.assertLess(time.monotonic() - started, 3)
self.assertIsNotNone(client.process.poll())
self.assertFalse(any(thread.is_alive() for thread in client._threads))
def test_long_wait_tool_has_a_response_budget_beyond_its_polling_timeout(self):
client = Session.__new__(Session)
client.timeout = 30
client.request = mock.Mock(return_value={'content': [{'text': '{"ok":true}'}]})
client.call('wait_for_text', {'text': '开始', 'timeout': 300})
self.assertEqual(client.request.call_args.args[2], 305)
def test_session_uses_real_bridge_against_isolated_fake_server(self):
from lc_mcp.fake_engine import FakeEngine
from lc_mcp.protocol import Server
from lc_mcp.server import make_tcp
from lc_mcp.tasks import TaskRunner
home = Path(self.folder.name)
runtime = home / 'lc_mcp'
runtime.mkdir()
(runtime / 'token').write_text('test-only-token')
engine = FakeEngine()
tcp = make_tcp(Server(engine, TaskRunner(str(home / 'runs'))), '127.0.0.1', 0, 'test-only-token')
serving = threading.Thread(target=tcp.serve_forever, daemon=True)
serving.start()
try:
with mock.patch.dict(os.environ, {'LCP_PORT': str(tcp.server_address[1]),
'HOME': str(home), 'USERPROFILE': str(home)}):
with Session(timeout=5) as client:
self.assertTrue(client.call('get_state')['ok'])
self.assertTrue(client.call('drag', {'from_x': 1, 'from_y': 2,
'to_x': 3, 'to_y': 4})['ok'])
self.assertTrue(client.call('type_text', {'text': '中文'})['ok'])
self.assertEqual(engine.actions[-1], ('type', '中文'))
finally:
tcp.shutdown()
tcp.server_close()
serving.join(2)
if __name__ == '__main__':
unittest.main()
+111
View File
@@ -0,0 +1,111 @@
import json
from pathlib import Path
import sys
import tempfile
import unittest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
from run_state import prepare_run, save_json, record_claim, claim_is_current, complete_run
class RunStateTest(unittest.TestCase):
def setUp(self):
self.folder = tempfile.TemporaryDirectory()
self.addCleanup(self.folder.cleanup)
self.home = Path(self.folder.name)
self.run_path = self.home / 'mirror_current_run.json'
self.context_path = self.home / 'jev_shop_context.json'
def test_completed_run_starts_with_fresh_floor_and_no_claim_or_upgrades(self):
old_run, old_context = prepare_run(self.home)
old_run['status'] = 'completed'
old_context.update(floor=5, claim_requested=True, symbol_upgraded=True, battle_context=True)
save_json(self.run_path, old_run)
save_json(self.context_path, old_context)
run, context = prepare_run(self.home)
self.assertNotEqual(run['run_id'], old_run['run_id'])
self.assertEqual(context, {'run_id': run['run_id'], 'floor': 1, 'refresh_used': 0})
self.assertFalse(claim_is_current(run, context))
def test_resume_keeps_matching_run_and_verified_claim(self):
old_run, context = prepare_run(self.home)
context.update(floor=4, symbol_upgraded=True)
record_claim(self.home, old_run, context, {'path': 'completion.png'})
run, resumed = prepare_run(self.home, resume=True)
self.assertEqual(run['run_id'], old_run['run_id'])
self.assertEqual(run['start_utc'], old_run['start_utc'])
self.assertEqual(resumed['floor'], 4)
self.assertTrue(resumed['symbol_upgraded'])
self.assertTrue(claim_is_current(run, resumed))
def test_explicit_new_run_discards_even_unfinished_state(self):
old_run, _ = prepare_run(self.home)
run, _ = prepare_run(self.home, new_run=True)
self.assertNotEqual(run['run_id'], old_run['run_id'])
def test_resume_requires_an_unfinished_record(self):
with self.assertRaises(ValueError):
prepare_run(self.home, resume=True)
run, _ = prepare_run(self.home)
run['status'] = 'completed'
save_json(self.run_path, run)
with self.assertRaises(ValueError):
prepare_run(self.home, resume=True)
def test_orphan_context_does_not_supply_completion_to_new_run(self):
save_json(self.context_path, {'floor': 5, 'claim_requested': True})
run, context = prepare_run(self.home)
self.assertEqual(context['floor'], 1)
self.assertFalse(claim_is_current(run, context))
def test_legacy_unfinished_run_preserves_upgrades_but_discards_unbound_claim(self):
save_json(self.run_path, {'start_utc': '2026-10-02T18:13:22+08:00', 'status': 'user_takeover'})
save_json(self.context_path, {'floor': 5, 'symbol_upgraded': True,
'claim_requested': True, 'battle_context': True})
run, context = prepare_run(self.home, resume=True)
self.assertEqual(context['floor'], 5)
self.assertTrue(context['symbol_upgraded'])
self.assertNotIn('battle_context', context)
self.assertFalse(claim_is_current(run, context))
self.assertTrue(run['legacy_context_migrated'])
def test_mismatched_ids_fail_before_overwriting_the_context(self):
prepare_run(self.home)
save_json(self.context_path, {'run_id': 'another-run', 'floor': 5})
before = self.context_path.read_bytes()
with self.assertRaises(ValueError):
prepare_run(self.home)
self.assertEqual(self.context_path.read_bytes(), before)
def test_completion_requires_matching_claim_and_keeps_both_screens(self):
run, context = prepare_run(self.home)
with self.assertRaises(ValueError):
complete_run(self.home, run, context, {'path': 'home.png'})
record_claim(self.home, run, context, {'path': 'completion.png'})
context['claim_run_id'] = 'another-run'
with self.assertRaises(ValueError):
complete_run(self.home, run, context, {'path': 'home.png'})
context['claim_run_id'] = run['run_id']
complete_run(self.home, run, context, {'path': 'home.png'})
saved = json.loads(self.run_path.read_text())
self.assertTrue(saved['dungeon_completed'])
self.assertEqual(saved['claim_evidence']['path'], 'completion.png')
self.assertEqual(saved['completion_evidence']['path'], 'home.png')
def test_atomic_snapshot_retains_old_data_if_serialization_fails(self):
save_json(self.context_path, {'floor': 2})
with self.assertRaises(TypeError):
save_json(self.context_path, {'bad': {1, 2}})
self.assertEqual(json.loads(self.context_path.read_text()), {'floor': 2})
self.assertFalse(list(self.home.glob('*.tmp')))
def test_new_run_can_recover_from_corrupt_old_files(self):
self.run_path.write_text('truncated')
self.context_path.write_text('truncated')
run, context = prepare_run(self.home, new_run=True)
self.assertEqual(run['status'], 'running')
self.assertEqual(context['floor'], 1)
if __name__ == '__main__':
unittest.main()