feat(webui): inline editing for per-repo allow_concurrent/max_retries/review_enabled

Adds PATCH /api/config/repos/{owner}/{name}, a dedicated seam distinct
from the generic dotted-key PATCH /api/config path: a repository is
one element of []config.Repo, not a flat key. Traced the mechanism
before adding any new plumbing -- overlay.Nest/ApplyOverlayValues
already handle "repos" as a single whole-slice key with no dot, so
the fix is a webui handler plus a bootstrap seam that reads the
daemon's live, untrimmed Repos (never webui.RepoView, which omits
Preflight/TestGlob), mutates the one matched field via the pure
applyRepoFieldUpdate helper, and republishes through the existing
UpdateConfig(ctx, {"repos": repos}) path unchanged.

config-row.js's row() gains an opts.save override so an edit can be
redirected to a different persistence call. The repositories table
renders an autosaving checkbox for the two booleans and a
save-on-blur/Enter input for max_retries, via api.configRepoUpdate.

Closes archie-core-b6ew.6, and with it archie-core-b6ew.
This commit is contained in:
sam committed 2026-09-02 22:35:03 +10:00
1 parent a444253209
commit a4fbd8d6ef
12 files changed
+427 -20

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
package webui
import (
"encoding/json"
"errors"
"net/http"
)
// RepoEditableFields are the per-repository fields the dashboard may change
// through handleConfigRepoUpdate. Deliberately narrow: these three are
// booleans/small ints with no cross-field validation, unlike Gate or
// Protect, which stay file-edited (archie-core-b6ew.4/.6).
var RepoEditableFields = map[string]bool{
"allow_concurrent": true,
"max_retries": true,
"review_enabled": true,
}
// handleConfigRepoUpdate applies one field change to one repository,
// identified by owner/name in the path. It exists because the repository
// list is a []config.Repo, not a flat dotted key -- PATCH /api/config's
// generic path (overlay.Nest + configuration.ApplyOverlayValues) has no way
// to address "element N of this slice." UpdateRepoField's implementation
// (wired by the composition root) reads the daemon's own live config.Repo
// values (never the trimmed, wire-safe RepoView), changes just the one
// field, and republishes the full repository list through the same
// validate-persist-publish path PATCH /api/config already uses -- see
// bootstrap.go's installUpdateConfigHandler for why that matters: the wire
// view omits fields (Preflight, TestGlob) that a naive round-trip through
// ConfigView would silently drop.
func (s *Server) handleConfigRepoUpdate(w http.ResponseWriter, r *http.Request) {
if s.UpdateRepoField == nil {
http.Error(w, ErrConfigUpdateUnavailable.Error(), http.StatusServiceUnavailable)
return
}
owner := r.PathValue("owner")
name := r.PathValue("name")
var body struct {
Field string `json:"field"`
Value any `json:"value"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
return
}
if !RepoEditableFields[body.Field] {
http.Error(w, "field "+body.Field+" is not editable from the dashboard", http.StatusBadRequest)
return
}
if err := s.UpdateRepoField(r.Context(), owner, name, body.Field, body.Value); err != nil {
switch {
case errors.Is(err, ErrConfigUpdateUnavailable):
http.Error(w, err.Error(), http.StatusServiceUnavailable)
case errors.Is(err, ErrConfigUpdateInvalid):
http.Error(w, err.Error(), http.StatusBadRequest)
default:
http.Error(w, "repo update failed: "+err.Error(), http.StatusInternalServerError)
}
return
}
writeJSON(w, map[string]any{"ok": true})
}
+88
View File
@@ -0,0 +1,88 @@
package webui
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHandleConfigRepoUpdateAppliesViaSeam(t *testing.T) {
srv := newTestServer(t)
type call struct {
owner, name, field string
value any
}
var got call
srv.UpdateRepoField = func(_ context.Context, owner, name, field string, value any) error {
got = call{owner, name, field, value}
return nil
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
strings.NewReader(`{"field": "allow_concurrent", "value": true}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
if got != (call{"acme", "widget", "allow_concurrent", true}) {
t.Errorf("seam called with %+v", got)
}
}
// TestHandleConfigRepoUpdateRejectsUneditableFields proves the handler
// itself refuses a field outside RepoEditableFields before ever calling the
// seam -- Gate, Protect, and every other repo field stay file-edited.
func TestHandleConfigRepoUpdateRejectsUneditableFields(t *testing.T) {
srv := newTestServer(t)
called := false
srv.UpdateRepoField = func(context.Context, string, string, string, any) error {
called = true
return nil
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
strings.NewReader(`{"field": "gate", "value": [["rm", "-rf", "/"]]}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", w.Code)
}
if called {
t.Error("UpdateRepoField was called for a field outside RepoEditableFields")
}
}
func TestHandleConfigRepoUpdateUnavailableMapsTo503(t *testing.T) {
srv := newTestServer(t)
srv.UpdateRepoField = nil
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
strings.NewReader(`{"field": "allow_concurrent", "value": true}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503", w.Code)
}
}
func TestHandleConfigRepoUpdateSeamErrorMapsTo400(t *testing.T) {
srv := newTestServer(t)
srv.UpdateRepoField = func(context.Context, string, string, string, any) error {
return ErrConfigUpdateInvalid
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
strings.NewReader(`{"field": "max_retries", "value": 5}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", w.Code)
}
}
+9
View File
@@ -92,6 +92,14 @@ type Server struct {
// directly.
ResetConfig func(context.Context, string) error
// UpdateRepoField changes one editable field (RepoEditableFields) on
// the repository identified by owner/name, republishing the full
// repository list through the same path as UpdateConfig. Optional:
// when nil, PATCH /api/config/repos/{owner}/{name} answers 503. See
// handleConfigRepoUpdate for why this needs its own seam rather than
// going through UpdateConfig's dotted-key path directly.
UpdateRepoField func(ctx context.Context, owner, name, field string, value any) error
// Channels reports actual adapter lifecycle, independently of configuration
// presence. Nil preserves the configuration-only fallback for tests and
// minimal embedding.
@@ -276,6 +284,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/version", s.handleVersion)
mux.HandleFunc("PATCH /api/config", s.handleConfigUpdate)
mux.HandleFunc("POST /api/config/reset", s.handleConfigReset)
mux.HandleFunc("PATCH /api/config/repos/{owner}/{name}", s.handleConfigRepoUpdate)
mux.HandleFunc("GET /api/logs", s.handleLogs)
mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs)
mux.HandleFunc("GET /api/logs/stream", s.handleLogStream)