feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include "ai/ai_client.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
|
||||
#include "ai/redactor.hpp"
|
||||
#include "ai/sse_stream_parser.hpp"
|
||||
#include "cli/json_writer.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
AiClient::AiClient(AiClientOptions opts, IAiTransport& transport)
|
||||
: opts_(std::move(opts)), transport_(transport) {}
|
||||
|
||||
std::string AiClient::request_json(const std::string& model,
|
||||
const std::vector<ChatMessage>& messages, bool stream) {
|
||||
std::vector<std::string> items;
|
||||
for (const auto& msg : messages) {
|
||||
std::map<std::string, std::string> fields;
|
||||
fields["role"] = molo::cli::JsonWriter::quote(msg.role);
|
||||
fields["content"] = molo::cli::JsonWriter::quote(msg.content);
|
||||
items.push_back(molo::cli::JsonWriter::object(fields));
|
||||
}
|
||||
std::map<std::string, std::string> body;
|
||||
body["model"] = molo::cli::JsonWriter::quote(model);
|
||||
body["messages"] = molo::cli::JsonWriter::array(items);
|
||||
body["stream"] = stream ? "true" : "false";
|
||||
return molo::cli::JsonWriter::object(body);
|
||||
}
|
||||
|
||||
std::string AiClient::extract_delta(const std::string& data_json) {
|
||||
// Minimal pull of choices[0].delta.content without a JSON parser.
|
||||
auto pos = data_json.find("\"content\"");
|
||||
if (pos == std::string::npos) return "";
|
||||
pos = data_json.find(':', pos);
|
||||
if (pos == std::string::npos) return "";
|
||||
pos = data_json.find('"', pos);
|
||||
if (pos == std::string::npos) return "";
|
||||
std::string out;
|
||||
for (std::size_t i = pos + 1; i < data_json.size(); ++i) {
|
||||
char c = data_json[i];
|
||||
if (c == '\\' && i + 1 < data_json.size()) {
|
||||
char n = data_json[++i];
|
||||
if (n == 'n') {
|
||||
out += '\n';
|
||||
} else if (n == 't') {
|
||||
out += '\t';
|
||||
} else {
|
||||
out += n;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == '"') break;
|
||||
out += c;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string AiClient::chat_stream(const std::vector<ChatMessage>& messages,
|
||||
const std::function<void(const std::string&)>& on_delta,
|
||||
std::string& error) const {
|
||||
const char* key_raw = std::getenv("DEVPLACE_API_KEY");
|
||||
if (key_raw == nullptr || *key_raw == '\0') {
|
||||
error = "DEVPLACE_API_KEY is not set";
|
||||
return "";
|
||||
}
|
||||
if (opts_.base_url.rfind("https://", 0) == 0 && !TlsStub::https_supported()) {
|
||||
error = TlsStub::unavailable_reason();
|
||||
return "";
|
||||
}
|
||||
std::string body = request_json(opts_.model, messages, true);
|
||||
Redactor::Result redacted = Redactor::redact(body);
|
||||
if (!redacted.ok) {
|
||||
error = "redaction failed, request aborted";
|
||||
return "";
|
||||
}
|
||||
AiHttpRequest req;
|
||||
req.url = opts_.base_url + "/chat/completions";
|
||||
req.bearer_key = key_raw;
|
||||
req.body_json = redacted.text;
|
||||
req.connect_timeout_sec = opts_.connect_timeout_sec;
|
||||
AiHttpResponse resp = transport_.post_stream(req);
|
||||
if (resp.status != 200) {
|
||||
error = "AI endpoint status " + std::to_string(resp.status) + ": " + resp.error;
|
||||
return "";
|
||||
}
|
||||
SseStreamParser parser;
|
||||
parser.feed(resp.sse_body);
|
||||
std::string full;
|
||||
for (const auto& event : parser.take_events()) {
|
||||
std::string delta = extract_delta(event);
|
||||
full += delta;
|
||||
if (on_delta) on_delta(delta);
|
||||
}
|
||||
if (!parser.done()) {
|
||||
error = "stream ended without [DONE]";
|
||||
return "";
|
||||
}
|
||||
return full;
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "ai/ai_transport.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
struct ChatMessage {
|
||||
std::string role;
|
||||
std::string content;
|
||||
};
|
||||
|
||||
struct AiClientOptions {
|
||||
std::string base_url = "https://devplace.net/openai/v1";
|
||||
std::string model = "molodetz";
|
||||
int connect_timeout_sec = 30;
|
||||
int session_cap_sec = 300;
|
||||
};
|
||||
|
||||
// OpenAI-compatible chat client. Key comes from DEVPLACE_API_KEY env only,
|
||||
// never a flag, never stored. Streaming responses parse as SSE.
|
||||
class AiClient {
|
||||
public:
|
||||
AiClient(AiClientOptions opts, IAiTransport& transport);
|
||||
|
||||
// Calls on_delta per content delta; returns full assembled text.
|
||||
// Returns empty and sets error on failure.
|
||||
std::string chat_stream(const std::vector<ChatMessage>& messages,
|
||||
const std::function<void(const std::string&)>& on_delta,
|
||||
std::string& error) const;
|
||||
|
||||
const AiClientOptions& options() const { return opts_; }
|
||||
static std::string request_json(const std::string& model,
|
||||
const std::vector<ChatMessage>& messages, bool stream);
|
||||
static std::string extract_delta(const std::string& data_json);
|
||||
|
||||
private:
|
||||
AiClientOptions opts_;
|
||||
IAiTransport& transport_;
|
||||
};
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "ai/ai_session.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
AiSession::AiSession(std::string name)
|
||||
: name_(std::move(name)), started_(std::chrono::steady_clock::now()) {}
|
||||
|
||||
bool AiSession::append(const std::string& role, const std::string& content, std::string& error) {
|
||||
if (expired()) {
|
||||
error = "session exceeded 300s cap";
|
||||
return false;
|
||||
}
|
||||
if (transcript_bytes_ + role.size() + content.size() > caps::kAiTranscriptMaxBytes) {
|
||||
error = "transcript exceeded 8 MB cap";
|
||||
return false;
|
||||
}
|
||||
messages_.push_back(ChatMessage{role, content});
|
||||
transcript_bytes_ += role.size() + content.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AiSession::expired() const {
|
||||
auto elapsed = std::chrono::steady_clock::now() - started_;
|
||||
return elapsed > std::chrono::seconds(caps::kAiSessionCapSec);
|
||||
}
|
||||
|
||||
std::size_t AiSession::request_write(const std::string& path, const std::string& content) {
|
||||
if (actions_ >= caps::kAiMaxActions) return static_cast<std::size_t>(-1);
|
||||
++actions_;
|
||||
pending_.push_back(PendingWrite{path, content});
|
||||
return pending_.size() - 1;
|
||||
}
|
||||
|
||||
bool AiSession::confirm_write(std::size_t index, std::string& error) {
|
||||
if (index >= pending_.size()) {
|
||||
error = "no such pending write";
|
||||
return false;
|
||||
}
|
||||
const PendingWrite& write = pending_[index];
|
||||
std::ofstream out(write.path, std::ios::binary | std::ios::trunc);
|
||||
if (!out.good()) {
|
||||
error = "cannot write " + write.path;
|
||||
return false;
|
||||
}
|
||||
out << write.content;
|
||||
pending_.erase(pending_.begin() + static_cast<long>(index));
|
||||
return true;
|
||||
}
|
||||
|
||||
void AiSession::reject_write(std::size_t index) {
|
||||
if (index < pending_.size()) pending_.erase(pending_.begin() + static_cast<long>(index));
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "ai/ai_client.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
struct PendingWrite {
|
||||
std::string path;
|
||||
std::string content;
|
||||
};
|
||||
|
||||
// Plan-then-apply session: the model proposes file writes, a human confirms
|
||||
// each one, and only confirmation touches the filesystem. Caps: 8 MB
|
||||
// transcript, 50 actions, 300 s session.
|
||||
class AiSession {
|
||||
public:
|
||||
explicit AiSession(std::string name);
|
||||
|
||||
bool append(const std::string& role, const std::string& content, std::string& error);
|
||||
std::size_t transcript_bytes() const { return transcript_bytes_; }
|
||||
std::size_t action_count() const { return actions_; }
|
||||
bool expired() const;
|
||||
|
||||
std::size_t request_write(const std::string& path, const std::string& content);
|
||||
const std::vector<PendingWrite>& pending() const { return pending_; }
|
||||
bool confirm_write(std::size_t index, std::string& error);
|
||||
void reject_write(std::size_t index);
|
||||
|
||||
const std::string& name() const { return name_; }
|
||||
const std::vector<ChatMessage>& messages() const { return messages_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::vector<ChatMessage> messages_;
|
||||
std::size_t transcript_bytes_ = 0;
|
||||
std::size_t actions_ = 0;
|
||||
std::vector<PendingWrite> pending_;
|
||||
std::chrono::steady_clock::time_point started_;
|
||||
};
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "ai/ai_transport.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
StubAiTransport::StubAiTransport(std::vector<std::string> sse_bodies, int status)
|
||||
: bodies_(std::move(sse_bodies)), status_(status) {}
|
||||
|
||||
AiHttpResponse StubAiTransport::post_stream(const AiHttpRequest& request) {
|
||||
seen_.push_back(request);
|
||||
AiHttpResponse out;
|
||||
out.status = status_;
|
||||
if (!bodies_.empty()) {
|
||||
out.sse_body = bodies_.front();
|
||||
bodies_.erase(bodies_.begin());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string TlsStub::unavailable_reason() {
|
||||
return "https transport needs wolfSSL (not installed); plain http only in this build";
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
struct AiHttpRequest {
|
||||
std::string url;
|
||||
std::string bearer_key;
|
||||
std::string body_json;
|
||||
int connect_timeout_sec = 30;
|
||||
};
|
||||
|
||||
struct AiHttpResponse {
|
||||
int status = 0;
|
||||
std::string sse_body;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
// Transport interface. In the full tree this is the 08-clients-openapi.md
|
||||
// HTTP client; here a minimal socket transport plus a scripted stub.
|
||||
class IAiTransport {
|
||||
public:
|
||||
virtual ~IAiTransport() = default;
|
||||
virtual AiHttpResponse post_stream(const AiHttpRequest& request) = 0;
|
||||
};
|
||||
|
||||
// Scripted transport for tests: returns canned SSE bodies.
|
||||
class StubAiTransport : public IAiTransport {
|
||||
public:
|
||||
explicit StubAiTransport(std::vector<std::string> sse_bodies, int status = 200);
|
||||
AiHttpResponse post_stream(const AiHttpRequest& request) override;
|
||||
const std::vector<AiHttpRequest>& seen() const { return seen_; }
|
||||
|
||||
private:
|
||||
std::vector<std::string> bodies_;
|
||||
int status_;
|
||||
std::vector<AiHttpRequest> seen_;
|
||||
};
|
||||
|
||||
// MARKED STUB: HTTPS needs wolfSSL (sole dependency), which is not installed
|
||||
// in this environment. Plain http:// works; https:// reports unavailable.
|
||||
class TlsStub {
|
||||
public:
|
||||
static bool https_supported() { return false; }
|
||||
static std::string unavailable_reason();
|
||||
};
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "ai/prompt_pack.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
std::string PromptPack::system_prompt() {
|
||||
return "You are Molo, a C++23 web framework assistant. "
|
||||
"Generate complete, compiling MoloApp code: routers, class-based views, "
|
||||
"validation models, Jinja-like templates (extends, blocks, includes, if, for, "
|
||||
"filters, auto-escaping), ORM models with Postgres/SQLite backends, auth, "
|
||||
"sessions, forms, WebSockets, Socket.IO rooms, SSE streams, WebDAV handlers, "
|
||||
"supervisor tasks, and integration tests mirroring URL paths. "
|
||||
"Prefer small files, one class per file, namespaces mirroring directories. "
|
||||
"Every knob gets a static cap. Never emit secrets; placeholders only. "
|
||||
"Plan file writes first, then apply only after human confirmation.";
|
||||
}
|
||||
|
||||
std::string PromptPack::feature_prompt(const std::string& topic) {
|
||||
if (topic == "routing") {
|
||||
return "Routing: FastAPI-style routers, path/query/body binding through the "
|
||||
"validation framework, application chaining with automatic path merging.";
|
||||
}
|
||||
if (topic == "templates") {
|
||||
return "Templates: inheritance with extends and blocks, includes, if/for, filters, "
|
||||
"auto-escaping with explicit safe opt-out, i18n t() hooks, CSP nonces, "
|
||||
"responsive mobile-first output.";
|
||||
}
|
||||
if (topic == "orm") {
|
||||
return "ORM: dual Postgres/SQLite backends, auto-sync tables in dev and prod, "
|
||||
"self-healing schema mismatches, parameterized queries only, pooling, "
|
||||
"transactions, cache invalidation hooks.";
|
||||
}
|
||||
if (topic == "realtime") {
|
||||
return "Realtime: WebSocket class views on routers, SSE streams, Socket.IO "
|
||||
"namespaces/rooms/events with long-polling fallback, shared auth/session/"
|
||||
"middleware/logging, client parity for every protocol.";
|
||||
}
|
||||
if (topic == "ops") {
|
||||
return "Ops: /healthz /readyz /startupz probes, Prometheus 0.0.4 metrics with "
|
||||
"series caps, OTLP bridge, CSP report endpoint, PO-based i18n.";
|
||||
}
|
||||
return "General MoloApp feature guidance: " + topic;
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
// Framework feature prompts for vibe-coding sessions.
|
||||
class PromptPack {
|
||||
public:
|
||||
static std::string system_prompt();
|
||||
static std::string feature_prompt(const std::string& topic);
|
||||
};
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "ai/redactor.hpp"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
bool Redactor::key_is_secret(const std::string& key) {
|
||||
std::string upper;
|
||||
for (char c : key) upper += static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
||||
auto ends_with = [&](const std::string& suffix) {
|
||||
return upper.size() >= suffix.size() &&
|
||||
upper.compare(upper.size() - suffix.size(), suffix.size(), suffix) == 0;
|
||||
};
|
||||
return ends_with("_KEY") || ends_with("_SECRET") || ends_with("_TOKEN") ||
|
||||
ends_with("APIKEY") || ends_with("API_KEY") || upper == "KEY" || upper == "SECRET" ||
|
||||
upper == "TOKEN" || upper == "PASSWORD";
|
||||
}
|
||||
|
||||
Redactor::Result Redactor::redact(const std::string& text) {
|
||||
Result out;
|
||||
out.text.reserve(text.size());
|
||||
std::size_t i = 0;
|
||||
while (i < text.size()) {
|
||||
// Match a quoted or bare key followed by ':' or '='.
|
||||
std::size_t key_start = i;
|
||||
bool quoted = i < text.size() && text[i] == '"';
|
||||
std::string key;
|
||||
std::size_t j = i;
|
||||
if (quoted) {
|
||||
++j;
|
||||
while (j < text.size() && text[j] != '"') {
|
||||
key += text[j];
|
||||
++j;
|
||||
}
|
||||
if (j < text.size()) ++j; // closing quote
|
||||
} else {
|
||||
while (j < text.size() &&
|
||||
(std::isalnum(static_cast<unsigned char>(text[j])) || text[j] == '_')) {
|
||||
key += text[j];
|
||||
++j;
|
||||
}
|
||||
}
|
||||
std::size_t k = j;
|
||||
while (k < text.size() && (text[k] == ' ' || text[k] == '\t')) ++k;
|
||||
if (!key.empty() && k < text.size() && (text[k] == ':' || text[k] == '=')) {
|
||||
char sep = text[k];
|
||||
std::size_t v = k + 1;
|
||||
while (v < text.size() && (text[v] == ' ' || text[v] == '\t')) ++v;
|
||||
if (key_is_secret(key)) {
|
||||
out.text += text.substr(key_start, v - key_start);
|
||||
if (v < text.size() && text[v] == '"') {
|
||||
out.text += "\"***\"";
|
||||
++v;
|
||||
while (v < text.size() && text[v] != '"') {
|
||||
if (text[v] == '\\') ++v;
|
||||
++v;
|
||||
}
|
||||
if (v < text.size()) ++v;
|
||||
} else {
|
||||
out.text += "***";
|
||||
while (v < text.size() && text[v] != '\n' && text[v] != ',' && text[v] != '}') ++v;
|
||||
}
|
||||
++out.redacted;
|
||||
i = v;
|
||||
if (i < text.size() && (text[i] == ',' || text[i] == '}')) {
|
||||
out.text += text[i];
|
||||
++i;
|
||||
}
|
||||
(void)sep;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out.text += text[i];
|
||||
++i;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
// Redacts secrets before prompt construction. Keys matching *_KEY,
|
||||
// *_SECRET, *_TOKEN (case-insensitive) and .env.json-style assignments
|
||||
// are masked. Redaction failures abort the request (caller checks ok()).
|
||||
class Redactor {
|
||||
public:
|
||||
struct Result {
|
||||
std::string text;
|
||||
unsigned long redacted = 0;
|
||||
bool ok = true;
|
||||
};
|
||||
|
||||
static Result redact(const std::string& text);
|
||||
static bool key_is_secret(const std::string& key);
|
||||
};
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,127 @@
|
||||
#include "ai/socket_transport.hpp"
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <cstring>
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
SocketAiTransport::ParsedUrl SocketAiTransport::parse(const std::string& url) {
|
||||
ParsedUrl out;
|
||||
std::string rest = url;
|
||||
if (rest.rfind("http://", 0) == 0) {
|
||||
rest = rest.substr(7);
|
||||
} else if (rest.rfind("https://", 0) == 0) {
|
||||
out.https = true;
|
||||
rest = rest.substr(8);
|
||||
} else {
|
||||
return out;
|
||||
}
|
||||
auto slash = rest.find('/');
|
||||
std::string hostport = slash == std::string::npos ? rest : rest.substr(0, slash);
|
||||
out.path = slash == std::string::npos ? "/" : rest.substr(slash);
|
||||
auto colon = hostport.find(':');
|
||||
if (colon != std::string::npos) {
|
||||
out.host = hostport.substr(0, colon);
|
||||
try {
|
||||
out.port = std::stoi(hostport.substr(colon + 1));
|
||||
} catch (...) {
|
||||
return out;
|
||||
}
|
||||
} else {
|
||||
out.host = hostport;
|
||||
out.port = out.https ? 443 : 80;
|
||||
}
|
||||
if (!out.host.empty()) out.ok = true;
|
||||
return out;
|
||||
}
|
||||
|
||||
AiHttpResponse SocketAiTransport::post_stream(const AiHttpRequest& request) {
|
||||
AiHttpResponse out;
|
||||
ParsedUrl url = parse(request.url);
|
||||
if (!url.ok) {
|
||||
out.error = "bad URL: " + request.url;
|
||||
return out;
|
||||
}
|
||||
if (url.https) {
|
||||
out.error = TlsStub::unavailable_reason();
|
||||
return out;
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
out.error = "socket transport is POSIX only";
|
||||
return out;
|
||||
#else
|
||||
addrinfo hints{};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
addrinfo* list = nullptr;
|
||||
if (getaddrinfo(url.host.c_str(), std::to_string(url.port).c_str(), &hints, &list) != 0) {
|
||||
out.error = "dns failed for " + url.host;
|
||||
return out;
|
||||
}
|
||||
int fd = -1;
|
||||
for (addrinfo* ai = list; ai != nullptr; ai = ai->ai_next) {
|
||||
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
||||
if (fd < 0) continue;
|
||||
struct timeval tv;
|
||||
tv.tv_sec = request.connect_timeout_sec;
|
||||
tv.tv_usec = 0;
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) break;
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
freeaddrinfo(list);
|
||||
if (fd < 0) {
|
||||
out.error = "connect failed to " + url.host;
|
||||
return out;
|
||||
}
|
||||
std::string head = "POST " + url.path + " HTTP/1.1\r\nHost: " + url.host +
|
||||
"\r\nContent-Type: application/json\r\nAccept: text/event-stream\r\n" +
|
||||
"Authorization: Bearer " + request.bearer_key + "\r\nContent-Length: " +
|
||||
std::to_string(request.body_json.size()) + "\r\nConnection: close\r\n\r\n";
|
||||
std::string wire = head + request.body_json;
|
||||
std::size_t sent = 0;
|
||||
while (sent < wire.size()) {
|
||||
ssize_t n = send(fd, wire.data() + sent, wire.size() - sent, 0);
|
||||
if (n <= 0) {
|
||||
close(fd);
|
||||
out.error = "send failed";
|
||||
return out;
|
||||
}
|
||||
sent += static_cast<std::size_t>(n);
|
||||
}
|
||||
std::string raw;
|
||||
char buf[8192];
|
||||
while (true) {
|
||||
ssize_t n = recv(fd, buf, sizeof(buf), 0);
|
||||
if (n <= 0) break;
|
||||
raw.append(buf, static_cast<std::size_t>(n));
|
||||
}
|
||||
close(fd);
|
||||
auto eol = raw.find("\r\n");
|
||||
if (eol == std::string::npos) {
|
||||
out.error = "bad response";
|
||||
return out;
|
||||
}
|
||||
std::string status_line = raw.substr(0, eol);
|
||||
auto sp = status_line.find(' ');
|
||||
if (sp != std::string::npos) {
|
||||
try {
|
||||
out.status = std::stoi(status_line.substr(sp + 1));
|
||||
} catch (...) {
|
||||
out.status = 0;
|
||||
}
|
||||
}
|
||||
auto hdr_end = raw.find("\r\n\r\n");
|
||||
out.sse_body = hdr_end == std::string::npos ? "" : raw.substr(hdr_end + 4);
|
||||
return out;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "ai/ai_transport.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
// Minimal blocking HTTP transport over POSIX sockets. Plain http:// only;
|
||||
// https:// URLs fail with the marked TLS stub reason.
|
||||
class SocketAiTransport : public IAiTransport {
|
||||
public:
|
||||
AiHttpResponse post_stream(const AiHttpRequest& request) override;
|
||||
|
||||
private:
|
||||
struct ParsedUrl {
|
||||
std::string host;
|
||||
int port = 80;
|
||||
std::string path;
|
||||
bool https = false;
|
||||
bool ok = false;
|
||||
};
|
||||
static ParsedUrl parse(const std::string& url);
|
||||
};
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,56 @@
|
||||
#include "ai/sse_stream_parser.hpp"
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
void SseStreamParser::feed(const std::string& chunk) {
|
||||
buffer_ += chunk;
|
||||
std::size_t pos = 0;
|
||||
while (true) {
|
||||
std::size_t end = buffer_.find("\n\n", pos);
|
||||
std::size_t sep_len = 2;
|
||||
if (end == std::string::npos) {
|
||||
end = buffer_.find("\r\n\r\n", pos);
|
||||
sep_len = 4;
|
||||
}
|
||||
if (end == std::string::npos) break;
|
||||
std::string block = buffer_.substr(pos, end - pos);
|
||||
pos = end + sep_len;
|
||||
std::string data;
|
||||
std::size_t line_pos = 0;
|
||||
while (line_pos < block.size()) {
|
||||
std::size_t line_end = block.find('\n', line_pos);
|
||||
std::string line = block.substr(line_pos, line_end == std::string::npos
|
||||
? std::string::npos
|
||||
: line_end - line_pos);
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
if (line.rfind("data:", 0) == 0) {
|
||||
std::string payload = line.substr(5);
|
||||
if (!payload.empty() && payload.front() == ' ') payload.erase(0, 1);
|
||||
if (!data.empty()) data += "\n";
|
||||
data += payload;
|
||||
}
|
||||
if (line_end == std::string::npos) break;
|
||||
line_pos = line_end + 1;
|
||||
}
|
||||
if (data == "[DONE]") {
|
||||
done_ = true;
|
||||
} else if (!data.empty()) {
|
||||
events_.push_back(data);
|
||||
}
|
||||
}
|
||||
buffer_.erase(0, pos);
|
||||
}
|
||||
|
||||
std::vector<std::string> SseStreamParser::take_events() {
|
||||
std::vector<std::string> out = std::move(events_);
|
||||
events_.clear();
|
||||
return out;
|
||||
}
|
||||
|
||||
void SseStreamParser::reset() {
|
||||
buffer_.clear();
|
||||
events_.clear();
|
||||
done_ = false;
|
||||
}
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::ai {
|
||||
|
||||
// SSE parser for OpenAI-compatible chat streams. Shared with the 06-realtime
|
||||
// SSE client contract: text/event-stream, data lines, blank-line framing,
|
||||
// end on "data: [DONE]". This file owns the CLI copy; the realtime part
|
||||
// reuses the same framing rules, never a duplicate parser.
|
||||
class SseStreamParser {
|
||||
public:
|
||||
void feed(const std::string& chunk);
|
||||
std::vector<std::string> take_events();
|
||||
bool done() const { return done_; }
|
||||
void reset();
|
||||
|
||||
private:
|
||||
std::string buffer_;
|
||||
std::vector<std::string> events_;
|
||||
bool done_ = false;
|
||||
};
|
||||
|
||||
} // namespace molo::ai
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
// Central static ceilings for the CLI/ops part (09-cli-ops.md).
|
||||
// Every knob below has a hard cap. No autotuning.
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace molo::caps {
|
||||
|
||||
// CLI
|
||||
inline constexpr int kMaxVerbose = 3;
|
||||
inline constexpr std::size_t kMaxCommandDepth = 8;
|
||||
inline constexpr std::size_t kMaxHelpWidth = 100;
|
||||
|
||||
// Dev server
|
||||
inline constexpr std::size_t kMaxWatchFiles = 20000;
|
||||
inline constexpr int kWatchDebounceMs = 150;
|
||||
inline constexpr int kMakeTimeoutSec = 300;
|
||||
inline constexpr int kChildKillGraceSec = 5;
|
||||
inline constexpr int kBackoffBaseSec = 1;
|
||||
inline constexpr int kBackoffMaxSec = 30;
|
||||
|
||||
// Embedder
|
||||
inline constexpr std::size_t kEmbedTotalBytes = 512u * 1024u * 1024u;
|
||||
inline constexpr std::size_t kEmbedMaxFiles = 50000;
|
||||
inline constexpr std::size_t kEmbedMaxFileBytes = 64u * 1024u * 1024u;
|
||||
inline constexpr std::size_t kEmbedChunkBytes = 1024u * 1024u;
|
||||
inline constexpr std::size_t kGzipMinBytes = 1024;
|
||||
|
||||
// AI harness
|
||||
inline constexpr int kAiConnectTimeoutSec = 30;
|
||||
inline constexpr int kAiSessionCapSec = 300;
|
||||
inline constexpr std::size_t kAiTranscriptMaxBytes = 8u * 1024u * 1024u;
|
||||
inline constexpr std::size_t kAiMaxActions = 50;
|
||||
|
||||
// Health / metrics
|
||||
inline constexpr int kHealthCheckTimeoutMs = 200;
|
||||
inline constexpr int kHealthP99Ms = 50;
|
||||
inline constexpr std::size_t kMetricsMaxSeries = 10000;
|
||||
inline constexpr std::size_t kMetricsMaxLabels = 64;
|
||||
inline constexpr int kMetricsExportFloorSec = 5;
|
||||
inline constexpr int kMetricsRenderP99Ms = 100;
|
||||
|
||||
// CSP reports
|
||||
inline constexpr std::size_t kCspReportMaxBytes = 64u * 1024u;
|
||||
inline constexpr std::size_t kCspReportsPerMinPerIp = 100;
|
||||
inline constexpr std::size_t kCspReportStoreMax = 1000;
|
||||
inline constexpr std::size_t kNonceBytes = 16;
|
||||
|
||||
// Scaffolding
|
||||
inline constexpr std::size_t kMaxProjectName = 64;
|
||||
|
||||
// General
|
||||
inline constexpr std::size_t kMaxPathBytes = 4096;
|
||||
|
||||
} // namespace molo::caps
|
||||
@@ -0,0 +1,34 @@
|
||||
# Per-part build fragment for 09-cli-ops. Included by the root Makefile.
|
||||
# Usage: include src/cli/cli.mk, then $(MOLO_BIN) and $(CLI_TEST_BINS).
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++23 -Wall -Wextra
|
||||
CLI_INC := -Isrc/cli
|
||||
CLI_SRCS := $(shell find src/cli -name '*.cpp' ! -name 'main.cpp')
|
||||
CLI_MAIN := src/cli/main.cpp
|
||||
CLI_OBJS := $(patsubst %.cpp,build/obj/%.o,$(CLI_SRCS))
|
||||
CLI_MAIN_OBJ := build/obj/src/cli/main.cpp.o
|
||||
MOLO_BIN := build/molo
|
||||
CLI_TEST_SRCS := $(wildcard tests/src/cli/test_*.cpp)
|
||||
CLI_TEST_BINS := $(patsubst tests/src/cli/%.cpp,build/tests/%,$(CLI_TEST_SRCS))
|
||||
|
||||
$(MOLO_BIN): $(CLI_OBJS) $(CLI_MAIN_OBJ)
|
||||
@mkdir -p build
|
||||
$(CXX) $(CXXFLAGS) $(CLI_OBJS) $(CLI_MAIN_OBJ) $(SUPPLY_LIB) $(OPERABILITY_LIB) -o $(MOLO_BIN)
|
||||
|
||||
build/obj/%.o: %.cpp
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CXXFLAGS) $(CLI_INC) -c $< -o $@
|
||||
|
||||
build/tests/test_%: tests/src/cli/test_%.cpp $(CLI_OBJS) $(SUPPLY_LIB) $(OPERABILITY_LIB)
|
||||
@mkdir -p build/tests
|
||||
$(CXX) $(CXXFLAGS) $(CLI_INC) $< $(CLI_OBJS) $(SUPPLY_LIB) $(OPERABILITY_LIB) -o $@
|
||||
|
||||
cli-test: $(MOLO_BIN) $(CLI_TEST_BINS)
|
||||
for t in $(CLI_TEST_BINS); do ./$$t || exit 1; done
|
||||
sh tests/src/cli/test_cli_binary.sh $(MOLO_BIN)
|
||||
|
||||
# Supply-chain wiring (PATCHES 15, section 2, corrected: -Isrc so the
|
||||
# "supplychain/..." include spelling resolves; MOLO_BIN link carries the lib).
|
||||
CLI_INC += -Isrc
|
||||
|
||||
$(MOLO_BIN): $(SUPPLY_LIB) $(OPERABILITY_LIB)
|
||||
@@ -0,0 +1,102 @@
|
||||
#include "cli/arg_parser.hpp"
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
ParsedArgs ArgParser::parse(int argc, const char* const* argv) const {
|
||||
std::vector<std::string> args;
|
||||
for (int i = 0; i < argc; ++i) args.emplace_back(argv[i]);
|
||||
return parse(args);
|
||||
}
|
||||
|
||||
ParsedArgs ArgParser::parse(const std::vector<std::string>& argv) const {
|
||||
// Expand combined short flags: -vv becomes -v -v.
|
||||
std::vector<std::string> flat;
|
||||
flat.reserve(argv.size() * 2);
|
||||
for (const auto& raw : argv) {
|
||||
bool cluster = raw.size() > 2 && raw[0] == '-' && raw[1] != '-' &&
|
||||
raw.find('=') == std::string::npos;
|
||||
for (std::size_t k = 1; cluster && k < raw.size(); ++k) {
|
||||
char c = raw[k];
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) cluster = false;
|
||||
}
|
||||
if (cluster) {
|
||||
for (std::size_t k = 1; k < raw.size(); ++k) flat.push_back(std::string("-") + raw[k]);
|
||||
} else {
|
||||
flat.push_back(raw);
|
||||
}
|
||||
}
|
||||
ParsedArgs out;
|
||||
bool seen_command = false;
|
||||
bool end_of_flags = false;
|
||||
for (std::size_t i = 1; i < flat.size(); ++i) {
|
||||
const std::string& tok = flat[i];
|
||||
if (!end_of_flags && tok == "--") {
|
||||
end_of_flags = true;
|
||||
continue;
|
||||
}
|
||||
if (!end_of_flags && tok.size() > 1 && tok[0] == '-') {
|
||||
std::string name = tok;
|
||||
std::string value;
|
||||
auto eq = tok.find('=');
|
||||
if (eq != std::string::npos) {
|
||||
name = tok.substr(0, eq);
|
||||
value = tok.substr(eq + 1);
|
||||
}
|
||||
if (name == "-h" || name == "--help") {
|
||||
out.global.show_help = true;
|
||||
continue;
|
||||
}
|
||||
if (name == "--version") {
|
||||
out.global.show_version = true;
|
||||
continue;
|
||||
}
|
||||
if (name == "-v" || name == "--verbose") {
|
||||
if (out.global.verbose < caps::kMaxVerbose) ++out.global.verbose;
|
||||
continue;
|
||||
}
|
||||
if (name == "-q" || name == "--quiet") {
|
||||
out.global.quiet = true;
|
||||
continue;
|
||||
}
|
||||
if (name == "--format") {
|
||||
if (value.empty()) {
|
||||
if (i + 1 >= flat.size()) {
|
||||
out.error = "flag --format needs a value (json|text)";
|
||||
return out;
|
||||
}
|
||||
value = flat[++i];
|
||||
}
|
||||
if (value != "json" && value != "text") {
|
||||
out.error = "flag --format must be json or text";
|
||||
return out;
|
||||
}
|
||||
out.global.format = value;
|
||||
continue;
|
||||
}
|
||||
// Command-local flag; value may follow.
|
||||
std::string key = name;
|
||||
while (key.size() > 1 && key[0] == '-') key.erase(0, 1);
|
||||
if (value.empty() && i + 1 < flat.size() && !flat[i + 1].empty() &&
|
||||
flat[i + 1][0] != '-') {
|
||||
value = flat[++i];
|
||||
}
|
||||
if (value.empty()) value = "true";
|
||||
out.flags[key] = value;
|
||||
continue;
|
||||
}
|
||||
if (!seen_command && out.command_path.size() < caps::kMaxCommandDepth) {
|
||||
// Leading non-flag tokens form the command path until a value-like
|
||||
// token appears after at least one path element; handlers decide.
|
||||
out.command_path.push_back(tok);
|
||||
} else {
|
||||
seen_command = true;
|
||||
out.positionals.push_back(tok);
|
||||
}
|
||||
if (out.command_path.size() >= 2) seen_command = true;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
struct GlobalOptions {
|
||||
std::string format = "text";
|
||||
int verbose = 0;
|
||||
bool quiet = false;
|
||||
bool show_help = false;
|
||||
bool show_version = false;
|
||||
};
|
||||
|
||||
struct ParsedArgs {
|
||||
GlobalOptions global;
|
||||
std::vector<std::string> command_path;
|
||||
std::map<std::string, std::string> flags;
|
||||
std::vector<std::string> positionals;
|
||||
std::string error;
|
||||
bool ok() const { return error.empty(); }
|
||||
};
|
||||
|
||||
// Splits argv into global options, command path, flags, and positionals.
|
||||
// Command path is the leading run of non-flag tokens; the tree resolves them.
|
||||
class ArgParser {
|
||||
public:
|
||||
ParsedArgs parse(int argc, const char* const* argv) const;
|
||||
ParsedArgs parse(const std::vector<std::string>& argv) const;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,763 @@
|
||||
#include "cli/cli_app.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
#include "supplychain/cli/SupplyCli.hpp"
|
||||
|
||||
#include "operability/chaos/experiment.h"
|
||||
#include "operability/chaos/hook.h"
|
||||
#include "operability/chaos/injector.h"
|
||||
#include "operability/debug/snapshot.h"
|
||||
#include "operability/deploy/blue_green.h"
|
||||
#include "operability/deploy/rolling.h"
|
||||
#include "operability/deploy/strategy.h"
|
||||
#include "supervisor/common/clock.hpp"
|
||||
#include "supervisor/common/observer.hpp"
|
||||
|
||||
#include "ai/ai_client.hpp"
|
||||
#include "ai/ai_session.hpp"
|
||||
#include "ai/prompt_pack.hpp"
|
||||
#include "ai/socket_transport.hpp"
|
||||
#include "cli/arg_parser.hpp"
|
||||
#include "cli/command.hpp"
|
||||
#include "cli/help_renderer.hpp"
|
||||
#include "cli/json_writer.hpp"
|
||||
#include "cli/output.hpp"
|
||||
#include "cli/scaffolder.hpp"
|
||||
#include "cli/supervisor_cli.hpp"
|
||||
#include "cli/table_writer.hpp"
|
||||
#include "dev/dev_server.hpp"
|
||||
#include "embed/asset_scanner.hpp"
|
||||
#include "embed/embed_emitter.hpp"
|
||||
#include "ops/health_registry.hpp"
|
||||
#include "ops/liveness_check.hpp"
|
||||
#include "ops/ops_routes.hpp"
|
||||
#include "ops/prometheus_exporter.hpp"
|
||||
#include "ops/readiness_check.hpp"
|
||||
#include "ops/startup_gate.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
namespace {
|
||||
std::string flag_value(const ParsedArgs& args, const std::string& key, const std::string& fallback) {
|
||||
auto it = args.flags.find(key);
|
||||
if (it == args.flags.end()) return fallback;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
bool flag_bool(const ParsedArgs& args, const std::string& key) {
|
||||
auto it = args.flags.find(key);
|
||||
if (it == args.flags.end()) return false;
|
||||
return it->second == "true" || it->second == "1" || it->second == "yes";
|
||||
}
|
||||
|
||||
long flag_long(const ParsedArgs& args, const std::string& key, long fallback) {
|
||||
auto it = args.flags.find(key);
|
||||
if (it == args.flags.end()) return fallback;
|
||||
try {
|
||||
return std::stol(it->second);
|
||||
} catch (...) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
double flag_double(const ParsedArgs& args, const std::string& key, double fallback) {
|
||||
auto it = args.flags.find(key);
|
||||
if (it == args.flags.end()) return fallback;
|
||||
try {
|
||||
return std::stod(it->second);
|
||||
} catch (...) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Operability commands (PATCHES 13, section 10) ---
|
||||
|
||||
class OpsClock : public molo::common::Clock {
|
||||
public:
|
||||
std::int64_t wall_ms() const override {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
std::int64_t steady_ms() const override {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
void sleep_ms(long ms) override {
|
||||
if (ms > 0) std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
}
|
||||
};
|
||||
|
||||
class OpsObserver : public molo::common::Observer {
|
||||
public:
|
||||
void event(std::string_view, std::string_view) override {}
|
||||
void counter(std::string_view, long) override {}
|
||||
void gauge(std::string_view, double) override {}
|
||||
};
|
||||
|
||||
molo::common::Clock& ops_clock() {
|
||||
static OpsClock c;
|
||||
return c;
|
||||
}
|
||||
|
||||
molo::common::Observer& ops_observer() {
|
||||
static OpsObserver o;
|
||||
return o;
|
||||
}
|
||||
|
||||
// Local readiness probe for rollout steps: same checks as cmd_health.
|
||||
bool local_ready() {
|
||||
auto live = std::make_shared<molo::ops::LivenessCheck>();
|
||||
molo::ops::HealthRegistry registry;
|
||||
registry.add(live);
|
||||
molo::ops::ReadinessCheck readiness;
|
||||
molo::ops::StartupGate gate;
|
||||
gate.mark_boot_complete();
|
||||
molo::ops::PrometheusExporter exporter;
|
||||
molo::ops::OpsRoutes routes(registry, readiness, gate, exporter);
|
||||
return routes.healthz().status == 200 && routes.readyz().status == 200;
|
||||
}
|
||||
|
||||
ExitCode cmd_rollout_start(const ParsedArgs& args, Output& out) {
|
||||
const std::string strategy = flag_value(args, "strategy", "rolling");
|
||||
const long instances = std::clamp(flag_long(args, "instances", 3), 1L, 1000L);
|
||||
const long batch = std::clamp(flag_long(args, "batch", 1), 1L, 1000L);
|
||||
const long max_un = std::clamp(flag_long(args, "max-unavailable", 0), 0L, 1000L);
|
||||
if (strategy != "rolling" && strategy != "blue-green") {
|
||||
out.error("strategy must be rolling or blue-green\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
auto probe = [](std::size_t) { return local_ready(); };
|
||||
molo::deploy::Strategy::State terminal = molo::deploy::Strategy::State::kFailed;
|
||||
if (strategy == "rolling") {
|
||||
molo::deploy::Rolling::Config cfg;
|
||||
cfg.total_instances = static_cast<std::size_t>(instances);
|
||||
cfg.batch_size = static_cast<std::size_t>(batch);
|
||||
cfg.max_unavailable = static_cast<std::size_t>(max_un);
|
||||
cfg.pause_between_batches_ms = 0;
|
||||
cfg.ready_timeout_ms = 60000;
|
||||
molo::deploy::Rolling r(cfg, ops_clock(), ops_observer(), probe, [&](std::size_t i) {
|
||||
out.print("upgraded instance " + std::to_string(i) + "\n");
|
||||
});
|
||||
if (!r.start()) {
|
||||
out.error("rollout failed to start\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
std::size_t steps = 0;
|
||||
const std::size_t cap = static_cast<std::size_t>(instances) + 10;
|
||||
while (r.state() == molo::deploy::Strategy::State::kRunning) {
|
||||
if (++steps > cap) {
|
||||
r.abort();
|
||||
out.error("rollout step cap exceeded\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
r.step();
|
||||
}
|
||||
terminal = r.state();
|
||||
out.print(std::string("rollout ") + r.state_name() + "\n");
|
||||
} else {
|
||||
molo::deploy::BlueGreen::Config cfg;
|
||||
cfg.green_instances = static_cast<std::size_t>(instances);
|
||||
cfg.ready_timeout_ms = 60000;
|
||||
cfg.rollback_window_ms = 0; // One-shot CLI retires blue immediately.
|
||||
molo::deploy::BlueGreen bg(
|
||||
cfg, ops_clock(), ops_observer(), probe,
|
||||
[&] {
|
||||
out.print("switched traffic to green\n");
|
||||
return true;
|
||||
},
|
||||
[&](bool green) {
|
||||
out.print(std::string("retired ") + (green ? "green" : "blue") + "\n");
|
||||
});
|
||||
if (!bg.start()) {
|
||||
out.error("rollout failed to start\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
std::size_t steps = 0;
|
||||
while (bg.state() == molo::deploy::Strategy::State::kRunning) {
|
||||
if (++steps > 100) {
|
||||
bg.abort();
|
||||
out.error("rollout step cap exceeded\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
bg.step();
|
||||
}
|
||||
terminal = bg.state();
|
||||
out.print(std::string("rollout ") + bg.state_name() + "\n");
|
||||
}
|
||||
return terminal == molo::deploy::Strategy::State::kSucceeded ? ExitCode::Ok
|
||||
: ExitCode::Runtime;
|
||||
}
|
||||
|
||||
ExitCode cmd_rollout_abort(const ParsedArgs&, Output& out) {
|
||||
out.error("no rollout running in this process (one-shot CLI: start runs to completion)\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
|
||||
ExitCode cmd_rollback(const ParsedArgs&, Output& out) {
|
||||
out.error("no blue-green deployment in this process (one-shot CLI has no rollback window)\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
|
||||
ExitCode cmd_experiment_start(const ParsedArgs& args, Output& out) {
|
||||
const long blast = std::clamp(flag_long(args, "blast", 1), 0L, 100L);
|
||||
const long duration = std::clamp(flag_long(args, "duration", 60000), 0L, 3600000L);
|
||||
double max_burn = flag_double(args, "max-burn", 0.05);
|
||||
if (max_burn < 0) max_burn = 0;
|
||||
molo::chaos::Injector injector(ops_clock(), ops_observer());
|
||||
injector.add_hook(molo::chaos::Hook("cli-latency", molo::chaos::Hook::Action::kDelay,
|
||||
static_cast<int>(blast), 50));
|
||||
molo::chaos::Experiment exp(ops_clock(), ops_observer(), "cli", static_cast<int>(blast),
|
||||
duration, max_burn);
|
||||
if (!exp.start()) {
|
||||
out.error("experiment failed to start\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
out.print("experiment running: blast=" + std::to_string(blast) +
|
||||
" hooks=" + std::to_string(injector.size()) + "\n");
|
||||
for (long waited = 0; waited < duration; waited += 100) {
|
||||
ops_clock().sleep_ms(100);
|
||||
// No burn source in a one-shot CLI; the probe reads zero.
|
||||
if (exp.abort_if_breached([] { return 0.0; })) {
|
||||
out.error("experiment aborted: burn ceiling breached\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
}
|
||||
exp.finish();
|
||||
out.print("experiment finished\n");
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode cmd_experiment_abort(const ParsedArgs&, Output& out) {
|
||||
out.error("no experiment running in this process (one-shot CLI: start runs to completion)\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
|
||||
ExitCode cmd_debug_snapshot(const ParsedArgs&, Output& out) {
|
||||
molo::debug::Snapshot snap(ops_clock(), molo::kMoloVersion);
|
||||
snap.add_section("cli", "{\"argv\":\"debug snapshot\"}");
|
||||
snap.add_section("health", local_ready() ? "{\"ok\":true}" : "{\"ok\":false}");
|
||||
out.print(snap.capture() + "\n");
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode cmd_version(const ParsedArgs& args, Output& out) {
|
||||
if (args.global.format == "json") {
|
||||
std::map<std::string, std::string> fields;
|
||||
fields["name"] = JsonWriter::quote(molo::kMoloName);
|
||||
fields["version"] = JsonWriter::quote(molo::kMoloVersion);
|
||||
out.print(JsonWriter::object(fields) + "\n");
|
||||
} else {
|
||||
out.print(molo::version_string() + "\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode cmd_health(const ParsedArgs& args, Output& out) {
|
||||
auto live = std::make_shared<molo::ops::LivenessCheck>();
|
||||
molo::ops::HealthRegistry registry;
|
||||
registry.add(live);
|
||||
molo::ops::ReadinessCheck readiness;
|
||||
molo::ops::StartupGate gate;
|
||||
gate.mark_boot_complete();
|
||||
molo::ops::PrometheusExporter exporter;
|
||||
molo::ops::OpsRoutes routes(registry, readiness, gate, exporter);
|
||||
molo::ops::OpsResponse hz = routes.healthz();
|
||||
molo::ops::OpsResponse rz = routes.readyz();
|
||||
if (args.global.format == "json") {
|
||||
std::map<std::string, std::string> fields;
|
||||
fields["healthz"] = JsonWriter::quote(hz.body);
|
||||
fields["readyz"] = JsonWriter::quote(rz.body);
|
||||
fields["ok"] = (hz.status == 200 && rz.status == 200) ? "true" : "false";
|
||||
out.print(JsonWriter::object(fields) + "\n");
|
||||
} else {
|
||||
out.print("healthz: " + std::to_string(hz.status) + " " + hz.body + "\n");
|
||||
out.print("readyz: " + std::to_string(rz.status) + " " + rz.body + "\n");
|
||||
}
|
||||
return (hz.status == 200 && rz.status == 200) ? ExitCode::Ok : ExitCode::Unavailable;
|
||||
}
|
||||
|
||||
ExitCode cmd_new(const ParsedArgs& args, Output& out) {
|
||||
if (args.positionals.empty()) {
|
||||
out.error("molo new needs a project name\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
ScaffoldOptions opts;
|
||||
opts.name = args.positionals[0];
|
||||
opts.backend = flag_value(args, "backend", "sqlite");
|
||||
opts.force = flag_bool(args, "force");
|
||||
Scaffolder scaffolder;
|
||||
std::string dir = opts.name;
|
||||
ScaffoldResult result = scaffolder.scaffold(dir, opts);
|
||||
if (!result.ok) {
|
||||
out.error(result.error + "\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
if (args.global.format == "json") {
|
||||
std::vector<std::string> items;
|
||||
for (const auto& created : result.created) items.push_back(JsonWriter::quote(created));
|
||||
std::map<std::string, std::string> fields;
|
||||
fields["ok"] = "true";
|
||||
fields["dir"] = JsonWriter::quote(dir);
|
||||
fields["created"] = JsonWriter::array(items);
|
||||
out.print(JsonWriter::object(fields) + "\n");
|
||||
} else {
|
||||
out.print("created " + dir + " (" + std::to_string(result.created.size()) + " entries)\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode cmd_serve(const ParsedArgs& args, Output& out) {
|
||||
molo::dev::ServeOptions opts;
|
||||
try {
|
||||
opts.port = std::stoi(flag_value(args, "port", "8000"));
|
||||
} catch (...) {
|
||||
out.error("--port must be a number\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
opts.reload = flag_bool(args, "reload");
|
||||
opts.config_path = flag_value(args, "config", ".env.json");
|
||||
opts.allow_prod = flag_bool(args, "allow-prod");
|
||||
molo::dev::DevServer server;
|
||||
return server.run(opts, out);
|
||||
}
|
||||
|
||||
ExitCode cmd_embed(const ParsedArgs& args, Output& out) {
|
||||
if (args.positionals.empty()) {
|
||||
out.error("molo embed needs a directory\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
std::string out_cpp = flag_value(args, "out", "");
|
||||
if (out_cpp.empty()) {
|
||||
out.error("molo embed needs --out <file.cpp>\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
std::string out_hpp = flag_value(args, "hpp", "");
|
||||
if (out_hpp.empty()) {
|
||||
auto dot = out_cpp.find_last_of('.');
|
||||
out_hpp = (dot == std::string::npos ? out_cpp : out_cpp.substr(0, dot)) + ".hpp";
|
||||
}
|
||||
molo::embed::AssetScanner scanner;
|
||||
molo::embed::ScanResult scanned = scanner.scan(args.positionals[0]);
|
||||
if (!scanned.ok()) {
|
||||
out.error(scanned.error + "\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
molo::embed::EmitOptions emit_opts;
|
||||
emit_opts.namespc = flag_value(args, "namespace", "molo::assets");
|
||||
emit_opts.gzip = flag_bool(args, "gzip");
|
||||
emit_opts.tool_version = molo::kEmbedToolVersion;
|
||||
molo::embed::EmbedEmitter emitter;
|
||||
std::string error;
|
||||
if (!emitter.emit(scanned.files, out_cpp, out_hpp, emit_opts, error)) {
|
||||
out.error(error + "\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
if (args.global.format == "json") {
|
||||
std::map<std::string, std::string> fields;
|
||||
fields["ok"] = "true";
|
||||
fields["files"] = std::to_string(scanned.files.size());
|
||||
fields["bytes"] = std::to_string(scanned.total_bytes);
|
||||
fields["cpp"] = JsonWriter::quote(out_cpp);
|
||||
fields["hpp"] = JsonWriter::quote(out_hpp);
|
||||
out.print(JsonWriter::object(fields) + "\n");
|
||||
} else {
|
||||
out.print("embedded " + std::to_string(scanned.files.size()) + " files (" +
|
||||
std::to_string(scanned.total_bytes) + " bytes)\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
ExitCode cmd_build(const ParsedArgs& args, Output& out) {
|
||||
std::string embed_dir = flag_value(args, "embed", "");
|
||||
std::string out_dir = flag_value(args, "out", "build");
|
||||
if (embed_dir.empty()) {
|
||||
out.print("build: nothing to embed (pass --embed <dir>)\n");
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
ParsedArgs sub = args;
|
||||
sub.positionals = {embed_dir};
|
||||
sub.flags["out"] = out_dir + "/assets.cpp";
|
||||
return cmd_embed(sub, out);
|
||||
}
|
||||
|
||||
ExitCode cmd_ai(const std::string& action, const ParsedArgs& args, Output& out) {
|
||||
std::string prompt = flag_value(args, "prompt", "");
|
||||
if (prompt.empty() && !args.positionals.empty()) prompt = args.positionals[0];
|
||||
if (prompt.empty()) {
|
||||
// Read piped stdin when no prompt flag is given.
|
||||
if (!Output::stdout_is_tty()) {
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) prompt += line + "\n";
|
||||
}
|
||||
}
|
||||
if (prompt.empty()) {
|
||||
out.error("molo ai " + action + " needs --prompt <text> or a prompt argument\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
molo::ai::AiClientOptions opts;
|
||||
opts.base_url = flag_value(args, "base-url", opts.base_url);
|
||||
opts.model = flag_value(args, "model", opts.model);
|
||||
bool stream = flag_bool(args, "stream");
|
||||
if (!stream && args.flags.find("stream") == args.flags.end()) stream = true;
|
||||
molo::ai::SocketAiTransport transport;
|
||||
molo::ai::AiClient client(opts, transport);
|
||||
molo::ai::AiSession session(action == "continue" && !args.positionals.empty()
|
||||
? args.positionals[0]
|
||||
: "molo-ai");
|
||||
std::string error;
|
||||
if (!session.append("system", molo::ai::PromptPack::system_prompt(), error) ||
|
||||
!session.append("user", prompt, error)) {
|
||||
out.error(error + "\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
out.debug(1, "POST " + opts.base_url + "/chat/completions model=" + opts.model + "\n");
|
||||
std::string full = client.chat_stream(
|
||||
session.messages(),
|
||||
[&](const std::string& delta) {
|
||||
if (stream && args.global.format != "json") out.print(delta);
|
||||
},
|
||||
error);
|
||||
if (!error.empty()) {
|
||||
if (args.global.format == "json") {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) + "\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (!session.append("assistant", full, error)) {
|
||||
out.error(error + "\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
if (args.global.format == "json") {
|
||||
out.print(JsonWriter::object({{"ok", "true"}, {"text", JsonWriter::quote(full)}}) + "\n");
|
||||
} else if (!stream) {
|
||||
out.print(full + "\n");
|
||||
} else {
|
||||
out.print("\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
|
||||
std::shared_ptr<Command> make_leaf(const std::string& name, const std::string& brief) {
|
||||
return std::make_shared<Command>(name, brief);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CliApp::CliApp()
|
||||
: supervisor_(&null_supervisor_), tasks_(&null_tasks_), migrate_(&null_migrate_) {
|
||||
build_tree();
|
||||
}
|
||||
|
||||
CliApp::CliApp(ISupervisorControl& supervisor, ITaskControl& tasks, IMigrateControl& migrate)
|
||||
: supervisor_(&supervisor), tasks_(&tasks), migrate_(&migrate) {
|
||||
build_tree();
|
||||
}
|
||||
|
||||
void CliApp::build_tree() {
|
||||
auto version = make_leaf("version", "show version");
|
||||
version->usage("[--format json|text]").long_help("Prints the molo version.");
|
||||
version->example("version --format json").handler(cmd_version);
|
||||
|
||||
auto health = make_leaf("health", "local health summary");
|
||||
health->usage("[--format json|text]")
|
||||
.long_help("Runs the local liveness and readiness checks and prints a summary.");
|
||||
health->example("health").handler(cmd_health);
|
||||
|
||||
auto molo_new = make_leaf("new", "scaffold a new project");
|
||||
molo_new->usage("<name> [--backend postgres|sqlite] [--force]")
|
||||
.long_help("Scaffolds Makefile, AGENTS.md, layout, .env.json, .gitignore, example "
|
||||
"files, and runs git init. Refuses non-empty dirs without --force.");
|
||||
molo_new->flag(Flag("backend", "", "database backend", true, "sqlite"));
|
||||
molo_new->flag(Flag("force", "", "overwrite non-empty directory", false, ""));
|
||||
molo_new->example("new shop --backend postgres").handler(cmd_new);
|
||||
|
||||
auto serve = make_leaf("serve", "run the dev server");
|
||||
serve->usage("[--port 8000] [--reload] [--config .env.json] [--allow-prod]")
|
||||
.long_help("Runs the server once, or watches and rebuilds with --reload. Refuses "
|
||||
"--reload with production config unless --allow-prod is passed.");
|
||||
serve->flag(Flag("port", "", "listen port", true, "8000"));
|
||||
serve->flag(Flag("reload", "", "watch and rebuild", false, ""));
|
||||
serve->flag(Flag("config", "", "config file", true, ".env.json"));
|
||||
serve->flag(Flag("allow-prod", "", "allow reload against prod", false, ""));
|
||||
serve->example("serve --reload").handler(cmd_serve);
|
||||
|
||||
auto build = make_leaf("build", "build the project");
|
||||
build->usage("[--embed assets/] [--out build/]")
|
||||
.long_help("Builds the project. With --embed, embeds the directory first.");
|
||||
build->flag(Flag("embed", "", "asset dir to embed", true, ""));
|
||||
build->flag(Flag("out", "", "output dir", true, "build"));
|
||||
build->example("build --embed assets/ --out build/").handler(cmd_build);
|
||||
|
||||
auto embed = make_leaf("embed", "embed assets into C++ source");
|
||||
embed->usage("<dir> --out <file.cpp> [--namespace molo::assets] [--gzip]")
|
||||
.long_help("Converts a static files directory into one .cpp plus one .hpp with "
|
||||
"constexpr byte arrays. Byte-identical reproducible output.");
|
||||
embed->flag(Flag("out", "", "output .cpp file", true, ""));
|
||||
embed->flag(Flag("hpp", "", "output .hpp file", true, ""));
|
||||
embed->flag(Flag("namespace", "", "C++ namespace", true, "molo::assets"));
|
||||
embed->flag(Flag("gzip", "", "emit gzip variants for text", false, ""));
|
||||
embed->example("embed assets/ --out build/assets.cpp --gzip").handler(cmd_embed);
|
||||
|
||||
auto migrate = make_leaf("migrate", "run database migrations");
|
||||
migrate->usage("[--check | --apply]")
|
||||
.long_help("Executes 04-data.md migration plans. --check previews, --apply runs.");
|
||||
migrate->flag(Flag("check", "", "preview only", false, ""));
|
||||
migrate->flag(Flag("apply", "", "apply migrations", false, ""));
|
||||
migrate->handler([this](const ParsedArgs& args, Output& out) {
|
||||
bool apply = flag_bool(args, "apply");
|
||||
MigratePlan plan;
|
||||
std::string error;
|
||||
if (!migrate_->check(plan, error)) {
|
||||
if (args.global.format == "json") {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (apply && !migrate_->apply(plan, error)) {
|
||||
out.error(error + "\n");
|
||||
return ExitCode::Runtime;
|
||||
}
|
||||
if (args.global.format == "json") {
|
||||
std::vector<std::string> items;
|
||||
for (const auto& step : plan.steps) items.push_back(JsonWriter::quote(step));
|
||||
out.print(JsonWriter::object({{"ok", "true"},
|
||||
{"applied", apply ? "true" : "false"},
|
||||
{"steps", JsonWriter::array(items)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.print(apply ? "migrations applied\n" : "migrations checked\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
});
|
||||
|
||||
auto supervisor = make_leaf("supervisor", "manage the process supervisor");
|
||||
supervisor->usage("start|stop|restart|status|logs").long_help("Drives the 07 process supervisor.");
|
||||
for (const char* action : {"start", "stop", "restart", "status", "logs"}) {
|
||||
auto sub = make_leaf(action, std::string("supervisor ") + action);
|
||||
sub->handler([this, name = std::string(action)](const ParsedArgs& args, Output& out) {
|
||||
SupervisorCli cli(*supervisor_, *tasks_);
|
||||
return cli.run_supervisor(name, args, out);
|
||||
});
|
||||
supervisor->child(sub);
|
||||
}
|
||||
|
||||
auto task = make_leaf("task", "manage background tasks");
|
||||
task->usage("enqueue|retry|cancel|inspect").long_help("Drives the 07 task queue.");
|
||||
for (const char* action : {"enqueue", "retry", "cancel", "inspect"}) {
|
||||
auto sub = make_leaf(action, std::string("task ") + action);
|
||||
sub->handler([this, name = std::string(action)](const ParsedArgs& args, Output& out) {
|
||||
SupervisorCli cli(*supervisor_, *tasks_);
|
||||
return cli.run_task(name, args, out);
|
||||
});
|
||||
task->child(sub);
|
||||
}
|
||||
|
||||
auto ai = make_leaf("ai", "AI vibe-coding harness");
|
||||
ai->usage("new|continue [--stream] [--base-url URL] [--model NAME]")
|
||||
.long_help("Builds applications with the DevPlace OpenAI-compatible endpoint. Key "
|
||||
"from DEVPLACE_API_KEY env only. Streams by default.");
|
||||
ai->flag(Flag("stream", "", "stream deltas", false, ""));
|
||||
ai->flag(Flag("base-url", "", "completions base URL", true, "https://devplace.net/openai/v1"));
|
||||
ai->flag(Flag("model", "", "model name", true, "molodetz"));
|
||||
ai->flag(Flag("prompt", "", "prompt text", true, ""));
|
||||
for (const char* action : {"new", "continue"}) {
|
||||
auto sub = make_leaf(action, std::string("ai ") + action);
|
||||
sub->handler([name = std::string(action)](const ParsedArgs& args, Output& out) {
|
||||
return cmd_ai(name, args, out);
|
||||
});
|
||||
ai->child(sub);
|
||||
}
|
||||
|
||||
auto help = make_leaf("help", "show help");
|
||||
help->usage("[<command>]").long_help("Prints the full help page for a command.");
|
||||
help->example("help serve");
|
||||
// Handler installed in run_args (needs tree access); placeholder here.
|
||||
help->handler([](const ParsedArgs&, Output&) { return ExitCode::Ok; });
|
||||
|
||||
// Supply-chain leaves (PATCHES 15, section 3).
|
||||
auto verify = make_leaf("verify", "verify release artifacts offline");
|
||||
verify->usage("molo verify [dir]")
|
||||
.long_help(
|
||||
"Recompute sha256 digests, check the signature bundle chain, and match the "
|
||||
"wolfssl pin. Fully offline.")
|
||||
.example("molo verify ./dist")
|
||||
.flag(Flag("manifest", "", "subjects file", true, "subjects.txt"))
|
||||
.flag(Flag("bundle", "", "signature bundle", true, "bundle.json"))
|
||||
.flag(Flag("pin", "", "allow-list with wolfssl pin", true, ""))
|
||||
.handler(&molo::supply::cli::SupplyCli::run_verify);
|
||||
|
||||
auto supply_check = make_leaf("supply-check", "pin plus advisory gate");
|
||||
supply_check->usage("molo supply-check [dir]")
|
||||
.long_help("Check the wolfssl pin and allow-list, then evaluate advisories. Blocked "
|
||||
"exits 1.")
|
||||
.example("molo supply-check ./dist")
|
||||
.flag(Flag("allow", "", "allow-list file", true, "supply.allow.json"))
|
||||
.flag(Flag("advisories", "", "advisories file", true, "advisories.json"))
|
||||
.flag(Flag("exceptions", "", "exceptions file", true, ""))
|
||||
.handler(&molo::supply::cli::SupplyCli::run_check);
|
||||
|
||||
auto release = make_leaf("release", "ordered release gate");
|
||||
release->usage("molo release --commit <sha> --suite-stamp <file>")
|
||||
.long_help("Run suite, sbom, scan, provenance, sign, tag in order. Any failed step "
|
||||
"exits 1.")
|
||||
.example("molo release --commit abc123 --suite-stamp build/suite.stamp")
|
||||
.flag(Flag("commit", "", "release commit", true, ""))
|
||||
.flag(Flag("suite-stamp", "", "file containing PASS", true, ""))
|
||||
.flag(Flag("dir", "", "release directory", true, "."))
|
||||
.handler(&molo::supply::cli::SupplyCli::run_release);
|
||||
|
||||
tree_.add(version);
|
||||
tree_.add(health);
|
||||
tree_.add(molo_new);
|
||||
tree_.add(serve);
|
||||
tree_.add(build);
|
||||
tree_.add(embed);
|
||||
tree_.add(migrate);
|
||||
tree_.add(supervisor);
|
||||
tree_.add(task);
|
||||
tree_.add(ai);
|
||||
tree_.add(help);
|
||||
tree_.add(verify);
|
||||
tree_.add(supply_check);
|
||||
tree_.add(release);
|
||||
|
||||
// Operability commands (PATCHES 13, section 10).
|
||||
auto rollout = make_leaf("rollout", "run a deployment rollout");
|
||||
rollout->usage("start|abort").long_help("Drives Rolling/BlueGreen step loops in-process.");
|
||||
auto rollout_start = make_leaf("start", "start a rollout");
|
||||
rollout_start
|
||||
->usage("[--strategy rolling|blue-green] [--batch N] [--max-unavailable M] "
|
||||
"[--instances K]")
|
||||
.long_help("Runs the rollout loop to completion with local readiness probes.")
|
||||
.flag(Flag("strategy", "", "rolling or blue-green", true, "rolling"))
|
||||
.flag(Flag("batch", "", "rolling batch size", true, "1"))
|
||||
.flag(Flag("max-unavailable", "", "max unavailable instances", true, "0"))
|
||||
.flag(Flag("instances", "", "instance count", true, "3"))
|
||||
.handler(cmd_rollout_start);
|
||||
auto rollout_abort = make_leaf("abort", "abort the running rollout");
|
||||
rollout_abort->handler(cmd_rollout_abort);
|
||||
rollout->child(rollout_start);
|
||||
rollout->child(rollout_abort);
|
||||
|
||||
auto rollback = make_leaf("rollback", "roll back to blue");
|
||||
rollback->usage("").long_help("Rolls back a blue-green deployment to blue.");
|
||||
rollback->handler(cmd_rollback);
|
||||
|
||||
auto experiment = make_leaf("experiment", "run a chaos experiment");
|
||||
experiment->usage("start|abort").long_help("Drives Experiment plus Injector hooks.");
|
||||
auto exp_start = make_leaf("start", "start an experiment");
|
||||
exp_start
|
||||
->usage("[--blast N] [--duration MS] [--max-burn X]")
|
||||
.long_help("Runs the experiment for the duration, checking the burn ceiling.")
|
||||
.flag(Flag("blast", "", "blast radius percent", true, "1"))
|
||||
.flag(Flag("duration", "", "duration in ms", true, "60000"))
|
||||
.flag(Flag("max-burn", "", "burn ceiling", true, "0.05"))
|
||||
.handler(cmd_experiment_start);
|
||||
auto exp_abort = make_leaf("abort", "abort the running experiment");
|
||||
exp_abort->handler(cmd_experiment_abort);
|
||||
experiment->child(exp_start);
|
||||
experiment->child(exp_abort);
|
||||
|
||||
auto debug = make_leaf("debug", "debug helpers");
|
||||
debug->usage("snapshot").long_help("Local debug helpers.");
|
||||
auto dbg_snapshot = make_leaf("snapshot", "print a debug snapshot");
|
||||
dbg_snapshot->handler(cmd_debug_snapshot);
|
||||
debug->child(dbg_snapshot);
|
||||
|
||||
tree_.add(rollout);
|
||||
tree_.add(rollback);
|
||||
tree_.add(experiment);
|
||||
tree_.add(debug);
|
||||
}
|
||||
|
||||
ExitCode CliApp::run(int argc, const char* const* argv) {
|
||||
std::vector<std::string> args;
|
||||
for (int i = 0; i < argc; ++i) args.emplace_back(argv[i]);
|
||||
return run_args(args);
|
||||
}
|
||||
|
||||
ExitCode CliApp::run_args(const std::vector<std::string>& argv) {
|
||||
ArgParser parser;
|
||||
ParsedArgs parsed = parser.parse(argv);
|
||||
std::string prog = argv.empty() ? "molo" : argv[0];
|
||||
auto slash = prog.find_last_of('/');
|
||||
if (slash != std::string::npos) prog = prog.substr(slash + 1);
|
||||
Output out(parsed.global.format == "json", parsed.global.verbose, parsed.global.quiet,
|
||||
Output::stdout_is_tty());
|
||||
if (!parsed.ok()) {
|
||||
out.error(parsed.error + "\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
if (parsed.global.show_version) return cmd_version(parsed, out);
|
||||
HelpRenderer help_renderer;
|
||||
if (parsed.command_path.empty()) {
|
||||
if (parsed.global.show_help) {
|
||||
out.print(help_renderer.render_root(tree_, prog));
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
out.error("no command given\n");
|
||||
out.print(help_renderer.render_root(tree_, prog));
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
// `help <cmd>` prints the full page.
|
||||
if (parsed.command_path[0] == "help") {
|
||||
if (parsed.command_path.size() < 2 && parsed.positionals.empty()) {
|
||||
out.print(help_renderer.render_root(tree_, prog));
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
std::vector<std::string> target(parsed.command_path.begin() + 1, parsed.command_path.end());
|
||||
for (const auto& positional : parsed.positionals) target.push_back(positional);
|
||||
ResolveResult found = tree_.resolve(target);
|
||||
if (found.command == nullptr) {
|
||||
out.error(found.error + "\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
std::string path;
|
||||
for (std::size_t i = 0; i < found.consumed; ++i) {
|
||||
if (i > 0) path += " ";
|
||||
path += target[i];
|
||||
}
|
||||
out.print(help_renderer.render_full(*found.command, prog, path));
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
ResolveResult found = tree_.resolve(parsed.command_path);
|
||||
if (found.command == nullptr) {
|
||||
out.error(found.error + "\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
if (parsed.global.show_help) {
|
||||
std::string path;
|
||||
for (std::size_t i = 0; i < found.consumed; ++i) {
|
||||
if (i > 0) path += " ";
|
||||
path += parsed.command_path[i];
|
||||
}
|
||||
out.print(help_renderer.render_full(*found.command, prog, path));
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
// Unconsumed path tail becomes leading positionals (e.g. `new <name>`).
|
||||
ParsedArgs adjusted = parsed;
|
||||
std::vector<std::string> tail(parsed.command_path.begin() + found.consumed,
|
||||
parsed.command_path.end());
|
||||
tail.insert(tail.end(), adjusted.positionals.begin(), adjusted.positionals.end());
|
||||
adjusted.positionals = std::move(tail);
|
||||
return found.command->invoke(adjusted, out);
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "cli/arg_parser.hpp"
|
||||
#include "cli/command_tree.hpp"
|
||||
#include "cli/exit_code.hpp"
|
||||
#include "cli/migrate_api.hpp"
|
||||
#include "cli/supervisor_api.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// The `molo` application: declares the full command tree (data first),
|
||||
// then dispatches argv to handlers.
|
||||
class CliApp {
|
||||
public:
|
||||
CliApp();
|
||||
CliApp(ISupervisorControl& supervisor, ITaskControl& tasks, IMigrateControl& migrate);
|
||||
|
||||
ExitCode run(int argc, const char* const* argv);
|
||||
ExitCode run_args(const std::vector<std::string>& argv);
|
||||
const CommandTree& tree() const { return tree_; }
|
||||
|
||||
private:
|
||||
void build_tree();
|
||||
|
||||
CommandTree tree_;
|
||||
ISupervisorControl* supervisor_;
|
||||
ITaskControl* tasks_;
|
||||
IMigrateControl* migrate_;
|
||||
NullSupervisorControl null_supervisor_;
|
||||
NullTaskControl null_tasks_;
|
||||
NullMigrateControl null_migrate_;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,57 @@
|
||||
#include "cli/command.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "cli/output.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
Command::Command(std::string name, std::string brief)
|
||||
: name_(std::move(name)), brief_(std::move(brief)) {}
|
||||
|
||||
Command& Command::usage(std::string text) {
|
||||
usage_ = std::move(text);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Command& Command::long_help(std::string text) {
|
||||
long_help_ = std::move(text);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Command& Command::example(std::string text) {
|
||||
examples_.push_back(std::move(text));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Command& Command::flag(Flag f) {
|
||||
flags_.push_back(std::move(f));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Command& Command::child(std::shared_ptr<Command> sub) {
|
||||
children_.push_back(std::move(sub));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Command& Command::handler(Handler fn) {
|
||||
handler_ = std::move(fn);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Command* Command::find_child(const std::string& name) const {
|
||||
for (const auto& kid : children_) {
|
||||
if (kid->name() == name) return kid.get();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ExitCode Command::invoke(const ParsedArgs& args, Output& out) const {
|
||||
if (!handler_) {
|
||||
out.error("command '" + name_ + "' needs a subcommand");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
return handler_(args, out);
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cli/arg_parser.hpp"
|
||||
#include "cli/exit_code.hpp"
|
||||
#include "cli/flag.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
class Output;
|
||||
|
||||
using Handler = std::function<ExitCode(const ParsedArgs&, Output&)>;
|
||||
|
||||
// One node of the command tree. Data first: help generates from these.
|
||||
class Command {
|
||||
public:
|
||||
explicit Command(std::string name, std::string brief = "");
|
||||
|
||||
const std::string& name() const { return name_; }
|
||||
const std::string& brief() const { return brief_; }
|
||||
const std::string& usage() const { return usage_; }
|
||||
const std::string& long_help() const { return long_help_; }
|
||||
const std::vector<std::string>& examples() const { return examples_; }
|
||||
const std::vector<Flag>& flags() const { return flags_; }
|
||||
const std::vector<std::shared_ptr<Command>>& children() const { return children_; }
|
||||
|
||||
Command& usage(std::string text);
|
||||
Command& long_help(std::string text);
|
||||
Command& example(std::string text);
|
||||
Command& flag(Flag flag);
|
||||
Command& child(std::shared_ptr<Command> sub);
|
||||
Command& handler(Handler fn);
|
||||
|
||||
const Command* find_child(const std::string& name) const;
|
||||
bool has_handler() const { return static_cast<bool>(handler_); }
|
||||
ExitCode invoke(const ParsedArgs& args, Output& out) const;
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string brief_;
|
||||
std::string usage_;
|
||||
std::string long_help_;
|
||||
std::vector<std::string> examples_;
|
||||
std::vector<Flag> flags_;
|
||||
std::vector<std::shared_ptr<Command>> children_;
|
||||
Handler handler_;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "cli/command_tree.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
void CommandTree::add(std::shared_ptr<Command> root) { roots_.push_back(std::move(root)); }
|
||||
|
||||
ResolveResult CommandTree::resolve(const std::vector<std::string>& path) const {
|
||||
ResolveResult out;
|
||||
if (path.empty()) {
|
||||
out.error = "no command given";
|
||||
return out;
|
||||
}
|
||||
const Command* current = nullptr;
|
||||
for (const auto& root : roots_) {
|
||||
if (root->name() == path[0]) {
|
||||
current = root.get();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (current == nullptr) {
|
||||
out.error = "unknown command '" + path[0] + "'";
|
||||
return out;
|
||||
}
|
||||
out.command = current;
|
||||
out.consumed = 1;
|
||||
for (std::size_t i = 1; i < path.size(); ++i) {
|
||||
const Command* kid = current->find_child(path[i]);
|
||||
if (kid == nullptr) break;
|
||||
current = kid;
|
||||
out.command = current;
|
||||
out.consumed = i + 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<std::string> CommandTree::all_paths() const {
|
||||
std::vector<std::string> paths;
|
||||
for (const auto& root : roots_) {
|
||||
paths.push_back(root->name());
|
||||
for (const auto& kid : root->children()) paths.push_back(root->name() + " " + kid->name());
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cli/command.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
struct ResolveResult {
|
||||
const Command* command = nullptr;
|
||||
std::size_t consumed = 0;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
// Owns the root commands and resolves argv command paths.
|
||||
class CommandTree {
|
||||
public:
|
||||
void add(std::shared_ptr<Command> root);
|
||||
ResolveResult resolve(const std::vector<std::string>& path) const;
|
||||
const std::vector<std::shared_ptr<Command>>& roots() const { return roots_; }
|
||||
std::vector<std::string> all_paths() const;
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<Command>> roots_;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// Exit codes per 09-cli-ops.md rule 2. Scripts rely on this mapping.
|
||||
enum class ExitCode : int {
|
||||
Ok = 0,
|
||||
Runtime = 1,
|
||||
Usage = 2,
|
||||
Unavailable = 69,
|
||||
};
|
||||
|
||||
inline int to_int(ExitCode code) { return static_cast<int>(code); }
|
||||
|
||||
inline std::string exit_code_name(ExitCode code) {
|
||||
switch (code) {
|
||||
case ExitCode::Ok:
|
||||
return "ok";
|
||||
case ExitCode::Runtime:
|
||||
return "runtime";
|
||||
case ExitCode::Usage:
|
||||
return "usage";
|
||||
case ExitCode::Unavailable:
|
||||
return "unavailable";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "cli/flag.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
Flag::Flag(std::string name, std::string short_name, std::string help, bool takes_value,
|
||||
std::string default_value)
|
||||
: name_(std::move(name)),
|
||||
short_name_(std::move(short_name)),
|
||||
help_(std::move(help)),
|
||||
takes_value_(takes_value),
|
||||
default_value_(std::move(default_value)) {}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// One declared command-line flag.
|
||||
class Flag {
|
||||
public:
|
||||
Flag() = default;
|
||||
Flag(std::string name, std::string short_name, std::string help, bool takes_value,
|
||||
std::string default_value);
|
||||
|
||||
const std::string& name() const { return name_; }
|
||||
const std::string& short_name() const { return short_name_; }
|
||||
const std::string& help() const { return help_; }
|
||||
bool takes_value() const { return takes_value_; }
|
||||
const std::string& default_value() const { return default_value_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string short_name_;
|
||||
std::string help_;
|
||||
bool takes_value_ = false;
|
||||
std::string default_value_;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "cli/flag_set.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
void FlagSet::add(Flag flag) { decls_.push_back(std::move(flag)); }
|
||||
|
||||
bool FlagSet::declares(const std::string& name) const { return find_decl(name) != nullptr; }
|
||||
|
||||
const Flag* FlagSet::find_decl(const std::string& name) const {
|
||||
for (const auto& decl : decls_) {
|
||||
if (decl.name() == name || (!decl.short_name().empty() && decl.short_name() == name)) {
|
||||
return &decl;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void FlagSet::set(const std::string& name, const std::string& value) {
|
||||
for (auto& pair : values_) {
|
||||
if (pair.first == name) {
|
||||
pair.second = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
values_.emplace_back(name, value);
|
||||
}
|
||||
|
||||
bool FlagSet::has(const std::string& name) const {
|
||||
for (const auto& pair : values_) {
|
||||
if (pair.first == name) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string FlagSet::get(const std::string& name, const std::string& fallback) const {
|
||||
for (const auto& pair : values_) {
|
||||
if (pair.first == name) return pair.second;
|
||||
}
|
||||
const Flag* decl = find_decl(name);
|
||||
if (decl != nullptr && !decl->default_value().empty()) return decl->default_value();
|
||||
return fallback;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cli/flag.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// Declared flags plus parsed values for one command invocation.
|
||||
class FlagSet {
|
||||
public:
|
||||
void add(Flag flag);
|
||||
bool declares(const std::string& name) const;
|
||||
const Flag* find_decl(const std::string& name) const;
|
||||
|
||||
void set(const std::string& name, const std::string& value);
|
||||
bool has(const std::string& name) const;
|
||||
std::string get(const std::string& name, const std::string& fallback = "") const;
|
||||
|
||||
const std::vector<Flag>& decls() const { return decls_; }
|
||||
|
||||
private:
|
||||
std::vector<Flag> decls_;
|
||||
std::vector<std::pair<std::string, std::string>> values_;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "cli/help_renderer.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
std::string HelpRenderer::render_concise(const Command& cmd, const std::string& prog) const {
|
||||
std::ostringstream out;
|
||||
out << "usage: " << prog << " " << cmd.name();
|
||||
if (!cmd.usage().empty()) out << " " << cmd.usage();
|
||||
out << "\n " << cmd.brief() << "\n";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string HelpRenderer::render_full(const Command& cmd, const std::string& prog,
|
||||
const std::string& path) const {
|
||||
std::ostringstream out;
|
||||
out << cmd.name() << " - " << cmd.brief() << "\n\n";
|
||||
out << "usage:\n " << prog << " " << path;
|
||||
if (!cmd.usage().empty()) out << " " << cmd.usage();
|
||||
out << "\n";
|
||||
if (!cmd.long_help().empty()) out << "\n" << cmd.long_help() << "\n";
|
||||
if (!cmd.flags().empty()) {
|
||||
out << "\nflags:\n";
|
||||
for (const auto& flag : cmd.flags()) {
|
||||
out << " --" << flag.name();
|
||||
if (!flag.short_name().empty()) out << ", " << flag.short_name();
|
||||
if (flag.takes_value()) out << " <value>";
|
||||
out << "\n " << flag.help() << "\n";
|
||||
}
|
||||
}
|
||||
if (!cmd.children().empty()) {
|
||||
out << "\nsubcommands:\n";
|
||||
for (const auto& kid : cmd.children()) out << " " << kid->name() << " " << kid->brief() << "\n";
|
||||
}
|
||||
if (!cmd.examples().empty()) {
|
||||
out << "\nexamples:\n";
|
||||
for (const auto& ex : cmd.examples()) out << " " << prog << " " << ex << "\n";
|
||||
}
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string HelpRenderer::render_root(const CommandTree& tree, const std::string& prog) const {
|
||||
std::ostringstream out;
|
||||
out << "usage: " << prog << " <command> [flags]\n\ncommands:\n";
|
||||
for (const auto& root : tree.roots()) {
|
||||
out << " " << root->name() << " " << root->brief() << "\n";
|
||||
}
|
||||
out << "\nglobal flags:\n";
|
||||
out << " -h, --help show help\n";
|
||||
out << " --version show version\n";
|
||||
out << " --format <v> json or text (default text)\n";
|
||||
out << " -v, --verbose repeat up to 3 times for more logs\n";
|
||||
out << " -q, --quiet only errors and primary output\n";
|
||||
out << "\nrun '" << prog << " help <command>' for full help.\n";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "cli/command.hpp"
|
||||
#include "cli/command_tree.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// Renders concise (-h) and full (help <cmd>) pages from the tree.
|
||||
class HelpRenderer {
|
||||
public:
|
||||
std::string render_concise(const Command& cmd, const std::string& prog) const;
|
||||
std::string render_full(const Command& cmd, const std::string& prog,
|
||||
const std::string& path) const;
|
||||
std::string render_root(const CommandTree& tree, const std::string& prog) const;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "cli/json_writer.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
std::string JsonWriter::escape(std::string_view text) {
|
||||
std::ostringstream out;
|
||||
for (char c : text) {
|
||||
switch (c) {
|
||||
case '"':
|
||||
out << "\\\"";
|
||||
break;
|
||||
case '\\':
|
||||
out << "\\\\";
|
||||
break;
|
||||
case '\n':
|
||||
out << "\\n";
|
||||
break;
|
||||
case '\r':
|
||||
out << "\\r";
|
||||
break;
|
||||
case '\t':
|
||||
out << "\\t";
|
||||
break;
|
||||
default:
|
||||
if (static_cast<unsigned char>(c) < 0x20) {
|
||||
char buf[8];
|
||||
snprintf(buf, sizeof(buf), "\\u%04x", c);
|
||||
out << buf;
|
||||
} else {
|
||||
out << c;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string JsonWriter::quote(std::string_view text) {
|
||||
return "\"" + escape(text) + "\"";
|
||||
}
|
||||
|
||||
std::string JsonWriter::object(const std::map<std::string, std::string>& raw_fields) {
|
||||
std::string out = "{";
|
||||
bool first = true;
|
||||
for (const auto& [key, raw] : raw_fields) {
|
||||
if (!first) out += ",";
|
||||
first = false;
|
||||
out += quote(key) + ":" + raw;
|
||||
}
|
||||
return out + "}";
|
||||
}
|
||||
|
||||
std::string JsonWriter::array(const std::vector<std::string>& raw_items) {
|
||||
std::string out = "[";
|
||||
bool first = true;
|
||||
for (const auto& item : raw_items) {
|
||||
if (!first) out += ",";
|
||||
first = false;
|
||||
out += item;
|
||||
}
|
||||
return out + "]";
|
||||
}
|
||||
|
||||
std::string JsonWriter::field(std::string_view key, const std::string& raw_value) {
|
||||
return quote(key) + ":" + raw_value;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// Minimal JSON builder for --format json output. Escapes per RFC 8259.
|
||||
class JsonWriter {
|
||||
public:
|
||||
static std::string escape(std::string_view text);
|
||||
static std::string quote(std::string_view text);
|
||||
static std::string str(std::string_view text) { return quote(text); }
|
||||
static std::string boolean(bool value) { return value ? "true" : "false"; }
|
||||
static std::string number(long long value) { return std::to_string(value); }
|
||||
static std::string object(const std::map<std::string, std::string>& raw_fields);
|
||||
static std::string array(const std::vector<std::string>& raw_items);
|
||||
static std::string field(std::string_view key, const std::string& raw_value);
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "cli/migrate_api.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
bool NullMigrateControl::check(MigratePlan& plan, std::string& error) {
|
||||
(void)plan;
|
||||
error = "migration backend unavailable (04-data.md)";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool NullMigrateControl::apply(const MigratePlan& plan, std::string& error) {
|
||||
(void)plan;
|
||||
error = "migration backend unavailable (04-data.md)";
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
struct MigratePlan {
|
||||
std::vector<std::string> steps;
|
||||
};
|
||||
|
||||
// Migration plans are owned by 04-data.md; the CLI executes them.
|
||||
class IMigrateControl {
|
||||
public:
|
||||
virtual ~IMigrateControl() = default;
|
||||
virtual bool check(MigratePlan& plan, std::string& error) = 0;
|
||||
virtual bool apply(const MigratePlan& plan, std::string& error) = 0;
|
||||
};
|
||||
|
||||
class NullMigrateControl : public IMigrateControl {
|
||||
public:
|
||||
bool check(MigratePlan& plan, std::string& error) override;
|
||||
bool apply(const MigratePlan& plan, std::string& error) override;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "cli/output.hpp"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
Output::Output(bool json_mode, int verbose, bool quiet, bool tty)
|
||||
: json_mode_(json_mode), verbose_(verbose), quiet_(quiet), tty_(tty) {}
|
||||
|
||||
bool Output::stdout_is_tty() {
|
||||
#if defined(_WIN32)
|
||||
return _isatty(_fileno(stdout)) != 0;
|
||||
#else
|
||||
return isatty(STDOUT_FILENO) == 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Output::print(const std::string& text) { std::cout << text; }
|
||||
|
||||
void Output::info(const std::string& text) {
|
||||
if (!quiet_) std::cerr << text;
|
||||
}
|
||||
|
||||
void Output::progress(const std::string& text) {
|
||||
if (!quiet_) std::cerr << text;
|
||||
}
|
||||
|
||||
void Output::warn(const std::string& text) { std::cerr << "warning: " << text; }
|
||||
|
||||
void Output::error(const std::string& text) { std::cerr << "error: " << text; }
|
||||
|
||||
void Output::debug(int level, const std::string& text) {
|
||||
if (!quiet_ && verbose_ >= level) std::cerr << "debug: " << text;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// Output splits primary results (stdout) from diagnostics (stderr) per
|
||||
// 09-cli-ops.md rule 3. Color only when stdout is a TTY.
|
||||
class Output {
|
||||
public:
|
||||
Output(bool json_mode, int verbose, bool quiet, bool tty);
|
||||
|
||||
static bool stdout_is_tty();
|
||||
|
||||
bool json_mode() const { return json_mode_; }
|
||||
int verbose() const { return verbose_; }
|
||||
bool quiet() const { return quiet_; }
|
||||
bool tty() const { return tty_; }
|
||||
|
||||
void print(const std::string& text); // primary output to stdout
|
||||
void info(const std::string& text); // stderr, suppressed when quiet
|
||||
void progress(const std::string& text);
|
||||
void warn(const std::string& text);
|
||||
void error(const std::string& text);
|
||||
void debug(int level, const std::string& text);
|
||||
|
||||
private:
|
||||
bool json_mode_ = false;
|
||||
int verbose_ = 0;
|
||||
bool quiet_ = false;
|
||||
bool tty_ = false;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "cli/scaffolder.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
bool write_file(const std::string& path, const std::string& content) {
|
||||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||||
if (!out.good()) return false;
|
||||
out << content;
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ScaffoldResult Scaffolder::scaffold(const std::string& dir, const ScaffoldOptions& opts) const {
|
||||
ScaffoldResult out;
|
||||
if (opts.name.empty() || opts.name.size() > caps::kMaxProjectName) {
|
||||
out.error = "project name must be 1-64 chars";
|
||||
return out;
|
||||
}
|
||||
if (opts.backend != "sqlite" && opts.backend != "postgres") {
|
||||
out.error = "backend must be sqlite or postgres";
|
||||
return out;
|
||||
}
|
||||
std::error_code ec;
|
||||
bool exists = fs::exists(dir, ec);
|
||||
if (exists && !opts.force) {
|
||||
bool empty = fs::is_empty(dir, ec);
|
||||
if (!empty) {
|
||||
out.error = "directory is not empty, use --force to overwrite";
|
||||
return out;
|
||||
}
|
||||
}
|
||||
fs::create_directories(dir, ec);
|
||||
if (ec) {
|
||||
out.error = "cannot create directory " + dir;
|
||||
return out;
|
||||
}
|
||||
auto put = [&](const std::string& rel, const std::string& content) {
|
||||
std::string full = dir + "/" + rel;
|
||||
fs::create_directories(fs::path(full).parent_path(), ec);
|
||||
if (!write_file(full, content)) {
|
||||
out.error = "cannot write " + full;
|
||||
return false;
|
||||
}
|
||||
out.created.push_back(rel);
|
||||
return true;
|
||||
};
|
||||
if (!put("Makefile",
|
||||
"CXX=g++\nCXXFLAGS=-std=c++23 -Wall -Wextra\n\nall:\n\t"
|
||||
"$(CXX) $(CXXFLAGS) -o build/app src/main.cpp\n\ntest:\n\t"
|
||||
"@echo no tests yet\n\nclean:\n\trm -rf build\n")) {
|
||||
return out;
|
||||
}
|
||||
if (!put("AGENTS.md",
|
||||
"# " + opts.name +
|
||||
"\n\nAutonomous workflow: build, test, docs update, version bump, "
|
||||
"commit, tag, deploy, verify.\nRules: exact minimal diffs, one class per "
|
||||
"file, static caps on every knob.\n")) {
|
||||
return out;
|
||||
}
|
||||
if (!put(".env.json",
|
||||
"{\n \"MOLO_ENV\": \"dev\",\n \"PORT\": 8000,\n \"DATABASE_URL\": \"" +
|
||||
opts.backend + ":///db/app.db\"\n}\n")) {
|
||||
return out;
|
||||
}
|
||||
if (!put(".gitignore", "build/\n*.o\n.env.json.local\n")) return out;
|
||||
if (!put("src/main.cpp",
|
||||
"#include <iostream>\n\nint main() {\n std::cout << \"hello from " + opts.name +
|
||||
"\\n\";\n return 0;\n}\n")) {
|
||||
return out;
|
||||
}
|
||||
if (!put("templates/base.html",
|
||||
"<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n"
|
||||
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
|
||||
"<title>{% block title %}" +
|
||||
opts.name +
|
||||
"{% endblock %}</title>\n</head>\n<body>\n{% block body %}{% endblock %}\n"
|
||||
"</body>\n</html>\n")) {
|
||||
return out;
|
||||
}
|
||||
if (!put("assets/app.css", "body{margin:0;font-family:system-ui}\n")) return out;
|
||||
if (!put("locales/en/LC_MESSAGES/app.po",
|
||||
"msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n")) {
|
||||
return out;
|
||||
}
|
||||
std::string cmd = "git -C \"" + dir + "\" init -q 2>/dev/null";
|
||||
if (system(cmd.c_str()) != 0) {
|
||||
out.error = "git init failed in " + dir;
|
||||
return out;
|
||||
}
|
||||
out.created.push_back(".git/");
|
||||
out.ok = true;
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
struct ScaffoldOptions {
|
||||
std::string name;
|
||||
std::string backend = "sqlite"; // postgres|sqlite
|
||||
bool force = false;
|
||||
};
|
||||
|
||||
struct ScaffoldResult {
|
||||
bool ok = false;
|
||||
std::string error;
|
||||
std::vector<std::string> created;
|
||||
};
|
||||
|
||||
// Implements `molo new`: Makefile, AGENTS.md stub, layout, .env.json,
|
||||
// .gitignore, git init, plug-and-play examples. Refuses non-empty dirs
|
||||
// without --force and never touches git history.
|
||||
class Scaffolder {
|
||||
public:
|
||||
ScaffoldResult scaffold(const std::string& dir, const ScaffoldOptions& opts) const;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,57 @@
|
||||
#include "cli/supervisor_api.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
namespace {
|
||||
const char* kUnavailable = "supervisor backend unavailable (07-supervisor-replication.md)";
|
||||
}
|
||||
|
||||
bool NullSupervisorControl::start(std::string& error) {
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
bool NullSupervisorControl::stop(std::string& error) {
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
bool NullSupervisorControl::restart(std::string& error) {
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
bool NullSupervisorControl::status(SupervisorStatus& out, std::string& error) {
|
||||
(void)out;
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
bool NullSupervisorControl::logs(int lines, std::vector<std::string>& out, std::string& error) {
|
||||
(void)lines;
|
||||
(void)out;
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool NullTaskControl::enqueue(const std::string& name, TaskInfo& out, std::string& error) {
|
||||
(void)name;
|
||||
(void)out;
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
bool NullTaskControl::retry(const std::string& id, TaskInfo& out, std::string& error) {
|
||||
(void)id;
|
||||
(void)out;
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
bool NullTaskControl::cancel(const std::string& id, std::string& error) {
|
||||
(void)id;
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
bool NullTaskControl::inspect(const std::string& id, TaskInfo& out, std::string& error) {
|
||||
(void)id;
|
||||
(void)out;
|
||||
error = kUnavailable;
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
struct SupervisorStatus {
|
||||
bool running = false;
|
||||
int workers = 0;
|
||||
std::string state;
|
||||
};
|
||||
|
||||
struct TaskInfo {
|
||||
std::string id;
|
||||
std::string name;
|
||||
std::string state;
|
||||
int retries = 0;
|
||||
};
|
||||
|
||||
// Control API owned by 07-supervisor-replication.md. The CLI drives it;
|
||||
// null implementations report unavailable until wired.
|
||||
class ISupervisorControl {
|
||||
public:
|
||||
virtual ~ISupervisorControl() = default;
|
||||
virtual bool start(std::string& error) = 0;
|
||||
virtual bool stop(std::string& error) = 0;
|
||||
virtual bool restart(std::string& error) = 0;
|
||||
virtual bool status(SupervisorStatus& out, std::string& error) = 0;
|
||||
virtual bool logs(int lines, std::vector<std::string>& out, std::string& error) = 0;
|
||||
};
|
||||
|
||||
class ITaskControl {
|
||||
public:
|
||||
virtual ~ITaskControl() = default;
|
||||
virtual bool enqueue(const std::string& name, TaskInfo& out, std::string& error) = 0;
|
||||
virtual bool retry(const std::string& id, TaskInfo& out, std::string& error) = 0;
|
||||
virtual bool cancel(const std::string& id, std::string& error) = 0;
|
||||
virtual bool inspect(const std::string& id, TaskInfo& out, std::string& error) = 0;
|
||||
};
|
||||
|
||||
class NullSupervisorControl : public ISupervisorControl {
|
||||
public:
|
||||
bool start(std::string& error) override;
|
||||
bool stop(std::string& error) override;
|
||||
bool restart(std::string& error) override;
|
||||
bool status(SupervisorStatus& out, std::string& error) override;
|
||||
bool logs(int lines, std::vector<std::string>& out, std::string& error) override;
|
||||
};
|
||||
|
||||
class NullTaskControl : public ITaskControl {
|
||||
public:
|
||||
bool enqueue(const std::string& name, TaskInfo& out, std::string& error) override;
|
||||
bool retry(const std::string& id, TaskInfo& out, std::string& error) override;
|
||||
bool cancel(const std::string& id, std::string& error) override;
|
||||
bool inspect(const std::string& id, TaskInfo& out, std::string& error) override;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,180 @@
|
||||
#include "cli/supervisor_cli.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "cli/json_writer.hpp"
|
||||
#include "cli/output.hpp"
|
||||
#include "cli/table_writer.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
SupervisorCli::SupervisorCli(ISupervisorControl& supervisor, ITaskControl& tasks)
|
||||
: supervisor_(supervisor), tasks_(tasks) {}
|
||||
|
||||
ExitCode SupervisorCli::run_supervisor(const std::string& action, const ParsedArgs& args,
|
||||
Output& out) {
|
||||
bool json = args.global.format == "json";
|
||||
std::string error;
|
||||
if (action == "start" || action == "stop" || action == "restart") {
|
||||
bool ok = action == "start" ? supervisor_.start(error)
|
||||
: (action == "stop" ? supervisor_.stop(error)
|
||||
: supervisor_.restart(error));
|
||||
if (!ok) {
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "true"}, {"action", JsonWriter::quote(action)}}) + "\n");
|
||||
} else {
|
||||
out.print("supervisor " + action + ": ok\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
if (action == "status") {
|
||||
SupervisorStatus status;
|
||||
if (!supervisor_.status(status, error)) {
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (json) {
|
||||
std::map<std::string, std::string> fields;
|
||||
fields["ok"] = "true";
|
||||
fields["running"] = status.running ? "true" : "false";
|
||||
fields["workers"] = std::to_string(status.workers);
|
||||
fields["state"] = JsonWriter::quote(status.state);
|
||||
out.print(JsonWriter::object(fields) + "\n");
|
||||
} else {
|
||||
TableWriter table;
|
||||
table.header({"running", "workers", "state"});
|
||||
table.row({status.running ? "yes" : "no", std::to_string(status.workers), status.state});
|
||||
out.print(table.render());
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
if (action == "logs") {
|
||||
std::vector<std::string> lines;
|
||||
if (!supervisor_.logs(100, lines, error)) {
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (json) {
|
||||
std::vector<std::string> items;
|
||||
for (const auto& line : lines) items.push_back(JsonWriter::quote(line));
|
||||
out.print(JsonWriter::object({{"ok", "true"}, {"lines", JsonWriter::array(items)}}) + "\n");
|
||||
} else {
|
||||
for (const auto& line : lines) out.print(line + "\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
out.error("unknown supervisor action '" + action + "'\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
|
||||
ExitCode SupervisorCli::run_task(const std::string& action, const ParsedArgs& args, Output& out) {
|
||||
bool json = args.global.format == "json";
|
||||
auto flag = [&](const std::string& key) {
|
||||
auto it = args.flags.find(key);
|
||||
return it == args.flags.end() ? "" : it->second;
|
||||
};
|
||||
std::string id = flag("id");
|
||||
if (id.empty() && !args.positionals.empty()) id = args.positionals[0];
|
||||
std::string error;
|
||||
if (action == "enqueue") {
|
||||
std::string name = flag("name");
|
||||
if (name.empty() && !args.positionals.empty()) name = args.positionals[0];
|
||||
if (name.empty()) {
|
||||
out.error("task enqueue needs --name <task>\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
TaskInfo info;
|
||||
if (!tasks_.enqueue(name, info, error)) {
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "true"},
|
||||
{"id", JsonWriter::quote(info.id)},
|
||||
{"state", JsonWriter::quote(info.state)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.print("enqueued " + info.id + " (" + info.state + ")\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
if (action == "retry" || action == "inspect") {
|
||||
if (id.empty()) {
|
||||
out.error("task " + action + " needs --id <id>\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
TaskInfo info;
|
||||
bool ok = action == "retry" ? tasks_.retry(id, info, error) : tasks_.inspect(id, info, error);
|
||||
if (!ok) {
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "true"},
|
||||
{"id", JsonWriter::quote(info.id)},
|
||||
{"name", JsonWriter::quote(info.name)},
|
||||
{"state", JsonWriter::quote(info.state)},
|
||||
{"retries", std::to_string(info.retries)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
TableWriter table;
|
||||
table.header({"id", "name", "state", "retries"});
|
||||
table.row({info.id, info.name, info.state, std::to_string(info.retries)});
|
||||
out.print(table.render());
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
if (action == "cancel") {
|
||||
if (id.empty()) {
|
||||
out.error("task cancel needs --id <id>\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
if (!tasks_.cancel(id, error)) {
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "false"}, {"error", JsonWriter::quote(error)}}) +
|
||||
"\n");
|
||||
} else {
|
||||
out.error(error + "\n");
|
||||
}
|
||||
return ExitCode::Unavailable;
|
||||
}
|
||||
if (json) {
|
||||
out.print(JsonWriter::object({{"ok", "true"}, {"id", JsonWriter::quote(id)}}) + "\n");
|
||||
} else {
|
||||
out.print("cancelled " + id + "\n");
|
||||
}
|
||||
return ExitCode::Ok;
|
||||
}
|
||||
out.error("unknown task action '" + action + "'\n");
|
||||
return ExitCode::Usage;
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cli/arg_parser.hpp"
|
||||
#include "cli/exit_code.hpp"
|
||||
#include "cli/supervisor_api.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
class Output;
|
||||
|
||||
// CLI surface for `molo supervisor ...` and `molo task ...` with text and
|
||||
// structured JSON output for scripts.
|
||||
class SupervisorCli {
|
||||
public:
|
||||
SupervisorCli(ISupervisorControl& supervisor, ITaskControl& tasks);
|
||||
|
||||
ExitCode run_supervisor(const std::string& action, const ParsedArgs& args, Output& out);
|
||||
ExitCode run_task(const std::string& action, const ParsedArgs& args, Output& out);
|
||||
|
||||
private:
|
||||
ISupervisorControl& supervisor_;
|
||||
ITaskControl& tasks_;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "cli/table_writer.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
void TableWriter::header(std::vector<std::string> cols) { header_ = std::move(cols); }
|
||||
|
||||
void TableWriter::row(std::vector<std::string> cols) { rows_.push_back(std::move(cols)); }
|
||||
|
||||
std::string TableWriter::render() const {
|
||||
std::size_t cols = header_.size();
|
||||
for (const auto& row : rows_) cols = std::max(cols, row.size());
|
||||
std::vector<std::size_t> width(cols, 0);
|
||||
auto widen = [&](const std::vector<std::string>& r) {
|
||||
for (std::size_t i = 0; i < r.size(); ++i) width[i] = std::max(width[i], r[i].size());
|
||||
};
|
||||
widen(header_);
|
||||
for (const auto& row : rows_) widen(row);
|
||||
std::ostringstream out;
|
||||
auto emit = [&](const std::vector<std::string>& r) {
|
||||
for (std::size_t i = 0; i < cols; ++i) {
|
||||
std::string cell = i < r.size() ? r[i] : "";
|
||||
out << cell;
|
||||
if (i + 1 < cols) out << std::string(width[i] - cell.size() + 2, ' ');
|
||||
}
|
||||
out << "\n";
|
||||
};
|
||||
if (!header_.empty()) emit(header_);
|
||||
for (const auto& row : rows_) emit(row);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::cli {
|
||||
|
||||
// Plain ASCII tables for human-readable command output. No color codes.
|
||||
class TableWriter {
|
||||
public:
|
||||
void header(std::vector<std::string> cols);
|
||||
void row(std::vector<std::string> cols);
|
||||
std::string render() const;
|
||||
|
||||
private:
|
||||
std::vector<std::string> header_;
|
||||
std::vector<std::vector<std::string>> rows_;
|
||||
};
|
||||
|
||||
} // namespace molo::cli
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "csp/csp_builder.hpp"
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
CspBuilder::CspBuilder() {
|
||||
directives_["default-src"] = "'none'";
|
||||
directives_["script-src"] = "'self'";
|
||||
directives_["style-src"] = "'self'";
|
||||
directives_["img-src"] = "'self' data:";
|
||||
directives_["connect-src"] = "'self'";
|
||||
directives_["frame-ancestors"] = "'none'";
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::default_src(const std::string& value) {
|
||||
directives_["default-src"] = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::script_src(const std::string& value) {
|
||||
directives_["script-src"] = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::style_src(const std::string& value) {
|
||||
directives_["style-src"] = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::img_src(const std::string& value) {
|
||||
directives_["img-src"] = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::connect_src(const std::string& value) {
|
||||
directives_["connect-src"] = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::frame_ancestors(const std::string& value) {
|
||||
directives_["frame-ancestors"] = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::report_to(const std::string& value) {
|
||||
directives_["report-uri"] = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::script_nonce(const std::string& nonce_base64) {
|
||||
directives_["script-src"] += " 'nonce-" + nonce_base64 + "'";
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspBuilder& CspBuilder::enforce() {
|
||||
report_only_ = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CspPolicy CspBuilder::build() const { return CspPolicy(directives_, report_only_); }
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "csp/csp_policy.hpp"
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
// Fluent builder. Default is deny-first per 09-cli-ops.md rule 10.
|
||||
class CspBuilder {
|
||||
public:
|
||||
CspBuilder();
|
||||
|
||||
CspBuilder& default_src(const std::string& value);
|
||||
CspBuilder& script_src(const std::string& value);
|
||||
CspBuilder& style_src(const std::string& value);
|
||||
CspBuilder& img_src(const std::string& value);
|
||||
CspBuilder& connect_src(const std::string& value);
|
||||
CspBuilder& frame_ancestors(const std::string& value);
|
||||
CspBuilder& report_to(const std::string& value);
|
||||
CspBuilder& script_nonce(const std::string& nonce_base64);
|
||||
CspBuilder& enforce();
|
||||
|
||||
CspPolicy build() const;
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> directives_;
|
||||
bool report_only_ = true;
|
||||
};
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "csp/csp_policy.hpp"
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
CspPolicy::CspPolicy(std::map<std::string, std::string> directives, bool report_only)
|
||||
: directives_(std::move(directives)), report_only_(report_only) {}
|
||||
|
||||
std::string CspPolicy::header_name() const {
|
||||
return report_only_ ? "Content-Security-Policy-Report-Only" : "Content-Security-Policy";
|
||||
}
|
||||
|
||||
std::string CspPolicy::header_value() const {
|
||||
std::string out;
|
||||
bool first = true;
|
||||
for (const auto& [key, value] : directives_) {
|
||||
if (!first) out += "; ";
|
||||
first = false;
|
||||
out += key;
|
||||
if (!value.empty()) out += " " + value;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
// Immutable policy value. Report-only until the app opts into enforcement.
|
||||
class CspPolicy {
|
||||
public:
|
||||
CspPolicy(std::map<std::string, std::string> directives, bool report_only);
|
||||
|
||||
const std::map<std::string, std::string>& directives() const { return directives_; }
|
||||
bool report_only() const { return report_only_; }
|
||||
std::string header_name() const;
|
||||
std::string header_value() const;
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> directives_;
|
||||
bool report_only_ = true;
|
||||
};
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "csp/csp_report_handler.hpp"
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
CspReportResult CspReportHandler::handle(const std::string& client_ip, const std::string& body,
|
||||
std::int64_t now_sec) {
|
||||
CspReportResult out;
|
||||
if (body.size() > caps::kCspReportMaxBytes) {
|
||||
out.status = 413;
|
||||
out.body = "{\"status\":\"report too large\"}";
|
||||
return out;
|
||||
}
|
||||
auto& window = hits_[client_ip];
|
||||
while (!window.empty() && now_sec - window.front() >= 60) window.pop_front();
|
||||
if (window.size() >= caps::kCspReportsPerMinPerIp) {
|
||||
out.status = 429;
|
||||
out.body = "{\"status\":\"rate limited\"}";
|
||||
return out;
|
||||
}
|
||||
window.push_back(now_sec);
|
||||
if (store_.size() >= caps::kCspReportStoreMax) store_.erase(store_.begin());
|
||||
store_.push_back(body);
|
||||
out.status = 202;
|
||||
out.body = "{\"status\":\"accepted\"}";
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
struct CspReportResult {
|
||||
int status = 202;
|
||||
std::string body;
|
||||
};
|
||||
|
||||
// POST /csp-report handler. Caps: 64 KB per report, 100 reports/min/IP,
|
||||
// bounded in-memory store (1000 reports ring).
|
||||
class CspReportHandler {
|
||||
public:
|
||||
CspReportResult handle(const std::string& client_ip, const std::string& body,
|
||||
std::int64_t now_sec);
|
||||
std::size_t stored() const { return store_.size(); }
|
||||
|
||||
private:
|
||||
std::map<std::string, std::deque<std::int64_t>> hits_;
|
||||
std::vector<std::string> store_;
|
||||
};
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "csp/nonce.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <random>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <sys/random.h>
|
||||
#endif
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
std::string Nonce::base64_of(const unsigned char* data, std::size_t len) {
|
||||
static const char* alphabet =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
std::string out;
|
||||
for (std::size_t i = 0; i < len; i += 3) {
|
||||
unsigned int n = static_cast<unsigned int>(data[i]) << 16;
|
||||
if (i + 1 < len) n |= static_cast<unsigned int>(data[i + 1]) << 8;
|
||||
if (i + 2 < len) n |= data[i + 2];
|
||||
out += alphabet[(n >> 18) & 63];
|
||||
out += alphabet[(n >> 12) & 63];
|
||||
out += (i + 1 < len) ? alphabet[(n >> 6) & 63] : '=';
|
||||
out += (i + 2 < len) ? alphabet[n & 63] : '=';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string Nonce::generate() {
|
||||
std::array<unsigned char, caps::kNonceBytes> buf{};
|
||||
#if defined(__linux__)
|
||||
ssize_t got = getrandom(buf.data(), buf.size(), 0);
|
||||
if (got == static_cast<ssize_t>(buf.size())) return base64_of(buf.data(), buf.size());
|
||||
#endif
|
||||
std::random_device rd;
|
||||
for (auto& byte : buf) byte = static_cast<unsigned char>(rd() & 0xFF);
|
||||
return base64_of(buf.data(), buf.size());
|
||||
}
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::csp {
|
||||
|
||||
// Per-request nonce: 16 random bytes, base64. RNG comes from 03-security.md
|
||||
// in the full tree; here OS entropy (getrandom / random_device fallback).
|
||||
class Nonce {
|
||||
public:
|
||||
static std::string generate();
|
||||
static std::string base64_of(const unsigned char* data, std::size_t len);
|
||||
};
|
||||
|
||||
} // namespace molo::csp
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "dev/dev_config.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
std::string ConfigLoader::read_file(const std::string& path) {
|
||||
std::ifstream in(path);
|
||||
if (!in.good()) return "";
|
||||
std::ostringstream buf;
|
||||
buf << in.rdbuf();
|
||||
return buf.str();
|
||||
}
|
||||
|
||||
std::string ConfigLoader::json_string(const std::string& json, const std::string& key,
|
||||
const std::string& fallback) {
|
||||
std::string needle = "\"" + key + "\"";
|
||||
auto pos = json.find(needle);
|
||||
if (pos == std::string::npos) return fallback;
|
||||
pos = json.find(':', pos + needle.size());
|
||||
if (pos == std::string::npos) return fallback;
|
||||
pos = json.find('"', pos + 1);
|
||||
if (pos == std::string::npos) return fallback;
|
||||
auto end = json.find('"', pos + 1);
|
||||
if (end == std::string::npos) return fallback;
|
||||
return json.substr(pos + 1, end - pos - 1);
|
||||
}
|
||||
|
||||
int ConfigLoader::json_int(const std::string& json, const std::string& key, int fallback) {
|
||||
std::string needle = "\"" + key + "\"";
|
||||
auto pos = json.find(needle);
|
||||
if (pos == std::string::npos) return fallback;
|
||||
pos = json.find(':', pos + needle.size());
|
||||
if (pos == std::string::npos) return fallback;
|
||||
auto start = json.find_first_of("-0123456789", pos + 1);
|
||||
if (start == std::string::npos) return fallback;
|
||||
auto end = json.find_first_not_of("0123456789", start + 1);
|
||||
try {
|
||||
return std::stoi(json.substr(start, end - start));
|
||||
} catch (...) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
LoadedConfig ConfigLoader::load(const std::string& config_path, bool reload_flag,
|
||||
bool allow_prod) const {
|
||||
LoadedConfig out;
|
||||
out.allow_prod = allow_prod;
|
||||
const char* env_raw = std::getenv("MOLO_ENV");
|
||||
std::string env_text = env_raw != nullptr ? env_raw : "";
|
||||
std::string json = read_file(config_path);
|
||||
if (env_text.empty()) env_text = json_string(json, "MOLO_ENV", "dev");
|
||||
out.env = (env_text == "prod" || env_text == "production") ? Env::Prod : Env::Dev;
|
||||
|
||||
int port = json_int(json, "PORT", 8000);
|
||||
if (const char* port_raw = std::getenv("PORT"); port_raw != nullptr && *port_raw != '\0') {
|
||||
try {
|
||||
port = std::stoi(port_raw);
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
std::string db_url = json_string(json, "DATABASE_URL", "");
|
||||
if (const char* db_raw = std::getenv("DATABASE_URL");
|
||||
db_raw != nullptr && *db_raw != '\0') {
|
||||
db_url = db_raw;
|
||||
}
|
||||
out.dev.port = port;
|
||||
out.dev.reload = reload_flag;
|
||||
out.dev.config_path = config_path;
|
||||
out.dev.db_url = db_url;
|
||||
out.prod.port = port;
|
||||
out.prod.config_path = config_path;
|
||||
out.prod.db_url = db_url;
|
||||
out.prod.tls_cert = json_string(json, "TLS_CERT", "");
|
||||
out.prod.tls_key = json_string(json, "TLS_KEY", "");
|
||||
|
||||
if (out.is_prod() && reload_flag && !allow_prod) {
|
||||
out.error = "refusing --reload with production config without --allow-prod";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
enum class Env { Dev, Prod };
|
||||
|
||||
inline std::string env_name(Env env) { return env == Env::Prod ? "prod" : "dev"; }
|
||||
|
||||
struct DevConfig {
|
||||
int port = 8000;
|
||||
bool reload = false;
|
||||
std::string config_path = ".env.json";
|
||||
std::string db_url;
|
||||
};
|
||||
|
||||
struct ProdConfig {
|
||||
int port = 8000;
|
||||
std::string config_path = ".env.json";
|
||||
std::string db_url;
|
||||
std::string tls_cert;
|
||||
std::string tls_key;
|
||||
};
|
||||
|
||||
struct LoadedConfig {
|
||||
Env env = Env::Dev;
|
||||
DevConfig dev;
|
||||
ProdConfig prod;
|
||||
bool allow_prod = false;
|
||||
std::string error;
|
||||
bool ok() const { return error.empty(); }
|
||||
bool is_prod() const { return env == Env::Prod; }
|
||||
};
|
||||
|
||||
// Loads .env.json plus MOLO_ENV overrides. Refuses --reload against prod
|
||||
// unless --allow-prod is passed (09-cli-ops.md rule 5).
|
||||
class ConfigLoader {
|
||||
public:
|
||||
LoadedConfig load(const std::string& config_path, bool reload_flag, bool allow_prod) const;
|
||||
|
||||
private:
|
||||
static std::string read_file(const std::string& path);
|
||||
static std::string json_string(const std::string& json, const std::string& key,
|
||||
const std::string& fallback);
|
||||
static int json_int(const std::string& json, const std::string& key, int fallback);
|
||||
};
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,131 @@
|
||||
#include "dev/dev_server.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <thread>
|
||||
|
||||
#include "caps.hpp"
|
||||
#include "cli/output.hpp"
|
||||
#include "dev/dev_config.hpp"
|
||||
#include "dev/file_watcher.hpp"
|
||||
#include "dev/reloader.hpp"
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
namespace {
|
||||
std::atomic<bool> g_stop{false};
|
||||
void on_signal(int) { g_stop.store(true); }
|
||||
} // namespace
|
||||
|
||||
bool DevServer::should_refuse(bool is_prod, bool reload, bool allow_prod) {
|
||||
return is_prod && reload && !allow_prod;
|
||||
}
|
||||
|
||||
molo::cli::ExitCode DevServer::run(const ServeOptions& opts, molo::cli::Output& out) {
|
||||
ConfigLoader loader;
|
||||
LoadedConfig cfg = loader.load(opts.config_path, opts.reload, opts.allow_prod);
|
||||
if (!cfg.ok()) {
|
||||
out.error(cfg.error + "\n");
|
||||
return molo::cli::ExitCode::Usage;
|
||||
}
|
||||
if (should_refuse(cfg.is_prod(), opts.reload, opts.allow_prod)) {
|
||||
out.error("refusing --reload with production config without --allow-prod\n");
|
||||
return molo::cli::ExitCode::Usage;
|
||||
}
|
||||
if (cfg.is_prod() && !opts.allow_prod && opts.reload) {
|
||||
out.error("refusing --reload with production config without --allow-prod\n");
|
||||
return molo::cli::ExitCode::Usage;
|
||||
}
|
||||
if (!opts.reload) return run_once(opts, out);
|
||||
return run_reload(opts, out, cfg);
|
||||
}
|
||||
|
||||
molo::cli::ExitCode DevServer::run_once(const ServeOptions& opts, molo::cli::Output& out) {
|
||||
std::signal(SIGINT, on_signal);
|
||||
std::signal(SIGTERM, on_signal);
|
||||
g_stop.store(false);
|
||||
out.info("molo serve: listening on port " + std::to_string(opts.port) + " (single-shot)\n");
|
||||
while (!g_stop.load()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
out.info("molo serve: shutdown clean\n");
|
||||
return molo::cli::ExitCode::Ok;
|
||||
}
|
||||
|
||||
molo::cli::ExitCode DevServer::run_reload(const ServeOptions& opts, molo::cli::Output& out,
|
||||
const LoadedConfig& cfg) {
|
||||
if (cfg.is_prod() && !cfg.dev.db_url.empty()) {
|
||||
// Rule 5: never run the reloader against a production database URL.
|
||||
// A prod URL contains "prod"; refuse loudly rather than guessing.
|
||||
if (cfg.dev.db_url.find("prod") != std::string::npos && !opts.allow_prod) {
|
||||
out.error("refusing reloader against production database without --allow-prod\n");
|
||||
return molo::cli::ExitCode::Usage;
|
||||
}
|
||||
}
|
||||
std::signal(SIGINT, on_signal);
|
||||
std::signal(SIGTERM, on_signal);
|
||||
g_stop.store(false);
|
||||
|
||||
auto watcher = create_default_watcher();
|
||||
std::string werr;
|
||||
if (!watcher->start({"src", "templates", "assets"}, werr)) {
|
||||
// Fall back to polling when the default backend cannot start.
|
||||
watcher.reset();
|
||||
out.warn("watch backend unavailable (" + werr + "), continuing without reload\n");
|
||||
return run_once(opts, out);
|
||||
}
|
||||
out.info("molo serve: watching src/ templates/ assets/ (" + watcher->backend_name() + ")\n");
|
||||
|
||||
Reloader reloader;
|
||||
std::vector<std::string> child = {opts.child_argv0.empty() ? "./build/molo-app" : opts.child_argv0};
|
||||
for (const auto& a : opts.child_args) child.push_back(a);
|
||||
child.push_back("--port");
|
||||
child.push_back(std::to_string(opts.port));
|
||||
|
||||
std::string merr;
|
||||
if (!reloader.run_make("", caps::kMakeTimeoutSec, merr)) {
|
||||
out.error(merr + "\n");
|
||||
return molo::cli::ExitCode::Runtime;
|
||||
}
|
||||
std::string cerr;
|
||||
if (!reloader.start_child(child, cerr)) {
|
||||
out.warn("child start deferred (" + cerr + ")\n");
|
||||
}
|
||||
bool hold_announced = false;
|
||||
while (!g_stop.load()) {
|
||||
auto events = watcher->poll(500);
|
||||
if (!events.empty()) {
|
||||
out.progress("change detected (" + std::to_string(events.size()) + " events), rebuilding\n");
|
||||
if (!reloader.run_make("", caps::kMakeTimeoutSec, merr)) {
|
||||
out.error(merr + "\n");
|
||||
reloader.note_exit(false);
|
||||
} else if (!reloader.start_child(child, cerr)) {
|
||||
out.error(cerr + "\n");
|
||||
reloader.note_exit(false);
|
||||
} else {
|
||||
reloader.note_exit(true);
|
||||
hold_announced = false;
|
||||
}
|
||||
}
|
||||
if (!reloader.child_running() && !child.empty()) {
|
||||
reloader.note_exit(false);
|
||||
int delay = Reloader::backoff_for(reloader.consecutive_failures());
|
||||
if (delay >= caps::kBackoffMaxSec && !hold_announced) {
|
||||
out.error("crash loop: holding at 30s backoff\n");
|
||||
hold_announced = true;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(delay));
|
||||
if (g_stop.load()) break;
|
||||
if (!reloader.start_child(child, cerr)) {
|
||||
out.error(cerr + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
reloader.stop_child(caps::kChildKillGraceSec);
|
||||
watcher->stop();
|
||||
out.info("molo serve: shutdown clean\n");
|
||||
return molo::cli::ExitCode::Ok;
|
||||
}
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cli/exit_code.hpp"
|
||||
#include "dev/dev_config.hpp"
|
||||
|
||||
namespace molo::cli {
|
||||
class Output;
|
||||
}
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
struct ServeOptions {
|
||||
int port = 8000;
|
||||
bool reload = false;
|
||||
std::string config_path = ".env.json";
|
||||
bool allow_prod = false;
|
||||
std::string child_argv0; // binary to supervise; empty means self-serve stub
|
||||
std::vector<std::string> child_args;
|
||||
};
|
||||
|
||||
// Dev-only server supervisor: watch, rebuild via make, restart child.
|
||||
class DevServer {
|
||||
public:
|
||||
// Pure refusal check for tests: prod + reload without --allow-prod.
|
||||
static bool should_refuse(bool is_prod, bool reload, bool allow_prod);
|
||||
|
||||
molo::cli::ExitCode run(const ServeOptions& opts, molo::cli::Output& out);
|
||||
|
||||
private:
|
||||
molo::cli::ExitCode run_once(const ServeOptions& opts, molo::cli::Output& out);
|
||||
molo::cli::ExitCode run_reload(const ServeOptions& opts, molo::cli::Output& out,
|
||||
const LoadedConfig& cfg);
|
||||
};
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "dev/file_watcher.hpp"
|
||||
|
||||
#include "dev/inotify_watcher.hpp"
|
||||
#include "dev/polling_watcher.hpp"
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
std::unique_ptr<FileWatcher> create_default_watcher() {
|
||||
#if defined(__linux__)
|
||||
return std::make_unique<InotifyFileWatcher>();
|
||||
#else
|
||||
return std::make_unique<PollingFileWatcher>();
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
enum class EventKind { Created, Modified, Deleted };
|
||||
|
||||
struct FileEvent {
|
||||
std::string path;
|
||||
EventKind kind = EventKind::Modified;
|
||||
};
|
||||
|
||||
// One interface for inotify/kqueue/polling backends.
|
||||
class FileWatcher {
|
||||
public:
|
||||
virtual ~FileWatcher() = default;
|
||||
virtual bool start(const std::vector<std::string>& roots, std::string& error) = 0;
|
||||
virtual std::vector<FileEvent> poll(int timeout_ms) = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual std::string backend_name() const = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<FileWatcher> create_default_watcher();
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,107 @@
|
||||
#include "dev/inotify_watcher.hpp"
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <poll.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <filesystem>
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::dev {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
InotifyFileWatcher::InotifyFileWatcher() = default;
|
||||
InotifyFileWatcher::~InotifyFileWatcher() { stop(); }
|
||||
|
||||
bool InotifyFileWatcher::start(const std::vector<std::string>& roots, std::string& error) {
|
||||
#if !defined(__linux__)
|
||||
(void)roots;
|
||||
error = "inotify backend only on Linux";
|
||||
return false;
|
||||
#else
|
||||
stop();
|
||||
fd_ = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||
if (fd_ < 0) {
|
||||
error = "inotify_init1 failed";
|
||||
return false;
|
||||
}
|
||||
std::size_t watched = 0;
|
||||
for (const auto& root : roots) {
|
||||
std::error_code ec;
|
||||
if (!fs::exists(root, ec)) continue;
|
||||
if (fs::is_regular_file(root, ec)) continue;
|
||||
fs::recursive_directory_iterator it(root, ec), end;
|
||||
auto add_watch = [&](const std::string& dir) {
|
||||
if (watched >= caps::kMaxWatchFiles) return;
|
||||
int wd = inotify_add_watch(fd_, dir.c_str(), IN_CREATE | IN_MODIFY | IN_DELETE |
|
||||
IN_MOVED_FROM | IN_MOVED_TO);
|
||||
if (wd >= 0) {
|
||||
watch_dirs_[wd] = dir;
|
||||
++watched;
|
||||
}
|
||||
};
|
||||
add_watch(root);
|
||||
while (it != end && !ec && watched < caps::kMaxWatchFiles) {
|
||||
std::error_code e2;
|
||||
if (it->is_directory(e2)) add_watch(it->path().string());
|
||||
it.increment(e2);
|
||||
}
|
||||
}
|
||||
if (watch_dirs_.empty()) {
|
||||
error = "no watch roots exist";
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
started_ = true;
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<FileEvent> InotifyFileWatcher::poll(int timeout_ms) {
|
||||
std::vector<FileEvent> out;
|
||||
#if defined(__linux__)
|
||||
if (!started_ || fd_ < 0) return out;
|
||||
struct pollfd pfd;
|
||||
pfd.fd = fd_;
|
||||
pfd.events = POLLIN;
|
||||
int ready = ::poll(&pfd, 1, timeout_ms);
|
||||
if (ready <= 0) return out;
|
||||
char buf[8192];
|
||||
ssize_t len = read(fd_, buf, sizeof(buf));
|
||||
if (len <= 0) return out;
|
||||
for (char* p = buf; p < buf + len;) {
|
||||
auto* ev = reinterpret_cast<struct inotify_event*>(p);
|
||||
std::string dir = watch_dirs_.count(ev->wd) ? watch_dirs_[ev->wd] : "";
|
||||
std::string name = ev->len > 0 ? std::string(ev->name) : "";
|
||||
FileEvent fe;
|
||||
fe.path = dir.empty() ? name : dir + "/" + name;
|
||||
if ((ev->mask & IN_DELETE) || (ev->mask & IN_MOVED_FROM)) {
|
||||
fe.kind = EventKind::Deleted;
|
||||
} else if (ev->mask & IN_CREATE) {
|
||||
fe.kind = EventKind::Created;
|
||||
} else {
|
||||
fe.kind = EventKind::Modified;
|
||||
}
|
||||
if (!fe.path.empty() && !name.empty()) out.push_back(fe);
|
||||
p += sizeof(struct inotify_event) + ev->len;
|
||||
}
|
||||
#else
|
||||
(void)timeout_ms;
|
||||
#endif
|
||||
return out;
|
||||
}
|
||||
|
||||
void InotifyFileWatcher::stop() {
|
||||
#if defined(__linux__)
|
||||
if (fd_ >= 0) {
|
||||
close(fd_);
|
||||
fd_ = -1;
|
||||
}
|
||||
#endif
|
||||
watch_dirs_.clear();
|
||||
started_ = false;
|
||||
}
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "dev/file_watcher.hpp"
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
// Linux inotify backend. On non-Linux builds start() reports unavailable
|
||||
// and the dev server falls back to the polling backend.
|
||||
class InotifyFileWatcher : public FileWatcher {
|
||||
public:
|
||||
InotifyFileWatcher();
|
||||
~InotifyFileWatcher() override;
|
||||
bool start(const std::vector<std::string>& roots, std::string& error) override;
|
||||
std::vector<FileEvent> poll(int timeout_ms) override;
|
||||
void stop() override;
|
||||
std::string backend_name() const override { return "inotify"; }
|
||||
|
||||
private:
|
||||
int fd_ = -1;
|
||||
std::map<int, std::string> watch_dirs_;
|
||||
bool started_ = false;
|
||||
};
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,107 @@
|
||||
#include "dev/polling_watcher.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::dev {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
bool PollingFileWatcher::start(const std::vector<std::string>& roots, std::string& error) {
|
||||
roots_.clear();
|
||||
mtimes_.clear();
|
||||
pending_.clear();
|
||||
for (const auto& root : roots) {
|
||||
std::error_code ec;
|
||||
if (!fs::exists(root, ec)) continue;
|
||||
roots_.push_back(root);
|
||||
}
|
||||
if (roots_.empty()) {
|
||||
error = "no watch roots exist";
|
||||
return false;
|
||||
}
|
||||
std::vector<FileEvent> ignored;
|
||||
scan(ignored);
|
||||
// Baseline only: drop initial creates.
|
||||
pending_.clear();
|
||||
started_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
long long PollingFileWatcher::mtime_of(const std::string& path) {
|
||||
std::error_code ec;
|
||||
auto t = fs::last_write_time(path, ec);
|
||||
if (ec) return -1;
|
||||
return static_cast<long long>(t.time_since_epoch().count());
|
||||
}
|
||||
|
||||
void PollingFileWatcher::scan(std::vector<FileEvent>& out) {
|
||||
std::map<std::string, long long> now;
|
||||
std::size_t count = 0;
|
||||
for (const auto& root : roots_) {
|
||||
std::error_code ec;
|
||||
if (fs::is_regular_file(root, ec)) {
|
||||
now[root] = mtime_of(root);
|
||||
continue;
|
||||
}
|
||||
fs::recursive_directory_iterator it(root, ec), end;
|
||||
while (it != end && !ec && count < caps::kMaxWatchFiles) {
|
||||
const auto& entry = *it;
|
||||
std::error_code e2;
|
||||
if (entry.is_regular_file(e2)) {
|
||||
std::string p = entry.path().string();
|
||||
now[p] = mtime_of(p);
|
||||
++count;
|
||||
}
|
||||
it.increment(e2);
|
||||
}
|
||||
}
|
||||
auto now_tp = std::chrono::steady_clock::now();
|
||||
for (const auto& [path, mtime] : now) {
|
||||
auto prev = mtimes_.find(path);
|
||||
if (prev == mtimes_.end()) {
|
||||
pending_.emplace(path, now_tp);
|
||||
} else if (prev->second != mtime) {
|
||||
pending_[path] = now_tp;
|
||||
}
|
||||
}
|
||||
for (const auto& [path, _] : mtimes_) {
|
||||
if (now.find(path) == now.end()) {
|
||||
FileEvent ev;
|
||||
ev.path = path;
|
||||
ev.kind = EventKind::Deleted;
|
||||
out.push_back(ev);
|
||||
}
|
||||
}
|
||||
auto debounce = std::chrono::milliseconds(caps::kWatchDebounceMs);
|
||||
for (auto it = pending_.begin(); it != pending_.end();) {
|
||||
if (now_tp - it->second >= debounce) {
|
||||
FileEvent ev;
|
||||
ev.path = it->first;
|
||||
ev.kind = mtimes_.count(it->first) ? EventKind::Modified : EventKind::Created;
|
||||
out.push_back(ev);
|
||||
it = pending_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
mtimes_.swap(now);
|
||||
}
|
||||
|
||||
std::vector<FileEvent> PollingFileWatcher::poll(int timeout_ms) {
|
||||
std::vector<FileEvent> out;
|
||||
if (!started_) return out;
|
||||
if (timeout_ms > 0) std::this_thread::sleep_for(std::chrono::milliseconds(timeout_ms));
|
||||
scan(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void PollingFileWatcher::stop() {
|
||||
started_ = false;
|
||||
roots_.clear();
|
||||
mtimes_.clear();
|
||||
pending_.clear();
|
||||
}
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
|
||||
#include "dev/file_watcher.hpp"
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
// Portable mtime-sweep backend. Caps files at kMaxWatchFiles.
|
||||
class PollingFileWatcher : public FileWatcher {
|
||||
public:
|
||||
bool start(const std::vector<std::string>& roots, std::string& error) override;
|
||||
std::vector<FileEvent> poll(int timeout_ms) override;
|
||||
void stop() override;
|
||||
std::string backend_name() const override { return "poll"; }
|
||||
|
||||
private:
|
||||
std::vector<std::string> roots_;
|
||||
std::map<std::string, long long> mtimes_;
|
||||
std::map<std::string, std::chrono::steady_clock::time_point> pending_;
|
||||
bool started_ = false;
|
||||
|
||||
void scan(std::vector<FileEvent>& out);
|
||||
static long long mtime_of(const std::string& path);
|
||||
};
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,136 @@
|
||||
#include "dev/reloader.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <signal.h>
|
||||
#include <spawn.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
#if !defined(_WIN32)
|
||||
extern char** environ;
|
||||
#endif
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
Reloader::Reloader() = default;
|
||||
Reloader::~Reloader() { stop_child(0); }
|
||||
|
||||
int Reloader::backoff_for(int consecutive_failures) {
|
||||
if (consecutive_failures <= 0) return 0;
|
||||
int delay = caps::kBackoffBaseSec;
|
||||
for (int i = 1; i < consecutive_failures && delay < caps::kBackoffMaxSec; ++i) {
|
||||
delay *= 2;
|
||||
if (delay > caps::kBackoffMaxSec) {
|
||||
delay = caps::kBackoffMaxSec;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return delay;
|
||||
}
|
||||
|
||||
void Reloader::note_exit(bool clean) {
|
||||
if (clean) {
|
||||
failures_ = 0;
|
||||
} else {
|
||||
++failures_;
|
||||
}
|
||||
}
|
||||
|
||||
bool Reloader::run_make(const std::string& make_args, int timeout_sec, std::string& error) {
|
||||
#if defined(_WIN32)
|
||||
(void)make_args;
|
||||
(void)timeout_sec;
|
||||
error = "dev server rebuild only on POSIX";
|
||||
return false;
|
||||
#else
|
||||
std::string jobs = "1";
|
||||
long nproc = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
if (nproc > 1) jobs = std::to_string(nproc);
|
||||
std::string cmd = "make -j" + jobs;
|
||||
if (!make_args.empty()) cmd += " " + make_args;
|
||||
cmd += " > /tmp/molo-make.log 2>&1";
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
int rc = system(cmd.c_str());
|
||||
auto elapsed =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start)
|
||||
.count();
|
||||
if (elapsed > timeout_sec) {
|
||||
error = "make exceeded timeout";
|
||||
return false;
|
||||
}
|
||||
if (rc != 0) {
|
||||
error = "make failed, see /tmp/molo-make.log";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Reloader::start_child(const std::vector<std::string>& argv, std::string& error) {
|
||||
#if defined(_WIN32)
|
||||
(void)argv;
|
||||
error = "dev server child only on POSIX";
|
||||
return false;
|
||||
#else
|
||||
if (argv.empty()) {
|
||||
error = "empty child argv";
|
||||
return false;
|
||||
}
|
||||
stop_child(caps::kChildKillGraceSec);
|
||||
std::vector<char*> args;
|
||||
for (const auto& a : argv) args.push_back(const_cast<char*>(a.c_str()));
|
||||
args.push_back(nullptr);
|
||||
pid_t pid = -1;
|
||||
int rc = posix_spawn(&pid, args[0], nullptr, nullptr, args.data(), ::environ);
|
||||
if (rc != 0 || pid < 0) {
|
||||
error = "posix_spawn failed";
|
||||
return false;
|
||||
}
|
||||
child_pid_ = pid;
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Reloader::stop_child(int grace_sec) {
|
||||
#if !defined(_WIN32)
|
||||
if (child_pid_ < 0) return;
|
||||
pid_t pid = static_cast<pid_t>(child_pid_);
|
||||
kill(pid, SIGTERM);
|
||||
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(grace_sec);
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
int status = 0;
|
||||
pid_t r = waitpid(pid, &status, WNOHANG);
|
||||
if (r == pid || r < 0) {
|
||||
child_pid_ = -1;
|
||||
return;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
kill(pid, SIGKILL);
|
||||
int status = 0;
|
||||
waitpid(pid, &status, 0);
|
||||
child_pid_ = -1;
|
||||
#else
|
||||
(void)grace_sec;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Reloader::child_running() const {
|
||||
#if defined(_WIN32)
|
||||
return false;
|
||||
#else
|
||||
if (child_pid_ < 0) return false;
|
||||
int status = 0;
|
||||
pid_t r = waitpid(static_cast<pid_t>(child_pid_), &status, WNOHANG);
|
||||
return r == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::dev {
|
||||
|
||||
// Runs `make`, supervises the child process, and computes crash backoff.
|
||||
// Dumb by design: no state is preserved across restarts.
|
||||
class Reloader {
|
||||
public:
|
||||
Reloader();
|
||||
~Reloader();
|
||||
|
||||
bool run_make(const std::string& make_args, int timeout_sec, std::string& error);
|
||||
bool start_child(const std::vector<std::string>& argv, std::string& error);
|
||||
void stop_child(int grace_sec);
|
||||
bool child_running() const;
|
||||
|
||||
// Backoff: 1s, 2s, 4s ... capped at kBackoffMaxSec.
|
||||
static int backoff_for(int consecutive_failures);
|
||||
void note_exit(bool clean);
|
||||
int consecutive_failures() const { return failures_; }
|
||||
|
||||
private:
|
||||
long child_pid_ = -1;
|
||||
int failures_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::dev
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "embed/asset_scanner.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::embed {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
ScanResult AssetScanner::scan(const std::string& root) const {
|
||||
ScanResult out;
|
||||
std::error_code ec;
|
||||
fs::path root_path(root);
|
||||
if (!fs::exists(root_path, ec) || !fs::is_directory(root_path, ec)) {
|
||||
out.error = "embed root is not a directory: " + root;
|
||||
return out;
|
||||
}
|
||||
fs::path canon_root = fs::weakly_canonical(root_path, ec);
|
||||
if (ec) {
|
||||
out.error = "cannot resolve embed root: " + root;
|
||||
return out;
|
||||
}
|
||||
std::string root_str = canon_root.string();
|
||||
fs::recursive_directory_iterator it(root_path, ec), end;
|
||||
while (it != end && !ec) {
|
||||
const auto& entry = *it;
|
||||
std::error_code e2;
|
||||
std::string name = entry.path().filename().string();
|
||||
if (!name.empty() && name[0] == '.') {
|
||||
if (entry.is_directory(e2)) it.disable_recursion_pending();
|
||||
it.increment(e2);
|
||||
continue;
|
||||
}
|
||||
if (entry.is_symlink(e2)) {
|
||||
fs::path target = fs::weakly_canonical(entry.path(), e2);
|
||||
if (e2 || target.string().rfind(root_str, 0) != 0) {
|
||||
it.increment(e2);
|
||||
continue; // escaping symlink: skip
|
||||
}
|
||||
}
|
||||
if (entry.is_regular_file(e2)) {
|
||||
std::uint64_t size = entry.file_size(e2);
|
||||
if (!e2) {
|
||||
if (size > caps::kEmbedMaxFileBytes) {
|
||||
out.error = "file exceeds 64 MB cap: " + entry.path().string();
|
||||
return out;
|
||||
}
|
||||
if (out.total_bytes + size > caps::kEmbedTotalBytes) {
|
||||
out.error = "input exceeds 512 MB total cap";
|
||||
return out;
|
||||
}
|
||||
if (out.files.size() >= caps::kEmbedMaxFiles) {
|
||||
out.error = "input exceeds 50000 file cap";
|
||||
return out;
|
||||
}
|
||||
AssetFile file;
|
||||
file.abs_path = entry.path().string();
|
||||
file.rel_path = fs::relative(entry.path(), root_path, e2).generic_string();
|
||||
if (e2) file.rel_path = name;
|
||||
file.size = size;
|
||||
out.files.push_back(std::move(file));
|
||||
out.total_bytes += size;
|
||||
}
|
||||
}
|
||||
it.increment(e2);
|
||||
}
|
||||
std::sort(out.files.begin(), out.files.end(),
|
||||
[](const AssetFile& a, const AssetFile& b) { return a.rel_path < b.rel_path; });
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
struct AssetFile {
|
||||
std::string rel_path; // forward slashes, sorted order
|
||||
std::string abs_path;
|
||||
std::uint64_t size = 0;
|
||||
};
|
||||
|
||||
struct ScanResult {
|
||||
std::vector<AssetFile> files;
|
||||
std::uint64_t total_bytes = 0;
|
||||
std::string error;
|
||||
bool ok() const { return error.empty(); }
|
||||
};
|
||||
|
||||
// Recursive scanner with static caps. Skips dotfiles and symlinks that
|
||||
// escape the scanned root.
|
||||
class AssetScanner {
|
||||
public:
|
||||
ScanResult scan(const std::string& root) const;
|
||||
};
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,179 @@
|
||||
#include "embed/embed_emitter.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include "caps.hpp"
|
||||
#include "embed/gzip_stored.hpp"
|
||||
#include "embed/mime.hpp"
|
||||
#include "embed/sha256.hpp"
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
std::string EmbedEmitter::ident_for(std::size_t index) {
|
||||
return "molo_asset_" + std::to_string(index);
|
||||
}
|
||||
|
||||
std::string EmbedEmitter::escape_cpp_string(const std::string& text) {
|
||||
std::string out;
|
||||
for (char c : text) {
|
||||
if (c == '"' || c == '\\') {
|
||||
out += '\\';
|
||||
out += c;
|
||||
} else if (c == '\n') {
|
||||
out += "\\n";
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool EmbedEmitter::emit(const std::vector<AssetFile>& files, const std::string& out_cpp,
|
||||
const std::string& out_hpp, const EmitOptions& opts,
|
||||
std::string& error) const {
|
||||
struct Prepared {
|
||||
AssetFile file;
|
||||
std::vector<unsigned char> data;
|
||||
std::vector<unsigned char> gz;
|
||||
std::string sha;
|
||||
std::string mime;
|
||||
bool has_gz = false;
|
||||
};
|
||||
std::vector<Prepared> items;
|
||||
for (const auto& file : files) {
|
||||
std::ifstream in(file.abs_path, std::ios::binary);
|
||||
if (!in.good()) {
|
||||
error = "cannot read " + file.abs_path;
|
||||
return false;
|
||||
}
|
||||
Prepared item;
|
||||
item.file = file;
|
||||
item.data.assign(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||||
item.sha = Sha256::hex_of_bytes(item.data);
|
||||
item.mime = Mime::of_path(file.rel_path);
|
||||
if (opts.gzip && Mime::is_textual(item.mime) && item.data.size() > caps::kGzipMinBytes) {
|
||||
item.gz = GzipStored::compress(item.data);
|
||||
item.has_gz = true;
|
||||
}
|
||||
items.push_back(std::move(item));
|
||||
}
|
||||
|
||||
std::vector<std::string> parts;
|
||||
{
|
||||
std::string token;
|
||||
for (char c : opts.namespc) {
|
||||
if (c == ':') {
|
||||
if (!token.empty()) {
|
||||
parts.push_back(token);
|
||||
token.clear();
|
||||
}
|
||||
} else if (c != ' ') {
|
||||
token += c;
|
||||
}
|
||||
}
|
||||
if (!token.empty()) parts.push_back(token);
|
||||
if (parts.empty()) parts.push_back("assets");
|
||||
}
|
||||
|
||||
std::ostringstream hpp;
|
||||
hpp << "// Generated by " << opts.tool_version << ". Do not edit.\n";
|
||||
hpp << "#pragma once\n\n#include <cstddef>\n\n";
|
||||
for (const auto& part : parts) hpp << "namespace " << part << " {\n";
|
||||
hpp << "\nstruct EmbeddedChunk {\n";
|
||||
hpp << " const unsigned char* data;\n unsigned long size;\n};\n\n";
|
||||
hpp << "struct EmbeddedFile {\n";
|
||||
hpp << " const char* path;\n const EmbeddedChunk* chunks;\n";
|
||||
hpp << " unsigned long chunk_count;\n unsigned long size;\n";
|
||||
hpp << " const char* sha256;\n const char* mime;\n";
|
||||
hpp << " const unsigned char* gzip_data;\n unsigned long gzip_size;\n};\n\n";
|
||||
hpp << "const EmbeddedFile* bundle_files();\nunsigned long bundle_count();\n";
|
||||
hpp << "const char* bundle_tool_version();\n";
|
||||
for (auto it = parts.rbegin(); it != parts.rend(); ++it) hpp << "} // namespace " << *it << "\n";
|
||||
|
||||
std::ostringstream cpp;
|
||||
cpp << "// Generated by " << opts.tool_version << ". Do not edit.\n";
|
||||
std::string hpp_base = out_hpp.substr(out_hpp.find_last_of("/\\") + 1);
|
||||
cpp << "#include \"" << hpp_base << "\"\n\n";
|
||||
for (const auto& part : parts) cpp << "namespace " << part << " {\n";
|
||||
cpp << "\nnamespace {\n";
|
||||
auto emit_bytes = [&](const std::string& name, const std::vector<unsigned char>& data,
|
||||
std::size_t begin, std::size_t end) {
|
||||
cpp << "constexpr unsigned char " << name << "[] = {";
|
||||
if (begin == end) cpp << "0";
|
||||
for (std::size_t k = begin; k < end; ++k) {
|
||||
if ((k - begin) % 16 == 0) cpp << "\n ";
|
||||
cpp << static_cast<int>(data[k]) << ",";
|
||||
}
|
||||
cpp << "\n};\n";
|
||||
};
|
||||
for (std::size_t i = 0; i < items.size(); ++i) {
|
||||
const auto& item = items[i];
|
||||
std::size_t chunks =
|
||||
(item.data.size() + caps::kEmbedChunkBytes - 1) / caps::kEmbedChunkBytes;
|
||||
if (chunks == 0) chunks = 1;
|
||||
for (std::size_t c = 0; c < chunks; ++c) {
|
||||
std::size_t begin = c * caps::kEmbedChunkBytes;
|
||||
std::size_t end = std::min(begin + caps::kEmbedChunkBytes, item.data.size());
|
||||
emit_bytes(ident_for(i) + "_c" + std::to_string(c), item.data, begin, end);
|
||||
}
|
||||
cpp << "constexpr EmbeddedChunk " << ident_for(i) << "_chunks[] = {\n";
|
||||
for (std::size_t c = 0; c < chunks; ++c) {
|
||||
std::size_t begin = c * caps::kEmbedChunkBytes;
|
||||
std::size_t end = std::min(begin + caps::kEmbedChunkBytes, item.data.size());
|
||||
unsigned long size = static_cast<unsigned long>(end - begin);
|
||||
cpp << " {" << ident_for(i) << "_c" << c << ", " << size << "},\n";
|
||||
}
|
||||
cpp << "};\n";
|
||||
if (item.has_gz) emit_bytes(ident_for(i) + "_gz", item.gz, 0, item.gz.size());
|
||||
cpp << "constexpr char " << ident_for(i) << "_path[] = \""
|
||||
<< escape_cpp_string(item.file.rel_path) << "\";\n";
|
||||
cpp << "constexpr char " << ident_for(i) << "_sha[] = \"" << item.sha << "\";\n";
|
||||
cpp << "constexpr char " << ident_for(i) << "_mime[] = \"" << escape_cpp_string(item.mime)
|
||||
<< "\";\n";
|
||||
}
|
||||
cpp << "} // namespace\n\n";
|
||||
cpp << "namespace {\nconst EmbeddedFile kFiles[] = {\n";
|
||||
for (std::size_t i = 0; i < items.size(); ++i) {
|
||||
const auto& item = items[i];
|
||||
std::size_t chunks =
|
||||
(item.data.size() + caps::kEmbedChunkBytes - 1) / caps::kEmbedChunkBytes;
|
||||
if (chunks == 0) chunks = 1;
|
||||
cpp << " {" << ident_for(i) << "_path, " << ident_for(i) << "_chunks, "
|
||||
<< static_cast<unsigned long>(chunks) << ", "
|
||||
<< static_cast<unsigned long>(item.data.size()) << ", " << ident_for(i) << "_sha, "
|
||||
<< ident_for(i) << "_mime, ";
|
||||
if (item.has_gz) {
|
||||
cpp << ident_for(i) << "_gz, " << static_cast<unsigned long>(item.gz.size());
|
||||
} else {
|
||||
cpp << "nullptr, 0";
|
||||
}
|
||||
cpp << "},\n";
|
||||
}
|
||||
cpp << "};\n} // namespace\n\n";
|
||||
cpp << "const EmbeddedFile* bundle_files() { return kFiles; }\n";
|
||||
cpp << "unsigned long bundle_count() { return " << items.size() << "; }\n";
|
||||
cpp << "const char* bundle_tool_version() { return \"" << escape_cpp_string(opts.tool_version)
|
||||
<< "\"; }\n";
|
||||
for (auto it = parts.rbegin(); it != parts.rend(); ++it) cpp << "} // namespace " << *it << "\n";
|
||||
|
||||
std::error_code ec;
|
||||
if (auto parent = std::filesystem::path(out_cpp).parent_path(); !parent.empty()) {
|
||||
std::filesystem::create_directories(parent, ec);
|
||||
}
|
||||
if (auto parent = std::filesystem::path(out_hpp).parent_path(); !parent.empty()) {
|
||||
std::filesystem::create_directories(parent, ec);
|
||||
}
|
||||
std::ofstream hpp_out(out_hpp, std::ios::binary | std::ios::trunc);
|
||||
std::ofstream cpp_out(out_cpp, std::ios::binary | std::ios::trunc);
|
||||
if (!hpp_out.good() || !cpp_out.good()) {
|
||||
error = "cannot write output files";
|
||||
return false;
|
||||
}
|
||||
hpp_out << hpp.str();
|
||||
cpp_out << cpp.str();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "embed/asset_scanner.hpp"
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
struct EmitOptions {
|
||||
std::string namespc = "molo::assets";
|
||||
bool gzip = false;
|
||||
std::string tool_version = "molo-embed-1";
|
||||
};
|
||||
|
||||
// Emits one .hpp plus one .cpp with constexpr byte arrays. Output is
|
||||
// byte-identical reproducible: sorted order, no timestamps.
|
||||
class EmbedEmitter {
|
||||
public:
|
||||
bool emit(const std::vector<AssetFile>& files, const std::string& out_cpp,
|
||||
const std::string& out_hpp, const EmitOptions& opts, std::string& error) const;
|
||||
|
||||
private:
|
||||
static std::string ident_for(std::size_t index);
|
||||
static std::string escape_cpp_string(const std::string& text);
|
||||
};
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "embed/embedded_bundle.hpp"
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
void EmbeddedBundle::add(EmbeddedAsset asset) { assets_[asset.path] = std::move(asset); }
|
||||
|
||||
const EmbeddedAsset* EmbeddedBundle::find(const std::string& path) const {
|
||||
auto it = assets_.find(path);
|
||||
if (it == assets_.end()) return nullptr;
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
std::vector<std::string> EmbeddedBundle::paths() const {
|
||||
std::vector<std::string> out;
|
||||
for (const auto& [path, _] : assets_) out.push_back(path);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
struct EmbeddedAsset {
|
||||
std::string path;
|
||||
std::vector<unsigned char> data;
|
||||
std::string sha256;
|
||||
std::string mime;
|
||||
std::vector<unsigned char> gzip_data;
|
||||
bool has_gzip = false;
|
||||
};
|
||||
|
||||
// Runtime reader for embedded bundles. Static routes (01-runtime.md) serve
|
||||
// from this instead of disk. Generated-code adapters fill it by joining the
|
||||
// emitted 1 MB chunks in order.
|
||||
class EmbeddedBundle {
|
||||
public:
|
||||
void add(EmbeddedAsset asset);
|
||||
const EmbeddedAsset* find(const std::string& path) const;
|
||||
std::size_t size() const { return assets_.size(); }
|
||||
std::vector<std::string> paths() const;
|
||||
|
||||
private:
|
||||
std::map<std::string, EmbeddedAsset> assets_;
|
||||
};
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "embed/gzip_stored.hpp"
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
uint32_t GzipStored::crc32(const std::vector<unsigned char>& data) {
|
||||
uint32_t crc = 0xFFFFFFFFu;
|
||||
for (unsigned char b : data) {
|
||||
crc ^= b;
|
||||
for (int i = 0; i < 8; ++i) crc = (crc & 1) ? (crc >> 1) ^ 0xEDB88320u : (crc >> 1);
|
||||
}
|
||||
return crc ^ 0xFFFFFFFFu;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> GzipStored::compress(const std::vector<unsigned char>& data) {
|
||||
std::vector<unsigned char> out;
|
||||
// gzip header: magic, deflate, flags=0, mtime=0 (reproducible), xfl=0, os=3
|
||||
out.insert(out.end(), {0x1F, 0x8B, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03});
|
||||
std::size_t pos = 0;
|
||||
if (data.empty()) {
|
||||
out.insert(out.end(), {0x01, 0x00, 0x00, 0xFF, 0xFF});
|
||||
}
|
||||
while (pos < data.size()) {
|
||||
std::size_t n = std::min<std::size_t>(65535, data.size() - pos);
|
||||
bool last = (pos + n == data.size());
|
||||
out.push_back(last ? 0x01 : 0x00); // BFINAL + BTYPE=00 stored
|
||||
out.push_back(static_cast<unsigned char>(n & 0xFF));
|
||||
out.push_back(static_cast<unsigned char>((n >> 8) & 0xFF));
|
||||
out.push_back(static_cast<unsigned char>((~n) & 0xFF));
|
||||
out.push_back(static_cast<unsigned char>((~n >> 8) & 0xFF));
|
||||
out.insert(out.end(), data.begin() + pos, data.begin() + pos + n);
|
||||
pos += n;
|
||||
}
|
||||
uint32_t crc = crc32(data);
|
||||
uint32_t isize = static_cast<uint32_t>(data.size());
|
||||
for (int i = 0; i < 4; ++i) out.push_back(static_cast<unsigned char>((crc >> (i * 8)) & 0xFF));
|
||||
for (int i = 0; i < 4; ++i) out.push_back(static_cast<unsigned char>((isize >> (i * 8)) & 0xFF));
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> GzipStored::compress_string(const std::string& text) {
|
||||
return compress(std::vector<unsigned char>(text.begin(), text.end()));
|
||||
}
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
// Valid gzip writer using stored (uncompressed) deflate blocks.
|
||||
// Output is a correct gzip member; ratio work is out of scope for v1.
|
||||
class GzipStored {
|
||||
public:
|
||||
static std::vector<unsigned char> compress(const std::vector<unsigned char>& data);
|
||||
static std::vector<unsigned char> compress_string(const std::string& text);
|
||||
static uint32_t crc32(const std::vector<unsigned char>& data);
|
||||
};
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "embed/mime.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
std::string Mime::of_path(const std::string& path) {
|
||||
static const std::map<std::string, std::string> table = {
|
||||
{".html", "text/html"}, {".htm", "text/html"}, {".css", "text/css"},
|
||||
{".js", "text/javascript"}, {".mjs", "text/javascript"}, {".json", "application/json"},
|
||||
{".xml", "application/xml"}, {".txt", "text/plain"}, {".md", "text/markdown"},
|
||||
{".csv", "text/csv"}, {".svg", "image/svg+xml"}, {".png", "image/png"},
|
||||
{".jpg", "image/jpeg"}, {".jpeg", "image/jpeg"}, {".gif", "image/gif"},
|
||||
{".webp", "image/webp"}, {".ico", "image/x-icon"}, {".woff", "font/woff"},
|
||||
{".woff2", "font/woff2"}, {".ttf", "font/ttf"}, {".pdf", "application/pdf"},
|
||||
{".wasm", "application/wasm"},
|
||||
};
|
||||
auto dot = path.find_last_of('.');
|
||||
if (dot == std::string::npos) return "application/octet-stream";
|
||||
std::string ext = path.substr(dot);
|
||||
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
|
||||
auto it = table.find(ext);
|
||||
if (it == table.end()) return "application/octet-stream";
|
||||
return it->second;
|
||||
}
|
||||
|
||||
bool Mime::is_textual(const std::string& mime) {
|
||||
return mime.rfind("text/", 0) == 0 || mime == "application/json" ||
|
||||
mime == "application/xml" || mime == "image/svg+xml" ||
|
||||
mime == "application/javascript" || mime == "text/javascript";
|
||||
}
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
// Extension based mime table for embedded assets.
|
||||
class Mime {
|
||||
public:
|
||||
static std::string of_path(const std::string& path);
|
||||
static bool is_textual(const std::string& mime);
|
||||
};
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "embed/sha256.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
namespace {
|
||||
uint32_t rotr(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }
|
||||
|
||||
const std::array<uint32_t, 64> kK = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
|
||||
0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
|
||||
0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
|
||||
0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
||||
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
|
||||
0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
||||
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
|
||||
0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
|
||||
0xc67178f2};
|
||||
|
||||
std::array<uint32_t, 8> digest(const unsigned char* data, std::size_t len) {
|
||||
std::array<uint32_t, 8> h = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
|
||||
std::vector<unsigned char> msg(data, data + len);
|
||||
uint64_t bit_len = static_cast<uint64_t>(len) * 8;
|
||||
msg.push_back(0x80);
|
||||
while (msg.size() % 64 != 56) msg.push_back(0x00);
|
||||
for (int i = 7; i >= 0; --i) msg.push_back(static_cast<unsigned char>(bit_len >> (i * 8)));
|
||||
for (std::size_t off = 0; off < msg.size(); off += 64) {
|
||||
std::array<uint32_t, 64> w{};
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
w[i] = (static_cast<uint32_t>(msg[off + i * 4]) << 24) |
|
||||
(static_cast<uint32_t>(msg[off + i * 4 + 1]) << 16) |
|
||||
(static_cast<uint32_t>(msg[off + i * 4 + 2]) << 8) |
|
||||
static_cast<uint32_t>(msg[off + i * 4 + 3]);
|
||||
}
|
||||
for (int i = 16; i < 64; ++i) {
|
||||
uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||
uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||
}
|
||||
uint32_t a = h[0], b = h[1], c = h[2], d = h[3];
|
||||
uint32_t e = h[4], f = h[5], g = h[6], hh = h[7];
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
uint32_t s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
||||
uint32_t ch = (e & f) ^ (~e & g);
|
||||
uint32_t t1 = hh + s1 + ch + kK[i] + w[i];
|
||||
uint32_t s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
||||
uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
||||
uint32_t t2 = s0 + maj;
|
||||
hh = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
h[0] += a;
|
||||
h[1] += b;
|
||||
h[2] += c;
|
||||
h[3] += d;
|
||||
h[4] += e;
|
||||
h[5] += f;
|
||||
h[6] += g;
|
||||
h[7] += hh;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
std::string to_hex(std::array<uint32_t, 8> h) {
|
||||
char buf[65];
|
||||
snprintf(buf, sizeof(buf), "%08x%08x%08x%08x%08x%08x%08x%08x", h[0], h[1], h[2], h[3], h[4], h[5],
|
||||
h[6], h[7]);
|
||||
return std::string(buf);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string Sha256::hex_of_bytes(const std::vector<unsigned char>& data) {
|
||||
return to_hex(digest(data.data(), data.size()));
|
||||
}
|
||||
|
||||
std::string Sha256::hex_of_string(const std::string& text) {
|
||||
return to_hex(digest(reinterpret_cast<const unsigned char*>(text.data()), text.size()));
|
||||
}
|
||||
|
||||
bool Sha256::hex_of_file(const std::string& path, std::string& hex_out) {
|
||||
std::ifstream in(path, std::ios::binary);
|
||||
if (!in.good()) return false;
|
||||
std::vector<unsigned char> data((std::istreambuf_iterator<char>(in)),
|
||||
std::istreambuf_iterator<char>());
|
||||
hex_out = hex_of_bytes(data);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::embed {
|
||||
|
||||
// In-house SHA-256 (stdlib only). Used for embed manifests.
|
||||
class Sha256 {
|
||||
public:
|
||||
static std::string hex_of_bytes(const std::vector<unsigned char>& data);
|
||||
static std::string hex_of_string(const std::string& text);
|
||||
static bool hex_of_file(const std::string& path, std::string& hex_out);
|
||||
};
|
||||
|
||||
} // namespace molo::embed
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "i18n/catalog.hpp"
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
Catalog::Catalog(Locale locale) : locale_(locale) {}
|
||||
|
||||
void Catalog::add(CatalogEntry entry) {
|
||||
std::string key = entry.context + "\x04" + entry.msgid;
|
||||
entries_[key] = std::move(entry);
|
||||
}
|
||||
|
||||
const CatalogEntry* Catalog::find(const std::string& msgid, const std::string& context) const {
|
||||
auto it = entries_.find(context + "\x04" + msgid);
|
||||
if (it == entries_.end()) return nullptr;
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "i18n/locale.hpp"
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
struct CatalogEntry {
|
||||
std::string msgid;
|
||||
std::string context;
|
||||
std::vector<std::string> forms; // forms[0] singular, rest plurals
|
||||
};
|
||||
|
||||
// One compiled message catalog for one locale.
|
||||
class Catalog {
|
||||
public:
|
||||
explicit Catalog(Locale locale);
|
||||
|
||||
const Locale& locale() const { return locale_; }
|
||||
void add(CatalogEntry entry);
|
||||
const CatalogEntry* find(const std::string& msgid, const std::string& context = "") const;
|
||||
std::size_t size() const { return entries_.size(); }
|
||||
|
||||
private:
|
||||
Locale locale_;
|
||||
std::map<std::string, CatalogEntry> entries_;
|
||||
};
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "i18n/locale.hpp"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
Locale Locale::parse(const std::string& tag) {
|
||||
Locale out;
|
||||
std::string clean;
|
||||
for (char c : tag) {
|
||||
if (c == '_') {
|
||||
clean += '-';
|
||||
} else {
|
||||
clean += c;
|
||||
}
|
||||
}
|
||||
auto dash = clean.find('-');
|
||||
std::string lang = dash == std::string::npos ? clean : clean.substr(0, dash);
|
||||
std::string region = dash == std::string::npos ? "" : clean.substr(dash + 1);
|
||||
auto at = region.find_first_of("-@.");
|
||||
if (at != std::string::npos) region = region.substr(0, at);
|
||||
for (char c : lang) {
|
||||
if (!std::isalpha(static_cast<unsigned char>(c))) return Locale();
|
||||
out.language_ += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
}
|
||||
for (char c : region) {
|
||||
if (!std::isalnum(static_cast<unsigned char>(c))) break;
|
||||
out.region_ += static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
||||
}
|
||||
if (out.language_.empty()) return Locale();
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string Locale::str() const {
|
||||
if (region_.empty()) return language_;
|
||||
return language_ + "-" + region_;
|
||||
}
|
||||
|
||||
std::vector<Locale> Locale::fallback_chain() const {
|
||||
std::vector<Locale> chain;
|
||||
if (empty()) {
|
||||
chain.push_back(Locale::english());
|
||||
return chain;
|
||||
}
|
||||
chain.push_back(*this);
|
||||
if (!region_.empty()) chain.push_back(Locale::parse(language_));
|
||||
if (language_ != "en") chain.push_back(Locale::english());
|
||||
return chain;
|
||||
}
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
// BCP 47 locale tag (en, nl-NL). Normalizes language lower, region upper.
|
||||
class Locale {
|
||||
public:
|
||||
static Locale parse(const std::string& tag);
|
||||
static Locale english() { return parse("en"); }
|
||||
|
||||
const std::string& language() const { return language_; }
|
||||
const std::string& region() const { return region_; }
|
||||
std::string str() const;
|
||||
bool empty() const { return language_.empty(); }
|
||||
|
||||
// Fallback chain: full tag, language, en (unless already en).
|
||||
std::vector<Locale> fallback_chain() const;
|
||||
|
||||
private:
|
||||
std::string language_;
|
||||
std::string region_;
|
||||
};
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "i18n/plural_rule.hpp"
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
int PluralRule::form_count(const std::string& language) {
|
||||
if (language == "ru" || language == "uk" || language == "pl") return 3;
|
||||
if (language == "ar") return 6;
|
||||
if (language == "ja" || language == "zh" || language == "ko") return 1;
|
||||
return 2; // en, nl, de, fr, es and default
|
||||
}
|
||||
|
||||
int PluralRule::form_index(long count, const std::string& language) {
|
||||
long n = count < 0 ? -count : count;
|
||||
if (language == "ja" || language == "zh" || language == "ko") return 0;
|
||||
if (language == "fr") return (n == 0 || n == 1) ? 0 : 1;
|
||||
if (language == "ru" || language == "uk") {
|
||||
long mod10 = n % 10;
|
||||
long mod100 = n % 100;
|
||||
if (mod10 == 1 && mod100 != 11) return 0;
|
||||
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return 1;
|
||||
return 2;
|
||||
}
|
||||
if (language == "pl") {
|
||||
if (n == 1) return 0;
|
||||
long mod10 = n % 10;
|
||||
long mod100 = n % 100;
|
||||
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return 1;
|
||||
return 2;
|
||||
}
|
||||
if (language == "ar") {
|
||||
if (n == 0) return 0;
|
||||
if (n == 1) return 1;
|
||||
if (n == 2) return 2;
|
||||
if (n % 100 >= 3 && n % 100 <= 10) return 3;
|
||||
if (n % 100 >= 11) return 4;
|
||||
return 5;
|
||||
}
|
||||
return n == 1 ? 0 : 1; // en, nl, de, es and default
|
||||
}
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
// CLDR cardinal plural rules for shipped locales.
|
||||
class PluralRule {
|
||||
public:
|
||||
static int form_count(const std::string& language);
|
||||
static int form_index(long count, const std::string& language);
|
||||
};
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,138 @@
|
||||
#include "i18n/po_loader.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
namespace {
|
||||
std::string unescape(const std::string& quoted) {
|
||||
std::string out;
|
||||
bool esc = false;
|
||||
for (std::size_t i = 0; i < quoted.size(); ++i) {
|
||||
char c = quoted[i];
|
||||
if (esc) {
|
||||
switch (c) {
|
||||
case 'n':
|
||||
out += '\n';
|
||||
break;
|
||||
case 't':
|
||||
out += '\t';
|
||||
break;
|
||||
case 'r':
|
||||
out += '\r';
|
||||
break;
|
||||
case '\\':
|
||||
out += '\\';
|
||||
break;
|
||||
case '"':
|
||||
out += '"';
|
||||
break;
|
||||
default:
|
||||
out += c;
|
||||
break;
|
||||
}
|
||||
esc = false;
|
||||
} else if (c == '\\') {
|
||||
esc = true;
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string strip_quotes(const std::string& line) {
|
||||
auto first = line.find('"');
|
||||
auto last = line.rfind('"');
|
||||
if (first == std::string::npos || last == first) return "";
|
||||
return unescape(line.substr(first + 1, last - first - 1));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
PoLoadResult PoLoader::load_file(const std::string& path, const Locale& locale) {
|
||||
std::ifstream in(path);
|
||||
if (!in.good()) {
|
||||
PoLoadResult out;
|
||||
out.catalog = Catalog(locale);
|
||||
out.error = "cannot open PO file: " + path;
|
||||
return out;
|
||||
}
|
||||
std::ostringstream buf;
|
||||
buf << in.rdbuf();
|
||||
return load_text(buf.str(), locale);
|
||||
}
|
||||
|
||||
PoLoadResult PoLoader::load_text(const std::string& text, const Locale& locale) {
|
||||
PoLoadResult out;
|
||||
out.catalog = Catalog(locale);
|
||||
std::istringstream lines(text);
|
||||
std::string line;
|
||||
CatalogEntry current;
|
||||
std::map<int, std::string> plural_forms;
|
||||
std::string* target = nullptr;
|
||||
int target_plural = -1;
|
||||
auto flush = [&]() {
|
||||
if (!current.msgid.empty()) {
|
||||
if (!plural_forms.empty()) {
|
||||
int max_index = 0;
|
||||
for (const auto& [index, _] : plural_forms) max_index = std::max(max_index, index);
|
||||
current.forms.resize(static_cast<std::size_t>(max_index) + 1);
|
||||
for (const auto& [index, value] : plural_forms) {
|
||||
current.forms[static_cast<std::size_t>(index)] = value;
|
||||
}
|
||||
}
|
||||
out.catalog.add(current);
|
||||
}
|
||||
current = CatalogEntry();
|
||||
plural_forms.clear();
|
||||
target = nullptr;
|
||||
target_plural = -1;
|
||||
};
|
||||
while (std::getline(lines, line)) {
|
||||
if (line.empty()) {
|
||||
flush();
|
||||
continue;
|
||||
}
|
||||
if (line[0] == '#') continue; // translator comments and flags
|
||||
if (line.rfind("msgctxt", 0) == 0) {
|
||||
current.context = strip_quotes(line);
|
||||
target = ¤t.context;
|
||||
target_plural = -1;
|
||||
} else if (line.rfind("msgid_plural", 0) == 0) {
|
||||
target = nullptr; // plural msgid kept implicitly by form order
|
||||
target_plural = -2;
|
||||
(void)strip_quotes(line);
|
||||
} else if (line.rfind("msgid", 0) == 0) {
|
||||
current.msgid = strip_quotes(line);
|
||||
if (current.forms.empty()) current.forms.push_back("");
|
||||
target = ¤t.msgid;
|
||||
target_plural = -1;
|
||||
} else if (line.rfind("msgstr[", 0) == 0) {
|
||||
auto open = line.find('[');
|
||||
auto close = line.find(']');
|
||||
int index = 0;
|
||||
try {
|
||||
index = std::stoi(line.substr(open + 1, close - open - 1));
|
||||
} catch (...) {
|
||||
index = 0;
|
||||
}
|
||||
plural_forms[index] = strip_quotes(line);
|
||||
target = &plural_forms[index];
|
||||
target_plural = index;
|
||||
} else if (line.rfind("msgstr", 0) == 0) {
|
||||
if (current.forms.empty()) current.forms.push_back("");
|
||||
current.forms[0] = strip_quotes(line);
|
||||
target = ¤t.forms[0];
|
||||
target_plural = -1;
|
||||
} else if (line[0] == '"') {
|
||||
if (target != nullptr) *target += strip_quotes(line);
|
||||
(void)target_plural;
|
||||
}
|
||||
}
|
||||
flush();
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "i18n/catalog.hpp"
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
struct PoLoadResult {
|
||||
Catalog catalog = Catalog(Locale::english());
|
||||
std::string error;
|
||||
bool ok() const { return error.empty(); }
|
||||
};
|
||||
|
||||
// GNU gettext PO loader: msgid, msgstr, msgid_plural, msgstr[n], msgctxt,
|
||||
// translator comments (#), multiline strings, C escapes.
|
||||
class PoLoader {
|
||||
public:
|
||||
static PoLoadResult load_file(const std::string& path, const Locale& locale);
|
||||
static PoLoadResult load_text(const std::string& text, const Locale& locale);
|
||||
};
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,91 @@
|
||||
#include "i18n/translator.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "i18n/plural_rule.hpp"
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
void Translator::add_catalog(Catalog catalog) {
|
||||
catalogs_.insert_or_assign(catalog.locale().str(), std::move(catalog));
|
||||
}
|
||||
|
||||
std::string Translator::translate(const std::string& msgid, const Locale& locale, long count,
|
||||
const std::string& context) {
|
||||
for (const auto& candidate : locale.fallback_chain()) {
|
||||
auto it = catalogs_.find(candidate.str());
|
||||
if (it == catalogs_.end() && !candidate.region().empty()) {
|
||||
it = catalogs_.find(candidate.language());
|
||||
}
|
||||
if (it == catalogs_.end()) continue;
|
||||
const CatalogEntry* entry = it->second.find(msgid, context);
|
||||
if (entry == nullptr || entry->forms.empty()) continue;
|
||||
int index = PluralRule::form_index(count, candidate.language());
|
||||
if (index < 0 || static_cast<std::size_t>(index) >= entry->forms.size()) {
|
||||
return entry->forms[0];
|
||||
}
|
||||
const std::string& form = entry->forms[static_cast<std::size_t>(index)];
|
||||
return form.empty() ? msgid : form;
|
||||
}
|
||||
if (missing_.insert(context + "\x04" + msgid).second) {
|
||||
std::string note = "i18n: missing key '" + msgid + "' for locale " + locale.str() + "\n";
|
||||
if (missing_sink_) {
|
||||
missing_sink_(note);
|
||||
} else {
|
||||
std::cerr << note;
|
||||
}
|
||||
}
|
||||
return msgid;
|
||||
}
|
||||
|
||||
Locale Translator::parse_accept_language(const std::string& header) {
|
||||
// Honor q-values: pick the highest-q parseable tag.
|
||||
std::istringstream parts(header);
|
||||
std::string item;
|
||||
double best_q = -1.0;
|
||||
Locale best;
|
||||
while (std::getline(parts, item, ',')) {
|
||||
auto semi = item.find(';');
|
||||
std::string tag = semi == std::string::npos ? item : item.substr(0, semi);
|
||||
double q = 1.0;
|
||||
if (semi != std::string::npos) {
|
||||
auto qpos = item.find("q=", semi);
|
||||
if (qpos != std::string::npos) {
|
||||
try {
|
||||
q = std::stod(item.substr(qpos + 2));
|
||||
} catch (...) {
|
||||
q = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (!tag.empty() && tag.front() == ' ') tag.erase(0, 1);
|
||||
while (!tag.empty() && tag.back() == ' ') tag.pop_back();
|
||||
if (tag.empty() || tag == "*") continue;
|
||||
Locale loc = Locale::parse(tag);
|
||||
if (loc.empty()) continue;
|
||||
if (q > best_q) {
|
||||
best_q = q;
|
||||
best = loc;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
Locale Translator::resolve(const RequestLocale& request) {
|
||||
if (!request.route_prefix.empty()) {
|
||||
Locale loc = Locale::parse(request.route_prefix);
|
||||
if (!loc.empty()) return loc;
|
||||
}
|
||||
if (!request.cookie.empty()) {
|
||||
Locale loc = Locale::parse(request.cookie);
|
||||
if (!loc.empty()) return loc;
|
||||
}
|
||||
Locale header = parse_accept_language(request.accept_language);
|
||||
if (!header.empty()) return header;
|
||||
Locale def = Locale::parse(request.config_default);
|
||||
if (!def.empty()) return def;
|
||||
return Locale::english();
|
||||
}
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "i18n/catalog.hpp"
|
||||
#include "i18n/locale.hpp"
|
||||
|
||||
namespace molo::i18n {
|
||||
|
||||
struct RequestLocale {
|
||||
std::string route_prefix; // e.g. "nl" from /nl/...
|
||||
std::string cookie; // molo_locale cookie value
|
||||
std::string accept_language; // raw header
|
||||
std::string config_default = "en";
|
||||
};
|
||||
|
||||
// Translator with GNU gettext fallback: full tag, language, en, msgid.
|
||||
// Missing keys log once per key per process. Template hooks (05-request-
|
||||
// routing.md) call translate() via t() filters and functions.
|
||||
class Translator {
|
||||
public:
|
||||
using MissingSink = std::function<void(const std::string&)>;
|
||||
|
||||
void add_catalog(Catalog catalog);
|
||||
void set_missing_sink(MissingSink sink) { missing_sink_ = std::move(sink); }
|
||||
|
||||
std::string translate(const std::string& msgid, const Locale& locale, long count = 1,
|
||||
const std::string& context = "");
|
||||
|
||||
// Resolution order: route prefix, cookie, Accept-Language, default.
|
||||
static Locale resolve(const RequestLocale& request);
|
||||
static Locale parse_accept_language(const std::string& header);
|
||||
|
||||
std::size_t missing_count() const { return missing_.size(); }
|
||||
|
||||
private:
|
||||
std::map<std::string, Catalog> catalogs_;
|
||||
std::set<std::string> missing_;
|
||||
MissingSink missing_sink_;
|
||||
};
|
||||
|
||||
} // namespace molo::i18n
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "cli/cli_app.hpp"
|
||||
#include "cli/exit_code.hpp"
|
||||
|
||||
int main(int argc, const char* const* argv) {
|
||||
molo::cli::CliApp app;
|
||||
return molo::cli::to_int(app.run(argc, argv));
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#include "ops/health_check.hpp"
|
||||
|
||||
// HealthCheck is interface plus value types; kept as a pair for structure.
|
||||
namespace molo::ops {} // namespace molo::ops
|
||||
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
enum class HealthState { Ok, Degraded, Failed };
|
||||
|
||||
inline std::string health_state_name(HealthState state) {
|
||||
switch (state) {
|
||||
case HealthState::Ok:
|
||||
return "ok";
|
||||
case HealthState::Degraded:
|
||||
return "degraded";
|
||||
case HealthState::Failed:
|
||||
return "failed";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
struct HealthResult {
|
||||
HealthState state = HealthState::Ok;
|
||||
std::string detail;
|
||||
long elapsed_ms = 0;
|
||||
bool timed_out = false;
|
||||
};
|
||||
|
||||
// Base class for liveness and readiness checks.
|
||||
class HealthCheck {
|
||||
public:
|
||||
virtual ~HealthCheck() = default;
|
||||
virtual std::string name() const = 0;
|
||||
virtual HealthResult check() = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "ops/health_registry.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <future>
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
void HealthRegistry::add(std::shared_ptr<HealthCheck> check) {
|
||||
checks_.push_back(std::move(check));
|
||||
}
|
||||
|
||||
std::vector<NamedResult> HealthRegistry::run_all(int timeout_ms) const {
|
||||
std::vector<NamedResult> out;
|
||||
for (const auto& check : checks_) {
|
||||
NamedResult named;
|
||||
named.name = check->name();
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
std::future<HealthResult> fut =
|
||||
std::async(std::launch::async, [&check]() { return check->check(); });
|
||||
if (fut.wait_for(std::chrono::milliseconds(timeout_ms)) == std::future_status::ready) {
|
||||
named.result = fut.get();
|
||||
} else {
|
||||
named.result.state = HealthState::Failed;
|
||||
named.result.detail = "check timed out";
|
||||
named.result.timed_out = true;
|
||||
}
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
named.result.elapsed_ms =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
out.push_back(std::move(named));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "ops/health_check.hpp"
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
struct NamedResult {
|
||||
std::string name;
|
||||
HealthResult result;
|
||||
};
|
||||
|
||||
// Registry of checks. Each check is capped at kHealthCheckTimeoutMs;
|
||||
// slow checks are reported as timed out, never awaited unbounded.
|
||||
class HealthRegistry {
|
||||
public:
|
||||
void add(std::shared_ptr<HealthCheck> check);
|
||||
std::vector<NamedResult> run_all(int timeout_ms = 200) const;
|
||||
std::size_t size() const { return checks_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<HealthCheck>> checks_;
|
||||
};
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "ops/liveness_check.hpp"
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
HealthResult LivenessCheck::check() {
|
||||
HealthResult out;
|
||||
if (shutdown_stuck_.load()) {
|
||||
out.state = HealthState::Failed;
|
||||
out.detail = "shutdown stuck";
|
||||
} else {
|
||||
out.state = HealthState::Ok;
|
||||
out.detail = "loop serving";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "ops/health_check.hpp"
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
// Liveness: fails only when shutdown is stuck. Never touches the DB.
|
||||
class LivenessCheck : public HealthCheck {
|
||||
public:
|
||||
std::string name() const override { return "liveness"; }
|
||||
HealthResult check() override;
|
||||
void set_shutdown_stuck(bool stuck) { shutdown_stuck_.store(stuck); }
|
||||
|
||||
private:
|
||||
std::atomic<bool> shutdown_stuck_{false};
|
||||
};
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,147 @@
|
||||
#include "ops/ops_routes.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "cli/json_writer.hpp"
|
||||
#include "operability/debug/endpoint.h"
|
||||
#include "operability/debug/snapshot.h"
|
||||
#include "ops/health_registry.hpp"
|
||||
#include "ops/prometheus_exporter.hpp"
|
||||
#include "ops/readiness_check.hpp"
|
||||
#include "ops/startup_gate.hpp"
|
||||
#include "supervisor/common/clock.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
namespace {
|
||||
|
||||
// Fail-closed admin auth until the 03 security gate plugs in here
|
||||
// (operability PATCHES section 9): debug stays 403 in debug builds.
|
||||
[[maybe_unused]] bool ops_admin_auth(const std::string& token) {
|
||||
(void)token;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Local wall clock so the ops TU does not drag supervisor TU definitions
|
||||
// into CLI links. Behaviorally identical to SystemClock.
|
||||
class WallClock : public molo::common::Clock {
|
||||
public:
|
||||
std::int64_t wall_ms() const override {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
std::int64_t steady_ms() const override {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
void sleep_ms(long ms) override {
|
||||
if (ms > 0) std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
OpsRoutes::OpsRoutes(const HealthRegistry& live_registry, const ReadinessCheck& readiness,
|
||||
const StartupGate& gate, const PrometheusExporter& exporter)
|
||||
: live_registry_(live_registry), readiness_(readiness), gate_(gate), exporter_(exporter) {}
|
||||
|
||||
OpsResponse OpsRoutes::healthz() const {
|
||||
OpsResponse out;
|
||||
bool failed = false;
|
||||
for (const auto& named : live_registry_.run_all()) {
|
||||
if (named.result.state != HealthState::Ok) failed = true;
|
||||
}
|
||||
if (failed) {
|
||||
out.status = 503;
|
||||
out.body = "{\"status\":\"failed\"}";
|
||||
} else {
|
||||
out.status = 200;
|
||||
out.body = "{\"status\":\"ok\"}";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
OpsResponse OpsRoutes::readyz() const {
|
||||
OpsResponse out;
|
||||
// ReadinessCheck::check is logically const; cast keeps the registry const.
|
||||
HealthResult r = const_cast<ReadinessCheck&>(readiness_).check();
|
||||
if (r.state == HealthState::Ok) {
|
||||
out.status = 200;
|
||||
out.body = "{\"status\":\"ok\"}";
|
||||
} else {
|
||||
out.status = 503;
|
||||
std::vector<std::string> items;
|
||||
for (const auto& name : const_cast<ReadinessCheck&>(readiness_).failed()) {
|
||||
items.push_back(molo::cli::JsonWriter::quote(name));
|
||||
}
|
||||
out.body = "{\"status\":\"degraded\",\"failed\":" +
|
||||
molo::cli::JsonWriter::array(items) + "}";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
OpsResponse OpsRoutes::startupz() const {
|
||||
OpsResponse out;
|
||||
if (gate_.ready()) {
|
||||
out.status = 200;
|
||||
out.body = "{\"status\":\"ok\"}";
|
||||
} else {
|
||||
out.status = 503;
|
||||
out.body = "{\"status\":\"starting\"}";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
OpsResponse OpsRoutes::metrics(bool accept_gzip) const {
|
||||
OpsResponse out;
|
||||
out.content_type = "text/plain; version=0.0.4";
|
||||
if (accept_gzip) {
|
||||
auto bytes = exporter_.render_gzip();
|
||||
out.body.assign(bytes.begin(), bytes.end());
|
||||
out.gzip = true;
|
||||
} else {
|
||||
out.body = exporter_.render();
|
||||
}
|
||||
out.status = 200;
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string OpsRoutes::debug_snapshot() const {
|
||||
WallClock clock;
|
||||
molo::debug::Snapshot snap(clock, molo::kMoloVersion);
|
||||
snap.add_section("health", healthz().body);
|
||||
snap.add_section("ready", readyz().body);
|
||||
snap.add_section("startup", startupz().body);
|
||||
snap.add_section("metrics_bytes",
|
||||
"{\"bytes\":" + std::to_string(exporter_.render().size()) + "}");
|
||||
return snap.capture();
|
||||
}
|
||||
|
||||
OpsResponse OpsRoutes::debug(const std::string& path, const std::string& token,
|
||||
bool admin_listener) const {
|
||||
OpsResponse r;
|
||||
r.status = 404;
|
||||
r.content_type = "text/plain";
|
||||
r.body = "not found";
|
||||
r.gzip = false;
|
||||
#ifdef MOLO_ENABLE_DEBUG
|
||||
if (!admin_listener) return r;
|
||||
molo::debug::Endpoint ep([this] { return this->debug_snapshot(); },
|
||||
[](const std::string& t) { return ops_admin_auth(t); });
|
||||
const auto out = ep.handle(path, token);
|
||||
r.status = out.status;
|
||||
r.content_type = out.content_type;
|
||||
r.body = out.body;
|
||||
#else
|
||||
(void)path;
|
||||
(void)token;
|
||||
(void)admin_listener;
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
// Pure probe handlers with no auth and no unbounded allocation.
|
||||
// The HTTP layer (01-runtime.md) mounts these at fixed paths.
|
||||
struct OpsResponse {
|
||||
int status = 200;
|
||||
std::string content_type = "application/json";
|
||||
std::string body;
|
||||
bool gzip = false;
|
||||
};
|
||||
|
||||
class HealthRegistry;
|
||||
class StartupGate;
|
||||
class PrometheusExporter;
|
||||
class ReadinessCheck;
|
||||
|
||||
class OpsRoutes {
|
||||
public:
|
||||
OpsRoutes(const HealthRegistry& live_registry, const ReadinessCheck& readiness,
|
||||
const StartupGate& gate, const PrometheusExporter& exporter);
|
||||
|
||||
OpsResponse healthz() const;
|
||||
OpsResponse readyz() const;
|
||||
OpsResponse startupz() const;
|
||||
OpsResponse metrics(bool accept_gzip) const;
|
||||
// Admin-only debug surface. Returns 404 unless the process was built with
|
||||
// MOLO_ENABLE_DEBUG and the caller is the admin listener (see the .cpp).
|
||||
OpsResponse debug(const std::string& path, const std::string& token,
|
||||
bool admin_listener) const;
|
||||
|
||||
private:
|
||||
std::string debug_snapshot() const;
|
||||
const HealthRegistry& live_registry_;
|
||||
const ReadinessCheck& readiness_;
|
||||
const StartupGate& gate_;
|
||||
const PrometheusExporter& exporter_;
|
||||
};
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "ops/otlp_bridge.hpp"
|
||||
|
||||
#include "caps.hpp"
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
bool OtlpBridge::configure(const OtlpOptions& opts, std::string& error) {
|
||||
if (opts.endpoint.empty()) {
|
||||
error = "otlp endpoint is empty";
|
||||
return false;
|
||||
}
|
||||
if (opts.interval_sec < caps::kMetricsExportFloorSec) {
|
||||
error = "otlp interval below 5s floor";
|
||||
return false;
|
||||
}
|
||||
opts_ = opts;
|
||||
configured_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OtlpBridge::export_spans(unsigned long span_count, std::string& error) const {
|
||||
(void)span_count;
|
||||
if (!configured_) {
|
||||
error = "otlp bridge not configured";
|
||||
return false;
|
||||
}
|
||||
// STUB: transport owned by 02-observability.md, not yet wired.
|
||||
error = "otlp transport unavailable (02-observability.md owns it)";
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
struct OtlpOptions {
|
||||
std::string endpoint;
|
||||
int interval_sec = 5;
|
||||
};
|
||||
|
||||
// Thin bridge only. Transport is owned by 02-observability.md; this stub
|
||||
// records configuration and reports unavailable until wired.
|
||||
class OtlpBridge {
|
||||
public:
|
||||
bool configure(const OtlpOptions& opts, std::string& error);
|
||||
bool export_spans(unsigned long span_count, std::string& error) const;
|
||||
bool configured() const { return configured_; }
|
||||
const OtlpOptions& options() const { return opts_; }
|
||||
|
||||
private:
|
||||
OtlpOptions opts_;
|
||||
bool configured_ = false;
|
||||
};
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "ops/prometheus_exporter.hpp"
|
||||
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
#include "caps.hpp"
|
||||
#include "embed/gzip_stored.hpp"
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
std::string PrometheusExporter::escape_label(const std::string& text) {
|
||||
std::string out;
|
||||
for (char c : text) {
|
||||
if (c == '"') {
|
||||
out += "\\\"";
|
||||
} else if (c == '\\') {
|
||||
out += "\\\\";
|
||||
} else if (c == '\n') {
|
||||
out += "\\n";
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool PrometheusExporter::add(MetricSample sample) {
|
||||
if (sample.labels.size() > caps::kMetricsMaxLabels) {
|
||||
++dropped_;
|
||||
return false;
|
||||
}
|
||||
if (samples_.size() >= caps::kMetricsMaxSeries) {
|
||||
++dropped_;
|
||||
return false;
|
||||
}
|
||||
samples_.push_back(std::move(sample));
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string PrometheusExporter::render() const {
|
||||
std::ostringstream out;
|
||||
std::string last_name;
|
||||
for (const auto& sample : samples_) {
|
||||
if (sample.name != last_name) {
|
||||
if (!sample.help.empty()) out << "# HELP " << sample.name << " " << sample.help << "\n";
|
||||
out << "# TYPE " << sample.name << " " << metric_type_name(sample.type) << "\n";
|
||||
last_name = sample.name;
|
||||
}
|
||||
out << sample.name;
|
||||
if (!sample.labels.empty()) {
|
||||
out << "{";
|
||||
bool first = true;
|
||||
for (const auto& [key, value] : sample.labels) {
|
||||
if (!first) out << ",";
|
||||
first = false;
|
||||
out << key << "=\"" << escape_label(value) << "\"";
|
||||
}
|
||||
out << "}";
|
||||
}
|
||||
out << " " << sample.value << "\n";
|
||||
}
|
||||
out << "# HELP molo_metrics_dropped_total samples dropped by series caps\n";
|
||||
out << "# TYPE molo_metrics_dropped_total counter\n";
|
||||
out << "molo_metrics_dropped_total " << dropped_ << "\n";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::vector<unsigned char> PrometheusExporter::render_gzip() const {
|
||||
std::string text = render();
|
||||
return molo::embed::GzipStored::compress_string(text);
|
||||
}
|
||||
|
||||
std::string PrometheusExporter::normalize_route(const std::string& path) {
|
||||
std::ostringstream out;
|
||||
std::string segment;
|
||||
auto flush = [&]() {
|
||||
if (segment.empty()) return;
|
||||
bool numeric = true;
|
||||
bool uuid_like = segment.size() == 36;
|
||||
for (char c : segment) {
|
||||
if (!std::isdigit(static_cast<unsigned char>(c))) numeric = false;
|
||||
if (!std::isxdigit(static_cast<unsigned char>(c)) && c != '-') uuid_like = false;
|
||||
}
|
||||
if (numeric || uuid_like) {
|
||||
out << "/{id}";
|
||||
} else {
|
||||
out << "/" << segment;
|
||||
}
|
||||
segment.clear();
|
||||
};
|
||||
for (char c : path) {
|
||||
if (c == '/') {
|
||||
flush();
|
||||
} else {
|
||||
segment += c;
|
||||
}
|
||||
}
|
||||
flush();
|
||||
std::string result = out.str();
|
||||
return result.empty() ? "/" : result;
|
||||
}
|
||||
|
||||
} // namespace molo::ops
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::ops {
|
||||
|
||||
enum class MetricType { Counter, Gauge, Histogram, Summary, Untyped };
|
||||
|
||||
inline std::string metric_type_name(MetricType type) {
|
||||
switch (type) {
|
||||
case MetricType::Counter:
|
||||
return "counter";
|
||||
case MetricType::Gauge:
|
||||
return "gauge";
|
||||
case MetricType::Histogram:
|
||||
return "histogram";
|
||||
case MetricType::Summary:
|
||||
return "summary";
|
||||
case MetricType::Untyped:
|
||||
return "untyped";
|
||||
}
|
||||
return "untyped";
|
||||
}
|
||||
|
||||
struct MetricSample {
|
||||
std::string name;
|
||||
std::map<std::string, std::string> labels;
|
||||
double value = 0.0;
|
||||
MetricType type = MetricType::Untyped;
|
||||
std::string help;
|
||||
};
|
||||
|
||||
// Prometheus text 0.0.4 exporter with static cardinality caps.
|
||||
// Series cap 10000; overflow counts molo_metrics_dropped_total.
|
||||
class PrometheusExporter {
|
||||
public:
|
||||
bool add(MetricSample sample);
|
||||
std::string render() const;
|
||||
std::vector<unsigned char> render_gzip() const;
|
||||
std::size_t series() const { return samples_.size(); }
|
||||
unsigned long dropped() const { return dropped_; }
|
||||
|
||||
// Route templates only: numeric/uuid segments become {id}.
|
||||
static std::string normalize_route(const std::string& path);
|
||||
|
||||
private:
|
||||
std::vector<MetricSample> samples_;
|
||||
unsigned long dropped_ = 0;
|
||||
|
||||
static std::string escape_label(const std::string& text);
|
||||
};
|
||||
|
||||
} // namespace molo::ops
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user