alpha2-host 0.8.0

Native project-rendering sidecar and batch CLI for alpha2.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! How `serve` reads and writes its frames ([`alpha2_host_core::host_frames`]).
//!
//! The `alpha2-requests` thread owns stdin: it answers each `cancel` and `status` request at once
//! and queues every other frame for the request thread, so a cancel reaches a running export
//! without waiting. Every frame `serve` writes goes through one [`Responder`], which writes it
//! whole, its header as UTF-8 JSON.

use std::io::{Read, Write};
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};
use std::sync::{Arc, Condvar, Mutex, PoisonError};
use std::time::Duration;

use alpha2_host_core::host_frames::{
    Attachment, FrameLimits, MAGIC, ReadFrame, frame_bytes, read_frame, split_attachments,
};
use serde::Serialize;
use serde_json::{Value, json};

use crate::PROTOCOL_VERSION;
use crate::lifecycle::Lifecycle;
use crate::protocol::{
    HostError, MAX_CONTROL_HEADER_BYTES, MAX_REQUEST_ATTACHMENT_BYTES, MAX_REQUEST_HEADER_BYTES,
    MAX_RESPONSE_HEADER_BYTES, parse_cancel, parse_status,
};

/// Past this the reader stops reading until a request is taken, so a cancel behind that much
/// queued input is read late.
const MAX_QUEUED_BYTES: usize = 256 * 1024 * 1024;

const REQUEST_LIMITS: FrameLimits = FrameLimits {
    header_bytes: MAX_REQUEST_HEADER_BYTES,
    attachment_bytes: MAX_REQUEST_ATTACHMENT_BYTES,
};

/// JSON with every non-ASCII character escaped as `\uXXXX`.
///
/// `PowerShell` 5.1 decodes a native program's output in the console's OEM code page, where raw
/// UTF-8 would come out garbled.
pub(crate) fn json_line(value: &Value) -> Vec<u8> {
    let mut line = Vec::new();
    let mut serializer = serde_json::Serializer::with_formatter(&mut line, AsciiFormatter);
    // A `Value` always serializes: its keys are strings and its numbers finite.
    let _ = value.serialize(&mut serializer);
    line
}

struct AsciiFormatter;

impl serde_json::ser::Formatter for AsciiFormatter {
    fn write_string_fragment<W: ?Sized + Write>(
        &mut self,
        writer: &mut W,
        fragment: &str,
    ) -> std::io::Result<()> {
        let mut ascii_from = 0;
        for (at, character) in fragment.char_indices() {
            if character.is_ascii() {
                continue;
            }
            writer.write_all(&fragment.as_bytes()[ascii_from..at])?;
            let mut units = [0_u16; 2];
            for unit in character.encode_utf16(&mut units) {
                write!(writer, "\\u{unit:04x}")?;
            }
            ascii_from = at + character.len_utf8();
        }
        writer.write_all(&fragment.as_bytes()[ascii_from..])
    }
}

/// The protocol stream, or a test's stand-in, shared by every thread that writes a frame.
pub(crate) struct Responder {
    output: Mutex<Box<dyn Write + Send>>,
    header_limit: usize,
}

impl Responder {
    pub(crate) fn new(output: Box<dyn Write + Send>) -> Self {
        Self::with_header_limit(output, MAX_RESPONSE_HEADER_BYTES as usize)
    }

    pub(crate) const fn with_header_limit(
        output: Box<dyn Write + Send>,
        header_limit: usize,
    ) -> Self {
        Self {
            output: Mutex::new(output),
            header_limit,
        }
    }

    /// `header` as UTF-8 JSON, or `RESOURCE_LIMIT` when it is longer than the header limit.
    fn header_bytes(&self, header: &Value) -> Result<Vec<u8>, HostError> {
        let line = serde_json::to_vec(header).map_err(HostError::json)?;
        if line.len() > self.header_limit {
            return Err(HostError::new(
                "RESOURCE_LIMIT",
                format!(
                    "the response header is {} bytes, more than the {} a response header may be",
                    line.len(),
                    self.header_limit
                ),
            )
            .with_details(json!({"headerBytes": line.len(), "limit": self.header_limit})));
        }
        Ok(line)
    }

    /// Standard output, taken over so that nothing else this process prints reaches it
    /// ([`alpha2_os::claim_stdout`]).
    pub(crate) fn claimed_stdout() -> Self {
        match alpha2_os::claim_stdout() {
            Ok(stream) => Self::new(Box::new(stream)),
            Err(error) => {
                alpha2_os::diagnostic!(
                    "alpha2-host: standard output is shared with native code: {error}"
                );
                Self::new(Box::new(std::io::stdout()))
            }
        }
    }

