From 08f647fa8646b9b49f30af9e9205a338c26cf8d1 Mon Sep 17 00:00:00 2001 From: Luis Date: Mon, 26 Jan 2026 11:52:21 +0800 Subject: [PATCH] fix: use ObjectID timestamp for createdTime sorting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous implementation used `sortOrder` field as a proxy for creation time, but these values (~8.9×10^18) exceed JavaScript's safe integer range (2^53 ≈ 9×10^15), causing precision loss during arithmetic operations and incorrect sorting results. This fix extracts the actual Unix timestamp from MongoDB ObjectID's first 8 hex characters, which provides accurate creation time sorting. Co-Authored-By: Claude Opus 4.5 --- src/tools/task/list-tasks.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/tools/task/list-tasks.ts b/src/tools/task/list-tasks.ts index 51989cd..b274600 100644 --- a/src/tools/task/list-tasks.ts +++ b/src/tools/task/list-tasks.ts @@ -153,10 +153,19 @@ function sortTasks( break; } case "createdTime": { - // Use sortOrder as proxy for creation time - const orderA = a.sortOrder ?? 0; - const orderB = b.sortOrder ?? 0; - comparison = orderA - orderB; + // Extract timestamp from MongoDB ObjectID (first 8 hex chars) + // The sortOrder field values exceed JavaScript's safe integer range (2^53), + // causing precision loss during arithmetic operations. + // ObjectID's first 8 hex characters represent a Unix timestamp. + const getTimestampFromId = (id: string | undefined): number => { + if (typeof id === 'string' && id.length >= 8) { + return parseInt(id.substring(0, 8), 16); + } + return 0; + }; + const timeA = getTimestampFromId(a.id); + const timeB = getTimestampFromId(b.id); + comparison = timeA - timeB; break; } }