@ai-setting/roy-agent-core 1.5.56 → 1.5.57

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.
@@ -10,7 +10,7 @@ import {
10
10
  parseJSONCWithProtocols,
11
11
  substituteEnvVars,
12
12
  substituteProtocolRefs
13
- } from "../shared/@ai-setting/roy-agent-core-2c9ms8gv.js";
13
+ } from "../shared/@ai-setting/roy-agent-core-prdngx28.js";
14
14
  import"../shared/@ai-setting/roy-agent-core-qxnbvgwe.js";
15
15
  import"../shared/@ai-setting/roy-agent-core-qxhq8ven.js";
16
16
  import"../shared/@ai-setting/roy-agent-core-rgckng3p.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  AgentComponent,
3
3
  AgentComponentConfigSchema
4
- } from "../../shared/@ai-setting/roy-agent-core-qzhkgbyj.js";
4
+ } from "../../shared/@ai-setting/roy-agent-core-6ph5va4n.js";
5
5
  import"../../shared/@ai-setting/roy-agent-core-65yjzwv5.js";
6
6
  import"../../shared/@ai-setting/roy-agent-core-e25xkv53.js";
7
7
  import"../../shared/@ai-setting/roy-agent-core-nx3c3ce2.js";
package/dist/env/index.js CHANGED
@@ -38,14 +38,14 @@ import"../shared/@ai-setting/roy-agent-core-1ce3fqrk.js";
38
38
  import {
39
39
  AgentComponent,
40
40
  AgentComponentConfigSchema
41
- } from "../shared/@ai-setting/roy-agent-core-qzhkgbyj.js";
41
+ } from "../shared/@ai-setting/roy-agent-core-6ph5va4n.js";
42
42
  import"../shared/@ai-setting/roy-agent-core-65yjzwv5.js";
43
43
  import"../shared/@ai-setting/roy-agent-core-e25xkv53.js";
44
44
  import {
45
45
  TaskComponent
46
- } from "../shared/@ai-setting/roy-agent-core-2tna3t64.js";
46
+ } from "../shared/@ai-setting/roy-agent-core-58yjq16r.js";
47
47
  import"../shared/@ai-setting/roy-agent-core-8gxth0eh.js";
48
- import"../shared/@ai-setting/roy-agent-core-eraxs53g.js";
48
+ import"../shared/@ai-setting/roy-agent-core-48jprgqk.js";
49
49
  import"../shared/@ai-setting/roy-agent-core-w4f871e2.js";
50
50
  import"../shared/@ai-setting/roy-agent-core-nx3c3ce2.js";
51
51
  import {
@@ -3,7 +3,7 @@ import {
3
3
  PromptConfigSchema,
4
4
  PromptPathSchema,
5
5
  PromptRenderer
6
- } from "../../shared/@ai-setting/roy-agent-core-grt8ysq2.js";
6
+ } from "../../shared/@ai-setting/roy-agent-core-q16bh5e9.js";
7
7
  import"../../shared/@ai-setting/roy-agent-core-qxnbvgwe.js";
8
8
  import"../../shared/@ai-setting/roy-agent-core-qxhq8ven.js";
9
9
  import"../../shared/@ai-setting/roy-agent-core-rgckng3p.js";
@@ -3,7 +3,7 @@ import {
3
3
  BackgroundTaskManager,
4
4
  createDelegateTool,
5
5
  createStopTool
6
- } from "../../../shared/@ai-setting/roy-agent-core-eraxs53g.js";
6
+ } from "../../../shared/@ai-setting/roy-agent-core-48jprgqk.js";
7
7
  import"../../../shared/@ai-setting/roy-agent-core-nx3c3ce2.js";
8
8
  import"../../../shared/@ai-setting/roy-agent-core-qxnbvgwe.js";
9
9
  import"../../../shared/@ai-setting/roy-agent-core-92z6t4he.js";
@@ -5,11 +5,11 @@ import {
5
5
  TaskPriorityEnum,
6
6
  TaskStatusEnum,
7
7
  TaskTypeEnum
8
- } from "../../shared/@ai-setting/roy-agent-core-2tna3t64.js";
8
+ } from "../../shared/@ai-setting/roy-agent-core-58yjq16r.js";
9
9
  import {
10
10
  TaskEntityEventTypes
11
11
  } from "../../shared/@ai-setting/roy-agent-core-8gxth0eh.js";
12
- import"../../shared/@ai-setting/roy-agent-core-eraxs53g.js";
12
+ import"../../shared/@ai-setting/roy-agent-core-48jprgqk.js";
13
13
  import {
14
14
  SQLiteTaskStore,
15
15
  getDefaultTaskDbPath
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  PromptStore,
4
4
  getBuiltInPrompt,
5
5
  getBuiltInPromptNames
6
- } from "./shared/@ai-setting/roy-agent-core-grt8ysq2.js";
6
+ } from "./shared/@ai-setting/roy-agent-core-q16bh5e9.js";
7
7
  import {
8
8
  LLMComponent,
9
9
  LLMConfigSchema,
@@ -14,7 +14,7 @@ import {
14
14
  } from "./shared/@ai-setting/roy-agent-core-sfqx98j6.js";
15
15
  import {
16
16
  ConfigComponent
17
- } from "./shared/@ai-setting/roy-agent-core-2c9ms8gv.js";
17
+ } from "./shared/@ai-setting/roy-agent-core-prdngx28.js";
18
18
  import {
19
19
  CommandsComponent
20
20
  } from "./shared/@ai-setting/roy-agent-core-7wdjpbcr.js";
@@ -114,7 +114,7 @@ import"./shared/@ai-setting/roy-agent-core-1ce3fqrk.js";
114
114
  import {
115
115
  AgentComponent,
116
116
  AgentComponentConfigSchema
117
- } from "./shared/@ai-setting/roy-agent-core-qzhkgbyj.js";
117
+ } from "./shared/@ai-setting/roy-agent-core-6ph5va4n.js";
118
118
  import {
119
119
  createInvokeConfig,
120
120
  invoke,
@@ -127,9 +127,9 @@ import {
127
127
  } from "./shared/@ai-setting/roy-agent-core-e25xkv53.js";
128
128
  import {
129
129
  TaskComponent
130
- } from "./shared/@ai-setting/roy-agent-core-2tna3t64.js";
130
+ } from "./shared/@ai-setting/roy-agent-core-58yjq16r.js";
131
131
  import"./shared/@ai-setting/roy-agent-core-8gxth0eh.js";