    /// Writes [`MAGIC`], before the first frame.
    ///
    /// # Errors
    /// When the output refuses it.
    pub(crate) fn start(&self) -> Result<(), HostError> {
        self.write(&MAGIC)
    }

    /// # Errors
    /// When the output refuses the frame (a client that closed the pipe).
    pub(crate) fn send(&self, header: &Value) -> Result<(), HostError> {
        self.send_with(header, &[])
    }

    /// Lists `attachments` in the header and writes the frame in one piece, so another thread's
    /// frame never lands inside it.
    ///
    /// # Errors
    /// When the output refuses the frame, or the attachments exceed a frame. `RESOURCE_LIMIT`,
    /// with nothing written, when the header is longer than a response header may be.
    pub(crate) fn send_with(
        &self,
        header: &Value,
        attachments: &[Attachment],
    ) -> Result<(), HostError> {
        let frame = if attachments.is_empty() {
            frame_bytes(&self.header_bytes(header)?, &[])
        } else {
            let mut header = header.clone();
            header["attachments"] = attachments
                .iter()
                .map(|attachment| json!({"type": attachment.kind, "bytes": attachment.bytes.len()}))
                .collect();
            let parts = attachments
                .iter()
                .map(|attachment| attachment.bytes.as_slice())
                .collect::<Vec<_>>();
            frame_bytes(&self.header_bytes(&header)?, &parts)
        };
        self.write(&frame.map_err(HostError::io)?)
    }

    /// [`Self::send`], or `None` without writing when another thread holds the output.
    pub(crate) fn try_send(&self, header: &Value) -> Option<Result<(), HostError>> {
        let mut output = match self.output.try_lock() {
            Ok(output) => output,
            Err(std::sync::TryLockError::Poisoned(poisoned)) => poisoned.into_inner(),
            Err(std::sync::TryLockError::WouldBlock) => return None,
        };
        let frame = match self.header_bytes(header) {
            Ok(line) => frame_bytes(&line, &[]).map_err(HostError::io),
            Err(error) => Err(error),
        };
        let frame = match frame {
            Ok(frame) => frame,
            Err(error) => return Some(Err(error)),
        };
        Some(
            output
                .write_all(&frame)
                .and_then(|()| output.flush())
                .map_err(HostError::io),
        )
    }

    fn write(&self, bytes: &[u8]) -> Result<(), HostError> {
        let mut output = self.output.lock().unwrap_or_else(PoisonError::into_inner);
        output.write_all(bytes).map_err(HostError::io)?;
        output.flush().map_err(HostError::io)
    }
}

/// What `serve` writes first, [`MAGIC`] and its `ready` state, before anything that can take long.
pub(crate) fn write_ready(out: &Responder, lifecycle: &Lifecycle) -> Result<(), HostError> {
    out.start()?;
    out.send(&lifecycle.state_message())
}

pub(crate) enum InputFrame {
    /// A header in UTF-8, and its attachments or why they do not match the header.
    Request {
        header: String,
        attachments: Result<Vec<Attachment>, String>,
    },
    /// A frame of this many bytes, over a limit of [`REQUEST_LIMITS`].
    TooLarge(u64),
    NotUtf8,
    /// Stdin ended.
    End,
    /// Reading stdin failed, or the reader panicked: what was written after is lost.
    Failed(HostError),
}

impl InputFrame {
    fn queued_bytes(&self) -> usize {
        match self {
            Self::Request {
                header,
                attachments,
            } => {
                header.len()
                    + attachments.as_ref().map_or(0, |attachments| {
                        attachments.iter().map(|part| part.bytes.len()).sum()
                    })
            }
            Self::TooLarge(_) | Self::NotUtf8 | Self::End | Self::Failed(_) => 0,
        }
    }
}

#[derive(Default)]
struct Queued {
    bytes: Mutex<usize>,
    taken: Condvar,
}

pub(crate) struct FrameSender {
    sender: Sender<InputFrame>,
    queued: Arc<Queued>,
}

pub(crate) struct Requests {
    receiver: Receiver<InputFrame>,
    queued: Arc<Queued>,
}

pub(crate) fn request_queue() -> (FrameSender, Requests) {
    let (sender, receiver) = std::sync::mpsc::channel();
    let queued = Arc::new(Queued::default());
    (
        FrameSender {
            sender,
            queued: Arc::clone(&queued),
        },
        Requests { receiver, queued },
    )
}

