sid-isnt-done 0.4.0

sid is a UNIX-inspired coding agent for Anthropic-compatible APIs
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! JSONL wire protocol for `sid --raw`.
//!
//! The raw protocol is line-oriented UTF-8 JSON exchanged over stdin/stdout.
//! Clients send semantic requests instead of shell-style commands, and the
//! server emits typed events, prompts, and terminal results.

use std::sync::Arc;
use std::sync::Mutex as StdMutex;
use std::sync::OnceLock;

use claudius::Effort;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Current `sid --raw` protocol version.
pub const RAW_PROTOCOL_VERSION: u32 = 1;

/// A client request envelope sent to `sid --raw`.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RawRequestEnvelope {
    /// Protocol version understood by the client.
    pub protocol_version: u32,
    /// Request identifier chosen by the client.
    pub request_id: String,
    /// Semantic request payload.
    #[serde(flatten)]
    pub request: RawRequest,
}

/// Semantic requests accepted by `sid --raw`.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum RawRequest {
    /// Send a normal user turn to the agent.
    UserTurn {
        /// User-visible message text.
        text: String,
    },
    /// Reply to an outstanding server prompt.
    PromptResponse {
        /// Prompt identifier emitted by the server.
        prompt_id: String,
        /// Response text chosen by the client.
        response: String,
    },
    /// Show the current agent.
    ShowAgent,
    /// List configured agents.
    ListAgents,
    /// Switch to a different agent.
    SwitchAgent {
        /// Agent identifier to activate.
        agent: String,
    },
    /// Compact the current session into a child session.
    Compact,
    /// Clear the conversation transcript.
    Clear,
    /// Change the active model.
    SetModel {
        /// Model name to activate.
        model: String,
    },
    /// Change or clear the system prompt.
    SetSystemPrompt {
        /// New prompt text, or `null` to clear it.
        prompt: Option<String>,
    },
    /// Change the maximum response token limit.
    SetMaxTokens {
        /// Per-response token budget.
        max_tokens: u32,
    },
    /// Change or clear temperature.
    SetTemperature {
        /// Sampling temperature, or `null` to clear it.
        temperature: Option<f32>,
    },
    /// Change or clear top-p.
    SetTopP {
        /// Top-p value, or `null` to clear it.
        top_p: Option<f32>,
    },
    /// Change or clear top-k.
    SetTopK {
        /// Top-k value, or `null` to clear it.
        top_k: Option<u32>,
    },
    /// Add a stop sequence.
    AddStopSequence {
        /// Sequence to add.
        sequence: String,
    },
    /// Clear all stop sequences.
    ClearStopSequences,
    /// Return the active stop sequences.
    ListStopSequences,
    /// Change or clear the explicit thinking budget.
    SetThinkingBudget {
        /// Token budget, or `null` to disable explicit thinking.
        tokens: Option<u32>,
    },
    /// Enable adaptive thinking using the current effort setting.
    SetThinkingAdaptive,
    /// Change or clear the adaptive effort level.
    SetEffort {
        /// Effort level, or `null` to clear it.
        effort: Option<Effort>,
    },
    /// Change or clear the session spend limit.
    SetSpend {
        /// Spend limit in dollars, or `null` to clear it.
        dollars: Option<f64>,
    },
    /// Toggle prompt caching.
    SetCaching {
        /// Desired caching state.
        enabled: bool,
    },
    /// Save the transcript to an arbitrary path.
    SaveTranscript {
        /// Destination path.
        path: String,
    },
    /// Load a transcript from an arbitrary path.
    LoadTranscript {
        /// Source path.
        path: String,
    },
    /// Return session statistics.
    Stats,
    /// Return the current effective configuration.
    ShowConfig,
    /// Shut down the raw server.
    Shutdown,
}

/// A server-to-client JSONL message emitted by `sid --raw`.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RawServerMessage {
    /// Initial capability and session announcement.
    Hello(RawHello),
    /// Incremental event associated with a request.
    Event(RawEventEnvelope),
    /// Interactive prompt that requires a `prompt_response`.
    Prompt(RawPrompt),
    /// Terminal request result.
    Result(RawResultEnvelope),
}

/// Initial session announcement for a raw server instance.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RawHello {
    /// Protocol version emitted by the server.
    pub protocol_version: u32,
    /// Active session identifier.
    pub session_id: String,
    /// Active session directory path.
    pub session_dir: String,
    /// Workspace root visible to the agent.
    pub workspace_root: String,
    /// Active agent identifier.
    pub current_agent: String,
    /// Active model name.
    pub model: String,
    /// Whether the session was resumed from disk.
    pub resumed: bool,
    /// Whether the startup agent requires manual confirmation.
    pub startup_confirmation_required: bool,
    /// Whether sandboxing support is available.
    pub sandbox_available: bool,
}

/// Incremental event associated with a single request.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RawEventEnvelope {
    /// Protocol version emitted by the server.
    pub protocol_version: u32,
    /// Request identifier chosen by the client.
    pub request_id: String,
    /// Event payload.
    #[serde(flatten)]
    pub event: RawEvent,
}

