@actagent/feishu 2026.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/README.md +11 -0
  2. package/actagent.plugin.json +224 -0
  3. package/api.ts +33 -0
  4. package/channel-entry.ts +21 -0
  5. package/channel-plugin-api.ts +2 -0
  6. package/contract-api.ts +17 -0
  7. package/index.ts +83 -0
  8. package/legacy-state-migrations-api.ts +2 -0
  9. package/npm-shrinkwrap.json +539 -0
  10. package/package.json +64 -0
  11. package/runtime-api.ts +58 -0
  12. package/runtime-setter-api.ts +3 -0
  13. package/secret-contract-api.ts +6 -0
  14. package/security-contract-api.ts +2 -0
  15. package/session-key-api.ts +2 -0
  16. package/setup-api.ts +4 -0
  17. package/setup-entry.test.ts +33 -0
  18. package/setup-entry.ts +25 -0
  19. package/skills/feishu-doc/SKILL.md +211 -0
  20. package/skills/feishu-doc/references/block-types.md +103 -0
  21. package/skills/feishu-drive/SKILL.md +97 -0
  22. package/skills/feishu-perm/SKILL.md +119 -0
  23. package/skills/feishu-wiki/SKILL.md +113 -0
  24. package/src/accounts.test.ts +481 -0
  25. package/src/accounts.ts +380 -0
  26. package/src/agent-config.ts +22 -0
  27. package/src/app-registration.test.ts +62 -0
  28. package/src/app-registration.ts +355 -0
  29. package/src/approval-auth.test.ts +25 -0
  30. package/src/approval-auth.ts +26 -0
  31. package/src/async.test.ts +68 -0
  32. package/src/async.ts +109 -0
  33. package/src/audio-preflight.runtime.ts +10 -0
  34. package/src/bitable.test.ts +174 -0
  35. package/src/bitable.ts +781 -0
  36. package/src/bot-content.ts +488 -0
  37. package/src/bot-group-name.test.ts +148 -0
  38. package/src/bot-runtime-api.ts +13 -0
  39. package/src/bot-sender-name.test.ts +68 -0
  40. package/src/bot-sender-name.ts +137 -0
  41. package/src/bot.broadcast.test.ts +643 -0
  42. package/src/bot.card-action.test.ts +647 -0
  43. package/src/bot.checkBotMentioned.test.ts +266 -0
  44. package/src/bot.helpers.test.ts +136 -0
  45. package/src/bot.stripBotMention.test.ts +127 -0
  46. package/src/bot.test.ts +3817 -0
  47. package/src/bot.ts +1788 -0
  48. package/src/card-action.ts +515 -0
  49. package/src/card-interaction.test.ts +132 -0
  50. package/src/card-interaction.ts +160 -0
  51. package/src/card-test-helpers.ts +55 -0
  52. package/src/card-ux-approval.ts +66 -0
  53. package/src/card-ux-launcher.test.ts +126 -0
  54. package/src/card-ux-launcher.ts +136 -0
  55. package/src/card-ux-shared.ts +34 -0
  56. package/src/channel-runtime-api.ts +17 -0
  57. package/src/channel.runtime.ts +48 -0
  58. package/src/channel.test.ts +1337 -0
  59. package/src/channel.ts +1401 -0
  60. package/src/chat-schema.ts +30 -0
  61. package/src/chat.test.ts +295 -0
  62. package/src/chat.ts +198 -0
  63. package/src/client-timeout.ts +44 -0
  64. package/src/client.test.ts +463 -0
  65. package/src/client.ts +263 -0
  66. package/src/comment-dispatcher-runtime-api.ts +7 -0
  67. package/src/comment-dispatcher.test.ts +186 -0
  68. package/src/comment-dispatcher.ts +108 -0
  69. package/src/comment-handler-runtime-api.ts +4 -0
  70. package/src/comment-handler.test.ts +588 -0
  71. package/src/comment-handler.ts +304 -0
  72. package/src/comment-reaction.test.ts +139 -0
  73. package/src/comment-reaction.ts +260 -0
  74. package/src/comment-shared.test.ts +184 -0
  75. package/src/comment-shared.ts +405 -0
  76. package/src/comment-target.ts +45 -0
  77. package/src/config-schema.test.ts +327 -0
  78. package/src/config-schema.ts +338 -0
  79. package/src/conversation-id.test.ts +19 -0
  80. package/src/conversation-id.ts +199 -0
  81. package/src/dedup-migrations.test.ts +90 -0
  82. package/src/dedup-migrations.ts +103 -0
  83. package/src/dedup.test.ts +95 -0
  84. package/src/dedup.ts +304 -0
  85. package/src/dedupe-key.ts +68 -0
  86. package/src/directory.static.ts +62 -0
  87. package/src/directory.test.ts +142 -0
  88. package/src/directory.ts +125 -0
  89. package/src/doc-schema.ts +183 -0
  90. package/src/doctor.test.ts +382 -0
  91. package/src/doctor.ts +876 -0
  92. package/src/docx-batch-insert.test.ts +117 -0
  93. package/src/docx-batch-insert.ts +223 -0
  94. package/src/docx-color-text.ts +154 -0
  95. package/src/docx-table-ops.test.ts +54 -0
  96. package/src/docx-table-ops.ts +316 -0
  97. package/src/docx-types.ts +39 -0
  98. package/src/docx.account-selection.test.ts +96 -0
  99. package/src/docx.test.ts +706 -0
  100. package/src/docx.ts +1598 -0
  101. package/src/drive-schema.ts +93 -0
  102. package/src/drive.test.ts +1240 -0
  103. package/src/drive.ts +830 -0
  104. package/src/dynamic-agent.test.ts +156 -0
  105. package/src/dynamic-agent.ts +144 -0
  106. package/src/event-types.ts +46 -0
  107. package/src/external-keys.test.ts +21 -0
  108. package/src/external-keys.ts +20 -0
  109. package/src/lifecycle.test-support.ts +223 -0
  110. package/src/media.test.ts +956 -0
  111. package/src/media.ts +1106 -0
  112. package/src/mention-target.types.ts +6 -0
  113. package/src/mention.ts +115 -0
  114. package/src/message-action-contract.ts +14 -0
  115. package/src/monitor-state-runtime-api.ts +8 -0
  116. package/src/monitor-transport-runtime-api.ts +11 -0
  117. package/src/monitor.account.ts +501 -0
  118. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +215 -0
  119. package/src/monitor.bot-identity.ts +87 -0
  120. package/src/monitor.bot-menu-handler.ts +164 -0
  121. package/src/monitor.bot-menu.lifecycle.test-support.ts +221 -0
  122. package/src/monitor.bot-menu.test.ts +200 -0
  123. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +265 -0
  124. package/src/monitor.card-action.lifecycle.test-support.ts +418 -0
  125. package/src/monitor.cleanup.test.ts +384 -0
  126. package/src/monitor.comment-notice-handler.ts +106 -0
  127. package/src/monitor.comment.test.ts +968 -0
  128. package/src/monitor.comment.ts +1386 -0
  129. package/src/monitor.lifecycle.test.ts +5 -0
  130. package/src/monitor.message-handler.ts +346 -0
  131. package/src/monitor.reaction.test.ts +770 -0
  132. package/src/monitor.startup.test.ts +232 -0
  133. package/src/monitor.startup.ts +76 -0
  134. package/src/monitor.state.defaults.test.ts +47 -0
  135. package/src/monitor.state.ts +171 -0
  136. package/src/monitor.synthetic-error.ts +19 -0
  137. package/src/monitor.test-mocks.ts +47 -0
  138. package/src/monitor.transport.ts +451 -0
  139. package/src/monitor.ts +104 -0
  140. package/src/monitor.webhook-e2e.test.ts +284 -0
  141. package/src/monitor.webhook-security.test.ts +394 -0
  142. package/src/monitor.webhook.test-helpers.ts +138 -0
  143. package/src/outbound-runtime-api.ts +2 -0
  144. package/src/outbound.test.ts +1255 -0
  145. package/src/outbound.ts +742 -0
  146. package/src/perm-schema.ts +53 -0
  147. package/src/perm.ts +171 -0
  148. package/src/pins.ts +109 -0
  149. package/src/policy.test.ts +224 -0
  150. package/src/policy.ts +322 -0
  151. package/src/post.test.ts +106 -0
  152. package/src/post.ts +276 -0
  153. package/src/presentation-card.ts +204 -0
  154. package/src/probe.test.ts +310 -0
  155. package/src/probe.ts +181 -0
  156. package/src/processing-claims.ts +60 -0
  157. package/src/qr-terminal.ts +2 -0
  158. package/src/reactions.ts +124 -0
  159. package/src/reasoning-preview.test.ts +114 -0
  160. package/src/reasoning-preview.ts +29 -0
  161. package/src/reply-dispatcher-runtime-api.ts +8 -0
  162. package/src/reply-dispatcher.test.ts +2009 -0
  163. package/src/reply-dispatcher.ts +865 -0
  164. package/src/runtime.ts +10 -0
  165. package/src/secret-contract.ts +146 -0
  166. package/src/secret-input.ts +2 -0
  167. package/src/security-audit-shared.ts +70 -0
  168. package/src/security-audit.test.ts +60 -0
  169. package/src/security-audit.ts +2 -0
  170. package/src/send-result.ts +81 -0
  171. package/src/send-target.test.ts +87 -0
  172. package/src/send-target.ts +36 -0
  173. package/src/send.reply-fallback.test.ts +418 -0
  174. package/src/send.test.ts +661 -0
  175. package/src/send.ts +860 -0
  176. package/src/sequential-key.test.ts +73 -0
  177. package/src/sequential-key.ts +29 -0
  178. package/src/sequential-queue.test.ts +184 -0
  179. package/src/sequential-queue.ts +90 -0
  180. package/src/session-conversation.ts +42 -0
  181. package/src/session-route.ts +49 -0
  182. package/src/setup-core.ts +52 -0
  183. package/src/setup-surface.test.ts +485 -0
  184. package/src/setup-surface.ts +620 -0
  185. package/src/streaming-card.test.ts +549 -0
  186. package/src/streaming-card.ts +611 -0
  187. package/src/subagent-hooks.test.ts +632 -0
  188. package/src/subagent-hooks.ts +414 -0
  189. package/src/targets.ts +98 -0
  190. package/src/test-support/lifecycle-test-support.ts +459 -0
  191. package/src/thread-bindings.test.ts +181 -0
  192. package/src/thread-bindings.ts +332 -0
  193. package/src/tool-account-routing.test.ts +419 -0
  194. package/src/tool-account.test.ts +45 -0
  195. package/src/tool-account.ts +98 -0
  196. package/src/tool-factory-test-harness.ts +83 -0
  197. package/src/tool-result.test.ts +33 -0
  198. package/src/tool-result.ts +17 -0
  199. package/src/tools-config.test.ts +52 -0
  200. package/src/tools-config.ts +29 -0
  201. package/src/types.ts +111 -0
  202. package/src/typing.test.ts +145 -0
  203. package/src/typing.ts +215 -0
  204. package/src/wiki-schema.ts +70 -0
  205. package/src/wiki.ts +271 -0
  206. package/subagent-hooks-api.ts +22 -0
  207. package/tsconfig.json +16 -0
