推理策略 behaviour。
本模块定义推理策略统一契约,不嵌入 CMDC.Agent gen_statem 循环。
主库内置链式策略通过 CMDC.Plugin.Builtin.Reasoning 注入提示;树/图/投票/
精炼/自适应策略由 CMDC.Reasoning.Runner 编排 SubAgent 并行或递归分支。
设计边界
- 核心库暴露 behaviour、配置字段、链式策略 Plugin 和事件类型
- 默认
Options.reasoning_strategy == nil时保持现有 ReAct 式工具循环 - 策略运行状态由策略模块自行维护,核心库不假设其内部结构
Summary
Types
搜索分支规格,由 Runner 转换为 SubAgent 任务。
策略单步决策。
策略私有状态。核心库只做透传,不读取内部字段。
单个推理 thought。score 为 nil 表示尚未评分。
Types
@type branch() :: %{ :id => String.t(), :prompt => String.t(), optional(:parent_id) => String.t() | nil, optional(:metadata) => map() }
搜索分支规格,由 Runner 转换为 SubAgent 任务。
@type decision() :: {:continue, thought :: thought() | map() | String.t(), strategy_state()} | {:branch, [branch()], strategy_state()} | {:revise, feedback :: String.t(), strategy_state()} | {:done, answer :: term(), strategy_state()} | {:error, reason :: term(), strategy_state()}
策略单步决策。
{:continue, thought, state}— 单链策略追加 thought / 提示,由 Plugin 注入{:branch, branches, state}— 搜索策略生成分支,由 Runner 并行执行{:revise, feedback, state}— 精炼策略带反馈重试{:done, answer, state}— 策略收敛并产出答案{:error, reason, state}— 策略失败,由调用方决定降级或中止
@type strategy_state() :: term()
策略私有状态。核心库只做透传,不读取内部字段。
@type thought() :: %{ :id => String.t(), :content => String.t(), optional(:parent_id) => String.t() | nil, optional(:score) => float() | nil, optional(:metadata) => map() }
单个推理 thought。score 为 nil 表示尚未评分。
Callbacks
@callback aggregate(branch_results :: [thought()], strategy_state()) :: decision()
聚合分支结果。
Runner 完成并行分支后将 thought 列表交回策略,由策略决定继续、剪枝、 精炼或结束。
@callback init(opts :: keyword(), ctx :: CMDC.Context.t()) :: {:ok, strategy_state()} | {:error, term()}
初始化策略状态。
opts 来自 Options.reasoning_strategy 中 {Module, opts} 的第二项。
ctx 是当前 Agent 的只读执行上下文。
@callback name() :: String.t()
返回策略稳定名称。
该名称用于 EventBus 推理事件、审计投影和 UI 展示。
@callback step(strategy_state(), ctx :: CMDC.Context.t()) :: decision()
执行一次策略推进。
链式策略通常返回 {:continue, prompt_addon, state} 或
{:done, answer, state};搜索策略可返回 {:branch, branches, state}。