Skip to content

Commit 76c1d9f

Browse files
loganjLarry
andauthored
fix(agents): keep attention off until the owner turns it on (#951)
Signed-off-by: Logan Johnson <[email protected]> Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent 017ec30 commit 76c1d9f

4 files changed

Lines changed: 63 additions & 11 deletions

File tree

‎src/features/agents/service.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ it("imports a shared agent as a new identity with its name and picture, adopting
505505
it("delivers mentions and matching watches once, and never the agent's own events", async () => {
506506
const { service, run, emit } = await setup();
507507
await service.create({ type: "example/echo", name: "Echo" });
508+
await service.save(bot, { attentionEnabled: true });
508509
const mention = event("m", {
509510
tags: [
510511
["h", channel],
@@ -637,6 +638,7 @@ it("keeps handed-off activity local, without publishing working reactions", asyn
637638
it("wakes on a reply in a thread it started only by a mention or a watch", async () => {
638639
const { service, run, emit } = await setup();
639640
await service.create({ type: "example/echo", name: "Echo" });
641+
await service.save(bot, { attentionEnabled: true });
640642
run.mockImplementationOnce(async ({ agent }) => {
641643
await agent.publish({ kind: 9, content: "hi", tags: [["h", channel]] });
642644
});
@@ -1305,6 +1307,7 @@ it("fires a due timer once, then again an interval after it ran", async () => {
13051307
vi.useFakeTimers({ now: 1_000_000 });
13061308
const { service, run, storage } = await setup();
13071309
await service.create({ type: "example/echo", name: "Echo" });
1310+
await service.save(bot, { attentionEnabled: true });
13081311
await service.save(bot, {
13091312
attention: {
13101313
"watch/tick": {
@@ -1364,6 +1367,7 @@ it("keeps at most one occurrence of a timer waiting, and never runs one removed
13641367
vi.useFakeTimers({ now: 1_000_000 });
13651368
const { service, run } = await setup();
13661369
await service.create({ type: "example/echo", name: "Echo" });
1370+
await service.save(bot, { attentionEnabled: true });
13671371
const timer = (prompt: string) =>
13681372
({
13691373
type: "timer",
@@ -1400,6 +1404,7 @@ it("gives each due timer a turn when one's runs outlast its interval", async ()
14001404
vi.useFakeTimers({ now: 1_000_000 });
14011405
const { service, run } = await setup();
14021406
await service.create({ type: "example/echo", name: "Echo" });
1407+
await service.save(bot, { attentionEnabled: true });
14031408
const timer = (prompt: string) =>
14041409
({
14051410
type: "timer",
@@ -1434,6 +1439,7 @@ it("runs waiting events before a due timer, so a busy timer cannot starve them",
14341439
vi.useFakeTimers({ now: 1_000_000 });
14351440
const { service, run, emit } = await setup();
14361441
await service.create({ type: "example/echo", name: "Echo" });
1442+
await service.save(bot, { attentionEnabled: true });
14371443
await service.save(bot, {
14381444
attention: {
14391445
"watch/tick": {
@@ -1548,6 +1554,7 @@ it("keeps the cleanup for later when the session closes while archive state is r
15481554
it("runs once per matching watch, and passes a classifier watch it cannot classify", async () => {
15491555
const { service, run, emit } = await setup();
15501556
await service.create({ type: "example/echo", name: "Echo" });
1557+
await service.save(bot, { attentionEnabled: true });
15511558
await service.save(bot, {
15521559
attention: {
15531560
"watch/classified": {
@@ -1587,6 +1594,7 @@ it("runs once per matching watch, and passes a classifier watch it cannot classi
15871594
it("keeps DMs and reactions to its own messages away from watches", async () => {
15881595
const { service, run, emit } = await setup();
15891596
await service.create({ type: "example/echo", name: "Echo" });
1597+
await service.save(bot, { attentionEnabled: true });
15901598
await service.save(bot, {
15911599
attention: {
15921600
"watch/everything": {
@@ -1625,6 +1633,7 @@ it("keeps DMs and reactions to its own messages away from watches", async () =>
16251633
it("wakes a watch only for channels the agent is in, even when it watches all", async () => {
16261634
const { service, run, emit } = await setup();
16271635
await service.create({ type: "example/echo", name: "Echo" });
1636+
await service.save(bot, { attentionEnabled: true });
16281637
await service.save(bot, {
16291638
attention: {
16301639
"watch/everything": {
@@ -1655,9 +1664,29 @@ it("wakes a watch only for channels the agent is in, even when it watches all",
16551664
});
16561665
});
16571666

1667+
it("keeps a new agent's attention off until its owner turns it on", async () => {
1668+
const { service, run, emit } = await setup();
1669+
await service.create({ type: "example/echo", name: "Echo" });
1670+
expect(service.find(bot)?.attentionEnabled).toBe(false);
1671+
// Its type's default watch matches, but wakes nothing while off.
1672+
emit({ events: [event("one", { content: "deploy" })] });
1673+
await settle();
1674+
await settle();
1675+
expect(run).not.toHaveBeenCalled();
1676+
await service.save(bot, { attentionEnabled: true });
1677+
expect(service.find(bot)?.attentionEnabled).toBe(true);
1678+
emit({ events: [event("two", { content: "deploy" })] });
1679+
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(1));
1680+
expect(run.mock.calls[0]?.[0].trigger).toMatchObject({
1681+
type: "watch",
1682+
event: { id: "two".padEnd(64, "0") },
1683+
});
1684+
});
1685+
16581686
it("drops a queued watch run whose watch was disabled, or whose attention went off, before it ran", async () => {
16591687
const { service, run, emit } = await setup();
16601688
await service.create({ type: "example/echo", name: "Echo" });
1689+
await service.save(bot, { attentionEnabled: true });
16611690
let release = () => {};
16621691
run.mockImplementationOnce(
16631692
() => new Promise<void>((resolve) => (release = resolve)),
@@ -1699,6 +1728,7 @@ it("drops a queued watch run whose watch was disabled, or whose attention went o
16991728
it("drops a queued watch run when attention goes off and on, or its watch is replaced, before it ran", async () => {
17001729
const { service, run, emit } = await setup();
17011730
await service.create({ type: "example/echo", name: "Echo" });
1731+
await service.save(bot, { attentionEnabled: true });
17021732
const watch = service.find(bot)?.attention["watch/channel"]?.value;
17031733
let release = () => {};
17041734
const hold = () =>
@@ -1743,6 +1773,7 @@ it("drops a queued watch run when attention goes off and on, or its watch is rep
17431773
it("tells a run whose type queued its work whether that work is still current", async () => {
17441774
const { service, run, emit } = await setup();
17451775
await service.create({ type: "example/echo", name: "Echo" });
1776+
await service.save(bot, { attentionEnabled: true });
17461777
const watch = service.find(bot)?.attention["watch/channel"]?.value;
17471778
emit({ events: [event("one", { content: "deploy" })] });
17481779
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(1));
@@ -1773,6 +1804,7 @@ it("voids a handed-off timer occurrence once attention or the timer is reset", a
17731804
vi.useFakeTimers({ now: 1_000_000 });
17741805
const { service, run } = await setup();
17751806
await service.create({ type: "example/echo", name: "Echo" });
1807+
await service.save(bot, { attentionEnabled: true });
17761808
const timer = {
17771809
type: "timer",
17781810
interest_id: "default",
@@ -1813,6 +1845,7 @@ it("runs a spent timer again when its owner restarts it", async () => {
18131845
vi.useFakeTimers({ now: 1_000_000 });
18141846
const { service, run } = await setup();
18151847
await service.create({ type: "example/echo", name: "Echo" });
1848+
await service.save(bot, { attentionEnabled: true });
18161849
const once = {
18171850
type: "timer",
18181851
interest_id: "default",
@@ -2561,6 +2594,7 @@ async function handleOf(
25612594
it("lets a run read and write its attention with expected-state tokens", async () => {
25622595
const { service, run, emit } = await setup();
25632596
await service.create({ type: "example/echo", name: "Echo" });
2597+
await service.save(bot, { attentionEnabled: true });
25642598
const { attention } = await handleOf(run, emit);
25652599
expect(attention.enabled()).toBe(true);
25662600
expect(attention.classifier()).toBe("unavailable");

‎src/features/agents/service.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,8 +1045,8 @@ export class AgentsService extends Service implements Agents {
10451045
if (change.attentionEnabled !== undefined) {
10461046
const { attentionEnabled: _, ...rest } = record;
10471047
record = change.attentionEnabled
1048-
? rest
1049-
: { ...rest, attentionEnabled: false };
1048+
? { ...rest, attentionEnabled: true }
1049+
: rest;
10501050
}
10511051
for (const [slug, value] of Object.entries(change.attention ?? {}))
10521052
record = setAttention(
@@ -1410,7 +1410,7 @@ export class AgentsService extends Service implements Agents {
14101410
attention: EMPTY,
14111411
skipped: EMPTY,
14121412
timers: EMPTY,
1413-
attentionEnabled: true,
1413+
attentionEnabled: false,
14141414
config: undefined,
14151415
});
14161416
this.shown.set(fields.pubkey, agent);
@@ -1453,7 +1453,7 @@ export class AgentsService extends Service implements Agents {
14531453
attention: record.attention,
14541454
skipped: record.skipped ?? EMPTY,
14551455
timers: record.timers ?? EMPTY,
1456-
attentionEnabled: record.attentionEnabled !== false,
1456+
attentionEnabled: record.attentionEnabled === true,
14571457
config: record.config,
14581458
});
14591459
}
@@ -1733,7 +1733,7 @@ export class AgentsService extends Service implements Agents {
17331733
prior.attention === record.attention &&
17341734
prior.skipped === (record.skipped ?? EMPTY) &&
17351735
prior.timers === (record.timers ?? EMPTY) &&
1736-
prior.attentionEnabled === (record.attentionEnabled !== false) &&
1736+
prior.attentionEnabled === (record.attentionEnabled === true) &&
17371737
prior.config === record.config
17381738
? prior
17391739
: this.view(identity, record),
@@ -1757,7 +1757,7 @@ export class AgentsService extends Service implements Agents {
17571757
const runner = this.runners.get(identity.pubkey);
17581758
// Turning attention off voids the watch work it queued, even if it is
17591759
// turned on again before that work would run.
1760-
if (runner && this.records[identity.pubkey]?.attentionEnabled === false) {
1760+
if (runner && this.records[identity.pubkey]?.attentionEnabled !== true) {
17611761
runner.epoch++;
17621762
runner.queue = runner.queue.filter(
17631763
(job) => job.trigger.type !== "watch",
@@ -2280,7 +2280,7 @@ export class AgentsService extends Service implements Agents {
22802280
return this.recordOf(identity);
22812281
};
22822282
return Object.freeze({
2283-
enabled: () => record().attentionEnabled !== false,
2283+
enabled: () => record().attentionEnabled === true,
22842284
classifier: () => this.classifier(),
22852285
show: (slug: string, interest?: string) =>
22862286
show(record(), slug, seconds(), interest),

‎src/features/agents/store.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ it("round-trips records and drops attention objects a reader would ignore", () =
5555
expect(fixed.skipped).toEqual({});
5656
});
5757

58+
it("saves attention only as on; absent or anything else reads as off", () => {
59+
const storage = memoryStorage();
60+
writeRecords(storage, {
61+
[pubkey]: { ...record, attentionEnabled: true },
62+
["b".repeat(64)]: { ...record, pubkey: "b".repeat(64) },
63+
["c".repeat(64)]: {
64+
...record,
65+
pubkey: "c".repeat(64),
66+
attentionEnabled: false,
67+
},
68+
});
69+
const read = readRecords(storage);
70+
expect(read[pubkey]?.attentionEnabled).toBe(true);
71+
expect(read["b".repeat(64)]).not.toHaveProperty("attentionEnabled");
72+
expect(read["c".repeat(64)]).not.toHaveProperty("attentionEnabled");
73+
});
74+
5875
it("skips objects over the count limit in slug order, and refuses to add more", () => {
5976
const storage = memoryStorage();
6077
const attention = Object.fromEntries(

‎src/features/agents/store.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ export type AgentRecord = Readonly<{
2929
skipped?: Readonly<Record<string, SkippedObject>>;
3030
/** Plugin-owned config. The host never reads it. */
3131
config: unknown;
32-
/** False when the owner turned attention off: its watches and timers stay
33-
* stored but wake nothing, and the agent is not offered attention tools.
34-
* Absent means on. */
32+
/** True when the owner turned attention on. Off, its watches and timers
33+
* stay stored but wake nothing, and the agent is not offered attention
34+
* tools. Absent means off, so an agent has no attention until its owner
35+
* turns it on. */
3536
attentionEnabled?: boolean;
3637
/** Run state of its timers, by slug. Not config, so it never leaves the device;
3738
* kept here so removing a timer or the agent removes its state with it. */
@@ -101,7 +102,7 @@ export function readRecords(storage: Storage): Record<string, AgentRecord> {
101102
skipped,
102103
config: record.config,
103104
timers,
104-
...(record.attentionEnabled === false ? { attentionEnabled: false } : {}),
105+
...(record.attentionEnabled === true ? { attentionEnabled: true } : {}),
105106
};
106107
}
107108
return records;

0 commit comments

Comments
 (0)