Skip to content

fix(spawn): 稳定子进程 cwd 并统一错误映射#254

Closed
jlovec wants to merge 1 commit into
tiann:mainfrom
jlovec:contrib/spawn-fix-pr
Closed

fix(spawn): 稳定子进程 cwd 并统一错误映射#254
jlovec wants to merge 1 commit into
tiann:mainfrom
jlovec:contrib/spawn-fix-pr

Conversation

@jlovec
Copy link
Copy Markdown
Contributor

@jlovec jlovec commented Mar 6, 2026

背景

新建会话时,前端可能收到 Unexpected spawn result,根因是 CLI 子进程在容器/特定 cwd 下路径解析不稳定,以及 Hub 侧对部分 spawn 错误形态缺少归一化。

变更说明

  • CLI 侧:为 bun 子进程显式设置 --cwd projectRoot,稳定路径解析行为。
  • Hub 侧:扩展 spawn 错误归一化,覆盖 error 字段与目录审批请求形态,统一返回可消费错误。

影响范围

  • 会话创建链路的错误可观测性与稳定性提升。
  • 不涉及协议破坏性变更。

验证

  • 手工验证:复现场景下不再出现前端 Unexpected spawn result
  • 回归检查:现有会话创建路径可正常工作。

return {
command: process.execPath,
args: [entrypoint, ...args]
args: ['--cwd', projectRoot, entrypoint, ...args]
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] 运行时 cwd 被强制为项目根目录,可能破坏会话工作目录

Why this is a problem: 这里新增的 --cwd projectRoot 会覆盖 spawnHappyCLI 传入的 cwd(runner 启动会话时依赖该 cwd),导致 bun 开发模式下 CLI 进程 process.cwd() 固定为项目根目录,实际会话可能在错误目录启动。

Suggested fix:

// allow caller to preserve intended session cwd when needed
export function getHappyCliCommand(args: string[], workingDir?: string): HappyCliCommand {
    ...
    if (isBunRuntime) {
        const bunCwd = workingDir ?? projectRoot;
        return {
            command: process.execPath,
            args: ['--cwd', bunCwd, entrypoint, ...args]
        };
    }
}

// spawnHappyCLI
const desiredCwd = typeof options.cwd === 'string' ? options.cwd : undefined;
const { command, args: spawnArgs } = getHappyCliCommand(args, desiredCwd);

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] Bun 子进程 --cwd projectRoot 会覆盖 spawnHappyCLI 的目标 cwd,开发模式下可能导致会话在错误目录启动 cli/src/utils/spawnHappyCLI.ts:70
    Suggested fix:
    // allow caller to preserve intended session cwd when needed
    export function getHappyCliCommand(args: string[], workingDir?: string): HappyCliCommand {
        ...
        if (isBunRuntime) {
            const bunCwd = workingDir ?? projectRoot;
            return {
                command: process.execPath,
                args: ['--cwd', bunCwd, entrypoint, ...args]
            };
        }
    }
    
    // spawnHappyCLI
    const desiredCwd = typeof options.cwd === 'string' ? options.cwd : undefined;
    const { command, args: spawnArgs } = getHappyCliCommand(args, desiredCwd);

Summary

  • 发现 1 个潜在回归(开发模式会话 cwd 被固定为项目根目录)。

Testing

  • Not run (automation)

@jlovec jlovec closed this Mar 6, 2026
@jlovec jlovec deleted the contrib/spawn-fix-pr branch March 8, 2026 04:12
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.

1 participant