Skip to main content

superfluid_daemon/
cli.rs

1//! The `superfluid` command line: its help text, the `serve` flag table and the defaults that let
2//! `superfluid serve <model>` start with no other flag.
3
4use std::path::{Path, PathBuf};
5
6pub const DEFAULT_HTTP: &str = "127.0.0.1:8453";
7
8pub struct Flag {
9    pub name: &'static str,
10    pub value: Option<&'static str>,
11    pub default: &'static str,
12    pub help: &'static str,
13}
14
15pub struct Group {
16    pub title: &'static str,
17    pub flags: &'static [Flag],
18}
19
20const fn f(name: &'static str, value: Option<&'static str>, default: &'static str, help: &'static str) -> Flag {
21    Flag { name, value, default, help }
22}
23
24pub const SERVE: &[Group] = &[
25    Group {
26        title: "Model",
27        flags: &[
28            f("--model", Some("<path|org/model[:tag]>"), "", "A model to serve, repeatable; the first is the default. Also the first bare argument."),
29            f("--runtime", Some("<id>[@<install>]|<model>=<id>"), "by format", "The runtime to serve with (basert, llamacpp, mlx); `auto` clears a pick."),
30            f("--offline", None, "", "Fetch nothing: a model id is only looked for in its runtime's cache."),
31            f("--pull-<name>", Some("[value]"), "", "An option for the runtime that pulls a model id (e.g. --pull-file, --pull-revision)."),
32            f("--model-dir", Some("<dir>"), "", "Every model in this directory is listed and loaded on first use."),
33            f("--idle-timeout", Some("<sec>"), "0 (never)", "Unload a model that has been idle this long (never the default model)."),
34            f("--basert-lib", Some("<path>"), "search", "Where libbaseRT is, for the basert runtime."),
35        ],
36    },
37    Group {
38        title: "Server",
39        flags: &[
40            f("--http", Some("<addr:port>"), DEFAULT_HTTP, "Serve the OpenAI-, Anthropic- and Ollama-compatible HTTP APIs here."),
41            f("--no-http", None, "", "Serve only the native session API on the unix socket."),
42            f("--host", Some("<addr>"), "127.0.0.1", "The HTTP address, as a host..."),
43            f("--port", Some("<N>"), "8453", "...and a port; either fills the other from its default."),
44            f("--socket", Some("<path>"), "$SUPERFLUID_HOME/superfluid.sock", "The unix socket for the native session API."),
45            f("--sessions", Some("<dir>"), "$SUPERFLUID_HOME/sessions", "The session store (write-ahead log, parks, media, files). One server per directory."),
46            f("--api-key", Some("<key>"), "none", "Require `Authorization: Bearer <key>` (or `X-Api-Key`) on HTTP requests."),
47            f("--key-policy", Some("<file.json>"), "", "Per-key classes, rate and concurrency limits; turns authentication on."),
48            f("--rate-limit", Some("<rpm>"), "0 (off)", "Requests per minute per client IP."),
49            f("--drain-timeout", Some("<sec>"), "60", "On SIGUSR1, stop accepting and give in-flight requests this long."),
50            f("--nonstream-keepalive", Some("<sec>"), "0 (off)", "Send 200 at admission and a space every <sec> until a non-streaming body is ready."),
51            f("--web", Some("<127.0.0.1:port>"), "off", "The browser-facing API, with a token minted to <sessions>/web-token. Loopback only."),
52            f("--web-origin", Some("<url>"), "", "An allowed Origin for --web, repeatable."),
53        ],
54    },
55    Group {
56        title: "Generation",
57        flags: &[
58            f("--max-context", Some("<N|auto>"), "auto", "The context window per lane; auto sizes it for the device where the runtime can, else 8192."),
59            f("--max-batch", Some("<N>"), "8", "Concurrent decode lanes."),
60            f("--max-tokens", Some("<N>"), "fill the context", "The generation cap for requests that set none."),
61            f("--kv-bits", Some("<0|4|8|16|84>"), "0 (auto)", "KV cache precision, on runtimes that take it (basert)."),
62            f("--dialect", Some("<auto|chatml|atem|template|raw>"), "auto", "The chat codec."),
63            f("--tool-call-parser", Some("<auto|json|atem|gemma|harmony|glm>"), "auto", "Override the tool-call format the dialect speaks."),
64            f("--temperature", Some("<F>"), "model's", "Default temperature (0..2) for requests that set none."),
65            f("--top-p", Some("<F>"), "model's", "Default nucleus truncation (0..1)."),
66            f("--top-k", Some("<N>"), "model's", "Default top-k truncation."),
67            f("--min-p", Some("<F>"), "model's", "Default min-p truncation (0..1)."),
68            f("--repeat-penalty", Some("<F>"), "model's", "Default repetition penalty (0..2); 1.0 disables it."),
69        ],
70    },
71    Group {
72        title: "Sessions and caching",
73        flags: &[
74            f("--park", None, "off", "Seal a finishing durable session's KV to disk, to resume it later by id. --no-park turns it off."),
75            f("--park-lossy", None, "off", "Park in the lossy Q8 tier (about half the size); implies --park."),
76            f("--park-budget-gb", Some("<N>"), "20", "Disk budget per park directory, oldest evicted first; 0 is unbounded."),
77            f("--sessions-cache", None, "off", "Treat the session log as a cache: start a fresh one at each start."),
78            f("--tool-lease-ms", Some("<N>"), "0 (none)", "A deadline for every tool call."),
79            f("--prefix-blob-budget-mb", Some("<N>"), "engine's", "The recurrent-state snapshot cache on hybrid models (basert)."),
80        ],
81    },
82    Group {
83        title: "Files API",
84        flags: &[
85            f("--files-max-bytes", Some("<N>"), "unbounded", "Refuse an upload that would push stored bytes past this."),
86            f("--files-expiry", Some("<sec>"), "never", "Delete a stored file this long after upload."),
87            f("--files-sweep", Some("<sec>"), "300", "How often the expiry sweep runs."),
88        ],
89    },
90    Group {
91        title: "Scheduling and QoS",
92        flags: &[
93            f("--tick-decode-budget", Some("<N>"), "max-batch x 32", "Decode tokens granted per tick across all lanes."),
94            f("--tick-target-ms", Some("<N>"), "2000", "The tick length decode grants and adaptive prefill are sized towards."),
95            f("--prefill-budget", Some("<N>"), "4096", "Prompt tokens prefilled per tick (64..4096); 0 adapts it to the tick target."),
96            f("--starvation-ticks", Some("<N>"), "8", "Serve an agent lane first after this many ticks without a slice."),
97            f("--class-lanes", Some("<class>=N[,...]"), "uncapped", "Concurrent-lane cap per QoS class."),
98            f("--http-default-qos", Some("<class>"), "agent", "QoS class for HTTP requests that name none (interactive, completion, agent, background)."),
99            f("--no-http-qos-header", None, "", "Ignore the client's x-superfluid-qos header."),
100            f("--http-allow-batch-invariant", None, "", "Let an HTTP request ask for a batch-invariant lane."),
101            f("--worker-process", None, "on", "Run each engine in its own worker process. --no-worker-process runs it in-process."),
102            f("--os-pressure", None, "on", "Relieve OS memory pressure. --no-os-pressure turns it off."),
103            f("--pressure-high", Some("<pct>"), "85", "KV pool occupancy above which cache is evicted."),
104            f("--pressure-low", Some("<pct>"), "70", "The occupancy eviction brings it down to."),
105            f("--pin-budget-pct", Some("<pct>"), "50", "Share of the KV pool pinned prefixes may hold."),
106        ],
107    },
108    Group {
109        title: "Speculative decoding",
110        flags: &[
111            f("--speculate", Some("<auto|prompt-lookup|mtp-head|<drafter>|off>"), "off", "Draft tokens and verify them; also dflash:, dspark:, eagle3:, draft-model: <path>."),
112            f("--spec-draft-tokens", Some("<N>"), "per strategy", "Draft depth, 1..15."),
113            f("--spec-adaptive", None, "on", "Adapt the depth to the acceptance rate. --no-spec-adaptive pins it."),
114            f("--spec-min-yield", Some("<F>"), "0.75", "The yield below which a lane stops drafting."),
115            f("--spec-yield-rounds", Some("<N>"), "24", "Rounds over which the yield is judged."),
116            f("--spec-throughput-gate", None, "on", "Check that speculation beats plain decoding. --no-spec-throughput-gate skips it."),
117            f("--spec-gate-probe-tokens", Some("<N>"), "16", "The gate's probe length."),
118            f("--spec-min-speedup", Some("<F>"), "1.08", "The speed-up the gate requires."),
119            f("--spec-gate-reprobe", Some("<N>"), "32", "Tokens between re-probes."),
120            f("--spec-gate-reprobe-max", Some("<N>"), "1024", "The ceiling the re-probe interval backs off to."),
121            f("--spec-max-temperature", Some("<F>"), "none", "Cap a model-default temperature on a speculating model."),
122            f("--dspark-confidence", Some("<F>"), "engine's", "The DSpark drafter's confidence threshold."),
123            f("--spec-bitexact", None, "off", "Make speculated and plain decoding bit-exact. --no-spec-bitexact turns it off."),
124        ],
125    },
126    Group {
127        title: "Code completion (FIM)",
128        flags: &[
129            f("--fim-model", Some("<path>"), "", "A dedicated fill-in-the-middle model in its own worker."),
130            f("--fim-max-context", Some("<N>"), "min(window, 8192)", "The FIM model's window."),
131            f("--fim-max-batch", Some("<N>"), "2", "The FIM model's lanes."),
132            f("--completion-deadline-ms", Some("<N>"), "0 (none)", "A completion that cannot start within this expires."),
133            f("--completion-rate", Some("<F>"), "10", "Completions per second per client (0 = unmetered)."),
134            f("--completion-burst", Some("<N>"), "20", "Completions a client may fire back to back."),
135        ],
136    },
137    Group {
138        title: "Logging and telemetry",
139        flags: &[
140            f("--log-filter", Some("<directive>"), "info", "Log level, or per target (superfluid_daemon::scheduler=debug,info)."),
141            f("--verbose", None, "", "The same as --log-filter debug; also -v."),
142            f("--log-dir", Some("<dir>"), "stderr", "Write JSON logs to rotating files here."),
143            f("--log-file", Some("<path>"), "", "Send stderr to this file (appended)."),
144            f("--tui", None, "on a terminal", "The live terminal monitor. --no-tui turns it off."),
145            f("--otlp-metrics", Some("<url>"), "off", "Push scheduler metrics to an OTLP/HTTP collector."),
146            f("--otlp-interval-ms", Some("<N>"), "15000", "The metrics push interval."),
147            f("--otlp-endpoint", Some("<url>"), "off", "Push operational traces (never content) to an OTLP/HTTP collector."),
148            f("--otlp-header", Some("<k=v>"), "", "A header on every OTLP request, repeatable."),
149            f("--otlp-service-name", Some("<name>"), "superfluid", "The service.name resource attribute."),
150            f("--otlp-filter", Some("<directive>"), "info,...scheduler=debug", "Which spans are exported."),
151            f("--otlp-queue", Some("<N>"), "8192", "The span export queue; a full queue drops spans."),
152            f("--otlp-batch-ms", Some("<N>"), "1000", "The span export interval."),
153            f("--otlp-timeout-ms", Some("<N>"), "5000", "The export request timeout."),
154        ],
155    },
156    Group {
157        title: "Fleet head",
158        flags: &[
159            f("--fleet", Some("<host:port>[,...]"), "off", "Serve as a fleet head over these superfluid-noded agents."),
160            f("--fleet-listen", Some("<addr:port>"), "off", "Take nodes that join (`superfluid node join`), announced on the local network."),
161            f("--fleet-auth", Some("<path>"), "$SUPERFLUID_HOME/fleet/token", "The fleet token: the links to the nodes are encrypted with it."),
162            f("--fleet-policy", Some("<load-aware|least-loaded|round-robin>"), "load-aware", "Session placement."),
163            f("--fleet-pool-high", Some("<pct>"), "90", "Pass over a node whose KV pool is at least this full."),
164            f("--fleet-conns-per-node", Some("<N>"), "lanes", "Connections to each node you list; by default as many as it has lanes."),
165        ],
166    },
167];
168
169/// Accepted spellings that are not rows of their own in the table: the `--no-` halves of the
170/// switches, short and legacy aliases, and the retired flags `serve` still reads.
171const SERVE_ALIASES: &[(&str, Option<&str>)] = &[
172    ("--no-park", None),
173    ("--no-worker-process", None),
174    ("--no-os-pressure", None),
175    ("--no-spec-adaptive", None),
176    ("--no-spec-throughput-gate", None),
177    ("--no-spec-bitexact", None),
178    ("--no-tui", None),
179    ("-v", None),
180    ("--help", None),
181    ("-h", None),
182    ("--max-batch-size", Some("--max-batch")),
183    ("--request-timeout", Some("--completion-deadline-ms")),
184];
185
186const SERVE_RETIRED_WITH_VALUE: &[&str] = &[
187    "--prefix-cache-file",
188    "--prefix-cache-save-interval",
189    "--files-dir",
190    "--media-dir",
191    "--metallib",
192    "--prefill-chunk",
193    "--gpu-wait-timeout-ms",
194    "--decode-replay",
195];
196
197const SERVE_RETIRED_SWITCHES: &[&str] =
198    &["--paged-kv", "--prefix-cache", "--continuous-batching", "--paged-weights", "--no-paged-weights-retry", "--no-baked-decode"];
199
200fn serve_flag(name: &str) -> Option<&'static Flag> {
201    SERVE.iter().flat_map(|g| g.flags.iter()).find(|fl| fl.name == name)
202}
203
204/// Whether `serve` reads `name` with a value after it: `Some(true)` it does, `Some(false)` it is a
205/// switch, `None` no such flag.
206pub fn serve_takes_value(name: &str) -> Option<bool> {
207    if let Some(fl) = serve_flag(name) {
208        return Some(fl.value.is_some());
209    }
210    if let Some((_, alias)) = SERVE_ALIASES.iter().find(|(n, _)| *n == name) {
211        return Some(alias.and_then(serve_flag).is_some_and(|fl| fl.value.is_some()));
212    }
213    if SERVE_RETIRED_WITH_VALUE.contains(&name) {
214        return Some(true);
215    }
216    SERVE_RETIRED_SWITCHES.contains(&name).then_some(false)
217}
218
219/// Splits `--flag=value` into `--flag value` for every flag `takes_value` says reads a value.
220/// `--pull-<name>=<value>` is left whole: its parser reads that form itself.
221pub fn split_equals(args: &[String], takes_value: impl Fn(&str) -> Option<bool>) -> Vec<String> {
222    let mut out = Vec::with_capacity(args.len());
223    for a in args {
224        match a.split_once('=') {
225            Some((flag, value)) if flag.starts_with("--") && !flag.starts_with("--pull-") && takes_value(flag) == Some(true) => {
226                out.push(flag.to_string());
227                out.push(value.to_string());
228            }
229            _ => out.push(a.clone()),
230        }
231    }
232    out
233}
234
235fn distance(a: &str, b: &str) -> usize {
236    let b: Vec<char> = b.chars().collect();
237    let mut row: Vec<usize> = (0..=b.len()).collect();
238    for (i, ca) in a.chars().enumerate() {
239        let mut prev = row[0];
240        row[0] = i + 1;
241        for (j, cb) in b.iter().enumerate() {
242            let cur = row[j + 1];
243            row[j + 1] = (prev + usize::from(ca != *cb)).min(row[j] + 1).min(cur + 1);
244            prev = cur;
245        }
246    }
247    row[b.len()]
248}
249
250/// The known flag closest to `unknown`, when one is close enough to be what was meant.
251pub fn suggest<'a>(unknown: &str, known: impl IntoIterator<Item = &'a str>) -> Option<&'a str> {
252    let unknown = unknown.split_once('=').map_or(unknown, |(f, _)| f);
253    known
254        .into_iter()
255        .map(|k| (distance(unknown, k), k))
256        .filter(|(d, k)| *d <= 2.max(k.len() / 3) || (unknown.len() > 4 && k.starts_with(unknown)))
257        .min_by_key(|(d, _)| *d)
258        .map(|(_, k)| k)
259}
260
261pub fn serve_flag_names() -> impl Iterator<Item = &'static str> {
262    SERVE.iter().flat_map(|g| g.flags.iter().map(|fl| fl.name)).chain(SERVE_ALIASES.iter().map(|(n, _)| *n)).filter(|n| *n != "--pull-<name>")
263}
264
265pub fn version() -> String {
266    format!("superfluid {}", env!("CARGO_PKG_VERSION"))
267}
268
269pub fn overview() -> String {
270    format!(
271        "{}
272A serving daemon for local LLM inference: one scheduler, one session log and one set of APIs
273over llama.cpp, MLX and baseRT.
274
275Usage: superfluid <command> [options]
276
277Commands:
278  serve <model>        Serve a model over HTTP (OpenAI, Anthropic, Ollama) and a unix socket
279  launch <agent>       Run a coding agent (Claude Code, ...) against a local model
280  stop                 Stop the server `launch` started
281  fleet token          Print the fleet token, making it the first time
282  node join [<head>]   Serve a model to a fleet head from this machine
283  runtimes [<model>]   List the inference runtimes here, or which one serves a model
284  runtime <action>     Install, update, repair, remove or pin a runtime
285  session <action>     Inspect or export a session of a running server
286  media <action>       Store or garbage-collect media on a running server
287  top <host:port>...   Monitor running servers in the terminal
288  help [<command>]     Show help for a command
289
290Options:
291  -h, --help           Show help
292  -V, --version        Show the version
293
294Get started:
295  superfluid serve unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_M     # llama.cpp; installed and pulled on first run
296  superfluid serve mlx-community/Qwen3-4B-4bit        # MLX on Apple silicon
297  superfluid serve ./model.gguf
298  curl http://{DEFAULT_HTTP}/v1/models
299
300Run 'superfluid <command> --help' for a command's options.
301Docs: https://github.com/basecompute/superfluid/tree/main/docs
302",
303        version()
304    )
305}
306
307pub fn serve_help() -> String {
308    let mut out = String::from(
309        "Serve one or more models.
310
311Usage: superfluid serve <model> [options]
312
313  <model> is a local path (a .gguf file, an MLX directory, a .base bundle) or a Hugging Face
314  style id, org/model[:tag]. An id is pulled by its runtime on first use; the runtime is taken
315  from the id (a *GGUF* repo -> llamacpp, mlx-community/* or *mlx* -> mlx, else basert) unless
316  --runtime names one. A runtime that is not installed is installed first, unless --offline.
317
318Examples:
319  superfluid serve unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_M
320  superfluid serve ./Qwen3-4B-Q4_K_M.gguf --port 8000 --api-key secret
321  superfluid serve --model a.gguf --model b.gguf --max-batch 16
322
323Every option that takes a value accepts --flag value and --flag=value.
324",
325    );
326    for g in SERVE {
327        out.push_str(&format!("\n{}:\n", g.title));
328        for fl in g.flags {
329            let head = match fl.value {
330                Some(v) => format!("{} {v}", fl.name),
331                None => fl.name.to_string(),
332            };
333            let default = if fl.default.is_empty() { String::new() } else { format!(" [default: {}]", fl.default) };
334            out.push_str(&row(&head, &format!("{}{default}", fl.help)));
335        }
336    }
337    out
338}
339
340const HEAD: usize = 32;
341const WIDTH: usize = 100;
342
343/// One option row: the flag in a column of its own, the text word-wrapped beside it.
344fn row(head: &str, text: &str) -> String {
345    let mut lines: Vec<String> = Vec::new();
346    let mut line = String::new();
347    for word in text.split_whitespace() {
348        if !line.is_empty() && HEAD + 2 + line.len() + 1 + word.len() > WIDTH {
349            lines.push(std::mem::take(&mut line));
350        }
351        if !line.is_empty() {
352            line.push(' ');
353        }
354        line.push_str(word);
355    }
356    lines.push(line);
357    let pad = " ".repeat(HEAD + 2);
358    let mut out = if head.chars().count() + 2 > HEAD {
359        format!("  {head}\n{pad}{}\n", lines[0])
360    } else {
361        format!("  {head:<w$}{}\n", lines[0], w = HEAD)
362    };
363    for l in &lines[1..] {
364        out.push_str(&format!("{pad}{l}\n"));
365    }
366    out
367}
368
369pub const RUNTIMES_HELP: &str = "List the inference runtimes here, or which one serves a model.
370
371Usage: superfluid runtimes [<model>] [--json]
372
373  With no model: one row per runtime and install, its state, version, the formats it reads and
374  its device, then what to do about any that is not ready.
375  With a model: which runtime reads it and what that runtime serves and refuses (exit 1 if none).
376
377Options:
378  --json    Print JSON
379";
380
381pub const RUNTIME_HELP: &str = "Install and manage inference runtimes.
382
383Usage:
384  superfluid runtime install <id> [options]     Fetch and install a runtime for this machine
385  superfluid runtime update <id>                Install the newest tested build beside the current one
386  superfluid runtime repair <id> [<install>]    Reinstall an install in place
387  superfluid runtime remove <id> [<install>]    Remove one install, or all of them
388  superfluid runtime use <id> <install>|--auto  Pin the default install, or go back to the best-ranked
389
390  <id> is basert, llamacpp or mlx. Installs live under $SUPERFLUID_HOME/runtimes (~/.superfluid).
391
392Install options:
393  --version <V>          A runtime version (default: the newest this adapter was tested with)
394  --backend <B>          metal, cuda, vulkan, rocm, cpu, ... (default: the best for this machine)
395  --from <url|path>      An archive of your own (for mlx, a package index)
396  --sha256 <H>           The archive's digest, checked when given
397  --untested             Allow a version the adapter was not tested with
398  --dry-run              Print the plan and stop
399
400Examples:
401  superfluid runtime install llamacpp
402  superfluid runtime install llamacpp --backend vulkan --dry-run
403";
404
405pub const SESSION_HELP: &str = "Inspect or export a session of a running server.
406
407Usage:
408  superfluid session inspect <id> [--socket <path>] [--json]
409  superfluid session export <id> [--socket <path>] --format jsonl|otlp-jsonl [options]
410
411Options:
412  --socket <path>            The server's socket [default: $SUPERFLUID_HOME/superfluid.sock]
413  --json                     (inspect) Print JSON
414  --format <jsonl|otlp-jsonl> (export) Events as JSON lines, or OTLP trace lines [default: jsonl]
415  --out <dir>                (export) Write session-<id>.* here instead of stdout
416  --include-content          (export) Include token text
417  --model <name>             (export) Label the spans with this model
418  --endpoint <url>           (export) Push the traces to an OTLP/HTTP collector instead
419  --allow-content-egress     (export) Let --include-content cross the network with --endpoint
420";
421
422pub const MEDIA_HELP: &str = "Store or garbage-collect media on a running server.
423
424Usage:
425  superfluid media put <file> [--socket <path>]   Store a file; prints its hash
426  superfluid media gc [--socket <path>]           Drop media no session refers to
427
428Options:
429  --socket <path>    The server's socket [default: $SUPERFLUID_HOME/superfluid.sock]
430";
431
432pub const TOP_HELP: &str = "Monitor running servers in the terminal.
433
434Usage: superfluid top [--api-key <key>] <host:port> [<host:port> ...]
435
436  Each address is a `superfluid serve` HTTP endpoint; q or Esc quits.
437
438Options:
439  --api-key <key>    Sent on every scrape, for servers behind --api-key
440";
441
442/// The help for a subcommand, by name.
443pub fn command_help(command: &str) -> Option<String> {
444    Some(match command {
445        "serve" => serve_help(),
446        "runtimes" => RUNTIMES_HELP.to_string(),
447        "runtime" => RUNTIME_HELP.to_string(),
448        "session" => SESSION_HELP.to_string(),
449        "media" => MEDIA_HELP.to_string(),
450        "top" => TOP_HELP.to_string(),
451        "launch" => crate::launch::HELP.to_string(),
452        "fleet" => crate::fleet_token::HELP.to_string(),
453        "node" => crate::fleet_join::HELP.to_string(),
454        "stop" => crate::launch::STOP_HELP.to_string(),
455        _ => return None,
456    })
457}
458
459pub const COMMANDS: &[&str] = &["serve", "launch", "stop", "runtimes", "runtime", "session", "media", "top", "fleet", "node", "help", "version"];
460
461/// `$SUPERFLUID_HOME/sessions`.
462pub fn default_sessions(home: &Path) -> PathBuf {
463    home.join("sessions")
464}
465
466/// `$SUPERFLUID_HOME/superfluid.sock`, or a per-user socket in the temporary directory when that
467/// path is too long for a unix socket (`sun_path` is 104 bytes on macOS, 108 on Linux).
468pub fn default_socket(home: &Path, sun_path_len: usize) -> PathBuf {
469    let sock = home.join("superfluid.sock");
470    if sock.as_os_str().len() < sun_path_len {
471        return sock;
472    }
473    // SAFETY: getuid has no preconditions and cannot fail.
474    let uid = unsafe { libc::getuid() };
475    let tmp = std::env::temp_dir().join(format!("superfluid-{uid}.sock"));
476    if tmp.as_os_str().len() < sun_path_len {
477        tmp
478    } else {
479        PathBuf::from(format!("/tmp/superfluid-{uid}.sock"))
480    }
481}
482
483/// The runtime a model id goes to when no `--runtime` names one: a GGUF repository to llama.cpp,
484/// an MLX conversion to MLX, anything else to baseRT.
485pub fn runtime_for_id(id: &str) -> &'static str {
486    let id = id.split_once(':').map_or(id, |(id, _)| id).to_ascii_lowercase();
487    let (org, repo) = id.split_once('/').unwrap_or(("", &id));
488    if repo.contains("gguf") {
489        "llamacpp"
490    } else if org == "mlx-community" || repo.contains("mlx") {
491        "mlx"
492    } else {
493        "basert"
494    }
495}
496
497#[cfg(test)]
498mod tests {
499    use super::*;
500
501    fn strings(v: &[&str]) -> Vec<String> {
502        v.iter().map(|s| s.to_string()).collect()
503    }
504
505    #[test]
506    fn equals_forms_are_split_only_for_flags_that_take_a_value() {
507        let got = split_equals(
508            &strings(&["--port=8000", "--park", "--pull-file=m.gguf", "--model=a=b.gguf", "x=y", "--nope=1", "--no-http=1"]),
509            serve_takes_value,
510        );
511        assert_eq!(got, strings(&["--port", "8000", "--park", "--pull-file=m.gguf", "--model", "a=b.gguf", "x=y", "--nope=1", "--no-http=1"]));
512        assert_eq!(split_equals(&strings(&["--max-batch-size=4"]), serve_takes_value), strings(&["--max-batch-size", "4"]));
513    }
514
515    #[test]
516    fn a_near_miss_is_suggested_and_a_far_one_is_not() {
517        assert_eq!(suggest("--max-ctx", serve_flag_names()), Some("--max-context"));
518        assert_eq!(suggest("--temprature", serve_flag_names()), Some("--temperature"));
519        assert_eq!(suggest("--api_key", serve_flag_names()), Some("--api-key"));
520        assert_eq!(suggest("--sesions=/x", serve_flag_names()), Some("--sessions"));
521        assert_eq!(suggest("--frobnicate-everything", serve_flag_names()), None);
522    }
523
524    #[test]
525    fn a_runtime_is_read_from_a_model_id() {
526        assert_eq!(runtime_for_id("unsloth/Qwen3-0.6B-GGUF:Q4_K_M"), "llamacpp");
527        assert_eq!(runtime_for_id("bartowski/Llama-3.2-1B-Instruct-gguf"), "llamacpp");
528        assert_eq!(runtime_for_id("mlx-community/Qwen3-4B-4bit"), "mlx");
529        assert_eq!(runtime_for_id("someone/Qwen3-4B-MLX-8bit:main"), "mlx");
530        assert_eq!(runtime_for_id("basecompute/Qwen3-1.7B"), "basert");
531        assert_eq!(runtime_for_id("Qwen/Qwen3-0.6B"), "basert");
532    }
533
534    #[test]
535    fn the_default_socket_fits_a_unix_socket_path() {
536        assert_eq!(default_socket(Path::new("/home/u/.superfluid"), 104), Path::new("/home/u/.superfluid/superfluid.sock"));
537        let long = PathBuf::from(format!("/{}", "h".repeat(120)));
538        let s = default_socket(&long, 104);
539        assert!(s.as_os_str().len() < 104 && s.file_name().unwrap().to_string_lossy().starts_with("superfluid-"), "{}", s.display());
540    }
541
542    #[test]
543    fn serve_help_names_every_flag_in_its_table() {
544        let help = serve_help();
545        for name in SERVE.iter().flat_map(|g| g.flags.iter().map(|fl| fl.name)) {
546            assert!(help.contains(name), "{name}");
547        }
548        for line in help.lines() {
549            assert!(line.chars().count() <= WIDTH, "a help line runs on: {line}");
550        }
551    }
552
553    /// Every flag the `serve` parser matches is in the table (or an alias or a
554    /// retired flag), so `serve --help`, `--flag=value` and the suggestions know it.
555    #[test]
556    fn every_flag_serve_reads_is_known_here() {
557        let src = include_str!("serve.rs");
558        let start = src.find("SERVE-FLAGS-BEGIN").expect("marker");
559        let end = src.find("SERVE-FLAGS-END").expect("marker");
560        let body = &src[start..end];
561        let mut seen = 0;
562        for piece in body.split('"').skip(1).step_by(2) {
563            let long = piece.starts_with("--") && piece.len() > 2 && piece != "--pull-";
564            let short = piece.len() == 2 && piece.starts_with('-') && piece.as_bytes()[1].is_ascii_alphabetic();
565            if (long || short) && !piece.contains(' ') {
566                seen += 1;
567                assert!(serve_takes_value(piece).is_some(), "serve reads {piece}, which cli.rs does not list");
568            }
569        }
570        assert!(seen > 100, "the scan found {seen} flags");
571    }
572}