feat(admin): guided tuning editor that sends only the changes
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 28s
Foundation / server-image (push) Successful in 4m48s
Foundation / image (push) Successful in 5m37s

DEV's ingress refuses request bodies over about 1 KB, so adopting a whole
revamp file (5 KB) failed with a 413 the portal misreported as the API's 64 KiB
limit. POST /api/tuning?base=baseline|REV now takes only changed `key = value`
lines, applies them to the baseline file or the newest revision (409 stale_base
for an older one), keeping its comments, then checks and stores as before.

The Tuning page becomes a guided editor: sections, a field per setting with its
unit, plain reading and comment, a changes list with Check and Apply (or Adopt
as is), and loading a past revision or the baseline as changes. The full-text
editor stays as Text mode and names a proxy refusal plainly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-28 12:09:54 -04:00
co-authored by Claude Opus 5.5
parent 8dedde454d
commit 23ea24080e
7 changed files with 464 additions and 71 deletions
+99
View File
@@ -618,6 +618,70 @@ std::expected<CheckedTuning, OpsResponse> check_tuning(std::string_view text) {
return std::unexpected{error(503, "unavailable")};
return CheckedTuning{rules, *digest};
}
std::string_view trim_blank(std::string_view text) {
while (!text.empty() && (text.front() == ' ' || text.front() == '\t'))
text.remove_prefix(1);
while (!text.empty() && (text.back() == ' ' || text.back() == '\t' || text.back() == '\r'))
text.remove_suffix(1);
return text;
}
// The guided editor's changes applied to a base text: each `key = value` line (blank and `#`
// lines ignored) replaces the value on that key's line in `base`, keeping its spacing and
// comment, or is appended when the base has no such line. The parser checks the result.
std::expected<std::string, OpsResponse> apply_tuning_changes(std::string_view base,
std::string_view changes) {
const auto key_char = [](char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '.';
};
const auto value_char = [](char ch) {
return (ch >= '0' && ch <= '9') || ch == '_' || ch == '-';
};
std::string text(base);
std::vector<std::string_view> seen;
std::size_t line_number = 0;
for (std::size_t from = 0; from < changes.size();) {
const auto end = std::min(changes.find('\n', from), changes.size());
const auto line = trim_blank(changes.substr(from, end - from));
from = end + 1;
++line_number;
if (line.empty() || line.front() == '#')
continue;
const auto equals = line.find('=');
const auto key = trim_blank(line.substr(0, equals));
const auto value = equals == std::string_view::npos ? std::string_view{}
: trim_blank(line.substr(equals + 1));
if (key.empty() || value.empty() || !std::ranges::all_of(key, key_char) ||
!std::ranges::all_of(value, value_char) || std::ranges::contains(seen, key))
return std::unexpected{json(400, "{\"error\":\"invalid_change\",\"line\":" +
std::to_string(line_number) + "}\n")};
seen.push_back(key);
bool replaced = false;
for (std::size_t at = 0; at < text.size() && !replaced;) {
const auto stop = std::min(text.find('\n', at), text.size());
const std::string_view current{text.data() + at, stop - at};
const auto eq = current.find('=');
const auto name = trim_blank(current.substr(0, eq));
if (eq != std::string_view::npos && !name.starts_with('#') && name == key) {
auto first = eq + 1;
while (first < current.size() && (current[first] == ' ' || current[first] == '\t'))
++first;
auto last = std::min(current.find('#', first), current.size());
while (last > first && (current[last - 1] == ' ' || current[last - 1] == '\t' ||
current[last - 1] == '\r'))
--last;
text.replace(at + first, last - first, value);
replaced = true;
}
at = stop + 1;
}
if (!replaced) {
if (!text.empty() && text.back() != '\n')
text.push_back('\n');
text.append(key).append(" = ").append(value).push_back('\n');
}
}
return text;
}
} // namespace
OpsResponse AdminApi::commands(std::uint64_t region) {
@@ -845,6 +909,41 @@ OpsResponse AdminApi::tuning_text(std::uint64_t revision) {
OpsResponse AdminApi::set_tuning(std::string_view query_text, std::string_view text,
const broker::AccessTokenClaims& admin) {
// With `base`, the body holds only changed lines (the guided editor), applied to the
// baseline file or to the newest revision, so the whole file never travels (a proxy in
// front may bound request bodies far below max_api_body_bytes). A base revision that is no
// longer the newest is refused rather than silently overwriting a later revision.
std::string merged;
if (const auto base = query_value(query_text, "base")) {
std::string base_text;
if (*base == "baseline") {
if (config_.baseline_tuning.empty())
return error(400, "no_baseline");
base_text = config_.baseline_tuning;
} else {
const auto wanted = number(*base);
if (!wanted || *wanted == 0)
return error(400, "bad_base");
const auto newest =
query("SELECT revision, text FROM evolution.admin_tuning_text_v1($1, 0)",
{std::to_string(config_.world)});
if (!newest)
return error(503, "unavailable");
if (newest->empty() || at(newest->front(), 0) != std::to_string(*wanted))
return json(409, "{\"error\":\"stale_base\",\"newest\":" +
(newest->empty() ? std::string{"null"}
: text_value(at(newest->front(), 0))) +
"}\n");
base_text = at(newest->front(), 1).value_or("");
}
auto applied = apply_tuning_changes(base_text, text);
if (!applied)
return applied.error();
merged = std::move(*applied);
text = merged;
} else if (query_text.find("base=") != std::string_view::npos) {
return error(400, "bad_base");
}
const auto checked = check_tuning(text);
if (!checked)
return checked.error();