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
//! Memcached client: NetGet dials a memcached server and the model drives the text protocol.
//!
//! Three tasks per connection, all registered with the client so `stop_client` ends them:
//!
//! * **transport** owns the socket and the FIFO of requests in flight. It writes requests,
//!   reads replies one whole response at a time (`wire::parse_reply`), and queues each for
//!   the model. It never waits on the model, so a turn parked for a human cannot stall it.
//! * **turns** asks the model about each queued reply, in order, and hands every action it
//!   returns back to the transport. The chain request → reply → model → request continues
//!   through that queue, so it needs no recursion and no depth counter.
//! * **commands** executes actions injected from outside (the dashboard's `[ send ]`, MCP
//!   `send_to_client`) through the same path the model's actions take.

pub mod actions;
pub mod wire;

pub use actions::MemcachedClientProtocol;

use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::Arc;

use anyhow::{anyhow, Context, Result};
use serde_json::json;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::sync::{mpsc, oneshot};
use tracing::{debug, error, info, warn};

use crate::client::llm_budget::call_llm_for_client;
use crate::client::memcached::actions::{
    request_from_action, MEMCACHED_CONNECTED_EVENT, MEMCACHED_COUNTER_EVENT,
    MEMCACHED_DELETED_EVENT, MEMCACHED_ERROR_EVENT, MEMCACHED_EXISTS_EVENT, MEMCACHED_MISS_EVENT,
    MEMCACHED_NOT_FOUND_EVENT, MEMCACHED_NOT_STORED_EVENT, MEMCACHED_OK_EVENT,
    MEMCACHED_STATS_EVENT, MEMCACHED_STORED_EVENT, MEMCACHED_TOUCHED_EVENT, MEMCACHED_VALUE_EVENT,
    MEMCACHED_VERSION_EVENT, REQUEST_RESULT,
};
use crate::client::memcached::wire::{parse_reply, stats_json, Expect, Reply, Request};
use crate::llm::actions::client_trait::{Client, ClientActionResult};
use crate::llm::actions::protocol_trait::Protocol;
use crate::llm::ollama_client::OllamaClient;
use crate::protocol::Event;
use crate::state::app_state::AppState;
use crate::state::client_handles::{ClientCommand, ClientSendOutcome};
use crate::state::{AccessLogOwner, ClientId, ClientStatus};

/// Replies that may wait for the model while a turn is running. The transport never blocks
/// on this queue: when it is full a reply is dropped with `decision=turn_queue_full`.
const TURN_QUEUE_CAPACITY: usize = 256;

/// Requests written and not yet answered. A model that pipelines past this is refused rather
/// than letting the FIFO grow without bound.
const MAX_IN_FLIGHT: usize = 128;

/// Requests waiting for the transport to write them.
const OUTBOUND_CAPACITY: usize = 64;

/// What the turn and command tasks ask the transport to do.
enum Outbound {
    Request {
        request: Request,
        ack: oneshot::Sender<Result<usize, String>>,
    },
    Disconnect {
        ack: oneshot::Sender<()>,
    },
}

/// One thing the model is asked about.
enum Turn {
    Connected,
    Reply { expect: Expect, reply: Reply },
}

/// What one action did.
enum Applied {
    Sent(usize),
    Nothing,
    Disconnect,
}

pub struct MemcachedClient;

impl MemcachedClient {
    pub async fn connect_with_llm_actions(
        remote_addr: String,
        llm_client: OllamaClient,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
        client_id: ClientId,
    ) -> Result<SocketAddr> {
        // A client that lost its target must fail, never guess one.
        if remote_addr.trim().is_empty() {
            return Err(anyhow!(
                "memcached client needs a remote_addr (host:port); refusing to connect \
                 without one"
            ));
        }
        let stream = TcpStream::connect(&remote_addr)
            .await
            .with_context(|| format!("Failed to connect to memcached at {remote_addr}"))?;
        let local_addr = stream.local_addr()?;
        let peer = stream.peer_addr()?;
        info!("Memcached client {client_id} connected to {peer} (local {local_addr})");
        app_state
            .update_client_status(client_id, ClientStatus::Connected)
            .await;
        let _ = status_tx.send(format!("[CLIENT] Memcached client {client_id} connected"));
        let _ = status_tx.send("__UPDATE_UI__".to_string());

        let protocol = Arc::new(MemcachedClientProtocol::new());
        let (outbound_tx, outbound_rx) = mpsc::channel::<Outbound>(OUTBOUND_CAPACITY);
        let (turn_tx, turn_rx) = mpsc::channel::<Turn>(TURN_QUEUE_CAPACITY);

        // Registered before the connected turn can run, so the operator can reach the client
        // while that turn is parked on a manual rule.
        let command_rx =
            crate::client::command_support::register_command_channel(&app_state, client_id).await;
        app_state
            .spawn_client_task(
                client_id,
                command_loop(
                    command_rx,
                    protocol.clone(),
                    outbound_tx.clone(),
                    client_id,
                    app_state.clone(),
                    status_tx.clone(),
                ),
            )
            .await;

        let turn_abort = app_state
            .spawn_client_task(
                client_id,
                run_turns(
                    turn_rx,
                    outbound_tx,
                    protocol,
                    llm_client,
                    app_state.clone(),
                    status_tx.clone(),
                    client_id,
                    peer.to_string(),
                ),
            )
            .await;

        let _ = turn_tx.try_send(Turn::Connected);
        app_state
            .spawn_client_task(
                client_id,
                run_transport(
                    stream,
                    outbound_rx,
                    turn_tx,
                    app_state.clone(),
                    status_tx,
                    client_id,
                    turn_abort,
                ),
            )
            .await;

        Ok(local_addr)
    }
}

