P2-8 镜牢局内决策写入审计日志

Browse Source
- 新增 lc_mcp/decisions.py:每个决策追加一行 JSON(time/stage/choice/reason + 细节),
  写失败只丢该行;summarize 按 stage/reason 计数
- TaskRunner 用 LCP_DECISIONS 把 task_*.decisions.jsonl 路径交给子进程,
  task_status 返回 decisions.counts,清理旧日志时一并删除
- 选队/键盘寻路/饰品/事件补丁记录各自决策;商店购买与卡包选择用只观察的补丁记录
  (购买按钮点中前的商品;卡包取自上游日志里的选择结果与权重)
- 抽出 _override 统一临时替换 auto 方法并在结束时还原

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yws
2026-11-14 07:11:30 +00:00
co-authored by Claude Opus 5.5
parent 4376d9cfb2
commit 51525722cd
12 changed files with 575 additions and 40 deletions
+256
View File
@@ -0,0 +1,256 @@
# -*- coding: utf-8 -*-
"""决策审计日志(BACKLOG P2-8): 写入、统计、TaskRunner 归档, 以及商店/卡包两处只观察的审计。"""
import ast
import json
import logging
import os
import sys
import tempfile
import time
import types
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest import mock
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO))
from lc_mcp import aalc_patches as ap # noqa: E402
from lc_mcp.decisions import DECISIONS_ENV, SUFFIX, DecisionLog, summarize # noqa: E402
from lc_mcp.tasks import TaskRunner # noqa: E402
THEME = REPO / 'vendor' / 'aalc' / 'tasks' / 'mirror' / 'select_theme_pack.py'
def read(path):
with open(path, encoding='utf-8') as f:
return [json.loads(line) for line in f]
class DecisionLogTest(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.path = os.path.join(tmp.name, 'run' + SUFFIX)
def test_without_env_nothing_is_written(self):
log = DecisionLog()
self.assertIsNone(log.open(env={}))
log.record('gift', 1, 'keyword')
self.assertFalse(os.path.exists(self.path))
def test_records_and_counts(self):
log = DecisionLog()
self.assertEqual(log.open(env={DECISIONS_ENV: self.path}), self.path)
log.record('gift', 2, 'keyword', system='bleed', hits=[[], ['流血']])
log.record('gift', 1, 'no_keyword', system='bleed', hits=[[], []])
log.record('gift', 1, 'keyword', system='bleed', hits=[['出血'], []])
log.record('shop', 'carmilla', 'purchase_clicked')
entries = read(self.path)
self.assertEqual(entries[0]['hits'], [[], ['流血']])
self.assertEqual(set(entries[0]), {'time', 'stage', 'choice', 'reason', 'system', 'hits'})
self.assertEqual(summarize(self.path), {'gift': {'keyword': 2, 'no_keyword': 1},
'shop': {'purchase_clicked': 1}})
def test_bad_lines_are_skipped_and_missing_file_is_none(self):
with open(self.path, 'w', encoding='utf-8') as f:
f.write('{"stage": "road", "reason": "lowest"}\nnot json\n{"stage": "road"}\n[]\n')
self.assertEqual(summarize(self.path), {'road': {'lowest': 1}})
self.assertIsNone(summarize(self.path + '.missing'))
def test_write_failure_does_not_raise(self):
DecisionLog(os.path.dirname(self.path)).record('gift', 1, 'keyword') # 路径是目录
class TaskRunnerDecisionsTest(unittest.TestCase):
CHILD = ('import os\n'
'from lc_mcp.decisions import decisions\n'
'decisions.open()\n'
'decisions.record("theme_pack", "信仰", "preferred")\n'
'decisions.record("gift", 2, "keyword")\n'
'decisions.record("gift", 1, "no_keyword")\n'
'print("[lc_mcp] done")\n')
def wait(self, runner):
deadline = time.time() + 15
while time.time() < deadline and runner.running():
time.sleep(0.05)
def test_child_writes_next_to_its_log_and_status_counts_it(self):
with tempfile.TemporaryDirectory() as d:
runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', self.CHILD])
started = runner.start('mirror', {'count': 1})
self.assertTrue(started['ok'], started)
self.wait(runner)
status = runner.status()
self.assertEqual(status['state'], 'succeeded', status)
self.assertEqual(status['decisions']['path'], started['log'][:-len('.log')] + SUFFIX)
self.assertEqual(status['decisions']['counts'],
{'gift': {'keyword': 1, 'no_keyword': 1}, 'theme_pack': {'preferred': 1}})
def test_no_decisions_field_when_nothing_was_recorded(self):
with tempfile.TemporaryDirectory() as d:
runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', 'print(1)'])
runner.start('daily', {})
self.wait(runner)
self.assertNotIn('decisions', runner.status())
def test_prune_removes_decisions_with_their_log(self):
with tempfile.TemporaryDirectory() as d:
runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', 'print(1)'], keep_logs=1)
old = os.path.join(d, 'task_20260101_000000_mirror')
for path in (old + '.log', old + SUFFIX):
with open(path, 'w') as f:
f.write('{}\n')
os.utime(old + '.log', (1, 1))
runner.start('daily', {})
self.wait(runner)
self.assertFalse(os.path.exists(old + '.log'))
self.assertFalse(os.path.exists(old + SUFFIX))
class RecordingTest(unittest.TestCase):
"""把模块级 decisions 指到临时文件。"""
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.path = os.path.join(tmp.name, 'run' + SUFFIX)
patcher = mock.patch.object(ap.decisions, 'path', self.path)
patcher.start()
self.addCleanup(patcher.stop)
self.lines = []
def entries(self):
return read(self.path) if os.path.exists(self.path) else []
class ShopAuditTest(RecordingTest):
def setUp(self):
super().setUp()
self.auto = SimpleNamespace(click_element=mock.Mock(return_value=True),
mouse_action_with_pos=mock.Mock())
self.shop_cls = type('Shop', (), {'system': 'bleed'})
in_shop = types.ModuleType('tasks.mirror.in_shop')
in_shop.Shop = self.shop_cls
modules = {'tasks': types.ModuleType('tasks'), 'tasks.mirror': types.ModuleType('tasks.mirror'),
'tasks.mirror.in_shop': in_shop, 'module': types.ModuleType('module'),
'module.automation': SimpleNamespace(auto=self.auto)}
patcher = mock.patch.dict(sys.modules, modules)
patcher.start()
self.addCleanup(patcher.stop)
def test_purchases_are_recorded_with_the_item_clicked_before(self):
auto = self.auto
def buy_gifts(shop):
# 上游的三条购买路径: 必买项、四级素材、本体系饰品(按坐标)
auto.click_element('mirror/shop/must_purchase/carmilla.png', threshold=0.85)
auto.click_element(ap.SHOP_PURCHASE)
auto.click_element(ap.SHOP_CONFIRM)
auto.click_element('mirror/shop/level_IV_to_buy.png', threshold=0.82)
auto.click_element(ap.SHOP_PURCHASE)
auto.mouse_action_with_pos((300, 400), offset=True)
auto.click_element(ap.SHOP_PURCHASE, take_screenshot=True)
return 'done'
self.shop_cls.buy_gifts = buy_gifts
click, position = auto.click_element, auto.mouse_action_with_pos
ap.install_shop_audit(log=self.lines.append)
self.assertEqual(self.shop_cls().buy_gifts(), 'done')
self.assertEqual([(e['choice'], e['reason'], e['system']) for e in self.entries()],
[('carmilla', 'purchase_clicked', 'bleed'),
('level_IV_to_buy', 'purchase_clicked', 'bleed'),
('by_position', 'purchase_clicked', 'bleed')])
self.assertIs(auto.click_element, click)
self.assertIs(auto.mouse_action_with_pos, position)
position.assert_called_once_with((300, 400), offset=True)
def test_failed_purchase_click_is_not_recorded(self):
auto, real = self.auto, self.auto.click_element
def buy_gifts(shop):
auto.click_element('mirror/shop/must_purchase/carmilla.png')
real.return_value = False # 钱不够: 购买按钮匹配不上
auto.click_element(ap.SHOP_PURCHASE)
self.shop_cls.buy_gifts = buy_gifts
ap.install_shop_audit(log=self.lines.append)
self.shop_cls().buy_gifts()
self.assertEqual(self.entries(), [])
class ThemePackDecisionTest(unittest.TestCase):
def test_messages_to_decision(self):
weights = "当前主题包权重列表:[('信仰', 0), ('unknown', -5)]"
self.assertEqual(ap.theme_pack_decision([weights, '此次选择卡包关键词:信仰']),
('信仰', 'preferred', "[('信仰', 0), ('unknown', -5)]"))
self.assertEqual(ap.theme_pack_decision(['无匹配最低阈值的主题包,选择最高权重主题包\n此次选择卡包关键词:赌徒'])[:2],
('赌徒', 'highest_weight'))
self.assertEqual(ap.theme_pack_decision(['此次主题包选择了最左边的(活动)卡包'])[:2],
('最左边的活动卡包', 'event_pack'))
self.assertIsNone(ap.theme_pack_decision(['选择卡包: (1, 2)']))
@unittest.skipUnless(THEME.is_file(), 'vendor/aalc 子模块未检出')
class UpstreamThemePackAuditTest(RecordingTest):
"""执行上游真实的 select_theme_pack, 审计从它自己的日志里取结果。"""
class Match:
def __init__(self, value, text):
self.value, self.text, self.position = value, text, [0, 0]
def run_upstream(self, packs, thresholds=0):
tree = ast.parse(THEME.read_text(encoding='utf-8'))
node = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'select_theme_pack')
node.decorator_list = []
positions = [(300 + 300 * i, 300) for i in range(len(packs))]
weights = {name: weight for name, weight in packs if name}
def find_language_text(zh, en, crop):
x = (crop[0] + crop[2]) / 2
name = packs[min(range(len(packs)), key=lambda i: abs(positions[i][0] - x))][0]
return self.Match(weights[name], name) if name else False
auto = SimpleNamespace(
model=None, take_screenshot=lambda: object(), mouse_to_blank=lambda *a, **k: None,
find_element=lambda target, **kw: list(positions) if 'theme_pack_features' in target else False,
find_language_text=find_language_text, mouse_drag_down=mock.Mock(),
click_element=lambda *a, **k: False)
ns = dict(auto=auto, TextMatchResult=self.Match, sleep=lambda *a: None, back_init_menu=mock.Mock(),
log=logging.getLogger('AALC'),
cfg=SimpleNamespace(set_win_size=720, select_event_pack=False, skip_event_pack=False),
path_manager=SimpleNamespace(current_language='zh_cn'),
theme_list=SimpleNamespace(preferred_thresholds=thresholds,
get_effective_theme_pack_list=lambda *a: dict(weights)))
exec(compile(ast.Module(body=[node], type_ignores=[]), str(THEME), 'exec'), ns)
mirror_mod = types.ModuleType('tasks.mirror.mirror')
mirror_mod.select_theme_pack = ns['select_theme_pack']
package = types.ModuleType('tasks.mirror')
package.mirror = mirror_mod
logger = logging.getLogger('AALC')
with mock.patch.dict(sys.modules, {'tasks': types.ModuleType('tasks'), 'tasks.mirror': package,
'tasks.mirror.mirror': mirror_mod}), \
mock.patch.object(logger, 'level', logging.DEBUG), \
mock.patch.object(logger, 'propagate', False):
ap.install_theme_pack_audit(log=self.lines.append)
handlers = list(logger.handlers)
mirror_mod.select_theme_pack(False, 2, 1, False)
self.assertEqual(logger.handlers, handlers, '临时 handler 必须移除')
return auto.mouse_drag_down
def test_preferred_pack_is_recorded(self):
drag = self.run_upstream([('赌徒', -1), ('信仰', 2), (None, None)])
drag.assert_called_once_with(600, 300)
entry, = self.entries()
self.assertEqual((entry['stage'], entry['choice'], entry['reason'], entry['floor']),
('theme_pack', '信仰', 'preferred', 2))
self.assertIn("('信仰', 2)", entry['weights'])
if __name__ == '__main__':
unittest.main()
+9
View File
@@ -207,6 +207,15 @@ class UpstreamEventHandlingTest(unittest.TestCase):
self.assertIn('事件 梦中的电子羊: 1=你是特别的 | 2=你不是特别的。 | 3=离开; 选第 2 项(选项文字含「你不是特别的」)',
self.log)
def test_decisions_are_recorded_once_per_screen(self):
with mock.patch.object(ap.decisions, 'record') as record:
self.play('梦中的电子羊', [['你是特别的', '你不是特别的。']])
self.play('没见过的事件', [['战斗', '离开']])
self.assertEqual(record.call_args_list, [
mock.call('event', 2, 'text', event='梦中的电子羊', options=['你是特别的', '你不是特别的。'],
why='选项文字含「你不是特别的」'),
mock.call('event', None, 'not_in_table', options=['战斗', '离开'])])
def test_unknown_event_behaves_like_upstream(self):
options = [['战斗', '离开']]
self.assertEqual(self.play('没见过的事件', options), self.play('没见过的事件', options, patched=False))
+6
View File
@@ -237,6 +237,12 @@ class UpstreamAcquireEgoGiftTest(unittest.TestCase):
self.assertEqual(self.choose(cards)[0], [1])
self.assertIn('第1张=1(流血) 第2张=2(流血/出血)', self.log)
def test_decision_is_recorded_left_to_right(self):
cards = [card(300, '烧伤层数+1'), card(600, '施加2层流血')]
with mock.patch.object(ap.decisions, 'record') as record:
self.choose(cards)
record.assert_called_once_with('gift', 2, 'keyword', system='bleed', hits=[[], ['流血']])
def test_unknown_system_skips_ocr(self):
cards = [card(300, '烧伤'), card(600, '流血')]
chosen, screen = self.choose(cards, system='unknown')
+14 -2
View File
@@ -197,8 +197,9 @@ class SelectTeamByNameTest(unittest.TestCase):
def setUp(self):
self.lines = []
patcher = mock.patch.object(aalc_patches.time, 'sleep')
patcher.start()
for patcher in (mock.patch.object(aalc_patches.time, 'sleep'),
mock.patch.object(aalc_patches.decisions, 'record')):
self.records = patcher.start()
self.addCleanup(patcher.stop)
def select(self, auto, names):
@@ -253,6 +254,17 @@ class SelectTeamByNameTest(unittest.TestCase):
self.assertEqual(auto.clicks, [])
self.assertIn('前缀匹配到多条(流血2/流血队), 不点', self.log)
def test_decisions_are_recorded(self):
self.select(_FakeTeamScreen([self.TOP, ['流血队', '震颤']]), ('流血',))
self.records.assert_called_with('team', '流血队', 'prefix', wanted=['流血'])
self.select(_FakeTeamScreen([self.TOP], header='流血'), ('流血',))
self.records.assert_called_with('team', '流血', 'current', wanted=['流血'])
self.select(_FakeTeamScreen([self.TOP, self.BOTTOM]), ('编队#999',))
args, kwargs = self.records.call_args
self.assertEqual(args, ('team', None, 'not_found'))
self.assertEqual(kwargs['seen'][0], self.TOP)
self.assertEqual(len(kwargs['seen']), 4)
def test_header_not_changing_is_reported(self):
auto = _FakeTeamScreen([self.TOP, self.MIDDLE], click_changes_header=False)
self.assertFalse(self.select(auto, ('流血',)))
+14
View File
@@ -114,6 +114,9 @@ class TypedKeyboardRoadTest(unittest.TestCase):
for patch in (mock.patch.dict(sys.modules, modules), mock.patch.object(ap.time, 'sleep')):
patch.start()
self.addCleanup(patch.stop)
patch = mock.patch.object(ap.decisions, 'record')
self.records = patch.start()
self.addCleanup(patch.stop)
def install(self, env=None):
return ap.install_typed_keyboard_road(log=self.lines.append, env=env or {})
@@ -136,6 +139,17 @@ class TypedKeyboardRoadTest(unittest.TestCase):
self.assertEqual(self.keys, ['up', 'down'])
self.assertIn('更优的方向没能进入', '\n'.join(self.lines))
def test_decisions_are_recorded(self):
self.install()
self.entered = [False, True]
self.road.search_road_simple_keyboard()
self.records.assert_called_once_with('road', 'D:shop', 'next_best',
candidates=['U=event(1)', 'D=shop(2)', 'M=battle(4)'])
self.records.reset_mock()
self.auto.find_element.return_value = None
self.road.search_road_simple_keyboard()
self.records.assert_called_once_with('road', 'up', 'fallback', why='没找到巴士')
def test_falls_back_to_original_when_nothing_can_be_entered(self):
self.install()
self.entered = [False, False, False]