Skip to content

Commit 75f7c07

Browse files
committed
fix(codex): preserve displaced auth writers
1 parent ea8187d commit 75f7c07

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎src/codex/native-main-refresh-publication.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ export function recoverNativeMainRefreshPublication(context: NativeProfileContex
101101
const { staged, previous } = journalPaths(journal);
102102
const canonical = readExact(journal.targetPath);
103103
const stagedBytes = readExact(staged);
104+
const displacedPath = process.platform === "win32" ? previous : staged;
105+
const rollbackPath = process.platform === "win32" ? staged : previous;
106+
const displacedBytes = readExact(displacedPath);
104107
if (!canonical) throw new NativeMainRefreshPublicationError();
105108
if (digest(canonical) === journal.expectedSha256 && stagedBytes && digest(stagedBytes) === journal.replacementSha256) {
106109
try {
@@ -112,6 +115,20 @@ export function recoverNativeMainRefreshPublication(context: NativeProfileContex
112115
return;
113116
}
114117
if (digest(canonical) === journal.replacementSha256) {
118+
if (journal.phase === "prepared") {
119+
if (!displacedBytes) throw new NativeMainRefreshPublicationError();
120+
if (digest(displacedBytes) !== journal.expectedSha256) {
121+
try {
122+
restoreFilePreservingTarget(displacedPath, journal.targetPath, rollbackPath);
123+
const restored = readExact(journal.targetPath);
124+
if (!restored || !restored.equals(displacedBytes)) throw new NativeMainRefreshPublicationError();
125+
cleanup(context, journal);
126+
} catch (cause) {
127+
throw new NativeMainRefreshPublicationError({ cause });
128+
}
129+
return;
130+
}
131+
}
115132
try { cleanup(context, journal); } catch (cause) { throw new NativeMainRefreshPublicationError({ cause }); }
116133
return;
117134
}
@@ -157,6 +174,7 @@ export function publishNativeMainRefresh(
157174
targetPath,
158175
process.platform === "win32" ? staged : previous,
159176
);
177+
cleanup(context, journal);
160178
}
161179
throw new NativeMainRefreshPublicationError();
162180
}

‎tests/codex-main-account-refresh.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,68 @@ describe("native main token refresh", () => {
512512
expect(existsSync(join(home, stagedBasename))).toBe(true);
513513
});
514514

515+
test("restores an external writer displaced by an interrupted prepared exchange", () => {
516+
const authPath = join(home, "auth.json");
517+
const transactionId = "11111111-1111-4111-8111-111111111111";
518+
const stagedBasename = `.opencodex-native-main-refresh.${transactionId}.new`;
519+
const previousBasename = `.opencodex-native-main-refresh.${transactionId}.previous`;
520+
const stagedPath = join(home, stagedBasename);
521+
const previousPath = join(home, previousBasename);
522+
const journalPath = join(home, ".opencodex-native-main-refresh.json");
523+
const original = JSON.stringify({ tokens: { access_token: "old-access", refresh_token: "old-refresh" } });
524+
const replacement = JSON.stringify({ tokens: { access_token: "new-access", refresh_token: "new-refresh" } });
525+
const external = JSON.stringify({ tokens: { access_token: "external-access", refresh_token: "external-refresh" } });
526+
const digest = (value: string) => createHash("sha256").update(value).digest("hex");
527+
writeFileSync(authPath, replacement);
528+
writeFileSync(process.platform === "win32" ? previousPath : stagedPath, external);
529+
writeFileSync(journalPath, JSON.stringify({
530+
version: 1,
531+
transactionId,
532+
targetPath: authPath,
533+
stagedBasename,
534+
previousBasename,
535+
phase: "prepared",
536+
expectedSha256: digest(original),
537+
replacementSha256: digest(replacement),
538+
}));
539+
540+
recoverNativeMainRefreshPublication(resolveNativeProfileContext());
541+
542+
expect(readFileSync(authPath, "utf8")).toBe(external);
543+
expect(existsSync(journalPath)).toBe(false);
544+
expect(existsSync(stagedPath)).toBe(false);
545+
expect(existsSync(previousPath)).toBe(false);
546+
});
547+
548+
test("retains an unprovable prepared replacement without a displaced artifact", () => {
549+
const authPath = join(home, "auth.json");
550+
const transactionId = "11111111-1111-4111-8111-111111111111";
551+
const stagedBasename = `.opencodex-native-main-refresh.${transactionId}.new`;
552+
const previousBasename = `.opencodex-native-main-refresh.${transactionId}.previous`;
553+
const journalPath = join(home, ".opencodex-native-main-refresh.json");
554+
const original = JSON.stringify({ tokens: { access_token: "old-access", refresh_token: "old-refresh" } });
555+
const replacement = JSON.stringify({ tokens: { access_token: "new-access", refresh_token: "new-refresh" } });
556+
const digest = (value: string) => createHash("sha256").update(value).digest("hex");
557+
writeFileSync(authPath, replacement);
558+
writeFileSync(journalPath, JSON.stringify({
559+
version: 1,
560+
transactionId,
561+
targetPath: authPath,
562+
stagedBasename,
563+
previousBasename,
564+
phase: "prepared",
565+
expectedSha256: digest(original),
566+
replacementSha256: digest(replacement),
567+
}));
568+
569+
expect(() => recoverNativeMainRefreshPublication(resolveNativeProfileContext())).toThrow(
570+
"Native credential refresh could not be published",
571+
);
572+
573+
expect(readFileSync(authPath, "utf8")).toBe(replacement);
574+
expect(existsSync(journalPath)).toBe(true);
575+
});
576+
515577
test("cleans committed recovery journals with zero, one, and multiple exact remnants", () => {
516578
const original = JSON.stringify({ tokens: { refresh_token: "old-refresh", account_id: "account-main" } });
517579
const replacement = JSON.stringify({ tokens: { access_token: "access-b", refresh_token: "refresh-b", account_id: "account-main" } });

0 commit comments

Comments
 (0)