Skip to content

Commit c0ce2b3

Browse files
zhangmo8Copilot
andauthored
feat(agent): enhance agent (#1559)
* feat: Enhance Subagent Orchestrator Tool with background operations and improved task management - Updated subagent orchestrator schema to include new operations: 'run', 'list', 'info', 'log', 'wait', 'kill'. - Added support for background task execution with runId tracking. - Implemented validation for required fields based on operation type. - Introduced MutableRunState to manage task states and statuses. - Enhanced rendering of progress and final results for subagent runs. - Added new subagent slots: Explorer, Implementer, Reviewer. - Updated DeepChat agent settings to enable subagents by default. - Improved tool presenter to handle new tool result structures and provide better error handling. - Added tests for background run operations, ensuring correct behavior for list, info, and kill commands. * feat(systemPrompt): update DEFAULT_SYSTEM_PROMPT for enhanced clarity and functionality * feat(systemEnvPrompt): remove unnecessary file tags from system environment prompt * feat(toolPresenter): add focused inspection tools (find, grep, ls) and update related prompts * Completing task (#1562) * fix(agent): address review findings * chore: update * chore: update by code review --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
1 parent 6a44bc4 commit c0ce2b3

41 files changed

Lines changed: 2211 additions & 414 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/specs/agent-refactor/plan.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Agent Refactor Plan
2+
3+
## P0 Baseline
4+
5+
1. Default DeepChat subagent config:
6+
- Enable subagents by default.
7+
- Add self-based `explorer`, `implementer`, and `reviewer` slots.
8+
2. Prompt composition:
9+
- Build a single system prompt string.
10+
- Keep section order stable.
11+
- Add permission and verification policy sections.
12+
3. Tool execution:
13+
- Parallelize all-read-only canonical Agent tool rounds.
14+
- Preserve result writeback order.
15+
- Keep writes, edits, commands, and process operations serialized.
16+
17+
## P1 Stable Tool Profile
18+
19+
1. Add a main-layer session tool-profile cache.
20+
2. Select `code` profile when a project directory is present; otherwise use `general`.
21+
3. Refresh the profile when project directory, model, disabled tools, or active skills change.
22+
4. Keep schemas stable across ordinary user messages.
23+
24+
## P2 Follow-Up
25+
26+
1. Expand profile types for `research` and `analysis`.
27+
2. Add UI affordances for weak agent capability where only legacy tool fallback is available.
28+
3. Implement the documented tool-result envelope after review.
29+
4. Extend subagent lifecycle commands for background management.

docs/specs/agent-refactor/spec.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Agent Refactor Spec
2+
3+
## Goal
4+
5+
Raise the default DeepChat agent baseline without rewriting the presenter architecture. P0 must be useful on its own for code changes, requirement analysis, repository inspection, and structured research.
6+
7+
## Requirements
8+
9+
1. DeepChat agents default to subagents enabled unless explicitly disabled.
10+
2. Default subagent slots include self-based `explorer`, `implementer`, and `reviewer` roles.
11+
3. Provider requests receive one composed system message, not multiple system messages.
12+
4. The composed system prompt order is stable:
13+
- user/base prompt
14+
- runtime capabilities
15+
- environment and `AGENTS.md`
16+
- skills metadata and pinned skills
17+
- tooling rules
18+
- permission rules
19+
- verification policy
20+
5. Read-only canonical Agent tools may execute in parallel when a tool-call round contains only read-only calls.
21+
6. Mutating and runtime canonical tools remain serialized or permission-gated.
22+
7. Tool schemas are loaded through a stable session profile and cache. Ordinary user messages must not by themselves change the tool profile.
23+
8. Code-agent verification policy requires final answers to account for verification after code changes.
24+
25+
## Non-Goals
26+
27+
1. Do not implement a per-turn tool router.
28+
2. Do not rewrite IPC, renderer message flow, or provider runtime architecture.
29+
3. Do not change the tool-result envelope implementation before the protocol document is reviewed.
30+
4. Do not remove legacy function-call fallback in this phase.
31+
32+
## Acceptance
33+
34+
1. New or resolved DeepChat agent configs expose enabled subagents and the three default self slots.
35+
2. Agent runtime requests include at most one `system` role message.
36+
3. Prompt section order follows the requirement above and includes `AGENTS.md` via the environment section.
37+
4. A batch of only `read`/`ls`/`find`/`grep` Agent tool calls starts concurrently and writes tool results back in model call order.
38+
5. Any batch containing `write`/`edit`/`exec`/`process` stays serialized.
39+
6. Repeated ordinary messages in the same session reuse the stable tool profile unless project directory, disabled tools, model, or active skills change.

docs/specs/agent-refactor/tasks.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Agent Refactor Tasks
2+
3+
- [x] Add default self-based `explorer`, `implementer`, and `reviewer` subagent slots.
4+
- [x] Enable subagents by default for DeepChat configs unless explicitly disabled.
5+
- [x] Update DeepChat agent settings defaults to use the new slot set.
6+
- [x] Compose one system prompt with stable section ordering.
7+
- [x] Add permission and verification policy prompt sections.
8+
- [x] Add a session tool-profile cache in the main runtime.
9+
- [x] Parallelize all-read-only canonical Agent tool batches.
10+
- [x] Keep mixed or mutating tool batches serialized.
11+
- [x] Document the tool-result envelope before implementation.
12+
- [x] Add UI labeling for weak legacy-only tool-calling models.
13+
- [x] Implement the tool-result envelope after protocol review.
14+
- [x] Extend background subagent lifecycle operations.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Tool Result Envelope
2+
3+
This document defines the target tool-result protocol. The current implementation adds the envelope to Agent tool `rawData.toolResult` at the ToolPresenter boundary while keeping legacy `rawData.content` unchanged for provider-facing model context.
4+
5+
## Shape
6+
7+
```ts
8+
type AgentToolResult = {
9+
ok: boolean
10+
summary: string
11+
data?: unknown
12+
meta?: {
13+
truncated?: boolean
14+
nextOffset?: number
15+
offloadPath?: string
16+
tokenEstimate?: number
17+
resultCount?: number
18+
}
19+
error?: {
20+
code: string
21+
message: string
22+
recoverable?: boolean
23+
}
24+
}
25+
```
26+
27+
## Success
28+
29+
Successful tools set `ok: true`, provide a short model-readable `summary`, and put structured payloads in `data`.
30+
31+
Examples:
32+
33+
```json
34+
{
35+
"ok": true,
36+
"summary": "Found 12 matches in 3 files.",
37+
"data": {
38+
"matches": []
39+
},
40+
"meta": {
41+
"resultCount": 12,
42+
"truncated": false
43+
}
44+
}
45+
```
46+
47+
## Errors
48+
49+
Failed tools set `ok: false`, provide a short summary, and include a stable `error.code`.
50+
51+
```json
52+
{
53+
"ok": false,
54+
"summary": "Invalid arguments for edit.",
55+
"error": {
56+
"code": "INVALID_ARGUMENT",
57+
"message": "oldText is required.",
58+
"recoverable": true
59+
}
60+
}
61+
```
62+
63+
## Truncation
64+
65+
Tools that return partial data set `meta.truncated: true`. If the same tool can continue from a position, it also sets `meta.nextOffset`.
66+
67+
Renderer display:
68+
69+
- Show the `summary` in compact cards.
70+
- Show truncation and next-page affordances from `meta`.
71+
- Keep `data` available for rich views, but do not require the renderer to parse provider-facing prose.
72+
73+
Model-readable behavior:
74+
75+
- Always include the `summary`.
76+
- Include enough `data` for the next likely model step.
77+
- Prefer pagination over returning huge blobs.
78+
79+
## Offload
80+
81+
Large outputs may be written to an offload file and represented by `meta.offloadPath`.
82+
83+
Rules:
84+
85+
- `summary` must explain what was offloaded.
86+
- `data` may contain a preview.
87+
- The offload path must be readable by the canonical `read` tool when the session has access.
88+
89+
## Batch Results
90+
91+
When multiple tool calls are executed in one model round:
92+
93+
- Preserve the model's original tool-call order in returned tool messages.
94+
- Each tool message contains one envelope.
95+
- Parallel execution must not reorder renderer updates or provider-facing tool result messages.
96+
97+
## Renderer Contract
98+
99+
The renderer should treat the envelope as the stable display protocol:
100+
101+
- `ok` controls success/error styling.
102+
- `summary` powers the collapsed text.
103+
- `data` feeds rich tool-specific rendering.
104+
- `meta` handles pagination, truncation, and offload UI.
105+
- `error` provides retry hints and diagnostics.
106+
107+
## Migration Notes
108+
109+
1. Canonical Agent tools now receive an envelope through `rawData.toolResult` unless a tool already provides a specialized `toolResult`.
110+
2. Legacy raw tool output remains available through `rawData.content`.
111+
3. Renderer cards can migrate to the envelope without changing provider-facing tool messages.
112+
4. MCP passthrough tools and external servers can be extended after renderer support lands.

src/main/lib/agentRuntime/systemEnvPromptBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,13 @@ export async function buildSystemEnvPrompt(
148148
const promptLines = [
149149
`You are powered by the model named ${modelName}.`,
150150
`The exact model ID is ${exactModelId}`,
151-
`## Here is some useful information about the environment you are running in:`,
151+
`Here is some useful information about the environment you are running in:`,
152+
'<env>',
152153
`Working directory: ${workdir}`,
153154
`Is directory a git repo: ${isGitRepository(workdir) ? 'yes' : 'no'}`,
154155
`Platform: ${platform}`,
155-
`Today's date: ${now.toDateString()}`
156+
`Today's date: ${now.toDateString()}`,
157+
'</env>'
156158
]
157159

158160
if (agentsContent.trim().length > 0) {

src/main/presenter/agentRepository/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import type {
1313
CreateDeepChatAgentInput,
1414
UpdateDeepChatAgentInput
1515
} from '@shared/types/agent-interface'
16-
import { normalizeDeepChatSubagentConfig } from '@shared/lib/deepchatSubagents'
16+
import {
17+
createDefaultDeepChatSubagentSlots,
18+
normalizeDeepChatSubagentConfig
19+
} from '@shared/lib/deepchatSubagents'
1720
import type { SQLitePresenter } from '../sqlitePresenter'
1821
import type { AgentRow } from '../sqlitePresenter/tables/agents'
1922

@@ -73,8 +76,9 @@ const mergeDeepChatConfig = (
7376
systemPrompt: overrideConfig.systemPrompt ?? baseConfig.systemPrompt ?? '',
7477
permissionMode: overrideConfig.permissionMode ?? baseConfig.permissionMode ?? 'full_access',
7578
disabledAgentTools: overrideConfig.disabledAgentTools ?? baseConfig.disabledAgentTools ?? [],
76-
subagentEnabled: overrideConfig.subagentEnabled ?? baseConfig.subagentEnabled ?? false,
77-
subagents: overrideConfig.subagents ?? baseConfig.subagents ?? [],
79+
subagentEnabled: overrideConfig.subagentEnabled ?? baseConfig.subagentEnabled ?? true,
80+
subagents:
81+
overrideConfig.subagents ?? baseConfig.subagents ?? createDefaultDeepChatSubagentSlots(),
7882
autoCompactionEnabled:
7983
overrideConfig.autoCompactionEnabled ?? baseConfig.autoCompactionEnabled ?? true,
8084
autoCompactionTriggerThreshold:

0 commit comments

Comments
 (0)