File size: 22,505 Bytes
5f78183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
"""

Data models for CERNenv: an LHC (Large Hadron Collider) style particle

physics discovery POMDP (Partially Observable Markov Decision Process).



The agent is a Large Language Model (LLM) acting as a high-energy physicist.

Each step it picks one structured action (configure beams, allocate

luminosity, run a trigger, fit a spectrum, request systematics, submit a

discovery claim, etc.) and receives a noisy detector-style observation.

The latent particle and detector parameters are the hidden ground truth.

"""

from __future__ import annotations

from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field

from openenv.core.env_server.types import Action, Observation


# ── Action vocabulary ───────────────────────────────────────────────────────


class ActionType(str, Enum):
    # ── Beam & data acquisition (DAQ) ─────────────────────────────────
    CONFIGURE_BEAM = "configure_beam"
    ALLOCATE_LUMINOSITY = "allocate_luminosity"
    SET_TRIGGER = "set_trigger"
    COLLECT_COLLISIONS = "collect_collisions"

    # ── Reconstruction & calibration ─────────────────────────────────
    CALIBRATE_DETECTOR = "calibrate_detector"
    RECONSTRUCT_TRACKS = "reconstruct_tracks"
    SELECT_CHANNEL = "select_channel"

    # ── Analysis ──────────────────────────────────────────────────────
    BUILD_INVARIANT_MASS = "build_invariant_mass"
    SUBTRACT_BACKGROUND = "subtract_background"
    FIT_RESONANCE = "fit_resonance"
    SCAN_BUMP = "scan_bump"
    MEASURE_ANGULAR = "measure_angular"
    ESTIMATE_SIGNIFICANCE = "estimate_significance"

    # ── Systematics & meta ───────────────────────────────────────────
    REQUEST_SYSTEMATICS = "request_systematics"
    REQUEST_THEORY_REVIEW = "request_theory_review"

    # ── Final ─────────────────────────────────────────────────────────
    SUBMIT_DISCOVERY_CLAIM = "submit_discovery_claim"


DAQ_ACTIONS = frozenset({
    ActionType.CONFIGURE_BEAM,
    ActionType.ALLOCATE_LUMINOSITY,
    ActionType.SET_TRIGGER,
    ActionType.COLLECT_COLLISIONS,
})

RECO_ACTIONS = frozenset({
    ActionType.CALIBRATE_DETECTOR,
    ActionType.RECONSTRUCT_TRACKS,
    ActionType.SELECT_CHANNEL,
})

ANALYSIS_ACTIONS = frozenset({
    ActionType.BUILD_INVARIANT_MASS,
    ActionType.SUBTRACT_BACKGROUND,
    ActionType.FIT_RESONANCE,
    ActionType.SCAN_BUMP,
    ActionType.MEASURE_ANGULAR,
    ActionType.ESTIMATE_SIGNIFICANCE,
})

META_ACTIONS = frozenset({
    ActionType.REQUEST_SYSTEMATICS,
    ActionType.REQUEST_THEORY_REVIEW,
    ActionType.SUBMIT_DISCOVERY_CLAIM,
})


# ── Detector channels & physics primitives ────────────────────────────────


class DetectorChannel(str, Enum):
    """Final-state decay channel the agent reconstructs in.



    Channels affect signal acceptance and background composition. Picking a

    channel where the true particle does not decay yields low signal yield

    no matter how much luminosity is collected — this is intentional.

    """

    DIPHOTON = "diphoton"          # γγ
    DILEPTON_EE = "dilepton_ee"    # e+ e-
    DILEPTON_MUMU = "dilepton_mumu"  # μ+ μ-
    DIJET = "dijet"                # jj
    FOUR_LEPTON = "four_lepton"    # 4ℓ
    BB = "bb"                      # b b-bar


class TriggerType(str, Enum):
    """Hardware-level event selection."""

    LOW_PT = "low_pt"          # broad acceptance, lots of background
    HIGH_PT = "high_pt"        # high-mass focus, lower QCD
    DIPHOTON_HLT = "diphoton_hlt"
    DILEPTON_HLT = "dilepton_hlt"
    JET_HLT = "jet_hlt"


class BeamEnergy(str, Enum):
    """LHC-style center-of-mass energies (TeV)."""

    E_7 = "7TeV"
    E_8 = "8TeV"
    E_13 = "13TeV"
    E_14 = "14TeV"


# ── Tool / instrument registry (for prompts and tool-fit reward) ──────────


