refactor: extract shared utilities, protocol constants, and retry loop

Phase 3: provider-retry.ts — shared Claude rotation loop (SSOT)
  - retryClaudeWithRotation extracted from message-agent-executor + task-scheduler
  - ~200 lines of duplicated retry logic removed

Phase 4: types.ts — AgentOutputPhase + VisiblePhase unified
Phase 5: types.ts — AgentConfig extended with claudeThinking/claudeThinkingBudget

Utilities (utils.ts):
  - getErrorMessage: 14 occurrences of instanceof Error pattern → 1 function
  - readJsonFile/writeJsonFile: 19 occurrences of JSON+fs pattern → 2 functions
  - fetchWithTimeout: 3 occurrences of AbortController pattern → 1 function
  - formatElapsedKorean: deduplicated from task-watch-status + message-turn-controller

Protocol (agent-protocol.ts):
  - OUTPUT_START/END_MARKER centralized (runners keep local copies with SSOT reference)
  - IMAGE_TAG_RE, IPC constants documented

Runner: show "대화 요약 중..." progress message during auto-compact

Net: -137 lines, 354/354 tests passing
This commit is contained in:
Eyejoker
2026-03-25 04:59:49 +09:00
parent b9f98fcc19
commit 5ea3439c5f
24 changed files with 562 additions and 442 deletions

View File

@@ -13,6 +13,7 @@
*/
import fs from 'fs';
import os from 'os';
import { getErrorMessage, readJsonFile } from './utils.js';
import path from 'path';
import { logger } from './logger.js';
@@ -72,13 +73,12 @@ function getCredentialsPath(accountIndex: number): string {
function readCredentials(accountIndex: number): CredentialsFile | null {
const credsPath = getCredentialsPath(accountIndex);
try {
if (!fs.existsSync(credsPath)) return null;
return JSON.parse(fs.readFileSync(credsPath, 'utf-8'));
} catch (err) {
logger.warn({ err, accountIndex }, 'Failed to read Claude credentials');
return null;
if (!fs.existsSync(credsPath)) return null;
const data = readJsonFile<CredentialsFile>(credsPath);
if (!data) {
logger.warn({ accountIndex }, 'Failed to read Claude credentials');
}
return data;
}
function writeCredentials(accountIndex: number, creds: CredentialsFile): void {
@@ -121,7 +121,7 @@ function syncToSessionDirs(credsPath: string): void {
}
} catch (err) {
logger.warn(
{ err: err instanceof Error ? err.message : String(err) },
{ err: getErrorMessage(err) },
'Failed to sync credentials to sessions',
);
}
@@ -165,7 +165,7 @@ function updateEnvTokens(): void {
logger.debug('Updated .env with refreshed tokens');
} catch (err) {
logger.warn(
{ err: err instanceof Error ? err.message : String(err) },
{ err: getErrorMessage(err) },
'Failed to update .env with refreshed tokens',
);
}
@@ -208,7 +208,7 @@ async function refreshToken(
);
} catch (err) {
logger.warn(
{ url: TOKEN_URL, err: err instanceof Error ? err.message : String(err) },
{ url: TOKEN_URL, err: getErrorMessage(err) },
'Token refresh request error',
);
}
@@ -276,7 +276,7 @@ async function checkAndRefreshAccount(
logger.error(
{
accountIndex,
err: err instanceof Error ? err.message : String(err),
err: getErrorMessage(err),
},
'Failed to refresh Claude OAuth token — manual re-login may be required',
);