netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Gearman job server — the model is the worker for every function.
//!
//! Clients speak the binary protocol (`\0REQ` packets) and administrators the text protocol
//! (`status`, `workers`, …); a leading `\0` says which one the next message is, so a connection
//! may mix them. A submitted job gets a NetGet-generated handle in `JOB_CREATED` at once, then
//! raises `gearman_job_submitted`; the model's answer becomes `WORK_STATUS`/`WORK_DATA` and one
//! outcome packet.
//!
//! Five properties worth knowing before changing the loop:
//!
//! 1. **Sizes are judged before anything is allocated.** A packet's declared size is checked
//!    against [`wire::MAX_PACKET_BYTES`] from its 12-byte header; an admin line is capped at
//!    [`wire::MAX_ADMIN_LINE`]. Either overrun is refused and the connection closed.
//! 2. **The model cannot write a packet, and cannot answer another job.** Its answers are
//!    rendered by [`wire`] with the handle of the job being answered; an answer that names
//!    another handle, or a packet that is not a job answer, is refused. A foreground job always
//!    ends with exactly one outcome: if the model gave none, NetGet sends `WORK_FAIL`.
//! 3. **Worker connections are refused, not half-served.** `CAN_DO`, `GRAB_JOB` and the rest
//!    get `ERROR not_supported` and the connection closes: the model is the worker, and a real
//!    worker that registered here would sleep forever waiting for a job NetGet never queues.
//! 4. **The deadlines wrap reads, not answers.** A client waiting for its job's result — the
//!    model thinking, or the event parked for a human — is not idle.
//! 5. **NetGet keeps only the jobs in flight** (handle, function, last progress), for
//!    `GET_STATUS` and the admin `status`; a job leaves the table when its answer is written.
pub mod actions;
pub mod wire;

use crate::llm::action_helper::call_llm;
use crate::llm::actions::protocol_trait::ActionResult;
use crate::llm::ollama_client::OllamaClient;
use crate::logging::emit::Log;
use crate::protocol::Event;
use crate::server::connection::ConnectionId;
use crate::state::app_state::AppState;
use actions::Work;
use anyhow::Result;
use std::collections::BTreeMap;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::{mpsc, Mutex};

pub use wire::MAX_PACKET_BYTES;

/// How long a new connection may send nothing. The `gearman` CLI writes its request at once;
/// 300 s is the window a `manual` rule gives a human, for a NetGet TCP client parked on its
/// operator. Lower it through `first_byte_timeout_secs` for a public listener.
const FIRST_BYTE_TIMEOUT: Duration = Duration::from_secs(300);

/// How long the server waits for the next message after answering one, and for the rest of a
/// message that has started arriving.
const IDLE_TIMEOUT: Duration = Duration::from_secs(300);

/// Concurrent connections admitted before new ones are refused — the house default.
const MAX_CONNECTIONS: usize = crate::server::accept_bounded::DEFAULT_MAX_CONNECTIONS;

/// Fixed `WORK_EXCEPTION` texts for a backend failure, for clients that enabled exceptions.
const EXCEPTION_UNAVAILABLE: &[u8] = b"job server backend unavailable";
const EXCEPTION_OVERLOADED: &[u8] = b"job server backend at capacity";

/// A job the model is working on.
#[derive(Debug, Clone)]
struct InFlight {
    function: String,
    numerator: u64,
    denominator: u64,
}

/// The jobs in flight on one server, by handle, and the handle counter.
#[derive(Default)]
struct Jobs {
    next: AtomicU64,
    table: std::sync::Mutex<BTreeMap<Vec<u8>, InFlight>>,
}

impl Jobs {
    fn start(&self, function: &str) -> Vec<u8> {
        let n = self.next.fetch_add(1, Ordering::SeqCst) + 1;
        let handle = format!("H:netget:{n}").into_bytes();
        self.table.lock().unwrap().insert(
            handle.clone(),
            InFlight {
                function: function.to_string(),
                numerator: 0,
                denominator: 0,
            },
        );
        handle
    }
    fn finish(&self, handle: &[u8]) {
        self.table.lock().unwrap().remove(handle);
    }
    fn get(&self, handle: &[u8]) -> Option<InFlight> {
        self.table.lock().unwrap().get(handle).cloned()
    }
    /// `(function, total, running, available workers)` for the admin `status`.
    fn by_function(&self) -> Vec<(String, u64, u64, u64)> {
        let mut counts: BTreeMap<String, u64> = BTreeMap::new();
        for job in self.table.lock().unwrap().values() {
            *counts.entry(job.function.clone()).or_default() += 1;
        }
        counts.into_iter().map(|(f, n)| (f, n, n, 0)).collect()
    }
}

