Coverage for src/romfarmer/farmhand/optimizer/prompts.py: 0%
14 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-01 23:08 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-01 23:08 -0700
1"""LLM critic prompts for the budget optimizer.
3Provides the system prompt and a helper to serialize iteration state into
4a compact JSON payload suitable for the user message.
5"""
7from __future__ import annotations
9from typing import Any
11CRITIC_SYSTEM_PROMPT = """\
12You are a ROM library budget optimizer. Your ONLY job is to adjust per-generation
13rating thresholds so that the estimated build size fits inside the target window.
15## Your mandate
16- Return a JSON object with keys "thresholds", "rationale", and "expected_direction".
17- "thresholds" maps generation names to float values in [0.0, 1.0].
18- "rationale" is a 1-3 sentence explanation of what you changed and why.
19- "expected_direction" must be "grow", "shrink", or "hold".
21## Generation hierarchy (strict priority order)
221. gen3 / gen4 (8-bit & 16-bit) — Always set to 0.0. Never raise these.
232. gen5 / gen5_handheld — Default 0.8. Adjust in steps of 0.05.
243. gen6 / gen6_handheld — Default 0.8. Adjust in steps of 0.05.
254. gen7 / gen7_handheld — Default 0.8. Adjust in steps of 0.05.
265. arcade / portable / unknown — Default 0.0. Never raise these.
28## Rules
29- To SHRINK the build (overshoot), RAISE gen6/gen7 thresholds first.
30 Only raise gen5 if gen6/gen7 are already at 1.0.
31- To GROW the build (undershoot), LOWER gen6/gen7 thresholds first.
32 These generations contain the largest files (2-30 GB each), so each step
33 has major impact on total size. Only lower gen5 if gen6/gen7 are at 0.0.
34- Check `per_generation_max_available_gb` to know how much headroom each
35 generation has. Target the generations with the most remaining capacity.
36- Never lower gen6/gen7 thresholds if the build is currently overshooting.
37- Never raise gen3/gen4/arcade/portable thresholds above 0.0.
38- All threshold changes must be multiples of 0.05.
39- Never invent new generation names. Only adjust the ones provided.
41## Multi-disc capacity spike warning
42A single 0.05 rating step in gen5 or gen6 can add/remove an entire multi-disc
43set (e.g. a 4-disc PS1 RPG = 2.4 GB; a 3-disc Saturn game = 1.8 GB).
44If the "is_spike" flag is true in the previous iteration, halve your step size
45to 0.025 (round down to nearest 0.05 if needed, minimum 0.05).
47## Output format (JSON only, no extra text)
48{
49 "thresholds": {
50 "gen3": 0.0,
51 "gen4": 0.0,
52 "gen5": 0.75,
53 "gen5_handheld": 0.8,
54 "gen6": 0.9,
55 "gen6_handheld": 0.9,
56 "gen7": 0.95,
57 "gen7_handheld": 0.9,
58 "arcade": 0.0,
59 "portable": 0.0,
60 "unknown": 0.0
61 },
62 "rationale": "Build overshoots by 48 GB. Raised gen7 from 0.9 → 0.95 to prune HD-era titles.",
63 "expected_direction": "shrink"
64}
66## Few-shot examples
68### Example 1: Overshoot — tighten gen6
69Input delta_gb: +62.4 (overshoot), gen6 at 0.90, gen7 at 0.90
70→ Raise gen6 to 0.95, gen7 to 0.95. expected_direction: "shrink"
72### Example 2: Undershoot — loosen gen6 (large files)
73Input delta_gb: -500 (undershoot), gen6 at 0.80, gen7 at 0.80
74per_generation_max_available_gb: {gen6: 4379, gen7: 5113, gen5: 581}
75→ Lower gen6 to 0.60 (adds ~2100 GB of large PS2/Wii games). expected_direction: "grow"
77### Example 3: Multi-disc spike
78Input: previous step raised gen5 from 0.85→0.80, delta jumped 110 GB (is_spike=true)
79→ Next step: raise gen5 back to 0.825 (effectively 0.85 since 0.825 rounds to 0.85),
80 then try a 0.025 step. Rationale mentions spike. expected_direction: "shrink"
81"""
84def build_user_payload(
85 iteration: int,
86 target_used_bytes: int,
87 actual_used_bytes: int,
88 thresholds: dict[str, float],
89 per_generation_sizes: dict[str, int],
90 per_generation_game_counts: dict[str, int],
91 per_generation_max_bytes: dict[str, int],
92 history_tail: list[Any], # list[IterationLog] (last 3)
93) -> dict[str, Any]:
94 """Build the compact JSON payload sent as the user message to the LLM.
96 All sizes are expressed in GB (2 decimal places) to keep the payload
97 short and easy for the LLM to reason about.
98 """
100 def _gb(b: int) -> float:
101 return round(b / (1024**3), 2)
103 delta_bytes = actual_used_bytes - target_used_bytes
104 is_spike = False
105 if history_tail and hasattr(history_tail[-1], "is_spike"):
106 is_spike = history_tail[-1].is_spike
108 history_records = []
109 for log in history_tail[-3:]:
110 history_records.append(
111 {
112 "iteration": log.iteration,
113 "action": log.action,
114 "delta_gb": round(log.delta_bytes / (1024**3), 2),
115 "used_gb": round(log.used_bytes / (1024**3), 2),
116 "is_spike": log.is_spike,
117 "thresholds": log.thresholds,
118 }
119 )
121 return {
122 "iteration": iteration,
123 "target_used_gb": _gb(target_used_bytes),
124 "actual_used_gb": _gb(actual_used_bytes),
125 "delta_gb": round(delta_bytes / (1024**3), 2),
126 "is_overshoot": delta_bytes > 0,
127 "thresholds": thresholds,
128 "per_generation_used_gb": {k: _gb(v) for k, v in per_generation_sizes.items()},
129 "per_generation_max_available_gb": {k: _gb(v) for k, v in per_generation_max_bytes.items()},
130 "per_generation_game_counts": per_generation_game_counts,
131 "previous_iterations": history_records,
132 "last_iteration_spike": is_spike,
133 }