Skip to content

Commit 11d731a

Browse files
baxenclaude
andcommitted
feat(agents2): react πŸ‘€ πŸ’¬ to a mention while its agent works
Until Agents2 agents show as typing, the host reacts to a mention as the agent with πŸ‘€ and πŸ’¬ when its run starts, and deletes both once the run and the work it handed off have settled (at most the longest run allowed). Plugins return from `run` early and finish their turns in the background, so `Delivery.workingUntil(done)` lets them say when that work settles. Claude Code and Codex now call it. The reactions live in working-reactions.ts and its one call in the service, so they can be deleted once typing indicators ship; `workingUntil` stays for those. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Bradley Axen <[email protected]>
1 parent 261db4c commit 11d731a

7 files changed

Lines changed: 203 additions & 8 deletions

File tree

β€Žsrc/bundled/claude-code/runtime.test.tsβ€Ž

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,29 @@ function setup(thread: readonly EventData[] = []) {
7777
upload: vi.fn(),
7878
remember: vi.fn(),
7979
};
80+
const workingUntil = vi.fn<Delivery["workingUntil"]>();
8081
const deliver = (trigger: Delivery["trigger"]) =>
8182
runtime.run({
8283
trigger,
8384
channelId: channel,
8485
agent,
8586
config: DEFAULT_CONFIG,
8687
signal: new AbortController().signal,
88+
workingUntil,
8789
} as Delivery);
8890
const claudes = () =>
8991
fake.processes.filter((process) => process.id === "claude");
90-
return { runtime, deliver, published, claudes, fake, read, data, agent };
92+
return {
93+
runtime,
94+
deliver,
95+
published,
96+
claudes,
97+
fake,
98+
read,
99+
data,
100+
agent,
101+
workingUntil,
102+
};
91103
}
92104

