Skip to content

Commit 6769aeb

Browse files
committed
sort snapshots by created at everywhere
1 parent 997185c commit 6769aeb

File tree

6 files changed

+12
-8
lines changed

6 files changed

+12
-8
lines changed

internal-packages/run-engine/src/engine/systems/dequeueSystem.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ export class DequeueSystem {
409409
friendlyId: newSnapshot.friendlyId,
410410
executionStatus: newSnapshot.executionStatus,
411411
description: newSnapshot.description,
412+
createdAt: newSnapshot.createdAt,
412413
},
413414
image: result.deployment?.imageReference ?? undefined,
414415
checkpoint: newSnapshot.checkpoint ?? undefined,

internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export function executionResultFromSnapshot(snapshot: TaskRunExecutionSnapshot):
144144
friendlyId: SnapshotId.toFriendlyId(snapshot.id),
145145
executionStatus: snapshot.executionStatus,
146146
description: snapshot.description,
147+
createdAt: snapshot.createdAt,
147148
},
148149
run: {
149150
id: snapshot.runId,
@@ -162,6 +163,7 @@ export function executionDataFromSnapshot(snapshot: EnhancedExecutionSnapshot):
162163
friendlyId: snapshot.friendlyId,
163164
executionStatus: snapshot.executionStatus,
164165
description: snapshot.description,
166+
createdAt: snapshot.createdAt,
165167
},
166168
run: {
167169
id: snapshot.runId,

internal-packages/run-engine/src/engine/systems/runAttemptSystem.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ export class RunAttemptSystem {
892892
friendlyId: newSnapshot.friendlyId,
893893
executionStatus: newSnapshot.executionStatus,
894894
description: newSnapshot.description,
895+
createdAt: newSnapshot.createdAt,
895896
},
896897
run: {
897898
id: newSnapshot.runId,

packages/cli-v3/src/entryPoints/managed/snapshot.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ function createRunExecutionData(
728728
friendlyId: snapshotFriendlyId,
729729
executionStatus: overrides.executionStatus ?? "EXECUTING",
730730
description: overrides.description ?? "Test snapshot",
731+
createdAt: new Date(),
731732
},
732733
completedWaitpoints: [],
733734
};

packages/cli-v3/src/entryPoints/managed/snapshot.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class SnapshotManager {
199199

200200
// Sort queue:
201201
// 1. Suspendable changes always go to the back
202-
// 2. Snapshot changes are ordered by ID
202+
// 2. Snapshot changes are ordered by creation time, with the latest snapshot last
203203
this.changeQueue.sort((a, b) => {
204204
if (a.change.type === "suspendable" && b.change.type === "snapshot") {
205205
return 1; // a goes after b
@@ -208,17 +208,15 @@ export class SnapshotManager {
208208
return -1; // a goes before b
209209
}
210210
if (a.change.type === "snapshot" && b.change.type === "snapshot") {
211-
// sort snapshot changes by creation time of the latest snapshot, CUIDs are sortable
212-
const aLatestSnapshot = a.change.snapshots[a.change.snapshots.length - 1];
213-
const bLatestSnapshot = b.change.snapshots[b.change.snapshots.length - 1];
211+
const snapshotA = a.change.snapshots[a.change.snapshots.length - 1];
212+
const snapshotB = b.change.snapshots[b.change.snapshots.length - 1];
214213

215-
if (!aLatestSnapshot || !bLatestSnapshot) {
214+
if (!snapshotA || !snapshotB) {
216215
return 0;
217216
}
218217

219-
return aLatestSnapshot.snapshot.friendlyId.localeCompare(
220-
bLatestSnapshot.snapshot.friendlyId
221-
);
218+
// Sort snapshot changes by creation time, old -> new
219+
return snapshotA.snapshot.createdAt.getTime() - snapshotB.snapshot.createdAt.getTime();
222220
}
223221
return 0; // both suspendable, maintain insertion order
224222
});

packages/core/src/v3/schemas/runEngine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const ExecutionSnapshot = z.object({
109109
friendlyId: z.string(),
110110
executionStatus: z.enum(Object.values(TaskRunExecutionStatus) as [TaskRunExecutionStatus]),
111111
description: z.string(),
112+
createdAt: z.coerce.date(),
112113
});
113114

114115
const BaseRunMetadata = z.object({

0 commit comments

Comments
 (0)