diff --git a/frontend-v2/e2e/workflow-agents.spec.ts b/frontend-v2/e2e/workflow-agents.spec.ts index ce69e574..235ba1cb 100644 --- a/frontend-v2/e2e/workflow-agents.spec.ts +++ b/frontend-v2/e2e/workflow-agents.spec.ts @@ -2,7 +2,7 @@ import { expect, test } from '@playwright/test' import { signIn } from './login.ts' type Executor = { agentId: string; agentName: string; modelId: string; modelName: string } -test('workflow agent: an empty directory retains desktop defaults for a new node', async ({ page }) => { +test('workflow agent: an empty directory leaves a new node executor blank without desktop defaults', async ({ page }) => { await page.route('**/api/v2/tenants/*/chat/executors', (route) => route.fulfill({ json: { items: [] } })) await page.goto('/workflows') await signIn(page) @@ -15,11 +15,14 @@ test('workflow agent: an empty directory retains desktop defaults for a new node await page.reload() await page.getByRole('complementary', { name: '工作流列表' }).getByRole('button', { name, exact: true }).click() await page.getByRole('toolbar', { name: '添加工作流节点' }).getByRole('button', { name: 'Agent', exact: true }).click() - await expect(page.getByText('请选择当前可用的 Agent 和模型。')).toBeVisible() + const modelButton = page.getByLabel('Agent 模型', { exact: true }) + await expect(modelButton).toContainText('没有可用 Agent 模型') + await expect(modelButton).not.toContainText(' · ') + await expect(page.getByText('请选择当前可用的 Agent 和模型。')).toHaveCount(0) await expect.poll(async () => { const draft = (await (await page.request.get(`${base}/${wid}/draft`)).json()).snapshot return JSON.parse(draft.graph).nodes.find((n: { data: { kind: string } }) => n.data.kind === 'agent')?.data.agentConfig.executor - }).toEqual({ agentCli: 'official/ora-space.codeagentcli', modelId: 'gpt-5' }) + }).toEqual({ agentCli: '', modelId: '' }) }) test('workflow agent: replace a legacy demo choice, persist real IDs, and execute the selected binding', async ({ page }, testInfo) => { diff --git a/frontend-v2/e2e/workflow-model-hydration.spec.ts b/frontend-v2/e2e/workflow-model-hydration.spec.ts index 7242357a..d7df1a44 100644 --- a/frontend-v2/e2e/workflow-model-hydration.spec.ts +++ b/frontend-v2/e2e/workflow-model-hydration.spec.ts @@ -42,3 +42,75 @@ for (const [label, model, expected] of [ expect(await savedExecutor()).toEqual({ agentCli: agent, modelId: expected }) }) } + +test('workflow editor shows empty model placeholder without desktop default when catalog is empty', async ({ page }, testInfo) => { + await page.route('**/api/v2/tenants/*/chat/executors', (route) => route.fulfill({ json: { items: [] } })) + await page.goto('/workflows') + await signIn(page) + const tid = (await (await page.request.get('/api/v1/me/tenants')).json()).items[0].id + const base = `/api/v2/tenants/${tid}/workflows` + const name = `空目录模型 ${Date.now()}` + const graph = JSON.stringify({ nodes: [ + { id: 'start', type: 'workflow', position: { x: 0, y: 0 }, data: { kind: 'start', title: '开始' } }, + ], edges: [] }) + const created = await page.request.post(base, { data: { name, graph } }) + expect(created.status()).toBe(201) + const wid = (await created.json()).workflow.id + + await page.reload() + await page.getByRole('complementary', { name: '工作流列表' }).getByRole('button', { name, exact: true }).click() + await page.getByRole('button', { name: 'Agent', exact: true }).click() + const addedNode = page.locator('.react-flow__node').filter({ hasNotText: '开始' }).first() + await expect(addedNode).toBeVisible() + await addedNode.click() + const modelButton = page.getByLabel('Agent 模型', { exact: true }) + await expect(modelButton).toContainText('没有可用 Agent 模型') + await expect(modelButton).not.toContainText(' · ') + await expect(modelButton).not.toContainText('gpt-5') + await page.screenshot({ path: testInfo.outputPath('model-empty-catalog.png'), animations: 'disabled' }) + + await page.getByRole('button', { name: '发布', exact: true }).click() + await expect(page.getByRole('alertdialog', { name: '发布工作流' })).toBeVisible() + const savedAgent = JSON.parse((await (await page.request.get(`${base}/${wid}/draft`)).json()).snapshot.graph).nodes + .find((node: { data: { kind: string } }) => node.data.kind === 'agent') + expect(savedAgent.data.agentConfig.executor).toEqual({ agentCli: '', modelId: '' }) +}) + +test('workflow editor repairs stale model when executor catalog arrives late after workflow open', async ({ page }) => { + let releaseCatalog!: () => void + const catalogReady = new Promise((resolve) => { + releaseCatalog = resolve + }) + await page.route('**/api/v2/tenants/*/chat/executors', async (route) => { + await catalogReady + await route.fulfill({ json: { items: executors } }) + }) + await page.goto('/workflows') + await signIn(page) + const tid = (await (await page.request.get('/api/v1/me/tenants')).json()).items[0].id + const base = `/api/v2/tenants/${tid}/workflows` + const name = `迟到目录 ${Date.now()}` + const graph = JSON.stringify({ nodes: [ + { id: 'start', type: 'workflow', position: { x: 0, y: 0 }, data: { kind: 'start', title: '开始' } }, + { id: 'agent', type: 'workflow', position: { x: 320, y: 0 }, data: { kind: 'agent', title: '模型检查', agentConfig: { executor: { agentCli: agent, modelId: 'deepseek/deepseek-v4-flash' }, prompt: 'hello' } } }, + ], edges: [{ id: 'edge', source: 'start', target: 'agent' }] }) + const created = await page.request.post(base, { data: { name, graph } }) + expect(created.status()).toBe(201) + const wid = (await created.json()).workflow.id + + await page.reload() + await page.getByRole('complementary', { name: '工作流列表' }).getByRole('button', { name, exact: true }).click() + await page.locator('.react-flow__node[data-id="agent"]').click() + const modelButton = page.getByLabel('Agent 模型', { exact: true }) + await expect(modelButton).toContainText('official/ora-space.opencode · deepseek/deepseek-v4-flash') + await expect(page.getByText('未保存', { exact: true })).toBeHidden() + + releaseCatalog() + await expect(modelButton).toContainText('当前模型') + await expect(page.getByText('未保存', { exact: true })).toBeHidden() + await page.getByRole('button', { name: '发布', exact: true }).click() + await expect(page.getByRole('alertdialog', { name: '发布工作流' })).toBeVisible() + const savedAgent = JSON.parse((await (await page.request.get(`${base}/${wid}/draft`)).json()).snapshot.graph).nodes + .find((node: { id: string }) => node.id === 'agent') + expect(savedAgent.data.agentConfig.executor).toEqual({ agentCli: agent, modelId: 'configured' }) +}) diff --git a/frontend-v2/e2e/workflow-retry.spec.ts b/frontend-v2/e2e/workflow-retry.spec.ts index 81e61336..b01313c6 100644 --- a/frontend-v2/e2e/workflow-retry.spec.ts +++ b/frontend-v2/e2e/workflow-retry.spec.ts @@ -229,3 +229,41 @@ test('a wait left under a failed run shows neither a countdown nor a scheduled r await db.end() } }) + +test('retried node exposes injected previous failure context in node details', async ({ page }, testInfo) => { + const { tid, rid } = await createRun(page) + const db = database() + await db.connect() + try { + const previous = randomUUID(), current = randomUUID(), now = Date.now() + const injected = '## 上一次尝试(第 1 次)失败信息\n\n原因: 结构化输出校验失败:缺少 summary 字段' + const firstFailure = { error_detail: { kind: 'structured_output', message: '缺少 summary 字段', source_chain: [], attempt: 1, auto_retryable: true, recorded_at: now } } + const secondFailure = { + error_detail: { kind: 'structured_output', message: '仍然缺少 summary 字段', source_chain: [], attempt: 2, auto_retryable: false, recorded_at: now + 1000 }, + injected_failure_context: injected, + retry_chain: [previous], + } + await db.query(`INSERT INTO v2_workflow_node_runs (tenant_id,run_id,scope_id,node_id,node_type,status,output,started_at,finished_at) + VALUES ($1,$2,$3,'start','start','succeeded','',now(),now())`, [tid, rid, `root:${rid}`]) + await db.query(`INSERT INTO v2_workflow_node_runs (id,tenant_id,run_id,scope_id,node_id,node_type,status,error,payload,started_at,finished_at,deleted_at) + VALUES ($1,$2,$3,$4,'agent','agent','failed','缺少 summary 字段',$5,now(),now(),now())`, [previous, tid, rid, `root:${rid}`, JSON.stringify(firstFailure)]) + await db.query(`INSERT INTO v2_workflow_node_runs (id,tenant_id,run_id,scope_id,node_id,node_type,status,error,payload,started_at,finished_at) + VALUES ($1,$2,$3,$4,'agent','agent','failed','仍然缺少 summary 字段',$5,now(),now())`, [current, tid, rid, `root:${rid}`, JSON.stringify(secondFailure)]) + await db.query(`UPDATE v2_workflow_runs SET status='failed',error='仍然缺少 summary 字段',state='{"current_nodes":["agent"]}',started_at=now(),finished_at=now() WHERE id=$1`, [rid]) + + await page.goto(`/chat?run=${rid}`) + const pane = page.getByRole('region', { name: '工作流运行' }) + await pane.getByRole('button', { name: '全图', exact: true }).click() + await pane.getByLabel('工作流全图俯瞰').getByText('重试任务', { exact: true }).click() + await pane.getByRole('button', { name: '打开节点详情' }).click() + const summary = pane.getByText('本次尝试注入的上次失败信息', { exact: true }).filter({ visible: true }).first() + await expect(summary).toBeVisible() + await summary.click() + const injectedContent = pane.getByText(/缺少 summary 字段/).filter({ visible: true }).first() + await expect(injectedContent).toBeVisible() + await injectedContent.scrollIntoViewIfNeeded() + await page.screenshot({ path: testInfo.outputPath('retry-injected-failure.png'), animations: 'disabled', fullPage: true }) + } finally { + await db.end() + } +}) diff --git a/frontend-v2/e2e/workflow-runs.spec.ts b/frontend-v2/e2e/workflow-runs.spec.ts index 0696ccfe..69eb22a0 100644 --- a/frontend-v2/e2e/workflow-runs.spec.ts +++ b/frontend-v2/e2e/workflow-runs.spec.ts @@ -61,6 +61,10 @@ test('workflow runs: v2-server executes a published workflow started from chat', await page.getByRole('menuitem', { name: new RegExp(name) }).click() const dialog = page.getByRole('dialog', { name: '运行工作流' }) await dialog.getByLabel('运行名称').fill(runName) + const injectLastFailure = dialog.getByRole('checkbox', { name: '节点重跑时把上次失败原因告诉智能体' }) + await expect(injectLastFailure).toBeVisible() + await expect(injectLastFailure).toBeChecked() + await screenshot(page, testInfo.outputPath('run-dialog-inject-default.png')) await dialog.getByRole('button', { name: '创建运行' }).click() const pane = page.getByRole('region', { name: '工作流运行' }) await expect(pane.getByRole('heading', { name: runName })).toBeVisible() @@ -180,6 +184,10 @@ test('workflow runs: a Condition takes one branch and the other shows as inactiv await page.getByRole('menuitem', { name: new RegExp(name) }).click() const dialog = page.getByRole('dialog', { name: '运行工作流' }) await dialog.getByLabel('运行名称').fill(runName) + const injectLastFailure = dialog.getByRole('checkbox', { name: '节点重跑时把上次失败原因告诉智能体' }) + await expect(injectLastFailure).toBeVisible() + await expect(injectLastFailure).toBeChecked() + await screenshot(page, testInfo.outputPath('run-dialog-inject-default.png')) await dialog.getByRole('button', { name: '创建运行' }).click() const pane = page.getByRole('region', { name: '工作流运行' }) await expect(pane.getByRole('heading', { name: runName })).toBeVisible() @@ -220,3 +228,37 @@ test('workflow runs: a Condition takes one branch and the other shows as inactiv await expect(pane.getByLabel('实际执行').filter({ visible: true }).first()).toContainText('· Fixture') await screenshot(page, testInfo.outputPath('s2-branch-agent-details.png')) }) + +test('workflow runs: unchecking injectLastFailure sends false on create and resets on reopen', async ({ page }) => { + await page.goto('/chat') + await signIn(page) + const tenantId = ((await (await page.request.get('/api/v1/me/tenants')).json()) as { items: { id: string }[] }).items[0]!.id + const suffix = Date.now().toString(36) + const name = `失败注入开关 ${suffix}` + const created = await page.request.post(`/api/v2/tenants/${tenantId}/workflows`, { data: { name, graph: chain } }) + expect(created.status()).toBe(201) + const workflowId = ((await created.json()) as { draft: { workflowId: string } }).draft.workflowId + expect((await page.request.post(`/api/v2/tenants/${tenantId}/workflows/${workflowId}/publish`, { data: { version: 'v1' } })).ok()).toBe(true) + + await page.reload() + await page.getByRole('button', { name: '运行工作流', exact: true }).click() + await page.getByRole('menuitem', { name: new RegExp(name) }).click() + const dialog = page.getByRole('dialog', { name: '运行工作流' }) + const checkbox = dialog.getByRole('checkbox', { name: '节点重跑时把上次失败原因告诉智能体' }) + await expect(checkbox).toBeChecked() + await checkbox.uncheck() + await expect(checkbox).not.toBeChecked() + + const createRequest = page.waitForRequest((req) => req.url().includes(`/workflows/${workflowId}/runs`) && req.method() === 'POST') + const createResponse = page.waitForResponse((res) => res.url().includes(`/workflows/${workflowId}/runs`) && res.request().method() === 'POST') + await dialog.getByLabel('运行名称').fill(`关闭注入 ${suffix}`) + await dialog.getByRole('button', { name: '创建运行' }).click() + expect((await createRequest).postDataJSON()).toMatchObject({ injectLastFailure: false }) + const createdRun = (await (await createResponse).json()) as { run: { id: string } } + expect((await page.request.delete(`/api/v2/tenants/${tenantId}/workflow-runs/${createdRun.run.id}`)).ok()).toBe(true) + + await page.getByRole('button', { name: '新建对话', exact: true }).click() + await page.getByRole('button', { name: '运行工作流', exact: true }).click() + await page.getByRole('menuitem', { name: new RegExp(name) }).click() + await expect(page.getByRole('dialog', { name: '运行工作流' }).getByRole('checkbox', { name: '节点重跑时把上次失败原因告诉智能体' })).toBeChecked() +}) diff --git a/frontend-v2/src/features/chat/workflow-run/run-flow.test.tsx b/frontend-v2/src/features/chat/workflow-run/run-flow.test.tsx index 5f35e3f9..c14ced72 100644 --- a/frontend-v2/src/features/chat/workflow-run/run-flow.test.tsx +++ b/frontend-v2/src/features/chat/workflow-run/run-flow.test.tsx @@ -694,4 +694,34 @@ describe('workflow runs in chat', () => { expect(screen.getByRole('heading', { level: 2, name: '梳理 v2 接口缺口' })).toBeInTheDocument() expect(within(project()).getByRole('button', { name: '工作流运行 临时运行(未运行)' })).not.toHaveAttribute('aria-current') }) + + it('sends injectLastFailure true by default, false when unchecked, and resets on reopen', async () => { + const user = userEvent.setup() + await renderPage() + const openDialog = async () => { + await user.click(within(project()).getByRole('button', { name: '在 ora-space/cloud 新建对话' })) + await user.click(await screen.findByRole('menuitem', { name: '运行工作流' })) + fireEvent.click(await screen.findByRole('menuitem', { name: /示例:代码审查/ })) + return screen.findByRole('dialog', { name: '运行工作流' }) + } + + let dialog = await openDialog() + let checkbox = within(dialog).getByRole('checkbox', { name: '节点重跑时把上次失败原因告诉智能体' }) + expect(checkbox).toBeChecked() + await user.click(within(dialog).getByRole('button', { name: '创建运行' })) + await waitFor(() => expect(runs.createdBodies).toHaveLength(1)) + expect(runs.createdBodies[0]?.injectLastFailure).toBe(true) + + dialog = await openDialog() + checkbox = within(dialog).getByRole('checkbox', { name: '节点重跑时把上次失败原因告诉智能体' }) + expect(checkbox).toBeChecked() + await user.click(checkbox) + expect(checkbox).not.toBeChecked() + await user.click(within(dialog).getByRole('button', { name: '创建运行' })) + await waitFor(() => expect(runs.createdBodies).toHaveLength(2)) + expect(runs.createdBodies[1]?.injectLastFailure).toBe(false) + + dialog = await openDialog() + expect(within(dialog).getByRole('checkbox', { name: '节点重跑时把上次失败原因告诉智能体' })).toBeChecked() + }) }) diff --git a/frontend-v2/src/features/chat/workflow-run/run-sidebar.tsx b/frontend-v2/src/features/chat/workflow-run/run-sidebar.tsx index 6f3fcff7..10bcd5ba 100644 --- a/frontend-v2/src/features/chat/workflow-run/run-sidebar.tsx +++ b/frontend-v2/src/features/chat/workflow-run/run-sidebar.tsx @@ -2,6 +2,7 @@ import { MoreHorizontalIcon, SearchIcon, Trash2Icon, WorkflowIcon } from 'lucide import { useQuery, useQueryClient } from '@tanstack/react-query' import { useState } from 'react' import { Button } from '@/components/ui/button' +import { Checkbox } from '@/components/ui/checkbox' import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { DropdownMenu, @@ -25,6 +26,7 @@ import { FadeText } from '../components/fade-text' import { useChatStore } from '../store' import { cancelRun, createRun, deleteRun, listPublishedWorkflows, projectRunsQuery, runKeys, type PublishedWorkflow, type RunListItem } from './server-runs' import { isNodeWorking, runStatusLabel, runStatusTone } from './run-status-style' +import { workflowRunTranslations } from './translations' /** * The "运行工作流" submenu of a project's +, as in desktop: published workflows only, under a @@ -131,58 +133,94 @@ function PublishedWorkflowPicker({ onPick }: { onPick: (workflow: PublishedWorkf */ export function RunWorkflowDialog({ projectId, workflow, onClose }: { projectId: string; workflow?: PublishedWorkflow; onClose: () => void }) { const [busy, setBusy] = useState(false) - const [error, setError] = useState() - const selectRun = useChatStore((s) => s.selectRun) - const tenantId = useChatStore((s) => s.tenantId) - const queryClient = useQueryClient() return ( - !open && !busy && (setError(undefined), onClose())}> + !open && !busy && onClose()}> {workflow && ( -
{ - e.preventDefault() - const name = new FormData(e.currentTarget).get('name') as string - setBusy(true) - try { - if (!tenantId) throw new Error('还没有读到团队,请稍后重试') - const runId = await createRun(createHttpWorkflowContracts(tenantId), tenantId, projectId, workflow, name) - void queryClient.invalidateQueries({ queryKey: runKeys.project(tenantId, projectId) }) - selectRun(runId) - setError(undefined) - onClose() - } catch (err) { - setError(err instanceof ContractError ? localizeContractError(err, workflowI18n.t) : err instanceof Error ? err.message : '创建运行失败') - } finally { - setBusy(false) - } - }} - > - - 运行工作流 - - 在项目里创建「{workflow.name}」{workflow.version} 的一次运行。创建后点「启动」填写输入。 - - -
- - -
- {error &&

{error}

} - - }>取消 - - -
+ )}
) } +function RunWorkflowForm({ + projectId, + workflow, + busy, + onBusyChange, + onClose, +}: { + projectId: string + workflow: PublishedWorkflow + busy: boolean + onBusyChange: (busy: boolean) => void + onClose: () => void +}) { + const [injectLastFailure, setInjectLastFailure] = useState(true) + const [error, setError] = useState() + const selectRun = useChatStore((s) => s.selectRun) + const tenantId = useChatStore((s) => s.tenantId) + const queryClient = useQueryClient() + return ( +
{ + e.preventDefault() + const name = new FormData(e.currentTarget).get('name') as string + onBusyChange(true) + try { + if (!tenantId) throw new Error('还没有读到团队,请稍后重试') + const runId = await createRun(createHttpWorkflowContracts(tenantId), tenantId, projectId, workflow, name, injectLastFailure) + void queryClient.invalidateQueries({ queryKey: runKeys.project(tenantId, projectId) }) + selectRun(runId) + setError(undefined) + onClose() + } catch (err) { + setError(err instanceof ContractError ? localizeContractError(err, workflowI18n.t) : err instanceof Error ? err.message : '创建运行失败') + } finally { + onBusyChange(false) + } + }} + > + + 运行工作流 + + 在项目里创建「{workflow.name}」{workflow.version} 的一次运行。创建后点「启动」填写输入。 + + +
+ + +
+
+ setInjectLastFailure(checked === true)} + /> + +
+ {error &&

{error}

} + + }>取消 + + +
+ ) +} + /** A project's workflow runs, newest first, listed with its sessions as desktop does. */ export function ProjectRuns({ projectId }: { projectId: string }) { const tenantId = useChatStore((s) => s.tenantId) diff --git a/frontend-v2/src/features/chat/workflow-run/server-run-adapter.test.ts b/frontend-v2/src/features/chat/workflow-run/server-run-adapter.test.ts index cfaa61bb..c225d0f8 100644 --- a/frontend-v2/src/features/chat/workflow-run/server-run-adapter.test.ts +++ b/frontend-v2/src/features/chat/workflow-run/server-run-adapter.test.ts @@ -474,4 +474,20 @@ describe('attempts a resume from failure replaced', () => { const adjacent = nodeRun({ id: 'a4', nodeId: 'a', nodeType: 'agent', status: 'failed', payload: JSON.stringify({ error_detail: { kind: 'session', attempt: 4 } }) }) expect(labels([adjacent], [3])).toEqual([[3, 'manual_resume']]) }) + + it('projects payload.injected_failure_context when non-empty and omits otherwise', () => { + const injected = toGraphWorkflowRun( + detail({}, [ + nodeRun({ id: 'a2', nodeId: 'a', nodeType: 'agent', status: 'running', finishedAt: null, payload: JSON.stringify({ injected_failure_context: '## 上一次尝试(第 1 次)失败信息' }) }), + ]), + definition, + ) + expect(injected.nodeStates.a?.injectedFailureContext).toBe('## 上一次尝试(第 1 次)失败信息') + + const empty = toGraphWorkflowRun( + detail({}, [nodeRun({ id: 'a3', nodeId: 'a', nodeType: 'agent', status: 'running', finishedAt: null, payload: JSON.stringify({ injected_failure_context: '' }) })]), + definition, + ) + expect(empty.nodeStates.a?.injectedFailureContext).toBeUndefined() + }) }) diff --git a/frontend-v2/src/features/chat/workflow-run/server-run-adapter.ts b/frontend-v2/src/features/chat/workflow-run/server-run-adapter.ts index f5644010..84cad466 100644 --- a/frontend-v2/src/features/chat/workflow-run/server-run-adapter.ts +++ b/frontend-v2/src/features/chat/workflow-run/server-run-adapter.ts @@ -143,6 +143,9 @@ function nodeState(runId: string, node: WorkflowNodeRun, decisions: Record { +export async function createRun(library: WorkflowContracts, tenantId: string, projectId: string, offered: PublishedWorkflow, name: string, injectLastFailure = true): Promise { if (demoMockEnabled() && offered.id === cdaseWorkflow.id) return createCdaseRun(tenantId, projectId, name) const { published } = await library.get({ workflowId: offered.id }) if (!published) throw new Error('这个工作流已经没有发布版本,请重新选择') @@ -266,7 +266,7 @@ export async function createRun(library: WorkflowContracts, tenantId: string, pr } const trimmed = name.trim() const { run } = await call(() => - createWorkflowRun(tenantId, offered.id, { projectId, snapshotId: offered.snapshotId, ...(trimmed ? { name: trimmed } : {}) }), + createWorkflowRun(tenantId, offered.id, { projectId, snapshotId: offered.snapshotId, ...(trimmed ? { name: trimmed } : {}), injectLastFailure }), ) return run.id } diff --git a/frontend-v2/src/features/chat/workflow-run/test-run-server.ts b/frontend-v2/src/features/chat/workflow-run/test-run-server.ts index 93469a8c..59695e78 100644 --- a/frontend-v2/src/features/chat/workflow-run/test-run-server.ts +++ b/frontend-v2/src/features/chat/workflow-run/test-run-server.ts @@ -9,6 +9,7 @@ import { parseWorkflowGraph } from '@/lib/workflows/runtime' export interface RunServer { runs: Map seen: string[] + createdBodies: Array> /** When set, a started run stops at its first Agent, running, until cancelled. */ hold: boolean /** When set, a resume preview reports nodes still running (v2-server's idle gate). */ @@ -29,7 +30,7 @@ export interface RunServer { */ export function serveRuns(tenantId: string, backend: LocalWorkflowBackend): RunServer { const server: RunServer = { - runs: new Map(), seen: [], hold: false, + runs: new Map(), seen: [], createdBodies: [], hold: false, finishHeld: (runId) => { const entry = server.runs.get(runId)! const held = entry.detail.nodes.find((n) => n.status === 'running')! @@ -144,6 +145,7 @@ export function serveRuns(tenantId: string, backend: LocalWorkflowBackend): RunS if (created && method === 'POST') { const { published } = backend.get({ workflowId: created[1]! }) if (!published || published.id !== body.snapshotId) return refuse(config, 400, 'workflow_no_published_snapshot') + server.createdBodies.push(body) const at = now() const run: WorkflowRun = { id: `run-${++ids}`, workflowId: created[1]!, snapshotId: published.id, projectId: body.projectId as string, diff --git a/frontend-v2/src/features/workflows/editor/workflow-editor.tsx b/frontend-v2/src/features/workflows/editor/workflow-editor.tsx index 21e393c6..97ff9171 100644 --- a/frontend-v2/src/features/workflows/editor/workflow-editor.tsx +++ b/frontend-v2/src/features/workflows/editor/workflow-editor.tsx @@ -70,6 +70,7 @@ import { supportsWorkflowNodeScope, type DemoWorkflow, type MockWorkflowVersion, + type WorkflowAgentModel, type WorkflowCapabilities, type WorkflowGlobalVariable, type WorkflowAnnotationData, @@ -366,6 +367,37 @@ export function WorkflowEditor(props: WorkflowEditorProps = {}) { ); } +/** Keeps the selected CLI stable and only substitutes a discovered model, or fills a blank pair with the first catalog entry. */ +type WorkflowNodeExecutor = { agentCli: string; modelId: string }; + +function repairWorkflowAgentExecutor( + executor: WorkflowNodeExecutor, + availableAgentModels: readonly WorkflowAgentModel[], +): WorkflowNodeExecutor { + if (availableAgentModels.length === 0) { + return executor; + } + const { agentCli, modelId } = executor; + if (agentCli === "" && modelId === "") { + const first = availableAgentModels[0]!; + return { agentCli: first.agentCli, modelId: first.modelId }; + } + if ( + availableAgentModels.some( + (model) => model.agentCli === agentCli && model.modelId === modelId, + ) + ) { + return executor; + } + const modelForCli = availableAgentModels.find( + (model) => model.agentCli === agentCli, + ); + if (modelForCli === undefined) { + return executor; + } + return { agentCli, modelId: modelForCli.modelId }; +} + /** Owns the selected draft graph and publishes library actions to the sidebar list. */ function WorkflowEditorContent({ capabilities: capabilitiesOverride, @@ -414,14 +446,13 @@ function WorkflowEditorContent({ mcps, defaultAgentConfig: { ...baseCapabilities.defaultAgentConfig, - ...(defaultExecutor === undefined - ? {} - : { - executor: { + executor: + defaultExecutor === undefined + ? { agentCli: "", modelId: "" } + : { agentCli: defaultExecutor.agentCli, modelId: defaultExecutor.modelId, }, - }), // Roles are optional; a new agent node starts with no role selected. roleId: "", mcps: [], @@ -495,6 +526,23 @@ function WorkflowEditorContent({ const [hydratedWorkflowId, setHydratedWorkflowId] = useState( null, ); + /** Workflow id whose Agent models have been checked against a non-empty executor catalog. */ + const [modelHydratedWorkflowId, setModelHydratedWorkflowId] = useState< + string | null + >(null); + /** Baseline per-node executors captured on initial hydrate so late catalog repair skips user edits. */ + const [hydratedNodeExecutors, setHydratedNodeExecutors] = useState< + Readonly> + >({}); + /** Set when late catalog arrival repaired an already-mounted workflow, to refresh clean history. */ + const [lateModelRepairedWorkflowId, setLateModelRepairedWorkflowId] = + useState(null); + const clearHydratedWorkflow = useCallback(() => { + setHydratedWorkflowId(null); + setModelHydratedWorkflowId(null); + setHydratedNodeExecutors({}); + setLateModelRepairedWorkflowId(null); + }, []); const [previewedVersion, setPreviewedVersion] = useState(null); const [publishDialogOpen, setPublishDialogOpen] = useState(false); @@ -640,31 +688,33 @@ function WorkflowEditorContent({ ) { return node; } - const { agentCli, modelId } = node.data.agentConfig.executor; + const repaired = repairWorkflowAgentExecutor( + node.data.agentConfig.executor, + availableAgentModels, + ); if ( - availableAgentModels.some( - (model) => model.agentCli === agentCli && model.modelId === modelId, - ) + repaired.agentCli === node.data.agentConfig.executor.agentCli && + repaired.modelId === node.data.agentConfig.executor.modelId ) { return node; } - const modelForCli = availableAgentModels.find( - (model) => model.agentCli === agentCli, - ); return { ...node, data: { ...node.data, agentConfig: { ...node.data.agentConfig, - executor: { - agentCli, - modelId: modelForCli?.modelId ?? modelId, - }, + executor: repaired, }, }, }; }); + const initialExecutors: Record = {}; + for (const node of nodes) { + if (node.data.kind === "agent" && node.data.agentConfig !== undefined) { + initialExecutors[node.id] = node.data.agentConfig.executor; + } + } const hydratedWorkflow: DemoWorkflow = { id: draftQuery.data.workflow.id, name: draftQuery.data.workflow.name, @@ -680,6 +730,13 @@ function WorkflowEditorContent({ }; setWorkflow(hydratedWorkflow); setHydratedWorkflowId(draftQuery.data.workflow.id); + setHydratedNodeExecutors(initialExecutors); + setModelHydratedWorkflowId( + capabilitiesOverride !== undefined || availableAgentModels.length > 0 + ? draftQuery.data.workflow.id + : null, + ); + setLateModelRepairedWorkflowId(null); // Capture the server name in the same hydrate turn so autosave can skip no-op renames. // eslint-disable-next-line react-hooks/refs -- render-phase hydrate pairs this with setState persistedNameRef.current = draftQuery.data.workflow.name; @@ -689,6 +746,60 @@ function WorkflowEditorContent({ droppedNodesRef.current = parsed.droppedNodeCount > 0 ? parsed : null; } + // Reconcile untouched or blank Agent executors once a late/retried catalog arrives, without + // marking the draft dirty or overwriting an executor the user already picked. + if ( + workflow !== null && + hydratedWorkflowId === resolvedWorkflowId && + modelHydratedWorkflowId !== resolvedWorkflowId && + capabilitiesOverride === undefined && + availableAgentModels.length > 0 + ) { + const nextNodes = workflow.nodes.map((node) => { + if (node.data.kind !== "agent" || node.data.agentConfig === undefined) { + return node; + } + const currentExecutor = node.data.agentConfig.executor; + const hydratedExecutor = hydratedNodeExecutors[node.id]; + const isBlank = + currentExecutor.agentCli === "" && currentExecutor.modelId === ""; + const matchesHydrated = + hydratedExecutor !== undefined && + hydratedExecutor.agentCli === currentExecutor.agentCli && + hydratedExecutor.modelId === currentExecutor.modelId; + if (!isBlank && !matchesHydrated) { + return node; + } + const repairedExecutor = repairWorkflowAgentExecutor( + currentExecutor, + availableAgentModels, + ); + if ( + repairedExecutor.agentCli === currentExecutor.agentCli && + repairedExecutor.modelId === currentExecutor.modelId + ) { + return node; + } + return { + ...node, + data: { + ...node.data, + agentConfig: { + ...node.data.agentConfig, + executor: repairedExecutor, + }, + }, + }; + }); + const repairedAny = nextNodes.some((node, index) => node !== workflow.nodes[index]); + if (repairedAny) { + setWorkflow({ ...workflow, nodes: nextNodes }); + setLateModelRepairedWorkflowId(resolvedWorkflowId); + } + setModelHydratedWorkflowId(resolvedWorkflowId); + } + + const historyHydrateEditGenerationRef = useRef(null); // History belongs to the mounted draft session, so a workflow switch or // activation starts at a clean baseline while ordinary edits keep the stack. useEffect(() => { @@ -698,10 +809,29 @@ function WorkflowEditorContent({ hydratedWorkflowId === resolvedWorkflowId && workflowRef.current !== null ) { + historyHydrateEditGenerationRef.current = editGenerationRef.current; resetWorkflowHistory(workflowRef.current); } }, [hydratedWorkflowId, resetWorkflowHistory, resolvedWorkflowId]); + useEffect(() => { + if ( + hydratedWorkflowId !== resolvedWorkflowId || + lateModelRepairedWorkflowId !== resolvedWorkflowId || + workflowRef.current === null + ) { + return; + } + if (historyHydrateEditGenerationRef.current === editGenerationRef.current) { + resetWorkflowHistory(workflowRef.current); + } + }, [ + hydratedWorkflowId, + lateModelRepairedWorkflowId, + resetWorkflowHistory, + resolvedWorkflowId, + ]); + // Explain the nodes the hydrate had to drop, once the draft carrying them has committed. // Staying silent would be worse than the loss itself: the dropped nodes leave the draft on // the next autosave, so the user needs to hear about them while the original graph is still @@ -1208,7 +1338,7 @@ function WorkflowEditorContent({ // Unmount the left draft now rather than when the next one hydrates, so the canvas // never shows (and edits) the previous workflow under the new selection. setPreviewedVersion(null); - setHydratedWorkflowId(null); + clearHydratedWorkflow(); setWorkflow(null); setSelectedWorkflowId(workflowId); setSwitchingWorkflow(false); @@ -1231,7 +1361,7 @@ function WorkflowEditorContent({ // when it returns, does not overwrite this selection. workflowSwitchGeneration.current += 1; setPreviewedVersion(null); - setHydratedWorkflowId(null); + clearHydratedWorkflow(); setWorkflow(null); setSelectedWorkflowId(result.workflow.id); return true; @@ -1307,7 +1437,7 @@ function WorkflowEditorContent({ await deleteWorkflowMutation.mutateAsync(workflowId); if (resolvedWorkflowId === workflowId) { setSelectedWorkflowId(null); - setHydratedWorkflowId(null); + clearHydratedWorkflow(); setWorkflow(null); } toast.success( @@ -1455,7 +1585,7 @@ function WorkflowEditorContent({ autosave.cancel(); workflowSwitchGeneration.current += 1; setPreviewedVersion(null); - setHydratedWorkflowId(null); + clearHydratedWorkflow(); setWorkflow(null); setSelectedWorkflowId(result.workflow.id); setImportState(null); @@ -1628,7 +1758,7 @@ function WorkflowEditorContent({ }); // Discard any pre-activate dirty flag; the synced draft remounts next. autosave.cancel(); - setHydratedWorkflowId(null); + clearHydratedWorkflow(); toast.success( t("settings.workflow.activateVersionSuccess", { version: version.version, diff --git a/frontend-v2/src/features/workflows/editor/workflow-inspector.test.tsx b/frontend-v2/src/features/workflows/editor/workflow-inspector.test.tsx index b70cdf54..94e0389a 100644 --- a/frontend-v2/src/features/workflows/editor/workflow-inspector.test.tsx +++ b/frontend-v2/src/features/workflows/editor/workflow-inspector.test.tsx @@ -225,11 +225,13 @@ function StatefulInspectorHarness({ }, ], unappliedSettings, + agentModelsLoading, }: { node: Node; capabilities: ReturnType; variableCatalog?: WorkflowVariableCatalogEntry[]; unappliedSettings?: readonly string[]; + agentModelsLoading?: boolean; }) { const [current, setCurrent] = useState(node); return ( @@ -245,6 +247,7 @@ function StatefulInspectorHarness({ onDelete={() => undefined} onCloseNode={() => undefined} unappliedSettings={unappliedSettings} + agentModelsLoading={agentModelsLoading} /> @@ -952,6 +955,40 @@ describe("WorkflowInspector unapplied-setting hints", () => { expect(screen.getByTestId("workflow-unapplied-interactive")).toHaveTextContent("暂不生效"); expect(screen.queryByTestId("workflow-interactive-partial")).toBeNull(); }); + + it("shows localized empty/loading placeholder when executor fields are blank", async () => { + await appI18n.changeLanguage("zh-CN"); + const agent = createAgentNode(); + const blankNode: Node = { + ...agent, + data: { + ...agent.data, + agentConfig: { + ...agent.data.agentConfig!, + executor: { agentCli: "", modelId: "" }, + }, + }, + }; + const emptyCapabilities = createMockWorkflowCapabilities("zh-CN", []); + const { rerender } = render( + , + ); + expect(screen.getByLabelText("Agent 模型")).toHaveTextContent("加载中…"); + expect(screen.getByLabelText("Agent 模型")).not.toHaveTextContent("·"); + + rerender( + , + ); + expect(screen.getByLabelText("Agent 模型")).toHaveTextContent("没有可用 Agent 模型"); + expect(screen.queryByText("请选择当前可用的 Agent 和模型。")).not.toBeInTheDocument(); + }); }); /** Serves one `/auth/providers` answer for the deployment-mode query. */ diff --git a/frontend-v2/src/features/workflows/editor/workflow-inspector.tsx b/frontend-v2/src/features/workflows/editor/workflow-inspector.tsx index a63fd705..ebf84390 100644 --- a/frontend-v2/src/features/workflows/editor/workflow-inspector.tsx +++ b/frontend-v2/src/features/workflows/editor/workflow-inspector.tsx @@ -488,10 +488,16 @@ function AgentConfigurationFields({ model.agentCli === config.executor.agentCli && model.modelId === config.executor.modelId, ); + const emptyExecutor = + config.executor.agentCli === "" && config.executor.modelId === ""; const selectedModel = configuredModel ?? { agentCli: config.executor.agentCli, modelId: config.executor.modelId, - label: `${agentLabel(offeredAgents, config.executor.agentCli)} · ${config.executor.modelId}`, + label: emptyExecutor + ? modelsLoading + ? t("chat.modelSelector.loading") + : t("settings.workflow.noAvailableAgentModels") + : `${agentLabel(offeredAgents, config.executor.agentCli)} · ${config.executor.modelId}`, }; const modelsForSelectedCli = modelsByCli?.get(currentAgentCli) ?? @@ -738,7 +744,10 @@ function AgentConfigurationFields({ - {!modelsLoading && !modelsError && configuredModel === undefined && ( + {!modelsLoading && + !modelsError && + configuredModel === undefined && + !(emptyExecutor && capabilities.agentModels.length === 0) && (

{t("settings.workflow.selectAvailableAgentModel")}

diff --git a/frontend-v2/src/features/workflows/editor/workflow-model-hydration.test.tsx b/frontend-v2/src/features/workflows/editor/workflow-model-hydration.test.tsx index 487970b2..1709c5bd 100644 --- a/frontend-v2/src/features/workflows/editor/workflow-model-hydration.test.tsx +++ b/frontend-v2/src/features/workflows/editor/workflow-model-hydration.test.tsx @@ -18,13 +18,21 @@ import { useWorkflowEditorStore } from "./workflow-editor-store"; const tenantId = "model-hydration"; const agent = "official/ora-space.opencode"; const catalog = [ - { id: "other-agent", agentId: "another-agent", agentName: "Other Agent", modelId: "foreign", modelName: "Foreign" }, - { id: "first", agentId: agent, agentName: "OpenCode", modelId: "configured", modelName: "Current" }, - { id: "second", agentId: agent, agentName: "OpenCode", modelId: "second", modelName: "Second" }, + { id: "other-agent", name: "Other Agent · Foreign", agentId: "another-agent", agentName: "Other Agent", modelId: "foreign", modelName: "Foreign" }, + { id: "first", name: "OpenCode · Current", agentId: agent, agentName: "OpenCode", modelId: "configured", modelName: "Current" }, + { id: "second", name: "OpenCode · Second", agentId: agent, agentName: "OpenCode", modelId: "second", modelName: "Second" }, ]; -/** Mounts the real editor with a catalog already available when the draft hydrates. */ -async function openEditor(modelId: string, options: { models?: typeof catalog; agentCli?: string; override?: boolean } = {}) { +/** Mounts the real editor with an optional pre-seeded or deferred executor catalog. */ +async function openEditor( + modelId: string, + options: { + models?: typeof catalog | null; + agentCli?: string; + override?: boolean; + onList?: (client: ReturnType) => void; + } = {}, +) { fakeBackend(() => undefined); const backend = new LocalWorkflowBackend({ storage: memoryWorkflowStorage() }); const initialExecutor = { agentCli: options.agentCli ?? agent, modelId }; @@ -40,8 +48,13 @@ async function openEditor(modelId: string, options: { models?: typeof catalog; a const workflowId = created.workflow.id; const update = vi.spyOn(backend, "updateDraft"); const client = createContractsClient(localWorkflowContracts(backend), tenantId); + if (options.models === null && options.onList) { + options.onList(client); + } const queries = new QueryClient({ defaultOptions: { queries: { retry: false, staleTime: Infinity } } }); - queries.setQueryData(["workflow-catalog", "executors", tenantId], { items: options.models ?? catalog }); + if (options.models !== null) { + queries.setQueryData(["workflow-catalog", "executors", tenantId], { items: options.models ?? catalog }); + } useWorkflowEditorStore.setState({ selectedWorkflowId: workflowId, managerError: null }); render( @@ -52,7 +65,7 @@ async function openEditor(modelId: string, options: { models?: typeof catalog; a await screen.findByLabelText("工作流画布"); const savedExecutor = () => JSON.parse(backend.getDraft({ workflowId }).snapshot.graph).nodes .find((node: { id: string }) => node.id === "agent").data.agentConfig.executor; - return { backend, workflowId, update, initialExecutor, savedExecutor }; + return { backend, workflowId, queries, client, update, initialExecutor, savedExecutor }; } describe("editor model hydration", () => { @@ -76,15 +89,91 @@ describe("editor model hydration", () => { expect(state.savedExecutor()).toEqual({ agentCli: options.agentCli ?? agent, modelId: expected }); }); - it("uses desktop's nonblank base executor for a new Agent when the catalog is empty", async () => { - const state = await openEditor("stale", { models: [] }); + it("re-runs stale model hydration when the catalog arrives late without dirtying the clean draft", async () => { + const state = await openEditor("deepseek/deepseek-v4-flash", { models: null }); + expect(screen.queryByText("未保存")).not.toBeInTheDocument(); + expect(state.update).not.toHaveBeenCalled(); + + state.queries.setQueryData(["workflow-catalog", "executors", tenantId], { items: catalog }); + await waitFor(() => expect(screen.queryByText("未保存")).not.toBeInTheDocument()); + expect(state.update).not.toHaveBeenCalled(); + expect(state.savedExecutor()).toEqual(state.initialExecutor); + + fireEvent.click(screen.getByRole("button", { name: "发布" })); + await screen.findByRole("alertdialog", { name: "发布工作流" }); + await waitFor(() => expect(state.update).toHaveBeenCalledOnce()); + expect(state.savedExecutor()).toEqual({ agentCli: agent, modelId: "configured" }); + }); + + it("defaults a new Agent node to the catalog's first model", async () => { + const state = await openEditor("configured"); fireEvent.click(screen.getByRole("button", { name: "Agent" })); expect(await screen.findByText("未保存")).toBeInTheDocument(); fireEvent.click(screen.getByRole("button", { name: "发布" })); await screen.findByRole("alertdialog", { name: "发布工作流" }); + await waitFor(() => expect(state.update).toHaveBeenCalled()); const nodes = JSON.parse(state.backend.getDraft({ workflowId: state.workflowId }).snapshot.graph).nodes; const added = nodes.find((node: { id: string; data: { kind: string } }) => node.data.kind === "agent" && node.id !== "agent"); - expect(added.data.agentConfig.executor).toEqual(createMockWorkflowCapabilities("zh-CN").defaultAgentConfig.executor); - expect(added.data.agentConfig.executor.modelId).not.toBe(""); + expect(added.data.agentConfig.executor).toEqual({ agentCli: catalog[0]!.agentId, modelId: catalog[0]!.modelId }); + }); + + it("leaves a new Agent executor blank when the catalog is empty and fills it once the catalog loads", async () => { + const state = await openEditor("stale", { models: [] }); + fireEvent.click(screen.getByRole("button", { name: "Agent" })); + expect(await screen.findByText("未保存")).toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "发布" })); + await screen.findByRole("alertdialog", { name: "发布工作流" }); + await waitFor(() => expect(state.update).toHaveBeenCalledTimes(1)); + let nodes = JSON.parse(state.backend.getDraft({ workflowId: state.workflowId }).snapshot.graph).nodes; + let added = nodes.find((node: { id: string; data: { kind: string } }) => node.data.kind === "agent" && node.id !== "agent"); + expect(added.data.agentConfig.executor).toEqual({ agentCli: "", modelId: "" }); + + fireEvent.click(screen.getByRole("button", { name: "取消" })); + state.queries.setQueryData(["workflow-catalog", "executors", tenantId], { items: catalog }); + await waitFor(() => expect(screen.getAllByText(/another-agent · foreign|Foreign/).length).toBeGreaterThan(0)); + fireEvent.click(screen.getByRole("button", { name: "发布" })); + await screen.findByRole("alertdialog", { name: "发布工作流" }); + await waitFor(() => expect(state.update).toHaveBeenCalledTimes(2)); + nodes = JSON.parse(state.backend.getDraft({ workflowId: state.workflowId }).snapshot.graph).nodes; + added = nodes.find((node: { id: string; data: { kind: string } }) => node.data.kind === "agent" && node.id !== "agent"); + expect(added.data.agentConfig.executor).toEqual({ agentCli: catalog[0]!.agentId, modelId: catalog[0]!.modelId }); + }); + + it("re-runs stale model hydration after an initial executor catalog error recovers", async () => { + let listSpy: ReturnType | undefined; + const state = await openEditor("deepseek/deepseek-v4-flash", { + models: null, + onList: (client) => { + listSpy = vi + .spyOn(client.executors, "list") + .mockRejectedValueOnce(new Error("executors unavailable")) + .mockResolvedValueOnce({ items: catalog }); + }, + }); + await waitFor(() => expect(listSpy).toHaveBeenCalledTimes(1)); + expect(screen.queryByText("未保存")).not.toBeInTheDocument(); + expect(state.update).not.toHaveBeenCalled(); + + await state.queries.refetchQueries({ queryKey: ["workflow-catalog", "executors", tenantId] }); + await waitFor(() => expect(listSpy).toHaveBeenCalledTimes(2)); + expect(screen.queryByText("未保存")).not.toBeInTheDocument(); + expect(state.update).not.toHaveBeenCalled(); + + fireEvent.click(screen.getByRole("button", { name: "发布" })); + await screen.findByRole("alertdialog", { name: "发布工作流" }); + await waitFor(() => expect(state.update).toHaveBeenCalledOnce()); + expect(state.savedExecutor()).toEqual({ agentCli: agent, modelId: "configured" }); + }); + + it("keeps prior edits and undo history intact when a late catalog repairs an untouched node", async () => { + const state = await openEditor("deepseek/deepseek-v4-flash", { models: null }); + fireEvent.click(screen.getByRole("button", { name: "Agent" })); + expect(await screen.findByText("未保存")).toBeInTheDocument(); + expect(screen.getByRole("button", { name: "撤销" })).toBeEnabled(); + + state.queries.setQueryData(["workflow-catalog", "executors", tenantId], { items: catalog }); + await waitFor(() => expect(screen.getAllByText(/another-agent · foreign|Foreign/).length).toBeGreaterThan(0)); + expect(screen.getByRole("button", { name: "撤销" })).toBeEnabled(); + expect(screen.getByText("未保存")).toBeInTheDocument(); }); }); diff --git a/frontend-v2/src/lib/workflows/api/generated.ts b/frontend-v2/src/lib/workflows/api/generated.ts index fd5b79c1..c7912f20 100644 --- a/frontend-v2/src/lib/workflows/api/generated.ts +++ b/frontend-v2/src/lib/workflows/api/generated.ts @@ -384,6 +384,7 @@ export interface WorkflowRunCreate { name?: string; triggerKind?: WorkflowRunCreateTriggerKind; triggerRef?: string; + injectLastFailure?: boolean; } export interface WorkflowRunCreated { diff --git a/v2-server/internal/nodeservices/limits_test.go b/v2-server/internal/nodeservices/limits_test.go index 6a6696e7..2066c176 100644 --- a/v2-server/internal/nodeservices/limits_test.go +++ b/v2-server/internal/nodeservices/limits_test.go @@ -1,12 +1,14 @@ package nodeservices import ( + "context" "errors" "fmt" "io" "net/http" "net/http/httptest" "strings" + "sync" "testing" "time" ) @@ -91,36 +93,188 @@ func readCutOff(t *testing.T, p Provider) (string, time.Duration) { return string(data), time.Since(began) } +// fakeTimer replaces wall-clock sleeps for deterministic limit tests. +type fakeTimer struct { + mu sync.Mutex + stopped bool + resets []time.Duration + notify chan struct{} + fn func() +} + +func newFakeTimer(fn func()) *fakeTimer { + return &fakeTimer{notify: make(chan struct{}, 1), fn: fn} +} + +func (f *fakeTimer) Stop() bool { + f.mu.Lock() + defer f.mu.Unlock() + wasActive := !f.stopped + f.stopped = true + return wasActive +} + +func (f *fakeTimer) Reset(d time.Duration) bool { + f.mu.Lock() + wasActive := !f.stopped + f.stopped = false + f.resets = append(f.resets, d) + f.mu.Unlock() + select { + case f.notify <- struct{}{}: + default: + } + return wasActive +} + +func (f *fakeTimer) fire() { + f.mu.Lock() + if f.stopped { + f.mu.Unlock() + return + } + f.stopped = true + fn := f.fn + f.mu.Unlock() + fn() +} + +func (f *fakeTimer) resetCount() int { + f.mu.Lock() + defer f.mu.Unlock() + return len(f.resets) +} + +func (f *fakeTimer) waitResets(ctx context.Context, want int) bool { + for { + if f.resetCount() >= want { + return true + } + select { + case <-f.notify: + case <-ctx.Done(): + return false + } + } +} + func TestRemoteProviderIdleTimeoutAbortsResponse(t *testing.T) { - stopped := make(chan struct{}, 2) + const idleLimit = 300 * time.Millisecond + const totalLimit = time.Minute + for _, name := range []string{"direct", "wrapped"} { + t.Run(name, func(t *testing.T) { + stopped := make(chan struct{}, 1) + var mu sync.Mutex + var idleTimer *fakeTimer + idleReady := make(chan *fakeTimer, 1) + chunksDelivered := make(chan struct{}) + + upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + var timer *fakeTimer + select { + case timer = <-idleReady: + case <-r.Context().Done(): + return + } + for i := range 3 { + _, _ = fmt.Fprintf(flushWriter{w}, "data: {\"n\":%d}\n\n", i) + if !timer.waitResets(r.Context(), i+1) { + return + } + } + close(chunksDelivered) + <-r.Context().Done() + stopped <- struct{}{} + })) + defer upstream.Close() + + p, err := NewRemoteProvider(upstream.URL, "backend-key", upstream.Client().Transport) + if err != nil { + t.Fatal(err) + } + if err := p.SetLimits(idleLimit, totalLimit); err != nil { + t.Fatal(err) + } + p.afterFunc = func(d time.Duration, fn func()) limitTimer { + timer := newFakeTimer(fn) + if d == idleLimit { + mu.Lock() + idleTimer = timer + mu.Unlock() + idleReady <- timer + } + return timer + } + + ctx := t.Context() + go func() { + select { + case <-chunksDelivered: + mu.Lock() + timer := idleTimer + mu.Unlock() + timer.fire() + case <-ctx.Done(): + } + }() + + var provider Provider = p + if name == "wrapped" { + provider = unwrapping{p} + } + data, elapsed := readCutOff(t, provider) + if !strings.Contains(data, `{"n":2}`) || elapsed > 5*time.Second { + t.Fatalf("%s: idle cut after %s with %q", name, elapsed, data) + } + select { + case <-stopped: + case <-time.After(5 * time.Second): + t.Fatalf("%s: upstream call not cancelled", name) + } + }) + } +} + +// Even when an HTTP/1.1 TLS upstream responds to cancellation by returning cleanly (emitting a +// chunked EOF before the client socket finishes closing), CompleteObserved must still mark the +// response cut rather than reporting normal completion. +func TestRemoteProviderCancelledStreamWithCleanUpstreamEOFStillAborts(t *testing.T) { upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") - for i := range 3 { - _, _ = fmt.Fprintf(flushWriter{w}, "data: {\"n\":%d}\n\n", i) - time.Sleep(100 * time.Millisecond) - } + _, _ = io.WriteString(flushWriter{w}, "data: {\"n\":0}\n\n") <-r.Context().Done() - stopped <- struct{}{} })) defer upstream.Close() p, err := NewRemoteProvider(upstream.URL, "backend-key", upstream.Client().Transport) if err != nil { t.Fatal(err) } - // Chunks 100ms apart stay under the idle limit; the stall after them does not. - if err := p.SetLimits(300*time.Millisecond, time.Minute); err != nil { + const idleLimit = 300 * time.Millisecond + if err := p.SetLimits(idleLimit, time.Minute); err != nil { t.Fatal(err) } - for name, provider := range map[string]Provider{"direct": p, "wrapped": unwrapping{p}} { - data, elapsed := readCutOff(t, provider) - if !strings.Contains(data, `{"n":2}`) || elapsed > 5*time.Second { - t.Fatalf("%s: idle cut after %s with %q", name, elapsed, data) + idleReady := make(chan *fakeTimer, 1) + p.afterFunc = func(d time.Duration, fn func()) limitTimer { + timer := newFakeTimer(fn) + if d == idleLimit { + idleReady <- timer } + return timer + } + ctx := t.Context() + go func() { select { - case <-stopped: - case <-time.After(5 * time.Second): - t.Fatalf("%s: upstream call not cancelled", name) + case timer := <-idleReady: + if timer.waitResets(ctx, 1) { + timer.fire() + } + case <-ctx.Done(): } + }() + data, _ := readCutOff(t, p) + if !strings.Contains(data, `{"n":0}`) { + t.Fatalf("missing first chunk: %q", data) } } diff --git a/v2-server/internal/nodeservices/provider.go b/v2-server/internal/nodeservices/provider.go index c434b351..b9f51dfd 100644 --- a/v2-server/internal/nodeservices/provider.go +++ b/v2-server/internal/nodeservices/provider.go @@ -22,6 +22,19 @@ type RemoteProvider struct { protocol Protocol client *http.Client idle, total time.Duration + afterFunc func(time.Duration, func()) limitTimer +} + +type limitTimer interface { + Stop() bool + Reset(time.Duration) bool +} + +func (p *RemoteProvider) startTimer(d time.Duration, fn func()) limitTimer { + if p.afterFunc != nil { + return p.afterFunc(d, fn) + } + return time.AfterFunc(d, fn) } // Default limits of one upstream call. A call is cut off when no upstream data arrives for the @@ -135,9 +148,9 @@ func (p *RemoteProvider) CompleteObserved(w http.ResponseWriter, r *http.Request return 0, false } // The idle timer covers the wait for the response header too and restarts on every chunk. - total := time.AfterFunc(p.total, func() { cancel(errMaxDuration) }) + total := p.startTimer(p.total, func() { cancel(errMaxDuration) }) defer total.Stop() - idle := time.AfterFunc(p.idle, func() { cancel(errIdleTimeout) }) + idle := p.startTimer(p.idle, func() { cancel(errIdleTimeout) }) defer idle.Stop() resp, err := p.client.Do(req) if err != nil { @@ -164,6 +177,8 @@ func (p *RemoteProvider) CompleteObserved(w http.ResponseWriter, r *http.Request w.Header().Set("Cache-Control", "no-store") w.WriteHeader(resp.StatusCode) _, err = io.Copy(flushWriter{w}, idleReader{resp.Body, idle, p.idle}) + idle.Stop() + total.Stop() if err == nil { // net/http passes a body's io.EOF through even after the request was cancelled, so a stream // a limit or the caller cut off can still end cleanly here: it only completed if nothing @@ -188,7 +203,7 @@ func (p *RemoteProvider) CompleteObserved(w http.ResponseWriter, r *http.Request // idleReader restarts the idle timer whenever upstream data arrives. type idleReader struct { r io.Reader - timer *time.Timer + timer limitTimer idle time.Duration } diff --git a/v2-server/internal/server/workflow_runs.go b/v2-server/internal/server/workflow_runs.go index e1b08a09..4ca7f855 100644 --- a/v2-server/internal/server/workflow_runs.go +++ b/v2-server/internal/server/workflow_runs.go @@ -211,12 +211,13 @@ func (h *handler) createWorkflowRun(w http.ResponseWriter, r *http.Request, a ac return } var in struct { - ProjectID string `json:"projectId"` - SnapshotID string `json:"snapshotId"` - Input *string `json:"input"` - Name string `json:"name"` - TriggerKind string `json:"triggerKind"` - TriggerRef string `json:"triggerRef"` + ProjectID string `json:"projectId"` + SnapshotID string `json:"snapshotId"` + Input *string `json:"input"` + Name string `json:"name"` + TriggerKind string `json:"triggerKind"` + TriggerRef string `json:"triggerRef"` + InjectLastFailure *bool `json:"injectLastFailure"` } if !decodeWorkflow(w, r, &in) { return @@ -234,11 +235,12 @@ func (h *handler) createWorkflowRun(w http.ResponseWriter, r *http.Request, a ac return } run, err := s.CreateWorkflowRun(r.Context(), tenant, id, in.SnapshotID, store.WorkflowRunInput{ - ProjectID: in.ProjectID, - Name: strings.TrimSpace(in.Name), - Input: in.Input, - TriggerKind: in.TriggerKind, - TriggerRef: in.TriggerRef, + ProjectID: in.ProjectID, + Name: strings.TrimSpace(in.Name), + Input: in.Input, + TriggerKind: in.TriggerKind, + TriggerRef: in.TriggerRef, + InjectLastFailure: in.InjectLastFailure, }) if err != nil { workflowRunFault(w, err, "create workflow run") diff --git a/v2-server/internal/server/workflow_runs_test.go b/v2-server/internal/server/workflow_runs_test.go index 7510f730..35f92d94 100644 --- a/v2-server/internal/server/workflow_runs_test.go +++ b/v2-server/internal/server/workflow_runs_test.go @@ -6,6 +6,7 @@ import ( "github.com/wanglongan587/cloud/v2-server/internal/execution" "github.com/wanglongan587/cloud/v2-server/internal/store" + wf "github.com/wanglongan587/cloud/v2-server/internal/workflow" ) func workflowRunProject(t *testing.T, s *store.Store, user, tenant string) store.Project { @@ -217,4 +218,18 @@ func TestWorkflowRunsOverHTTP(t *testing.T) { if status, deletedWorkflow := call("DELETE", base+"/"+secondID, nil); status != 200 || deletedWorkflow["workflowId"] != secondID { t.Fatalf("delete released workflow = %d %v", status, deletedWorkflow) } + + // injectLastFailure=false persists into the pre-start run payload while omitted/true keeps it on. + status, noInject := call("POST", base+"/"+id+"/runs", map[string]any{"projectId": project.ID, "injectLastFailure": false}) + if status != 201 { + t.Fatalf("create run with injectLastFailure=false = %d %v", status, noInject) + } + noInjectID := noInject["run"].(map[string]any)["id"].(string) + storedNoInject, err := s.WorkflowRun(ctx, tid, noInjectID) + if err != nil { + t.Fatal(err) + } + if p, ok, err := wf.DecodeRunPayload(storedNoInject.RuntimePayload); err != nil || !ok || p.InjectLastFailure { + t.Fatalf("decoded injectLastFailure=false payload = %+v ok=%v err=%v", p, ok, err) + } } diff --git a/v2-server/internal/store/workflow_last_failed_attempt_test.go b/v2-server/internal/store/workflow_last_failed_attempt_test.go new file mode 100644 index 00000000..4e5e22a2 --- /dev/null +++ b/v2-server/internal/store/workflow_last_failed_attempt_test.go @@ -0,0 +1,134 @@ +package store_test + +import ( + "context" + "testing" + + "github.com/wanglongan587/cloud/v2-server/internal/store" + "github.com/wanglongan587/cloud/v2-server/internal/store/storetest" +) + +// failedAndReplaced creates one started attempt of (nodeID, iteration) and retries it: the attempt +// becomes a soft-deleted failure and the retry waits to start (started_at NULL), the history +// desktop reads when it injects the previous failure. It returns both rows. +func failedAndReplaced(t *testing.T, s *store.Store, tenant, runID, nodeID string, iteration *int) (failed, next store.WorkflowNodeRun) { + t.Helper() + ctx := context.Background() + failed, err := s.CreateWorkflowNodeRun(ctx, tenant, runID, store.WorkflowNodeRunInput{ + NodeID: nodeID, NodeType: "agent", Iteration: iteration, + }) + if err != nil { + t.Fatal(err) + } + if failed.StartedAt == nil { + t.Fatalf("a dispatched attempt starts: %+v", failed) + } + next, err = s.RetryWorkflowNodeRun(ctx, tenant, runID, failed.ID, store.WorkflowNodeRetry{ + Failure: store.WorkflowNodeFailure{Error: "失败原因"}, + NextPayload: []byte(`{}`), + }) + if err != nil { + t.Fatal(err) + } + if next.StartedAt != nil { + t.Fatalf("a retry waits for its wake: %+v", next) + } + // Check the replacement really left a soft-deleted failure behind, so a green finder below is + // not green because there was nothing to find. + attempts, err := s.WorkflowRunFailedAttempts(ctx, tenant, runID) + if err != nil { + t.Fatal(err) + } + for _, attempt := range attempts { + if attempt.ID == failed.ID { + if attempt.Status != store.WorkflowStatusFailed || attempt.StartedAt == nil { + t.Fatalf("failed attempt = %+v", attempt) + } + return attempt, next + } + } + t.Fatalf("attempt %s was not recorded as failed", failed.ID) + return store.WorkflowNodeRun{}, store.WorkflowNodeRun{} +} + +func findLastFailed(t *testing.T, s *store.Store, tenant, runID, nodeID string, iteration *int) *store.WorkflowNodeRun { + t.Helper() + found, err := s.FindLastFailedWorkflowNodeAttempt(context.Background(), tenant, runID, nodeID, iteration) + if err != nil { + t.Fatal(err) + } + return found +} + +// A retry that succeeded recovered the failure: the retry's success still counts after a resume or +// restart soft-deleted it, so a later attempt of the same node must not inherit the old failure. +// Without the succeeded-attempt guard the soft-deleted failure below is returned instead. +func TestLastFailedNodeAttemptIgnoresAFailureARetryRecovered(t *testing.T) { + s := storetest.Open(t) + user, tenant := storetest.Login(t, s, "last-failed-recovered") + ctx := context.Background() + run, _ := startedRun(t, s, user, tenant, "重试成功") + failed, next := failedAndReplaced(t, s, tenant, run.ID, "agent-recovered", nil) + + if _, err := s.WakeWorkflowNodeRun(ctx, tenant, run.ID, next.ID, nil); err != nil { + t.Fatal(err) + } + answer := "修好了" + if _, err := s.CompleteWorkflowNodeRun(ctx, tenant, run.ID, next.ID, store.WorkflowNodeResult{Output: &answer}); err != nil { + t.Fatal(err) + } + + if found := findLastFailed(t, s, tenant, run.ID, "agent-recovered", nil); found != nil { + t.Fatalf("recovered failure %s still offered as the previous failure: %+v", failed.ID, found) + } +} + +// Round 1 of a Loop must not inherit round 0's failure: the finder matches the iteration exactly, +// including "no iteration at all" for a node outside any round. +func TestLastFailedNodeAttemptIsScopedToItsIteration(t *testing.T) { + s := storetest.Open(t) + user, tenant := storetest.Login(t, s, "last-failed-iteration") + run, _ := startedRun(t, s, user, tenant, "轮次") + round := 0 + failed, _ := failedAndReplaced(t, s, tenant, run.ID, "agent-round", &round) + other := 1 + + if found := findLastFailed(t, s, tenant, run.ID, "agent-round", &other); found != nil { + t.Fatalf("round 1 inherited round 0's failure: %+v", found) + } + if found := findLastFailed(t, s, tenant, run.ID, "agent-round", nil); found != nil { + t.Fatalf("a node outside the Loop inherited round 0's failure: %+v", found) + } + found := findLastFailed(t, s, tenant, run.ID, "agent-round", &round) + if found == nil || found.ID != failed.ID { + t.Fatalf("round 0 failure = %+v, want %s", found, failed.ID) + } +} + +// A retry a restart settled before it ran must not hide the attempt that really failed: it carries +// started_at NULL, so the finder only offers attempts that started. +func TestLastFailedNodeAttemptIgnoresAFailureThatNeverStarted(t *testing.T) { + s := storetest.Open(t) + user, tenant := storetest.Login(t, s, "last-failed-unstarted") + ctx := context.Background() + run, _ := startedRun(t, s, user, tenant, "重启") + + // The first attempt started and failed; its retry only waited. + started, waiting := failedAndReplaced(t, s, tenant, run.ID, "agent-restart", nil) + if _, err := s.FailWorkflowNodeRun(ctx, tenant, run.ID, waiting.ID, store.WorkflowNodeFailure{Error: "重启前没跑起来"}, nil); err != nil { + t.Fatal(err) + } + // Resuming clears the run's live attempts, so the failed wait is soft-deleted like history. + if err := s.ResumeWorkflowEngineRun(ctx, run.ID, func(store.WorkflowResumeState) ([]string, error) { + return []string{"agent-restart"}, nil + }, func(payload []byte, _ []string) ([]byte, error) { + return payload, nil + }); err != nil { + t.Fatal(err) + } + + found := findLastFailed(t, s, tenant, run.ID, "agent-restart", nil) + if found == nil || found.ID != started.ID { + t.Fatalf("previous failure = %+v, want the started attempt %s", found, started.ID) + } +} diff --git a/v2-server/internal/store/workflow_run_lifecycle.go b/v2-server/internal/store/workflow_run_lifecycle.go index 566fab71..741ccc12 100644 --- a/v2-server/internal/store/workflow_run_lifecycle.go +++ b/v2-server/internal/store/workflow_run_lifecycle.go @@ -87,6 +87,33 @@ func (s *Store) BindWorkflowNodeSession(ctx context.Context, tenant, runID, node return node, err } +// MergeWorkflowNodePayload applies fold to a running node attempt's payload while holding the run lock. +func (s *Store) MergeWorkflowNodePayload(ctx context.Context, tenant, runID, nodeRunID string, fold func([]byte) ([]byte, error)) (WorkflowNodeRun, error) { + var node WorkflowNodeRun + err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { + if err := lockWorkflowRun(ctx, tx, tenant, runID); err != nil { + return err + } + current, err := lockWorkflowNodeRun(ctx, tx, tenant, runID, nodeRunID) + if err != nil { + return err + } + if current.Status != WorkflowStatusRunning { + return ErrWorkflowNodeRunState + } + payload, err := fold(current.Payload) + if err != nil { + return err + } + node, err = scanNodeRun(tx.QueryRow(ctx, ` + UPDATE v2_workflow_node_runs SET payload = $4, updated_at = now() + WHERE tenant_id = $1 AND run_id = $2 AND id = $3 AND deleted_at IS NULL + RETURNING `+workflowNodeRunColumns, tenant, runID, nodeRunID, payload)) + return err + }) + return node, err +} + // CompleteWorkflowNodeRun records a successful attempt with its public answer and optional payload. // A late completion against an attempt that already finished is a no-op returning the current row, // because a retried or cancelled attempt may still race with its own result. diff --git a/v2-server/internal/store/workflow_runs.go b/v2-server/internal/store/workflow_runs.go index 992d40e4..5fcc15ea 100644 --- a/v2-server/internal/store/workflow_runs.go +++ b/v2-server/internal/store/workflow_runs.go @@ -57,11 +57,12 @@ const ( // the run points at the published snapshot, and the schedule seam passes its stable trigger identity // instead of duplicating engine state. type WorkflowRunInput struct { - ProjectID string - Name string - Input *string - TriggerKind string - TriggerRef string + ProjectID string + Name string + Input *string + TriggerKind string + TriggerRef string + InjectLastFailure *bool } // WorkflowRun is one run row without its nodes. State and RuntimePayload stay opaque JSON: the @@ -238,13 +239,17 @@ func createWorkflowRun(ctx context.Context, tx pgx.Tx, tenant, workflowID, snaps if trigger == "" { trigger = "manual" } + payload := []byte("{}") + if in.InjectLastFailure != nil && !*in.InjectLastFailure { + payload = []byte(`{"injectLastFailure":false}`) + } run, err = scanWorkflowRun(tx.QueryRow(ctx, ` INSERT INTO v2_workflow_runs (tenant_id, workflow_id, snapshot_id, project_id, name, status, state, input, - trigger_kind, trigger_ref) - VALUES ($1, $2, $3, $4, $5, 'pending', '{"current_nodes":[]}'::jsonb, $6, $7, $8) + trigger_kind, trigger_ref, payload) + VALUES ($1, $2, $3, $4, $5, 'pending', '{"current_nodes":[]}'::jsonb, $6, $7, $8, $9) RETURNING `+workflowRunColumns, - tenant, workflowID, snapshotID, in.ProjectID, name, in.Input, trigger, in.TriggerRef)) + tenant, workflowID, snapshotID, in.ProjectID, name, in.Input, trigger, in.TriggerRef, payload)) var pgErr *pgconn.PgError if errors.As(err, &pgErr) && pgErr.Code == "23505" && pgErr.ConstraintName == "v2_workflow_runs_schedule_trigger" { return ErrWorkflowRunTriggerTaken @@ -481,6 +486,40 @@ func (s *Store) WorkflowRunNodes(ctx context.Context, tenant, runID string) ([]W return nodes, nil } +// FindLastFailedWorkflowNodeAttempt returns the most recent soft-deleted started failed attempt +// for (runID, nodeID, iteration) when no later succeeded attempt has replaced it, mirroring +// desktop's find_last_failed_attempt. +func (s *Store) FindLastFailedWorkflowNodeAttempt(ctx context.Context, tenant, runID, nodeID string, iteration *int) (*WorkflowNodeRun, error) { + node, err := scanNodeRun(s.pool.QueryRow(ctx, "SELECT "+prefixed("f", workflowNodeRunColumns)+` + FROM v2_workflow_node_runs f + WHERE f.tenant_id = $1 + AND f.run_id = $2 + AND f.node_id = $3 + AND f.iteration IS NOT DISTINCT FROM $4 + AND f.started_at IS NOT NULL + AND f.status = 'failed' + AND f.deleted_at IS NOT NULL + AND NOT EXISTS ( + SELECT 1 FROM v2_workflow_node_runs s + WHERE s.tenant_id = f.tenant_id + AND s.run_id = f.run_id + AND s.node_id = f.node_id + AND s.iteration IS NOT DISTINCT FROM f.iteration + AND s.status = 'succeeded' + AND s.id <> f.id + AND s.created_at >= f.created_at + ) + ORDER BY f.created_at DESC, f.finished_at DESC NULLS LAST, f.id DESC + LIMIT 1`, tenant, runID, nodeID, iteration)) + if errors.Is(err, ErrWorkflowNodeRunNotFound) { + return nil, nil + } + if err != nil { + return nil, fmt.Errorf("find last failed node attempt: %w", err) + } + return &node, nil +} + // WorkflowRunFailedAttempts lists the soft-deleted failed rows of one run, oldest first, as // desktop's list_failed_attempts: a retry replaces a failed attempt by soft-deleting it, so these // rows are exactly the attempts that failed and were run again. Each keeps its error_detail. diff --git a/v2-server/internal/taskworkflow/host.go b/v2-server/internal/taskworkflow/host.go index 7508aeb6..9b269525 100644 --- a/v2-server/internal/taskworkflow/host.go +++ b/v2-server/internal/taskworkflow/host.go @@ -26,13 +26,15 @@ type Runs interface { // Host owns watchers until Close and uses the database to serialize cross-kind admission. type Host struct { - db *store.Store - runs Runs - ctx context.Context - stop context.CancelFunc - workers sync.WaitGroup - mu sync.Mutex - locks map[string]*taskLock + db *store.Store + runs Runs + ctx context.Context + stop context.CancelFunc + workers sync.WaitGroup + mu sync.Mutex + locks map[string]*taskLock + beforeRefuse func() + retryInterval time.Duration } // taskLock counts both its holder and queued callers so idle entries can be reclaimed safely. @@ -89,10 +91,12 @@ func (h *Host) Start(ctx context.Context, in store.NewTaskWorkflow, values map[s } owned := context.WithoutCancel(ctx) if err := h.runs.Prepare(owned, in.TenantID, in.UserID, id, in.Input, values); err != nil { - return id, h.refuse(owned, in.TenantID, id, err) + _, err = h.refuse(owned, in.TenantID, in.UserID, id, err, true) + return id, err } if err := h.db.CheckTaskWorkflowStart(owned, in.TenantID, id); err != nil { - return id, h.refuse(owned, in.TenantID, id, err) + _, err = h.refuse(owned, in.TenantID, in.UserID, id, err, true) + return id, err } events, cancel := h.runs.Subscribe(id) if err = h.runs.Start(owned, in.TenantID, in.UserID, id); err != nil { @@ -179,11 +183,11 @@ func (h *Host) startFailure(ctx context.Context, tenant, user, id string, cause h.cancelExecutedStart(ctx, tenant, user, id) return true } - h.refuse(ctx, tenant, id, stoppedBeforeStart) - return false + executed, _ := h.refuse(ctx, tenant, user, id, stoppedBeforeStart, false) + return executed } - h.refuse(ctx, tenant, id, cause) - return false + executed, _ := h.refuse(ctx, tenant, user, id, cause, false) + return executed } // cancelExecutedStart stops a run that already began executing through the workflow host and @@ -288,36 +292,50 @@ func (h *Host) lock(id string) func() { } // refuse keeps admission reserved until cancellation and link restoration commit together, and -// retries transient failures, so a transient error cannot abandon an admission. It only ever -// covers starts that never executed; callers route executed runs to the workflow host instead. -func (h *Host) refuse(ctx context.Context, tenant, id string, cause error) error { - clean := func(ctx context.Context) error { +// retries transient failures, so a transient error cannot abandon an admission. If the run turns +// out to have already begun executing (ErrWorkflowRunState), it hands off to cancelExecutedStart +// and keeps (or opens) a watcher so the real outcome settles. +func (h *Host) refuse(ctx context.Context, tenant, user, id string, cause error, watchOnExecute bool) (bool, error) { + clean := func(ctx context.Context, subscribeOnExecute bool) (bool, error) { + if h.beforeRefuse != nil { + h.beforeRefuse() + } err := h.db.RefuseTaskWorkflow(ctx, tenant, id, cause.Error()) if errors.Is(err, store.ErrWorkflowRunState) { - // The run turned out to have executed: a refusal can never succeed, and normal - // settlement owns the run from here. Stop retrying without hiding the original cause. - return nil + if subscribeOnExecute { + events, cancel := h.runs.Subscribe(id) + h.cancelExecutedStart(ctx, tenant, user, id) + h.watch(tenant, id, events, cancel) + } else { + h.cancelExecutedStart(ctx, tenant, user, id) + } + return true, nil } - return err + return false, err } - if err := clean(ctx); err != nil { + executed, err := clean(ctx, watchOnExecute) + if err != nil { + interval := h.retryInterval + if interval <= 0 { + interval = time.Second + } h.workers.Add(1) go func() { defer h.workers.Done() - tick := time.NewTicker(time.Second) + tick := time.NewTicker(interval) defer tick.Stop() for { select { case <-h.ctx.Done(): return case <-tick.C: - if clean(h.ctx) == nil { + if _, err := clean(h.ctx, true); err == nil { return } } } }() - return errors.Join(cause, err) + return false, errors.Join(cause, err) } - return cause + return executed, cause } diff --git a/v2-server/internal/taskworkflow/host_test.go b/v2-server/internal/taskworkflow/host_test.go new file mode 100644 index 00000000..ecac37e2 --- /dev/null +++ b/v2-server/internal/taskworkflow/host_test.go @@ -0,0 +1,385 @@ +package taskworkflow + +import ( + "context" + "encoding/json" + "errors" + "sync" + "testing" + "time" + + "github.com/wanglongan587/cloud/v2-server/internal/execution" + "github.com/wanglongan587/cloud/v2-server/internal/store" + "github.com/wanglongan587/cloud/v2-server/internal/store/storetest" +) + +const minimalWorkflowGraph = `{ + "nodes": [ + {"id": "start", "type": "workflow", "data": {"kind": "start", "title": "Start"}}, + {"id": "out", "type": "workflow", "data": {"kind": "output", "title": "Out", "outputMode": "text"}} + ], + "edges": [ + {"id": "e1", "source": "start", "target": "out"} + ] +}` + +type fakeRuns struct { + prepareFn func(ctx context.Context, tenant, user, id string, input *string, values map[string]json.RawMessage) error + startFn func(ctx context.Context, tenant, user, id string) error + cancelFn func(ctx context.Context, tenant, user, id string) error + + mu sync.Mutex + subscribes int + unsubscribes int + cancels []string + cancelCh chan string + subs map[string][]chan struct{} +} + +func newFakeRuns() *fakeRuns { + return &fakeRuns{ + cancelCh: make(chan string, 8), + subs: map[string][]chan struct{}{}, + } +} + +func (r *fakeRuns) Prepare(ctx context.Context, tenant, user, id string, input *string, values map[string]json.RawMessage) error { + if r.prepareFn != nil { + return r.prepareFn(ctx, tenant, user, id, input, values) + } + return nil +} + +func (r *fakeRuns) Start(ctx context.Context, tenant, user, id string) error { + if r.startFn != nil { + return r.startFn(ctx, tenant, user, id) + } + return nil +} + +func (r *fakeRuns) Cancel(ctx context.Context, tenant, user, id string) error { + r.mu.Lock() + r.cancels = append(r.cancels, id) + r.mu.Unlock() + r.cancelCh <- id + if r.cancelFn != nil { + return r.cancelFn(ctx, tenant, user, id) + } + return nil +} + +func (r *fakeRuns) Subscribe(id string) (<-chan struct{}, func()) { + ch := make(chan struct{}, 4) + r.mu.Lock() + r.subscribes++ + r.subs[id] = append(r.subs[id], ch) + r.mu.Unlock() + var once sync.Once + return ch, func() { + once.Do(func() { + r.mu.Lock() + r.unsubscribes++ + r.mu.Unlock() + }) + } +} + +func (r *fakeRuns) notify(id string) { + r.mu.Lock() + list := append([]chan struct{}(nil), r.subs[id]...) + r.mu.Unlock() + for _, ch := range list { + select { + case ch <- struct{}{}: + default: + } + } +} + +func (r *fakeRuns) counts() (subscribes, unsubscribes int, cancels []string) { + r.mu.Lock() + defer r.mu.Unlock() + return r.subscribes, r.unsubscribes, append([]string(nil), r.cancels...) +} + +type fixture struct { + s *store.Store + tenant string + user string + wfID string + snapID string + task store.Task +} + +func setupFixture(t *testing.T) fixture { + t.Helper() + ctx := t.Context() + s := storetest.Open(t) + user, tenant := storetest.Login(t, s, "taskworkflow-runner") + project, err := s.CreateProject(ctx, execution.Scope{TenantID: tenant, UserID: user}, store.Project{ + ID: "proj-tw", + Name: "TaskWorkflow Test Project", + RepositoryURL: "https://example.invalid/demo.git", + Branch: "main", + Commit: "0123456789abcdef0123456789abcdef01234567", + MirrorPath: "/tmp/proj-tw", + SnapshotRef: "refs/ora/snapshots/proj-tw", + }) + if err != nil { + t.Fatal(err) + } + wf, _, err := s.CreateWorkflow(ctx, tenant, "处理流", minimalWorkflowGraph) + if err != nil { + t.Fatal(err) + } + snap, err := s.PublishWorkflow(ctx, tenant, wf.ID, "v1", nil) + if err != nil { + t.Fatal(err) + } + task, err := s.CreateTask(ctx, tenant, user, store.TaskInput{ + Title: "需要处理", + ProjectID: project.ID, + Assignee: &store.TaskAssigneeRef{Type: "human", ID: user}, + }) + if err != nil { + t.Fatal(err) + } + return fixture{ + s: s, + tenant: tenant, + user: user, + wfID: wf.ID, + snapID: snap.ID, + task: task, + } +} + +func waitSettled(t *testing.T, f fixture, runID, wantStatus, wantTaskStatus string) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for time.Now().Before(deadline) { + items, err := f.s.TaskWorkflowRuns(t.Context(), f.tenant, f.task.ID) + if err != nil { + t.Fatal(err) + } + for _, item := range items { + if item.WorkflowRunID == runID && item.LinkStatus == wantStatus { + gotTask, err := f.s.Task(t.Context(), f.tenant, f.task.ID) + if err != nil { + t.Fatal(err) + } + if gotTask.Status == wantTaskStatus { + return + } + } + } + time.Sleep(15 * time.Millisecond) + } + items, _ := f.s.TaskWorkflowRuns(t.Context(), f.tenant, f.task.ID) + gotTask, _ := f.s.Task(t.Context(), f.tenant, f.task.ID) + t.Fatalf("run %s did not settle to %q (task=%+v items=%+v)", runID, wantStatus, gotTask, items) +} + +func TestStartFailureRefuseStateRaceCancelsAndKeepsExistingWatch(t *testing.T) { + f := setupFixture(t) + runs := newFakeRuns() + startErr := errors.New("start failed after dispatch") + runs.startFn = func(context.Context, string, string, string) error { + return startErr + } + runs.cancelFn = func(context.Context, string, string, string) error { + return errors.New("execution stop unconfirmed") + } + + h := New(f.s, runs) + var raced sync.Once + var runID string + var raceErr error + h.beforeRefuse = func() { + // Simulate StartWorkflowRun committing between TaskWorkflowOutcome seeing "pending" + // and RefuseTaskWorkflow attempting the CAS back to cancelled. + raced.Do(func() { + _, raceErr = f.s.StartWorkflowRun(context.Background(), f.tenant, runID) + }) + } + + var err error + in := store.NewTaskWorkflow{ + TenantID: f.tenant, + TaskID: f.task.ID, + UserID: f.user, + Key: "race-start-failure", + WorkflowID: f.wfID, + SnapshotID: f.snapID, + } + // Capture runID via Prepare because beforeRefuse runs before RefuseTaskWorkflow inside Start. + runs.prepareFn = func(_ context.Context, _, _, id string, _ *string, _ map[string]json.RawMessage) error { + runID = id + return nil + } + + gotID, err := h.Start(t.Context(), in, nil) + if !errors.Is(err, startErr) { + t.Fatalf("Start err = %v, want %v", err, startErr) + } + if raceErr != nil { + t.Fatalf("StartWorkflowRun in race hook failed: %v", raceErr) + } + if gotID == "" || gotID != runID { + t.Fatalf("gotID=%q runID=%q", gotID, runID) + } + + subs, unsubs, cancels := runs.counts() + if subs != 1 || unsubs != 0 { + t.Fatalf("subscribes=%d unsubscribes=%d, want 1/0 (must reuse existing watch)", subs, unsubs) + } + if len(cancels) != 1 || cancels[0] != runID { + t.Fatalf("cancels=%v, want [%s]", cancels, runID) + } + + // Cancel was unconfirmed so the handling link stays running until the real run completes. + if _, err := f.s.FinishWorkflowRun(t.Context(), f.tenant, runID, store.WorkflowStatusSucceeded, nil, nil); err != nil { + t.Fatal(err) + } + runs.notify(runID) + + waitSettled(t, f, runID, store.WorkflowStatusSucceeded, store.TaskReview) + h.Close() + + _, unsubs, _ = runs.counts() + if unsubs != 1 { + t.Fatalf("unsubscribes=%d after Close, want 1", unsubs) + } +} + +func TestPrepareRefuseStateRaceSubscribesCancelsAndWatches(t *testing.T) { + f := setupFixture(t) + runs := newFakeRuns() + prepareErr := errors.New("prepare failed late") + var runID string + var raced sync.Once + var raceErr error + runs.prepareFn = func(_ context.Context, _, _, id string, _ *string, _ map[string]json.RawMessage) error { + runID = id + return prepareErr + } + + h := New(f.s, runs) + h.beforeRefuse = func() { + raced.Do(func() { + _, raceErr = f.s.StartWorkflowRun(context.Background(), f.tenant, runID) + }) + } + + gotID, err := h.Start(t.Context(), store.NewTaskWorkflow{ + TenantID: f.tenant, + TaskID: f.task.ID, + UserID: f.user, + Key: "race-prepare", + WorkflowID: f.wfID, + SnapshotID: f.snapID, + }, nil) + if !errors.Is(err, prepareErr) { + t.Fatalf("Start err = %v, want %v", err, prepareErr) + } + if raceErr != nil { + t.Fatalf("StartWorkflowRun in race hook failed: %v", raceErr) + } + if gotID != runID { + t.Fatalf("gotID=%q runID=%q", gotID, runID) + } + + subs, _, cancels := runs.counts() + if subs != 1 || len(cancels) != 1 || cancels[0] != runID { + t.Fatalf("subs=%d cancels=%v, want 1/[%s]", subs, cancels, runID) + } + + if _, err := f.s.FinishWorkflowRun(t.Context(), f.tenant, runID, store.WorkflowStatusSucceeded, nil, nil); err != nil { + t.Fatal(err) + } + runs.notify(runID) + + waitSettled(t, f, runID, store.WorkflowStatusSucceeded, store.TaskReview) + h.Close() + + _, unsubs, _ := runs.counts() + if unsubs != 1 { + t.Fatalf("unsubscribes=%d after Close, want 1", unsubs) + } +} + +func TestRefuseRetryTransitionToExecutedSubscribesCancelsAndWatches(t *testing.T) { + f := setupFixture(t) + id, fresh, err := f.s.CreateTaskWorkflowRun(t.Context(), store.NewTaskWorkflow{ + TenantID: f.tenant, + TaskID: f.task.ID, + UserID: f.user, + Key: "retry-executed", + WorkflowID: f.wfID, + SnapshotID: f.snapID, + ExpectedVersion: f.task.Version, + }) + if err != nil || !fresh { + t.Fatalf("CreateTaskWorkflowRun = %q %v %v", id, fresh, err) + } + + runs := newFakeRuns() + runs.cancelFn = func(context.Context, string, string, string) error { + return errors.New("execution stop unconfirmed") + } + h := New(f.s, runs) + h.retryInterval = 10 * time.Millisecond + + var mu sync.Mutex + attempts := 0 + var startErr error + h.beforeRefuse = func() { + mu.Lock() + attempts++ + attempt := attempts + mu.Unlock() + // Attempt 1 uses a canceled context to trigger the background retry path. + // Attempt 2 executes inside the retry goroutine after the run has already started. + if attempt == 2 { + _, startErr = f.s.StartWorkflowRun(context.Background(), f.tenant, id) + } + } + + canceledCtx, cancel := context.WithCancel(t.Context()) + cancel() + cause := errors.New("initial dispatch failed") + executed, gotErr := h.refuse(canceledCtx, f.tenant, f.user, id, cause, false) + if executed || !errors.Is(gotErr, cause) || !errors.Is(gotErr, context.Canceled) { + t.Fatalf("refuse(canceled) = (%v, %v)", executed, gotErr) + } + + select { + case canceledID := <-runs.cancelCh: + if canceledID != id { + t.Fatalf("canceledID=%q want %q", canceledID, id) + } + case <-time.After(5 * time.Second): + t.Fatal("timed out waiting for retry goroutine to call Cancel") + } + if startErr != nil { + t.Fatalf("StartWorkflowRun in retry hook failed: %v", startErr) + } + + subs, _, _ := runs.counts() + if subs != 1 { + t.Fatalf("subscribes=%d, want 1 from background retry watcher", subs) + } + + if _, err := f.s.FinishWorkflowRun(t.Context(), f.tenant, id, store.WorkflowStatusSucceeded, nil, nil); err != nil { + t.Fatal(err) + } + runs.notify(id) + + waitSettled(t, f, id, store.WorkflowStatusSucceeded, store.TaskReview) + h.Close() + + _, unsubs, _ := runs.counts() + if unsubs != 1 { + t.Fatalf("unsubscribes=%d after Close, want 1", unsubs) + } +} diff --git a/v2-server/internal/workflow/engine.go b/v2-server/internal/workflow/engine.go index ef0f3a68..b7c6f8d6 100644 --- a/v2-server/internal/workflow/engine.go +++ b/v2-server/internal/workflow/engine.go @@ -260,14 +260,19 @@ func (e *Engine) StartAs(ctx context.Context, runID, actor string) error { } // preparePayload derives the run payload on first use (v2 creates runs before any graph-aware -// step) and mirrors the kickoff input, as desktop does at deploy time. +// step) while preserving creation-time options such as injectLastFailure, and mirrors input. func (e *Engine) preparePayload(g *Graph, raw []byte, input *string) (RunPayload, error) { p, ok, err := DecodeRunPayload(raw) if err != nil { return RunPayload{}, err } - if !ok { + if !ok || (p.StartNodeID == nil && p.Locale == "") { + inject := true + if ok { + inject = p.InjectLastFailure + } p = NewRunPayload(g, e.locale) + p.InjectLastFailure = inject } return p, p.MirrorInput(input) } diff --git a/v2-server/internal/workflow/engine_test.go b/v2-server/internal/workflow/engine_test.go index d4ef1190..7b3e50dd 100644 --- a/v2-server/internal/workflow/engine_test.go +++ b/v2-server/internal/workflow/engine_test.go @@ -289,3 +289,36 @@ func TestRenderTemplate(t *testing.T) { t.Error("a foreign writer set a variable") } } + +func TestPreparePayloadPreservesInjectLastFailureBeforeStart(t *testing.T) { + for name, tc := range map[string]struct { + raw []byte + want bool + }{ + "omitted": {nil, true}, + "empty_object": {[]byte(`{}`), true}, + "disabled_stub": {[]byte(`{"injectLastFailure":false}`), false}, + } { + t.Run(name, func(t *testing.T) { + repo := newMemoryRepo(chain, nil) + repo.run.Payload = tc.raw + e := NewEngine(repo, &recorder{}, nil, time.Now) + if err := e.UpdateInput(context.Background(), "run", str("主题"), map[string]json.RawMessage{"topic": json.RawMessage(`"支付"`)}); err != nil { + t.Fatal(err) + } + p, ok, err := DecodeRunPayload(repo.run.Payload) + if err != nil || !ok { + t.Fatalf("DecodeRunPayload = %+v %v %v", p, ok, err) + } + if p.StartNodeID == nil || *p.StartNodeID != "start" || p.Locale != LocaleZhCN { + t.Fatalf("prepared payload missing graph metadata: %+v", p) + } + if p.InjectLastFailure != tc.want { + t.Fatalf("InjectLastFailure = %v, want %v", p.InjectLastFailure, tc.want) + } + if got := p.VariablePool.Values["start.topic"]; got != "支付" { + t.Fatalf("start.topic = %v", got) + } + }) + } +} diff --git a/v2-server/internal/workflow/failure.go b/v2-server/internal/workflow/failure.go index c0eb3160..3524f13e 100644 --- a/v2-server/internal/workflow/failure.go +++ b/v2-server/internal/workflow/failure.go @@ -44,7 +44,9 @@ func (k FailureKind) resumable() bool { return false } -func (k FailureKind) injectsIntoPrompt() bool { +// InjectsIntoPrompt reports whether failures of this kind are safe and useful to feed back into +// the Agent's prompt on a subsequent retry or manual resume. +func (k FailureKind) InjectsIntoPrompt() bool { switch k { case FailureStructuredOutput, FailureAgentRefusal, FailureUnknownStopReason, FailureMultipleOutputs: return true @@ -108,7 +110,7 @@ func (f Failure) Detail(attempt int, injectLastFailure bool, now time.Time) map[ "source_chain": chain, "attempt": attempt, "resumable": f.Kind.resumable(), - "injects_previous_failure": f.Kind.injectsIntoPrompt() && injectLastFailure, + "injects_previous_failure": f.Kind.InjectsIntoPrompt() && injectLastFailure, "auto_retryable": f.Kind.autoRetryable(), "recorded_at": now.UnixMilli(), } diff --git a/v2-server/internal/workflowhost/agents.go b/v2-server/internal/workflowhost/agents.go index 72a21f0a..3ab2086b 100644 --- a/v2-server/internal/workflowhost/agents.go +++ b/v2-server/internal/workflowhost/agents.go @@ -96,6 +96,46 @@ func (h *Host) abandoned(d workflow.AgentDispatch) bool { return abandoned } +func (h *Host) resolvePreviousFailure(tenant string, d workflow.AgentDispatch) string { + run, err := h.store.WorkflowRun(h.ctx, tenant, d.RunID) + if err != nil { + if h.ctx.Err() == nil { + h.log.Warn("read workflow run for previous failure injection", "run", d.RunID, "error", err) + } + return "" + } + inject := true + if rp, ok, err := workflow.DecodeRunPayload(run.RuntimePayload); err != nil { + h.log.Warn("decode workflow run payload for previous failure injection", "run", d.RunID, "error", err) + return "" + } else if ok { + inject = rp.InjectLastFailure + } + if !inject { + return "" + } + prevRow, err := h.store.FindLastFailedWorkflowNodeAttempt(h.ctx, tenant, d.RunID, d.Node.ID, d.Iteration) + if err != nil { + if h.ctx.Err() == nil { + h.log.Warn("find previous failed workflow node attempt", "run", d.RunID, "node", d.Node.ID, "error", err) + } + return "" + } + prev := previousFailureForInjection(inject, prevRow) + if prev == nil { + return "" + } + return renderPreviousFailure(*prev, d.Locale) +} + +func (h *Host) recordInjectedFailure(tenant, runID, nodeRunID, text string) { + if _, err := h.store.MergeWorkflowNodePayload(h.ctx, tenant, runID, nodeRunID, func(existing []byte) ([]byte, error) { + return workflow.MergePayload(existing, map[string]any{"injected_failure_context": text}) + }); err != nil && h.ctx.Err() == nil { + h.log.Warn("record workflow node injected failure", "run", runID, "node_run", nodeRunID, "error", err) + } +} + func (h *Host) fail(d workflow.AgentDispatch, f workflow.Failure) { if err := h.engine.FailNode(h.ctx, d.RunID, d.NodeRunID, f); err != nil { h.log.Error("record workflow node failure", "run", d.RunID, "node_run", d.NodeRunID, "error", err) @@ -176,11 +216,12 @@ func (h *Host) runAgent(st *runState, d workflow.AgentDispatch, generation uint6 fail(workflow.Failure{Kind: workflow.FailurePromptTemplate, Message: err.Error()}) return } + injectedFailure := h.resolvePreviousFailure(st.scope.TenantID, d) caller := execution.Caller{Kind: execution.CallerWorkflow, ID: d.RunID, NodeID: d.Node.ID, AttemptID: d.NodeRunID} request := execution.StartRequest{ MCP: mcp, Skills: skills, Caller: caller, Target: execution.Target{ProjectID: d.ProjectID}, - ExecutorID: executor.executor.ID, Prompt: agentPrompt(d, task, roleContent), + ExecutorID: executor.executor.ID, Prompt: agentPrompt(d, task, roleContent, injectedFailure), } if interactive { // The session outlives its turns until completion, a stop or the run's end ends it. @@ -200,6 +241,9 @@ func (h *Host) runAgent(st *runState, d workflow.AgentDispatch, generation uint6 } return } + if injectedFailure != "" { + h.recordInjectedFailure(st.scope.TenantID, d.RunID, d.NodeRunID, injectedFailure) + } ref, err := h.exec.Start(h.ctx, st.scope, request) cancelling, last, unstopped := st.endStart(a, ref, err) if err != nil { diff --git a/v2-server/internal/workflowhost/executor_test.go b/v2-server/internal/workflowhost/executor_test.go index 0e92a4e9..954188b8 100644 --- a/v2-server/internal/workflowhost/executor_test.go +++ b/v2-server/internal/workflowhost/executor_test.go @@ -82,3 +82,33 @@ func TestMissingModelFailureFlags(t *testing.T) { } } } + +func TestMatchingModelByNameRecordsRequestedNameInPayload(t *testing.T) { + f := newFixture(t) + graph := strings.Replace(twoAgents, `"modelId":"fixture"`, `"modelId":"Other Model"`, 1) + runID := f.run(t, graph, str("input")) + if _, err := f.host.StartRun(context.Background(), f.scope, runID); err != nil { + t.Fatal(err) + } + f.exec.finish(f.exec.awaitStart(t), completed("draft")) + f.exec.finish(f.exec.awaitStart(t), completed("reviewed")) + run, nodes := f.await(t, runID, terminal) + if run.Status != store.WorkflowStatusSucceeded { + t.Fatalf("run = %+v", run) + } + var payload struct { + Model struct { + ID string `json:"id"` + Name string `json:"name"` + Requested string `json:"requested"` + Fallback bool `json:"fallback"` + } `json:"model"` + } + b := node(nodes, "b") + if err := json.Unmarshal(b.Payload, &payload); err != nil { + t.Fatal(err) + } + if payload.Model.ID != "other" || payload.Model.Name != "Other Model" || payload.Model.Requested != "Other Model" || payload.Model.Fallback { + t.Fatalf("model payload = %+v (%s)", payload.Model, b.Payload) + } +} diff --git a/v2-server/internal/workflowhost/prompt.go b/v2-server/internal/workflowhost/prompt.go index 59ebba32..83cf7adb 100644 --- a/v2-server/internal/workflowhost/prompt.go +++ b/v2-server/internal/workflowhost/prompt.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/wanglongan587/cloud/v2-server/internal/execution" + "github.com/wanglongan587/cloud/v2-server/internal/store" "github.com/wanglongan587/cloud/v2-server/internal/workflow" ) @@ -16,14 +17,14 @@ import ( const promptWorkspaceRoot = "." // agentPrompt is desktop's assemble_workflow_prompt (crates/backend/src/workflow/run/prompt.rs) -// without the parts v2 does not apply: previous-failure injection (v2 never retries). -// The node's prompt is already rendered against the pool, and roleContent is the role definition -// read from the tenant's role catalog just before this attempt started; an empty content sends no -// {@code system_instructions} block, as desktop does for an empty role. Each section is one ACP -// text block, separated by a blank line because providers may concatenate block text verbatim. A -// structured-output node ends with its output contract, last so the final-response constraint has -// the strongest recency. -func agentPrompt(d workflow.AgentDispatch, renderedTask, roleContent string) execution.Prompt { +// agentPrompt is desktop's assemble_workflow_prompt (crates/backend/src/workflow/run/prompt.rs). +// The node's prompt is already rendered against the pool, roleContent is the role definition read +// from the tenant's role catalog just before this attempt started (an empty content sends no +// {@code system_instructions} block, as desktop does for an empty role), and previousFailure is +// the rendered retry/resume context when enabled. Each section is one ACP text block, separated by +// a blank line because providers may concatenate block text verbatim. A structured-output node +// ends with its output contract, last so the final-response constraint has the strongest recency. +func agentPrompt(d workflow.AgentDispatch, renderedTask, roleContent, previousFailure string) execution.Prompt { c := promptCopyFor(d.Locale) sections := []string{ workspaceRootBlock(d.Locale), @@ -38,6 +39,9 @@ func agentPrompt(d workflow.AgentDispatch, renderedTask, roleContent string) exe if names := d.Node.Agent().Skills; len(names) > 0 { sections = append(sections, "\nRead and follow the selected imported skills before doing this step. They are available under .opencode/skills or .claude/skills in this workspace. A plugin selection supplies its packaged skills.\n"+strings.Join(names, "\n")+"\n") } + if previousFailure != "" { + sections = append(sections, previousFailure) + } if schema := d.Node.Agent().Structured; schema != nil { sections = append(sections, structuredContractBlock(schema, d.Locale)) } @@ -211,3 +215,111 @@ func promptCopyFor(locale string) promptCopy { }, } } + +const previousFailureOutputLimit = 4000 + +type previousFailureForPrompt struct { + Attempt int + Kind workflow.FailureKind + Message string + Output *string +} + +// previousFailureForInjection filters a soft-deleted failed attempt for prompt injection, mirroring +// desktop's previous_failure_for_injection: only injectable failure kinds pass when enabled, empty +// detail messages fall back to the row error, and only structured-output failures include output +// (bounded to 4000 runes with a trailing ellipsis). +func previousFailureForInjection(inject bool, prev *store.WorkflowNodeRun) *previousFailureForPrompt { + if !inject || prev == nil || len(prev.Payload) == 0 { + return nil + } + var decoded struct { + ErrorDetail *struct { + Kind workflow.FailureKind `json:"kind"` + Message string `json:"message"` + Attempt int `json:"attempt"` + } `json:"error_detail"` + } + if json.Unmarshal(prev.Payload, &decoded) != nil || decoded.ErrorDetail == nil { + return nil + } + detail := decoded.ErrorDetail + if !detail.Kind.InjectsIntoPrompt() { + return nil + } + message := detail.Message + if message == "" && prev.Error != nil { + message = *prev.Error + } + var output *string + if detail.Kind == workflow.FailureStructuredOutput && prev.Output != nil { + truncated := truncateRunes(*prev.Output, previousFailureOutputLimit) + output = &truncated + } + return &previousFailureForPrompt{ + Attempt: detail.Attempt, + Kind: detail.Kind, + Message: message, + Output: output, + } +} + +func truncateRunes(s string, limit int) string { + runes := []rune(s) + if len(runes) <= limit { + return s + } + return string(runes[:limit]) + "…" +} + +func failureKindLabel(kind workflow.FailureKind, locale string) string { + if locale == workflow.LocaleZhCN { + switch kind { + case workflow.FailureStructuredOutput: + return "结构化输出不合格" + case workflow.FailureAgentRefusal: + return "智能体拒绝了请求" + case workflow.FailureUnknownStopReason: + return "未知的停止原因" + case workflow.FailureMultipleOutputs: + return "多个输出节点同时完成" + default: + return string(kind) + } + } + switch kind { + case workflow.FailureStructuredOutput: + return "Structured output invalid" + case workflow.FailureAgentRefusal: + return "Agent refused the request" + case workflow.FailureUnknownStopReason: + return "Unknown stop reason" + case workflow.FailureMultipleOutputs: + return "Multiple output nodes completed" + default: + return string(kind) + } +} + +func renderPreviousFailure(prev previousFailureForPrompt, locale string) string { + kindLabel := failureKindLabel(prev.Kind, locale) + var text string + if locale == workflow.LocaleZhCN { + text = fmt.Sprintf( + "## 上一次尝试(第 %d 次)失败信息\n该步骤此前已尝试 %d 次并失败。以下是最近一次失败的原因,请避免重蹈覆辙,并按当前步骤要求重新完成。\n失败类型:%s\n失败信息:%s", + prev.Attempt, prev.Attempt, kindLabel, prev.Message, + ) + if prev.Output != nil { + text += fmt.Sprintf("\n上一次的最终输出(供对照,可能已截断):\n```text\n%s\n```", *prev.Output) + } + return text + } + text = fmt.Sprintf( + "## Previous attempt (%d) failed\nThis step already ran %d time(s) and failed. Below is the most recent failure; do not repeat it, and complete the step as instructed.\nFailure type: %s\nFailure message: %s", + prev.Attempt, prev.Attempt, kindLabel, prev.Message, + ) + if prev.Output != nil { + text += fmt.Sprintf("\nPrevious final output (for reference, may be truncated):\n```text\n%s\n```", *prev.Output) + } + return text +} diff --git a/v2-server/internal/workflowhost/resume_test.go b/v2-server/internal/workflowhost/resume_test.go index 79be9e62..5728d9b9 100644 --- a/v2-server/internal/workflowhost/resume_test.go +++ b/v2-server/internal/workflowhost/resume_test.go @@ -403,3 +403,72 @@ func TestResumeRestartsAFailedLoopFromRoundOne(t *testing.T) { t.Fatalf("the old round kept live rows: %+v", rows) } } + +func TestResumeInjectsPreviousFailureAndRespectsDisabledFlag(t *testing.T) { + for _, inject := range []bool{true, false} { + t.Run(string(rune('0'+map[bool]int{true: 1, false: 0}[inject])), func(t *testing.T) { + f := newFixture(t) + ctx := context.Background() + graph := strings.Replace(reviewBranches, `"outputContract":`, `"retry":{"enabled":false,"maxRetries":0,"initialDelaySeconds":10},"outputContract":`, 1) + w, _, err := f.s.CreateWorkflow(ctx, f.scope.TenantID, "恢复注入", graph) + if err != nil { + t.Fatal(err) + } + if _, err := f.s.PublishWorkflow(ctx, f.scope.TenantID, w.ID, "v1", nil); err != nil { + t.Fatal(err) + } + run, err := f.s.CreateWorkflowRun(ctx, f.scope.TenantID, w.ID, "", store.WorkflowRunInput{ + ProjectID: f.project, + Input: str("支付评审"), + InjectLastFailure: &inject, + }) + if err != nil { + t.Fatal(err) + } + if _, err := f.host.StartRun(ctx, f.scope, run.ID); err != nil { + t.Fatal(err) + } + f.exec.finish(expectAgent(t, f, "review"), completed(`{"verdict":"bad"}`)) + failedRun, _ := f.await(t, run.ID, terminal) + if failedRun.Status != "failed" { + t.Fatalf("run = %+v", failedRun) + } + if _, err := f.host.ResumeRun(ctx, f.scope, run.ID); err != nil { + t.Fatal(err) + } + resumedSession := expectAgent(t, f, "review") + prompt := promptText(t, f.exec.request(resumedSession).Prompt) + nodes, err := f.s.WorkflowRunNodes(ctx, f.scope.TenantID, run.ID) + if err != nil { + t.Fatal(err) + } + var payload map[string]any + if len(node(nodes, "review").Payload) > 0 { + if err := json.Unmarshal(node(nodes, "review").Payload, &payload); err != nil { + t.Fatal(err) + } + } + if inject { + if !strings.Contains(prompt, "## 上一次尝试(第 1 次)失败信息") || !strings.Contains(prompt, `{"verdict":"bad"}`) { + t.Fatalf("missing previous failure in resumed prompt:\n%s", prompt) + } + if _, ok := payload["injected_failure_context"].(string); !ok { + t.Fatalf("resumed row missing injected_failure_context: %s", node(nodes, "review").Payload) + } + } else { + if strings.Contains(prompt, "上一次尝试") { + t.Fatalf("unexpected previous failure when disabled:\n%s", prompt) + } + if _, exists := payload["injected_failure_context"]; exists { + t.Fatalf("unexpected injected_failure_context when disabled: %s", node(nodes, "review").Payload) + } + } + f.exec.finish(resumedSession, completed(`{"approved":true}`)) + f.exec.finish(expectAgent(t, f, "ship"), completed("已发布")) + settled, _ := f.await(t, run.ID, terminal) + if settled.Status != "succeeded" { + t.Fatalf("settled = %+v", settled) + } + }) + } +} diff --git a/v2-server/internal/workflowhost/retry_test.go b/v2-server/internal/workflowhost/retry_test.go index c6d2edb3..4a6c1efa 100644 --- a/v2-server/internal/workflowhost/retry_test.go +++ b/v2-server/internal/workflowhost/retry_test.go @@ -710,3 +710,129 @@ func TestReplacedAttemptRecordsTheRunsInjectSetting(t *testing.T) { }) } } + +func TestRetryInjectsPreviousFailureAndRespectsSwitch(t *testing.T) { + for _, inject := range []bool{true, false} { + t.Run(fmt.Sprintf("inject=%v", inject), func(t *testing.T) { + f := newFixture(t) + clock := retryClock(t, f) + ctx := context.Background() + graph := strings.Replace(reviewBranches, `"outputContract":`, `"retry":{"enabled":true,"maxRetries":1,"initialDelaySeconds":0},"outputContract":`, 1) + w, _, err := f.s.CreateWorkflow(ctx, f.scope.TenantID, "注入重试", graph) + if err != nil { + t.Fatal(err) + } + if _, err := f.s.PublishWorkflow(ctx, f.scope.TenantID, w.ID, "v1", nil); err != nil { + t.Fatal(err) + } + run, err := f.s.CreateWorkflowRun(ctx, f.scope.TenantID, w.ID, "", store.WorkflowRunInput{ + ProjectID: f.project, + Input: str("检查支付"), + InjectLastFailure: &inject, + }) + if err != nil { + t.Fatal(err) + } + if _, err := f.host.UpdateRunInput(ctx, f.scope, run.ID, str("更新后输入"), nil); err != nil { + t.Fatal(err) + } + if _, err := f.host.StartRun(ctx, f.scope, run.ID); err != nil { + t.Fatal(err) + } + firstSession := f.exec.awaitStart(t) + f.exec.finish(firstSession, completed(strings.Repeat("x", 4005))) + + waitRow, p := awaitWait(t, f, run.ID, "review") + clock.Store(p.Wait.Due) + if err := f.host.engine.WakeRetry(ctx, run.ID, waitRow.ID); err != nil { + t.Fatal(err) + } + retrySession := f.exec.awaitStart(t) + prompt := promptText(t, f.exec.request(retrySession).Prompt) + + nodes, err := f.s.WorkflowRunNodes(ctx, f.scope.TenantID, run.ID) + if err != nil { + t.Fatal(err) + } + retryRow := node(nodes, "review") + var stored map[string]any + if len(retryRow.Payload) > 0 { + if err := json.Unmarshal(retryRow.Payload, &stored); err != nil { + t.Fatal(err) + } + } + if inject { + if !strings.Contains(prompt, "## 上一次尝试(第 1 次)失败信息") || !strings.Contains(prompt, "失败类型:结构化输出不合格") { + t.Fatalf("missing injected previous failure block in prompt:\n%s", prompt) + } + wantTruncated := strings.Repeat("x", 4000) + "…" + if !strings.Contains(prompt, wantTruncated) || strings.Contains(prompt, strings.Repeat("x", 4001)) { + t.Fatalf("structured output not bounded to 4000 runes with ellipsis:\n%s", prompt) + } + injectIdx := strings.Index(prompt, "## 上一次尝试(第 1 次)失败信息") + contractIdx := strings.Index(prompt, "") + if contractIdx == -1 || injectIdx == -1 || injectIdx > contractIdx { + t.Fatalf("expected previous failure before structured contract (inject=%d contract=%d)", injectIdx, contractIdx) + } + recorded, _ := stored["injected_failure_context"].(string) + if !strings.Contains(recorded, "## 上一次尝试(第 1 次)失败信息") || !strings.Contains(recorded, wantTruncated) { + t.Fatalf("injected_failure_context = %q", recorded) + } + } else { + if strings.Contains(prompt, "上一次尝试") || strings.Contains(prompt, "Previous attempt") { + t.Fatalf("unexpected injected previous failure when disabled:\n%s", prompt) + } + if _, exists := stored["injected_failure_context"]; exists { + t.Fatalf("unexpected injected_failure_context persisted when disabled: %v", stored) + } + } + + f.exec.finish(retrySession, completed(`{"approved":true}`)) + f.exec.finish(expectAgent(t, f, "ship"), completed("已发布")) + settled, after := f.await(t, run.ID, terminal) + if settled.Status != "succeeded" { + t.Fatalf("run = %+v", settled) + } + if inject { + var donePayload map[string]any + if err := json.Unmarshal(node(after, "review").Payload, &donePayload); err != nil { + t.Fatal(err) + } + if _, ok := donePayload["injected_failure_context"].(string); !ok { + t.Fatalf("completed node lost injected_failure_context: %s", node(after, "review").Payload) + } + } + }) + } +} + +func TestPreviousFailureForInjectionFilteringAndEnglishFormatting(t *testing.T) { + errMsg := "refused by policy" + refusal := &store.WorkflowNodeRun{ + Error: &errMsg, + Payload: []byte(`{"error_detail":{"kind":"agent_refusal","attempt":2,"message":""}}`), + } + prev := previousFailureForInjection(true, refusal) + if prev == nil || prev.Attempt != 2 || prev.Message != errMsg || prev.Output != nil { + t.Fatalf("prev = %+v", prev) + } + rendered := renderPreviousFailure(*prev, workflow.LocaleEnUS) + for _, want := range []string{ + "## Previous attempt (2) failed", + "Failure type: Agent refused the request", + "Failure message: refused by policy", + } { + if !strings.Contains(rendered, want) { + t.Fatalf("rendered missing %q:\n%s", want, rendered) + } + } + sessionFail := &store.WorkflowNodeRun{ + Payload: []byte(`{"error_detail":{"kind":"session","attempt":1,"message":"boom"}}`), + } + if got := previousFailureForInjection(true, sessionFail); got != nil { + t.Fatalf("non-injectable kind returned %+v", got) + } + if got := previousFailureForInjection(false, refusal); got != nil { + t.Fatalf("disabled flag returned %+v", got) + } +} diff --git a/v2-server/workflows.openapi.json b/v2-server/workflows.openapi.json index 3f4979ee..07290159 100644 --- a/v2-server/workflows.openapi.json +++ b/v2-server/workflows.openapi.json @@ -1278,7 +1278,8 @@ "input": {"type": "string", "nullable": true}, "name": {"type": "string"}, "triggerKind": {"type": "string", "enum": ["manual", "schedule"]}, - "triggerRef": {"type": "string"} + "triggerRef": {"type": "string"}, + "injectLastFailure": {"type": "boolean"} } }, "WorkflowRunCreated": {