From 3880684b6442f71b999244bf7f49bfc1afc012ba Mon Sep 17 00:00:00 2001 From: Sagar Date: Fri, 25 Sep 2026 22:45:30 +0530 Subject: [PATCH 1/2] fix(rollback): order snapshots deterministically in listSnapshots listSnapshots() is documented as returning "newest first", but it sorted on manifest.timestamp, which new Date().toISOString() only resolves to whole milliseconds. Snapshots created in quick succession therefore share a timestamp, localeCompare returns 0, and the stable sort falls back to fs.readdirSync() order, which is arbitrary. Measured over 300 back-to-back createSnapshot() calls: timestamps tied in 80.7% of runs and listSnapshots()[0] was not the newest in 41.0%, silently breaking the documented contract. This surfaced in CI as an intermittent failure of the "should list available snapshots" assertion in test/unit/rollback.test.js (expected 'second', received 'first'). Store a high-resolution, monotonic createdAt alongside the ISO timestamp and sort on that, falling back to Date.parse(timestamp) for manifests written by older versions so existing snapshot directories keep working, with the snapshot id as a final tie-break for full determinism. After the fix the same 300-call probe reports 0 ties and 0 misorderings. --- src/utils/rollback.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/utils/rollback.js b/src/utils/rollback.js index 366fb9c..84e101d 100644 --- a/src/utils/rollback.js +++ b/src/utils/rollback.js @@ -17,6 +17,17 @@ function ensureDir(dir) { } } +/** + * High-resolution, monotonically increasing wall-clock reading in milliseconds. + * `new Date()` only has millisecond resolution, so two snapshots created in quick + * succession can share a timestamp and become impossible to order. This gives each + * snapshot a distinct sort key. + * @returns {number} + */ +function highResNow() { + return performance.timeOrigin + performance.now(); +} + /** * Creates a snapshot of one or more files for later rollback. * @param {string[]} filePaths - Absolute paths to snapshot. @@ -33,6 +44,7 @@ export function createSnapshot(filePaths, options = {}) { const manifest = { id: snapshotId, timestamp: new Date().toISOString(), + createdAt: highResNow(), reason: options.reason || 'manual', command: options.command || '', files: [], @@ -113,11 +125,19 @@ export function listSnapshots() { return dirs.map((d) => { try { const manifest = JSON.parse(fs.readFileSync(path.join(snapshotDir, d, 'manifest.json'), 'utf8')); - return { id: manifest.id, timestamp: manifest.timestamp, reason: manifest.reason, command: manifest.command, fileCount: manifest.files.length }; + return { id: manifest.id, timestamp: manifest.timestamp, createdAt: manifest.createdAt, reason: manifest.reason, command: manifest.command, fileCount: manifest.files.length }; } catch (_e) { return null; } - }).filter(Boolean).sort((a, b) => b.timestamp.localeCompare(a.timestamp)); + }).filter(Boolean).sort((a, b) => { + // Order by the high-resolution creation time. Manifests written by older + // versions have no createdAt, so fall back to the ISO timestamp; the id is a + // final tie-break so the result is always deterministic. + const at = typeof a.createdAt === 'number' ? a.createdAt : Date.parse(a.timestamp) || 0; + const bt = typeof b.createdAt === 'number' ? b.createdAt : Date.parse(b.timestamp) || 0; + if (at !== bt) return bt - at; + return b.id.localeCompare(a.id); + }); } /** From 4ff3d4a8c2b6144a1a7bad8677d14c8e499d7d91 Mon Sep 17 00:00:00 2001 From: Sagar Date: Fri, 25 Sep 2026 22:46:11 +0530 Subject: [PATCH 2/2] fix(ci): grant contents:write so Auto Release can push its tag The Auto Release job had no permissions block, so it inherited the repository's default read-only GITHUB_TOKEN. release-it bumped the version, created the commit and tag locally, then failed to push them with "Permission to sagar0163/Nebula_cli.git denied to github-actions[bot]" (HTTP 403), which also left its tag-rollback attempt failing the same way. The job needs contents:write for the version-bump push, the v${version} tag, and the GitHub Release it creates. npm publishing is already disabled via "npm.publish": false in .release-it.json, so no registry token is involved. --- .github/workflows/release.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d17db1d..284d042 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,6 +12,10 @@ on: jobs: release: runs-on: ubuntu-latest + # release-it pushes the version-bump commit and tag back to the repo, which the + # default read-only GITHUB_TOKEN cannot do (fails with HTTP 403). + permissions: + contents: write # Skip if commit message contains "release" or "ci skip" if: "!contains(github.event.head_commit.message, 'release') && !contains(github.event.head_commit.message, 'ci skip')" steps: