mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-04 00:03:59 +00:00
fix(shm): 审核修复——arena 并发安全 + ToolCall ring 抢占 + arenaRead 校验
§13.2: arenaAlloc 改 CAS bump(修复并发覆盖);arenaRead 增加 generation + arena 边界校验 §13.3: Reserve 从 Store+Load 改 CAS-free→Reserved 状态机抢占帧(修复并发分配重复); 增加 RESERVED 中间态;SetReading/SetReady 改 CAS 返回 bool;ReleaseFrame 清零跳过 state §13.5/13.6: 注入方法接入 resolveText(从 arena 读 SharedRef);cleanerProxy 加 arenaMu 串行化+每次重置 §13.8: ContextPolicy prune topK 改为 maxContextSize-1(不再硬编码 20) tool.register 校验 context_policy 只允许 none/prune
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package proc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
@ -154,6 +155,56 @@ func TestSharedRef_PackUnpack(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolCallRing_ConcurrentReserveUnique(t *testing.T) {
|
||||
data := make([]byte, int(trlOffFrameBase+toolRingCap*toolFrameSize))
|
||||
if err := InitToolRing(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ring, err := AttachToolRing(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
indices := make(chan uint32, toolRingCap)
|
||||
var wg sync.WaitGroup
|
||||
for i := uint32(0); i < toolRingCap; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
idx, reserveErr := ring.Reserve()
|
||||
if reserveErr != nil {
|
||||
t.Errorf("Reserve: %v", reserveErr)
|
||||
return
|
||||
}
|
||||
indices <- idx
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(indices)
|
||||
|
||||
seen := make(map[uint32]bool, toolRingCap)
|
||||
for idx := range indices {
|
||||
if seen[idx] {
|
||||
t.Fatalf("并发 Reserve 重复分配帧 %d", idx)
|
||||
}
|
||||
seen[idx] = true
|
||||
}
|
||||
if len(seen) != int(toolRingCap) {
|
||||
t.Fatalf("唯一帧数=%d,期望 %d", len(seen), toolRingCap)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachToolRingRejectsInvalidLayout(t *testing.T) {
|
||||
data := make([]byte, trlOffFrameBase)
|
||||
putU32(data[trlOffMagic:], toolRingMagic)
|
||||
putU32(data[trlOffVersion:], toolRingVersion)
|
||||
putU32(data[trlOffCap:], toolRingCap)
|
||||
putU32(data[trlOffFrameSize:], toolFrameSize)
|
||||
if _, err := AttachToolRing(data); err == nil {
|
||||
t.Fatal("越界布局应被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolFrameLayoutAligned(t *testing.T) {
|
||||
// 确保帧布局字段偏移与内存布局一致(安全断言)
|
||||
var frame toolFrame
|
||||
|
||||
Reference in New Issue
Block a user