Files
aevolution/include/evo/server/admin/admin_api.hpp
T
dingoandClaude Opus 5.5 b1cc3b121b
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 4m44s
Foundation / image (push) Successful in 5m33s
feat(admin): live admin commands in trace 5 (schema 63)
The admin portal can now spawn wildlife, add bubbles, add to or release the
spent pool, remove a cell and grant a cell's reserve on a running rules-13
region (ADR-0182 section 10). No new owner format or region opening.

- Trace 5: trace 4 plus 64-byte command records counted in the tick header's
  last word; a batch without commands stays trace 4 (older builds replay it).
  Bundle 15 admits trace 5.
- CellularLives::admin_candidate applies a tick's commands after its births,
  each on a copy kept only when it applies and closes: imports through the
  boundary ledger, removals into the spent pool with a new `removed` corpse
  cause, spawned roots via the ADR-0180 seed (now a shared helper) funded by
  an exact import. Refused commands change nothing.
- Schema 63: the immutable admin_commands queue (spawn identities from the
  life allocator), pending/history reads, and binding of every recorded
  command exactly once inside both cellular publishers; the SQL trace parsers
  read trace 5 through its trace-4 normal form.
- The region polls pending commands every 2 s and appends up to 16 per tick;
  the admin API and portal gain region actions, per-cell Remove/Grant and the
  command history.
- Fixes an existing test that did not advance its head after a tick commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:14:04 -04:00

105 lines
5.6 KiB
C++

#pragma once
#include "evo/persistence/postgres.hpp"
#include "evo/server/broker/access_token.hpp"
#include "evo/server/ops_http.hpp"
#include <chrono>
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
// ADR-0182 read-only admin API and portal host. One world; every /api route needs a Keycloak
// access token carrying the admin realm role. The portal's static files and its OIDC settings
// are public; they carry no secret.
//
// GET /healthz ok
// GET / and the portal's files the portal (index.html, app.js, style.css)
// GET /config.json issuer, client id and world for the portal's sign-in
// GET /api/me the signed-in admin's subject and email
// GET /api/accounts every account with session, lives, deaths and resets
// GET /api/accounts/<uuid> one account and its life requests
// GET /api/regions the world's regions, leases and heads
// GET /api/regions/<n>/view the region's live view (render_cellular_admin_view)
// GET /api/regions/<n>/session-window the current session window and its last changes
// POST /api/regions/<n>/session-window?minutes=M[&reason=R]
// opens or extends the window for M (1..240) minutes
// from now, or closes it (M = 0), as the signed-in admin
// (ADR-0087 amendment). No request body.
// GET /api/regions/<n>/commands the region's last admin commands and where they applied
// POST /api/regions/<n>/commands?kind=K[&count=N][&target=ID][&energy=PJ][&carbon=FG]
// [&nitrogen=FG][&phosphorus=FG][&other=FG][&reason=R]
// queues a command (remove_cell, grant_reserve,
// add_bubbles, add_pool, release_pool, spawn_wildlife)
namespace evo::server::admin {
struct AdminAsset {
std::string path; // "/index.html", ...; "/" serves "/index.html".
std::string content_type, body;
};
struct AdminConfig {
std::uint64_t world{};
std::vector<std::uint64_t> view_regions; // Regions whose live view can be fetched.
std::string portal_config_json; // Served at /config.json.
std::string content_security_policy; // Sent with every portal file.
std::chrono::milliseconds db_timeout{3000};
std::chrono::milliseconds view_cache{750}; // A fetched view answers repeats this long.
};
using AdminConnect = std::function<std::unique_ptr<persistence::Postgres>()>;
// The region's /admin/view body, or nullopt when it cannot be reached or is not ready.
using AdminRegionView = std::function<std::optional<std::string>(std::uint64_t region)>;
class AdminApi {
public:
AdminApi(AdminConfig config, broker::TokenVerifier verifier, AdminConnect connect,
AdminRegionView view, std::vector<AdminAsset> assets);
// `now_seconds` is wall time (token expiry); `now` paces the view cache.
[[nodiscard]] OpsResponse handle(const ApiRequest& request, std::uint64_t now_seconds,
std::chrono::steady_clock::time_point now);
private:
[[nodiscard]] OpsResponse api(std::string_view method, std::string_view path,
std::string_view query, const broker::AccessTokenClaims& admin,
std::chrono::steady_clock::time_point now);
[[nodiscard]] OpsResponse accounts(std::optional<std::string_view> account);
[[nodiscard]] OpsResponse regions();
[[nodiscard]] OpsResponse view(std::uint64_t region, std::chrono::steady_clock::time_point now);
[[nodiscard]] OpsResponse window(std::uint64_t region);
[[nodiscard]] OpsResponse commands(std::uint64_t region);
[[nodiscard]] OpsResponse enqueue(std::uint64_t region, std::string_view query,
const broker::AccessTokenClaims& admin);
[[nodiscard]] OpsResponse set_window(std::uint64_t region, std::string_view query,
const broker::AccessTokenClaims& admin);
[[nodiscard]] std::optional<persistence::PgRows> query(const std::string& sql,
std::vector<std::string> parameters);
AdminConfig config_;
broker::TokenVerifier verifier_;
AdminConnect connect_;
AdminRegionView view_;
std::vector<AdminAsset> assets_;
std::unique_ptr<persistence::Postgres> db_;
struct CachedView {
std::chrono::steady_clock::time_point at;
std::string body;
};
std::map<std::uint64_t, CachedView> views_;
};
// Bounded blocking HTTP/1.1 GET of `path` from "host:port" (a name is resolved): the body of
// a 200 answer of at most `limit` bytes, or nullopt on any error, other status or timeout.
[[nodiscard]] std::optional<std::string> http_get(std::string_view address, std::string_view path,
std::chrono::milliseconds timeout,
std::size_t limit);
// JSON string literal (quotes included) with control characters escaped.
[[nodiscard]] std::string json_string(std::string_view text);
// The decoded value of `name` in a URL query ("a=1&b=x%20y"), or nullopt when absent or
// malformed (bad escape, a decoded control character, over `limit` bytes).
[[nodiscard]] std::optional<std::string> query_value(std::string_view query, std::string_view name,
std::size_t limit = 200);
} // namespace evo::server::admin