-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(archive): use sequence numbers instead of date prefix for deterministic ordering #787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,8 +264,11 @@ export class ArchiveCommand { | |
| } | ||
| } | ||
|
|
||
| // Create archive directory with date prefix | ||
| const archiveName = `${this.getArchiveDate()}-${changeName}`; | ||
| // Create archive directory with sequence number prefix. | ||
| // Note: sequence assignment is non-atomic, but this is a single-user CLI | ||
| // tool where concurrent archive operations are not a realistic scenario. | ||
| const sequenceNumber = await this.getNextSequenceNumber(archiveDir); | ||
| const archiveName = `${sequenceNumber}-${changeName}`; | ||
| const archivePath = path.join(archiveDir, archiveName); | ||
|
|
||
| // Check if archive already exists | ||
|
|
@@ -332,8 +335,27 @@ export class ArchiveCommand { | |
| } | ||
| } | ||
|
|
||
| private getArchiveDate(): string { | ||
| // Returns date in YYYY-MM-DD format | ||
| return new Date().toISOString().split('T')[0]; | ||
| private async getNextSequenceNumber(archiveDir: string): Promise<string> { | ||
| let max = 0; | ||
| try { | ||
| const entries = await fs.readdir(archiveDir); | ||
| for (const entry of entries) { | ||
| const match = entry.match(/^(\d+)-/); | ||
| if (match) { | ||
| const num = parseInt(match[1], 10); | ||
| if (num > max) max = num; | ||
| } | ||
| } | ||
| } catch (error: any) { | ||
| // Archive directory may not exist yet | ||
| if (error?.code !== 'ENOENT') { | ||
| throw error; | ||
| } | ||
| } | ||
| const next = max + 1; | ||
| if (next > 999) { | ||
| throw new Error('Archive sequence overflow (>999). Increase prefix width before continuing.'); | ||
| } | ||
| return String(next).padStart(3, '0'); | ||
| } | ||
|
Comment on lines
+338
to
360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Race condition: if two archive operations run concurrently for different changes, both may read the same max value and use the same sequence number (e.g., both get "006"), breaking deterministic ordering. Consider using atomic operations or file locking, or document that concurrent archiving is unsupported.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a single-user CLI tool — concurrent archive operations aren't a realistic scenario. (Same point discussed in the coderabbit thread above.)
Comment on lines
+338
to
360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider handling concurrent archiving: if two archives run simultaneously, they may read the same max and use duplicate sequence numbers, undermining deterministic ordering. Atomic operations or a note in docs would help.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate of the above — answered in the coderabbit thread. Single-user CLI, concurrency is not applicable. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a newbie. I have a simple question. What happens after 999? Or will there be no situation after 999?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With padStart(3, '0'), once we hit 1000 the prefix becomes 4 digits, which breaks lexical sorting ("1000" sorts before "999"). I'll add an overflow guard that throws a clear error if it exceeds 999. In practice, 999 archived specs should be more than sufficient for any project.