fix(persistence): keep folder re-tiering and plugin context on named saves

Folder workspace unread writes re-tier sort stamps again, and plugins
hear an active folder's rename, now that those saves name their domain
instead of the whole profile. The folder-note test drains the load's
whole save so it checks the named save.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Brennan Benson
2026-11-09 20:46:38 -08:00
co-authored by Claude Opus 5.5
parent 452a386c3b
commit 17e1ec01c2
7 changed files with 119 additions and 34 deletions
@@ -13,6 +13,12 @@ export const WORKSPACE_LINEAGE_DOMAINS = [
'crossStoreLineageByChildKey'
] as const
/** Records that carry sort stamps and unread flags: worktree metadata and folder workspaces. */
export const WORKSPACE_RECORD_DOMAINS: readonly string[] = [
...WORKTREE_METADATA_DOMAINS,
'folderWorkspaces'
]
/** Repo, project, folder, lineage and SSH catalog domains; listeners read these as a catalog change. */
export const WORKSPACE_CATALOG_SAVE_DOMAINS: readonly string[] = [
'repos',
@@ -304,6 +304,8 @@ describe('Store', () => {
})
const store = await createStore()
// Drain the load's whole save so the edit below saves only what it names.
store.flush()
store.updateFolderWorkspace('fw-1', { diffComments: [] })
store.flush()
@@ -268,6 +268,7 @@ describe('workspace.changed', () => {
written = listener
return () => undefined
},
onStateWritten: () => () => undefined,
delayMs: 10
})
written()
@@ -290,4 +291,35 @@ describe('workspace.changed', () => {
vi.useRealTimers()
}
})
it('re-reads the context after a catalog save such as a folder rename, not a UI save', async () => {
vi.useFakeTimers()
try {
const delegate = pluginRuntimeDelegate(
runtime([{ handle: 'term_a', title: 'zsh' }]),
() => []
)
const events = { wantsEvent: vi.fn(() => true), emitEvent: vi.fn() }
let saved: (domains: readonly string[] | undefined) => void = () => undefined
const stop = watchPluginWorkspaceContext({
delegate,
events,
onSessionWritten: () => () => undefined,
onStateWritten: (listener) => {
saved = listener
return () => undefined
},
delayMs: 10
})
saved(['ui'])
await vi.advanceTimersByTimeAsync(20)
expect(events.emitEvent).not.toHaveBeenCalled()
saved(['folderWorkspaces'])
await vi.advanceTimersByTimeAsync(20)
expect(events.emitEvent).toHaveBeenCalledWith('workspace.changed', expect.anything())
stop()
} finally {
vi.useRealTimers()
}
})
})
+12 -2
View File
@@ -1,4 +1,5 @@
import type { PluginEventName } from '../../shared/plugins/manifest/plugin-manifest'
import { savedWorkspaceCatalog } from '../persistence/profile-save-domains'
import type { PluginAgentStatusChangedPayload } from '../../shared/plugins/events/plugin-events'
import { PLUGIN_WORKSPACE_TERMINAL_LIMIT } from '../../shared/plugins/workers/plugin-host-api'
import type { PluginRuntimeDelegate } from './workers/plugin-host-service-bindings'
@@ -158,6 +159,7 @@ export function watchPluginWorkspaceContext(input: {
delegate: PluginRuntimeDelegate
events: PluginEvents
onSessionWritten: (listener: () => void) => () => void
onStateWritten: (listener: (domains: readonly string[] | undefined) => void) => () => void
delayMs?: number
}): () => void {
let timer: ReturnType<typeof setTimeout> | null = null
@@ -175,11 +177,19 @@ export function watchPluginWorkspaceContext(input: {
input.events.emitEvent('workspace.changed', change)
}
}
const stop = input.onSessionWritten(() => {
const schedule = (): void => {
timer ??= setTimeout(() => void check(), input.delayMs ?? WORKSPACE_CHANGE_DEBOUNCE_MS)
}
const stopSession = input.onSessionWritten(schedule)
// A folder workspace rename reaches the context through the catalog, not the session.
const stopCatalog = input.onStateWritten((domains) => {
if (savedWorkspaceCatalog(domains)) {
schedule()
}
})
return () => {
stop()
stopSession()
stopCatalog()
if (timer) {
clearTimeout(timer)
}
@@ -29,7 +29,7 @@ import {
} from '../../../shared/worktree/host-qualified-identity'
import { anySleepKeyNames } from '../terminal/stop/worktree-terminal-sleep-key'
import { resolveAgentWorkspaceExecutionHostId } from '../../agent-hooks/observations/restored-status-dead-terminal-sweep'
import { WORKTREE_METADATA_DOMAINS } from '../../persistence/profile-save-domains'
import { WORKSPACE_RECORD_DOMAINS } from '../../persistence/profile-save-domains'
import {
isWslMoveProjectBlocked,
wslMoveProjectRefForWorkspace
@@ -106,7 +106,7 @@ export abstract class OrcaRuntimeWithSidebarTemporal extends OrcaRuntimeWithDete
)
// Every unread write (window, phone, CLI, activation) lands in a record; a Done may re-tier.
this.store?.onStateWritten?.((domains) => {
if (domains === undefined || WORKTREE_METADATA_DOMAINS.some((d) => domains.includes(d))) {
if (domains === undefined || WORKSPACE_RECORD_DOMAINS.some((d) => domains.includes(d))) {
this.sortStamps.noteRecordsWritten()
}
})
@@ -4,6 +4,7 @@ import { HEADLESS_RUNTIME_WINDOW_ID } from '../../../shared/runtime-session-cont
import type { WorktreeMeta } from '../../../shared/worktree/meta-types'
import { AgentHookServer } from '../../agent-hooks/server/agent-hook-server'
import { OrcaRuntimeService } from '../orca-runtime'
import { folderWorkspaceKey } from '../../../shared/workspace-scope'
import {
hostWorktreeTarget,
type HostWorktreeTarget
@@ -17,11 +18,15 @@ import {
vi.mock('../../../shared/sidebar-inputs-switch', () => ({ ORCA_SIDEBAR_INPUTS_IS_MAIN: true }))
const WT = 'repo-1::/tmp/worktree-a'
const FOLDER_ID = 'fw-1'
function createHeadlessRuntime(
stored: Pick<WorktreeMeta, 'lastActivityAt' | 'smartTier' | 'smartAt'> = { lastActivityAt: 0 }
stored: Pick<WorktreeMeta, 'lastActivityAt' | 'smartTier' | 'smartAt'> = { lastActivityAt: 0 },
workspaceId = WT
) {
const meta: Record<string, WorktreeMeta> = { [WT]: storeWorktreeMeta(stored) }
// A folder workspace keeps its stamps and unread flag on its own record.
const folder: WorktreeMeta & { id: string } = { ...storeWorktreeMeta(stored), id: FOLDER_ID }
const written = new Set<(domains: readonly string[] | undefined) => void>()
const repo = storeRepo({ id: 'repo-1', path: '/tmp/repo-1', connectionId: null })
const store = {
@@ -36,6 +41,13 @@ function createHeadlessRuntime(
written.forEach((listener) => listener(['worktreeMeta']))
return meta[id]
},
getFolderWorkspace: (id: string) => (id === FOLDER_ID ? folder : undefined),
getFolderWorkspaces: () => [folder],
updateFolderWorkspace: (id: string, updates: Partial<WorktreeMeta>) => {
Object.assign(folder, updates)
written.forEach((listener) => listener(['folderWorkspaces']))
return folder
},
onStateWritten: (listener: (domains: readonly string[] | undefined) => void) => {
written.add(listener)
return () => written.delete(listener)
@@ -45,12 +57,18 @@ function createHeadlessRuntime(
// No window: a server with no client attached must still see its own bells and titles.
runtime.syncWindowGraph(HEADLESS_RUNTIME_WINDOW_ID, {
tabs: [
{ tabId: 'tab-1', worktreeId: WT, title: 'Terminal', activeLeafId: 'pane:1', layout: null }
{
tabId: 'tab-1',
worktreeId: workspaceId,
title: 'Terminal',
activeLeafId: 'pane:1',
layout: null
}
],
leaves: [
{
tabId: 'tab-1',
worktreeId: WT,
worktreeId: workspaceId,
leafId: 'pane:1',
paneRuntimeId: 1,
ptyId: 'pty-1',
@@ -58,7 +76,13 @@ function createHeadlessRuntime(
}
]
})
return { runtime, meta, setWorktreeMeta: store.setWorktreeMeta }
return {
runtime,
meta,
folder,
setWorktreeMeta: store.setWorktreeMeta,
updateFolderWorkspace: store.updateFolderWorkspace
}
}
describe('host sort stamps on a headless runtime', () => {
@@ -115,31 +139,41 @@ describe('host sort stamps on a headless runtime', () => {
})
})
it('re-tiers a read Done beside a Working pane when any writer clears the flag', () => {
vi.useFakeTimers({ now: 80_000 })
try {
const { runtime, meta, setWorktreeMeta } = createHeadlessRuntime()
const server = new AgentHookServer()
runtime.attachAgentStatusStore(server)
const pane = (id: string, state: 'working' | 'done'): void =>
server.ingestTerminalStatus({
paneKey: `tab-1:${id}`,
tabId: 'tab-1',
worktreeId: WT,
payload: { state, prompt: 'task', agentType: 'claude' }
})
pane('11111111-1111-4111-8111-111111111111', 'working')
vi.setSystemTime(90_000)
pane('22222222-2222-4222-8222-222222222222', 'working')
vi.setSystemTime(100_000)
pane('11111111-1111-4111-8111-111111111111', 'done')
expect(meta[WT]).toMatchObject({ isUnread: true, smartTier: 'done', smartAt: 100_000 })
it.each(['worktree', 'folder'] as const)(
're-tiers a read Done beside a Working pane when any writer clears the %s flag',
(kind) => {
vi.useFakeTimers({ now: 80_000 })
try {
const workspaceId = kind === 'folder' ? folderWorkspaceKey(FOLDER_ID) : WT
const { runtime, meta, folder, setWorktreeMeta, updateFolderWorkspace } =
createHeadlessRuntime(undefined, workspaceId)
const record = (): WorktreeMeta => (kind === 'folder' ? folder : meta[WT])
const server = new AgentHookServer()
runtime.attachAgentStatusStore(server)
const pane = (id: string, state: 'working' | 'done'): void =>
server.ingestTerminalStatus({
paneKey: `tab-1:${id}`,
tabId: 'tab-1',
worktreeId: workspaceId,
payload: { state, prompt: 'task', agentType: 'claude' }
})
pane('11111111-1111-4111-8111-111111111111', 'working')
vi.setSystemTime(90_000)
pane('22222222-2222-4222-8222-222222222222', 'working')
vi.setSystemTime(100_000)
pane('11111111-1111-4111-8111-111111111111', 'done')
expect(record()).toMatchObject({ isUnread: true, smartTier: 'done', smartAt: 100_000 })
// A client opened it (the window's metadata write, a phone, workspace activation).
setWorktreeMeta(hostWorktreeTarget({ id: WT, hostId: 'local' }), { isUnread: false })
expect(meta[WT]).toMatchObject({ isUnread: false, smartTier: 'working', smartAt: 90_000 })
} finally {
vi.useRealTimers()
// A client opened it (the window's metadata write, a phone, workspace activation).
if (kind === 'folder') {
updateFolderWorkspace(FOLDER_ID, { isUnread: false })
} else {
setWorktreeMeta(hostWorktreeTarget({ id: WT, hostId: 'local' }), { isUnread: false })
}
expect(record()).toMatchObject({ isUnread: false, smartTier: 'working', smartAt: 90_000 })
} finally {
vi.useRealTimers()
}
}
})
)
})
+2 -1
View File
@@ -304,7 +304,8 @@ export async function initializeMainProcessPlugins(runtime: OrcaRuntimeService):
watchPluginWorkspaceContext({
delegate: pluginDelegate,
events: pluginService,
onSessionWritten: (listener) => store.onWorkspaceSessionWritten(listener)
onSessionWritten: (listener) => store.onWorkspaceSessionWritten(listener),
onStateWritten: (listener) => store.onStateWritten(listener)
})
runtime.onWorktreeLifecycle(emitPluginWorktreeLifecycle)
}