Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/450.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A tree merge now leaves the Bot that keeps the role a traceable handoff line instead of nothing: after the duplicate is archived, the kept Bot's prompt reads `Inherited from <name> (source <id>, merged <time>): role "…"; N child Bots moved to you; its chat and the originals stay on the archived Bot, and <copy count> memory entries were copied into yours` (no / 1 / up to 20, the cap it actually copies), and a host that allows the tree in `flybot.memoryPlugins` records the same note in the kept Bot's own memory through the new `flybot.handoff.note` seam (metadata `source: handoff`, `from`, `at`). The archived Bot's chat and memory are only read, never rewritten, while up to 20 of its entries are copied into the kept Bot (`source: merged`); undo removes the prompt line, while the memory note stays as the record of the merge ([#3308](https://github.com/ThinkFlowLab/FlyBot/issues/3308), part of [#450](https://github.com/ThinkFlowLab/FlyBot/issues/450)).
57 changes: 57 additions & 0 deletions core/src/host/handoff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// The merge handoff (#450 item 1): one line from the Bot that is archived on a merge into the Bot
// that keeps its role. It has the shape of a part handoff note (parts.js): a note plus a pointer to
// where the rest stays — here the archived Bot's own chat and memory, which are never moved or
// rewritten. The note lands in the kept Bot's own memory through the ordinary memory path
// (secret-scanned, size-capped, journaled), so it outlives a prompt rewrite, and the call answers
// the source reference (Bot id, name, time) a plugin puts in its prompt line.
//
// - OFF unless the host names the plugin: the same `config.flybot.memoryPlugins` allow-list as
// `flybot.memory`, because the note is a memory write. An unnamed plugin is refused.
// - Typed and bounded. A call names two live Bots and one non-empty string note; anything else is
// refused with a short machine `code`, and a refused call writes nothing.
// - The source Bot is only read: no entry of it is changed, so an archived Bot keeps its own
// memory exactly as it was.
// The entry text is the note plus a prefix naming the source, kept under the memory entry cap
// (memory.js MEMORY_LIMITS.entry, 300): a 40-character name, a 24-character id and 21 characters of
// wording leave 200 for the note, so a note at the cap still fits. A longer name or id is clipped,
// never the note.
export const HANDOFF = Object.freeze({ note: 200, name: 40, id: 24 })

const clip = (value, max) => [...String(value ?? '')].slice(0, max).join('')
const iso = at => new Date(at).toISOString()
const fail = code => ({ ok: false, code })
const text = value => (typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : '')

/**
* Builds the `note(input)` the host exposes as `flybot.handoff.note` for one plugin.
* `allow` is the host's memory allow-list; a plugin not in it may not leave a note.
*/
export function createHandoff(rt, { allow = [], now = Date.now } = {}) {
const allowed = allow instanceof Set ? allow : new Set(Array.isArray(allow) ? allow : [])
return async function handoffNote(plugin, input) {
if (!allowed.has(plugin)) return fail('not-allowed')
if (rt.readOnly?.() !== undefined) return fail('read-only')
if (input === null || typeof input !== 'object' || Array.isArray(input)) return fail('bad-input')
const from = text(input.from)
if (from === '') return fail('no-source')
const to = text(input.to)
if (to === '') return fail('no-target')
if (from === to) return fail('same-bot')
if (typeof input.note !== 'string') return fail('bad-note')
const note = text(input.note)
if (note === '') return fail('empty-note')
if ([...note].length > HANDOFF.note) return fail('note-too-long')
const source = rt.botOf?.(from)
if (source === undefined) return fail('source-gone')
if (rt.botOf?.(to) === undefined) return fail('target-gone')
if (typeof rt.botMemoryAppend !== 'function') return fail('no-api')
const at = now()
const line = `${clip(source.name ?? 'a Bot', HANDOFF.name)} (${clip(from, HANDOFF.id)}) was merged into you: ${note}`
try {
await rt.botMemoryAppend(to, line, { source: 'handoff', from: clip(from, HANDOFF.id), at: iso(at) })
} catch (error) {
return fail(String(error?.code ?? 'failed').slice(0, 40))
}
return { ok: true, ref: { bot: from, name: source.name ?? '', at: iso(at) } }
}
}
7 changes: 7 additions & 0 deletions core/src/host/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { SOURCE_KIND } from './constants.js'
import { currentParticipants, isFaceRoom } from './room-membership.js'
import { faceMentions, mentionedByHandle } from './face-mentions.js'
import { FEED_EVENTS, installFeed } from './flybot-feed.js'
import { createHandoff } from './handoff.js'
import { createPluginModel } from './plugin-model.js'
import { defineTool } from './tool-schema.js'
import { retiredBot } from './store.js'
Expand Down Expand Up @@ -114,6 +115,8 @@ export function install(rt) {
const bypassGrants = config?.flybot?.bypass ?? {}
// Plugins the host allows to read and write a Bot's own memory (#450 item 1). Empty by default.
const memoryPlugins = new Set(config?.flybot?.memoryPlugins ?? [])
// The merge handoff note (#450 item 1) is a memory write, so it takes the same allow-list.
const handoffNote = createHandoff(rt, { allow: memoryPlugins })
// A sanctioned, capped model call for a named plugin (#450). Empty allow-list by default.
// A per-plugin default model (#579): a call from a plugin named here uses it unless it names one.
const modelAsk = createPluginModel(rt, { allow: config?.flybot?.modelPlugins ?? [], models: config?.flybot?.modelFor ?? {} })
Expand Down Expand Up @@ -1264,6 +1267,10 @@ export function install(rt) {
.catch(error => ({ ok: false, code: String(error?.code ?? 'failed').slice(0, 40) }))
},
},
// The merge handoff note (#450 item 1): one line for the Bot that keeps a merged Bot's role.
// The note is written into THAT Bot's memory (never the archived one) and the source reference
// comes back for the plugin's prompt line. Same allow-list as memory; never throws.
handoff: { note: input => handoffNote(plugin, input) },
// A sanctioned, capped model call for THIS plugin (#450): allow-listed by config, text only,
// no side effect. Resolves `{ ok: true, text }` or `{ ok: false, code }`.
model: { ask: input => modelAsk(plugin, input) },
Expand Down
15 changes: 15 additions & 0 deletions core/test/flybot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,21 @@ test('#450: flybot.memory reads and writes a Bot only for an allow-listed plugin
assert.ok(listed.memory.read('a').some(entry => entry.text === 'The user prefers tea'))
})

