From 9c20938628eb37de4448a0add656523c417da637 Mon Sep 17 00:00:00 2001 From: yiling Date: Sun, 31 May 2026 10:56:52 +0800 Subject: [PATCH] fix(agent): assign lower sortIndex to newer versions to ensure they are marked as latest --- src/workflow/activities/db.test.ts | 73 ++++++++++++++++++++++++++++++ src/workflow/activities/db.ts | 15 ++---- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/workflow/activities/db.test.ts b/src/workflow/activities/db.test.ts index e4ae528a..413e7511 100644 --- a/src/workflow/activities/db.test.ts +++ b/src/workflow/activities/db.test.ts @@ -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) + }) }) }) diff --git a/src/workflow/activities/db.ts b/src/workflow/activities/db.ts index 9ccc6281..47d9b623 100644 --- a/src/workflow/activities/db.ts +++ b/src/workflow/activities/db.ts @@ -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 { 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, })