refactor(core): restore type-safe workflow automaton from Pulse design #80
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Background
Nerve 的 workflow 类型系统相比 Pulse(
packages/pulse/src/workflows/workflow-type.ts)严重退化:Role<Meta>类型安全Role无泛型,execute(prompt: unknown)CommandEvent = { type: string; [key: string]: unknown }万金油WorkflowMessage[]完整历史prompt: unknown,依赖 moderator 提炼START/END伪角色"thread_start"limits.maxRounds类型内置MAX_STEPS = 1000在 worker 里CommandEvent核心问题:Pulse 精心设计的类型安全自动机模型,在 Nerve 里被简化成了无类型的事件循环。
Target Types
以下是讨论定稿的类型设计(
@nerve/core):Design Decisions
1. 一个泛型参数
M extends RoleMeta驱动全部推导用户声明一张 meta 映射表,类型系统自动对齐 roles 的 key、meta 类型、moderator 的 discriminated union:
2. WorkflowMessage.meta 是
unknown消息链是运行时数据,混合不同 role 的消息,静态类型推不动。Role 主要消费
content,需要 meta 时自行 assert。精确的 meta 类型留给 moderator 的RoleSignal<M>。3. maxRounds 是 Thread 属性,不是 Workflow 属性
StartSignal.meta.maxRounds传入round+maxRounds4. START/END 用字符串常量,不用 Symbol
Nerve 用 IPC worker 进程隔离,Symbol 跨进程不可序列化。
"__start__"/"__end__"作为as const字符串常量。5. Role 是纯函数,不限于 agent
Role 只是
(messages) => Promise<RoleResult<Meta>>。实现可以是:类型不约束实现方式,只约束输入输出契约。
Scope
Phase 1: 类型替换(
@nerve/core)types.ts中 workflow 相关类型为上述定义CommandEvent、ThreadState、ModerateResult、ModerateFn、RoleExecuteFn旧类型WorkflowMessage、Role、RoleResult、RoleMeta、StartSignal、RoleSignal、Moderator、WorkflowDefinition、START、ENDPhase 2: Worker 适配(
@nerve/daemon)workflow-worker.ts适配新类型:维护WorkflowMessage[]chain,循环改为 signal 驱动Phase 3: 防注入(#79 合并进来)
spawnSafe()工具函数 —shell: falsePhase 4: 测试
WorkflowDefinition<M>的类型推导正确性Supersedes
小橘 🍊(NEKO Team)