From 4a2d89406b4f2665b8614715618a8915315758fd Mon Sep 17 00:00:00 2001 From: cflmy Date: Thu, 24 Sep 2026 19:34:05 +0800 Subject: [PATCH] fix(agent): preserve llm_free resources under plan force Stop plan from clobbering an existing resources workbook with an explore skeleton. Add llm.prompt helpers and agent-plan-preserve gold. Co-authored-by: Cursor --- doc/design/ext-agent.md | 2 +- doc/design/ext-llm.md | 5 ++- doc/roadmap/ai-stack.md | 9 +++-- ext/ai/agent.mq.md | 12 +++++- ext/ai/llm.mq.md | 40 +++++++++++++----- ext/ai/大模型.mq.md | 35 +++++++++++----- scripts/agent-harness.sh | 1 + tests/ext/agent-plan-preserve.mq.md | 54 +++++++++++++++++++++++++ tests/ext/fixtures/preserve-skill.mq.md | 10 +++++ tests/gold.rs | 11 +++++ 10 files changed, 151 insertions(+), 28 deletions(-) create mode 100644 tests/ext/agent-plan-preserve.mq.md create mode 100644 tests/ext/fixtures/preserve-skill.mq.md diff --git a/doc/design/ext-agent.md b/doc/design/ext-agent.md index 4b4c081..6f79cf1 100644 --- a/doc/design/ext-agent.md +++ b/doc/design/ext-agent.md @@ -194,7 +194,7 @@ ext/ | `## 分解` | `## decompose` |(可公开或内部)目标 → 子任务表 | | `## 清空历史` | `## clear_history` | 若仍保留会话式辅助状态则清空;**不以隐藏 chat 袋为真相源**——真相在文档写回 | -**Skill Compilation(v2)**:Agent 的记忆不是向量袋,而是可执行 `.mq.md`。路径:`episodes/` → `experiences/` → `candidates/` → `skills/vN.mq.md` → OKF `agent-kb`(`llm_free`)。编译技能正文为合法 Marqdo(`**result = …**` + 斜体返回);金样 `tests/ext/agent-zero-llm.mq.md` 验收 policy+skill 全路径 `execution.llm_calls=0`。 +**Skill Compilation(v2)**:Agent 的记忆不是向量袋,而是可执行 `.mq.md`。路径:`episodes/` → `experiences/` → `candidates/` → `skills/vN.mq.md` → OKF `agent-kb`(`llm_free`)。编译技能正文为合法 Marqdo(`**result = …**` + 斜体返回);金样 `tests/ext/agent-zero-llm.mq.md` 验收 policy+skill 全路径 `execution.llm_calls=0`。`plan` 对已存在的 `resources/.mq.md` **不覆盖**(`agent-plan-preserve`)。 **LLM 成本**:`model_turn` 走 `model.ask` / `stream`+`collect`,把 `LLMResult.tokens` 写入 episode / `execution`。 diff --git a/doc/design/ext-llm.md b/doc/design/ext-llm.md index b3f5edc..23ac67d 100644 --- a/doc/design/ext-llm.md +++ b/doc/design/ext-llm.md @@ -31,7 +31,7 @@ import llm:ext/ai/llm.mq.md | `## load_env` | `## 加载环境` | Load `.env` | | `## create` | `## 创建` | Factory → LLM handle | | `## fast` / `## reasoning` | `## 快速` / `## 推理` | Named handles | -| `## prompt_load` | `## 加载提示` | Load prompt artifact (`type: prompt` → body) | +| `## prompt_load` / `## prompt` / `## prompt_body` | `## 加载提示` / `## 提示` / `## 提示正文` | Prompt artifacts (`type: prompt` → body) | | `## ask` (module) | `## 提问` | Convenience → **text** (`prompt=` or `path=`) | | `## collect` | `## 收集` | Events → text | | `## stream_result` | `## 流式结果` | Alias of `collect` | @@ -54,7 +54,8 @@ import llm:ext/ai/llm.mq.md **events = > `fast`.stream prompt=`prompt`** **text = > llm.collect events=`events`** -**body = > llm.prompt_load path="fixtures/prompt-marqdo.md"** +**body = > llm.prompt path="fixtures/prompt-marqdo.md"** +**body2 = > llm.prompt text=`raw`** ``` ### LLMResult diff --git a/doc/roadmap/ai-stack.md b/doc/roadmap/ai-stack.md index 26f06e0..525db6d 100644 --- a/doc/roadmap/ai-stack.md +++ b/doc/roadmap/ai-stack.md @@ -31,7 +31,7 @@ | `create` 工厂;handle 带 `backend` | **done** | | LLMResult:`text` / `model` / `finish` / `backend` / `usage` / `tokens` | **done** | | Named models(`llm.fast` / `llm.reasoning`) | **done** | -| Prompt document:`prompt_load` + `type: prompt` 正文 | **done**(`ask path=` / `llm.ask` 无参吃当前文档仍暂缓) | +| Prompt document:`prompt` / `prompt_load` + `type: prompt` 正文 | **done**(`llm.ask` 无参吃当前入口文档仍暂缓,需 `sys.module_source`) | | 拆 `ext/ai/llm/openai.mq.md` + `ollama` 后端 | **done** | | tool_calls 完整 Result | 暂缓 | | Transport → 可选 ABI 插件 | 暂缓 | @@ -43,7 +43,7 @@ **events = > model.stream prompt=`p`** **text = > llm.collect events=`events`** **fast = > llm.fast** -**prompt = > llm.prompt_load path="prompts/task.md"** +**prompt = > llm.prompt path="prompts/task.md"** ``` ## 4. Agent(002–004)— 已/待 @@ -55,8 +55,9 @@ | Policy Compilation → `policies/router.mq.md` | **done** | | `run` 串联 route → execute → record → learn | **done** | | Agent 默认走 `model.ask` + Result.usage 记成本 | **done**(`model_turn`) | -| Prompt artifact / document-as-prompt | **done**(经 llm `path=`) | +| Prompt artifact / document-as-prompt | **done**(经 llm `path=` / `prompt`) | | 零 LLM 执行(policy+skill → `llm_calls=0`) | **done** | +| `plan` 不覆盖已有 `llm_free` resource | **done** | | 真 MCP client / resume / Jev 接线 / 小模型 live 路由 | 暂缓 | ## 5. 联调顺序 @@ -69,5 +70,5 @@ ## 6. 验收金样 -- Agent:`tests/ext/agent-v2-learn.mq.md` · `agent-p4-route.mq.md` · `agent-zero-llm.mq.md` · `scripts/agent-harness.sh` +- Agent:`tests/ext/agent-v2-learn.mq.md` · `agent-p4-route.mq.md` · `agent-zero-llm.mq.md` · `agent-plan-preserve.mq.md` · `scripts/agent-harness.sh` - LLM:`tests/ext/llm-ask-offline.mq.md` · `llm-stream-offline.mq.md` · `llm-ctor-offline.mq.md` · `llm-named-offline.mq.md` diff --git a/ext/ai/agent.mq.md b/ext/ai/agent.mq.md index 8296697..e1bcfed 100644 --- a/ext/ai/agent.mq.md +++ b/ext/ai/agent.mq.md @@ -2250,18 +2250,28 @@ Reuse lookup: exact → alias → canonicalize → optional local n-gram `near` **b = > json.get value=`parts` key="b"** **c = > json.get value=`parts` key="c"** **path = workbook_dir + a + slug + b + ts + c** + > fs.write_text path=`path` text=`skel` 2. `explore` **parts = > json.parse text={"a":"/explore/","b":"/","c":".mq.md"}** **a = > json.get value=`parts` key="a"** **b = > json.get value=`parts` key="b"** **c = > json.get value=`parts` key="c"** **path = kb_dir + a + slug + b + explore_attempt + c** + **ex = > fs.exists path=`path`** + 1. `ex` + **_ = 1** + 2. * + > fs.write_text path=`path` text=`skel` 3. * **parts = > json.parse text={"a":"/resources/","b":".mq.md"}** **a = > json.get value=`parts` key="a"** **b = > json.get value=`parts` key="b"** **path = kb_dir + a + slug + b** - > fs.write_text path=`path` text=`skel` + **ex = > fs.exists path=`path`** + 1. `ex` + **_ = 1** + 2. * + > fs.write_text path=`path` text=`skel` 1. `confirm` **out = > json.parse text={"status":"pending"}** diff --git a/ext/ai/llm.mq.md b/ext/ai/llm.mq.md index 48d95e0..023a390 100644 --- a/ext/ai/llm.mq.md +++ b/ext/ai/llm.mq.md @@ -96,21 +96,19 @@ Named strong/reasoning handle. Model from arg, else `MARQDO_LLM_REASONING_MODEL` *> create model=`mdl` base_url=`base_url` api_key=`api_key` name="reasoning" backend="openai-compatible"* -## prompt_load - + `path` +## prompt_body + + `src` -Load a prompt artifact (`.mq.md` / `.md`) from disk for `ask`. -If frontmatter declares `type: prompt` (or `类型: 提示`), return the body after the closing `---`; otherwise the full file text. +If `src` starts with YAML frontmatter declaring `type: prompt` / `类型: 提示`, return the body after the closing `---`; otherwise return `src` unchanged. -**raw = > fs.read_text path=`path`** -**sw = > text.starts_with text=`raw` prefix="---"** +**sw = > text.starts_with text=`src` prefix="---"** 1. not `sw` - *raw* + *src* 2. * - **parts = > split value=`raw` sep="\n---\n"** + **parts = > split value=`src` sep="\n---\n"** **n = > len value=`parts`** 1. `n` < 2 - *raw* + *src* 2. * **fm = > at value=`parts` index=0** **is_p = > text.contains text=`fm` sub="type: prompt"** @@ -122,7 +120,29 @@ If frontmatter declares `type: prompt` (or `类型: 提示`), return the body af **body = > at value=`parts` index=1** *> text.str_trim s=`body`* 2. * - *raw* + *src* + +## prompt_load + + `path` + +Load a prompt artifact (`.mq.md` / `.md`) from disk for `ask`. + +**raw = > fs.read_text path=`path`** +*> prompt_body src=`raw`* + +## prompt + + `path`=None + + `text`=None + +Prompt artifact helper: `path=` loads a file; `text=` strips `type: prompt` frontmatter from an in-memory string. + +1. `path` + *> prompt_load path=`path`* +2. `text` + *> prompt_body src=`text`* +3. * + > print text=ext/ai/llm.prompt: pass path= or text= + > sys.exit code=1 ## ask + `prompt`=None diff --git a/ext/ai/大模型.mq.md b/ext/ai/大模型.mq.md index 268b6ec..71cb43e 100644 --- a/ext/ai/大模型.mq.md +++ b/ext/ai/大模型.mq.md @@ -82,20 +82,17 @@ import openai:ext/ai/llm/openai.mq.md *> 创建 模型=`mdl` 基址=`基址` 密钥=`密钥` 名="reasoning" 后端="openai-compatible"* -## 加载提示 - + `路径` +## 提示正文 + + `源` -加载提示文档。若 frontmatter 含 `type: prompt` / `类型: 提示`,返回闭合 `---` 后的正文。 - -**raw = > 文件.读文本 路径=`路径`** -**sw = > 文本.开头是 内容=`raw` 前缀="---"** +**sw = > 文本.开头是 内容=`源` 前缀="---"** 1. 非 `sw` - *raw* + *源* 2. * - **parts = > split value=`raw` sep="\n---\n"** + **parts = > split value=`源` sep="\n---\n"** **n = > len value=`parts`** 1. `n` < 2 - *raw* + *源* 2. * **fm = > at value=`parts` index=0** **is_p = > 文本.包含 内容=`fm` 子串="type: prompt"** @@ -107,7 +104,25 @@ import openai:ext/ai/llm/openai.mq.md **body = > at value=`parts` index=1** *> 文本.去空白 s=`body`* 2. * - *raw* + *源* + +## 加载提示 + + `路径` + +**raw = > 文件.读文本 路径=`路径`** +*> 提示正文 源=`raw`* + +## 提示 + + `路径`=None + + `文本`=None + +1. `路径` + *> 加载提示 路径=`路径`* +2. `文本` + *> 提示正文 源=`文本`* +3. * + > 打印 内容=ext/ai/大模型.提示: 请传 路径= 或 文本= + > 系统.退出 码=1 ## 提问 + `提示`=None diff --git a/scripts/agent-harness.sh b/scripts/agent-harness.sh index dbc13d1..da6c6c6 100755 --- a/scripts/agent-harness.sh +++ b/scripts/agent-harness.sh @@ -33,6 +33,7 @@ run tests/ext/agent-workbook-patch-a0.mq.md run tests/ext/agent-v2-learn.mq.md run tests/ext/agent-p4-route.mq.md run tests/ext/agent-zero-llm.mq.md +run tests/ext/agent-plan-preserve.mq.md run examples/agent-okf-flywheel/index.mq.md run examples/agent-pong/index.mq.md diff --git a/tests/ext/agent-plan-preserve.mq.md b/tests/ext/agent-plan-preserve.mq.md new file mode 100644 index 0000000..14e671d --- /dev/null +++ b/tests/ext/agent-plan-preserve.mq.md @@ -0,0 +1,54 @@ +--- +title: plan must not overwrite existing llm_free resources +description: force+confirm plan keeps a pre-seeded compiled skill workbook intact (offline). +import agent:ext/ai/agent.mq.md +import fs:lib/fs.mq.md +import json:lib/json.mq.md +import llm:ext/ai/llm.mq.md +import plugin:lib/plugin.mq.md +import sys:lib/sys.mq.md +import text:lib/text.mq.md +--- + +# main + +**p = > plugin.native_path name="agent"** +1. `p` + > plugin.load path=`p` +2. * + > print text=no-agent-plugin + > sys.exit code=1 + +**kb = ".marqdo/agent-kb-preserve-test"** +**ex = > fs.exists path=`kb`** +1. `ex` + > fs.remove_tree path=`kb` +2. * + **_ = 1** +> fs.make_dirs path=`kb` +**parts = > json.parse text={"res":"/resources/","ext":".mq.md","dir":"/resources"}** +**res_dir = kb + [dir](`parts`)** +> fs.make_dirs path=`res_dir` + +**goal = "Preserve compiled skill body under force plan"** +**slug = > agent_goal_slug goal=`goal`** +**res = kb + [res](`parts`) + slug + [ext](`parts`)** +**body = > fs.read_text path="fixtures/preserve-skill.mq.md"** +> fs.write_text path=`res` text=`body` + +**model = > llm.create** +`tools` = + +| 工具 | +|------| + +**ag = > agent.agent model=`model` tools=`tools` standing="preserve probe"** +**out = > `ag`.plan goal=`goal` kb_dir=`kb` force=True confirm=True writeback=False reuse=False** +**st = > json.get value=`out` key="status"** +> print text=`st` + +**after = > fs.read_text path=`res`** +**kept = > text.contains text=`after` sub="kept"** +**no_skel = > text.contains text=`after` sub="worker.step"** +> print text=`kept` +> print text=`no_skel` diff --git a/tests/ext/fixtures/preserve-skill.mq.md b/tests/ext/fixtures/preserve-skill.mq.md new file mode 100644 index 0000000..4530d5a --- /dev/null +++ b/tests/ext/fixtures/preserve-skill.mq.md @@ -0,0 +1,10 @@ +--- +type: skill +status: compiled +llm_free: true +--- + +# main + +**`result` = "kept"** +*`result`* diff --git a/tests/gold.rs b/tests/gold.rs index ba45927..d078f05 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -3808,6 +3808,17 @@ auto/marqdo", ); } +#[test] +fn ext_agent_plan_preserve() { + ensure_agent_plugin_built(); + assert_out_llm_offline( + "tests/ext/agent-plan-preserve.mq.md", + "pending +True +False", + ); +} + #[test] fn ext_agent_zero_llm() { ensure_agent_plugin_built();