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
446
447
448
449
450
451
452
453
//! Raw-mode transport utilities for `sid --raw`.
//!
//! This module owns the JSONL stdin/stdout transport, prompt handling, and
//! external-tool output forwarding used by raw-mode frontends.

use std::io::{self, BufRead, BufReader, Write};
use std::sync::Arc;
use std::sync::Mutex as StdMutex;

use claudius::{OperatorLine, Renderer, StopReason, StreamContext};

use crate::raw_protocol::{
    RAW_PROTOCOL_VERSION, RawEvent, RawEventEnvelope, RawPrompt, RawRequest, RawRequestEnvelope,
    RawResultEnvelope, RawServerError, RawServerMessage, ToolOutputEvent, ToolOutputObserver,
};

/// Raw-protocol decode error that preserves any discovered request id.
#[derive(Debug)]
pub struct ParsedRequestError {
    /// Request id extracted from the partially decoded envelope, if any.
    pub request_id: String,
    /// Optional machine-readable error code.
    pub code: Option<String>,
    /// Human-readable error message.
    pub message: String,
}

/// Shared JSONL output sink used by the raw server and tool-output observer.
pub struct SharedOutput<W>
where
    W: Write + Send + 'static,
{
    writer: Arc<StdMutex<W>>,
}

impl<W> Clone for SharedOutput<W>
where
    W: Write + Send + 'static,
{
    fn clone(&self) -> Self {
        Self {
            writer: self.writer.clone(),
        }
    }
}

impl<W> SharedOutput<W>
where
    W: Write + Send + 'static,
{
    /// Create a new shared output sink.
    pub fn new(writer: W) -> Self {
        Self {
            writer: Arc::new(StdMutex::new(writer)),
        }
    }

    /// Write one JSONL server message.
    pub fn write_message(&self, message: &RawServerMessage) -> io::Result<()> {
        let payload = serde_json::to_vec(message)
            .map_err(|err| io::Error::other(format!("failed to serialize raw message: {err}")))?;
        let mut writer = self
            .writer
            .lock()
            .map_err(|_| io::Error::other("raw output lock poisoned"))?;
        writer.write_all(&payload)?;
        writer.write_all(b"\n")?;
        writer.flush()
    }

    /// Execute `f` with a mutable borrow of the underlying writer.
    #[cfg(test)]
    pub(crate) fn with_writer<T>(&self, f: impl FnOnce(&W) -> T) -> T {
        let writer = self.writer.lock().expect("raw output lock poisoned");
        f(&writer)
    }
}

/// Line-oriented raw JSONL server that also implements `claudius::Renderer`.
pub struct RawServer<R, W>
where
    R: BufRead + Send,
    W: Write + Send + 'static,
{
    input: R,
    output: SharedOutput<W>,
    active_request_id: Option<String>,
    next_prompt_id: u64,
}

impl RawServer<BufReader<io::Stdin>, io::Stdout> {
    /// Create a raw server bound to process stdin/stdout.
    pub fn stdio() -> Self {
        Self::new(BufReader::new(io::stdin()), io::stdout())
    }
}

impl<R, W> RawServer<R, W>
where
    R: BufRead + Send,
    W: Write + Send + 'static,
{
    /// Create a raw server from arbitrary reader/writer handles.
    pub fn new(input: R, output: W) -> Self {
        Self {
            input,
            output: SharedOutput::new(output),
            active_request_id: None,
            next_prompt_id: 1,
        }
    }

    /// Clone the shared output sink.
    pub fn output(&self) -> SharedOutput<W> {
        self.output.clone()
    }

    /// Set the request id associated with future streamed events.
    pub fn set_request_id(&mut self, request_id: String) {
        self.active_request_id = Some(request_id);
    }

    /// Clear the request id associated with future streamed events.
    pub fn clear_request_id(&mut self) {
        self.active_request_id = None;
    }

    /// Write one JSONL server message.
    pub fn write_message(&self, message: &RawServerMessage) -> io::Result<()> {
        self.output.write_message(message)
    }

    /// Write a successful terminal result.
    pub fn write_ok_result(
        &self,
        request_id: &str,
        data: Option<serde_json::Value>,
    ) -> io::Result<()> {
        self.write_message(&RawServerMessage::Result(RawResultEnvelope {
            protocol_version: RAW_PROTOCOL_VERSION,
            request_id: request_id.to_string(),
            ok: true,
            data,
            error: None,
        }))
    }

    /// Write a failed terminal result.
    pub fn write_error_result(
        &self,
        request_id: &str,
        code: Option<&str>,
        message: &str,
    ) -> io::Result<()> {
        self.write_message(&RawServerMessage::Result(RawResultEnvelope {
            protocol_version: RAW_PROTOCOL_VERSION,
            request_id: request_id.to_string(),
            ok: false,
            data: None,
            error: Some(RawServerError {
                code: code.map(str::to_string),
                message: message.to_string(),
            }),
        }))
    }

    /// Read the next valid request, auto-reporting malformed request lines.
    pub fn read_request(&mut self) -> io::Result<Option<RawRequestEnvelope>> {
        loop {
            let mut line = String::new();
            let read = self.input.read_line(&mut line)?;
            if read == 0 {
                return Ok(None);
            }
            if line.trim().is_empty() {
                continue;
            }
            match parse_request_line(&line) {
                Ok(request) => return Ok(Some(request)),
                Err(err) => {
                    self.write_error_result(&err.request_id, err.code.as_deref(), &err.message)?;
                }
            }
        }
    }

    fn emit_event(&self, event: RawEvent) -> io::Result<()> {
        let request_id = self.active_request_id.as_ref().cloned().unwrap_or_default();
        self.write_message(&RawServerMessage::Event(RawEventEnvelope {
            protocol_version: RAW_PROTOCOL_VERSION,
            request_id,
            event,
        }))
    }
}