/// Own the socket for the life of the connection.
async fn run_transport(
    stream: TcpStream,
    mut outbound_rx: mpsc::Receiver<Outbound>,
    turn_tx: mpsc::Sender<Turn>,
    app_state: Arc<AppState>,
    status_tx: mpsc::UnboundedSender<String>,
    client_id: ClientId,
    turn_abort: tokio::task::AbortHandle,
) {
    let (mut reader, mut writer) = tokio::io::split(stream);
    let mut pending: VecDeque<Expect> = VecDeque::new();
    let mut buf: Vec<u8> = Vec::with_capacity(16 * 1024);
    let mut chunk = vec![0u8; 16 * 1024];

    let status = loop {
        tokio::select! {
            read = reader.read(&mut chunk) => {
                let n = match read {
                    Ok(0) => {
                        info!("Memcached client {client_id} closed by server");
                        break ClientStatus::Disconnected;
                    }
                    Ok(n) => n,
                    Err(e) => {
                        error!("Memcached client {client_id} read error: {e}");
                        break ClientStatus::Error(e.to_string());
                    }
                };
                buf.extend_from_slice(&chunk[..n]);
                if let Err(e) = drain_replies(&mut buf, &mut pending, &turn_tx, client_id) {
                    error!("Memcached client {client_id} protocol error: {e}");
                    break ClientStatus::Error(e);
                }
            }
            out = outbound_rx.recv() => match out {
                None => break ClientStatus::Disconnected,
                Some(Outbound::Disconnect { ack }) => {
                    let _ = writer.shutdown().await;
                    let _ = ack.send(());
                    info!("Memcached client {client_id} disconnected on request");
                    break ClientStatus::Disconnected;
                }
                Some(Outbound::Request { request, ack }) => {
                    if pending.len() >= MAX_IN_FLIGHT {
                        let _ = ack.send(Err(format!(
                            "{MAX_IN_FLIGHT} requests are already waiting for a reply"
                        )));
                        continue;
                    }
                    let bytes = request.encode();
                    let written = async {
                        writer.write_all(&bytes).await?;
                        writer.flush().await
                    }
                    .await;
                    match written {
                        Ok(()) => {
                            debug!(
                                "Memcached client {client_id} sent {} ({} bytes)",
                                request.command(),
                                bytes.len()
                            );
                            pending.push_back(request.expect());
                            let _ = ack.send(Ok(bytes.len()));
                        }
                        Err(e) => {
                            let _ = ack.send(Err(format!("write failed: {e}")));
                            error!("Memcached client {client_id} write error: {e}");
                            break ClientStatus::Error(e.to_string());
                        }
                    }
                }
            }
        }
    };

    turn_abort.abort();
    app_state.remove_client_handle(client_id).await;
    app_state.update_client_status(client_id, status).await;
    let _ = status_tx.send(format!(
        "[CLIENT] Memcached client {client_id} disconnected"
    ));
    let _ = status_tx.send("__UPDATE_UI__".to_string());
}

/// Take every complete reply off the front of `buf`, pairing each with its request.
fn drain_replies(
    buf: &mut Vec<u8>,
    pending: &mut VecDeque<Expect>,
    turn_tx: &mpsc::Sender<Turn>,
    client_id: ClientId,
) -> Result<(), String> {
    while !buf.is_empty() {
        let Some(expect) = pending.front() else {
            return Err(format!(
                "server sent {} octets with no request in flight",
                buf.len()
            ));
        };
        match parse_reply(buf, expect).map_err(|e| e.to_string())? {
            None => return Ok(()),
            Some((reply, used)) => {
                buf.drain(..used);
                let expect = pending.pop_front().expect("front() was Some");
                if turn_tx.try_send(Turn::Reply { expect, reply }).is_err() {
                    warn!(
                        "Memcached client {client_id} dropped a reply: the model is \
                         {TURN_QUEUE_CAPACITY} replies behind decision=turn_queue_full"
                    );
                }
            }
        }
    }
    Ok(())
}

