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
248 changes: 127 additions & 121 deletions src/bundled/bestie/BestieTab.test.tsx

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions src/bundled/bestie/BestieTab.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useRef, useState } from "react";
import { watchSlug } from "../../features/agents/attention";
import { interestSlug, watchSlug } from "../../features/agents/attention";
import type {
Agent,
AgentAddedTab,
Expand Down Expand Up @@ -334,6 +334,8 @@ function FeatureSwitch({
);
const instructions = instructionsOf(agent);
const timer = feature.timer && agent.attention[watchSlug(feature.timer.id)];
const part =
feature.timer && (timer || agent.attention[interestSlug(feature.timer.id)]);
const required = FEATURES.find(({ id }) => id === feature.requires);
const blocked = !!required && !featureOn(agent, required);
const checked = featureOn(agent, feature);
Expand Down Expand Up @@ -425,12 +427,13 @@ function FeatureSwitch({
disabled={saving || ((blocked || formerCheckIn) && !checked)}
onCheckedChange={(on) => void save(on)}
/>
{/* A timer left without its section, as when undoing a failed On
failed too, still runs: Remove takes it away. */}
{timer && !checked && (
{/* A timer or Interest left without the rest, as when undoing a failed
On failed too, or one was deleted in Attention: Remove takes it
away. */}
{part && !checked && (
<PreferenceRow
title={`${feature.title}'s timer is still saved`}
subtitle="Its section in Instructions is missing, so it reads as off."
title={`${feature.title} is partly saved`}
subtitle="Part of it is missing, so it reads as off. Remove clears the rest."
trailing={(label) => (
<Button
variant="outline"
Expand Down
84 changes: 67 additions & 17 deletions src/bundled/bestie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,20 @@ section and private channels stay, which its Former Bestie tab can turn off.
Both need an account to save none to.

**Remembers you** adds memory rules to the agent's instructions, in a section
marked `<!-- buzz:bestie/memory v1 -->` … `<!-- /buzz:bestie/memory -->`, and
turning it off removes that section. The switch is on while the section is
marked `<!-- buzz:bestie/memory v2 -->` … `<!-- /buzz:bestie/memory -->`, and
turning it off removes that section. The section is the memory map and its
rules, which every turn needs: each slug with its size and who writes it
(`core`, `mem/user`, `mem/memory`, `mem/entities/<type>/<id>` cards,
`mem/hypotheses`, `mem/notes/YYYY-MM-DD`, `mem/heartbeat`), add-then-organize
(each card write is linked from that day's note, which is how Nightly review
finds it), what is a fact and what a guess, and checking a fact against its source before
relying on it. Sizes are asked for in the text; nothing enforces them yet. An
agent that already has the v1 section keeps it until Remembers you is turned
off and on again, or until the upgrade from Every hour below replaces it, which
it does only while the section's text is still v1 as it shipped.
Nightly review never drops what v1 kept in `mem/memory`: before it rebuilds
the index, it moves each claim there that is not a card line, a "Now" line or
a lesson to its card, or to `mem/hypotheses` if it is a guess. The switch is on while the section is
there, whatever you edit inside it or type next to its markers. Off removes
it with your edits inside it, and keeps text you put before its opening
marker or after its closing one. If you edit, remove or copy one of its
Expand Down Expand Up @@ -115,21 +127,59 @@ Public channels
need nothing: agents read them already. Adding Bestie to new channels you
create is not set up yet.

**Check-ins → Every hour** needs Remembers you, and turning memory off turns it
off too. On adds the `buzz:bestie/checkin.hourly` Hourly upkeep section to the
instructions and an hourly Attention timer, `watch/bestie-hourly-check-in`,
that wakes Bestie in your DM with it. Each check-in reads what is new in
that DM and in the private channels Bestie was added to, and reports only in
the DM. The Watching tab shows the timer. Off removes the section and the
timer. The timer runs while Buzz is open and the Watching plugin is on: this
plugin doesn't call
`controlAttention()`, so turning Watching off still turns Attention off for
every agent. While the timer can't run, the row says it is paused, and a
timer paused on its own in the Attention tab says to turn the switch off and on
again to restart it. If the instructions fail to save after the timer did, On
removes the timer again; a timer still left without its section gets a
**Remove** row. After a sleep it runs once, not once for each missed hour. Each
check-in is a feature in `features.ts`, so another kind is one more entry.
**Check-ins** need Remembers you, and turning memory off turns them off too.
There are two, each its own switch, and neither needs the other. A check-in is
an Attention Interest, whose instructions hold its steps, and a timer that
wakes Bestie in your DM with it. It adds nothing to the instructions every turn
carries, so a chat turn doesn't carry the steps. The Watching tab shows the
timers. Off removes the Interest and the timer. It reads as on only while both
are there.

- **Hourly notes** (`bestie-notes`, every hour) reads what is new in the
channels Bestie is a member of, since a mark for each channel in
`mem/heartbeat`. It never joins a channel or adds itself to one, so it reads
your DM and the private channels you added it to, and no others. If no
channel has anything new, none is new to it and no follow-up is due, it stops:
no memory write, no receipt, no message. Otherwise it appends quoted lines,
each with its source, to `mem/notes/YYYY-MM-DD`, and dated lines to the
entity cards they are about. It messages you in the DM only when a follow-up
you asked for is due.
- **Nightly review** (`bestie-dream`, once a day, about 03:00 on this device)
is silent. It checks the claims it changes against their source messages,
promotes a hypothesis to a fact only when you confirmed it or it has three
observations on three days and none against, retires one on a contradiction,
keeps `mem/user` to 4 KB, organizes the cards, rebuilds the index and its
"Now" section in `mem/memory`, and adds a short reflection to the day's
notes. It never sends a message. It does not need Hourly notes.

The timers run while Buzz is open and the Watching plugin is on: this plugin
doesn't call `controlAttention()`, so turning Watching off still turns
Attention off for every agent. While a timer can't run, its row says it is
paused, and a timer paused on its own in the Attention tab says to turn the
switch off and on again to restart it. A check-in whose Interest or timer was
deleted shows **Remove** for what is left. After a sleep a timer runs once, not
once for each missed run. Nightly review is armed at the latest 03:00 here, so
its first run is the next 03:00; after each run Attention schedules the next
one a day later, so a run made late by a sleep moves later until the timer is
turned off and on again. Each check-in is a feature in `features.ts`, so
another kind is one more entry.

**Upgrading from Every hour.** Earlier versions had one check-in, `Every hour`:
a `buzz:bestie/checkin.hourly` section of the instructions with all its steps,
and a timer, `watch/bestie-hourly-check-in`. Its steps added Bestie to each
private channel it had been added to, which Hourly notes no longer does. For
your Bestie, whenever the account's choice is known, the plugin turns on Hourly
notes and Nightly review in the DM the old timer ran in, when the old check-in
was on with memory, paused if its timer or Interest was paused in Attention.
That is one Attention change that removes the old Interest and timer before it
adds the new ones, so it saves whole or not at all, and an agent at its limit
of 250 watches and timers has the old timer's room. Then it removes the old
section, and replaces a v1 memory section where it is with the current one,
unless you edited it: one whose text differs from v1 as it shipped, beyond line
endings and trailing spaces, stays as it is. A failure leaves what is left of the old check-in to try again. An old check-in that read as off,
such as one with only its timer left, is only removed. Another agent's is
stopped, as any former Bestie's. For an agent also in another community, only
the timers change, as its instructions are shared.

**Changing your Bestie.** Only your Bestie checks in: whenever the saved Bestie
is known, chosen here or read from your account, any other agent here has its
Expand Down
193 changes: 181 additions & 12 deletions src/bundled/bestie/features.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { expect, it } from "vitest";
import { validateObject } from "../../features/agents/attention";
import {
checkinHourly,
dream,
FEATURES,
featureState,
hasFeature,
LEGACY,
memory,
notes,
restoredFeature,
upgradedFeature,
withFeature,
} from "./features";
import { v1CheckIn, v1Memory } from "./test-helpers";

const OPEN = "<!-- buzz:bestie/memory v1 -->";
const OPEN = `<!-- buzz:bestie/memory v${memory.version} -->`;
const CLOSE = "<!-- /buzz:bestie/memory -->";

it("adds memory once, at the end, and removes only what it added", () => {
Expand Down Expand Up @@ -93,9 +99,7 @@ it("shows a section whose markers were edited, and adds no second one", () => {
}
expect(featureState("Be brief.", memory)).toBe("off");
// Another feature's markers are not this one's.
expect(featureState(withFeature("", checkinHourly, true), memory)).toBe(
"off",
);
expect(featureState(withFeature("", v1CheckIn, true), memory)).toBe("off");
});

it("restores an edited section only where its markers show it was", () => {
Expand Down Expand Up @@ -126,7 +130,7 @@ it("restores an edited section only where its markers show it was", () => {
memory,
),
).toBeUndefined();
const other = withFeature("", checkinHourly, true);
const other = withFeature("", v1CheckIn, true);
expect(
restoredFeature(
`${OPEN.replace(" v1", "")}\nrules\n\n${other}\n${CLOSE.replace(" -->", "-->")}`,
Expand Down Expand Up @@ -176,10 +180,175 @@ it("keeps the owner's line ending, or its absence, through on and off", () => {
).toBe("Be brief.\n");
});

it("has the check-in read the private channels Bestie was added to", () => {
const load = checkinHourly.text
.split("\n")
.find((line) => line.startsWith("1. LOAD."));
expect(load).toContain("each private channel you have been added to");
expect(load).toContain("you are a guest there and can't post");
it("keeps the steps of Hourly notes and Nightly review out of the instructions every turn carries", () => {
// Only memory has a section; the other two are a timer and an Interest.
expect(FEATURES.filter(({ text }) => text).map(({ id }) => id)).toEqual([
"memory",
]);
for (const feature of [notes, dream]) {
const owner = "Be brief.\n";
expect(withFeature(owner, feature, true)).toBe(owner);
expect(featureState(owner, feature)).toBe("off");
// Each wakes the agent with its steps, and needs memory, no other check-in.
expect(feature.requires).toBe("memory");
expect(feature.timer?.interest.length).toBeGreaterThan(1_000);
expect(
validateObject(`interest/${feature.timer?.id}`, {
type: "interest",
instructions: feature.timer?.interest,
}),
).toBeUndefined();
}
// What every turn carries is small and has no wake's steps.
expect(memory.text?.length).toBeLessThan(4_000);
expect(memory.text).not.toMatch(/1\. LOOK|1\. READ/);
});

it("gives each check-in its own timer, and the nightly review a daily one at 03:00", () => {
const timers = FEATURES.flatMap(({ timer }) => (timer ? [timer] : []));
expect(timers.map(({ id }) => id)).toEqual(["bestie-notes", "bestie-dream"]);
expect(notes.timer).toMatchObject({ everySecs: 3600, id: "bestie-notes" });
expect(notes.timer?.atHour).toBeUndefined();
expect(dream.timer).toMatchObject({ everySecs: 86_400, atHour: 3 });
// The old hourly check-in is one to remove, not one to turn on.
expect(FEATURES.map(({ id }) => id)).not.toContain(LEGACY[0]?.id);
expect(timers.map(({ id }) => id)).not.toContain(LEGACY[0]?.timer?.id);
});

it("has no step that adds Bestie to a channel it was added to", () => {
const all = [memory.text, notes.timer?.interest, dream.timer?.interest].join(
"\n",
);
expect(all).not.toMatch(/added to|add .*to channels|you are a guest/i);
// Notes read the channels it is in, and say not to join others.
expect(notes.timer?.interest).toContain(
"Call channel_list: the channels you are in.",
);
expect(notes.timer?.interest).toContain(
"Never join a channel, add yourself to one or read one you are not in.",
);
});

it("has Hourly notes stop with no write when nothing is new, and message only for a due follow-up", () => {
const steps = notes.timer?.interest ?? "";
expect(steps).toContain("no memory write, no receipt, no message");
expect(steps).toContain("STOP, writing nothing and sending nothing");
expect(steps).toContain("Only if a follow-up the owner asked for is due now");
// The receipt is a step after the notes, which a stopped pass never reaches.
expect(steps.indexOf("STOP")).toBeLessThan(steps.indexOf("receipt (time"));
});

it("has Nightly review silent, strict about promotion and bounded in what it changes", () => {
const steps = dream.timer?.interest ?? "";
expect(steps).toContain("you never send a message");
expect(steps).toContain("Send no message.");
expect(steps).toContain(
"3 or more observations on 3 or more different days and none against",
);
expect(steps).toContain("read its source event");
expect(steps).toContain("at most 4 KB");
expect(steps).toContain("MOVED TO [[slug]]");
expect(steps).toContain('Append a "Dream" section');
// A reflection has explicit ends, and the stop check leaves out only what
// lies between them: a note holding only a reflection is no work, and a
// card link appended after its end line is.
const start = '"<!-- dream:start -->"';
const end = '"<!-- dream:end -->"';
expect(steps).toContain(
`Start it with a line ${start} and end it with a line ${end}, so later lines are not part of it.`,
);
expect(steps).toContain(
`no note line dated on or after last_dream outside a reflection (from a ${start} line to the next ${end} line; anything after that end line is new)`,
);
expect(steps).not.toContain('outside a "Dream" section');
// A night with no new note still retires stale hypotheses.
expect(steps).toContain("Stop and write nothing only if there is no note");
// Lines are dated by day, so a line from later on last_dream's day counts.
expect(steps).toContain("mem/notes/<date> dated on or after last_dream");
expect(steps).toContain("no line in mem/user dated on or after it");
expect(steps).not.toMatch(/since last_dream|dated after it/);
expect(steps).toContain("step 3 still marks and retires stale hypotheses");
// The cards it reads, and whether it stops, both come from the notes, so
// every card write links its card from a note: in chat and in Hourly notes.
const link = "[[mem/entities/<type>/<id>]]";
expect(steps).toContain(`each card those notes link as ${link}`);
expect(memory.text).toContain(
`Each time you write to a card, also append a line to mem/notes/<today> with the source event id that links the card as ${link}`,
);
expect(notes.timer?.interest).toContain(
`link the card from the note line as ${link}`,
);
});

it("takes the old hourly check-in's section away, as it was saved", () => {
const on = withFeature(
withFeature("Be brief.\n", memory, true),
v1CheckIn,
true,
);
expect(on).toContain("each private channel you have been added to");
const [old] = LEGACY;
expect(old && featureState(on, old)).toBe("on");
const off = old ? withFeature(on, old, false) : on;
expect(off).toBe(withFeature("Be brief.\n", memory, true));
expect(off).not.toContain("Hourly upkeep");
});

it("upgrades an earlier version's whole section where it is, and nothing else", () => {
const owner = (section: string) =>
`Be brief.\n\n${section}\n\nAlways answer in French.\n`;
const old = withFeature("", v1Memory, true).trimEnd();
const current = withFeature("", memory, true).trimEnd();
expect(upgradedFeature(owner(old), memory)).toBe(owner(current));
// The current version, an edited section and no section stay as they are.
expect(upgradedFeature(owner(current), memory)).toBe(owner(current));
const edited = owner(old).replace(CLOSE, "");
expect(upgradedFeature(edited, memory)).toBe(edited);
expect(upgradedFeature("Be brief.", memory)).toBe("Be brief.");
// Stock text in other line endings, or with trailing spaces, is still stock.
const crlf = owner(old)
.replace(/\n/g, "\r\n")
.replace(/\.\r\n/g, ". \r\n");
expect(upgradedFeature(crlf, memory)).toContain(OPEN);
expect(upgradedFeature(crlf, memory)).not.toContain("memory v1");
});

it("leaves an earlier version's section the owner edited inside its markers as it is", () => {
const old = withFeature("Be brief.", v1Memory, true);
for (const edited of [
// A rule added, one changed and one removed, all between whole markers.
old.replace("Rules:", "Rules:\n- Never store my medical details."),
old.replace("Never duplicate a claim", "Always duplicate a claim"),
old.replace(/^- Quoted, forwarded.*\n/m, ""),
]) {
expect(edited).not.toBe(old);
expect(featureState(edited, memory)).toBe("on");
expect(upgradedFeature(edited, memory)).toBe(edited);
}
});

it("pins version 1 of Remembers you as it shipped", () => {
const text = memory.earlier?.[1] ?? "";
expect(text.startsWith("# Memory\n")).toBe(true);
expect(text).toContain(
"- mem/memory: curated longer-term knowledge. Replace superseded claims; never duplicate.",
);
expect(text).not.toContain("mem/entities");
});

it("has Nightly review keep what an earlier version saved in mem/memory before rebuilding it", () => {
const steps = dream.timer?.interest ?? "";
// v1 chat turns kept prose there; the rebuild keeps only cards, Now and lessons.
expect(v1Memory.text).toContain("mem/memory: curated longer-term knowledge");
expect(steps).toContain(
'Before you write mem/memory again, move each claim in it that is not a card line, a "Now" line or a lesson, such as one an earlier version of your memory rules saved there, to its card, or to mem/hypotheses if it is a guess, with its date and source; never drop one.',
);
// Every night, not only the first: a v1 section may still be in the instructions.
const step = steps.slice(
steps.indexOf("5. ORGANIZE"),
steps.indexOf("6. REFLECT"),
);
expect(step.indexOf("Before you write mem/memory again")).toBeLessThan(
step.indexOf("Then write mem/memory again"),
);
});
Loading
Loading