返回 CodeWhale
session_structcopy.rs
根目录 / crates / command-contract / src / facets / session_structcopy.rs
1 //! Read-only observations for one human-selected structural copy.
2 //! Replaces the command's concrete session/model/plan observations. The host
3 //! excludes private instruction/reasoning and inline image bodies before this
4 //! boundary; portable rendering still sanitizes visible content. These are not
5 //! complete session objects, executable callbacks, or pre-rendered envelopes.
6
7 use serde::{Deserialize, Serialize};
8 use serde_json::Value;
9
10 #[derive(Debug, Clone, PartialEq, Eq)]
11 pub enum StructcopyError {
12 Unavailable,
13 Busy,
14 /// Raw preparation error text already exposed by the original command.
15 Preparation(String),
16 }
17
18 #[derive(Debug, Clone, PartialEq)]
19 pub struct StructcopyTranscript {
20 pub index: usize,
21 pub role: String,
22 pub content: StructcopyContent,
23 }
24
25 #[derive(Debug, Clone, PartialEq)]
26 pub enum StructcopyContent {
27 InternalContext,
28 Visible(Vec<StructcopyBlock>),
29 }
30
31 #[derive(Debug, Clone, PartialEq)]
32 pub enum StructcopyBlock {
33 Text(String),
34 ThinkingOmitted,
35 ToolUse {
36 id: String,
37 name: String,
38 input: Value,
39 caller_type: Option<String>,
40 },
41 ToolResult {
42 tool_use_id: String,
43 result: StructcopyToolResult,
44 },
45 /// Only HTTP(S) URLs; the host must omit local and inline image payloads.
46 ImageUrl(String),
47 ImageOmitted,
48 ServerToolUse {
49 id: String,
50 name: String,
51 input: Value,
52 },
53 ToolSearchToolResult {
54 tool_use_id: String,
55 content: Value,
56 },
57 CodeExecutionToolResult {
58 tool_use_id: String,
59 content: Value,
60 },
61 }
62
63 #[derive(Debug, Clone, PartialEq)]
64 pub struct StructcopyToolPair {
65 pub name: String,
66 pub input: Value,
67 /// No result is distinct from a result with unknown error status.
68 pub result: Option<StructcopyToolResult>,
69 }
70
71 #[derive(Debug, Clone, PartialEq)]
72 pub struct StructcopyToolResult {
73 pub content: String,
74 pub is_error: Option<bool>,
75 /// The host's existing safe structured-result filter has already run.
76 pub content_blocks: Option<Vec<Value>>,
77 }
78
79 #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
80 #[serde(deny_unknown_fields)]
81 pub struct StructcopyPlan {
82 #[serde(default, skip_serializing_if = "Option::is_none")]
83 pub title: Option<String>,
84 #[serde(default, skip_serializing_if = "Option::is_none")]
85 pub objective: Option<String>,
86 #[serde(default, skip_serializing_if = "Option::is_none")]
87 pub context_summary: Option<String>,
88 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub explanation: Option<String>,
90 #[serde(default, skip_serializing_if = "Vec::is_empty")]
91 pub sources_used: Vec<String>,
92 #[serde(default, skip_serializing_if = "Vec::is_empty")]
93 pub critical_files: Vec<String>,
94 #[serde(default, skip_serializing_if = "Vec::is_empty")]
95 pub constraints: Vec<String>,
96 #[serde(default, skip_serializing_if = "Option::is_none")]
97 pub recommended_approach: Option<String>,
98 #[serde(default, skip_serializing_if = "Option::is_none")]
99 pub verification_plan: Option<String>,
100 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub risks_and_unknowns: Option<String>,
102 #[serde(default, skip_serializing_if = "Option::is_none")]
103 pub handoff_packet: Option<String>,
104 #[serde(default, skip_serializing_if = "Vec::is_empty")]
105 pub replan_if: Vec<String>,
106 #[serde(default, skip_serializing_if = "Vec::is_empty")]
107 pub items: Vec<StructcopyPlanItem>,
108 }
109
110 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
111 pub struct StructcopyPlanItem {
112 pub step: String,
113 pub status: StructcopyPlanStatus,
114 }
115
116 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
117 #[serde(rename_all = "snake_case")]
118 pub enum StructcopyPlanStatus {
119 Pending,
120 InProgress,
121 Completed,
122 }
123
124 /// Typed form of the existing bounded workflow summary projection. It carries
125 /// no raw journal events, owner IDs, paths, or executable runtime state.
126 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
127 #[serde(deny_unknown_fields)]
128 pub struct StructcopyWorkflow {
129 pub run_id: String,
130 pub status: StructcopyWorkflowStatus,
131 pub lifecycle_seq: u64,
132 pub started_at_ms: u64,
133 pub completed_at_ms: Option<u64>,
134 pub source_file: Option<String>,
135 pub workflow_id: Option<String>,
136 pub workflow_goal: Option<String>,
137 pub child_count: usize,
138 pub schema_error_count: usize,
139 pub schema_repair_count: u64,
140 pub dispatch_failure_count: u64,
141 pub progress_count: u64,
142 pub last_progress: Option<String>,
143 pub event_count: usize,
144 pub last_event_type: Option<String>,
145 /// Unknown execution counts serialize as null, never measured zero.
146 pub leaf_count: Option<usize>,
147 pub branch_count: Option<usize>,
148 pub control_count: Option<usize>,
149 pub execution_status: Option<StructcopyExecutionStatus>,
150 pub gate_count: usize,
151 pub blocked_gate_count: usize,
152 pub gate_status: Vec<StructcopyGate>,
153 pub error: Option<String>,
154 pub usage: Option<StructcopyUsage>,
155 pub events_dropped: u64,
156 }
157
158 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
159 #[serde(rename_all = "snake_case")]
160 pub enum StructcopyWorkflowStatus {
161 Running,
162 Completed,
163 Degraded,
164 Failed,
165 Cancelled,
166 }
167
168 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
169 #[serde(rename_all = "snake_case")]
170 pub enum StructcopyExecutionStatus {
171 Pending,
172 Running,
173 Succeeded,
174 Failed,
175 Cancelled,
176 BudgetExceeded,
177 }
178
179 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
180 pub struct StructcopyGate {
181 pub gate_id: String,
182 pub state: String,
183 #[serde(default, skip_serializing_if = "Option::is_none")]
184 pub blocked_reason: Option<String>,
185 }
186
187 #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
188 pub struct StructcopyUsage {
189 #[serde(default, skip_serializing_if = "Option::is_none")]
190 pub input_tokens: Option<u64>,
191 #[serde(default, skip_serializing_if = "Option::is_none")]
192 pub output_tokens: Option<u64>,
193 #[serde(default, skip_serializing_if = "Option::is_none")]
194 pub total_tokens: Option<u64>,
195 #[serde(default, skip_serializing_if = "Option::is_none")]
196 pub cost_microusd: Option<u64>,
197 #[serde(default, skip_serializing_if = "Option::is_none")]
198 pub tool_calls: Option<u64>,
199 #[serde(default)]
200 pub tasks_reported: u64,
201 }
202
203 /// Host observations only. Root eligibility, sorting and labels are command
204 /// policy, and remain portable. Canonicalization failure is ordinary absence.
205 #[derive(Debug, Clone, Default, PartialEq, Eq)]
206 pub struct StructcopyPathRoots {
207 pub workspace: String,
208 pub canonical_workspace: Option<String>,
209 pub home: Option<String>,
210 }
211
212 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
213 pub enum StructcopyTransport {
214 Native,
215 /// Terminal-client output is queued, never delivery confirmation.
216 TerminalQueued,
217 }
218
219 /// Exact authority for `/structcopy`, independent of session export/recovery.
220 /// Observations do not mutate the session. Clipboard is the sole side effect;
221 /// the caller chooses it only after successful rendering and never for stdout.
222 pub trait CommandSessionStructcopyContext {
223 fn transcript_item(&self, index: usize) -> Result<StructcopyTranscript, StructcopyError>;
224 fn tool_pair(&self, call_id: &str) -> Result<StructcopyToolPair, StructcopyError>;
225 fn plan_snapshot(&self) -> Result<StructcopyPlan, StructcopyError>;
226 fn workflow_projection(&self, run_id: &str) -> Result<StructcopyWorkflow, StructcopyError>;
227 fn path_roots(&self) -> StructcopyPathRoots;
228 fn write_clipboard(&self, text: &str) -> Result<StructcopyTransport, String>;
229 }
230
230 lines RUST