/// The events one reply becomes. A `get` becomes one per requested key, in request order.
fn events_for(expect: &Expect, reply: Reply) -> Vec<Event> {
    use crate::utils::sanitize::line_field;
    match (expect, reply) {
        (Expect::Values { keys, .. }, Reply::Values { items, .. }) => keys
            .iter()
            .map(|key| match items.iter().find(|i| &i.key == key) {
                None => Event::new(&MEMCACHED_MISS_EVENT, json!({ "key": key })),
                Some(item) => match std::str::from_utf8(&item.data) {
                    Ok(text) => {
                        let mut data = json!({
                            "key": key,
                            "value": text,
                            "flags": item.flags,
                        });
                        if let Some(cas) = item.cas {
                            data["cas"] = json!(cas);
                        }
                        Event::new(&MEMCACHED_VALUE_EVENT, data)
                    }
                    Err(_) => Event::new(
                        &MEMCACHED_ERROR_EVENT,
                        json!({
                            "kind": "non_text_value",
                            "message": format!(
                                "the value stored under {key:?} is {} bytes that are not \
                                 valid UTF-8; this client carries values as text only and \
                                 does not re-encode them",
                                item.data.len()
                            ),
                            "command": "get",
                            "key": key,
                        }),
                    ),
                },
            })
            .collect(),
        (Expect::Stats { group }, Reply::Stats { entries }) => {
            let mut data = json!({ "stats": stats_json(&entries) });
            if let Some(g) = group {
                data["group"] = json!(g);
            }
            vec![Event::new(&MEMCACHED_STATS_EVENT, data)]
        }
        (expect, Reply::Error { kind, message }) => {
            let (command, key) = match expect {
                Expect::Values { keys, with_cas } => (
                    if *with_cas { "gets" } else { "get" },
                    keys.first().cloned(),
                ),
                Expect::Status { command, key } => (*command, key.clone()),
                Expect::Stats { .. } => ("stats", None),
            };
            let mut data = json!({
                "kind": kind,
                "message": line_field(&message),
                "command": command,
            });
            if let Some(k) = key {
                data["key"] = json!(k);
            }
            vec![Event::new(&MEMCACHED_ERROR_EVENT, data)]
        }
        (Expect::Status { command, key }, Reply::Status { line }) => {
            let key = key.clone().unwrap_or_default();
            let command = *command;
            let event = match (command, line.as_str()) {
                (_, "STORED") => Event::new(
                    &MEMCACHED_STORED_EVENT,
                    json!({"command": command, "key": key}),
                ),
                (_, "NOT_STORED") => Event::new(
                    &MEMCACHED_NOT_STORED_EVENT,
                    json!({"command": command, "key": key}),
                ),
                (_, "EXISTS") => Event::new(
                    &MEMCACHED_EXISTS_EVENT,
                    json!({"command": command, "key": key}),
                ),
                (_, "NOT_FOUND") => Event::new(
                    &MEMCACHED_NOT_FOUND_EVENT,
                    json!({"command": command, "key": key}),
                ),
                ("delete", "DELETED") => Event::new(&MEMCACHED_DELETED_EVENT, json!({"key": key})),
                ("touch", "TOUCHED") => Event::new(&MEMCACHED_TOUCHED_EVENT, json!({"key": key})),
                ("flush_all", "OK") => Event::new(&MEMCACHED_OK_EVENT, json!({"command": command})),
                ("version", line) if line.starts_with("VERSION ") => Event::new(
                    &MEMCACHED_VERSION_EVENT,
                    json!({"version": line_field(&line["VERSION ".len()..])}),
                ),
                ("incr" | "decr", line) if line.parse::<u64>().is_ok() => Event::new(
                    &MEMCACHED_COUNTER_EVENT,
                    json!({
                        "command": command,
                        "key": key,
                        "value": line.parse::<u64>().unwrap_or_default(),
                    }),
                ),
                (_, line) => Event::new(
                    &MEMCACHED_ERROR_EVENT,
                    json!({
                        "kind": "unexpected_reply",
                        "message": format!(
                            "the server answered {:?}, which is not a reply to {command}",
                            line_field(line)
                        ),
                        "command": command,
                        "key": key,
                    }),
                ),
            };
            vec![event]
        }
        // `parse_reply` only produces a reply of the shape its `Expect` asks for.
        (_, other) => {
            warn!("Memcached reply {other:?} does not match its request {expect:?}");
            Vec::new()
        }
    }
}