impl<R, W> Renderer for RawServer<R, W>
where
    R: BufRead + Send,
    W: Write + Send + 'static,
{
    fn start_agent(&mut self, context: &dyn StreamContext) {
        let _ = self.emit_event(RawEvent::AgentStart {
            label: context.label().map(str::to_string),
            depth: context.depth(),
        });
    }

    fn finish_agent(&mut self, context: &dyn StreamContext, stop_reason: Option<&StopReason>) {
        let _ = self.emit_event(RawEvent::AgentFinish {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            stop_reason: stop_reason.map(|reason| format!("{reason:?}")),
        });
    }

    fn print_text(&mut self, context: &dyn StreamContext, text: &str) {
        let _ = self.emit_event(RawEvent::AssistantTextDelta {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            text: text.to_string(),
        });
    }

    fn print_thinking(&mut self, context: &dyn StreamContext, text: &str) {
        let _ = self.emit_event(RawEvent::ThinkingDelta {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            text: text.to_string(),
        });
    }

    fn print_error(&mut self, context: &dyn StreamContext, error: &str) {
        let _ = self.emit_event(RawEvent::Error {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            message: error.to_string(),
        });
    }

    fn print_info(&mut self, context: &dyn StreamContext, info: &str) {
        let _ = self.emit_event(RawEvent::Info {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            message: info.to_string(),
        });
    }

    fn start_tool_use(&mut self, context: &dyn StreamContext, name: &str, id: &str) {
        let _ = self.emit_event(RawEvent::ToolUseStart {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            name: name.to_string(),
            tool_use_id: id.to_string(),
        });
    }

    fn print_tool_input(&mut self, context: &dyn StreamContext, partial_json: &str) {
        let _ = self.emit_event(RawEvent::ToolInputDelta {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            partial_json: partial_json.to_string(),
        });
    }

    fn finish_tool_use(&mut self, context: &dyn StreamContext) {
        let _ = self.emit_event(RawEvent::ToolUseEnd {
            label: context.label().map(str::to_string),
            depth: context.depth(),
        });
    }

    fn start_tool_result(
        &mut self,
        context: &dyn StreamContext,
        tool_use_id: &str,
        is_error: bool,
    ) {
        let _ = self.emit_event(RawEvent::ToolResultStart {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            tool_use_id: tool_use_id.to_string(),
            is_error,
        });
    }

    fn print_tool_result_text(&mut self, context: &dyn StreamContext, text: &str) {
        let _ = self.emit_event(RawEvent::ToolResultTextDelta {
            label: context.label().map(str::to_string),
            depth: context.depth(),
            text: text.to_string(),
        });
    }

    fn finish_tool_result(&mut self, context: &dyn StreamContext) {
        let _ = self.emit_event(RawEvent::ToolResultEnd {
            label: context.label().map(str::to_string),
            depth: context.depth(),
        });
    }

    fn finish_response(&mut self, context: &dyn StreamContext) {
        let _ = self.emit_event(RawEvent::ResponseFinish {
            label: context.label().map(str::to_string),
            depth: context.depth(),
        });
    }

    fn print_interrupted(&mut self, context: &dyn StreamContext) {
        let _ = self.emit_event(RawEvent::Interrupted {
            label: context.label().map(str::to_string),
            depth: context.depth(),
        });
    }

    fn read_operator_line(&mut self, prompt: &str) -> io::Result<Option<OperatorLine>> {
        let request_id = self
            .active_request_id
            .as_ref()
            .cloned()
            .unwrap_or_else(|| "prompt".to_string());
        let prompt_id = format!("prompt-{}", self.next_prompt_id);
        self.next_prompt_id = self.next_prompt_id.saturating_add(1);
        self.write_message(&RawServerMessage::Prompt(RawPrompt {
            protocol_version: RAW_PROTOCOL_VERSION,
            request_id: request_id.clone(),
            prompt_id: prompt_id.clone(),
            kind: "confirmation".to_string(),
            message: prompt.to_string(),
            choices: vec!["yes".to_string(), "no".to_string()],
        }))?;
        loop {
            let Some(request) = self.read_request()? else {
                return Ok(Some(OperatorLine::Eof));
            };
            match request.request {
                RawRequest::PromptResponse {
                    prompt_id: response_prompt_id,
                    response,
                } if response_prompt_id == prompt_id => {
                    return Ok(Some(OperatorLine::Line(response)));
                }
                RawRequest::PromptResponse { .. } => {
                    self.write_error_result(
                        &request.request_id,
                        Some("stale_prompt"),
                        "prompt_response does not match the active prompt",
                    )?;
                }
                _ => {
                    self.write_error_result(
                        &request.request_id,
                        Some("busy"),
                        "server is waiting for prompt_response",
                    )?;
                }
            }
        }
    }
}

