docs(comments): 注释中文化 · internal/runtime/orchestration(29 文件,纯替换)
- 只翻注释/文档字符串:剥离注释后代码行逐字节不变(机械校验只剩允许的行尾注释形态) - 裁决编号与 design 节号(02.4.3 / 11.4.2 / design/05 §9 等)逐字保留 - 复核:go build 绿;orchestration 及其子包(audit/comm)测试绿;gofmt 干净
This commit is contained in:
1 parent
fe90f64a56
commit
eb1828ee7a
29 files changed
+751
-949
No files matched your search
@@ -10,45 +10,41 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
||||
)
|
||||
|
||||
// SubAgent is a single spawned sub-agent instance. The identity fields (ID, Goal,
|
||||
// RoleID, PersonaID, Mode, AttachInfo, ParentSession, Data, ToolWhitelist,
|
||||
// Context, WorkingMemory) are set once at Spawn and never mutated. The lifecycle fields (Status, Summary,
|
||||
// Result, Err) are written by the sub-agent's background goroutine and read through
|
||||
// the synchronized accessors below, so reading them before the sub-agent finishes is
|
||||
// safe. A sub-agent is terminal exactly once its Done reports true; Join returns
|
||||
// once every sub-agent is terminal, Wait once the named ids are.
|
||||
// SubAgent 是单个已派发的子代理实例。身份字段(ID、Goal、RoleID、PersonaID、Mode、
|
||||
// AttachInfo、ParentSession、Data、ToolWhitelist、Context、WorkingMemory)在 Spawn 时
|
||||
// 一次设定,此后永不改动。生命周期字段(Status、Summary、Result、Err)由子代理的后台
|
||||
// goroutine 写入,并经下方同步访问器读取,因此在子代理结束前读取它们是安全的。当且
|
||||
// 仅当 Done 报告 true 时子代理处于终结态;Join 在全部子代理终结后返回,Wait 在指名的
|
||||
// id 全部终结后返回。
|
||||
//
|
||||
// SubAgent is created by (*Scheduler).Spawn; it should not be built directly.
|
||||
// SubAgent 由 (*Scheduler).Spawn 创建;不应直接构造。
|
||||
type SubAgent struct {
|
||||
// ID uniquely identifies the sub-agent within its scheduler.
|
||||
// ID 在所属调度器内唯一标识该子代理。
|
||||
ID string
|
||||
// Goal is the objective the sub-agent pursues.
|
||||
// Goal 是子代理追求的目标。
|
||||
Goal string
|
||||
// RoleID is the bound role/persona.
|
||||
// RoleID 是绑定的角色/persona。
|
||||
RoleID string
|
||||
// PersonaID is the target role the sub-agent runs as: its instructions and its
|
||||
// memory scope (design/22 §8-2). Blank means "run as the spawning turn's role",
|
||||
// the pre-existing behavior. The dispatch tool fills it (validated against the
|
||||
// per-role agent pool) and Spawn stamps it onto the run's session env.
|
||||
// PersonaID 是子代理运行时充当的目标角色:其指令与记忆作用域(design/22 §8-2)。
|
||||
// 留空表示「按派发回合的角色运行」,即既有行为。派发工具填充它(已对照按角色
|
||||
// agent 池校验),Spawn 把它盖到运行的会话 env 上。
|
||||
PersonaID string
|
||||
// Mode is the bound behavior mode.
|
||||
// Mode 是绑定的行为模式。
|
||||
Mode string
|
||||
// AttachInfo is the free-form attachment text supplied at spawn.
|
||||
// AttachInfo 是派发时提供的自由形式附加文本。
|
||||
AttachInfo string
|
||||
// ParentSession is the session whose flow graph owns this sub-agent's branch
|
||||
// (blank when the branch is not visualized).
|
||||
// ParentSession 是拥有本子代理分支的流程图所属会话(该分支不被可视化时为空)。
|
||||
ParentSession string
|
||||
// Data is the optional structured payload supplied at spawn.
|
||||
// Data 是派发时提供的可选结构化载荷。
|
||||
Data any
|
||||
// ToolWhitelist 是该子代理的工具白名单(design/05 §7 裁决 02.4.1):
|
||||
// nil/空=继承主 agent 全量工具集;非空=只放行所列工具。Spawn 时深拷贝。
|
||||
ToolWhitelist []string
|
||||
// Context is the isolated conversation context. It is a deep copy of the
|
||||
// AgentSpec.Context plus the task user message, and is never shared with the
|
||||
// front session.
|
||||
// Context 是隔离的会话上下文。它是 AgentSpec.Context 的深拷贝加上任务 user
|
||||
// 消息,绝不与前台会话共享。
|
||||
Context []model.Message
|
||||
// WorkingMemory is the sub-agent's own working-memory window, created fresh
|
||||
// at Spawn so it never overlaps another sub-agent or the front session.
|
||||
// WorkingMemory 是该子代理自己的工作记忆窗口,在 Spawn 时全新创建,因此绝不与
|
||||
// 另一个子代理或前台会话重叠。
|
||||
WorkingMemory *memory.WorkingMemory
|
||||
// timeout 是该子代理生效的运行时限(按调用覆盖优先于调度器默认,
|
||||
// design/05 §7 裁决 02.4.4);非正表示除取消外无期限。Spawn 时落定,只读。
|
||||
@@ -67,74 +63,69 @@ type SubAgent struct {
|
||||
abortReq bool
|
||||
doneClosed bool
|
||||
|
||||
// finishedCh is closed exactly once when the sub-agent reaches a terminal
|
||||
// state, so Wait can block per-id without polling.
|
||||
// finishedCh 在子代理进入终结态时恰好关闭一次,因此 Wait 能按 id 阻塞而无需
|
||||
// 轮询。
|
||||
finishedCh chan struct{}
|
||||
|
||||
launchOnce sync.Once
|
||||
finishOnce sync.Once
|
||||
}
|
||||
|
||||
// Status returns the sub-agent's current lifecycle status.
|
||||
// Status 返回子代理当前的生命周期状态。
|
||||
func (s *SubAgent) Status() Status {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.status
|
||||
}
|
||||
|
||||
// Summary returns the execution summary written when the sub-agent finalized:
|
||||
// a completion summary on success, or a failure/abort reason otherwise. It is
|
||||
// empty until the sub-agent is terminal.
|
||||
// Summary 返回子代理收尾时写入的执行摘要:成功时是完成摘要,否则是失败/中止原因。
|
||||
// 子代理进入终结态之前它为空。
|
||||
func (s *SubAgent) Summary() string {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.summary
|
||||
}
|
||||
|
||||
// Result returns the loop result on a successful run, or nil when the run failed
|
||||
// or was aborted.
|
||||
// Result 返回运行成功时的循环结果;运行失败或被中止时返回 nil。
|
||||
func (s *SubAgent) Result() *loop.Result {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.result
|
||||
}
|
||||
|
||||
// Err returns the error that ended a failed or aborted run (nil on success).
|
||||
// Err 返回结束失败或中止运行的那个错误(成功时为 nil)。
|
||||
func (s *SubAgent) Err() error {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.runErr
|
||||
}
|
||||
|
||||
// StartedAt returns when the sub-agent transitioned to running (zero if it never
|
||||
// started).
|
||||
// StartedAt 返回子代理转入 running 的时间(从未启动时为零值)。
|
||||
func (s *SubAgent) StartedAt() time.Time {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.started
|
||||
}
|
||||
|
||||
// FinishedAt returns when the sub-agent reached a terminal state (zero if not
|
||||
// terminal yet).
|
||||
// FinishedAt 返回子代理进入终结态的时间(尚未终结时为零值)。
|
||||
func (s *SubAgent) FinishedAt() time.Time {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.finished
|
||||
}
|
||||
|
||||
// Done reports whether the sub-agent reached a terminal state.
|
||||
// Done 报告子代理是否已进入终结态。
|
||||
func (s *SubAgent) Done() bool { return s.Status().Terminal() }
|
||||
|
||||
// waitDone returns the channel closed when the sub-agent reaches a terminal
|
||||
// state. It is what (*Scheduler).Wait blocks on, per-id, without polling.
|
||||
// waitDone 返回子代理进入终结态时被关闭的 channel。它是 (*Scheduler).Wait 按 id 阻塞
|
||||
// 时等待的对象,无需轮询。
|
||||
func (s *SubAgent) waitDone() <-chan struct{} { return s.finishedCh }
|
||||
|
||||
// markFinished closes the completion channel exactly once. It is called from
|
||||
// finish under finishOnce, so a sub-agent wakes every Wait caller exactly once.
|
||||
// markFinished 恰好关闭一次完成 channel。它在 finish 里于 finishOnce 之下调用,因此
|
||||
// 每个子代理恰好唤醒它的每个 Wait 调用方一次。
|
||||
func (s *SubAgent) markFinished() { close(s.finishedCh) }
|
||||
|
||||
// Snapshot returns a point-in-time copy of the sub-agent's state, safe to read
|
||||
// while the sub-agent is still running.
|
||||
// Snapshot 返回子代理状态的某一时刻拷贝,在子代理仍在运行时读取也安全。
|
||||
func (s *SubAgent) Snapshot() SubAgentResult {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
@@ -158,8 +149,8 @@ func (s *SubAgent) Snapshot() SubAgentResult {
|
||||
return snap
|
||||
}
|
||||
|
||||
// transitionRunning moves a pending sub-agent to running. It returns false when
|
||||
// the sub-agent was already aborted (so it must not run) or is not pending.
|
||||
// transitionRunning 把 pending 的子代理转入 running。子代理已被中止(因此不得运行)
|
||||
// 或当前不是 pending 时返回 false。
|
||||
func (s *SubAgent) transitionRunning() bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -171,7 +162,7 @@ func (s *SubAgent) transitionRunning() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// progress returns the current status snapshot for a progress event.
|
||||
// progress 返回用于进度事件的当前状态快照。
|
||||
func (s *SubAgent) progress() Progress {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
@@ -184,8 +175,8 @@ func (s *SubAgent) progress() Progress {
|
||||
}
|
||||
}
|
||||
|
||||
// setOutcome records the terminal status, summary and error after the run ends.
|
||||
// It is called at most once per sub-agent, guarded by finishOnce.
|
||||
// setOutcome 在运行结束后记录终结状态、摘要与错误。每个子代理至多调用一次,由
|
||||
// finishOnce 把守。
|
||||
func (s *SubAgent) setOutcome(status Status, summary string, err error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -195,16 +186,15 @@ func (s *SubAgent) setOutcome(status Status, summary string, err error) {
|
||||
s.finished = time.Now()
|
||||
}
|
||||
|
||||
// setResult records the successful loop result.
|
||||
// setResult 记录成功时的循环结果。
|
||||
func (s *SubAgent) setResult(res *loop.Result) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.result = res
|
||||
}
|
||||
|
||||
// setCancel stores the run context's cancel function so Abort can interrupt the
|
||||
// loop. If the sub-agent was already aborted (cancelRun raced ahead), the stored
|
||||
// cancel is invoked immediately.
|
||||
// setCancel 保存运行上下文的取消函数,使 Abort 能中断循环。若子代理已被中止
|
||||
// (cancelRun 先行),则立即调用保存的 cancel。
|
||||
func (s *SubAgent) setCancel(c context.CancelFunc) {
|
||||
s.mu.Lock()
|
||||
s.cancel = c
|
||||
@@ -215,10 +205,9 @@ func (s *SubAgent) setCancel(c context.CancelFunc) {
|
||||
}
|
||||
}
|
||||
|
||||
// cancelRun requests cancellation of the sub-agent. It is idempotent and safe for
|
||||
// concurrent use: the done channel is closed once and the stored cancel (when set)
|
||||
// is invoked once. A pending sub-agent unblocks from its parallel slot via done; a
|
||||
// running sub-agent is interrupted via the cancel func.
|
||||
// cancelRun 请求取消该子代理。它幂等且可并发使用:done channel 只关闭一次,已保存的
|
||||
// cancel(若已设置)只调用一次。排队中的子代理经 done 解除对并行槽位的阻塞;运行中的
|
||||
// 子代理经 cancel 函数被中断。
|
||||
func (s *SubAgent) cancelRun() {
|
||||
s.mu.Lock()
|
||||
s.abortReq = true
|
||||
@@ -234,7 +223,7 @@ func (s *SubAgent) cancelRun() {
|
||||
}
|
||||
}
|
||||
|
||||
// aborted reports whether a cancellation was requested for the sub-agent.
|
||||
// aborted 报告该子代理是否已被请求取消。
|
||||
func (s *SubAgent) aborted() bool {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
Reference in new issue
Block a user