Files
kandev/apps/backend/internal/mcp/server/server_test.go
T
João Salavisa 57adf1449c feat(review): native code-review agent with inline findings in the Changes panel (#1957)
* feat(review): persist and publish native code-review findings

Adds the backend core for reviewing a task's working diff before a PR
exists: task-scoped review runs and anchored, advisory findings that the
Changes/Review panel can render as inline comments.

- task_review_runs / task_review_findings schema, models, and repository,
  with supersede-on-re-review and a restart sweep that cancels in-flight
  runs rather than leaving them looking alive.
- ReviewService as the single write path, publishing bus events that the
  WS gateway forwards to the client.
- internal/review: reviewer-identity resolution (agent profile > code-review
  utility agent > user default, failing closed on passthrough-only profiles),
  change-set collection, prompt batching, response parsing, anchor
  extraction, and run orchestration.
- publish_review_findings_kandev MCP tool plus the task.review.* WS actions.
- Go djb2 that matches apps/web/lib/utils/hash.ts, asserted from shared
  fixtures on both sides so stale detection agrees across the wire.

Spec: docs/specs/native-code-review/spec.md
Plan: docs/plans/native-code-review/plan.md

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(review): add run_code_review workflow step and wire the review runner

Makes a review pass triggerable both ways and actually runnable end to end.

- run_code_review on_enter action across models, engine compile, and an
  orchestrator callback that logs rather than propagates failures, so a task
  never gets stuck in a step because no reviewer was configured.
- The step's optional agent_profile_id is what lets a step review with a
  different model than it implemented with; export/import swap it for a
  portable agent name/model/mode so it survives a cross-workspace round trip
  instead of carrying a dangling id.
- Concrete adapters for reviewer identity, changed-file collection, prompt
  rendering from the code-review utility agent's editable prompt, and
  inference (session-bound agentctl, falling back to the host instance so a
  review works on a task whose agent is not running).
- Boot sweep cancels review runs interrupted by a restart; the E2E reset
  clears review state before deleting the tasks that own it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(web): add review findings state, helpers, and live updates

Frontend plumbing for native code review, ahead of the UI that renders it.

- Review types, WS message map, and an API client that preserves the backend's
  machine-readable failure code so the surface can show an actionable message.
- A review store slice that merges findings by id (a re-publish refreshes an
  issue rather than listing it twice) and keeps a bounded run history.
- Findings helpers: composite file keys matching reviewFileKey, staleness via
  the same per-file diff hash that drives review marks, and best-effort
  re-anchoring from the stored anchor text so a moved finding follows the code
  it was written about instead of pointing at whatever now occupies its line.
- Hooks for backfill, resolve/dismiss/reopen with optimistic rollback, and
  sending a finding to the agent as follow-up context without changing its
  status — handing a finding to an agent is not the human accepting it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(web): render review findings inline in the Changes diff

Findings now appear where the code is, in the same annotation surface as
anchored review comments, with the run control that produces them.

- ReviewFindingCard: severity chip, category, Markdown body, and the three
  advisory actions. A suggestion is labelled "not applied automatically"
  because it is display-only in this iteration. Resolved and dismissed
  findings collapse to one muted line with an Undo, so a reviewed diff does
  not stay cluttered while the human can still change their mind.
- A review-finding annotation type threaded through the diff viewer, anchored
  from the same per-file diff hash the backend stamped. A finding whose diff
  moved is re-anchored from its stored text when that text survives, and
  otherwise reported as unanchored rather than placed against unrelated code.
- Review changes control with idle / running / cancel / failed states, and an
  inline Settings link for the one failure the user can actually fix.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(web): make the review control reachable from the Review toolbar

Mounts the Review changes control next to the walkthrough action and lets an
inline finding wire its own actions.

The finding card container now reads the active task and session from the store
instead of taking four callbacks threaded through FileDiffViewer and DiffViewer;
an inline finding annotation only exists when the active task has findings, so
the store is always present and every other diff consumer is spared the
plumbing. Staleness likewise hashes the diff the viewer already holds, which is
already normalized, so it matches the hash the backend stamped without passing
it down.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(web): surface stale review findings above their file's diff

A finding whose diff moved and whose anchor text is gone had nowhere to render,
so it was silently dropped. It now appears in a collapsed per-file banner above
the diff: the finding may still be valid, and placing it at its stored line
would point at whatever code now occupies that line instead.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs(review): mark the landed native code-review plan tasks done

Tasks 01-09 (backend persistence through inline diff rendering) are
implemented, tested, and committed. Tasks 10-13 — findings overview and step
editor, mobile parity, E2E, and public docs — remain pending.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(review): cover the on-demand review path end to end

Adds the mock-agent review responder and a Playwright spec, so both the happy
path and the no-reviewer path are verified against a real backend.

The mock agent answers any prompt carrying the review sentinel with fenced JSON,
deriving its anchors from the prompt's own diff rather than hardcoding them, so
findings land on files and lines the fixture really changed. It includes one
deliberately malformed entry to exercise the rejected-entry path.

Writing it caught an off-by-one shared by three diff walkers: splitting on "\n"
leaves an empty element after a trailing newline, which carries no diff marker
and was being counted as a context line, pushing new-side numbering one past the
end of a hunk. Interior blank lines still count, since a whitespace-stripped
blank context line legitimately appears as "".

Also adds a listInferenceAgents helper: default_utility_agent_id needs the
registered agent-type id, and an agent row UUID is rejected as not
inference-capable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(review): close review-run races and stale client findings

Addresses the three blockers and three suggestions from the PR #1957 automated
reviews. Two independent reviewers found each blocker separately.

- Launch claimed the task slot only after inserting the run row, so two
  concurrent launches both created a run and the loser returned a pending row
  with no goroutine behind it — a review that never finished until the next
  restart. The slot is now claimed before any DB write, and freed on every
  early return.
- Cancelling only marked the row. The goroutine ran on the shared background
  context, finished inference, and its completion overwrote the cancelled
  status and published findings the user had declined. Runs now carry a cancel
  handle, the WS action routes through the runner so the live context is
  cancelled, CompleteRun/FailRun refuse to resurrect a terminal run, and a
  cancelled pass neither publishes nor re-reports itself as failed.
- The backend deletes a superseded finding and inserts a replacement with a new
  id, but the publish event carried only the new one, so every open browser
  showed both at the same anchor until a reload. The event now names the
  superseded ids and the slice drops them.
- Run() waited on the shared WaitGroup, blocking on every other task's review;
  it now waits on its own pass only.
- createReviewSlice is typed over the `set` it actually uses, so call sites no
  longer pass superfluous arguments (CodeQL js/superfluous-trailing-arguments).
- A GetAgentProfile fault no longer collapses into "profile not found", which
  would have told the user to configure an agent they already have.

The cancel bug exposed a hole in the original tests: they asserted cancel could
not override a terminal run, but never the reverse. Both directions are covered
now, along with the concurrent-launch race, per-run waiting, and the supersede
path on both sides of the wire. Test fakes now mirror the service's terminal
guard rather than hiding it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 15:05:56 +01:00

434 lines
17 KiB
Go

package mcp
import (
"encoding/json"
"testing"
"github.com/kandev/kandev/internal/common/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestLogger(t *testing.T) *logger.Logger {
t.Helper()
log, err := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "console"})
require.NoError(t, err)
return log
}
// getRegisteredToolNames returns the names of all tools registered on the MCP server.
func getRegisteredToolNames(s *Server) []string {
toolsMap := s.mcpServer.ListTools()
names := make([]string, 0, len(toolsMap))
for name := range toolsMap {
names = append(names, name)
}
return names
}
func TestServerModeTask_RegistersCorrectTools(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, ModeTask)
require.NotNil(t, s)
tools := getRegisteredToolNames(s)
// Task mode should have kanban tools
assert.Contains(t, tools, "list_workspaces_kandev")
assert.Contains(t, tools, "list_workflows_kandev")
assert.Contains(t, tools, "list_workflow_steps_kandev")
assert.Contains(t, tools, "list_tasks_kandev")
assert.Contains(t, tools, "create_task_kandev")
assert.Contains(t, tools, "update_task_kandev")
assert.Contains(t, tools, "move_task_kandev")
assert.Contains(t, tools, "message_task_kandev")
assert.Contains(t, tools, "stop_task_kandev")
assert.Contains(t, tools, "get_task_conversation_kandev")
// Task mode should have plan tools
assert.Contains(t, tools, "create_task_plan_kandev")
assert.Contains(t, tools, "get_task_plan_kandev")
assert.Contains(t, tools, "update_task_plan_kandev")
assert.Contains(t, tools, "delete_task_plan_kandev")
// Task mode should have interaction tools
assert.Contains(t, tools, "ask_user_question_kandev")
// Task mode should have profile listing tools (needed for create_task)
assert.Contains(t, tools, "list_agents_kandev")
assert.Contains(t, tools, "list_executor_profiles_kandev")
// Task mode keeps list_related_tasks_kandev (sibling discovery) but
// drops the task-document tools — those are office-only.
assert.Contains(t, tools, "list_related_tasks_kandev")
assert.NotContains(t, tools, "list_task_documents_kandev")
assert.NotContains(t, tools, "get_task_document_kandev")
assert.NotContains(t, tools, "write_task_document_kandev")
// Task mode exposes delete + archive so agents can clean up the tasks
// they fan out. Restore/unarchive is intentionally NOT exposed via MCP —
// it stays a user action in the UI.
assert.Contains(t, tools, "delete_task_kandev")
assert.Contains(t, tools, "archive_task_kandev")
assert.NotContains(t, tools, "restore_task_kandev")
// Task mode should NOT have config/mutation tools
assert.NotContains(t, tools, "create_workflow_kandev")
assert.NotContains(t, tools, "update_workflow_kandev")
assert.NotContains(t, tools, "delete_workflow_kandev")
assert.NotContains(t, tools, "create_workflow_step_kandev")
assert.NotContains(t, tools, "update_workflow_step_kandev")
assert.NotContains(t, tools, "update_agent_kandev")
assert.NotContains(t, tools, "create_agent_profile_kandev")
assert.NotContains(t, tools, "delete_agent_profile_kandev")
assert.NotContains(t, tools, "list_agent_profiles_kandev")
assert.NotContains(t, tools, "update_agent_profile_kandev")
assert.NotContains(t, tools, "get_mcp_config_kandev")
assert.NotContains(t, tools, "update_mcp_config_kandev")
assert.NotContains(t, tools, "list_executors_kandev")
assert.NotContains(t, tools, "create_executor_profile_kandev")
assert.NotContains(t, tools, "update_executor_profile_kandev")
assert.NotContains(t, tools, "delete_executor_profile_kandev")
assert.NotContains(t, tools, "update_task_state_kandev")
assert.NotContains(t, tools, "delete_workflow_step_kandev")
assert.NotContains(t, tools, "reorder_workflow_steps_kandev")
}
func TestServerModeConfig_RegistersCorrectTools(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, ModeConfig)
require.NotNil(t, s)
tools := getRegisteredToolNames(s)
// Config mode should have workflow config tools
assert.Contains(t, tools, "list_workspaces_kandev")
assert.Contains(t, tools, "list_workflows_kandev")
assert.Contains(t, tools, "list_repositories_kandev")
assert.Contains(t, tools, "create_workflow_kandev")
assert.Contains(t, tools, "update_workflow_kandev")
assert.Contains(t, tools, "delete_workflow_kandev")
assert.Contains(t, tools, "import_workflow_kandev")
assert.Contains(t, tools, "list_workflow_steps_kandev")
assert.Contains(t, tools, "create_workflow_step_kandev")
assert.Contains(t, tools, "update_workflow_step_kandev")
assert.Contains(t, tools, "delete_workflow_step_kandev")
assert.Contains(t, tools, "reorder_workflow_steps_kandev")
// Config mode should have agent tools
assert.Contains(t, tools, "list_agents_kandev")
assert.Contains(t, tools, "update_agent_kandev")
assert.Contains(t, tools, "create_agent_profile_kandev")
assert.Contains(t, tools, "delete_agent_profile_kandev")
// Config mode should have MCP config tools
assert.Contains(t, tools, "list_agent_profiles_kandev")
assert.Contains(t, tools, "update_agent_profile_kandev")
assert.Contains(t, tools, "get_mcp_config_kandev")
assert.Contains(t, tools, "update_mcp_config_kandev")
// Config mode should have executor profile tools
assert.Contains(t, tools, "list_executors_kandev")
assert.Contains(t, tools, "list_executor_profiles_kandev")
assert.Contains(t, tools, "create_executor_profile_kandev")
assert.Contains(t, tools, "update_executor_profile_kandev")
assert.Contains(t, tools, "delete_executor_profile_kandev")
// Config mode should have task tools
assert.Contains(t, tools, "list_tasks_kandev")
assert.Contains(t, tools, "move_task_kandev")
assert.Contains(t, tools, "delete_task_kandev")
assert.Contains(t, tools, "archive_task_kandev")
assert.Contains(t, tools, "update_task_state_kandev")
assert.Contains(t, tools, "get_task_conversation_kandev")
// Config mode should have interaction tools
assert.Contains(t, tools, "ask_user_question_kandev")
// Config mode should NOT have plan tools
assert.NotContains(t, tools, "create_task_plan_kandev")
assert.NotContains(t, tools, "get_task_plan_kandev")
assert.NotContains(t, tools, "update_task_plan_kandev")
assert.NotContains(t, tools, "delete_task_plan_kandev")
// Config mode should NOT have task-mode kanban create/update tools
assert.NotContains(t, tools, "create_task_kandev")
assert.NotContains(t, tools, "update_task_kandev")
assert.NotContains(t, tools, "stop_task_kandev")
}
func TestServerModeDefault_DefaultsToTask(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, "")
require.NotNil(t, s)
assert.Equal(t, ModeTask, s.mode)
tools := getRegisteredToolNames(s)
assert.Contains(t, tools, "create_task_kandev")
assert.Contains(t, tools, "create_task_plan_kandev")
assert.Contains(t, tools, "stop_task_kandev")
assert.NotContains(t, tools, "create_workflow_step_kandev")
}
func TestServerModeConfig_DisableAskQuestion(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", true, ModeConfig)
require.NotNil(t, s)
tools := getRegisteredToolNames(s)
assert.NotContains(t, tools, "ask_user_question_kandev")
assert.Contains(t, tools, "list_agents_kandev")
assert.Contains(t, tools, "create_workflow_step_kandev")
}
func TestServerModeTask_DisableAskQuestion(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", true, ModeTask)
require.NotNil(t, s)
tools := getRegisteredToolNames(s)
assert.NotContains(t, tools, "ask_user_question_kandev")
assert.Contains(t, tools, "create_task_kandev")
assert.Contains(t, tools, "create_task_plan_kandev")
}
func TestServerModeTask_ToolCount(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, ModeTask)
tools := getRegisteredToolNames(s)
// 15 kanban (incl. delete + archive task + stop_task + spawn_session) +
// 1 add_branch_to_task + 1 add_workspace_sources + 1 update_repository_base_branch +
// 1 step_complete (ADR 0015) + 1 interaction + 4 plan + 3 walkthrough +
// 1 publish_review_findings + 1 related-tasks = 29.
// Task-document tools (list/get/write) are office-only.
assert.Contains(t, tools, "step_complete_kandev", "ADR 0015 explicit-completion signal must be registered in task mode")
assert.Contains(t, tools, "show_walkthrough_kandev", "walkthrough tool must be registered in task mode")
assert.Contains(t, tools, "publish_review_findings_kandev", "native code-review publishing must be registered in task mode")
assert.Contains(t, tools, "spawn_session_kandev", "spawn_session must be registered in task mode")
assert.Contains(t, tools, "add_workspace_sources_kandev")
assert.Equal(t, 29, len(tools))
}
func TestServerStepCompleteTool_TaskOnlyAndDiscoverable(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
taskServer := New(backend, "test-session", "test-task", 10005, log, "", false, ModeTask)
stepComplete, ok := taskServer.mcpServer.ListTools()["step_complete_kandev"]
require.True(t, ok, "task mode must register step_complete_kandev")
serialized, err := json.Marshal(stepComplete.Tool)
require.NoError(t, err)
var tool map[string]json.RawMessage
require.NoError(t, json.Unmarshal(serialized, &tool))
assert.NotContains(t, tool, "_meta", "task tools should use normal client discovery")
for _, mode := range []string{ModeOffice, ModeConfig, ModeExternal} {
t.Run(mode, func(t *testing.T) {
restrictedServer := New(backend, "test-session", "test-task", 10005, log, "", false, mode)
assert.NotContains(t, restrictedServer.mcpServer.ListTools(), "step_complete_kandev")
})
}
}
func TestServerModeConfig_ToolCount(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, ModeConfig)
tools := getRegisteredToolNames(s)
// 12 workflow (incl. list_repositories + import_workflow) + 4 agent + 4 mcp + 5 executor + 6 task + 1 interaction = 32
assert.NotContains(t, tools, "step_complete_kandev", "step_complete_kandev requires a live task session; must NOT register in config mode")
assert.Equal(t, 32, len(tools))
}
func TestServerModeConfig_ToolDescriptions(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, ModeConfig)
toolsMap := s.mcpServer.ListTools()
assert.Contains(t, toolsMap["create_workflow_step_kandev"].Tool.Description, "Create a new workflow step")
assert.Contains(t, toolsMap["list_agents_kandev"].Tool.Description, "List all configured agents")
assert.Contains(t, toolsMap["get_mcp_config_kandev"].Tool.Description, "Get MCP server configuration")
}
func TestServerModeOffice_RegistersCorrectTools(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, ModeOffice)
require.NotNil(t, s)
tools := getRegisteredToolNames(s)
// Office mode should have plan tools
assert.Contains(t, tools, "create_task_plan_kandev")
assert.Contains(t, tools, "get_task_plan_kandev")
assert.Contains(t, tools, "update_task_plan_kandev")
assert.Contains(t, tools, "delete_task_plan_kandev")
// Office mode should have interaction tools
assert.Contains(t, tools, "ask_user_question_kandev")
// delegate_task_kandev was removed from ModeOffice when the
// agentctl CLI started covering task creation/delegation via
// `agentctl kandev task create --parent $KANDEV_TASK_ID …`.
assert.NotContains(t, tools, "delegate_task_kandev")
// Office mode exposes the cross-task handoff tools.
assert.Contains(t, tools, "list_related_tasks_kandev")
assert.Contains(t, tools, "list_task_documents_kandev")
assert.Contains(t, tools, "get_task_document_kandev")
assert.Contains(t, tools, "write_task_document_kandev")
// Office mode should NOT have kanban tools
assert.NotContains(t, tools, "create_task_kandev")
assert.NotContains(t, tools, "list_tasks_kandev")
assert.NotContains(t, tools, "update_task_kandev")
assert.NotContains(t, tools, "list_workspaces_kandev")
assert.NotContains(t, tools, "list_workflows_kandev")
assert.NotContains(t, tools, "list_workflow_steps_kandev")
assert.NotContains(t, tools, "list_agents_kandev")
assert.NotContains(t, tools, "list_executor_profiles_kandev")
assert.NotContains(t, tools, "stop_task_kandev")
// Office mode should NOT have config tools
assert.NotContains(t, tools, "create_workflow_kandev")
assert.NotContains(t, tools, "update_workflow_kandev")
assert.NotContains(t, tools, "update_agent_kandev")
}
func TestServerModeOffice_ToolCount(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", false, ModeOffice)
tools := getRegisteredToolNames(s)
// 4 plan + 1 interaction + 1 related-tasks + 3 task-documents = 9
// (delegate_task_kandev retired in favour of `agentctl kandev task create …`).
assert.NotContains(t, tools, "step_complete_kandev", "step_complete_kandev is kanban-task-only; office mode advances tasks via its own approval surface")
assert.Equal(t, 9, len(tools))
}
func TestServerModeOffice_DisableAskQuestion(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "test-session", "test-task", 10005, log, "", true, ModeOffice)
require.NotNil(t, s)
tools := getRegisteredToolNames(s)
assert.NotContains(t, tools, "ask_user_question_kandev")
assert.Contains(t, tools, "create_task_plan_kandev")
// delegate_task_kandev was retired from ModeOffice (now lives in
// the agentctl CLI as `agentctl kandev task create --parent …`).
assert.NotContains(t, tools, "delegate_task_kandev")
// 4 plan + 1 related-tasks + 3 task-documents = 8 (no ask_user_question, no delegate)
assert.Equal(t, 8, len(tools))
}
func TestServerModeConstants(t *testing.T) {
assert.Equal(t, "task", ModeTask)
assert.Equal(t, "config", ModeConfig)
assert.Equal(t, "external", ModeExternal)
assert.Equal(t, "office", ModeOffice)
}
func TestServerModeExternal_RegistersCorrectTools(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "", "", 0, log, "", true, ModeExternal)
require.NotNil(t, s)
tools := getRegisteredToolNames(s)
// External mode includes all config tools
assert.Contains(t, tools, "list_workspaces_kandev")
assert.Contains(t, tools, "list_repositories_kandev")
assert.Contains(t, tools, "create_workflow_kandev")
assert.Contains(t, tools, "list_agents_kandev")
assert.Contains(t, tools, "get_mcp_config_kandev")
assert.Contains(t, tools, "list_executors_kandev")
assert.Contains(t, tools, "move_task_kandev")
// External mode includes create_task_kandev so external agents can spawn tasks
assert.Contains(t, tools, "create_task_kandev")
createTask := s.mcpServer.ListTools()["create_task_kandev"]
assert.Contains(t, createTask.Tool.Description, "explicit agent_profile_id always wins")
assert.Contains(t, createTask.Tool.Description, "current_task")
assert.Contains(t, createTask.Tool.Description, "workspace_default")
assert.Contains(t, createTask.Tool.Description, "workflow profiles first")
// External mode does NOT include session-scoped tools
assert.NotContains(t, tools, "ask_user_question_kandev")
assert.NotContains(t, tools, "create_task_plan_kandev")
assert.NotContains(t, tools, "get_task_plan_kandev")
assert.NotContains(t, tools, "update_task_plan_kandev")
assert.NotContains(t, tools, "delete_task_plan_kandev")
// External mode does NOT include kanban update_task_kandev (config has its own update_task_state)
assert.NotContains(t, tools, "update_task_kandev")
// External mode does NOT include message_task_kandev (no live session context)
assert.NotContains(t, tools, "message_task_kandev")
assert.NotContains(t, tools, "stop_task_kandev")
}
func TestServerModeExternal_ToolCount(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := New(backend, "", "", 0, log, "", true, ModeExternal)
tools := getRegisteredToolNames(s)
// 12 workflow (incl. list_repositories + import_workflow) + 4 agent + 4 mcp + 5 executor + 6 task + 1 create_task = 32.
// add_branch_to_task_kandev is task-mode only — external coding agents have no live session to attach a worktree to.
assert.Equal(t, 32, len(tools))
assert.NotContains(t, tools, "add_branch_to_task_kandev")
}
func TestNewExternal_Constructs(t *testing.T) {
log := newTestLogger(t)
backend := NewChannelBackendClient(log)
defer backend.Close()
s := NewExternal(backend, log, "")
require.NotNil(t, s)
assert.Equal(t, ModeExternal, s.mode)
assert.True(t, s.disableAskQuestion)
assert.Empty(t, s.sessionID)
assert.Empty(t, s.taskID)
assert.NotNil(t, s.sseServer)
assert.NotNil(t, s.httpServer)
assert.Equal(t, "/mcp/message?sessionId=session-1", s.sseServer.GetMessageEndpointForClient(nil, "session-1"))
}