Skip to content

Commit 913de10

Browse files
committed
fix(beam): restart delivery when the receiver changes
1 parent ee6dfc3 commit 913de10

5 files changed

Lines changed: 44 additions & 30 deletions

File tree

‎docs/plugins/beam.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Beam can also act as the sender: an opt-in mirror that continuously publishes th
153153
}
154154
```
155155

156-
- `endpoint` (required): the final remote receiver URL. Redirect responses (301, 302, 303, 307, and 308) are not followed; configure the destination URL directly. After a redirect, repeated polls are suppressed for the current mirror service instance. A Gateway restart probes the configured endpoint once again so a receiver corrected at the same URL can recover. HTTPS is enforced for non-loopback hosts; plaintext `http://` is accepted only for `localhost`/`127.0.0.1`/`::1` development.
156+
- `endpoint` (required): the final remote receiver URL. Changing it starts fresh delivery to that receiver, including unchanged active sessions; pending terminal retries for the previous receiver are discarded, leaving its rows to expire normally. Redirect responses (301, 302, 303, 307, and 308) are not followed; configure the destination URL directly. After a redirect, repeated polls are suppressed for the current mirror service instance. A Gateway restart probes the configured endpoint once again so a receiver corrected at the same URL can recover. HTTPS is enforced for non-loopback hosts; plaintext `http://` is accepted only for `localhost`/`127.0.0.1`/`::1` development.
157157
- `token`: Gateway credential for the remote receiver, sent as `Authorization: Bearer`. Accepts a plain string or a secret reference; a configured-but-unresolved token pauses mirroring instead of sending unauthenticated requests. Deployments fronted by an identity-aware proxy need an ingress that accepts this bearer credential.
158158
- `catalogs` (required): the session catalog ids to mirror, as explicit per-catalog consent — an omitted or empty list mirrors nothing. The local `beam` receiver catalog is always excluded so two mirrored Gateways cannot re-mirror each other's rows.
159159
- `pollSeconds` (default 30, minimum 10): how often the mirror scans local catalogs.

‎extensions/beam/src/beam.test-support.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const beamTestNow = Date.parse("2026-07-27T12:00:00.000Z");
1010
export const beamTestLogger = { warn: () => {}, info: () => {} };
1111
export type BeamTestSession = { threadId: string; name?: string; recencyAt: number };
1212

