From 3d79ec95288fea8a4c7d40d62ef7db421c183590 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 27 Nov 2026 01:37:36 +0100 Subject: [PATCH] fix(goals): fall back to raw goal only when no LLM provider is configured (#585) --- agent/goals.go | 7 +++- agent/goals_contract_test.go | 13 +++++++ server/goals.go | 18 +++++++++ server/goals_test.go | 75 ++++++++++++++++++++++++++++++++++++ server/task_llm.go | 2 +- 5 files changed, 113 insertions(+), 2 deletions(-) diff --git a/agent/goals.go b/agent/goals.go index 0f17e4de..004f6ac8 100644 --- a/agent/goals.go +++ b/agent/goals.go @@ -83,6 +83,11 @@ var ErrGoalDecompositionContract = errors.New("goal decomposition did not produc // with an empty task goal. var ErrGoalDecompositionEmptyInput = errors.New("goal decomposition requires a non-empty task goal") +// ErrGoalDecompositionNoProvider reports that goal decomposition cannot run +// because no LLM provider is configured. Callers distinguish this from a +// structured tool-call contract failure (ErrGoalDecompositionContract). +var ErrGoalDecompositionNoProvider = errors.New("goal decomposition failed: no LLM provider configured") + // goalsStrictRetryPrompt is sent once when the initial goal decomposition run // finishes without persisting any non-empty goal via a structured set_goals // tool call (e.g. the model emitted set_goals in plain text instead). @@ -122,7 +127,7 @@ func DecomposeGoals(ctx context.Context, prov llm.Provider, dataDir, goalText, d // per-reply output cap (0 = send none). func DecomposeGoalsWithProvider(ctx context.Context, prov llm.Provider, dataDir, goalText, desc string, as *db.AssetStore, ts *db.ExplorationStore, taskID int64, nonStreaming bool, maxTokens int, emit func(db.Activity)) ([]GoalSpec, error) { if prov == nil { - return nil, errors.New("goal decomposition failed: no LLM provider configured") + return nil, ErrGoalDecompositionNoProvider } if ts == nil { return nil, errors.New("goal decomposition failed: exploration store is not initialized") diff --git a/agent/goals_contract_test.go b/agent/goals_contract_test.go index b9139d82..5bb691d9 100644 --- a/agent/goals_contract_test.go +++ b/agent/goals_contract_test.go @@ -264,3 +264,16 @@ func TestPlannerRefusesEmptyContextAndOverviewOmitsEmptyFields(t *testing.T) { t.Fatalf("expected 1 LLM call when context is valid, got %d", prov.calls()) } } +func TestDecomposeGoalsNoProviderReturnsSentinel(t *testing.T) { + ts := guardTaskStore(t) + got, err := DecomposeGoals(t.Context(), nil, t.TempDir(), "Audit target", "desc", nil, ts, 0, nil) + if !errors.Is(err, ErrGoalDecompositionNoProvider) { + t.Fatalf("expected ErrGoalDecompositionNoProvider, got goals=%+v err=%v", got, err) + } + if errors.Is(err, ErrGoalDecompositionContract) { + t.Fatalf("no-provider error must not match ErrGoalDecompositionContract: %v", err) + } + if len(got) != 0 { + t.Fatalf("expected no goals returned, got %+v", got) + } +} diff --git a/server/goals.go b/server/goals.go index adb0b4dd..9aa51f53 100644 --- a/server/goals.go +++ b/server/goals.go @@ -2,6 +2,7 @@ package server import ( "context" + "errors" "fmt" "log" "sort" @@ -406,6 +407,23 @@ func (s *Server) createGoals(ctx context.Context, t *Task, emit func(db.Activity decomposed, err := agent.DecomposeGoalsWithProvider(ctx, goalRuntime, s.m.dir, t.Goal, t.Description, as, t.Store, taskID, goalRuntime.nonStreaming(), goalRuntime.maxTokens(), emit) s.engine.EndLLMCall(t.ID) if err != nil { + if errors.Is(err, agent.ErrGoalDecompositionNoProvider) { + // No LLM provider is configured yet: preserve the pre-existing fallback + // so the task gets a single raw goal node linked to its origin fact and + // stays alive until an LLM is configured. Structured contract failures + // from an actual model remain strict and fail the task. + g := strings.TrimSpace(t.Goal) + log.Printf("[goals] task %s: no LLM provider configured, seeding raw goal as a single goal", t.ID) + origin, _ := t.Store.OriginFactID() + id, addErr := t.Store.AddNode(db.KindGoal, map[string]any{"text": g}, 0, "open", "system", nil) + if addErr != nil { + return nil, fmt.Errorf("seed raw goal: %w", addErr) + } + if origin > 0 && id > 0 { + _ = t.Store.Link(origin, db.RelSpawns, id) // goal descends from the task root (origin fact) + } + return []goalSpec{{Text: g}}, nil + } return nil, err } var specs []goalSpec diff --git a/server/goals_test.go b/server/goals_test.go index 47363671..fe42e55f 100644 --- a/server/goals_test.go +++ b/server/goals_test.go @@ -130,3 +130,78 @@ func TestStartTaskEngineFailsTaskOnPlainTextDecomposer(t *testing.T) { t.Fatalf("expected error activity describing goal decomposition failure, got %+v", acts) } } + +// TestCreateGoalsNoProviderSeedsRawGoalAndKeepsTaskAlive verifies that when no +// LLM provider is configured, createGoals falls back to seeding the raw task +// goal as a single goal node linked from the origin fact, keeping the task +// alive instead of marking it failed. +func TestCreateGoalsNoProviderSeedsRawGoalAndKeepsTaskAlive(t *testing.T) { + skipWithoutDSN(t) + m, err := NewManager(t.TempDir(), "") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = m.Close() }) + if m.pg == nil { + db.SkipNoPG(t) + } + // Ensure no global LLM profile is configured in this test DB. + profiles, err := m.pg.ListProfiles() + if err != nil { + t.Fatal(err) + } + if len(profiles) > 0 { + t.Skip("test database already has configured LLM profiles") + } + + ctx, cancel := context.WithCancel(context.Background()) + s := New(ctx, m, "", m.dir, m.dir) + t.Cleanup(func() { stopTestServer(t, s, cancel) }) + + const rawGoal = "Probe http://127.0.0.1:9/v1 for unauthenticated exposure" + task, err := m.CreateTaskWithOptions("no-provider fallback", rawGoal, db.TaskCreateOptions{}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _, _ = m.DeleteTask(task.ID, DeleteTaskOptions{}) }) + + // Seed origin fact as launchTask does so the spawns link is exercised. + s.seed(task, rawGoal) + originID, err := task.Store.OriginFactID() + if err != nil || originID <= 0 { + t.Fatalf("expected origin fact seeded, got id=%d err=%v", originID, err) + } + + goals, err := s.createGoals(ctx, task, nil) + if err != nil { + t.Fatalf("createGoals without provider must fall back cleanly, got err=%v", err) + } + if len(goals) != 1 || goals[0].Text != rawGoal { + t.Fatalf("unexpected fallback goals: %+v", goals) + } + + nodes, err := task.Store.ListByKind(db.KindGoal, 10) + if err != nil { + t.Fatal(err) + } + if len(nodes) != 1 { + t.Fatalf("expected 1 persisted goal node, got %d", len(nodes)) + } + edges, err := task.Store.Edges(100) + if err != nil { + t.Fatal(err) + } + linked := false + for _, e := range edges { + if e.From == originID && e.Rel == db.RelSpawns && e.To == nodes[0].ID { + linked = true + break + } + } + if !linked { + t.Fatalf("fallback goal node #%d not linked from origin fact #%d via %q (edges=%+v)", nodes[0].ID, originID, db.RelSpawns, edges) + } + if st := task.lifecycleSnapshot().Status; st == "failed" { + t.Fatalf("task without provider must stay alive, got status=%q", st) + } +} diff --git a/server/task_llm.go b/server/task_llm.go index 4e3968cf..6753dcaf 100644 --- a/server/task_llm.go +++ b/server/task_llm.go @@ -248,7 +248,7 @@ func (r *taskLLMRuntime) currentFor(ctx context.Context) (taskLLMSelection, erro } prov, cfg, id, ok := r.s.globalProviderWithProfile() if !ok { - return sel, fmt.Errorf("task %s has no available fallback LLM provider", r.taskID) + return sel, fmt.Errorf("task %s has no available fallback LLM provider: %w", r.taskID, agent.ErrGoalDecompositionNoProvider) } sel.provider, sel.retry = prov, cfg.Retry sel.healthProfileID = healthIDFor(prov, id)