Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Guard learned skill deletion and retries

Fei2411b23b0249410e
diff --git a/ds-bot/src/host/learning.js b/ds-bot/src/host/learning.js
index c8123a87..b5b433a5 100644
--- a/ds-bot/src/host/learning.js
+++ b/ds-bot/src/host/learning.js
@@ -346,12 +346,18 @@ export function install(rt) {
if (bot === undefined) throw new Error('The Bot that learned this skill is gone')
const current = skillsOf()[proposal.name]
const store = data()
- if (proposal.action === 'create' && !(proposal.saved && current !== undefined)) {
- if (current !== undefined) throw new Error(`A skill named ${proposal.name} exists now`)
- const item = await rt.capabilitySave({ name: proposal.name, kind: 'skill', description: proposal.description, content: proposal.content }, { botId: bot.id, ui: false })
- proposal.saved = true
- store.learned[bot.id] = [...new Set([...(store.learned[bot.id] ?? []), item.name])]
- await persist()
+ if (proposal.action === 'create') {
+ if (proposal.saved) {
+ if (current === undefined || current.description !== proposal.description || current.content !== proposal.content) {
+ throw new Error(`${proposal.name} changed since this was proposed`)
+ }
+ } else {
+ if (current !== undefined) throw new Error(`A skill named ${proposal.name} exists now`)
+ const item = await rt.capabilitySave({ name: proposal.name, kind: 'skill', description: proposal.description, content: proposal.content }, { botId: bot.id, ui: false })
+ proposal.saved = true
+ store.learned[bot.id] = [...new Set([...(store.learned[bot.id] ?? []), item.name])]
+ await persist()
+ }
} else if (proposal.action === 'update') {
if (current === undefined || hashOf(`${current.description}\n${current.content}`) !== proposal.base) throw new Error(`${proposal.name} changed since this was proposed`)
const item = await rt.capabilitySave({ name: proposal.name, kind: 'skill', description: proposal.description, content: proposal.content }, { botId: bot.id, ui: false })
@@ -366,6 +372,15 @@ export function install(rt) {
throw new Error(`Unknown endpoint ${endpoint}`)
}
+ async function removeLearned(name) {
+ const learned = data().learned
+ const deleted = String(name ?? '')
+ for (const [botId, names] of Object.entries(learned)) {
+ learned[botId] = (Array.isArray(names) ? names : []).filter(entry => entry !== deleted)
+ }
+ await persist()
+ }
+
// Per Bot, its proposals waiting for the user.
const learningView = () => {
const byBot = {}
@@ -373,5 +388,18 @@ export function install(rt) {
return byBot
}
+ const capabilityDelete = rt.capabilityDelete
+ rt.capabilityDelete = async (...args) => {
+ const result = await capabilityDelete(...args)
+ await removeLearned(args[0])
+ return result
+ }
+ const capabilityEndpoint = rt.capabilityEndpoint
+ rt.capabilityEndpoint = async (endpoint, body, ...args) => {
+ const result = await capabilityEndpoint(endpoint, body, ...args)
+ if (endpoint === 'capability-delete') await removeLearned(body?.name)
+ return result
+ }
+
Object.assign(rt, { learningView, learningEndpoint, learningRun: run })
}
diff --git a/ds-bot/test/learning.test.js b/ds-bot/test/learning.test.js
index 2326c088..5e042524 100644
--- a/ds-bot/test/learning.test.js
+++ b/ds-bot/test/learning.test.js
@@ -268,6 +268,79 @@ test('a failed create attachment can be retried without resaving or losing the p
assert.ok(runtime.teamState().bots.b2.capabilities.includes('release-checks'))
})
+test('a failed create retry refuses a skill edited after it was saved', async t => {
+ const runtime = await startRuntime({ replies: [createReply] })
+ t.after(() => runtime.stop())
+ await finishTurn(runtime.ctx)
+ await until(() => Object.keys(runtime.learningState().proposals ?? {}).length === 1)
+ const proposal = Object.values(runtime.learningState().proposals)[0]
+ const endpoint = runtime.rt.capabilityEndpoint
+ let fail = true
+ runtime.rt.capabilityEndpoint = async (name, body, ...args) => {
+ if (name === 'bot-capability' && fail) {
+ fail = false
+ throw new Error('temporary attachment failure')
+ }
+ return endpoint(name, body, ...args)
+ }
+
+ const first = await runtime.call('learning-answer', { id: proposal.id, approve: true })
+ assert.equal(first.ok, false)
+ assert.equal(runtime.learningState().proposals[proposal.id].saved, true)
+ const edit = await runtime.call('capability-save', {
+ name: 'release-checks',
+ kind: 'skill',
+ description: 'Use when checking a release.',
+ content: 'Run the revised release checks.',
+ })
+ assert.equal(edit.ok, true)
+
+ const retry = await runtime.call('learning-answer', { id: proposal.id, approve: true })
+ assert.equal(retry.ok, false)
+ assert.match(JSON.stringify(retry), /release-checks changed since this was proposed/)
+ assert.ok(runtime.learningState().proposals[proposal.id])
+ assert.equal(runtime.teamState().capabilities.items['release-checks'].content, 'Run the revised release checks.')
+ assert.equal(runtime.teamState().bots.b2.capabilities.includes('release-checks'), false)
+})
+
+test('deleting a learned skill clears ownership before a same-name skill is recreated', async t => {
+ const history = { b2: [...ownChat] }
+ const runtime = await startRuntime({
+ history,
+ replies: [createReply, noneReply],
+ config: { learningTimes: { tickMs: 100_000 } },
+ })
+ t.after(() => runtime.stop())
+ await finishTurn(runtime.ctx)
+ await runtime.rt.learningRun()
+ await until(() => Object.keys(runtime.learningState().proposals ?? {}).length === 1)
+ const proposal = Object.values(runtime.learningState().proposals)[0]
+ const answer = await runtime.call('learning-answer', { id: proposal.id, approve: true })
+ assert.equal(answer.value.applied, true)
+ assert.deepEqual(runtime.learningState().learned.b2, ['release-checks'])
+
+ const deleted = await runtime.call('capability-delete', { name: 'release-checks' })
+ assert.equal(deleted.ok, true)
+ assert.deepEqual(runtime.learningState().learned.b2, [])
+ await runtime.call('capability-save', {
+ name: 'release-checks',
+ kind: 'skill',
+ description: 'Use when checking a release.',
+ content: 'Run the release checks.',
+ })
+ await runtime.call('bot-capability', { botId: 'b2', name: 'release-checks', attached: true })
+ assert.ok(runtime.teamState().bots.b2.capabilities.includes('release-checks'))
+
+ addNextTurn(history)
+ await finishTurn(runtime.ctx)
+ await runtime.rt.learningRun()
+ assert.equal(runtime.requests.length, 2)
+ const request = runtime.requests[1].messages[0].content[0].text
+ assert.match(request, /This Bot has no learned skills yet\./)
+ assert.match(request, /Other skills on the team:\n- release-checks:/)
+ assert.deepEqual(runtime.learningState().learned.b2, [])
+})
+
test('declined proposals are recorded and included in the next skill request', async t => {
const history = { b2: [...ownChat] }
const runtime = await startRuntime({ history, replies: [createReply, noneReply] })