Compare commits

..
Author SHA1 Message Date
weichen 7f60baeaf8 Run serial-flagged tool calls in order while others fan out
ci / vet-and-test (pull_request) Successful in 54s
ci / docker-checkout-hostgateway (pull_request) Successful in 1s
Why: Mutual exclusion alone does not preserve order. With parallel
execution the host cannot keep order-sensitive tools (card streaming
begin/append/commit, plan updates) correct by locking a mutex, because
whichever goroutine grabs the lock first is not necessarily the first
declared call - a real failure observed in the backend card-stream test.

Evidence: go test ./... green; TestLoopSerialToolsKeepOrder asserts the
serial calls execute in declaration order (s1->s2->s3) while parallel
calls still fan out.

Impact: New Config.SerialTools set. In parallel mode, matching calls run
sequentially on the caller goroutine in model-declared order; others
stay concurrent. Empty set keeps the previous all-parallel behavior;
sequential mode is untouched.

Refs: Dev task feature/agent-e1-parallel-fixes (F5).
2026-10-10 08:58:31 +00:00
weichen ef2fa75006 Merge pull request 'Inject the active tool call into the Execute context' (#43) from feature/tool-call-context into main
ci / vet-and-test (push) Successful in 50s
ci / docker-checkout-hostgateway (push) Successful in 1s
2026-10-10 08:50:13 +00:00
weichen a5da18b710 Inject the active tool call into the Execute context
ci / vet-and-test (pull_request) Successful in 50s
ci / docker-checkout-hostgateway (pull_request) Successful in 1s
Why: With parallel tool execution the host-side tool wrapper can no
longer infer which invocation it is serving from a single in-flight
slot; tools need the call identity (ID/name/arguments) to pair process
state and per-call bookkeeping. Context injection keeps the tools
registry signature unchanged.

Evidence: go test ./... green; TestLoopToolSeesOwnCallInContext runs
two parallel calls and asserts each reads its own ID from ctx.

Impact: Additive WithToolCall/ToolCallFromContext helpers; runOne now
wraps the ctx before Execute. Callers that never inject keep ok=false
and are unaffected.

Refs: Dev task feature/agent-e1-parallel-fixes (F5 parallel wiring).
2026-10-10 08:48:26 +00:00
weichen d6267fba15 Merge pull request 'Carry tool CallID in loop events and add reasoning_split to the chat client' (#42) from feature/e1-parallel-reasoning into main
ci / vet-and-test (push) Successful in 49s
ci / docker-checkout-hostgateway (push) Successful in 0s
2026-10-10 08:36:09 +00:00
3 changed files with 194 additions and 1 deletions
+53 -1
View File
@@ -69,6 +69,9 @@ type Config struct {
Trace func(name string, attrs map[string]string) func()
MaxTurns int
ToolExecution string
// SerialTools 是并行执行下的顺序敏感工具名集合:这些调用按模型给出的
// 原顺序在调用方 goroutine 逐个执行(互斥且保序),其余调用并发执行。
SerialTools map[string]bool
GetSteering func() []llm.Message
GetFollowUp func() []llm.Message
// OnEvent 观察循环事件。同一次 Run 内不会并发调用
@@ -597,7 +600,10 @@ func serializeHostCallbacks(cfg *Config) {
func executeTools(ctx context.Context, cfg Config, assistant llm.Message) []llm.Message {
if cfg.ToolExecution == ExecParallel && len(assistant.ToolCalls) > 1 {
return executeParallel(ctx, cfg, assistant)
if len(cfg.SerialTools) == 0 {
return executeParallel(ctx, cfg, assistant)
}
return executeMixed(ctx, cfg, assistant)
}
out := make([]llm.Message, 0, len(assistant.ToolCalls))
for _, call := range assistant.ToolCalls {
@@ -610,6 +616,35 @@ func executeTools(ctx context.Context, cfg Config, assistant llm.Message) []llm.
return out
}
// executeMixed 并行执行非串行调用,串行名(SerialTools)按模型给出的原顺序
// 在调用方 goroutine 逐个执行——互斥且保序(卡片流等有先后依赖的工具不能只
// 靠互斥:并发派发下先抢到锁的不一定是先声明的调用)。
func executeMixed(ctx context.Context, cfg Config, assistant llm.Message) []llm.Message {
out := make([]llm.Message, len(assistant.ToolCalls))
var wg sync.WaitGroup
serialIdx := make([]int, 0, len(assistant.ToolCalls))
for i, call := range assistant.ToolCalls {
if cfg.SerialTools[call.Name] {
serialIdx = append(serialIdx, i)
continue
}
wg.Add(1)
go func(i int, call llm.ToolCall) {
defer wg.Done()
out[i] = runOne(ctx, cfg, call)
}(i, call)
}
for _, i := range serialIdx {
if err := ctx.Err(); err != nil {
out[i] = cancelledToolMessage(assistant.ToolCalls[i], err)
continue
}
out[i] = runOne(ctx, cfg, assistant.ToolCalls[i])
}
wg.Wait()
return out
}
func executeParallel(ctx context.Context, cfg Config, assistant llm.Message) []llm.Message {
out := make([]llm.Message, len(assistant.ToolCalls))
var wg sync.WaitGroup
@@ -709,6 +744,22 @@ func noteToolHook(cfg Config, call llm.ToolCall, msg llm.Message) {
cfg.Hooks.Emit(hooks.Call{Event: ev, Tool: call.Name, Args: call.Arguments, Result: msg.Content, IsError: msg.IsError})
}
// toolCallKey 是把当前工具调用注入 Execute ctx 的私有键:并行执行下工具实现
// 需要知道自己服务于哪次调用(配对过程事件、按调用记账),经
// ToolCallFromContext 读取;旧宿主/直接构造的 ctx 返回 ok=false。
type toolCallKey struct{}
// WithToolCall 把 call 挂进 ctx(内核 runOne 在执行前注入)。
func WithToolCall(ctx context.Context, call llm.ToolCall) context.Context {
return context.WithValue(ctx, toolCallKey{}, call)
}
// ToolCallFromContext 取出当前正在执行的调用;不存在时 ok=false。
func ToolCallFromContext(ctx context.Context) (llm.ToolCall, bool) {
call, ok := ctx.Value(toolCallKey{}).(llm.ToolCall)
return call, ok
}
func runOne(ctx context.Context, cfg Config, call llm.ToolCall) llm.Message {
if err := ctx.Err(); err != nil {
return cancelledToolMessage(call, err)
@@ -757,6 +808,7 @@ func runOne(ctx context.Context, cfg Config, call llm.ToolCall) llm.Message {
defer endSpan()
res := tools.Result{Content: "no tools configured", IsError: true}
known := cfg.Tools != nil && cfg.Tools.Has(call.Name)
ctx = WithToolCall(ctx, call)
switch {
case known:
res = cfg.Tools.Execute(ctx, call.Name, json.RawMessage(call.Arguments))
+69
View File
@@ -0,0 +1,69 @@
package runtime
import (
"context"
"encoding/json"
"sync"
"testing"
"agent-native/runtime/llm"
"agent-native/runtime/tools"
)
// ctxEchoTool 把自己从 ctx 读到的调用 ID 记下来(并发安全),验证内核
// 在 Execute 前注入了当前调用。
type ctxEchoTool struct {
mu sync.Mutex
ids []string
bad int
}
func (c *ctxEchoTool) Name() string { return "echo" }
func (c *ctxEchoTool) Description() string { return "echo" }
func (c *ctxEchoTool) Parameters() json.RawMessage { return json.RawMessage(`{"type":"object"}`) }
func (c *ctxEchoTool) Execute(ctx context.Context, _ json.RawMessage) (tools.Result, error) {
call, ok := ToolCallFromContext(ctx)
c.mu.Lock()
defer c.mu.Unlock()
if !ok || call.ID == "" || call.Name != "echo" {
c.bad++
return tools.Result{Content: "call missing from context", IsError: true}, nil
}
c.ids = append(c.ids, call.ID)
return tools.Result{Content: "ok " + call.ID}, nil
}
// TestLoopToolSeesOwnCallInContext 并行执行两个调用,各自读到自己的 ID。
func TestLoopToolSeesOwnCallInContext(t *testing.T) {
tool := &ctxEchoTool{}
reg := tools.NewRegistry(t.TempDir())
reg.Register(tool)
res, err := Run(context.Background(), Config{
Model: "fake",
Tools: reg,
ToolExecution: ExecParallel,
Stream: script(
llm.Message{Role: llm.RoleAssistant, StopReason: llm.StopToolCalls, ToolCalls: []llm.ToolCall{
{ID: "pa", Name: "echo", Arguments: `{}`},
{ID: "pb", Name: "echo", Arguments: `{}`},
}},
llm.Message{Role: llm.RoleAssistant, Content: "done", StopReason: llm.StopEnd},
),
}, llm.Message{Role: llm.RoleUser, Content: "go"})
if err != nil {
t.Fatal(err)
}
tool.mu.Lock()
defer tool.mu.Unlock()
if tool.bad != 0 || len(tool.ids) != 2 {
t.Fatalf("bad=%d ids=%v", tool.bad, tool.ids)
}
seen := map[string]bool{}
for _, id := range tool.ids {
seen[id] = true
}
if !seen["pa"] || !seen["pb"] {
t.Fatalf("ids = %v", tool.ids)
}
_ = res
}
+72
View File
@@ -0,0 +1,72 @@
package runtime
import (
"context"
"encoding/json"
"sync"
"testing"
"agent-native/runtime/llm"
"agent-native/runtime/tools"
)
// orderTool 记录自己的执行序号(并发安全)。
type orderTool struct {
name string
mu sync.Mutex
seq []string
}
func (o *orderTool) Name() string { return o.name }
func (o *orderTool) Description() string { return o.name }
func (o *orderTool) Parameters() json.RawMessage { return json.RawMessage(`{"type":"object"}`) }
func (o *orderTool) Execute(ctx context.Context, _ json.RawMessage) (tools.Result, error) {
id := o.name
if call, ok := ToolCallFromContext(ctx); ok {
id = call.ID
}
o.mu.Lock()
o.seq = append(o.seq, id)
o.mu.Unlock()
return tools.Result{Content: "ran " + id}, nil
}
// TestLoopSerialToolsKeepOrder 并行模式下 SerialTools 命中的调用按声明顺序执行
// (互斥不保序不够:卡片流等有先后依赖),未命中的并发执行。
func TestLoopSerialToolsKeepOrder(t *testing.T) {
cards := &orderTool{name: "card"}
reads := &orderTool{name: "read"}
reg := tools.NewRegistry(t.TempDir())
reg.Register(cards)
reg.Register(reads)
res, err := Run(context.Background(), Config{
Model: "fake", Tools: reg,
ToolExecution: ExecParallel,
SerialTools: map[string]bool{"card": true},
Stream: script(
llm.Message{Role: llm.RoleAssistant, StopReason: llm.StopToolCalls, ToolCalls: []llm.ToolCall{
{ID: "s1", Name: "card", Arguments: `{}`},
{ID: "p1", Name: "read", Arguments: `{}`},
{ID: "s2", Name: "card", Arguments: `{}`},
{ID: "s3", Name: "card", Arguments: `{}`},
}},
llm.Message{Role: llm.RoleAssistant, Content: "done", StopReason: llm.StopEnd},
),
}, llm.Message{Role: llm.RoleUser, Content: "go"})
if err != nil {
t.Fatal(err)
}
cards.mu.Lock()
got := append([]string(nil), cards.seq...)
cards.mu.Unlock()
if len(got) != 3 || got[0] != "s1" || got[1] != "s2" || got[2] != "s3" {
t.Fatalf("card executions must follow declaration order s1→s2→s3, got %v", got)
}
reads.mu.Lock()
nRead := len(reads.seq)
reads.mu.Unlock()
if nRead != 1 {
t.Fatalf("read executions = %d", nRead)
}
_ = res
}