Skip to content

feat(core): add run_in_background support for Agent tool#3076

Draft
tanzhenxin wants to merge 7 commits intomainfrom
feat/background-subagent
Draft

feat(core): add run_in_background support for Agent tool#3076
tanzhenxin wants to merge 7 commits intomainfrom
feat/background-subagent

Conversation

@tanzhenxin
Copy link
Copy Markdown
Collaborator

@tanzhenxin tanzhenxin commented Apr 10, 2026

TLDR

Add run_in_background parameter to the Agent tool, enabling sub-agents to run asynchronously. When set to true, the parent agent receives an immediate launch confirmation and continues working. A notification is delivered to the parent conversation when the background agent completes.

Ported from the claw-code reference implementation's background agent feature.

Screenshots / Video Demo

N/A — the feature is exercised via model tool calls, not a visual UI change. The observable behavior is:

  1. Agent tool returns immediately with "Background agent launched: ..."
  2. After the background agent finishes, a notification appears in the conversation
  3. The model processes the notification and reports results to the user

Dive Deeper

Key design decisions

  • Deny-by-default permissions for background agents — background agents cannot show interactive permission prompts. Instead of YOLO (auto-approve everything), tool calls that need approval are auto-denied after PermissionRequest hooks have had a chance to decide. This matches claw-code's shouldAvoidPermissionPrompts approach: hooks first, then auto-deny, never deadlock.
  • Independent AbortControllers — background agents survive ESC cancellation of the parent's current turn. They're only aborted on session exit (via Config.shutdown()).
  • State race guardscomplete() and fail() are no-ops if the agent has already been cancelled, preventing race conditions during concurrent shutdown.
  • Between-turn notification delivery — notifications are queued and submitted when the parent goes idle, avoiding mid-reasoning interruption.
  • Typed notification path — notifications use SendMessageType.Notification with structured { displayText, modelText } payloads instead of stringly-typed encoding. The display item shows a concise summary while the model receives the full result.
  • Session resume support — notifications are recorded as user-role messages with subtype: 'notification' and a displayText payload, so both the API history and UI display are restored correctly on resume.
  • Non-interactive mode rejectionrun_in_background is rejected in headless mode because the notification delivery infrastructure requires the interactive message loop.

Reviewer Test Plan

Build and bundle first: npm run build && npm run bundle

Start an interactive session with --approval-mode yolo. Ask the model to launch a background agent (e.g. "use the Agent tool with run_in_background true, Explore subagent, prompt list files"). You should see:

  1. The tool widget shows "Running in background" and the model responds immediately
  2. A notification appears: ● Background agent "..." completed.
  3. The model picks up the notification and summarizes the result

Unit tests: cd packages/core && npx vitest run src/agents/background-tasks.test.ts src/tools/agent.test.ts

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Follow-up work

  • Headless mode support — add a notification drain loop in nonInteractiveCli.ts so background agents can complete and deliver results before the process exits. See .claude/todos/background-agent-headless-mode.md.
  • UI visibility — register background agents with AgentViewContext so they appear as tabs in the tab bar with live output, status indicators, and keyboard navigation. Reuses the existing Arena multi-agent display infrastructure. See .claude/design/background-agent-ui.md.
  • Cron notification unification — migrate cron from its own cronQueueRef to the same typed notification queue that background agents use. See .claude/todos/notification-unification.md.

Linked issues / bugs

Enable sub-agents to run asynchronously via `run_in_background: true`
parameter. Background agents execute independently from the parent,
which receives an immediate launch confirmation and continues working.
A notification is injected into the parent conversation when the
background agent completes.

Key changes:
- BackgroundTaskRegistry tracks lifecycle of background agents
- Agent tool gains async execution path with fire-and-forget semantics
- Background agents use YOLO approval mode to prevent deadlock
- Independent AbortControllers survive parent ESC cancellation
- CLI bridges notifications via useMessageQueue for between-turn delivery
- State race guards prevent complete/fail after cancellation
- Session cleanup aborts all running background agents
@tanzhenxin
Copy link
Copy Markdown
Collaborator Author

E2E Test Results

Test Mode Pre-Implementation (global qwen v0.14.2) Post-Implementation (local dist/cli.js)
A1 — Schema includes run_in_background Headless Agent tool has 3 params only (description, prompt, subagent_type). No run_in_background. PASS — Agent tool now has 4 params including run_in_background: boolean. Confirmed in API logs.
B1 — Single background agent launch + notification Interactive Model ignored unknown param, ran agent synchronously. LAUNCH_CONFIRMED appeared only after agent finished (~30s). No notification. PASS — Tool returned immediately with launch confirmation. LAUNCH_CONFIRMED appeared within 2s. <task-notification> XML injected between turns with agent result.
B2 — Multiple concurrent background agents Interactive N/A (param not accepted) PASS — Two agents launched in single turn with distinct IDs. Parent returned BOTH_LAUNCHED before either notification. Two separate <task-notification> blocks appeared.
C1 — Error handling (nonexistent file) Interactive N/A (param not accepted) PASS — Agent launched immediately. Notification arrived with status: completed — Explore agent handled missing file gracefully, reported "File not found" in result.

@github-actions
Copy link
Copy Markdown
Contributor

📋 Review Summary

This PR introduces a well-designed background agent execution feature that enables sub-agents to run asynchronously with proper lifecycle management and notification delivery. The implementation demonstrates solid architectural thinking with careful attention to race conditions, cleanup, and user experience. Overall, this is a high-quality implementation that follows existing patterns in the codebase.

🔍 General Feedback

  • Strong architecture: The three-layer design (BackgroundTaskRegistry → Agent tool async path → CLI notification bridge) is clean and well-separated
  • Good test coverage: 11 unit tests cover the core registry functionality including edge cases and race conditions
  • Thoughtful design decisions: The YOLO approval mode for background agents prevents UI deadlocks, and the between-turn notification delivery avoids mid-reasoning interruption
  • Proper cleanup: Session exit cleanup and independent AbortControllers show attention to resource management
  • Code quality: Consistent with existing codebase patterns, proper TypeScript typing, and appropriate use of debug logging

🎯 Specific Feedback

🟡 High

  • packages/core/src/tools/agent.ts:586 - The background agent config override using Object.create(this.config) with a overridden getApprovalMode method is a bit of a hack. Consider whether the Config class could support a proper factory method or builder pattern for creating derived configs with specific overrides. This would be more type-safe and maintainable.

  • packages/core/src/agents/background-tasks.ts:78 - The emitNotification method catches errors but only logs them. If the notification callback fails, there's no fallback or retry mechanism. Consider whether failed notifications should be queued or logged to a visible location since users depend on these notifications.

🟢 Medium

  • packages/core/src/tools/agent.ts:590-596 - The fire-and-forget async block doesn't track the promise anywhere. If Node.js has unhandled promise rejection tracking, this should be fine, but consider adding a .catch() handler that logs failures at minimum, even if the registry.fail() is already called.

  • packages/core/src/agents/background-tasks.ts:159 - The XML notification format uses template literals with manual string concatenation for the optional sections. Consider using a more structured approach or a template function to make the XML generation clearer and less error-prone.

  • packages/cli/src/ui/AppContainer.tsx:806 - The notification callback cleanup sets an empty function () => {}. Consider extracting this to a constant NOOP_CALLBACK at module level for clarity and to avoid creating new function instances on cleanup.

🔵 Low

  • packages/core/src/tools/agent.ts:567-647 - The background execution path is quite long (80 lines). Consider extracting the fire-and-forget logic into a separate private method like launchBackgroundAgent() to improve readability and make the main execute() method more scannable.

  • packages/core/src/agents/background-tasks.ts:146 - The statusText variable uses a nested ternary expression. Consider refactoring to a switch statement or object lookup for better readability:

    const statusTextMap: Record<BackgroundAgentStatus, string> = {
      completed: 'completed',
      failed: `failed: ${entry.error || 'Unknown error'}`,
      cancelled: 'was cancelled',
      running: 'was running', // fallback
    };
  • packages/core/src/tools/agent.ts:568 - The comment uses Unicode box-drawing characters (──) which may not render consistently across all terminals and editors. Consider using standard ASCII characters for better portability.

✅ Highlights

  • Excellent race condition handling: The state guards in complete(), fail(), and cancel() methods (checking status !== 'running') demonstrate careful thinking about concurrent execution scenarios
  • Smart notification timing: Delivering notifications between turns via the message queue rather than interrupting mid-reasoning shows deep understanding of the agent interaction model
  • Comprehensive test coverage: The test file covers normal flows, error cases, cancellation, and the critical race condition scenarios (tests 9-10 specifically test the cancellation guards)
  • Clean separation of concerns: BackgroundTaskRegistry is purely about lifecycle tracking, the Agent tool handles execution, and AppContainer bridges to the UI - each has a single responsibility
  • Defensive error handling: The emitNotification try-catch and the hook system error handling show robust error tolerance

@tanzhenxin tanzhenxin added the type/feature-request New feature or enhancement request label Apr 10, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 10, 2026

Code Coverage Summary