/// Tool-output observer that forwards chunks onto the raw JSONL stream.
pub struct RawToolOutputObserver<W>
where
    W: Write + Send + 'static,
{
    output: SharedOutput<W>,
}

impl<W> RawToolOutputObserver<W>
where
    W: Write + Send + 'static,
{
    /// Create a new observer that writes raw `tool_output` events.
    pub fn new(output: SharedOutput<W>) -> Self {
        Self { output }
    }
}

impl<W> ToolOutputObserver for RawToolOutputObserver<W>
where
    W: Write + Send + 'static,
{
    fn on_tool_output(&self, event: &ToolOutputEvent) {
        let _ = self
            .output
            .write_message(&RawServerMessage::Event(RawEventEnvelope {
                protocol_version: RAW_PROTOCOL_VERSION,
                request_id: event.request_id.clone(),
                event: RawEvent::ToolOutput {
                    tool_name: event.tool_name.clone(),
                    tool_use_id: event.tool_use_id.clone(),
                    stream: event.stream.clone(),
                    text: event.text.clone(),
                    data_b64: event.data_b64.clone(),
                },
            }));
    }
}

/// Parse a raw request line while preserving any extracted request id.
pub fn parse_request_line(line: &str) -> Result<RawRequestEnvelope, ParsedRequestError> {
    let value: serde_json::Value =
        serde_json::from_str(line).map_err(|err| ParsedRequestError {
            request_id: String::new(),
            code: Some("invalid_request_json".to_string()),
            message: format!("failed to parse raw request JSON: {err}"),
        })?;
    let request_id = value
        .get("request_id")
        .and_then(serde_json::Value::as_str)
        .unwrap_or_default()
        .to_string();
    serde_json::from_value(value).map_err(|err| ParsedRequestError {
        request_id,
        code: Some("invalid_request".to_string()),
        message: format!("failed to decode raw request: {err}"),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_request_line_extracts_request_id_on_decode_error() {
        let err = parse_request_line(r#"{"request_id":"abc","protocol_version":1}"#).unwrap_err();
        assert_eq!(err.request_id, "abc");
        assert_eq!(err.code.as_deref(), Some("invalid_request"));
    }

    #[test]
    fn prompt_wait_rejects_non_prompt_requests() {
        let input = BufReader::new(
            br#"{"protocol_version":1,"request_id":"bad","op":"stats"}
{"protocol_version":1,"request_id":"good","op":"prompt_response","prompt_id":"prompt-1","response":"yes"}
"#
            .as_slice(),
        );
        let output = Vec::new();
        let mut server = RawServer::new(input, output);
        server.set_request_id("req-1".to_string());
        let line = server.read_operator_line("Continue?").unwrap();
        assert_eq!(line, Some(OperatorLine::Line("yes".to_string())));

        let text = server
            .output
            .with_writer(|writer| String::from_utf8_lossy(writer).into_owned());
        assert!(text.contains("\"type\":\"prompt\""));
        assert!(text.contains("\"request_id\":\"bad\""));
        assert!(text.contains("\"code\":\"busy\""));
    }
}