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
//! Gearman actions: what the model is told, and how its answers become packets.
//!
//! The model is the worker. A submitted job raises `gearman_job_submitted`; the model answers
//! with progress, partial data and exactly one outcome (complete, fail, exception) or an
//! `ERROR`. Every action is rendered by [`super::wire`], so the model supplies a result, two
//! numbers or a text, never a packet.
//!
//! The job handle is NetGet's. An action may name it (`job_handle`, copied from the event), in
//! which case the executor renders the whole packet — that is what an action injected from the
//! dashboard needs. Without it the executor returns the answer structured and the session loop
//! renders it with the handle of the job being answered.

use super::wire;
use crate::llm::actions::{
    protocol_trait::{ActionResult, Protocol, Server},
    ActionDefinition, Parameter,
};
use crate::protocol::log_template::LogTemplate;
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::{anyhow, Context, Result};
use serde_json::{json, Value};
use std::sync::LazyLock;

pub struct GearmanProtocol;

impl GearmanProtocol {
    pub fn new() -> Self {
        Self
    }
}

impl Default for GearmanProtocol {
    fn default() -> Self {
        Self::new()
    }
}

/// The name of the structured result for an answer that did not name its job handle.
pub const WORK_RESULT: &str = "gearman_work";

impl Protocol for GearmanProtocol {
    fn get_startup_parameters(&self) -> Vec<crate::llm::actions::ParameterDefinition> {
        vec![
            crate::llm::actions::ParameterDefinition {
                name: "first_byte_timeout_secs".to_string(),
                type_hint: "number".to_string(),
                description: "Seconds a new connection may send nothing before the server \
                              closes it. Default 300, the window a `manual` rule gives a human - \
                              the peer may be NetGet's own TCP client parked for its operator."
                    .to_string(),
                required: false,
                example: json!(300),
                default: Some(serde_json::json!(super::FIRST_BYTE_TIMEOUT.as_secs())),
            },
            crate::llm::actions::ParameterDefinition {
                name: "idle_timeout_secs".to_string(),
                type_hint: "number".to_string(),
                description: "Seconds the server waits for the next packet or admin line after \
                              answering one. Default 300. A client waiting for its job's result \
                              is not idle: the deadline wraps reads, not the model's answer."
                    .to_string(),
                required: false,
                example: json!(300),
                default: Some(serde_json::json!(super::IDLE_TIMEOUT.as_secs())),
            },
        ]
    }
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        Vec::new()
    }
    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        vec![
            complete_action(),
            fail_action(),
            status_action(),
            data_action(),
            exception_action(),
            error_action(),
            close_connection_action(),
        ]
    }
    fn protocol_name(&self) -> &'static str {
        "Gearman"
    }
    fn get_event_types(&self) -> Vec<EventType> {
        vec![GEARMAN_JOB_SUBMITTED_EVENT.clone()]
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>Gearman"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec!["gearman", "gearmand", "job server", "gearman worker"]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{
            DevelopmentState, PrivilegeRequirement, ProtocolMetadataV2,
        };

        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Beta)
            .well_known_port(4730)
            // 4730 is unprivileged, and so is every port a test picks.
            .privilege_requirement(PrivilegeRequirement::None)
            .implementation(
                "Hand-written Gearman job server over tokio TCP: the binary \\0REQ/\\0RES \
                 packet protocol (client side of the server) and the text admin protocol, both \
                 rendered by NetGet; job handles generated by NetGet",
            )
            .llm_control(
                "The worker: for every submitted job, progress (WORK_STATUS), partial output \
                 (WORK_DATA) and the outcome - WORK_COMPLETE with a result, WORK_FAIL, \
                 WORK_EXCEPTION, or ERROR",
            )
            .e2e_testing(
                "tests/server/gearman/real_client_test.rs drives the gearmand project's own \
                 gearman(1) client (libgearman, C++; Homebrew `gearman`, Ubuntu \
                 `gearman-tools`) through foreground jobs at normal and high priority, a \
                 background job and --ping, and gearadmin through --status, --workers and \
                 --server-version, asserting on what they printed and how they exited, with \
                 Wireshark's gearman dissector reading a recorded exchange. It fails, never \
                 skips, when the binaries are absent. tests/server/gearman/e2e_test.rs covers \
                 the mocked-model path on a raw socket.",
            )
            .notes(
                "Implements SUBMIT_JOB and its _BG, _HIGH, _HIGH_BG, _LOW and _LOW_BG variants \
                 (JOB_CREATED, then WORK_STATUS/WORK_DATA and one of WORK_COMPLETE, WORK_FAIL, \
                 WORK_EXCEPTION, or ERROR for foreground jobs; nothing after JOB_CREATED for \
                 background ones), GET_STATUS against the jobs currently in \
                 flight, ECHO_REQ, OPTION_REQ (only `exceptions`) and SET_CLIENT_ID. The admin \
                 protocol answers status (jobs in flight per function), workers (none: the \
                 model is not a worker connection) and version; maxqueue and shutdown are \
                 refused. Worker packets (CAN_DO, GRAB_JOB, ...) are refused with ERROR and \
                 the connection closed: the model is the worker. SUBMIT_JOB_SCHED/EPOCH and \
                 reduce jobs answer ERROR. Packets are capped at 1 MiB, judged from the \
                 declared size before allocation. On backend failure, no answer, or an answer \
                 that does not fit the job, a foreground job gets WORK_FAIL (WORK_EXCEPTION \
                 with a fixed text when the client enabled exceptions).",
            )
            .max_inbound_bytes(wire::MAX_PACKET_BYTES)
            // WORK_FAIL for the job, which every Gearman client reports as a failed job.
            .answers_on_failure()
            .build()
    }
    fn description(&self) -> &'static str {
        "Gearman job server - the model is the worker for every function"
    }
    fn example_prompt(&self) -> &'static str {
        "Gearman job server on port 4730 - the reverse function returns its input reversed"
    }
    fn group_name(&self) -> &'static str {
        "Application"
    }
    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;
        StartupExamples::new(
            json!({
                "type": "open_server",
                "port": 4730,
                "base_stack": "gearman",
                "instruction": "Gearman job server. The function 'reverse' returns its \
                                workload reversed; the function 'wordcount' returns the number \
                                of words. Fail any other function."
            }),
            json!({
                "type": "open_server",
                "port": 4730,
                "base_stack": "gearman",
                "event_handlers": [{
                    "event_pattern": "gearman_job_submitted",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "import json, sys\nevent = json.load(sys.stdin)['event']\nif event.get('function') == 'reverse':\n    actions = [{'type': 'complete_gearman_job', 'result': event.get('workload', '')[::-1]}]\nelse:\n    actions = [{'type': 'fail_gearman_job'}]\nprint(json.dumps({'actions': actions}))"
                    }
                }]
            }),
            json!({
                "type": "open_server",
                "port": 4730,
                "base_stack": "gearman",
                "event_handlers": [{
                    "event_pattern": "gearman_job_submitted",
                    "handler": {
                        "type": "static",
                        "actions": [
                            {"type": "send_gearman_status", "numerator": 1, "denominator": 2},
                            {"type": "complete_gearman_job", "result": "done"}
                        ]
                    }
                }]
            }),
        )
    }
}