132
- import"./shared/@ai-setting/roy-agent-core-eraxs53g.js";
132
+ import"./shared/@ai-setting/roy-agent-core-48jprgqk.js";
133
133
  import {
134
134
  SQLiteTaskStore,
135
135
  getDefaultTaskDbPath
@@ -9,8 +9,10 @@ import {
9
9
  init_global_hook_manager
10
10
  } from "./roy-agent-core-7tp56w6n.js";
11
11
  import {
12
- init_env_context,
13
- setCurrentTaskId
12
+ init_context
13
+ } from "./roy-agent-core-qg4rma4c.js";
14
+ import {
15
+ getCurrentTaskId
14
16
  } from "./roy-agent-core-y5d04fm3.js";
15
17
  import {
16
18
  TracedAs,
@@ -26,8 +28,8 @@ import {
26
28
 
27
29
  // src/env/task/delegate/delegate-tool.ts
28
30
  init_logger();
29
- init_env_context();
30
31
  init_decorator();
32
+ init_context();
31
33
  import { z } from "zod";
32
34
 
33
35
  // src/env/task/delegate/subagent-resolver.ts
@@ -205,10 +207,7 @@ var DelegateToolParameters = z.object({
205
207
  description: z.string().describe("A short (3-5 words) description of the task"),
206
208
  prompt: z.string().describe("The task for the agent to perform"),
207
209
  subagent_type: z.string().describe("The type of specialized agent to use for this task").default("general"),
208
- background: z.boolean().describe("Whether to run the task in background. If true, returns immediately and notifies when complete (default: false)").default(false),
209
210
  timeout: z.number().describe("Task timeout in milliseconds. Default: 1800000 (30 minutes). If set, task will be terminated after timeout.").optional(),
210
- cleanup: z.enum(["delete", "keep"]).describe("Whether to delete sub session after completion. 'delete' removes the session, 'keep' retains it (default: keep)").default("keep").optional(),
211
- task_id: z.number().describe("Optional task ID to associate with this delegate task, for tracking in operation records").optional(),
212
211
  reason: z.string().describe("Brief reason for calling this tool (max 30 chars, e.g., 'Delegate refactor task')").optional()
213
212
  });
214
213
  var DEFAULT_TIMEOUT = 1800000;
@@ -239,8 +238,8 @@ class BackgroundTaskManager {
239
238
  }
240
239
  }
241
240
  async createTask(options) {
242
- const { parentSessionId, description, prompt, subagentType, timeout, cleanup, taskId } = options;
243
- const taskIdGen = `task_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
241
+ const { parentSessionId, description, prompt, subagentType, timeout } = options;
242
+ const taskIdGen = `process_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
244
243
  const sessionComponent = this.getSessionComponent();
245
244
  if (!sessionComponent) {
246
245
  throw new Error("SessionComponent not found");
@@ -254,9 +253,6 @@ class BackgroundTaskManager {
254
253
  created_by: "subagent",
255
254
  task_description: description
256
255
  };
257
- if (taskId) {
258
- metadata.task_id = taskId;
259
- }
260
256
  const subSession = await sessionComponent.create({
261
257
  title: `${description} (@${subagentType} subagent)`,
262
258
  metadata
@@ -273,8 +269,7 @@ class BackgroundTaskManager {
273
269
  subagentType,
274
270
  status: "pending",
275
271
  createdAt: Date.now(),
276
- abortController,
277
- taskId
272
+ abortController
278
273
  };
279
274
  this.tasks.set(taskIdGen, task);
280
275
  this.abortControllers.set(taskIdGen, abortController);
@@ -283,11 +278,10 @@ class BackgroundTaskManager {
283
278
  subSessionId: subSession.id,
284
279
  parentSessionId,
285
280
  description,
286
- subagentType,
287
- associatedTaskId: taskId
281
+ subagentType
288
282
  };
289
283
  this.publishBackgroundEvent(BackgroundTaskEventTypes.STARTED, startedPayload, parentSessionId);
290
- this.executeTask(taskIdGen, prompt, timeout, cleanup, parentSessionId).catch((err) => {
284
+ this.executeTask(taskIdGen, prompt, timeout, parentSessionId).catch((err) => {
291
285
  logger.error(`[BackgroundTaskManager] executeTask unhandled rejection`, {
292
286
  taskId: taskIdGen,
293
287
  error: err instanceof Error ? err.message : String(err)
@@ -295,7 +289,7 @@ class BackgroundTaskManager {
295
289
  });
296
290
  return { taskId: taskIdGen, subSessionId: subSession.id };
297
291
  }
298
- async executeTask(taskId, prompt, timeout, cleanup, parentSessionId) {
292
+ async executeTask(taskId, prompt, timeout, parentSessionId) {
299
293
  const task = this.tasks.get(taskId);
300
294
  if (!task) {
301
295
  logger.warn(`[BackgroundTaskManager] executeTask: Task not found`, { taskId });
@@ -321,6 +315,13 @@ class BackgroundTaskManager {
321
315
  task.completedAt = Date.now();
322
316
  task.result = result;
323
317
  const executionTimeMs = task.completedAt - task.startedAt;
318
+ task.associatedTaskId = getCurrentTaskId();
319
+ if (task.associatedTaskId === undefined && result) {
320
+ const match = result.match(/Task\s+#?(\d+)/i);
321
+ if (match) {
322
+ task.associatedTaskId = parseInt(match[1], 10);
323
+ }
324
+ }
324
325
  const completedPayload = {
325
326
  backgroundTaskId: task.id,
326
327
  subSessionId: task.subSessionId,
@@ -329,7 +330,7 @@ class BackgroundTaskManager {
329
330
  result: task.result,
330
331
  description: task.description,
331
332
  executionTimeMs,
332
- associatedTaskId: task.taskId
333
+ associatedTaskId: task.associatedTaskId
333
334
  };
334
335
  this.publishBackgroundEvent(BackgroundTaskEventTypes.COMPLETED, completedPayload, parentSessionId || task.parentSessionId);
335
336
  logger.info(`[BackgroundTaskManager] Task completed successfully`, {
@@ -340,6 +341,7 @@ class BackgroundTaskManager {
340
341
  const errorMessage = error instanceof Error ? error.message : String(error);
341
342
  const isStoppedError = errorMessage.toLowerCase().includes("stopped") || errorMessage.toLowerCase().includes("aborted");
342
343
  const executionTimeMs = task.startedAt ? Date.now() - task.startedAt : 0;
344
+ task.associatedTaskId = getCurrentTaskId();
343
345
  if (isStoppedError) {
344
346
  task.status = "stopped";
345
347
  const stoppedPayload = {
@@ -350,7 +352,7 @@ class BackgroundTaskManager {
350
352
  description: task.description,
351
353
  error: errorMessage,
352
354
  executionTimeMs,
353
- associatedTaskId: task.taskId
355
+ associatedTaskId: task.associatedTaskId
354
356
  };
355
357
  this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, parentSessionId || task.parentSessionId);
356
358
  } else if (errorMessage.includes("timeout")) {
@@ -363,7 +365,7 @@ class BackgroundTaskManager {
363
365
  description: task.description,
364
366
  error: errorMessage,
365
367
  executionTimeMs,
366
- associatedTaskId: task.taskId
368
+ associatedTaskId: task.associatedTaskId
367
369
  };
368
370
  this.publishBackgroundEvent(BackgroundTaskEventTypes.TIMEOUT, timeoutPayload, parentSessionId || task.parentSessionId);
369
371
  } else {
@@ -373,10 +375,9 @@ class BackgroundTaskManager {
373
375
  subSessionId: task.subSessionId,
374
376
  parentSessionId: parentSessionId || task.parentSessionId,
375
377
  status: "failed",
376
- description: task.description,
377
378
  error: errorMessage,
378
379
  executionTimeMs,
379
- associatedTaskId: task.taskId
380
+ associatedTaskId: task.associatedTaskId
380
381
  };
381
382
  this.publishBackgroundEvent(BackgroundTaskEventTypes.FAILED, failedPayload, parentSessionId || task.parentSessionId);
382
383
  }
@@ -453,7 +454,8 @@ ${prompt}
453
454
  ## 执行规范
454
455
  - 使用可用工具完成任务
455
456
  - 返回清晰的任务执行结果摘要
456
- - 适时调用 task_operation_create 记录进展`;
457
+ - 适时调用 task_operation_create 记录进展
458
+ - 如果你通过 task_create 创建了任务,请在最终响应中明确报告 task_id(例如:Task #627)`;
457
459
  if (!agentComponent) {
458
460
  throw new Error("AgentComponent not found");
459
461
  }
@@ -471,15 +473,26 @@ ${prompt}
471
473
  return new Promise((resolve, reject) => {
472
474
  const timer = setTimeout(() => {
473
475
  reject(new Error(`Task execution timeout after ${timeoutMs}ms`));
476
+ try {
477
+ agentComponent?.abort?.(subagentType);
478
+ } catch {}
474
479
  }, timeoutMs);
475
480
  signal?.addEventListener("abort", () => {
476
481
  clearTimeout(timer);
482
+ try {
483
+ const env = this.env;
484
+ const agentComp = env?.getComponent?.("agent");
485
+ if (agentComp?.abort) {
486
+ agentComp.abort(subagentType);
487
+ }
488
+ } catch {}
477
489
  reject(new Error("Task execution stopped"));
478
490
  });
479
491
  this.env.handle_query?.(delegateQuery, {
480
492
  sessionId,
481
493
  deniedTools: deniedTools.length > 0 ? deniedTools : undefined,
482
- agentType: subagentType
494
+ agentType: subagentType,
495
+ abort: signal
483
496
  })?.then((result) => {
484
497
  clearTimeout(timer);
485
498
  resolve(result);
@@ -512,7 +525,7 @@ ${prompt}
512
525
  description: task.description,
513
526
  error: "Task stopped by user",
514
527
  executionTimeMs: task.startedAt ? task.completedAt - task.startedAt : 0,
515
- associatedTaskId: task.taskId
528
+ associatedTaskId: task.associatedTaskId
516
529
  };
517
530
  this.publishBackgroundEvent(BackgroundTaskEventTypes.STOPPED, stoppedPayload, task.parentSessionId);
518
531
  return { success: true, task, message: "Task has been stopped" };
@@ -554,7 +567,8 @@ function getAgentRegistry(taskComponent) {
554
567
  }
555
568
  function createDelegateTool(taskComponent) {
556
569
  const env = taskComponent.env;
557
- const backgroundTaskManager = new BackgroundTaskManager(env);
570
+ const existingManager = taskComponent.backgroundTaskManager;
571
+ const backgroundTaskManager = existingManager ?? new BackgroundTaskManager(env);
558
572
  const registry = getAgentRegistry(taskComponent);
559
573
  taskComponent._backgroundTaskManager = backgroundTaskManager;
560
574
  const tool = {
@@ -569,26 +583,35 @@ When using the delegate_task tool, you must specify a subagent_type parameter to
569
583
  ## When to use delegate_task:
570
584
  - When you need to perform complex, multi-step tasks that require independent execution
571
585
  - When you need to explore a codebase thoroughly (use "explore" subagent)
572
- - When you need to run long-running tasks without blocking the main agent (use background=true)
586
+ - When you need to run long-running tasks without blocking the main agent
587
+
588
+ ## How it works:
589
+ This tool **always runs in background mode**. It returns immediately with:
590
+ - **bgProcessId**: Internal background process ID (use with \`stop_task\` to cancel, different from Task #)
591
+ - **subSessionId**: Sub-agent session ID (use with \`session_get\` to see full execution log, always retained)
592
+
593
+ **After delegating, just WAIT for the notification.** The background process will automatically notify you when:
594
+ - ✅ **completed** — with \`bgprocess:\` + \`Task #\` (if it created a task) + result summary
595
+ - ⏱️ **timeout** — with \`bgprocess:\` + \`Task #\` (if available)
596
+ - ❌ **failed** — with \`bgprocess:\` + \`Task #\` + error details
597
+ - \uD83D\uDED1 **stopped** — with \`bgprocess:\` + \`Task #\`
598
+
599
+ The sub-agent runs independently and creates its own tasks via \`task_create\`.
600
+ No need to poll — just wait for the notification and use \`task_get\` with the Task # reported in it.
573
601
 
574
602
  ## Parameters:
575
603
  - **description**: A short (3-5 words) description of the task
576
604
  - **prompt**: The task for the agent to perform
577
605
  - **subagent_type**: The type of specialized agent to use (e.g., "general", "explore")
578
- - **background**: Whether to run in background (default: false)
579
- - **timeout**: Task timeout in milliseconds (optional)
580
- - **task_id**: Optional task ID to associate with this delegate task
581
-
582
- ## Synchronous vs Background mode:
583
- **Synchronous mode (default)**: Waits for subagent to complete and returns result directly
584
- **Background mode (background=true)**: Returns immediately with "accepted" status, runs independently`,
606
+ - **timeout**: Task timeout in milliseconds (optional, default 30 minutes)
607
+ - **reason**: Brief reason for calling this tool (optional)`,
585
608
  parameters: DelegateToolParameters,
586
609
  execute: async (args, ctx) => {
587
610
  const startTime = Date.now();
588
611
  const params = DelegateToolParameters.parse(args);
589
- const { description, prompt, subagent_type = "general", background = false, timeout, cleanup, task_id, reason } = params;
612
+ const { description, prompt, subagent_type = "general", timeout, reason } = params;
590
613
  const parentSessionId = ctx.session_id || "default";
591
- logger.info(`[delegate_task] Called: description=${description}, subagent_type=${subagent_type}, background=${background}, task_id=${task_id}`);
614
+ logger.info(`[delegate_task] Called: description=${description}, subagent_type=${subagent_type}`);
592
615
  const registry2 = getAgentRegistry(taskComponent);
593
616
  if (!isKnownSubagentType(subagent_type, registry2)) {
594
617
  return {
@@ -601,12 +624,9 @@ When using the delegate_task tool, you must specify a subagent_type parameter to
601
624
  const tagService = taskComponent.getTagService();
602
625
  let promptWithTaskInfo = prompt;
603
626
  const delegateCtx = {
604
- taskId: task_id,
605
627
  prompt: promptWithTaskInfo,
606
628
  subagentType: subagent_type,
607
- background,
608
629
  timeout,
609
- cleanup,
610
630
  reason,
611
631
  tagService
612
632
  };
@@ -619,212 +639,31 @@ When using the delegate_task tool, you must specify a subagent_type parameter to
619
639
  };
620
640
  await globalHookManager.execute(TaskHookPoints.DELEGATE_BEFORE, hookCtx, { sessionId: parentSessionId });
621
641
  promptWithTaskInfo = delegateCtx.prompt;
622
- if (background) {
623
- return await handleBackgroundTask(taskComponent, backgroundTaskManager, parentSessionId, description, promptWithTaskInfo, delegateCtx.subagentType, delegateCtx.timeout, delegateCtx.cleanup, delegateCtx.taskId);
624
- } else {
625
- return await handleSyncTask(taskComponent, parentSessionId, description, promptWithTaskInfo, delegateCtx.subagentType, delegateCtx.timeout, delegateCtx.taskId);
626
- }
642
+ return await handleBackgroundTask(backgroundTaskManager, parentSessionId, description, promptWithTaskInfo, delegateCtx.subagentType, delegateCtx.timeout);
627
643
  }
628
644
  };
629
645
  return tool;
630
646
  }
631
- async function handleSyncTask(taskComponent, parentSessionId, description, prompt, subagentType, timeout, taskId) {
647
+ async function handleBackgroundTask(backgroundTaskManager, parentSessionId, description, prompt, subagentType, timeout) {
632
648
  const startTime = Date.now();
633
649
  try {
634
- const sessionComponent = taskComponent.env?.getComponent?.("session");
635
- if (!sessionComponent) {
636
- return {
637
- success: false,
638
- output: "",
639
- error: "SessionComponent not found",
640
- metadata: { execution_time_ms: Date.now() - startTime }
641
- };
642
- }
643
- const parentSession = await sessionComponent.get(parentSessionId);
644
- if (!parentSession) {
645
- return {
646
- success: false,
647
- output: "",
648
- error: `Parent session not found: ${parentSessionId}`,
649
- metadata: { execution_time_ms: Date.now() - startTime }
650
- };
651
- }
652
- const metadata = {
653
- subagent_type: subagentType,
654
- created_by: "subagent",
655
- task_description: description
656
- };
657
- if (taskId) {
658
- metadata.task_id = taskId;
659
- }
660
- const subSession = await sessionComponent.create({
661
- parentID: parentSessionId,
662
- title: `${description} (@${subagentType} subagent)`,
663
- metadata
664
- });
665
- if (!subSession) {
666
- return {
667
- success: false,
668
- output: "",
669
- error: "Failed to create sub-session",
670
- metadata: { execution_time_ms: Date.now() - startTime }
671
- };
672
- }
673
- const agentComponent = taskComponent.env?.getComponent?.("agent");
674
- const registry = getAgentRegistry(taskComponent);
675
- const resolved = await resolveSubAgentConfig(subagentType, { registry });
676
- const subAgent = getSubAgentSpec(subagentType);
677
- let basePrompt = resolved?.basePrompt || subAgent?.promptOverride || `You are a subagent. Complete this task: {task_description}`;
678
- const deniedTools = resolved?.deniedTools || subAgent?.deniedTools || [];
679
- const allowedTools = resolved?.allowedTools || subAgent?.allowedTools;
680
- let fullPrompt = basePrompt.replace(/{task_description}/g, description || "N/A");
681
- fullPrompt += `
682
-
683
- ---
684
-
685
- # Session Info
686
- - task_id: ${taskId || "N/A"}
687
- - session_id: ${subSession.id}
688
-
689
- ---
690
-
691
- ## 用户指令
692
- ${prompt}
693
-
694
- ---
695
-
696
- ## 执行规范
697
- - 使用可用工具完成任务
698
- - 返回清晰的任务执行结果摘要
699
- - 适时调用 task_operation_create 记录进展`;
700
- if (!agentComponent) {
701
- return {
702
- success: false,
703
- output: "",
704
- error: "AgentComponent not found",
705
- metadata: { execution_time_ms: Date.now() - startTime }
706
- };
707
- }
708
- const subAgentForRegister = subAgent ? { ...subAgent, allowedTools } : allowedTools ? {
709
- id: subagentType,
710
- name: subagentType,
711
- mode: "subagent",
712
- description: resolved?.description || "Custom agent",
713
- allowedTools,
714
- deniedTools
715
- } : undefined;
716
- ensureSubAgentRegistered(agentComponent, subagentType, basePrompt, subAgentForRegister, deniedTools);
717
- const isWorkflowAgent = registry?.get(subagentType)?.type === "workflow";
718
- const delegateQuery = isWorkflowAgent ? prompt : fullPrompt;
719
- const timeoutMs = timeout || DEFAULT_TIMEOUT;
720
- let result;
721
- try {
722
- result = await Promise.race([
723
- taskComponent.env?.handle_query?.(delegateQuery, {
724
- sessionId: subSession.id,
725
- deniedTools: deniedTools.length > 0 ? deniedTools : undefined,
726
- agentType: subagentType
727
- }),
728
- new Promise((_, reject) => setTimeout(() => reject(new Error(`Task execution timeout after ${timeoutMs}ms`)), timeoutMs))
729
- ]);
730
- } catch (error) {
731
- result = error instanceof Error ? error.message : String(error);
732
- }
733
- await sessionComponent.addMessage(subSession.id, {
734
- role: "assistant",
735
- content: result
736
- });
737
- let taskNotification = "";
738
- if (taskId) {
739
- taskNotification = `
740
-
741
- ---
742
- \uD83D\uDCCB [委托任务完成] 已委托子智能体完成 Task #${taskId} 的处理工作,请知悉。可通过 task_get #${taskId} 查看最新状态。`;
743
- }
744
- setCurrentTaskId(undefined);
745
- logger.info(`[delegate_task.sync] Task completed, currentTaskId cleared from EnvContext${taskId ? ` (was: #${taskId})` : ""}`);
746
- return {
747
- success: true,
748
- output: result + taskNotification + `
749
-
750
- ` + [
751
- "<task_metadata>",
752
- `session_id: ${subSession.id}`,
753
- `subagent_type: ${subagentType}`,
754
- "</task_metadata>"
755
- ].join(`
756
- `),
757
- metadata: {
758
- execution_time_ms: Date.now() - startTime,
759
- sessionId: subSession.id
760
- }
761
- };
762
- } catch (error) {
763
- setCurrentTaskId(undefined);
764
- logger.info(`[delegate_task.sync] Error occurred, currentTaskId cleared from EnvContext${taskId ? ` (was: #${taskId})` : ""}`);
765
- return {
766
- success: false,
767
- output: "",
768
- error: error instanceof Error ? error.message : String(error),
769
- metadata: { execution_time_ms: Date.now() - startTime }
770
- };
771
- }
772
- }
773
- async function handleBackgroundTask(taskComponent, backgroundTaskManager, parentSessionId, description, prompt, subagentType, timeout, cleanup, taskId) {
774
- const startTime = Date.now();
775
- try {
776
- let associatedTaskId = taskId;
777
- if (!associatedTaskId) {
778
- const newTask = await taskComponent.createTask({
779
- title: description,
780
- description: `Background task delegated to ${subagentType} subagent`,
781
- priority: "medium",
782
- goals_and_expected_deliverables: prompt,
783
- sessionId: parentSessionId,
784
- project_path: "unknown",
785
- context: "unknown"
786
- });
787
- associatedTaskId = newTask.id;
788
- await taskComponent.createOperation({
789
- taskId: associatedTaskId,
790
- sessionId: parentSessionId,
791
- actionType: "create",
792
- actionTitle: `Delegated to ${subagentType} subagent`,
793
- actionDescription: `Background task started: ${description}`
794
- });
795
- } else {
796
- await taskComponent.updateTask(associatedTaskId, {
797
- status: "active",
798
- current_status: `Running ${subagentType} subagent`
799
- });
800
- await taskComponent.createOperation({
801
- taskId: associatedTaskId,
802
- sessionId: parentSessionId,
803
- actionType: "progress",
804
- actionTitle: `Started ${subagentType} subagent`,
805
- actionDescription: `Background task: ${description}`
806
- });
807
- }
808
- const { taskId: bgTaskId, subSessionId } = await backgroundTaskManager.createTask({
650
+ const { taskId: bgProcessId, subSessionId } = await backgroundTaskManager.createTask({
809
651
  parentSessionId,
810
652
  description,
811
653
  prompt,
812
654
  subagentType,
813
- timeout,
814
- cleanup,
815
- taskId: associatedTaskId
655
+ timeout
816
656
  });
817
657
  const output = [
818
658
  `✅ Background task accepted`,
819
659
  "",
820
- `\uD83D\uDCCB Task ID: ${bgTaskId}`,
660
+ `\uD83D\uDCCB Process ID: ${bgProcessId}`,
821
661
  `\uD83D\uDCDD Description: ${description}`,
822
662
  `\uD83E\uDD16 Sub-agent: ${subagentType}`,
823
- associatedTaskId ? `\uD83D\uDCCC Associated Task: #${associatedTaskId}` : "",
824
663
  `⏱️ Timeout: ${(timeout || DEFAULT_TIMEOUT) / 1000}s`,
825
664
  "",
826
- `Use stop_task with task_id="${bgTaskId}" to cancel this task.`
827
- ].filter(Boolean).join(`
665
+ `Use stop_task with task_id="${bgProcessId}" to cancel this task.`
666
+ ].join(`
828
667
  `);
829
668
  return {
830
669
  success: true,
@@ -834,8 +673,7 @@ async function handleBackgroundTask(taskComponent, backgroundTaskManager, parent
834
673
  sessionId: subSessionId,
835
674
  background: true,
836
675
  status: "accepted",
837
- task_id: associatedTaskId,
838
- bgTaskId
676
+ bgProcessId
839
677
  }
840
678
  };
841
679
  } catch (error) {
@@ -5,7 +5,7 @@ import {
5
5
  BackgroundTaskManager,
6
6
  createDelegateTool,
7
7
  createStopTool
8
- } from "./roy-agent-core-eraxs53g.js";
8
+ } from "./roy-agent-core-48jprgqk.js";
9
9
  import {
10
10
  SQLiteTaskStore,
11
11
  getDefaultTaskDbPath
@@ -572,6 +572,7 @@ class AgentComponent extends BaseComponent {
572
572
  const parentEnvContext = getEnvContext();
573
573
  const envContext = {
574
574
  ...parentEnvContext,
575
+ agentContext: parentEnvContext?.agentContext ?? {},
575
576
  traceId: effectiveContext.metadata?.traceId,
576
577
  sessionId: effectiveContext.sessionId,
577
578
  userId: effectiveContext.metadata?.userId ?? "",
@@ -339,7 +339,7 @@ class FileSource {
339
339
  }
340
340
  try {
341
341
  this.watcherHandle = fsSync.watch(this.filePath, async (eventType) => {
342
- if (eventType === "change") {
342
+ if (eventType === "change" || eventType === "rename") {
343
343
  await new Promise((r) => setTimeout(r, 50));
344
344
  await this.reloadFromFile();
345
345
  }
@@ -163,18 +163,21 @@ var builtInPrompts = {
163
163
 
164
164
  **roy** 的提示词中内置了任务生命周期——它知道如何使用 \`task_create\`、\`task_update\`、\`task_operation_create\` 和 \`task_complete\` 来跟踪每项工作。
165
165
 
166
- 推荐使用(长时间运行任务使用后台模式):
166
+ 推荐使用:
167
167
  \`\`\`
168
168
  delegate_task(
169
169
  description="简要 3-5 字摘要",
170
170
  prompt="详细说明。
171
171
 
172
172
  重要:遵循任务作为一等公民原则 — 使用 \`task_create\` 创建任务,使用 \`task_update\` 跟踪进度,使用 \`task_operation_create\` 记录里程碑,并使用 \`task_complete\` 完成任务。",
173
- subagent_type="roy",
174
- background=true
173
+ subagent_type="roy"
175
174
  )
176
175
  \`\`\`
177
176
 
177
+ > **提示**:\`delegate_task\` 现在**总是后台模式**,立即返回。父 agent 通过返回的 \`task_id\` 用 \`task_get\` 查询进度。
178
+ > - delegate_task 内部自动创建关联 Task,调用方无需传入 \`task_id\`
179
+ > - sub-session 始终保留(不会自动删除),可通过 \`session_get\` 查看完整执行日志
180
+
178
181
  ### \uD83E\uDD48 次选:\`strict-task-agent\`(需要严格验证时使用)
179
182
  结构化 Plan → Execute → Verify 工作流智能体,内置自动重试(最多 8 次)。在以下情况使用:
180
183
  - **复杂功能开发** — 需要严格的 Plan→Execute→Verify 纪律
@@ -468,7 +471,7 @@ roy-agent <command> <subcmd> --help
468
471
  | \`task_list\` | List tasks with optional filters for status/priority |
469
472
  | \`task_complete\` | Mark a task as completed (progress=100, status=completed). Creates an operation record. |
470
473
  | \`task_delete\` | Delete a task and all its operation records |
471
- | \`delegate_task\` | Delegate complex tasks to a sub-agent (sync or background mode) |
474
+ | \`delegate_task\` | Delegate complex tasks to a sub-agent (always background mode) |
472
475
  | \`stop_task\` | Stop a running background task |
473
476
  | \`task_operation_create\` | Create operation records to track progress/milestones |
474
477
  | \`task_operation_list\` | List operation records for a task |
@@ -510,9 +513,8 @@ Follow this lifecycle for every task:
510
513
 
511
514
  ┌─────────────────────────────────────────────────────────────────┐
512
515
  │ 3. EXECUTE (delegate_task) │
513
- │ - Delegate sub-tasks to sub-agents for parallel execution
514
- │ - Use background mode (background=true) for long-running
515
- │ tasks to avoid blocking │
516
+ │ - delegate_task 总是后台执行,立即返回
517
+ │ - 通过返回的 task_id 主动用 task_get 查询进度
516
518
  │ │
517
519
  │ Background mode is recommended when: │
518
520
  │ - Task duration > 5 minutes │
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-setting/roy-agent-core",
3
- "version": "1.5.56",
3
+ "version": "1.5.57",
4
4
  "type": "module",
5
5
  "description": "Core SDK for roy-agent - Environment, Components, Tools, Sessions, Tasks",
6
6
  "main": "./dist/index.js",