Files
codebase-memory-mcp/src/ui/http_server.h
T
Martin Vogel d58afe562d revert(release): re-embed runtime assets into the single shipped binary
Externalizing the integration templates (#1492/#1493) and the UI bundle
(#1501/#1503) was done to reduce the Microsoft `Wacatac.B!ml` surface. It did
not work: across dry runs the flagged artifact count stayed at ~3 and the
detections merely moved between artifacts.

Dissection of run 31286803592 shows there is no structural cause to fix. The
verdicts split across every axis at once — linux-amd64 (dynamic) flagged while
linux-amd64-portable (static) is clean, but linux-arm64 (dynamic) clean while
linux-arm64-portable (static) is flagged. The two macOS binaries have identical
segment structure and split clean/flagged. Siblings from one build landed in
different variant buckets (.B vs .C). Entropy is low everywhere
(code_vectors.bin 4.166, grammar tables 3.464 bits/byte, against 7.5-8.0 for
packed payloads), so the packed-payload hypothesis is excluded too.

So the complexity bought nothing, and installation goes back to being
self-contained: one binary that carries its own UI and agent integration
templates, with no adjacent data file that has to resolve before `install`
works. Only the UI-capable composition ships from now on, under the historical
unsuffixed archive name.

Removed: src/ui/asset_pack.{c,h}, asset_pack_stub.c, asset_manifest_stub.c,
scripts/pack-ui-assets.mjs, src/cli/integration_assets.{c,h},
assets/cbm-integrations.json, scripts/gen-integrations-hash.sh, the
--verify-runtime-assets probe (nothing adjacent left to verify), and the
composition gates A6/A7 whose property is now deliberately inverted.

Restored: scripts/embed-frontend.sh, src/ui/embedded_{assets.h,stub.c}, the
compiled-in hook/adapter template bodies, and the embed/EMBED_OBJS build path.

Kept from the reverted commits, re-applied by hand where a wholesale file
restore would have dropped them:
  - cbm_module_path_utf8() in both self-path sites. GetModuleFileNameA renders
    through the ANSI code page and mangles non-ASCII install paths.
  - the /__cbm/ui-readiness HMAC proof, secure_random and cbm_hmac_sha256, so
    `daemon start --open` still waits for a genuine CBM listener.
  - X-Content-Type-Options: nosniff on served assets.
  - the MinGW noexecstack gate, -lbcrypt, and the cppcheck/zip CI fixes.

Archives are now codebase-memory-mcp-<os>-<arch>[-portable] with exactly four
members (binary, LICENSE, installer, THIRD_PARTY_NOTICES.md). That restores the
names every static package manifest already points at — aur, chocolatey,
homebrew, scoop, winget and glama were all broken by the -ui- rename.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-08-09 13:06:42 +02:00

110 lines
5.0 KiB
C

/*
* http_server.h — Embedded HTTP server for the graph visualization UI.
*
* Binds to 127.0.0.1:<port> only (localhost).
* Serves embedded frontend assets and proxies /rpc to a dedicated
* read-only cbm_mcp_server_t instance.
*
* Runs in a background pthread, same pattern as the watcher thread.
*/
#ifndef CBM_UI_HTTP_SERVER_H
#define CBM_UI_HTTP_SERVER_H
#include "ui/httpd.h"
#include "foundation/sha256.h"
#include <stdbool.h>
#include <stddef.h>
typedef struct cbm_http_server cbm_http_server_t;
struct cbm_watcher;
/* Daemon-owned index boundary. The callback blocks until the coordinated
* operation finishes; the HTTP server runs it on a tracked, joinable thread. */
typedef int (*cbm_http_index_executor_fn)(void *context, const char *root_path,
const char *project_name);
typedef bool (*cbm_http_project_mutation_begin_fn)(void *context, const char *project);
typedef void (*cbm_http_project_mutation_end_fn)(void *context, const char *project);
/* Create an HTTP server on the given port.
* Creates its own cbm_mcp_server_t with a separate read-only SQLite connection.
* Returns NULL on failure (e.g. port in use). */
cbm_http_server_t *cbm_http_server_new(int port);
/* Free a quiescent HTTP server. Returns false without freeing if the run loop
* or an index callback can still access it; callers must fail-stop rather than
* continue cleanup in that state. */
bool cbm_http_server_free(cbm_http_server_t *srv);
/* Signal the HTTP server to stop (safe to call from any thread). */
void cbm_http_server_stop(cbm_http_server_t *srv);
/* Claim a future run before handing the server pointer to a new thread. This
* closes the create-to-child-start lifetime gap. If thread creation fails,
* cancel the still-scheduled run before freeing. */
bool cbm_http_server_schedule_run(cbm_http_server_t *srv);
bool cbm_http_server_cancel_scheduled_run(cbm_http_server_t *srv);
/* Run the scheduled HTTP server event loop from the background thread.
* Blocks until cbm_http_server_stop() is called. */
void cbm_http_server_run(cbm_http_server_t *srv);
/* Observation-only phase seam for deterministic concurrency tests. */
cbm_httpd_activity_t cbm_http_server_activity_for_test(cbm_http_server_t *srv);
/* Check if the server started successfully (listener bound). */
bool cbm_http_server_is_running(const cbm_http_server_t *srv);
/* The actually-bound port (useful when constructed with port 0 in tests). */
int cbm_http_server_port(const cbm_http_server_t *srv);
/* Override the per-connection receive deadline (tests use short values). */
void cbm_http_server_set_recv_deadline_ms(cbm_http_server_t *srv, int ms);
/* Set external watcher reference for UI project lifecycle actions. Not owned. */
void cbm_http_server_set_watcher(cbm_http_server_t *srv, struct cbm_watcher *watcher);
/* Route UI indexing through the daemon's shared operation registry. */
void cbm_http_server_set_index_executor(cbm_http_server_t *srv, cbm_http_index_executor_fn executor,
void *context);
/* Route direct UI mutations and /rpc mutation tools through the daemon's
* per-project coordination gate. Direct UI calls are non-blocking: begin=false
* is returned to the browser as a retryable locked response. */
void cbm_http_server_set_project_mutation_guard(cbm_http_server_t *srv,
cbm_http_project_mutation_begin_fn begin,
cbm_http_project_mutation_end_fn end,
void *context);
/* Copy the daemon generation's private readiness secret into the server
* before its run loop starts. GET /__cbm/ui-readiness returns only a
* challenge-bound HMAC proof; the secret itself is never served. */
void cbm_http_server_set_readiness_secret(cbm_http_server_t *srv,
const uint8_t secret[CBM_SHA256_DIGEST_LEN]);
/* Initialize the log ring buffer mutex. Must be called once before any threads. */
void cbm_ui_log_init(void);
/* Append a log line to the UI ring buffer (called from log hook). */
void cbm_ui_log_append(const char *line);
/* Set the binary path for subprocess spawning (call from main). */
void cbm_http_server_set_binary_path(const char *path);
/* Resolve argv[0] into an executable path suitable for subprocess spawning. */
bool cbm_http_server_resolve_binary_path(const char *argv0, char *out, size_t outsz);
/* Pure git-remote URL helpers used by GET /api/repo-info. Exposed for tests. */
/* Normalize a git remote (scp-style / ssh:// / https://) to a canonical
* "https://host/org/repo" web base, with trailing ".git" and any embedded
* credentials removed. malloc'd or NULL. Caller frees. */
char *cbm_ui_git_web_base(const char *url);
/* Copy `url` with any "user[:password]@" userinfo stripped from a
* scheme://authority URL (scp-style is left unchanged). malloc'd, or NULL when
* `url` is NULL. Caller frees. */
char *cbm_ui_git_strip_credentials(const char *url);
#endif /* CBM_UI_HTTP_SERVER_H */