Skip to content

refactor(runtime): isolate process and filesystem platform adapters - #881

Open
AaronWangCong wants to merge 2 commits into
Tencent:mainfrom
AaronWangCong:codex/windows-platform-adapters
Open

AaronWangCong wants to merge 2 commits into
Tencent:mainfrom
AaronWangCong:codex/windows-platform-adapters

Conversation

@AaronWangCong

@AaronWangCong AaronWangCong commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Extracts process ownership, file locking, Git index ownership and preview hard-link checks into platform adapters, preserving Unix callers and providing the process, lock and file primitives used by the Windows Runner port in #882. Unix adapters keep the existing process-group, flock, inode and symlink behavior. Windows adapters use kill-on-close Job objects, LockFileEx and native file identity checks.

Part 1 of the Windows execution-machine work from #875. This foundation merges first. After it merges, #882 and #875 will rebase onto the official main branch to remove these shared changes from their comparisons. After #882 merges, #875 will rebase again to the delivery-only changes. Changes to the shared foundation land here and flow to the followers by rebase.

Scope

  • Platform core / API / runner
  • Shared behavior contract

Compatibility

No database or deployment changes. Unix process execution and locking retain their current implementations. Windows Jobs own descendants until the root command exits. Windows runtime/Runner callers and their complete Windows builds follow in #882.

Validation

  • Linux amd64 and Darwin arm64 Runner/Gate builds; Linux runtime test-package compilation.

  • Native Windows process-group, procwatch, preview and lock regression tests.

  • On this exact commit, local Linux parity verification passed 208 tests/subtests across procwatch, Git SSH, previews, runtime locks/identity/index cancellation and HTTP native-file locks. Focused Linux vet checks passed.

  • python scripts/check-public.py, git diff --check and Gitleaks 8.30.1 pass on this exact foundation.

  • Windows CI runs the standalone process/procwatch/preview adapter packages and real lock/index source tests with minimal caller declarations. This isolates the foundation while the Windows runtime callers are introduced in feat(runner): enable Windows task runtimes and isolated SSH #882.

  • Both lock tests explicitly unlock before closing handles; repeated Windows and Linux checks verify contention and reacquisition.

  • Official CI also covers the existing platform, browser, coverage and E2E suites.

Safety

  • No credentials, internal endpoints, production data or private deployment files included.
  • User files and unrelated changes preserved.
  • No Classic generated bundles or copied third-party content involved.

@Yanami-2K Yanami-2K left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unix 侧核对过是等价重构:procwatch 的 Setpgid/Cancel 与 ErrWaitDelay 忽略整体移入 group_unix.go,flock、Nlink、Stat_t.Uid 的判断和错误路径都未变,EWOULDBLOCK 改用 errors.Is 也等价。问题集中在新增的 Windows 逻辑:

  1. Windows 代码在 CI 中既不编译也不测试。本 PR 的目标是「让 Runner 能在 Windows 上编译」,但当前所有检查都跑在 ubuntu/macos 上,internal/processgroup/process_windows.go、internal/procwatch/group_windows.go、internal/preview/archive_links_windows.go、internal/runtime/workspace_lock_windows.go、internal/httpapi/assistant_native_lock_windows.go 以及对应的 *_windows_test.go 全部没有被 CI 编译或执行,只有描述里的本地结果。合入后任何人改动 Unix 签名都可能静默打碎 Windows 构建。建议至少在 .github/workflows/platform.yml 加一个 windows-latest 作业,跑 go build ./cmd/...(Runner/Gate)和 go test ./internal/processgroup ./internal/procwatch ./internal/preview ./internal/runtime -run 'Windows|Lock|Hardlink';不方便加 Windows runner 的话,最低要求是在 Linux 上跑 GOOS=windows go vet 和 GOOS=windows go test -c 覆盖这些包。

  2. 测试依赖「关闭句柄立即释放 LockFileEx」,在 Windows 上有间歇失败风险。internal/runtime/workspace_lock_test.go 末尾 second.Close() 后立刻断言 workspaceInUse 为 false,internal/httpapi/assistant_native_lock_test.go 的 second.Close() 后立刻让子进程期望 available。Windows 文档明确说关闭句柄时由系统解除锁,但时机取决于系统资源,不保证同步。建议测试里先显式 unlock 再 Close,或者对「只关闭句柄」的断言加上有限重试;生产代码 unlockFile 已经是先解锁再关闭,不受影响。

  3. 与 #882 / 草稿 #875 的关系:三个 PR 含有同一套底层适配代码。保留本 PR 作为底层先合入是合理的,请在描述里写明合入后 #882 和 #875 会 rebase 去掉重复部分,避免三处各自修改同一段代码。

说明:internal/processgroup/process_windows.go(resumeInitialThread/Kill/Alive 部分)、process_windows_test.go、procwatch_windows_test.go 的 patch 被工具截断,这几段没有看全。

@Yanami-2K Yanami-2K left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

逐个核对了 Unix 侧是否「行为不变」:workspace_lock_unix.go/assistant_native_lock_unix.go 仍是 Flock(LOCK_EX|LOCK_NB),繁忙判断从 == 改为 errors.Is 等价,openAssistantNativeSession 失败时仍先 Close 再返回 errAssistantNativeBusy;procwatch/group_unix.go 仍设 Setpgid 和对进程组的 SIGKILL Cancel,并把原来在 Run 里对 exec.ErrWaitDelay 的忽略原样保留;preview 硬链接检查和 gitIndexLockHeldUnder 的 UID 读取只是搬到平台文件,判断条件不变。Windows 侧用 kill-on-close Job + 挂起启动后再入 Job,避免子进程逃逸;cleanup 先关 Job 再等 root 退出,不会死等;硬链接元数据读取失败时 fail closed,且有原生 Windows 测试(进程树取消、未托管进程不被误杀、长路径、元数据缺失),CI 含 Windows 作业全绿。作为 #882/#875 的前置抽象,动机成立,建议先合入。

非阻塞建议:

  • internal/runtime/git_index_lock_windows.go 返回 ok=false 后,gitIndexLockHeldUnder 报的是「index lock ... is not a regular file」,在 Windows 上文案会误导排查,建议对「平台无法确认属主」单独给出错误信息。
  • .github/workflows/platform-adapters-windows.yml 用正则从 sandbox_process.go 抽取声明、拷贝源文件到临时包里测试,源文件一旦重排就会失败。请在 #882 合入后尽快换成直接测试真实包。

@Robert0622

Copy link
Copy Markdown
Collaborator

LoopForge PR Review — head 3b456dceb1014f4dd44e2197f10d699c9f6c7e56

Verdict: ✅ Pass

  • First review. Scope: 28 files, +1002/-44 (base dd56585 → head 3b456dc), foundation-only step of the Windows execution-machine effort (companion to feat(runner): enable Windows task runtimes and isolated SSH #882/feat(runner): deliver Windows execution machine lifecycle #875). Adds Windows adapters for: process-tree kill via Job Objects (internal/processgroup/process_windows.go), process-group watch (internal/procwatch/group_windows.go), workspace/git-index file locking via LockFileEx (internal/runtime/workspace_lock_windows.go, git_index_lock_windows.go), long-path-safe preview archive extraction (internal/preview/archive_links_windows.go), and a native-tool session lock (internal/httpapi/assistant_native_lock_windows.go), plus a new platform-adapters-windows.yml CI job.
  • Checked out the head SHA in an isolated, credential-free sandbox (go1.26.11, no production access). go build/go vet and go test ./internal/preview/... ./internal/processgroup/... ./internal/procwatch/... ./internal/runtime/... (Linux/unix build tags) — all pass, including the new workspace_lock_test.go, assistant_native_lock_test.go (under internal/httpapi) and the archive-link cases.
  • Cross-compiled GOOS=windows GOARCH=amd64 go build ./internal/runtime/...: it still fails on pre-existing unix-only syscall.Setpgid/syscall.Kill usage in browser.go/process.go/sandbox_local.go. Confirmed this is not a regression: the same failure reproduces on base dd56585 before this PR (and that base build additionally fails on git_isolation.go's syscall.Stat_t, which this PR already fixes). This PR's own new Windows files compile fine on their own; the CI workflow added here deliberately copies only the ported files (workspace_lock*.go, git_index_lock_windows*.go, assistant_native_lock_windows.go, plus processgroup/procwatch/preview) into throwaway packages rather than building the whole module, with an inline comment noting the remaining runtime/HTTP callers land in the dependent runtime PR — matches what I verified locally.
  • golang.org/x/sys is already a direct dependency (go.mod v0.49.0); no new module footprint added for the windows-only imports.
  • No blocking issues found for this foundation-only scope.

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.

4 participants