Skip to content

Commit b287d63

Browse files
authored
fix: clear stale binding owners and restore global subagent status (#133578)
* fix: clear stale binding owners and restore global subagent status * test: keep locale navigation checks independent of release dates
1 parent 621ef9e commit b287d63

24 files changed

Lines changed: 501 additions & 194 deletions

‎docs/plugins/sdk-channel-plugins.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ For activity updates, use `service.touch(binding.bindingId, at, binding.conversa
429429
Omit scope only for intentional global cleanup or an existing legacy cross-channel
430430
operation. Scope does not change binding ids or require a new adapter method.
431431

432+
Refreshing the same target session and target kind preserves omitted runtime
433+
metadata. Replacing either starts fresh target metadata, so a new session cannot
434+
inherit the previous plugin owner, agent, or label. Keep conversation transport
435+
details and explicit lifecycle settings separate from target metadata.
436+
432437
## Approvals and channel capabilities
433438

434439
Most channel plugins do not need approval-specific code. Core owns same-chat

‎docs/tools/subagents.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ waiting.
337337

338338
Automatic continuation is specific to the plugin runtime API above. Ordinary
339339
follow-ups through routes not tracked as sub-agent runs neither continue the
340-
paused run nor announce its requester. Explicit `subagents` steering is
341-
different: it deliberately replaces the yielded run and continues the same
342-
child session.
340+
paused run nor announce its requester.
343341

344342
Among plugin runtime follow-ups, continuation applies to those that use default
345343
delivery. A follow-up that supplies its own requester or completion-delivery

‎extensions/discord/src/monitor/thread-bindings.lifecycle.test.ts‎

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,66 +1254,69 @@ describe("thread binding lifecycle", () => {
12541254
expect(hoisted.restPost).not.toHaveBeenCalled();
12551255
});
12561256

1257-
it("preserves direct-binding metadata when rebinding the same conversation", async () => {
1258-
createTestThreadBindingManager({
1259-
accountId: "default",
1260-
persist: false,
1261-
enableSweeper: false,
1262-
idleTimeoutMs: 24 * 60 * 60 * 1000,
1263-
maxAgeMs: 0,
1264-
});
1265-
1266-
await getSessionBindingService().bind({
1267-
targetSessionKey: "plugin-binding:openclaw-codex-app-server:dm",
1268-
targetKind: "session",
1269-
conversation: {
1270-
channel: "discord",
1257+
it.each([false, true])(
1258+
"inherits runtime metadata only when refreshing the same target (replace=%s)",
1259+
async (replace) => {
1260+
createTestThreadBindingManager({
12711261
accountId: "default",
1272-
conversationId: "user:1177378744822943744",
1273-
},
1274-
placement: "current",
1275-
metadata: {
1276-
pluginBindingOwner: "plugin",
1277-
pluginId: "openclaw-codex-app-server",
1278-
pluginRoot: "/Users/huntharo/github/openclaw-app-server",
1279-
agentId: "codex",
1280-
boundBy: "system",
1281-
},
1282-
});
1262+
persist: false,
1263+
enableSweeper: false,
1264+
idleTimeoutMs: 24 * 60 * 60 * 1000,
1265+
maxAgeMs: 0,
1266+
});
12831267

1284-
await getSessionBindingService().bind({
1285-
targetSessionKey: "plugin-binding:openclaw-codex-app-server:dm",
1286-
targetKind: "session",
1287-
conversation: {
1288-
channel: "discord",
1289-
accountId: "default",
1290-
conversationId: "user:1177378744822943744",
1291-
},
1292-
placement: "current",
1293-
metadata: {
1294-
label: "codex-dm",
1295-
},
1296-
});
1268+
await getSessionBindingService().bind({
1269+
targetSessionKey: "plugin-binding:owner-plugin:dm",
1270+
targetKind: "session",
1271+
conversation: {
1272+
channel: "discord",
1273+
accountId: "default",
1274+
conversationId: "user:1177378744822943744",
1275+
},
1276+
placement: "current",
1277+
metadata: {
1278+
pluginBindingOwner: "plugin",
1279+
pluginId: "owner-plugin",
1280+
pluginRoot: "/plugins/owner-plugin",
1281+
agentId: "previous-agent",
1282+
boundBy: "system",
1283+
},
1284+
});
12971285

1298-
const resolved = requireRecord(
1299-
getSessionBindingService().resolveByConversation({
1300-
channel: "discord",
1301-
accountId: "default",
1302-
conversationId: "user:1177378744822943744",
1303-
}),
1304-
"resolved binding",
1305-
);
1306-
expectFields(requireRecord(resolved.metadata, "resolved metadata"), "resolved metadata", {
1307-
pluginBindingOwner: "plugin",
1308-
pluginId: "openclaw-codex-app-server",
1309-
pluginRoot: "/Users/huntharo/github/openclaw-app-server",
1310-
agentId: "codex",
1311-
boundBy: "system",
1312-
label: "codex-dm",
1313-
});
1314-
expect(hoisted.restGet).not.toHaveBeenCalled();
1315-
expect(hoisted.restPost).not.toHaveBeenCalled();
1316-
});
1286+
await getSessionBindingService().bind({
1287+
targetSessionKey: replace ? "agent:main:acp:replacement" : "plugin-binding:owner-plugin:dm",
1288+
targetKind: "session",
1289+
conversation: {
1290+
channel: "discord",
1291+
accountId: "default",
1292+
conversationId: "user:1177378744822943744",
1293+
},
1294+
placement: "current",
1295+
metadata: {
1296+
label: "updated",
1297+
},
1298+
});
1299+
1300+
const resolved = requireRecord(
1301+
getSessionBindingService().resolveByConversation({
1302+
channel: "discord",
1303+
accountId: "default",
1304+
conversationId: "user:1177378744822943744",
1305+
}),
1306+
"resolved binding",
1307+
);
1308+
expectFields(requireRecord(resolved.metadata, "resolved metadata"), "resolved metadata", {
1309+
pluginBindingOwner: replace ? undefined : "plugin",
1310+
pluginId: replace ? undefined : "owner-plugin",
1311+
pluginRoot: replace ? undefined : "/plugins/owner-plugin",
1312+
agentId: replace ? "main" : "previous-agent",
1313+
boundBy: "system",
1314+
label: "updated",
1315+
});
1316+
expect(hoisted.restGet).not.toHaveBeenCalled();
1317+
expect(hoisted.restPost).not.toHaveBeenCalled();
1318+
},
1319+
);
13171320

13181321
it("keeps overlapping thread ids isolated per account", async () => {
13191322
const a = createTestThreadBindingManager({

‎extensions/discord/src/monitor/thread-bindings.manager.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ export function createThreadBindingManager(params: {
345345
}
346346

347347
const targetKind = normalizeTargetKind(bindParams.targetKind, targetSessionKey);
348+
const previous =
349+
existingValue?.targetSessionKey === targetSessionKey &&
350+
existingValue.targetKind === targetKind
351+
? existingValue
352+
: undefined;
348353
let webhookId =
349354
normalizeOptionalString(bindParams.webhookId) ??
350355
normalizeOptionalString(existingValue?.webhookId) ??
@@ -378,16 +383,15 @@ export function createThreadBindingManager(params: {
378383
targetSessionKey,
379384
agentId:
380385
normalizeOptionalString(bindParams.agentId) ??
381-
normalizeOptionalString(existingValue?.agentId) ??
386+
normalizeOptionalString(previous?.agentId) ??
382387
resolveSessionAgentIdStrict({ config: cfg, sessionKey: targetSessionKey }),
383388
label:
384-
normalizeOptionalString(bindParams.label) ??
385-
normalizeOptionalString(existingValue?.label),
389+
normalizeOptionalString(bindParams.label) ?? normalizeOptionalString(previous?.label),
386390
webhookId: webhookId || undefined,
387391
webhookToken: webhookToken || undefined,
388392
boundBy:
389393
normalizeOptionalString(bindParams.boundBy) ??
390-
normalizeOptionalString(existingValue?.boundBy) ??
394+
normalizeOptionalString(previous?.boundBy) ??
391395
"system",
392396
boundAt: now,
393397
lastActivityAt: now,
@@ -396,12 +400,7 @@ export function createThreadBindingManager(params: {
396400
? existingValue.idleTimeoutMs
397401
: idleTimeoutMs,
398402
maxAgeMs: typeof existingValue?.maxAgeMs === "number" ? existingValue.maxAgeMs : maxAgeMs,
399-
metadata:
400-
bindParams.metadata && typeof bindParams.metadata === "object"
401-
? { ...existingValue?.metadata, ...bindParams.metadata }
402-
: existingValue?.metadata
403-
? { ...existingValue.metadata }
404-
: undefined,
403+
metadata: { ...previous?.metadata, ...bindParams.metadata },
405404
};
406405

407406
setBindingRecord(record);

‎extensions/feishu/src/thread-bindings.test.ts‎

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -255,68 +255,71 @@ describe("Feishu thread bindings", () => {
255255
).toBeNull();
256256
});
257257

258-
it("preserves delivery routing metadata when rebinding the same conversation", async () => {
259-
vi.spyOn(Date, "now").mockReturnValue(1_700_000_100_000);
260-
const manager = createFeishuThreadBindingManager({ cfg: baseCfg, accountId: "default" });
261-
262-
manager.bindConversation({
263-
conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
264-
parentConversationId: "oc_group_chat",
265-
targetKind: "subagent",
266-
targetSessionKey: "agent:main:subagent:child",
267-
metadata: {
268-
agentId: "codex",
269-
label: "child",
270-
boundBy: "system",
271-
deliveryTo: "user:ou_sender_1",
272-
deliveryThreadId: "om_topic_root",
273-
},
274-
});
258+
it.each([false, true])(
259+
"preserves conversation delivery fields without inheriting another target's owner (replace=%s)",
260+
async (replace) => {
261+
vi.spyOn(Date, "now").mockReturnValue(1_700_000_100_000);
262+
const manager = createFeishuThreadBindingManager({ cfg: baseCfg, accountId: "default" });
275263

276-
await getSessionBindingService().bind({
277-
targetSessionKey: "agent:main:subagent:child",
278-
targetKind: "subagent",
279-
conversation: {
280-
channel: "feishu",
281-
accountId: "default",
264+
manager.bindConversation({
282265
conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
283266
parentConversationId: "oc_group_chat",
284-
},
285-
placement: "current",
286-
metadata: {
287-
label: "child",
288-
},
289-
});
267+
targetKind: "subagent",
268+
targetSessionKey: "agent:main:subagent:child",
269+
metadata: {
270+
agentId: "previous-agent",
271+
label: "child",
272+
boundBy: "system",
273+
deliveryTo: "user:ou_sender_1",
274+
deliveryThreadId: "om_topic_root",
275+
},
276+
});
290277

291-
expect(
292-
getSessionBindingService().resolveByConversation({
293-
channel: "feishu",
294-
accountId: "default",
295-
conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
296-
}),
297-
).toEqual({
298-
bindingId: "default:oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
299-
targetSessionKey: "agent:main:subagent:child",
300-
targetKind: "subagent",
301-
conversation: {
302-
channel: "feishu",
303-
accountId: "default",
304-
conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
305-
parentConversationId: "oc_group_chat",
306-
},
307-
status: "active",
308-
boundAt: 1_700_000_100_000,
309-
expiresAt: 1_700_086_500_000,
310-
metadata: {
311-
agentId: "codex",
312-
label: "child",
313-
boundBy: "system",
314-
deliveryTo: "user:ou_sender_1",
315-
deliveryThreadId: "om_topic_root",
316-
lastActivityAt: 1_700_000_100_000,
317-
idleTimeoutMs: 86_400_000,
318-
maxAgeMs: 0,
319-
},
320-
});
321-
});
278+
await getSessionBindingService().bind({
279+
targetSessionKey: replace ? "agent:main:subagent:replacement" : "agent:main:subagent:child",
280+
targetKind: "subagent",
281+
conversation: {
282+
channel: "feishu",
283+
accountId: "default",
284+
conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
285+
parentConversationId: "oc_group_chat",
286+
},
287+
placement: "current",
288+
metadata: {
289+
label: "child",
290+
},
291+
});
292+
293+
expect(
294+
getSessionBindingService().resolveByConversation({
295+
channel: "feishu",
296+
accountId: "default",
297+
conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
298+
}),
299+
).toEqual({
300+
bindingId: "default:oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
301+
targetSessionKey: replace ? "agent:main:subagent:replacement" : "agent:main:subagent:child",
302+
targetKind: "subagent",
303+
conversation: {
304+
channel: "feishu",
305+
accountId: "default",
306+
conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
307+
parentConversationId: "oc_group_chat",
308+
},
309+
status: "active",
310+
boundAt: 1_700_000_100_000,
311+
expiresAt: 1_700_086_500_000,
312+
metadata: {
313+
agentId: replace ? "main" : "previous-agent",
314+
label: "child",
315+
boundBy: replace ? undefined : "system",
316+
deliveryTo: "user:ou_sender_1",
317+
deliveryThreadId: "om_topic_root",
318+
lastActivityAt: 1_700_000_100_000,
319+
idleTimeoutMs: 86_400_000,
320+
maxAgeMs: 0,
321+
},
322+
});
323+
},
324+
);
322325
});

‎extensions/feishu/src/thread-bindings.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ export function createFeishuThreadBindingManager(params: {
194194
return null;
195195
}
196196
const existingLocal = manager.getByConversationId(normalizedConversationId);
197+
const storedTargetKind = toFeishuTargetKind(targetKind);
198+
const previous =
199+
existingLocal?.targetSessionKey === normalizedTargetSessionKey &&
200+
existingLocal.targetKind === storedTargetKind
201+
? existingLocal
202+
: undefined;
197203
const now = Date.now();
198204
const record: FeishuThreadBindingRecord = {
199205
accountId,
@@ -208,23 +214,17 @@ export function createFeishuThreadBindingManager(params: {
208214
typeof metadata?.deliveryThreadId === "string" && metadata.deliveryThreadId.trim()
209215
? metadata.deliveryThreadId.trim()
210216
: existingLocal?.deliveryThreadId,
211-
targetKind: toFeishuTargetKind(targetKind),
217+
targetKind: storedTargetKind,
212218
targetSessionKey: normalizedTargetSessionKey,
213219
agentId:
214220
normalizeOptionalString(metadata?.agentId) ??
215-
existingLocal?.agentId ??
221+
previous?.agentId ??
216222
resolveSessionAgentIdStrict({
217223
config: params.cfg,
218224
sessionKey: normalizedTargetSessionKey,
219225
}),
220-
label:
221-
typeof metadata?.label === "string" && metadata.label.trim()
222-
? metadata.label.trim()
223-
: existingLocal?.label,
224-
boundBy:
225-
typeof metadata?.boundBy === "string" && metadata.boundBy.trim()
226-
? metadata.boundBy.trim()
227-
: existingLocal?.boundBy,
226+
label: normalizeOptionalString(metadata?.label) ?? previous?.label,
227+
boundBy: normalizeOptionalString(metadata?.boundBy) ?? previous?.boundBy,
228228
boundAt: now,
229229
lastActivityAt: now,
230230
};

0 commit comments

Comments
 (0)