Skip to content

Commit 2e36587

Browse files
Honeyclaude
andcommitted
fix(agents2): keep agents running when another community is selected
Agents2 listened only to the selected community, and the Claude Code and Codex runtimes stopped the processes of agents that left that list, so switching communities killed work mid-turn and left the other community unheard. Agents2 now binds each joined community the viewer has an agent in (opened through a new Communities.open, without selecting it), delivers each community's live events only to its own agents, and exposes the running agents and each agent's community connection. The runtimes keep processes for every running agent and read conversations, names and memory from the agent's own community. Processes stop when the agent is deleted, its community is left or disconnected, or the viewer signs out. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Honey <8e307ae0076a4dab6b94b036ea3edc7e08f823a625269c1e6919e881a048b4d2@buzz.block.builderlab.xyz>
1 parent f8705a9 commit 2e36587

17 files changed

Lines changed: 382 additions & 174 deletions

File tree

‎docs/communities.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ community origin and viewer; channel IDs alone are not sufficient keys.
114114
## Session lifetime
115115

116116
Selecting a saved membership lazily acquires its session. Sessions survive page
117-
navigation and switching; startup opens only the selected one. Opened communities
117+
navigation and switching; startup opens only the selected one, plus each joined
118+
community where the viewer has an Agents2 agent, which `open` connects without
119+
selecting so its agents keep running. Opened communities
118120
retain their existing per-session query/cache budgets. Arbitrary destinations mean
119121
there is **no longer a two-session maximum**: retained session count grows with
120122
communities opened until app disposal. This slice does not add background eviction