class ToolCategory(str, Enum):
    DAQ = "daq"
    RECONSTRUCTION = "reconstruction"
    CALIBRATION = "calibration"
    ANALYSIS = "analysis"
    STATISTICS = "statistics"
    SYSTEMATICS = "systematics"


class ToolSpec(BaseModel):
    name: str
    category: ToolCategory
    description: str = ""
    typical_runtime_hours: float = 0.5
    typical_cost_musd: float = 0.0  # in millions of USD (compute / beam time proxy)
    requires_gpu: bool = False
    channels: List[str] = Field(default_factory=list)


TOOL_REGISTRY: Dict[str, ToolSpec] = {
    "ATLAS_HLT": ToolSpec(
        name="ATLAS_HLT",
        category=ToolCategory.DAQ,
        description="ATLAS High-Level Trigger system for online event selection",
        typical_runtime_hours=0.0,
        channels=["diphoton", "dilepton_ee", "dilepton_mumu", "four_lepton", "dijet", "bb"],
    ),
    "CMS_HLT": ToolSpec(
        name="CMS_HLT",
        category=ToolCategory.DAQ,
        description="CMS High-Level Trigger system",
        typical_runtime_hours=0.0,
        channels=["diphoton", "dilepton_ee", "dilepton_mumu", "four_lepton", "dijet", "bb"],
    ),
    "GEANT4": ToolSpec(
        name="GEANT4",
        category=ToolCategory.RECONSTRUCTION,
        description="Detector simulation toolkit for full event reconstruction",
        typical_runtime_hours=1.0,
        typical_cost_musd=0.05,
        requires_gpu=False,
    ),
    "Athena": ToolSpec(
        name="Athena",
        category=ToolCategory.RECONSTRUCTION,
        description="ATLAS reconstruction framework",
        typical_runtime_hours=0.8,
    ),
    "CMSSW": ToolSpec(
        name="CMSSW",
        category=ToolCategory.RECONSTRUCTION,
        description="CMS reconstruction software",
        typical_runtime_hours=0.8,
    ),
    "ECAL_calibration": ToolSpec(
        name="ECAL_calibration",
        category=ToolCategory.CALIBRATION,
        description="Electromagnetic calorimeter energy-scale calibration",
        typical_runtime_hours=0.3,
    ),
    "Tracker_alignment": ToolSpec(
        name="Tracker_alignment",
        category=ToolCategory.CALIBRATION,
        description="Inner tracker alignment for momentum precision",
        typical_runtime_hours=0.4,
    ),
    "ROOT_RooFit": ToolSpec(
        name="ROOT_RooFit",
        category=ToolCategory.ANALYSIS,
        description="Maximum-likelihood spectrum fitting toolkit",
        typical_runtime_hours=0.2,
    ),
    "MadGraph": ToolSpec(
        name="MadGraph",
        category=ToolCategory.ANALYSIS,
        description="Matrix-element generator for signal+background templates",
        typical_runtime_hours=1.5,
        typical_cost_musd=0.02,
    ),
    "Pythia8": ToolSpec(
        name="Pythia8",
        category=ToolCategory.ANALYSIS,
        description="Parton-shower and hadronisation generator",
        typical_runtime_hours=0.5,
    ),
    "BumpHunter": ToolSpec(
        name="BumpHunter",
        category=ToolCategory.STATISTICS,
        description="Sliding-window local-significance bump-hunting algorithm",
        typical_runtime_hours=0.1,
    ),
    "CLs_fit": ToolSpec(
        name="CLs_fit",
        category=ToolCategory.STATISTICS,
        description="Modified-frequentist CLs limits and significance",
        typical_runtime_hours=0.1,
    ),
    "Asimov_significance": ToolSpec(
        name="Asimov_significance",
        category=ToolCategory.STATISTICS,
        description="Asymptotic significance from Asimov dataset",
        typical_runtime_hours=0.05,
    ),
    "JES_systematics": ToolSpec(
        name="JES_systematics",
        category=ToolCategory.SYSTEMATICS,
        description="Jet energy-scale systematic study",
        typical_runtime_hours=0.4,
    ),
    "Luminosity_calibration": ToolSpec(
        name="Luminosity_calibration",
        category=ToolCategory.SYSTEMATICS,
        description="Van der Meer scan luminosity calibration",
        typical_runtime_hours=0.3,
    ),
}


# ── Action schema ──────────────────────────────────────────────────────────


