Files
HomeAgent/internal/plugin/proc/evtring.go
JianFeeeee 6d38a164eb chore(module): Go module 路径随托管地迁移 gitcode → github
## 背景

仓库早已迁移到 GitHub(`github.com/JianFeeeee/HomeAgent`,CI/CD 都在那边跑),
gitcode 保留为**国内只读镜像**(`ci.yml` 的 sync-gitcode-main 快进推送)。
但 go.mod 的 module 路径一直是 `gitcode.com/JianFeeeee/...` —— 既与权威源
不符,`meta.SourceURL`(AGPL §13 的源码提供入口)也指向镜像而非实际运行的代码。

## 关键区分(这是本次改动安全的前提)

两个 module 是**不同**的,影响面差一个数量级:

| module | 谁依赖 | 改它的后果 |
|---|---|---|
| `.../HomeAgent`(主仓) | **只有主仓自己** | 内部重构,零外部影响 |
| `.../homeagent-sdk`(SDK 仓) | 主仓 49 文件 + 所有外部插件 | 需重编插件 |

★ 实测确认:**插件不 import 主仓 module**(`strings qq/plugin.bin | grep HomeAgent` = 0),
只依赖 SDK 仓。而插件与内核的边界是 **RPC 方法名字符串**
(`plugin.init` / `tool.invoke`…,见 proc/protocol.go),**与 Go module 名无关**。
⇒ module 改名**不改变任何 wire 协议**,现网已装的插件照常运行。

SDK 的 GitHub 仓名是 `homeagentsdk`(无连字符),module 路径必须与之精确匹配,
否则 `go get` 会因路径不符失败,故取 `github.com/JianFeeeee/homeagentsdk`。

## 改动

- `go.mod` / `go.sum`:两个 module 路径 + require/replace
- 277 个 Go 文件 + Makefile + 打包脚本的 import 与 ldflags 路径
- SDK 仓 `meta.CoreModule`、`tools/hmapdev` 的 module 声明与工程模板
- 对外文档/链接的仓库地址(README、PLUGIN_DEV、OVERVIEW 等)

## 明确**保留** gitcode 的地方(镜像机制,不能动)

- `GITCODE_TOKEN`、`GC_URL="…@gitcode.com"`(CI 同步 job)
- `gitcode.com/api/v5/...`(release 附件上传)
- git remote `gitcode`(镜像拉取/推送)
- `docs/zh/ci-cd-runbook.md`(整篇讲镜像运维)

## 验证

- `go build ./...` / `go vet ./...` 绿
- `go test ./... -count=1` → 48 包 ok / 0 FAIL
- hmapdev 工具链编译通过(`third_party/homeagent-sdk/tools/hmapdev`)
- 全仓残留检查:module 形态的 `gitcode.com/JianFeeeee` = 0
- 镜像机制抽验:GC_URL / @gitcode / api/v5 / remote 均完好

## 顺带修正

`hmapdev/templates.go` 的注释此前写「gitcode 的模块不在 proxy.golang.org 上」——
module 已迁 GitHub,该论断不再成立。改为如实说明**为何仍默认 replace 到本机 SDK**:
离线可构建、避免版本漂移、内网可用。理由与托管地无关,故 replace 保留。
2026-10-08 10:40:17 +08:00

