Skip to content

Commit eb1d64f

Browse files
rileycraneOpus 5.5 Maxclaude
committed
fix(agents): report a conversation wake that fails before its turn starts
A timer or watch that delivers to a conversation can fail before its turn reaches the harness: the agent's protection is unavailable, its start is refused, its root is not in the channel, or the handover times out. The bundled harnesses report failures only inside their own handoff, so this path reached only console.error, which the packaged app does not keep. The occurrence was spent with no trace. The runner now posts a ⚠️ notice in that conversation that mentions the owner, once until one of the wake's turns starts again. It is not sent when the agent stopped, the wake is no longer current, or the agent is not a member there. A refused root is not replied to. The occurrence is still used, not retried. Co-authored-by: Opus 5.5 Max <d9fc2aa400ad81a2f289921a1720811e5d922c43e079f37241d4be458d8d92aa@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Riley Crane <[email protected]>
1 parent 8aa3adf commit eb1d64f

3 files changed

Lines changed: 153 additions & 1 deletion

File tree

‎docs/buzz-help.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,10 @@ tab, asks **Discard changes?** first; **Keep editing** keeps them.
15601560
watched commands stop when their Buzz process exits.
15611561
During a handoff between Buzz builds, the new copy waits for the previous
15621562
command to stop, then continues the watch.
1563+
When a timer or watch that delivers to a conversation wakes the agent but its
1564+
turn can't start, the agent posts a ⚠️ notice there that mentions its owner.
1565+
That occurrence is used, not retried, and a wake that keeps failing is
1566+
reported again only after one of its turns starts.
15631567
- **Shared compute** is off by default; enable it in Settings → Plugins, then
15641568
turn on sharing separately. **Mesh agents**, when it is enabled, use saved Interest
15651569
instructions for watched turns in both Chat and Agent mode. Watched messages

