Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
serve mode: reach the expert-history save on SIGTERM
JustVugga97495274b99
diff --git a/c/glm53.c b/c/glm53.cindex 64388463..be6ff2a6 100644--- a/c/glm53.c+++ b/c/glm53.c@@ -3322,6 +3322,7 @@ int main(int argc, char **argv) {tok_load(&serve_tok, tokenizer_path);const char *batch = getenv("SERVE_BATCH");arm_stops(snap, &serve_tok, batch && atoi(batch));+ coli_rt_term_arm(); /* SIGTERM must reach the save below (#1629) */serve_loop(&served, &serve_tok);glm53_telemetry_save();rt_destroy();diff --git a/c/inkling.c b/c/inkling.cindex 7d74b97e..62045c27 100644--- a/c/inkling.c+++ b/c/inkling.c@@ -2529,6 +2529,7 @@ int main(int argc, char **argv) {pins_load(&m, snap);char tkp[2048]; snprintf(tkp, sizeof(tkp), "%s/tokenizer.json", snap);Tok T; tok_load(&T, tkp);+ coli_rt_term_arm(); /* SIGTERM must reach the save below (#1629) */serve_loop(&m, &T);usage_save(&m, snap);return 0;diff --git a/c/kimi_k3.c b/c/kimi_k3.cindex 34ba89ef..278c17fd 100644--- a/c/kimi_k3.c+++ b/c/kimi_k3.c@@ -3211,7 +3211,13 @@ int main(int argc, char **argv){fprintf(stderr,"[K3] tokenizer.json loaded (family=%s)\n",T.kimi?"kimi":(T.o200k?"o200k":"cl100k")); } }if(serving){if(!has_tok){ fprintf(stderr,"serve mode needs tokenizer.json\n"); return 1; }+ coli_rt_term_arm(); /* SIGTERM must reach the save below (#1629) */serve_loop(&m,&T);+ /* Questo ramo non salvava affatto la storia: gli altri motori lo+ * fanno subito dopo il loop, qui mancava del tutto, quindi la+ * modalita serve non ha mai contribuito alla cache appresa --+ * nemmeno uscendo in modo pulito. */+ if(g_k3_usage[0]) rt_save(g_k3_usage,0);if(g_k3_val_fp){ fflush(g_k3_val_fp); fclose(g_k3_val_fp); g_k3_val_fp=NULL; }if(g_k3_val_lfp){ fflush(g_k3_val_lfp); fclose(g_k3_val_lfp); g_k3_val_lfp=NULL; }#ifdef COLI_METALdiff --git a/c/olmoe.c b/c/olmoe.cindex 0270f0de..dd5ebae5 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -1711,6 +1711,7 @@ int main(int argc, char **argv) {Tok T;char tokpath[2048]; snprintf(tokpath, sizeof(tokpath), "%s/tokenizer.json", snap);tok_load(&T, tokpath);+ coli_rt_term_arm(); /* SIGTERM must reach the save below (#1629) */serve_loop(&m, &T, ctx_cap);{ const char *up = getenv("COLI_USAGE");if (up && *up) rt_save(up, 0); }diff --git a/c/qwen38.c b/c/qwen38.cindex e569cb3c..0d8c939f 100644--- a/c/qwen38.c+++ b/c/qwen38.c@@ -1790,6 +1790,7 @@ int main(int argc, char **argv) {if (getenv("SERVE") && getenv("SERVE")[0] == '1') {if (!g_tok) { fprintf(stderr, "[serve] tokenizer.json required (put in SNAP or set TOK)\n");q38_model_free(&m); rt_destroy(); return 1; }+ coli_rt_term_arm(); /* SIGTERM must reach the save below (#1629) */serve_loop(&m);rt_save(g_q38_usage, 0);q38_prefix_cache_release(&m);diff --git a/c/route_trace.h b/c/route_trace.hindex be3bfa11..08e444a0 100644--- a/c/route_trace.h+++ b/c/route_trace.h@@ -424,4 +424,52 @@ static int rt_router_pick(int best, int kk, int experts, int layer) {return kk < experts ? kk : 0;}+/* ---- SIGTERM in serve mode: reach rt_save instead of dying before it -------+ *+ * The expert history is written once, after the serve loop returns. A server+ * is not stopped that way: `kill <pid>`, `systemctl stop` and launchd all send+ * SIGTERM, whose default action kills the process outright. The loop never+ * returns, rt_save never runs, and a long serving session contributes nothing+ * to the learned cache -- while one-shot chat mode, which exits on stdin EOF,+ * saves normally. So the data is collected and then thrown away, which is the+ * worst of both. Reported in #1629.+ *+ * The fix is not a save inside the handler. rt_save() allocates and writes a+ * file, neither of which is async-signal-safe, and doing it from a handler+ * that can fire in the middle of the very structures it serialises is how a+ * good history file becomes a corrupt one. The handler only raises a flag and+ * lets the blocking read fail; the loop then exits through the SAME path as+ * stdin EOF, and the existing save at the bottom of main() runs on a quiet+ * process.+ *+ * NO SA_RESTART, deliberately. The serve loop blocks in fgets/getline waiting+ * for the next request. With SA_RESTART the read silently resumes after the+ * handler returns, the flag is set and never looked at again, and+ * `systemctl stop` hangs until its TimeoutStopSec turns into SIGKILL. Without+ * it the read returns NULL/EINTR, which every serve loop already treats as+ * "the gateway is gone" -- the path they take on a clean shutdown. colibri.c+ * carries the same reasoning for its own handler (#810).+ *+ * SIGINT is left alone. On these engines it is still the default, immediate+ * death: making Ctrl-C wait for an in-flight turn, which on a disk-streaming+ * engine is minutes, would read as a hang. colibri.c can afford a soft SIGINT+ * because it can end the current turn; these cannot, so they keep the+ * behaviour their users already expect. */+#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)+#include <signal.h>+static volatile sig_atomic_t coli_rt_term_flag = 0;+static void coli_rt_term_handler(int sig) { (void)sig; coli_rt_term_flag = 1; }+/* Call once, just before entering a serve loop. */+static inline void coli_rt_term_arm(void) {+ struct sigaction sa;+ memset(&sa, 0, sizeof(sa));+ sa.sa_handler = coli_rt_term_handler;+ sigemptyset(&sa.sa_mask);+ sa.sa_flags = 0; /* see above: SA_RESTART would hang the stop */+ sigaction(SIGTERM, &sa, NULL);+}+#else+static inline void coli_rt_term_arm(void) {} /* Windows: no sigaction, behaviour unchanged */+#endif+#endif /* ROUTE_TRACE_H */