fix(agent): assign lower sortIndex to newer versions to ensure they are marked as latest

This commit is contained in:
yiling
2026-05-31 10:56:52 +08:00
parent 3f5b564726
commit 9c20938628
2 changed files with 78 additions and 10 deletions
+73
View File
@@ -567,5 +567,78 @@ describe('Database Activities', () => {
expect(types).toContain('ai_metadata_autofill')
expect(types).toContain('ai_embedding')
})
it('should assign a lower sortIndex to newer versions so they are correctly marked as latest', async () => {
const team = await prisma.team.create({ data: { name: 'Version Team' } })
const user = await prisma.user.create({
data: { name: 'Version User', email: 'vuser@example.com' },
})
await prisma.teamMember.create({
data: { teamId: team.id, userId: user.id, role: 'owner' },
})
const project = await prisma.project.create({
data: { name: 'Version Project', teamId: team.id },
})
const parentFolder = await prisma.asset.create({
data: {
name: 'Parent Folder',
type: 'folder',
projectId: project.id,
status: 'uploaded',
},
})
// Create file1 (V1)
const file1 = await prisma.asset.create({
data: {
name: 'file1.txt',
type: 'file',
parentId: parentFolder.id,
projectId: project.id,
status: 'uploaded',
sizeByte: 100,
},
})
// 1. Create file2 (V2) - Creates a new stack
const file2Result = await executeAgentToolActivity({
taskId: 'vstack-task-1',
toolName: 'create_version',
args: {
parent: file1.id,
s3Key: 'file/file2-key/raw',
name: 'file2.txt',
size: 150,
contentType: 'text/plain',
},
userId: user.id,
})
// Fetch from DB to check sortIndex
const updatedFile1 = await prisma.asset.findUnique({ where: { id: file1.id } })
const updatedFile2 = await prisma.asset.findUnique({ where: { id: file2Result.id } })
// Newest version (file2) must have a strictly lower sortIndex than older version (file1)
expect(updatedFile2!.sortIndex! < updatedFile1!.sortIndex!).toBe(true)
// 2. Create file3 (V3) - Appends to existing stack
const file3Result = await executeAgentToolActivity({
taskId: 'vstack-task-2',
toolName: 'create_version',
args: {
parent: file2Result.id,
s3Key: 'file/file3-key/raw',
name: 'file3.txt',
size: 200,
contentType: 'text/plain',
},
userId: user.id,
})
const updatedFile3 = await prisma.asset.findUnique({ where: { id: file3Result.id } })
// Newest version (file3) must have a strictly lower sortIndex than version 2 (file2)
expect(updatedFile3!.sortIndex! < updatedFile2!.sortIndex!).toBe(true)
})
})
})
+5 -10
View File
@@ -625,11 +625,6 @@ export async function updateWorkflowTaskActivity(params: UpdateWorkflowTaskParam
// Agent System Tools & Context Activities
// ==========================================
function generateSortIndex(previous?: string | null): string {
if (!previous) return generateKeyBetween(null, null)
return generateKeyBetween(previous, null)
}
export async function getAssetPathContextActivity(assetId: string): Promise<string> {
const parts: { name: string; id: string }[] = []
let currentId: string | null = assetId
@@ -924,12 +919,12 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
creatorId: userId,
})
// Generate sort index for new version inside the stack
const lastChild = await prisma.asset.findFirst({
// Generate sort index for new version inside the stack (newest gets the lowest sortIndex)
const firstChild = await prisma.asset.findFirst({
where: { parentId: stackId },
orderBy: { sortIndex: 'desc' },
orderBy: { sortIndex: 'asc' },
})
const newSortIndex = generateSortIndex(lastChild?.sortIndex)
const newSortIndex = generateKeyBetween(null, firstChild?.sortIndex || null)
await prisma.$transaction(async (tx) => {
// Assign sort index
@@ -992,7 +987,7 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const txVersionStackService = new VersionStackService(tx as any)
await txVersionStackService.createVersionStack({
fileIds: [parentFile.id, newFile.id],
fileIds: [newFile.id, parentFile.id],
projectId: parentFile.projectId!,
creatorId: userId,
})