Package Lines Statements Functions Branches
CLI N/A% N/A% N/A% N/A%
Core 74.87% 74.87% 78.01% 81.49%
CLI Package - Full Text Report
CLI full-text-summary.txt not found at: coverage_artifact/cli/coverage/full-text-summary.txt
Core Package - Full Text Report
-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |   74.87 |    81.49 |   78.01 |   74.87 |                   
 src               |     100 |      100 |     100 |     100 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
 src/__mocks__/fs  |       0 |        0 |       0 |       0 |                   
  promises.ts      |       0 |        0 |       0 |       0 | 1-48              
 src/agents        |   93.51 |    84.21 |     100 |   93.51 |                   
  ...ound-tasks.ts |    93.2 |    84.21 |     100 |    93.2 | ...73,176,197-198 
  index.ts         |     100 |      100 |     100 |     100 |                   
 src/agents/arena  |   64.56 |    66.66 |   68.49 |   64.56 |                   
  ...gentClient.ts |   79.47 |    88.88 |   81.81 |   79.47 | ...68-183,189-204 
  ArenaManager.ts  |    61.9 |    63.09 |   67.27 |    61.9 | ...1611,1620-1630 
  arena-events.ts  |   64.44 |      100 |      50 |   64.44 | ...71-175,178-183 
  index.ts         |     100 |      100 |     100 |     100 |                   
  types.ts         |     100 |      100 |     100 |     100 |                   
 ...gents/backends |   76.12 |    85.79 |   72.22 |   76.12 |                   
  ITermBackend.ts  |   97.97 |    93.93 |     100 |   97.97 | ...78-180,255,307 
  ...essBackend.ts |    91.6 |    89.09 |   81.81 |    91.6 | ...15-235,294,379 
  TmuxBackend.ts   |    90.7 |    76.55 |   97.36 |    90.7 | ...87,697,743-747 
  detect.ts        |   31.25 |      100 |       0 |   31.25 | 34-88             
  index.ts         |     100 |      100 |     100 |     100 |                   
  iterm-it2.ts     |     100 |     92.1 |     100 |     100 | 37-38,106         
  tmux-commands.ts |    6.64 |      100 |    3.03 |    6.64 | ...93-363,386-503 
  types.ts         |     100 |      100 |     100 |     100 |                   
 ...agents/runtime |   80.13 |    75.15 |   70.45 |   80.13 |                   
  agent-core.ts    |   71.77 |    66.66 |   56.52 |   71.77 | ...-987,1014-1060 
  agent-events.ts  |   86.48 |      100 |      75 |   86.48 | 218-222           
  ...t-headless.ts |   79.52 |       75 |      55 |   79.52 | ...54-355,358-359 
  ...nteractive.ts |   83.53 |    78.12 |   77.77 |   83.53 | ...01,503,505,508 
  ...statistics.ts |   98.19 |    82.35 |     100 |   98.19 | 127,151,192,225   
  agent-types.ts   |     100 |      100 |     100 |     100 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
 src/config        |   74.02 |    73.23 |   63.31 |   74.02 |                   
  config.ts        |   71.03 |    69.14 |   57.05 |   71.03 | ...2308,2312-2315 
  constants.ts     |     100 |      100 |     100 |     100 |                   
  models.ts        |     100 |      100 |     100 |     100 |                   
  storage.ts       |   95.72 |    92.85 |   91.66 |   95.72 | ...06-207,241-242 
 ...nfirmation-bus |   98.29 |    97.14 |     100 |   98.29 |                   
  message-bus.ts   |   98.14 |    97.05 |     100 |   98.14 | 42-43             
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/core          |   80.16 |    80.21 |   89.93 |   80.16 |                   
  baseLlmClient.ts |     100 |    96.42 |     100 |     100 | 115               
  client.ts        |   72.94 |       75 |   81.81 |   72.94 | ...23,952,978-994 
  ...tGenerator.ts |    72.1 |    61.11 |     100 |    72.1 | ...42,344,351-354 
  ...lScheduler.ts |   71.53 |    76.62 |      90 |   71.53 | ...1695,1752-1756 
  geminiChat.ts    |   82.79 |    83.76 |   90.32 |   82.79 | ...77-888,922-925 
  geminiRequest.ts |     100 |      100 |     100 |     100 |                   
  logger.ts        |   82.25 |    81.81 |     100 |   82.25 | ...57-361,407-421 
  ...tyDefaults.ts |     100 |      100 |     100 |     100 |                   
  ...olExecutor.ts |   92.59 |       75 |      50 |   92.59 | 41-42             
  ...on-helpers.ts |      75 |       48 |     100 |      75 | ...82,186,198-207 
  prompts.ts       |   88.84 |    88.05 |      75 |   88.84 | ...-899,1102-1103 
  tokenLimits.ts   |     100 |    88.23 |     100 |     100 | 50-51             
  ...okTriggers.ts |   99.31 |     90.9 |     100 |   99.31 | 124,135           
  turn.ts          |   96.27 |    88.46 |     100 |   96.27 | ...75,388-389,437 
 ...ntentGenerator |   93.72 |    73.43 |    90.9 |   93.72 |                   
  ...tGenerator.ts |   95.99 |    72.17 |   86.66 |   95.99 | ...03-304,438,494 
  converter.ts     |   93.47 |       75 |     100 |   93.47 | ...87-488,498,558 
  index.ts         |       0 |        0 |       0 |       0 | 1-21              
 ...ntentGenerator |   91.37 |    70.31 |   93.33 |   91.37 |                   
  ...tGenerator.ts |      90 |    70.49 |   92.85 |      90 | ...77-283,301-302 
  index.ts         |     100 |    66.66 |     100 |     100 | 45                
 ...ntentGenerator |   90.76 |    76.63 |      85 |   90.76 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
  ...tGenerator.ts |   90.72 |    76.63 |      85 |   90.72 | ...06,516-517,545 
 ...ntentGenerator |   75.52 |    83.85 |   91.42 |   75.52 |                   
  constants.ts     |     100 |      100 |     100 |     100 |                   
  converter.ts     |   70.98 |    78.77 |   88.88 |   70.98 | ...1321,1342-1351 
  errorHandler.ts  |     100 |      100 |     100 |     100 |                   
  index.ts         |       0 |        0 |       0 |       0 | 1-94              
  ...tGenerator.ts |   48.78 |    91.66 |   77.77 |   48.78 | ...10-163,166-167 
  pipeline.ts      |   93.94 |    87.17 |     100 |   93.94 | ...90,425-426,434 
  ...CallParser.ts |   90.66 |    88.57 |     100 |   90.66 | ...15-319,349-350 
 ...rator/provider |   95.91 |    85.71 |   93.75 |   95.91 |                   
  dashscope.ts     |   97.22 |    87.69 |   93.33 |   97.22 | ...10-211,287-288 
  deepseek.ts      |   90.76 |       75 |     100 |   90.76 | 40-41,45-46,59-60 
  default.ts       |   94.44 |    84.21 |   85.71 |   94.44 | 85-86,150-152     
  index.ts         |     100 |      100 |     100 |     100 |                   
  modelscope.ts    |     100 |      100 |     100 |     100 |                   
  openrouter.ts    |     100 |      100 |     100 |     100 |                   
  types.ts         |       0 |        0 |       0 |       0 |                   
 src/extension     |   60.18 |    79.65 |   78.22 |   60.18 |                   
  ...-converter.ts |   63.68 |    47.82 |      90 |   63.68 | ...76-777,786-818 
  ...ionManager.ts |   44.67 |    83.59 |   65.11 |   44.67 | ...1335,1356-1375 
  ...onSettings.ts |   93.46 |    93.05 |     100 |   93.46 | ...17-221,228-232 
  ...-converter.ts |   54.88 |    94.44 |      60 |   54.88 | ...35-146,158-192 
  github.ts        |   44.94 |    88.52 |      60 |   44.94 | ...53-359,398-451 
  index.ts         |     100 |      100 |     100 |     100 |                   
  marketplace.ts   |   97.29 |    93.75 |     100 |   97.29 | ...64,184-185,274 
  npm.ts           |   48.66 |    76.08 |      75 |   48.66 | ...18-420,427-431 
  override.ts      |   94.11 |    88.88 |     100 |   94.11 | 63-64,81-82       
  settings.ts      |   66.26 |      100 |      50 |   66.26 | 81-108,143-149    
  storage.ts       |   94.73 |       90 |     100 |   94.73 | 41-42             
  ...ableSchema.ts |     100 |      100 |     100 |     100 |                   
  variables.ts     |   88.75 |    83.33 |     100 |   88.75 | ...28-231,234-237 
 src/followup      |   52.95 |    89.32 |   76.31 |   52.95 |                   
  followupState.ts |      96 |    89.74 |     100 |      96 | 159-161,218-219   
  forkedQuery.ts   |   96.69 |    77.14 |     100 |   96.69 | 143,213-214,261   
  index.ts         |     100 |      100 |     100 |     100 |                   
  overlayFs.ts     |   95.06 |       84 |     100 |   95.06 | 78,108,122,133    
  speculation.ts   |    13.4 |      100 |   16.66 |    13.4 | 88-458,518-563    
  ...onToolGate.ts |     100 |    96.29 |     100 |     100 | 93                
  ...nGenerator.ts |   38.27 |    95.12 |   33.33 |   38.27 | ...07-309,344-374 
 src/generated     |       0 |        0 |       0 |       0 |                   
  git-commit.ts    |       0 |        0 |       0 |       0 | 1-10              
 src/hooks         |   85.38 |    85.89 |   90.99 |   85.38 |                   
  ...Aggregator.ts |   96.17 |       90 |     100 |   96.17 | ...74,276-277,350 
  ...entHandler.ts |   95.03 |       84 |    91.3 |   95.03 | ...17,570-571,581 
  hookPlanner.ts   |   84.67 |    79.06 |      90 |   84.67 | ...26,132,150-161 
  hookRegistry.ts  |   83.94 |    79.03 |     100 |   83.94 | ...38,340,342,344 
  hookRunner.ts    |   71.19 |    74.54 |   77.77 |   71.19 | ...34-435,444-445 
  hookSystem.ts    |     100 |      100 |     100 |     100 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
  trustedHooks.ts  |    9.75 |        0 |       0 |    9.75 | 24-118            
  types.ts         |   91.42 |    94.59 |   85.18 |   91.42 | ...93-294,354-358 
 src/ide           |   74.28 |    83.39 |   78.33 |   74.28 |                   
  constants.ts     |     100 |      100 |     100 |     100 |                   
  detect-ide.ts    |     100 |      100 |     100 |     100 |                   
  ide-client.ts    |    64.2 |    81.48 |   66.66 |    64.2 | ...9-970,999-1007 
  ide-installer.ts |   89.06 |    79.31 |     100 |   89.06 | ...36,143-147,160 
  ideContext.ts    |     100 |      100 |     100 |     100 |                   
  process-utils.ts |   84.84 |    71.79 |     100 |   84.84 | ...37,151,193-194 
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/lsp           |   33.39 |    43.56 |   44.91 |   33.39 |                   
  ...nfigLoader.ts |   70.27 |    35.89 |   94.73 |   70.27 | ...20-422,426-432 
  ...ionFactory.ts |    4.29 |        0 |       0 |    4.29 | ...20-371,377-394 
  ...Normalizer.ts |   23.09 |    13.72 |   30.43 |   23.09 | ...04-905,909-924 
  ...verManager.ts |   10.47 |       75 |      25 |   10.47 | ...56-675,681-711 
  ...eLspClient.ts |   17.89 |      100 |       0 |   17.89 | ...37-244,254-258 
  ...LspService.ts |   45.87 |    62.13 |   66.66 |   45.87 | ...1282,1299-1309 
  constants.ts     |     100 |      100 |     100 |     100 |                   
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/mcp           |   78.69 |    75.68 |   75.92 |   78.69 |                   
  constants.ts     |     100 |      100 |     100 |     100 |                   
  ...h-provider.ts |   86.95 |      100 |   33.33 |   86.95 | ...,93,97,101-102 
  ...h-provider.ts |   73.77 |    54.45 |     100 |   73.77 | ...80-887,894-896 
  ...en-storage.ts |   98.62 |    97.72 |     100 |   98.62 | 87-88             
  oauth-utils.ts   |   70.58 |    85.29 |    90.9 |   70.58 | ...70-290,315-344 
  ...n-provider.ts |   89.83 |    95.83 |   45.45 |   89.83 | ...43,147,151-152 
 .../token-storage |   79.48 |    86.66 |   86.36 |   79.48 |                   
  ...en-storage.ts |     100 |      100 |     100 |     100 |                   
  ...en-storage.ts |   82.75 |    82.35 |   92.85 |   82.75 | ...62-172,180-181 
  ...en-storage.ts |     100 |      100 |     100 |     100 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
  ...en-storage.ts |   68.14 |    82.35 |   64.28 |   68.14 | ...81-295,298-314 
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/mocks         |       0 |        0 |       0 |       0 |                   
  msw.ts           |       0 |        0 |       0 |       0 | 1-9               
 src/models        |   88.03 |    83.91 |   86.95 |   88.03 |                   
  constants.ts     |     100 |      100 |     100 |     100 |                   
  ...tor-config.ts |   88.67 |     90.9 |     100 |   88.67 | 112,118,121-130   
  index.ts         |     100 |      100 |     100 |     100 |                   
  ...nfigErrors.ts |   74.22 |    47.82 |   84.61 |   74.22 | ...,67-74,106-117 
  ...igResolver.ts |    97.5 |    86.44 |     100 |    97.5 | ...95,301,316-317 
  modelRegistry.ts |     100 |    98.21 |     100 |     100 | 182               
  modelsConfig.ts  |      83 |    81.37 |   81.57 |      83 | ...1168,1197-1198 
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/output        |     100 |      100 |     100 |     100 |                   
  ...-formatter.ts |     100 |      100 |     100 |     100 |                   
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/permissions   |   70.58 |       88 |    48.2 |   70.58 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
  ...on-manager.ts |   80.82 |    81.72 |   79.16 |   80.82 | ...56-757,764-773 
  rule-parser.ts   |   95.81 |    94.08 |     100 |   95.81 | ...36-837,981-983 
  ...-semantics.ts |   58.28 |    85.27 |    30.2 |   58.28 | ...1604-1614,1643 
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/prompts       |   83.63 |      100 |    87.5 |   83.63 |                   
  mcp-prompts.ts   |   18.18 |      100 |       0 |   18.18 | 11-19             
  ...t-registry.ts |     100 |      100 |     100 |     100 |                   
 src/qwen          |   85.92 |       80 |   97.18 |   85.92 |                   
  ...tGenerator.ts |   98.64 |    98.18 |     100 |   98.64 | 105-106           
  qwenOAuth2.ts    |   84.74 |    75.78 |   93.33 |   84.74 | ...4,963-979,1009 
  ...kenManager.ts |   83.79 |    76.22 |     100 |   83.79 | ...63-768,789-794 
 src/services      |   81.71 |    80.89 |    85.9 |   81.71 |                   
  ...ionService.ts |   97.88 |    93.67 |     100 |   97.88 | 251,253-257       
  ...ingService.ts |   63.94 |    48.38 |      80 |   63.94 | ...52-464,480-481 
  cronScheduler.ts |   97.56 |    92.98 |     100 |   97.56 | 62-63,77,155      
  ...eryService.ts |   80.43 |    95.45 |      75 |   80.43 | ...19-134,140-141 
  ...temService.ts |   89.76 |     85.1 |   88.88 |   89.76 | ...89,191,266-273 
  gitService.ts    |   68.08 |     92.3 |   55.55 |   68.08 | ...10-120,123-127 
  ...reeService.ts |   68.75 |    67.04 |   86.95 |   68.75 | ...88-789,805,821 
  ...ionService.ts |   98.98 |     98.3 |     100 |   98.98 | 260-261           
  ...ionService.ts |   79.23 |    73.19 |   88.88 |   79.23 | ...53-674,682-706 
  ...ionService.ts |   83.46 |    78.53 |   83.33 |   83.46 | ...1017,1023-1028 
 src/skills        |   76.31 |    80.14 |   77.77 |   76.31 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
  skill-load.ts    |   90.69 |    77.77 |     100 |   90.69 | ...24,144,156-158 
  skill-manager.ts |   71.57 |    80.76 |   73.91 |   71.57 | ...91-699,702-711 
  types.ts         |     100 |      100 |     100 |     100 |                   
 src/subagents     |   83.36 |    83.39 |    90.9 |   83.36 |                   
  ...tin-agents.ts |     100 |      100 |     100 |     100 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
  ...-selection.ts |     100 |      100 |     100 |     100 |                   
  ...nt-manager.ts |   76.95 |    75.73 |   86.66 |   76.95 | ...-972,1049-1050 
  types.ts         |     100 |      100 |     100 |     100 |                   
  validation.ts    |    92.1 |    94.93 |     100 |    92.1 | 51-56,60-65,69-74 
 src/telemetry     |   68.01 |    84.36 |    72.3 |   68.01 |                   
  config.ts        |     100 |      100 |     100 |     100 |                   
  constants.ts     |     100 |      100 |     100 |     100 |                   
  ...-exporters.ts |   36.76 |      100 |   22.22 |   36.76 | ...84,87-88,91-92 
  index.ts         |     100 |      100 |     100 |     100 |                   
  ...t.circular.ts |       0 |        0 |       0 |       0 | 1-111             
  ...t.circular.ts |       0 |        0 |       0 |       0 | 1-128             
  loggers.ts       |   53.23 |    62.68 |   54.76 |   53.23 | ...1130,1133-1157 
  metrics.ts       |   78.17 |    82.95 |   78.84 |   78.17 | ...09-846,849-878 
  sanitize.ts      |      80 |    83.33 |     100 |      80 | 35-36,41-42       
  sdk.ts           |   85.13 |    56.25 |     100 |   85.13 | ...78,184-185,191 
  ...etry-utils.ts |     100 |      100 |     100 |     100 |                   
  ...l-decision.ts |     100 |      100 |     100 |     100 |                   
  types.ts         |   77.31 |    94.17 |   81.81 |   77.31 | ...1105,1108-1137 
  uiTelemetry.ts   |   91.87 |    96.15 |   78.57 |   91.87 | ...67-168,174-181 
 ...ry/qwen-logger |   68.18 |    80.21 |   64.91 |   68.18 |                   
  event-types.ts   |       0 |        0 |       0 |       0 |                   
  qwen-logger.ts   |   68.18 |       80 |   64.28 |   68.18 | ...1040,1078-1079 
 src/test-utils    |   92.85 |    97.05 |   70.96 |   92.85 |                   
  config.ts        |     100 |      100 |     100 |     100 |                   
  index.ts         |     100 |      100 |     100 |     100 |                   
  mock-tool.ts     |   91.02 |    96.66 |   68.96 |   91.02 | ...31,195-196,209 
  ...aceContext.ts |     100 |      100 |     100 |     100 |                   
 src/tools         |   74.27 |    79.64 |   79.84 |   74.27 |                   
  agent.ts         |   80.85 |    88.42 |    87.5 |   80.85 | ...89-659,679-684 
  ...erQuestion.ts |   87.89 |     73.8 |    90.9 |   87.89 | ...44-345,349-350 
  cron-create.ts   |   97.61 |    88.88 |   83.33 |   97.61 | 30-31             
  cron-delete.ts   |   96.55 |      100 |   83.33 |   96.55 | 26-27             
  cron-list.ts     |   96.22 |      100 |   83.33 |   96.22 | 25-26             
  diffOptions.ts   |     100 |      100 |     100 |     100 |                   
  edit.ts          |   81.37 |    85.05 |   73.33 |   81.37 | ...03-504,587-637 
  exitPlanMode.ts  |   84.61 |    85.71 |     100 |   84.61 | ...60-163,177-189 
  glob.ts          |   91.57 |    88.33 |   84.61 |   91.57 | ...20,163,293,296 
  grep.ts          |   71.64 |    87.34 |   72.22 |   71.64 | ...84,524,532-539 
  ls.ts            |   96.72 |    90.14 |     100 |   96.72 | 169-174,205,209   
  lsp.ts           |   72.58 |    60.29 |   90.32 |   72.58 | ...1202,1204-1205 
  ...nt-manager.ts |   47.47 |       60 |   44.44 |   47.47 | ...73-491,494-531 
  mcp-client.ts    |   29.13 |    69.44 |   46.87 |   29.13 | ...1420,1424-1427 
  mcp-tool.ts      |   90.92 |    88.88 |   96.42 |   90.92 | ...89-590,640-641 
  memoryTool.ts    |   74.48 |    83.05 |   90.47 |   74.48 | ...48-356,458-542 
  ...iable-tool.ts |     100 |    84.61 |     100 |     100 | 102,109           
  read-file.ts     |   96.47 |     87.8 |   88.88 |   96.47 | 68,70,72-73,79-80 
  ripGrep.ts       |   96.46 |     91.3 |     100 |   96.46 | ...93,296,374-375 
  ...-transport.ts |    6.34 |        0 |       0 |    6.34 | 47-145            
  shell.ts         |   86.14 |    78.12 |    92.3 |   86.14 | ...59-463,657-658 
  skill.ts         |   94.08 |    88.88 |   84.61 |   94.08 | ...16,255-258,262 
  todoWrite.ts     |   85.42 |    84.09 |   84.61 |   85.42 | ...05-410,432-433 
  tool-error.ts    |     100 |      100 |     100 |     100 |                   
  tool-names.ts    |     100 |      100 |     100 |     100 |                   
  tool-registry.ts |   62.79 |    65.38 |   59.37 |   62.79 | ...34-543,550-566 
  tools.ts         |   83.84 |    89.58 |   82.35 |   83.84 | ...18-419,435-441 
  web-fetch.ts     |   86.09 |    60.86 |   91.66 |   86.09 | ...53-254,256-257 
  write-file.ts    |    81.6 |    78.18 |      75 |    81.6 | ...05-408,420-455 
 ...ols/web-search |   72.42 |    76.59 |   76.47 |   72.42 |                   
  base-provider.ts |    40.9 |    33.33 |     100 |    40.9 | 40-43,48-56       
  index.ts         |   76.85 |    84.61 |   84.61 |   76.85 | ...62-166,272-282 
  types.ts         |       0 |        0 |       0 |       0 | 1                 
  utils.ts         |      60 |       50 |      50 |      60 | 35-42             
 ...arch/providers |   46.73 |    61.11 |   72.72 |   46.73 |                   
  ...e-provider.ts |       8 |        0 |       0 |       8 | 68-83,89-199      
  ...e-provider.ts |      82 |    55.55 |     100 |      82 | 57-58,61-62,72-76 
  ...y-provider.ts |   89.79 |       75 |     100 |   89.79 | 62-66             
 src/utils         |   85.77 |    87.28 |   89.94 |   85.77 |                   
  LruCache.ts      |       0 |        0 |       0 |       0 | 1-41              
  ...ssageQueue.ts |     100 |      100 |     100 |     100 |                   
  ...cFileWrite.ts |   76.08 |    44.44 |     100 |   76.08 | 61-70,72          
  browser.ts       |    7.69 |      100 |       0 |    7.69 | 17-56             
  ...igResolver.ts |     100 |      100 |     100 |     100 |                   
  cronDisplay.ts   |   42.85 |    23.07 |     100 |   42.85 | 26-31,33-45,47-54 
  cronParser.ts    |   89.74 |    85.71 |     100 |   89.74 | ...,63-64,183-186 
  debugLogger.ts   |   96.12 |    93.75 |   93.75 |   96.12 | 164-168           
  editHelper.ts    |   92.67 |    82.14 |     100 |   92.67 | ...52-454,463-464 
  editor.ts        |   96.98 |    93.87 |     100 |   96.98 | ...93-194,196-197 
  ...arResolver.ts |   94.28 |    88.88 |     100 |   94.28 | 28-29,125-126     
  ...entContext.ts |     100 |       95 |     100 |     100 | 83                
  errorParsing.ts  |   96.92 |       95 |     100 |   96.92 | 36-37             
  ...rReporting.ts |   88.46 |       90 |     100 |   88.46 | 69-74             
  errors.ts        |    68.7 |    77.27 |   53.33 |    68.7 | ...86-202,206-212 
  fetch.ts         |   71.97 |    71.42 |   71.42 |   71.97 | ...38,144,157,182 
  fileUtils.ts     |   91.68 |    83.85 |   94.73 |   91.68 | ...28-734,748-754 
  formatters.ts    |   54.54 |       50 |     100 |   54.54 | 12-16             
  ...eUtilities.ts |   89.21 |    86.66 |     100 |   89.21 | 16-17,49-55,65-66 
  ...rStructure.ts |   94.36 |    94.28 |     100 |   94.36 | ...17-120,330-335 
  getPty.ts        |    12.5 |      100 |       0 |    12.5 | 21-34             
  ...noreParser.ts |    92.3 |    89.36 |     100 |    92.3 | ...15-116,186-187 
  gitUtils.ts      |   36.66 |    76.92 |      50 |   36.66 | ...4,88-89,97-148 
  iconvHelper.ts   |     100 |      100 |     100 |     100 |                   
  ...rePatterns.ts |     100 |      100 |     100 |     100 |                   
  ...ionManager.ts |     100 |     90.9 |     100 |     100 | 26                
  ...lPromptIds.ts |     100 |      100 |     100 |     100 |                   
  jsonl-utils.ts   |    8.87 |      100 |       0 |    8.87 | ...51-184,190-196 
  ...-detection.ts |     100 |      100 |     100 |     100 |                   
  ...yDiscovery.ts |   82.97 |    76.59 |     100 |   82.97 | ...75,292-293,296 
  ...tProcessor.ts |   93.63 |       90 |     100 |   93.63 | ...96-302,384-385 
  ...Inspectors.ts |   61.53 |      100 |      50 |   61.53 | 18-23             
  ...kerChecker.ts |   84.04 |    78.94 |     100 |   84.04 | 68-69,79-84,92-98 
  openaiLogger.ts  |   86.27 |    82.14 |     100 |   86.27 | ...05-107,130-135 
  partUtils.ts     |     100 |      100 |     100 |     100 |                   
  pathReader.ts    |     100 |      100 |     100 |     100 |                   
  paths.ts         |   95.67 |    94.52 |     100 |   95.67 | ...,70-71,103-104 
  ...ectSummary.ts |    3.75 |      100 |       0 |    3.75 | 27-119            
  ...tIdContext.ts |     100 |      100 |     100 |     100 |                   
  proxyUtils.ts    |     100 |      100 |     100 |     100 |                   
  ...rDetection.ts |   58.57 |       76 |     100 |   58.57 | ...4,88-89,95-100 
  ...noreParser.ts |   85.45 |    85.18 |     100 |   85.45 | ...59,65-66,72-73 
  rateLimit.ts     |      90 |    83.87 |     100 |      90 | 70,81-83          
  readManyFiles.ts |   85.95 |    85.71 |     100 |   85.95 | ...80-182,198-209 
  retry.ts         |   70.14 |    76.92 |     100 |   70.14 | ...88,206,213-214 
  ripgrepUtils.ts  |   46.53 |    83.33 |   66.66 |   46.53 | ...32-233,245-322 
  ...tchOptions.ts |   55.88 |       50 |      75 |   55.88 | ...29-130,151-152 
  safeJsonParse.ts |   74.07 |    83.33 |     100 |   74.07 | 40-46             
  ...nStringify.ts |     100 |      100 |     100 |     100 |                   
  ...aConverter.ts |   90.78 |    87.87 |     100 |   90.78 | ...41-42,93,95-96 
  ...aValidator.ts |   93.43 |    76.66 |     100 |   93.43 | ...46,155-158,212 
  ...r-launcher.ts |   76.52 |     87.5 |   66.66 |   76.52 | ...33,135,153-191 
  shell-utils.ts   |    83.6 |    90.63 |     100 |    83.6 | ...1040,1047-1051 
  ...lAstParser.ts |   95.58 |    85.71 |     100 |   95.58 | ...1059-1061,1071 
  ...nlyChecker.ts |   95.75 |    92.39 |     100 |   95.75 | ...00-301,313-314 
  ...tGenerator.ts |     100 |     90.9 |     100 |     100 | 129               
  symlink.ts       |   77.77 |       50 |     100 |   77.77 | 44,54-59          
  ...emEncoding.ts |   96.36 |    91.17 |     100 |   96.36 | 59-60,124-125     
  ...Serializer.ts |   99.07 |    91.22 |     100 |   99.07 | 90,156-158        
  testUtils.ts     |   53.33 |      100 |   33.33 |   53.33 | ...53,59-64,70-72 
  textUtils.ts     |      60 |      100 |   66.66 |      60 | 36-55             
  thoughtUtils.ts  |     100 |    92.85 |     100 |     100 | 71                
  ...-converter.ts |   94.59 |    85.71 |     100 |   94.59 | 35-36             
  tool-utils.ts    |    93.6 |     91.3 |     100 |    93.6 | ...58-159,162-163 
  truncation.ts    |     100 |       92 |     100 |     100 | 52,71             
  ...aceContext.ts |   96.22 |       92 |   93.33 |   96.22 | ...15-116,133,160 
  yaml-parser.ts   |      92 |    83.67 |     100 |      92 | 49-53,65-69       
 ...ils/filesearch |   96.17 |     91.4 |     100 |   96.17 |                   
  crawlCache.ts    |     100 |      100 |     100 |     100 |                   
  crawler.ts       |   96.22 |     92.3 |     100 |   96.22 | 66-67             
  fileSearch.ts    |   93.22 |    87.14 |     100 |   93.22 | ...30-231,233-234 
  ignore.ts        |     100 |      100 |     100 |     100 |                   
  result-cache.ts  |     100 |     92.3 |     100 |     100 | 46                
 ...uest-tokenizer |   56.63 |    74.52 |   74.19 |   56.63 |                   
  ...eTokenizer.ts |   41.86 |    76.47 |   69.23 |   41.86 | ...70-443,453-507 
  index.ts         |     100 |      100 |     100 |     100 |                   
  ...tTokenizer.ts |   68.39 |    69.49 |    90.9 |   68.39 | ...24-325,327-328 
  ...ageFormats.ts |      76 |      100 |   33.33 |      76 | 45-48,55-56       
  textTokenizer.ts |     100 |      100 |     100 |     100 |                   
  types.ts         |       0 |        0 |       0 |       0 | 1                 