‎src/app/services.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function createServices() {
7676
subscribe: communities.subscribe,
7777
});
7878
const relay = communities.relay;
79-
const agents2 = new Agents2Service(ctx, relay);
79+
const agents2 = new Agents2Service(ctx, communities);
8080
ctx.effect(() => bindAgentMentions(agentControl, communities));
8181
ctx.effect(() => bindTeamTextSync(agentControl, communities));
8282
const notifications = new NotificationsService(

‎src/bundled/agents2/Agents2Page.test.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const agent = (name: string, n: number): Agent => ({
4949
});
5050

5151
function setup(agents: Agent[], { shown = true } = {}) {
52-
let snapshot: AgentsSnapshot = { status: "ready", agents };
52+
let snapshot: AgentsSnapshot = { status: "ready", agents, running: agents };
5353
const listeners = new Set<() => void>();
5454
const type = {
5555
key: "test/echo",
@@ -73,6 +73,7 @@ function setup(agents: Agent[], { shown = true } = {}) {
7373
return () => listeners.delete(listener);
7474
},
7575
find: (pubkey: string) => snapshot.agents.find((a) => a.pubkey === pubkey),
76+
relay: () => undefined,
7677
register: vi.fn(),
7778
create: vi.fn(async () => {
7879
// `shown: false`: it was made for a community no longer selected.

‎src/bundled/builderlab/index.test.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
66
import userEvent from "@testing-library/user-event";
77
import { HostService } from "../../features/host/service";
88
import { SettingsCardsService } from "../../features/settings/service";
9-
import { Agents2Service } from "../../features/agents2/service";
9+
import {
10+
Agents2Service,
11+
type AgentCommunities,
12+
} from "../../features/agents2/service";
1013
import type { RelayData } from "../../features/relay/service";
1114
import { Agents2Page } from "../agents2/Agents2Page";
1215
import * as builderlab from "./index";
@@ -42,7 +45,12 @@ afterEach(() => {
4245
});
4346

4447
function provideAgents2(root: Context, relay: RelayData) {
45-
return new Agents2Service(root, relay, {
48+
const communities = {
49+
snapshot: () => ({ memberships: [], selected: null }),
50+
subscribe: () => () => {},
51+
open: () => relay,
52+
} as unknown as AgentCommunities;
53+
return new Agents2Service(root, communities, {
4654
list: async () => [],
4755
create: vi.fn(),
4856
rename: vi.fn(),

‎src/bundled/claude-code/index.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@ export const TYPE_ID = "claude-code";
1111
/** A turn's run returns once Claude has it, so this bounds only the hand-over. */
1212
const HANDOVER_MS = 2 * 60_000;
1313

14-
export const inject = ["agents2", "host", "relay"];
14+
export const inject = ["agents2", "host"];
1515
export const apply: PluginModule["apply"] = (ctx) => {
1616
// Only the desktop app can start `claude`, and setup and file uploads run
1717
// `bash` and `base64`, which stock Windows lacks; no unusable type elsewhere.
1818
if (!isTauri() || !/Mac|Linux/i.test(navigator.platform)) return;
19-
const { agents2, host, relay } = ctx;
19+
const { agents2, host } = ctx;
2020
const typeKey = `${ctx.pluginOwner?.id ?? "buzz.claude-code"}/${TYPE_ID}`;
2121
const spawn = host.spawn?.bind(host);
2222
const setup = new ClaudeSetup(spawn);
23-
const runtime = new ClaudeRuntime(host, relay);
23+
const runtime = new ClaudeRuntime(host, agents2.relay);
2424
const sync = () => {
2525
const snapshot = agents2.snapshot();
26-
// Agents leaving view, the app signing out included, stop their processes.
26+
// Agents that stop running, the app signing out included, stop their
27+
// processes; switching communities does not.
2728
if (snapshot.status === "loading") return;
28-
runtime.sync(snapshot.agents.filter((agent) => agent.type === typeKey));
29+
runtime.sync(snapshot.running.filter((agent) => agent.type === typeKey));
2930
};
30-
// Agents come and go with the community; a spare session is warmed for each.
31+
// A spare session is warmed for each running agent.
3132
if (spawn) {
3233
const unsubscribe = agents2.subscribe(sync);
3334
sync();

‎src/bundled/claude-code/runtime.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function setup(thread: readonly EventData[] = []) {
5858
getItem: (key: string) => data.get(key) ?? null,
5959
setItem: (key: string, value: string) => void data.set(key, value),
6060
} as Storage;
61-
const runtime = new ClaudeRuntime(host, relay, storage);
61+
const runtime = new ClaudeRuntime(host, () => relay, storage);
6262
const published: { kind: number; content: string; tags: string[][] }[] = [];
6363
const publish = vi.fn(async (template: (typeof published)[number]) => {
6464
published.push(template);

‎src/bundled/claude-code/runtime.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export class ClaudeRuntime {
8080

8181
constructor(
8282
private readonly host: Host,
83-
private readonly relay: RelayData,
83+
/** The owner's connection to each agent's own community. */
84+
private readonly relay: (pubkey: string) => RelayData | undefined,
8485
private readonly storage: Storage = globalThis.localStorage,
8586
) {}
8687

@@ -95,7 +96,7 @@ export class ClaudeRuntime {
9596
* session, changed settings restart idle sessions, removed agents stop. */
9697
sync(agents: readonly Agent[]) {
9798
const current = new Set(agents.map((agent) => agent.pubkey));
98-
// Out of view (another community, offline) or deleted: either way its
99+
// Deleted, signed out or its community disconnected: either way its
99100
// processes stop. Saved sessions stay so it resumes when it is back.
100101
for (const [pubkey, entry] of this.agents)
101102
if (!current.has(pubkey)) {
@@ -242,8 +243,8 @@ export class ClaudeRuntime {
242243
channelId: string,
243244
fresh = false,
244245
) {
245-
const snapshot = this.relay.snapshot();
246-
const session = snapshot.status === "ready" ? snapshot.session : undefined;
246+
const snapshot = this.relay(self)?.snapshot();
247+
const session = snapshot?.status === "ready" ? snapshot.session : undefined;
247248
const summary =
248249
session?.channels.list().channels.find((item) => item.id === channelId) ??
249250
session?.channels.get?.(channelId);
@@ -406,8 +407,8 @@ export class ClaudeRuntime {
406407
return reading;
407408
}
408409
private async readMemories(pubkey: string): Promise<readonly Memory[]> {
409-
const snapshot = this.relay.snapshot();
410-
if (snapshot.status !== "ready") throw new Error("Buzz is not connected");
410+
const snapshot = this.relay(pubkey)?.snapshot();
411+
if (snapshot?.status !== "ready") throw new Error("Buzz is not connected");
411412
const view = snapshot.session.agentMemories.open(pubkey);
412413
try {
413414
const done = new Promise<void>((resolve) => {

‎src/bundled/claude-code/tabs.test.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ function applied({ loggedIn = true } = {}) {
6161
const ctx = {
6262
pluginOwner: { id: "buzz.claude-code", revision: "r1" },
6363
host: { spawn, request: vi.fn() },
64-
relay: { snapshot: () => ({ status: "signed-out" }) },
6564
agents2: {
6665
register: (registered: AgentType<Config>) => {
6766
type = registered;
6867
},
69-
snapshot: () => ({ status: "ready", agents: [] }),
68+
snapshot: () => ({ status: "ready", agents: [], running: [] }),
69+
relay: () => undefined,
7070
subscribe: () => () => undefined,
7171
},
7272
effect: vi.fn(),
@@ -91,7 +91,7 @@ async function choose(label: string, option: string) {
9191
}
9292

9393
it("is an agent type on the desktop app", () => {
94-
expect(inject).toEqual(["agents2", "host", "relay"]);
94+
expect(inject).toEqual(["agents2", "host"]);
9595
const { type } = installed();
9696
expect(type).toMatchObject({ id: "claude-code", title: "Claude Code" });
9797
expect(type.defaults()).toEqual({ config: DEFAULT_CONFIG });

‎src/bundled/codex/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ current turn finishes. Expect the marker.
4747
Thread scope is the default. Channel scope shares a session across channel
4848
threads; DMs always share a conversation. Workspace changes start a fresh session.
4949
Bindings persist locally under the community and agent identity. Earlier bindings without dynamic tools are ignored; new conversations start fresh. Switching
50-
communities, removing an agent, or disabling the plugin ends its server.
50+
communities does not end its server; removing the agent, leaving or disconnecting
51+
from its community, or disabling the plugin does.
5152
If a saved Codex thread no longer exists, the next mention starts a fresh one.
5253
To inspect a saved session, see [View a Codex session](../../../docs/view-codex-session.md).
5354

‎src/bundled/codex/index.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ function applied() {
2323
react: React,
2424
pluginOwner: { id: "buzz.codex", revision: "r1" },
2525
host: { spawn: vi.fn(), request: vi.fn() },
26-
relay: { snapshot: () => ({ status: "signed-out" }), subscribe },
27-
communityReader: { snapshot: () => ({}), subscribe },
2826
agents2: {
2927
register: (registered: AgentType) => {
3028
type = registered;
3129
},
32-
snapshot: () => ({ status: "ready", agents: [] }),
30+
snapshot: () => ({ status: "ready", agents: [], running: [] }),
31+
relay: () => undefined,
3332
subscribe,
3433
},
3534
effect: vi.fn(),

0 commit comments

Comments
 (0)