impl FrameSender {
    /// Waits while the queue holds more than [`MAX_QUEUED_BYTES`]. Returns `false` when the
    /// request thread is gone.
    pub(crate) fn send(&self, frame: InputFrame) -> bool {
        let bytes = frame.queued_bytes();
        let mut queued = self
            .queued
            .bytes
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        while *queued > 0 && queued.saturating_add(bytes) > MAX_QUEUED_BYTES {
            queued = self
                .queued
                .taken
                .wait(queued)
                .unwrap_or_else(PoisonError::into_inner);
        }
        *queued += bytes;
        drop(queued);
        self.sender.send(frame).is_ok()
    }
}

impl Requests {
    pub(crate) fn next(&self) -> InputFrame {
        let Ok(frame) = self.receiver.recv() else {
            return InputFrame::End;
        };
        self.taken(frame)
    }

    /// `None` when no frame arrived within `limit`.
    pub(crate) fn next_within(&self, limit: Duration) -> Option<InputFrame> {
        match self.receiver.recv_timeout(limit) {
            Ok(frame) => Some(self.taken(frame)),
            Err(RecvTimeoutError::Timeout) => None,
            Err(RecvTimeoutError::Disconnected) => Some(InputFrame::End),
        }
    }

    /// A frame already read, without waiting for one.
    pub(crate) fn queued(&self) -> Option<InputFrame> {
        self.receiver.try_recv().ok().map(|frame| self.taken(frame))
    }

    fn taken(&self, frame: InputFrame) -> InputFrame {
        let mut queued = self
            .queued
            .bytes
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        *queued = queued.saturating_sub(frame.queued_bytes());
        self.queued.taken.notify_all();
        frame
    }
}

/// Only the `attachments` field is read; the rest of the header is skipped, not parsed into a tree.
#[derive(serde::Deserialize)]
struct Declared {
    #[serde(default)]
    attachments: Value,
}

fn input_frame(frame: alpha2_host_core::host_frames::Frame) -> InputFrame {
    let Ok(header) = String::from_utf8(frame.header) else {
        return InputFrame::NotUtf8;
    };
    let attachments = if frame.attachments.is_empty() {
        Ok(Vec::new())
    } else {
        serde_json::from_str::<Declared>(&header)
            .map_err(|error| format!("invalid request: {error}"))
            .and_then(|declared| {
                split_attachments(
                    &json!({ "attachments": declared.attachments }),
                    frame.attachments,
                )
            })
    };
    InputFrame::Request {
        header,
        attachments,
    }
}

pub(crate) fn read_requests(
    input: &mut impl Read,
    frames: &FrameSender,
    out: &Responder,
    lifecycle: &Lifecycle,
) {
    loop {
        let frame = match read_frame(input, REQUEST_LIMITS) {
            Ok(ReadFrame::Frame(frame)) => input_frame(frame),
            Ok(ReadFrame::TooLarge {
                header_bytes,
                attachment_bytes,
            }) => InputFrame::TooLarge(u64::from(header_bytes) + u64::from(attachment_bytes)),
            Ok(ReadFrame::End) => {
                // No acknowledgement can arrive any more.
                crate::extract_request::close_ack_routes();
                frames.send(InputFrame::End);
                return;
            }
            Err(error) => {
                crate::extract_request::close_ack_routes();
                frames.send(InputFrame::Failed(HostError::new(
                    "IO_ERROR",
                    format!("reading requests failed: {error}"),
                )));
                return;
            }
        };
        // Acknowledgements go to their running extract request, never the request queue.
        if let InputFrame::Request {
            header,
            attachments: Ok(attachments),
        } = &frame
            && attachments.is_empty()
            && crate::protocol::operation_from_header(header).as_deref() == Some("extract.ack")
        {
            let request_id = crate::protocol::request_id_from_header(header);
            crate::extract_request::route_ack(&request_id, header.clone());
            continue;
        }
        if let InputFrame::Request {
            header,
            attachments: Ok(attachments),
        } = &frame
            && attachments.is_empty()
            && header.len() <= MAX_CONTROL_HEADER_BYTES
        {
            let answer = match crate::protocol::operation_from_header(header).as_deref() {
                Some("cancel") => Some(cancel_answer(header)),
                Some("status") => Some(status_answer(header, lifecycle)),
                _ => None,
            };
            if let Some(mut answer) = answer {
                answer["hostState"] = json!(lifecycle.state().as_str());
                let _ = out.send(&answer);
                continue;
            }
        }
        if !frames.send(frame) {
            return;
        }
    }
}