-------------------|---------|----------|---------|---------|-------------------

For detailed HTML reports, please see the 'coverage-reports-22.x-ubuntu-latest' artifact from the main CI run.

tanzhenxin and others added 4 commits April 10, 2026 14:55
- Add prefix/separator protocol to distinguish background notifications from user input
- Show concise summary in UI while sending full details to LLM
- Add 'notification' history item type with specialized display
- Add 'background' agent status for background-running agents
- Prevent notifications from polluting prompt history (up-arrow)
- Truncate long descriptions in display text

This improves the UX for background agents by showing cleaner, more concise
notifications while preserving full context for the LLM.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Headless mode skips AppContainer, so the notification callback is never
registered and background agent results would be silently dropped. Return
an error prompting the model to retry without run_in_background.
…tification queue

Replace the stringly-typed \x00__BG_NOTIFY__\x00 prefix/separator
encoding with a typed notification path using SendMessageType.Notification.

- Add SendMessageType.Notification to the enum
- Change BackgroundNotificationCallback to emit (displayText, modelText)
- Move notification queue from AppContainer into useGeminiStream (mirrors
  the cron queue pattern): register on registry, queue structured items,
  drain on idle via submitQuery
- prepareQueryForGemini short-circuits for Notification type (skips slash
  commands, shell mode, @-commands, prompt history logging)
