diff --git a/benchmark/runtime/planning.py b/benchmark/runtime/planning.py index c824408cab..431eaf84f1 100644 --- a/benchmark/runtime/planning.py +++ b/benchmark/runtime/planning.py @@ -58,6 +58,7 @@ def task_plan_packet(env: dict[str, str], cli: list[str]) -> dict: cwd=env["LOOPX_PROJECT"], env=env, text=True, + encoding="utf-8", capture_output=True, check=True, timeout=120, diff --git a/loopx/control_plane/goals/planning_batch.py b/loopx/control_plane/goals/planning_batch.py index 94a1284a0c..b25ce01da4 100644 --- a/loopx/control_plane/goals/planning_batch.py +++ b/loopx/control_plane/goals/planning_batch.py @@ -706,6 +706,7 @@ def _call(self, *args: str) -> dict[str, object]: cwd=self.cwd, env=self.env, text=True, + encoding="utf-8", capture_output=True, timeout=120, ) diff --git a/tests/control_plane/test_benchmark_planning_batch.py b/tests/control_plane/test_benchmark_planning_batch.py index 1814783e30..d64b6fd257 100644 --- a/tests/control_plane/test_benchmark_planning_batch.py +++ b/tests/control_plane/test_benchmark_planning_batch.py @@ -384,6 +384,40 @@ def test_planning_batch_non_bmp_utf16_boundaries_match_real_cli(tmp_path, monkey assert outcome["sources"][0]["summary"] == emoji * 256 +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_planning_batch_packet_survives_gbk_parent_locale(tmp_path, monkeypatch, provider): + import subprocess + from benchmark.runtime.planning_batch import apply_initial_plan + + env, _, _, _, _ = _environment(tmp_path, monkeypatch, provider, replan="empty") + env["LOOPX_TASK_STAGE"] = "plan" + objective = "中文目标:统计并交付结果。" + Path(env["LOOPX_TASK_DOC"]).write_text(objective + "\n", encoding="utf-8") + # A cp936 host decodes an unpinned text-mode child read as gbk, while the + # wrapped CLI always emits UTF-8 JSON (ensure_ascii=False). + monkeypatch.setattr(subprocess, "_text_encoding", lambda: "gbk") + + packet = task_plan_packet(env, loopx_command(env)) + todo_text = "[P0] 统计中文结果。" + proposal = { + "input_digest": packet["input_digest"], + "expected_provider_revision": packet["goal_acceptance"]["provider_revision"], + "outcome": { + "objective": objective, + "requirements": [{"id": "deliver", "description": "交付并验证中文结果。"}], + "source": {"ref": "checkpoint:utf8", "summary": "中文规划回执。"}, + }, + "todos": [{"text": todo_text, "action_kind": "implement"}], + } + ready = apply_initial_plan(env, proposal) + assert ready["status"] == "ready" + after = task_plan_packet(env, loopx_command(env)) + stored = {item["todo_id"]: item for item in after["existing_todos"]} + assert stored[ready["todo_ids"][0]]["text"] == todo_text + outcome = after["goal_acceptance"]["goal_acceptance_contract"]["outcome_requirements"]["outcomes"][0] + assert outcome["objective"] == objective + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_planning_batch_target_key_boundary_real_cli_state(tmp_path, monkeypatch, provider): import os diff --git a/tests/test_runtime_subprocess_utf8.py b/tests/test_runtime_subprocess_utf8.py index 992bdfcfc0..e10400df2c 100644 --- a/tests/test_runtime_subprocess_utf8.py +++ b/tests/test_runtime_subprocess_utf8.py @@ -141,3 +141,28 @@ def test_change_quality_scope_git_helper_pins_codec_only_in_text_mode( assert raw.returncode == 0 assert isinstance(raw.stdout, bytes) assert "中文笔记.txt".encode("utf-8") in raw.stdout + + +def test_planning_batch_cli_backend_survives_gbk_host_locale(monkeypatch) -> None: + """The batch-planning CLI receipt decodes UTF-8 JSON on a cp936 host.""" + + from loopx.control_plane.goals.planning_batch import CliPlanningBatchBackend + + monkeypatch.setattr(subprocess, "_text_encoding", lambda: "gbk") + payload = '{"ok": true, "title": "中文目标"}' + script = ( + "import sys; sys.stdout.buffer.write(" + + repr(payload.encode("utf-8")) + + ")" + ) + backend = CliPlanningBatchBackend( + cli=[sys.executable, "-c", script], + cwd=".", + env={}, + packet_reader=lambda: {}, + project=".", + ) + + receipt = backend._call("status", "read") + + assert receipt["title"] == "中文目标"