impl Server for GearmanProtocol {
    fn spawn(
        &self,
        ctx: crate::protocol::SpawnContext,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
    > {
        Box::pin(async move {
            let secs = |name: &str| -> anyhow::Result<Option<u64>> {
                Ok(ctx
                    .startup_params
                    .as_ref()
                    .map(|p| p.get_optional_u64(name))
                    .transpose()?
                    .flatten())
            };
            let first_byte_timeout_secs = secs("first_byte_timeout_secs")?;
            let idle_timeout_secs = secs("idle_timeout_secs")?;

            crate::server::gearman::GearmanServer::spawn_with_llm_actions(
                ctx.legacy_listen_addr(),
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.server_id,
                first_byte_timeout_secs,
                idle_timeout_secs,
            )
            .await
        })
    }

    fn execute_action(&self, action: Value) -> Result<ActionResult> {
        let action_type = action
            .get("type")
            .and_then(|v| v.as_str())
            .context("Missing 'type' field in action")?;

        let work = match action_type {
            "complete_gearman_job" => Work::Complete(text(&action, "result")?.into_bytes()),
            "fail_gearman_job" => Work::Fail,
            "send_gearman_status" => {
                let numerator = number(&action, "numerator")?;
                let denominator = number(&action, "denominator")?;
                if numerator > denominator {
                    return Err(anyhow!(
                        "numerator ({numerator}) must not exceed denominator ({denominator})"
                    ));
                }
                Work::Status(numerator, denominator)
            }
            "send_gearman_data" => Work::Data(text(&action, "data")?.into_bytes()),
            "send_gearman_exception" => Work::Exception(text(&action, "text")?.into_bytes()),
            "send_gearman_error" => {
                let code = action
                    .get("code")
                    .and_then(Value::as_str)
                    .context("send_gearman_error needs 'code'")?;
                let text = action.get("text").and_then(Value::as_str).unwrap_or("");
                return Ok(ActionResult::Output(
                    wire::error(code, text).map_err(|e| anyhow!("send_gearman_error: {e}"))?,
                ));
            }
            "close_connection" => return Ok(ActionResult::CloseConnection),
            _ => return Err(anyhow!("Unknown Gearman action: {}", action_type)),
        };
        match action.get("job_handle").and_then(Value::as_str) {
            Some(handle) if !handle.is_empty() => {
                Ok(ActionResult::Output(work.render(handle.as_bytes())))
            }
            _ => Ok(ActionResult::Custom {
                name: WORK_RESULT.to_string(),
                data: work.to_json(),
            }),
        }
    }
}

/// One answer to a job, before it has a handle.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Work {
    Status(u64, u64),
    Data(Vec<u8>),
    Complete(Vec<u8>),
    Fail,
    Exception(Vec<u8>),
}

