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
+23 -1
View File
@@ -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', () => {
+73 -65
View File
@@ -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: {