Skip to content

Commit ec1418b

Browse files
committed
fix: commit shim state inside guarded repair
1 parent 2fd538b commit ec1418b

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/codex/shim.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ function rollbackGuardedRefresh(journal: readonly GuardedRefreshJournalEntry[]):
681681
function applyGuardedRefreshTransaction(
682682
operations: readonly GuardedRefreshOperation[],
683683
beforeGuardedRefresh?: (wrapperPath: string, index: number) => void,
684+
commitState?: () => void,
684685
): boolean {
685686
const journal: GuardedRefreshJournalEntry[] = [];
686687
let applyError: Error | null = null;
@@ -716,6 +717,14 @@ function applyGuardedRefreshTransaction(
716717
}
717718
}
718719

720+
if (!fingerprintMismatch && !applyError && commitState) {
721+
try {
722+
commitState();
723+
} catch (error) {
724+
applyError = error instanceof Error ? error : new Error(String(error));
725+
}
726+
}
727+
719728
if (fingerprintMismatch || applyError) {
720729
const rollbackErrors = rollbackGuardedRefresh(journal);
721730
if (applyError || rollbackErrors.length > 0) {
@@ -748,7 +757,23 @@ function installCodexShimInternal(options: InstallCodexShimInternalOptions): { i
748757
if (!operations || operations.length === 0) {
749758
return { installed: false, message: "Codex shim auto-restore deferred because tracked launchers changed." };
750759
}
751-
if (!applyGuardedRefreshTransaction(operations, options.beforeGuardedRefresh)) {
760+
const originalStateBytes = readFileSync(statePath());
761+
const commitState = (): void => {
762+
try {
763+
writeState(primaryState(files));
764+
} catch (writeError) {
765+
try {
766+
writeFileSync(statePath(), originalStateBytes);
767+
} catch (restoreError) {
768+
throw new AggregateError(
769+
[writeError, restoreError],
770+
"Codex shim state commit and restoration failed",
771+
);
772+
}
773+
throw writeError;
774+
}
775+
};
776+
if (!applyGuardedRefreshTransaction(operations, options.beforeGuardedRefresh, commitState)) {
752777
return { installed: false, message: "Codex shim auto-restore deferred because tracked launchers changed." };
753778
}
754779
return {

0 commit comments

Comments
 (0)