Files
goclaw/internal/http/providers_codex_pool_activity.go
Kai (Tam Nhu) Tran abb10976f7 feat(codex-pool,create_image): collapse primary_first + route pools through create_image chain (#1006)
* refactor(codex-pool): remove redundant primary_first strategy

* test(tools): update tool schema fixtures to pointer form

* feat(create_image): route Codex pools through chain with member failover

Codex pool chain entries now iterate pool members per the pool's own
strategy (round_robin or priority_order) and fail over internally before
the outer chain advances to the next entry.

- ChatGPTOAuthRouter.GenerateImage implements NativeImageProvider:
  iterates orderedProviders, tries each member, advances round-robin state
  only on success, aggregates errors on pool exhaustion.
- media_provider_chain.wrapPoolProvider wraps a *CodexProvider in a router
  when RoutingDefaults has extras or a non-primary-first strategy. Solo
  Codex (no extras) stays unwrapped.
- Router exposes ProviderType() so chain telemetry records "chatgpt_oauth".

Closes #1008

* feat(ui/builtin-tools): pool badge on create_image chain entries

Chain entry card shows a read-only "Pool · <strategy>" badge when the
entry's provider is a Codex pool base. Strategy label translates in
en/vi/zh. No new toggle or selector — pool config lives on the provider
itself.

#1008

* refactor(providers): move Codex test helpers to providertest subpackage

Addresses code-review High finding: NewTestCodexProviderFast and its
staticTestTokenSource were exported from a non-_test.go file, compiling
into the production binary. Relocating to internal/providers/providertest/
keeps the helper importable from other packages' tests without leaking
test-only symbols into production.

Also tightens wrapPoolProvider — pools with zero extra members no longer
get wrapped in a router (KISS: nothing to rotate between).

- Added CodexProvider.WithRetryConfig as a legitimate fluent option (the
  test helper now uses the public API).
- Dropped the internal-package test helper file.

#1008

* docs(pr-1006): add pool badge UI evidence

Force-UI style capture of the read-only "Pool · Round-robin" badge on
the create_image chain entry card when the selected provider carries
settings.codex_pool with extras. Captured against staging gateway.

* refactor(ui/builtin-tools): unify pool UX with Create Agent pattern

The create_image chain Provider dropdown was listing pool members alongside
pool owners, letting users accidentally bypass pool semantics by picking a
member directly. Matches the pattern already used by Create Agent: hide
pool members, show an inline "Pool" chip on owners.

- Filter pool members from the chain Provider dropdown via
  getChatGPTOAuthPoolOwnership.ownerByMember.
- Inline "Pool" chip on owner options, reusing the existing
  providers:list.poolBadge i18n key.
- Drop the separate card-level "Pool · <strategy>" badge — the dropdown chip
  alone conveys the information without duplication.
- Remove the now-unused isPoolProvider / poolStrategyOf helpers and the
  builtin.mediaChain.poolBadge* i18n keys we briefly introduced.

#1008

* docs(pr-1006): add pool-filtered dropdown screenshot

* fix(ui/builtin-tools): migrate stale pool-member chain entries on load

Red-team blocker: a chain saved before the pool-aware UI landed may
reference a pool member by name (e.g. openai-codex-2). After the
dropdown started hiding members, such an entry produced:
- empty Select trigger (no SelectItem matches the stored value)
- conflicting Row 1 label still showing the member name
- silent runtime misroute (bare solo call, no pool wrap)

parseInitialEntries now consults getChatGPTOAuthPoolOwnership and
rewrites any chain entry whose provider is a pool member to the owner's
name + id. The next save persists the migrated value. Safe no-op for
non-pool entries and for entries already pointing at an owner.

Also memoize enabledProviders in the parent form so the card's useMemo
boundaries actually hold (minor perf ding flagged by same review).

* docs(pr-1006): refresh HTML evidence to match filter-based UX

* fix(ui/agent-codex-pool): traffic policy reflects effective strategy under inherit

On the agent's OpenAI Account Pool page, when Agent routing mode is
Use Provider Defaults, the Traffic Policy buttons painted the draft's
placeholder strategy ("priority_order") as selected — contradicting the
top-of-page chip which already correctly shows the provider's effective
strategy. The removal of primary_first in this PR unmasked the latent
bug: the placeholder used to be a deprecated value that didn't match any
live button, so nothing appeared selected.

Derive selectedStrategy from defaultRouting when mode === "inherit":
the button highlight now mirrors what actually runs. Buttons remain
disabled in inherit mode (unchanged), but the displayed selection no
longer misleads the user.

* docs(pr-1006): add screenshot of inherit-mode Traffic Policy fix

* fix(permissions): remove duplicate MethodSessionsCompact entry

The writeExact slice listed MethodSessionsCompact twice. slices.Contains
still returned correct results so runtime behavior is unchanged, but the
duplicate entry was dead code.

Spotted during review of PR #1006.

---------

Co-authored-by: viettranx <edu@200lab.io>
2026-04-24 00:16:14 +07:00

178 lines
5.6 KiB
Go

package http
import (
"context"
"log/slog"
"net/http"
"slices"
"strconv"
"github.com/google/uuid"
"github.com/nextlevelbuilder/goclaw/internal/i18n"
"github.com/nextlevelbuilder/goclaw/internal/providers"
"github.com/nextlevelbuilder/goclaw/internal/store"
)
// providerCodexPoolAgentCount tracks per-agent request counts for provider-scoped activity.
type providerCodexPoolAgentCount struct {
AgentID uuid.UUID `json:"agent_id"`
AgentKey string `json:"agent_key,omitempty"`
RequestCount int `json:"request_count"`
}
// handleProviderCodexPoolActivity returns pool activity aggregated across all agents
// that use this provider's Codex pool. Only valid for chatgpt_oauth pool owners.
func (h *ProvidersHandler) handleProviderCodexPoolActivity(w http.ResponseWriter, r *http.Request) {
locale := extractLocale(r)
if h.tracingStore == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
"error": i18n.T(locale, i18n.MsgInvalidRequest, "tracing store unavailable"),
})
return
}
id, err := uuid.Parse(r.PathValue("id"))
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": i18n.T(locale, i18n.MsgInvalidID, "provider")})
return
}
provider, err := h.store.GetProvider(r.Context(), id)
if err != nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": i18n.T(locale, i18n.MsgNotFound, "provider", id.String())})
return
}
if provider.ProviderType != store.ProviderChatGPTOAuth {
writeJSON(w, http.StatusOK, emptyProviderPoolActivityResponse())
return
}
// Resolve pool providers from provider settings (capped at 20 to bound query size)
const maxPoolCandidates = 20
settings := store.ParseChatGPTOAuthProviderSettings(provider.Settings)
poolCandidates := []string{provider.Name}
strategy := store.ChatGPTOAuthStrategyPriority
if settings != nil && settings.CodexPool != nil {
if settings.CodexPool.Strategy != "" {
strategy = settings.CodexPool.Strategy
}
for _, name := range settings.CodexPool.ExtraProviderNames {
if name != "" && !slices.Contains(poolCandidates, name) {
poolCandidates = append(poolCandidates, name)
if len(poolCandidates) >= maxPoolCandidates {
break
}
}
}
}
// Filter to registered Codex providers
poolProviders := registeredCodexPoolProviders(h.providerReg, provider.TenantID, poolCandidates)
if len(poolProviders) == 0 {
writeJSON(w, http.StatusOK, emptyProviderPoolActivityResponse())
return
}
limit := 18
if raw := r.URL.Query().Get("limit"); raw != "" {
if parsed, err := strconv.Atoi(raw); err == nil && parsed > 0 && parsed <= 50 {
limit = parsed
}
}
statsLimit := maxInt(limit, codexPoolRuntimeHealthSampleSize)
rawSpans, err := h.tracingStore.ListCodexPoolSpansByProviders(r.Context(), provider.TenantID, poolProviders, statsLimit)
if err != nil {
slog.Error("providers.codex_pool_activity", "provider", provider.Name, "error", err)
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": i18n.T(locale, i18n.MsgFailedToList, "pool activity")})
return
}
// Filter spans to those with routing evidence matching pool providers
spans := make([]store.CodexPoolSpan, 0, len(rawSpans))
agentRequestCounts := make(map[uuid.UUID]int)
for _, item := range rawSpans {
if evidence := providers.ExtractChatGPTOAuthRoutingEvidence(item.Metadata); evidence.HasData() {
if !providerInPool(poolProviders, evidence.SelectedProvider) && !providerInPool(poolProviders, evidence.ServingProvider) {
continue
}
} else if !providerInPool(poolProviders, item.Provider) {
continue
}
spans = append(spans, item.CodexPoolSpan)
agentRequestCounts[item.AgentID]++
}
providerCounts, recent := buildCodexPoolActivity(poolProviders, spans)
if len(recent) > limit {
recent = recent[:limit]
}
// Build top agents list sorted by request count
topAgents := buildTopAgents(r.Context(), agentRequestCounts, h.agents)
writeJSON(w, http.StatusOK, map[string]any{
"strategy": strategy,
"pool_providers": poolProviders,
"stats_sample_size": len(spans),
"provider_counts": providerCounts,
"recent_requests": recent,
"top_agents": topAgents,
})
}
func emptyProviderPoolActivityResponse() map[string]any {
return map[string]any{
"strategy": store.ChatGPTOAuthStrategyPriority,
"pool_providers": []string{},
"stats_sample_size": 0,
"provider_counts": []codexPoolProviderCount{},
"recent_requests": []codexPoolRecentRequest{},
"top_agents": []providerCodexPoolAgentCount{},
}
}
// buildTopAgents converts agent request counts into a sorted slice, resolving agent keys.
func buildTopAgents(ctx context.Context, counts map[uuid.UUID]int, agentStore store.AgentCRUDStore) []providerCodexPoolAgentCount {
if len(counts) == 0 {
return []providerCodexPoolAgentCount{}
}
agentIDs := make([]uuid.UUID, 0, len(counts))
for id := range counts {
agentIDs = append(agentIDs, id)
}
keyByID := make(map[uuid.UUID]string, len(agentIDs))
if agentStore != nil {
if agents, err := agentStore.GetByIDs(ctx, agentIDs); err != nil {
slog.Warn("providers.codex_pool_activity.resolve_agents", "error", err, "count", len(agentIDs))
} else {
for _, ag := range agents {
keyByID[ag.ID] = ag.AgentKey
}
}
}
result := make([]providerCodexPoolAgentCount, 0, len(counts))
for id, count := range counts {
result = append(result, providerCodexPoolAgentCount{
AgentID: id,
AgentKey: keyByID[id],
RequestCount: count,
})
}
slices.SortFunc(result, func(a, b providerCodexPoolAgentCount) int {
return b.RequestCount - a.RequestCount
})
if len(result) > 10 {
result = result[:10]
}
return result
}