summaryrefslogtreecommitdiff
path: root/internal/cli/cloudrunners.go
diff options
context:
space:
mode:
authorClaude Sonnet 5 <noreply@anthropic.com>2026-07-03 23:01:50 +0000
committerClaude Sonnet 5 <noreply@anthropic.com>2026-07-03 23:01:50 +0000
commit787b7fb1aed92c2b701724a7741576053b93cccb (patch)
tree372aac85a10093a20ce3152e2c11fcfe20bb3e9d /internal/cli/cloudrunners.go
parent1f203a7ac0efad15ec3fc0a4c5b335ad7073a52f (diff)
feat(role): add versioned role configs + escalation ladder + scheduler (Phase 5)
Two parts: Part A (fixes a gap from Phase 1): Groq/OpenRouter/OpenAI were documented in docs/api-keys-setup.md as usable once configured, but nothing actually constructed runners for them. internal/cli/cloudrunners.go consolidates anthropic/google/groq/openrouter/openai NativeRunner construction into one table-driven registerCloudRunners() helper, replacing the two hand-written per-provider blocks in serve.go/run.go. Groq/OpenRouter/OpenAI reuse openaicompat (no new adapter code) at SandboxKind: "docker". Part B: the token-husbanding harness's core routing mechanism. - internal/role: RoleConfig/Tier/Rung -- a role's system prompt and a multi-tier (provider, model) escalation ladder, versioned via config_json. - storage: new role_configs table (draft/active/retired, UNIQUE(role, version)) with transactional activate-retires-prior-active semantics; new executions.escalation_rung column. - task.AgentConfig.Role string -- purely additive; every existing task shape (Agent.Role == "") is unaffected, proven by TestPool_Execute_NonRoleTask_Unaffected plus the full pre-existing suite passing unchanged. - executor.Pool.execute(): role-typed tasks with no Agent.Type yet resolve tier 0 of their active ladder (round-robin across multi-candidate tiers, skipping rate-limited providers, falling back to soonest-clearing) before the existing pickAgent/Classifier path runs; SystemPrompt applies to Agent.SystemPromptAppend. Already-resolved role tasks (scheduler resubmits) get their escalation_rung re-derived read-only via findTierIndex. - internal/scheduler: polls role-typed FAILED tasks, retries at the same rung under MaxRetries or escalates to the next tier's first candidate when budget.Accountant.Allow() permits (emitting event.KindEscalated), else leaves the task FAILED with a final:true KindEscalated event. An in-memory per-execution-ID "handled" set keeps the poll loop convergent. Started by `serve` only, config knob [scheduler].poll_interval_seconds. - internal/api: POST/GET /api/roles/{role}/versions, POST /api/roles/{role}/activate -- unauthenticated, matching the existing projects/tasks REST endpoints' auth posture (only chatbot MCP, agent MCP, and WebSocket are api_token-gated in this codebase today). Documented as stored-but-not-yet-enforced (CLAUDE.md Design Debt, matching how task.Priority/RetryConfig are already documented): RoleConfig.Tools/ SandboxKind don't affect dispatch yet; DefaultBudgetUSD is read narrowly as the scheduler's escalation cost estimate, not enforced at initial dispatch; scheduler escalation always targets Candidates[0] (no round-robin, unlike initial-dispatch tier-0 resolution); the scheduler's dedupe is per-process and resets on restart (idempotent, harmless). go build/vet/test -race -count=1 all pass, 21 packages. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1moSNCJRcP6kykA4tyUSs
Diffstat (limited to 'internal/cli/cloudrunners.go')
-rw-r--r--internal/cli/cloudrunners.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/internal/cli/cloudrunners.go b/internal/cli/cloudrunners.go
new file mode 100644
index 0000000..55b3efb
--- /dev/null
+++ b/internal/cli/cloudrunners.go
@@ -0,0 +1,91 @@
+package cli
+
+import (
+ "log/slog"
+ "net/http"
+ "time"
+
+ "github.com/thepeterstone/claudomator/internal/config"
+ "github.com/thepeterstone/claudomator/internal/executor"
+ "github.com/thepeterstone/claudomator/internal/llm"
+ "github.com/thepeterstone/claudomator/internal/provider"
+ "github.com/thepeterstone/claudomator/internal/provider/anthropic"
+ "github.com/thepeterstone/claudomator/internal/provider/google"
+ "github.com/thepeterstone/claudomator/internal/provider/openaicompat"
+)
+
+// cloudProviderSpec describes how to construct a provider.Provider for one
+// cloud provider entry in cfg.Providers, and which RunnersConfig field gates
+// it. anthropic/google use their native wire-format adapters; groq/
+// openrouter/openai are OpenAI-wire-compatible and reuse the Phase 1
+// openaicompat adapter — no dedicated adapter package needed for those three
+// (see docs/api-keys-setup.md).
+type cloudProviderSpec struct {
+ name string
+ enabled func(config.RunnersConfig) bool
+ build func(pc config.ProviderConfig, timeout time.Duration) provider.Provider
+}
+
+func openaiCompatBuild(pc config.ProviderConfig, timeout time.Duration) provider.Provider {
+ return openaicompat.New(&llm.Client{
+ Endpoint: pc.Endpoint,
+ Model: pc.DefaultModel,
+ APIKey: pc.APIKey,
+ HTTPClient: &http.Client{Timeout: timeout},
+ })
+}
+
+var cloudProviderSpecs = []cloudProviderSpec{
+ {
+ name: "anthropic",
+ enabled: config.RunnersConfig.AnthropicEnabled,
+ build: func(pc config.ProviderConfig, timeout time.Duration) provider.Provider {
+ return anthropic.New(pc.APIKey, pc.Endpoint, timeout)
+ },
+ },
+ {
+ name: "google",
+ enabled: config.RunnersConfig.GoogleEnabled,
+ build: func(pc config.ProviderConfig, timeout time.Duration) provider.Provider {
+ return google.New(pc.APIKey, pc.Endpoint, timeout)
+ },
+ },
+ {name: "groq", enabled: config.RunnersConfig.GroqEnabled, build: openaiCompatBuild},
+ {name: "openrouter", enabled: config.RunnersConfig.OpenRouterEnabled, build: openaiCompatBuild},
+ {name: "openai", enabled: config.RunnersConfig.OpenAIEnabled, build: openaiCompatBuild},
+}
+
+// registerCloudRunners constructs and adds a NativeRunner (SandboxKind:
+// "docker", matching the original anthropic/google-only wiring — cloud
+// providers get real container isolation, not the weaker HostSandbox local
+// runners use) into runners for every cloud provider in cloudProviderSpecs
+// that has a non-empty cfg.Providers[name].APIKey and is enabled via its
+// RunnersConfig.<Name>Enabled() gate. Called identically from both `serve`
+// and `run` — the only difference between them (logDir) is a parameter, and
+// neither command does anything provider-specific beyond this.
+func registerCloudRunners(runners map[string]executor.Runner, cfg *config.Config, logger *slog.Logger, logDir string) {
+ for _, spec := range cloudProviderSpecs {
+ pc, ok := cfg.Providers[spec.name]
+ if !ok || pc.APIKey == "" || !spec.enabled(cfg.Runners) {
+ continue
+ }
+ timeout := time.Duration(0)
+ if pc.TimeoutSeconds > 0 {
+ timeout = time.Duration(pc.TimeoutSeconds) * time.Second
+ }
+ runners[spec.name] = &executor.NativeRunner{
+ Provider: spec.build(pc, timeout),
+ Logger: logger,
+ LogDir: logDir,
+ DefaultModel: pc.DefaultModel,
+ // Native cloud providers get real container isolation
+ // (sandbox.DockerSandbox) rather than HostSandbox's host-side
+ // path-prefix confinement — see NativeRunner.SandboxKind doc.
+ SandboxKind: "docker",
+ SandboxImage: cfg.SandboxImage,
+ }
+ if logger != nil {
+ logger.Info(spec.name+" runner registered", "default_model", pc.DefaultModel, "sandbox", "docker")
+ }
+ }
+}