-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution-partition.ts
More file actions
203 lines (194 loc) · 8.92 KB
/
Copy pathexecution-partition.ts
File metadata and controls
203 lines (194 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import {
ExecutionPartitionInputSchema,
ExecutionPartitionOutputSchema,
ExecutionPartitionMcpOutputSchema,
type ExecutionPartitionInput,
} from '../schemas/execution-partition.js';
import { getExecutionPartitionSystemPrompt, formatExecutionPartitionUserMessage } from '../prompts/execution-partition-system.js';
import { callReview } from '../services/reviewer.js';
import type { TokenUsage } from '../services/reviewer.js';
import { resolveWorkspaceScope } from '../services/filesystem.js';
import { computeIterationMeta, isIterationLimitExceeded, computeCostWarning } from '../services/review-limits.js';
import { logUsage } from '../services/usage-logger.js';
const ZERO_USAGE: TokenUsage = { input_tokens: 0, output_tokens: 0, total_tokens: 0, api_calls: 0, provider: 'none', model: 'none', estimated_cost_usd: null };
export function registerExecutionPartitionTool(server: McpServer): void {
server.registerTool(
'request_execution_partition',
{
title: 'DUUL Execution Partition (Project Manager)',
description:
'DUUL optional: Partition an approved plan into executable subtasks. ' +
'REQUIRED fields: approved_plan (full plan markdown — do NOT leave empty), workspace_root (absolute path). ' +
'Optional: working_directories, changed_files, entrypoints, artifact_refs, max_parallelism, iteration_count. ' +
'NEVER call with an empty object — populate approved_plan with actual plan text before invoking. ' +
'Returns dependency graph, spawn strategy, and handoff contracts.',
inputSchema: ExecutionPartitionInputSchema,
outputSchema: ExecutionPartitionMcpOutputSchema,
},
async (input) => {
try {
const args = input as ExecutionPartitionInput;
if (
!args ||
typeof args.approved_plan !== 'string' ||
args.approved_plan.trim().length < 20 ||
typeof args.workspace_root !== 'string' ||
args.workspace_root.trim().length === 0
) {
const message =
'ERROR: `approved_plan` and `workspace_root` fields are both required. ' +
'`approved_plan` must contain the full plan text (min 20 chars). ' +
'`workspace_root` must be an absolute path. ' +
'You called request_execution_partition with missing or empty content. ' +
'Retry with: { "approved_plan": "<plan text>", "workspace_root": "<absolute path>" }. ' +
'Do NOT call this tool again with an empty input.';
console.error(`[duul] execution-partition rejected: missing/empty approved_plan or workspace_root`);
return {
content: [{ type: 'text' as const, text: message }],
isError: true,
};
}
const iterMeta = computeIterationMeta('partition', args.iteration_count, args.max_review_iterations);
// Short-circuit if iteration limit exceeded
if (isIterationLimitExceeded('partition', args.iteration_count, args.max_review_iterations)) {
console.error(
`[duul] Partition iteration limit exceeded: ${args.iteration_count} > ${iterMeta.iteration_limit}`,
);
const limitResult = {
execution_mode: 'serial' as const,
rationale: `Iteration limit reached (${iterMeta.iteration_count}/${iterMeta.iteration_limit}). Human review required to continue.`,
requires_human_checkpoint: true,
human_checkpoint_reasons: [`Iteration limit reached — ${iterMeta.iteration_count}/${iterMeta.iteration_limit} iterations used.`],
spawn_strategy: 'reuse_workspace' as const,
handoff_artifact_pattern: '.context/subtasks/{subtask_id}.json',
subtask_result_schema_version: '1.0' as const,
subtasks: [{
id: 'full-plan',
title: 'Execute full plan serially (limit reached)',
goal: args.approved_plan.slice(0, 200) + '...',
can_run_in_parallel: false,
depends_on: [],
workspace_name_hint: 'main',
spawn_strategy: 'reuse_workspace' as const,
scope: {
working_directories: args.working_directories,
changed_files: args.changed_files,
entrypoints: args.entrypoints,
},
handoff_contract: [],
completion_criteria: ['All items in the approved plan are implemented'],
review_focus: ['Full plan implementation'],
risk_level: 'medium' as const,
}],
global_checkpoints: [],
merge_order: ['full-plan'],
retry_policy: {
max_retries: 2,
on_review_revise: 'retry_subtask' as const,
on_tool_exhaustion: 'retry_with_narrower_scope' as const,
on_conflict: 'serialize_and_retry' as const,
on_blocker: 'escalate_to_human' as const,
on_max_retries_exceeded: 'abort_subtask_and_report' as const,
},
review_id: '',
...iterMeta,
cost_warning: null,
token_usage: ZERO_USAGE,
};
return {
content: [{ type: 'text' as const, text: JSON.stringify(limitResult, null, 2) }],
structuredContent: limitResult,
};
}
const scope = resolveWorkspaceScope(args);
const systemPrompt = getExecutionPartitionSystemPrompt();
const userMessage = formatExecutionPartitionUserMessage(
args.approved_plan,
args.constraints,
{
changedFiles: args.changed_files,
entrypoints: args.entrypoints,
artifactRefs: args.artifact_refs,
workingDirectories: args.working_directories,
},
args.max_parallelism,
);
const { parsed, reviewId, usage } = await callReview({
systemPrompt,
userMessage,
schemaName: 'execution_partition_output',
outputSchema: ExecutionPartitionOutputSchema,
workspaceScope: scope,
previousReviewId: args.previous_review_id,
toolName: 'partition',
reviewerConfig: args.reviewer_config,
createFallback: (_reason, _usedTools) => ({
execution_mode: 'serial' as const,
rationale: 'Partition could not be completed — tool loop exhausted. Defaulting to serial execution.',
requires_human_checkpoint: true,
human_checkpoint_reasons: ['Partition was incomplete — human should verify before proceeding.'],
spawn_strategy: 'reuse_workspace' as const,
handoff_artifact_pattern: '.context/subtasks/{subtask_id}.json',
subtask_result_schema_version: '1.0' as const,
subtasks: [{
id: 'full-plan',
title: 'Execute full plan serially',
goal: args.approved_plan.slice(0, 200) + '...',
can_run_in_parallel: false,
depends_on: [],
workspace_name_hint: 'main',
spawn_strategy: 'reuse_workspace' as const,
scope: {
working_directories: args.working_directories,
changed_files: args.changed_files,
entrypoints: args.entrypoints,
},
handoff_contract: [],
completion_criteria: ['All items in the approved plan are implemented'],
review_focus: ['Full plan implementation'],
risk_level: 'medium' as const,
}],
global_checkpoints: [],
merge_order: ['full-plan'],
retry_policy: {
max_retries: 2,
on_review_revise: 'retry_subtask' as const,
on_tool_exhaustion: 'retry_with_narrower_scope' as const,
on_conflict: 'serialize_and_retry' as const,
on_blocker: 'escalate_to_human' as const,
on_max_retries_exceeded: 'abort_subtask_and_report' as const,
},
}),
});
const result = {
...parsed,
review_id: reviewId,
...iterMeta,
cost_warning: computeCostWarning(iterMeta, usage.estimated_cost_usd),
token_usage: usage,
};
logUsage('execution_partition', result.token_usage, {
review_id: reviewId,
iteration_count: iterMeta.iteration_count,
});
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
structuredContent: result,
};
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
console.error(`[duul] execution-partition error: ${message}`);
return {
content: [{ type: 'text' as const, text: `Execution partition failed: ${message}` }],
isError: true,
};
}
},
);
}