fix(tmux): preserve configured fallback port

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-27 13:51:16 +09:00
parent 467248535e
commit c65f90ee09
2 changed files with 60 additions and 16 deletions
+58 -15
View File
@@ -3,7 +3,7 @@ import { describe, test, expect, mock, beforeEach, spyOn, afterAll } from 'bun:t
import type { TmuxConfig } from '../../config/schema'
import type { WindowState, PaneAction } from './types'
import type { ActionResult, ExecuteContext } from './action-executor'
import type { TmuxUtilDeps } from './manager'
import type { TmuxSessionManager as TmuxSessionManagerType, TmuxUtilDeps } from './manager'
import * as sharedModule from '../../shared'
type ExecuteActionsResult = {
@@ -287,24 +287,67 @@ describe('TmuxSessionManager', () => {
test('falls back to default port when serverUrl has port 0', async () => {
// given
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = {
...createMockContext(),
serverUrl: new URL('http://127.0.0.1:0/'),
}
const config = createTmuxConfig({ enabled: true,
layout: 'main-vertical',
main_pane_size: 60,
main_pane_min_width: 80,
agent_pane_min_width: 40, })
const previousOpenCodePort = process.env.OPENCODE_PORT
delete process.env.OPENCODE_PORT
let manager: TmuxSessionManagerType | undefined
try {
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = {
...createMockContext(),
serverUrl: new URL('http://127.0.0.1:0/'),
}
const config = createTmuxConfig({ enabled: true,
layout: 'main-vertical',
main_pane_size: 60,
main_pane_min_width: 80,
agent_pane_min_width: 40, })
// when
const manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
// when
manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
} finally {
if (previousOpenCodePort === undefined) {
delete process.env.OPENCODE_PORT
} else {
process.env.OPENCODE_PORT = previousOpenCodePort
}
}
// then
expect((manager as any).serverUrl).toBe('http://localhost:4096')
})
test('falls back to configured OPENCODE_PORT when serverUrl has port 0', async () => {
// given
const previousOpenCodePort = process.env.OPENCODE_PORT
process.env.OPENCODE_PORT = '5678'
let manager: TmuxSessionManagerType | undefined
try {
mockIsInsideTmux.mockReturnValue(true)
const { TmuxSessionManager } = await import('./manager')
const ctx = {
...createMockContext(),
serverUrl: new URL('http://127.0.0.1:0/'),
}
const config = createTmuxConfig({ enabled: true,
layout: 'main-vertical',
main_pane_size: 60,
main_pane_min_width: 80,
agent_pane_min_width: 40, })
// when
manager = new TmuxSessionManager(ctx, config, mockTmuxDeps)
} finally {
if (previousOpenCodePort === undefined) {
delete process.env.OPENCODE_PORT
} else {
process.env.OPENCODE_PORT = previousOpenCodePort
}
}
// then
expect((manager as any).serverUrl).toBe('http://localhost:5678')
})
})
describe('onSessionCreated', () => {
@@ -1989,7 +2032,7 @@ describe('TmuxSessionManager', () => {
const cleanupPromise = manager.cleanup()
// then
await expect(cleanupPromise).resolves.toBeUndefined()
expect(await cleanupPromise).toBeUndefined()
expect(mockKillTmuxSessionIfExists).toHaveBeenCalledTimes(1)
})
})
+2 -1
View File
@@ -72,7 +72,8 @@ export class TmuxSessionManager {
this.client = ctx.client
this.tmuxConfig = tmuxConfig
this.deps = deps
const fallbackUrl = "http://localhost:4096"
const defaultPort = process.env.OPENCODE_PORT ?? "4096"
const fallbackUrl = `http://localhost:${defaultPort}`
const rawServerUrl = ctx.serverUrl?.toString()
try {
if (rawServerUrl) {