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
//! Agent Events
//!
//! Event types for communication between the SRBN Orchestrator and TUI.
//! Enables async, decoupled control flow for interactive agent sessions.
use serde::{Deserialize, Serialize};
/// PSP-5 Phase 4: Per-plugin readiness summary for session-start reporting
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginReadiness {
/// Plugin name (e.g. "rust", "python")
pub plugin_name: String,
/// Verifier stages that have at least one available tool
pub available_stages: Vec<String>,
/// Verifier stages where only a fallback or no tool is available
pub degraded_stages: Vec<String>,
/// LSP status description
pub lsp_status: String,
}
/// Events emitted by the Orchestrator for TUI consumption
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AgentEvent {
/// Task status changed
TaskStatusChanged { node_id: String, status: NodeStatus },
/// Plan generated by Architect
PlanGenerated(crate::types::TaskPlan),
/// Lyapunov energy updated
EnergyUpdated { node_id: String, energy: f32 },
/// Log message for display
Log(String),
/// Node completed successfully
NodeCompleted { node_id: String, goal: String },
/// Approval required before proceeding
ApprovalRequest {
request_id: String,
node_id: String,
action_type: ActionType,
description: String,
diff: Option<String>,
},
/// Orchestration finished
Complete { success: bool, message: String },
/// Error occurred
Error(String),
// =========================================================================
// PSP-5: Lifecycle Events
// =========================================================================
/// PSP-5: Plan ready after sheafification with detected plugins and execution mode
PlanReady {
nodes: usize,
plugins: Vec<String>,
execution_mode: String,
},
/// PSP-5: Node selected for execution
NodeSelected {
node_id: String,
goal: String,
node_class: String,
},
/// PSP-5: Deterministic fallback planner activated
FallbackPlanner { reason: String },
/// PSP-5: Verification completed for a node
VerificationComplete {
node_id: String,
syntax_ok: bool,
build_ok: bool,
tests_ok: bool,
lint_ok: bool,
diagnostics_count: usize,
tests_passed: usize,
tests_failed: usize,
energy: f32,
/// PSP-5 Phase 7: Full energy component breakdown
energy_components: crate::types::EnergyComponents,
/// PSP-5 Phase 7: Per-stage verification outcomes with sensor status
stage_outcomes: Vec<crate::types::StageOutcome>,
/// PSP-5 Phase 7: Whether verification ran in degraded mode
degraded: bool,
/// PSP-5 Phase 7: Human-readable reasons for each degraded stage
degraded_reasons: Vec<String>,
/// PSP-5 Phase 7: Summary suitable for display
summary: String,
/// PSP-5 Phase 7: Node class for display context
node_class: String,
},
/// PSP-5: Artifact bundle applied to workspace
BundleApplied {
node_id: String,
files_created: Vec<String>,
files_modified: Vec<String>,
/// PSP-5 Phase 7: Number of write (new file) operations
writes_count: usize,
/// PSP-5 Phase 7: Number of diff (patch) operations
diffs_count: usize,
/// PSP-5 Phase 7: Node class for display context
node_class: String,
},
/// PSP-5 Phase 4: A sensor fell back to an alternative tool
SensorFallback {
node_id: String,
stage: String,
primary: String,
actual: String,
reason: String,
},
/// PSP-5 Phase 4: Verification completed with degraded stages
DegradedVerification {
node_id: String,
degraded_stages: Vec<String>,
stability_blocked: bool,
},
/// PSP-5 Phase 5: Non-convergence classified with a repair action
EscalationClassified {
node_id: String,
category: String,
action: String,
},
/// PSP-5 Phase 5: Sheaf validation completed for a node
SheafValidationComplete {
node_id: String,
validators_run: usize,
failures: usize,
v_sheaf: f32,
},
/// PSP-5 Phase 5: Graph rewrite applied (split, interface insertion, replan)
GraphRewriteApplied {
trigger_node: String,
action: String,
nodes_affected: usize,
},
/// PSP-5 Phase 6: Provisional branch created for speculative child work
BranchCreated {
branch_id: String,
node_id: String,
parent_node_id: String,
},
/// PSP-5 Phase 6: Interface sealed for a node (dependents may proceed)
InterfaceSealed {
node_id: String,
sealed_paths: Vec<String>,
seal_hash: String,
},
/// PSP-5 Phase 6: Provisional branches flushed due to parent failure
BranchFlushed {
parent_node_id: String,
flushed_branch_ids: Vec<String>,
reason: String,
},
/// PSP-5 Phase 6: Blocked dependent unblocked after parent seal
DependentUnblocked {
child_node_id: String,
parent_node_id: String,
},
/// PSP-5 Phase 6: Provisional branch merged into committed state
BranchMerged { branch_id: String, node_id: String },
/// PSP-5 Phase 3: Context assembly degraded (budget exceeded or missing artifacts)
ContextDegraded {
node_id: String,
budget_exceeded: bool,
missing_owned_files: Vec<String>,
included_file_count: usize,
total_bytes: usize,
reason: String,
},
/// PSP-5 Phase 3: Context blocked — required structural context is untrustworthy.
/// The node SHALL NOT proceed silently (PSP-5 §3 requirement).
ContextBlocked {
node_id: String,
missing_owned_files: Vec<String>,
reason: String,
},
/// PSP-5 Phase 3: Structural dependency pre-check failed — a required
/// dependency only has prose summaries, no machine-verifiable digests.
StructuralDependencyMissing {
node_id: String,
dependency_node_id: String,
reason: String,
},
/// PSP-5 Phase 1/4: Model fallback triggered for a tier after structured-output failure
ModelFallback {
node_id: String,
tier: String,
primary_model: String,
fallback_model: String,
reason: String,
},
/// PSP-5 Phase 3: Context provenance drift detected on resume
ProvenanceDrift {
node_id: String,
missing_files: Vec<String>,
reason: String,
},
/// PSP-5 Phase 4: Tool readiness snapshot captured at session start
ToolReadiness {
/// Per-plugin readiness: (plugin_name, available_stages, degraded_stages, lsp_status)
plugins: Vec<PluginReadiness>,
/// Verifier strictness mode in effect
strictness: String,
},
/// PSP-5 Phase 8: Budget envelope updated (step/cost consumed)
BudgetUpdated {
steps_used: u32,
max_steps: Option<u32>,
cost_used_usd: f64,
max_cost_usd: Option<f64>,
revisions_used: u32,
max_revisions: Option<u32>,
},
/// PSP-5 Phase 8: Plan revision superseded by a new revision
PlanRevised {
revision_id: String,
sequence: u32,
reason: String,
node_count: usize,
},
/// PSP-5 Phase 10: File deleted via safe delete operation
FileDeleted { node_id: String, path: String },
/// PSP-5 Phase 10: File moved/renamed via safe move operation
FileMoved {
node_id: String,
from: String,
to: String,
},
}
/// Node status for TUI display (mirrors NodeState but simplified)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NodeStatus {
/// Queued, waiting for dependencies
Queued,
/// Planning phase (Architect)
Planning,
Pending,
/// Active coding / implementation phase
Coding,
Running,
Verifying,
/// PSP-5: Node is retrying after a failed verification
Retrying,
/// PSP-5 Phase 7: Sheaf consistency check underway
SheafCheck,
/// PSP-5 Phase 7: Committing stable state to ledger
Committing,
Completed,
Failed,
Escalated,
Aborted,
}
/// Type of action requiring approval
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ActionType {
/// File creation or modification
FileWrite { path: String },
/// Shell command execution
Command { command: String },
/// Multiple files in a sub-graph
SubGraph { node_count: usize },
/// Project initialization (with editable name)
ProjectInit {
command: String,
suggested_name: String,
},
/// PSP-5: Multi-file artifact bundle write
BundleWrite {
/// Node that produced the bundle
node_id: String,
/// Files being written or modified
files: Vec<String>,
},
}
/// Actions sent from TUI to Orchestrator
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AgentAction {
/// Approve a pending request
Approve { request_id: String },
/// Approve with an edited value (e.g., project name)
ApproveWithEdit {
request_id: String,
edited_value: String,
},
/// Reject a pending request
Reject {
request_id: String,
reason: Option<String>,
},
/// Pause orchestration
Pause,
/// Resume orchestration
Resume,
/// Abort the entire session
Abort,
/// PSP-5 Phase 7: Request correction with structured user feedback
RequestCorrection {
request_id: String,
feedback: String,
},
}
/// Channel types for agent communication
pub mod channel {
use super::{AgentAction, AgentEvent};
use tokio::sync::mpsc;
/// Sender for AgentEvents (Orchestrator → TUI)
pub type EventSender = mpsc::UnboundedSender<AgentEvent>;
/// Receiver for AgentEvents (Orchestrator → TUI)
pub type EventReceiver = mpsc::UnboundedReceiver<AgentEvent>;
/// Sender for AgentActions (TUI → Orchestrator)
pub type ActionSender = mpsc::UnboundedSender<AgentAction>;
/// Receiver for AgentActions (TUI → Orchestrator)
pub type ActionReceiver = mpsc::UnboundedReceiver<AgentAction>;
/// Create event channel (Orchestrator → TUI)
pub fn event_channel() -> (EventSender, EventReceiver) {
mpsc::unbounded_channel()
}
/// Create action channel (TUI → Orchestrator)
pub fn action_channel() -> (ActionSender, ActionReceiver) {
mpsc::unbounded_channel()
}
}