pub struct GearmanServer;

impl GearmanServer {
    pub async fn spawn_with_llm_actions(
        listen_addr: SocketAddr,
        llm_client: OllamaClient,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
        server_id: crate::state::ServerId,
        first_byte_timeout_secs: Option<u64>,
        idle_timeout_secs: Option<u64>,
    ) -> Result<SocketAddr> {
        let first_timeout = first_byte_timeout_secs
            .map(Duration::from_secs)
            .unwrap_or(FIRST_BYTE_TIMEOUT);
        let idle_timeout = idle_timeout_secs
            .map(Duration::from_secs)
            .unwrap_or(IDLE_TIMEOUT);
        let listener = TcpListener::bind(listen_addr).await?;
        let local_addr = listener.local_addr()?;

        Log::new(Some(&status_tx)).info(format!("Gearman job server listening on {}", local_addr));

        let protocol = Arc::new(actions::GearmanProtocol::new());
        let jobs = Arc::new(Jobs::default());
        let task_registrar = app_state.clone();
        let limiter = crate::server::accept_bounded::ConnectionLimiter::new(MAX_CONNECTIONS);
        let accept_handle = tokio::spawn(async move {
            loop {
                // A peer over the cap gets no bytes: a packet it did not ask for would be read
                // as the answer to its first request.
                match crate::server::accept_bounded::accept_bounded(
                    &listener,
                    &limiter,
                    b"",
                    "Gearman",
                    Some(&status_tx),
                )
                .await
                {
                    Ok((socket, peer_addr, permit)) => {
                        let connection_id =
                            ConnectionId::new(app_state.get_next_unified_id().await);
                        use crate::state::server::{
                            ConnectionState as ServerConnectionState, ConnectionStatus,
                            ProtocolConnectionInfo,
                        };
                        let now = crate::utils::clock::Instant::now();
                        app_state
                            .add_connection_to_server(
                                server_id,
                                ServerConnectionState {
                                    id: connection_id,
                                    remote_addr: peer_addr,
                                    local_addr,
                                    bytes_sent: 0,
                                    bytes_received: 0,
                                    packets_sent: 0,
                                    packets_received: 0,
                                    last_activity: now,
                                    status: ConnectionStatus::Active,
                                    status_changed_at: now,
                                    protocol_info: ProtocolConnectionInfo::empty(),
                                },
                            )
                            .await;
                        let _ = status_tx.send("__UPDATE_UI__".to_string());
                        Log::new(Some(&status_tx))
                            .info(format!("Gearman client connected from {}", peer_addr));

                        let session = Session {
                            peer_addr,
                            llm_client: llm_client.clone(),
                            app_state: app_state.clone(),
                            status_tx: status_tx.clone(),
                            server_id,
                            protocol: protocol.clone(),
                            connection_id,
                            first_timeout,
                            idle_timeout,
                            jobs: jobs.clone(),
                        };
                        // Tracked, not detached: stop_server must abort this task too.
                        let task_owner = app_state.clone();
                        task_owner
                            .spawn_server_task(server_id, async move {
                                let _permit = permit;
                                session.run(socket).await
                            })
                            .await;
                    }
                    Err(e) => {
                        Log::new(Some(&status_tx)).error(format!("Gearman accept error: {}", e));
                        break;
                    }
                }
            }
        });

        task_registrar
            .register_server_task(server_id, accept_handle)
            .await;
        Ok(local_addr)
    }
}

type Writer = Arc<Mutex<tokio::io::WriteHalf<tokio::net::TcpStream>>>;

struct Session {
    peer_addr: SocketAddr,
    llm_client: OllamaClient,
    app_state: Arc<AppState>,
    status_tx: mpsc::UnboundedSender<String>,
    server_id: crate::state::ServerId,
    protocol: Arc<actions::GearmanProtocol>,
    connection_id: ConnectionId,
    first_timeout: Duration,
    idle_timeout: Duration,
    jobs: Arc<Jobs>,
}

/// One message read from the connection.
enum Message {
    Packet(wire::Header, Vec<u8>),
    Admin(String),
}