class ExperimentAction(Action):
    """One structured experimental step at the LHC."""

    action_type: ActionType = Field(
        ...,
        description=(
            "Discrete LHC pipeline step. The environment enforces physics "
            "prerequisites: you cannot fit a spectrum before collecting data, "
            "or claim a discovery before estimating significance."
        ),
    )
    method: Optional[str] = Field(
        None,
        description=(
            "Optional named instrument or framework (e.g. 'ROOT_RooFit', "
            "'BumpHunter', 'Pythia8'). Affects cost, runtime, and tool-fit reward."
        ),
    )
    parameters: Dict[str, Any] = Field(
        default_factory=dict,
        description=(
            "Action-specific settings such as beam energy, integrated luminosity "
            "(fb^-1), trigger selection, decay channel, mass window, fit model."
        ),
    )
    justification: Optional[str] = Field(
        None,
        description="Short scientific rationale for picking this step now.",
    )
    confidence: float = Field(
        0.5, ge=0.0, le=1.0,
        description="Agent confidence in the chosen step.",
    )


# ── Outputs ────────────────────────────────────────────────────────────────


class OutputType(str, Enum):
    BEAM_CONFIG = "beam_config"
    LUMINOSITY_LOG = "luminosity_log"
    TRIGGER_REPORT = "trigger_report"
    COLLISION_BATCH = "collision_batch"
    CALIBRATION_REPORT = "calibration_report"
    RECONSTRUCTION = "reconstruction"
    CHANNEL_SELECTION = "channel_selection"
    INVARIANT_MASS_HIST = "invariant_mass_hist"
    BACKGROUND_SUBTRACTION = "background_subtraction"
    FIT_RESULT = "fit_result"
    BUMP_SCAN = "bump_scan"
    ANGULAR_RESULT = "angular_result"
    SIGNIFICANCE = "significance"
    SYSTEMATICS_REPORT = "systematics_report"
    THEORY_REVIEW = "theory_review"
    DISCOVERY_CLAIM = "discovery_claim"
    FAILURE_REPORT = "failure_report"


class IntermediateOutput(BaseModel):
    """A single noisy detector or analysis artifact."""

    output_type: OutputType
    step_index: int
    success: bool = True
    quality_score: float = Field(1.0, ge=0.0, le=1.0)
    summary: str = ""
    data: Dict[str, Any] = Field(default_factory=dict)
    uncertainty: float = Field(0.0, ge=0.0, le=1.0)
    warnings: List[str] = Field(default_factory=list)
    artifacts_available: List[str] = Field(default_factory=list)


# ── Observable state components ───────────────────────────────────────────


class ResourceUsage(BaseModel):
    """Agent-visible resource counters."""

    budget_used_musd: float = 0.0
    budget_remaining_musd: float = 100.0
    luminosity_used_fb: float = 0.0
    luminosity_remaining_fb: float = 300.0
    time_used_days: float = 0.0
    time_remaining_days: float = 365.0
    compute_hours_used: float = 0.0


class PipelineStepRecord(BaseModel):
    step_index: int
    action_type: ActionType
    method: Optional[str] = None
    parameters: Dict[str, Any] = Field(default_factory=dict)
    output_summary: str = ""
    output_type: OutputType
    success: bool = True
    quality_score: float = 1.0
    cost_musd: float = 0.0
    luminosity_cost_fb: float = 0.0
    time_cost_days: float = 0.0


class PaperReference(BaseModel):
    title: str
    citation: Optional[str] = None
    doi: Optional[str] = None
    arxiv_id: Optional[str] = None
    url: Optional[str] = None


class ExpectedFinding(BaseModel):
    finding: str
    category: str = "claim"
    keywords: List[str] = Field(default_factory=list)


class TaskSpec(BaseModel):
    """The physics question the agent is given for this episode."""

    problem_statement: str = "Discover and characterise an unknown resonance."
    target_collider: str = "LHC"
    beam_energy_options: List[str] = Field(
        default_factory=lambda: [e.value for e in BeamEnergy],
    )
    available_channels: List[str] = Field(
        default_factory=lambda: [c.value for c in DetectorChannel],
    )
    available_triggers: List[str] = Field(
        default_factory=lambda: [t.value for t in TriggerType],
    )
    available_tools: List[str] = Field(
        default_factory=lambda: list(TOOL_REGISTRY.keys()),
    )
    mass_search_window_gev: List[float] = Field(default_factory=lambda: [50.0, 1000.0])
    budget_limit_musd: float = 100.0
    luminosity_budget_fb: float = 300.0
    time_limit_days: float = 365.0
    prior_observations: List[str] = Field(default_factory=list)
    success_criteria: List[str] = Field(default_factory=list)
    paper_references: List[PaperReference] = Field(default_factory=list)
    expected_findings: List[ExpectedFinding] = Field(default_factory=list)
    difficulty: str = "medium"


