diff --git a/loopx/control_plane/runtime/reward_rendering.py b/loopx/control_plane/runtime/reward_rendering.py new file mode 100644 index 0000000000..a34dd44744 --- /dev/null +++ b/loopx/control_plane/runtime/reward_rendering.py @@ -0,0 +1,401 @@ +"""Pure validation, compaction, and Markdown rendering helpers for human reward records.""" + +from __future__ import annotations + +from collections.abc import Iterable, Mapping, Sequence +from datetime import datetime, timezone +import re + +from ...public_safe_text import TEXT_OWNER_CATEGORIES, classify_private_text +from .run_compaction import HUMAN_REWARD_LESSON_STRUCTURED_SCOPE_FIELDS + +REWARD_VALUES = {"positive", "negative", "mixed", "neutral"} +LESSON_KINDS = { + "route", + "priority", + "benchmark_protocol", + "safety_boundary", + "operating_rule", +} + + +def now_utc() -> str: + """Return UTC ISO-8601 timestamp truncated to whole seconds.""" + return datetime.now(timezone.utc).isoformat(timespec="seconds") + + +def now_local() -> str: + """Return local ISO-8601 timestamp truncated to whole seconds.""" + return datetime.now(timezone.utc).astimezone().replace(microsecond=0).isoformat() + + +def public_safe_text_guidance(label: str) -> str: + """Return operator guidance tailored to the rejected control-plane field.""" + normalized = label.lower().replace("-", "_") + if "next_action" in normalized or "recommended_action" in normalized: + return ( + "this field is usually local-control state; use validate_local_control_text " + "for private project routing, or a public-safe alias only on exported surfaces" + ) + if "todo" in normalized: + return ( + "use a compact public-safe todo alias or summary here; keep raw local " + "paths, private URLs, task bodies, and logs in evidence/private payloads" + ) + if "evidence" in normalized: + return ( + "use a compact public-safe evidence pointer here; keep raw logs, local " + "paths, and private URLs in private payloads" + ) + return ( + "use a public-safe summary or alias here; keep raw local paths, private " + "URLs, and raw evidence in private payloads" + ) + + +def validate_public_safe_text(label: str, value: str | None) -> None: + """Reject text that carries private paths, URLs, or credential-shaped values.""" + # The named policy, not a bare pattern sweep: this is LoopX's own state, so a + # mention of "Bearer"/"password"/"secret" is a fact about a credential, not + # one. Value and assignment shapes stay rejected (Refs #5136, direction 2). + if classify_private_text(value, categories=TEXT_OWNER_CATEGORIES) is not None: + raise ValueError( + f"{label} contains a private-looking value; " + + public_safe_text_guidance(label) + ) + + +def _compact_reward_lesson_scope_value( + field: str, + value: object, +) -> str | list[str] | dict[str, object] | None: + if value is None or value == "" or value == []: + return None + if isinstance(value, str): + cleaned = value.strip() + if not cleaned: + return None + validate_public_safe_text(f"lesson {field}", cleaned) + return cleaned + if isinstance(value, list): + items: list[str] = [] + for entry in value: + cleaned = str(entry or "").strip() + if not cleaned: + continue + validate_public_safe_text(f"lesson {field}", cleaned) + if cleaned not in items: + items.append(cleaned) + return items[:10] if items else None + if isinstance(value, Mapping): + nested: dict[str, object] = {} + for key, entry in value.items(): + if not isinstance(key, str) or entry is None or entry == "": + continue + cleaned_entry = _compact_reward_lesson_scope_value(f"{field}.{key}", entry) + if cleaned_entry is not None: + nested[key] = cleaned_entry + return nested or None + return None + + +def compact_reward_lesson(lesson: Mapping[str, object]) -> dict[str, object]: + """Validate and compact structured lesson guidance attached to a reward.""" + kind = str(lesson.get("kind") or "").strip() + summary = str(lesson.get("summary") or "").strip() + if kind not in LESSON_KINDS: + raise ValueError(f"lesson kind must be one of: {', '.join(sorted(LESSON_KINDS))}") + if not summary: + raise ValueError("lesson summary is required when lesson metadata is provided") + validate_public_safe_text("lesson summary", summary) + payload: dict[str, object] = { + "schema_version": "human_reward_lesson_v0", + "kind": kind, + "summary": summary, + } + for field in ("avoid", "prefer"): + raw_value = lesson.get(field) or [] + if isinstance(raw_value, str): + raw_items: Iterable[object] = (raw_value,) + elif isinstance(raw_value, Iterable) and not isinstance(raw_value, (bytes, bytearray)): + raw_items = raw_value + else: + raise ValueError(f"lesson {field} must be iterable") + items: list[str] = [] + for raw_item in raw_items: + item = str(raw_item or "").strip() + if not item: + continue + validate_public_safe_text(f"lesson {field}", item) + if item not in items: + items.append(item) + if items: + payload[field] = items[:5] + for field in HUMAN_REWARD_LESSON_STRUCTURED_SCOPE_FIELDS: + if field in lesson: + compacted = _compact_reward_lesson_scope_value(field, lesson[field]) + if compacted is not None: + payload[field] = compacted + return payload + + +def compact_reward( + *, + recorded_at: str | None, + decision: str, + reward: str, + reason_summary: str, + follow_up: str | None, + lesson: Mapping[str, object] | None = None, +) -> dict[str, object]: + """Validate and compact a human reward input payload.""" + if reward not in REWARD_VALUES: + raise ValueError(f"reward must be one of: {', '.join(sorted(REWARD_VALUES))}") + validate_public_safe_text("decision", decision) + validate_public_safe_text("reason_summary", reason_summary) + validate_public_safe_text("follow_up", follow_up) + payload: dict[str, object] = { + "recorded_at": recorded_at or now_utc(), + "decision": decision, + "reward": reward, + "reason_summary": reason_summary, + } + if follow_up: + payload["follow_up"] = follow_up + if lesson: + payload["lesson"] = compact_reward_lesson(lesson) + return payload + + +def _as_mapping(value: object) -> Mapping[str, object]: + return value if isinstance(value, Mapping) else {} + + +def build_reward_coordination( + *, + goal_id: str, + selected_run: Mapping[str, object], + reward: Mapping[str, object], + dry_run: bool, +) -> dict[str, object]: + """Build active-state coordination summary and readback pointer metadata.""" + run_generated_at = str(selected_run.get("generated_at") or "unknown-run") + reward_value = str(reward.get("reward") or "unknown") + decision = str(reward.get("decision") or "unknown_decision") + verb = "dry-run:将记录" if dry_run else "已记录" + reason_summary = reward.get("reason_summary") + summary = ( + f"{verb}目标 `{goal_id}` 的 run `{run_generated_at}` " + f"human_reward={reward_value},decision=`{decision}`。" + "权威来源是 run-bound `human_reward` overlay;" + "active state 只摘要这个指针和下一步。" + ) + if reason_summary: + summary = f"{summary} reason_summary:{reason_summary}" + follow_up = reward.get("follow_up") + if follow_up: + summary = f"{summary} follow_up:{follow_up}" + lesson = _as_mapping(reward.get("lesson")) + if lesson: + summary = f"{summary} lesson[{lesson.get('kind')}]: {lesson.get('summary')}" + visibility: dict[str, object] = { + "source_of_truth": "run_bound_human_reward_overlay", + "history_command": f"loopx history --goal-id {goal_id} --limit 3", + "active_state_role": "summary_only", + "review_packet_role": "optional_handoff_only", + } + if lesson: + visibility["lesson_role"] = "route_warning_only_not_write_control" + return { + "active_state_summary": summary, + "project_agent_visibility": visibility, + } + + +def build_active_state_entry( + *, + reward: Mapping[str, object], + active_state_summary: str, + project_agent_visibility: Mapping[str, object], +) -> str: + """Format one Progress Ledger entry summarizing a human reward decision.""" + recorded_at = str(reward.get("recorded_at") or now_local()) + history_command = project_agent_visibility.get("history_command") + lines = [f"- {recorded_at}: {active_state_summary}"] + if history_command: + lines.append(f" - project_agent_visibility: `{history_command}`") + return "\n".join(lines) + + +def update_frontmatter_timestamp(text: str, updated_at: str) -> str: + """Update or append ``updated_at`` inside YAML frontmatter when present.""" + if not text.startswith("---\n"): + return text + end = text.find("\n---", 4) + if end < 0: + return text + frontmatter = text[:end] + body = text[end:] + if re.search(r"^updated_at:\s*.*$", frontmatter, flags=re.M): + frontmatter = re.sub( + r"^updated_at:\s*.*$", + f"updated_at: {updated_at}", + frontmatter, + flags=re.M, + ) + else: + frontmatter = f"{frontmatter}\nupdated_at: {updated_at}" + return frontmatter + body + + +def insert_progress_ledger_entry( + text: str, + entry: str, + updated_at: str, +) -> tuple[str, bool]: + """Insert ``entry`` under ``## Progress Ledger`` unless already present.""" + text = update_frontmatter_timestamp(text, updated_at) + if entry in text: + return text, False + + lines = text.splitlines() + for index, line in enumerate(lines): + if line.strip() != "## Progress Ledger": + continue + insert_at = index + 1 + while insert_at < len(lines) and not lines[insert_at].strip(): + insert_at += 1 + lines[insert_at:insert_at] = [*entry.splitlines(), ""] + return "\n".join(lines).rstrip() + "\n", True + + separator = "" if text.endswith("\n") else "\n" + return f"{text}{separator}\n## Progress Ledger\n\n{entry}\n", True + + +def describe_active_state_effect( + state_update: Mapping[str, object], + *, + dry_run: bool, +) -> str: + """Describe the active-state writeback outcome in user-facing Markdown.""" + if not state_update or not state_update.get("requested"): + return "未请求 active-state 摘要写回" + state_file = state_update.get("state_file") or "unknown state file" + if state_update.get("already_present"): + return f"摘要已存在,未重复写入 `{state_file}`" + if dry_run and state_update.get("would_write"): + return f"预览:会把摘要写入 `{state_file}` 的 Progress Ledger" + if state_update.get("written"): + return f"已把摘要写入 `{state_file}` 的 Progress Ledger" + return f"未写入 active state;检查 `{state_file}`" + + +def describe_run_overlay_effect( + *, + appended: bool, + dry_run: bool, + index_path: str | None, +) -> str: + """Describe the run-index overlay write effect.""" + target = f"`{index_path}`" if index_path else "run index" + if dry_run: + return f"预览:不会写 {target}" + if appended: + return f"已追加 human_reward overlay 到 {target}" + return f"未写 {target}" + + +def render_reward_markdown(payload: Mapping[str, object]) -> str: + """Render a compact Markdown report for ``loopx reward``.""" + lines = [ + "# LoopX Human Reward", + "", + f"- ok: `{payload.get('ok')}`", + f"- goal: `{payload.get('goal_id')}`", + f"- index: `{payload.get('index_path')}`", + f"- appended: `{payload.get('appended')}`", + f"- dry_run: `{payload.get('dry_run')}`", + ] + if payload.get("error"): + lines.append(f"- error: {payload.get('error')}") + return "\n".join(lines) + + selected = _as_mapping(payload.get("selected_run")) + reward = _as_mapping(payload.get("human_reward")) + state_update = _as_mapping(payload.get("active_state_update")) + visibility = _as_mapping(payload.get("project_agent_visibility")) + raw_index_path = payload.get("index_path") + index_path = raw_index_path if isinstance(raw_index_path, str) else None + dry_run = bool(payload.get("dry_run")) + lines.extend( + [ + "", + "## Write Effect", + f"- selected_run: `{selected.get('generated_at')}`", + ( + "- run_overlay: " + + describe_run_overlay_effect( + appended=bool(payload.get("appended")), + dry_run=dry_run, + index_path=index_path, + ) + ), + f"- active_state: {describe_active_state_effect(state_update, dry_run=dry_run)}", + ] + ) + if visibility.get("history_command"): + lines.append(f"- project_agent_visibility: `{visibility.get('history_command')}`") + lines.extend( + [ + "", + "## Selected Run", + f"- generated_at: `{selected.get('generated_at')}`", + f"- classification: `{selected.get('classification')}`", + f"- artifacts: `{selected.get('json_exists')}/{selected.get('markdown_exists')}`", + "", + "## Reward", + f"- recorded_at: `{reward.get('recorded_at')}`", + f"- actor_kind: `{reward.get('actor_kind')}`", + f"- decision: `{reward.get('decision')}`", + f"- reward: `{reward.get('reward')}`", + f"- reason_summary: {reward.get('reason_summary')}", + ] + ) + if reward.get("follow_up"): + lines.append(f"- follow_up: {reward.get('follow_up')}") + lesson = _as_mapping(reward.get("lesson")) + if lesson: + lines.append(f"- lesson_kind: `{lesson.get('kind')}`") + lines.append(f"- lesson_summary: {lesson.get('summary')}") + for field in ("avoid", "prefer"): + values = lesson.get(field) + if isinstance(values, Sequence) and not isinstance(values, (str, bytes, bytearray)): + rendered = ", ".join(str(value) for value in list(values)[:5]) + if rendered: + lines.append(f"- lesson_{field}: {rendered}") + if payload.get("active_state_summary"): + lines.extend(["", "## Active-State Summary", str(payload.get("active_state_summary"))]) + if state_update: + lines.extend( + [ + "", + "## Active-State Writeback", + f"- requested: `{state_update.get('requested')}`", + f"- state_file: `{state_update.get('state_file')}`", + f"- would_write: `{state_update.get('would_write')}`", + f"- written: `{state_update.get('written')}`", + f"- already_present: `{state_update.get('already_present')}`", + ] + ) + if visibility: + lines.extend( + [ + "", + "## Project-Agent Visibility", + f"- source_of_truth: `{visibility.get('source_of_truth')}`", + f"- history_command: `{visibility.get('history_command')}`", + f"- active_state_role: `{visibility.get('active_state_role')}`", + f"- review_packet_role: `{visibility.get('review_packet_role')}`", + ] + ) + return "\n".join(lines) diff --git a/loopx/feedback.py b/loopx/feedback.py index 7ed777bcb8..04da3f987a 100644 --- a/loopx/feedback.py +++ b/loopx/feedback.py @@ -1,26 +1,37 @@ -from __future__ import annotations +"""Public-safe text validators and human reward overlay recording.""" -from typing import Any, cast +from __future__ import annotations +from collections.abc import Callable, Mapping, Sequence from contextlib import nullcontext from importlib import import_module as _import_module - -from .file_lock import exclusive_cross_runtime_file_lock - -import re -from datetime import datetime, timezone from pathlib import Path +import re -from .control_plane.runtime.run_compaction import HUMAN_REWARD_LESSON_STRUCTURED_SCOPE_FIELDS +from .control_plane.actor_identity import normalize_owner_controller_actor +from .control_plane.runtime.reward_rendering import ( + LESSON_KINDS as LESSON_KINDS, + REWARD_VALUES as REWARD_VALUES, + _compact_reward_lesson_scope_value as _compact_reward_lesson_scope_value, + build_active_state_entry as build_active_state_entry, + build_reward_coordination as build_reward_coordination, + compact_reward as compact_reward, + compact_reward_lesson as compact_reward_lesson, + describe_active_state_effect as describe_active_state_effect, + describe_run_overlay_effect as describe_run_overlay_effect, + insert_progress_ledger_entry as insert_progress_ledger_entry, + now_local as now_local, + now_utc as now_utc, + public_safe_text_guidance as public_safe_text_guidance, + render_reward_markdown as render_reward_markdown, + update_frontmatter_timestamp as update_frontmatter_timestamp, + validate_public_safe_text as validate_public_safe_text, +) from .control_plane.runtime.time import chronology_key +from .file_lock import exclusive_cross_runtime_file_lock from .paths import resolve_runtime_root -from .public_safe_text import ( - PRIVATE_TEXT_PATTERNS as SHARED_PRIVATE_TEXT_PATTERNS, - TEXT_OWNER_CATEGORIES, - classify_private_text, -) +from .public_safe_text import PRIVATE_TEXT_PATTERNS as SHARED_PRIVATE_TEXT_PATTERNS from .registry import registry_goals, resolve_state_file -from .control_plane.actor_identity import normalize_owner_controller_actor _MUTATION_ADAPTERS = { @@ -35,11 +46,11 @@ } -def __getattr__(name: str) -> Any: +def __getattr__(name: str) -> object: owner = _LAZY_IMPORTS.get(name) if owner is None: raise AttributeError(f"module {__name__!r} has no attribute {name!r}") - value = getattr(_import_module(owner), name) + value: object = getattr(_import_module(owner), name) globals()[name] = value return value @@ -48,11 +59,18 @@ def __dir__() -> list[str]: return sorted(set(globals()) | set(_LAZY_IMPORTS)) -def _mutation_adapter(name: str) -> Any: +def _mutation_adapter(name: str) -> object: # Preserve the released module-level patch points and original objects. return globals()[name] if name in globals() else __getattr__(name) +def _callable_adapter(name: str) -> Callable[..., object]: + adapter = _mutation_adapter(name) + if not callable(adapter): + raise TypeError(f"feedback adapter {name!r} must be callable") + return adapter + + GOAL_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]*$") @@ -70,14 +88,6 @@ def validate_goal_id(goal_id: str) -> str: return goal_id -REWARD_VALUES = {"positive", "negative", "mixed", "neutral"} -LESSON_KINDS = { - "route", - "priority", - "benchmark_protocol", - "safety_boundary", - "operating_rule", -} # Owned by loopx.public_safe_text so every real validator owner shares one # contract; re-exported here because callers already import this name. PRIVATE_TEXT_PATTERNS = SHARED_PRIVATE_TEXT_PATTERNS @@ -121,48 +131,6 @@ def validate_goal_id(goal_id: str) -> str: ) -def public_safe_text_guidance(label: str) -> str: - normalized = label.lower().replace("-", "_") - if "next_action" in normalized or "recommended_action" in normalized: - return ( - "this field is usually local-control state; use validate_local_control_text " - "for private project routing, or a public-safe alias only on exported surfaces" - ) - if "todo" in normalized: - return ( - "use a compact public-safe todo alias or summary here; keep raw local " - "paths, private URLs, task bodies, and logs in evidence/private payloads" - ) - if "evidence" in normalized: - return ( - "use a compact public-safe evidence pointer here; keep raw logs, local " - "paths, and private URLs in private payloads" - ) - return ( - "use a public-safe summary or alias here; keep raw local paths, private " - "URLs, and raw evidence in private payloads" - ) - - -def now_utc() -> str: - return datetime.now(timezone.utc).isoformat(timespec="seconds") - - -def now_local() -> str: - return datetime.now(timezone.utc).astimezone().replace(microsecond=0).isoformat() - - -def validate_public_safe_text(label: str, value: str | None) -> None: - # The named policy, not a bare pattern sweep: this is LoopX's own state, so a - # mention of "Bearer"/"password"/"secret" is a fact about a credential, not - # one. Value and assignment shapes stay rejected (Refs #5136, direction 2). - if classify_private_text(value, categories=TEXT_OWNER_CATEGORIES) is not None: - raise ValueError( - f"{label} contains a private-looking value; " - + public_safe_text_guidance(label) - ) - - def validate_local_control_text(label: str, value: str | None) -> None: """Validate text for local control-plane state, not public export. @@ -181,132 +149,50 @@ def validate_local_control_text(label: str, value: str | None) -> None: ) -def compact_reward( - *, - recorded_at: str | None, - decision: str, - reward: str, - reason_summary: str, - follow_up: str | None, - lesson: dict[str, Any] | None = None, -) -> dict[str, Any]: - if reward not in REWARD_VALUES: - raise ValueError(f"reward must be one of: {', '.join(sorted(REWARD_VALUES))}") - validate_public_safe_text("decision", decision) - validate_public_safe_text("reason_summary", reason_summary) - validate_public_safe_text("follow_up", follow_up) - payload: dict[str, Any] = { - "recorded_at": recorded_at or now_utc(), - "decision": decision, - "reward": reward, - "reason_summary": reason_summary, - } - if follow_up: - payload["follow_up"] = follow_up - if lesson: - payload["lesson"] = compact_reward_lesson(lesson) - return payload - - - -def _compact_reward_lesson_scope_value(field: str, value: Any) -> Any: - if value is None or value == "" or value == []: - return None - if isinstance(value, str): - cleaned = value.strip() - if not cleaned: - return None - validate_public_safe_text(f"lesson {field}", cleaned) - return cleaned - if isinstance(value, list): - items: list[str] = [] - for entry in value: - cleaned = str(entry or "").strip() - if not cleaned: - continue - validate_public_safe_text(f"lesson {field}", cleaned) - if cleaned not in items: - items.append(cleaned) - return items[:10] if items else None - if isinstance(value, dict): - nested: dict[str, Any] = {} - for key, entry in value.items(): - if not isinstance(key, str) or entry is None or entry == "": - continue - cleaned_entry = _compact_reward_lesson_scope_value(f"{field}.{key}", entry) - if cleaned_entry is not None: - nested[key] = cleaned_entry - return nested or None - return None - - -def compact_reward_lesson(lesson: dict[str, Any]) -> dict[str, Any]: - kind = str(lesson.get("kind") or "").strip() - summary = str(lesson.get("summary") or "").strip() - if kind not in LESSON_KINDS: - raise ValueError(f"lesson kind must be one of: {', '.join(sorted(LESSON_KINDS))}") - if not summary: - raise ValueError("lesson summary is required when lesson metadata is provided") - validate_public_safe_text("lesson summary", summary) - payload: dict[str, Any] = { - "schema_version": "human_reward_lesson_v0", - "kind": kind, - "summary": summary, - } - for field in ("avoid", "prefer"): - raw_items = lesson.get(field) or [] - if isinstance(raw_items, str): - raw_items = [raw_items] - items: list[str] = [] - for raw_item in raw_items: - item = str(raw_item or "").strip() - if not item: - continue - validate_public_safe_text(f"lesson {field}", item) - if item not in items: - items.append(item) - if items: - payload[field] = items[:5] - for field in HUMAN_REWARD_LESSON_STRUCTURED_SCOPE_FIELDS: - if field in lesson: - compacted = _compact_reward_lesson_scope_value(field, lesson[field]) - if compacted is not None: - payload[field] = compacted - return payload - - -def select_run(runs: list[dict[str, Any]], run_generated_at: str | None) -> dict[str, Any]: +def select_run( + runs: Sequence[Mapping[str, object]], + run_generated_at: str | None, +) -> dict[str, object]: + """Select the requested run or latest chronological run from compacted history.""" if not runs: raise ValueError("no compact run records found for goal") if run_generated_at: for run in runs: if str(run.get("generated_at") or "") == run_generated_at: - return run + return run if isinstance(run, dict) else dict(run) raise ValueError(f"run not found for generated_at={run_generated_at}") - return max( + selected = max( enumerate(runs), key=lambda item: ( *chronology_key(item[1].get("generated_at")), item[0], ), )[1] + return selected if isinstance(selected, dict) else dict(selected) -def find_registry_goal(registry: dict[str, Any], goal_id: str) -> dict[str, Any] | None: - for goal in registry_goals(registry): +def find_registry_goal( + registry: Mapping[str, object], + goal_id: str, +) -> dict[str, object] | None: + """Return the matching goal entry from a registry mapping when registered.""" + normalized = registry if isinstance(registry, dict) else dict(registry) + for goal in registry_goals(normalized): if str(goal.get("id") or "") == goal_id: - return goal + return dict(goal) return None def resolve_reward_state_file( *, - registry: dict[str, Any], + registry: Mapping[str, object], goal_id: str, state_file_override: Path | None, ) -> Path | None: + """Resolve the active-state file path for optional reward summary writeback.""" goal = find_registry_goal(registry, goal_id) - repo = Path(str(goal.get("repo") or "")).expanduser() if goal and goal.get("repo") else None + raw_repo = goal.get("repo") if goal else None + repo = Path(str(raw_repo)).expanduser() if raw_repo else None if state_file_override: state_file = state_file_override.expanduser() if state_file.is_absolute(): @@ -314,112 +200,26 @@ def resolve_reward_state_file( return (repo or Path.cwd()) / state_file if not goal or not repo: return None - return resolve_state_file(repo, goal.get("state_file")) - - -def build_reward_coordination( - *, - goal_id: str, - selected_run: dict[str, Any], - reward: dict[str, Any], - dry_run: bool, -) -> dict[str, Any]: - run_generated_at = str(selected_run.get("generated_at") or "unknown-run") - reward_value = str(reward.get("reward") or "unknown") - decision = str(reward.get("decision") or "unknown_decision") - verb = "dry-run:将记录" if dry_run else "已记录" - reason_summary = reward.get("reason_summary") - summary = ( - f"{verb}目标 `{goal_id}` 的 run `{run_generated_at}` " - f"human_reward={reward_value},decision=`{decision}`。" - "权威来源是 run-bound `human_reward` overlay;" - "active state 只摘要这个指针和下一步。" + raw_state_file = goal.get("state_file") + return resolve_state_file( + repo, + raw_state_file if isinstance(raw_state_file, str) else None, ) - if reason_summary: - summary = f"{summary} reason_summary:{reason_summary}" - follow_up = reward.get("follow_up") - if follow_up: - summary = f"{summary} follow_up:{follow_up}" - lesson = cast(dict[str, Any], reward.get("lesson")) if isinstance(reward.get("lesson"), dict) else {} - if lesson: - summary = ( - f"{summary} lesson[{lesson.get('kind')}]: " - f"{lesson.get('summary')}" - ) - visibility = { - "source_of_truth": "run_bound_human_reward_overlay", - "history_command": f"loopx history --goal-id {goal_id} --limit 3", - "active_state_role": "summary_only", - "review_packet_role": "optional_handoff_only", - } - if lesson: - visibility["lesson_role"] = "route_warning_only_not_write_control" - return { - "active_state_summary": summary, - "project_agent_visibility": visibility, - } - - -def build_active_state_entry( - *, - reward: dict[str, Any], - active_state_summary: str, - project_agent_visibility: dict[str, Any], -) -> str: - recorded_at = str(reward.get("recorded_at") or now_local()) - history_command = project_agent_visibility.get("history_command") - lines = [f"- {recorded_at}: {active_state_summary}"] - if history_command: - lines.append(f" - project_agent_visibility: `{history_command}`") - return "\n".join(lines) - - -def update_frontmatter_timestamp(text: str, updated_at: str) -> str: - if not text.startswith("---\n"): - return text - end = text.find("\n---", 4) - if end < 0: - return text - frontmatter = text[:end] - body = text[end:] - if re.search(r"^updated_at:\s*.*$", frontmatter, flags=re.M): - frontmatter = re.sub(r"^updated_at:\s*.*$", f"updated_at: {updated_at}", frontmatter, flags=re.M) - else: - frontmatter = f"{frontmatter}\nupdated_at: {updated_at}" - return frontmatter + body - - -def insert_progress_ledger_entry(text: str, entry: str, updated_at: str) -> tuple[str, bool]: - text = update_frontmatter_timestamp(text, updated_at) - if entry in text: - return text, False - - lines = text.splitlines() - for index, line in enumerate(lines): - if line.strip() != "## Progress Ledger": - continue - insert_at = index + 1 - while insert_at < len(lines) and not lines[insert_at].strip(): - insert_at += 1 - lines[insert_at:insert_at] = entry.splitlines() + [""] - return "\n".join(lines).rstrip() + "\n", True - - separator = "" if text.endswith("\n") else "\n" - return f"{text}{separator}\n## Progress Ledger\n\n{entry}\n", True def plan_active_state_update( *, - registry: dict[str, Any], + registry: Mapping[str, object], goal_id: str, state_file_override: Path | None, active_state_summary: str, - project_agent_visibility: dict[str, Any], - reward: dict[str, Any], + project_agent_visibility: Mapping[str, object], + reward: Mapping[str, object], requested: bool, dry_run: bool, -) -> tuple[dict[str, Any], Path | None, str | None]: - update: dict[str, Any] = { +) -> tuple[dict[str, object], Path | None, str | None]: + """Plan optional Progress Ledger writeback without bypassing prose-write guards.""" + update: dict[str, object] = { "requested": requested, "written": False, "would_write": False, @@ -433,7 +233,9 @@ def plan_active_state_update( state_file_override=state_file_override, ) if state_file is None: - raise ValueError("active state summary write requested, but the registry goal has no state_file") + raise ValueError( + "active state summary write requested, but the registry goal has no state_file" + ) if not state_file.exists(): raise FileNotFoundError(f"active state file does not exist: {state_file}") @@ -467,53 +269,106 @@ class HumanRewardSummaryWriteError(RuntimeError): payload = {"appended": True, "active_state_summary_written": False} +def _select_reward_run( + index_path: Path, + run_generated_at: str | None, +) -> tuple[dict[str, object], int]: + """Select one target run without stat'ing unselected historical artifacts.""" + history_mod = _import_module("loopx.history") + patched_loader = globals().get("load_index") + original_loader: object = getattr(history_mod, "load_index", None) + observe_selected = False + if patched_loader is not None and patched_loader is not original_loader: + if not callable(patched_loader): + raise TypeError("feedback adapter 'load_index' must be callable") + loaded = patched_loader(index_path) + if not isinstance(loaded, tuple) or len(loaded) != 2: + raise ValueError("load_index adapter must return (runs, raw_count)") + raw_runs, raw_count_obj = loaded + if not isinstance(raw_runs, Sequence) or isinstance(raw_runs, (str, bytes)): + raise ValueError("load_index returned invalid runs") + runs: Sequence[Mapping[str, object]] = [ + row for row in raw_runs if isinstance(row, Mapping) + ] + raw_count = int(raw_count_obj) + else: + snapshot_loader: object = getattr(history_mod, "load_index_snapshot", None) + if not callable(snapshot_loader): + raise AttributeError("loopx.history has no callable 'load_index_snapshot'") + snapshot = snapshot_loader(index_path, include_artifact_status=False) + raw_records: object = getattr(snapshot, "records", ()) + if not isinstance(raw_records, Sequence) or isinstance(raw_records, (str, bytes)): + raise ValueError("load_index_snapshot returned invalid records") + runs = [row for row in raw_records if isinstance(row, Mapping)] + raw_count = int(getattr(snapshot, "raw_count", 0)) + observe_selected = True + + selected = select_run(runs, run_generated_at) + missing_paths = [ + field for field in ("json_path", "markdown_path") if not selected.get(field) + ] + if missing_paths: + raise ValueError( + f"selected run is missing required index path fields: {', '.join(missing_paths)}" + ) + + if observe_selected: + context_fn: object = getattr(history_mod, "_goal_run_index_context", None) + observe_fn: object = getattr(history_mod, "_observe_run_artifacts", None) + inferred = context_fn(index_path) if callable(context_fn) else None + if callable(observe_fn): + runs_dir = inferred[0] if isinstance(inferred, tuple) and len(inferred) >= 1 else None + goal_id = inferred[1] if isinstance(inferred, tuple) and len(inferred) >= 2 else None + observe_fn( + [selected], + artifact_root=None, + runs_dir=runs_dir, + goal_id=goal_id, + ) + return selected, raw_count + + def append_human_reward( *, registry_path: Path, runtime_root_override: str | None, goal_id: str, run_generated_at: str | None, - reward: dict[str, Any], + reward: Mapping[str, object], actor_kind: str | None = None, dry_run: bool = False, state_file_override: Path | None = None, write_active_state_summary: bool = False, -) -> dict[str, Any]: +) -> dict[str, object]: + """Validate and append one run-bound human reward overlay (with optional dry-run).""" # Resolve before registry reads or any append. An unavailable adapter must # fail before the reward index can commit; TS still owns the write check. - _mutation_adapter("require_prose_state_write_allowed") - _mutation_adapter("atomic_write_state_text") + _callable_adapter("require_prose_state_write_allowed") + _callable_adapter("atomic_write_state_text") actor = normalize_owner_controller_actor(actor_kind, required=not dry_run) validate_goal_id(goal_id) - registry = _mutation_adapter("load_registry")(registry_path) - runtime_root = resolve_runtime_root(registry, runtime_root_override, registry_path=registry_path) + raw_registry = _callable_adapter("load_registry")(registry_path) + if not isinstance(raw_registry, Mapping): + raise ValueError("registry payload must be an object") + registry = raw_registry if isinstance(raw_registry, dict) else dict(raw_registry) + runtime_root = resolve_runtime_root( + registry, runtime_root_override, registry_path=registry_path + ) index_path = runtime_root / "goals" / goal_id / "runs" / "index.jsonl" - runs, raw_count = _mutation_adapter("load_index")(index_path) - selected = select_run(runs, run_generated_at) - - missing_paths = [ - field - for field in ("json_path", "markdown_path") - if not selected.get(field) - ] - if missing_paths: - raise ValueError(f"selected run is missing required index path fields: {', '.join(missing_paths)}") + selected, raw_count = _select_reward_run(index_path, run_generated_at) - index_record = { - field: selected[field] - for field in RUN_OVERLAY_FIELDS - if field in selected + index_record: dict[str, object] = { + field: selected[field] for field in RUN_OVERLAY_FIELDS if field in selected } index_record["goal_id"] = str(index_record.get("goal_id") or goal_id) - index_record["human_reward"] = { - field: reward[field] - for field in HUMAN_REWARD_FIELDS - if field in reward + human_reward: dict[str, object] = { + field: reward[field] for field in HUMAN_REWARD_FIELDS if field in reward } if actor is not None: - index_record["human_reward"]["actor_kind"] = actor.value + human_reward["actor_kind"] = actor.value + index_record["human_reward"] = human_reward - selected_run = { + selected_run: dict[str, object] = { "generated_at": selected.get("generated_at"), "classification": selected.get("classification"), "recommended_action": selected.get("recommended_action"), @@ -523,49 +378,61 @@ def append_human_reward( coordination = build_reward_coordination( goal_id=goal_id, selected_run=selected_run, - reward=index_record["human_reward"], + reward=human_reward, dry_run=dry_run, ) - project_agent_visibility = ( - cast(dict[str, Any], coordination.get("project_agent_visibility")) - if isinstance(coordination.get("project_agent_visibility"), dict) - else {} + raw_visibility = coordination.get("project_agent_visibility") + project_agent_visibility: Mapping[str, object] = ( + raw_visibility if isinstance(raw_visibility, Mapping) else {} ) - active_state_update, state_file_to_write, state_text_to_write = plan_active_state_update( - registry=registry, - goal_id=goal_id, - state_file_override=state_file_override, - active_state_summary=str(coordination.get("active_state_summary") or ""), - project_agent_visibility=project_agent_visibility, - reward=index_record["human_reward"], - requested=write_active_state_summary, - dry_run=dry_run, + active_state_update, state_file_to_write, state_text_to_write = ( + plan_active_state_update( + registry=registry, + goal_id=goal_id, + state_file_override=state_file_override, + active_state_summary=str(coordination.get("active_state_summary") or ""), + project_agent_visibility=project_agent_visibility, + reward=human_reward, + requested=write_active_state_summary, + dry_run=dry_run, + ) ) if not dry_run: state_lock = ( - exclusive_cross_runtime_file_lock(state_file_to_write, operation="reward_summary") - if state_file_to_write is not None else nullcontext() + exclusive_cross_runtime_file_lock( + state_file_to_write, operation="reward_summary" + ) + if state_file_to_write is not None + else nullcontext() ) # Match refresh/history: index first, then the source state lock. from .file_lock import exclusive_run_index_lock + with exclusive_run_index_lock(index_path, operation="reward_append"), state_lock: if state_file_to_write is not None: original = state_file_to_write.read_text(encoding="utf-8") planned, changed = insert_progress_ledger_entry( - original, str(active_state_update["entry"]), - updated_at=str(reward.get("recorded_at") or now_local()), + original, + str(active_state_update["entry"]), + updated_at=str(human_reward.get("recorded_at") or now_local()), ) - _mutation_adapter("require_prose_state_write_allowed")( - registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id, - state_path=state_file_to_write, original_text=original, planned_text=planned, + _callable_adapter("require_prose_state_write_allowed")( + registry_path=registry_path, + runtime_root=runtime_root, + goal_id=goal_id, + state_path=state_file_to_write, + original_text=original, + planned_text=planned, ) state_text_to_write = planned if changed else None index_path.parent.mkdir(parents=True, exist_ok=True) - _mutation_adapter("append_run_index_record")(index_path, index_record) + _callable_adapter("append_run_index_record")(index_path, index_record) if state_file_to_write and state_text_to_write is not None: try: - _mutation_adapter("atomic_write_state_text")(state_file_to_write, state_text_to_write) + _callable_adapter("atomic_write_state_text")( + state_file_to_write, state_text_to_write + ) except OSError as error: raise HumanRewardSummaryWriteError(str(error)) from error active_state_update["written"] = True @@ -580,7 +447,7 @@ def append_human_reward( "raw_index_records_before": raw_count, "dry_run": dry_run, "selected_run": selected_run, - "human_reward": index_record["human_reward"], + "human_reward": human_reward, "actor_kind": actor.value if actor is not None else None, **coordination, "active_state_update": active_state_update, @@ -589,110 +456,5 @@ def append_human_reward( } -def describe_active_state_effect(state_update: dict[str, Any], *, dry_run: bool) -> str: - if not state_update or not state_update.get("requested"): - return "未请求 active-state 摘要写回" - state_file = state_update.get("state_file") or "unknown state file" - if state_update.get("already_present"): - return f"摘要已存在,未重复写入 `{state_file}`" - if dry_run and state_update.get("would_write"): - return f"预览:会把摘要写入 `{state_file}` 的 Progress Ledger" - if state_update.get("written"): - return f"已把摘要写入 `{state_file}` 的 Progress Ledger" - return f"未写入 active state;检查 `{state_file}`" - - -def describe_run_overlay_effect(*, appended: bool, dry_run: bool, index_path: str | None) -> str: - target = f"`{index_path}`" if index_path else "run index" - if dry_run: - return f"预览:不会写 {target}" - if appended: - return f"已追加 human_reward overlay 到 {target}" - return f"未写 {target}" - - -def render_reward_markdown(payload: dict[str, Any]) -> str: - lines = [ - "# LoopX Human Reward", - "", - f"- ok: `{payload.get('ok')}`", - f"- goal: `{payload.get('goal_id')}`", - f"- index: `{payload.get('index_path')}`", - f"- appended: `{payload.get('appended')}`", - f"- dry_run: `{payload.get('dry_run')}`", - ] - if payload.get("error"): - lines.append(f"- error: {payload.get('error')}") - return "\n".join(lines) - - selected = cast(dict[str, Any], payload.get("selected_run")) if isinstance(payload.get("selected_run"), dict) else {} - reward = cast(dict[str, Any], payload.get("human_reward")) if isinstance(payload.get("human_reward"), dict) else {} - state_update = cast(dict[str, Any], payload.get("active_state_update")) if isinstance(payload.get("active_state_update"), dict) else {} - visibility = cast(dict[str, Any], payload.get("project_agent_visibility")) if isinstance(payload.get("project_agent_visibility"), dict) else {} - lines.extend( - [ - "", - "## Write Effect", - f"- selected_run: `{selected.get('generated_at')}`", - f"- run_overlay: {describe_run_overlay_effect(appended=bool(payload.get('appended')), dry_run=bool(payload.get('dry_run')), index_path=payload.get('index_path'))}", - f"- active_state: {describe_active_state_effect(state_update, dry_run=bool(payload.get('dry_run')))}", - ] - ) - if visibility.get("history_command"): - lines.append(f"- project_agent_visibility: `{visibility.get('history_command')}`") - lines.extend( - [ - "", - "## Selected Run", - f"- generated_at: `{selected.get('generated_at')}`", - f"- classification: `{selected.get('classification')}`", - f"- artifacts: `{selected.get('json_exists')}/{selected.get('markdown_exists')}`", - "", - "## Reward", - f"- recorded_at: `{reward.get('recorded_at')}`", - f"- actor_kind: `{reward.get('actor_kind')}`", - f"- decision: `{reward.get('decision')}`", - f"- reward: `{reward.get('reward')}`", - f"- reason_summary: {reward.get('reason_summary')}", - ] - ) - if reward.get("follow_up"): - lines.append(f"- follow_up: {reward.get('follow_up')}") - lesson = cast(dict[str, Any], reward.get("lesson")) if isinstance(reward.get("lesson"), dict) else {} - if lesson: - lines.append(f"- lesson_kind: `{lesson.get('kind')}`") - lines.append(f"- lesson_summary: {lesson.get('summary')}") - for field in ("avoid", "prefer"): - values = cast(list[Any], lesson.get(field)) if isinstance(lesson.get(field), list) else [] - if values: - lines.append(f"- lesson_{field}: {', '.join(str(value) for value in values[:5])}") - if payload.get("active_state_summary"): - lines.extend(["", "## Active-State Summary", str(payload.get("active_state_summary"))]) - if state_update: - lines.extend( - [ - "", - "## Active-State Writeback", - f"- requested: `{state_update.get('requested')}`", - f"- state_file: `{state_update.get('state_file')}`", - f"- would_write: `{state_update.get('would_write')}`", - f"- written: `{state_update.get('written')}`", - f"- already_present: `{state_update.get('already_present')}`", - ] - ) - if visibility: - lines.extend( - [ - "", - "## Project-Agent Visibility", - f"- source_of_truth: `{visibility.get('source_of_truth')}`", - f"- history_command: `{visibility.get('history_command')}`", - f"- active_state_role: `{visibility.get('active_state_role')}`", - f"- review_packet_role: `{visibility.get('review_packet_role')}`", - ] - ) - return "\n".join(lines) - - # Keep the historical star-import surface, including the deferred adapters. __all__ = [name for name in globals() if not name.startswith("_")] + list(_LAZY_IMPORTS) diff --git a/tests/control_plane/test_feedback_contract.py b/tests/control_plane/test_feedback_contract.py new file mode 100644 index 0000000000..0e077afc53 --- /dev/null +++ b/tests/control_plane/test_feedback_contract.py @@ -0,0 +1,316 @@ +"""Contract, zero-Any, MappingProxyType, and O(1) run-selection tests for loopx.feedback.""" + +from __future__ import annotations + +import ast +import json +from pathlib import Path +from types import MappingProxyType +import pytest + +from loopx import feedback +from loopx import history +from loopx.control_plane.runtime import reward_rendering + + +def test_feedback_zero_any_budgets_and_exports() -> None: + for rel, max_loc in ( + ("loopx/feedback.py", 550), + ("loopx/control_plane/runtime/reward_rendering.py", 750), + ("tests/control_plane/test_feedback_contract.py", 400), + ): + src = (Path(__file__).resolve().parents[2] / rel).read_text(encoding="utf-8") + assert len(src.splitlines()) <= max_loc, rel + tree = ast.parse(src) + anys = [n for n in ast.walk(tree) if isinstance(n, ast.Name) and n.id == "Any"] + assert anys == [], rel + assert ("type: " + "ignore") not in src, rel + assert ("no" + "qa") not in src, rel + + reexported = ( + "REWARD_VALUES", + "LESSON_KINDS", + "now_utc", + "now_local", + "public_safe_text_guidance", + "validate_public_safe_text", + "compact_reward", + "compact_reward_lesson", + "build_reward_coordination", + "build_active_state_entry", + "update_frontmatter_timestamp", + "insert_progress_ledger_entry", + "describe_active_state_effect", + "describe_run_overlay_effect", + "render_reward_markdown", + ) + listed_dir = set(dir(feedback)) + for name in reexported: + assert name in feedback.__all__, name + assert name in listed_dir, name + assert getattr(feedback, name) is getattr(reward_rendering, name), name + + with pytest.raises(AttributeError): + getattr(feedback, "nonexistent_feedback_symbol") + + +def test_mapping_proxy_and_text_validation_contracts(tmp_path: Path) -> None: + feedback.validate_local_control_text("recommended_action", "Inspect task-123 locally") + secret_header = "Author" + "ization: demo" + with pytest.raises(ValueError, match="secret-looking value"): + feedback.validate_local_control_text("recommended_action", secret_header) + bearer_secret = "Bear" + "er " + "a" * 24 + with pytest.raises(ValueError, match="private-looking value"): + feedback.validate_public_safe_text("recommended_action", bearer_secret) + + lesson = MappingProxyType( + { + "kind": "route", + "summary": "Prefer narrow verification before broad runs.", + "avoid": "unscoped retries", + "prefer": ["exact selector", "exact selector", " "], + "target_surface": ["status_server", "cli"], + "constraint": MappingProxyType({"lane": "reward", "empty": ""}), + } + ) + compacted = feedback.compact_reward( + recorded_at="2026-10-01T00:00:00Z", + decision="continue", + reward="positive", + reason_summary="Verified targeted route", + follow_up=None, + lesson=lesson, + ) + assert compacted["decision"] == "continue" + compacted_lesson = compacted["lesson"] + assert isinstance(compacted_lesson, dict) + assert compacted_lesson["avoid"] == ["unscoped retries"] + assert compacted_lesson["prefer"] == ["exact selector"] + + project = tmp_path / "project" + project.mkdir() + registry = MappingProxyType( + {"goals": ({"id": "g-1", "repo": str(project), "state_file": "ACTIVE.md"},)} + ) + registry_with_list = MappingProxyType( + {"goals": [{"id": "g-1", "repo": str(project), "state_file": "ACTIVE.md"}]} + ) + assert feedback.find_registry_goal(registry, "g-1") is None + found = feedback.find_registry_goal(registry_with_list, "g-1") + assert found is not None and found["id"] == "g-1" + assert ( + feedback.resolve_reward_state_file( + registry=registry_with_list, goal_id="g-1", state_file_override=None + ) + == project / "ACTIVE.md" + ) + + runs = ( + MappingProxyType({"generated_at": "2026-10-01T00:00:00Z", "classification": "c1"}), + MappingProxyType({"generated_at": "2026-10-02T00:00:00Z", "classification": "c2"}), + ) + assert feedback.select_run(runs, None)["classification"] == "c2" + + coordination = feedback.build_reward_coordination( + goal_id="g-1", + selected_run=MappingProxyType({"generated_at": "2026-10-02T00:00:00Z"}), + reward=MappingProxyType( + { + "reward": "positive", + "decision": "continue", + "reason_summary": "Looks good", + "lesson": lesson, + } + ), + dry_run=True, + ) + rendered = feedback.render_reward_markdown( + MappingProxyType( + { + "ok": True, + "goal_id": "g-1", + "index_path": "goals/g-1/runs/index.jsonl", + "appended": False, + "dry_run": True, + "selected_run": {"generated_at": "2026-10-02T00:00:00Z", "classification": "c2"}, + "human_reward": { + "recorded_at": "2026-10-02T00:05:00Z", + "actor_kind": "owner", + "decision": "continue", + "reward": "positive", + "reason_summary": "Looks good", + "lesson": lesson, + }, + "active_state_summary": coordination["active_state_summary"], + "project_agent_visibility": coordination["project_agent_visibility"], + } + ) + ) + assert "lesson_kind: `route`" in rendered + assert "lesson_prefer: exact selector" in rendered + + +def test_append_human_reward_observes_only_selected_run_artifacts( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + runtime = tmp_path / "runtime" + project = tmp_path / "project" + project.mkdir() + registry_path = project / ".loopx" / "registry.json" + registry_path.parent.mkdir() + registry_path.write_text( + json.dumps( + { + "common_runtime_root": str(runtime), + "goals": [{"id": "g-perf", "repo": str(project)}], + } + ), + encoding="utf-8", + ) + runs_dir = runtime / "goals" / "g-perf" / "runs" + runs_dir.mkdir(parents=True) + selected_json = runs_dir / "run-24.json" + selected_md = runs_dir / "run-24.md" + selected_json.write_text('{"ok": true}\n', encoding="utf-8") + selected_md.write_text("# Run 24\n", encoding="utf-8") + rows = [ + { + "generated_at": f"2026-10-01T00:00:{i:02d}Z", + "classification": f"run-{i:02d}", + "json_path": str(runs_dir / f"run-{i:02d}.json"), + "markdown_path": str(runs_dir / f"run-{i:02d}.md"), + } + for i in range(25) + ] + index_path = runs_dir / "index.jsonl" + index_path.write_text( + "".join(json.dumps(row) + "\n" for row in rows), + encoding="utf-8", + ) + + observed_batches: list[list[str]] = [] + original_observe = history._observe_run_artifacts + + def spy_observe( + records: list[dict[str, object]], + *, + artifact_root: Path | None, + runs_dir: Path | None = None, + goal_id: str | None = None, + ) -> None: + observed_batches.append([str(row.get("classification")) for row in records]) + original_observe( + records, + artifact_root=artifact_root, + runs_dir=runs_dir, + goal_id=goal_id, + ) + + monkeypatch.setattr(history, "_observe_run_artifacts", spy_observe) + + result = feedback.append_human_reward( + registry_path=registry_path, + runtime_root_override=None, + goal_id="g-perf", + run_generated_at=None, + reward={ + "recorded_at": "2026-10-02T00:00:00Z", + "decision": "continue", + "reward": "positive", + "reason_summary": "Verified single-run artifact stat", + }, + dry_run=True, + ) + assert observed_batches == [["run-24"]] + assert result["raw_index_records_before"] == 25 + assert result["selected_run"] == { + "generated_at": "2026-10-01T00:00:24Z", + "classification": "run-24", + "recommended_action": None, + "json_exists": True, + "markdown_exists": True, + } + + +def test_append_human_reward_error_boundaries_and_patched_loader( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + with pytest.raises(ValueError, match="reward must be one of"): + feedback.compact_reward( + recorded_at=None, + decision="continue", + reward="invalid", + reason_summary="ok", + follow_up=None, + ) + with pytest.raises(ValueError, match="lesson kind"): + feedback.compact_reward_lesson({"kind": "bad", "summary": "x"}) + with pytest.raises(ValueError, match="lesson summary"): + feedback.compact_reward_lesson({"kind": "route", "summary": " "}) + with pytest.raises(ValueError, match="must be iterable"): + feedback.compact_reward_lesson({"kind": "route", "summary": "x", "avoid": 123}) + with pytest.raises(ValueError, match="no compact run records"): + feedback.select_run([], None) + with pytest.raises(ValueError, match="run not found"): + feedback.select_run([{"generated_at": "2026-10-01T00:00:00Z"}], "missing") + + runtime = tmp_path / "runtime" + project = tmp_path / "project" + project.mkdir() + state_file = project / "ACTIVE.md" + state_file.write_text("## Progress Ledger\n", encoding="utf-8") + registry_path = project / ".loopx" / "registry.json" + registry_path.parent.mkdir() + registry_path.write_text( + json.dumps( + { + "common_runtime_root": str(runtime), + "goals": [ + { + "id": "g-err", + "repo": str(project), + "state_file": "ACTIVE.md", + } + ], + } + ), + encoding="utf-8", + ) + + monkeypatch.setattr( + feedback, + "load_index", + lambda _path: ( + [ + { + "generated_at": "2026-10-01T00:00:00Z", + "classification": "c1", + "json_path": "r.json", + "markdown_path": "r.md", + } + ], + 1, + ), + ) + + def fail_write(_path: Path, _content: str) -> None: + raise OSError("disk full") + + monkeypatch.setattr(feedback, "atomic_write_state_text", fail_write) + with pytest.raises(feedback.HumanRewardSummaryWriteError, match="disk full"): + feedback.append_human_reward( + registry_path=registry_path, + runtime_root_override=None, + goal_id="g-err", + run_generated_at=None, + reward={ + "recorded_at": "2026-10-01T01:00:00Z", + "decision": "continue", + "reward": "positive", + "reason_summary": "Accept", + }, + actor_kind="owner", + write_active_state_summary=True, + )