fix(auth): write Claude session-refreshed token back to canonical creds
Root cause of recurring "Refresh token expired / invalid_grant" logouts: the main refresh loop and each agent session's Claude CLI share one OAuth token family but refresh independently. Anthropic rotates refresh tokens and revokes the whole family if an already-rotated token is reused, so when a session refreshed mid-turn the canonical copy went stale and its next refresh was rejected — forcing a manual re-login. Add syncClaudeSessionAuthBack (mirrors the existing Codex syncCodexSessionAuthBack): after each Claude turn, if the session's CLAUDE_CONFIG_DIR credentials are strictly newer than canonical (and same subscription), adopt them into the canonical file. writeCredentials then fans the current token out to every session dir, so no stale copy lingers to trigger family revocation. Decision logic extracted to the pure, tested shouldAdoptSessionOAuth. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
type PreparedCodexSessionAuth,
|
||||
} from './agent-runner-environment.js';
|
||||
import { syncCodexSessionAuthBack } from './codex-token-rotation.js';
|
||||
import { syncClaudeSessionAuthBack } from './token-refresh.js';
|
||||
import { runSpawnedAgentProcess } from './agent-runner-process.js';
|
||||
import { getStoredRoomSkillOverrides } from './db.js';
|
||||
export {
|
||||
@@ -119,6 +120,31 @@ function createCodexAuthSessionFinalizer(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* After a Claude agent turn, adopt any token the session's Claude CLI refreshed
|
||||
* (rotated) back into the canonical credentials, so the main refresh loop never
|
||||
* ends up holding an already-rotated token (which Anthropic revokes as a family).
|
||||
* No-op for Codex sessions (sessionClaudeDir undefined).
|
||||
*/
|
||||
function createClaudeAuthSessionFinalizer(
|
||||
sessionClaudeDir: string | undefined,
|
||||
): () => void {
|
||||
let finalized = false;
|
||||
return () => {
|
||||
if (finalized) return;
|
||||
finalized = true;
|
||||
if (!sessionClaudeDir) return;
|
||||
try {
|
||||
syncClaudeSessionAuthBack(sessionClaudeDir);
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
{ err, sessionClaudeDir },
|
||||
'Failed to sync Claude session auth back to canonical credentials',
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export async function runAgentProcess(
|
||||
group: RegisteredGroup,
|
||||
input: AgentInput,
|
||||
@@ -199,6 +225,15 @@ export async function runAgentProcess(
|
||||
const finalizeCodexAuthSessionOnce = createCodexAuthSessionFinalizer(
|
||||
() => codexSessionAuth,
|
||||
);
|
||||
const finalizeClaudeAuthSessionOnce = createClaudeAuthSessionFinalizer(
|
||||
(group.agentType || 'claude-code') === 'codex'
|
||||
? undefined
|
||||
: env.CLAUDE_CONFIG_DIR,
|
||||
);
|
||||
const finalizeAuthSessionOnce = () => {
|
||||
finalizeCodexAuthSessionOnce();
|
||||
finalizeClaudeAuthSessionOnce();
|
||||
};
|
||||
|
||||
// Check if runner is built
|
||||
const distEntry = path.join(runnerDir, 'dist', 'index.js');
|
||||
@@ -255,14 +290,14 @@ export async function runAgentProcess(
|
||||
logsDir,
|
||||
startTime,
|
||||
onOutput,
|
||||
onTerminalStreamedOutputFlushed: finalizeCodexAuthSessionOnce,
|
||||
onTerminalStreamedOutputFlushed: finalizeAuthSessionOnce,
|
||||
})
|
||||
.then((output) => {
|
||||
finalizeCodexAuthSessionOnce();
|
||||
finalizeAuthSessionOnce();
|
||||
resolve(output);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
finalizeCodexAuthSessionOnce();
|
||||
finalizeAuthSessionOnce();
|
||||
logger.error(
|
||||
{ err, processName, chatJid: input.chatJid, runId: input.runId },
|
||||
'Spawned agent process runner failed',
|
||||
|
||||
Reference in New Issue
Block a user