13-
export function beamTestMirrorConfig(overrides: Record<string, unknown> = {}): unknown {
13+
export function beamTestMirrorConfig(overrides: Record<string, unknown> = {}) {
1414
return {
1515
plugins: {
1616
entries: {

‎extensions/beam/src/mirror.test.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,15 @@ describe("createBeamMirrorRunner", () => {
341341
const runtime = {
342342
config: { current: () => beamTestMirrorConfig({ endpoint }) },
343343
} as unknown as PluginRuntime;
344+
let active = true;
344345
const createRunner = () =>
345346
createBeamTestRunner({
346347
runtime,
347-
listCatalogs: () => [createBeamTestCatalog()],
348+
listCatalogs: () => [
349+
createBeamTestCatalog({
350+
sessions: () => (active ? [{ threadId: "t1", recencyAt: beamTestNow }] : []),
351+
}),
352+
],
348353
});
349354
const runner = createRunner();
350355

@@ -354,8 +359,24 @@ describe("createBeamMirrorRunner", () => {
354359
await restartedRunner.tick();
355360
endpoint = `${origin}/direct`;
356361
await restartedRunner.tick();
357-
362+
await restartedRunner.tick();
363+
endpoint = `${origin}/another-receiver`;
364+
active = false;
365+
await restartedRunner.tick();
358366
expect(requests).toEqual(["/redirecting", "/redirecting", "/direct"]);
367+
active = true;
368+
await restartedRunner.tick();
369+
await restartedRunner.tick();
370+
endpoint = `${origin}/direct`;
371+
await restartedRunner.tick();
372+
373+
expect(requests).toEqual([
374+
"/redirecting",
375+
"/redirecting",
376+
"/direct",
377+
"/another-receiver",
378+
"/direct",
379+
]);
359380
} finally {
360381
await closeTestServer(server);
361382
}

‎extensions/beam/src/mirror.ts‎

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,10 @@ function boundedNumber(value: unknown, fallback: number, min: number, max: numbe
7575
}
7676

7777
/** Returns the mirror config, undefined when mirroring is not configured, or an error string. */
78-
export function parseBeamMirrorConfig(config: unknown): BeamMirrorConfig | undefined | string {
79-
if (!isRecord(config)) {
80-
return undefined;
81-
}
82-
const plugins = isRecord(config.plugins) ? config.plugins : undefined;
83-
const entries = isRecord(plugins?.entries) ? plugins.entries : undefined;
84-
const entry = isRecord(entries?.beam) ? entries.beam : undefined;
85-
const pluginConfig = isRecord(entry?.config) ? entry.config : undefined;
86-
const mirror = pluginConfig?.mirror;
78+
export function parseBeamMirrorConfig(
79+
config: ReturnType<PluginRuntime["config"]["current"]>,
80+
): BeamMirrorConfig | undefined | string {
81+
const mirror = config.plugins?.entries?.beam?.config?.mirror;
8782
if (mirror === undefined) {
8883
return undefined;
8984
}
@@ -235,7 +230,8 @@ export function createBeamMirrorRunner(params: {
235230
const { signal } = controller;
236231
let lastWarnAt = 0;
237232
let warnedProcessHomeIsolation = false;
238-
let redirectBlockedEndpoint: string | undefined;
233+
let endpoint = "";
234+
let redirectBlocked = false;
239235
let activeTick: Promise<void> | undefined;
240236
let stopPromise: Promise<void> | undefined;
241237
const stopError = new Error("Beam mirror stopped");
@@ -289,15 +285,10 @@ export function createBeamMirrorRunner(params: {
289285
}
290286
};
291287

292-
const upload = async (
293-
endpoint: string,
294-
token: string | undefined,
295-
payload: BeamUpload,
296-
): Promise<boolean> => {
297-
if (signal.aborted || redirectBlockedEndpoint === endpoint) {
288+
const upload = async (token: string | undefined, payload: BeamUpload): Promise<boolean> => {
289+
if (signal.aborted || redirectBlocked) {
298290
return false;
299291
}
300-
redirectBlockedEndpoint = undefined;
301292

302293
let guarded: Awaited<ReturnType<typeof fetchWithSsrFGuard>>;
303294
try {
@@ -326,7 +317,7 @@ export function createBeamMirrorRunner(params: {
326317
// Repeating the same poll cannot satisfy direct-only delivery. Hold this exact
327318
// endpoint for this service instance; a fresh instance probes once so a receiver
328319
// fixed in place can recover without a meaningless config change.
329-
redirectBlockedEndpoint = endpoint;
320+
redirectBlocked = true;
330321
params.logger.warn(
331322
`beam mirror upload blocked for ${payload.source}: receiver returned redirect (${error.status}); redirects are not followed; configure the final endpoint`,
332323
);
@@ -340,9 +331,8 @@ export function createBeamMirrorRunner(params: {
340331
signal.throwIfAborted();
341332
if (!response.ok) {
342333
warnThrottled(`beam mirror upload failed (${response.status}) for ${payload.source}`);
343-
return false;
344334
}
345-
return true;
335+
return response.ok;
346336
} finally {
347337
// The mirror uses only the status; cancel the ignored payload so slow
348338
// receiver responses cannot retain connection slots across poll retries.
@@ -396,6 +386,12 @@ export function createBeamMirrorRunner(params: {
396386
warnThrottled(`beam mirror disabled: ${mirror}`);
397387
return;
398388
}
389+
if (endpoint !== mirror.endpoint) {
390+
// Acknowledgements, terminal retries, and redirect blocks belong to one receiver.
391+
endpoint = mirror.endpoint;
392+
tracked.clear();
393+
redirectBlocked = false;
394+
}
399395
let agentId: string;
400396
try {
401397
agentId = resolveSessionAgentIdsStrict({ config: config as OpenClawConfig }).defaultAgentId;
@@ -489,7 +485,7 @@ export function createBeamMirrorRunner(params: {
489485
if (tracked.get(key)?.fingerprint === fingerprint) {
490486
continue;
491487
}
492-
const uploaded = await upload(mirror.endpoint, token, payload);
488+
const uploaded = await upload(token, payload);
493489
signal.throwIfAborted();
494490
if (uploaded) {
495491
trackSuccessfulUpload(key, candidate, fingerprint);
@@ -529,7 +525,7 @@ export function createBeamMirrorRunner(params: {
529525
try {
530526
const payload = await buildUpload(agentId, catalog, entry.candidate, true);
531527
signal.throwIfAborted();
532-
uploaded = await upload(mirror.endpoint, token, payload);
528+
uploaded = await upload(token, payload);
533529
signal.throwIfAborted();
534530
} catch {
535531
signal.throwIfAborted();

‎ui/src/pages/chat/route-loader-catalog-share.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,9 @@ export async function loadCatalogShareRouteFromLocation(
116116
face: "chat",
117117
};
118118
}
119-
if (matches.length === 0) {
120-
return missingSessionRouteData(context, "chat", agentId);
121-
}
122119
const session = matches[0];
123120
if (!session) {
124-
return routeError(t("chat.sessionRoute.catalogShareUnavailable"));
121+
return missingSessionRouteData(context, "chat", agentId);
125122
}
126123
const pathname = buildControlUiCatalogSharePath({
127124
shareRoute,

0 commit comments

Comments
 (0)