This website requires JavaScript.
0
Issues
Pull Requests
Milestones
Explore
0
Create…
New Repository
New Migration
New Organization
public
Signed in as
public
Profile
Starred
Subscriptions
Settings
Help
Sign Out
yws
/
lc-mcp
Watch
1
Star
0
Fork
0
Code
Issues
Pull Requests
Actions
Packages
Projects
Releases
Wiki
Activity
Files
lc-mcp
/
scripts
/
or
Cancel
Edit File
Preview
Preview Changes
Spaces
Tabs
2
4
8
Wrap
No wrap
# -*- coding: utf-8 -*- """值守体检: 日志与报告交叉核对, 并挑出可疑记录。 用法: python audit_runs.py [--dir DIR] [--json] 退出码: 0 一切吻合 / 1 发现不一致 为什么需要这个脚本: 上一轮我照着自己写在 docs/WATCH.md 里的办法做核对, 结果 6 条全部报"对不上" —— 但数据其实是吻合的。坑在于: **日志的结论行写在一轮结束时, 报告的 started 是 开始时**, 两者天然差一个 elapsed_s。拿两个时间戳直接比必然全部失败。 正确关系是: 报告.started + elapsed ≈ 日志结论时刻。 代码里的时间关系比人记的可靠, 所以把核对做成脚本而不是让人照着文档敲 PowerShell —— 我手敲那次就误判了。 检查三件事: 1. 每份报告都能在日志里找到对应结论(时间对得上, 耗时和结果文本一致) 2. 日志里每条结论都有对应报告(没有"跑了但没留报告"的运行) 3. 疑似 mock 残留: 同一秒扎堆、或内容自相矛盾的结论行 """ import argparse import glob import json import os import re import sys from collections import defaultdict from datetime import datetime, timedelta DIR = os.path.expanduser(os.path.join('~', 'lc_mcp', 'modules')) LOG_NAME = 'convert.log' # 报告.started 与日志结论时刻的允许偏差(两边各写一次时间戳) TOL_S = 5 CONCLUSION_RE = re.compile( r'^\[(?P<t>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\s*===\s*(?P<result>.+?)\s*' r'\((?P<ok>\w+)\), 耗时 (?P<secs>\d+)s ===') START_RE = re.compile( r'^\[(?P<t>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]\s*===\s*开始: 体力换模块\s*===') # 明显不可能出现在真实运行里的说法 IMPOSSIBLE = ('powershell not found', 'not found', 'No such file') def parse_runs(path): """把日志解析成一轮一轮的 (开始, 结束, 结果, 耗时)。 ⛔ 为什么要配对而不是只取结论行: 第一版只用结论行, 拿"报告.started + elapsed" 去和结论时刻比 —— 能对上就说明 elapsed 一致, 所以"耗时不符"那个分支永远不可能 触发, 是死代码。看起来在查、实际查不到的东西比没有更糟。 改成按日志里的"=== 开始: 体力换模块 ===" 配对, 报告的 started 才能和**开始**时刻 真正对照, 耗时也能独立核对。 """ out = [] if not os.path.exists(path): return out pending = [] with open(path, encoding='utf-8') as f: for line in f: line = line.strip() ms = START_RE.match(line) if ms: pending.append(datetime.strptime(ms.group('t'), '%Y-%m-%d %H:%M:%S')) continue mc = CONCLUSION_RE.match(line) if mc: start = pending.pop(0) if pending else None out.append({'start': start, 'end': datetime.strptime(mc.group('t'), '%Y-%m-%d %H:%M:%S'), 'result': mc.group('result'), 'ok': mc.group('ok'), 'secs': int(mc.group('secs')), 'line': line}) return out # 兼容旧名字: 只要结论行 def parse_log(path): return parse_runs(path) def load_reports(d): out = [] for p in sorted(glob.glob(os.path.join(d, 'convert_*.json'))): try: with open(p, encoding='utf-8') as f: r = json.load(f) except (OSError, ValueError): continue out.append({'file': os.path.basename(p), 'start': datetime.strptime(r['started'], '%Y-%m-%d %H:%M:%S'), 'secs': r.get('elapsed_s'), 'result': r.get('result', ''), 'ok': bool(r.get('ok')), # ⛔ steps / converted 也得留着: 第一版只挑了 5 个字段, 结果 # "跨轮检查模组数"拿不到数据, 又是个查不出东西的检查。 'steps': r.get('steps') or [], 'converted': r.get('converted')}) out.sort(key=lambda r: r['start']) return out def same_second_clusters(conclusions): """同一秒里出现多行结论 = 几乎必然是测试批量产生的。""" bysec = defaultdict(list) for c in conclusions: bysec[c['end']].append(c) return {k: v for k, v in bysec.items() if len(v) > 1} def impossible_claims(conclusions): """内容自相矛盾的结论(比如 powershell 明明在却说找不到)。""" out = [] for c in conclusions: low = c['result'] if any(k.lower() in low.lower() for k in IMPOSSIBLE): out.append(c) return out OWNED_RE = re.compile(r'模组\s*(\d+)\s*->\s*(\d+)') def module_trajectory(reports): """跨轮检查模组数有没有减少。 ⛔ 2026-10-17 遇到一次: 22:05 合成后是 35, 而 06:04 那轮读到的合成前是 33。 当时怀疑是强制关游戏丢了未保存状态, 追查了一轮 —— **结果是人自己升阶用掉了**。 (所以现在 close_game 仍是优雅关闭: 那是独立的健壮性改进, 和这件事无关。) 教训: 这类"数字对不上"的现象, **默认解释可能是人手动操作**, 而不是 bug。 所以这里的措辞是"需要确认", 不是"出问题了" —— 唯一的例外是它能反过来证明 某件自动化的事: 如果模组数**自己变多了**, 那才是真异常(自动化之外的来源)。 自己从 steps[0].info 里解出模组数(不依赖调用方预先算好)。只比较**接连两轮都 成功合成**的情况, 免得把"跳过轮次"误判成倒退。 """ seq = [r for r in reports if r.get('converted')] seq.sort(key=lambda r: r['start']) owned = [] for r in seq: step = (r.get('steps') or [{}])[0] m = OWNED_RE.search(step.get('info', '') or '') owned.append((r, (int(m.group(1)), int(m.group(2))) if m else None)) out = [] for (prev, pa), (cur, ca) in zip(owned, owned[1:]): if pa is None or ca is None: continue if ca[0] < pa[1]: out.append('模组数比上一轮少: %s 合成后是 %s, 而 %s 读到合成前只有 %s ' '—— 若是人工升阶用掉的属正常; 否则要查' % (prev['start'].strftime('%m-%d %H:%M'), pa[1], cur['start'].strftime('%m-%d %H:%M'), ca[0])) elif ca[0] > pa[1]: # 自动化只会按合成前后的差值增加, 无端变多说明有别的来源 out.append('模组数无端变多: %s 合成后是 %s, 而 %s 读到合成前有 %s ' '—— 自动化之外还有别的来源, 值得看一眼' % (prev['start'].strftime('%m-%d %H:%M'), pa[1], cur['start'].strftime('%m-%d %H:%M'), ca[0])) return out def audit(d): runs = parse_runs(os.path.join(d, LOG_NAME)) reports = load_reports(d) problems = [] # 0. 跨轮: 模组数不该倒退 problems.extend(module_trajectory(reports)) # 1. 每份报告都要有对应的一轮 for r in reports: hit = [c for c in runs if c['start'] is not None and abs((c['start'] - r['start']).total_seconds()) <= TOL_S] if not hit: problems.append('报告 %s (started %s) 在日志里找不到对应的开始行' % (r['file'], r['start'])) continue c = hit[0] # 结论时刻应当 = 开始 + 耗时 expect_end = c['start'] + timedelta(seconds=r['secs'] or 0) if abs((c['end'] - expect_end).total_seconds()) > TOL_S: problems.append('%s 时间对不上: 开始 %s + 报告耗时 %ss 应为 %s, 日志结论在 %s' % (r['file'], c['start'].strftime('%H:%M:%S'), r['secs'], expect_end.strftime('%H:%M:%S'), c['end'].strftime('%H:%M:%S'))) if c['secs'] != r['secs']: problems.append('%s 耗时不符: 日志 %ss vs 报告 %ss' % (r['file'], c['secs'], r['secs'])) if c['result'] != r['result']: problems.append('%s 结果文本不符:\n 日志 %s\n 报告 %s' % (r['file'], c['result'], r['result'])) # 2. 每一轮都要有对应报告 for c in runs: hit = [r for r in reports if c['start'] is not None and abs((r['start'] - c['start']).total_seconds()) <= TOL_S] if not hit: problems.append('日志 %s 那轮没有对应报告: %s' % (c['end'].strftime('%H:%M:%S'), c['result'][:50])) # 3. mock 残留 for sec, group in same_second_clusters(runs).items(): problems.append('同一秒(%s)出现 %d 行结论, 疑似测试残留' % (sec.strftime('%H:%M:%S'), len(group))) for c in impossible_claims(runs): problems.append('可疑内容(%s): %s' % (c['end'].strftime('%H:%M:%S'), c['result'][:60])) return {'runs': len(runs), 'reports': len(reports), 'problems': problems} def main(argv=None): ap = argparse.ArgumentParser(description='值守体检: 日志与报告交叉核对') ap.add_argument('--dir', default=DIR) ap.add_argument('--json', action='store_true') args = ap.parse_args(argv) r = audit(args.dir) if args.json: json.dump(r, sys.stdout, ensure_ascii=False, indent=2) print() return 1 if r['problems'] else 0 print('日志 %d 轮 / 报告 %d 份' % (r['runs'], r['reports'])) if not r['problems']: print('✅ 全部吻合, 没有可疑记录') return 0 # 不叫"问题" —— 有些只是"需要确认"(比如人手动升阶用掉了模块) print('⚠️ 有 %d 处需要确认:' % len(r['problems'])) for p in r['problems']: print(' - %s' % p) return 1 if __name__ == '__main__': raise SystemExit(main())
Commit Changes
Add a Signed-off-by trailer by the committer at the end of the commit log message.
Commit directly to the
main
branch.
Create a
new branch
for this commit and start a pull request.
Commit Changes
Cancel