fix: re-inject plan mode instructions on follow-up prompts (#507)
This commit is contained in:
@@ -108,6 +108,9 @@ type mockAgentManager struct {
|
||||
mu sync.Mutex
|
||||
stopAgentWithReasonArgs []stopAgentCall // tracks StopAgentWithReason calls
|
||||
|
||||
// Prompt tracking
|
||||
capturedPrompts []string // tracks prompts passed to PromptAgent
|
||||
|
||||
// Passthrough stdin tracking
|
||||
passthroughStdinCalls []passthroughStdinCall
|
||||
passthroughStdinErr error
|
||||
@@ -141,7 +144,8 @@ func (m *mockAgentManager) StopAgentWithReason(_ context.Context, agentExecution
|
||||
})
|
||||
return nil
|
||||
}
|
||||
func (m *mockAgentManager) PromptAgent(_ context.Context, _ string, _ string, _ []v1.MessageAttachment) (*executor.PromptResult, error) {
|
||||
func (m *mockAgentManager) PromptAgent(_ context.Context, _ string, prompt string, _ []v1.MessageAttachment) (*executor.PromptResult, error) {
|
||||
m.capturedPrompts = append(m.capturedPrompts, prompt)
|
||||
if m.promptErr != nil {
|
||||
return nil, m.promptErr
|
||||
}
|
||||
|
||||
@@ -1334,6 +1334,11 @@ func (s *Service) PromptTask(ctx context.Context, taskID, sessionID string, prom
|
||||
effectivePrompt = sysprompt.InjectConfigContext(sessionID, prompt)
|
||||
}
|
||||
|
||||
// Inject plan mode prefix for follow-up messages in plan mode sessions.
|
||||
if planMode {
|
||||
effectivePrompt = sysprompt.InjectPlanMode(effectivePrompt)
|
||||
}
|
||||
|
||||
// Check if model switching is requested
|
||||
if result, switched, err := s.trySwitchModel(ctx, taskID, sessionID, model, effectivePrompt, session); switched || err != nil {
|
||||
return result, err
|
||||
|
||||
@@ -117,6 +117,70 @@ func TestPromptTask_TransientErrorDoesNotMoveTaskToReview(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptTask_PlanModeInjectsPrefix(t *testing.T) {
|
||||
repo := setupTestRepo(t)
|
||||
taskRepo := newMockTaskRepo()
|
||||
agentMgr := &mockAgentManager{
|
||||
isAgentRunning: true,
|
||||
}
|
||||
svc := createTestServiceWithAgent(repo, newMockStepGetter(), taskRepo, agentMgr)
|
||||
svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{})
|
||||
|
||||
seedTaskAndSession(t, repo, "task1", "session1", models.TaskSessionStateWaitingForInput)
|
||||
session, err := repo.GetTaskSession(context.Background(), "session1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load session: %v", err)
|
||||
}
|
||||
session.AgentExecutionID = "exec-1"
|
||||
if err := repo.UpdateTaskSession(context.Background(), session); err != nil {
|
||||
t.Fatalf("failed to update session: %v", err)
|
||||
}
|
||||
|
||||
_, err = svc.PromptTask(context.Background(), "task1", "session1", "update the plan", "", true, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if len(agentMgr.capturedPrompts) != 1 {
|
||||
t.Fatalf("expected 1 prompt, got %d", len(agentMgr.capturedPrompts))
|
||||
}
|
||||
if !strings.Contains(agentMgr.capturedPrompts[0], "PLAN MODE ACTIVE") {
|
||||
t.Fatalf("expected plan mode prefix in prompt, got: %s", agentMgr.capturedPrompts[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptTask_NoPlanModeDoesNotInjectPrefix(t *testing.T) {
|
||||
repo := setupTestRepo(t)
|
||||
taskRepo := newMockTaskRepo()
|
||||
agentMgr := &mockAgentManager{
|
||||
isAgentRunning: true,
|
||||
}
|
||||
svc := createTestServiceWithAgent(repo, newMockStepGetter(), taskRepo, agentMgr)
|
||||
svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{})
|
||||
|
||||
seedTaskAndSession(t, repo, "task1", "session1", models.TaskSessionStateWaitingForInput)
|
||||
session, err := repo.GetTaskSession(context.Background(), "session1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load session: %v", err)
|
||||
}
|
||||
session.AgentExecutionID = "exec-1"
|
||||
if err := repo.UpdateTaskSession(context.Background(), session); err != nil {
|
||||
t.Fatalf("failed to update session: %v", err)
|
||||
}
|
||||
|
||||
_, err = svc.PromptTask(context.Background(), "task1", "session1", "implement the feature", "", false, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if len(agentMgr.capturedPrompts) != 1 {
|
||||
t.Fatalf("expected 1 prompt, got %d", len(agentMgr.capturedPrompts))
|
||||
}
|
||||
if strings.Contains(agentMgr.capturedPrompts[0], "PLAN MODE ACTIVE") {
|
||||
t.Fatalf("expected no plan mode prefix in prompt, got: %s", agentMgr.capturedPrompts[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptTask_ResetInProgressReturnsSentinelError(t *testing.T) {
|
||||
repo := setupTestRepo(t)
|
||||
taskRepo := newMockTaskRepo()
|
||||
|
||||
@@ -55,6 +55,10 @@ WORKFLOW:
|
||||
5. Save your changes using the update_task_plan_kandev MCP tool (or create_task_plan_kandev if no plan exists yet).
|
||||
6. After saving, STOP and wait for the user to review.
|
||||
|
||||
When the user sends comments or feedback on the plan, treat them as revision requests:
|
||||
- Read the current plan, apply the requested changes, and save the updated plan.
|
||||
- Do NOT start implementing code changes. Stay in planning mode.
|
||||
|
||||
This instruction applies to THIS PROMPT ONLY.`
|
||||
|
||||
// KandevContext is the system prompt that provides Kandev-specific instructions
|
||||
|
||||
@@ -605,9 +605,15 @@ export class ApiClient {
|
||||
|
||||
// --- Session ---
|
||||
|
||||
async listSessionMessages(
|
||||
sessionId: string,
|
||||
): Promise<{ messages: Array<{ id: string; content: string; author_type: string }> }> {
|
||||
async listSessionMessages(sessionId: string): Promise<{
|
||||
messages: Array<{
|
||||
id: string;
|
||||
content: string;
|
||||
author_type: string;
|
||||
raw_content?: string;
|
||||
metadata?: Record<string, unknown>;
|
||||
}>;
|
||||
}> {
|
||||
return this.request("GET", `/api/v1/task-sessions/${sessionId}/messages`);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
import { type Page } from "@playwright/test";
|
||||
import { test, expect } from "../../fixtures/test-base";
|
||||
import type { SeedData } from "../../fixtures/test-base";
|
||||
import type { ApiClient } from "../../helpers/api-client";
|
||||
import { SessionPage } from "../../pages/session-page";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Shared helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** MCP script that creates plan content the agent can interact with. */
|
||||
const PLAN_SCRIPT = [
|
||||
'e2e:thinking("Creating plan...")',
|
||||
"e2e:delay(100)",
|
||||
'e2e:mcp:kandev:create_task_plan({"task_id":"{task_id}","content":"## Plan\\n\\n1. Analyze requirements\\n2. Implement solution\\n3. Write tests","title":"Implementation Plan"})',
|
||||
"e2e:delay(100)",
|
||||
'e2e:message("Plan created.")',
|
||||
].join("\n");
|
||||
|
||||
/** Create a task with agent, navigate to session, wait for idle. */
|
||||
async function seedTaskAndWaitForIdle(
|
||||
testPage: Page,
|
||||
apiClient: ApiClient,
|
||||
seedData: SeedData,
|
||||
title: string,
|
||||
description = "/e2e:simple-message",
|
||||
) {
|
||||
const task = await apiClient.createTaskWithAgent(
|
||||
seedData.workspaceId,
|
||||
title,
|
||||
seedData.agentProfileId,
|
||||
{
|
||||
description,
|
||||
workflow_id: seedData.workflowId,
|
||||
workflow_step_id: seedData.startStepId,
|
||||
repository_ids: [seedData.repositoryId],
|
||||
},
|
||||
);
|
||||
|
||||
await testPage.goto(`/t/${task.id}`);
|
||||
|
||||
const session = new SessionPage(testPage);
|
||||
await session.waitForLoad();
|
||||
await expect(session.idleInput()).toBeVisible({ timeout: 30_000 });
|
||||
|
||||
return { session, taskId: task.id, sessionId: task.session_id! };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Plan mode follow-up message tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test.describe("Plan mode follow-up messages", () => {
|
||||
test.describe.configure({ retries: 1 });
|
||||
|
||||
test("follow-up message in plan mode shows badge and stores plan_mode metadata", async ({
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
}) => {
|
||||
test.setTimeout(90_000);
|
||||
|
||||
const { session, sessionId } = await seedTaskAndWaitForIdle(
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
"Plan mode follow-up test",
|
||||
);
|
||||
|
||||
// Enable plan mode after initial agent turn completes.
|
||||
await session.togglePlanMode();
|
||||
await expect(session.planModeInput()).toBeVisible({ timeout: 10_000 });
|
||||
|
||||
// Send a follow-up message in plan mode.
|
||||
await session.sendMessage("Please add error handling to step 3 of the plan");
|
||||
|
||||
// The follow-up message should have the plan mode badge.
|
||||
const planBadges = session.chat.getByText("Plan mode", { exact: true });
|
||||
await expect(planBadges.last()).toBeVisible({ timeout: 15_000 });
|
||||
|
||||
// Wait for agent to complete and return to plan mode idle state.
|
||||
await expect(session.planModeInput()).toBeVisible({ timeout: 30_000 });
|
||||
|
||||
// Verify the stored message has plan_mode metadata (confirms frontend sent plan_mode: true
|
||||
// and backend stored it, which triggers InjectPlanMode in PromptTask).
|
||||
const { messages } = await apiClient.listSessionMessages(sessionId);
|
||||
const userFollowUp = messages.filter((m) => m.author_type === "user").pop();
|
||||
expect(userFollowUp).toBeDefined();
|
||||
expect(userFollowUp!.metadata?.plan_mode).toBe(true);
|
||||
});
|
||||
|
||||
test("follow-up message without plan mode has no badge or plan_mode metadata", async ({
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
}) => {
|
||||
test.setTimeout(90_000);
|
||||
|
||||
const { session, sessionId } = await seedTaskAndWaitForIdle(
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
"No plan mode follow-up test",
|
||||
);
|
||||
|
||||
// Send a follow-up without plan mode.
|
||||
await session.sendMessage("implement the feature now");
|
||||
await expect(session.idleInput()).toBeVisible({ timeout: 30_000 });
|
||||
|
||||
// No plan mode badge.
|
||||
const planBadges = session.chat.getByText("Plan mode", { exact: true });
|
||||
await expect(planBadges).toHaveCount(0, { timeout: 5_000 });
|
||||
|
||||
// Verify no plan_mode metadata on the stored message.
|
||||
const { messages } = await apiClient.listSessionMessages(sessionId);
|
||||
const userFollowUp = messages.filter((m) => m.author_type === "user").pop();
|
||||
expect(userFollowUp).toBeDefined();
|
||||
expect(userFollowUp!.metadata?.plan_mode).toBeFalsy();
|
||||
});
|
||||
|
||||
test("plan comment via Run button sends to agent with plan mode", async ({
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
}) => {
|
||||
test.setTimeout(120_000);
|
||||
|
||||
// Create a task that produces plan content via MCP.
|
||||
const { session, sessionId } = await seedTaskAndWaitForIdle(
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
"Plan comment Run test",
|
||||
PLAN_SCRIPT,
|
||||
);
|
||||
|
||||
// Toggle plan mode so the plan panel is visible with content.
|
||||
await session.togglePlanMode();
|
||||
await expect(session.planModeInput()).toBeVisible({ timeout: 10_000 });
|
||||
await expect(session.planPanel.getByText("Analyze requirements", { exact: false })).toBeVisible(
|
||||
{
|
||||
timeout: 15_000,
|
||||
},
|
||||
);
|
||||
|
||||
// Select text in the plan editor to trigger the comment popover.
|
||||
const editor = session.planPanel.locator(".ProseMirror");
|
||||
await editor.click();
|
||||
|
||||
// Select all text with keyboard.
|
||||
const modifier = process.platform === "darwin" ? "Meta" : "Control";
|
||||
await testPage.keyboard.press(`${modifier}+a`);
|
||||
|
||||
// Open comment popover via Cmd+Shift+C.
|
||||
await testPage.keyboard.press(`${modifier}+Shift+c`);
|
||||
|
||||
// Fill in the comment textarea.
|
||||
const textarea = testPage.locator('textarea[placeholder="Add your comment or instruction..."]');
|
||||
await expect(textarea).toBeVisible({ timeout: 5_000 });
|
||||
await textarea.fill("Split step 2 into smaller sub-steps");
|
||||
|
||||
// Click the "Run" button (use exact match to avoid matching sidebar task button).
|
||||
const runBtn = testPage.getByRole("button", { name: "Run", exact: true });
|
||||
await expect(runBtn).toBeVisible({ timeout: 5_000 });
|
||||
await runBtn.click();
|
||||
|
||||
// The comment should appear in the chat formatted as plan comment markdown.
|
||||
await expect(session.chat.getByText("Comment 1:", { exact: false })).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
await expect(
|
||||
session.chat.getByText("Split step 2 into smaller sub-steps", { exact: false }),
|
||||
).toBeVisible({ timeout: 5_000 });
|
||||
|
||||
// Plan mode badge should be visible on the comment message.
|
||||
const planBadges = session.chat.getByText("Plan mode", { exact: true });
|
||||
await expect(planBadges.last()).toBeVisible({ timeout: 10_000 });
|
||||
|
||||
// Wait for agent to complete.
|
||||
await expect(session.planModeInput()).toBeVisible({ timeout: 30_000 });
|
||||
|
||||
// Verify the comment message was stored with plan_mode metadata.
|
||||
const { messages } = await apiClient.listSessionMessages(sessionId);
|
||||
const commentMsg = messages
|
||||
.filter((m) => m.author_type === "user")
|
||||
.find((m) => m.content.includes("Split step 2"));
|
||||
expect(commentMsg).toBeDefined();
|
||||
expect(commentMsg!.metadata?.plan_mode).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useCallback } from "react";
|
||||
import { getWebSocketClient } from "@/lib/ws/connection";
|
||||
import { appendToQueue } from "@/lib/api/domains/queue-api";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import { useCommentsStore } from "@/lib/state/slices/comments";
|
||||
import {
|
||||
formatReviewCommentsAsMarkdown,
|
||||
@@ -62,6 +63,9 @@ type UseRunCommentParams = {
|
||||
*/
|
||||
export function useRunComment({ sessionId, taskId, isAgentBusy }: UseRunCommentParams) {
|
||||
const markCommentsSent = useCommentsStore((s) => s.markCommentsSent);
|
||||
const planModeEnabled = useAppStore((s) =>
|
||||
sessionId ? (s.chatInput.planModeBySessionId[sessionId] ?? false) : false,
|
||||
);
|
||||
|
||||
const runComment = useCallback(
|
||||
async (comment: Comment) => {
|
||||
@@ -75,6 +79,7 @@ export function useRunComment({ sessionId, taskId, isAgentBusy }: UseRunCommentP
|
||||
session_id: sessionId,
|
||||
task_id: taskId,
|
||||
content,
|
||||
...(planModeEnabled && { plan_mode: true }),
|
||||
});
|
||||
} else {
|
||||
const client = getWebSocketClient();
|
||||
@@ -86,6 +91,7 @@ export function useRunComment({ sessionId, taskId, isAgentBusy }: UseRunCommentP
|
||||
task_id: taskId,
|
||||
session_id: sessionId,
|
||||
content,
|
||||
...(planModeEnabled && { plan_mode: true }),
|
||||
...(comment.source !== "plan" && { has_review_comments: true }),
|
||||
},
|
||||
10000,
|
||||
@@ -98,7 +104,7 @@ export function useRunComment({ sessionId, taskId, isAgentBusy }: UseRunCommentP
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[sessionId, taskId, isAgentBusy, markCommentsSent],
|
||||
[sessionId, taskId, isAgentBusy, planModeEnabled, markCommentsSent],
|
||||
);
|
||||
|
||||
return { runComment };
|
||||
|
||||
Reference in New Issue
Block a user