enum ReadError {
    Eof,
    TimedOut,
    TooLarge(u32),
    BadMagic,
    LineTooLong,
    Io(std::io::Error),
}

impl Session {
    async fn run(self, socket: tokio::net::TcpStream) {
        let (mut reader, write_half) = tokio::io::split(socket);
        let writer: Writer = Arc::new(Mutex::new(write_half));

        let peer_rx = crate::server::peer_support::register_peer_channel(
            &self.app_state,
            self.server_id,
            self.connection_id.as_u32(),
        )
        .await;
        crate::server::peer_support::spawn_peer_command_task(
            peer_rx,
            self.protocol.clone(),
            self.app_state.clone(),
            self.server_id,
            self.connection_id.as_u32(),
            writer.clone(),
            self.status_tx.clone(),
        );

        {
            let mut framer = Framer::new(&mut reader);
            self.session(&mut framer, &writer).await;
        }

        self.app_state
            .remove_peer_handle(self.server_id, self.connection_id.as_u32())
            .await;
        let _ = writer.lock().await.shutdown().await;
        linger(&mut reader).await;
        self.app_state
            .update_connection_status(
                self.server_id,
                self.connection_id,
                crate::state::server::ConnectionStatus::Closed,
            )
            .await;
        let _ = self.status_tx.send("__UPDATE_UI__".to_string());
    }

    async fn write(&self, writer: &Writer, data: &[u8]) -> bool {
        let ok = {
            let mut w = writer.lock().await;
            w.write_all(data).await.is_ok() && w.flush().await.is_ok()
        };
        if ok {
            self.app_state
                .update_connection_stats(
                    self.server_id,
                    self.connection_id,
                    None,
                    Some(data.len() as u64),
                    None,
                    Some(1),
                )
                .await;
        }
        ok
    }

    async fn error(&self, writer: &Writer, code: &str, text: &str) -> bool {
        match wire::error(code, text) {
            Ok(packet) => self.write(writer, &packet).await,
            Err(_) => false,
        }
    }

    async fn session<R: AsyncRead + Unpin>(&self, framer: &mut Framer<'_, R>, writer: &Writer) {
        let log = Log::new(Some(&self.status_tx));
        let mut exceptions = false;
        let mut first = true;

        loop {
            let timeout = if first {
                self.first_timeout
            } else {
                self.idle_timeout
            };
            let message = match framer.next_message(timeout, self.idle_timeout).await {
                Ok(m) => m,
                Err(ReadError::Eof) => {
                    log.info(format!("Gearman client {} disconnected", self.peer_addr));
                    return;
                }
                Err(ReadError::TimedOut) => {
                    log.info(format!(
                        "Gearman client {} sent nothing within {}s; closing",
                        self.peer_addr,
                        timeout.as_secs()
                    ));
                    return;
                }
                Err(ReadError::TooLarge(size)) => {
                    log.warn(format!(
                        "Gearman packet from {} declared {} bytes, over {} \
                         decision=fail_closed_too_large",
                        self.peer_addr,
                        size,
                        wire::MAX_PACKET_BYTES
                    ));
                    self.error(
                        writer,
                        "too_large",
                        "packet exceeds the server's size limit",
                    )
                    .await;
                    return;
                }
                Err(ReadError::BadMagic) => {
                    log.warn(format!(
                        "Gearman peer {} sent a packet without \\0REQ decision=fail_closed_bad_magic",
                        self.peer_addr
                    ));
                    return;
                }
                Err(ReadError::LineTooLong) => {
                    log.warn(format!(
                        "Gearman admin line from {} exceeded {} bytes \
                         decision=fail_closed_line_too_long",
                        self.peer_addr,
                        wire::MAX_ADMIN_LINE
                    ));
                    let _ = self
                        .write(
                            writer,
                            wire::admin_error("LINE_TOO_LONG", "Command line too long").as_bytes(),
                        )
                        .await;
                    return;
                }
                Err(ReadError::Io(e)) => {
                    log.error(format!("Gearman read error from {}: {}", self.peer_addr, e));
                    return;
                }
            };
            first = false;

            let keep_going = match message {
                Message::Admin(line) => self.admin(&line, writer).await,
                Message::Packet(header, data) => {
                    self.app_state
                        .update_connection_stats(
                            self.server_id,
                            self.connection_id,
                            Some((wire::HEADER_LEN + data.len()) as u64),
                            None,
                            Some(1),
                            None,
                        )
                        .await;
                    self.packet(&header, &data, &mut exceptions, writer).await
                }
            };
            if !keep_going {
                return;
            }
        }
    }

