Files
tasksd/tasksd-http.c
Hermes Agent 0a084532bc tasksd-http: Web-Oberflaeche als CGI; task_update; TASKDIR
tasksd-http ist ein C99-CGI-Programm (HTML 4.01) ueber den MCP-Server von
tasksd (vendor/cgi/cgi.h, tests/test-http.sh, Makefile, README).

Neues Werkzeug task_update: korrigiert die beschreibenden Felder einer Task
(title, goal, scope, tags, priority); Zustandswechsel bleiben bei
task_next/done/pause/drop.  Die Umgebungsvariable TASKDIR bleibt als alter
Name gueltig.  tests/run.py angepasst.
2026-11-19 09:55:33 +00:00

652 lines
19 KiB
C

/*
* tasksd-http — CGI front-end for tasksd.
*
* C99, HTML 4.01, no JavaScript, no auth. Every list, read, create, edit and
* state change goes through tasksd's MCP server. The CGI never reads the task
* files itself.
*
* Routes (mounted at /cgi-bin/tasksd-http under busybox httpd):
* / — tasks, filter with ?state=…&scope=…
* /scopes — scope overview with counts
* /new — create a task
* /t/<id> — one task with controls
* /t/<id>/edit — title, goal, scope, priority, tags
* /t/<id>/pause — POST, note optional
* /t/<id>/resume — POST, note optional
* /t/<id>/done — POST, result optional
* /t/<id>/drop — POST, result optional
*
* Environment: TASKSD_URL (default http://127.0.0.1:8781/mcp)
*
* Build:
* cc -std=c99 -O2 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L \
* -Ivendor/libmcp -Ivendor/cgi -o tasksd-http tasksd-http.c
*/
#define _POSIX_C_SOURCE 200809L
#define MCP_IMPLEMENTATION
#include "mcp.h"
#include "cgi.h"
#define ENV_URL "TASKSD_URL"
#define DEF_URL "http://127.0.0.1:8781/mcp"
#define APP "tasksd-http"
static void state_badge(const char *state)
{
const char *s = state != NULL ? state : "?";
fputs("<span class=\"state s-", stdout);
cg_html(s);
fputs("\">", stdout);
cg_html(s);
fputs("</span>", stdout);
}
static void state_tabs(const char *scope, const char *active)
{
static const char *states[] = { "", "pending", "running", "paused",
"done", "dropped" };
static const char *labels[] = { "All", "Pending", "Running", "Paused",
"Done", "Dropped" };
fputs("<div class=\"actions\">", stdout);
for (size_t i = 0; i < sizeof(states) / sizeof(states[0]); i++) {
int on = active != NULL && strcmp(active, states[i]) == 0;
if (active == NULL && states[i][0] == '\0')
on = 1;
fputs("<a href=\"", stdout);
cg_link("/?state=");
fputs(states[i], stdout);
if (scope != NULL && *scope != '\0') {
fputs("&amp;scope=", stdout);
cg_urlenc(scope);
}
fputs("\">", stdout);
if (on)
fputs("<b>", stdout);
fputs(labels[i], stdout);
if (on)
fputs("</b>", stdout);
fputs("</a> ", stdout);
}
fputs("</div>\n", stdout);
}
static void task_row(const mcp_json *t)
{
const char *id = mcp_arg_string(t, "id");
const char *title = mcp_arg_string(t, "title");
const char *scope = mcp_arg_string(t, "scope");
const char *state = mcp_arg_string(t, "state");
const char *tags = mcp_arg_string(t, "tags");
double prio = mcp_arg_num(t, "priority", 0);
if (id == NULL)
return;
fputs("<tr><td><a href=\"", stdout);
cg_link("/t/");
cg_html(id);
fputs("\">", stdout);
cg_html(id);
fputs("</a></td><td>", stdout);
state_badge(state);
fputs("</td><td><a href=\"", stdout);
cg_link("/t/");
cg_html(id);
fputs("\"><b>", stdout);
cg_html(title != NULL ? title : "(untitled)");
fputs("</b></a><br><span class=\"meta\">scope ", stdout);
cg_html(scope != NULL ? scope : "");
printf(" · priority %.0f", prio);
if (tags != NULL && *tags != '\0') {
fputs(" · tags ", stdout);
cg_html(tags);
}
fputs("</span></td><td>", stdout);
cg_time((long)mcp_arg_num(t, "created", 0));
fputs("</td></tr>\n", stdout);
}
static void render_tasks(const mcp_json *arr)
{
size_t n = mcp_array_len(arr);
if (n == 0) {
fputs("<p class=\"meta\">No tasks match.</p>\n", stdout);
return;
}
fputs("<table class=\"list\" summary=\"Tasks\"><tr><th>ID</th><th>State</th><th>Task</th>"
"<th>Created</th></tr>\n", stdout);
for (size_t i = 0; i < n; i++)
task_row(mcp_array_at(arr, i));
fputs("</table>\n", stdout);
}
static void route_list(const char *query)
{
char *scope = cg_param(query, "scope");
char *state = cg_param(query, "state");
char *limstr = cg_param(query, "limit");
int limit = limstr != NULL ? atoi(limstr) : 100;
mcp_buf args;
int first = 1;
mcp_json *v;
if (limit <= 0 || limit > 500)
limit = 100;
mcp_buf_init(&args);
mcp_buf_puts(&args, "{");
cg_args_add_str(&args, &first, "scope", scope);
if (state != NULL && *state != '\0' && strcmp(state, "all") != 0)
cg_args_add_str(&args, &first, "state", state);
cg_args_add_int(&args, &first, "limit", limit);
mcp_buf_puts(&args, "}");
v = cg_rpc_json(ENV_URL, DEF_URL, "task_list", args.p);
mcp_buf_free(&args);
cg_head(APP, "Tasks");
fputs("<h1>Tasks</h1>\n", stdout);
state_tabs(scope, state);
fputs("<form class=\"inline\" action=\"", stdout);
cg_link("/");
fputs("\" method=\"get\"><label>Scope "
"<input type=\"text\" name=\"scope\" size=\"28\" value=\"", stdout);
cg_html(scope != NULL ? scope : "");
fputs("\"></label> ", stdout);
if (state != NULL && *state != '\0') {
fputs("<input type=\"hidden\" name=\"state\" value=\"", stdout);
cg_html(state);
fputs("\">", stdout);
}
fputs("<input type=\"submit\" value=\"Filter\"></form>\n", stdout);
fputs("<div class=\"actions\"><a href=\"", stdout);
cg_link("/new");
fputs("\">Create a task</a><a href=\"", stdout);
cg_link("/scopes");
fputs("\">Scopes</a></div>\n", stdout);
if (v == NULL) {
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
} else {
render_tasks(mcp_arg(v, "tasks"));
printf("<p class=\"meta\">%.0f task(s) shown</p>\n",
mcp_arg_num(v, "count", 0));
mcp_json_free(v);
}
cg_foot();
free(scope);
free(state);
free(limstr);
}
static void route_scopes(void)
{
mcp_json *v = cg_rpc_json(ENV_URL, DEF_URL, "task_scopes", "{}");
cg_head(APP, "Task scopes");
fputs("<h1>Task scopes</h1>\n", stdout);
if (v == NULL) {
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
} else {
const mcp_json *arr = mcp_arg(v, "scopes");
size_t n = mcp_array_len(arr);
fputs("<table class=\"list\" summary=\"Task counts per scope\"><tr><th>Scope</th><th>Pending</th>"
"<th>Running</th><th>Paused</th><th>Done</th><th>Dropped</th>"
"</tr>\n", stdout);
for (size_t i = 0; i < n; i++) {
const mcp_json *s = mcp_array_at(arr, i);
const char *scope = mcp_arg_string(s, "scope");
if (scope == NULL)
continue;
fputs("<tr><td><a href=\"", stdout);
cg_link("/?scope=");
cg_urlenc(scope);
fputs("\">", stdout);
cg_html(scope);
fputs("</a></td>", stdout);
printf("<td>%.0f</td><td>%.0f</td><td>%.0f</td>"
"<td>%.0f</td><td>%.0f</td></tr>\n",
mcp_arg_num(s, "pending", 0),
mcp_arg_num(s, "running", 0),
mcp_arg_num(s, "paused", 0),
mcp_arg_num(s, "done", 0),
mcp_arg_num(s, "dropped", 0));
}
fputs("</table>\n", stdout);
mcp_json_free(v);
}
fputs("<div class=\"actions\"><a href=\"", stdout);
cg_link("/");
fputs("\">All tasks</a></div>\n", stdout);
cg_foot();
}
static void task_kv(const char *key, const char *val)
{
if (val == NULL || *val == '\0')
return;
fputs("<div class=\"kv\"><b>", stdout);
cg_html(key);
fputs("</b><br>", stdout);
cg_md(val);
fputs("</div>\n", stdout);
}
static void action_form(const char *id, const char *action, const char *label,
const char *field, const char *placeholder)
{
fputs("<form method=\"post\" action=\"", stdout);
cg_link("/t/");
cg_html(id);
fputs("/", stdout);
cg_html(action);
fputs("\"><p><label>", stdout);
cg_html(placeholder);
fputs("<br><textarea name=\"", stdout);
cg_html(field);
fputs("\" rows=\"3\" cols=\"70\"></textarea></label></p>"
"<p><input type=\"submit\" value=\"", stdout);
cg_html(label);
fputs("\"></p></form>\n", stdout);
}
static void task_actions(const char *id, const char *state)
{
int paused = state != NULL && strcmp(state, "paused") == 0;
int closed = state != NULL &&
(strcmp(state, "done") == 0 || strcmp(state, "dropped") == 0);
fputs("<h2>Controls</h2>\n", stdout);
if (paused) {
action_form(id, "resume", "Resume", "note",
"Note (what was left, why now continues)");
} else if (!closed) {
action_form(id, "pause", "Pause", "note",
"Note (why parked, what is left)");
}
if (!closed) {
action_form(id, "done", "Mark done", "result",
"Result (what was achieved, evidence)");
action_form(id, "drop", "Drop", "result",
"Reason (why it is not worth doing)");
}
}
static void route_detail(const char *id)
{
mcp_buf args;
int first = 1;
char *text;
mcp_json *v;
mcp_buf_init(&args);
mcp_buf_puts(&args, "{");
cg_args_add_str(&args, &first, "id", id);
mcp_buf_puts(&args, "}");
text = cg_rpc(ENV_URL, DEF_URL, "task_get", args.p);
mcp_buf_free(&args);
if (text == NULL) {
cg_error_page(APP, "Task not found", "tasksd did not answer.");
return;
}
v = mcp_parse(text);
free(text);
if (v == NULL) {
cg_error_page(APP, "Task not found", "tasksd returned no task.");
return;
}
cg_head(APP, id);
{
const char *title = mcp_arg_string(v, "title");
const char *scope = mcp_arg_string(v, "scope");
const char *state = mcp_arg_string(v, "state");
const char *tags = mcp_arg_string(v, "tags");
const char *result = mcp_arg_string(v, "result");
fputs("<h1>", stdout);
cg_html(title != NULL ? title : id);
fputs("</h1>\n<p>", stdout);
state_badge(state);
fputs(" <span class=\"meta\">", stdout);
cg_html(id);
fputs(" · scope ", stdout);
if (scope != NULL) {
fputs("<a href=\"", stdout);
cg_link("/?scope=");
cg_urlenc(scope);
fputs("\">", stdout);
cg_html(scope);
fputs("</a>", stdout);
}
printf(" · priority %.0f", mcp_arg_num(v, "priority", 0));
if (tags != NULL && *tags != '\0') {
fputs(" · tags ", stdout);
cg_html(tags);
}
fputs("</span></p>\n", stdout);
task_kv("Goal", mcp_arg_string(v, "goal"));
if (result != NULL && *result != '\0')
task_kv("Result", result);
fputs("<p class=\"meta\">created ", stdout);
cg_time((long)mcp_arg_num(v, "created", 0));
fputs(" · started ", stdout);
cg_time((long)mcp_arg_num(v, "started", 0));
fputs(" · ended ", stdout);
cg_time((long)mcp_arg_num(v, "ended", 0));
fputs("</p>\n", stdout);
fputs("<div class=\"actions\"><a href=\"", stdout);
cg_link("/t/");
cg_html(id);
fputs("/edit\">Edit title/goal/scope</a><a href=\"", stdout);
cg_link("/");
fputs("\">All tasks</a></div>\n", stdout);
task_actions(id, state);
}
mcp_json_free(v);
cg_foot();
}
static void edit_form(const char *id, const mcp_json *v)
{
const char *title = mcp_arg_string(v, "title");
const char *goal = mcp_arg_string(v, "goal");
const char *scope = mcp_arg_string(v, "scope");
const char *tags = mcp_arg_string(v, "tags");
fputs("<form method=\"post\" action=\"", stdout);
cg_link("/t/");
cg_html(id);
fputs("/edit\">\n<p><label>Title<br>"
"<input type=\"text\" name=\"title\" size=\"76\" value=\"", stdout);
cg_html(title != NULL ? title : "");
fputs("\"></label></p>\n<p><label>Goal (why it exists)<br>"
"<textarea name=\"goal\" rows=\"8\" cols=\"76\">", stdout);
cg_html(goal != NULL ? goal : "");
fputs("</textarea></label></p>\n<p><label>Scope<br>"
"<input type=\"text\" name=\"scope\" size=\"40\" value=\"", stdout);
cg_html(scope != NULL ? scope : "");
fputs("\"></label></p>\n<p><label>Priority<br>"
"<input type=\"text\" name=\"priority\" size=\"8\" value=\"", stdout);
printf("%.0f", mcp_arg_num(v, "priority", 0));
fputs("\"></label></p>\n<p><label>Tags (comma separated)<br>"
"<input type=\"text\" name=\"tags\" size=\"48\" value=\"", stdout);
cg_html(tags != NULL ? tags : "");
fputs("\"></label></p>\n<p><input type=\"submit\" value=\"Save\"></p>\n"
"</form>\n", stdout);
}
static void route_edit(const char *id, const char *body)
{
if (strcmp(cg_method(), "POST") == 0) {
char *title = cg_param(body, "title");
char *goal = cg_param(body, "goal");
char *scope = cg_param(body, "scope");
char *tags = cg_param(body, "tags");
char *prio = cg_param(body, "priority");
mcp_buf args;
int first = 1;
mcp_json *v;
mcp_buf_init(&args);
mcp_buf_puts(&args, "{");
cg_args_add_str(&args, &first, "id", id);
cg_args_add_str(&args, &first, "title", title);
cg_args_add_str(&args, &first, "goal", goal);
cg_args_add_str(&args, &first, "scope", scope);
cg_args_add_str(&args, &first, "tags", tags);
if (prio != NULL && *prio != '\0')
cg_args_add_int(&args, &first, "priority", atoi(prio));
mcp_buf_puts(&args, "}");
v = cg_rpc_json(ENV_URL, DEF_URL, "task_update", args.p);
mcp_buf_free(&args);
cg_head(APP, "Edit task");
if (v == NULL) {
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
} else {
fputs("<h1>Updated</h1>\n<div class=\"msg\">", stdout);
cg_html(mcp_arg_string(v, "id"));
fputs(" — ", stdout);
cg_html(mcp_arg_string(v, "title"));
fputs("</div>\n<div class=\"actions\"><a href=\"", stdout);
cg_link("/t/");
cg_html(id);
fputs("\">Back to the task</a></div>\n", stdout);
mcp_json_free(v);
}
cg_foot();
free(title); free(goal); free(scope); free(tags); free(prio);
return;
}
{
mcp_buf args;
int first = 1;
char *text;
mcp_json *v;
mcp_buf_init(&args);
mcp_buf_puts(&args, "{");
cg_args_add_str(&args, &first, "id", id);
mcp_buf_puts(&args, "}");
text = cg_rpc(ENV_URL, DEF_URL, "task_get", args.p);
mcp_buf_free(&args);
cg_head(APP, "Edit task");
fputs("<h1>Edit ", stdout);
cg_html(id);
fputs("</h1>\n", stdout);
if (text == NULL) {
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
} else {
v = mcp_parse(text);
free(text);
if (v != NULL) {
edit_form(id, v);
mcp_json_free(v);
} else {
fputs("<div class=\"err\">Task not found.</div>\n", stdout);
}
}
cg_foot();
}
}
static void route_new(const char *body)
{
if (strcmp(cg_method(), "POST") != 0) {
cg_head(APP, "Create a task");
fputs("<h1>Create a task</h1>\n<form method=\"post\" action=\"", stdout);
cg_link("/new");
fputs("\">\n<p><label>Title (what to do)<br>"
"<input type=\"text\" name=\"title\" size=\"76\"></label></p>\n"
"<p><label>Goal (why it exists — required)<br>"
"<textarea name=\"goal\" rows=\"8\" cols=\"76\"></textarea>"
"</label></p>\n"
"<p><label>Scope (e.g. makronix, fleet/console)<br>"
"<input type=\"text\" name=\"scope\" size=\"40\"></label></p>\n"
"<p><label>Priority (higher runs first)<br>"
"<input type=\"text\" name=\"priority\" size=\"8\" value=\"0\">"
"</label></p>\n"
"<p><label>Tags (comma separated)<br>"
"<input type=\"text\" name=\"tags\" size=\"48\"></label></p>\n"
"<p><input type=\"submit\" value=\"Create task\"></p>\n</form>\n",
stdout);
cg_foot();
return;
}
{
char *title = cg_param(body, "title");
char *goal = cg_param(body, "goal");
char *scope = cg_param(body, "scope");
char *tags = cg_param(body, "tags");
char *prio = cg_param(body, "priority");
mcp_buf args;
int first = 1;
mcp_json *v;
if (title == NULL || *title == '\0' || goal == NULL ||
*goal == '\0' || scope == NULL || *scope == '\0') {
cg_error_page(APP, "Create a task",
"Title, goal and scope are required.");
free(title); free(goal); free(scope); free(tags); free(prio);
return;
}
mcp_buf_init(&args);
mcp_buf_puts(&args, "{");
cg_args_add_str(&args, &first, "title", title);
cg_args_add_str(&args, &first, "goal", goal);
cg_args_add_str(&args, &first, "scope", scope);
if (prio != NULL && *prio != '\0')
cg_args_add_int(&args, &first, "priority", atoi(prio));
cg_args_add_str(&args, &first, "tags", tags);
mcp_buf_puts(&args, "}");
v = cg_rpc_json(ENV_URL, DEF_URL, "task_create", args.p);
mcp_buf_free(&args);
cg_head(APP, "Create a task");
if (v == NULL) {
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
} else {
const char *id = mcp_arg_string(v, "id");
fputs("<h1>Created</h1>\n<div class=\"msg\">", stdout);
cg_html(id != NULL ? id : "");
fputs(" — ", stdout);
cg_html(mcp_arg_string(v, "title"));
fputs("</div>\n<div class=\"actions\"><a href=\"", stdout);
cg_link("/t/");
cg_html(id != NULL ? id : "");
fputs("\">Open the task</a><a href=\"", stdout);
cg_link("/");
fputs("\">All tasks</a></div>\n", stdout);
mcp_json_free(v);
}
cg_foot();
free(title); free(goal); free(scope); free(tags); free(prio);
}
}
static void route_state(const char *id, const char *action, const char *body)
{
const char *tool = NULL;
const char *field = NULL;
char *note = NULL;
mcp_buf args;
int first = 1;
mcp_json *v;
if (strcmp(cg_method(), "POST") != 0) {
cg_error_page(APP, "Task state", "State changes need a POST.");
return;
}
if (strcmp(action, "pause") == 0) {
tool = "task_pause";
field = "note";
} else if (strcmp(action, "resume") == 0) {
tool = "task_resume";
field = "note";
} else if (strcmp(action, "done") == 0) {
tool = "task_done";
field = "result";
} else if (strcmp(action, "drop") == 0) {
tool = "task_drop";
field = "result";
}
if (tool == NULL) {
cg_error_page(APP, "Task state", "Unknown action.");
return;
}
note = cg_param(body, field);
mcp_buf_init(&args);
mcp_buf_puts(&args, "{");
cg_args_add_str(&args, &first, "id", id);
cg_args_add_str(&args, &first, field, note);
mcp_buf_puts(&args, "}");
v = cg_rpc_json(ENV_URL, DEF_URL, tool, args.p);
mcp_buf_free(&args);
cg_head(APP, "Task state");
if (v == NULL) {
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
} else {
const char *state = mcp_arg_string(v, "state");
fputs("<h1>", stdout);
cg_html(state != NULL ? state : "changed");
fputs("</h1>\n<div class=\"msg\">", stdout);
cg_html(mcp_arg_string(v, "id"));
fputs(" — ", stdout);
cg_html(mcp_arg_string(v, "title"));
fputs("</div>\n<div class=\"actions\"><a href=\"", stdout);
cg_link("/t/");
cg_html(id);
fputs("\">Back to the task</a><a href=\"", stdout);
cg_link("/");
fputs("\">All tasks</a></div>\n", stdout);
mcp_json_free(v);
}
cg_foot();
free(note);
}
/* ------------------------------------------------------------------ router */
static void handle(const char *path, const char *query, const char *body)
{
if (strcmp(path, "/") == 0 || strcmp(path, "") == 0) {
route_list(query);
return;
}
if (strcmp(path, "/scopes") == 0) {
route_scopes();
return;
}
if (strcmp(path, "/new") == 0) {
route_new(body);
return;
}
if (strncmp(path, "/t/", 3) == 0) {
const char *rest = path + 3;
size_t idlen = strcspn(rest, "/");
char id[128];
const char *tail;
if (idlen == 0 || idlen >= sizeof(id)) {
cg_error_page(APP, "Not found", "No task id in the path.");
return;
}
memcpy(id, rest, idlen);
id[idlen] = '\0';
cg_url_decode(id);
tail = rest + idlen;
if (strcmp(tail, "/edit") == 0) {
route_edit(id, body);
return;
}
if (tail[0] == '/' && tail[1] != '\0') {
route_state(id, tail + 1, body);
return;
}
if (*tail == '\0') {
route_detail(id);
return;
}
}
cg_error_page(APP, "Not found", "No such page.");
}
int main(void)
{
char path[4096], query[4096];
char *body = NULL;
cg_route(path, sizeof(path), query, sizeof(query));
if (strcmp(cg_method(), "POST") == 0)
body = cg_read_body();
cg_header();
handle(path, query, body != NULL ? body : "");
free(body);
return 0;
}