Skip to content

Commit dc22866

Browse files
raysonmengrayson951005@gmail.com
andauthored
fix: 日志路径迁移到 StateDirResolver (#21) (#63)
* fix: 日志路径迁移到 StateDirResolver / migrate adapter log paths from /tmp to StateDirResolver (#21) claude-adapter.ts 和 codex-adapter.ts 移除硬编码 /tmp/agentbridge.log, 通过构造函数注入 logFile 参数,默认值从 StateDirResolver 获取。 bridge.ts 和 daemon.ts 显式传入 stateDir.logFile。 * fix: bridge.ts 启动早期 ensure state dir / ensure state dir before first log write Codex review 反馈:fresh machine 首次启动时 state dir 不存在, 早期日志会被 catch {} 吞掉。在 bridge.ts 顶层加 stateDir.ensure()。 --------- Co-authored-by: rayson951005@gmail.com <raysonmeng@qq.com>
1 parent 83f6c64 commit dc22866

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/bridge.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import { disabledReplyError, type BridgeDisabledReason } from "./bridge-disabled
1010
import type { BridgeMessage } from "./types";
1111

1212
const stateDir = new StateDirResolver();
13+
stateDir.ensure();
1314
const configService = new ConfigService();
1415
const config = configService.loadOrDefault();
1516

1617
const CONTROL_PORT = parseInt(process.env.AGENTBRIDGE_CONTROL_PORT ?? "4502", 10);
1718
const daemonLifecycle = new DaemonLifecycle({ stateDir, controlPort: CONTROL_PORT, log });
1819
const CONTROL_WS_URL = daemonLifecycle.controlWsUrl;
1920

20-
const claude = new ClaudeAdapter();
21+
const claude = new ClaudeAdapter(stateDir.logFile);
2122
const daemonClient = new DaemonClient(CONTROL_WS_URL);
2223

2324
let shuttingDown = false;

src/claude-adapter.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import { EventEmitter } from "node:events";
2222
import { randomUUID } from "node:crypto";
2323
import { appendFileSync } from "node:fs";
24+
import { StateDirResolver } from "./state-dir";
2425
import type { BridgeMessage } from "./types";
2526

2627
export type ReplySender = (msg: BridgeMessage, requireReply?: boolean) => Promise<{ success: boolean; error?: string }>;
@@ -60,14 +61,13 @@ export const CLAUDE_INSTRUCTIONS = [
6061
"- If the reply tool returns a busy error, Codex is still executing — wait and try again later.",
6162
].join("\n");
6263

63-
const LOG_FILE = "/tmp/agentbridge.log";
64-
6564
export class ClaudeAdapter extends EventEmitter {
6665
private server: Server;
6766
private notificationSeq = 0;
6867
private sessionId: string;
6968
private readonly notificationIdPrefix: string;
7069
private replySender: ReplySender | null = null;
70+
private readonly logFile: string;
7171

7272
// Dual-mode transport
7373
private readonly configuredMode: DeliveryMode;
@@ -76,8 +76,9 @@ export class ClaudeAdapter extends EventEmitter {
7676
private readonly maxBufferedMessages: number;
7777
private droppedMessageCount = 0;
7878

79-
constructor() {
79+
constructor(logFile = new StateDirResolver().logFile) {
8080
super();
81+
this.logFile = logFile;
8182
this.sessionId = `codex_${Date.now()}`;
8283
this.notificationIdPrefix = randomUUID().replace(/-/g, "").slice(0, 12);
8384

@@ -337,7 +338,7 @@ export class ClaudeAdapter extends EventEmitter {
337338
const line = `[${new Date().toISOString()}] [ClaudeAdapter] ${msg}\n`;
338339
process.stderr.write(line);
339340
try {
340-
appendFileSync(LOG_FILE, line);
341+
appendFileSync(this.logFile, line);
341342
} catch {}
342343
}
343344
}

src/codex-adapter.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { spawn, execSync, type ChildProcess } from "node:child_process";
1313
import { createInterface } from "node:readline";
1414
import { EventEmitter } from "node:events";
1515
import { appendFileSync } from "node:fs";
16+
import { StateDirResolver } from "./state-dir";
1617
import type { BridgeMessage } from "./types";
1718
import type { ServerWebSocket } from "bun";
1819
import {
@@ -56,8 +57,6 @@ interface PendingServerResponse {
5657
timestamp: number;
5758
}
5859

59-
const LOG_FILE = "/tmp/agentbridge.log";
60-
6160
interface PendingRequest {
6261
method: AppServerTrackedRequestMethod;
6362
threadId?: string;
@@ -76,6 +75,7 @@ export class CodexAdapter extends EventEmitter {
7675
private nextInjectionId = -1;
7776
private appPort: number;
7877
private proxyPort: number;
78+
private readonly logFile: string;
7979
private tuiConnId = 0; // tracks which TUI connection is "current" (primary)
8080
private connIdCounter = 0; // monotonically increasing counter for unique conn IDs
8181
// Secondary (picker) connections: each gets its own dedicated app-server WS
@@ -107,10 +107,11 @@ export class CodexAdapter extends EventEmitter {
107107
// Generation counter to prevent stale app-server close handlers from interfering
108108
private appServerGeneration = 0;
109109

110-
constructor(appPort = 4500, proxyPort = 4501) {
110+
constructor(appPort = 4500, proxyPort = 4501, logFile = new StateDirResolver().logFile) {
111111
super();
112112
this.appPort = appPort;
113113
this.proxyPort = proxyPort;
114+
this.logFile = logFile;
114115
}
115116

116117
get appServerUrl() { return `ws://127.0.0.1:${this.appPort}`; }
@@ -1240,6 +1241,6 @@ export class CodexAdapter extends EventEmitter {
12401241
private log(msg: string) {
12411242
const line = `[${new Date().toISOString()}] [CodexAdapter] ${msg}\n`;
12421243
process.stderr.write(line);
1243-
try { appendFileSync(LOG_FILE, line); } catch {}
1244+
try { appendFileSync(this.logFile, line); } catch {}
12441245
}
12451246
}

src/daemon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const ATTENTION_WINDOW_MS = parseInt(process.env.AGENTBRIDGE_ATTENTION_WINDOW_MS
4141

4242
const daemonLifecycle = new DaemonLifecycle({ stateDir, controlPort: CONTROL_PORT, log });
4343

44-
const codex = new CodexAdapter(CODEX_APP_PORT, CODEX_PROXY_PORT);
44+
const codex = new CodexAdapter(CODEX_APP_PORT, CODEX_PROXY_PORT, stateDir.logFile);
4545
const attachCmd = `codex --enable tui_app_server --remote ${codex.proxyUrl}`;
4646

4747
let controlServer: ReturnType<typeof Bun.serve> | null = null;

0 commit comments

Comments
 (0)