Files
multica/server/internal/handler/qoder_catalog.go
T
0fe2ebf02e MUL-8045: feat(runtimes): Qoder Cloud Agent as a managed cloud runtime (#9402)
* feat(runtimes): add Qoder Cloud Agent managed remote execution

* fix(qoder): satisfy migration and localization CI contracts

* fix(qoder): bound execution and scope managed credentials

* fix(qoder): avoid upstream migration number collisions

* fix(qoder): gate issue creation and correlate tool results

* fix(qoder): synchronize guarded issue lifecycle transitions

* test(agent): publish Hermes holder identity atomically

* feat(agents): runtime-first create flow and cloud runtimes section for Qoder Cloud Agent

- Three-step create-agent flow (runtime -> configure -> done) with local,
  remote and cloud sections; Qoder connect form inline in the runtime step
- Runtimes page regrouped into remote and cloud sections with Qoder card
- Generalize Qoder storage to cloud_agent_connection (migrations 586/587)
- Locales for en, zh-Hans, ja, ko, fr; desktop and web routes for done step
- Fix status-picker fixture owner_id and diagnostics route coverage after main merge

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(runtimes): unify runtimes page into machines and cloud services

- One Add menu replaces Add remote runtime, the duplicated Add a computer
  and the web-only Cloud Runtime header button
- Group by machines / cloud services instead of local / remote / cloud, so
  web and desktop share one structure; this device is pinned with a badge
- Cloud services use the machine row anatomy in a single ListGrid table;
  unconnected Qoder is a row with Connect, coming-soon providers are rows
- Empty groups show the next step inline; search and online/offline filter
- Rename computer to machine across runtimes copy in all locales

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* test(runtimes): cover unified runtimes page; pin status and action tracks

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* refactor(runtimes): trim redundant descriptive copy on runtimes page

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(runtimes): keep row actions visible at narrow widths

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* refactor(runtimes): align Qoder add-menu item with other cloud services

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(runtimes): close the Qoder dialog after a first-time connection

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(agents): pick the runtime from one grouped list

The runtime step showed four kinds of cards, a comparison panel and
per-section descriptions. It is now one list in the runtimes-page layout:
machines (this one open, others folded to a row) and cloud services. An
unconnected Qoder opens the runtimes-page connect dialog in place and is
selected once it comes online.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(agents): show owners as 'you' and bare version numbers in the runtime list

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(qoder): disconnect action and dedicated Qoder runtime page

Disconnect removes the encrypted connection and the managed runtime, unbinding
its agents after the admin confirms them. The Qoder row now opens its own page
(status, agents, usage, configuration, pause/resume, disconnect) instead of the
machine layout; generic runtime delete and visibility refuse a managed runtime.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(agents): land on the done page after creating from step 2 on desktop

Desktop's location is the tab URL, which moves to /agents/new/done/<id>
while the configure page is still mounted. Its missing ?runtime= tripped
the skipped-step-1 redirect and replaced the tab back to /agents/new, so
Create looked like it did nothing. Only redirect on the manual route.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(agents): show only settings a Qoder agent can run with

The Qoder bridge fails every task whose agent carries a model, thinking or
speed override, env, custom args, custom skills, MCP config or connected
apps, and it runs issues only. The agent detail page still offered all of
those editors, so one save could break every later run.

For a qoder_cloud agent the page now keeps Instructions under Capabilities,
General / Access / QCA Agent under Settings, drops the model rows and the
header model chip, shows the QCA agent in the summary, and locks the
runtime. Other agents no longer see the Qoder runtime in their picker,
since moving onto it needs a QCA agent the server would otherwise reject.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(agents): hide conversation starters for Qoder agents

Starters only appear in a chat, and the Qoder bridge runs issues only.
The create flow already leaves them out for this runtime.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(runtimes): show the Qoder connection as cards with per-section editing

The Qoder runtime page rendered the whole connection as one always-editable
form. It now summarizes the connection, repositories and limits as read-only
cards; each section opens its own dialog and saves only that section.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(qoder): reuse a follow-up session only with the same repository and credential

Resolve repository credentials before deciding to continue a prior session,
and stamp each session with a fingerprint of its repositories, branches and
tokens. A follow-up continues only when the fingerprint still matches, so a
changed repository, branch or rotated/revoked token never rides into an old
session.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(qoder): keep a paused connection paused when its settings are saved

Saving repositories, limits or the connection no longer re-enables a paused
runtime. Resume goes through a new POST /qoder/resume.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(agents): drop settings a Qoder agent cannot run when creating one

A draft started on another runtime could carry hidden skills, model
overrides and starters into a Qoder agent that then failed every task. The
create request now drops them for Qoder, and the API rejects skills and
execution overrides on Qoder agents at save time.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* chore(db): regenerate sqlc models for cloud_agent_connection

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(runtimes): never list the Qoder runtime under Machines

When the server could not run Qoder (no MULTICA_VCS_SECRET_KEY /
MULTICA_QODER_STATE_DIR), the managed Qoder runtime fell back into the
Machines group as if it were a computer.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* refactor(qoder): drop MULTICA_QODER_STATE_DIR

The managed bridge keeps its run journal encrypted in cloud_agent_connection
and a database advisory lock already elects one supervisor, so the state
directory only held a redundant file lock and acted as a feature switch.
Qoder is now available whenever MULTICA_VCS_SECRET_KEY is set. The
standalone qoder-runtime command still uses its on-disk state_dir.

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(qoder): keep the branch in the session fingerprint after a journal restore

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(agents): validate Qoder agents' effective settings on every write

Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>

---------

Co-authored-by: 安辰 <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: multica-agent <[email protected]>
2026-10-22 01:33:38 +08:00

254 lines
7.7 KiB
Go

package handler
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5"
"github.com/multica-ai/multica/server/internal/qoderruntime"
db "github.com/multica-ai/multica/server/pkg/db/generated"
)
const qoderAPIURL = "https://api.qoder.com/api/v1/cloud"
// Managed connections may only reach Qoder's public regional APIs, preventing SSRF.
var qoderAPIURLs = map[string]bool{
qoderAPIURL: true,
"https://api.qoder.com.cn/api/v1/cloud": true,
}
func qoderBaseURL(raw string) (string, bool) {
base := strings.TrimRight(strings.TrimSpace(raw), "/")
if base == "" {
return qoderAPIURL, true
}
return base, qoderAPIURLs[base]
}
type qoderCatalogItem struct {
ID string `json:"id"`
Name string `json:"name"`
}
// Only names and identifiers leave the server. Provider configurations may contain secrets.
func fetchQoderCatalog(ctx context.Context, baseURL, token, kind string) ([]qoderCatalogItem, error) {
client := &http.Client{Timeout: 15 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
items := []qoderCatalogItem{}
cursor := ""
seen := map[string]bool{}
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
for page := 0; page < 10; page++ {
query := url.Values{"limit": {"100"}}
if cursor != "" {
query.Set("after_id", cursor)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/"+kind+"?"+query.Encode(), nil)
if err != nil {
return nil, fmt.Errorf("invalid QCA catalog request")
}
req.Header.Set("Authorization", "Bearer "+token)
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("QCA connection failed")
}
if res.StatusCode != http.StatusOK {
res.Body.Close()
return nil, fmt.Errorf("QCA catalog returned HTTP %d", res.StatusCode)
}
var result struct {
Data []struct {
ID string `json:"id"`
Name string `json:"name"`
ArchivedAt *string `json:"archived_at"`
} `json:"data"`
HasMore bool `json:"has_more"`
LastID string `json:"last_id"`
}
raw, err := io.ReadAll(io.LimitReader(res.Body, (2<<20)+1))
res.Body.Close()
if err != nil || len(raw) > 2<<20 || json.Unmarshal(raw, &result) != nil || result.Data == nil {
return nil, fmt.Errorf("invalid QCA catalog response")
}
for _, item := range result.Data {
prefix := "env_"
if kind == "agents" {
prefix = "agent_"
}
if !strings.HasPrefix(item.ID, prefix) {
return nil, fmt.Errorf("invalid QCA catalog identity")
}
if item.ArchivedAt != nil || seen[item.ID] {
continue
}
seen[item.ID] = true
name := strings.TrimSpace(item.Name)
if name == "" {
name = item.ID
}
items = append(items, qoderCatalogItem{ID: item.ID, Name: name})
}
if !result.HasMore {
return items, nil
}
if result.LastID == "" || result.LastID == cursor || len(result.Data) == 0 {
return nil, fmt.Errorf("invalid QCA pagination")
}
cursor = result.LastID
}
return nil, fmt.Errorf("QCA catalog exceeds pagination limit")
}
func (h *Handler) ListQoderAgents(w http.ResponseWriter, r *http.Request) {
ws, ok := parseUUIDOrBadRequest(w, chi.URLParam(r, "id"), "workspace_id")
if !ok {
return
}
if h.QoderRateLimiter != nil && !h.QoderRateLimiter.Allow(r.Context(), "qoder-catalog:"+uuidToString(ws)) {
w.Header().Set("Retry-After", "60")
writeError(w, http.StatusTooManyRequests, "QCA catalog request limit reached")
return
}
cfg, _, _, _, err := h.readQoderSettings(r.Context(), uuidToString(ws))
if errors.Is(err, pgx.ErrNoRows) {
writeError(w, 409, "configure a QCA connection first")
return
}
if err != nil {
writeError(w, 500, "cannot read QCA configuration")
return
}
base, ok := qoderBaseURL(cfg.BaseURL)
if !ok {
writeError(w, 409, "unsupported QCA API URL")
return
}
items, err := fetchQoderCatalog(r.Context(), base, cfg.QoderToken, "agents")
if err != nil {
writeError(w, 502, err.Error())
return
}
writeJSON(w, 200, map[string]any{"items": items})
}
func (h *Handler) ListQoderEnvironments(w http.ResponseWriter, r *http.Request) {
ws, ok := parseUUIDOrBadRequest(w, chi.URLParam(r, "id"), "workspace_id")
if !ok {
return
}
if h.QoderRateLimiter != nil && !h.QoderRateLimiter.Allow(r.Context(), "qoder-catalog:"+uuidToString(ws)) {
w.Header().Set("Retry-After", "60")
writeError(w, http.StatusTooManyRequests, "QCA catalog request limit reached")
return
}
var input struct {
Token string `json:"qoder_token"`
BaseURL string `json:"base_url"`
}
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 16<<10))
dec.DisallowUnknownFields()
if dec.Decode(&input) != nil {
writeError(w, 400, "invalid QCA token request")
return
}
var extra any
if dec.Decode(&extra) != io.EOF {
writeError(w, 400, "request must contain one JSON object")
return
}
cfg, _, _, _, err := h.readQoderSettings(r.Context(), uuidToString(ws))
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
writeError(w, 500, "cannot read QCA configuration")
return
}
requested := input.BaseURL
if strings.TrimSpace(requested) == "" {
requested = cfg.BaseURL
}
base, ok := qoderBaseURL(requested)
if !ok {
writeError(w, 400, "unsupported QCA API URL")
return
}
token := strings.TrimSpace(input.Token)
if token == "" {
token = cfg.QoderToken
}
if token == "" || strings.ContainsAny(token, "\r\n\t ") {
writeError(w, 400, "QCA token is required")
return
}
items, err := fetchQoderCatalog(r.Context(), base, token, "environments")
if err != nil {
writeError(w, 502, err.Error())
return
}
writeJSON(w, 200, map[string]any{"items": items})
}
// qoderOverrides lists per-agent execution settings the Qoder bridge refuses
// at run time (see qoderruntime.prepare); saving them would create an agent
// that fails every task.
type qoderOverrides struct {
skills, customEnv, customArgs int
mcpConfig json.RawMessage
model, thinkingLevel, serviceTier string
}
func (o qoderOverrides) validate() error {
mcp := strings.TrimSpace(string(o.mcpConfig))
if o.skills > 0 || o.customEnv > 0 || o.customArgs > 0 || (mcp != "" && mcp != "null" && mcp != "{}") || o.model != "" || o.thinkingLevel != "" || o.serviceTier != "" {
return fmt.Errorf("Qoder agents take skills, tools, credentials and model from the Qoder Agent; remove them from this agent")
}
return nil
}
// storedQoderOverrides reads an agent's saved execution settings. Skills live
// in their own table and are counted by the caller when they matter.
func storedQoderOverrides(agent db.Agent) qoderOverrides {
var args []string
_ = json.Unmarshal(agent.CustomArgs, &args)
return qoderOverrides{
customEnv: len(unmarshalCustomEnv(agent)),
customArgs: len(args),
mcpConfig: agent.McpConfig,
model: agent.Model.String,
thinkingLevel: agent.ThinkingLevel.String,
serviceTier: agent.ServiceTier.String,
}
}
// rejectQoderOverrides writes a 400 and reports true when the agent runs on
// Qoder and o would leave it with settings the bridge refuses at run time.
func (h *Handler) rejectQoderOverrides(w http.ResponseWriter, r *http.Request, agent db.Agent, o qoderOverrides) bool {
provider, ok := h.resolveAgentProvider(r, agent.WorkspaceID, agent.RuntimeID)
if !ok || provider != "qoder_cloud" {
return false
}
if err := o.validate(); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return true
}
return false
}
func validateQoderAgentConfig(raw []byte) error {
if _, err := qoderruntime.ParseAgentConfig(raw); err != nil {
if strings.HasPrefix(err.Error(), "default repository") {
return err
}
return fmt.Errorf("select a QCA Agent in the agent runtime configuration")
}
return nil
}