Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix(cli): sort sandbox list by timestamp instead of locale date string (#1573)

Mish Ushakov4990471484fc
diff --git a/.changeset/tricky-months-sort.md b/.changeset/tricky-months-sort.md
new file mode 100644
index 000000000..64a4e2240
--- /dev/null
+++ b/.changeset/tricky-months-sort.md
@@ -0,0 +1,5 @@
+---
+'@e2b/cli': patch
+---
+
+Fix `sandbox list` sorting sandboxes by locale-formatted date string instead of the underlying timestamp, which broke chronological order across single-digit/double-digit month and day boundaries
diff --git a/packages/cli/src/commands/sandbox/list.ts b/packages/cli/src/commands/sandbox/list.ts
index 6e779f3ca..a4c716902 100644
--- a/packages/cli/src/commands/sandbox/list.ts
+++ b/packages/cli/src/commands/sandbox/list.ts
@@ -62,6 +62,23 @@ export const listCommand = new commander.Command('list')
}
})
+export function buildTableRows(sandboxes: SandboxInfo[]) {
+ return sandboxes
+ .slice()
+ .sort(
+ (a, b) =>
+ new Date(a.startedAt).getTime() - new Date(b.startedAt).getTime() ||
+ a.sandboxId.localeCompare(b.sandboxId)
+ )
+ .map((sandbox) => ({
+ ...sandbox,
+ startedAt: new Date(sandbox.startedAt).toLocaleString(),
+ endAt: new Date(sandbox.endAt).toLocaleString(),
+ state: sandbox.state.charAt(0).toUpperCase() + sandbox.state.slice(1), // capitalize
+ metadata: JSON.stringify(sandbox.metadata),
+ }))
+}
+
function renderTable(
sandboxes: SandboxInfo[],
state: components['schemas']['SandboxState'][]
@@ -91,19 +108,7 @@ function renderTable(
{ name: 'metadata', alignment: 'left', title: 'Metadata' },
],
disabledColumns: ['clientID'],
- rows: sandboxes
- .map((sandbox) => ({
- ...sandbox,
- startedAt: new Date(sandbox.startedAt).toLocaleString(),
- endAt: new Date(sandbox.endAt).toLocaleString(),
- state: sandbox.state.charAt(0).toUpperCase() + sandbox.state.slice(1), // capitalize
- metadata: JSON.stringify(sandbox.metadata),
- }))
- .sort(
- (a, b) =>
- a.startedAt.localeCompare(b.startedAt) ||
- a.sandboxId.localeCompare(b.sandboxId)
- ),
+ rows: buildTableRows(sandboxes),
style: {
headerTop: {
left: '',
diff --git a/packages/cli/tests/commands/sandbox/list.test.ts b/packages/cli/tests/commands/sandbox/list.test.ts
new file mode 100644
index 000000000..80c613eb2
--- /dev/null
+++ b/packages/cli/tests/commands/sandbox/list.test.ts
@@ -0,0 +1,60 @@
+import { describe, expect, test } from 'vitest'
+import { SandboxInfo } from 'e2b'
+
+import { buildTableRows } from '../../../src/commands/sandbox/list'
+
+function sandbox(sandboxId: string, startedAt: Date): SandboxInfo {
+ return {
+ sandboxId,
+ templateId: 'template-id',
+ metadata: {},
+ startedAt,
+ endAt: new Date(startedAt.getTime() + 60_000),
+ state: 'running',
+ cpuCount: 2,
+ memoryMB: 512,
+ envdVersion: '0.2.0',
+ }
+}
+
+describe('sandbox list table rows', () => {
+ test('sorts by timestamp, not by locale-formatted date string', () => {
+ // In en-US, "9/1/2026, ..." > "10/1/2026, ..." lexicographically,
+ // so string sorting would put September after October.
+ const september = sandbox('sbx-sep', new Date('2026-09-01T10:00:00Z'))
+ const october = sandbox('sbx-oct', new Date('2026-10-01T09:00:00Z'))
+
+ const rows = buildTableRows([october, september])
+
+ expect(rows.map((r) => r.sandboxId)).toEqual(['sbx-sep', 'sbx-oct'])
+ })
+
+ test('breaks ties on identical timestamps by sandbox ID', () => {
+ const startedAt = new Date('2026-09-01T10:00:00Z')
+ const rows = buildTableRows([
+ sandbox('sbx-b', startedAt),
+ sandbox('sbx-a', startedAt),
+ ])
+
+ expect(rows.map((r) => r.sandboxId)).toEqual(['sbx-a', 'sbx-b'])
+ })
+
+ test('formats dates and state for display', () => {
+ const startedAt = new Date('2026-09-01T10:00:00Z')
+ const [row] = buildTableRows([sandbox('sbx-1', startedAt)])
+
+ expect(row.startedAt).toBe(startedAt.toLocaleString())
+ expect(row.state).toBe('Running')
+ expect(row.metadata).toBe('{}')
+ })
+
+ test('does not mutate the input array', () => {
+ const september = sandbox('sbx-sep', new Date('2026-09-01T10:00:00Z'))
+ const october = sandbox('sbx-oct', new Date('2026-10-01T09:00:00Z'))
+ const input = [october, september]
+
+ buildTableRows(input)
+
+ expect(input.map((s) => s.sandboxId)).toEqual(['sbx-oct', 'sbx-sep'])
+ })
+})