Skip to content

Commit d27d03b

Browse files
fix(provider-core): recheck a failed sign-in when settings are applied (pingdotgg#34485)
1 parent 4d52d32 commit d27d03b

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎packages/provider-core/src/server/managedProvider.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,66 @@ describe("makeManagedServerProvider", () => {
472472
).pipe(Effect.provide(layerAlwaysRunTest)),
473473
);
474474

475+
it.effect(
476+
"rechecks an auth failure when settings are applied, even if this provider's did not change",
477+
() =>
478+
Effect.scoped(
479+
Effect.gen(function* () {
480+
const settingsChanges = yield* PubSub.unbounded<
481+
TestSettings & { readonly revision: number }
482+
>();
483+
const checkCalls = yield* Ref.make(0);
484+
const seenRevisions: Array<number> = [];
485+
const releaseInitialCheck = yield* Deferred.make<void>();
486+
const markerApplied = yield* Deferred.make<void>();
487+
const unauthenticatedSnapshot: ServerProvider = {
488+
...refreshedSnapshot,
489+
status: "error",
490+
auth: { status: "unauthenticated" },
491+
message: "Codex CLI is not authenticated. Run `codex login` and try again.",
492+
};
493+
const provider = yield* makeManagedServerProvider<
494+
TestSettings & { readonly revision: number }
495+
>({
496+
resolveMaintenance: () => Effect.succeed(maintenanceCapabilities),
497+
getSettings: Effect.succeed({ enabled: true, revision: 0 }),
498+
streamSettings: Stream.fromPubSub(settingsChanges),
499+
haveSettingsChanged: (previous, next) => {
500+
seenRevisions.push(next.revision);
501+
if (next.revision === 2) Deferred.doneUnsafe(markerApplied, Effect.void);
502+
return previous.enabled !== next.enabled;
503+
},
504+
initialSnapshot: () => Effect.succeed(initialSnapshot),
505+
// The credential is fixed (e.g. `codex login`) after the first, failed probe.
506+
checkProvider: Deferred.await(releaseInitialCheck).pipe(
507+
Effect.andThen(Ref.updateAndGet(checkCalls, (count) => count + 1)),
508+
Effect.map((count) => (count === 1 ? unauthenticatedSnapshot : refreshedSnapshot)),
509+
),
510+
refreshInterval: "1 hour",
511+
});
512+
const initialProbe = yield* provider.streamChanges.pipe(
513+
Stream.take(1),
514+
Stream.runDrain,
515+
Effect.forkChild,
516+
);
517+
yield* Effect.yieldNow;
518+
yield* Deferred.succeed(releaseInitialCheck, undefined);
519+
yield* Fiber.join(initialProbe);
520+
assert.deepStrictEqual(yield* provider.getSnapshot, unauthenticatedSnapshot);
521+
522+
// A settings apply that does not touch this provider's settings, then a marker:
523+
// the consumer is sequential, so the first apply has finished once the marker is seen.
524+
yield* PubSub.publish(settingsChanges, { enabled: true, revision: 1 });
525+
yield* PubSub.publish(settingsChanges, { enabled: true, revision: 2 });
526+
yield* Deferred.await(markerApplied);
527+
528+
assert.deepStrictEqual(seenRevisions, [1, 2]);
529+
assert.isAtLeast(yield* Ref.get(checkCalls), 2);
530+
assert.deepStrictEqual((yield* provider.getSnapshot).auth, refreshedSnapshot.auth);
531+
}),
532+
).pipe(Effect.provide(layerAlwaysRunTest)),
533+
);
534+
475535
it.effect("streams supplemental snapshot updates after the base provider check completes", () =>
476536
Effect.scoped(
477537
Effect.gen(function* () {

‎packages/provider-core/src/server/managedProvider.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,15 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
143143
nextSettings: Settings,
144144
options?: { readonly forceRefresh?: boolean },
145145
) {
146-
const forceRefresh = options?.forceRefresh === true;
147146
const previousSettings = yield* Ref.get(settingsRef);
148-
if (!forceRefresh && !input.haveSettingsChanged(previousSettings, nextSettings)) {
147+
const settingsChanged =
148+
options?.forceRefresh === true || input.haveSettingsChanged(previousSettings, nextSettings);
149+
// Nothing watches credential files, so any settings apply rechecks a failed sign-in.
150+
const { snapshot: currentSnapshot } = yield* Ref.get(snapshotStateRef);
151+
const forceRefresh =
152+
options?.forceRefresh === true ||
153+
(currentSnapshot.enabled && currentSnapshot.auth.status === "unauthenticated");
154+
if (!forceRefresh && !settingsChanged) {
149155
yield* Ref.set(settingsRef, nextSettings);
150156
return yield* Ref.get(snapshotStateRef).pipe(Effect.map((state) => state.snapshot));
151157
}

0 commit comments

Comments
 (0)