Skip to content

Commit b2e7063

Browse files
committed
fix(server): gate Codex recovery advice for managed sessions
1 parent 5e5c2e0 commit b2e7063

2 files changed

Lines changed: 78 additions & 40 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
915915
Effect.void,
916916
onRequest: (method: string, params: unknown) => Effect.Effect<void> = () => Effect.void,
917917
readChildMetadata?: Parameters<typeof withCodexReplayChildMetadata>[2],
918+
resolveRuntime?: CodexAdapterV2.CodexAdapterV2Options["resolveRuntime"],
918919
) =>
919920
Effect.gen(function* () {
920921
const fileSystem = yield* FileSystem.FileSystem;
@@ -958,6 +959,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
958959
instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID,
959960
settings: DEFAULT_CODEX_SETTINGS,
960961
environment: {},
962+
...(resolveRuntime === undefined ? {} : { resolveRuntime }),
961963
clientFactory,
962964
crypto: yield* Crypto.Crypto,
963965
fileSystem,
@@ -1147,28 +1149,34 @@ describe("CodexAdapterV2 post-settle continuation", () => {
11471149
),
11481150
);
11491151

1150-
it.effect.each([
1151-
{
1152-
code: "unauthorized",
1153-
message:
1154-
"Your access token could not be refreshed because you have since logged out or signed in to another account. Please sign in again.",
1155-
stale: true,
1156-
},
1157-
{
1158-
code: "unauthorized",
1159-
message:
1160-
"Error running remote compact task: Your access token could not be refreshed because you have since logged out or signed in to another account. Please sign in again.",
1161-
stale: true,
1162-
},
1163-
{
1164-
code: "unauthorized",
1165-
message:
1166-
"Your access token could not be refreshed because your refresh token has expired. Please log out and sign in again.",
1167-
stale: false,
1168-
},
1169-
])(
1170-
"marks only external account mismatch for idle recovery: $message",
1171-
({ code, message, stale }) =>
1152+
it.effect.each(
1153+
[
1154+
{
1155+
code: "unauthorized",
1156+
message:
1157+
"Your access token could not be refreshed because you have since logged out or signed in to another account. Please sign in again.",
1158+
stale: true,
1159+
},
1160+
{
1161+
code: "unauthorized",
1162+
message:
1163+
"Error running remote compact task: Your access token could not be refreshed because you have since logged out or signed in to another account. Please sign in again.",
1164+
stale: true,
1165+
},
1166+
{
1167+
code: "unauthorized",
1168+
message:
1169+
"Your access token could not be refreshed because your refresh token has expired. Please log out and sign in again.",
1170+
stale: false,
1171+
},
1172+
].flatMap((failure) =>
1173+
[false, true].flatMap((managed) =>
1174+
[false, true].map((notifyError) => ({ ...failure, managed, notifyError })),
1175+
),
1176+
),
1177+
)(
1178+
"handles auth failure with managed=$managed, notifyError=$notifyError: $message",
1179+
({ code, message, stale, managed, notifyError }) =>
11721180
Effect.scoped(
11731181
Effect.gen(function* () {
11741182
const nativeThreadId = "auth-change-thread";
@@ -1179,19 +1187,23 @@ describe("CodexAdapterV2 post-settle continuation", () => {
11791187
scenario: "external-auth-change",
11801188
entries: [
11811189
...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "hello" }),
1182-
{
1183-
type: "emit_inbound",
1184-
label: "error",
1185-
frame: {
1186-
method: "error",
1187-
params: {
1188-
threadId: nativeThreadId,
1189-
turnId: nativeTurnId,
1190-
error,
1191-
willRetry: false,
1192-
},
1193-
},
1194-
},
1190+
...(notifyError
1191+
? [
1192+
{
1193+
type: "emit_inbound" as const,
1194+
label: "error",
1195+
frame: {
1196+
method: "error",
1197+
params: {
1198+
threadId: nativeThreadId,
1199+
turnId: nativeTurnId,
1200+
error,
1201+
willRetry: false,
1202+
},
1203+
},
1204+
},
1205+
]
1206+
: []),
11951207
{
11961208
type: "emit_inbound",
11971209
label: "failed",
@@ -1208,6 +1220,16 @@ describe("CodexAdapterV2 post-settle continuation", () => {
12081220
},
12091221
],
12101222
}),
1223+
undefined,
1224+
undefined,
1225+
undefined,
1226+
managed
1227+
? Effect.succeed({
1228+
config: DEFAULT_CODEX_SETTINGS,
1229+
environment: {},
1230+
revision: "managed-test",
1231+
})
1232+
: undefined,
12111233
);
12121234
yield* harness.runtime.startTurn(
12131235
makeCodexTestTurnInput({
@@ -1219,10 +1241,17 @@ describe("CodexAdapterV2 post-settle continuation", () => {
12191241
}),
12201242
);
12211243
yield* harness.firstTerminal;
1222-
assert.equal(yield* harness.runtime.hasStaleCredentials ?? Effect.succeed(false), stale);
1244+
assert.equal(
1245+
yield* harness.runtime.hasStaleCredentials ?? Effect.succeed(false),
1246+
stale && !managed,
1247+
);
12231248
assert.equal(harness.terminalEvents()[0]?.status, "failed");
12241249
assert.include(harness.terminalEvents()[0]?.failure?.message, message);
1225-
if (stale) {
1250+
if (managed) {
1251+
assert.isUndefined(harness.runtime.hasStaleCredentials);
1252+
assert.equal(harness.terminalEvents()[0]?.failure?.message, message);
1253+
}
1254+
if (stale && !managed) {
12261255
const replacement = yield* harness.adapter.openSession({
12271256
threadId: harness.threadId,
12281257
providerSessionId: ProviderSessionId.make("auth-change-replacement"),

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,12 @@ function isCodexAccountMismatch(message: string, code: string | null | undefined
345345
);
346346
}
347347

348-
function codexAuthRecoveryMessage(message: string | undefined, code: string | null | undefined) {
349-
return message !== undefined && isCodexAccountMismatch(message, code)
348+
function codexAuthRecoveryMessage(
349+
message: string | undefined,
350+
code: string | null | undefined,
351+
allowAuthRecovery: boolean,
352+
) {
353+
return allowAuthRecovery && message !== undefined && isCodexAccountMismatch(message, code)
350354
? `${message} Let other Codex turns and background work finish, then send your message again. If it still fails, run codex login in this provider's home and restart T3 when Codex is idle.`
351355
: message;
352356
}
@@ -4567,6 +4571,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
45674571
message: codexAuthRecoveryMessage(
45684572
payload.error.additionalDetails?.trim() || payload.error.message,
45694573
notificationCode,
4574+
resolvedRuntime === undefined,
45704575
),
45714576
code: notificationCode,
45724577
class: codexUsageLimitError(notificationCode, payload.error.codexErrorInfo)
@@ -5669,7 +5674,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
56695674
? previousFailure.failure
56705675
: withCodexManagedFailure(
56715676
makeProviderFailure({
5672-
message: codexAuthRecoveryMessage(input.failureMessage, failureCode),
5677+
message: codexAuthRecoveryMessage(
5678+
input.failureMessage,
5679+
failureCode,
5680+
resolvedRuntime === undefined,
5681+
),
56735682
code: failureCode,
56745683
class: failureClass,
56755684
}),

0 commit comments

Comments
 (0)