Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

11.7 KB
Raw Blame
#include "boxlite.h"
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>

typedef struct GetRequest {
  int done;
  CBoxHandle *box;
  BoxliteErrorCode error_code;
} GetRequest;

typedef struct AgentRequest {
  int done;
  struct CAgent *agent;
  BoxliteErrorCode error_code;
  pthread_t caller;
} AgentRequest;

static void on_agent_created(struct CAgent *agent, const CBoxliteError *error,
                             void *data) {
  AgentRequest *request = data;
  assert(agent != NULL);
  assert(error == NULL);
  assert(pthread_equal(pthread_self(), request->caller));
  request->agent = agent;
  request->error_code = Ok;
  request->done = 1;
}

static void write_all(int fd, const char *data) {
  size_t remaining = strlen(data);
  while (remaining > 0) {
    ssize_t written = write(fd, data, remaining);
    if (written <= 0) {
      _exit(10);
    }
    data += written;
    remaining -= (size_t)written;
  }
}

static int accept_request(int listener, const char *line, const char *body) {
  int client = accept(listener, NULL, NULL);
  assert(client >= 0);
  char request[4096] = {0};
  size_t used = 0;
  char *head_end = NULL;
  while ((head_end = strstr(request, "\r\n\r\n")) == NULL) {
    assert(used < sizeof(request) - 1);
    ssize_t count = read(client, request + used, sizeof(request) - used - 1);
    assert(count > 0);
    used += (size_t)count;
  }
  assert(strncmp(request, line, strlen(line)) == 0);
  if (body != NULL) {
    const char *header = request;
    size_t body_len = 0;
    while ((header = strstr(header, "\r\n")) != NULL && header < head_end) {
      header += 2;
      if (strncasecmp(header, "content-length:", 15) == 0) {
        char *end = NULL;
        errno = 0;
        unsigned long length = strtoul(header + 15, &end, 10);
        assert(errno == 0 && end != header + 15);
        assert(end[0] == '\r' && end[1] == '\n');
        assert(length < sizeof(request));
        body_len = (size_t)length;
        break;
      }
    }
    assert(body_len == strlen(body));
    size_t body_start = (size_t)(head_end + 4 - request);
    assert(body_len < sizeof(request) - body_start);
    while (used < body_start + body_len) {
      ssize_t count =
          read(client, request + used, body_start + body_len - used);
      assert(count > 0);
      used += (size_t)count;
    }
    assert(memcmp(request + body_start, body, body_len) == 0);
  }
  return client;
}

static pid_t start_box_server(uint16_t *port) {
  int listener = socket(AF_INET, SOCK_STREAM, 0);
  assert(listener >= 0);
  struct sockaddr_in address = {0};
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = htonl(0x7f000001U);
  assert(bind(listener, (struct sockaddr *)&address, sizeof(address)) == 0);
  assert(listen(listener, 1) == 0);
  socklen_t address_len = sizeof(address);
  assert(getsockname(listener, (struct sockaddr *)&address, &address_len) == 0);
  *port = ntohs(address.sin_port);

  pid_t child = fork();
  assert(child >= 0);
  if (child == 0) {
    alarm(15);
    int client =
        accept_request(listener, "GET /v1/boxes/agent-box HTTP/1.1\r\n", NULL);
    write_all(client,
              "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
              "Connection: close\r\n\r\n"
              "{\"box_id\":\"agent-box\",\"name\":null,\"status\":\"running\","
              "\"created_at\":\"2026-07-14T00:00:00Z\","
              "\"updated_at\":\"2026-07-14T00:00:00Z\",\"pid\":42,"
              "\"image\":\"alpine:latest\",\"cpus\":1,\"memory_mib\":512}");
    close(client);
    client =
        accept_request(listener, "POST /v1/boxes/agent-box/agents HTTP/1.1\r\n",
                       "{\"agent\":\"opencode\"}");
    write_all(
        client,
        "HTTP/1.1 201 Created\r\nContent-Type: application/json\r\n"
        "Connection: close\r\n\r\n"
        "{\"id\":\"ses_c_rest\",\"agent\":\"opencode\","
        "\"state\":{\"id\":\"ses_c_rest\",\"status\":\"ready\","
        "\"cursor\":null,\"active_turn_id\":null,\"queued_turn_ids\":[],"
        "\"requests\":[],\"steering\":[]},\"config\":{\"tools\":[],\"mcp\":[]},"
        "\"capabilities\":{\"operations\":[],\"input_kinds\":[],"
        "\"max_queued_turns\":null,\"max_pending_steering\":null,"
        "\"idempotency_retention_seconds\":null}}");
    close(client);
    close(listener);
    _exit(0);
  }
  close(listener);
  return child;
}

