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 host must rewind the matching request and its reply.
88    pub rewind_conversation: bool,
89    /// The request had no recorded end and `/undo force` ran it, so edits
90    /// made to these files since it started went back too.
91    pub forced: bool,
92    pub files: Vec<DebugRestoredFile>,
93    pub skipped: Vec<PathBuf>,
94    pub sync: SessionSyncPayload,
95}
96
97#[derive(Debug, Clone, PartialEq)]
98pub enum DebugUndoOutcome {
99    RepoUnavailable {
100        workspace: PathBuf,
101        error: String,
102    },
103    SnapshotPending,
104    NoSnapshots,
105    NoSession,
106    NoOwnedSteps,
107    NoDifference,
108    /// The last request has no recorded end, so undoing it would also put
109    /// back edits made since. Nothing was written; `force` runs it.
110    OpenEnded,
111    CompareFailed(String),
112    SnapshotFailed(String),
113    ChangedSince {
114        paths: Vec<String>,
115    },
116    ListFailed(String),
117    RestoreBlocked(String),
118    RestoreFailed(String),
119    Restored(DebugUndoRestored),
120}
121
122pub trait CommandDebugUndoContext {
123    /// Undoes the last request's file changes, and removes that request from
124    /// the conversation when it is still its last one. Preserves snapshot
125    /// ownership, regular-file checks, backup and preflight, and asks for no
126    /// trust: it only puts back, inside the workspace, what that request
127    /// changed. `force` never waives owned identity or a complete write record. Never falls
128    /// back to chat truncation: the portable handler decides that from the
129    /// typed outcome.
130    fn undo_files(&mut self, force: bool) -> DebugUndoOutcome;
131}