@ai-setting/roy-agent-core 1.7.37 → 1.7.39

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 (82) hide show
  1. package/dist/config/index.js +7 -7
  2. package/dist/env/agent/index.js +10 -10
  3. package/dist/env/commands/index.js +7 -7
  4. package/dist/env/context/index.js +7 -3
  5. package/dist/env/debug/index.js +7 -7
  6. package/dist/env/event-source/index.js +24 -14
  7. package/dist/env/hook/index.js +5 -5
  8. package/dist/env/index.js +33 -32
  9. package/dist/env/llm/index.js +7 -7
  10. package/dist/env/log-trace/index.js +7 -7
  11. package/dist/env/mcp/index.js +7 -7
  12. package/dist/env/memory/index.js +7 -7
  13. package/dist/env/plugin/index.js +7 -7
  14. package/dist/env/prompt/index.js +7 -7
  15. package/dist/env/session/index.js +7 -7
  16. package/dist/env/skill/index.js +7 -7
  17. package/dist/env/task/delegate/index.js +7 -6
  18. package/dist/env/task/index.js +12 -11
  19. package/dist/env/task/plugins/index.js +7 -7
  20. package/dist/env/task/storage/index.js +2 -2
  21. package/dist/env/task/tools/index.js +3 -3
  22. package/dist/env/tool/built-in/index.js +2 -2
  23. package/dist/env/tool/index.js +8 -8
  24. package/dist/env/workflow/engine/index.js +9 -9
  25. package/dist/env/workflow/index.js +17 -17
  26. package/dist/env/workflow/nodes/index.js +2 -2
  27. package/dist/env/workflow/service/index.js +2 -2
  28. package/dist/env/workflow/storage/index.js +3 -3
  29. package/dist/env/workflow/tools/index.js +11 -11
  30. package/dist/env/workflow/utils/index.js +4 -4
  31. package/dist/index.js +53 -43
  32. package/dist/shared/@ai-setting/{roy-agent-core-yf0ex8y1.js → roy-agent-core-333apxaa.js} +1 -1
  33. package/dist/shared/@ai-setting/{roy-agent-core-pxyyrxnn.js → roy-agent-core-34je386q.js} +4 -4
  34. package/dist/shared/@ai-setting/{roy-agent-core-q8vew3zh.js → roy-agent-core-3h7ajma5.js} +2 -2
  35. package/dist/shared/@ai-setting/{roy-agent-core-e4v61k3j.js → roy-agent-core-47wrv4ex.js} +94 -32
  36. package/dist/shared/@ai-setting/{roy-agent-core-9drbkjfr.js → roy-agent-core-4cj6hram.js} +1 -1
  37. package/dist/shared/@ai-setting/{roy-agent-core-fytqnxrd.js → roy-agent-core-4cqcdk7k.js} +3 -3
  38. package/dist/shared/@ai-setting/{roy-agent-core-zda3rfr3.js → roy-agent-core-4kbvfnn1.js} +54 -5
  39. package/dist/shared/@ai-setting/{roy-agent-core-bb57xjfh.js → roy-agent-core-5exnp10h.js} +11 -8
  40. package/dist/shared/@ai-setting/{roy-agent-core-jq7g5nxz.js → roy-agent-core-5j4awbgc.js} +3 -3
  41. package/dist/shared/@ai-setting/{roy-agent-core-1vj7dd14.js → roy-agent-core-6mnwqcqf.js} +1 -1
  42. package/dist/shared/@ai-setting/{roy-agent-core-wgttzq8e.js → roy-agent-core-6vbgswjw.js} +1 -1
  43. package/dist/shared/@ai-setting/{roy-agent-core-1q6gjxba.js → roy-agent-core-6vwf03cz.js} +10 -7
  44. package/dist/shared/@ai-setting/{roy-agent-core-a8zf7ts4.js → roy-agent-core-754b0078.js} +5 -5
  45. package/dist/shared/@ai-setting/{roy-agent-core-j9ejsmsj.js → roy-agent-core-8nv07zgr.js} +1 -1
  46. package/dist/shared/@ai-setting/{roy-agent-core-yw07bvqz.js → roy-agent-core-9cqpdrnk.js} +3 -3
  47. package/dist/shared/@ai-setting/{roy-agent-core-q3zvkbj6.js → roy-agent-core-9qn92jhf.js} +2 -2
  48. package/dist/shared/@ai-setting/{roy-agent-core-m7dppx2p.js → roy-agent-core-9t675te0.js} +2 -2
  49. package/dist/shared/@ai-setting/{roy-agent-core-fym6zz5a.js → roy-agent-core-a236mps6.js} +2 -2
  50. package/dist/shared/@ai-setting/{roy-agent-core-qhjm0bxm.js → roy-agent-core-a9pbda78.js} +23 -13
  51. package/dist/shared/@ai-setting/{roy-agent-core-s53yya0w.js → roy-agent-core-asxe1nt4.js} +5 -5
  52. package/dist/shared/@ai-setting/{roy-agent-core-em8618g6.js → roy-agent-core-cazvr63z.js} +237 -14
  53. package/dist/shared/@ai-setting/{roy-agent-core-ds5f75pg.js → roy-agent-core-ck60j1q9.js} +19 -1
  54. package/dist/shared/@ai-setting/{roy-agent-core-w457586k.js → roy-agent-core-cw89yj9d.js} +1 -1
  55. package/dist/shared/@ai-setting/{roy-agent-core-462v5d24.js → roy-agent-core-cysnmvcy.js} +1 -1
  56. package/dist/shared/@ai-setting/{roy-agent-core-88tfzaxg.js → roy-agent-core-f18jk3cw.js} +1 -1
  57. package/dist/shared/@ai-setting/{roy-agent-core-qz9jb9sp.js → roy-agent-core-hkj5v9xq.js} +1 -1
  58. package/dist/shared/@ai-setting/{roy-agent-core-dzfyjzgw.js → roy-agent-core-jadrj0bz.js} +27 -2
  59. package/dist/shared/@ai-setting/{roy-agent-core-dfay0w56.js → roy-agent-core-jawzpf5k.js} +2 -2
  60. package/dist/shared/@ai-setting/{roy-agent-core-zekkw27m.js → roy-agent-core-jg8cv1f7.js} +128 -29
  61. package/dist/shared/@ai-setting/{roy-agent-core-mwaesrpc.js → roy-agent-core-jjw7xcxv.js} +4 -4
  62. package/dist/shared/@ai-setting/{roy-agent-core-2qke91fm.js → roy-agent-core-krppch0s.js} +3 -3
  63. package/dist/shared/@ai-setting/{roy-agent-core-mxwaq8ey.js → roy-agent-core-m3ffkexb.js} +13 -13
  64. package/dist/shared/@ai-setting/{roy-agent-core-2wbdfrtg.js → roy-agent-core-n1ceexfz.js} +1 -1
  65. package/dist/shared/@ai-setting/{roy-agent-core-1btakphh.js → roy-agent-core-pc8x07p0.js} +1 -1
  66. package/dist/shared/@ai-setting/{roy-agent-core-d5gwqrr5.js → roy-agent-core-pd0exvnq.js} +1 -1
  67. package/dist/shared/@ai-setting/{roy-agent-core-1nvqn17p.js → roy-agent-core-qfktfbrr.js} +4 -4
  68. package/dist/shared/@ai-setting/{roy-agent-core-x4wev4rn.js → roy-agent-core-rczz7e81.js} +5 -5
  69. package/dist/shared/@ai-setting/{roy-agent-core-e3qjt852.js → roy-agent-core-rgpkygbc.js} +1 -1
  70. package/dist/shared/@ai-setting/{roy-agent-core-rx8whb18.js → roy-agent-core-s44egzrh.js} +35 -4
  71. package/dist/shared/@ai-setting/{roy-agent-core-k5csz66y.js → roy-agent-core-tm57tmds.js} +1 -1
  72. package/dist/shared/@ai-setting/{roy-agent-core-qhtejtpc.js → roy-agent-core-vsrfzaze.js} +1 -1
  73. package/dist/shared/@ai-setting/{roy-agent-core-whta3f53.js → roy-agent-core-wn59p0zw.js} +1 -1
  74. package/dist/shared/@ai-setting/{roy-agent-core-mdaew65f.js → roy-agent-core-wybj8emq.js} +1 -1
  75. package/dist/shared/@ai-setting/{roy-agent-core-ddq5hcp5.js → roy-agent-core-x6c45yqd.js} +3 -1
  76. package/dist/shared/@ai-setting/roy-agent-core-xyys5m1a.js +159 -0
  77. package/dist/shared/@ai-setting/{roy-agent-core-wphva2vs.js → roy-agent-core-y1n63tse.js} +2 -2
  78. package/dist/shared/@ai-setting/{roy-agent-core-jcdsghyr.js → roy-agent-core-yjjqm4ww.js} +1 -1
  79. package/dist/shared/@ai-setting/{roy-agent-core-n3pwkwjh.js → roy-agent-core-za09t5g3.js} +1 -1
  80. package/dist/shared/@ai-setting/{roy-agent-core-n82acszf.js → roy-agent-core-zbb3sk72.js} +17 -3
  81. package/package.json +1 -1
  82. /package/dist/shared/@ai-setting/{roy-agent-core-k5hxvaf0.js → roy-agent-core-3ztzc83c.js} +0 -0