static void on_get(CBoxHandle *box, CBoxliteError *error, void *data) {
  GetRequest *request = data;
  request->box = box;
  request->error_code = error->code;
  request->done = 1;
}

static void test_live_box_namespace(void) {
  uint16_t port = 0;
  pid_t server = start_box_server(&port);
  char base_url[64];
  int url_len =
      snprintf( // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
          base_url, sizeof(base_url), "http://127.0.0.1:%u", port);
  assert(url_len > 0 && (size_t)url_len < sizeof(base_url));

  CBoxliteError error = {0};
  CBoxliteRestOptions *options = NULL;
  assert(boxlite_rest_options_new(base_url, &options, &error) == Ok);
  CBoxliteRuntime *runtime = NULL;
  assert(boxlite_rest_runtime_new_with_options(options, &runtime, &error) ==
         Ok);
  boxlite_rest_options_free(options);

  GetRequest request = {0};
  assert(boxlite_get(runtime, "agent-box", on_get, &request, &error) == Ok);
  for (int attempt = 0; attempt < 20 && !request.done; attempt++) {
    assert(boxlite_runtime_drain(runtime, 500, &error) >= 0);
  }
  assert(request.done && request.error_code == Ok && request.box != NULL);

  struct CAgents *agents = NULL;
  assert(boxlite_box_agents(request.box, &agents, &error) == Ok);
  assert(agents != NULL);
  AgentRequest agent_request = {.caller = pthread_self()};
  const char invalid_utf8[] = {(char)0xff, '\0'};
  struct CAgentCreateOptions invalid_options = {.config_json = "{not-json"};
  assert(boxlite_agents_create(agents, "opencode", &invalid_options,
                               on_agent_created, &agent_request,
                               &error) == InvalidArgument);
  boxlite_error_free(&error);
  invalid_options.config_json = "{\"model\":123}";
  assert(boxlite_agents_create(agents, "opencode", &invalid_options,
                               on_agent_created, &agent_request,
                               &error) == InvalidArgument);
  boxlite_error_free(&error);
  assert(boxlite_agents_create(agents, invalid_utf8, NULL, on_agent_created,
                               &agent_request, &error) == InvalidArgument);
  boxlite_error_free(&error);
  assert(boxlite_agents_create(agents, "opencode", NULL, NULL, &agent_request,
                               &error) == InvalidArgument);
  boxlite_error_free(&error);
  assert(!agent_request.done);
  assert(boxlite_runtime_drain(runtime, 0, &error) == 0);
  assert(boxlite_agents_create(agents, "opencode", NULL, on_agent_created,
                               &agent_request, &error) == Ok);
  assert(!agent_request.done);
  for (int attempt = 0; attempt < 20 && !agent_request.done; attempt++) {
    assert(boxlite_runtime_drain(runtime, 500, &error) >= 0);
  }
  assert(agent_request.done && agent_request.error_code == Ok);
  assert(agent_request.agent != NULL);
  char *state = NULL;
  assert(boxlite_agent_state(agent_request.agent, &state, &error) == Ok);
  assert(state != NULL && strstr(state, "\"id\":\"ses_c_rest\"") != NULL);
  boxlite_free_string(state);
  boxlite_agent_free(agent_request.agent);
  boxlite_agents_free(agents);
  boxlite_box_free(request.box);
  boxlite_runtime_free(runtime);
  boxlite_error_free(&error);

  int status = 0;
  assert(waitpid(server, &status, 0) == server);
  assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
}