@@ -0,0 +1,632 @@
1
+ // Feishu tests cover subagent hooks plugin behavior.
2
+ import {
3
+ getRequiredHookHandler,
4
+ registerHookHandlersForTest,
5
+ } from "actagent/plugin-sdk/channel-test-helpers";
6
+ import { beforeEach, describe, expect, it } from "vitest";
7
+ import type { ACTAgentBotConfig, ACTAgentPluginApi } from "../runtime-api.js";
8
+ import { registerFeishuSubagentHooks } from "../subagent-hooks-api.js";
9
+ import { handleFeishuSubagentSpawning } from "./subagent-hooks.js";
10
+ import {
11
+ createFeishuThreadBindingManager,
12
+ testing as threadBindingTesting,
13
+ } from "./thread-bindings.js";
14
+
15
+ const baseConfig: ACTAgentBotConfig = {
16
+ session: { mainKey: "main", scope: "per-sender" },
17
+ channels: { feishu: {} },
18
+ };
19
+
20
+ function registerHandlersForTest(config: Record<string, unknown> = baseConfig) {
21
+ return registerHookHandlersForTest<ACTAgentPluginApi>({
22
+ config,
23
+ register: (api) => {
24
+ registerFeishuSubagentHooks(api);
25
+ api.on("subagent_spawning", (event, ctx) => handleFeishuSubagentSpawning(event, ctx));
26
+ },
27
+ });
28
+ }
29
+
30
+ async function expectHookError(value: unknown, expectedErrorFragment: string): Promise<void> {
31
+ const result = (await value) as { status?: unknown; error?: unknown };
32
+ expect(result.status).toBe("error");
33
+ expect(result.error).toContain(expectedErrorFragment);
34
+ }
35
+
36
+ describe("feishu subagent hook handlers", () => {
37
+ beforeEach(() => {
38
+ threadBindingTesting.resetFeishuThreadBindingsForTests();
39
+ });
40
+
41
+ it("binds a Feishu DM conversation on subagent_spawning", async () => {
42
+ const handlers = registerHandlersForTest();
43
+ const handler = getRequiredHookHandler(handlers, "subagent_spawning");
44
+ createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
45
+
46
+ const result = await handler(
47
+ {
48
+ childSessionKey: "agent:main:subagent:child",
49
+ agentId: "codex",
50
+ label: "banana",
51
+ mode: "session",
52
+ requester: {
53
+ channel: "feishu",
54
+ accountId: "work",
55
+ to: "user:ou_sender_1",
56
+ },
57
+ threadRequested: true,
58
+ },
59
+ {},
60
+ );
61
+
62
+ expect(result).toEqual({
63
+ status: "ok",
64
+ threadBindingReady: true,
65
+ deliveryOrigin: {
66
+ channel: "feishu",
67
+ accountId: "work",
68
+ to: "user:ou_sender_1",
69
+ },
70
+ });
71
+
72
+ const deliveryTargetHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
73
+ await expect(
74
+ deliveryTargetHandler(
75
+ {
76
+ childSessionKey: "agent:main:subagent:child",
77
+ requesterSessionKey: "agent:main:main",
78
+ requesterOrigin: {
79
+ channel: "feishu",
80
+ accountId: "work",
81
+ to: "user:ou_sender_1",
82
+ },
83
+ expectsCompletionMessage: true,
84
+ },
85
+ {},
86
+ ),
87
+ ).resolves.toEqual({
88
+ origin: {
89
+ channel: "feishu",
90
+ accountId: "work",
91
+ to: "user:ou_sender_1",
92
+ },
93
+ });
94
+ });
95
+
96
+ it("preserves the original Feishu DM delivery target", async () => {
97
+ const handlers = registerHandlersForTest();
98
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
99
+ const manager = createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
100
+
101
+ manager.bindConversation({
102
+ conversationId: "ou_sender_1",
103
+ targetKind: "subagent",
104
+ targetSessionKey: "agent:main:subagent:chat-dm-child",
105
+ metadata: {
106
+ deliveryTo: "chat:oc_dm_chat_1",
107
+ boundBy: "system",
108
+ },
109
+ });
110
+
111
+ await expect(
112
+ deliveryHandler(
113
+ {
114
+ childSessionKey: "agent:main:subagent:chat-dm-child",
115
+ requesterSessionKey: "agent:main:main",
116
+ requesterOrigin: {
117
+ channel: "feishu",
118
+ accountId: "work",
119
+ to: "chat:oc_dm_chat_1",
120
+ },
121
+ expectsCompletionMessage: true,
122
+ },
123
+ {},
124
+ ),
125
+ ).resolves.toEqual({
126
+ origin: {
127
+ channel: "feishu",
128
+ accountId: "work",
129
+ to: "chat:oc_dm_chat_1",
130
+ },
131
+ });
132
+ });
133
+
134
+ it("binds a Feishu topic conversation and preserves parent context", async () => {
135
+ const handlers = registerHandlersForTest();
136
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
137
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
138
+ createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
139
+
140
+ const result = await spawnHandler(
141
+ {
142
+ childSessionKey: "agent:main:subagent:topic-child",
143
+ agentId: "codex",
144
+ label: "topic-child",
145
+ mode: "session",
146
+ requester: {
147
+ channel: "feishu",
148
+ accountId: "work",
149
+ to: "chat:oc_group_chat",
150
+ threadId: "om_topic_root",
151
+ },
152
+ threadRequested: true,
153
+ },
154
+ {},
155
+ );
156
+
157
+ expect(result).toEqual({
158
+ status: "ok",
159
+ threadBindingReady: true,
160
+ deliveryOrigin: {
161
+ channel: "feishu",
162
+ accountId: "work",
163
+ to: "chat:oc_group_chat",
164
+ threadId: "om_topic_root",
165
+ },
166
+ });
167
+ await expect(
168
+ deliveryHandler(
169
+ {
170
+ childSessionKey: "agent:main:subagent:topic-child",
171
+ requesterSessionKey: "agent:main:main",
172
+ requesterOrigin: {
173
+ channel: "feishu",
174
+ accountId: "work",
175
+ to: "chat:oc_group_chat",
176
+ threadId: "om_topic_root",
177
+ },
178
+ expectsCompletionMessage: true,
179
+ },
180
+ {},
181
+ ),
182
+ ).resolves.toEqual({
183
+ origin: {
184
+ channel: "feishu",
185
+ accountId: "work",
186
+ to: "chat:oc_group_chat",
187
+ threadId: "om_topic_root",
188
+ },
189
+ });
190
+ });
191
+
192
+ it("uses the requester session binding to preserve sender-scoped topic conversations", async () => {
193
+ const handlers = registerHandlersForTest();
194
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
195
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
196
+ const manager = createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
197
+
198
+ manager.bindConversation({
199
+ conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
200
+ parentConversationId: "oc_group_chat",
201
+ targetKind: "subagent",
202
+ targetSessionKey: "agent:main:parent",
203
+ metadata: {
204
+ agentId: "codex",
205
+ label: "parent",
206
+ boundBy: "system",
207
+ },
208
+ });
209
+
210
+ const reboundResult = await spawnHandler(
211
+ {
212
+ childSessionKey: "agent:main:subagent:sender-child",
213
+ agentId: "codex",
214
+ label: "sender-child",
215
+ mode: "session",
216
+ requester: {
217
+ channel: "feishu",
218
+ accountId: "work",
219
+ to: "chat:oc_group_chat",
220
+ threadId: "om_topic_root",
221
+ },
222
+ threadRequested: true,
223
+ },
224
+ {
225
+ requesterSessionKey: "agent:main:parent",
226
+ },
227
+ );
228
+
229
+ expect(reboundResult).toEqual({
230
+ status: "ok",
231
+ threadBindingReady: true,
232
+ deliveryOrigin: {
233
+ channel: "feishu",
234
+ accountId: "work",
235
+ to: "chat:oc_group_chat",
236
+ threadId: "om_topic_root",
237
+ },
238
+ });
239
+ const childBindings = manager.listBySessionKey("agent:main:subagent:sender-child");
240
+ expect(childBindings).toHaveLength(1);
241
+ expect(childBindings[0]?.conversationId).toBe(
242
+ "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
243
+ );
244
+ expect(childBindings[0]?.parentConversationId).toBe("oc_group_chat");
245
+ await expect(
246
+ deliveryHandler(
247
+ {
248
+ childSessionKey: "agent:main:subagent:sender-child",
249
+ requesterSessionKey: "agent:main:parent",
250
+ requesterOrigin: {
251
+ channel: "feishu",
252
+ accountId: "work",
253
+ to: "chat:oc_group_chat",
254
+ threadId: "om_topic_root",
255
+ },
256
+ expectsCompletionMessage: true,
257
+ },
258
+ {},
259
+ ),
260
+ ).resolves.toEqual({
261
+ origin: {
262
+ channel: "feishu",
263
+ accountId: "work",
264
+ to: "chat:oc_group_chat",
265
+ threadId: "om_topic_root",
266
+ },
267
+ });
268
+ });
269
+
270
+ it("prefers requester-matching bindings when multiple child bindings exist", async () => {
271
+ const handlers = registerHandlersForTest();
272
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
273
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
274
+ createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
275
+
276
+ await spawnHandler(
277
+ {
278
+ childSessionKey: "agent:main:subagent:shared",
279
+ agentId: "codex",
280
+ label: "shared",
281
+ mode: "session",
282
+ requester: {
283
+ channel: "feishu",
284
+ accountId: "work",
285
+ to: "user:ou_sender_1",
286
+ },
287
+ threadRequested: true,
288
+ },
289
+ {},
290
+ );
291
+ await spawnHandler(
292
+ {
293
+ childSessionKey: "agent:main:subagent:shared",
294
+ agentId: "codex",
295
+ label: "shared",
296
+ mode: "session",
297
+ requester: {
298
+ channel: "feishu",
299
+ accountId: "work",
300
+ to: "user:ou_sender_2",
301
+ },
302
+ threadRequested: true,
303
+ },
304
+ {},
305
+ );
306
+
307
+ await expect(
308
+ deliveryHandler(
309
+ {
310
+ childSessionKey: "agent:main:subagent:shared",
311
+ requesterSessionKey: "agent:main:main",
312
+ requesterOrigin: {
313
+ channel: "feishu",
314
+ accountId: "work",
315
+ to: "user:ou_sender_2",
316
+ },
317
+ expectsCompletionMessage: true,
318
+ },
319
+ {},
320
+ ),
321
+ ).resolves.toEqual({
322
+ origin: {
323
+ channel: "feishu",
324
+ accountId: "work",
325
+ to: "user:ou_sender_2",
326
+ },
327
+ });
328
+ });
329
+
330
+ it("fails closed when requester-session bindings remain ambiguous for the same topic", async () => {
331
+ const handlers = registerHandlersForTest();
332
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
333
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
334
+ const manager = createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
335
+
336
+ manager.bindConversation({
337
+ conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
338
+ parentConversationId: "oc_group_chat",
339
+ targetKind: "subagent",
340
+ targetSessionKey: "agent:main:parent",
341
+ metadata: { boundBy: "system" },
342
+ });
343
+ manager.bindConversation({
344
+ conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_2",
345
+ parentConversationId: "oc_group_chat",
346
+ targetKind: "subagent",
347
+ targetSessionKey: "agent:main:parent",
348
+ metadata: { boundBy: "system" },
349
+ });
350
+
351
+ await expectHookError(
352
+ spawnHandler(
353
+ {
354
+ childSessionKey: "agent:main:subagent:ambiguous-child",
355
+ agentId: "codex",
356
+ label: "ambiguous-child",
357
+ mode: "session",
358
+ requester: {
359
+ channel: "feishu",
360
+ accountId: "work",
361
+ to: "chat:oc_group_chat",
362
+ threadId: "om_topic_root",
363
+ },
364
+ threadRequested: true,
365
+ },
366
+ {
367
+ requesterSessionKey: "agent:main:parent",
368
+ },
369
+ ),
370
+ "direct messages or topic conversations",
371
+ );
372
+
373
+ await expect(
374
+ deliveryHandler(
375
+ {
376
+ childSessionKey: "agent:main:subagent:ambiguous-child",
377
+ requesterSessionKey: "agent:main:parent",
378
+ requesterOrigin: {
379
+ channel: "feishu",
380
+ accountId: "work",
381
+ to: "chat:oc_group_chat",
382
+ threadId: "om_topic_root",
383
+ },
384
+ expectsCompletionMessage: true,
385
+ },
386
+ {},
387
+ ),
388
+ ).resolves.toBeUndefined();
389
+ });
390
+
391
+ it("fails closed when both topic-level and sender-scoped requester bindings exist", async () => {
392
+ const handlers = registerHandlersForTest();
393
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
394
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
395
+ const manager = createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
396
+
397
+ manager.bindConversation({
398
+ conversationId: "oc_group_chat:topic:om_topic_root",
399
+ parentConversationId: "oc_group_chat",
400
+ targetKind: "subagent",
401
+ targetSessionKey: "agent:main:parent",
402
+ metadata: { boundBy: "system" },
403
+ });
404
+ manager.bindConversation({
405
+ conversationId: "oc_group_chat:topic:om_topic_root:sender:ou_sender_1",
406
+ parentConversationId: "oc_group_chat",
407
+ targetKind: "subagent",
408
+ targetSessionKey: "agent:main:parent",
409
+ metadata: { boundBy: "system" },
410
+ });
411
+
412
+ await expectHookError(
413
+ spawnHandler(
414
+ {
415
+ childSessionKey: "agent:main:subagent:mixed-topic-child",
416
+ agentId: "codex",
417
+ label: "mixed-topic-child",
418
+ mode: "session",
419
+ requester: {
420
+ channel: "feishu",
421
+ accountId: "work",
422
+ to: "chat:oc_group_chat",
423
+ threadId: "om_topic_root",
424
+ },
425
+ threadRequested: true,
426
+ },
427
+ {
428
+ requesterSessionKey: "agent:main:parent",
429
+ },
430
+ ),
431
+ "direct messages or topic conversations",
432
+ );
433
+
434
+ await expect(
435
+ deliveryHandler(
436
+ {
437
+ childSessionKey: "agent:main:subagent:mixed-topic-child",
438
+ requesterSessionKey: "agent:main:parent",
439
+ requesterOrigin: {
440
+ channel: "feishu",
441
+ accountId: "work",
442
+ to: "chat:oc_group_chat",
443
+ threadId: "om_topic_root",
444
+ },
445
+ expectsCompletionMessage: true,
446
+ },
447
+ {},
448
+ ),
449
+ ).resolves.toBeUndefined();
450
+ });
451
+
452
+ it("no-ops for non-Feishu channels and non-threaded spawns", async () => {
453
+ const handlers = registerHandlersForTest();
454
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
455
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
456
+ const endedHandler = getRequiredHookHandler(handlers, "subagent_ended");
457
+
458
+ await expect(
459
+ spawnHandler(
460
+ {
461
+ childSessionKey: "agent:main:subagent:child",
462
+ agentId: "codex",
463
+ mode: "run",
464
+ requester: {
465
+ channel: "discord",
466
+ accountId: "work",
467
+ to: "channel:123",
468
+ },
469
+ threadRequested: true,
470
+ },
471
+ {},
472
+ ),
473
+ ).resolves.toBeUndefined();
474
+
475
+ await expect(
476
+ spawnHandler(
477
+ {
478
+ childSessionKey: "agent:main:subagent:child",
479
+ agentId: "codex",
480
+ mode: "run",
481
+ requester: {
482
+ channel: "feishu",
483
+ accountId: "work",
484
+ to: "user:ou_sender_1",
485
+ },
486
+ threadRequested: false,
487
+ },
488
+ {},
489
+ ),
490
+ ).resolves.toBeUndefined();
491
+
492
+ await expect(
493
+ deliveryHandler(
494
+ {
495
+ childSessionKey: "agent:main:subagent:child",
496
+ requesterSessionKey: "agent:main:main",
497
+ requesterOrigin: {
498
+ channel: "discord",
499
+ accountId: "work",
500
+ to: "channel:123",
501
+ },
502
+ expectsCompletionMessage: true,
503
+ },
504
+ {},
505
+ ),
506
+ ).resolves.toBeUndefined();
507
+
508
+ await expect(
509
+ endedHandler(
510
+ {
511
+ targetSessionKey: "agent:main:subagent:child",
512
+ targetKind: "subagent",
513
+ reason: "done",
514
+ accountId: "work",
515
+ },
516
+ {},
517
+ ),
518
+ ).resolves.toBeUndefined();
519
+ });
520
+
521
+ it("returns an error for unsupported non-topic Feishu group conversations", async () => {
522
+ const handler = getRequiredHookHandler(registerHandlersForTest(), "subagent_spawning");
523
+ createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
524
+
525
+ await expectHookError(
526
+ handler(
527
+ {
528
+ childSessionKey: "agent:main:subagent:child",
529
+ agentId: "codex",
530
+ mode: "session",
531
+ requester: {
532
+ channel: "feishu",
533
+ accountId: "work",
534
+ to: "chat:oc_group_chat",
535
+ },
536
+ threadRequested: true,
537
+ },
538
+ {},
539
+ ),
540
+ "direct messages or topic conversations",
541
+ );
542
+ });
543
+
544
+ it("unbinds Feishu bindings on subagent_ended", async () => {
545
+ const handlers = registerHandlersForTest();
546
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
547
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
548
+ const endedHandler = getRequiredHookHandler(handlers, "subagent_ended");
549
+ createFeishuThreadBindingManager({ cfg: baseConfig, accountId: "work" });
550
+
551
+ await spawnHandler(
552
+ {
553
+ childSessionKey: "agent:main:subagent:child",
554
+ agentId: "codex",
555
+ mode: "session",
556
+ requester: {
557
+ channel: "feishu",
558
+ accountId: "work",
559
+ to: "user:ou_sender_1",
560
+ },
561
+ threadRequested: true,
562
+ },
563
+ {},
564
+ );
565
+
566
+ await endedHandler(
567
+ {
568
+ targetSessionKey: "agent:main:subagent:child",
569
+ targetKind: "subagent",
570
+ reason: "done",
571
+ accountId: "work",
572
+ },
573
+ {},
574
+ );
575
+
576
+ await expect(
577
+ deliveryHandler(
578
+ {
579
+ childSessionKey: "agent:main:subagent:child",
580
+ requesterSessionKey: "agent:main:main",
581
+ requesterOrigin: {
582
+ channel: "feishu",
583
+ accountId: "work",
584
+ to: "user:ou_sender_1",
585
+ },
586
+ expectsCompletionMessage: true,
587
+ },
588
+ {},
589
+ ),
590
+ ).resolves.toBeUndefined();
591
+ });
592
+
593
+ it("fails closed when the Feishu monitor-owned binding manager is unavailable", async () => {
594
+ const handlers = registerHandlersForTest();
595
+ const spawnHandler = getRequiredHookHandler(handlers, "subagent_spawning");
596
+ const deliveryHandler = getRequiredHookHandler(handlers, "subagent_delivery_target");
597
+
598
+ await expectHookError(
599
+ spawnHandler(
600
+ {
601
+ childSessionKey: "agent:main:subagent:no-manager",
602
+ agentId: "codex",
603
+ mode: "session",
604
+ requester: {
605
+ channel: "feishu",
606
+ accountId: "work",
607
+ to: "user:ou_sender_1",
608
+ },
609
+ threadRequested: true,
610
+ },
611
+ {},
612
+ ),
613
+ "monitor is not active",
614
+ );
615
+
616
+ await expect(
617
+ deliveryHandler(
618
+ {
619
+ childSessionKey: "agent:main:subagent:no-manager",
620
+ requesterSessionKey: "agent:main:main",
621
+ requesterOrigin: {
622
+ channel: "feishu",
623
+ accountId: "work",
624
+ to: "user:ou_sender_1",
625
+ },
626
+ expectsCompletionMessage: true,
627
+ },
628
+ {},
629
+ ),
630
+ ).resolves.toBeUndefined();
631
+ });
632
+ });