/// Streaming events emitted while a request is in progress.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum RawEvent {
    /// Agent stream started.
    AgentStart {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
    },
    /// Agent stream finished.
    AgentFinish {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Optional stop reason string.
        stop_reason: Option<String>,
    },
    /// Assistant text delta.
    AssistantTextDelta {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Text chunk.
        text: String,
    },
    /// Thinking text delta.
    ThinkingDelta {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Text chunk.
        text: String,
    },
    /// Informational text.
    Info {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Message text.
        message: String,
    },
    /// Error text.
    Error {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Message text.
        message: String,
    },
    /// Tool use block started.
    ToolUseStart {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Tool display name.
        name: String,
        /// API-level tool-use identifier.
        tool_use_id: String,
    },
    /// Tool input delta.
    ToolInputDelta {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Partial JSON chunk.
        partial_json: String,
    },
    /// Tool use block finished.
    ToolUseEnd {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
    },
    /// Tool result block started.
    ToolResultStart {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// API-level tool-use identifier.
        tool_use_id: String,
        /// Whether the result is an error.
        is_error: bool,
    },
    /// Tool result text delta.
    ToolResultTextDelta {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
        /// Text chunk.
        text: String,
    },
    /// Tool result block finished.
    ToolResultEnd {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
    },
    /// Response finished.
    ResponseFinish {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
    },
    /// Stream interrupted.
    Interrupted {
        /// Optional stream label.
        label: Option<String>,
        /// Stream nesting depth.
        depth: usize,
    },
    /// External tool stdout/stderr chunk.
    ToolOutput {
        /// Display name of the tool.
        tool_name: String,
        /// API-level tool-use identifier.
        tool_use_id: String,
        /// Output stream name such as `stdout` or `stderr`.
        stream: String,
        /// UTF-8 text chunk, when available.
        #[serde(skip_serializing_if = "Option::is_none")]
        text: Option<String>,
        /// Base64-encoded bytes for non-UTF-8 chunks.
        #[serde(skip_serializing_if = "Option::is_none")]
        data_b64: Option<String>,
    },
}

/// A server prompt waiting for a `prompt_response`.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RawPrompt {
    /// Protocol version emitted by the server.
    pub protocol_version: u32,
    /// Request identifier associated with the prompt.
    pub request_id: String,
    /// Stable prompt identifier used by the matching response.
    pub prompt_id: String,
    /// Prompt type.
    pub kind: String,
    /// Prompt text.
    pub message: String,
    /// Allowed textual choices.
    pub choices: Vec<String>,
}

/// Terminal response for a client request.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct RawResultEnvelope {
    /// Protocol version emitted by the server.
    pub protocol_version: u32,
    /// Request identifier chosen by the client.
    pub request_id: String,
    /// `true` for success, `false` for error.
    pub ok: bool,
    /// Optional structured success payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
    /// Optional structured error payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<RawServerError>,
}

/// Structured error payload for a failed raw request.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RawServerError {
    /// Optional machine-readable error code.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
    /// Human-readable error message.
    pub message: String,
}

/// Structured external-tool output emitted outside the model event stream.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ToolOutputEvent {
    /// Request identifier currently associated with the tool run.
    pub request_id: String,
    /// Display name of the tool.
    pub tool_name: String,
    /// API-level tool-use identifier.
    pub tool_use_id: String,
    /// Output stream name such as `stdout` or `stderr`.
    pub stream: String,
    /// UTF-8 text chunk, when available.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    /// Base64-encoded bytes for non-UTF-8 chunks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_b64: Option<String>,
}

/// Observer used by the raw runtime to receive external tool stdout/stderr.
pub trait ToolOutputObserver: Send + Sync {
    /// Called for each captured tool-output chunk.
    fn on_tool_output(&self, event: &ToolOutputEvent);
}

/// RAII handle that restores the previously installed observer on drop.
pub struct ToolOutputObserverRegistration {
    previous: Option<Arc<dyn ToolOutputObserver>>,
}

impl Drop for ToolOutputObserverRegistration {
    fn drop(&mut self) {
        set_active_tool_output_observer(self.previous.take());
    }
}

/// Install a process-local tool-output observer for the current request scope.
pub fn install_tool_output_observer(
    observer: Option<Arc<dyn ToolOutputObserver>>,
) -> ToolOutputObserverRegistration {
    let previous = set_active_tool_output_observer(observer);
    ToolOutputObserverRegistration { previous }
}

pub(crate) fn notify_tool_output_observer(event: &ToolOutputEvent) {
    let observer = active_tool_output_observer()
        .lock()
        .expect("tool output observer lock poisoned")
        .clone();
    if let Some(observer) = observer {
        observer.on_tool_output(event);
    }
}

pub(crate) fn has_active_tool_output_observer() -> bool {
    active_tool_output_observer()
        .lock()
        .expect("tool output observer lock poisoned")
        .is_some()
}

fn active_tool_output_observer() -> &'static StdMutex<Option<Arc<dyn ToolOutputObserver>>> {
    static ACTIVE: OnceLock<StdMutex<Option<Arc<dyn ToolOutputObserver>>>> = OnceLock::new();
    ACTIVE.get_or_init(|| StdMutex::new(None))
}

fn set_active_tool_output_observer(
    observer: Option<Arc<dyn ToolOutputObserver>>,
) -> Option<Arc<dyn ToolOutputObserver>> {
    let mut slot = active_tool_output_observer()
        .lock()
        .expect("tool output observer lock poisoned");
    std::mem::replace(&mut *slot, observer)
}