Skip to content

The agent that grows with you

fix(desktop): name pasted composer images with local time, not UTC

Paramon30a8b539d9de
diff --git a/apps/desktop/electron/composer-image-name.test.ts b/apps/desktop/electron/composer-image-name.test.ts
new file mode 100644
index 00000000000..df842d92fe5
--- /dev/null
+++ b/apps/desktop/electron/composer-image-name.test.ts
@@ -0,0 +1,59 @@
+/**
+ * Tests for electron/composer-image-name.ts.
+ *
+ * Run with: vitest run --project electron composer-image-name
+ * (Wired into npm test:desktop:platforms in package.json.)
+ *
+ * Regression (issue #96403): pasted composer screenshots were named with a
+ * UTC timestamp (Date.toISOString()), so the filename disagreed with the
+ * file's mtime, which the OS shows in local time — e.g. 8 hours off for
+ * UTC+8 users. The stamp must be built from the date's LOCAL components.
+ */
+
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { composerImageTimestamp } from './composer-image-name'
+
+// A duck-typed Date whose local and UTC readings deliberately differ, so the
+// test distinguishes local-time formatting from toISOString() on any host,
+// regardless of the machine's own timezone (CI runners often run in UTC,
+// where a real Date could not tell the two apart).
+const LOCAL_VS_UTC_SKEWED = {
+ getFullYear: () => 2026,
+ getMonth: () => 7, // August, 0-based
+ getDate: () => 18,
+ getHours: () => 20,
+ getMinutes: () => 52,
+ getSeconds: () => 56,
+ getMilliseconds: () => 282,
+ toISOString: () => '2026-08-18T12:52:56.282Z'
+} as unknown as Date
+
+test('composerImageTimestamp renders local wall-clock components, not UTC', () => {
+ // Local reading is 20:52:56.282; the UTC reading (what a toISOString()-based
+ // stamp would produce) is 12:52:56.282. The filename must carry the local
+ // one so it agrees with the file's mtime.
+ assert.equal(composerImageTimestamp(LOCAL_VS_UTC_SKEWED), '2026-08-18_20-52-56-282')
+})
+
+test('composerImageTimestamp zero-pads every component', () => {
+ const singleDigitComponents = {
+ getFullYear: () => 2026,
+ getMonth: () => 0, // January
+ getDate: () => 3,
+ getHours: () => 4,
+ getMinutes: () => 5,
+ getSeconds: () => 6,
+ getMilliseconds: () => 7
+ } as unknown as Date
+
+ assert.equal(composerImageTimestamp(singleDigitComponents), '2026-01-03_04-05-06-007')
+})
+
+test('composerImageTimestamp keeps the legacy filename shape for real dates', () => {
+ // Same shape the toISOString()-based stamp produced, so existing tooling
+ // that parses composer_* filenames keeps working.
+ assert.match(composerImageTimestamp(), /^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-\d{3}$/)
+})
diff --git a/apps/desktop/electron/composer-image-name.ts b/apps/desktop/electron/composer-image-name.ts
new file mode 100644
index 00000000000..7df3adce05c
--- /dev/null
+++ b/apps/desktop/electron/composer-image-name.ts
@@ -0,0 +1,30 @@
+/**
+ * Filename stamping for pasted composer images (composer-images/).
+ *
+ * The timestamp embedded in a composer image filename must be the user's
+ * LOCAL wall-clock time: these files are browsed and sorted by humans
+ * alongside their filesystem mtimes, which the OS displays in local time.
+ * Rendering the stamp with Date.toISOString() writes UTC into the filename,
+ * so for anyone off UTC the name disagrees with the file's mtime by the zone
+ * offset (issue #96403 — e.g. 8 hours off for UTC+8 users).
+ *
+ * Machine-facing artifacts (run logs, emergency backups) intentionally keep
+ * UTC stamps; this helper is for the user-facing composer-images/ directory
+ * only.
+ */
+
+function pad(value: number, width: number): string {
+ return String(value).padStart(width, '0')
+}
+
+/**
+ * Format `date` as `YYYY-MM-DD_HH-MM-SS-mmm` in LOCAL time — the same shape
+ * the previous toISOString()-based stamp produced, but in the user's timezone.
+ */
+export function composerImageTimestamp(date: Date = new Date()): string {
+ return (
+ `${date.getFullYear()}-${pad(date.getMonth() + 1, 2)}-${pad(date.getDate(), 2)}` +
+ `_${pad(date.getHours(), 2)}-${pad(date.getMinutes(), 2)}-${pad(date.getSeconds(), 2)}` +
+ `-${pad(date.getMilliseconds(), 3)}`
+ )
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 3022df021fe..b10d96eee17 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -123,6 +123,7 @@ import { shouldAttemptCloudBootCascade } from './cloud-boot-cascade'
import { discoverWithTeamFallback } from './cloud-discovery'
import { createCloudSessionRecovery } from './cloud-session-recovery'
import { installCommandScreenshot } from './command-screenshot'
+import { composerImageTimestamp } from './composer-image-name'
import { writeComposerPaste } from './composer-paste'
import { applyConnectionChange, teardownSshState } from './connection-apply'
import {
@@ -6078,7 +6079,7 @@ async function writeComposerImage(buffer, ext = '.png', name = '') {
const safeExt = /^\.[a-z0-9]{1,5}$/.test(normalizedExt) ? normalizedExt : '.png'
const dir = path.join(app.getPath('userData'), 'composer-images')
await fs.promises.mkdir(dir, { recursive: true })
- const stamp = new Date().toISOString().replace(/[:.]/g, '-').replace('T', '_').replace('Z', '')
+ const stamp = composerImageTimestamp()
const random = crypto.randomBytes(3).toString('hex')
const baseName = String(name || '')