aps-agent/poc/pi-fallback/smoke/shim_server.py

26 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
# 诊断轮 server 启动 shim:在进程内对 fallback_lane._GUARD_TS_TEMPLATE 做单行内存修正
# (模板注释行 `返回 { block: true, reason }` 的单花括号在 str.format 时炸 KeyError: ' block',
# 使真实 runner 必败)。本 shim 只改内存、不改产品文件;目的=验证该 bug 是否唯一拦路点。
# 修正后生成的 guard TS 与「修复后的产品」逐字节一致(可由 guardSanity 校验)。
import os
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[3])) # 仓库根
from server.agent_core import fallback_lane
_ORIG = "返回 { block: true, reason } 即可"
_FIXED = "返回 {{ block: true, reason }} 即可"
assert _ORIG in fallback_lane._GUARD_TS_TEMPLATE, "模板注释行已变化,shim 失效,停止诊断"
fallback_lane._GUARD_TS_TEMPLATE = fallback_lane._GUARD_TS_TEMPLATE.replace(_ORIG, _FIXED)
# 自检:format 不再炸,且生成的 TS 含正确单花括号 JS
_probe = fallback_lane._GUARD_TS_TEMPLATE.format(RUN_ROOT_POSIX="/tmp/x")
assert "return { block: true, reason };" in _probe
import uvicorn # noqa: E402
uvicorn.run("server.main:app", host="127.0.0.1",
port=int(os.environ.get("APS_PORT", "8392")), log_level="info")