feat(gateway,loop): 忙时并行回合基础管线(I1′ 有条件放宽)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- turnId 由编排器生成、loop Run 复用(硬前提:否则 turn_begin 与流帧各铸一个,客户端两级分桶立即失配)
- StateSink 增带 turnId 的变体(未接线时退化为现签名,不丢状态帧)
- 上行 chat.parallel(WS chat 帧 + REST 三入口同形;省略 = 逐字不变)
- 单会话额度默认 2 / 硬上限 3 / <2 关闭;额度满或未声明 ⇒ 落回既有排队语义
- I1′:checkpoint 归属、历史配对、忙态计数三处从会话级单例升为路级
- session_idle 收紧为「所有在途路收尾且 pending 空」(仅并行时生效;单路逐字不变)
- 预留接口(形状定死、无实现无接线):tool/legfile.go 的 FileClaimer / ErrFileBusy / WithLegID
- 顺手修一个真 bug:并行路 ctx 派生自提交请求作用域,REST 同步入口一写响应就把它连坐杀死(改 context.WithoutCancel 派生)
- 23 个新用例(含 -race -count=3);变异 6 条(turnId 复用 / idle 时点 / 并行升级 / checkpoint 键 / 编排器交 id / pending_ack reason)
- 契约变更点 10 条待监工升 v0.65(本人未改 Docs/**)
This commit is contained in:
AskaEth committed 2026-10-05 09:18:50 +08:00
1 parent 7d2ff74cfc
commit 89e13cf53d
23 files changed
+1921 -275

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
package tool
import (
"context"
"errors"
"fmt"
)
// 本文件是**接口预留**(design/15 §2.5.7 与主人 2026-10-05 裁决 3),本切片
// **不实现也不接线**:忙时并行(design/15 §2.5)落地后,同一会话可以有多条在途
// 回合,而写类工具(执行命令 / 修改文件 / 对外发消息 / 改配置,即 design/11 §4
// 的敏感清单)在两路之间会真正争抢同一个副作用目标。
//
// 主人口径(裁决 3)是**文件级**互斥,而不是会话级串行:
// - 工具先检测目标文件此刻是否正被**别的在途路**操作;
// - **同一文件**正在被操作 ⇒ 把「该文件正在被操作」**显式**作为工具结果返回给
// LLM,并提示稍后再试(**不静默、不排队死等**);
// - **不是同一个文件** ⇒ 允许并行操作。
//
// 为什么只留接口:写类工具的「目标文件」需要逐工具声明(命令、补丁、配置写入
// 各自的目标语义不同),且占用登记要挂在**路**的生命周期上(路一收尾就必须释放,
// 否则一个中途报错的路会把那个文件永久锁住)。这两件都属于后续切片,本切片只把
// 通道与判定口径钉死,使实现方不必再改工具接口。
//
// 身份与权限**不走这条通道**:它只回答「此刻有没有另一路正在动这个文件」,与
// actor 元数据 / 策略引擎无关(项目红线)。
// ErrFileBusy 报告目标文件此刻正被另一条在途路操作。它不是执行失败:调用方
// (写类工具)应把它**显式**返回给 LLM,让模型稍后重试,而不是静默吞掉或排队
// 死等(主人裁决 3)。错误文案面向模型,因此写清「同一文件被另一路占用」与
// 「请稍后再试」,绝不泄露另一路的内容。
var ErrFileBusy = errors.New("tool: 目标文件正在被另一条并行回合操作,请稍后再试")
// FileBusyError 把 ErrFileBusy 与具体目标绑成一条可直接交给 LLM 的错误,
// 同时保持 errors.Is(err, ErrFileBusy) 成立(%w 包装)。target 是**服务端
// 规范化后**的文件标识,调用方不得把用户原始输入当作它(那会把路径穿越或
// 隐私内容带进模型上下文)。
func FileBusyError(target string) error {
return fmt.Errorf("%w(目标:%s)", ErrFileBusy, target)
}
// FileClaimer 是按**文件级**粒度登记在途操作的接口(主人裁决 3)。实现方
// (后续切片)负责:
// - 以规范化后的目标为键,记录「哪一条路正在操作它」;
// - Claim 在目标已被**另一条路**占用时返回 ErrFileBusy(同一路重复 Claim
// 同一目标必须是幂等的,否则一条路自己的多步工具调用会互相挡);
// - Release 无条件释放该路在该目标上的占用(路收尾时的兜底),legID 不是
// 占用者时是空操作。
//
// 实现必须是**有界的**:占用表随路收尾清空,绝不随会话长度增长。
type FileClaimer interface {
Claim(ctx context.Context, legID, target string) error
Release(ctx context.Context, legID, target string)
}
// legIDKey 是「本回合属于哪条并发路」的 context key。
type legIDKey struct{}
// WithLegID 把本回合所属的并发路标识挂到 ctx 上。循环在 Run 顶部按 ctx 上挂的
// 路标识播种一次(单路 / 子代理 / 后台思考为空串 ⇒ 不挂载),工具经
// Dispatcher.Invoke 的 ctx 读到它,从而知道「我是哪一路」——这是文件级占用
// 登记的唯一来源,绝不从模型输出推断。
//
// 空 legID 是空操作:单路时未挂载,读取侧返回空串,与加入本特性之前逐字相同。
func WithLegID(ctx context.Context, legID string) context.Context {
if ctx == nil || legID == "" {
return ctx
}
return context.WithValue(ctx, legIDKey{}, legID)
}
// LegIDFrom 返回 ctx 上挂的并发路标识;未挂载(单路、子代理、未接线)时返回
// 空串。空串表示「本回合不与其他路并行」,此时文件级互斥**无需生效**。
func LegIDFrom(ctx context.Context) string {
if ctx == nil {
return ""
}
s, _ := ctx.Value(legIDKey{}).(string)
return s
}
+97
View File
@@ -0,0 +1,97 @@
package tool
import (
"context"
"errors"
"testing"
)
// 本文件钉住 design/15 §2.5.7(主人 2026-10-05 裁决 3)为**后续切片**预留的
// 文件级互斥通道:本切片不实现、不接线,但通道形状与判定口径必须现在定死,
// 否则实现方以后还得改工具接口。
//
// 两件事缺一不可:① 「我是哪一路」只能来自 ctx(绝不从模型输出推断);
// ② 「该文件正被另一条路操作」必须是一条**能被 errors.Is 识别**的哨兵错误,
// 让写类工具把它当成正常返回值显式交给 LLM,而不是静默吞掉或排队死等。
// TestLegIDContextRoundTrip 钉住路标识的 ctx 通道:空 legID 是空操作(单路 /
// 子代理 / 未接线),nil ctx 不 panic。
func TestLegIDContextRoundTrip(t *testing.T) {
t.Parallel()
ctx := WithLegID(context.Background(), "turn-a")
if got := LegIDFrom(ctx); got != "turn-a" {
t.Fatalf("LegIDFrom = %q, want turn-a", got)
}
if got := LegIDFrom(WithLegID(context.Background(), "")); got != "" {
t.Fatalf("空 legID 应是不挂载的 ctx 副本,得到 %q", got)
}
if got := LegIDFrom(nil); got != "" {
t.Fatalf("nil ctx 应返回空串(单路 ⇒ 文件级互斥无需生效),得到 %q", got)
}
}
// TestFileBusyErrorWrapsSentinel 钉住「同一文件正在被别的路操作」的错误形状:
// 它必须能被 errors.Is 识别(调用方按它决定「让模型稍后再试」而不是「执行失败」),
// 且文案里带规范化后的目标,便于模型理解为何重试。
func TestFileBusyErrorWrapsSentinel(t *testing.T) {
t.Parallel()
err := FileBusyError("reports/2026.csv")
if !errors.Is(err, ErrFileBusy) {
t.Fatalf("errors.Is(err, ErrFileBusy) = false,错误形状已断: %v", err)
}
// 它绝不是执行失败:调用方不得把它当成「工具坏了」上报。
if errors.Is(err, ErrUnknownTool) {
t.Fatal("文件占用被误判成了未知工具")
}
if got := err.Error(); got == "" {
t.Fatal("面向模型的错误文案不能为空")
}
}
// fakeFileClaimer 是接口预留的形状检验:它证明 FileClaimer 只要两个方法
// (Claim / Release)就能表达「文件级、按路登记、被占用即明确拒绝」的口径,
// 不需要工具侧再改签名 —— 本切片到此为止,不接线任何实现。
type fakeFileClaimer struct {
owner map[string]string
}
func (f *fakeFileClaimer) Claim(_ context.Context, legID, target string) error {
if holder, ok := f.owner[target]; ok && holder != legID {
return FileBusyError(target)
}
f.owner[target] = legID
return nil
}
func (f *fakeFileClaimer) Release(_ context.Context, legID, target string) {
if f.owner[target] == legID {
delete(f.owner, target)
}
}
// TestFileClaimerShapeFileLevel 钉住接口预留的**粒度**语义(主人裁决 3):
// 同一目标被别的路占住 ⇒ 明确拒绝;**不同目标** ⇒ 允许并行;同一路重复 Claim
// 自己的目标 ⇒ 幂等;Release 之后别的路可以接手。
func TestFileClaimerShapeFileLevel(t *testing.T) {
t.Parallel()
var claimer FileClaimer = &fakeFileClaimer{owner: map[string]string{}}
ctx := context.Background()
if err := claimer.Claim(ctx, "turn-a", "a.txt"); err != nil {
t.Fatalf("首次占用: %v", err)
}
if err := claimer.Claim(ctx, "turn-a", "a.txt"); err != nil {
t.Fatalf("同一路重复占用同一目标必须是幂等的: %v", err)
}
if err := claimer.Claim(ctx, "turn-b", "b.txt"); err != nil {
t.Fatalf("不同文件必须允许并行操作: %v", err)
}
err := claimer.Claim(ctx, "turn-b", "a.txt")
if !errors.Is(err, ErrFileBusy) {
t.Fatalf("同一文件被另一路占用时必须明确拒绝,得到 %v", err)
}
claimer.Release(ctx, "turn-a", "a.txt")
if err := claimer.Claim(ctx, "turn-b", "a.txt"); err != nil {
t.Fatalf("占用释放后应可接手: %v", err)
}
}