"""An explicitly versioned request must never downgrade into actor-local state.""" import io import sys import unittest from pathlib import Path from unittest.mock import MagicMock, patch sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts')) import nao_quality_runtime as runtime class ProtocolRuntimeBoundaryTest(unittest.TestCase): def payload(self, context): return dict(turnId='55555555-5555-4555-8555-555555555555', sessionId='22222222-2222-4222-8222-222222222222', actorId='11111111-1111-4111-8111-111111111111', releaseId='33333333-3333-4333-8333-333333333333', accessToken='synthetic', question='取消客户筛选', context=context) def test_invalid_protocol_marker_stops_before_any_actor_or_model_state(self): invalid = [None, [], '1', {}, {'version': 2}, {'version': True}, {'version': 1.0}, {'version': '1'}] for marker in invalid: with self.subTest(marker=marker): manager = MagicMock() manager.acquire_actor.side_effect = AssertionError('legacy actor state reached') with patch.object(runtime, 'RuntimeManager', return_value=manager), \ patch.object(runtime, 'plan_agent_task') as planner: result = runtime.run_turn(self.payload({'_protocol': marker, '_serverHistory': [{'answer': 'OLD'}], '_task': {'filters': {'product': 'OLD'}}}), stdout=io.StringIO(), stdin=io.StringIO()) self.assertEqual(result['status'], 'FAILED') self.assertIn('分析请求', result['message']) self.assertNotIn('本机', result['message']) manager.acquire_actor.assert_not_called() planner.assert_not_called() self.assertEqual(result['toolCalls'], []) def test_valid_version_and_absent_legacy_marker_remain_distinct(self): legacy = runtime._normalize_request(self.payload({'year': 2026})) self.assertNotIn('_protocol', legacy['context']) valid = runtime._normalize_request(self.payload({'_protocol': {'version': 1, 'attemptId': '22222222-2222-4222-8222-222222222222'}})) self.assertEqual(valid['context']['_protocol']['version'], 1) if __name__ == '__main__': unittest.main()