fake

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package fake 提供脚本化的 executor.Adapter 实现,供集成测试驱动全链路。

职责:

  • 按脚本(Step 队列)逐步产出 AdapterEvent:Permission(发权限请求后阻塞至 RespondPermission)、Question(发提问后阻塞至 Send)、Finish(发回合结果)
  • 完整记录 Send / RespondPermission / Stop 的全部实参,供测试断言
  • 支持运行中追加脚本(Add),模拟「协调者续接指令 → executor 继续执行并再完成」

边界:

  • 无真实执行:不碰文件系统、不启动进程,只做事件流编排
  • 不写 store、不做审批判断(与 executor 契约一致,见 executor.go 包级边界)
  • 仅服务于测试与演示(agentd --executor=fake 冒烟),不用于生产

事件流语义:

  • 事件通道在 Stop 前保持打开:任务进入 waiting_review 后协调者可能继续续接, 新步骤由 Add 注入;Stop 后通道关闭,manager 的中介循环随之退出
  • 未启动任务调 Events 返回已关闭通道(契约:通道关闭 = 执行终结), range 立即结束而非永久阻塞(P1-11,与 opencode adapter 语义一致)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fake

type Fake struct {
	// contains filtered or unexported fields
}

Fake 是脚本化 Adapter,记录全部 Send/RespondPermission/Stop 实参供断言。

并发安全:所有记录与脚本队列的访问都在 mu 保护下;事件通道按任务隔离。

func New

func New(script []Step) *Fake

New 创建脚本化 fake adapter。

参数:

  • script: 初始步骤队列;运行中可用 Add 追加(典型为测试在续接前注入下一步)

func (*Fake) Add

func (f *Fake) Add(taskID string, steps ...Step)

Add 为任务追加脚本步骤。

注意:

  • 追加后步骤要等「续接门禁」放行才会执行:脚本耗尽后(任务已进 waiting_review), runner 阻塞在 Send 上,协调者的 continue 指令(Send)到达才解锁后续步骤—— 这保证「executor 先收到指令、再继续产出事件」的顺序,测试据此确定性断言

func (*Fake) Events

func (f *Fake) Events(taskID string) <-chan executor.AdapterEvent

Events 返回任务的事件流通道;任务未启动时返回**已关闭**通道。

契约(与 opencode adapter 一致,P1-11):通道关闭 = 执行终结,消费方 (manager 中介循环)靠 range 在关闭时立即退出。未启动任务若返回惰性新建的 打开通道,range 会永久阻塞——对未知任务等价于旧的 nil 通道。已启动任务的 通道由 run 循环在 Stop/退出时关闭(defer close(evCh))。

func (*Fake) LastDecision

func (f *Fake) LastDecision() string

LastDecision 返回最后一次 RespondPermission 收到的 decision;未收到任何应答 时返回空串(测试探查用,如断言审批者自动批准路径 executor 收到 once)。

func (*Fake) PermID

func (f *Fake) PermID() string

PermID 返回本 fake 发出的第一个权限请求的 PermissionID(测试探查用)。

为什么只需「第一个」:单任务测试里权限请求从 perm-1 递增,首个即对应 任务的第一个权限工单 id(taskID:perm-1)。

func (*Fake) Perms

func (f *Fake) Perms() []PermCall

Perms 返回全部 RespondPermission 调用实参(按调用顺序)。

func (*Fake) RespondPermission

func (f *Fake) RespondPermission(_ context.Context, taskID, permID, decision, reason string) error

RespondPermission 记录实参并解除对应任务的 Permission 步骤阻塞。

func (*Fake) Send

func (f *Fake) Send(_ context.Context, taskID, text string) error

Send 记录实参;若恰有 Question 步骤在阻塞则解除其阻塞,否则作为「续接指令」记录, 运行循环继续等待后续步骤(模拟 executor 收到修改指令后继续干活)。

func (*Fake) Sends

func (f *Fake) Sends() []SendCall

Sends 返回全部 Send 调用实参(按调用顺序)。

func (*Fake) SetPermError

func (f *Fake) SetPermError(err error)

SetPermError 注入 RespondPermission 的错误返回值(默认 nil 不注入)。

用途:同 SetSendError,面向权限应答中继的失败场景。

func (*Fake) SetSendError

func (f *Fake) SetSendError(err error)

SetSendError 注入 Send 的错误返回值(默认 nil 不注入)。

用途:构造「executor 侧递送失败」的测试场景——如模拟 adapter 无该任务运行态 (opencode adapter 的「任务不在运行中」错误),用于 reply 中继失败路径的断言。

func (*Fake) Start

func (f *Fake) Start(_ context.Context, req executor.StartReq) error

Start 启动任务的脚本执行循环并立即返回(异步)。

func (*Fake) Stop

func (f *Fake) Stop(taskID string) error

Stop 记录并终止任务的脚本循环,事件通道随即关闭。

注意:

  • 幂等:重复 Stop 不 panic,事件通道只关闭一次

func (*Fake) Stops

func (f *Fake) Stops() []string

Stops 返回已收到 Stop 的任务 ID 列表。

type PermCall

type PermCall struct {
	TaskID   string
	PermID   string
	Decision string
	Reason   string // 协调者的拒绝理由;批准时为空
}

PermCall 记录一次 RespondPermission 调用的实参。

type SendCall

type SendCall struct {
	TaskID string
	Text   string
}

SendCall 记录一次 Send 调用的实参。

type Step

type Step struct {
	Permission string
	Question   string
	Finish     executor.Result
}

Step 是 fake 脚本的一个步骤,三种形态互斥(至多填一个字段):

  • Permission: 发 permission 事件(PermissionID 形如 perm-<n>),阻塞至 RespondPermission 被调
  • Question: 发 question 事件,阻塞至 Send 被调
  • Finish: 发 result 事件(Result.OK/FailReason 决定完成还是失败)

Jump to

Keyboard shortcuts

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