class DiscoveryClaim(BaseModel):
    """Structured final claim graded against hidden truth."""

    claim: str = ""
    mass_estimate_gev: Optional[float] = None
    mass_uncertainty_gev: Optional[float] = None
    width_estimate_gev: Optional[float] = None
    significance_sigma: Optional[float] = None
    decay_channel: Optional[str] = None
    spin_hypothesis: Optional[int] = None  # 0, 1, 2
    parity: Optional[str] = None  # "+", "-"
    cross_section_fb: Optional[float] = None
    confidence: float = Field(0.5, ge=0.0, le=1.0)
    evidence_steps: List[int] = Field(default_factory=list)


class CollisionObservation(Observation):
    """Full observable state returned to the agent each step.



    Excludes the hidden particle truth and hidden detector systematics.

    """

    task: TaskSpec = Field(default_factory=TaskSpec)
    step_index: int = 0
    pipeline_history: List[PipelineStepRecord] = Field(default_factory=list)
    available_channels: List[str] = Field(default_factory=list)
    available_triggers: List[str] = Field(default_factory=list)
    available_tools: List[str] = Field(default_factory=list)
    resource_usage: ResourceUsage = Field(default_factory=ResourceUsage)
    latest_output: Optional[IntermediateOutput] = None
    all_outputs: List[IntermediateOutput] = Field(default_factory=list)
    candidate_masses_gev: List[float] = Field(default_factory=list)
    candidate_significances: List[float] = Field(default_factory=list)
    selected_channel: Optional[str] = None
    selected_beam_energy: Optional[str] = None
    cumulative_significance: float = 0.0
    uncertainty_summary: Dict[str, float] = Field(default_factory=dict)
    rule_violations: List[str] = Field(default_factory=list)
    step_reward_breakdown: Dict[str, float] = Field(default_factory=dict)


# ── Agent-facing prompt helpers ───────────────────────────────────────────


AGENT_ACTION_GUIDANCE: Dict[ActionType, str] = {
    ActionType.CONFIGURE_BEAM: (
        "Pick the LHC center-of-mass energy. Higher energy reaches heavier "
        "resonances but costs more per fb^-1. Required before collecting data."
    ),
    ActionType.ALLOCATE_LUMINOSITY: (
        "Schedule a chunk of integrated luminosity (fb^-1). More luminosity "
        "means more events but uses budget and time. Required before collecting."
    ),
    ActionType.SET_TRIGGER: (
        "Choose a hardware/HLT trigger. Match the trigger to the channel of "
        "interest; mismatched triggers throw away signal."
    ),
    ActionType.COLLECT_COLLISIONS: (
        "Run the experiment. Returns a noisy raw event count plus background "
        "estimate, conditioned on beam, luminosity, trigger, and channel."
    ),
    ActionType.CALIBRATE_DETECTOR: (
        "Apply ECAL/tracker calibration. Reduces systematic uncertainty; "
        "neglecting it inflates fit uncertainty later."
    ),
    ActionType.RECONSTRUCT_TRACKS: (
        "Reconstruct charged-particle tracks and physics objects. Required "
        "before any analysis-level step."
    ),
    ActionType.SELECT_CHANNEL: (
        "Pick the decay channel to study (γγ, ℓℓ, jj, 4ℓ, bb). Wrong channel "
        "= small signal acceptance regardless of luminosity."
    ),
    ActionType.BUILD_INVARIANT_MASS: (
        "Construct the invariant-mass histogram in the chosen channel and "
        "mass window."
    ),
    ActionType.SUBTRACT_BACKGROUND: (
        "Fit a smooth background model and subtract it to expose any peak."
    ),
    ActionType.FIT_RESONANCE: (
        "Fit a Breit-Wigner / Crystal Ball line shape. Returns mass, width, "
        "and statistical uncertainty."
    ),
    ActionType.SCAN_BUMP: (
        "Run a sliding-window bump hunt over the mass window. Reports the "
        "most-significant candidate region."
    ),
    ActionType.MEASURE_ANGULAR: (
        "Measure decay angular distribution to constrain spin/parity. "
        "Useful only after a peak is identified."
    ),
    ActionType.ESTIMATE_SIGNIFICANCE: (
        "Compute the statistical significance of a candidate signal in σ. "
        "Required before claiming a discovery."
    ),
    ActionType.REQUEST_SYSTEMATICS: (
        "Run a systematics study (JES, luminosity, calibration). Improves "
        "uncertainty estimates and reduces overconfidence penalty."
    ),
    ActionType.REQUEST_THEORY_REVIEW: (
        "Ask a theorist sub-agent to review the evidence; small extra signal "
        "but not a substitute for missing data."
    ),
    ActionType.SUBMIT_DISCOVERY_CLAIM: (
        "Submit a structured discovery claim. Graded on mass calibration, "
        "significance, channel, spin hypothesis, and overconfidence."
    ),
}


