@@ -0,0 +1,541 @@
1+// The read, edit, and bash capabilities, exposed as codemode globals.
2+
3+#include "host.h"
4+
5+#include <errno.h>
6+#include <fcntl.h>
7+#include <poll.h>
8+#include <signal.h>
9+#include <stdarg.h>
10+#include <stdint.h>
11+#include <stdio.h>
12+#include <stdlib.h>
13+#include <string.h>
14+#include <strings.h>
15+#include <sys/stat.h>
16+#include <sys/wait.h>
17+#include <unistd.h>
18+
19+#include <yyjson.h>
20+
21+#define MAX_TEXT 100000
22+#define MAX_IMAGE_B64 (5u * 1024 * 1024)
23+#define MAX_READ (16u * 1024 * 1024)
24+#define MAX_HEAD 10000
25+#define MAX_TAIL 6000
26+#define TRUNCATED "\n\n... output truncated ...\n\n"
27+
28+// ---- small helpers --------------------------------------------------------
29+
30+static void *xmalloc(size_t n) {
31+ void *p = malloc(n);
32+ if (!p) abort();
33+ return p;
34+}
35+
36+static char *xstrdup(const char *s) {
37+ size_t n = strlen(s) + 1;
38+ char *p = xmalloc(n);
39+ memcpy(p, s, n);
40+ return p;
41+}
42+
43+typedef struct {
44+ char *data;
45+ size_t len;
46+ size_t cap;
47+} buf;
48+
49+static void buf_add(buf *b, const char *p, size_t n) {
50+ if (b->len + n + 1 > b->cap) {
51+ size_t cap = b->cap ? b->cap : 64;
52+ while (cap < b->len + n + 1) cap *= 2;
53+ b->data = realloc(b->data, cap);
54+ if (!b->data) abort();
55+ b->cap = cap;
56+ }
57+ memcpy(b->data + b->len, p, n);
58+ b->len += n;
59+ b->data[b->len] = '\0';
60+}
61+
62+static void buf_addz(buf *b, const char *s) {
63+ buf_add(b, s, strlen(s));
64+}
65+
66+static cage_host_result ok_json(char *json) {
67+ return (cage_host_result){.json = json};
68+}
69+
70+static cage_host_result err(const char *fmt, ...) {
71+ char detail[512];
72+ va_list ap;
73+ va_start(ap, fmt);
74+ vsnprintf(detail, sizeof detail, fmt, ap);
75+ va_end(ap);
76+ return (cage_host_result){.error = xstrdup(detail)};
77+}
78+
79+// A JSON string value; `s` must be valid UTF-8 and NUL-terminated.
80+static char *json_string(const char *s) {
81+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
82+ yyjson_mut_doc_set_root(doc, yyjson_mut_strcpy(doc, s));
83+ char *out = yyjson_mut_write(doc, 0, NULL);
84+ yyjson_mut_doc_free(doc);
85+ return out;
86+}
87+
88+// nth argument of the JSON array the runtime passes to a host function.
89+static yyjson_val *arg_at(yyjson_doc *doc, size_t i) {
90+ if (!doc) return NULL;
91+ yyjson_val *root = yyjson_doc_get_root(doc);
92+ if (!yyjson_is_arr(root)) return NULL;
93+ return yyjson_arr_get(root, i);
94+}
95+
96+// ---- UTF-8 ----------------------------------------------------------------
97+
98+// Validates one sequence at `p` (n bytes remain), returning its length.
99+static bool utf8_seq(const unsigned char *p, size_t n, size_t *len) {
100+ unsigned char c = p[0];
101+ if (c >= 0xC2 && c <= 0xDF) {
102+ if (n >= 2 && (p[1] & 0xC0) == 0x80) return (*len = 2, true);
103+ } else if (c == 0xE0) {
104+ if (n >= 3 && p[1] >= 0xA0 && p[1] <= 0xBF && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
105+ } else if (c >= 0xE1 && c <= 0xEC) {
106+ if (n >= 3 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
107+ } else if (c == 0xED) {
108+ if (n >= 3 && p[1] >= 0x80 && p[1] <= 0x9F && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
109+ } else if (c >= 0xEE && c <= 0xEF) {
110+ if (n >= 3 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) return (*len = 3, true);
111+ } else if (c == 0xF0) {
112+ if (n >= 4 && p[1] >= 0x90 && p[1] <= 0xBF && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80)
113+ return (*len = 4, true);
114+ } else if (c >= 0xF1 && c <= 0xF3) {
115+ if (n >= 4 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80)
116+ return (*len = 4, true);
117+ } else if (c == 0xF4) {
118+ if (n >= 4 && p[1] >= 0x80 && p[1] <= 0x8F && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80)
119+ return (*len = 4, true);
120+ }
121+ return false;
122+}
123+
124+// Replaces invalid byte sequences with U+FFFD, so the text survives the JSON
125+// and session round trips. Mirrors a UTF-8 decode with replacement.
126+static char *utf8_clean(const char *in, size_t n) {
127+ buf b = {0};
128+ const unsigned char *p = (const unsigned char *)in;
129+ size_t i = 0;
130+ while (i < n) {
131+ size_t len;
132+ if (p[i] < 0x80) {
133+ buf_add(&b, (const char *)&p[i], 1);
134+ i += 1;
135+ } else if (utf8_seq(p + i, n - i, &len)) {
136+ buf_add(&b, (const char *)(p + i), len);
137+ i += len;
138+ } else {
139+ buf_add(&b, "\xef\xbf\xbd", 3);
140+ i += 1;
141+ }
142+ }
143+ if (!b.data) buf_add(&b, "", 1);
144+ return b.data;
145+}
146+
147+// ---- file helpers ---------------------------------------------------------
148+
149+// Reads up to MAX_READ bytes. Returns 0, or -1 with errno set.
150+static int slurp(const char *path, buf *out) {
151+ int fd = open(path, O_RDONLY);
152+ if (fd < 0) return -1;
153+ struct stat st;
154+ if (fstat(fd, &st) != 0) {
155+ int e = errno;
156+ close(fd);
157+ errno = e;
158+ return -1;
159+ }
160+ if (S_ISDIR(st.st_mode)) {
161+ close(fd);
162+ errno = EISDIR;
163+ return -1;
164+ }
165+ for (;;) {
166+ if (out->len >= MAX_READ) break;
167+ char tmp[65536];
168+ size_t want = sizeof tmp;
169+ if (out->len + want > MAX_READ) want = MAX_READ - out->len;
170+ ssize_t n = read(fd, tmp, want);
171+ if (n < 0) {
172+ if (errno == EINTR) continue;
173+ int e = errno;
174+ close(fd);
175+ errno = e;
176+ return -1;
177+ }
178+ if (n == 0) break;
179+ buf_add(out, tmp, (size_t)n);
180+ }
181+ close(fd);
182+ return 0;
183+}
184+
185+static int spill(const char *path, const char *data, size_t len) {
186+ int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
187+ if (fd < 0) return -1;
188+ size_t off = 0;
189+ while (off < len) {
190+ ssize_t n = write(fd, data + off, len - off);
191+ if (n < 0) {
192+ if (errno == EINTR) continue;
193+ int e = errno;
194+ close(fd);
195+ errno = e;
196+ return -1;
197+ }
198+ off += (size_t)n;
199+ }
200+ if (close(fd) != 0) return -1;
201+ return 0;
202+}
203+
204+static bool find_sub(const char *hay, size_t hlen, const char *needle, size_t nlen, size_t *at) {
205+ if (nlen == 0 || nlen > hlen) return false;
206+ for (size_t i = 0; i + nlen <= hlen; i++) {
207+ if (hay[i] == needle[0] && memcmp(hay + i, needle, nlen) == 0) {
208+ *at = i;
209+ return true;
210+ }
211+ }
212+ return false;
213+}
214+
215+static char *base64_encode(const unsigned char *data, size_t len) {
216+ static const char table[] =
217+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
218+ char *out = xmalloc(4 * ((len + 2) / 3) + 1);
219+ size_t i = 0, o = 0;
220+ for (; i + 3 <= len; i += 3) {
221+ uint32_t v = (uint32_t)data[i] << 16 | (uint32_t)data[i + 1] << 8 | data[i + 2];
222+ out[o++] = table[(v >> 18) & 63];
223+ out[o++] = table[(v >> 12) & 63];
224+ out[o++] = table[(v >> 6) & 63];
225+ out[o++] = table[v & 63];
226+ }
227+ if (len - i == 1) {
228+ uint32_t v = (uint32_t)data[i] << 16;
229+ out[o++] = table[(v >> 18) & 63];
230+ out[o++] = table[(v >> 12) & 63];
231+ out[o++] = '=';
232+ out[o++] = '=';
233+ } else if (len - i == 2) {
234+ uint32_t v = (uint32_t)data[i] << 16 | (uint32_t)data[i + 1] << 8;
235+ out[o++] = table[(v >> 18) & 63];
236+ out[o++] = table[(v >> 12) & 63];
237+ out[o++] = table[(v >> 6) & 63];
238+ out[o++] = '=';
239+ }
240+ out[o] = '\0';
241+ return out;
242+}
243+
244+static const char *image_mime(const char *path) {
245+ const char *dot = strrchr(path, '.');
246+ if (!dot) return NULL;
247+ const char *e = dot + 1;
248+ if (strcasecmp(e, "png") == 0) return "image/png";
249+ if (strcasecmp(e, "jpg") == 0 || strcasecmp(e, "jpeg") == 0) return "image/jpeg";
250+ if (strcasecmp(e, "webp") == 0) return "image/webp";
251+ return NULL;
252+}
253+
254+// ---- read -----------------------------------------------------------------
255+
256+static cage_host_result host_read(const char *args_json, void *user) {
257+ cage_runtime *rt = user;
258+ yyjson_doc *doc = yyjson_read(args_json, strlen(args_json), 0);
259+ yyjson_val *a = arg_at(doc, 0);
260+ if (!yyjson_is_str(a)) {
261+ yyjson_doc_free(doc);
262+ return err("read: path must be a string");
263+ }
264+ const char *path = yyjson_get_str(a);
265+
266+ buf data = {0};
267+ if (slurp(path, &data) != 0) {
268+ cage_host_result r = err("read failed: %s: %s", path, strerror(errno));
269+ free(data.data);
270+ yyjson_doc_free(doc);
271+ return r;
272+ }
273+
274+ const char *mime = image_mime(path);
275+ if (mime) {
276+ if (!cage_runtime_supports_images(rt)) {
277+ cage_host_result r = err("read failed: %s is an image and the current model does not accept image input", path);
278+ free(data.data);
279+ yyjson_doc_free(doc);
280+ return r;
281+ }
282+ char *b64 = base64_encode((const unsigned char *)data.data, data.len);
283+ if (strlen(b64) > MAX_IMAGE_B64) {
284+ cage_host_result r = err("read failed: %s encodes to %zu bytes, over the %u byte image limit",
285+ path, strlen(b64), MAX_IMAGE_B64);
286+ free(b64);
287+ free(data.data);
288+ yyjson_doc_free(doc);
289+ return r;
290+ }
291+ char msg[1024];
292+ snprintf(msg, sizeof msg, "read %s (%s, %zu bytes)", path, mime, data.len);
293+ cage_runtime_add_image(rt, mime, b64);
294+ free(b64);
295+ free(data.data);
296+ yyjson_doc_free(doc);
297+ return ok_json(json_string(msg));
298+ }
299+
300+ size_t sniff = data.len < 8000 ? data.len : 8000;
301+ if (sniff > 0 && memchr(data.data, 0, sniff)) {
302+ cage_host_result r = err("read failed: %s is binary; use bash (file, xxd)", path);
303+ free(data.data);
304+ yyjson_doc_free(doc);
305+ return r;
306+ }
307+
308+ size_t take = data.len > MAX_TEXT ? MAX_TEXT : data.len;
309+ char *clean = utf8_clean(data.data ? data.data : "", take);
310+ if (data.len > MAX_TEXT) {
311+ buf out = {0};
312+ buf_addz(&out, clean);
313+ char marker[256];
314+ snprintf(marker, sizeof marker, "\n\n... truncated ... %s is %zu characters\n", path, data.len);
315+ buf_addz(&out, marker);
316+ free(clean);
317+ clean = out.data;
318+ }
319+ free(data.data);
320+ yyjson_doc_free(doc);
321+ return ok_json(json_string(clean));
322+}
323+
324+// ---- edit -----------------------------------------------------------------
325+
326+static cage_host_result host_edit(const char *args_json, void *user) {
327+ (void)user;
328+ yyjson_doc *doc = yyjson_read(args_json, strlen(args_json), 0);
329+ yyjson_val *pa = arg_at(doc, 0), *oa = arg_at(doc, 1), *na = arg_at(doc, 2);
330+ if (!yyjson_is_str(pa) || !yyjson_is_str(oa) || !yyjson_is_str(na)) {
331+ yyjson_doc_free(doc);
332+ return err("edit: path, oldText and newText must be strings");
333+ }
334+ const char *path = yyjson_get_str(pa);
335+ const char *old = yyjson_get_str(oa);
336+ size_t old_len = yyjson_get_len(oa);
337+ const char *nw = yyjson_get_str(na);
338+ size_t new_len = yyjson_get_len(na);
339+
340+ if (old_len == 0) {
341+ if (access(path, F_OK) == 0) {
342+ cage_host_result r = err("edit failed: %s already exists", path);
343+ yyjson_doc_free(doc);
344+ return r;
345+ }
346+ if (spill(path, nw, new_len) != 0) {
347+ cage_host_result r = err("edit failed: %s: %s", path, strerror(errno));
348+ yyjson_doc_free(doc);
349+ return r;
350+ }
351+ char msg[1024];
352+ snprintf(msg, sizeof msg, "created %s", path);
353+ yyjson_doc_free(doc);
354+ return ok_json(json_string(msg));
355+ }
356+
357+ buf data = {0};
358+ if (slurp(path, &data) != 0) {
359+ cage_host_result r = err("edit failed: %s: %s", path, strerror(errno));
360+ free(data.data);
361+ yyjson_doc_free(doc);
362+ return r;
363+ }
364+
365+ size_t at;
366+ if (!find_sub(data.data, data.len, old, old_len, &at)) {
367+ cage_host_result r = err("edit failed: oldText not found in %s", path);
368+ free(data.data);
369+ yyjson_doc_free(doc);
370+ return r;
371+ }
372+ size_t second;
373+ if (find_sub(data.data + at + 1, data.len - at - 1, old, old_len, &second)) {
374+ cage_host_result r = err("edit failed: oldText matches more than once in %s", path);
375+ free(data.data);
376+ yyjson_doc_free(doc);
377+ return r;
378+ }
379+
380+ buf out = {0};
381+ buf_add(&out, data.data, at);
382+ buf_add(&out, nw, new_len);
383+ buf_add(&out, data.data + at + old_len, data.len - at - old_len);
384+ if (spill(path, out.data, out.len) != 0) {
385+ cage_host_result r = err("edit failed: %s: %s", path, strerror(errno));
386+ free(out.data);
387+ free(data.data);
388+ yyjson_doc_free(doc);
389+ return r;
390+ }
391+
392+ char msg[1024];
393+ snprintf(msg, sizeof msg, "edited %s", path);
394+ free(out.data);
395+ free(data.data);
396+ yyjson_doc_free(doc);
397+ return ok_json(json_string(msg));
398+}
399+
400+// ---- bash -----------------------------------------------------------------
401+
402+// Keeps only the last MAX_TAIL bytes, flagging that a gap was dropped.
403+static void keep_tail(buf *t, bool *truncated) {
404+ if (t->len <= MAX_TAIL) return;
405+ memmove(t->data, t->data + t->len - MAX_TAIL, MAX_TAIL);
406+ t->len = MAX_TAIL;
407+ t->data[t->len] = '\0';
408+ *truncated = true;
409+}
410+
411+static cage_host_result host_bash(const char *args_json, void *user) {
412+ cage_runtime *rt = user;
413+ yyjson_doc *doc = yyjson_read(args_json, strlen(args_json), 0);
414+ yyjson_val *a = arg_at(doc, 0);
415+ if (!yyjson_is_str(a)) {
416+ yyjson_doc_free(doc);
417+ return err("bash: command must be a string");
418+ }
419+ const char *command = yyjson_get_str(a);
420+
421+ int pipefd[2];
422+ if (pipe(pipefd) != 0) {
423+ cage_host_result r = err("bash: %s", strerror(errno));
424+ yyjson_doc_free(doc);
425+ return r;
426+ }
427+ pid_t pid = fork();
428+ if (pid < 0) {
429+ close(pipefd[0]);
430+ close(pipefd[1]);
431+ cage_host_result r = err("bash: fork: %s", strerror(errno));
432+ yyjson_doc_free(doc);
433+ return r;
434+ }
435+ if (pid == 0) {
436+ setpgid(0, 0);
437+ if (dup2(pipefd[1], STDOUT_FILENO) < 0 || dup2(pipefd[1], STDERR_FILENO) < 0) _exit(127);
438+ close(pipefd[0]);
439+ close(pipefd[1]);
440+ int devnull = open("/dev/null", O_RDONLY);
441+ if (devnull >= 0) {
442+ dup2(devnull, STDIN_FILENO);
443+ close(devnull);
444+ }
445+ execlp("bash", "bash", "-c", command, (char *)NULL);
446+ _exit(127);
447+ }
448+ close(pipefd[1]);
449+ setpgid(pid, pid);
450+
451+ buf head = {0}, tail = {0};
452+ bool truncated = false;
453+ bool cancelled = false;
454+
455+ char chunk[8192];
456+ for (;;) {
457+ if (cage_runtime_cancelled(rt)) {
458+ cancelled = true;
459+ break;
460+ }
461+ struct pollfd pfd = {.fd = pipefd[0], .events = POLLIN};
462+ int pr = poll(&pfd, 1, 100);
463+ if (pr < 0) {
464+ if (errno == EINTR) continue;
465+ break;
466+ }
467+ if (pr == 0) continue;
468+ ssize_t n = read(pipefd[0], chunk, sizeof chunk);
469+ if (n < 0) {
470+ if (errno == EINTR) continue;
471+ break;
472+ }
473+ if (n == 0) break;
474+ chunk[n] = '\0';
475+ cage_runtime_output(rt, chunk);
476+ if (head.len < MAX_HEAD) {
477+ size_t take = MAX_HEAD - head.len;
478+ if ((size_t)n < take) take = (size_t)n;
479+ buf_add(&head, chunk, take);
480+ if (take < (size_t)n) {
481+ buf_add(&tail, chunk + take, (size_t)n - take);
482+ keep_tail(&tail, &truncated);
483+ }
484+ } else {
485+ buf_add(&tail, chunk, (size_t)n);
486+ keep_tail(&tail, &truncated);
487+ }
488+ }
489+ close(pipefd[0]);
490+
491+ int status = 0;
492+ if (cancelled) {
493+ kill(-pid, SIGTERM);
494+ bool reaped = false;
495+ for (int i = 0; i < 30; i++) {
496+ if (waitpid(pid, &status, WNOHANG) == pid) {
497+ reaped = true;
498+ break;
499+ }
500+ usleep(100000);
501+ }
502+ if (!reaped) {
503+ kill(-pid, SIGKILL);
504+ while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
505+ }
506+ } else {
507+ while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
508+ }
509+
510+ int code;
511+ if (cancelled) code = 143;
512+ else if (WIFEXITED(status)) code = WEXITSTATUS(status);
513+ else if (WIFSIGNALED(status)) code = 128 + WTERMSIG(status);
514+ else code = 0;
515+
516+ buf body = {0};
517+ buf_add(&body, head.data ? head.data : "", head.len);
518+ if (truncated) buf_addz(&body, TRUNCATED);
519+ buf_add(&body, tail.data ? tail.data : "", tail.len);
520+
521+ char *clean = utf8_clean(body.data ? body.data : "", body.len);
522+ yyjson_mut_doc *jd = yyjson_mut_doc_new(NULL);
523+ yyjson_mut_val *o = yyjson_mut_obj(jd);
524+ yyjson_mut_doc_set_root(jd, o);
525+ yyjson_mut_obj_add_strcpy(jd, o, "output", clean);
526+ yyjson_mut_obj_add_int(jd, o, "exitCode", code);
527+ char *json = yyjson_mut_write(jd, 0, NULL);
528+ yyjson_mut_doc_free(jd);
529+ free(clean);
530+ free(body.data);
531+ free(head.data);
532+ free(tail.data);
533+ yyjson_doc_free(doc);
534+ return ok_json(json);
535+}
536+
537+void cage_hosts_register(cage_runtime *rt) {
538+ cage_runtime_register(rt, "read", host_read, rt);
539+ cage_runtime_register(rt, "edit", host_edit, rt);
540+ cage_runtime_register(rt, "bash", host_bash, rt);
541+}