From 18651405072e9e4a3a5cadc8c777ff331109099e Mon Sep 17 00:00:00 2001 From: David Young Date: Wed, 25 Nov 2026 12:29:44 -0700 Subject: [PATCH] Phase 13, part 6: each team's Core MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On the server and multi-tenant editions a team's admin enters its Core's domain and account under Settings → Core & Hermes, instead of core.conf; Hermes connects from the team's side with an agent's key. Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 14 +++++- internal/web/changes.go | 10 ++--- internal/web/connections.go | 8 ++-- internal/web/core.go | 76 ++++++++++++++++++++++++++++++++ internal/web/crm.go | 8 ++-- internal/web/leadforms.go | 4 +- internal/web/listmonk.go | 4 +- internal/web/outreach.go | 2 +- internal/web/pages.go | 28 ++++++++---- internal/web/server.go | 1 + internal/web/teams_test.go | 22 +++++++++ internal/web/templates/core.html | 19 ++++++-- internal/web/tracking.go | 4 +- 13 files changed, 166 insertions(+), 34 deletions(-) create mode 100644 internal/web/core.go diff --git a/PLAN.md b/PLAN.md index 144630d..487b909 100644 --- a/PLAN.md +++ b/PLAN.md @@ -55,7 +55,7 @@ tools over MCP, approvals, limits and visibility, and the JSON API. Schema 39. **Phase 13 (multi-tenant) is under way** on branch `phase13`, with a -Postgres schema per team (decided 2026-11-26); parts 1–5 are built. +Postgres schema per team (decided 2026-11-26); parts 1–6 are built. **Phase 12 (signing in with the Core, OIDC) is set aside for now** (2026-11-26): before it starts, the user will share the Federated Core's @@ -2883,6 +2883,18 @@ stand. - Left for Phase 14, which sets them by plan: limits on outgoing fetches and storage. +**Part 6 built 2026-11-26** on `phase13`. +- On the server and multi-tenant editions, each team's admin enters its + Core's domain, and the Core account Hermes works for, under Settings → + Core & Hermes (a team setting, `core:team`); the local edition still + reads CoreDesktop's `core.conf`. Everything that used the Core (EspoCRM, + Listmonk's and Plausible's addresses, the MCP hub, the checks) reads the + team's. Saving or clearing it is recorded as a connection. +- On those editions the page tells how Hermes connects: an agent with the + CRM scope (Settings → Agents), its key, and `https://…/mcp`; the socket + and the hand setup are the local edition's. The platform never holds a + Core's admin credentials. + 1. **Teams everywhere.** - Every table that holds a company's data gets `team_id`, and its unique keys include it. The settings table's key becomes diff --git a/internal/web/changes.go b/internal/web/changes.go index 44a031a..a82a7f2 100644 --- a/internal/web/changes.go +++ b/internal/web/changes.go @@ -26,8 +26,8 @@ func (s *Server) giteaURL() string { if _, err := s.DB.GetJSON(giteaURLKey, &u); err == nil && u != "" { return u } - if s.Core != nil && s.Core.Domain != "" { - return "https://gitea." + s.Core.Domain + if s.core() != nil && s.core().Domain != "" { + return "https://gitea." + s.core().Domain } return "" } @@ -53,14 +53,14 @@ func (s *Server) applier(site seoSite) *seo.Applier { // commitAuthor is who Supermarket's commits are by: the Core's user, via // Supermarket. func (s *Server) commitAuthor() string { - if s.Core != nil && s.Core.Email != "" { - name := s.Core.Name + if s.core() != nil && s.core().Email != "" { + name := s.core().Name if name == "" { name = "Supermarket" } else { name += " (via Supermarket)" } - return name + " <" + s.Core.Email + ">" + return name + " <" + s.core().Email + ">" } return "Supermarket " } diff --git a/internal/web/connections.go b/internal/web/connections.go index 7c34f92..4c3b8ac 100644 --- a/internal/web/connections.go +++ b/internal/web/connections.go @@ -248,12 +248,12 @@ func (s *Server) connectionsPage(w http.ResponseWriter, r *http.Request) { } rows = append(rows, row) } - // The Core and Hermes, on the local edition. + // The Core and Hermes. var core []connRow - if !s.signsIn() { + { row := connRow{service: service{ID: "core", Name: "Federated Core", For: "The Core's services and the CRM, through Hermes", Link: "/settings/core"}, State: "missing"} - if s.Core != nil { - row.State, row.Detail = "connected", coreDomain(s.Core) + if s.core() != nil { + row.State, row.Detail = "connected", coreDomain(s.core()) } core = append(core, row) hermes := connRow{service: service{ID: "hermes", Name: "Hermes bridge", For: "The CRM, through the Core's MCP hub", Link: "/settings/core"}, State: "missing"} diff --git a/internal/web/core.go b/internal/web/core.go new file mode 100644 index 0000000..4352e6c --- /dev/null +++ b/internal/web/core.go @@ -0,0 +1,76 @@ +package web + +import ( + "errors" + "net/http" + "regexp" + "strings" + + "supermarket/internal/core" + "supermarket/internal/users" +) + +// The Federated Core a team uses. The local edition reads CoreDesktop's +// core.conf; on the server and multi-tenant editions each team's admin +// enters its Core's domain under Settings → Core & Hermes. Hermes and the +// CRM bridge then connect from the team's side with an agent's key: the +// platform never holds a Core's admin credentials. + +const coreKey = "core:team" + +type coreSetting struct { + Domain string `json:"domain"` + Email string `json:"email"` // the Core account Hermes acts for (its MCP hub user) +} + +// core is the team's Core, or nil. +func (s *Server) core() *core.Core { + if !s.signsIn() { + return s.Core + } + var c coreSetting + if ok, err := s.DB.GetJSON(coreKey, &c); err != nil || !ok || c.Domain == "" { + return nil + } + return &core.Core{Domain: c.Domain, Email: c.Email} +} + +var hostRe = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$`) + +// coreSave sets (or, empty, forgets) the team's Core. +func (s *Server) coreSave(w http.ResponseWriter, r *http.Request) { + if !s.signsIn() { + http.NotFound(w, r) + return + } + domain := strings.ToLower(strings.TrimSpace(r.PostFormValue("domain"))) + domain = strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(domain, "https://"), "http://"), "/") + email := strings.TrimSpace(r.PostFormValue("email")) + if domain == "" { + if err := s.DB.PutJSON(coreKey, coreSetting{}); err != nil { + s.serverError(w, err) + return + } + s.recordConnection(r, "core", false) + redirect(w, r, "/settings/core", "saved") + return + } + var problem error + if !hostRe.MatchString(domain) { + problem = errors.New("give the Core's domain, such as example.com") + } else if email != "" { + if _, err := users.NormalEmail(email); err != nil { + problem = errors.New("the Core account isn't an email address") + } + } + if problem != nil { + s.renderCore(w, r, sentence(problem.Error())+".") + return + } + if err := s.DB.PutJSON(coreKey, coreSetting{Domain: domain, Email: email}); err != nil { + s.serverError(w, err) + return + } + s.recordConnection(r, "core", true) + redirect(w, r, "/settings/core", "saved") +} diff --git a/internal/web/crm.go b/internal/web/crm.go index 97cc213..7a70ff4 100644 --- a/internal/web/crm.go +++ b/internal/web/crm.go @@ -77,7 +77,7 @@ func (s *Server) renderCRM(w http.ResponseWriter, r *http.Request, errMsg string "Job": hermesCRMJob, "JobID": s.crmJobID(), "Waiting": counts[crm.StatusPending] + counts[crm.StatusClaimed], - "Unreachable": s.Core != nil && unreachableError(actions) != "", + "Unreachable": s.core() != nil && unreachableError(actions) != "", }}) } @@ -127,11 +127,11 @@ func (s *Server) crmReach(w http.ResponseWriter, r *http.Request) { return } failure := unreachableError(actions) - if s.Core == nil || failure == "" { + if s.core() == nil || failure == "" { s.fragment(w, r, "crm", "reach", nil) return } - check := probe(r.Context(), core.Service{Name: "EspoCRM", URL: espoCRMURL(s.Core)}) + check := probe(r.Context(), core.Service{Name: "EspoCRM", URL: espoCRMURL(s.core())}) tried := "" for _, m := range connErr.FindAllStringSubmatch(failure, -1) { if m[2] != "" { @@ -143,7 +143,7 @@ func (s *Server) crmReach(w http.ResponseWriter, r *http.Request) { "Check": check, "Tried": tried, "Error": failure, - "HubHost": "mcphub." + s.Core.Domain, + "HubHost": "mcphub." + s.core().Domain, }) } diff --git a/internal/web/leadforms.go b/internal/web/leadforms.go index 301a9a1..60b3070 100644 --- a/internal/web/leadforms.go +++ b/internal/web/leadforms.go @@ -36,8 +36,8 @@ func (s *Server) renderForms(w http.ResponseWriter, r *http.Request, errMsg, fla all, cur := s.currentSite(r) data := map[string]any{"Sites": all, "Current": cur, "Gitea": s.gitea() != nil, "Payload": sites.LeadPayloadFields, "Goal": tracking.LeadGoal, "Plausible": s.plausible() != nil} - if s.Core != nil && s.Core.Domain != "" { - data["CRM"] = "https://espocrm." + s.Core.Domain + if s.core() != nil && s.core().Domain != "" { + data["CRM"] = "https://espocrm." + s.core().Domain } if cur != nil && cur.Repo != "" { var capture string diff --git a/internal/web/listmonk.go b/internal/web/listmonk.go index 2309f0c..86506b3 100644 --- a/internal/web/listmonk.go +++ b/internal/web/listmonk.go @@ -36,8 +36,8 @@ var listmonkToken = secretField{"listmonk", secrets.ListmonkToken, "API token", func (s *Server) listmonkConfig() listmonkSettings { var set listmonkSettings _, _ = s.DB.GetJSON(listmonkKey, &set) - if set.URL == "" && s.Core != nil { - set.URL = "https://listmonk." + s.Core.Domain + if set.URL == "" && s.core() != nil { + set.URL = "https://listmonk." + s.core().Domain } return set } diff --git a/internal/web/outreach.go b/internal/web/outreach.go index 9c45c3b..7c4b82b 100644 --- a/internal/web/outreach.go +++ b/internal/web/outreach.go @@ -24,7 +24,7 @@ var mailPassword = secretField{"mail", secrets.MailPassword, "Password", // outreachSettings are the saved email settings, or the defaults. func (s *Server) outreachSettings() (outreach.Settings, bool) { p, _ := profile.Load(s.DB) - set, ok, err := outreach.Load(s.DB, s.Core, p) + set, ok, err := outreach.Load(s.DB, s.core(), p) if err != nil { s.Log.Error("loading the email settings", "err", err) } diff --git a/internal/web/pages.go b/internal/web/pages.go index be321ba..d7ca611 100644 --- a/internal/web/pages.go +++ b/internal/web/pages.go @@ -143,8 +143,8 @@ func (s *Server) dashboard(w http.ResponseWriter, r *http.Request) { Detail: inventorySummary(counts)}, {Label: "Social accounts", Done: len(s.postingAccounts()) > 0, Link: "/settings/accounts", Detail: s.connectionSummary(conns)}, - {Label: "Federated Core", Done: s.Core != nil, Link: "/settings/core", - Detail: ifElse(s.Core != nil, coreDomain(s.Core), "No Core found; run `configure core` in CoreDesktop")}, + {Label: "Federated Core", Done: s.core() != nil, Link: "/settings/core", + Detail: ifElse(s.core() != nil, coreDomain(s.core()), "No Core found; run `configure core` in CoreDesktop")}, {Label: "Hermes bridge", Done: last != nil, Link: "/settings/core", Detail: ifElse(last != nil, "Last check-in "+ago(atOf(last)), "Hermes hasn't checked in yet")}, } @@ -248,16 +248,22 @@ type coreData struct { Prompt string HermesConfig string HermesEnv string + Server bool // the server editions: the Core is entered here, Hermes uses an agent key + Problem string } func (s *Server) corePage(w http.ResponseWriter, r *http.Request) { + s.renderCore(w, r, "") +} + +func (s *Server) renderCore(w http.ResponseWriter, r *http.Request, problem string) { last, err := bridge.Last(s.DB) if err != nil { s.serverError(w, err) return } d := coreData{ - Core: s.Core, + Core: s.core(), MCPURL: "http://127.0.0.1:" + publicPort(s.PublicURL) + "/mcp", MCPToken: s.MCPToken, MCPSocket: s.MCPSocket, @@ -265,9 +271,9 @@ func (s *Server) corePage(w http.ResponseWriter, r *http.Request) { Prompt: bridge.CheckinPrompt, } hub := "https://mcphub./sse?as_user=" - if s.Core != nil { - d.User = s.Core.User() - hub = s.Core.MCPHubURL() + if s.core() != nil { + d.User = s.core().User() + hub = s.core().MCPHubURL() } d.HubURL = hub d.HermesConfig = `mcp_servers: @@ -280,7 +286,11 @@ func (s *Server) corePage(w http.ResponseWriter, r *http.Request) { command: socat args: ["STDIO", "UNIX-CONNECT:` + d.MCPSocket + `"]` d.HermesEnv = `MCPHUB_AUTH=` - s.render(w, r, "core", page{Title: "Core & Hermes", Nav: "settings/core", Data: d}) + d.Server, d.Problem = s.signsIn(), problem + if d.Server { + d.MCPURL = s.mcpURL() + } + s.render(w, r, "core", page{Title: "Core & Hermes", Nav: "settings/core", Error: problem, Data: d}) } func publicPort(u string) string { @@ -334,11 +344,11 @@ func probe(ctx context.Context, svc core.Service) serviceCheck { // coreChecks asks each Core service whether it answers. It's an htmx // fragment, loaded after the page so a slow service doesn't hold it up. func (s *Server) coreChecks(w http.ResponseWriter, r *http.Request) { - if s.Core == nil { + if s.core() == nil { s.fragment(w, r, "core", "checks", nil) return } - services := s.Core.Services() + services := s.core().Services() checks := make([]serviceCheck, len(services)) var wg sync.WaitGroup for i, svc := range services { diff --git a/internal/web/server.go b/internal/web/server.go index 78a88c1..7d1bf0d 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -550,6 +550,7 @@ func (s *Server) Handler() (http.Handler, error) { mux.HandleFunc("POST /settings/posting/{id}/remove", (*Server).postingRemove) mux.HandleFunc("POST /settings/account/posting/{id}/remove", (*Server).postingRemove) mux.HandleFunc("GET /settings/core", (*Server).corePage) + mux.HandleFunc("POST /settings/core", (*Server).coreSave) mux.HandleFunc("GET /settings/core/checks", (*Server).coreChecks) // Where these pages used to be, for bookmarks. diff --git a/internal/web/teams_test.go b/internal/web/teams_test.go index b78e67b..fe6f575 100644 --- a/internal/web/teams_test.go +++ b/internal/web/teams_test.go @@ -358,3 +358,25 @@ func TestJobsRunPerTeam(t *testing.T) { t.Errorf("files %s", ts.Files.String()) } } + +func TestCorePerTeam(t *testing.T) { + s, h := newMultiEdition(t) + _, _, ac := newTeam(t, s, "Acme", "olu@acme.example") + _, _, bc := newTeam(t, s, "Bolt", "bea@bolt.example") + if rec := as(h, ac, http.MethodPost, "/settings/core", url.Values{"domain": {"not a domain"}}); !strings.Contains(rec.Body.String(), "the Core's domain") { + t.Errorf("bad domain: %s", rec.Body.String()) + } + if rec := as(h, ac, http.MethodPost, "/settings/core", url.Values{"domain": {"https://core.acme.example/"}, "email": {"olu@core.acme.example"}}); rec.Code != http.StatusSeeOther { + t.Fatalf("save: %d", rec.Code) + } + body := as(h, ac, http.MethodGet, "/settings/core", nil).Body.String() + if !strings.Contains(body, `value="core.acme.example"`) || !strings.Contains(body, "Settings → Agents") || strings.Contains(body, "mcp.sock") { + t.Errorf("core page: %s", body) + } + if body := as(h, bc, http.MethodGet, "/settings/core", nil).Body.String(); strings.Contains(body, "acme") { + t.Error("another team's Core") + } + if body := as(h, ac, http.MethodGet, "/settings/connections", nil).Body.String(); !strings.Contains(body, "core.acme.example") { + t.Error("not on Connections") + } +} diff --git a/internal/web/templates/core.html b/internal/web/templates/core.html index 9a83e65..db92cc7 100644 --- a/internal/web/templates/core.html +++ b/internal/web/templates/core.html @@ -4,18 +4,29 @@

Federated Core

+ {{if .Server}} +
+ + +
Empty: no Core. Supermarket never holds the Core's admin credentials.
+
+ {{end}} {{if .Core}}

{{.Core.Domain}} · {{.Core.Email}}{{if .Core.Name}} · {{.Core.Name}}{{end}}

-

From CoreDesktop's configure core. To use another Core, change it there and run update.

+ {{if not .Server}}

From CoreDesktop's configure core. To use another Core, change it there and run update.

{{end}}

Checking the Core's services…

- {{else}} + {{else if not .Server}}

No Core found. Run configure core in CoreDesktop, then update. Without a Core, everything works except EspoCRM, Plausible and publishing through Gitea.

{{end}}

Hermes bridge

+ {{if .Server}} +

Hermes, on your side, is the only thing that talks to your Core's MCP hub. Make it an agent under Settings → Agents with CRM through the Core (read and act), then give Hermes the agent's key and {{.MCPURL}} (streamable HTTP, Authorization: Bearer <key>).

+ {{else}}

Hermes, CoreDesktop's agent, is the only thing that talks to your Core's MCP hub. It reaches Supermarket through the socket {{.MCPSocket}}; other agents, such as Claude Code, use {{.MCPURL}} with the MCP token.

+ {{end}} {{with .Last}}

Hermes last checked in {{ago .At}}.{{if .Note}} It said: “{{.Note}}”{{end}}

{{if .MCPHubTools}} @@ -33,14 +44,14 @@

Then reload this page.

-
+{{if not .Server}}
Set up Hermes by hand

install.sh does this for you. To do it yourself, add this to ~/.hermes/config.yaml:

{{.HermesConfig}}

and this to ~/.hermes/.env, so no password is in config.yaml:

{{.HermesEnv}}

Hermes runs in CoreDesktop's sandbox, so also let it reach the hub: add mcphub.{{if .Core}}{{.Core.Domain}}{{else}}<your Core's domain>{{end}} to network.allowedDomains in ~/.config/coredesktop/sandbox/hermes.json. Then restart Hermes' gateway: systemctl --user restart hermes-gateway.

-
Show the MCP token
{{.MCPToken}}

Also in mcp-token in the data folder.

+
Show the MCP token
{{.MCPToken}}

Also in mcp-token in the data folder.

{{end}}
{{end}}{{template "settings-end"}}{{end}} diff --git a/internal/web/tracking.go b/internal/web/tracking.go index aed4959..f4f1e2f 100644 --- a/internal/web/tracking.go +++ b/internal/web/tracking.go @@ -37,8 +37,8 @@ func (s *Server) plausibleURL() string { if _, err := s.DB.GetJSON(plausibleURLKey, &u); err == nil && u != "" { return u } - if s.Core != nil && s.Core.Domain != "" { - return "https://plausible." + s.Core.Domain + if s.core() != nil && s.core().Domain != "" { + return "https://plausible." + s.core().Domain } return "https://plausible.io" }