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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//! 9P2000 file server. Rust owns the message codec, version negotiation, the fid table, walks
//! (including partial walks and ".."), open modes, directory paging, offsets and every bound;
//! the handler decides what exists, what each directory lists, each file's content, and
//! whether each change is accepted. Nothing is stored.
pub mod actions;
pub mod wire;

use crate::llm::action_helper::call_llm;
use crate::llm::actions::protocol_trait::ActionResult;
use crate::logging::emit::Log;
use crate::protocol::{Event, EventType, SpawnContext};
use crate::server::connection::ConnectionId;
use crate::state::server::{ConnectionState, ConnectionStatus, ProtocolConnectionInfo};
use anyhow::{Context, Result};
use serde_json::{json, Value};
use std::{collections::HashMap, net::SocketAddr, time::Duration};
use tokio::{
    io::AsyncWriteExt,
    net::{TcpListener, TcpStream},
};
use wire::{Qid, Reader, Stat, Writer};

/// Default silence allowed between messages.
pub const IDLE_TIMEOUT: Duration = Duration::from_secs(300);
pub const MAX_IDLE_TIMEOUT_SECS: u64 = 86400;
const DEFAULT_OWNER: &str = "netget";

fn idle(ctx: &SpawnContext) -> Result<Duration> {
    let secs = ctx
        .startup_params
        .as_ref()
        .map(|p| p.get_optional_u64("idle_timeout_secs"))
        .transpose()?
        .flatten()
        .unwrap_or(IDLE_TIMEOUT.as_secs());
    anyhow::ensure!(
        (1..=MAX_IDLE_TIMEOUT_SECS).contains(&secs),
        "idle_timeout_secs must be between 1 and {MAX_IDLE_TIMEOUT_SECS}"
    );
    Ok(Duration::from_secs(secs))
}