@@ -2,6 +2,9 @@ import {
2
2
  ProcessRegistry,
3
3
  runWithProcessRegistryAsync
4
4
  } from "./roy-agent-core-tbrn9wdm.js";
5
+ import {
6
+ getSharedEventStore
7
+ } from "./roy-agent-core-xyys5m1a.js";
5
8
  import {
6
9
  TaskHookPoints
7
10
  } from "./roy-agent-core-1pg0fepg.js";
@@ -10,26 +13,26 @@ import {
10
13
  } from "./roy-agent-core-skqt40yy.js";
11
14
  import {
12
15
  DEFAULT_SUBAGENT_PROMPT
13
- } from "./roy-agent-core-n3pwkwjh.js";
16
+ } from "./roy-agent-core-za09t5g3.js";
14
17
  import {
15
18
  runWithBgTaskIdAsync
16
19
  } from "./roy-agent-core-c1v263jn.js";
17
20
  import {
18
21
  globalHookManager,
19
22
  init_global_hook_manager
20
- } from "./roy-agent-core-fym6zz5a.js";
23
+ } from "./roy-agent-core-a236mps6.js";
21
24
  import {
22
25
  init_context
23
- } from "./roy-agent-core-whta3f53.js";
26
+ } from "./roy-agent-core-wn59p0zw.js";
24
27
  import {
25
28
  getCurrentTaskId,
26
29
  getEnvContext
27
- } from "./roy-agent-core-n82acszf.js";
30
+ } from "./roy-agent-core-zbb3sk72.js";
28
31
  import {
29
32
  TracedAs,
30
33
  init_decorator,
31
34
  wrapFunction
32
- } from "./roy-agent-core-qhjm0bxm.js";
35
+ } from "./roy-agent-core-a9pbda78.js";
33
36
  import {
34
37
  createLogger,
35
38
  init_logger
@@ -855,7 +858,8 @@ var DelegateToolParameters = z.object({
855
858
  timeout: z.number().describe("Task timeout in milliseconds. Default: 1800000 (30 minutes). If set, task will be terminated after timeout.").optional(),
856
859
  reason: z.string().describe("Brief reason for calling this tool (max 30 chars, e.g., 'Delegate refactor task')").optional(),
857
860
  similar_task_ids: z.array(z.number()).max(3).describe("Similar task IDs (max 3) to reference for improving task execution. Search for similar tasks before calling this tool."),
858
- sub_session_id: z.string().describe("Existing sub-agent session ID to resume. " + "If provided, the sub-agent will continue from the previous conversation " + "context (used for retry on timeout/failure). " + "Leave empty for a new session.").optional()
861
+ sub_session_id: z.string().describe("Existing sub-agent session ID to resume. " + "If provided, the sub-agent will continue from the previous conversation " + "context (used for retry on timeout/failure). " + "Leave empty for a new session.").optional(),
862
+ triggerEventId: z.string().describe("EventStore trigger event ID (ESPlugin v2). Pass it when this delegate_task " + "is triggered by an external event source (e.g. Feishu message) so the " + "completion notification can reply to the original chat instead of a " + "fallback channel. Leave empty if not triggered by an event source.").optional()
859
863
  });
860
864
  function buildEntryAgentPrompt(options) {
861
865
  const { workflowName, originalPrompt, description } = options;
@@ -1042,19 +1046,39 @@ class BackgroundTaskManager {
1042
1046
  getSessionComponent() {
1043
1047
  return this.env?.getComponent?.("session");
1044
1048
  }
1045
- publishBackgroundEvent(type, payload, parentSessionId) {
1049
+ async resolveAssociatedTaskIdBySubSession(subSessionId) {
1050
+ if (!subSessionId)
1051
+ return;
1052
+ try {
1053
+ const taskComponent = this.env?.getComponent?.("task");
1054
+ const id = await taskComponent?.getTaskIdBySessionId?.(subSessionId);
1055
+ return typeof id === "number" && id > 0 ? id : undefined;
1056
+ } catch (err) {
1057
+ logger3.debug(`[BackgroundTaskManager] resolveAssociatedTaskIdBySubSession failed for ${subSessionId}: ${err instanceof Error ? err.message : String(err)}`);
1058
+ return;
1059
+ }
1060
+ }
1061
+ publishBackgroundEvent(type, payload, parentSessionId, triggerEventId, replyChannel) {
1046
1062
  if (this.env?.pushEnvEvent) {
1047
1063
  this.env.pushEnvEvent({
1048
1064
  type,
1049
1065
  payload,
1050
1066
  metadata: {
1051
1067
  source: "task.background",
1052
- trigger_session_id: parentSessionId
1068
+ trigger_session_id: parentSessionId,
1069
+ ...triggerEventId ? { triggerEventId } : {},
1070
+ ...replyChannel ? { replyChannel } : {}
1053
1071
  }
1054
1072
  });
1055
- logger3.info(`[BackgroundTaskManager] Event published: ${type}`);
1073
+ logger3.info(`[BackgroundTaskManager] Event published: ${type}`, {
1074
+ triggerEventId,
1075
+ replyChannelChatId: replyChannel?.chatId
1076
+ });
1056
1077
  }
1057
1078
  }
1079
+ publishBackgroundEventWithTrigger(type, payload, parentSessionId, triggerEventId, replyChannel) {
1080
+ this.publishBackgroundEvent(type, payload, parentSessionId, triggerEventId, replyChannel);
1081
+ }
1058
1082
  static parseAskUserErrorFromMessage(error) {
1059
1083
  const rawErrorMessage = error instanceof Error ? error.message : String(error);
1060
1084
  if (error && typeof error === "object" && error.name === "AskUserError" && error.query) {
@@ -1089,7 +1113,7 @@ class BackgroundTaskManager {
1089
1113
  return null;
1090
1114
  }
1091
1115
  async createTask(options) {
1092
- const { parentSessionId, description, prompt, subagentType, timeout, similarTaskIds, existingSubSessionId } = options;
1116
+ const { parentSessionId, description, prompt, subagentType, timeout, similarTaskIds, existingSubSessionId, triggerEventId } = options;
1093
1117
  const taskIdGen = `process_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
1094
1118
  const sessionComponent = this.getSessionComponent();
1095
1119
  if (!sessionComponent) {
@@ -1149,6 +1173,9 @@ class BackgroundTaskManager {
1149
1173
  metadata.larkProfile = profile;
1150
1174
  }
1151
1175
  }
1176
+ if (triggerEventId) {
1177
+ metadata.triggerEventId = triggerEventId;
1178
+ }
1152
1179
  subSession = await sessionComponent.create({
1153
1180
  title: `${description} (@${subagentType} subagent)`,
1154
1181
  parentSessionId,
@@ -1158,6 +1185,7 @@ class BackgroundTaskManager {
1158
1185
  throw new Error("Failed to create sub-session");
1159
1186
  }
1160
1187
  }
1188
+ const replyChannelSnapshot = captureReplyChannelSnapshot();
1161
1189
  const abortController = new AbortController;
1162
1190
  if (this.processRegistries.size >= this.maxProcessRegistries) {
1163
1191
  this.cleanupEmptyProcessRegistries();
@@ -1180,7 +1208,9 @@ class BackgroundTaskManager {
1180
1208
  createdAt,
1181
1209
  lastUpdatedAt: createdAt,
1182
1210
  abortController,
1183
- similarTaskIds
1211
+ similarTaskIds,
1212
+ triggerEventId,
1213
+ replyChannel: replyChannelSnapshot
1184
1214
  };
1185
1215
  this.tasks.set(taskIdGen, task);
1186
1216
  this.abortControllers.set(taskIdGen, abortController);
@@ -1191,7 +1221,7 @@ class BackgroundTaskManager {
1191
1221
  description,
1192
1222
  subagentType
1193
1223
  };
1194
- this.publishBackgroundEvent(BackgroundTaskEventTypes.STARTED, startedPayload, parentSessionId);
1224
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.STARTED, startedPayload, parentSessionId, triggerEventId, replyChannelSnapshot);
1195
1225
  this.executeTask(taskIdGen, prompt, timeout, parentSessionId).catch((err) => {
1196
1226
  logger3.error(`[BackgroundTaskManager] executeTask unhandled rejection`, {
1197
1227
  taskId: taskIdGen,
@@ -1232,11 +1262,8 @@ class BackgroundTaskManager {
1232
1262
  task.result = result;
1233
1263
  const executionTimeMs = task.completedAt - task.startedAt;
1234
1264
  task.associatedTaskId = getCurrentTaskId();
1235
- if (task.associatedTaskId === undefined && result) {
1236
- const match = result.match(/Task\s+#?(\d+)/i);
1237
- if (match) {
1238
- task.associatedTaskId = parseInt(match[1], 10);
1239
- }
1265
+ if (task.associatedTaskId === undefined) {
1266
+ task.associatedTaskId = await this.resolveAssociatedTaskIdBySubSession(task.subSessionId);
1240
1267
  }
1241
1268
  const completedPayload = {
1242
1269
  backgroundTaskId: task.id,
@@ -1248,7 +1275,7 @@ class BackgroundTaskManager {
1248
1275
  executionTimeMs,
1249
1276
  associatedTaskId: task.associatedTaskId
1250
1277
  };
1251
- this.publishBackgroundEvent(BackgroundTaskEventTypes.COMPLETED, completedPayload, parentSessionId || task.parentSessionId);
1278
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.COMPLETED, completedPayload, parentSessionId || task.parentSessionId, task.triggerEventId, task.replyChannel);
1252
1279
  logger3.info(`[BackgroundTaskManager] Task completed successfully`, {
1253
1280
  taskId,
1254
1281
  executionTimeMs
@@ -1262,6 +1289,9 @@ class BackgroundTaskManager {
1262
1289
  task.completedAt = completedAt;
1263
1290
  task.lastUpdatedAt = completedAt;
1264
1291
  task.associatedTaskId = getCurrentTaskId();
1292
+ if (task.associatedTaskId === undefined) {
1293
+ task.associatedTaskId = await this.resolveAssociatedTaskIdBySubSession(task.subSessionId);
1294
+ }
1265
1295
  if (askUserInfo) {
1266
1296
  task.status = "awaiting-user";
1267
1297
  task.error = askUserInfo.rawErrorMessage;
@@ -1285,7 +1315,7 @@ class BackgroundTaskManager {
1285
1315
  ],
1286
1316
  associatedTaskId: task.associatedTaskId
1287
1317
  };
1288
- this.publishBackgroundEvent(BackgroundTaskEventTypes.ASK_USER, askUserPayload, parentSessionId || task.parentSessionId);
1318
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.ASK_USER, askUserPayload, parentSessionId || task.parentSessionId, task.triggerEventId, task.replyChannel);
1289
1319
  logger3.info(`[BackgroundTaskManager] Task awaiting user input (ask_user cascade)`, {
1290
1320
  taskId,
1291
1321
  subSessionId: task.subSessionId,
@@ -1307,7 +1337,7 @@ class BackgroundTaskManager {
1307
1337
  executionTimeMs,
1308
1338
  associatedTaskId: task.associatedTaskId
1309
1339
  };
1310
- this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, parentSessionId || task.parentSessionId);
1340
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, parentSessionId || task.parentSessionId, task.triggerEventId, task.replyChannel);
1311
1341
  } else if (errorMessage.startsWith("Task execution timeout after")) {
1312
1342
  task.status = "timeout";
1313
1343
  const timeoutPayload = {
@@ -1320,7 +1350,7 @@ class BackgroundTaskManager {
1320
1350
  executionTimeMs,
1321
1351
  associatedTaskId: task.associatedTaskId
1322
1352
  };
1323
- this.publishBackgroundEvent(BackgroundTaskEventTypes.TIMEOUT, timeoutPayload, parentSessionId || task.parentSessionId);
1353
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.TIMEOUT, timeoutPayload, parentSessionId || task.parentSessionId, task.triggerEventId, task.replyChannel);
1324
1354
  } else {
1325
1355
  task.status = "failed";
1326
1356
  const failedPayload = {
@@ -1332,7 +1362,7 @@ class BackgroundTaskManager {
1332
1362
  executionTimeMs,
1333
1363
  associatedTaskId: task.associatedTaskId
1334
1364
  };
1335
- this.publishBackgroundEvent(BackgroundTaskEventTypes.FAILED, failedPayload, parentSessionId || task.parentSessionId);
1365
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.FAILED, failedPayload, parentSessionId || task.parentSessionId, task.triggerEventId, task.replyChannel);
1336
1366
  }
1337
1367
  task.error = errorMessage;
1338
1368
  logger3.error(`[BackgroundTaskManager] Task execution error`, {
@@ -1375,7 +1405,7 @@ class BackgroundTaskManager {
1375
1405
  status: task.status,
1376
1406
  associatedTaskId: task.associatedTaskId
1377
1407
  };
1378
- this.publishBackgroundEvent(BackgroundTaskEventTypes.PROGRESS, progressPayload, parentSessionId);
1408
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.PROGRESS, progressPayload, parentSessionId, task.triggerEventId, task.replyChannel);
1379
1409
  }, PROGRESS_INTERVAL);
1380
1410
  this.progressTimers.set(taskId, timer);
1381
1411
  }
@@ -1665,7 +1695,7 @@ ${reuseSection.markdown}`;
1665
1695
  executionTimeMs: task.startedAt ? task.completedAt - task.startedAt : 0,
1666
1696
  associatedTaskId: task.associatedTaskId
1667
1697
  };
1668
- this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, task.parentSessionId);
1698
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, task.parentSessionId, task.triggerEventId, task.replyChannel);
1669
1699
  this.finalizeTask(taskId, "stopped");
1670
1700
  return {
1671
1701
  success: true,
@@ -1756,7 +1786,7 @@ ${reuseSection.markdown}`;
1756
1786
  executionTimeMs: taskSafe.startedAt && taskSafe.completedAt ? taskSafe.completedAt - taskSafe.startedAt : 0,
1757
1787
  associatedTaskId: taskSafe.associatedTaskId
1758
1788
  };
1759
- this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, taskSafe.parentSessionId);
1789
+ this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, taskSafe.parentSessionId, taskSafe.triggerEventId, taskSafe.replyChannel);
1760
1790
  }
1761
1791
  }
1762
1792
  }
@@ -1908,7 +1938,36 @@ No need to poll — just wait for the notification and use \`task_get\` with the
1908
1938
  const params = DelegateToolParameters.parse(args);
1909
1939
  const { description, prompt, subagent_type = "general", timeout, reason, similar_task_ids } = params;
1910
1940
  const parentSessionId = ctx.session_id || "default";
1911
- logger3.info(`[delegate_task] Called: description=${description}, subagent_type=${subagent_type}`);
1941
+ const resolvedTriggerEventId = params.triggerEventId ?? deriveTriggerEventIdFromEnvContext();
1942
+ if (resolvedTriggerEventId && resolvedTriggerEventId !== params.triggerEventId) {
1943
+ logger3.info(`[delegate_task] triggerEventId auto-derived from EnvContext: ${resolvedTriggerEventId}`);
1944
+ }
1945
+ logger3.info(`[delegate_task] Called: description=${description}, subagent_type=${subagent_type}, triggerEventId=${resolvedTriggerEventId ?? "(none)"}`);
1946
+ if (resolvedTriggerEventId) {
1947
+ const store = getSharedEventStore();
1948
+ if (store) {
1949
+ try {
1950
+ const persisted = await store.getEventById(resolvedTriggerEventId);
1951
+ if (!persisted) {
1952
+ return {
1953
+ success: false,
1954
+ output: "",
1955
+ error: `Unknown triggerEventId: ${resolvedTriggerEventId} (not found in EventStore)`,
1956
+ metadata: { execution_time_ms: Date.now() - startTime }
1957
+ };
1958
+ }
1959
+ } catch (err) {
1960
+ return {
1961
+ success: false,
1962
+ output: "",
1963
+ error: `Failed to verify triggerEventId ${resolvedTriggerEventId}: ${err instanceof Error ? err.message : String(err)}`,
1964
+ metadata: { execution_time_ms: Date.now() - startTime }
1965
+ };
1966
+ }
1967
+ } else {
1968
+ logger3.warn(`[delegate_task] triggerEventId=${resolvedTriggerEventId} supplied but no shared EventStore registered; skipping verification`);
1969
+ }
1970
+ }
1912
1971
  const registry2 = getAgentRegistry(taskComponent);
1913
1972
  if (!isKnownSubagentType(subagent_type, registry2)) {
1914
1973
  return {
@@ -1936,12 +1995,51 @@ No need to poll — just wait for the notification and use \`task_get\` with the
1936
1995
  };
1937
1996
  await globalHookManager.execute(TaskHookPoints.DELEGATE_BEFORE, delegateCtx, { sessionId: parentSessionId });
1938
1997
  promptWithTaskInfo = delegateCtx.prompt;
1939
- return await handleBackgroundTask(backgroundTaskManager, parentSessionId, description, promptWithTaskInfo, delegateCtx.subagentType, delegateCtx.timeout, similar_task_ids, params.sub_session_id);
1998
+ return await handleBackgroundTask(backgroundTaskManager, parentSessionId, description, promptWithTaskInfo, delegateCtx.subagentType, delegateCtx.timeout, similar_task_ids, params.sub_session_id, resolvedTriggerEventId);
1940
1999
  }
1941
2000
  };
2001
+ tool.execute = wrapFunction(tool.execute, "delegate.execute", {
2002
+ recordParams: true,
2003
+ recordResult: false,
2004
+ recordError: true
2005
+ });
1942
2006
  return tool;
1943
2007
  }
1944
- async function handleBackgroundTask(backgroundTaskManager, parentSessionId, description, prompt, subagentType, timeout, similarTaskIds, existingSubSessionId) {
2008
+ function _deriveTriggerEventIdFromEnvContext() {
2009
+ const env = getEnvContext();
2010
+ if (!env)
2011
+ return;
2012
+ const envEvent = env.envEvent;
2013
+ const fromMeta = envEvent?.metadata?.triggerEventId;
2014
+ if (typeof fromMeta === "string" && fromMeta.length > 0)
2015
+ return fromMeta;
2016
+ const fromPayload = envEvent?.payload?.triggerEventId;
2017
+ if (typeof fromPayload === "string" && fromPayload.length > 0)
2018
+ return fromPayload;
2019
+ return;
2020
+ }
2021
+ var deriveTriggerEventIdFromEnvContext = wrapFunction(_deriveTriggerEventIdFromEnvContext, "delegate.deriveTriggerEventId", { recordParams: false, recordResult: true, recordError: true });
2022
+ function _captureReplyChannelSnapshot() {
2023
+ const replyChannel = getEnvContext()?.replyChannel;
2024
+ if (!replyChannel)
2025
+ return;
2026
+ if (!replyChannel.chatId && !replyChannel.messageId)
2027
+ return;
2028
+ const params = { ...replyChannel.params ?? {} };
2029
+ if (replyChannel.userId && params.userId === undefined) {
2030
+ params.userId = replyChannel.userId;
2031
+ }
2032
+ return {
2033
+ type: replyChannel.type,
2034
+ appId: replyChannel.appId,
2035
+ chatId: replyChannel.chatId,
2036
+ messageId: replyChannel.messageId,
2037
+ profile: replyChannel.profile,
2038
+ params: Object.keys(params).length > 0 ? params : undefined
2039
+ };
2040
+ }
2041
+ var captureReplyChannelSnapshot = wrapFunction(_captureReplyChannelSnapshot, "delegate.captureReplyChannel", { recordParams: false, recordResult: true, recordError: true });
2042
+ async function handleBackgroundTask(backgroundTaskManager, parentSessionId, description, prompt, subagentType, timeout, similarTaskIds, existingSubSessionId, triggerEventId) {
1945
2043
  const startTime = Date.now();
1946
2044
  try {
1947
2045
  let actualSubagentType = subagentType;
@@ -1966,7 +2064,8 @@ async function handleBackgroundTask(backgroundTaskManager, parentSessionId, desc
1966
2064
  timeout,
1967
2065
  similarTaskIds,
1968
2066
  originalWorkflowName,
1969
- existingSubSessionId
2067
+ existingSubSessionId,
2068
+ triggerEventId
1970
2069
  });
1971
2070
  const output = [
1972
2071
  `✅ Background task accepted`,
@@ -11,20 +11,20 @@ import {
11
11
  } from "./roy-agent-core-7hh0brvs.js";
12
12
  import {
13
13
  BaseComponent
14
- } from "./roy-agent-core-m7dppx2p.js";
14
+ } from "./roy-agent-core-9t675te0.js";
15
15
  import {
16
16
  ToolHookPoints,
17
17
  globalHookManager,
18
18
  init_global_hook_manager
19
- } from "./roy-agent-core-fym6zz5a.js";
19
+ } from "./roy-agent-core-a236mps6.js";
20
20
  import {
21
21
  getCurrentTaskId,
22
22
  init_env_context
23
- } from "./roy-agent-core-n82acszf.js";
23
+ } from "./roy-agent-core-zbb3sk72.js";
24
24
  import {
25
25
  TracedAs,
26
26
  init_decorator
27
- } from "./roy-agent-core-qhjm0bxm.js";
27
+ } from "./roy-agent-core-a9pbda78.js";
28
28
  import {
29
29
  createLogger,
30
30
  init_logger
@@ -10,14 +10,14 @@ import {
10
10
  } from "./roy-agent-core-7hh0brvs.js";
11
11
  import {
12
12
  BaseComponent
13
- } from "./roy-agent-core-m7dppx2p.js";
13
+ } from "./roy-agent-core-9t675te0.js";
14
14
  import {
15
15
  createHook
16
- } from "./roy-agent-core-w457586k.js";
16
+ } from "./roy-agent-core-cw89yj9d.js";
17
17
  import {
18
18
  globalHookManager,
19
19
  init_global_hook_manager
20
- } from "./roy-agent-core-fym6zz5a.js";
20
+ } from "./roy-agent-core-a236mps6.js";
21
21
  import {
22
22
  createLogger,
23
23
  init_logger
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  exports_node_registry_helper,
3
3
  init_node_registry_helper
4
- } from "./roy-agent-core-qz9jb9sp.js";
4
+ } from "./roy-agent-core-hkj5v9xq.js";
5
5
  import {
6
6
  askUserTool,
7
7
  createRunWorkflowTool,
@@ -13,18 +13,18 @@ import {
13
13
  createWorkflowSearchTool,
14
14
  createWorkflowTagListTool,
15
15
  createWorkflowValidateTool
16
- } from "./roy-agent-core-pxyyrxnn.js";
16
+ } from "./roy-agent-core-34je386q.js";
17
17
  import {
18
18
  WorkflowService
19
- } from "./roy-agent-core-j9ejsmsj.js";
19
+ } from "./roy-agent-core-8nv07zgr.js";
20
20
  import {
21
21
  WorkflowEngine,
22
22
  exports_engine,
23
23
  init_engine
24
- } from "./roy-agent-core-x4wev4rn.js";
24
+ } from "./roy-agent-core-rczz7e81.js";
25
25
  import {
26
26
  BaseComponent
27
- } from "./roy-agent-core-m7dppx2p.js";
27
+ } from "./roy-agent-core-9t675te0.js";
28
28
  import {
29
29
  createLogger,
30
30
  init_logger
@@ -104,7 +104,7 @@ class WorkflowComponent extends BaseComponent {
104
104
  return;
105
105
  try {
106
106
  const { NodeRegistry } = await import("../../env/workflow/engine/index.js");
107
- const { registerDecoratorNodeType: registerDecoratorNodeType2 } = await import("./roy-agent-core-wphva2vs.js");
107
+ const { registerDecoratorNodeType: registerDecoratorNodeType2 } = await import("./roy-agent-core-y1n63tse.js");
108
108
  this.nodeRegistry = new NodeRegistry;
109
109
  registerDecoratorNodeType2(this.nodeRegistry);
110
110
  this.decoratorsRegistered = true;
@@ -134,14 +134,14 @@ class WorkflowComponent extends BaseComponent {
134
134
  }
135
135
  async runWorkflow(definition, input, options) {
136
136
  const { NodeRegistry } = await import("../../env/workflow/engine/index.js");
137
- const { registerDecoratorNodeType: registerDecoratorNodeType2 } = await import("./roy-agent-core-wphva2vs.js");
137
+ const { registerDecoratorNodeType: registerDecoratorNodeType2 } = await import("./roy-agent-core-y1n63tse.js");
138
138
  const toolComponent = this.toolComponent || this._workflowEnv?.getComponent("tool");
139
139
  const skillComponent = this._workflowEnv?.getComponent("skill");
140
140
  let agentRunner = this.agentRunner;
141
141
  if (!agentRunner && this._workflowEnv) {
142
142
  const agentComponent = this._workflowEnv.getComponent("agent");
143
143
  if (agentComponent) {
144
- const { AgentComponentAdapter } = await import("./roy-agent-core-s53yya0w.js");
144
+ const { AgentComponentAdapter } = await import("./roy-agent-core-asxe1nt4.js");
145
145
  agentRunner = new AgentComponentAdapter(agentComponent, {}, this.sessionComponent);
146
146
  }
147
147
  }
@@ -163,7 +163,7 @@ class WorkflowComponent extends BaseComponent {
163
163
  if (this.workflowService) {
164
164
  this.workflowService = null;
165
165
  try {
166
- const { closeDatabase } = await import("./roy-agent-core-yw07bvqz.js");
166
+ const { closeDatabase } = await import("./roy-agent-core-9cqpdrnk.js");
167
167
  closeDatabase();
168
168
  componentLogger.info("[WorkflowComponent] SQLite database closed");
169
169
  } catch (error) {
@@ -241,9 +241,9 @@ class WorkflowComponent extends BaseComponent {
241
241
  }
242
242
  async initSqliteService() {
243
243
  try {
244
- const { getDatabase, initializeTables } = await import("./roy-agent-core-yw07bvqz.js");
245
- const { WorkflowRepository } = await import("./roy-agent-core-jq7g5nxz.js");
246
- const { TagRepository } = await import("./roy-agent-core-q3zvkbj6.js");
244
+ const { getDatabase, initializeTables } = await import("./roy-agent-core-9cqpdrnk.js");
245
+ const { WorkflowRepository } = await import("./roy-agent-core-5j4awbgc.js");
246
+ const { TagRepository } = await import("./roy-agent-core-9qn92jhf.js");
247
247
  const db = getDatabase();
248
248
  initializeTables();
249
249
  const workflowRepository = new WorkflowRepository(db);
@@ -309,7 +309,7 @@ class WorkflowComponent extends BaseComponent {
309
309
  this.workflowService = null;
310
310
  this.nodeRegistry = null;
311
311
  try {
312
- const { closeDatabase } = await import("./roy-agent-core-yw07bvqz.js");
312
+ const { closeDatabase } = await import("./roy-agent-core-9cqpdrnk.js");
313
313
  closeDatabase();
314
314
  } catch {}
315
315
  this._status = "stopped";
@@ -4,7 +4,7 @@ import {
4
4
  } from "./roy-agent-core-7hh0brvs.js";
5
5
  import {
6
6
  BaseComponent
7
- } from "./roy-agent-core-m7dppx2p.js";
7
+ } from "./roy-agent-core-9t675te0.js";
8
8
  import {
9
9
  createLogger,
10
10
  init_logger
@@ -10,7 +10,7 @@ import {
10
10
  } from "./roy-agent-core-7hh0brvs.js";
11
11
  import {
12
12
  BaseComponent
13
- } from "./roy-agent-core-m7dppx2p.js";
13
+ } from "./roy-agent-core-9t675te0.js";
14
14
  import {
15
15
  createLogger,
16
16
  init_logger
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  TracedAs,
3
3
  init_decorator
4
- } from "./roy-agent-core-qhjm0bxm.js";
4
+ } from "./roy-agent-core-a9pbda78.js";
5
5
  import {
6
6
  __legacyDecorateClassTS
7
7
  } from "./roy-agent-core-fs0mn2jk.js";
@@ -3,17 +3,17 @@ import {
3
3
  } from "./roy-agent-core-rmn29nrh.js";
4
4
  import {
5
5
  BaseComponent
6
- } from "./roy-agent-core-m7dppx2p.js";
6
+ } from "./roy-agent-core-9t675te0.js";
7
7
  import {
8
8
  globalHookManager,
9
9
  init_global_hook_manager
10
- } from "./roy-agent-core-fym6zz5a.js";
10
+ } from "./roy-agent-core-a236mps6.js";
11
11
  import {
12
12
  init_context
13
- } from "./roy-agent-core-whta3f53.js";
13
+ } from "./roy-agent-core-wn59p0zw.js";
14
14
  import {
15
15
  getEnvContextOrEmpty
16
- } from "./roy-agent-core-n82acszf.js";
16
+ } from "./roy-agent-core-zbb3sk72.js";
17
17
  import {
18
18
  createLogger,
19
19
  init_logger
@@ -7,7 +7,7 @@ import {
7
7
  import {
8
8
  AgentComponentAdapter,
9
9
  init_agent_component_adapter
10
- } from "./roy-agent-core-q8vew3zh.js";
10
+ } from "./roy-agent-core-3h7ajma5.js";
11
11
  import {
12
12
  PythonCallNode,
13
13
  SkillNode,
@@ -17,7 +17,7 @@ import {
17
17
  init_skill_node,
18
18
  init_tool_node,
19
19
  init_workflow_node
20
- } from "./roy-agent-core-jcdsghyr.js";
20
+ } from "./roy-agent-core-yjjqm4ww.js";
21
21
  import {
22
22
  WORKFLOW_SUBMIT_OUTPUT_TOOL_NAME,
23
23
  init_json_schema_to_zod,
@@ -29,11 +29,11 @@ import {
29
29
  import {
30
30
  TemplateResolver,
31
31
  init_template_resolver
32
- } from "./roy-agent-core-yf0ex8y1.js";
32
+ } from "./roy-agent-core-333apxaa.js";
33
33
  import {
34
34
  DAGManager,
35
35
  init_dag_manager
36
- } from "./roy-agent-core-k5csz66y.js";
36
+ } from "./roy-agent-core-tm57tmds.js";
37
37
  import {
38
38
  WorkflowError,
39
39
  createWorkflowEvent,
@@ -49,7 +49,7 @@ import {
49
49
  import {
50
50
  TracedAs,
51
51
  init_decorator
52
- } from "./roy-agent-core-qhjm0bxm.js";
52
+ } from "./roy-agent-core-a9pbda78.js";
53
53
  import {
54
54
  createLogger,
55
55
  init_logger
@@ -11,7 +11,7 @@ import {
11
11
  import {
12
12
  init_decorator,
13
13
  wrapFunction
14
- } from "./roy-agent-core-qhjm0bxm.js";
14
+ } from "./roy-agent-core-a9pbda78.js";
15
15
  import {
16
16
  createLogger,
17
17
  exports_tracer_provider,
@@ -12,11 +12,11 @@ import {
12
12
  } from "./roy-agent-core-7hh0brvs.js";
13
13
  import {
14
14
  BaseComponent
15
- } from "./roy-agent-core-m7dppx2p.js";
15
+ } from "./roy-agent-core-9t675te0.js";
16
16
  import {
17
17
  TracedAs,
18
18
  init_decorator
19
- } from "./roy-agent-core-qhjm0bxm.js";
19
+ } from "./roy-agent-core-a9pbda78.js";
20
20
  import {
21
21
  createLogger,
22
22
  init_logger
@@ -337,6 +337,23 @@ class SessionComponent extends BaseComponent {
337
337
  chatBindingCache = new Map;
338
338
  static CHAT_BINDING_CACHE_TTL_MS = 60000;
339
339
  static CHAT_BINDING_CACHE_MAX = 1000;
340
+ chatBindingLocks = new Map;
341
+ runChatBindingExclusive(chatId, fn) {
342
+ const prev = this.chatBindingLocks.get(chatId) ?? Promise.resolve();
343
+ const run = prev.catch(() => {}).then(() => fn());
344
+ const tail = run.then(() => {
345
+ return;
346
+ }, () => {
347
+ return;
348
+ });
349
+ this.chatBindingLocks.set(chatId, tail);
350
+ tail.then(() => {
351
+ if (this.chatBindingLocks.get(chatId) === tail) {
352
+ this.chatBindingLocks.delete(chatId);
353
+ }
354
+ });
355
+ return run;
356
+ }
340
357
  async upsertChatTaskBinding(opts) {
341
358
  if (!opts.chatId || typeof opts.chatId !== "string") {
342
359
  throw new Error("upsertChatTaskBinding: chatId is required (non-empty string)");
@@ -344,6 +361,9 @@ class SessionComponent extends BaseComponent {
344
361
  if (typeof opts.taskId !== "number" || !Number.isInteger(opts.taskId) || opts.taskId <= 0) {
345
362
  throw new Error(`upsertChatTaskBinding: taskId must be a positive integer, got ${JSON.stringify(opts.taskId)}`);
346
363
  }
364
+ return this.runChatBindingExclusive(opts.chatId, () => this.upsertChatTaskBindingLocked(opts));
365
+ }
366
+ async upsertChatTaskBindingLocked(opts) {
347
367
  const now = new Date().toISOString();
348
368
  const desiredStatus = opts.status ?? "ready";
349
369
  const existing = await this.getChatTaskBinding(opts.chatId);
@@ -959,14 +979,25 @@ __legacyDecorateClassTS([
959
979
  TracedAs("session.upsertChatTaskBinding", {
960
980
  recordParams: false,
961
981
  recordResult: false,
962
- log: true
982
+ log: true,
983
+ resultFilter: (binding) => ({
984
+ chatId: binding?.chatId ?? null,
985
+ taskId: binding?.taskId ?? null,
986
+ status: binding?.status ?? null,
987
+ rebindCount: binding?.rebindCount ?? null
988
+ })
963
989
  })
964
990
  ], SessionComponent.prototype, "upsertChatTaskBinding", null);
965
991
  __legacyDecorateClassTS([
966
992
  TracedAs("session.getChatTaskBinding", {
967
993
  recordParams: false,
968
994
  recordResult: false,
969
- log: false
995
+ log: false,
996
+ resultFilter: (binding) => ({
997
+ found: !!binding,
998
+ taskId: binding?.taskId ?? null,
999
+ status: binding?.status ?? null
1000
+ })
970
1001
  })
971
1002
  ], SessionComponent.prototype, "getChatTaskBinding", null);
972
1003
  __legacyDecorateClassTS([
@@ -5,7 +5,7 @@ import {
5
5
  import {
6
6
  TracedAs,
7
7
  init_decorator
8
- } from "./roy-agent-core-qhjm0bxm.js";
8
+ } from "./roy-agent-core-a9pbda78.js";
9
9
  import {
10
10
  __esm,
11
11
  __legacyDecorateClassTS
@@ -2,7 +2,7 @@ import {
2
2
  getEnvContextOrEmpty,
3
3
  init_env_context,
4
4
  setCurrentTaskId
5
- } from "./roy-agent-core-n82acszf.js";
5
+ } from "./roy-agent-core-zbb3sk72.js";
6
6
  import {
7
7
  createLogger,
8
8
  init_logger