static int callbacks = 0;
static void agent_cb(struct CAgent *agent, const CBoxliteError *error,
                     void *data) {
  (void)agent;
  (void)error;
  (void)data;
  callbacks++;
}
static void turn_cb(struct CAgentTurn *turn, const CBoxliteError *error,
                    void *data) {
  (void)turn;
  (void)error;
  (void)data;
  callbacks++;
}
static void json_cb(const char *json, const CBoxliteError *error, void *data) {
  (void)json;
  (void)error;
  (void)data;
  callbacks++;
}
static void done_cb(const CBoxliteError *error, void *data) {
  (void)error;
  (void)data;
  callbacks++;
}
#define CHECK_INVALID(call, operation)                                         \
  do {                                                                         \
    assert((call) == InvalidArgument);                                         \
    assert(error.code == InvalidArgument);                                     \
    boxlite_error_free(&error);                                                \
  } while (0)

int main(void) {
  test_live_box_namespace();
  CBoxliteError error = {0};
  struct CAgentCreateOptions create_options = {.cwd = "/workspace",
                                               .config_json =
                                                   "{\"model\":\"model\"}",
                                               .idempotency_key = "create-1"};
  struct CAgentSendOptions send_options = {.mode = AgentSendReject,
                                           .data_json = "{\"key\":1}",
                                           .idempotency_key = "send-1"};
  struct CAgentWaitOptions wait_options = {.timeout_seconds = 1};
  struct CAgentEventsOptions events_options = {.after = "cursor"};
  struct CAgents *agents = NULL;
  assert(boxlite_box_agents(NULL, &agents, &error) == InvalidArgument);
  assert(agents == NULL);
  boxlite_error_free(&error);
  CHECK_INVALID(boxlite_agents_create(NULL, "agent", &create_options, agent_cb,
                                      NULL, &error),
                "agents.create");
  CHECK_INVALID(boxlite_agents_get(NULL, "agent", agent_cb, NULL, &error),
                "agents.get");
  CHECK_INVALID(boxlite_agent_close(NULL, NULL, done_cb, NULL, &error),
                "agent.close");
  CHECK_INVALID(
      boxlite_agent_send(NULL, "task", &send_options, turn_cb, NULL, &error),
      "agent.send");
  CHECK_INVALID(boxlite_agent_get_turn(NULL, "turn", turn_cb, NULL, &error),
                "agent.get_turn");
  CHECK_INVALID(boxlite_agent_requests(NULL, json_cb, NULL, &error),
                "agent.requests");
  CHECK_INVALID(
      boxlite_agent_respond(NULL, "request",
                            "{\"type\":\"permission\",\"approved\":false}",
                            NULL, turn_cb, NULL, &error),
      "agent.respond");
  CHECK_INVALID(
      boxlite_agent_turn_wait(NULL, &wait_options, json_cb, NULL, &error),
      "turn.wait");
  CHECK_INVALID(
      boxlite_agent_turn_steer(NULL, "direction", NULL, json_cb, NULL, &error),
      "turn.steer");
  CHECK_INVALID(boxlite_agent_turn_interrupt(NULL, NULL, done_cb, NULL, &error),
                "turn.interrupt");
  CHECK_INVALID(
      boxlite_agent_events(NULL, &events_options, NULL, json_cb, NULL, &error),
      "agent.events");
  CHECK_INVALID(
      boxlite_agent_turn_events(NULL, NULL, NULL, json_cb, NULL, &error),
      "turn.events");
  char *json = (char *)&callbacks;
  CHECK_INVALID(boxlite_agent_state(NULL, &json, &error), "agent.state");
  assert(json == NULL);
  json = (char *)&callbacks;
  CHECK_INVALID(boxlite_agent_config(NULL, &json, &error), "agent.config");
  assert(json == NULL);
  json = (char *)&callbacks;
  CHECK_INVALID(boxlite_agent_capabilities(NULL, &json, &error),
                "agent.capabilities");
  assert(json == NULL);
  json = (char *)&callbacks;
  CHECK_INVALID(boxlite_agent_turn_state(NULL, &json, &error), "turn.state");
  assert(json == NULL);
  assert(callbacks == 0);
  boxlite_agents_free(NULL);
  boxlite_agent_free(NULL);
  boxlite_agent_turn_free(NULL);
  boxlite_agent_events_free(NULL);
  return 0;
}