A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Add AGENTS.md upkeep proposals
Fei2411d78ecdfb235d
diff --git a/docs/plans/2026-10-27-agents-md-upkeep.md b/docs/plans/2026-10-27-agents-md-upkeep.mdnew file mode 100644index 00000000..a050fd61--- /dev/null+++ b/docs/plans/2026-10-27-agents-md-upkeep.md@@ -0,0 +1,32 @@+# 从项目工作中提出 AGENTS.md 更新++- 日期:2026-10-27 定行为+- 代码:`ds-bot/src/host/learning.js`、`src/client/learning-card.js`++## 为什么++用户对项目的长期要求、反复纠正和检查步骤适合保存在项目的 `AGENTS.md`,让之后在该文件夹工作的 Bot 都能读取。文件应保持简短、准确;Bot 只能提出有工作证据支持的修改,不能自行写入。++## 已定的行为++- 复用现有学习调度和待审工作记录。一个文件夹每轮最多审阅一次,并汇总其中所有 Bot 的工作。+- 只审阅 `workFolder` 检查通过,且包含 `.git` 或 `AGENTS.md` 的文件夹。不存在的 `AGENTS.md` 只可在含 `.git` 的文件夹中新建。+- 默认不修改。候选规则必须适用于大多数后续任务、有证据支持、不显而易见、长期有效,且不能与文件中已有规则重复。+- 提案只包含 1–3 项精确的整行添加、替换或删除。检查密钥、日期、版本和任务编号;过大的文件、无效或没有实际变化的结果不提出。+- 提案显示在相关 Bot 的对话卡片中,包含文件夹、逐项差异和理由。只有用户批准后才写入。+- 批准前重新检查文件是否仍与提案时相同;匹配时在同一文件夹写临时文件并原子替换。文件变化时保留提案,用户可以拒绝。+- 拒绝的修改按文件夹记录,供之后的审阅参考。`agentsMd: false` 只关闭项目文件审阅,不关闭技能学习。++## 不做什么++- 不自动写入或替换项目文件。+- 不为普通任务创建定时器,不审阅没有项目标记的文件夹,不将单次任务进展写入 `AGENTS.md`。++## 已知限制++- Bot 是否提出更新取决于模型能否从有限的工作记录中识别出持久、可复用的规则;不确定时应不提出。+- 只处理符合大小限制且能读取的 `AGENTS.md`。++## 变更++- 2026-10-27:定下项目文件静默审阅、用户审批后原子写入的行为。diff --git a/docs/plans/README.md b/docs/plans/README.mdindex 283d53e4..1d8150b6 100644--- a/docs/plans/README.md+++ b/docs/plans/README.md@@ -43,6 +43,7 @@| [浏览器插件](2026-10-21-browser-plugins.md) | 全团队一个浏览器后端,每个 Bot 一份 profile;`refs` / `script` / `task` || [上下文与记忆](2026-10-09-context-and-memory.md) | 对话分段、上下文管家(压缩还是换段)、交接包、`read_own_chat`;记忆的存放、格式、权限、整理和冻结 || [Bot 运行配置](2026-10-10-bot-runtime.md) | Agent 预设、每个 Bot 的模型和思考强度、工作文件夹、能力(MCP、技能、能力包) |+| [AGENTS.md 维护](2026-10-27-agents-md-upkeep.md) | 从项目工作中审阅持久规则;用户批准后才原子写入 || [长消息](2026-10-16-long-messages.md) | state.json 只存开头;超过 2 万字的 Bot 消息转成 DSH 文件;成员和界面怎么读全文 || [桌面应用](2026-10-16-desktop-app.md) | 壳、引擎和界面怎么分;锁定的 DSH 版本、独立的数据位置、首次启动和导入、插件怎么兼容 DSH、安装和更新 || [Bot 之间派活](2026-10-16-bot-handoffs.md) | Bot 看得到对方的状态;没读的消息合成一条;派活树:下面的活交回后才往上交,不许发给在等结果的 Bot;不设总量和链长上限 |diff --git a/ds-bot/client.js b/ds-bot/client.jsindex c2368129..7883c1a0 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -120,8 +120,10 @@ var ZH = {"New skill": "新技能","Awaiting approval": "等待批准",Added: "新增",+ Modified: "已修改","Show all {count} lines": "展开全部 {count} 行","Skill update": "技能更新",+ "AGENTS.md update": "AGENTS.md 更新",Approve: "批准",Decline: "拒绝","Save changes": "保存更改",@@ -18979,6 +18981,9 @@ function fileLines(text) {const body = String(text ?? "").replace(/\n+$/, "");return body === "" ? [] : body.split("\n");}+function editLines(text) {+ return typeof text === "string" ? text.split(/\r?\n/) : [];+}var DIFF_CELLS = 25e4;function lineDiff(before, after) {const a = fileLines(before);@@ -19045,6 +19050,21 @@ function ProposalCard({ proposal, initialError = "", onFailure, onResolved, acti};const update = proposal.action === "update";const rows2 = (0, import_react46.useMemo)(() => lineDiff(update ? proposal.previous?.content : "", proposal.content), [update, proposal.previous?.content, proposal.content]);+ if (proposal.kind === "agents") {+ const blocks = proposal.edits.map((edit) => [+ ...(edit.op === "add" ? [] : editLines(edit.old)).map((line) => ({ kind: "removed", line })),+ ...(edit.op === "remove" ? [] : editLines(edit.text)).map((line) => ({ kind: "added", line }))+ ]);+ return (0, import_react46.createElement)(+ "section",+ { className: "bt-q bt-learning-card", role: "group", "aria-labelledby": titleId, "aria-busy": busy },+ (0, import_react46.createElement)(ProposalHead, { titleId, icon: PencilIcon, title: t("AGENTS.md update") }),+ (0, import_react46.createElement)(FileBox, { name: "AGENTS.md", path: proposal.folder, state: proposal.exists === false ? "Added" : "Modified", blocks }),+ proposal.edits.map((edit, index) => edit.why ? (0, import_react46.createElement)("p", { className: "bt-learning-why", key: index }, edit.why) : null),+ error ? (0, import_react46.createElement)("div", { className: "bt-learning-error", role: "alert" }, error) : null,+ (0, import_react46.createElement)(ProposalFoot, { busy, answer })+ );+ }const oldDetail = update && typeof proposal.previous?.description === "string" && proposal.previous.description !== proposal.description ? proposal.previous.description : void 0;return (0, import_react46.createElement)("section",@@ -19056,16 +19076,16 @@ function ProposalCard({ proposal, initialError = "", onFailure, onResolved, acti(0, import_react46.createElement)(ProposalFoot, { busy, answer }));}-function ProposalHead({ titleId, title, kind, detail, oldDetail }) {+function ProposalHead({ titleId, icon: icon2 = MemoryIcon, title, kind, detail, oldDetail }) {return (0, import_react46.createElement)("div",{ className: "bt-q-head" },- (0, import_react46.createElement)("span", { className: "bt-learning-icon", "aria-hidden": true }, (0, import_react46.createElement)(MemoryIcon)),+ (0, import_react46.createElement)("span", { className: "bt-learning-icon", "aria-hidden": true }, (0, import_react46.createElement)(icon2)),(0, import_react46.createElement)("div",{ className: "bt-q-text" },(0, import_react46.createElement)("p", { className: "bt-q-title", id: titleId }, title),- (0, import_react46.createElement)("div", { className: "bt-q-detail" }, kind),+ kind ? (0, import_react46.createElement)("div", { className: "bt-q-detail" }, kind) : null,oldDetail ? (0, import_react46.createElement)("p", { className: "bt-learning-description bt-learning-old" }, (0, import_react46.createElement)("del", null, oldDetail)) : null,detail ? (0, import_react46.createElement)("p", { className: "bt-learning-description" }, detail) : null),@@ -19117,7 +19137,7 @@ function FileBox({ name, path, state, blocks }) {{ className: `bt-learning-row bt-learning-${row.kind}`, key: `${blockIndex}-${rowIndex}` },(0, import_react46.createElement)("span", { className: "bt-learning-num", "aria-hidden": true }, row.number ?? ""),(0, import_react46.createElement)("span", { className: "bt-learning-sign", "aria-hidden": true }, SIGNS[row.kind]),- (0, import_react46.createElement)("span", { className: "bt-learning-text" }, row.line)+ (0, import_react46.createElement)("span", { className: "bt-learning-text" }, row.line || " ")))])),diff --git a/ds-bot/dev/learning-eval.mjs b/ds-bot/dev/learning-eval.mjsindex ce7776ae..89ed5d90 100644--- a/ds-bot/dev/learning-eval.mjs+++ b/ds-bot/dev/learning-eval.mjs@@ -6,20 +6,21 @@// better with the skill than without, and does an unrelated task stay clean?// Usage (no defaults; nothing is saved but the report):// LEARN_EVAL_BASE_URL=… LEARN_EVAL_MODEL=… LEARN_EVAL_KEY=… \-// node --import ./test/harness-resolve.mjs dev/learning-eval.mjs [--reps 3] [--cases skill-cases.json] [--only id,id] [--out report.json]+// node --import ./test/harness-resolve.mjs dev/learning-eval.mjs [--kind skill|agents] [--reps 3] [--cases case-file.json] [--only id,id] [--out report.json]import { readFileSync, writeFileSync } from 'node:fs'import { parseArgs } from 'node:util'-import { parseJson, skillProblem, skillRequest, skillSystem, LEARNING } from '../src/host/learning.js'+import { agentsProblem, agentsRequest, agentsSystem, applyEdits, parseJson, skillProblem, skillRequest, skillSystem, LEARNING } from '../src/host/learning.js'import { writtenIn } from '../src/host/memory.js'-const { values: args } = parseArgs({ options: { reps: { type: 'string', default: '3' }, only: { type: 'string' }, cases: { type: 'string', default: 'skill-cases.json' }, out: { type: 'string' }, stage: { type: 'string', default: 'decide,effect' } } })+const { values: args } = parseArgs({ options: { kind: { type: 'string', default: 'skill' }, reps: { type: 'string', default: '3' }, only: { type: 'string' }, cases: { type: 'string' }, out: { type: 'string' }, stage: { type: 'string', default: 'decide,effect' } } })const base = process.env.LEARN_EVAL_BASE_URLconst model = process.env.LEARN_EVAL_MODELconst key = process.env.LEARN_EVAL_KEYif (!base || !model || !key) throw new Error('Set LEARN_EVAL_BASE_URL, LEARN_EVAL_MODEL and LEARN_EVAL_KEY.')const reps = Number(args.reps)const stages = new Set(args.stage.split(','))-const cases = JSON.parse(readFileSync(new URL(`./learning-eval/${args.cases}`, import.meta.url), 'utf8')).cases+const caseFile = args.cases ?? (args.kind === 'agents' ? 'agents-cases.json' : 'skill-cases.json')+const cases = JSON.parse(readFileSync(new URL(`./learning-eval/${caseFile}`, import.meta.url), 'utf8')).cases.filter(entry => !args.only || args.only.split(',').includes(entry.id))async function chat(system, user) {@@ -42,8 +43,74 @@ async function chat(system, user) {}const re = source => new RegExp(source, 'i')+const agentRe = source => {+ const inline = source.match(/^\(\?([ims]+)\)/)+ const flags = new Set(['i', 'u', ...(inline?.[1] ?? '')])+ return new RegExp(inline ? source.slice(inline[0].length) : source, [...flags].join(''))+}let tokens = 0+if (args.kind === 'agents') {+ async function decideAgents(entry) {+ const current = entry.agents+ const user = entry.lines.filter(line => line.startsWith('User: ')).map(line => line.slice(6))+ const request = agentsRequest({ folder: '/project', current, lines: entry.lines, declined: [], language: writtenIn(user, 'English') })+ const reply = await chat(agentsSystem(), request)+ tokens += reply.tokens+ const proposal = parseJson(reply.text)+ const problem = agentsProblem(proposal, current)+ const misses = []+ if (entry.expect === 'none') {+ if (proposal?.action !== 'none') misses.push(`action ${proposal?.action ?? 'invalid'}, expected none`)+ } else if (proposal?.action !== 'edit' || problem !== undefined) {+ misses.push(`invalid edit${problem ? ` (${problem})` : ''}`)+ } else {+ const editText = proposal.edits.map(edit => edit.text ?? '').join('\n')+ const result = applyEdits(current, proposal.edits)+ for (const must of entry.must ?? []) if (!agentRe(must).test(editText)) misses.push(`missing /${must}/`)+ for (const bad of entry.mustNot ?? []) {+ if (agentRe(bad).test(editText)) misses.push(`edit has /${bad}/`)+ if (agentRe(bad).test(result)) misses.push(`result has /${bad}/`)+ }+ if (entry.needsOp && !proposal.edits.some(edit => entry.needsOp.includes(edit.op))) misses.push(`needs op ${entry.needsOp.join(' or ')}`)+ }+ return { pass: misses.length === 0, action: problem === undefined ? proposal?.action : 'invalid', misses, proposal: problem ? reply.text : proposal }+ }+ async function effectAgents(entry, edited) {+ const system = file => `You are a coding agent working in this project. Its AGENTS.md:\n\n${file ?? '(no AGENTS.md)'}`+ const rows = []+ for (const [arm, file] of [['before', entry.agents], ['after', edited]]) {+ for (let rep = 0; rep < reps; rep += 1) {+ const reply = await chat(system(file), entry.effect.task)+ tokens += reply.tokens+ const misses = []+ for (const pattern of entry.effect.expect ?? []) if (!agentRe(pattern).test(reply.text)) misses.push(`missing /${pattern}/`)+ for (const pattern of entry.effect.expectNot ?? []) if (agentRe(pattern).test(reply.text)) misses.push(`has /${pattern}/`)+ rows.push({ arm, pass: misses.length === 0, misses, answer: reply.text })+ }+ }+ return rows+ }+ const report = { kind: 'agents', model, reps, cases: [] }+ for (const entry of cases) {+ const result = { id: entry.id, expect: entry.expect, decide: [] }+ if (stages.has('decide')) for (let rep = 0; rep < reps; rep += 1) result.decide.push(await decideAgents(entry))+ const made = result.decide.find(run => run.action === 'edit')?.proposal+ if (stages.has('effect') && entry.effect && made) result.effect = await effectAgents(entry, applyEdits(entry.agents, made.edits))+ report.cases.push(result)+ const passed = result.decide.filter(run => run.pass).length+ const before = result.effect?.filter(run => run.arm === 'before' && run.pass).length ?? 0+ const after = result.effect?.filter(run => run.arm === 'after' && run.pass).length ?? 0+ console.log(`${entry.id.padEnd(22)} decide ${passed}/${result.decide.length}${result.effect ? ` effect before ${before}/${reps} after ${after}/${reps}` : ''}`)+ for (const run of result.decide.filter(item => !item.pass)) console.log(` - ${run.misses.join('; ')}`)+ }+ const all = report.cases.flatMap(entry => entry.decide)+ const effects = report.cases.flatMap(entry => entry.effect ?? [])+ report.tokens = tokens+ console.log(`decide total ${all.filter(run => run.pass).length}/${all.length}; effect ${effects.filter(run => run.pass).length}/${effects.length}; tokens ${tokens}`)+ if (args.out) writeFileSync(args.out, JSON.stringify(report, null, 1))+} else if (args.kind === 'skill') {+async function decide(entry) {const learned = entry.learned ?? []const skills = Object.fromEntries([...learned, ...(entry.library ?? [])].map(skill => [skill.name, { kind: 'skill', content: '', ...skill }]))@@ -112,3 +179,6 @@ const all = report.cases.flatMap(entry => entry.decide)const quiet = report.cases.filter(entry => entry.expect === 'none').flatMap(entry => entry.decide)console.log(`decide total ${all.filter(run => run.pass).length}/${all.length}; restraint ${quiet.filter(run => run.pass).length}/${quiet.length}; tokens ${tokens}`)if (args.out) writeFileSync(args.out, JSON.stringify(report, null, 1))+} else {+ throw new Error(`Unknown evaluation kind ${args.kind}`)+}diff --git a/ds-bot/dev/learning-eval/agents-cases-test.json b/ds-bot/dev/learning-eval/agents-cases-test.jsonnew file mode 100644index 00000000..da60b1e8--- /dev/null+++ b/ds-bot/dev/learning-eval/agents-cases-test.json@@ -0,0 +1,75 @@+{+ "note": "Held-out AGENTS.md review cases: never used while tuning. Run once per prompt version.",+ "cases": [+ {+ "id": "t-chat",+ "expect": "none",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: What does this regex do: ^\\d{3}-\\d{4}$ ?",+ "It matches strings like 555-1234: three digits, a dash, four digits.",+ "User: ok"+ ]+ },+ {+ "id": "t-long-routine",+ "expect": "none",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: Add CSV export to the reports page.",+ "Added an Export button that builds a CSV from the table, with a test for quoting commas. npm test passes.",+ "User: Also include the header row.",+ "Done, test updated.",+ "User: Great."+ ]+ },+ {+ "id": "t-check-cmd",+ "expect": "edit",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: Fix the type error in api.ts.",+ "Fixed the type; npm test passes.",+ "User: npm test does not type-check. Run npm run typecheck too, CI fails on it.",+ "Ran npm run typecheck, fixed two more errors; both pass now.",+ "User: Do both before every commit, every time."+ ],+ "must": [+ "typecheck"+ ],+ "mustNot": [+ "api\\.ts"+ ],+ "effect": {+ "task": "You edited src/util.ts. List, in order, the commands you run before committing.",+ "expect": [+ "typecheck"+ ]+ }+ },+ {+ "id": "t-report-format",+ "expect": "edit",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: Update the dependency versions.",+ "I updated 14 packages. First I looked at each changelog, then ... (a long story of every step)",+ "User: Too long. When you finish, give me three lines: what changed, how you checked it, what is left.",+ "What changed: 14 packages updated. Checked: npm test passes. Left: none.",+ "User: Yes, report like that every time."+ ],+ "must": [+ "(three|3)"+ ],+ "mustNot": [+ "14 packages"+ ],+ "effect": {+ "task": "You just added input validation to the signup form and the tests pass. Write your final report to the user.",+ "expect": [+ "(?is)^(?:(?!\\n\\s*\\n).){0,600}$"+ ]+ }+ }+ ]+}\ No newline at end of filediff --git a/ds-bot/dev/learning-eval/agents-cases.json b/ds-bot/dev/learning-eval/agents-cases.jsonnew file mode 100644index 00000000..2dffbc23--- /dev/null+++ b/ds-bot/dev/learning-eval/agents-cases.json@@ -0,0 +1,114 @@+{+ "note": "AGENTS.md review tuning cases.",+ "cases": [+ {+ "id": "a-routine",+ "expect": "none",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: Add a dark mode toggle to settings.",+ "Added the toggle, stored the choice in localStorage, ran npm test: all pass.",+ "User: Looks good."+ ]+ },+ {+ "id": "a-one-off",+ "expect": "none",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: Fix the crash in parser.js when input is empty.",+ "Found it: `tokens[0]` on an empty array. Added a guard and a test. npm test passes.",+ "User: Thanks, ship it in v2.4.1."+ ]+ },+ {+ "id": "a-already-said",+ "expect": "none",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: Did you run the tests before committing?",+ "Yes, npm test passed before the commit.",+ "User: Good, always do that."+ ]+ },+ {+ "id": "a-lang-repeat",+ "expect": "edit",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: 改一下登录页的按钮颜色。",+ "Changed the button color to the brand blue. Tests pass.",+ "User: 用中文回答我,说过很多次了。",+ "好的。按钮已改为品牌蓝,测试通过。",+ "User: 还有,回答先说结论,别写一大段过程。",+ "结论:已完成。"+ ],+ "must": [+ "中文|Chinese",+ "结论|conclusion"+ ],+ "mustNot": [+ "登录|login|button|按钮"+ ],+ "effect": {+ "task": "The user asks: \"Rename the `getUser` helper to `fetchUser` everywhere.\" Write your reply to the user after doing it (made-up results are fine).",+ "expect": [+ "[\\u4e00-\\u9fff]"+ ]+ }+ },+ {+ "id": "a-push-rule",+ "expect": "edit",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n",+ "lines": [+ "User: Push the fix.",+ "Pushed to main.",+ "User: Never push to main. Open a branch and a PR to dev, I told you last week too.",+ "Sorry. Reverted main, pushed branch fix/header and opened a PR to dev.",+ "User: Right. Every change goes through a PR to dev."+ ],+ "must": [+ "main",+ "dev",+ "(PR|pull request)"+ ],+ "mustNot": [+ "fix/header",+ "last week"+ ],+ "effect": {+ "task": "You finished a small change to README.md in this repo. List the exact git commands you run now to deliver it.",+ "expect": [+ "checkout -b|switch -c|branch",+ "dev"+ ],+ "expectNot": [+ "push\\s+origin\\s+main\\b"+ ]+ }+ },+ {+ "id": "a-stale",+ "expect": "edit",+ "agents": "# Project\n\n## Work rules\n- Run `npm test` before every commit.\n- Do not edit files under `vendor/`.\n- Use yarn, never npm.\n",+ "lines": [+ "User: Install lodash.",+ "Ran yarn add lodash.",+ "User: We moved to pnpm last month, yarn is gone. Use pnpm from now on.",+ "Removed yarn.lock and ran pnpm add lodash; pnpm test passes."+ ],+ "must": [+ "pnpm"+ ],+ "mustNot": [+ "Use yarn, never npm",+ "last month"+ ],+ "needsOp": [+ "replace",+ "remove"+ ]+ }+ ]+}\ No newline at end of filediff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.jsindex 697ed182..8a1390d1 100644--- a/ds-bot/src/client/i18n.js+++ b/ds-bot/src/client/i18n.js@@ -84,8 +84,10 @@ const ZH = {'New skill': '新技能','Awaiting approval': '等待批准',Added: '新增',+ Modified: '已修改','Show all {count} lines': '展开全部 {count} 行','Skill update': '技能更新',+ 'AGENTS.md update': 'AGENTS.md 更新',Approve: '批准',Decline: '拒绝','Save changes': '保存更改',diff --git a/ds-bot/src/client/learning-card-model.js b/ds-bot/src/client/learning-card-model.jsindex 7f99e073..46dc2622 100644--- a/ds-bot/src/client/learning-card-model.js+++ b/ds-bot/src/client/learning-card-model.js@@ -18,6 +18,10 @@ export function fileLines(text) {return body === '' ? [] : body.split('\n')}+export function editLines(text) {+ return typeof text === 'string' ? text.split(/\r?\n/) : []+}+const DIFF_CELLS = 250_000export function lineDiff(before, after) {diff --git a/ds-bot/src/client/learning-card.js b/ds-bot/src/client/learning-card.jsindex bf2b7d81..61d4a526 100644--- a/ds-bot/src/client/learning-card.js+++ b/ds-bot/src/client/learning-card.js@@ -1,9 +1,9 @@import { createElement as h, useId, useMemo, useRef, useState } from 'react'import { useTurnFacts } from './btw.js'-import { lineDiff, proposalPlacement } from './learning-card-model.js'+import { editLines, lineDiff, proposalPlacement } from './learning-card-model.js'import { currentPart } from './text.js'import { t } from './i18n.js'-import { CheckIcon, MemoryIcon } from './icons.js'+import { CheckIcon, MemoryIcon, PencilIcon } from './icons.js'import { spring, SPRING_TAP } from './motion.js'const failedAnswers = new Map()@@ -31,6 +31,18 @@ function ProposalCard({ proposal, initialError = '', onFailure, onResolved, acti}const update = proposal.action === 'update'const rows = useMemo(() => lineDiff(update ? proposal.previous?.content : '', proposal.content), [update, proposal.previous?.content, proposal.content])+ if (proposal.kind === 'agents') {+ const blocks = proposal.edits.map(edit => [+ ...(edit.op === 'add' ? [] : editLines(edit.old)).map(line => ({ kind: 'removed', line })),+ ...(edit.op === 'remove' ? [] : editLines(edit.text)).map(line => ({ kind: 'added', line })),+ ])+ return h('section', { className: 'bt-q bt-learning-card', role: 'group', 'aria-labelledby': titleId, 'aria-busy': busy },+ h(ProposalHead, { titleId, icon: PencilIcon, title: t('AGENTS.md update') }),+ h(FileBox, { name: 'AGENTS.md', path: proposal.folder, state: proposal.exists === false ? 'Added' : 'Modified', blocks }),+ proposal.edits.map((edit, index) => edit.why ? h('p', { className: 'bt-learning-why', key: index }, edit.why) : null),+ error ? h('div', { className: 'bt-learning-error', role: 'alert' }, error) : null,+ h(ProposalFoot, { busy, answer }))+ }const oldDetail = update && typeof proposal.previous?.description === 'string' && proposal.previous.description !== proposal.description ? proposal.previous.description : undefinedreturn h('section', { className: 'bt-q bt-learning-card', role: 'group', 'aria-labelledby': titleId, 'aria-busy': busy },h(ProposalHead, { titleId, title: proposal.name, kind: t(update ? 'Skill update' : 'New skill'), detail: proposal.description, oldDetail }),@@ -40,12 +52,12 @@ function ProposalCard({ proposal, initialError = '', onFailure, onResolved, actih(ProposalFoot, { busy, answer }))}-function ProposalHead({ titleId, title, kind, detail, oldDetail }) {+function ProposalHead({ titleId, icon = MemoryIcon, title, kind, detail, oldDetail }) {return h('div', { className: 'bt-q-head' },- h('span', { className: 'bt-learning-icon', 'aria-hidden': true }, h(MemoryIcon)),+ h('span', { className: 'bt-learning-icon', 'aria-hidden': true }, h(icon)),h('div', { className: 'bt-q-text' },h('p', { className: 'bt-q-title', id: titleId }, title),- h('div', { className: 'bt-q-detail' }, kind),+ kind ? h('div', { className: 'bt-q-detail' }, kind) : null,oldDetail ? h('p', { className: 'bt-learning-description bt-learning-old' }, h('del', null, oldDetail)) : null,detail ? h('p', { className: 'bt-learning-description' }, detail) : null),h('span', { className: 'bt-q-badge bt-learning-status' }, h('span', { className: 'bt-learning-dot', 'aria-hidden': true }), t('Awaiting approval')))@@ -82,7 +94,7 @@ function FileBox({ name, path, state, blocks }) {...rows.map((row, rowIndex) => h('div', { className: `bt-learning-row bt-learning-${row.kind}`, key: `${blockIndex}-${rowIndex}` },h('span', { className: 'bt-learning-num', 'aria-hidden': true }, row.number ?? ''),h('span', { className: 'bt-learning-sign', 'aria-hidden': true }, SIGNS[row.kind]),- h('span', { className: 'bt-learning-text' }, row.line))),+ h('span', { className: 'bt-learning-text' }, row.line || '\u00a0'))),])),folds ? h('div', { className: shut ? 'bt-learning-morebar bt-learning-morebar-shut' : 'bt-learning-morebar' },h('button', { type: 'button', className: 'bt-learning-more', 'aria-expanded': open, onClick: () => setOpen(!open) },diff --git a/ds-bot/src/host/learning.js b/ds-bot/src/host/learning.jsindex a13b1f7d..4d532061 100644--- a/ds-bot/src/host/learning.js+++ b/ds-bot/src/host/learning.js@@ -1,13 +1,12 @@// Learning from work. After a long turn a Bot's chat is marked for review. When the// team has been idle for a while, and at most every few hours, one background pass-// reads what was said since the last pass and asks the Bot's model whether the work-// taught a skill worth keeping. Most work teaches nothing, so most passes propose-// nothing. A proposal never takes effect by itself: it waits as a card in the Bot's-// chat until the user approves it, and a declined one is remembered so the same idea-// does not come back.+// reads what was said since the last pass and asks whether it taught a skill worth+// keeping or a lasting project rule. Most work teaches nothing, so most passes+// propose nothing. A proposal waits as a card in the Bot's chat until approved, and+// declined changes are remembered so the same idea does not come back.import { createHash, randomUUID } from 'node:crypto'import { readFileSync } from 'node:fs'-import { mkdir, writeFile } from 'node:fs/promises'+import { access, mkdir, readFile, unlink, writeFile } from 'node:fs/promises'import { dirname, join } from 'node:path'import { replaceFile } from './files.js'import { looksLikeKey, looksSecret, writtenIn } from './memory.js'@@ -27,6 +26,8 @@ export const LEARNING = {descriptionMax: 300,contentMax: 8_000,declinedMax: 20,+ agentsFileMax: 16_000,+ agentsEditMax: 400,}const clip = (text, max) => (text.length > max ? `${text.slice(0, max)} […]` : text)@@ -94,12 +95,147 @@ export function skillRequest({ bot, lines, learned = [], library = [], declinedreturn parts.join('\n\n')}+export function agentsSystem() {+ return [+ 'You review a stretch of finished work that AI Bots did in one project folder, and decide whether the project\'s AGENTS.md should change.',+ 'Every agent that works in this folder reads AGENTS.md at the start of every task. So each line costs attention on every task: a file that is short and current helps; a long or stale one hurts. Most work should change nothing: answer "none" unless a change passes every test below.',+ '',+ 'What belongs in AGENTS.md:',+ '- How the user wants agents to work here: requirements, rules, and corrections the user stated, especially ones the user had to repeat.',+ '- How the user wants to be talked to: language, length, format, what to report.',+ '- Things to always do and never do in this project, with a short reason when it is not obvious.',+ '- Project facts an agent needs on almost every task and cannot see at once from the code: the command that checks the work, a folder that must not be touched, a step that must come before a commit or a push.',+ '',+ 'Tests for each change:',+ '1. Almost every task: the rule matters to most future work in this folder, not to one kind of task. A procedure for one kind of task belongs in a skill, not here.',+ '2. Earned: the work shows it. The user said it, or repeated it, or corrected an agent, or the same problem came back. Never invent rules the work did not show.',+ '3. Not obvious: a capable agent would not do it anyway, and reading the code or the docs does not show it at once.',+ '4. Lasting: still true next month. Never add task progress, plans, dates, versions, pull request or issue numbers, file lists, pasted errors or logs, or keys, passwords, and tokens.',+ '5. Not already said: if the file already says it, change nothing; if the file says it unclearly or partly, replace that line instead of adding one.',+ '',+ 'Also remove or fix lines the work shows to be wrong or outdated, such as a rule the user reversed, and merge lines that say the same thing. Do not remove a line only because this work did not use it. Keep the user\'s own words and the file\'s language and style; do not rewrite lines that are fine.',+ '',+ 'Answer with one JSON object only, one of:',+ '{"action":"none","reason":"<one sentence>"}',+ '{"action":"edit","edits":[<1 to 3 edits>]}',+ 'Each edit is one of:',+ '{"op":"add","after":"<an exact whole line of the file to add below, or empty for the end>","text":"<the new line or lines>","why":"<one sentence: what in this work shows it>"}',+ '{"op":"replace","old":"<exact text of the file, one or more whole lines>","text":"<the new text>","why":"…"}',+ '{"op":"remove","old":"<exact text of the file, one or more whole lines>","why":"…"}',+ 'Write new lines in the form the file already uses, such as "- " bullets under the right heading.',+ ].join('\n')+}++// The folder, its AGENTS.md, declined edits, and work.+export function agentsRequest({ folder, current, lines, declined = [], language }) {+ const parts = [`Project folder: ${folder}`]+ parts.push(current === undefined ? 'There is no AGENTS.md yet.' : `Current AGENTS.md:\n\`\`\`md\n${current}\n\`\`\``)+ const turnedDown = declined.filter(entry => entry.kind === 'agents' && entry.folder === folder)+ .flatMap(entry => entry.edits ?? [])+ .map(edit => `- ${edit.op}: ${clip(edit.text || edit.old || '', 200)}`)+ if (turnedDown.length > 0) parts.push(`Changes the user turned down before; do not propose them again:\n${turnedDown.join('\n')}`)+ if (language) parts.push(`The user writes in ${language}.`)+ parts.push(`The work, oldest first:\n${lines.join('\n')}`)+ return parts.join('\n\n')+}+// What keeps a proposal from the user, or undefined when it may be shown.// `skills`: the team's skills by name; `learned`: names this Bot learned.// Skills often talk about code such as `password == input`; read keys everywhere, but// password wording only outside code.const holdsSecret = text => looksLikeKey(text) || looksSecret(text.replace(/```[\s\S]*?```/g, ' ').replace(/`[^`\n]*`/g, ' '))+const textOf = value => typeof value === 'string' ? value : ''+const lineBlock = value => value.split(/\r?\n/)+const occurrences = (lines, block) => {+ const found = []+ for (let at = 0; at <= lines.length - block.length; at += 1) {+ if (block.every((line, index) => lines[at + index] === line)) found.push(at)+ }+ return found+}++export function withoutReviewedAgentsRecords(records, reviewedOrders) {+ const reviewed = new Set(reviewedOrders)+ return records.filter(record => !reviewed.has(record.order))+}++export function applyEdits(current, edits) {+ let text = current ?? ''+ let newline = text.includes('\r\n') ? '\r\n' : '\n'+ let trailing = text.endsWith('\n')+ let lines = text === '' ? [] : text.split(/\r?\n/)+ if (trailing) lines.pop()+ for (const edit of edits) {+ if (!['add', 'replace', 'remove'].includes(edit.op)) throw new Error('unknown edit operation')+ if (edit.op === 'add') {+ const added = lineBlock(textOf(edit.text))+ const after = textOf(edit.after)+ if (current === undefined && after !== '') throw new Error('a new AGENTS.md can only add at the end')+ if (!text) {+ lines = added+ trailing = true+ } else if (!after) {+ lines.push(...added)+ trailing = true+ } else {+ const positions = occurrences(lines, [after])+ if (positions.length !== 1) throw new Error('the add anchor must occur once as a whole line')+ const at = positions[0]+ lines.splice(at + 1, 0, ...added)+ }+ } else {+ const old = lineBlock(textOf(edit.old))+ if (current === undefined) throw new Error('a new AGENTS.md can only add lines')+ const positions = occurrences(lines, old)+ if (positions.length !== 1) throw new Error('the old text must occur once as whole lines')+ const at = positions[0]+ const replacement = edit.op === 'replace' ? lineBlock(textOf(edit.text)) : []+ lines.splice(at, old.length, ...replacement)+ }+ text = lines.join(newline) + (trailing && lines.length > 0 ? newline : '')+ newline = text.includes('\r\n') ? '\r\n' : '\n'+ trailing = text.endsWith('\n')+ lines = text === '' ? [] : text.split(/\r?\n/)+ if (trailing) lines.pop()+ }+ return text+}++export function agentsProblem(proposal, current) {+ if (proposal === undefined) return 'the answer is not a JSON object'+ if (proposal.action === 'none') return undefined+ if (proposal.action !== 'edit') return `unknown action ${String(proposal.action)}`+ if (!Array.isArray(proposal.edits) || proposal.edits.length < 1 || proposal.edits.length > 3) return 'an edit needs 1 to 3 changes'+ const initial = current ?? ''+ let result = initial+ for (const edit of proposal.edits) {+ if (!edit || !['add', 'replace', 'remove'].includes(edit.op)) return 'unknown edit operation'+ const text = textOf(edit.text)+ const old = textOf(edit.old)+ const after = textOf(edit.after)+ const why = textOf(edit.why)+ if (why.trim() === '') return 'each edit needs a reason'+ if (edit.op !== 'remove' && text === '') return 'an add or replacement needs text'+ if (text.length > LEARNING.agentsEditMax) return `edit text must have at most ${LEARNING.agentsEditMax} characters`+ if (holdsSecret(text) || holdsSecret(why)) return 'it looks like it holds a secret'+ if (/#\d{2,}|\b\d{4}-\d{2}-\d{2}\b|\bv\d+\.\d+/u.test(text)) return 'the edit contains a task number, date, or version'+ const lines = lineBlock(result)+ if (edit.op === 'add') {+ if (typeof edit.after !== 'string') return 'an add needs an anchor'+ if (current === undefined && after !== '') return 'a new AGENTS.md can only add at the end'+ if (after !== '' && (after.includes('\n') || occurrences(lines, [after]).length !== 1)) return 'the add anchor must be one exact whole line'+ } else {+ if (current === undefined) return 'a new AGENTS.md can only add lines'+ if (old === '' || old.startsWith('\n') || old.endsWith('\n') || occurrences(lines, lineBlock(old)).length !== 1) return 'the old text must occur exactly once as whole lines'+ }+ result = applyEdits(result, [edit])+ }+ if (result.length > Math.max(initial.length + 800, 4_000)) return 'the resulting AGENTS.md is too large'+ if (result === initial) return 'the edits change nothing'+ return undefined+}+export function skillProblem(proposal, { skills, learned, limit = LEARNING.learnedMax }) {if (proposal === undefined) return 'the answer is not a JSON object'const { action } = proposal@@ -142,9 +278,9 @@ export function install(rt) {const root = join(home, 'learning')const path = join(root, 'state.json')- // `pending`: Bot id → the chat Sessions with a long turn since the last pass.- // `cursors`: Session id → the newest line a pass has read. `proposals`: waiting- // for the user, by id. `learned`: skill names learned by each Bot.+ // `pending`: Bot id → chats with a long turn since the last pass. `agentsPending`:+ // folder → queued work waiting for AGENTS.md review. `cursors`: newest line read.+ // `proposals`: waiting for the user, by id. `learned`: skill names learned by Bot.let savedconst data = () => {if (saved !== undefined) return saved@@ -159,6 +295,7 @@ export function install(rt) {saved.proposals ??= {}saved.declined ??= []saved.learned ??= {}+ saved.agentsPending ??= {}return saved}let writing = Promise.resolve()@@ -200,6 +337,8 @@ export function install(rt) {const cursor = data().cursors[sessionId]const kept = []const userLines = []+ const keptRecords = []+ const userRecords = []let used = 0let userUsed = 0let overBudget = false@@ -210,16 +349,18 @@ export function install(rt) {const text = clip(line.text, line.who === 'user' ? settings.userLineMax : settings.botLineMax)if (!overBudget && used + text.length <= settings.textMax) {kept.push(text)+ keptRecords.push({ text, time: line.time })used += text.lengthcontinue}overBudget = trueif (text.startsWith('User: ') && userUsed + text.length <= settings.userTextMax) {userLines.push(text)+ userRecords.push({ text, time: line.time })userUsed += text.length}}- return { lines: [...kept, ...userLines].reverse(), through }+ return { lines: [...kept, ...userLines].reverse(), work: [...keptRecords, ...userRecords].reverse(), through }}const skillsOf = () => Object.fromEntries(Object.entries(state.capabilities?.items ?? {}).filter(([, item]) => item.kind === 'skill'))@@ -235,7 +376,7 @@ export function install(rt) {lines,learned: learned.map(name => skills[name]),library,- declined: data().declined,+ declined: data().declined.filter(entry => entry.kind !== 'agents'),language: writtenIn(lines.filter(line => line.startsWith('User: ')).map(line => line.slice(6)), rt.uiLanguage?.()),limit: settings.learnedMax,})@@ -273,6 +414,78 @@ export function install(rt) {}}+ async function reviewAgents(bot, folder, current, lines) {+ const language = writtenIn(lines.filter(line => line.startsWith('User: ')).map(line => line.slice(6)), rt.uiLanguage?.())+ const request = agentsRequest({ folder, current, lines, declined: data().declined, language })+ const reply = await rt.botShot(bot, agentsSystem(), request, { sessionId: rt.chatOf(bot.id), kind: 'memory' })+ const answer = parseJson(reply)+ if (answer === undefined || answer === null || typeof answer !== 'object') throw new Error('the answer is not a JSON object')+ if (Array.isArray(answer.edits) && answer.edits.some(edit => [edit?.text, edit?.why].some(value => typeof value === 'string' && rt.redactSecrets(value) !== value))) {+ rt.warn('ds-bot: the AGENTS.md review of %s proposed content containing a saved secret', bot.name)+ return undefined+ }+ const problem = agentsProblem(answer, current)+ if (problem !== undefined) {+ if (answer?.action !== 'none') rt.warn('ds-bot: the AGENTS.md review of %s proposed nothing usable: %s', bot.name, problem)+ return undefined+ }+ if (answer.action === 'none') return undefined+ const content = applyEdits(current, answer.edits)+ return {+ id: randomUUID(),+ kind: 'agents',+ botId: bot.id,+ at: Date.now(),+ folder,+ path: join(folder, 'AGENTS.md'),+ base: hashOf(current ?? ''),+ exists: current !== undefined,+ edits: answer.edits.map(edit => ({ ...edit, text: textOf(edit.text), old: textOf(edit.old), why: textOf(edit.why) })),+ content,+ }+ }++ const exists = async path => {+ try { await access(path); return true } catch { return false }+ }+ async function agentsFile(folder) {+ const path = join(folder, 'AGENTS.md')+ try {+ const content = await readFile(path, 'utf8')+ return content.length <= LEARNING.agentsFileMax ? content : undefined+ } catch (error) {+ if (error?.code === 'ENOENT') return undefined+ throw error+ }+ }++ function queueAgentsWork(folder, bot, records) {+ if (records.length === 0) return+ const queue = data().agentsPending[folder] ??= { botId: bot.id, records: [] }+ if (rt.botOf(queue.botId) === undefined) queue.botId = bot.id+ queue.records ??= []+ let order = queue.records.reduce((max, item) => Math.max(max, item.order ?? 0), -1) + 1+ for (const item of records) queue.records.push({ botId: bot.id, text: item.text, time: item.time, order: order++ })+ const max = settings.textMax + settings.userTextMax+ let size = queue.records.reduce((sum, item) => sum + item.text.length, 0)+ const removed = new Set()+ const oldest = [...queue.records].sort((a, b) => (a.time ?? 0) - (b.time ?? 0) || a.order - b.order)+ for (const item of oldest) {+ if (size <= max) break+ if (item.text.startsWith('User: ')) continue+ removed.add(item)+ size -= item.text.length+ }+ for (const item of oldest) {+ if (size <= max) break+ if (!item.text.startsWith('User: ')) continue+ removed.add(item)+ size -= item.text.length+ }+ if (removed.size > 0) queue.records = queue.records.filter(item => !removed.has(item))+ if (queue.records.length === 0) delete data().agentsPending[folder]+ }+// One pass over every Bot with a long turn since the last one. A Bot with a// proposal still waiting is skipped: its chat is read again in the next pass.let running@@ -300,16 +513,81 @@ export function install(rt) {store.proposals[proposal.id] = proposalmade += 1}+ if (config.agentsMd !== false) {+ for (const entry of read) {+ try {+ const agent = await rt.resolveAgent(entry.sessionId)+ const reported = rt.sessionFolder(agent.session)+ if (!reported) continue+ const folder = await rt.real(reported)+ try { await rt.workFolder(folder) } catch { continue }+ if (!await exists(join(folder, '.git')) && !await exists(join(folder, 'AGENTS.md'))) continue+ queueAgentsWork(folder, bot, entry.work ?? [])+ } catch (error) {+ rt.warn('ds-bot: the AGENTS.md folder lookup for %s failed: %s', entry.sessionId, error?.message ?? error)+ }+ }+ }for (const entry of read) if (entry.through !== undefined) store.cursors[entry.sessionId] = entry.throughconst remaining = (store.pending[botId] ?? []).filter(sessionId =>!notedAtStart.has(sessionId) || noted.get(sessionId) !== notedAtStart.get(sessionId))if (remaining.length === 0) delete store.pending[botId]else store.pending[botId] = remaining} catch (error) {- rt.warn('ds-bot: the skill review of %s failed: %s', bot.name, error?.message ?? error)+ rt.warn('ds-bot: the learning review of %s failed: %s', bot.name, error?.message ?? error)continue}}+ if (config.agentsMd !== false) {+ for (const [folder, group] of Object.entries(store.agentsPending)) {+ let bot = rt.botOf(group.botId)+ if (bot === undefined) {+ const contributor = group.records.find(item => rt.botOf(item.botId) !== undefined)+ if (contributor === undefined) {+ delete store.agentsPending[folder]+ continue+ }+ group.botId = contributor.botId+ bot = rt.botOf(group.botId)+ }+ if (Object.values(store.proposals).some(entry => entry.kind === 'agents' && entry.folder === folder)) continue+ try {+ const reviewRecords = [...group.records]+ const reviewedOrders = reviewRecords.map(item => item.order)+ const current = await agentsFile(folder)+ if (current === undefined && await exists(join(folder, 'AGENTS.md'))) continue+ const lines = []+ let used = 0+ let userUsed = 0+ let overBudget = false+ for (const item of reviewRecords.sort((a, b) => (a.time ?? 0) - (b.time ?? 0) || a.order - b.order)) {+ if (!overBudget && used + item.text.length <= settings.textMax) {+ lines.push(item.text)+ used += item.text.length+ continue+ }+ overBudget = true+ if (item.text.startsWith('User: ') && userUsed + item.text.length <= settings.userTextMax) {+ lines.push(item.text)+ userUsed += item.text.length+ }+ }+ if (lines.length === 0) continue+ const proposal = await reviewAgents(bot, folder, current, lines)+ if (proposal !== undefined) {+ store.proposals[proposal.id] = proposal+ made += 1+ }+ const queue = store.agentsPending[folder]+ if (queue) {+ queue.records = withoutReviewedAgentsRecords(queue.records ?? [], reviewedOrders)+ if (queue.records.length === 0) delete store.agentsPending[folder]+ }+ } catch (error) {+ rt.warn('ds-bot: the AGENTS.md review of %s failed: %s', folder, error?.message ?? error)+ }+ }+ }await persist()return made})().finally(() => { running = undefined })@@ -318,7 +596,9 @@ export function install(rt) {const due = () => {const store = data()- if (Object.keys(store.pending).length === 0 || rt.readOnly() !== undefined) return false+ const hasPending = Object.values(store.pending).some(sessions => sessions.length > 0)+ || (config.agentsMd !== false && Object.values(store.agentsPending).some(queue => queue.records?.length > 0))+ if (!hasPending || rt.readOnly() !== undefined) return falseif (Object.keys(rt.workView()).length > 0 || Date.now() - lastActivity < settings.idleMs) return falsereturn store.lastRun === undefined || Date.now() - store.lastRun >= settings.intervalMs}@@ -337,17 +617,61 @@ export function install(rt) {if (proposal === undefined) throw Object.assign(new Error('This proposal is no longer open'), { code: 'bot/stale' })if (approve !== true) {delete store.proposals[id]- store.declined = [...store.declined, { name: proposal.name, description: proposal.description, at: Date.now() }].slice(-settings.declinedMax)+ const decline = proposal.kind === 'agents'+ ? { kind: 'agents', folder: proposal.folder, edits: proposal.edits.map(({ op, text, old }) => ({ op, text, old })), at: Date.now() }+ : { name: proposal.name, description: proposal.description, at: Date.now() }+ store.declined = [...store.declined, decline].slice(-settings.declinedMax)await persist()return { applied: false }}// A failed approval keeps the card, so the user can still decline it.- await applySkill(proposal)+ if (proposal.kind === 'agents') await applyAgents(proposal)+ else await applySkill(proposal)delete store.proposals[id]await persist()return { applied: true }}+ const agentsApplying = new Map()+ async function applyAgents(proposal) {+ const path = proposal.path+ const previous = agentsApplying.get(path) ?? Promise.resolve()+ let release+ const lock = new Promise(resolve => { release = resolve })+ const queued = previous.catch(() => {}).then(() => lock)+ agentsApplying.set(path, queued)+ await previous.catch(() => {})+ try {+ const folder = await rt.real(proposal.folder)+ await rt.workFolder(folder)+ if (join(folder, 'AGENTS.md') !== path) throw new Error('AGENTS.md changed since this was proposed')+ const readCurrent = async () => {+ try { return await readFile(path, 'utf8') } catch (error) {+ if (error?.code !== 'ENOENT') throw error+ return undefined+ }+ }+ const checkCurrent = current => {+ const existsNow = current !== undefined+ if (existsNow !== proposal.exists || hashOf(current ?? '') !== proposal.base) {+ throw new Error('AGENTS.md changed since this was proposed')+ }+ }+ checkCurrent(await readCurrent())+ const temp = `${path}.${process.pid}.${randomUUID()}.tmp`+ try {+ await writeFile(temp, proposal.content, 'utf8')+ checkCurrent(await readCurrent())+ await replaceFile(temp, path)+ } finally {+ await unlink(temp).catch(() => {})+ }+ } finally {+ release()+ if (agentsApplying.get(path) === queued) agentsApplying.delete(path)+ }+ }+async function applySkill(proposal) {const bot = rt.botOf(proposal.botId)if (bot === undefined) throw new Error('The Bot that learned this skill is gone')diff --git a/ds-bot/test/learning-card.test.js b/ds-bot/test/learning-card.test.jsindex c63471d6..72a0b103 100644--- a/ds-bot/test/learning-card.test.js+++ b/ds-bot/test/learning-card.test.js@@ -1,6 +1,6 @@import assert from 'node:assert/strict'import { test } from 'node:test'-import { lineDiff, proposalPlacement } from '../src/client/learning-card-model.js'+import { editLines, lineDiff, proposalPlacement } from '../src/client/learning-card-model.js'const turns = new Map([[1, { turn: 1, end: { time: 100 }, status: 'closed' }],@@ -56,3 +56,9 @@ test('a skill update keeps a long shared head and tail unchanged', () => {assert.deepEqual(rows.slice(3000, 3002), [{ kind: 'removed', line: 'before' }, { kind: 'added', line: 'after', number: 3001 }])assert.deepEqual(rows.at(-1), { kind: 'same', line: 'tail 2999', number: 6001 })})++test('AGENTS.md edit previews keep the blank lines the edit writes', () => {+ assert.deepEqual(editLines('\n'), ['', ''])+ assert.deepEqual(editLines('Rule\n\n'), ['Rule', '', ''])+ assert.deepEqual(editLines(undefined), [])+})diff --git a/ds-bot/test/learning.test.js b/ds-bot/test/learning.test.jsindex 2043ad1b..185a69ff 100644--- a/ds-bot/test/learning.test.js+++ b/ds-bot/test/learning.test.js@@ -1,10 +1,10 @@import assert from 'node:assert/strict'-import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'import { tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'import { createRuntime } from '../index.js'-import { LEARNING, parseJson, skillProblem } from '../src/host/learning.js'+import { agentsProblem, agentsRequest, applyEdits, LEARNING, parseJson, skillProblem, withoutReviewedAgentsRecords } from '../src/host/learning.js'import { fakeContext } from './fake-ctx.js'const TEAM = {@@ -140,6 +140,323 @@ function addNextTurn(history) {)}+function projectFolder(t, { git = true, agents = '# Project\n- Existing rule\n' } = {}) {+ const folder = mkdtempSync(join(tmpdir(), 'ds-bot-project-'))+ if (git) mkdirSync(join(folder, '.git'))+ if (agents !== null) writeFileSync(join(folder, 'AGENTS.md'), agents)+ t.after(() => rmSync(folder, { recursive: true, force: true, maxRetries: 5 }))+ return folder+}++const agentsEdit = JSON.stringify({+ action: 'edit',+ edits: [{ op: 'add', after: '- Existing rule', text: '- Keep the report concise.', why: 'The user corrected the report format more than once.' }],+})++test('agentsProblem validates edit safety and whole-line matches', () => {+ const current = '# Project\n- Existing rule\n'+ assert.equal(agentsProblem({ action: 'none' }, current), undefined)+ const replace = (old, text = '- New rule') => ({ action: 'edit', edits: [{ op: 'replace', old, text, why: 'Repeated correction.' }] })+ assert.match(agentsProblem(replace('- Missing'), current), /exactly once/)+ assert.match(agentsProblem(replace('- Existing rule'), `${current}- Existing rule\n`), /exactly once/)+ assert.match(agentsProblem(replace('Existing rule'), current), /exactly once/)+ assert.match(agentsProblem({ action: 'edit', edits: [{ op: 'add', after: '', text: '- Task #123 happened 2026-10-28 v2.1', why: 'Repeated correction.' }] }, current), /number, date, or version/)+ assert.match(agentsProblem({ action: 'edit', edits: [{ op: 'add', after: '', text: '- Password is hunter2', why: 'Repeated correction.' }] }, current), /secret/)+ assert.match(agentsProblem({ action: 'edit', edits: [{ op: 'add', after: '', text: '- Keep this rule.', why: 'The password is hunter2.' }] }, current), /secret/)+ assert.match(agentsProblem(replace('- Existing rule'), undefined), /only add lines/)+ assert.match(agentsProblem({ action: 'edit', edits: [{ op: 'add', after: '', text: 'x'.repeat(LEARNING.agentsEditMax + 1), why: 'Repeated correction.' }] }, current), /at most/)+ assert.match(agentsProblem({ action: 'edit', edits: [{ op: 'replace', old: '- Existing rule', text: '- Existing rule', why: 'Repeated correction.' }] }, current), /change nothing/)+ const long = `# ${'x'.repeat(3997)}`+ const large = { action: 'edit', edits: [1, 2, 3].map(index => ({ op: 'add', after: '', text: `- ${index}${'y'.repeat(397)}`, why: 'Repeated correction.' })) }+ assert.match(agentsProblem(large, long), /too large/)+})++test('applyEdits supports end and anchored add, replace, remove, and a new file', () => {+ const current = '# Project\n- Existing rule\n'+ assert.equal(applyEdits(current, [{ op: 'add', after: '', text: '- End rule' }]), '# Project\n- Existing rule\n- End rule\n')+ assert.equal(applyEdits(current, [{ op: 'add', after: '# Project', text: '- Before rule' }]), '# Project\n- Before rule\n- Existing rule\n')+ assert.equal(applyEdits(current, [{ op: 'replace', old: '- Existing rule', text: '- Updated rule' }]), '# Project\n- Updated rule\n')+ assert.equal(applyEdits(current, [{ op: 'remove', old: '- Existing rule' }]), '# Project\n')+ assert.equal(applyEdits(undefined, [{ op: 'add', after: '', text: '# Project\n- New rule' }]), '# Project\n- New rule\n')+})++test('AGENTS review keeps records appended while its model is running', () => {+ const reviewed = [{ text: 'User: Earlier work', order: 0 }]+ const queued = [...reviewed, { text: 'User: New work', order: 1 }]+ assert.deepEqual(withoutReviewedAgentsRecords(queued, reviewed.map(item => item.order)), [{ text: 'User: New work', order: 1 }])+})++test('agentsRequest includes only the declined edits for its folder', () => {+ const request = agentsRequest({+ folder: '/project', current: undefined, lines: ['User: Keep reports short.'],+ declined: [{ kind: 'agents', folder: '/project', edits: [{ op: 'add', text: '- Keep reports short.' }] }, { kind: 'agents', folder: '/other', edits: [{ op: 'add', text: '- Other' }] }],+ })+ assert.match(request, /There is no AGENTS.md yet/)+ assert.match(request, /Keep reports short/)+ assert.doesNotMatch(request, /Other/)+})++test('a Git folder with pending work gets one AGENTS proposal and approval writes it atomically', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ const runtime = await startRuntime({ seed, history: { b2: [...ownChat] }, replies: [noneReply, agentsEdit], config: { learningTimes: { tickMs: 100_000 } } })+ t.after(() => runtime.stop())+ runtime.rt.partLines = async function* () {+ for (let seq = 30; seq > 0; seq -= 1) yield { seq, time: seq, who: 'bot', text: `Bot: ${'x'.repeat(1_200)}` }+ yield { seq: 0, time: 0, who: 'user', text: 'User: Keep the early correction after long Bot output.' }+ }+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ const proposal = (await runtime.call('state')).value.learning.b2[0]+ assert.equal(proposal.kind, 'agents')+ assert.equal(proposal.folder, await runtime.rt.real(folder))+ assert.match(JSON.stringify(runtime.requests.at(-1)), /Keep the early correction after long Bot output/)+ const answer = await runtime.call('learning-answer', { id: proposal.id, approve: true })+ assert.equal(answer.value.applied, true)+ assert.equal(readFileSync(join(folder, 'AGENTS.md'), 'utf8'), '# Project\n- Existing rule\n- Keep the report concise.\n')+ assert.deepEqual(readdirSync(folder).filter(name => name.endsWith('.tmp')), [])+})++test('a failed work-folder check keeps an AGENTS proposal open', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ const runtime = await startRuntime({ seed, replies: [noneReply, agentsEdit], config: { learningTimes: { tickMs: 100_000 } } })+ t.after(() => runtime.stop())+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ const proposal = (await runtime.call('state')).value.learning.b2[0]+ const workFolder = runtime.rt.workFolder+ runtime.rt.workFolder = async () => { throw new Error('folder is no longer available') }+ const failed = await runtime.call('learning-answer', { id: proposal.id, approve: true })+ assert.equal(failed.ok, false)+ assert.equal((await runtime.call('state')).value.learning.b2.length, 1)+ runtime.rt.workFolder = workFolder+ assert.equal((await runtime.call('learning-answer', { id: proposal.id, approve: true })).value.applied, true)+})++test('a Git folder without AGENTS.md can get a new file approved', async t => {+ const folder = projectFolder(t, { agents: null })+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ const reply = JSON.stringify({ action: 'edit', edits: [{ op: 'add', after: '', text: '# Project\n- New rule', why: 'The user corrected this repeatedly.' }] })+ const runtime = await startRuntime({ seed, history: { b2: [...ownChat] }, replies: [noneReply, reply], config: { learningTimes: { tickMs: 100_000 } } })+ t.after(() => runtime.stop())+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ const proposal = (await runtime.call('state')).value.learning.b2[0]+ assert.equal(proposal.exists, false)+ assert.equal((await runtime.call('learning-answer', { id: proposal.id, approve: true })).value.applied, true)+ assert.equal(readFileSync(join(folder, 'AGENTS.md'), 'utf8'), '# Project\n- New rule\n')+})++test('a folder is reviewed once with the pending work from every Bot that used it', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ seed.bots.b3 = { id: 'b3', sessionId: 'b3', name: 'Coder', role: 'Coding', model: 'deepseek/v4', instructions: '', capabilities: [], workDir: folder, createdAt: 3 }+ const runtime = await startRuntime({+ seed,+ replies: [noneReply, noneReply, agentsEdit],+ config: { learningTimes: { tickMs: 100_000 } },+ })+ t.after(() => runtime.stop())+ runtime.rt.partLines = async function* (sessionId) {+ yield { seq: 11, time: sessionId === 'b2' ? 1 : 2, who: 'user', text: `User: Work from ${sessionId}` }+ }+ await finishTurn(runtime.ctx, { id: 'b2' })+ await finishTurn(runtime.ctx, { id: 'b3' })+ await runtime.rt.learningRun()+ assert.equal(runtime.requests.length, 3)+ assert.match(JSON.stringify(runtime.requests.at(-1)), /Work from b2/)+ assert.match(JSON.stringify(runtime.requests.at(-1)), /Work from b3/)+ assert.equal((await runtime.call('state')).value.learning.b2.length, 1)+})++test('work for a folder with an open proposal stays queued until the card is declined', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ seed.bots.b3 = { id: 'b3', sessionId: 'b3', name: 'Coder', role: 'Coding', model: 'deepseek/v4', instructions: '', capabilities: [], workDir: folder, createdAt: 3 }+ const runtime = await startRuntime({+ seed,+ replies: [noneReply, agentsEdit, noneReply, agentsEdit],+ config: { learningTimes: { tickMs: 100_000 } },+ })+ t.after(() => runtime.stop())+ runtime.rt.partLines = async function* (sessionId) {+ yield { seq: 11, time: 1, who: 'user', text: `User: Work from ${sessionId}` }+ }+ await finishTurn(runtime.ctx, { id: 'b2' })+ await runtime.rt.learningRun()+ const proposal = (await runtime.call('state')).value.learning.b2[0]++ await finishTurn(runtime.ctx, { id: 'b3' })+ await runtime.rt.learningRun()+ const queued = runtime.learningState().agentsPending[await runtime.rt.real(folder)]+ assert.match(queued.records.map(item => item.text).join('\n'), /Work from b3/)+ assert.equal(runtime.requests.length, 3)+ assert.equal((await runtime.call('state')).value.learning.b2.length, 1)++ await runtime.call('learning-answer', { id: proposal.id, approve: false })+ await runtime.rt.learningRun()+ assert.match(runtime.requests.at(-1).messages[0].content[0].text, /Work from b3/)+ assert.equal(runtime.learningState().agentsPending[await runtime.rt.real(folder)], undefined)+})++test('a failed AGENTS model call keeps its folder work for the next pass', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ const runtime = await startRuntime({+ seed,+ replies: [createReply, new Error('AGENTS model failed'), agentsEdit],+ config: { learningTimes: { tickMs: 100_000 } },+ })+ t.after(() => runtime.stop())+ runtime.rt.partLines = async function* () {+ yield { seq: 11, time: 1, who: 'user', text: 'User: Keep the same folder work' }+ }+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ const folderKey = await runtime.rt.real(folder)+ assert.match(runtime.learningState().agentsPending[folderKey].records[0].text, /same folder work/)+ assert.equal((await runtime.call('state')).value.learning.b2[0].kind, 'skill')+ const firstRequest = runtime.requests.at(-1).messages[0].content[0].text++ await runtime.rt.learningRun()+ assert.match(runtime.requests.at(-1).messages[0].content[0].text, /same folder work/)+ assert.equal(runtime.requests.at(-1).messages[0].content[0].text, firstRequest)+ assert.equal(runtime.learningState().agentsPending[folderKey], undefined)+ assert.ok((await runtime.call('state')).value.learning.b2.some(proposal => proposal.kind === 'agents'))+})++test('AGENTS review falls back to another contributor when its Bot is deleted', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ seed.bots.b3 = { id: 'b3', sessionId: 'b3', name: 'Coder', role: 'Coding', model: 'deepseek/v4', instructions: '', capabilities: [], workDir: folder, createdAt: 3 }+ const runtime = await startRuntime({+ seed,+ replies: [noneReply, new Error('retry this folder later'), noneReply, new Error('retry after Bot C adds work'), agentsEdit],+ config: { learningTimes: { tickMs: 100_000 } },+ })+ t.after(() => runtime.stop())+ const folderPath = await runtime.rt.real(folder)+ runtime.rt.partLines = async function* (sessionId) {+ yield { seq: 11, time: 1, who: 'user', text: `User: Work from ${sessionId}` }+ }+ await finishTurn(runtime.ctx, { id: 'b2' })+ await runtime.rt.learningRun()+ assert.equal(runtime.learningState().agentsPending[folderPath].botId, 'b2')+ assert.deepEqual(runtime.learningState().agentsPending[folderPath].records.map(item => item.botId), ['b2'])++ await finishTurn(runtime.ctx, { id: 'b3' })+ await runtime.rt.learningRun()+ assert.equal(runtime.learningState().agentsPending[folderPath].botId, 'b2')+ assert.deepEqual(runtime.learningState().agentsPending[folderPath].records.map(item => item.botId), ['b2', 'b3'])++ await runtime.call('delete-bot', { id: 'b2' })+ await runtime.rt.learningRun()+ assert.equal(runtime.learningState().agentsPending[folderPath], undefined)+ const view = (await runtime.call('state')).value.learning+ assert.ok(view.b3.some(proposal => proposal.kind === 'agents'))+ assert.match(runtime.requests.at(-1).messages[0].content[0].text, /Work from b2/)+ assert.match(runtime.requests.at(-1).messages[0].content[0].text, /Work from b3/)+})++test('AGENTS folder queue is removed when all contributing Bots are deleted', async t => {+ const folder = projectFolder(t)+ const folderPath = realpathSync(folder)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b3 = { id: 'b3', sessionId: 'b3', name: 'Coder', role: 'Coding', model: 'deepseek/v4', instructions: '', capabilities: [], workDir: folder, createdAt: 3 }+ const runtime = await startRuntime({+ seed,+ learningSeed: {+ agentsPending: {+ [folderPath]: {+ botId: 'b2',+ records: [+ { botId: 'b2', text: 'User: Work from b2', time: 1, order: 0 },+ { botId: 'b3', text: 'User: Work from b3', time: 2, order: 1 },+ ],+ },+ },+ },+ config: { learningTimes: { tickMs: 100_000 } },+ })+ t.after(() => runtime.stop())+ await runtime.call('delete-bot', { id: 'b2' })+ await runtime.call('delete-bot', { id: 'b3' })+ await runtime.rt.learningRun()+ assert.equal(runtime.learningState().agentsPending[folderPath], undefined)+ assert.equal(runtime.requests.length, 0)+})++test('agentsMd false keeps folder queues and does not schedule a review', async t => {+ const folder = projectFolder(t)+ const folderPath = realpathSync(folder)+ const runtime = await startRuntime({+ learningSeed: {+ agentsPending: {+ [folderPath]: { botId: 'b2', records: [{ text: 'User: Keep this queued', time: 1, order: 0 }] },+ },+ },+ config: { agentsMd: false, learningTimes: { idleMs: 0, intervalMs: 0, tickMs: 10 } },+ })+ t.after(() => runtime.stop())+ await new Promise(resolve => setTimeout(resolve, 60))+ assert.equal(runtime.learningState().lastRun, undefined)+ assert.deepEqual(runtime.learningState().agentsPending[folderPath].records, [{ text: 'User: Keep this queued', time: 1, order: 0 }])+ assert.equal(runtime.requests.length, 0)+})++test('a changed AGENTS proposal stays open, can be declined, and is included in the next folder request', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ const history = { b2: [...ownChat] }+ const runtime = await startRuntime({ seed, history, replies: [noneReply, agentsEdit, noneReply, noneReply], config: { learningTimes: { tickMs: 100_000 } } })+ t.after(() => runtime.stop())+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ const proposal = (await runtime.call('state')).value.learning.b2[0]+ writeFileSync(join(folder, 'AGENTS.md'), '# Project\n- Changed by the user\n')+ const stale = await runtime.call('learning-answer', { id: proposal.id, approve: true })+ assert.match(JSON.stringify(stale), /AGENTS.md changed since this was proposed/)+ assert.equal((await runtime.call('state')).value.learning.b2.length, 1)+ assert.equal((await runtime.call('learning-answer', { id: proposal.id, approve: false })).value.applied, false)+ addNextTurn(history)+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ assert.match(JSON.stringify(runtime.requests.at(-1)), /Changes the user turned down before/)+ assert.match(JSON.stringify(runtime.requests.at(-1)), /Keep the report concise/)+})++test('folders without a project marker, DSH data folders, and agentsMd:false are not reviewed', async t => {+ const cases = [+ { folder: projectFolder(t, { git: false, agents: null }), config: {} },+ { folder: projectFolder(t, { agents: `# ${'x'.repeat(16_000)}` }), config: {} },+ { folder: undefined, config: {} },+ { folder: projectFolder(t), config: { agentsMd: false } },+ ]+ for (const item of cases) {+ const seed = JSON.parse(JSON.stringify(TEAM))+ const options = { replies: [noneReply], config: { learningTimes: { tickMs: 100_000 }, ...item.config } }+ const runtimeOptions = item.folder+ ? { ...options, seed: { ...seed, bots: { ...seed.bots, b2: { ...seed.bots.b2, workDir: item.folder } } } }+ : options+ const runtime = await startRuntime(runtimeOptions)+ t.after(() => runtime.stop())+ if (!item.folder) runtime.rt.botOf('b2').workDir = runtime.home+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ assert.equal(runtime.requests.length, 1)+ assert.deepEqual((await runtime.call('state')).value.learning ?? {}, {})+ }+})+test('parseJson accepts fenced and unfenced objects and ignores garbage', () => {assert.deepEqual(parseJson('```json\n{"action":"none"}\n```'), { action: 'none' })assert.deepEqual(parseJson('Answer: {"action":"none"}'), { action: 'none' })@@ -196,6 +513,26 @@ test('a none reply makes no proposal and advances the chat cursor', async t => {assert.deepEqual((await runtime.call('state')).value.learning ?? {}, {})})+test('a folder lookup failure does not repeat the skill proposal', async t => {+ const runtime = await startRuntime({ config: { learningTimes: { tickMs: 100_000 } } })+ t.after(() => runtime.stop())+ await finishTurn(runtime.ctx)+ await until(() => runtime.learningState().pending?.b2?.includes('b2'))+ runtime.rt.partLines = async function* () {+ yield { seq: 10, time: 1, who: 'user', text: 'User: Please review this work' }+ yield { seq: 11, time: 2, who: 'assistant', text: 'Bot: I finished the work.' }+ }+ runtime.rt.botShot = async () => createReply+ runtime.rt.resolveAgent = async () => { throw new Error('folder lookup failed') }++ await runtime.rt.learningRun()++ assert.equal(Object.keys(runtime.learningState().proposals ?? {}).length, 1)+ assert.equal(runtime.learningState().pending?.b2, undefined)+ await runtime.rt.learningRun()+ assert.equal(Object.keys(runtime.learningState().proposals ?? {}).length, 1)+})+test('long turns ending during review remain pending for the next pass', async t => {const history = { b2: [...ownChat], b3: [...ownChat] }const runtime = await startRuntime({@@ -516,6 +853,24 @@ test('a saved secret in a proposed skill is not shown as a proposal', async t =>assert.deepEqual(runtime.learningState().proposals, {})})+test('an AGENTS edit containing a saved secret is not shown as a proposal', async t => {+ const folder = projectFolder(t)+ const seed = JSON.parse(JSON.stringify(TEAM))+ seed.bots.b2.workDir = folder+ const secretValue = 'agents-saved-secret-42'+ const reply = JSON.stringify({+ action: 'edit',+ edits: [{ op: 'add', after: '- Existing rule', text: `- Use ${secretValue}.`, why: 'The user corrected this repeatedly.' }],+ })+ const runtime = await startRuntime({ seed, replies: [noneReply, reply], config: { learningTimes: { tickMs: 100_000 } } })+ t.after(() => runtime.stop())+ await runtime.call('secret-set', { name: 'AGENTS_TEST', value: secretValue })+ await finishTurn(runtime.ctx)+ await runtime.rt.learningRun()+ assert.equal(Object.values(runtime.learningState().proposals ?? {}).length, 0)+ assert.equal(Object.keys(runtime.learningState().agentsPending ?? {}).length, 0)+})+test('user corrections remain in long chat reviews after the text budget', async t => {const runtime = await startRuntime({ replies: [noneReply], config: { learningTimes: { tickMs: 100_000 } } })t.after(() => runtime.stop())