feat(agent): Skill Compilation + Adaptive Routing (v2/P4)

Episode → maybe_learn → llm_free skills, plus abstract router backends
(marqdo/small-llm/jev?/llm/auto) via plugins/agent; no core host changes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-24 13:28:50 +08:00
1 parent 8489d90926
commit a9ee858f53
14 files changed
+5785 -7

No files matched your search

+303 -1
View File
@@ -1,6 +1,6 @@
---
title: ext/ai/agent
description: Document-driven agent — step / plan; tools via lib/subtask; runtime via ABI v2 agent plugin.
description: Document-driven agent v2 — step / plan / run; Skill Compilation via ABI plugin (episodes → learn → llm_free).
import llm:ext/ai/llm.mq.md
import json:lib/json.mq.md
import sys:lib/sys.mq.md
@@ -1515,6 +1515,308 @@ Clear the plugin session bag for this agent id.
> agent_history_clear id=`id`
****
## resolve
+ `task`
+ `kb_dir`=.marqdo/agent-kb
+ `near_match`=True
+ `near_threshold`=0.78
Skill resolver (v2): exact → alias → canonical → near. Returns `mode` / `resource` / `llm_free` / `confidence`. Plugin ABI — no core host_* calls.
**tools = > json.get value=`self` key="tools"**
*> agent_resolve task=`task` kb_dir=`kb_dir` near_match=`near_match` near_threshold=`near_threshold` tools=`tools`*
## route
+ `task`
+ `backend`=auto
+ `kb_dir`=.marqdo/agent-kb
+ `memory_dir`=.marqdo/agent-memory
+ `threshold`=0.78
Adaptive Routing (Phase 4): abstract router — `marqdo` | `small-llm` | `jev` | `llm` | `auto`. Jev is optional. `auto` cascades policy → kb → optional jev → small-llm request. May return `needs_llm=True` + `prompt` for the caller to complete with a small/large model, then `route_apply`.
**tools = > json.get value=`self` key="tools"**
*> agent_route task=`task` backend=`backend` kb_dir=`kb_dir` memory_dir=`memory_dir` threshold=`threshold` tools=`tools`*
## route_apply
+ `task`
+ `reply`
+ `kb_dir`=.marqdo/agent-kb
+ `backend`=small-llm
Apply a router model line (`SKILL:<slug>` / `EXPLORE`) to a kb skill hit.
*> agent_route_apply task=`task` reply=`reply` kb_dir=`kb_dir` backend=`backend`*
## compile_policy
+ `memory_dir`=.marqdo/agent-memory
+ `min_evidence`=3
+ `force`=False
Policy Compilation: stable episode skill selections → `policies/router.mq.md` (deterministic Marqdo rules).
*> agent_compile_policy memory_dir=`memory_dir` min_evidence=`min_evidence` force=`force`*
## maybe_compile_policy
+ `memory_dir`=.marqdo/agent-memory
+ `min_evidence`=3
Learn gate for the Adaptive Router policy layer.
*> agent_maybe_compile_policy memory_dir=`memory_dir` min_evidence=`min_evidence`*
## record_episode
+ `task`
+ `status`=ok
+ `result`=None
+ `mode`=explore
+ `skill`=none
+ `observation`=None
+ `workbook`=None
+ `cache`=None
+ `match`=None
+ `llm_calls`=0
+ `tool_calls`=0
+ `error`=None
+ `memory_dir`=.marqdo/agent-memory
Persist one run as `.mq.md` under `memory_dir/episodes/YYYY/MM/` (v2 Phase 1). Memory is executable documentation, not a vector bag.
*> agent_record_episode task=`task` status=`status` result=`result` mode=`mode` skill=`skill` observation=`observation` workbook=`workbook` cache=`cache` match=`match` llm_calls=`llm_calls` tool_calls=`tool_calls` error=`error` memory_dir=`memory_dir`*
## maybe_learn
+ `task`
+ `kb_dir`=.marqdo/agent-kb
+ `memory_dir`=.marqdo/agent-memory
+ `improve_every`=3
+ `promote`=True
Offline learn gate: when ≥ `improve_every` successful episodes share a task hash, generalize → synthesize → compile → optional OKF promote (`llm_free`). Also attempts Policy Compilation for Adaptive Routing.
**skill_learn = > agent_maybe_learn task=`task` kb_dir=`kb_dir` memory_dir=`memory_dir` improve_every=`improve_every` promote=`promote`**
**policy_learn = > agent_maybe_compile_policy memory_dir=`memory_dir` min_evidence=`improve_every`**
**out = > json.parse text={"learned":false}**
**sl = > json.get value=`skill_learn` key="learned"**
**out = > json.set map=`out` key="learned" value=`sl`**
**out = > json.set map=`out` key="skill" value=`skill_learn`**
**out = > json.set map=`out` key="policy" value=`policy_learn`**
*out*
## compile
+ `task`
+ `kb_dir`=.marqdo/agent-kb
+ `memory_dir`=.marqdo/agent-memory
+ `min_evidence`=3
+ `promote`=True
+ `force`=False
Compile a candidate skill into versioned `skills/<slug>/vN.mq.md` and optionally promote into agent-kb.
*> agent_compile_skill task=`task` kb_dir=`kb_dir` memory_dir=`memory_dir` min_evidence=`min_evidence` promote=`promote` force=`force`*
## metrics
+ `memory_dir`=.marqdo/agent-memory
Reasoning amortization stats over recorded episodes (`avg_llm_calls`, `compiled_runs`, …).
*> agent_metrics memory_dir=`memory_dir`*
## run
+ `task`
+ `kb_dir`=.marqdo/agent-kb
+ `memory_dir`=.marqdo/agent-memory
+ `learn`=True
+ `verify`=True
+ `writeback`=True
+ `reuse`=True
+ `near_match`=True
+ `near_threshold`=0.78
+ `promote`=True
+ `improve_every`=3
+ `explore_n`=3
+ `max_rounds`=4
+ `stream`=False
+ `echo`=False
+ `trace`=False
+ `force`=False
+ `router`=auto
+ `router_model`=None
Primary author entry (v2 + P4): Adaptive `route` → execute (policy/kb skill) or `plan` → record episode → maybe_learn (skill + policy). `router` = `auto`|`marqdo`|`small-llm`|`jev`|`llm`. Optional `router_model` (llm handle) answers `needs_llm` prompts — small models welcome; Jev is never required.
**routed = > `self`.route task=`task` backend=`router` kb_dir=`kb_dir` memory_dir=`memory_dir` threshold=`near_threshold`**
**route_matched = > json.get value=`routed` key="matched"**
**needs_llm = > json.get value=`routed` key="needs_llm"**
**exec_mode = "explore"**
**llm_calls = 0**
**out = None**
**route_backend = > json.get value=`routed` key="backend"**
1. `route_matched`
**path = > json.get value=`routed` key="resource"**
1. `path`
**aw = > await_workbook path=`path`**
**code = > json.get value=`aw` key="code"**
**child_val = > json.get value=`aw` key="value"**
**last_obs = > json.get value=`aw` key="observation"**
1. `code` == 0
> agent_kb_record_hit kb_dir=`kb_dir` goal=`task` improve_every=`improve_every`
**lf = > json.get value=`routed` key="llm_free"**
1. `lf`
**exec_mode = "compiled"**
2. *
**exec_mode = "routed"**
**out = > json.parse text={"status":"ok"}**
**out = > json.set map=`out` key="status" value="ok"**
**out = > json.set map=`out` key="goal" value=`task`**
**out = > json.set map=`out` key="result" value=`child_val`**
**out = > json.set map=`out` key="workbook" value=`path`**
**out = > json.set map=`out` key="cache" value="hit"**
**rmode = > json.get value=`routed` key="mode"**
**out = > json.set map=`out` key="match" value=`rmode`**
**sk = > json.get value=`routed` key="skill"**
**out = > json.set map=`out` key="skill" value=`sk`**
**out = > json.set map=`out` key="observation" value=`last_obs`**
**out = > json.set map=`out` key="summary" value="adaptive route hit"**
**out = > json.set map=`out` key="route" value=`routed`**
**llm_calls = 0**
2. *
**route_matched = False**
2. *
**route_matched = False**
2. *
**_ = 1**
1. `route_matched`
**_ = 1**
2. `needs_llm`
1. `router_model`
**prompt = > json.get value=`routed` key="prompt"**
1. `stream`
**evs = > router_model.complete prompt=`prompt` stream=True echo=`echo`**
**reply = > llm.stream_result events=`evs`**
2. *
**reply = > router_model.complete prompt=`prompt`**
**applied = > `self`.route_apply task=`task` reply=`reply` kb_dir=`kb_dir` backend=`router`**
**llm_calls = 1**
**am = > json.get value=`applied` key="matched"**
1. `am`
**path = > json.get value=`applied` key="resource"**
**aw = > await_workbook path=`path`**
**code = > json.get value=`aw` key="code"**
**child_val = > json.get value=`aw` key="value"**
**last_obs = > json.get value=`aw` key="observation"**
1. `code` == 0
**exec_mode = "routed"**
**out = > json.parse text={"status":"ok"}**
**out = > json.set map=`out` key="status" value="ok"**
**out = > json.set map=`out` key="goal" value=`task`**
**out = > json.set map=`out` key="result" value=`child_val`**
**out = > json.set map=`out` key="workbook" value=`path`**
**out = > json.set map=`out` key="cache" value="soft-hit"**
**out = > json.set map=`out` key="match" value="routed"**
**sk = > json.get value=`applied` key="skill"**
**out = > json.set map=`out` key="skill" value=`sk`**
**out = > json.set map=`out` key="observation" value=`last_obs`**
**out = > json.set map=`out` key="summary" value="router model selected skill"**
**out = > json.set map=`out` key="route" value=`applied`**
**route_matched = True**
2. *
**route_matched = False**
2. *
**route_matched = False**
2. *
**route_matched = False**
3. *
**_ = 1**
1. `route_matched`
**_ = 1**
2. *
**resolved = > `self`.resolve task=`task` kb_dir=`kb_dir` near_match=`near_match` near_threshold=`near_threshold`**
**mode = > json.get value=`resolved` key="mode"**
**llm_free = > json.get value=`resolved` key="llm_free"**
1. `llm_free`
**path = > json.get value=`resolved` key="resource"**
**aw = > await_workbook path=`path`**
**code = > json.get value=`aw` key="code"**
**child_val = > json.get value=`aw` key="value"**
**last_obs = > json.get value=`aw` key="observation"**
1. `code` == 0
> agent_kb_record_hit kb_dir=`kb_dir` goal=`task` improve_every=`improve_every`
**exec_mode = "compiled"**
**out = > json.parse text={"status":"ok"}**
**out = > json.set map=`out` key="status" value="ok"**
**out = > json.set map=`out` key="goal" value=`task`**
**out = > json.set map=`out` key="result" value=`child_val`**
**out = > json.set map=`out` key="workbook" value=`path`**
**out = > json.set map=`out` key="cache" value="hit"**
**out = > json.set map=`out` key="match" value=`mode`**
**sk = > json.get value=`resolved` key="skill"**
**out = > json.set map=`out` key="skill" value=`sk`**
**out = > json.set map=`out` key="observation" value=`last_obs`**
**out = > json.set map=`out` key="summary" value="compiled skill (llm_free)"**
**llm_calls = 0**
2. *
**out = > `self`.plan goal=`task` kb_dir=`kb_dir` writeback=`writeback` reuse=`reuse` near_match=`near_match` near_threshold=`near_threshold` promote=`promote` improve_every=`improve_every` explore_n=`explore_n` max_rounds=`max_rounds` stream=`stream` echo=`echo` trace=`trace` force=`force`**
**exec_mode = "explore"**
**llm_calls = 1**
2. *
**out = > `self`.plan goal=`task` kb_dir=`kb_dir` writeback=`writeback` reuse=`reuse` near_match=`near_match` near_threshold=`near_threshold` promote=`promote` improve_every=`improve_every` explore_n=`explore_n` max_rounds=`max_rounds` stream=`stream` echo=`echo` trace=`trace` force=`force`**
**cache = > json.get value=`out` key="cache"**
1. `cache` == hit
**exec_mode = "compiled"**
**llm_calls = 0**
2. `cache` == soft-hit
**exec_mode = "near"**
**llm_calls = 0**
3. *
**exec_mode = "explore"**
**llm_calls = 1**
**st = > json.get value=`out` key="status"**
**res = > json.get value=`out` key="result"**
**wb_path = > json.get value=`out` key="workbook"**
**obs = > json.get value=`out` key="observation"**
**sk2 = > json.get value=`out` key="skill"**
**cache2 = > json.get value=`out` key="cache"**
**match2 = > json.get value=`out` key="match"**
**err = > json.get value=`out` key="error"**
1. `verify`
1. `st` == "ok"
**verification = "status: passed"**
2. *
**verification = "status: failed"**
2. *
**verification = "status: skipped"**
**ep = > `self`.record_episode task=`task` status=`st` result=`res` mode=`exec_mode` skill=`sk2` observation=`obs` workbook=`wb_path` cache=`cache2` match=`match2` llm_calls=`llm_calls` error=`err` memory_dir=`memory_dir`**
**learning = None**
1. `learn`
1. `st` == "ok"
**learning = > `self`.maybe_learn task=`task` kb_dir=`kb_dir` memory_dir=`memory_dir` improve_every=`improve_every` promote=`promote`**
2. *
**learning = None**
2. *
**_ = 1**
**execution = > json.parse text={"mode":"explore","llm_calls":0}**
**execution = > json.set map=`execution` key="mode" value=`exec_mode`**
**execution = > json.set map=`execution` key="llm_calls" value=`llm_calls`**
**execution = > json.set map=`execution` key="skill" value=`sk2`**
**execution = > json.set map=`execution` key="router" value=`route_backend`**
**out = > json.set map=`out` key="execution" value=`execution`**
**out = > json.set map=`out` key="episode" value=`ep`**
**out = > json.set map=`out` key="route" value=`routed`**
1. `learning`
**out = > json.set map=`out` key="learning" value=`learning`**
2. *
**_ = 1**
*out*
## step
+ `task`
+ `writeback`=True
+110 -1
View File
@@ -1,6 +1,6 @@
---
title: ext/ai/智能体
description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务;运行时经 ABI v2 agent 插件。
description: 文档驱动智能体 v2 — 单步 / 多步 / 运行;技能编译经 ABI v2 agent 插件(情景 → 学习 → llm_free)。
导入 大模型:ext/ai/大模型.mq.md
import agent:ext/ai/agent.mq.md
import json:lib/json.mq.md
@@ -212,3 +212,112 @@ import json:lib/json.mq.md
**standing = > json.get value=`self` key="standing"**
**en = > agent.agent model=`model` tools=`tools` standing=`standing`**
*> en.plan goal=`目标` workbook_dir=`工作簿目录` workbook=`工作簿` max_rounds=`最大轮次` confirm=`确认分解` writeback=`写回` skeleton=`骨架` reuse=`复用` optimize=`优化` force=`强制` soft_match=`软命中` near_match=`近义命中` near_threshold=`近义阈值` promote=`晋升` kb_dir=`知识库目录` improve_every=`改进周期` explore_n=`探索次数` stream=`流式` echo=`打印增量` trace=`轨迹`*
## 解析
+ `任务`
+ `知识库目录`=.marqdo/agent-kb
+ `近义命中`=True
+ `近义阈值`=0.78
技能解析(v2):exact → alias → canonical → near。
**model = > json.get value=`self` key="model"**
**tools = > json.get value=`self` key="tools"**
**standing = > json.get value=`self` key="standing"**
**en = > agent.agent model=`model` tools=`tools` standing=`standing`**
*> en.resolve task=`任务` kb_dir=`知识库目录` near_match=`近义命中` near_threshold=`近义阈值`*
## 记录情景
+ `任务`
+ `状态`=ok
+ `结果`=None
+ `模式`=explore
+ `技能`=none
+ `观察`=None
+ `工作簿`=None
+ `缓存`=None
+ `匹配`=None
+ `大模型次数`=0
+ `工具次数`=0
+ `错误`=None
+ `记忆目录`=.marqdo/agent-memory
将一次执行沉淀为 `.mq.md` 情景记忆。
**model = > json.get value=`self` key="model"**
**tools = > json.get value=`self` key="tools"**
**standing = > json.get value=`self` key="standing"**
**en = > agent.agent model=`model` tools=`tools` standing=`standing`**
*> en.record_episode task=`任务` status=`状态` result=`结果` mode=`模式` skill=`技能` observation=`观察` workbook=`工作簿` cache=`缓存` match=`匹配` llm_calls=`大模型次数` tool_calls=`工具次数` error=`错误` memory_dir=`记忆目录`*
## 尝试学习
+ `任务`
+ `知识库目录`=.marqdo/agent-kb
+ `记忆目录`=.marqdo/agent-memory
+ `改进周期`=3
+ `晋升`=True
情景足够时:归纳 → 合成候选 → 编译 → 可选晋升为 `llm_free` 技能。
**model = > json.get value=`self` key="model"**
**tools = > json.get value=`self` key="tools"**
**standing = > json.get value=`self` key="standing"**
**en = > agent.agent model=`model` tools=`tools` standing=`standing`**
*> en.maybe_learn task=`任务` kb_dir=`知识库目录` memory_dir=`记忆目录` improve_every=`改进周期` promote=`晋升`*
## 运行
+ `任务`
+ `知识库目录`=.marqdo/agent-kb
+ `记忆目录`=.marqdo/agent-memory
+ `学习`=True
+ `校验`=True
+ `写回`=True
+ `复用`=True
+ `近义命中`=True
+ `近义阈值`=0.78
+ `晋升`=True
+ `改进周期`=3
+ `探索次数`=3
+ `最大轮次`=4
+ `流式`=False
+ `打印增量`=False
+ `轨迹`=False
+ `强制`=False
+ `路由`=auto
+ `路由模型`=None
v2+P4 主入口:自适应路由 → 执行 → 记录情景 → 尝试学习(技能 + 策略)。`路由`=`auto`|`marqdo`|`small-llm`|`jev`|`llm`;Jev 可选,小模型可作决策器。
**model = > json.get value=`self` key="model"**
**tools = > json.get value=`self` key="tools"**
**standing = > json.get value=`self` key="standing"**
**en = > agent.agent model=`model` tools=`tools` standing=`standing`**
*> en.run task=`任务` kb_dir=`知识库目录` memory_dir=`记忆目录` learn=`学习` verify=`校验` writeback=`写回` reuse=`复用` near_match=`近义命中` near_threshold=`近义阈值` promote=`晋升` improve_every=`改进周期` explore_n=`探索次数` max_rounds=`最大轮次` stream=`流式` echo=`打印增量` trace=`轨迹` force=`强制` router=`路由` router_model=`路由模型`*
## 路由
+ `任务`
+ `后端`=auto
+ `知识库目录`=.marqdo/agent-kb
+ `记忆目录`=.marqdo/agent-memory
+ `阈值`=0.78
自适应路由(阶段 4):`marqdo` / `small-llm` / `jev` / `llm` / `auto`。
**model = > json.get value=`self` key="model"**
**tools = > json.get value=`self` key="tools"**
**standing = > json.get value=`self` key="standing"**
**en = > agent.agent model=`model` tools=`tools` standing=`standing`**
*> en.route task=`任务` backend=`后端` kb_dir=`知识库目录` memory_dir=`记忆目录` threshold=`阈值`*
## 编译策略
+ `记忆目录`=.marqdo/agent-memory
+ `最少证据`=3
+ `强制`=False
策略编译:稳定的 task→skill 映射 → `policies/router.mq.md`。
**model = > json.get value=`self` key="model"**
**tools = > json.get value=`self` key="tools"**
**standing = > json.get value=`self` key="standing"**
**en = > agent.agent model=`model` tools=`tools` standing=`standing`**
*> en.compile_policy memory_dir=`记忆目录` min_evidence=`最少证据` force=`强制`*