pub async fn spawn(ctx: SpawnContext) -> Result<SocketAddr> {
    let idle = idle(&ctx)?;
    let listener = TcpListener::bind(ctx.legacy_listen_addr()).await?;
    let addr = listener.local_addr()?;
    Log::new(Some(&ctx.status_tx)).info(format!("9P listening on {addr}"));
    let state = ctx.state.clone();
    let server_id = ctx.server_id;
    let accept = tokio::spawn(async move {
        let limiter = crate::server::accept_bounded::ConnectionLimiter::new(
            crate::server::accept_bounded::DEFAULT_MAX_CONNECTIONS,
        );
        loop {
            let (socket, peer, permit) = match crate::server::accept_bounded::accept_bounded(
                &listener,
                &limiter,
                b"",
                "9P",
                Some(&ctx.status_tx),
            )
            .await
            {
                Ok(v) => v,
                Err(_) => break,
            };
            let id = ConnectionId::new(ctx.state.get_next_unified_id().await);
            let now = crate::utils::clock::Instant::now();
            ctx.state
                .add_connection_to_server(
                    server_id,
                    ConnectionState {
                        id,
                        remote_addr: peer,
                        local_addr: 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 child = ctx.clone();
            ctx.state
                .spawn_server_task(server_id, async move {
                    let _permit = permit;
                    if let Err(e) = session(&child, id, socket, idle).await {
                        Log::new(Some(&child.status_tx))
                            .warn(format!("9P connection {id} ended: {e}"));
                    }
                    child
                        .state
                        .update_connection_status(server_id, id, ConnectionStatus::Closed)
                        .await;
                    let _ = child.status_tx.send("__UPDATE_UI__".into());
                })
                .await;
        }
    });
    state.register_server_task(server_id, accept).await;
    Ok(addr)
}

/// What a fid names, and what an open fid has fetched.
struct Fid {
    path: String,
    qid: Qid,
    uname: String,
    open: Option<u8>,
    remove_on_clunk: bool,
    /// File content from the last read at offset 0.
    content: Option<Vec<u8>>,
    /// Packed directory entries from the last read at offset 0, and the paging cursor.
    entries: Vec<Vec<u8>>,
    next_index: usize,
    next_offset: u64,
}

impl Fid {
    fn new(path: String, qid: Qid, uname: String) -> Self {
        Self {
            path,
            qid,
            uname,
            open: None,
            remove_on_clunk: false,
            content: None,
            entries: Vec::new(),
            next_index: 0,
            next_offset: 0,
        }
    }
}

fn root_qid() -> Qid {
    Qid {
        kind: wire::QTDIR,
        version: 0,
        path: wire::qid_path("/"),
    }
}

fn qid_of(path: &str, entry: &Value) -> Qid {
    Qid {
        kind: if entry["kind"] == "dir" {
            wire::QTDIR
        } else {
            wire::QTFILE
        },
        version: entry["mtime"].as_u64().unwrap_or(0) as u32,
        path: wire::qid_path(path),
    }
}

fn stat_of(path: &str, name: &str, entry: &Value) -> Stat {
    let dir = entry["kind"] == "dir";
    let perm = entry["mode"]
        .as_u64()
        .unwrap_or(if dir { 0o755 } else { 0o644 }) as u32;
    let owner = entry["owner"].as_str().unwrap_or(DEFAULT_OWNER).to_string();
    let mtime = entry["mtime"].as_u64().unwrap_or(0) as u32;
    Stat {
        kind: 0,
        dev: 0,
        qid: qid_of(path, entry),
        mode: perm | if dir { wire::DMDIR } else { 0 },
        atime: mtime,
        mtime,
        length: if dir {
            0
        } else {
            entry["size"].as_u64().unwrap_or(0)
        },
        name: name.to_string(),
        uid: owner.clone(),
        gid: owner,
        muid: DEFAULT_OWNER.to_string(),
    }
}

/// A request the handler answered, refused, or could not answer.
enum Answer {
    /// The action name and its data.
    Given(String, Value),
    /// Rerror text to send.
    Refused(String),
}

fn outcome(ctx: &SpawnContext, id: ConnectionId, operation: &str, decision: &str) {
    let summary = format!("9P connection {id} operation={operation} decision={decision}");
    let log = Log::new(Some(&ctx.status_tx));
    if decision.starts_with("fail_closed_") || decision == "model_silent" {
        log.error(summary);
    } else {
        log.info(summary);
    }
}

async fn ask(
    ctx: &SpawnContext,
    id: ConnectionId,
    event_type: &'static EventType,
    data: Value,
    expected: &[&str],
) -> Answer {
    let op = event_type.id.clone();
    let generic = crate::utils::WireFailure::Unavailable
        .prefixed_text()
        .to_string();
    let event = Event::new(event_type, data);
    let result = match call_llm(
        &ctx.llm_client,
        &ctx.state,
        ctx.server_id,
        Some(id),
        &event,
        &actions::NinepProtocol,
    )
    .await
    {
        Ok(result) => result,
        Err(error) => {
            outcome(ctx, id, &op, "fail_closed_llm_error");
            return Answer::Refused(
                crate::utils::wire_failure::prefixed_wire_failure_text(&error).to_string(),
            );
        }
    };
    if !result.failures.is_empty() {
        outcome(ctx, id, &op, "fail_closed_invalid_reply");
        return Answer::Refused(generic);
    }
    let mut found = Vec::new();
    let mut pending = result.protocol_results;
    while let Some(result) = pending.pop() {
        match result {
            ActionResult::Custom { name, data } if name.starts_with("ninep_") => {
                found.push((name, data))
            }
            ActionResult::Multiple(items) => pending.extend(items),
            _ => {}
        }
    }
    match found.len() {
        0 => {
            outcome(ctx, id, &op, "model_silent");
            Answer::Refused(generic)
        }
        1 => {
            let (name, data) = found.pop().unwrap_or_default();
            if name == "ninep_error" {
                outcome(ctx, id, &op, "model_reject");
                Answer::Refused(data["message"].as_str().unwrap_or("refused").to_string())
            } else if expected.contains(&name.as_str()) {
                outcome(ctx, id, &op, "model_answer");
                Answer::Given(name, data)
            } else {
                outcome(ctx, id, &op, "fail_closed_invalid_reply");
                Answer::Refused(generic)
            }
        }
        _ => {
            outcome(ctx, id, &op, "fail_closed_invalid_reply");
            Answer::Refused(generic)
        }
    }
}

/// Ask whether `path` exists. `Ok(entry)` or `Err(Rerror text)`.
async fn lookup(
    ctx: &SpawnContext,
    id: ConnectionId,
    path: &str,
    uname: &str,
) -> std::result::Result<Value, String> {
    if path == "/" {
        return Ok(json!({"kind": "dir"}));
    }
    match ask(
        ctx,
        id,
        &actions::STAT_EVENT,
        json!({"path": path, "uname": uname}),
        &["ninep_entry", "ninep_not_found"],
    )
    .await
    {
        Answer::Given(name, data) if name == "ninep_entry" => Ok(data),
        Answer::Given(..) => Err("file does not exist".into()),
        Answer::Refused(text) => Err(text),
    }
}

/// Ask for a change; `Ok(())` when accepted.
async fn change(
    ctx: &SpawnContext,
    id: ConnectionId,
    event_type: &'static EventType,
    data: Value,
) -> std::result::Result<(), String> {
    match ask(ctx, id, event_type, data, &["ninep_ok"]).await {
        Answer::Given(..) => Ok(()),
        Answer::Refused(text) => Err(text),
    }
}

struct Conn<'a> {
    ctx: &'a SpawnContext,
    id: ConnectionId,
    msize: u32,
    negotiated: bool,
    fids: HashMap<u32, Fid>,
}

fn rerror(tag: u16, text: &str) -> Vec<u8> {
    let text: String = text.chars().take(255).collect();
    Writer::new().string(&text).finish(wire::RERROR, tag)
}

impl Conn<'_> {
    fn iounit(&self) -> u32 {
        self.msize - wire::IOHDRSZ
    }

    fn fid(&mut self, fid: u32) -> std::result::Result<&mut Fid, String> {
        self.fids
            .get_mut(&fid)
            .ok_or_else(|| "unknown fid".to_string())
    }

    fn insert(&mut self, fid: u32, value: Fid) -> std::result::Result<(), String> {
        if fid == wire::NOFID {
            return Err("invalid fid".into());
        }
        if !self.fids.contains_key(&fid) && self.fids.len() >= wire::MAX_FIDS {
            return Err("too many fids".into());
        }
        self.fids.insert(fid, value);
        Ok(())
    }

    /// Answer one T-message. `Err` only for a malformed message, which ends the connection.
    async fn handle(&mut self, kind: u8, tag: u16, body: &[u8]) -> Result<Vec<u8>> {
        let mut r = Reader::new(body);
        if kind == wire::TVERSION {
            let msize = r.u32()?;
            let version = r.string()?;
            anyhow::ensure!(msize >= wire::MIN_MSIZE, "msize {msize} is too small");
            self.msize = msize.min(wire::MAX_MSIZE);
            self.fids.clear();
            let reply = if version.starts_with(wire::VERSION) {
                self.negotiated = true;
                wire::VERSION
            } else {
                "unknown"
            };
            return Ok(Writer::new()
                .u32(self.msize)
                .string(reply)
                .finish(wire::RVERSION, tag));
        }
        if !self.negotiated {
            return Ok(rerror(tag, "version not negotiated"));
        }
        let reply = match kind {
            wire::TAUTH => Err("authentication not required".to_string()),
            wire::TATTACH => {
                let fid = r.u32()?;
                let _afid = r.u32()?;
                let uname = r.string()?;
                let _aname = r.string()?;
                if self.fids.contains_key(&fid) {
                    Err("fid already in use".into())
                } else {
                    self.insert(fid, Fid::new("/".into(), root_qid(), uname))
                        .map(|_| Writer::new().qid(&root_qid()).finish(wire::RATTACH, tag))
                }
            }
            wire::TFLUSH => {
                // Requests are answered in order, so whatever was flushed has been answered.
                r.u16()?;
                Ok(Writer::new().finish(wire::RFLUSH, tag))
            }
            wire::TWALK => self.walk(tag, &mut r).await?,
            wire::TOPEN => {
                let fid = r.u32()?;
                let mode = r.u8()?;
                self.open(tag, fid, mode).await
            }
            wire::TCREATE => {
                let fid = r.u32()?;
                let name = r.string()?;
                let perm = r.u32()?;
                let mode = r.u8()?;
                self.create(tag, fid, &name, perm, mode).await
            }
            wire::TREAD => {
                let fid = r.u32()?;
                let offset = r.u64()?;
                let count = r.u32()?;
                self.read(tag, fid, offset, count).await
            }
            wire::TWRITE => {
                let fid = r.u32()?;
                let offset = r.u64()?;
                let count = r.u32()? as usize;
                let data = r.take(count)?;
                self.write(tag, fid, offset, data).await
            }
            wire::TCLUNK => {
                let fid = r.u32()?;
                match self.fids.remove(&fid) {
                    Some(f) => {
                        if f.remove_on_clunk {
                            let _ = change(
                                self.ctx,
                                self.id,
                                &actions::REMOVE_EVENT,
                                json!({"path": f.path, "uname": f.uname}),
                            )
                            .await;
                        }
                        Ok(Writer::new().finish(wire::RCLUNK, tag))
                    }
                    None => Err("unknown fid".into()),
                }
            }
            wire::TREMOVE => {
                let fid = r.u32()?;
                // The fid is clunked whether or not the remove succeeds.
                match self.fids.remove(&fid) {
                    Some(f) if f.path == "/" => Err("cannot remove the root".into()),
                    Some(f) => change(
                        self.ctx,
                        self.id,
                        &actions::REMOVE_EVENT,
                        json!({"path": f.path, "uname": f.uname}),
                    )
                    .await
                    .map(|_| Writer::new().finish(wire::RREMOVE, tag)),
                    None => Err("unknown fid".into()),
                }
            }
            wire::TSTAT => {
                let fid = r.u32()?;
                self.stat(tag, fid).await
            }
            wire::TWSTAT => {
                let fid = r.u32()?;
                let _len = r.u16()?;
                let stat = r.stat()?;
                self.wstat(tag, fid, stat).await
            }
            other => Err(format!("unknown message type {other}")),
        };
        Ok(reply.unwrap_or_else(|text| rerror(tag, &text)))
    }

    async fn walk(
        &mut self,
        tag: u16,
        r: &mut Reader<'_>,
    ) -> Result<std::result::Result<Vec<u8>, String>> {
        let fid = r.u32()?;
        let newfid = r.u32()?;
        let count = r.u16()? as usize;
        anyhow::ensure!(count <= wire::MAX_WELEM, "walk of {count} elements");
        let mut names = Vec::with_capacity(count);
        for _ in 0..count {
            names.push(r.string()?);
        }
        let (start, uname, open) = match self.fids.get(&fid) {
            Some(f) => (f.path.clone(), f.uname.clone(), f.open.is_some()),
            None => return Ok(Err("unknown fid".into())),
        };
        if open {
            return Ok(Err("cannot walk an open fid".into()));
        }
        if newfid != fid && self.fids.contains_key(&newfid) {
            return Ok(Err("fid already in use".into()));
        }
        let mut path = start.clone();
        let mut qid = match self.fids.get(&fid) {
            Some(f) => f.qid,
            None => root_qid(),
        };
        let mut qids = Vec::new();
        for name in &names {
            if !qid.is_dir() {
                break;
            }
            let next = if name == ".." {
                wire::parent(&path)
            } else if let Err(e) = wire::check_name(name) {
                if qids.is_empty() {
                    return Ok(Err(e.to_string()));
                }
                break;
            } else {
                wire::join(&path, name)
            };
            match lookup(self.ctx, self.id, &next, &uname).await {
                Ok(entry) => {
                    qid = if next == "/" {
                        root_qid()
                    } else {
                        qid_of(&next, &entry)
                    };
                    path = next;
                    qids.push(qid);
                }
                Err(text) if qids.is_empty() => return Ok(Err(text)),
                Err(_) => break,
            }
        }
        if !names.is_empty() && qids.is_empty() {
            return Ok(Err("not a directory".into()));
        }
        if qids.len() == names.len() {
            if let Err(e) = self.insert(newfid, Fid::new(path, qid, uname)) {
                return Ok(Err(e));
            }
        }
        let mut w = Writer::new().u16(qids.len() as u16);
        for q in &qids {
            w = w.qid(q);
        }
        Ok(Ok(w.finish(wire::RWALK, tag)))
    }

    async fn open(&mut self, tag: u16, fid: u32, mode: u8) -> std::result::Result<Vec<u8>, String> {
        let iounit = self.iounit();
        let (ctx, id) = (self.ctx, self.id);
        let f = self.fid(fid)?;
        if f.open.is_some() {
            return Err("fid already open".into());
        }
        let access = mode & 3;
        if f.qid.is_dir()
            && (access == wire::OWRITE || access == wire::ORDWR || mode & wire::OTRUNC != 0)
        {
            return Err("is a directory".into());
        }
        if mode & wire::OTRUNC != 0 {
            change(
                ctx,
                id,
                &actions::WSTAT_EVENT,
                json!({"path": f.path, "length": 0, "uname": f.uname}),
            )
            .await?;
        }
        f.open = Some(access);
        f.remove_on_clunk = mode & wire::ORCLOSE != 0;
        Ok(Writer::new()
            .qid(&f.qid)
            .u32(iounit)
            .finish(wire::ROPEN, tag))
    }

    async fn create(
        &mut self,
        tag: u16,
        fid: u32,
        name: &str,
        perm: u32,
        mode: u8,
    ) -> std::result::Result<Vec<u8>, String> {
        let iounit = self.iounit();
        let (ctx, id) = (self.ctx, self.id);
        wire::check_name(name).map_err(|e| e.to_string())?;
        if name == ".." {
            return Err("invalid name".into());
        }
        let f = self.fid(fid)?;
        if !f.qid.is_dir() || f.open.is_some() {
            return Err("create needs an unopened directory fid".into());
        }
        let dir = perm & wire::DMDIR != 0;
        let path = wire::join(&f.path, name);
        change(
            ctx,
            id,
            &actions::CREATE_EVENT,
            json!({"path": path, "kind": if dir {"dir"} else {"file"}, "mode": perm & 0o777, "uname": f.uname}),
        )
        .await?;
        let entry = json!({"kind": if dir {"dir"} else {"file"}});
        f.qid = qid_of(&path, &entry);
        f.path = path;
        f.open = Some(mode & 3);
        f.remove_on_clunk = mode & wire::ORCLOSE != 0;
        f.content = Some(Vec::new());
        Ok(Writer::new()
            .qid(&f.qid)
            .u32(iounit)
            .finish(wire::RCREATE, tag))
    }

    async fn read(
        &mut self,
        tag: u16,
        fid: u32,
        offset: u64,
        count: u32,
    ) -> std::result::Result<Vec<u8>, String> {
        let count = count.min(self.iounit()) as usize;
        let (ctx, id) = (self.ctx, self.id);
        let f = self.fid(fid)?;
        match f.open {
            Some(wire::OREAD | wire::ORDWR | wire::OEXEC) => {}
            _ => return Err("fid not open for reading".into()),
        }
        if f.qid.is_dir() {
            if offset == 0 {
                let listing = match ask(
                    ctx,
                    id,
                    &actions::LIST_EVENT,
                    json!({"path": f.path, "uname": f.uname}),
                    &["ninep_listing"],
                )
                .await
                {
                    Answer::Given(_, data) => data,
                    Answer::Refused(text) => return Err(text),
                };
                f.entries = listing["entries"]
                    .as_array()
                    .map(|entries| {
                        entries
                            .iter()
                            .map(|e| {
                                let name = e["name"].as_str().unwrap_or_default();
                                wire::encode_stat(&stat_of(&wire::join(&f.path, name), name, e))
                            })
                            .collect()
                    })
                    .unwrap_or_default();
                f.next_index = 0;
                f.next_offset = 0;
            } else if offset != f.next_offset {
                return Err("bad offset in directory read".into());
            }
            let mut data = Vec::new();
            while let Some(entry) = f.entries.get(f.next_index) {
                if data.len() + entry.len() > count {
                    if data.is_empty() {
                        return Err("read count too small for a directory entry".into());
                    }
                    break;
                }
                data.extend_from_slice(entry);
                f.next_index += 1;
            }
            f.next_offset += data.len() as u64;
            return Ok(Writer::new()
                .u32(data.len() as u32)
                .bytes(&data)
                .finish(wire::RREAD, tag));
        }
        if offset == 0 || f.content.is_none() {
            let answer = ask(
                ctx,
                id,
                &actions::READ_EVENT,
                json!({"path": f.path, "uname": f.uname}),
                &["ninep_content"],
            )
            .await;
            let data = match answer {
                Answer::Given(_, data) => data,
                Answer::Refused(text) => return Err(text),
            };
            let bytes = wire::from_text(
                data["data"].as_str().unwrap_or_default(),
                data["encoding"].as_str(),
            )
            .map_err(|e| e.to_string())?;
            f.content = Some(bytes);
        }
        let content = f.content.as_deref().unwrap_or_default();
        let start = (offset as usize).min(content.len());
        let end = (start + count).min(content.len());
        let slice = &content[start..end];
        Ok(Writer::new()
            .u32(slice.len() as u32)
            .bytes(slice)
            .finish(wire::RREAD, tag))
    }

    async fn write(
        &mut self,
        tag: u16,
        fid: u32,
        offset: u64,
        data: &[u8],
    ) -> std::result::Result<Vec<u8>, String> {
        let (ctx, id) = (self.ctx, self.id);
        let f = self.fid(fid)?;
        match f.open {
            Some(wire::OWRITE | wire::ORDWR) => {}
            _ => return Err("fid not open for writing".into()),
        }
        if f.qid.is_dir() {
            return Err("is a directory".into());
        }
        if offset as usize + data.len() > wire::MAX_CONTENT {
            return Err("file too large".into());
        }
        let (text, encoding) = wire::to_text(data);
        change(
            ctx,
            id,
            &actions::WRITE_EVENT,
            json!({"path": f.path, "offset": offset, "data": text, "encoding": encoding, "uname": f.uname}),
        )
        .await?;
        f.content = None;
        Ok(Writer::new()
            .u32(data.len() as u32)
            .finish(wire::RWRITE, tag))
    }

    async fn stat(&mut self, tag: u16, fid: u32) -> std::result::Result<Vec<u8>, String> {
        let (ctx, id) = (self.ctx, self.id);
        let f = self.fid(fid)?;
        let (path, uname) = (f.path.clone(), f.uname.clone());
        let entry = lookup(ctx, id, &path, &uname).await?;
        let stat = wire::encode_stat(&stat_of(&path, &wire::base(&path), &entry));
        Ok(Writer::new()
            .u16(stat.len() as u16)
            .bytes(&stat)
            .finish(wire::RSTAT, tag))
    }

    async fn wstat(
        &mut self,
        tag: u16,
        fid: u32,
        stat: Stat,
    ) -> std::result::Result<Vec<u8>, String> {
        let (ctx, id) = (self.ctx, self.id);
        let f = self.fid(fid)?;
        let mut event = json!({"path": f.path, "uname": f.uname});
        let mut changed = false;
        if !stat.name.is_empty() && stat.name != wire::base(&f.path) {
            wire::check_name(&stat.name).map_err(|e| e.to_string())?;
            if f.path == "/" || stat.name == ".." {
                return Err("cannot rename this".into());
            }
            event["name"] = json!(stat.name);
            changed = true;
        }
        if stat.length != u64::MAX {
            if f.qid.is_dir() {
                return Err("is a directory".into());
            }
            event["length"] = json!(stat.length);
            changed = true;
        }
        if stat.mode != u32::MAX {
            event["mode"] = json!(stat.mode & 0o777);
            changed = true;
        }
        // A wstat of all "don't touch" values is a sync request: nothing to decide.
        if changed {
            change(ctx, id, &actions::WSTAT_EVENT, event).await?;
            if !stat.name.is_empty() && stat.name != wire::base(&f.path) {
                f.path = wire::join(&wire::parent(&f.path), &stat.name);
                f.qid.path = wire::qid_path(&f.path);
            }
            f.content = None;
        }
        Ok(Writer::new().finish(wire::RWSTAT, tag))
    }
}

async fn session(
    ctx: &SpawnContext,
    id: ConnectionId,
    socket: TcpStream,
    idle: Duration,
) -> Result<()> {
    let (mut reader, mut writer) = tokio::io::split(socket);
    let mut conn = Conn {
        ctx,
        id,
        msize: wire::MAX_MSIZE,
        negotiated: false,
        fids: HashMap::new(),
    };
    loop {
        let Some((kind, tag, body)) = wire::read_message(&mut reader, conn.msize, idle).await?
        else {
            return Ok(());
        };
        ctx.state
            .update_connection_stats(
                ctx.server_id,
                id,
                Some(body.len() as u64 + 7),
                None,
                Some(1),
                None,
            )
            .await;
        let reply = conn.handle(kind, tag, &body).await?;
        tokio::time::timeout(wire::IO_TIMEOUT, writer.write_all(&reply))
            .await
            .context("9P write deadline")??;
        ctx.state
            .update_connection_stats(
                ctx.server_id,
                id,
                None,
                Some(reply.len() as u64),
                None,
                Some(1),
            )
            .await;
    }
}