- Remove BACKGROUND_NOTIFICATION_PREFIX/SEPARATOR constants
Background agent cleanup belongs in Config.shutdown() alongside other
resource teardown (skillManager, toolRegistry, arenaRuntime), not in
AppContainer's registerCleanup. This also ensures headless mode gets
cleanup for free.
@tanzhenxin tanzhenxin force-pushed the feat/background-subagent branch from da16bf6 to bcce78d Compare April 10, 2026 08:44
Background agent notifications were missing after session resume because
they were never recorded in the chat history. The model text was absent
from the API history and the display item was lost.

- Add recordNotification() to ChatRecordingService — stores as user-role
  message with subtype 'notification' and displayText payload
- Thread notificationDisplayText through submitQuery → sendMessageStream
- Restore as HistoryItemNotification in resumeHistoryUtils
Background agents were using YOLO approval mode which auto-approves all
tool calls — too permissive. Replace with shouldAvoidPermissionPrompts
which auto-denies tool calls that need interactive approval, matching
claw-code's approach.

The permission flow for background agents is now:
1. L3/L4 permission rules (allow/deny) — same as foreground
2. Approval mode overrides (AUTO_EDIT for edits) — same as foreground
3. PermissionRequest hooks — can override the denial
4. Auto-deny — if no hook decided, deny because prompts are unavailable
@tanzhenxin
Copy link
Copy Markdown
Collaborator Author

E2E Test Report

All tests run against local build (node dist/cli.js), 2026-04-10.

Test Group Mode Result Evidence
A1 Parameter acceptance Headless PASS run_in_background listed as optional boolean parameter
B1 Background execution Interactive PASS LAUNCH_CONFIRMED immediate, notification delivered, model processed result
D1 Session cleanup Interactive PASS /exit during running agent, session terminated <1s, no hang
E1 Permission deny Interactive PASS Write tool denied in default mode, output.txt not created, notification explained denial

E1 details (new test — deny-by-default permissions)

Parent session in --approval-mode default. Background agent asked to write a file via Write tool. The tool was denied because background agents use shouldAvoidPermissionPrompts (auto-deny after hooks). The agent completed and the notification reported: "couldn't write the file because it needed permission approval and as a background agent, it can't prompt for confirmation." File was never created on disk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/feature-request New feature or enhancement request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant