Skip to content

feat(gateway): 新增独立进程骨架与本地IPC探活契约#256

Merged
pionxe merged 9 commits into
1024XEngineer:mainfrom
pionxe:feat/gateway-step1-ipc-skeleton
Apr 14, 2026
Merged

feat(gateway): 新增独立进程骨架与本地IPC探活契约#256
pionxe merged 9 commits into
1024XEngineer:mainfrom
pionxe:feat/gateway-step1-ipc-skeleton

Conversation

@pionxe
Copy link
Copy Markdown
Collaborator

@pionxe pionxe commented Apr 13, 2026

概述

本 PR 新增 NeoCode 的 Gateway 独立进程骨架,初步解耦外部入口与内部运行逻辑。引入本地 IPC 服务端作为统一入口,承接跨端接入的第一层协议处理。当前阶段仅实现 ping -> pong 探活与未实现动作的标准错误返回,为后续 URL Scheme、剪贴板和 CLI/TUI 迁移打基础。

关联 Issue

Closes #250

核心改动

  • 新增 Gateway 入口与目录骨架(cmd/neocode-gatewayinternal/gateway/transport
  • 实现基于 Unix Domain Socket / Windows Named Pipe 的 IPC 服务端监听
  • 定义内部通信的 Ping/Pong 基础消息契约
  • 添加对应的单元测试/跨平台传输测试用例

如何测试 (Test Plan)

  1. 运行 go test ./cmd/neocode-gateway ./internal/gateway/...
  2. 启动守护进程:go run ./cmd/neocode-gateway(或 --listen <自定义地址>)。
  3. 发送 Ping 包:
    • 请求:{"type":"request","action":"ping","request_id":"smoke-1"}
    • 期望响应:{"type":"ack","action":"ping","request_id":"smoke-1","payload":{"message":"pong"}}
  4. 回归旧 CLI:运行 go run ./cmd/neocode --help,确认旧功能不受影响。

影响范围

  • 新增模块cmd/neocode-gatewayinternal/gateway/transportinternal/gateway/server.go
  • 破坏性变更:无(旧 CLI 功能不受影响,Gateway 进程暂未接入主线)
  • 依赖变更:新增 github.com/Microsoft/go-winio(Windows Named Pipe)

@pionxe
Copy link
Copy Markdown
Collaborator Author

pionxe commented Apr 13, 2026

/review -codex

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 13, 2026

Codecov Report

❌ Patch coverage is 84.87654% with 49 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/neocode-gateway/main.go 54.76% 19 Missing ⚠️
internal/gateway/transport/listen_unix.go 54.76% 12 Missing and 7 partials ⚠️
internal/gateway/server.go 96.00% 8 Missing and 1 partial ⚠️
internal/gateway/transport/address_unix.go 60.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

@pionxe
Copy link
Copy Markdown
Collaborator Author

pionxe commented Apr 13, 2026

/review

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

@pionxe
Copy link
Copy Markdown
Collaborator Author

pionxe commented Apr 13, 2026

@xgopilot 请你解决review中的问题,并将覆盖率提升至百分之八十以上!

@fennoai
Copy link
Copy Markdown

fennoai Bot commented Apr 13, 2026

处理结果

已按你的要求完成两项工作:修复 review 问题,并将覆盖率提升到 80% 以上。

已完成改动

  • 修复 internal/gateway/transport/listen_unix.go 权限副作用:
    • 现在仅当 socket 父目录由当前流程新建时才执行 chmod 0700
    • 对“已存在目录”不再改权,避免误改 /tmp. 等用户目录权限。
    • 新增 ensureSocketDir 统一处理目录存在性/类型校验。
  • 补充测试以覆盖关键分支:
    • cmd/neocode-gateway/main_test.go
    • internal/gateway/transport/address_unix_test.go
    • 扩展 internal/gateway/transport/listen_unix_test.go(新增已有目录不改权、路径非目录、stale socket 清理、cleanup listener 分支等场景)

验证结果

  • go test ./cmd/neocode-gateway ./internal/gateway/... -coverprofile=/tmp/gateway.cover
  • go tool cover -func=/tmp/gateway.cover
  • 总覆盖率:89.0%(已超过 80%)
  • 额外回归:go test ./... 全部通过

推送信息

  • 已提交并推送到当前 PR 分支:pionxe:feat/gateway-step1-ipc-skeleton
  • Commit: 542ba74

View job run

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: pionxe <148670367+pionxe@users.noreply.github.com>
@phantom5099
Copy link
Copy Markdown
Collaborator

/review -codex

Copy link
Copy Markdown

@fennoai fennoai Bot left a comment

Choose a reason for hiding this comment

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

发现了几处需要优先处理的问题:一个可复现的并发竞态(go test -race 可触发)、两个连接层面的可用性/抗滥用风险,以及一处 Windows 测试并发污染风险。其余变更整体结构清晰。

Comment thread internal/gateway/server.go Outdated
continue
}

s.wg.Add(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Serve 在这里执行 s.wg.Add(1),而 Close 会并发进入 s.wg.Wait()(见 server.go:149),存在 WaitGroup 并发误用。这个问题可被 go test -race ./internal/gateway -run TestServeCloseDuringAcceptDoesNotLeakConnection -count 30 复现。建议把“连接登记 + 计数增加 + 关闭态判断”放到同一受保护临界区,确保不会与 Wait 并发交错。

Comment thread internal/gateway/server.go Outdated
continue
}

s.wg.Add(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

每个连接都会启动一个 goroutine,当前没有并发上限或背压策略。在本地 IPC 场景下仍可能被大量连接拖垮(goroutine/FD 持续增长)。建议增加连接并发上限(例如 semaphore)并在超限时快速拒绝。

default:
}

frame, err := decodeFrame(reader)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

读取帧和写回响应都没有 deadline。慢连接或不发送完整帧的客户端会长期占用处理 goroutine(Slowloris 风险)。建议在每轮读/写前设置读写超时,并在活跃连接上刷新 idle timeout。

}
}

func TestNewRestrictedPipeConfigErrorBranch(t *testing.T) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

这组测试会修改包级函数变量(如 currentProcessUserSIDFnwellKnownSIDStringFn 等),但同包内另一些测试使用了 t.Parallel() 并会读取这些全局变量,Windows 下容易出现并发污染和偶发失败。建议对这类“覆写全局钩子”的测试串行执行,或加互斥保护。

@pionxe
Copy link
Copy Markdown
Collaborator Author

pionxe commented Apr 14, 2026

/simplify

@fennoai
Copy link
Copy Markdown

fennoai Bot commented Apr 14, 2026

完成情况

已按触发指令完成:读取历史评论并处理未解决 review 点,随后执行了一轮 code-simplifier 子代理复查。

本次已落地修改

  • 修复 WaitGroup 并发误用:把连接登记与 wg.Add(1) 合并到同一受锁临界区,避免 ServeCloseAdd/Wait 竞态。
  • 增加连接并发上限:新增 MaxConnections 选项及默认值 DefaultMaxConnections,超限连接快速拒绝。
  • 增加读写超时:新增 ReadTimeout/WriteTimeout 选项及默认值,并在每轮读写前设置 deadline,降低慢连接占用风险。
  • 处理 Windows ACL 测试并发污染:移除相关 t.Parallel(),避免覆写包级 hook 时与同包测试并发互扰。
  • 补充/更新测试覆盖:新增连接上限分支、默认参数、读超时与写超时场景测试。

验证结果

  • go test ./cmd/neocode-gateway ./internal/gateway/... 通过
  • go test -race ./internal/gateway -run TestServeCloseDuringAcceptDoesNotLeakConnection -count 30 通过(原竞态已消失)

code-simplifier 结果

  • 已调用子代理执行简化复查。
  • 结论:当前改动已是较小且清晰的修复形态,无需再做额外简化提交。

提交与推送

未完成项

  • Windows 专属测试(//go:build windows)未在当前 Linux 环境实跑,仅完成代码层并发安全修正。

View job run

…rdening

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: pionxe <148670367+pionxe@users.noreply.github.com>
@Cai-Tang-www
Copy link
Copy Markdown
Collaborator

@xgopilot 你确定没有问题了吗
再review一下

@fennoai
Copy link
Copy Markdown

fennoai Bot commented Apr 14, 2026

Review 结果

本轮基于 origin/main...HEAD 进行了二次审查(重点看了 internal/gateway/server.gointernal/gateway/transport/*cmd/neocode-gateway/main.go 及新增测试),未发现新的阻塞级问题

已执行校验

  • go test ./cmd/neocode-gateway ./internal/gateway/... 通过

残余风险与测试缺口

  • 当前环境为 Linux,//go:build windows 相关用例(Named Pipe + ACL)未实机执行;建议在 Windows CI/本地再跑一轮 go test ./internal/gateway/transport -run Windows -v(或等效矩阵)确认平台侧行为。

View job run

@pionxe pionxe merged commit cadee4c into 1024XEngineer:main Apr 14, 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.

[EPIC-GW-01] 搭建 Gateway 独立进程骨架与 IPC 通信契约

4 participants