From c2affcad4211ed1a146bf24060f5f2b4ee30f611 Mon Sep 17 00:00:00 2001 From: Peter Zhang <18501667167@qq.com> Date: Sun, 31 May 2026 16:16:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=20hardcode=20?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=BE=93=E5=85=A5=E9=AA=8C=E8=AF=81=20-=20Cl?= =?UTF-8?q?oses=20#8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 _DEFAULT_INPUT 硬编码默认输入文件路径 - INPUT_JSON 仅从 IR_INPUT_JSON 环境变量获取 - load_input_document() 无输入时给出明确错误提示 - 新增 test_no_hardcoded_input_file / test_set_input_file_accepts_none Co-Authored-By: Claude Opus 4.7 --- skills/ir_generation_skill/config.py | 29 ++++++++++++++++++++++------ tests/test_config.py | 27 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/skills/ir_generation_skill/config.py b/skills/ir_generation_skill/config.py index 984d3c1..41e8fff 100644 --- a/skills/ir_generation_skill/config.py +++ b/skills/ir_generation_skill/config.py @@ -4,6 +4,7 @@ Reads API keys from a secrets.yaml file, falling back to environment variables. """ import os +import sys import json import yaml @@ -23,11 +24,9 @@ PROMPTS_DIR = os.path.join(BASE_DIR, "prompts") TESTS_DIR = os.path.join(BASE_DIR, "tests") OUTPUT_DIR = IR_OUTPUT # backward compatibility alias -# Input file (the parsed PRD JSON) -_DEFAULT_INPUT = os.path.join( - PROJECT_OUTPUT, "车机娱乐系统禁止功能文档_脱敏 v0.9_v2_updated.json", -) -INPUT_JSON = os.environ.get("IR_INPUT_JSON", _DEFAULT_INPUT) +# Input file (the parsed PRD JSON) — must be set via env var or CLI +# No hardcoded default to avoid silently processing the wrong document. +INPUT_JSON = os.environ.get("IR_INPUT_JSON", None) def set_input_file(path: str) -> None: @@ -125,8 +124,26 @@ def llm_client(): def load_input_document(path: str | None = None) -> dict: - """Load the parsed PRD JSON document.""" + """Load the parsed PRD JSON document. + + Args: + path: Explicit file path. If None, reads from IR_INPUT_JSON env var. + + Raises: + FileNotFoundError: If no path is configured. + SystemExit: If the configured path does not exist. + """ path = path or INPUT_JSON + if not path: + print("错误: 未指定输入文件。请通过以下任一方式指定:", file=sys.stderr) + print(" 1. 设置环境变量: IR_INPUT_JSON=", file=sys.stderr) + print(" 2. 通过 main.py: python main.py --input ", file=sys.stderr) + print(" 3. 通过 step 脚本: python step1_semantic_index.py --input ", file=sys.stderr) + print(" 4. 程序调用: config.set_input_file()", file=sys.stderr) + sys.exit(1) + if not os.path.isfile(path): + print(f"错误: 输入文件不存在: {path}", file=sys.stderr) + sys.exit(1) with open(path, "r", encoding="utf-8") as f: return json.load(f) diff --git a/tests/test_config.py b/tests/test_config.py index c9a3eb0..fb4b651 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -19,6 +19,33 @@ def test_set_input_file(): config.set_input_file(original) +def test_no_hardcoded_input_file(): + """INPUT_JSON should not have a hardcoded default — comes from env or None.""" + # When IR_INPUT_JSON env is not set, INPUT_JSON should be None + env_val = os.environ.pop("IR_INPUT_JSON", None) + try: + import importlib + importlib.reload(config) + # After reload without env var, INPUT_JSON should be None + assert config.INPUT_JSON is None, \ + f"INPUT_JSON should be None when env not set, got: {config.INPUT_JSON}" + finally: + if env_val is not None: + os.environ["IR_INPUT_JSON"] = env_val + importlib.reload(config) + + +def test_set_input_file_accepts_none(): + """set_input_file(None) should work for resetting.""" + original = config.INPUT_JSON + try: + config.set_input_file("/tmp/test.json") + config.set_input_file(None) + assert config.INPUT_JSON is None + finally: + config.set_input_file(original) + + def test_config_constants_exist(): """Verify all expected path constants are defined.""" assert config.BASE_DIR