311 lines
8.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package proc
import (
"encoding/binary"
"encoding/json"
"fmt"
"sync"
"sync/atomic"
"time"
pubsdk "github.com/JianFeeeee/homeagentsdk/sdk"
)
// ---- 事件类型编码(编译时确定,与 pubsdk.EventType 一一对应)----
var evtTypeNames = [evtTypeMax]string{
"raw_input",
"agent_output",
"agent_llm_chain",
"tool_call",
"reasoning",
"stage",
"system",
"reasoning_delta",
"content_delta",
"skill_detected",
}
var evtTypeIndex = map[string]uint32{
"raw_input": evtTypeRawInput,
"agent_output": evtTypeAgentOutput,
"agent_llm_chain": evtTypeAgentLLMChain,
"tool_call": evtTypeToolCall,
"reasoning": evtTypeReasoning,
"stage": evtTypeStage,
"system": evtTypeSystem,
"reasoning_delta": evtTypeReasoningDelta,
"content_delta": evtTypeContentDelta,
"skill_detected": evtTypeSkillDetected,
}
func encodeEvtType(t pubsdk.EventType) uint32 {
if idx, ok := evtTypeIndex[string(t)]; ok {
return idx
}
return 0xFFFFFFFF // 未知类型:子进程 typeMask 用 0 匹配全部,此值不影响
}
func decodeEvtType(idx uint32) pubsdk.EventType {
if int(idx) < len(evtTypeNames) {
return pubsdk.EventType(evtTypeNames[idx])
}
return ""
}
func evtTypeMask(types ...pubsdk.EventType) uint32 {
var mask uint32
for _, t := range types {
if idx, ok := evtTypeIndex[string(t)]; ok {
mask |= 1 << idx
}
}
return mask
}
// ---- 事件环共享段布局(§3.6)----
const (
evtRingMagic uint32 = 0x48455654 // "HEVT"
evtRingVersion uint32 = 1
evtRingCap uint32 = 8192 // 2^13,满足流式场景突发(实验 4)
evtRingSlotLen uint32 = 32 // seq(8)+type(4)+off(4)+len(4)+pad(12)
evtOffMagic uint32 = 0
evtOffVersion uint32 = 4
evtOffWriteSeq uint32 = 8
evtOffCap uint32 = 16
evtOffSlots uint32 = 20
evtTypeRawInput uint32 = 0
evtTypeAgentOutput uint32 = 1
evtTypeAgentLLMChain uint32 = 2
evtTypeToolCall uint32 = 3
evtTypeReasoning uint32 = 4
evtTypeStage uint32 = 5
evtTypeSystem uint32 = 6
evtTypeReasoningDelta uint32 = 7
evtTypeContentDelta uint32 = 8
evtTypeSkillDetected uint32 = 9
evtTypeMax uint32 = 10
evtHeaderSize = 20
evtArenaCap = 64 * 1024
evtTotalSize = int(evtHeaderSize + evtRingCap*evtRingSlotLen + evtArenaCap)
)
// ---- 内核侧:EvtRing ----
type EvtRing struct {
data []byte
writeSeq atomic.Uint64
cap uint32
slotsBase uint32
arenaBase uint32
arenaCap uint32
arenaUsed atomic.Uint32
mu sync.Mutex
}
func NewEvtRing(data []byte) (*EvtRing, error) {
if uint32(len(data)) < evtHeaderSize+evtRingCap*evtRingSlotLen+evtArenaCap {
return nil, fmt.Errorf("事件环段太小:需要 %d,实际 %d", evtTotalSize, len(data))
}
if got := binary.LittleEndian.Uint32(data[evtOffMagic:]); got != evtRingMagic {
return nil, fmt.Errorf("事件环魔数不匹配(0x%x)", got)
}
return &EvtRing{
data: data,
cap: evtRingCap,
slotsBase: evtOffSlots,
arenaBase: evtOffSlots + evtRingCap*evtRingSlotLen,
arenaCap: evtArenaCap,
}, nil
}
func (r *EvtRing) Init() {
binary.LittleEndian.PutUint32(r.data[evtOffMagic:], evtRingMagic)
binary.LittleEndian.PutUint32(r.data[evtOffVersion:], evtRingVersion)
binary.LittleEndian.PutUint32(r.data[evtOffCap:], r.cap)
r.writeSeq.Store(0)
}
// Written 返回已写入的事件条数(含因环满而只标记未落盘的那些)。
//
// 供诊断与测试观测「某次 Publish 是否真的通过了 EventRing」——
// 这比读内部字段稳定,也是关停退订验证所需的可观察量。
func (r *EvtRing) Written() uint64 { return r.writeSeq.Load() }
// WritePush post-and-forget,**绝不阻塞**(§3.6 约束 B)。
func (r *EvtRing) WritePush(evtType pubsdk.EventType, payload []byte) {
seq := r.writeSeq.Add(1) - 1
var off uint32
r.mu.Lock()
used := r.arenaUsed.Load()
if used+uint32(len(payload)) <= r.arenaCap {
off = r.arenaBase + used
r.arenaUsed.Store(used + uint32(len(payload)))
copy(r.data[off:], payload)
}
r.mu.Unlock()
idx := seq % uint64(r.cap)
slotOff := r.slotsBase + uint32(idx)*evtRingSlotLen
binary.LittleEndian.PutUint64(r.data[slotOff:], seq)
binary.LittleEndian.PutUint32(r.data[slotOff+8:], encodeEvtType(evtType))
binary.LittleEndian.PutUint32(r.data[slotOff+12:], off)
binary.LittleEndian.PutUint32(r.data[slotOff+16:], uint32(len(payload)))
binary.LittleEndian.PutUint64(r.data[evtOffWriteSeq:], seq+1)
}
// ---- 子进程侧:EvtConsumer ----
type EvtConsumer struct {
ringData []byte
evtfd evtfdReader
handler func(*pubsdk.Event) error
readSeq uint64
typeMask uint32
mu sync.Mutex
running bool
stop chan struct{}
stopOnce sync.Once
done chan struct{}
}
type evtfdReader interface {
Read(b []byte) (int, error)
}
// evtfdFder 是可取出原始 fd 的通知句柄(*os.File 满足)。
//
// 有它才能用 poll(2) 加超时等待可读。**为什么不能用 SetReadDeadline**:
// eventfd/pipe 经 os.NewFile 包装后不会注册进 Go netpoller(os.NewFile 对
// 非 open 得到的 fd 一律按非 pollable 处理),Read 退化成阻塞 syscall,
// SetReadDeadline 返回错误且不生效——Run 会永久卡在 syscall.Read,
// 此时调用方若已 munmap 区域(host.Close),恢复后的 drainEvents 就是
// 读已解除映射的内存:SIGSEGV,recover 捕不到。
type evtfdFder interface {
Fd() uintptr
}
func NewEvtConsumer(ringData []byte, evtfd evtfdReader, mask uint32, handler func(*pubsdk.Event) error) *EvtConsumer {
return &EvtConsumer{
ringData: ringData,
evtfd: evtfd,
handler: handler,
typeMask: mask,
stop: make(chan struct{}),
done: make(chan struct{}),
}
}
// readWakeInterval 是 poll 超时间隔:保证 Run 至少这么频繁地检查 stop。
const readWakeInterval = 100 * time.Millisecond
func (c *EvtConsumer) Run() {
c.mu.Lock()
if c.running {
c.mu.Unlock()
return
}
c.running = true
c.mu.Unlock()
defer func() {
c.mu.Lock()
c.running = false
c.mu.Unlock()
close(c.done)
}()
buf := make([]byte, 8)
for {
select {
case <-c.stop:
return
default:
}
// 显式 poll(2) 加超时:只有它能打破阻塞 Read,让 Stop 真正生效。
if f, ok := c.evtfd.(evtfdFder); ok {
ready, err := pollEvtfd(int(f.Fd()), int(readWakeInterval/time.Millisecond))
if err != nil {
return // 通知句柄已失效,退出以免空转
}
if !ready {
continue // 超时:回到顶部检查 stop
}
}
// 阻塞等待内核通知(走 netpoller,只 park goroutine)
if _, err := c.evtfd.Read(buf); err != nil {
select {
case <-c.stop:
return
default:
}
continue
}
c.drainEvents()
}
}
func (c *EvtConsumer) drainEvents() {
writeSeq := binary.LittleEndian.Uint64(c.ringData[evtOffWriteSeq:])
cap := uint64(evtRingCap)
for c.readSeq < writeSeq {
// 每处理一条就检查一次 stop:handler 可能很慢,
// 不加这个检查的话 Stop 要等整轮 drain 完才生效。
select {
case <-c.stop:
return
default:
}
if writeSeq-c.readSeq > cap {
c.readSeq = writeSeq - cap
}
idx := c.readSeq % cap
slotOff := evtOffSlots + uint32(idx)*evtRingSlotLen
seq := binary.LittleEndian.Uint64(c.ringData[slotOff:])
etype := binary.LittleEndian.Uint32(c.ringData[slotOff+8:])
off := binary.LittleEndian.Uint32(c.ringData[slotOff+12:])
slen := binary.LittleEndian.Uint32(c.ringData[slotOff+16:])
if seq != c.readSeq {
// slot 已被新事件覆盖——逐个扫太慢(溢出场景 readSeq=0 要跳 100+ 步),
// 直接跳到 writeSeq 附近找下一个可读 slot。
// 简化:溢出后直接跳到 writeSeq - cap(最旧的可读事件)。
if writeSeq > cap {
c.readSeq = writeSeq - cap
} else {
c.readSeq = writeSeq
}
continue
}
// 位掩码过滤
if c.typeMask != 0 && (1<<etype)&c.typeMask == 0 {
c.readSeq++
continue
}
if off > 0 && slen > 0 && uint64(off)+uint64(slen) <= uint64(len(c.ringData)) {
payload := make([]byte, slen)
copy(payload, c.ringData[off:off+slen])
var evt pubsdk.Event
if err := json.Unmarshal(payload, &evt); err == nil {
c.handler(&evt)
}
}
c.readSeq++
}
}
// Stop 请求消费者退出。
//
// 非阻塞:Stop 返回**不代表** Run 已退出(最多 readWakeInterval 后退出)。
// 若要在 Stop 之后释放 ringData(host.Close 会 munmap 整个区域),
// 必须先 Stop() 再 Wait()。
func (c *EvtConsumer) Stop() {
c.stopOnce.Do(func() { close(c.stop) })
}
// Wait 阻塞至 Run 退出。返回后 drainEvents 保证不会再访问 ringData。
func (c *EvtConsumer) Wait() { <-c.done }