‎src/features/agents/runner.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ export class AgentRunners {
614614
private readonly readAddressable = new WeakMap<RelayEvent, boolean>();
615615
private readonly sourceJobs = new Map<Job, string>();
616616
private readonly failedSources = new WeakSet<AttentionObject>();
617+
/** Wakes whose failure to start was reported, until one of their turns
618+
* starts again, so a failing repeat reports once. */
619+
private readonly reportedWakes = new Set<string>();
617620
/** A terminal producer retains its registration until every queued turn settles. */
618621
async completeSource(key: AgentKey, object: AttentionObject) {
619622
await this.host.change(key, (record) => {
@@ -2723,6 +2726,7 @@ export class AgentRunners {
27232726
? (title: string) => this.host.named(agent, trigger.event, title)
27242727
: undefined;
27252728
let attempted = false;
2729+
let rootRefused = false;
27262730
const woke = () => {
27272731
attempted = true;
27282732
if (!addressed(trigger) && job.matched)
@@ -2816,8 +2820,10 @@ export class AgentRunners {
28162820
if (anchor) {
28172821
if (!delivery.current()) return;
28182822
if (anchor.root) {
2819-
if (!(await this.rootInChannel(agent, anchor.channel, anchor.root)))
2823+
if (!(await this.rootInChannel(agent, anchor.channel, anchor.root))) {
2824+
rootRefused = true;
28202825
throw new Error("The delivery root is not in its channel");
2826+
}
28212827
if (!delivery.current()) return;
28222828
}
28232829
const wakeText = async () => {
@@ -2894,15 +2900,53 @@ export class AgentRunners {
28942900
.catch((error) =>
28952901
console.warn("Completed subscription was not removed", error),
28962902
);
2903+
// Set only for a wake that delivers to a conversation.
2904+
const reported =
2905+
destination &&
2906+
trigger.type !== "mention" &&
2907+
trigger.type !== "plugin" &&
2908+
trigger.type !== "attention"
2909+
? { key: `${agent.key}\0${trigger.slug}`, trigger, destination }
2910+
: undefined;
28972911
// Handover expiry releases the queue, never the actual work's outcome.
28982912
void completion.then(
28992913
() => {
29002914
if (!attempted) job.work?.remove();
29012915
working.release();
2916+
if (reported) this.reportedWakes.delete(reported.key);
29022917
},
29032918
(error) => {
29042919
if (!attempted && !this.stillCurrent(runner, job)) job.work?.remove();
29052920
working.release({ ok: false, error: message(error) });
2921+
// A wake that fails before its turn is handed over has no harness to
2922+
// report it, so it says so where its turn would have been, once until
2923+
// one of its turns starts again.
2924+
if (
2925+
!reported ||
2926+
lifetime.aborted ||
2927+
this.reportedWakes.has(reported.key) ||
2928+
!this.stillCurrent(runner, job) ||
2929+
this.host.member(agent, wakeAnchor(reported.destination)) !== true
2930+
)
2931+
return;
2932+
this.reportedWakes.add(reported.key);
2933+
const what = reported.trigger.type === "timer" ? "timer" : "watch";
2934+
const slug = reported.trigger.slug.replace(/^watch\//, "");
2935+
// A root refused as not in its channel is not replied to.
2936+
const root = rootRefused ? undefined : reported.destination.root;
2937+
void handle
2938+
.publish({
2939+
kind: 9,
2940+
content: `⚠️ My ${what} ${slug} woke me but its turn couldn't start: ${message(error).slice(0, 500)}`,
2941+
tags: [
2942+
["h", reported.destination.channel],
2943+
...(root ? [["e", root, "", "reply"]] : []),
2944+
["p", agent.owner],
2945+
],
2946+
})
2947+
.catch((failure) =>
2948+
console.warn("A failed wake could not be reported", failure),
2949+
);
29062950
},
29072951
);
29082952
const aborted = new Promise<never>((_, reject) => {

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14139,6 +14139,110 @@ it.each(
1413914139
},
1414014140
);
1414114141

14142+
describe("a timer turn that fails before it starts", () => {
14143+
const timer = (max: number, root?: string) => ({
14144+
type: "timer" as const,
14145+
interest_id: "default",
14146+
prompt: "Draft the digest",
14147+
enabled: true,
14148+
interval_secs: 60,
14149+
armed_at: 1_000,
14150+
max_occurrences: max,
14151+
expires_at: null,
14152+
channel,
14153+
...(root ? { root } : {}),
14154+
});
14155+
let error: ReturnType<typeof vi.spyOn>;
14156+
beforeEach(() => {
14157+
vi.useFakeTimers({ now: 1_000_000 });
14158+
error = vi.spyOn(console, "error").mockImplementation(() => {});
14159+
});
14160+
afterEach(() => error.mockRestore());
14161+
14162+
it.each([undefined, "e".repeat(64)])(
14163+
"is reported once in its conversation (root: %s)",
14164+
async (root) => {
14165+
const { service, run, native } = await setup();
14166+
run.mockRejectedValue(new Error("no session key"));
14167+
await service.create({ type: "example/echo", name: "Echo" });
14168+
if (root)
14169+
native.query.mockResolvedValue([
14170+
event(root, { tags: [["h", channel]] }),
14171+
]);
14172+
await service.save(bot, {
14173+
attentionEnabled: true,
14174+
attention: { "watch/weekly": timer(1, root) },
14175+
});
14176+
await vi.advanceTimersByTimeAsync(60_000);
14177+
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(1));
14178+
await vi.waitFor(async () =>
14179+
expect(await published(native, 9)).toHaveLength(1),
14180+
);
14181+
const [notice] = await published(native, 9);
14182+
expect(notice.content).toBe(
14183+
"⚠️ My timer weekly woke me but its turn couldn't start: no session key",
14184+
);
14185+
expect(notice.tags).toEqual([
14186+
["h", channel],
14187+
...(root ? [["e", root, "", "reply"]] : []),
14188+
["p", viewer],
14189+
]);
14190+
// The failed occurrence stays spent; nothing retries it.
14191+
await vi.advanceTimersByTimeAsync(180_000);
14192+
expect(run).toHaveBeenCalledTimes(1);
14193+
expect(await published(native, 9)).toHaveLength(1);
14194+
expect(service.find(bot)?.timers["watch/weekly"]?.used).toBe(1);
14195+
},
14196+
);
14197+
14198+
it("reports a repeat again only after one of its turns started", async () => {
14199+
const { service, run, native } = await setup();
14200+
run
14201+
.mockRejectedValueOnce(new Error("first"))
14202+
.mockRejectedValueOnce(new Error("second"))
14203+
.mockResolvedValueOnce(undefined)
14204+
.mockRejectedValueOnce(new Error("fourth"));
14205+
await service.create({ type: "example/echo", name: "Echo" });
14206+
await service.save(bot, {
14207+
attentionEnabled: true,
14208+
attention: { "watch/weekly": timer(4) },
14209+
});
14210+
for (let occurrence = 1; occurrence <= 4; occurrence++) {
14211+
await vi.advanceTimersByTimeAsync(60_000);
14212+
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(occurrence));
14213+
}
14214+
await vi.advanceTimersByTimeAsync(1_000);
14215+
const notices = (await published(native, 9)).map(({ content }) => content);
14216+
expect(notices).toEqual([
14217+
"⚠️ My timer weekly woke me but its turn couldn't start: first",
14218+
"⚠️ My timer weekly woke me but its turn couldn't start: fourth",
14219+
]);
14220+
});
14221+
14222+
it("is reported at the channel level when its root is not in the channel", async () => {
14223+
const { service, run, native } = await setup();
14224+
await service.create({ type: "example/echo", name: "Echo" });
14225+
native.query.mockResolvedValue([]);
14226+
await service.save(bot, {
14227+
attentionEnabled: true,
14228+
attention: { "watch/weekly": timer(1, "e".repeat(64)) },
14229+
});
14230+
await vi.advanceTimersByTimeAsync(60_000);
14231+
await vi.waitFor(async () =>
14232+
expect(await published(native, 9)).toHaveLength(1),
14233+
);
14234+
expect(run).not.toHaveBeenCalled();
14235+
const [notice] = await published(native, 9);
14236+
expect(notice.content).toBe(
14237+
"⚠️ My timer weekly woke me but its turn couldn't start: The delivery root is not in its channel",
14238+
);
14239+
expect(notice.tags).toEqual([
14240+
["h", channel],
14241+
["p", viewer],
14242+
]);
14243+
});
14244+
});
14245+
1414214246
it("delivers matched events to their configured address rather than the listening channel", async () => {
1414314247
const runOnce = vi.fn(async () => ({ ok: true }));
1414414248
const { service, run, emit, native, session, activity } = await setup({

0 commit comments

Comments
 (0)