diff --git a/packages/aelios/README.md b/packages/aelios/README.md index 4e46d36..641d94b 100644 --- a/packages/aelios/README.md +++ b/packages/aelios/README.md @@ -196,6 +196,12 @@ the completed reply's retained text or tool arguments still skips recording. The reply delivered to the client remains unchanged. Each record is sent once and never retried. +DS Bot's background memory reviews use the Bot's model but are not chat turns. +When the current user item matches the complete DS Bot memory-review template, +the plugin skips both recall and recording for that request and its reply. +Normal conversation, tool continuations and messages discussing or quoting the +review remain eligible. This filter does not remove previously recorded reviews. + ## Things to know - **Don't double up.** If an agent already uses an Aelios gateway as its diff --git a/packages/aelios/aelios.hooks.mjs b/packages/aelios/aelios.hooks.mjs index 26e94bd..38abaaa 100644 --- a/packages/aelios/aelios.hooks.mjs +++ b/packages/aelios/aelios.hooks.mjs @@ -12,9 +12,9 @@ // the request (slim.mjs) and puts the memory it gets back after the last // user message. Anything that goes wrong leaves the request as it came. import { createHash } from "node:crypto" -import { aeliosProtocol, appendPatch, hasSecret, humanTurn, lastUserText, serverState, slim, slimReply, spoken } from "./slim.mjs" +import { aeliosProtocol, appendPatch, hasSecret, humanTurn, isMemoryReview, lastUserText, serverState, slim, slimReply, spoken } from "./slim.mjs" -const version = "0.1.0" +const version = "0.1.1" const maxResponseBytes = 256 << 10 const maxRecordBytes = 2 << 20 const recordTimeoutMs = 10_000 @@ -80,7 +80,8 @@ async function recall(body, ctx, o, report) { return } const slimmed = slim(body, protocol) - if (hasSecret(lastUserText(slimmed, protocol))) { + const userText = lastUserText(slimmed, protocol) + if (isMemoryReview(userText) || hasSecret(userText)) { if (ctx.state) delete ctx.state.aelios report("skipped") return diff --git a/packages/aelios/aelios.test.mjs b/packages/aelios/aelios.test.mjs index 9cfef73..7a16954 100644 --- a/packages/aelios/aelios.test.mjs +++ b/packages/aelios/aelios.test.mjs @@ -215,6 +215,57 @@ for (const record of [false, true]) { }) } +const memoryReview = (group = false) => [ + group ? 'The group chat: "Team", with the user and Writer (admin).' : 'The Bot: Writer, Assistant.', + '', + 'MEMORY.md has 12 of 4000 characters (own) and 0 of 4000 (team).', + '', + group ? 'Its memory now (group: shared by the members of this group; team: shared by every Bot):' : "Its memory now (own: this Bot's; team: shared by every Bot):", + '[1] (own) A synthetic fact.', + '', + 'New in this chat, oldest first:', + 'User: hello', + 'Assistant: hi', + '', + 'Write every new or changed entry in English.', +].join('\n') + +for (const protocol of ['anthropic', 'chat', 'responses']) { + for (const group of [false, true]) { + test(`${protocol}: DS Bot ${group ? 'group' : 'own'} memory review exports neither prompt nor reply`, async () => { + mock() + const [c, reports] = ctx(protocol, { options: { record: true } }) + c.state.aelios = { admitted: true, payload: { identity: 'default' } } + const item = { role: 'user', content: [{ type: protocol === 'responses' ? 'input_text' : 'text', text: memoryReview(group) }] } + const req = protocol === 'responses' ? { input: [item] } : { messages: [item] } + const original = structuredClone(req) + expect(await onRequest(req, c)).toBeUndefined() + expect(req).toEqual(original) + expect(c.state.aelios).toBeUndefined() + onResponse({ choices: [{ message: { content: '{"changes":[]}' } }] }, { ...c, success: true }) + await flush() + expect(calls).toHaveLength(0) + expect(reports).toEqual([['skipped']]) + }) + } +} + +for (const text of [ + `The Bot: Writer, Assistant.\n\nI want to change my bot's memory.`, + `I found Aelios storing this:\n${memoryReview()}`, + `\`\`\`\n${memoryReview()}\n\`\`\``, +]) { + test(`a person discussing a memory review is still recalled and recorded: ${text.slice(0, 40)}`, async () => { + mock() + const [c] = ctx('chat', { options: { record: true } }) + await onRequest({ messages: [{ role: 'user', content: text }] }, c) + onResponse({ choices: [{ message: { content: 'Let us fix it.' } }] }, { ...c, success: true }) + await flush() + expect(of('/v1/hook/recall')).toHaveLength(1) + expect(of('/v1/hook/record')).toHaveLength(1) + }) +} + test("transport-wrapped human speech reaches recall and receives memory", async () => { mock() const [c] = ctx("chat") diff --git a/packages/aelios/package.json b/packages/aelios/package.json index a62d30b..f3b2a20 100644 --- a/packages/aelios/package.json +++ b/packages/aelios/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/gateway-aelios", - "version": "0.1.0", + "version": "0.1.1", "description": "Aelios long-term memory for every agent behind magpie: recall before each turn, optional recording after it", "type": "module", "files": [ diff --git a/packages/aelios/slim.mjs b/packages/aelios/slim.mjs index ca64d41..7542665 100644 --- a/packages/aelios/slim.mjs +++ b/packages/aelios/slim.mjs @@ -160,6 +160,17 @@ export function lastUserText(body, protocol) { return visible(last.content) } +// DS Bot sends background memory reviews through the Bot's own model. They +// have a user role, but neither this prompt nor its JSON reply is conversation. +// Require the whole template so a person quoting it to report a bug stays human. +export function isMemoryReview(text) { + const normalized = text.replace(/\r\n/g, "\n").trim() + return /^(?:The Bot: [^\n]+\.|The group chat: [^\n]+\.)\n\n/.test(normalized) && + /\n\nIts memory now \((?:own: this Bot's|group: shared by the members of this group); team: shared by every Bot\):\n/.test(normalized) && + /\n\nNew in [^\n]+, oldest first(?: \(newer messages come in the next review\))?:\n/.test(normalized) && + /\n\nWrite every new or changed entry in [^\n]+\.$/.test(normalized) +} + function visible(c) { if (typeof c === "string") return c if (!Array.isArray(c)) return ""