AGENT_ENVIRONMENT_RULES: List[str] = [
    "Each successful action returns summarized evidence; do not repeat steps.",
    "Hard prerequisites are enforced: data collection requires beam+luminosity+trigger; "
    "analysis requires reconstruction and a chosen channel.",
    "A discovery claim requires a fitted resonance and an estimated significance.",
    "Tools listed in available_tools are pre-filtered for this episode; prefer them.",
    "Submitting an overconfident wrong claim is heavily penalised.",
]


def build_agent_system_prompt() -> str:
    lines = [
        "You are an expert high-energy physicist running an analysis at the LHC.",
        "",
        "At each turn you observe the experiment state and pick one structured next step",
        "to maximise the probability of correctly characterising a hidden resonance.",
        "",
        "Environment rules:",
    ]
    lines.extend(f"  - {rule}" for rule in AGENT_ENVIRONMENT_RULES)
    lines.append("")
    lines.append("Action guidance:")
    lines.extend(
        f"  - {a.value}: {AGENT_ACTION_GUIDANCE[a]}" for a in ActionType
    )
    lines.extend([
        "",
        "Respond with ONLY a single valid JSON object, no extra prose:",
        '{"action_type": "...", "method": null, "parameters": {}, "justification": "...", "confidence": 0.8}',
        "",
        "For submit_discovery_claim, structure parameters['claim'] as:",
        '{"mass_estimate_gev": 125.0, "mass_uncertainty_gev": 0.5, "width_estimate_gev": 0.004,'
        ' "significance_sigma": 5.2, "decay_channel": "diphoton", "spin_hypothesis": 0,'
        ' "parity": "+", "cross_section_fb": 50.0, "confidence": 0.9}',
    ])
    return "\n".join(lines)


def build_agent_observation_context(

    obs: CollisionObservation,

    *,

    max_tools: int = 6,

    max_channels: int = 4,

) -> str:
    parts: List[str] = []

    parts.append(
        f"Mass search window: [{obs.task.mass_search_window_gev[0]:.0f}, "
        f"{obs.task.mass_search_window_gev[1]:.0f}] GeV; "
        f"difficulty={obs.task.difficulty}."
    )

    chans = list(dict.fromkeys(obs.available_channels or obs.task.available_channels))
    if chans:
        parts.append("Available channels: " + ", ".join(chans[:max_channels]))

    tools = list(dict.fromkeys(obs.available_tools or obs.task.available_tools))
    if tools:
        parts.append("Available tools: " + ", ".join(tools[:max_tools]))

    if obs.selected_channel:
        parts.append(f"Selected channel: {obs.selected_channel}")
    if obs.selected_beam_energy:
        parts.append(f"Beam energy: {obs.selected_beam_energy}")

    if obs.candidate_masses_gev:
        masses = [f"{m:.1f}" for m in obs.candidate_masses_gev[:3]]
        sigmas = [f"{s:.1f}" for s in obs.candidate_significances[:3]]
        parts.append(
            "Candidate peaks (GeV / σ): "
            + ", ".join(f"{m}/{s}" for m, s in zip(masses, sigmas))
        )

    return "\n".join(parts)


__all__ = [
    "ActionType",
    "DAQ_ACTIONS",
    "RECO_ACTIONS",
    "ANALYSIS_ACTIONS",
    "META_ACTIONS",
    "DetectorChannel",
    "TriggerType",
    "BeamEnergy",
    "ToolCategory",
    "ToolSpec",
    "TOOL_REGISTRY",
    "ExperimentAction",
    "OutputType",
    "IntermediateOutput",
    "ResourceUsage",
    "PipelineStepRecord",
    "PaperReference",
    "ExpectedFinding",
    "TaskSpec",
    "DiscoveryClaim",
    "CollisionObservation",
    "AGENT_ACTION_GUIDANCE",
    "AGENT_ENVIRONMENT_RULES",
    "build_agent_system_prompt",
    "build_agent_observation_context",
]