Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,23 @@ Never commit credentials; use `.env.example` as reference.
| [LOCAL_INFERENCE.md](LOCAL_INFERENCE.md) | llama.cpp, models, tegrastats |
| [ASAP.md](ASAP.md) | Manifest, marketplace, compliance harness |
| [SECURITY.md](SECURITY.md) | Threat model, sandbox audit |

## Read scheduled jobs

Send an authenticated `GET /api/cron/page` request for the first 16 jobs, in ID order.
Each entry includes a schedule preview and a message preview. ShellClaw keeps the full text for delivery.
If `next_after` contains a cursor, send it in `GET /api/cron/page?after=CURSOR`.
Use the job `key` in `POST /api/cron/job/KEY/toggle` and `DELETE /api/cron/job/KEY`.
A null cursor means that the list is complete.

`GET /api/cron` returns the original array with full messages when it fits in one response.
If the list exceeds 64 jobs or the response buffer, this endpoint returns HTTP 413.

Send a schedule to `POST /api/cron/preview` to see the next run before you save it.
The gateway evaluates the expression in its local time zone. Both the day of the month and the weekday must match.
For example, `0 9 1 * 1` runs at 09:00 when the first day of a month is a Monday.

Each entry in a page includes `delivery_status`. A paused job stays paused after a restart.
A job with `waiting_for_channel` stays in the queue until its delivery channel becomes available.
A job with `invalid_destination` keeps this status when the scheduler disables it. A request to resume it returns HTTP 409.
Create a job with a supported destination before you delete the old job.
72 changes: 47 additions & 25 deletions src/core/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ static char *dup_sqlite_text(sqlite3_stmt *stmt, int col)
return strdup(p ? (const char *)p : "");
}

/* Copy complete UTF-8 characters. Keep the flag so the scheduler can reject a shortened address. */
static int copy_cron_destination(sqlite3_stmt *stmt, int column, char *dst, size_t capacity)
{
const unsigned char *text = sqlite3_column_text(stmt, column);
size_t length = (size_t)sqlite3_column_bytes(stmt, column);
size_t end = length;
if (length >= capacity) {
end = capacity - 1;
while (end > 0 && (text[end] & 0xc0) == 0x80)
end--;
}
if (text && end) memcpy(dst, text, end);
dst[end] = '\0';
return length >= capacity;
}

void cron_job_row_free(cron_job_row_t *row)
{
if (!row) return;
Expand All @@ -398,10 +414,8 @@ static int fill_cron_job_row(sqlite3_stmt *stmt, cron_job_row_t *out)
copy_str_bounded(out->id, sizeof(out->id), (const char *)sqlite3_column_text(stmt, 0));
out->schedule = dup_sqlite_text(stmt, 1);
out->message = dup_sqlite_text(stmt, 2);
out->channel_truncated = (size_t)sqlite3_column_bytes(stmt, 3) >= sizeof(out->channel);
out->recipient_truncated = (size_t)sqlite3_column_bytes(stmt, 4) >= sizeof(out->recipient);
copy_str_bounded(out->channel, sizeof(out->channel), (const char *)sqlite3_column_text(stmt, 3));
copy_str_bounded(out->recipient, sizeof(out->recipient), (const char *)sqlite3_column_text(stmt, 4));
out->channel_truncated = copy_cron_destination(stmt, 3, out->channel, sizeof(out->channel));
out->recipient_truncated = copy_cron_destination(stmt, 4, out->recipient, sizeof(out->recipient));
out->next_run = sqlite3_column_int64(stmt, 5);
out->enabled = sqlite3_column_int(stmt, 6);
if (!out->schedule || !out->message) {
Expand Down Expand Up @@ -490,26 +504,38 @@ int cron_job_update_next_run(const char *id, long long next_run)
return ret;
}

int cron_job_list(cron_job_row_t *out, int max_count)
static int read_cron_rows(const char *sql, const char *after, cron_job_row_t *out, int max_count)
{
sqlite3_stmt *stmt = NULL;
int count = 0, rc;
if (!g_db || !out || max_count <= 0) return -1;
memory_sqlite_entry_probe();
const char *sql = "SELECT id, schedule, message, channel, recipient, next_run, enabled "
"FROM cron_jobs ORDER BY next_run ASC, id ASC";
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(g_db, sql, -1, &stmt, NULL) != SQLITE_OK) return -1;
int count = 0;
while (count < max_count && sqlite3_step(stmt) == SQLITE_ROW) {
if (fill_cron_job_row(stmt, &out[count]) != 0) {
for (int i = 0; i < count; i++)
cron_job_row_free(&out[i]);
sqlite3_finalize(stmt);
return -1;
}
sqlite3_bind_int(stmt, 1, max_count);
if (after) sqlite3_bind_text(stmt, 2, after, -1, SQLITE_TRANSIENT);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
if (fill_cron_job_row(stmt, &out[count]) != 0) break;
count++;
}
sqlite3_finalize(stmt);
return count;
if (rc == SQLITE_DONE) return count;
for (int i = 0; i < count; i++) cron_job_row_free(&out[i]);
return -1;
}

