-
Notifications
You must be signed in to change notification settings - Fork 1
feat(auth): snapshot before destructive actions #87
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
Open
ndycode
wants to merge
1
commit into
git-plan/13-restore-preview
Choose a base branch
from
git-plan/14-auto-snapshots
base: git-plan/13-restore-preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,10 @@ import { existsSync, promises as fs } from "node:fs"; | |||||||||||||||||||||||||||
| import { tmpdir } from "node:os"; | ||||||||||||||||||||||||||||
| import { dirname, join } from "node:path"; | ||||||||||||||||||||||||||||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||
| deleteSavedAccounts, | ||||||||||||||||||||||||||||
| resetLocalState, | ||||||||||||||||||||||||||||
| } from "../lib/destructive-actions.js"; | ||||||||||||||||||||||||||||
| import { clearQuotaCache, getQuotaCachePath } from "../lib/quota-cache.js"; | ||||||||||||||||||||||||||||
| import { getConfigDir, getProjectStorageKey } from "../lib/storage/paths.js"; | ||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||
|
|
@@ -12,6 +16,7 @@ import { | |||||||||||||||||||||||||||
| deduplicateAccountsByEmail, | ||||||||||||||||||||||||||||
| exportAccounts, | ||||||||||||||||||||||||||||
| formatStorageErrorHint, | ||||||||||||||||||||||||||||
| getNamedBackupsDirectoryPath, | ||||||||||||||||||||||||||||
| getStoragePath, | ||||||||||||||||||||||||||||
| importAccounts, | ||||||||||||||||||||||||||||
| listNamedBackups, | ||||||||||||||||||||||||||||
|
|
@@ -23,6 +28,7 @@ import { | |||||||||||||||||||||||||||
| saveAccounts, | ||||||||||||||||||||||||||||
| setStoragePath, | ||||||||||||||||||||||||||||
| setStoragePathDirect, | ||||||||||||||||||||||||||||
| snapshotAccountStorage, | ||||||||||||||||||||||||||||
| withAccountStorageTransaction, | ||||||||||||||||||||||||||||
| } from "../lib/storage.js"; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -656,6 +662,221 @@ describe("storage", () => { | |||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| describe("account storage snapshots", () => { | ||||||||||||||||||||||||||||
| const testWorkDir = join( | ||||||||||||||||||||||||||||
| tmpdir(), | ||||||||||||||||||||||||||||
| "codex-snapshot-" + Math.random().toString(36).slice(2), | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| let testStoragePath = ""; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| beforeEach(async () => { | ||||||||||||||||||||||||||||
| await fs.mkdir(testWorkDir, { recursive: true }); | ||||||||||||||||||||||||||||
| testStoragePath = join(testWorkDir, "openai-codex-accounts.json"); | ||||||||||||||||||||||||||||
| setStoragePathDirect(testStoragePath); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| afterEach(async () => { | ||||||||||||||||||||||||||||
| setStoragePathDirect(null); | ||||||||||||||||||||||||||||
| vi.restoreAllMocks(); | ||||||||||||||||||||||||||||
| await fs.rm(testWorkDir, { recursive: true, force: true }); | ||||||||||||||||||||||||||||
|
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. P2: Avoid bare Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| it("creates deterministic named backup when accounts exist", async () => { | ||||||||||||||||||||||||||||
| await saveAccounts({ | ||||||||||||||||||||||||||||
| version: 3, | ||||||||||||||||||||||||||||
| activeIndex: 0, | ||||||||||||||||||||||||||||
| accounts: [ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| accountId: "primary", | ||||||||||||||||||||||||||||
| refreshToken: "ref-primary", | ||||||||||||||||||||||||||||
| addedAt: 1, | ||||||||||||||||||||||||||||
| lastUsed: 1, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const fixedNow = Date.UTC(2024, 0, 2, 3, 4, 5); | ||||||||||||||||||||||||||||
| const snapshot = await snapshotAccountStorage({ | ||||||||||||||||||||||||||||
| reason: "delete-saved-accounts", | ||||||||||||||||||||||||||||
| now: fixedNow, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| expect(snapshot?.name).toBe( | ||||||||||||||||||||||||||||
| "accounts-delete-saved-accounts-snapshot-2024-01-02_03-04-05", | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| expect(snapshot?.path && existsSync(snapshot.path)).toBe(true); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| it("skips snapshot when no accounts exist", async () => { | ||||||||||||||||||||||||||||
| const snapshot = await snapshotAccountStorage({ | ||||||||||||||||||||||||||||
| reason: "delete-saved-accounts", | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| expect(snapshot).toBeNull(); | ||||||||||||||||||||||||||||
| expect(existsSync(getNamedBackupsDirectoryPath())).toBe(false); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| it("warn failure policy returns null when snapshot creation fails", async () => { | ||||||||||||||||||||||||||||
| await saveAccounts({ | ||||||||||||||||||||||||||||
| version: 3, | ||||||||||||||||||||||||||||
| activeIndex: 0, | ||||||||||||||||||||||||||||
| accounts: [ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| accountId: "primary", | ||||||||||||||||||||||||||||
| refreshToken: "ref-primary", | ||||||||||||||||||||||||||||
| addedAt: 1, | ||||||||||||||||||||||||||||
| lastUsed: 1, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const failingBackup = vi | ||||||||||||||||||||||||||||
| .fn() | ||||||||||||||||||||||||||||
| .mockRejectedValue(new Error("snapshot failed")); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await expect( | ||||||||||||||||||||||||||||
| snapshotAccountStorage({ | ||||||||||||||||||||||||||||
| reason: "reset-local-state", | ||||||||||||||||||||||||||||
| createBackup: failingBackup, | ||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||
| ).resolves.toBeNull(); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| it("propagates when failure policy is error", async () => { | ||||||||||||||||||||||||||||
| await saveAccounts({ | ||||||||||||||||||||||||||||
| version: 3, | ||||||||||||||||||||||||||||
| activeIndex: 0, | ||||||||||||||||||||||||||||
| accounts: [ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| accountId: "primary", | ||||||||||||||||||||||||||||
| refreshToken: "ref-primary", | ||||||||||||||||||||||||||||
| addedAt: 1, | ||||||||||||||||||||||||||||
| lastUsed: 1, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const failingBackup = vi | ||||||||||||||||||||||||||||
| .fn() | ||||||||||||||||||||||||||||
| .mockRejectedValue(new Error("snapshot failed")); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await expect( | ||||||||||||||||||||||||||||
| snapshotAccountStorage({ | ||||||||||||||||||||||||||||
| reason: "reset-local-state", | ||||||||||||||||||||||||||||
| failurePolicy: "error", | ||||||||||||||||||||||||||||
| createBackup: failingBackup, | ||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||
| ).rejects.toThrow(/snapshot failed/); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| it("creates snapshot before deleteSavedAccounts and preserves named backup", async () => { | ||||||||||||||||||||||||||||
| await saveAccounts({ | ||||||||||||||||||||||||||||
| version: 3, | ||||||||||||||||||||||||||||
| activeIndex: 0, | ||||||||||||||||||||||||||||
| accounts: [ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| accountId: "primary", | ||||||||||||||||||||||||||||
| refreshToken: "ref-primary", | ||||||||||||||||||||||||||||
| addedAt: 1, | ||||||||||||||||||||||||||||
| lastUsed: 1, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await deleteSavedAccounts(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const backupsDir = getNamedBackupsDirectoryPath(); | ||||||||||||||||||||||||||||
| const entries = existsSync(backupsDir) | ||||||||||||||||||||||||||||
| ? await fs.readdir(backupsDir) | ||||||||||||||||||||||||||||
| : []; | ||||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||||
| entries.some((name) => | ||||||||||||||||||||||||||||
| name.startsWith("accounts-delete-saved-accounts-snapshot-"), | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| ).toBe(true); | ||||||||||||||||||||||||||||
| expect(await loadAccounts()).toBeNull(); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| it("creates snapshot before resetLocalState", async () => { | ||||||||||||||||||||||||||||
| await saveAccounts({ | ||||||||||||||||||||||||||||
| version: 3, | ||||||||||||||||||||||||||||
| activeIndex: 0, | ||||||||||||||||||||||||||||
| accounts: [ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| accountId: "primary", | ||||||||||||||||||||||||||||
| refreshToken: "ref-primary", | ||||||||||||||||||||||||||||
| addedAt: 1, | ||||||||||||||||||||||||||||
| lastUsed: 1, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await resetLocalState(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const backupsDir = getNamedBackupsDirectoryPath(); | ||||||||||||||||||||||||||||
| const entries = existsSync(backupsDir) | ||||||||||||||||||||||||||||
| ? await fs.readdir(backupsDir) | ||||||||||||||||||||||||||||
| : []; | ||||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||||
| entries.some((name) => | ||||||||||||||||||||||||||||
| name.startsWith("accounts-reset-local-state-snapshot-"), | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| ).toBe(true); | ||||||||||||||||||||||||||||
| expect(await loadAccounts()).toBeNull(); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| it("captures snapshot before importAccounts when accounts already exist", async () => { | ||||||||||||||||||||||||||||
| const importPath = join(testWorkDir, "import.json"); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await saveAccounts({ | ||||||||||||||||||||||||||||
| version: 3, | ||||||||||||||||||||||||||||
| activeIndex: 0, | ||||||||||||||||||||||||||||
| accounts: [ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| accountId: "existing", | ||||||||||||||||||||||||||||
| refreshToken: "ref-existing", | ||||||||||||||||||||||||||||
| addedAt: 1, | ||||||||||||||||||||||||||||
| lastUsed: 1, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await fs.writeFile( | ||||||||||||||||||||||||||||
| importPath, | ||||||||||||||||||||||||||||
| JSON.stringify({ | ||||||||||||||||||||||||||||
| version: 3, | ||||||||||||||||||||||||||||
| activeIndex: 0, | ||||||||||||||||||||||||||||
| accounts: [ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| accountId: "imported", | ||||||||||||||||||||||||||||
| refreshToken: "ref-import", | ||||||||||||||||||||||||||||
| addedAt: 2, | ||||||||||||||||||||||||||||
| lastUsed: 2, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||
| "utf-8", | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await importAccounts(importPath); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const backupsDir = getNamedBackupsDirectoryPath(); | ||||||||||||||||||||||||||||
| const entries = existsSync(backupsDir) | ||||||||||||||||||||||||||||
| ? await fs.readdir(backupsDir) | ||||||||||||||||||||||||||||
| : []; | ||||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||||
| entries.some((name) => | ||||||||||||||||||||||||||||
| name.startsWith("accounts-import-accounts-snapshot-"), | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| ).toBe(true); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const loaded = await loadAccounts(); | ||||||||||||||||||||||||||||
| expect(loaded?.accounts).toHaveLength(2); | ||||||||||||||||||||||||||||
| expect(loaded?.accounts.map((account) => account.accountId)).toEqual( | ||||||||||||||||||||||||||||
| expect.arrayContaining(["existing", "imported"]), | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| describe("filename migration (TDD)", () => { | ||||||||||||||||||||||||||||
| it("should migrate from old filename to new filename", async () => { | ||||||||||||||||||||||||||||
| // This test is tricky because it depends on the internal state of getStoragePath() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
timestamp collision + silent overwrite on concurrent calls
nowdefaults toDate.now(), which has millisecond resolution, butformatTimestampForSnapshottruncates to seconds. two snapshot calls within the same second (e.g. rapid automated ops, or a second import right after the first) produce the identical filename. becauseforce = trueis the hard-coded default, the secondcreateNamedBackupcall silently overwrites the first snapshot with no error.on windows, an antivirus scanner can hold the file open between the two writes. the first write succeeds, the av locks the file, the second
exportAccountscall throws — and sincefailurePolicy = "warn"is the default in all three call sites in this PR, the error is swallowed and the user proceeds with a destructive action with zero guaranteed checkpoint. there is no regression test that reproduces this race.options:
formatTimestampForSnapshot(add_${date.getUTCMilliseconds().toString().padStart(3,"0")}suffix)Prompt To Fix With AI