    /// Answer one admin line. `false` closes the connection.
    async fn admin(&self, line: &str, writer: &Writer) -> bool {
        let log = Log::new(Some(&self.status_tx));
        if line.trim().is_empty() {
            return true;
        }
        let reply = match wire::parse_admin(line) {
            wire::AdminCommand::Status => wire::admin_status(&self.jobs.by_function()),
            wire::AdminCommand::Workers => ".\n".to_string(),
            wire::AdminCommand::Version => {
                format!("OK netget-{}\n", env!("CARGO_PKG_VERSION"))
            }
            wire::AdminCommand::MaxQueue | wire::AdminCommand::Shutdown => {
                log.warn(format!(
                    "Gearman admin {:?} from {} refused decision=fail_closed_admin_write",
                    line.trim(),
                    self.peer_addr
                ));
                wire::admin_error("NOT_SUPPORTED", "This server does not change its state")
            }
            wire::AdminCommand::Unknown => {
                wire::admin_error("UNKNOWN_COMMAND", "Unknown server command")
            }
        };
        self.write(writer, reply.as_bytes()).await
    }

    /// Answer one binary packet. `false` closes the connection.
    async fn packet(
        &self,
        header: &wire::Header,
        data: &[u8],
        exceptions: &mut bool,
        writer: &Writer,
    ) -> bool {
        let log = Log::new(Some(&self.status_tx));
        let request = match wire::parse_request(header, data) {
            Ok(r) => r,
            Err(e) => {
                log.warn(format!(
                    "Gearman packet type {} from {}: {:?} decision=fail_closed_bad_arguments",
                    header.packet_type, self.peer_addr, e
                ));
                return self
                    .error(writer, "invalid_arguments", "malformed packet arguments")
                    .await;
            }
        };
        match request {
            wire::Request::Echo(data) => {
                self.write(writer, &wire::response(wire::ECHO_RES, &[&data]))
                    .await
            }
            wire::Request::Option(name) if name == b"exceptions" => {
                *exceptions = true;
                self.write(writer, &wire::response(wire::OPTION_RES, &[&name]))
                    .await
            }
            wire::Request::Option(_) => {
                self.error(writer, "unknown_option", "the only option is exceptions")
                    .await
            }
            wire::Request::SetClientId => true,
            wire::Request::GetStatus(handle) => {
                let reply = match self.jobs.get(&handle) {
                    Some(job) => {
                        wire::status_res(&handle, true, true, job.numerator, job.denominator)
                    }
                    None => wire::status_res(&handle, false, false, 0, 0),
                };
                self.write(writer, &reply).await
            }
            wire::Request::Worker(t) => {
                log.warn(format!(
                    "Gearman worker packet {} from {} refused decision=fail_closed_worker",
                    t, self.peer_addr
                ));
                self.error(
                    writer,
                    "not_supported",
                    "this server runs jobs itself; worker connections are not accepted",
                )
                .await;
                false
            }
            wire::Request::Unsupported(t) => {
                log.warn(format!(
                    "Gearman packet type {} from {} is not supported \
                     decision=fail_closed_unsupported",
                    t, self.peer_addr
                ));
                self.error(writer, "not_supported", "packet type not supported")
                    .await
            }
            wire::Request::Submit {
                function,
                unique,
                workload,
                priority,
                background,
            } => {
                let handle = self.jobs.start(&function);
                if !self.write(writer, &wire::job_created(&handle)).await {
                    self.jobs.finish(&handle);
                    return false;
                }
                let event = Event::new(
                    &actions::GEARMAN_JOB_SUBMITTED_EVENT,
                    serde_json::json!({
                        "function": function,
                        "unique_id": unique,
                        "workload": String::from_utf8_lossy(&workload),
                        "workload_bytes": workload.len(),
                        "priority": priority.name(),
                        "background": background,
                        "job_handle": String::from_utf8_lossy(&handle),
                    }),
                );
                let (packets, close) = self.run_job(&event, &handle, background, *exceptions).await;
                let mut ok = true;
                if !background {
                    for packet in &packets {
                        if let Some((wire::WORK_STATUS, args)) = wire::read_response(packet) {
                            let n = String::from_utf8_lossy(&args[1]).parse().unwrap_or(0);
                            let d = String::from_utf8_lossy(&args[2]).parse().unwrap_or(0);
                            if let Some(job) =
                                self.jobs.table.lock().unwrap().get_mut(handle.as_slice())
                            {
                                job.numerator = n;
                                job.denominator = d;
                            }
                        }
                        if !self.write(writer, packet).await {
                            ok = false;
                            break;
                        }
                    }
                }
                self.jobs.finish(&handle);
                ok && !close
            }
        }
    }

