Files
orca/src/shared/pty-session-id-format.ts
T
e0e79f1ccd fix(resource-manager): show saved folder workspace names and groups (#20324)
* fix(resource-manager): resolve folder workspace names and groups

* fix: recover local folder PTY attribution after restart

* fix(resource-manager): keep ambiguous-id rows and open folder rows

Ambiguity filtering removed both rows of a workspace-id collision from
worktreeById, so step 3 of the merge dropped browser-only rows for any
id present on two execution hosts. Carry ambiguity as a separate
MergeContext signal that gates only folder host/name attribution; the
existence check and the old repo-level host default are unchanged.

Folder-workspace rows rendered as enabled buttons but navigateToWorktree
resolved only worktrees, so clicks were a silent no-op. Route folder
keys through activateAndRevealWorkspace, which owns host selection and
path-status gating.

* test(resource-manager): repair the merge-call ratchet anchor

The ambiguous-id fix added `ambiguousWorktreeIds` after `worktreeById` in the
mergeSnapshotAndSessions call, so the parity test's end anchor no longer matched:
indexOf returned -1 and slice(start, -1) silently widened the scan to the rest of
the file. The test still passed but stopped pinning the merge call site.

Verified: removing `...resourceSessionBindings` now fails the test again.

---------

Co-authored-by: m4air <m4air@Mac.localdomain>
Co-authored-by: Neil <neil@stably.ai>
2026-09-12 20:35:48 -07:00

47 lines
1.9 KiB
TypeScript

/**
* Shared helpers for the minted PTY session id format.
*
* Why split out of `src/main/daemon/pty-session-id.ts`: the renderer-side
* merge in `mergeSnapshotAndSessions.ts` and the boot-time hydration in
* `attach-main-window-services.ts` both need to recover the owning
* worktreeId from a session id. Three call sites silently re-implementing
* the same parser (one of them looser than the others) was the seed of
* the resource-usage REMOTE-mislabel bug. Centralising the format here
* keeps a single definition that both the main process and the renderer
* can import.
*/
import { parseWorkspaceKey } from './workspace-scope'
export const PTY_SESSION_ID_SEPARATOR = '@@'
export const WORKTREE_ID_SEPARATOR = '::'
/**
* Recover the owning worktreeId from a minted session id.
*
* Why stricter than `lastIndexOf('@@')`: callers that drive memory
* attribution must not synthesize a worktreeId for a sessionId that was
* not minted by us — e.g. a bare UUID. Requiring both the `@@` separator
* AND a Git worktree or folder workspace identity rejects those imposters cleanly.
* Returns `{ worktreeId: null }` when the id does not match the minted
* format.
*/
export function parsePtySessionId(sessionId: string): { worktreeId: string | null } {
const idx = sessionId.lastIndexOf(PTY_SESSION_ID_SEPARATOR)
if (idx <= 0) {
return { worktreeId: null }
}
const candidate = sessionId.slice(0, idx)
if (parseWorkspaceKey(candidate)?.type === 'folder') {
return { worktreeId: candidate }
}
// Why: require non-empty halves on both sides of `::` so degenerate
// ids like `::@@…`, `repo::@@…`, or `::path@@…` don't synthesize a
// phantom worktreeId for memory attribution.
const sepIdx = candidate.indexOf(WORKTREE_ID_SEPARATOR)
if (sepIdx <= 0 || sepIdx + WORKTREE_ID_SEPARATOR.length >= candidate.length) {
return { worktreeId: null }
}
return { worktreeId: candidate }
}