Skip to main content

codewhale_command_contract/facets/
debug_operations.rs

1//! Typed host boundaries replacing direct App access in the remaining debug
2//! commands. Read-only receipts/diff/change cannot acquire mutation authority.
3//! Snapshot planning, safe restoration and history updates remain host-owned;
4//! command parsing, fallback decisions and report rendering remain portable.
5
6use super::{Receipt, SessionSyncPayload};
7use std::path::PathBuf;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum DebugReceiptError {
11    ApprovalLog(String),
12    Build(String),
13}
14
15pub trait CommandDebugReceiptsContext {
16    fn receipt(&self, turn: Option<&str>) -> Result<Receipt, DebugReceiptError>;
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct DebugChangeProjection {
21    pub changelog: &'static str,
22    pub is_english: bool,
23    pub translation_available: bool,
24    pub translation_target: &'static str,
25}
26
27pub trait CommandDebugChangeContext {
28    fn change_projection(&self) -> DebugChangeProjection;
29}
30
31pub trait CommandDebugHistoryContext {
32    fn last_user_input(&self) -> Option<String>;
33    fn load_composer(&mut self, input: String);
34    /// Prepare a last-exchange rollback without mutating the live transcript.
35    /// The host applies it only after the Engine acknowledges the truncated
36    /// conversation; retry additionally waits for durable persistence (#6788).
37    fn undo_conversation(&mut self) -> DebugConversationUndo;
38}
39
40/// Prepared conversation-only undo: display cells to remove after Engine
41/// acknowledgement, and the conversation to install as its history.
42#[derive(Debug, Clone, PartialEq)]
43pub struct DebugConversationUndo {
44    pub removed: usize,
45    pub sync: SessionSyncPayload,
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub enum DebugDiffObservation {
50    GitUnavailable,
51    /// Nothing to compare against: the session has saved no restore point
52    /// for this workspace and the workspace is not a git repository.
53    NoBaseline,
54    Output {
55        names: String,
56        stat: String,
57        /// The unified patch behind `names` and `stat`, bounded by the host.
58        patch: String,
59        /// The host cut `patch` at its bound; `names` and `stat` are whole.
60        patch_truncated: bool,
61    },
62    Failed(String),
63}
64
65pub trait CommandDebugDiffContext {
66    fn diff(&self) -> DebugDiffObservation;
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub enum DebugRestoreAction {
71    Modified,
72    Recreated,
73    Removed,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
77pub struct DebugRestoredFile {
78    pub action: DebugRestoreAction,
79    pub path: PathBuf,
80}
81
82#[derive(Debug, Clone, PartialEq)]
83pub struct DebugUndoRestored {
84    /// First line of the undone request's prompt, when its restore point
85    /// recorded one.
86    pub request: Option<String>,
87    /// Whether the request and its reply were also removed from the
88    /// conversation. `false` when the conversation no longer held it.
89    pub conversation_removed: bool,
90    /// The request had no recorded end and `/undo force` ran it, so edits
91    /// made to these files since it started went back too.
92    pub forced: bool,
93    pub files: Vec<DebugRestoredFile>,
94    pub skipped: Vec<PathBuf>,
95    pub sync: SessionSyncPayload,
96}
97
98#[derive(Debug, Clone, PartialEq)]
99pub enum DebugUndoOutcome {
100    RepoUnavailable {
101        workspace: PathBuf,
102        error: String,
103    },
104    SnapshotPending,
105    NoSnapshots,
106    NoSession,
107    NoOwnedSteps,
108    NoDifference,
109    /// The last request has no recorded end, so undoing it would also put
110    /// back edits made since. Nothing was written; `force` runs it.
111    OpenEnded,
112    CompareFailed(String),
113    SnapshotFailed(String),
114    ChangedSince {
115        paths: Vec<String>,
116    },
117    ListFailed(String),
118    RestoreBlocked(String),
119    RestoreFailed(String),
120    Restored(DebugUndoRestored),
121}
122
123pub trait CommandDebugUndoContext {
124    /// Undoes the last request's file changes, and removes that request from
125    /// the conversation when it is still its last one. Preserves snapshot
126    /// ownership, regular-file checks, backup and preflight, and asks for no
127    /// trust: it only puts back, inside the workspace, what that request
128    /// changed. `force` accepts a request with no recorded end. Never falls
129    /// back to chat truncation: the portable handler decides that from the
130    /// typed outcome.
131    fn undo_files(&mut self, force: bool) -> DebugUndoOutcome;
132}