From 787b7fb1aed92c2b701724a7741576053b93cccb Mon Sep 17 00:00:00 2001 From: Claude Sonnet 5 Date: Fri, 3 Jul 2026 23:01:50 +0000 Subject: 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 Claude-Session: https://claude.ai/code/session_01V1moSNCJRcP6kykA4tyUSs --- internal/cli/cloudrunners_test.go | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 internal/cli/cloudrunners_test.go (limited to 'internal/cli/cloudrunners_test.go') diff --git a/internal/cli/cloudrunners_test.go b/internal/cli/cloudrunners_test.go new file mode 100644 index 0000000..7943347 --- /dev/null +++ b/internal/cli/cloudrunners_test.go @@ -0,0 +1,78 @@ +package cli + +import ( + "log/slog" + "os" + "testing" + + "github.com/thepeterstone/claudomator/internal/config" + "github.com/thepeterstone/claudomator/internal/executor" +) + +// TestRegisterCloudRunners_RegistersAllFiveConfiguredProviders confirms +// registerCloudRunners (used identically by both `serve` and `run`) wires up +// a NativeRunner for every cloud provider that has a non-empty API key — +// including groq/openrouter/openai, which docs/api-keys-setup.md documented +// as "works once configured" before this phase actually configured them. +func TestRegisterCloudRunners_RegistersAllFiveConfiguredProviders(t *testing.T) { + cfg := &config.Config{ + SandboxImage: "test-image:latest", + Providers: map[string]config.ProviderConfig{ + "anthropic": {APIKey: "ak", DefaultModel: "claude-sonnet-5"}, + "google": {APIKey: "gk", DefaultModel: "gemini-2.5-flash"}, + "groq": {APIKey: "grk", Endpoint: "https://api.groq.com/openai/v1", DefaultModel: "llama-3.3-70b-versatile"}, + "openrouter": {APIKey: "ork", Endpoint: "https://openrouter.ai/api/v1", DefaultModel: "meta-llama/llama-3.3-70b-instruct:free"}, + "openai": {APIKey: "oak", Endpoint: "https://api.openai.com/v1", DefaultModel: "gpt-4o-mini"}, + }, + } + runners := map[string]executor.Runner{} + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) + registerCloudRunners(runners, cfg, logger, t.TempDir()) + + for _, name := range []string{"anthropic", "google", "groq", "openrouter", "openai"} { + r, ok := runners[name] + if !ok { + t.Errorf("expected a %q runner to be registered", name) + continue + } + nr, ok := r.(*executor.NativeRunner) + if !ok { + t.Errorf("%q runner should be a *executor.NativeRunner", name) + continue + } + if nr.Provider == nil { + t.Errorf("%q runner has a nil Provider", name) + } + if nr.SandboxKind != "docker" { + t.Errorf("%q runner should use SandboxKind \"docker\", got %q", name, nr.SandboxKind) + } + } +} + +// TestRegisterCloudRunners_SkipsProvidersWithoutAPIKeyOrDisabled confirms +// providers are skipped when unconfigured (no api_key) or explicitly +// disabled via [runners], matching the anthropic/google-only behavior this +// generalizes. +func TestRegisterCloudRunners_SkipsProvidersWithoutAPIKeyOrDisabled(t *testing.T) { + f := false + cfg := &config.Config{ + Providers: map[string]config.ProviderConfig{ + "groq": {APIKey: "", DefaultModel: "x"}, // no key + "openrouter": {APIKey: "ork", DefaultModel: "x"}, + }, + Runners: config.RunnersConfig{OpenRouter: &f}, // explicitly disabled + } + runners := map[string]executor.Runner{} + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) + registerCloudRunners(runners, cfg, logger, t.TempDir()) + + if _, ok := runners["groq"]; ok { + t.Error("groq should not be registered without an api_key") + } + if _, ok := runners["openrouter"]; ok { + t.Error("openrouter should not be registered when explicitly disabled") + } + if _, ok := runners["openai"]; ok { + t.Error("openai should not be registered when absent from cfg.Providers") + } +} -- cgit v1.2.3