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
This commit is contained in:
Yiling-J
2026-10-31 12:02:30 +08:00
committed by GitHub
parent 77bd1422d8
commit ff3d71ed3d
11 changed files with 343 additions and 185 deletions
@@ -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' },