int cron_job_list(cron_job_row_t *out, int max_count)
{
return read_cron_rows("SELECT id, schedule, message, channel, recipient, next_run, enabled "
"FROM cron_jobs ORDER BY next_run, id LIMIT ?1", NULL, out, max_count);
}

int cron_job_list_previews(const char *after, cron_job_row_t *out, int max_count)
{
/* SQLite substr counts Unicode characters and preserves complete UTF-8 sequences. */
const char *sql = "SELECT id, substr(schedule,1,128) || CASE WHEN length(schedule)>128 THEN '...' ELSE '' END, "
"substr(message,1,160) || CASE WHEN length(message)>160 THEN '...' ELSE '' END, "
"channel, recipient, next_run, enabled FROM cron_jobs WHERE id > ?2 ORDER BY id LIMIT ?1";
return read_cron_rows(sql, after ? after : "", out, max_count);
}

int cron_job_list_due(long long now, const cron_due_key_t *after,
Expand All @@ -535,14 +561,10 @@ int cron_job_list_due(long long now, const cron_due_key_t *after,
copy_str_bounded(out[count].id, sizeof(out[count].id),
(const char *)sqlite3_column_text(stmt, 0));
out[count].next_run = sqlite3_column_int64(stmt, 1);
out[count].channel_truncated =
(size_t)sqlite3_column_bytes(stmt, 2) >= sizeof(out[count].channel);
out[count].recipient_truncated =
(size_t)sqlite3_column_bytes(stmt, 3) >= sizeof(out[count].recipient);
copy_str_bounded(out[count].channel, sizeof(out[count].channel),
(const char *)sqlite3_column_text(stmt, 2));
copy_str_bounded(out[count].recipient, sizeof(out[count].recipient),
(const char *)sqlite3_column_text(stmt, 3));
out[count].channel_truncated = copy_cron_destination(stmt, 2, out[count].channel,
sizeof(out[count].channel));
out[count].recipient_truncated = copy_cron_destination(stmt, 3, out[count].recipient,
sizeof(out[count].recipient));
count++;
}
sqlite3_finalize(stmt);
Expand Down
3 changes: 3 additions & 0 deletions src/core/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ int cron_job_list_due(long long now, const cron_due_key_t *after,
*/
int cron_job_list(cron_job_row_t *out, int max_count);

/** List job previews after an exclusive ID, in ID order. Free each returned row. */
int cron_job_list_previews(const char *after, cron_job_row_t *out, int max_count);

/**
* Get the next due job (next_run <= now, enabled).
*
Expand Down
8 changes: 5 additions & 3 deletions src/gateway/http_lws.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ static int http_body_content_length(struct lws *wsi, long *cl_out)
return 0;
}

enum { HTTP_REQUEST_URI_SIZE = 512 };

static int http_copy_request_uri(struct lws *wsi, char *uri, int uri_size)
{
int n;
Expand All @@ -94,7 +96,7 @@ static int http_copy_request_uri(struct lws *wsi, char *uri, int uri_size)

static void http_dispatch_body(http_server_ctx_t *ctx, struct lws *wsi, http_conn_t *conn)
{
char uri[256];
char uri[HTTP_REQUEST_URI_SIZE];
int uri_len;
int method;
if (!conn || !conn->has_body || conn->body_dispatched)
Expand Down Expand Up @@ -189,7 +191,7 @@ int http_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user,
if (!ctx) return 0;
switch (reason) {
case LWS_CALLBACK_HTTP: {
char uri[256];
char uri[HTTP_REQUEST_URI_SIZE];
int uri_len = http_copy_request_uri(wsi, uri, sizeof(uri));
if (uri_len <= 0 || uri_len >= (int)sizeof(uri)) {
lws_return_http_status(wsi, 400, "Bad request");
Expand Down Expand Up @@ -245,7 +247,7 @@ int http_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user,
const unsigned char *data = NULL;
size_t data_len = 0;
const char *ct = NULL;
char path_buf[256];
char path_buf[sizeof(uri)];
if (uri_len >= (int)sizeof(path_buf)) uri_len = (int)sizeof(path_buf) - 1;
memcpy(path_buf, uri, (size_t)uri_len);
path_buf[uri_len] = '\0';
Expand Down
Loading
Loading