Files
orca/src/shared/folder-workspaces.ts
T
JinjingandOrca d41cb21e94 Sta 4062 folder note rollback (#14232)
* fix(persistence): keep folder-workspace notes across a build rollback

normalizeFolderWorkspaces rebuilds each FolderWorkspace field-by-field, so the
inline diffComments field #14112 added is dropped by any build that predates it
— and the next full-state write makes the loss durable with no user edit.

Move the on-disk home to an optional top-level PersistedState.folderWorkspaceDiffComments,
which older builds round-trip untouched through their {...defaults, ...parsed} load
spread and omit-style getDurableState(). load() hydrates it onto the records and
deletes it from Store state; buildStateToSave() is the only producer. The in-memory
FolderWorkspace shape, and therefore every IPC/RPC/renderer/mobile path, is unchanged.

Co-authored-by: Orca <help@stably.ai>

* fix(persistence): prefer inline folder notes over a stale map entry

Hydrate preferred a non-empty folderWorkspaceDiffComments entry over non-empty
inline notes. A rollback to a notes-capable #14112 build writes notes inline and
leaves the older map untouched, so re-upgrading deleted everything authored while
rolled back. Inline now wins when present; the map only fills a stripped record.

Co-authored-by: Orca <help@stably.ai>

* Extract folder workspace diff comments to dedicated module

Moves normalizeFolderWorkspaceDiffComments and
collectFolderWorkspaceDiffComments from persistence.ts to a new
folder-workspace-diff-comments.ts module for better code organization.

---------

Co-authored-by: Orca <help@stably.ai>
2026-08-13 00:34:57 -07:00

116 lines
4.4 KiB
TypeScript

import type { FolderWorkspace, ProjectGroup } from './types'
import { isTuiAgent } from './tui-agent-config'
import { normalizeStoredTaskSourceContext } from './task-source-context'
import { normalizeWorkspaceLinkedItem } from './workspace-linked-item'
import { isWorkspaceLinkedItemSourceContextMatch } from './workspace-linked-item-source-context'
import { normalizeWorkspaceCreatorProvenance } from './workspace-creator-provenance'
export function normalizeFolderWorkspaceName(
name: string | null | undefined,
fallback = 'Untitled workspace'
): string {
const trimmed = typeof name === 'string' ? name.trim() : ''
return trimmed.length > 0 ? trimmed : fallback
}
export function normalizeFolderWorkspaces(
value: unknown,
projectGroups: readonly ProjectGroup[]
): FolderWorkspace[] {
if (!Array.isArray(value)) {
return []
}
const folderGroups = new Map<string, ProjectGroup>()
for (const group of projectGroups) {
if (group.parentPath) {
folderGroups.set(group.id, group)
}
}
const workspaces: FolderWorkspace[] = []
const seen = new Set<string>()
for (const candidate of value) {
if (!candidate || typeof candidate !== 'object') {
continue
}
const raw = candidate as Partial<FolderWorkspace>
if (
typeof raw.id !== 'string' ||
raw.id.trim().length === 0 ||
seen.has(raw.id) ||
typeof raw.projectGroupId !== 'string' ||
!folderGroups.has(raw.projectGroupId)
) {
continue
}
const group = folderGroups.get(raw.projectGroupId)
const folderPath =
typeof raw.folderPath === 'string' && raw.folderPath.trim().length > 0
? raw.folderPath
: group?.parentPath
if (!folderPath) {
continue
}
const now = Date.now()
const linkedTask = normalizeWorkspaceLinkedItem(raw.linkedTask)
const linkedTaskSourceContext = normalizeStoredTaskSourceContext(raw.linkedTaskSourceContext)
const creatorProvenance = normalizeWorkspaceCreatorProvenance(raw.creatorProvenance)
seen.add(raw.id)
workspaces.push({
id: raw.id,
projectGroupId: raw.projectGroupId,
name: normalizeFolderWorkspaceName(raw.name),
folderPath,
connectionId:
typeof raw.connectionId === 'string'
? raw.connectionId
: raw.connectionId === null
? null
: (group?.connectionId ?? null),
...(creatorProvenance ? { creatorProvenance } : {}),
linkedTask,
linkedTaskSourceContext: isWorkspaceLinkedItemSourceContextMatch(
linkedTask,
linkedTaskSourceContext
)
? linkedTaskSourceContext
: null,
comment: typeof raw.comment === 'string' ? raw.comment : '',
isArchived: raw.isArchived === true,
isUnread: raw.isUnread === true,
isPinned: raw.isPinned === true,
sortOrder:
typeof raw.sortOrder === 'number' && Number.isFinite(raw.sortOrder) ? raw.sortOrder : now,
...(typeof raw.manualOrder === 'number' && Number.isFinite(raw.manualOrder)
? { manualOrder: raw.manualOrder }
: {}),
...(typeof raw.workspaceStatus === 'string' && raw.workspaceStatus.trim().length > 0
? { workspaceStatus: raw.workspaceStatus }
: {}),
...(isTuiAgent(raw.createdWithAgent) ? { createdWithAgent: raw.createdWithAgent } : {}),
...(raw.pendingFirstAgentMessageRename === true
? { pendingFirstAgentMessageRename: true }
: {}),
...(typeof raw.firstAgentMessageRenameError === 'string'
? { firstAgentMessageRenameError: raw.firstAgentMessageRenameError }
: raw.firstAgentMessageRenameError === null
? { firstAgentMessageRenameError: null }
: {}),
lastActivityAt:
typeof raw.lastActivityAt === 'number' && Number.isFinite(raw.lastActivityAt)
? raw.lastActivityAt
: 0,
createdAt:
typeof raw.createdAt === 'number' && Number.isFinite(raw.createdAt) ? raw.createdAt : now,
updatedAt:
typeof raw.updatedAt === 'number' && Number.isFinite(raw.updatedAt) ? raw.updatedAt : now,
// Legacy read: unreleased #14112 builds wrote notes inline. Canonical home is
// PersistedState.folderWorkspaceDiffComments.
...(Array.isArray(raw.diffComments) ? { diffComments: raw.diffComments } : {})
})
}
return workspaces.sort(
(left, right) => right.sortOrder - left.sortOrder || left.name.localeCompare(right.name)
)
}