93105
it("hands a mention to its thread's session", async () => {
@@ -201,6 +213,7 @@ it("runs a mention on the model and effort its delivery picked", async () => {
201213
model: "haiku",
202214
effort: "low",
203215
signal: new AbortController().signal,
216+
workingUntil: () => undefined,
204217
} as Delivery);
205218
await flush(10);
206219
const claude = claudes().find((process) => process.prompts.length);
@@ -352,7 +365,7 @@ it("says so in the thread when a turn fails", async () => {
352365
["p", self],
353366
],
354367
});
355-
const { runtime, deliver, published, claudes } = setup();
368+
const { runtime, deliver, published, claudes, workingUntil } = setup();
356369
runtime.sync([{ pubkey: self, config: DEFAULT_CONFIG } as Agent]);
357370
await flush(10);
358371
const [spare] = claudes();
@@ -361,8 +374,16 @@ it("says so in the thread when a turn fails", async () => {
361374
await deliver({ type: "mention", event: mention });
362375
await flush(10);
363376
expect(spare.prompts).toHaveLength(1);
377+
// The host counts the agent as working until the turn it handed off ends.
378+
let settled = false;
379+
const done = workingUntil.mock.calls[0]?.[0];
380+
if (!done) throw new Error("the turn was not handed to workingUntil");
381+
void Promise.resolve(done).then(() => (settled = true));
382+
await flush(10);
383+
expect(settled).toBe(false);
364384
spare.exit(1, "Error: Invalid API key\n");
365385
await flush(10);
386+
expect(settled).toBe(true);
366387
const report = published.find((event) => event.kind === 9);
367388
expect(report?.content).toBe(
368389
"⚠️ I couldn't finish that: Error: Invalid API key",
@@ -421,6 +442,7 @@ it("acts only on its owner's messages unless told to answer anyone", async () =>
421442
},
422443
config: { ...DEFAULT_CONFIG, respondTo: "anyone" },
423444
signal: new AbortController().signal,
445+
workingUntil: () => undefined,
424446
} as unknown as Delivery);
425447
await flush(10);
426448
expect(claudes().some((process) => process.prompts.length)).toBe(true);

β€Žsrc/bundled/claude-code/runtime.tsβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export class ClaudeRuntime {
225225
channelId,
226226
model,
227227
effort,
228+
workingUntil,
228229
}: Delivery) {
229230
const settings = config(raw);
230231
if (
@@ -250,6 +251,7 @@ export class ClaudeRuntime {
250251
}),
251252
0,
252253
);
254+
workingUntil(done);
253255
this.watch(done, () => undefined);
254256
return;
255257
}
@@ -284,6 +286,8 @@ export class ClaudeRuntime {
284286
fresh: async () => (await prompt(true)).text,
285287
pick: { ...(model ? { model } : {}), ...(effort ? { effort } : {}) },
286288
});
289+
// The turn outlives `run`, which the host is told so it shows it as working.
290+
workingUntil(done);
287291
this.notify();
288292
this.watch(done, async (error) => {
289293
if (error) await this.report(agent, channel, event, error);

β€Žsrc/bundled/codex/runtime.test.tsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function fixture(
230230
size: 12,
231231
type: mime,
232232
}));
233+
const workingUntil = vi.fn<Delivery["workingUntil"]>();
233234
const agent = {
234235
pubkey,
235236
owner,
@@ -266,6 +267,7 @@ function fixture(
266267
instructions: "TEST_INSTRUCTIONS",
267268
},
268269
signal: new AbortController().signal,
270+
workingUntil,
269271
};
270272
if (result.trigger.type !== "timer")
271273
events.push(result.trigger.event as EventData);
@@ -383,6 +385,7 @@ function fixture(
383385
read,
384386
storage,
385387
delivery,
388+
workingUntil,
386389
starts,
387390
complete,
388391
interrupt,
@@ -701,8 +704,17 @@ it("serializes owner follow-ups and routes tool replies to the latest accepted r
701704
await vi.waitFor(() =>
702705
expect(f.sent.filter((w) => w.method === "turn/steer")).toHaveLength(2),
703706
);
707+
// Each mention counts as working until the turn it was steered into ends.
708+
let settled = 0;
709+
for (const [done] of f.workingUntil.mock.calls)
710+
void Promise.resolve(done).then(() => settled++);
711+
expect(f.workingUntil).toHaveBeenCalledTimes(3);
712+
// A macrotask lets every pending promise chain finish first.
713+
await new Promise((resolve) => setTimeout(resolve, 0));
714+
expect(settled).toBe(0);
704715
f.complete();
705716
await vi.waitFor(() => expect(f.publish).toHaveBeenCalledTimes(1));
717+
await vi.waitFor(() => expect(settled).toBe(3));
706718
const steering = f.sent.filter((wire) => wire.method === "turn/steer");
707719
expect(JSON.stringify(steering[0]?.params)).toContain("FIRST_STEER");
708720
expect(JSON.stringify(steering[1]?.params)).toContain("SECOND_STEER");

β€Žsrc/bundled/codex/runtime.tsβ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,12 @@ export class CodexRuntime {
411411
active.steering = accepted.catch((error) =>
412412
console.error("Codex steering failed", error),
413413
);
414+
// The steered turn, or one started in its place, answers this mention.
415+
const steered = lane;
416+
delivery.workingUntil(accepted.then(() => steered.tail));
414417
return;
415418
}
416-
this.start(request, text, key, lane, entry);
419+
delivery.workingUntil(this.start(request, text, key, lane, entry));
417420
}
418421
private start(
419422
request: Request,
@@ -437,6 +440,7 @@ export class CodexRuntime {
437440
.finally(() => {
438441
if (current.tail === job && !current.active) entry.lanes.delete(key);
439442
});
443+
return job;
440444
}
441445
private async history(request: Request, signal: AbortSignal) {
442446
signal.throwIfAborted();

β€Žsrc/features/agents2/service.test.tsβ€Ž

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { RelayEvent } from "../relay/events";
44
import type { LiveBatch, LiveListener } from "../relay/incoming";
55
import type { RelayData, RelaySnapshot } from "../relay/service";
66
import type { RelaySession } from "../relay/session";
7-
import type { AgentIdentity, AgentsNative } from "./native";
7+
import type { AgentEventTemplate, AgentIdentity, AgentsNative } from "./native";
88
import {
99
Agents2Service,
1010
type AgentCommunities,
@@ -231,8 +231,13 @@ function fakeNative(identities: AgentIdentity[] = []) {
231231
steps.push("forget");
232232
identities.splice(at(pubkey), 1);
233233
}),
234-
publish: vi.fn(async (pubkey: string, template: { kind: number }) =>
235-
event(`p${++published}`, { pubkey, kind: template.kind }),
234+
publish: vi.fn(async (pubkey: string, template: AgentEventTemplate) =>
235+
event(`p${++published}`, {
236+
pubkey,
237+
kind: template.kind,
238+
content: template.content,
239+
tags: (template.tags ?? []).map((tag) => [...tag]),
240+
}),
236241
),
237242
publishProfile: vi.fn(async (_: string) => {}),
238243
query: vi.fn(async () => []),
@@ -325,6 +330,14 @@ async function setup({
325330
* microtask queue is empty, so this does not depend on how fast runners are. */
326331
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
327332

333+
/** The events native published as an agent, of `kind`, in order. */
334+
async function published(native: ReturnType<typeof fakeNative>, kind: number) {
335+
const events = await Promise.all(
336+
native.publish.mock.results.map(({ value }) => value),
337+
);
338+
return events.filter((event) => event.kind === kind);
339+
}
340+
328341
beforeEach(() => {
329342
vi.useRealTimers();
330343
steps.length = 0;
@@ -415,6 +428,55 @@ it("delivers mentions and matching watches once, and never the agent's own event
415428
expect(first?.config).toEqual({ reply: "ok" });
416429
});
417430

431+
it("reacts πŸ‘€ and πŸ’¬ to a mention until the work its run handed off settles", async () => {
432+
const { service, run, emit, native } = await setup();
433+
await service.create({ type: "example/echo", name: "Echo" });
434+
let finish = () => {};
435+
const turn = new Promise<void>((resolve) => {
436+
finish = resolve;
437+
});
438+
run.mockImplementationOnce(async ({ workingUntil }) => workingUntil(turn));
439+
const mention = event("m", {
440+
tags: [
441+
["h", channel],
442+
["p", bot],
443+
],
444+
});
445+
emit({ events: [mention], channelId: channel });
446+
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(1));
447+
await settle();
448+
const reactions = await published(native, 7);
449+
expect(reactions.map(({ content, tags }) => [content, tags])).toEqual(
450+
["πŸ‘€", "πŸ’¬"].map((emoji) => [
451+
emoji,
452+
[
453+
["h", channel],
454+
["e", mention.id],
455+
],
456+
]),
457+
);
458+
// `run` has returned, but the turn it handed off is still going.
459+
expect(await published(native, 5)).toEqual([]);
460+
finish();
461+
await vi.waitFor(async () =>
462+
expect(await published(native, 5)).toHaveLength(1),
463+
);
464+
expect((await published(native, 5))[0]?.tags).toEqual([
465+
["h", channel],
466+
...reactions.map(({ id }) => ["e", id]),
467+
["k", "7"],
468+
]);
469+
// Only mentions are reacted to, and a failed run takes its reactions back.
470+
run.mockRejectedValueOnce(new Error("boom"));
471+
emit({ events: [event("m2", { tags: mention.tags })], channelId: channel });
472+
emit({ events: [event("w", { content: "deploy" })], channelId: channel });
473+
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(3));
474+
await vi.waitFor(async () =>
475+
expect(await published(native, 5)).toHaveLength(2),
476+
);
477+
expect(await published(native, 7)).toHaveLength(4);
478+
});
479+
418480
it("treats a reply to something the agent published as addressed", async () => {
419481
const { service, run, emit } = await setup();
420482
await service.create({ type: "example/echo", name: "Echo" });
@@ -736,7 +798,7 @@ it("is not woken by reactions, deletions or DMs", async () => {
736798
});
737799

738800
it("does not let one owner's agents wake each other by replying", async () => {
739-
const { service, run, emit } = await setup();
801+
const { service, run, emit, native } = await setup();
740802
await service.create({ type: "example/echo", name: "Echo" });
741803
await service.create({ type: "example/echo", name: "Sibling" });
742804
run.mockImplementation(async ({ agent, trigger }) => {
@@ -753,7 +815,7 @@ it("does not let one owner's agents wake each other by replying", async () => {
753815
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(1));
754816
// The sibling replies to Echo's message, naming it, and also says "deploy",
755817
// which Echo's watch matches. Neither wakes Echo.
756-
const echoMessage = "p1".padEnd(64, "0");
818+
const echoMessage = (await published(native, 9))[0]?.id;
757819
emit({
758820
events: [
759821
event("reply", {

β€Žsrc/features/agents2/service.tsβ€Ž

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
type AgentRecord,
5353
type SkippedObject,
5454
} from "./store";
55+
import { reactWhileWorking } from "./working-reactions";
5556

5657
/** One of the viewer's agents, in the community at `relay`. */
5758
export type Agent<Config = unknown> = Readonly<{
@@ -145,6 +146,9 @@ export type Delivery<Config = unknown> = Readonly<{
145146
/** Aborts on the run's deadline, or when the agent is removed or its type is
146147
* replaced. Advisory: the handle keeps working, so finishing up is harmless. */
147148
signal: AbortSignal;
149+
/** Keeps the run counted as working after `run` returns, until `done`
150+
* settles, for a type that hands its work off and returns early. */
151+
workingUntil(done: PromiseLike<unknown>): void;
148152
}>;
149153
export type Effort = Readonly<{ id: string; label: string }>;
150154
/** A model with its effort levels, lowest first. */
@@ -1154,6 +1158,7 @@ export class Agents2Service extends Service implements Agents2 {
11541158
remember: (slug: string, body: string, after: number) =>
11551159
this.require().remember(agent.pubkey, slug, body, after),
11561160
});
1161+
const working = this.working(runner, job);
11571162
try {
11581163
await Promise.race([
11591164
Promise.resolve().then(async () => {
@@ -1167,6 +1172,7 @@ export class Agents2Service extends Service implements Agents2 {
11671172
config: agent.config,
11681173
...pick,
11691174
signal,
1175+
workingUntil: working.hold,
11701176
}),
11711177
);
11721178
}),
@@ -1180,11 +1186,50 @@ export class Agents2Service extends Service implements Agents2 {
11801186
} catch (error) {
11811187
if (!lifetime.aborted)
11821188
console.error(`Agent run failed: ${agent.name}`, error);
1189+
} finally {
1190+
working.release();
11831191
}
11841192
}
11851193
runner.running = false;
11861194
}
11871195

1196+
/** Counts a run as working until it and everything it handed to
1197+
* `workingUntil` have settled, or the longest run allowed has passed. A
1198+
* mention run is reacted to meanwhile. */
1199+
private working(runner: Runner, { trigger, channelId }: Job) {
1200+
const native = this.native;
1201+
const event = trigger.type === "mention" ? trigger.event : undefined;
1202+
const channel =
1203+
event && (channelId ?? event.tags.find((tag) => tag[0] === "h")?.[1]);
1204+
const done =
1205+
native && event && channel
1206+
? reactWhileWorking(
1207+
(template) => native.publish(runner.pubkey, template),
1208+
event,
1209+
channel,
1210+
)
1211+
: undefined;
1212+
let holds = 1;
1213+
const stop = () => {
1214+
if (holds < 0) return;
1215+
holds = -1;
1216+
clearTimeout(timer);
1217+
done?.();
1218+
};
1219+
const timer = setTimeout(stop, TIMEOUT_LIMIT_MS);
1220+
const release = () => {
1221+
if (holds > 0 && --holds === 0) stop();
1222+
};
1223+
return {
1224+
hold(work: PromiseLike<unknown>) {
1225+
if (holds <= 0) return;
1226+
holds++;
1227+
Promise.resolve(work).then(release, release);
1228+
},
1229+
release,
1230+
};
1231+
}
1232+
11881233
private async claim(runner: Runner) {
11891234
try {
11901235
runner.mayHold = (await this.native?.claim(runner.pubkey)) ?? false;

0 commit comments

Comments
Β (0)