Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions apps/desktop/src/app/session/hooks/use-hermes-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// @vitest-environment jsdom
import { act, renderHook } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { getHermesConfig } from '@/hermes'
import { persistString } from '@/lib/storage'
import { $currentCwd, setCurrentCwd } from '@/store/session'

import { useHermesConfig } from './use-hermes-config'

vi.mock('@/hermes', () => ({
getHermesConfig: vi.fn(),
getHermesConfigDefaults: vi.fn().mockResolvedValue({})
}))

const WORKSPACE_CWD_KEY = 'hermes.desktop.workspace-cwd'

const mockConfig = (config: Record<string, unknown>) =>
vi.mocked(getHermesConfig).mockResolvedValue(config as Awaited<ReturnType<typeof getHermesConfig>>)

describe('useHermesConfig refreshHermesConfig', () => {
beforeEach(() => {
// Reset atoms and localStorage between tests
setCurrentCwd('')
persistString(WORKSPACE_CWD_KEY, null)
})

it('applies terminal.cwd from config even when localStorage has a stale value', async () => {
// Simulate a stale remembered workspace cwd
persistString(WORKSPACE_CWD_KEY, '/Users/old/stale-project')
setCurrentCwd('/Users/old/stale-project')

mockConfig({ terminal: { cwd: '/Users/example/new-workspace' } })

const { result } = renderHook(() =>
useHermesConfig({
activeSessionIdRef: { current: null },
refreshProjectBranch: vi.fn().mockResolvedValue(undefined)
})
)

await act(async () => {
await result.current.refreshHermesConfig()
})

// The configured terminal.cwd must override the stale localStorage value
expect($currentCwd.get()).toBe('/Users/example/new-workspace')
})

it('keeps the active session workspace when a session is running', async () => {
setCurrentCwd('/workspace/attached-project')

mockConfig({ terminal: { cwd: '/Users/example/new-workspace' } })

const { result } = renderHook(() =>
useHermesConfig({
activeSessionIdRef: { current: 'session-1' },
refreshProjectBranch: vi.fn().mockResolvedValue(undefined)
})
)

await act(async () => {
await result.current.refreshHermesConfig()
})

// Config refreshes mid-session must not yank the workspace out from
// under the attached session.
expect($currentCwd.get()).toBe('/workspace/attached-project')
})

it('uses empty string when terminal.cwd is not set and localStorage is empty', async () => {
mockConfig({})

const { result } = renderHook(() =>
useHermesConfig({
activeSessionIdRef: { current: null },
refreshProjectBranch: vi.fn().mockResolvedValue(undefined)
})
)

await act(async () => {
await result.current.refreshHermesConfig()
})

expect($currentCwd.get()).toBe('')
})

it('ignores terminal.cwd when it is "."', async () => {
mockConfig({ terminal: { cwd: '.' } })

const { result } = renderHook(() =>
useHermesConfig({
activeSessionIdRef: { current: null },
refreshProjectBranch: vi.fn().mockResolvedValue(undefined)
})
)

await act(async () => {
await result.current.refreshHermesConfig()
})

expect($currentCwd.get()).toBe('')
})

it('calls refreshProjectBranch with the configured cwd', async () => {
const refreshProjectBranch = vi.fn().mockResolvedValue(undefined)
setCurrentCwd('')

mockConfig({ terminal: { cwd: '/workspace/project-a' } })

const { result } = renderHook(() =>
useHermesConfig({
activeSessionIdRef: { current: null },
refreshProjectBranch
})
)

await act(async () => {
await result.current.refreshHermesConfig()
})

expect(refreshProjectBranch).toHaveBeenCalledWith('/workspace/project-a')
})

it('refreshes the branch for the session cwd (not config) when a session is active', async () => {
const refreshProjectBranch = vi.fn().mockResolvedValue(undefined)
setCurrentCwd('/workspace/attached-project')

mockConfig({ terminal: { cwd: '/Users/example/new-workspace' } })

const { result } = renderHook(() =>
useHermesConfig({
activeSessionIdRef: { current: 'session-1' },
refreshProjectBranch
})
)

await act(async () => {
await result.current.refreshHermesConfig()
})

expect(refreshProjectBranch).toHaveBeenCalledWith('/workspace/attached-project')
})
})
5 changes: 4 additions & 1 deletion apps/desktop/src/app/session/hooks/use-hermes-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export function useHermesConfig({ activeSessionIdRef, refreshProjectBranch }: He
const cwd = (config.terminal?.cwd ?? '').trim()

if (cwd && cwd !== '.') {
setCurrentCwd(prev => prev || cwd)
// Configured terminal.cwd beats a stale remembered workspace cwd
// (#38855) — but never yank the workspace out from under an active
// session; those keep their own cwd until the user detaches.
setCurrentCwd(prev => (activeSessionIdRef.current ? prev : cwd))
void refreshProjectBranch($currentCwd.get() || cwd)
}

Expand Down
1 change: 1 addition & 0 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

# Auto-extracted from noreply emails + manual overrides
AUTHOR_MAP = {
"sahibzada@fastino.ai": "sahibzada-allahyar", # PR #39227 salvage (desktop: configured terminal.cwd overrides a stale remembered workspace-cwd localStorage value when no session is active; #38855)
"jvsantos.cunha@gmail.com": "plcunha", # PR #55300 salvage (gateway: record child gateway peer metadata after a compression session-id rotation and repoint stale sessions.json compression-parent entries to the recovered live child; consolidated in the compression-routing-integrity salvage)
"jakepresent1@gmail.com": "jakepresent", # PR #55721 salvage (gateway: identity-guard stale in-flight compression splits — a late run may publish its compressed child only if its run generation is still current and the session key still points at the run's original parent, so an old run can't overwrite a newer /new or moved binding)
"zhangml@tech.icbc.com.cn": "zmlgit", # PR #54872 salvage (multiplex-profile kanban: route task notifications via the owning profile's adapter + wake the creator agent with a synthetic internal MessageEvent on terminal events)
Expand Down
Loading