Skip to content
pi-c Artifact [f952ac067d]
Login

Artifact [f952ac067d]

Artifact f952ac067dcae14932c587134ed791e684525ae84e0ceee99bcf728496276703:


/* pic bash: a command the user runs ("!cmd" in the terminal), journaled as Pi's
 * bashExecution message. It runs through the same supervisor as the bash tool
 * (output limits, process group, abort on Ctrl-C), but is not a tool call: the
 * model sees it as a user message on its next request (unless excluded). */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pic.h"
#include "procs.h"

/* Pi's bashExecutionToText. */
char *pic_bash_text(const pic_bash_msg *m) {
    size_t cap = strlen(m->command) + strlen(m->output) + 96;
    char *t = malloc(cap);
    int n;
    if (!t) {
        return NULL;
    }
    n = snprintf(t, cap, "Ran `%s`\n", m->command);
    if (m->output[0]) {
        n += snprintf(t + n, cap - (size_t)n, "```\n%s\n```", m->output);
    } else {
        n += snprintf(t + n, cap - (size_t)n, "(no output)");
    }
    if (m->cancelled) {
        n += snprintf(t + n, cap - (size_t)n, "\n\n(command cancelled)");
    } else if (m->has_exit && m->exit_code != 0) {
        n += snprintf(t + n, cap - (size_t)n, "\n\nCommand exited with code %d", m->exit_code);
    }
    (void)n;
    return t;
}

/* The text without escape sequences; CRLF and a lone CR become LF. In place. */
static void clean(char *s) {
    size_t o = 0;
    for (size_t i = 0; s[i]; i++) {
        if (s[i] == 0x1b && s[i + 1] == '[') {
            i += 2;
            while (s[i] && !(s[i] >= 0x40 && s[i] <= 0x7e)) {
                i++;
            }
            if (!s[i]) {
                break;
            }
        } else if (s[i] == '\r') {
            if (s[i + 1] != '\n') {
                s[o++] = '\n';
            }
        } else {
            s[o++] = s[i];
        }
    }
    s[o] = '\0';
}

typedef struct {
    pic_store *s;
    void (*live)(void *ctx, const char *text);
    void *ctx;
} live_ctx;

static void on_partial(void *p, const char *text) {
    live_ctx *l = p;
    char *copy = strdup(text);
    if (copy) {
        clean(copy);
        l->live(l->ctx, copy);
        free(copy);
    }
}

static int started(void *ctx, const char *plan, size_t plan_len) {
    (void)ctx;
    (void)plan;
    (void)plan_len;
    return 0;
}

int pic_bash_run(pic_store *s, const char *workspace, const char *session, const char *command, int exclude,
                 int exec_seconds, void (*live)(void *ctx, const char *text), void *ctx, pic_bash_msg *out,
                 char *session_out, size_t cap) {
    sqlite3_int64 seq = pic_next_entry_seq(s);
    char name[48], *result, *full_copy = NULL;
    pic_tool_output full = {NULL, 0};
    live_ctx l = {s, live, ctx};
    pic_tool_hooks hooks;
    pic_tool_args args;
    int is_error = 0, rc;
    size_t len;
    const char *suffix;
    if (!seq) {
        return -1;
    }
    (void)snprintf(name, sizeof name, "pic:bash/%lld", (long long)seq);
    memset(&hooks, 0, sizeof hooks);
    memset(&args, 0, sizeof args);
    hooks.exec_seconds = exec_seconds;
    hooks.started = started;
    hooks.output_name = name;
    hooks.output = &full;
    hooks.proc_kind = "user-bash";
    pic_proc_scope(workspace, session && strcmp(session, "latest") != 0 ? session : NULL);
    if (live) {
        hooks.partial = on_partial;
        hooks.partial_ctx = &l;
    }
    args.command = command;
    result = pic_tool_run(workspace, "bash", &args, &hooks, &is_error);
    if (!result) {
        free(full.data);
        (void)snprintf(s->err, sizeof s->err, "could not run the command");
        return -1;
    }
    memset(out, 0, sizeof *out);
    out->command = command;
    out->exclude = exclude;
    len = strlen(result);
    /* The supervisor's status line, if any, ends the result. */
    suffix = strstr(result, "\n\nCommand ");
    while (suffix && strstr(suffix + 2, "\n\nCommand ")) {
        suffix = strstr(suffix + 2, "\n\nCommand ");
    }
    if (suffix && strcmp(suffix, "\n\nCommand aborted") == 0) {
        out->cancelled = 1;
        result[len - strlen(suffix)] = '\0';
    } else if (suffix && sscanf(suffix, "\n\nCommand exited with code %d", &out->exit_code) == 1) {
        out->has_exit = 1;
        result[len - strlen(suffix)] = '\0';
    } else if (!is_error) {
        out->has_exit = 1; /* exit 0 */
    }
    if (strcmp(result, "(no output)") == 0) {
        result[0] = '\0';
    }
    out->truncated = strstr(result, "\n\n[Showing ") != NULL || strstr(result, "[Showing last ") != NULL;
    clean(result);
    out->output = result;
    if (full.data) {
        full_copy = malloc(full.len + 1); /* the supervisor's copy has no terminator */
        if (full_copy) {
            memcpy(full_copy, full.data, full.len);
            full_copy[full.len] = '\0';
            clean(full_copy);
            out->full = full_copy;
            out->full_len = strlen(full_copy);
        }
        free(full.data);
    }
    /* Another pic may journal an entry while the command runs, taking the
     * number its output was named for: the name in the notice follows. */
    for (int tries = 0;; tries++) {
        sqlite3_int64 named = seq;
        char old_name[48], *at = NULL, *renamed;
        rc = pic_bash_record(s, session, out, session_out, cap, NULL, &named);
        if (rc != 1) {
            break;
        }
        (void)snprintf(old_name, sizeof old_name, "pic:bash/%lld", (long long)seq);
        for (char *hit = strstr(result, old_name); hit; hit = strstr(hit + 1, old_name)) {
            at = hit; /* the notice ends the output */
        }
        if (tries >= 100) {
            (void)snprintf(s->err, sizeof s->err, "could not journal the command: the journal is too busy");
            rc = -1;
            break;
        }
        if (!at) { /* nothing was named */
            seq = named;
            continue;
        }
        if (!(renamed = malloc(strlen(result) + 32))) {
            (void)snprintf(s->err, sizeof s->err, "out of memory");
            rc = -1;
            break;
        }
        (void)snprintf(renamed, strlen(result) + 32, "%.*spic:bash/%lld%s", (int)(at - result), result, (long long)named,
                       at + strlen(old_name));
        free(result);
        out->output = result = renamed;
        seq = named;
    }
    free(full_copy);
    out->full = NULL;
    return rc;
}