/// Answer queued replies with the model, one at a time and in arrival order.
#[allow(clippy::too_many_arguments)]
async fn run_turns(
    mut turn_rx: mpsc::Receiver<Turn>,
    outbound_tx: mpsc::Sender<Outbound>,
    protocol: Arc<MemcachedClientProtocol>,
    llm_client: OllamaClient,
    app_state: Arc<AppState>,
    status_tx: mpsc::UnboundedSender<String>,
    client_id: ClientId,
    remote_addr: String,
) {
    while let Some(turn) = turn_rx.recv().await {
        let events = match turn {
            Turn::Connected => vec![Event::new(
                &MEMCACHED_CONNECTED_EVENT,
                json!({ "remote_addr": remote_addr }),
            )],
            Turn::Reply { expect, reply } => events_for(&expect, reply),
        };
        for event in events {
            let Some(instruction) = app_state.get_instruction_for_client(client_id).await else {
                continue;
            };
            let memory = app_state
                .get_memory_for_client(client_id)
                .await
                .unwrap_or_default();
            match call_llm_for_client(
                &llm_client,
                &app_state,
                client_id.to_string(),
                &instruction,
                &memory,
                Some(&event),
                protocol.as_ref(),
                &status_tx,
            )
            .await
            {
                Ok(result) => {
                    if let Some(mem) = result.memory_updates {
                        app_state.set_memory_for_client(client_id, mem).await;
                    }
                    info!(
                        "Memcached client {client_id} {} decision={} actions={}",
                        event.id(),
                        if result.actions.is_empty() {
                            "model_silent"
                        } else {
                            "model_actions"
                        },
                        result.actions.len()
                    );
                    for action in result.actions {
                        match apply_action(protocol.as_ref(), &outbound_tx, action).await {
                            Ok(Applied::Disconnect) => return,
                            Ok(_) => {}
                            Err(e) => {
                                error!("Memcached client {client_id} action failed: {e}");
                                let _ = status_tx.send(format!(
                                    "[ERROR] Memcached client {client_id} action failed: {e}"
                                ));
                            }
                        }
                    }
                }
                Err(e) => {
                    error!(
                        "Memcached client {client_id} {} decision=llm_error: {e}",
                        event.id()
                    );
                }
            }
        }
    }
}

/// Execute one action and hand its request to the transport. Shared by the model's turns and
/// injected commands, so both take exactly one path onto the wire.
async fn apply_action(
    protocol: &MemcachedClientProtocol,
    outbound_tx: &mpsc::Sender<Outbound>,
    action: serde_json::Value,
) -> Result<Applied> {
    match protocol.execute_action(action)? {
        ClientActionResult::Custom { name, data } if name == REQUEST_RESULT => {
            let request = request_from_action(&data)?
                .ok_or_else(|| anyhow!("a request action produced no request"))?;
            let (ack, done) = oneshot::channel();
            outbound_tx
                .send(Outbound::Request { request, ack })
                .await
                .map_err(|_| anyhow!("the connection is closed"))?;
            let bytes = done
                .await
                .map_err(|_| anyhow!("the connection closed before the request was written"))?
                .map_err(|e| anyhow!(e))?;
            Ok(Applied::Sent(bytes))
        }
        ClientActionResult::Disconnect => {
            let (ack, done) = oneshot::channel();
            if outbound_tx.send(Outbound::Disconnect { ack }).await.is_ok() {
                let _ = done.await;
            }
            Ok(Applied::Disconnect)
        }
        _ => Ok(Applied::Nothing),
    }
}

/// Drain injected commands until the channel closes or one of them disconnects.
async fn command_loop(
    mut command_rx: mpsc::Receiver<ClientCommand>,
    protocol: Arc<MemcachedClientProtocol>,
    outbound_tx: mpsc::Sender<Outbound>,
    client_id: ClientId,
    app_state: Arc<AppState>,
    status_tx: mpsc::UnboundedSender<String>,
) {
    while let Some(command) = command_rx.recv().await {
        let action = command.action.clone();
        let outcome = match apply_action(protocol.as_ref(), &outbound_tx, action.clone()).await {
            Ok(Applied::Sent(bytes_sent)) => ClientSendOutcome::Sent { bytes_sent },
            Ok(Applied::Nothing) => ClientSendOutcome::Executed {
                detail: "executed (nothing to write)".to_string(),
            },
            Ok(Applied::Disconnect) => ClientSendOutcome::Disconnected,
            Err(e) => ClientSendOutcome::Rejected {
                error: e.to_string(),
            },
        };
        app_state
            .record_access_log(
                AccessLogOwner::Client(client_id.as_u32()),
                protocol.protocol_name(),
                None,
                "injected_action",
                action,
                vec![serde_json::to_value(&outcome).unwrap_or_default()],
            )
            .await;
        let disconnect = matches!(outcome, ClientSendOutcome::Disconnected);
        let _ = status_tx.send("__UPDATE_UI__".to_string());
        crate::client::command_support::reply(command, Ok(outcome));
        if disconnect {
            break;
        }
    }
}