impl Work {
    pub fn render(&self, handle: &[u8]) -> Vec<u8> {
        match self {
            Work::Status(n, d) => wire::work_status(handle, *n, *d),
            Work::Data(data) => wire::work_data(handle, data),
            Work::Complete(result) => wire::work_complete(handle, result),
            Work::Fail => wire::work_fail(handle),
            Work::Exception(text) => wire::work_exception(handle, text),
        }
    }

    fn to_json(&self) -> Value {
        let text = |b: &[u8]| String::from_utf8_lossy(b).into_owned();
        match self {
            Work::Status(n, d) => json!({"kind": "status", "numerator": n, "denominator": d}),
            Work::Data(data) => json!({"kind": "data", "data": text(data)}),
            Work::Complete(result) => json!({"kind": "complete", "result": text(result)}),
            Work::Fail => json!({"kind": "fail"}),
            Work::Exception(t) => json!({"kind": "exception", "text": text(t)}),
        }
    }

    /// Read back what [`Work::to_json`] produced.
    pub fn from_json(value: &Value) -> Option<Work> {
        let s = |k: &str| {
            value
                .get(k)
                .and_then(Value::as_str)
                .map(|s| s.as_bytes().to_vec())
        };
        Some(match value.get("kind")?.as_str()? {
            "status" => Work::Status(
                value.get("numerator")?.as_u64()?,
                value.get("denominator")?.as_u64()?,
            ),
            "data" => Work::Data(s("data")?),
            "complete" => Work::Complete(s("result")?),
            "fail" => Work::Fail,
            "exception" => Work::Exception(s("text")?),
            _ => return None,
        })
    }
}

fn text(action: &Value, name: &str) -> Result<String> {
    match action.get(name) {
        Some(Value::String(s)) => Ok(s.clone()),
        Some(Value::Number(n)) => Ok(n.to_string()),
        None | Some(Value::Null) => Ok(String::new()),
        Some(_) => Err(anyhow!("'{name}' must be text")),
    }
}

fn number(action: &Value, name: &str) -> Result<u64> {
    action
        .get(name)
        .and_then(|v| {
            v.as_u64()
                .or_else(|| v.as_str().and_then(|s| s.trim().parse().ok()))
        })
        .with_context(|| format!("'{name}' must be a non-negative integer"))
}

fn handle_param() -> Parameter {
    Parameter {
        name: "job_handle".to_string(),
        type_hint: "string".to_string(),
        description: "The job's handle from the event. Optional when answering the event; \
                      needed when sending to a connection from outside an event."
            .to_string(),
        required: false,
    }
}

fn complete_action() -> ActionDefinition {
    ActionDefinition {
        name: "complete_gearman_job".to_string(),
        description: "Finish the job successfully with its result (WORK_COMPLETE). The client \
                      prints the result."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "result".to_string(),
                type_hint: "string".to_string(),
                description: "The job's output as text".to_string(),
                required: true,
            },
            handle_param(),
        ],
        example: json!({"type": "complete_gearman_job", "result": "dlrow olleh"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Gearman WORK_COMPLETE")
                .with_debug("Gearman complete_gearman_job"),
        ),
    }
}

fn fail_action() -> ActionDefinition {
    ActionDefinition {
        name: "fail_gearman_job".to_string(),
        description: "Finish the job as failed (WORK_FAIL), e.g. for a function you do not \
                      provide or a workload you cannot process."
            .to_string(),
        parameters: vec![handle_param()],
        example: json!({"type": "fail_gearman_job"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Gearman WORK_FAIL")
                .with_debug("Gearman fail_gearman_job"),
        ),
    }
}

fn status_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_gearman_status".to_string(),
        description: "Report progress (WORK_STATUS numerator/denominator) before finishing; \
                      follow it with complete_gearman_job or fail_gearman_job in the same \
                      answer."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "numerator".to_string(),
                type_hint: "number".to_string(),
                description: "How much of the job is done, in any unit you choose".to_string(),
                required: true,
            },
            Parameter {
                name: "denominator".to_string(),
                type_hint: "number".to_string(),
                description: "How much work the whole job is, in the same unit as numerator"
                    .to_string(),
                required: true,
            },
            handle_param(),
        ],
        example: json!({"type": "send_gearman_status", "numerator": 1, "denominator": 2}),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Gearman WORK_STATUS {numerator}/{denominator}")
                .with_debug("Gearman send_gearman_status"),
        ),
    }
}

fn data_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_gearman_data".to_string(),
        description: "Send partial output (WORK_DATA) before finishing; the client prints it \
                      ahead of the final result."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "data".to_string(),
                type_hint: "string".to_string(),
                description: "A piece of the job's output as text".to_string(),
                required: true,
            },
            handle_param(),
        ],
        example: json!({"type": "send_gearman_data", "data": "first half, "}),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Gearman WORK_DATA")
                .with_debug("Gearman send_gearman_data"),
        ),
    }
}

fn exception_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_gearman_exception".to_string(),
        description: "Finish the job with an exception message (WORK_EXCEPTION). Clients that \
                      did not ask for exceptions receive WORK_FAIL instead."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "text".to_string(),
                type_hint: "string".to_string(),
                description: "What went wrong".to_string(),
                required: true,
            },
            handle_param(),
        ],
        example: json!({"type": "send_gearman_exception", "text": "image is corrupt"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Gearman WORK_EXCEPTION")
                .with_debug("Gearman send_gearman_exception"),
        ),
    }
}

fn error_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_gearman_error".to_string(),
        description: "Refuse the submission itself with a server ERROR packet instead of \
                      running it."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "code".to_string(),
                type_hint: "string".to_string(),
                description: "An error identifier such as queue_full or unknown_function"
                    .to_string(),
                required: true,
            },
            Parameter {
                name: "text".to_string(),
                type_hint: "string".to_string(),
                description: "One line explaining the error".to_string(),
                required: true,
            },
        ],
        example: json!({"type": "send_gearman_error", "code": "queue_full", "text": "try again later"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Gearman ERROR {code}")
                .with_debug("Gearman send_gearman_error: code={code}"),
        ),
    }
}

fn close_connection_action() -> ActionDefinition {
    ActionDefinition {
        name: "close_connection".to_string(),
        description: "Close the Gearman connection after any reply".to_string(),
        parameters: vec![],
        example: json!({"type": "close_connection"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("Gearman connection closed")
                .with_debug("Gearman close_connection"),
        ),
    }
}

/// `SUBMIT_JOB` and its variants.
pub static GEARMAN_JOB_SUBMITTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "gearman_job_submitted",
        "A client submitted a job. You are the worker: run the function on the workload and \
         answer with complete_gearman_job (optionally preceded by send_gearman_status or \
         send_gearman_data), fail_gearman_job, send_gearman_exception, or send_gearman_error. \
         For a background job the client is not waiting; your answer is only logged.",
        json!({"type": "complete_gearman_job", "result": "dlrow olleh"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "function".to_string(),
            type_hint: "string".to_string(),
            description: "The function the client asked for".to_string(),
            required: true,
        },
        Parameter {
            name: "workload".to_string(),
            type_hint: "string".to_string(),
            description: "The job's input as text (invalid UTF-8 replaced)".to_string(),
            required: true,
        },
        Parameter {
            name: "workload_bytes".to_string(),
            type_hint: "number".to_string(),
            description: "The input's length in bytes".to_string(),
            required: true,
        },
        Parameter {
            name: "unique_id".to_string(),
            type_hint: "string".to_string(),
            description: "The client's unique id for the job (may be empty)".to_string(),
            required: true,
        },
        Parameter {
            name: "priority".to_string(),
            type_hint: "string".to_string(),
            description: "Queue priority the client asked for".to_string(),
            required: true,
        }
        .with_choices(["normal", "high", "low"]),
        Parameter {
            name: "background".to_string(),
            type_hint: "boolean".to_string(),
            description: "true when the client is not waiting for the result".to_string(),
            required: true,
        },
        Parameter {
            name: "job_handle".to_string(),
            type_hint: "string".to_string(),
            description: "The handle NetGet gave the job".to_string(),
            required: true,
        },
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("Gearman job {function} ({workload_bytes} bytes)")
            .with_debug("Gearman gearman_job_submitted: function={function} handle={job_handle}"),
    )
    .with_actions(vec![
        complete_action(),
        fail_action(),
        status_action(),
        data_action(),
        exception_action(),
        error_action(),
        close_connection_action(),
    ])
    .with_alternative_example(json!({"type": "fail_gearman_job"}))
});