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 { 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)
+11 -38
View File
@@ -12,12 +12,9 @@ import {
getAssetActivity,
getProjectAutofillFieldsActivity,
updateAssetMetadataActivity,
updateCommentActivity,
} from '../activities/agent'
export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
let placeholderCommentId: string | undefined
try {
// Update status to processing
await updateTaskStatusActivity({
@@ -25,16 +22,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
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<void> {
// 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<void> {
})
}
// 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<void> {
} 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
@@ -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' },
+12 -31
View File
@@ -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<void> {
let placeholderCommentId: string | undefined
try {
// Update status to processing
await updateTaskStatusActivity({
@@ -34,16 +32,6 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
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<void> {
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<void> {
} 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