Help us shape the future of Go! Take the 2026 Go Developer Survey

toolsched

package
v0.12.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package toolsched 工具并发调度器:一条模型消息里的多个工具调用按注解与依赖 调度——依赖就绪即跑、并发受上限约束、独占任务不与他人并发。

对标 ZCode tool/scheduler.ts + executor/batch-runner.ts:并发判定由注解驱动 (destructive 一票否决 → concurrentSafe 显式优先 → readOnly 次之 → 具名未声明 保守不并行);注解生产面见 mcp.ToolHints(tools/list 注解归一),本包只消费 同构的 Hints 三态声明,不依赖 MCP。

失败不阻断后续:前序非并发安全工具的本地失败(如 todo 写盘)不应误截断 后续任务的执行——后续任务的去留由其自身的执行结果与调用方取消信号决定。 这是 ZCode 踩坑后固化的语义(曾因失败跳过全部后续组导致误截断)。

执行模型(v0.10.32 起):Execute 按 DependsOn 就绪队列调度 + MaxConcurrency 信号量限流,不再按「组」做全屏障——同层可并行任务被切成多组时原先组间串行 (15 个安全任务 / 上限 10 会跑成 10+5 两段串行)。Schedule.Groups 保留为 静态规划视图(分层/切组/独占,可测试可检视),不再驱动执行序。

用法:

s := &toolsched.Scheduler{MaxConcurrency: 10}
sched, err := s.Schedule(calls)   // 规划分组(可测试的纯函数)
results := s.Execute(ctx, tasks)  // 按依赖就绪执行

Index

Constants

View Source
const DefaultMaxConcurrency = 10

DefaultMaxConcurrency 组大小缺省上限(对标 ZCode DEFAULT_MAX_CONCURRENCY)。

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	ID        string   // 调用唯一标识(模型侧 tool call id)
	Name      string   // 工具名(可空;匿名调用无从按名兜底)
	DependsOn []string // 显式前置(全部完成后才可执行)
	Hints     Hints
	// StopAfter 声明「本调用成功即停轮」(对标 ZCode plan 批准类
	// turnControl.stopTurnAfterResult):成功后剩余组取消,未执行调用以
	// Skipped 结果呈现;失败不触发(停轮是成功语义)。停轮门独占单例组——
	// 与后续调用同组并发会让取消失去意义。
	StopAfter bool
}

Call 单次调用声明。

type Hints

type Hints struct {
	ReadOnly    *bool // 只读(无副作用)
	Idempotent  *bool // 幂等/并发安全(显式声明优先于 ReadOnly 推定)
	Destructive *bool // 破坏性(一票否决并行)
}

Hints 工具注解(三态:nil=未声明)。与 mcp.ToolHints 字段同构——非 MCP 工具 按同形态声明即可入调度。

type Item

type Item struct {
	Call
	CanRunParallel bool
}

Item 调度产物项(输入序,含并行判定)。

type Result

type Result struct {
	ID      string
	Name    string
	Output  string
	Err     error
	Skipped bool // 因 StopAfter/ctx 取消未执行
}

Result 单任务结果(输入序返回,与完成序无关)。

type Schedule

type Schedule struct {
	Items []Item // 输入序(含 CanRunParallel)
	// Groups 规划分组:依赖分层 + 并行切组(≤ MaxConcurrency)+ 非并行独占。
	// 仅供检视/测试/审计;Execute 不按组屏障执行(见包注释「执行模型」)。
	Groups [][]string
}

Schedule 调度结果:静态规划视图(Groups)+ 输入序判定(Items)。

type Scheduler

type Scheduler struct {
	// MaxConcurrency 单组大小上限(≤0 → DefaultMaxConcurrency)。
	MaxConcurrency int
	// ReadOnlyTools 按名兜底表:未声明注解的具名工具查此表判只读
	// (如 bianque 侧内置只读工具清单);查不到保守不并行。
	ReadOnlyTools map[string]bool
}

Scheduler 调度器。零值可用(MaxConcurrency 缺省 DefaultMaxConcurrency)。

func (*Scheduler) Execute

func (s *Scheduler) Execute(ctx context.Context, tasks []Task) []Result

Execute 按依赖就绪执行(引擎见 runner)。结果按输入序返回。 前序失败不阻断后续;ctx 取消或 StopAfter 成功后,未启动任务记 Skipped。

func (*Scheduler) Schedule

func (s *Scheduler) Schedule(calls []Call) (*Schedule, error)

Schedule 计算调度(纯函数,不执行)。ID 重复/依赖悬空/依赖环均报错—— 调用图来自模型输出与装配配置,越界即确定性错误而非静默容错。

type Task

type Task struct {
	Call
	// Run 执行体(nil 视为任务错误,不阻断其余任务)。
	// 契约:不得阻塞不返回(长操作应尊重 ctx);不得 panic——执行体来自外部
	//(MCP 工具适配层等),panic 由调度器隔离转为该任务的 Err(与 worker.Pool
	// 的外部代码隔离同纪律),不击穿进程。
	Run func(ctx context.Context) (string, error)
}

Task 可执行任务:声明(Call)+ 执行体。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL