"""react.progressDiscipline 开关:可选注入 PROGRESS.md 待办纪律,防多步任务"乱跳/重做/失忆"。

默认 off(现有 agent 零变化);开则注入;simple 子智能体走 _compile_lam,天然不受影响。
见 docs/PROGRESS_DISCIPLINE_DESIGN.md。
"""
from __future__ import annotations

from lambdagent.fromconfig.compiler import _compile_react, _compile_lam


def _system_prompt(term):
    th = getattr(term, "_think_ref", None) or term
    for _ in range(6):
        if hasattr(th, "system_prompt"):
            return th.system_prompt
        th = getattr(th, "agent", None)
        if th is None:
            break
    return ""


_BASE = {
    "name": "t", "type": "react", "systemPrompt": "你是助手。",
    "model": {"provider": "dashscope", "name": "qwen-plus"},
    "localTools": [],
}


def test_default_off_no_injection():
    term = _compile_react(dict(_BASE, react={"maxSteps": 5}), {})
    assert "PROGRESS.md" not in _system_prompt(term)


def test_explicit_false_no_injection():
    term = _compile_react(dict(_BASE, react={"maxSteps": 5, "progressDiscipline": False}), {})
    assert "PROGRESS.md" not in _system_prompt(term)


def test_on_injects_discipline():
    term = _compile_react(dict(_BASE, react={"maxSteps": 5, "progressDiscipline": True}), {})
    sp = _system_prompt(term)
    assert "PROGRESS.md" in sp
    assert "不重做已" in sp  # 铁律存在


def test_simple_agent_unaffected_even_if_flag_set():
    # simple 走 _compile_lam,不读 react 配置 → 即便误设 progressDiscipline 也不注入。
    cfg = dict(_BASE, type="simple", react={"progressDiscipline": True})
    term = _compile_lam(cfg, "think", overrides={})
    assert "PROGRESS.md" not in _system_prompt(term)