    /// Ask the model to run a job; returns the packets to write (empty for a background job)
    /// and whether to close afterwards.
    async fn run_job(
        &self,
        event: &Event,
        handle: &[u8],
        background: bool,
        exceptions: bool,
    ) -> (Vec<Vec<u8>>, bool) {
        let log = Log::new(Some(&self.status_tx));
        let shown = String::from_utf8_lossy(handle).into_owned();
        let result = match call_llm(
            &self.llm_client,
            &self.app_state,
            self.server_id,
            Some(self.connection_id),
            event,
            self.protocol.as_ref(),
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                let (category, text) = match crate::utils::WireFailure::classify(&e) {
                    crate::utils::WireFailure::Overloaded => ("overloaded", EXCEPTION_OVERLOADED),
                    crate::utils::WireFailure::Unavailable => {
                        ("unavailable", EXCEPTION_UNAVAILABLE)
                    }
                };
                log.warn(format!(
                    "Gearman job {} from {} decision=fail_closed_llm_error category={}",
                    shown, self.peer_addr, category
                ));
                log.debug(format!("Gearman LLM call failed: {}", e));
                let packet = if exceptions {
                    wire::work_exception(handle, text)
                } else {
                    wire::work_fail(handle)
                };
                return (if background { vec![] } else { vec![packet] }, false);
            }
        };
        for message in &result.messages {
            log.info(message);
        }

        let mut items = Vec::new();
        let mut close = false;
        let mut stack = result.protocol_results;
        stack.reverse();
        while let Some(item) = stack.pop() {
            match item {
                ActionResult::CloseConnection => close = true,
                ActionResult::Multiple(inner) => stack.extend(inner.into_iter().rev()),
                other => items.push(other),
            }
        }

        let mut packets: Vec<Vec<u8>> = Vec::new();
        let mut outcome: Option<u32> = None;
        let mut mismatch = None;
        for item in items {
            if outcome.is_some() {
                log.warn(format!(
                    "Gearman job {shown}: the model answered after the job's outcome; dropped"
                ));
                break;
            }
            let packet = match item {
                ActionResult::Output(bytes) => match wire::read_response(&bytes) {
                    Some((wire::ERROR, _)) => bytes,
                    Some((
                        wire::WORK_STATUS
                        | wire::WORK_DATA
                        | wire::WORK_COMPLETE
                        | wire::WORK_FAIL
                        | wire::WORK_EXCEPTION,
                        args,
                    )) if args[0] == handle => bytes,
                    other => {
                        mismatch = Some(format!("{:?}", other.map(|(t, _)| t)));
                        break;
                    }
                },
                ActionResult::Custom { name, data } if name == actions::WORK_RESULT => {
                    match Work::from_json(&data) {
                        Some(work) => work.render(handle),
                        None => {
                            mismatch = Some("unreadable work result".to_string());
                            break;
                        }
                    }
                }
                other => {
                    mismatch = Some(format!("{other:?}"));
                    break;
                }
            };
            let t = wire::read_response(&packet).map(|(t, _)| t).unwrap_or(0);
            let packet = if t == wire::WORK_EXCEPTION && !exceptions {
                // The client did not ask for exceptions: gearmand sends it WORK_FAIL.
                wire::work_fail(handle)
            } else {
                packet
            };
            if matches!(
                t,
                wire::WORK_COMPLETE | wire::WORK_FAIL | wire::WORK_EXCEPTION | wire::ERROR
            ) {
                outcome = Some(t);
            }
            packets.push(packet);
        }