test('#450: flybot.handoff leaves a merge note in the kept Bot, gated like memory', async (t) => {
const { fb, stop } = await start({ flybot: { memoryPlugins: ['probe'] } })
t.after(stop)
assert.deepEqual(await fb.scope('stranger').handoff.note({ from: 'a', to: 'b', note: 'x' }), { ok: false, code: 'not-allowed' })
const probe = fb.scope('probe')
assert.deepEqual(await probe.handoff.note({ from: 'a', to: 'b', note: 7 }), { ok: false, code: 'bad-note' })
const result = await probe.handoff.note({ from: 'a', to: 'b', note: 'role "Boss"; no child Bots moved' })
assert.equal(result.ok, true)
assert.equal(result.ref.bot, 'a')
assert.equal(result.ref.name, 'Chief')
const kept = probe.memory.read('b')
assert.ok(kept.some(entry => entry.meta.source === 'handoff' && entry.meta.from === 'a' && entry.text.startsWith('Chief (a) was merged into you')), JSON.stringify(kept))
assert.deepEqual(probe.memory.read('a'), [], 'the archived Bot is only read')
})

test('onboarding announces the first Main Bot to plugins', async (t) => {
const { fb, call, stop } = await start({ empty: true })
t.after(stop)
Expand Down
69 changes: 69 additions & 0 deletions core/test/handoff.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// #450 item 1: the merge handoff seam. A plugin allowed by the host leaves one line with the Bot
// that keeps a merged Bot's role; the note lands in the kept Bot's own memory with the source Bot
// as metadata, and the archived Bot is only read. These are the refusals a plugin can hit.
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { HANDOFF, createHandoff } from '../src/host/handoff.js'

function fakeRt({ readOnly = false, names = {} } = {}) {
const bots = new Map([['old', { id: 'old', name: names.old ?? 'Editor' }], ['new', { id: 'new', name: names.new ?? 'Editor Prime' }]])
const writes = []
return {
rt: {
readOnly: () => (readOnly ? 'another host holds the team' : undefined),
botOf: id => bots.get(id),
botMemoryAppend: async (id, text, meta) => { writes.push({ id, text, meta }) },
},
writes,
}
}

test('#450: a plugin the host did not name may not leave a handoff note', async () => {
const { rt, writes } = fakeRt()
const note = createHandoff(rt, { allow: [] })
assert.deepEqual(await note('flybot-tree', { from: 'old', to: 'new', note: 'did dev' }), { ok: false, code: 'not-allowed' })
assert.equal(writes.length, 0, 'no memory write happens')
})

test('#450: a handoff note needs a live source and target, a string note and room under the cap', async () => {
const { rt, writes } = fakeRt()
const note = createHandoff(rt, { allow: ['flybot-tree'] })
assert.deepEqual(await note('flybot-tree', { to: 'new', note: 'x' }), { ok: false, code: 'no-source' })
assert.deepEqual(await note('flybot-tree', { from: 'gone', to: 'new', note: 'x' }), { ok: false, code: 'source-gone' })
assert.deepEqual(await note('flybot-tree', { from: 'old', to: 'gone', note: 'x' }), { ok: false, code: 'target-gone' })
assert.deepEqual(await note('flybot-tree', { from: 'old', to: 'old', note: 'x' }), { ok: false, code: 'same-bot' })
assert.deepEqual(await note('flybot-tree', { from: 'old', to: 'new', note: 42 }), { ok: false, code: 'bad-note' })
assert.deepEqual(await note('flybot-tree', { from: 'old', to: 'new', note: ' ' }), { ok: false, code: 'empty-note' })
assert.deepEqual(await note('flybot-tree', { from: 'old', to: 'new', note: 'x'.repeat(HANDOFF.note + 1) }), { ok: false, code: 'note-too-long' })
assert.equal(writes.length, 0, 'a refused call writes nothing')
})

test('#450: a handoff note lands in the kept Bot only and answers the source reference', async () => {
const { rt, writes } = fakeRt()
const at = Date.UTC(2026, 10, 22, 9)
const note = createHandoff(rt, { allow: ['flybot-tree'], now: () => at })
const result = await note('flybot-tree', { from: 'old', to: 'new', note: 'role "editor"; 1 child Bot moved to you' })
assert.deepEqual(result, { ok: true, ref: { bot: 'old', name: 'Editor', at: new Date(at).toISOString() } })
assert.equal(writes.length, 1)
assert.equal(writes[0].id, 'new', 'the kept Bot gets the note; the archived Bot is only read')
assert.match(writes[0].text, /^Editor \(old\) was merged into you: role "editor"/)
assert.deepEqual(writes[0].meta, { source: 'handoff', from: 'old', at: new Date(at).toISOString() })
})

test('#450: a read-only host refuses the handoff note before any memory write', async () => {
const { rt, writes } = fakeRt({ readOnly: true })
const note = createHandoff(rt, { allow: ['flybot-tree'] })
assert.deepEqual(await note('flybot-tree', { from: 'old', to: 'new', note: 'x' }), { ok: false, code: 'read-only' })
assert.equal(writes.length, 0)
})

test('#450: a note at the cap, with a long source name, still fits the memory entry cap', async () => {
const { rt, writes } = fakeRt({ names: { old: 'E'.repeat(400) } })
const at = Date.UTC(2026, 10, 22, 9)
const note = createHandoff(rt, { allow: ['flybot-tree'], now: () => at })
assert.equal((await note('flybot-tree', { from: 'old', to: 'new', note: 'x'.repeat(HANDOFF.note) })).ok, true)
const text = writes[0].text
assert.ok([...text].length <= 300, `the memory entry holds at most 300 characters: ${[...text].length}`)
assert.ok(text.startsWith(`${'E'.repeat(40)} (old) was merged into you: `), 'the source name is clipped, not the note')
assert.ok(text.endsWith('x'.repeat(HANDOFF.note)), 'the note stays whole')
})
29 changes: 29 additions & 0 deletions core/types/flybot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,25 @@ export interface ModelAsk {
}
export type ModelAnswer = { ok: true; text: string } | { ok: false; code: string }

/** A merge handoff note (#450 item 1): two live Bots and the one line the kept Bot keeps. */
export interface HandoffNote {
/** The Bot that was archived (only read). */
from: BotId
/** The Bot that keeps the role: the note is recorded in its own memory. */
to: BotId
/** One line, 1 to `HANDOFF.note` (200) characters; the host prefixes it with the source. */
note: string
}

/** The source reference `flybot.handoff.note` answers for a plugin's prompt line. */
export interface HandoffRef {
bot: BotId
name: string
/** ISO time the note was recorded. */
at: string
}
export type HandoffAnswer = { ok: true; ref: HandoffRef } | { ok: false; code: string }

/** What `flybot.projectHost()` answers: the core runtime and the shared pure helpers (#2830). */
export interface ProjectHost {
runtime: Record<string, unknown>
Expand Down Expand Up @@ -416,6 +435,16 @@ export interface FlyBot {
read(botId: BotId): MemoryEntry[]
append(botId: BotId, text: string, meta?: Record<string, string>): Promise<{ ok: boolean; code?: string }>
}
/**
* The merge handoff (#450 item 1): one line from a Bot that is being archived into the Bot that
* keeps its role. It takes the same `config.flybot.memoryPlugins` allow-list as `memory`, because
* the note is a memory write: the host records it in the `to` Bot's own memory (metadata
* `source: handoff`, `from`, `at`), reads the `from` Bot only, and answers the source reference
* `{ ok: true, ref: { bot, name, at } }` for the plugin's prompt line. A refused call writes
* nothing: `{ ok: false, code }` — `no-source`, `source-gone`, `no-target`, `target-gone`,
* `same-bot`, `bad-note`, `empty-note`, `note-too-long`, `read-only`, `not-allowed`, `no-api`.
*/
handoff: { note(input: HandoffNote): Promise<HandoffAnswer> }
/**
* A sanctioned, capped model call for this plugin (#450). Off unless the host names the
* plugin in config.flybot.modelPlugins; text only (no side effect), capped input/output and
Expand Down
1 change: 1 addition & 0 deletions docs/subsystems/core-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ The host's own files carry the user's content — a Bot's memory and the memory
### Plugin memory seam (#450 item 1)

- `memory.js` exposes `rt.botMemoryRead(botId)` (a Bot's own entries, capped at 200) and `rt.botMemoryAppend(botId, text, meta)` (one entry through the ordinary path — secret-scanned, size-capped, journaled). `hooks.js` gates them as `flybot.memory` for a plugin the host names in `flybot.memoryPlugins` (empty by default). The tree uses it to carry a merged Bot's memory into the kept Bot. Tests: the `flybot.memory` case in `core/test/flybot.test.js`.
- `src/host/handoff.js` is the merge handoff beside `parts.js`'s part handoff: `createHandoff(rt, { allow })` builds `flybot.handoff.note({ from, to, note })` for `hooks.js`, on the same `flybot.memoryPlugins` allow-list. It validates the two Bot ids and the note (1–200 characters after whitespace is collapsed; every refusal is a short `code` and writes nothing), writes the note into the **`to`** Bot's memory through `rt.botMemoryAppend` (`source: handoff`, `from`, `at`), reads the `from` Bot only, and answers `{ ok: true, ref: { bot, name, at } }`. The source name and id in the entry prefix are clipped (40 / 24 characters) so the entry stays under `MEMORY_LIMITS.entry`. Tests: `core/test/handoff.test.js`, the `flybot.handoff` case in `core/test/flybot.test.js`.

### Decisions a Bot records (#396)

Expand Down
5 changes: 5 additions & 0 deletions docs/subsystems/plugin-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ FlyBot's plugin API surface on top of the patched core: host hooks, client slots
- **Off unless named.** The host config `flybot.memoryPlugins` is an allow-list; an unnamed plugin's `read` returns `[]` and its `append` resolves `{ ok: false, code: 'not-allowed' }`. The list is empty by default.
- Bounded and safe: at most 200 entries are read; an entry with a secret or over the entry size is refused; `append` never throws into the caller (it resolves a short `code`). A read-only host refuses the write. Tests: the `flybot.memory` case in `core/test/flybot.test.js`.

### The merge handoff note (#450 item 1)

- `flybot.handoff.note({ from, to, note })` is the one line a merge leaves with the Bot that keeps the merged Bot's role (`plugins/tree`). It takes the **same** `flybot.memoryPlugins` allow-list as `flybot.memory`, because the note is a memory write: `core/src/host/handoff.js` records it in the `to` Bot's own memory through the ordinary path (entry text `<name> (<id>) was merged into you: <note>`, metadata `source: handoff`, `from`, `at`), reads the `from` Bot only, and answers `{ ok: true, ref: { bot, name, at } }` — the source reference a plugin puts in its prompt line. An unnamed plugin is refused, and a refused call writes nothing.
- Typed and bounded: both ids must name live Bots and differ (`no-source`, `source-gone`, `no-target`, `target-gone`, `same-bot`); `note` must be a string of 1 to 200 characters after whitespace is collapsed (`bad-note`, `empty-note`, `note-too-long`); a read-only host answers `read-only`, and an older core without `rt.botMemoryAppend` answers `no-api`. The source name and id in the entry prefix are clipped to 40 and 24 characters so the entry stays under the memory entry cap. Tests: `core/test/handoff.test.js` and the `flybot.handoff` case in `core/test/flybot.test.js`.

## Tests

`core/test/flybot.test.js`, `core/test/flybot-client.test.js`, `core/test/plugin-model.test.js` via `npm test`.
Expand Down
Loading
Loading