Skip to main content

ralph_workflow/reducer/event/
prompt_input.rs

1//! Prompt input oversize detection and materialization events.
2//!
3//! These events make reducer-visible any transformation that affects the
4//! agent-visible prompt content (inline vs file reference, truncation, etc.).
5
6use crate::reducer::state::{MaterializedPromptInput, PromptInputKind};
7use serde::{Deserialize, Serialize};
8
9// Import from the parent event module, which defines PipelinePhase in mod.rs
10// and ErrorEvent in error.rs
11use crate::reducer::event::{ErrorEvent, PipelinePhase};
12
13/// Prompt input oversize detection and materialization events.
14///
15/// These events make reducer-visible any transformation that affects the
16/// agent-visible prompt content (inline vs file reference, truncation, etc.).
17///
18/// # Purpose
19///
20/// Large prompt inputs (PROMPT.md, PLAN.md, diffs) may exceed model context limits.
21/// When this occurs, handlers materialize the content as file references instead of
22/// inline text. These events record the materialization strategy for observability
23/// and to enable the reducer to track content transformations.
24///
25/// # Emitted By
26///
27/// - Prompt preparation handlers in `handler/*/prepare_prompt.rs`
28/// - XSD retry handlers
29#[derive(Clone, Serialize, Deserialize, Debug)]
30pub enum PromptInputEvent {
31    /// Oversize content detected, will be materialized as file reference.
32    OversizeDetected {
33        /// Pipeline phase where oversize was detected.
34        phase: PipelinePhase,
35        /// Type of content (prompt, plan, diff, etc.).
36        kind: PromptInputKind,
37        /// SHA256 hex digest of the content.
38        content_id_sha256: String,
39        /// Actual content size in bytes.
40        size_bytes: u64,
41        /// Configured size limit in bytes.
42        limit_bytes: u64,
43        /// Materialization policy applied.
44        policy: String,
45    },
46    /// Planning prompt inputs materialized.
47    PlanningInputsMaterialized {
48        /// Iteration number.
49        iteration: u32,
50        /// Materialized prompt input.
51        prompt: MaterializedPromptInput,
52    },
53    /// Development prompt inputs materialized.
54    DevelopmentInputsMaterialized {
55        /// Iteration number.
56        iteration: u32,
57        /// Materialized prompt input.
58        prompt: MaterializedPromptInput,
59        /// Materialized plan input.
60        plan: MaterializedPromptInput,
61    },
62    /// Review prompt inputs materialized.
63    ReviewInputsMaterialized {
64        /// Review pass number.
65        pass: u32,
66        /// Materialized plan input.
67        plan: MaterializedPromptInput,
68        /// Materialized diff input.
69        diff: MaterializedPromptInput,
70    },
71    /// Commit prompt inputs materialized.
72    CommitInputsMaterialized {
73        /// Commit attempt number.
74        attempt: u32,
75        /// Materialized diff input.
76        diff: MaterializedPromptInput,
77    },
78    /// XSD retry last output materialized.
79    XsdRetryLastOutputMaterialized {
80        /// Phase that produced the invalid output being retried.
81        phase: PipelinePhase,
82        /// Scope id within the phase (iteration/pass/attempt).
83        scope_id: u32,
84        /// Materialized representation of the last invalid output.
85        last_output: MaterializedPromptInput,
86    },
87    /// A typed error event returned by an effect handler.
88    ///
89    /// Effect handlers surface failures by returning `Err(ErrorEvent::... .into())`.
90    /// The event loop extracts the underlying `ErrorEvent` and re-emits it through
91    /// this existing category so the reducer can decide recovery strategy without
92    /// adding new top-level `PipelineEvent` variants.
93    HandlerError {
94        /// Phase during which the error occurred (best-effort; derived from current state).
95        phase: PipelinePhase,
96        /// The typed error event.
97        error: ErrorEvent,
98    },
99
100    /// PROMPT.md permissions locked (read-only) at pipeline startup.
101    ///
102    /// Emitted by LockPromptPermissions effect handler when attempting to
103    /// set PROMPT.md to read-only. If the operation fails, a warning is included
104    /// but the pipeline continues (best-effort protection).
105    PromptPermissionsLocked {
106        /// Warning if permission change failed (None if successful or file missing).
107        warning: Option<String>,
108    },
109    /// PROMPT.md permissions restore warning (best-effort).
110    ///
111    /// Emitted by RestorePromptPermissions effect handler when attempting to
112    /// set PROMPT.md back to writable. The pipeline continues, but the warning
113    /// is recorded for observability and resume diagnostics.
114    PromptPermissionsRestoreWarning {
115        /// Warning message when restore fails.
116        warning: String,
117    },
118
119    /// Template was rendered, carrying substitution log.
120    ///
121    /// Emitted by prompt preparation handlers after template rendering.
122    /// The substitution log enables validation based on tracked substitutions
123    /// rather than regex scanning the rendered output.
124    TemplateRendered {
125        /// Pipeline phase during which the template was rendered.
126        phase: PipelinePhase,
127        /// Template name (e.g., "commit_message_xml").
128        template_name: String,
129        /// Detailed substitution log from rendering.
130        log: crate::prompts::SubstitutionLog,
131    },
132}