        let decision = if let Some(what) = mismatch {
            log.warn(format!(
                "Gearman job {shown} from {} decision=fail_closed_mismatched_reply ({what}); \
                 answering WORK_FAIL",
                self.peer_addr
            ));
            packets = vec![wire::work_fail(handle)];
            None
        } else {
            match outcome {
                Some(wire::WORK_COMPLETE) => Some("model_answer"),
                Some(_) => Some("model_reject"),
                None if packets.is_empty() && !close => {
                    log.warn(format!(
                        "Gearman job {shown} from {} decision=model_silent ({} failed \
                         action(s)); answering WORK_FAIL",
                        self.peer_addr,
                        result.failures.len()
                    ));
                    packets.push(wire::work_fail(handle));
                    None
                }
                None if close => Some("model_close"),
                None => {
                    log.warn(format!(
                        "Gearman job {shown} from {} decision=fail_closed_unfinished: \
                         progress without an outcome; answering WORK_FAIL",
                        self.peer_addr
                    ));
                    packets.push(wire::work_fail(handle));
                    None
                }
            }
        };
        if let Some(decision) = decision {
            log.info(format!(
                "Gearman job {shown} from {} decision={} background={}",
                self.peer_addr, decision, background
            ));
        }
        (if background { Vec::new() } else { packets }, close)
    }
}

/// Keep reading briefly after the last reply and the half-close, so the close is a FIN rather
/// than an RST that could destroy that reply. Bounded both ways.
const LINGER_TIME: Duration = Duration::from_secs(2);
const LINGER_BYTES: usize = 64 * 1024;

async fn linger<R: AsyncRead + Unpin>(reader: &mut R) {
    let deadline = tokio::time::Instant::now() + LINGER_TIME;
    let mut sink = [0u8; 4096];
    let mut drained = 0usize;
    while drained < LINGER_BYTES {
        match tokio::time::timeout_at(deadline, reader.read(&mut sink)).await {
            Ok(Ok(n)) if n > 0 => drained += n,
            _ => break,
        }
    }
}

/// Reads packets and admin lines, keeping anything past what was asked for.
struct Framer<'a, R> {
    reader: &'a mut R,
    pending: Vec<u8>,
    chunk: Vec<u8>,
}

impl<'a, R: AsyncRead + Unpin> Framer<'a, R> {
    fn new(reader: &'a mut R) -> Self {
        Self {
            reader,
            pending: Vec::new(),
            chunk: vec![0u8; 8192],
        }
    }

    async fn fill(&mut self, timeout: Duration) -> Result<(), ReadError> {
        match tokio::time::timeout(timeout, self.reader.read(&mut self.chunk)).await {
            Err(_) => Err(ReadError::TimedOut),
            Ok(Ok(0)) => Err(ReadError::Eof),
            Ok(Ok(n)) => {
                self.pending.extend_from_slice(&self.chunk[..n]);
                Ok(())
            }
            Ok(Err(e)) => Err(ReadError::Io(e)),
        }
    }

    /// `first` bounds the wait for the message's first byte; `idle` every wait after it.
    async fn next_message(
        &mut self,
        first: Duration,
        idle: Duration,
    ) -> Result<Message, ReadError> {
        if self.pending.is_empty() {
            self.fill(first).await?;
        }
        if self.pending[0] == 0 {
            while self.pending.len() < wire::HEADER_LEN {
                self.fill(idle).await?;
            }
            let header = match wire::parse_header(&self.pending[..wire::HEADER_LEN]) {
                Ok(h) => h,
                Err(wire::HeaderError::TooLarge(size)) => return Err(ReadError::TooLarge(size)),
                Err(wire::HeaderError::BadMagic) => return Err(ReadError::BadMagic),
            };
            // Bounded by parse_header: at most MAX_PACKET_BYTES.
            let total = wire::HEADER_LEN + header.size as usize;
            while self.pending.len() < total {
                self.fill(idle).await?;
            }
            let packet: Vec<u8> = self.pending.drain(..total).collect();
            return Ok(Message::Packet(header, packet[wire::HEADER_LEN..].to_vec()));
        }
        loop {
            if let Some(at) = self.pending.iter().position(|b| *b == b'\n') {
                if at + 1 > wire::MAX_ADMIN_LINE {
                    return Err(ReadError::LineTooLong);
                }
                let line: Vec<u8> = self.pending.drain(..=at).collect();
                let text = String::from_utf8_lossy(&line[..at]);
                return Ok(Message::Admin(text.trim_end_matches('\r').to_string()));
            }
            if self.pending.len() >= wire::MAX_ADMIN_LINE {
                return Err(ReadError::LineTooLong);
            }
            self.fill(idle).await?;
        }
    }
}