From ff3d71ed3d20c383b065e0d377de449bfe2157d6 Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Sat, 31 Oct 2026 12:02:30 +0800 Subject: [PATCH] feat(agent)!: suppress success comments for autofill and embedding agents (#510) * feat(agent)!: suppress success comments for autofill and embedding agents * fix(agent): ensure createCommentActivity runs on workflow cancellation --- CHANGELOG.md | 2 + packages/agent/src/activities/agent.test.ts | 24 ++- packages/agent/src/activities/agent.ts | 138 +++++++++--------- .../src/workflows/agent-autofill.test.ts | 62 +++++--- .../agent/src/workflows/agent-autofill.ts | 49 ++----- .../src/workflows/agent-embedding.test.ts | 56 ++++--- .../agent/src/workflows/agent-embedding.ts | 43 ++---- packages/e2e/workflow/agent-autofill.test.ts | 126 +++++++++++++++- packages/e2e/workflow/agent-embedding.test.ts | 16 +- .../components/chat/message-input.test.tsx | 10 +- packages/workflow-core/src/workflow-utils.ts | 2 + 11 files changed, 343 insertions(+), 185 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2c3694a..eb4b23c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Changed +- **agent**: ⚠️ **Important Behavior Change**: Autofill and Embedding agents will no longer post completion comments when finishing successfully to prevent clutter in asset comment feeds. Comments are now only posted if a task encounters an error. + ## [0.6.2] - 2026-10-29 ### Added diff --git a/packages/agent/src/activities/agent.test.ts b/packages/agent/src/activities/agent.test.ts index f15072d0..6b990e56 100644 --- a/packages/agent/src/activities/agent.test.ts +++ b/packages/agent/src/activities/agent.test.ts @@ -23,7 +23,7 @@ import { import * as piAgent from '../index' import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core' import { DatabaseSessionStorage, type DatabaseSessionMetadata } from '../database-session-storage' -import { triggerLocalCancel, runInLocalTaskContext } from '@shumai/workflow-core' +import { triggerLocalCancel, runInLocalTaskContext, DBOS, DBOSError } from '@shumai/workflow-core' import { logger } from '@shumai/core/src/logger' import { @@ -1240,6 +1240,28 @@ describe('Agent Database Activities Integration', () => { const deleted = await prisma.assetComment.findUnique({ where: { id: comment.id } }) expect(deleted).toBeNull() }) + + it('should create comment even if cancelled with DBOSWorkflowCancelledError (runOnCancel: true)', async () => { + const realCancelErr = new DBOSError.DBOSWorkflowCancelledError('test-workflow-id') + const isWithinWorkflowSpy = vi.spyOn(DBOS, 'isWithinWorkflow').mockReturnValue(true) + const runStepSpy = vi.spyOn(DBOS, 'runStep').mockRejectedValue(realCancelErr) + + const comment = await createCommentActivity({ + assetId: asset.id, + message: 'Autofill failed: workflow cancelled', + agentId: 'default', + }) + + expect(comment.id).toBeDefined() + expect(comment.message).toBe('Autofill failed: workflow cancelled') + + const savedComment = await prisma.assetComment.findUnique({ where: { id: comment.id } }) + expect(savedComment).not.toBeNull() + expect(savedComment?.message).toBe('Autofill failed: workflow cancelled') + + isWithinWorkflowSpy.mockRestore() + runStepSpy.mockRestore() + }) }) describe('Context Retrievals', () => { diff --git a/packages/agent/src/activities/agent.ts b/packages/agent/src/activities/agent.ts index f3033671..0d4cec92 100644 --- a/packages/agent/src/activities/agent.ts +++ b/packages/agent/src/activities/agent.ts @@ -969,78 +969,86 @@ export const deleteCommentActivity = defineStep(async function deleteCommentActi }) }) -export const createCommentActivity = defineStep(async function createCommentActivity(params: { - assetId: string - message: string - sessionId?: string | null - agentId?: string | null - replyToId?: string | null -}) { - if (params.sessionId) { - const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default' +export const createCommentActivity = defineStep( + async function createCommentActivity(params: { + assetId: string + message: string + sessionId?: string | null + agentId?: string | null + replyToId?: string | null + }) { + if (params.sessionId) { + const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default' - // Fetch teamId from asset - const asset = await prisma.asset.findUnique({ - where: { id: params.assetId }, - include: { project: true }, - }) - if (!asset || !asset.project) { - throw ApplicationFailure.create({ message: 'asset or project not found', nonRetryable: true }) - } - const teamId = asset.project.teamId - - // Ensure user exists for agent - const userExists = await prisma.user.findUnique({ where: { id: agentId } }) - if (!userExists) { - await prisma.user.create({ - data: { - id: agentId, - name: 'Ai Agent', - email: `${agentId}@shumai.ai`, - type: 'agent', - }, + // Fetch teamId from asset + const asset = await prisma.asset.findUnique({ + where: { id: params.assetId }, + include: { project: true }, }) - } + if (!asset || !asset.project) { + throw ApplicationFailure.create({ + message: 'asset or project not found', + nonRetryable: true, + }) + } + const teamId = asset.project.teamId - // Ensure agent exists - const agentExists = await prisma.agent.findUnique({ where: { id: agentId } }) - if (!agentExists) { - await prisma.agent.create({ - data: { - id: agentId, - teamId, - type: 'chat', - config: { - provider: 'openai', - model: 'gpt-4', + // Ensure user exists for agent + const userExists = await prisma.user.findUnique({ where: { id: agentId } }) + if (!userExists) { + await prisma.user.create({ + data: { + id: agentId, + name: 'Ai Agent', + email: `${agentId}@shumai.ai`, + type: 'agent', }, - }, + }) + } + + // Ensure agent exists + const agentExists = await prisma.agent.findUnique({ where: { id: agentId } }) + if (!agentExists) { + await prisma.agent.create({ + data: { + id: agentId, + teamId, + type: 'chat', + config: { + provider: 'openai', + model: 'gpt-4', + }, + }, + }) + } + + // Ensure agent session exists + const sessionExists = await prisma.agentSession.findUnique({ + where: { id: params.sessionId }, }) + if (!sessionExists) { + await prisma.agentSession.create({ + data: { + id: params.sessionId, + agentId: agentId, + cwd: process.cwd(), + }, + }) + } } - // Ensure agent session exists - const sessionExists = await prisma.agentSession.findUnique({ where: { id: params.sessionId } }) - if (!sessionExists) { - await prisma.agentSession.create({ - data: { - id: params.sessionId, - agentId: agentId, - cwd: process.cwd(), - }, - }) - } - } - - return prisma.assetComment.create({ - data: { - assetId: params.assetId, - message: params.message, - sessionId: params.sessionId ?? null, - creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null, - replyToId: params.replyToId, - }, - }) -}) + return prisma.assetComment.create({ + data: { + assetId: params.assetId, + message: params.message, + sessionId: params.sessionId ?? null, + creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null, + replyToId: params.replyToId, + }, + }) + }, + { runOnCancel: true }, +) export const updateCommentActivity = defineStep( async function updateCommentActivity(params: { diff --git a/packages/agent/src/workflows/agent-autofill.test.ts b/packages/agent/src/workflows/agent-autofill.test.ts index a3cb3aec..801c1cbf 100644 --- a/packages/agent/src/workflows/agent-autofill.test.ts +++ b/packages/agent/src/workflows/agent-autofill.test.ts @@ -3,6 +3,7 @@ import { agentAutofillMedia } from './agent-autofill' import { prisma } from '@shumai/db' import { setupTestDbHooks } from '@shumai/db/test' import * as workflowCore from '@shumai/workflow-core' +import { DBOSError } from '@shumai/workflow-core' import * as agentActivities from '../activities/agent' vi.mock('@shumai/workflow-core', async () => { @@ -95,13 +96,9 @@ describe('Agent Autofill Workflow', () => { // Verify asset fetched expect(mockActivities.getAssetActivity).toHaveBeenCalledWith('a1') - // Verify placeholder comment created - expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({ - assetId: 'a1', - message: '__AUTOFILL__', - sessionId: 's1', - agentId: 'agent-1', - }) + // Verify no success or placeholder comments created + expect(mockActivities.createCommentActivity).not.toHaveBeenCalled() + expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled() // Verify fields and context fetched expect(mockActivities.getProjectAutofillFieldsActivity).toHaveBeenCalledWith('p1') @@ -147,13 +144,6 @@ describe('Agent Autofill Workflow', () => { ], }) - // Verify placeholder comment updated - expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({ - commentId: 'comment-placeholder-id', - message: 'Autofill completed successfully.', - sessionId: 'session-123', - }) - // Verify completed task status expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({ taskId: task.id, @@ -205,11 +195,8 @@ describe('Agent Autofill Workflow', () => { await agentAutofillMedia(task) - expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({ - commentId: 'comment-placeholder-id', - message: 'Autofill completed: No autofill fields defined in project.', - }) - + expect(mockActivities.createCommentActivity).not.toHaveBeenCalled() + expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled() expect(mockActivities.autofillAiActivity).not.toHaveBeenCalled() expect(mockActivities.updateAssetMetadataActivity).not.toHaveBeenCalled() @@ -219,7 +206,7 @@ describe('Agent Autofill Workflow', () => { }) }) - it('should handle failures, update placeholder with error, and set status to failed', async () => { + it('should handle failures, create comment with error, and set status to failed', async () => { mockActivities.getProjectAutofillFieldsActivity.mockRejectedValue(new Error('DB failure')) const task = await prisma.workflowTask.create({ @@ -232,10 +219,12 @@ describe('Agent Autofill Workflow', () => { await expect(agentAutofillMedia(task)).rejects.toThrow('DB failure') - // Verify placeholder updated with error - expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({ - commentId: 'comment-placeholder-id', + // Verify error comment created + expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({ + assetId: 'a1', message: 'Autofill failed: DB failure', + sessionId: task.id, + agentId: 'default', }) // Verify task failed status @@ -246,6 +235,33 @@ describe('Agent Autofill Workflow', () => { }) }) + it('should post an error comment when workflow is cancelled with DBOSWorkflowCancelledError', async () => { + const cancelErr = new DBOSError.DBOSWorkflowCancelledError('wf-cancel') + mockActivities.getProjectAutofillFieldsActivity.mockRejectedValue(cancelErr) + + const task = await prisma.workflowTask.create({ + data: { + type: 'ai_metadata_autofill', + status: 'pending', + assetId: 'a1', + }, + }) + + await expect(agentAutofillMedia(task)).rejects.toThrow() + + expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({ + assetId: 'a1', + message: expect.stringContaining('Autofill failed:'), + sessionId: task.id, + agentId: 'default', + }) + expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({ + taskId: task.id, + status: 'failed', + output: { error: expect.stringContaining('wf-cancel') }, + }) + }) + it('should fail with non-retryable ApplicationFailure when asset or project is not found', async () => { mockActivities.getAssetActivity.mockResolvedValue(null) diff --git a/packages/agent/src/workflows/agent-autofill.ts b/packages/agent/src/workflows/agent-autofill.ts index f6cfa6c0..805af839 100644 --- a/packages/agent/src/workflows/agent-autofill.ts +++ b/packages/agent/src/workflows/agent-autofill.ts @@ -12,12 +12,9 @@ import { getAssetActivity, getProjectAutofillFieldsActivity, updateAssetMetadataActivity, - updateCommentActivity, } from '../activities/agent' export async function agentAutofillMedia(task: WorkflowTask): Promise { - let placeholderCommentId: string | undefined - try { // Update status to processing await updateTaskStatusActivity({ @@ -25,16 +22,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise { status: 'processing', }) - // 0. Create Placeholder Comment - const payload = task.payload - const placeholder = await createCommentActivity({ - assetId: task.assetId, - message: '__AUTOFILL__', - sessionId: payload?.agent?.sessionId || task.id, - agentId: payload?.agent?.agentId || 'default', - }) - placeholderCommentId = placeholder.id - // 1. Get Asset const asset = await getAssetActivity(task.assetId) if (!asset || !asset.project) { @@ -46,12 +33,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise { // 2. Get Project Autofill Fields const fields = await getProjectAutofillFieldsActivity(projectId) if (fields.length === 0) { - if (placeholderCommentId) { - await updateCommentActivity({ - commentId: placeholderCommentId, - message: 'Autofill completed: No autofill fields defined in project.', - }) - } await updateTaskStatusActivity({ taskId: task.id, status: 'completed', @@ -124,15 +105,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise { }) } - // 7. Update Placeholder Comment - if (placeholderCommentId) { - await updateCommentActivity({ - commentId: placeholderCommentId, - message: 'Autofill completed successfully.', - sessionId: aiResult.sessionId, - }) - } - // Update status to completed await updateTaskStatusActivity({ taskId: task.id, @@ -141,16 +113,17 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise { } catch (err) { console.error(`AgentAutofillMedia failed for task ${task.id}:`, err) - // Update placeholder comment with error message - if (placeholderCommentId) { - try { - await updateCommentActivity({ - commentId: placeholderCommentId, - message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`, - }) - } catch (commentErr) { - console.error('Failed to update error comment:', commentErr) - } + // Create error comment + try { + const payload = task.payload + await createCommentActivity({ + assetId: task.assetId, + message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`, + sessionId: payload?.agent?.sessionId || task.id, + agentId: payload?.agent?.agentId || 'default', + }) + } catch (commentErr) { + console.error('Failed to create error comment:', commentErr) } // Update status to failed diff --git a/packages/agent/src/workflows/agent-embedding.test.ts b/packages/agent/src/workflows/agent-embedding.test.ts index 5ee554a2..3542d983 100644 --- a/packages/agent/src/workflows/agent-embedding.test.ts +++ b/packages/agent/src/workflows/agent-embedding.test.ts @@ -3,6 +3,7 @@ import { agentEmbeddingMedia } from './agent-embedding' import { prisma } from '@shumai/db' import { setupTestDbHooks } from '@shumai/db/test' import * as workflowCore from '@shumai/workflow-core' +import { DBOSError } from '@shumai/workflow-core' import * as agentActivities from '../activities/agent' import * as aiActivities from '../activities/ai' @@ -122,13 +123,9 @@ describe('Agent Embedding Workflow', () => { status: 'processing', }) - // Verify placeholder comment created - expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({ - assetId: 'a1', - message: '__EMBEDDING__', - sessionId: 's1', - agentId: 'agent-1', - }) + // Verify no comments created on success + expect(mockActivities.createCommentActivity).not.toHaveBeenCalled() + expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled() // Verify context fetching expect(mockActivities.getEmbeddingContextActivity).toHaveBeenCalledWith({ @@ -157,12 +154,6 @@ describe('Agent Embedding Workflow', () => { model: 'gemini-embedding-2', }) - // Verify placeholder comment updated - expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({ - commentId: 'comment-placeholder-id', - message: 'Embedding completed successfully.', - }) - // Verify completed task status expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({ taskId: task.id, @@ -417,7 +408,7 @@ describe('Agent Embedding Workflow', () => { }) }) - it('should handle failures, update placeholder with error, and set status to failed', async () => { + it('should handle failures, create comment with error, and set status to failed', async () => { mockActivities.generateImageEmbeddingActivity.mockRejectedValue( new Error('AI Service Unavailable'), ) @@ -433,11 +424,14 @@ describe('Agent Embedding Workflow', () => { await expect(agentEmbeddingMedia(task)).rejects.toThrow('AI Service Unavailable') - // Verify placeholder updated with error - expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({ - commentId: 'comment-placeholder-id', + // Verify error comment created + expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({ + assetId: 'a1', message: 'Embedding failed: AI Service Unavailable', + sessionId: task.id, + agentId: 'default', }) + expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled() // Verify status updated to failed expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({ @@ -447,6 +441,34 @@ describe('Agent Embedding Workflow', () => { }) }) + it('should post an error comment when workflow is cancelled with DBOSWorkflowCancelledError', async () => { + const cancelErr = new DBOSError.DBOSWorkflowCancelledError('wf-cancel') + mockActivities.generateImageEmbeddingActivity.mockRejectedValue(cancelErr) + + const task = await prisma.workflowTask.create({ + data: { + type: 'ai_embedding', + status: 'pending', + assetId: 'a1', + teamId: 't1', + }, + }) + + await expect(agentEmbeddingMedia(task)).rejects.toThrow() + + expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({ + assetId: 'a1', + message: expect.stringContaining('Embedding failed:'), + sessionId: task.id, + agentId: 'default', + }) + expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({ + taskId: task.id, + status: 'failed', + output: { error: expect.stringContaining('wf-cancel') }, + }) + }) + it('should save completed chunks to DB immediately before subsequent chunk failure', async () => { mockActivities.getEmbeddingContextActivity.mockResolvedValue({ agent: { id: 'b1' }, diff --git a/packages/agent/src/workflows/agent-embedding.ts b/packages/agent/src/workflows/agent-embedding.ts index 2d92f9db..092b9fb4 100644 --- a/packages/agent/src/workflows/agent-embedding.ts +++ b/packages/agent/src/workflows/agent-embedding.ts @@ -4,7 +4,7 @@ import { updateTaskStatusActivity, updateTaskUsageActivity, } from '@shumai/workflow-core' -import { createCommentActivity, updateCommentActivity } from '../activities/agent' +import { createCommentActivity } from '../activities/agent' import { deleteAssetEmbeddingsActivity, deleteS3ObjectActivity, @@ -25,8 +25,6 @@ export interface GeneratedEmbedding { export type { ExistingAssetEmbeddingInfo } export async function agentEmbeddingMedia(task: WorkflowTask): Promise { - let placeholderCommentId: string | undefined - try { // Update status to processing await updateTaskStatusActivity({ @@ -34,16 +32,6 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise { status: 'processing', }) - // 0. Create Placeholder Comment - const payload = task.payload - const placeholder = await createCommentActivity({ - assetId: task.assetId, - message: '__EMBEDDING__', - sessionId: payload?.agent?.sessionId || task.id, - agentId: payload?.agent?.agentId || 'default', - }) - placeholderCommentId = placeholder.id - if (!task.teamId) { throw ApplicationFailure.create({ message: 'Task has no teamId', nonRetryable: true }) } @@ -238,14 +226,6 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise { model: usage.model, }) - // 7. Update Placeholder Comment - if (placeholderCommentId) { - await updateCommentActivity({ - commentId: placeholderCommentId, - message: 'Embedding completed successfully.', - }) - } - // Update status to completed await updateTaskStatusActivity({ taskId: task.id, @@ -254,16 +234,17 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise { } catch (err) { console.error(`AgentEmbeddingMedia failed for task ${task.id}:`, err) - // Update placeholder comment with error message - if (placeholderCommentId) { - try { - await updateCommentActivity({ - commentId: placeholderCommentId, - message: `Embedding failed: ${err instanceof Error ? err.message : String(err)}`, - }) - } catch (commentErr) { - console.error('Failed to update error comment:', commentErr) - } + // Create error comment + try { + const payload = task.payload + await createCommentActivity({ + assetId: task.assetId, + message: `Embedding failed: ${err instanceof Error ? err.message : String(err)}`, + sessionId: payload?.agent?.sessionId || task.id, + agentId: payload?.agent?.agentId || 'default', + }) + } catch (commentErr) { + console.error('Failed to create error comment:', commentErr) } // Update status to failed diff --git a/packages/e2e/workflow/agent-autofill.test.ts b/packages/e2e/workflow/agent-autofill.test.ts index 2f49a023..15dc0a60 100644 --- a/packages/e2e/workflow/agent-autofill.test.ts +++ b/packages/e2e/workflow/agent-autofill.test.ts @@ -263,15 +263,12 @@ describe('Workflow E2E - agentAutofillMedia', () => { // 5. Verification expect(completedTask.status).toBe('completed') - // Verify placeholder comment is updated correctly + // Verify no comments created on successful autofill const comments = await prisma.assetComment.findMany({ where: { assetId: asset.id }, orderBy: { createdAt: 'asc' }, }) - expect(comments.length).toBeGreaterThan(0) - // The last updated message of placeholder comment should indicate success - const finalComment = comments[comments.length - 1] - expect(finalComment.message).toBe('Autofill completed successfully.') + expect(comments.length).toBe(0) // Verify AssetMetadataValue contains the AI-extracted title const titleVal = await prisma.assetMetadataValue.findUnique({ @@ -324,4 +321,123 @@ describe('Workflow E2E - agentAutofillMedia', () => { }) expect(manualNotesVal).toBeNull() }, 50000) + + it('should post an error comment when agent autofill workflow fails', async () => { + // 1. Seed Database + const team = await prisma.team.create({ + data: { name: 'E2E Autofill Fail Team' }, + }) + + const project = await prisma.project.create({ + data: { name: 'E2E Autofill Fail Project', teamId: team.id }, + }) + + await prisma.metadataField.create({ + data: { + key: 'title_fail_field', + scope: 'PROJECT', + projectId: project.id, + teamId: team.id, + config: { name: 'Title', type: 'text', autofillSource: 'CONTENT' }, + description: 'AI-extracted document title', + }, + }) + + const user = await prisma.user.create({ + data: { + name: 'Autofill Fail Agent User', + email: 'failautofill@shumai.ai', + type: 'agent', + }, + }) + + await prisma.teamMember.create({ + data: { + teamId: team.id, + userId: user.id, + role: 'editor', + }, + }) + + const provider = await prisma.provider.create({ + data: { + name: 'google-fail', + teamId: team.id, + config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' }, + }, + }) + + const model = await prisma.model.create({ + data: { + modelId: 'gemini-fail', + name: 'Gemini Fail', + providerId: provider.id, + config: { + input: ['text', 'image'], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 8192, + maxTokens: 2048, + reasoning: false, + }, + }, + }) + + const agent = await prisma.agent.create({ + data: { + id: user.id, + type: 'autofill', + enabled: true, + teamId: team.id, + providerId: provider.id, + modelId: model.id, + config: { provider: 'google', model: 'gemini-fail' }, + }, + }) + + const asset = await prisma.asset.create({ + data: { + name: 'fail-sample.mp4', + type: 'file', + projectId: project.id, + mediaType: 'video/mp4', + status: 'uploaded', + media: { + proxyType: 'video', + duration: 12.0, + videoTranscodes: [{ key: 'files/fail-sample.mp4', width: 1280, height: 720 }], + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- partial MediaInfo mock for testing + } as any, + }, + }) + + // Mock prompt to fail + vi.spyOn(AgentHarness.prototype, 'prompt').mockRejectedValueOnce( + new Error('LLM rate limit reached'), + ) + + const task = await prisma.workflowTask.create({ + data: { + type: 'ai_metadata_autofill', + status: 'pending', + assetId: asset.id, + projectId: project.id, + teamId: team.id, + payload: { + projectId: project.id, + agent: { sessionId: 'mock-session-fail', agentId: agent.id }, + }, + }, + }) + + await expect(workflowService.executeWait(task, 45000)).rejects.toThrow() + + // Verify error comment was created on the asset + const comments = await prisma.assetComment.findMany({ + where: { assetId: asset.id }, + orderBy: { createdAt: 'asc' }, + }) + expect(comments.length).toBe(1) + expect(comments[0].message).toContain('Autofill failed:') + expect(comments[0].message).toContain('LLM rate limit reached') + }, 50000) }) diff --git a/packages/e2e/workflow/agent-embedding.test.ts b/packages/e2e/workflow/agent-embedding.test.ts index 173ff6f4..06059dcc 100644 --- a/packages/e2e/workflow/agent-embedding.test.ts +++ b/packages/e2e/workflow/agent-embedding.test.ts @@ -178,14 +178,12 @@ describe('Workflow E2E - agentEmbeddingMedia', () => { // 5. Verification expect(completedTask.status).toBe('completed') - // Verify AssetComment indicates success + // Verify no comments created on successful embedding const comments = await prisma.assetComment.findMany({ where: { assetId: asset.id }, orderBy: { createdAt: 'asc' }, }) - expect(comments.length).toBeGreaterThan(0) - const finalComment = comments[comments.length - 1] - expect(finalComment.message).toBe('Embedding completed successfully.') + expect(comments.length).toBe(0) // Verify raw embedding is saved in DB const dbEmbeddings = (await prisma.$queryRaw` @@ -445,5 +443,15 @@ describe('Workflow E2E - agentEmbeddingMedia', () => { where: { id: task.id }, }) expect(dbTask?.status).toBe(WorkflowTaskStatus.failed) + + // Verify failure comment was posted to the asset + const comments = await prisma.assetComment.findMany({ + where: { assetId: asset.id }, + orderBy: { createdAt: 'asc' }, + }) + expect(comments.length).toBe(1) + expect(comments[0].message).toContain( + 'Embedding failed: unsupported media type for embeddings: audio/wav', + ) }, 50000) }) diff --git a/packages/webui/components/chat/message-input.test.tsx b/packages/webui/components/chat/message-input.test.tsx index ca6bc563..d1596968 100644 --- a/packages/webui/components/chat/message-input.test.tsx +++ b/packages/webui/components/chat/message-input.test.tsx @@ -1,9 +1,10 @@ // @vitest-environment happy-dom import React from 'react' import { cleanup, render, fireEvent } from '@testing-library/react' -import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import type { CommentInfo } from '@shumai/dtos' +import { useTeamContextStore } from '@/ui/stores/team-context' import { ChatInput } from './message-input' function createWrapper() { @@ -16,6 +17,13 @@ function createWrapper() { } describe('ChatInput auto-mention on reply', () => { + beforeEach(() => { + useTeamContextStore.setState({ + teamId: 'team-123', + projectTeamMap: { 'proj-1': 'team-123' }, + }) + }) + afterEach(() => { cleanup() vi.clearAllMocks() diff --git a/packages/workflow-core/src/workflow-utils.ts b/packages/workflow-core/src/workflow-utils.ts index 598f276b..e1dcb675 100644 --- a/packages/workflow-core/src/workflow-utils.ts +++ b/packages/workflow-core/src/workflow-utils.ts @@ -1,5 +1,7 @@ import { DBOS, Error as DBOSError } from '@dbos-inc/dbos-sdk' +export { DBOS, DBOSError } + /** * Task queue constants. */