-
-
Notifications
You must be signed in to change notification settings - Fork 95
Canonicalize workspace roots for session visibility #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a5a47f0
0d6ea2d
0b55969
4a603ea
5a33063
4895d26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { spawn, spawnSync, type ChildProcessWithoutNullStreams } from 'node:child_process' | ||
| import { createHash, randomBytes } from 'node:crypto' | ||
| import { mkdtemp, readFile, readdir, rename, rm, mkdir, stat, cp, lstat, readlink, symlink } from 'node:fs/promises' | ||
| import { mkdtemp, readFile, readdir, rename, rm, mkdir, stat, cp, lstat, readlink, symlink, realpath } from 'node:fs/promises' | ||
| import { createReadStream, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' | ||
| import type { IncomingMessage, ServerResponse } from 'node:http' | ||
| import { request as httpRequest } from 'node:http' | ||
|
|
@@ -79,7 +79,7 @@ type ServerRequestReply = { | |
| } | ||
| } | ||
|
|
||
| type WorkspaceRootsState = { | ||
| export type WorkspaceRootsState = { | ||
| order: string[] | ||
| labels: Record<string, string> | ||
| active: string[] | ||
|
|
@@ -1356,7 +1356,10 @@ export async function callRpcWithArchiveRecovery( | |
| params: unknown, | ||
| ): Promise<unknown> { | ||
| try { | ||
| return await appServer.rpc(method, params ?? null) | ||
| const result = await appServer.rpc(method, params ?? null) | ||
| return method === 'thread/list' | ||
| ? await canonicalizeThreadListResponseForRead(result) | ||
| : result | ||
| } catch (error) { | ||
| if (method !== 'thread/archive') { | ||
| throw error | ||
|
|
@@ -4272,6 +4275,101 @@ async function readMergedThreadTitleCache(): Promise<ThreadTitleCache> { | |
| return mergeThreadTitleCaches(persistedCache, sessionIndexCache) | ||
| } | ||
|
|
||
| type PathRealpathResolver = (path: string) => Promise<string> | ||
|
|
||
| async function canonicalizeWorkspaceRootPath( | ||
| value: string, | ||
| pathRealpath: PathRealpathResolver, | ||
| ): Promise<string> { | ||
| if (!isAbsolute(value)) return value | ||
| try { | ||
| return await pathRealpath(value) | ||
| } catch { | ||
| return value | ||
| } | ||
| } | ||
|
|
||
| async function canonicalizeWorkspaceRootPathList( | ||
| values: string[], | ||
| pathRealpath: PathRealpathResolver, | ||
| ): Promise<string[]> { | ||
| return normalizeStringArray(await Promise.all(values.map((value) => canonicalizeWorkspaceRootPath(value, pathRealpath)))) | ||
| } | ||
|
|
||
| export async function canonicalizeWorkspaceRootsStateForRead( | ||
| state: WorkspaceRootsState, | ||
| pathRealpath: PathRealpathResolver = realpath, | ||
| ): Promise<WorkspaceRootsState> { | ||
| const [order, active, projectOrder] = await Promise.all([ | ||
| canonicalizeWorkspaceRootPathList(state.order, pathRealpath), | ||
| canonicalizeWorkspaceRootPathList(state.active, pathRealpath), | ||
| canonicalizeWorkspaceRootPathList(state.projectOrder, pathRealpath), | ||
| ]) | ||
| const labelEntries = await Promise.all( | ||
| Object.entries(state.labels) | ||
| .sort(([first], [second]) => first.localeCompare(second)) | ||
| .map(async ([key, label]) => { | ||
| const canonicalKey = await canonicalizeWorkspaceRootPath(key, pathRealpath) | ||
| return { | ||
| canonicalKey, | ||
| label, | ||
| isCanonicalSource: canonicalKey === key, | ||
| } | ||
| }), | ||
| ) | ||
| const labels: Record<string, string> = {} | ||
| const labelSourceByCanonicalKey = new Map<string, { isCanonicalSource: boolean }>() | ||
| for (const entry of labelEntries) { | ||
| const existing = labelSourceByCanonicalKey.get(entry.canonicalKey) | ||
| if (existing?.isCanonicalSource === true && !entry.isCanonicalSource) continue | ||
| if (existing && existing.isCanonicalSource === entry.isCanonicalSource) continue | ||
| labels[entry.canonicalKey] = entry.label | ||
| labelSourceByCanonicalKey.set(entry.canonicalKey, { | ||
| isCanonicalSource: entry.isCanonicalSource, | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| order, | ||
| labels, | ||
| active, | ||
| projectOrder, | ||
| remoteProjects: state.remoteProjects.map((project) => ({ ...project })), | ||
| } | ||
| } | ||
|
|
||
| async function canonicalizeThreadCwdRecord( | ||
| value: unknown, | ||
| canonicalizeCwd: (cwd: string) => Promise<string>, | ||
| ): Promise<unknown> { | ||
| const record = asRecord(value) | ||
| const cwd = typeof record?.cwd === 'string' ? record.cwd : '' | ||
| if (!record || !cwd) return value | ||
| const canonicalCwd = await canonicalizeCwd(cwd) | ||
| return canonicalCwd === cwd ? value : { ...record, cwd: canonicalCwd } | ||
| } | ||
|
|
||
| export async function canonicalizeThreadListResponseForRead( | ||
| payload: unknown, | ||
| pathRealpath: PathRealpathResolver = realpath, | ||
| ): Promise<unknown> { | ||
| const record = asRecord(payload) | ||
| if (!record || !Array.isArray(record.data)) return payload | ||
| const cwdCanonicalizationByValue = new Map<string, Promise<string>>() | ||
| const canonicalizeCwd = (cwd: string): Promise<string> => { | ||
| let canonicalized = cwdCanonicalizationByValue.get(cwd) | ||
| if (!canonicalized) { | ||
| canonicalized = canonicalizeWorkspaceRootPath(cwd, pathRealpath) | ||
| cwdCanonicalizationByValue.set(cwd, canonicalized) | ||
| } | ||
| return canonicalized | ||
| } | ||
| return { | ||
| ...record, | ||
| data: await Promise.all(record.data.map((item) => canonicalizeThreadCwdRecord(item, canonicalizeCwd))), | ||
| } | ||
| } | ||
|
|
||
| async function readWorkspaceRootsState(): Promise<WorkspaceRootsState> { | ||
| const statePath = getCodexGlobalStatePath() | ||
| let payload: Record<string, unknown> = {} | ||
|
|
@@ -4284,13 +4382,13 @@ async function readWorkspaceRootsState(): Promise<WorkspaceRootsState> { | |
| payload = {} | ||
| } | ||
|
|
||
| return { | ||
| return await canonicalizeWorkspaceRootsStateForRead({ | ||
| order: normalizeStringArray(payload['electron-saved-workspace-roots']), | ||
| labels: normalizeStringRecord(payload['electron-workspace-root-labels']), | ||
| active: normalizeStringArray(payload['active-workspace-roots']), | ||
| projectOrder: normalizeStringArray(payload['project-order']), | ||
| remoteProjects: normalizeRemoteProjects(payload['remote-projects']), | ||
| } | ||
| }) | ||
|
Comment on lines
+4385
to
+4391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win Canonicalize before persisting, not only when reading. This normalizes the API response, but new roots written through Suggested directionasync function writeWorkspaceRootsState(nextState: WorkspaceRootsState): Promise<void> {
+ const canonicalState = await canonicalizeWorkspaceRootsStateForRead(nextState)
const statePath = getCodexGlobalStatePath()
let payload: Record<string, unknown> = {}
try {
const raw = await readFile(statePath, 'utf8')
payload = asRecord(JSON.parse(raw)) ?? {}
} catch {
payload = {}
}
- payload['electron-saved-workspace-roots'] = normalizeStringArray(nextState.order)
- payload['electron-workspace-root-labels'] = normalizeStringRecord(nextState.labels)
- payload['active-workspace-roots'] = normalizeStringArray(nextState.active)
- payload['project-order'] = normalizeStringArray(nextState.projectOrder)
+ payload['electron-saved-workspace-roots'] = normalizeStringArray(canonicalState.order)
+ payload['electron-workspace-root-labels'] = normalizeStringRecord(canonicalState.labels)
+ payload['active-workspace-roots'] = normalizeStringArray(canonicalState.active)
+ payload['project-order'] = normalizeStringArray(canonicalState.projectOrder)
await writeFile(statePath, JSON.stringify(payload), 'utf8')
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| async function writeWorkspaceRootsState(nextState: WorkspaceRootsState): Promise<void> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.