fn cancel_answer(header: &str) -> Value {
    match parse_cancel(header) {
        Ok(cancel) => {
            let state = crate::termination::terminations().cancel_request(&cancel.target);
            json!({
                "protocolVersion": PROTOCOL_VERSION,
                "requestId": cancel.request_id,
                "ok": true,
                "result": { "target": cancel.target, "state": state },
            })
        }
        Err(error) => json!({
            "protocolVersion": PROTOCOL_VERSION,
            "requestId": crate::protocol::request_id_from_header(header),
            "ok": false,
            "error": HostError::from(error),
        }),
    }
}

fn status_answer(header: &str, lifecycle: &Lifecycle) -> Value {
    match parse_status(header) {
        Ok(request_id) => json!({
            "protocolVersion": PROTOCOL_VERSION,
            "requestId": request_id,
            "ok": true,
            "result": lifecycle.status(),
        }),
        Err(error) => json!({
            "protocolVersion": PROTOCOL_VERSION,
            "requestId": crate::protocol::request_id_from_header(header),
            "ok": false,
            "error": HostError::from(error),
        }),
    }
}

pub(crate) fn start_reader(
    out: Arc<Responder>,
    lifecycle: Arc<Lifecycle>,
) -> Result<Requests, HostError> {
    let (sender, requests) = request_queue();
    std::thread::Builder::new()
        .name("alpha2-requests".to_owned())
        .spawn(move || {
            read_requests_guarded(&mut std::io::stdin().lock(), &sender, &out, &lifecycle);
        })
        .map_err(HostError::io)?;
    Ok(requests)
}

/// [`read_requests`], whose panic ends the input as a failure the dispatcher reports.
fn read_requests_guarded(
    input: &mut impl Read,
    frames: &FrameSender,
    out: &Responder,
    lifecycle: &Lifecycle,
) {
    let read = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        read_requests(input, frames, out, lifecycle);
    }));
    if let Err(payload) = read {
        crate::extract_request::close_ack_routes();
        frames.send(InputFrame::Failed(HostError::new(
            "INTERNAL",
            format!(
                "the request reader panicked: {}",
                crate::failure::panic_message(&*payload)
            ),
        )));
    }
}

#[cfg(test)]
pub(crate) fn requests_of(input: &[u8], out: &Responder, lifecycle: &Lifecycle) -> Requests {
    requests_from(&mut &input[..], out, lifecycle)
}

/// [`requests_of`] from any reader.
#[cfg(test)]
pub(crate) fn requests_from(
    input: &mut impl Read,
    out: &Responder,
    lifecycle: &Lifecycle,
) -> Requests {
    let (sender, requests) = request_queue();
    read_requests_guarded(input, &sender, out, lifecycle);
    requests
}

/// One request frame per line of `lines`, for tests written as JSON lines.
#[cfg(test)]
pub(crate) fn frames_of_lines(lines: &[u8]) -> Vec<u8> {
    let mut frames = Vec::new();
    for line in lines.split(|byte| *byte == b'\n') {
        let line = line.strip_suffix(b"\r").unwrap_or(line);
        if line.iter().all(u8::is_ascii_whitespace) {
            continue;
        }
        frames.extend(frame_bytes(line, &[]).expect("a test frame"));
    }
    frames
}

/// Each frame's header and attachments, from bytes `serve` wrote after its [`MAGIC`].
#[cfg(test)]
pub(crate) fn messages_of(mut output: &[u8]) -> Vec<(Value, Vec<Attachment>)> {
    let limits = FrameLimits {
        header_bytes: u32::MAX,
        attachment_bytes: u32::MAX,
    };
    let mut messages = Vec::new();
    while let ReadFrame::Frame(frame) = read_frame(&mut output, limits).expect("a slice reads") {
        let header: Value = serde_json::from_slice(&frame.header).expect("a header is JSON");
        let attachments = split_attachments(&header, frame.attachments).expect("attachments");
        messages.push((header, attachments));
    }
    assert!(
        output.is_empty(),
        "{} bytes after the last frame",
        output.len()
    );
    messages
}

#[cfg(test)]
#[derive(Clone, Default)]
pub(crate) struct Captured(Arc<Mutex<Vec<u8>>>);

#[cfg(test)]
impl Captured {
    pub(crate) fn responder(&self) -> Responder {
        Responder::new(Box::new(self.clone()))
    }

    pub(crate) fn responder_with_header_limit(&self, limit: usize) -> Responder {
        Responder::with_header_limit(Box::new(self.clone()), limit)
    }

    pub(crate) fn bytes(&self) -> Vec<u8> {
        self.0
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone()
    }
}

#[cfg(test)]
impl Write for Captured {
    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
        self.0
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .extend_from_slice(bytes);
        Ok(bytes.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}