Skip to content

修复 workspace 浏览页初始化后不加载目录#605

Merged
tiann merged 3 commits intotiann:mainfrom
sleepinginsummer:codex/fix-workspace-browser-load
May 10, 2026
Merged

修复 workspace 浏览页初始化后不加载目录#605
tiann merged 3 commits intotiann:mainfrom
sleepinginsummer:codex/fix-workspace-browser-load

Conversation

@sleepinginsummer
Copy link
Copy Markdown
Contributor

背景

/browse 页面在 runner 已经通过 --workspace-root 上报 workspace roots 的情况下,仍可能停留在「No subdirectories found / 未找到子目录」空态。

实际排查时,后端 list-directory RPC 能正常返回目录列表,机器 metadata 中也包含正确的 workspaceRoots,问题发生在前端 WorkspaceBrowser 的状态初始化流程。

问题原因

WorkspaceBrowser 中有两个 effect 会同时影响浏览根目录状态:

  • 一个 effect 根据当前机器的 workspaceRoots 设置 selectedRoot
  • 另一个 effect 在 machineId 变化时重置视图,并把 selectedRoot 清空。

当机器数据加载或切换机器时,后一个重置逻辑可能把刚设置好的 selectedRoot 清掉,导致自动加载目录的 effect 因 selectedRoot 为空而直接返回。最终 UI 没有 currentPath,也没有触发 list-directory,只显示空态。

修复内容

切换机器时不再把 selectedRoot 置空,而是同步设置为当前机器的第一个 workspaceRoot,同时保留清空 currentPathentrieserror 的重置行为。

这样自动加载 effect 可以继续以新的 root 作为入口请求目录,避免页面卡在空态。

验证

  • 本地执行 bun run typecheck:web 通过。
  • 本地执行 bun run build:web 通过。
  • 手动验证后端 list-directory 对同一 workspace root 能返回目录数据,修复点限定在前端状态同步逻辑。

@sleepinginsummer
Copy link
Copy Markdown
Contributor Author

已经实际部署验证过,修复之后没问题

@sleepinginsummer sleepinginsummer marked this pull request as ready for review May 9, 2026 10:33
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • [Major] workspaceRoots 的空数组 fallback 会触发无限重置 — workspaceRootsselectedMachine?.metadata?.workspaceRoots 为空时每次 render 都会创建新的 [],而这个 effect 现在依赖它并且每次都会 setEntries([]) 写入新的数组状态。选择一台未配置 workspace roots 的机器,或机器数据尚未加载到 roots 时,effect 会 render -> 新 workspaceRoots 引用 -> setEntries([]) -> render 循环,导致 /browse 空态也可能卡死。证据:web/src/components/WorkspaceBrowser.tsx:236
    Suggested fix:
    const workspaceRoots = useMemo(
        () => selectedMachine?.metadata?.workspaceRoots ?? [],
        [selectedMachine?.metadata?.workspaceRoots]
    )

Summary

  • Review mode: initial
  • 发现 1 个需要修复的问题,集中在本次修改后的 effect 依赖。

Testing

  • Not run (automation)

Comment thread web/src/components/WorkspaceBrowser.tsx Outdated
setEntries([])
setError(null)
}, [machineId])
}, [machineId, workspaceRoots])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Major] workspaceRoots 在没有 roots 时来自 selectedMachine?.metadata?.workspaceRoots ?? [],每次 render 都会得到新的空数组引用。这个 effect 现在依赖它,并且无条件 setEntries([]) 写入新的数组状态,所以未配置 workspace roots 或 roots 尚未加载时会进入 render 循环,/browse 的空态也可能卡死。

Suggested fix:

const workspaceRoots = useMemo(
    () => selectedMachine?.metadata?.workspaceRoots ?? [],
    [selectedMachine?.metadata?.workspaceRoots]
)

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review mode: initial

One issue: the new reset effect is now keyed on workspaceRoots as well as machineId, so any machine metadata refresh can clear browsing state even when the user has not switched machines.

Comment thread web/src/components/WorkspaceBrowser.tsx Outdated
setEntries([])
setError(null)
}, [machineId])
}, [machineId, workspaceRoots])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR] workspaceRoots should not be part of this reset trigger. useSSE replaces machine objects on normal sync updates, so this effect can clear currentPath, entries, and error while the user is still browsing the same machine. The root-selection effect above already reacts to root changes, so keep this reset tied to machineId only.\n\nSuggested fix:\ntsx\nuseEffect(() => {\n setSelectedRoot(workspaceRoots[0] ?? null)\n setCurrentPath(null)\n setEntries([])\n setError(null)\n}, [machineId])\n

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review mode: initial

No issues found in the changed lines. The fix matches the existing machine/root sync flow and I did not find a regression in selection or auto-load behavior.

Testing: Not run (review only). Residual risk: WorkspaceBrowser still has no direct automated coverage, so the root-selection path remains untested.

@tiann tiann merged commit 77f6aae into tiann:main May 10, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants