@oh-my-dsh/auto-team 0.1.5 → 0.2.0
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.
- package/README.md +155 -133
- package/README.zh.md +110 -127
- package/cordis.patch.yml +1 -1
- package/lib/client.js +616 -0
- package/lib/index.d.ts +12 -78
- package/lib/index.js +324 -788
- package/package.json +46 -21
- package/src/assets/icon.svg +13 -9
- package/src/assets/locale/en.json +1 -1
- package/src/assets/locale/zh.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,834 +1,370 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import Schema from "@deepseek-ai/schemastery";
|
|
2
|
+
import paths from "node:path";
|
|
3
|
+
//#region src/worktree-path.ts
|
|
4
|
+
function filePath(args) {
|
|
5
|
+
if (args === null || typeof args !== "object" || !("file_path" in args)) return;
|
|
6
|
+
const { file_path: path } = args;
|
|
7
|
+
if (typeof path === "string" && path.trim() !== "") return path;
|
|
8
|
+
}
|
|
9
|
+
function absoluteFilePath(args) {
|
|
10
|
+
const path = filePath(args);
|
|
11
|
+
if (path === void 0 || !paths.isAbsolute(path)) return;
|
|
12
|
+
if (process.platform === "win32" && paths.win32.parse(path).root.length <= 1) return;
|
|
13
|
+
return path;
|
|
14
|
+
}
|
|
15
|
+
async function insideWorktree(fs, file, options) {
|
|
16
|
+
const { cwd, signal } = options;
|
|
17
|
+
const [root, marker, target] = await Promise.all([
|
|
18
|
+
fs.resolve(cwd, options),
|
|
19
|
+
fs.resolve(".git", options),
|
|
20
|
+
fs.resolve(file, options)
|
|
21
|
+
]);
|
|
22
|
+
if (paths.relative(cwd, fs.processPath(root)) !== "" || !fs.contains(root, target) || !fs.contains(root, marker) || fs.contains(marker, target)) return false;
|
|
23
|
+
const [directory, gitLink] = await Promise.all([fs.lstat(cwd, options, signal), fs.lstat(".git", options, signal)]);
|
|
24
|
+
return directory?.type === "directory" && gitLink?.type === "file" && !fs.contains(target, root);
|
|
25
|
+
}
|
|
26
|
+
function instructionPath(cwd, file) {
|
|
27
|
+
if (file === void 0) return;
|
|
28
|
+
const { root } = paths.parse(file);
|
|
29
|
+
if (process.platform === "win32" && root !== "" && (root.length <= 1 || !paths.isAbsolute(file))) return;
|
|
30
|
+
const target = paths.resolve(cwd, file);
|
|
31
|
+
const parts = paths.relative(cwd, target).split(paths.sep);
|
|
32
|
+
if (process.platform === "win32" && parts.some((part) => /[ .:]$|:/u.test(part))) return;
|
|
33
|
+
const [first, second] = parts.map((part) => {
|
|
34
|
+
if (process.platform === "win32") return part.toLowerCase();
|
|
35
|
+
return part;
|
|
36
|
+
});
|
|
37
|
+
if (parts.length === 1) {
|
|
38
|
+
const instructions = paths.join(cwd, "AGENTS.md");
|
|
39
|
+
if (paths.relative(instructions, target) === "") return target;
|
|
23
40
|
}
|
|
24
|
-
if (
|
|
25
|
-
}
|
|
26
|
-
/** Weighted 0..1 difficulty score. */
|
|
27
|
-
function scoreAssessment(assessment, weights) {
|
|
28
|
-
validateWeights(weights);
|
|
29
|
-
const total = weights.scope + weights.parallelism + weights.ambiguity + weights.risk + weights.expectedSteps;
|
|
30
|
-
return (normalizeFactor("scope", assessment.scope) * weights.scope + normalizeFactor("parallelism", assessment.parallelism) * weights.parallelism + normalizeFactor("ambiguity", assessment.ambiguity) * weights.ambiguity + normalizeFactor("risk", assessment.risk) * weights.risk + normalizeFactor("expectedSteps", assessment.expectedSteps) * weights.expectedSteps) / total;
|
|
41
|
+
if (first === ".agents" && second !== void 0 && second !== "worktrees") return target;
|
|
31
42
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (!Number.isFinite(manualGoalFactor) || manualGoalFactor < 0 || manualGoalFactor > 1) throw new TypeError("manualGoalFactor must be between 0 and 1");
|
|
39
|
-
let threshold = baseThreshold;
|
|
40
|
-
if (manualGoal) threshold = baseThreshold * manualGoalFactor;
|
|
41
|
-
return Math.min(1, Math.max(0, threshold));
|
|
42
|
-
}
|
|
43
|
-
/** Compare a score with the effective threshold. */
|
|
44
|
-
function decideTeam(score, threshold) {
|
|
45
|
-
if (score >= threshold) return "team";
|
|
46
|
-
return "solo";
|
|
47
|
-
}
|
|
48
|
-
/** Score one assessment and freeze the decision. */
|
|
49
|
-
function createScoreDecision(assessment, options) {
|
|
50
|
-
const threshold = effectiveThreshold(options.threshold, options.manualGoal, options.manualGoalFactor);
|
|
51
|
-
const score = scoreAssessment(assessment, options.weights);
|
|
52
|
-
return {
|
|
53
|
-
score,
|
|
54
|
-
threshold,
|
|
55
|
-
manualGoal: options.manualGoal,
|
|
56
|
-
decision: decideTeam(score, threshold)
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
//#endregion
|
|
60
|
-
//#region src/config.ts
|
|
61
|
-
/**
|
|
62
|
-
* Serializable configuration, schema, and direct-call defaults for auto-team.
|
|
63
|
-
*
|
|
64
|
-
* @module @oh-my-dsh/auto-team/config
|
|
65
|
-
*/
|
|
66
|
-
const DEFAULT_WEIGHTS = {
|
|
67
|
-
scope: .3,
|
|
68
|
-
parallelism: .25,
|
|
69
|
-
ambiguity: .15,
|
|
70
|
-
risk: .15,
|
|
71
|
-
expectedSteps: .15
|
|
72
|
-
};
|
|
73
|
-
const DEFAULT_THRESHOLD = .55;
|
|
74
|
-
const DEFAULT_MANUAL_GOAL_FACTOR = .6;
|
|
75
|
-
const DEFAULT_REPORTING_AGENT_NAME = "reporter";
|
|
76
|
-
const DEFAULT_REPORTING_PROVIDER = "spawn";
|
|
77
|
-
const DEFAULT_GOAL_MAX_ROUNDS = 256;
|
|
78
|
-
const DEFAULT_ENFORCE_LEAD_NO_CODE = true;
|
|
79
|
-
const DEFAULT_LEAD_DENIED_TOOLS = ["write", "edit"];
|
|
80
|
-
const REPORTER_NAME_PATTERN = /^[a-z][a-z0-9-]{0,31}$/u;
|
|
81
|
-
const UNIT_RANGE = {
|
|
82
|
-
min: 0,
|
|
83
|
-
max: 1
|
|
84
|
-
};
|
|
85
|
-
/** Loader-visible configuration schema and defaults. */
|
|
86
|
-
const Config = schema.object({
|
|
87
|
-
message: schema.string().default("auto-team loaded"),
|
|
88
|
-
threshold: schema.number().min(0).max(1).default(DEFAULT_THRESHOLD),
|
|
89
|
-
manualGoalFactor: schema.number().min(0).max(1).default(DEFAULT_MANUAL_GOAL_FACTOR),
|
|
90
|
-
weights: schema.object({
|
|
91
|
-
scope: schema.number().min(0).max(1).default(DEFAULT_WEIGHTS.scope),
|
|
92
|
-
parallelism: schema.number().min(0).max(1).default(DEFAULT_WEIGHTS.parallelism),
|
|
93
|
-
ambiguity: schema.number().min(0).max(1).default(DEFAULT_WEIGHTS.ambiguity),
|
|
94
|
-
risk: schema.number().min(0).max(1).default(DEFAULT_WEIGHTS.risk),
|
|
95
|
-
expectedSteps: schema.number().min(0).max(1).default(DEFAULT_WEIGHTS.expectedSteps)
|
|
96
|
-
}),
|
|
97
|
-
reportingAgentName: schema.string().pattern(REPORTER_NAME_PATTERN).default(DEFAULT_REPORTING_AGENT_NAME),
|
|
98
|
-
reportingProvider: schema.string().default(DEFAULT_REPORTING_PROVIDER),
|
|
99
|
-
goalMaxRounds: schema.natural().min(1).default(DEFAULT_GOAL_MAX_ROUNDS),
|
|
100
|
-
enforceLeadNoCode: schema.boolean().default(DEFAULT_ENFORCE_LEAD_NO_CODE),
|
|
101
|
-
leadDeniedTools: schema.array(schema.string()).default([...DEFAULT_LEAD_DENIED_TOOLS])
|
|
102
|
-
});
|
|
103
|
-
/** Validate one finite number against an inclusive range. */
|
|
104
|
-
function numberInRange(name, value, range) {
|
|
105
|
-
if (!Number.isFinite(value) || value < range.min || value > range.max) throw new TypeError(`${name} must be between ${range.min} and ${range.max}`);
|
|
106
|
-
return value;
|
|
107
|
-
}
|
|
108
|
-
/** Merge partial factor weights over the defaults and validate the total. */
|
|
109
|
-
function resolveWeights(config) {
|
|
110
|
-
const weights = {
|
|
111
|
-
scope: config.weights?.scope ?? DEFAULT_WEIGHTS.scope,
|
|
112
|
-
parallelism: config.weights?.parallelism ?? DEFAULT_WEIGHTS.parallelism,
|
|
113
|
-
ambiguity: config.weights?.ambiguity ?? DEFAULT_WEIGHTS.ambiguity,
|
|
114
|
-
risk: config.weights?.risk ?? DEFAULT_WEIGHTS.risk,
|
|
115
|
-
expectedSteps: config.weights?.expectedSteps ?? DEFAULT_WEIGHTS.expectedSteps
|
|
116
|
-
};
|
|
117
|
-
validateWeights(weights);
|
|
118
|
-
return weights;
|
|
119
|
-
}
|
|
120
|
-
/** Resolve and validate the reporting teammate name. */
|
|
121
|
-
function resolveReportingAgentName(config) {
|
|
122
|
-
const name = config.reportingAgentName ?? DEFAULT_REPORTING_AGENT_NAME;
|
|
123
|
-
if (!REPORTER_NAME_PATTERN.test(name) || name === "lead") throw new TypeError("reportingAgentName must be 1–32 lower-kebab-case characters and must not be \"lead\"");
|
|
124
|
-
return name;
|
|
125
|
-
}
|
|
126
|
-
/** Resolve and validate the goal round cap. */
|
|
127
|
-
function resolveGoalMaxRounds(config) {
|
|
128
|
-
const rounds = config.goalMaxRounds ?? DEFAULT_GOAL_MAX_ROUNDS;
|
|
129
|
-
if (!Number.isSafeInteger(rounds) || rounds < 1) throw new TypeError("goalMaxRounds must be a positive safe integer");
|
|
130
|
-
return rounds;
|
|
131
|
-
}
|
|
132
|
-
/** Resolve and validate the two score thresholds. */
|
|
133
|
-
function resolveThresholds(config) {
|
|
134
|
-
return {
|
|
135
|
-
threshold: numberInRange("threshold", config.threshold ?? DEFAULT_THRESHOLD, UNIT_RANGE),
|
|
136
|
-
manualGoalFactor: numberInRange("manualGoalFactor", config.manualGoalFactor ?? DEFAULT_MANUAL_GOAL_FACTOR, UNIT_RANGE)
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
/** Resolve the same defaults for direct callers that bypass Cordis Loader. */
|
|
140
|
-
function resolveConfig(config = {}) {
|
|
141
|
-
return {
|
|
142
|
-
message: config.message ?? "auto-team loaded",
|
|
143
|
-
...resolveThresholds(config),
|
|
144
|
-
weights: resolveWeights(config),
|
|
145
|
-
reportingAgentName: resolveReportingAgentName(config),
|
|
146
|
-
reportingProvider: config.reportingProvider ?? DEFAULT_REPORTING_PROVIDER,
|
|
147
|
-
goalMaxRounds: resolveGoalMaxRounds(config),
|
|
148
|
-
enforceLeadNoCode: config.enforceLeadNoCode ?? DEFAULT_ENFORCE_LEAD_NO_CODE,
|
|
149
|
-
leadDeniedTools: [...config.leadDeniedTools ?? DEFAULT_LEAD_DENIED_TOOLS]
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
//#endregion
|
|
153
|
-
//#region src/projection.ts
|
|
154
|
-
const SCORE_RECORD_TYPE = "plugin:auto-team/score";
|
|
155
|
-
const RESET_RECORD_TYPE = "plugin:auto-team/reset";
|
|
156
|
-
const scoreRecordSchema = z.object({
|
|
157
|
-
score: z.number().min(0).max(1),
|
|
158
|
-
threshold: z.number().min(0).max(1),
|
|
159
|
-
manualGoal: z.boolean(),
|
|
160
|
-
decision: z.enum(["team", "solo"]),
|
|
161
|
-
goal: z.enum([
|
|
162
|
-
"started",
|
|
163
|
-
"reused",
|
|
164
|
-
"none"
|
|
165
|
-
])
|
|
166
|
-
});
|
|
167
|
-
const autoTeamProjectionStateSchema = z.object({
|
|
168
|
-
score: scoreRecordSchema.optional(),
|
|
169
|
-
failure: z.string().optional()
|
|
170
|
-
});
|
|
171
|
-
/** Fold the first score after the latest reset into one durable decision. */
|
|
172
|
-
function applyAutoTeamProjection(state, event) {
|
|
173
|
-
const record = pluginRecordOf(event);
|
|
174
|
-
if (record === void 0) return state;
|
|
175
|
-
if (record.type === RESET_RECORD_TYPE) {
|
|
176
|
-
if (state.score === void 0) return state;
|
|
177
|
-
if (state.failure === void 0) return {};
|
|
178
|
-
return { failure: state.failure };
|
|
43
|
+
async function resolvesToInstruction(fs, { rawPath, candidate }, options) {
|
|
44
|
+
try {
|
|
45
|
+
const [root, target] = await Promise.all([fs.resolve(options.cwd, options), fs.resolve(rawPath, options)]);
|
|
46
|
+
return paths.relative(options.cwd, fs.processPath(root)) === "" && fs.contains(root, target) && paths.relative(candidate, fs.processPath(target)) === "";
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
179
49
|
}
|
|
180
|
-
if (record.type !== SCORE_RECORD_TYPE) return state;
|
|
181
|
-
if (state.score !== void 0) return state;
|
|
182
|
-
const parsed = scoreRecordSchema.safeParse(record.data);
|
|
183
|
-
if (!parsed.success) return {
|
|
184
|
-
...state,
|
|
185
|
-
failure: parsed.error.message
|
|
186
|
-
};
|
|
187
|
-
return {
|
|
188
|
-
...state,
|
|
189
|
-
score: parsed.data
|
|
190
|
-
};
|
|
191
50
|
}
|
|
192
|
-
/** Host-only projection definition for the auto-team score. */
|
|
193
|
-
const autoTeamProjectionDefinition = {
|
|
194
|
-
key: "autoTeam",
|
|
195
|
-
stateSchema: autoTeamProjectionStateSchema,
|
|
196
|
-
init: () => ({}),
|
|
197
|
-
apply: applyAutoTeamProjection,
|
|
198
|
-
stateVersion: 1
|
|
199
|
-
};
|
|
200
51
|
//#endregion
|
|
201
|
-
//#region src/
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
If the decision is solo, continue as a normal single agent.
|
|
220
|
-
|
|
221
|
-
If the decision is team:
|
|
222
|
-
- Your role is the Main Git repository and the team. Code changes belong to execution agents. The Lead's ${leadDeniedTools.join(", ")} calls are denied in Team mode. Treat that gate as a policy check: use pwsh, run_code, and other tools for review, tests, and inspection only.
|
|
223
|
-
- The plugin starts or reuses the goal and ensures a reporting agent named "${reportingAgentName}".
|
|
224
|
-
- The reporter writes directly to the user on its own cadence. Monitor the team on your own schedule, and keep execution moving while details are resolved.
|
|
225
|
-
- Create shared tasks with disjoint write scopes, then spawn execution agents. Each execution agent creates its own worktree for its task before editing; the Lead works in the main checkout.
|
|
226
|
-
- Let the goal runtime drive multi-round continuation; follow its rounds.
|
|
227
|
-
- After the workers finish, review the final diff and run the tests in the main checkout.`;
|
|
228
|
-
}
|
|
229
|
-
/** Initial prompt for the dedicated reporting teammate. */
|
|
230
|
-
function reporterPrompt() {
|
|
231
|
-
return `You are the reporting agent for an auto-team task. Your reader is the user. The Lead monitors the team on its own schedule.
|
|
232
|
-
|
|
233
|
-
Each turn ends after one update or one answer.
|
|
234
|
-
|
|
235
|
-
- Status update: read list_agents once, then write one concise update for the user covering what changed, what is blocked, and what the user needs to decide.
|
|
236
|
-
- Detail question: answer the user's question directly.
|
|
237
|
-
|
|
238
|
-
Write the result in your reply to the user. Then end the turn and wait for the user's next message. Your work is status, questions, and coordination; code changes belong to execution agents.`;
|
|
239
|
-
}
|
|
240
|
-
//#endregion
|
|
241
|
-
//#region src/reset-command.ts
|
|
242
|
-
/** Build the user-facing score reset command. */
|
|
243
|
-
function createResetCommand(autoTeam, registerTool) {
|
|
244
|
-
return {
|
|
245
|
-
name: "auto-team-reset",
|
|
246
|
-
description: "Clear the frozen Auto Team score for this session.",
|
|
247
|
-
handler: (invocation) => {
|
|
248
|
-
if (autoTeam.decisionOf(invocation.agent) === void 0) return {
|
|
249
|
-
kind: "error",
|
|
250
|
-
text: "No frozen Auto Team score exists in this session."
|
|
251
|
-
};
|
|
252
|
-
autoTeam.reset(invocation.agent);
|
|
253
|
-
registerTool(invocation.agent);
|
|
254
|
-
const message = invocation.rawInput.trim();
|
|
255
|
-
if (message.length > 0) invocation.agent.steer(createMessage({
|
|
256
|
-
role: "user",
|
|
257
|
-
content: [{
|
|
258
|
-
type: "text",
|
|
259
|
-
text: message
|
|
260
|
-
}],
|
|
261
|
-
source: { kind: "user" }
|
|
262
|
-
}));
|
|
263
|
-
return { kind: "success" };
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
/** Structural guard for the optional host commands service. */
|
|
268
|
-
function isCommandRuntime(value) {
|
|
269
|
-
if (typeof value !== "object" || value === null || !("register" in value)) return false;
|
|
270
|
-
return typeof value.register === "function";
|
|
271
|
-
}
|
|
272
|
-
//#endregion
|
|
273
|
-
//#region src/auto-team.ts
|
|
274
|
-
/** Host-side auto-team state and operations. */
|
|
275
|
-
var AutoTeam = class {
|
|
276
|
-
records = /* @__PURE__ */ new WeakMap();
|
|
277
|
-
resets = /* @__PURE__ */ new Set();
|
|
278
|
-
deps;
|
|
279
|
-
config;
|
|
280
|
-
constructor(deps, config) {
|
|
281
|
-
this.deps = deps;
|
|
282
|
-
this.config = config;
|
|
283
|
-
}
|
|
284
|
-
/** Whether this agent already has a frozen score. */
|
|
285
|
-
hasScore(agent) {
|
|
286
|
-
return !this.resets.has(agent) && this.records.has(agent);
|
|
287
|
-
}
|
|
288
|
-
/** Seed a record restored from durable storage without scoring again. */
|
|
289
|
-
restore(agent, record) {
|
|
290
|
-
if (!this.resets.has(agent) && !this.records.has(agent)) this.records.set(agent, record);
|
|
291
|
-
}
|
|
292
|
-
/** The frozen team decision, when this agent has one. */
|
|
293
|
-
decisionOf(agent) {
|
|
294
|
-
if (this.resets.has(agent)) return;
|
|
295
|
-
return (this.records.get(agent) ?? this.deps.store.read(agent))?.decision;
|
|
296
|
-
}
|
|
297
|
-
/** Clear the frozen score after a user-triggered reset. */
|
|
298
|
-
reset(agent) {
|
|
299
|
-
this.deps.store.reset(agent);
|
|
300
|
-
this.records.delete(agent);
|
|
301
|
-
this.resets.add(agent);
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Score one task, freeze the decision, then start or reuse its goal and
|
|
305
|
-
* ensure the reporting teammate.
|
|
306
|
-
*/
|
|
307
|
-
async score(agent, request) {
|
|
308
|
-
let existing = this.records.get(agent) ?? this.deps.store.read(agent);
|
|
309
|
-
if (this.resets.has(agent)) existing = void 0;
|
|
310
|
-
if (existing !== void 0) {
|
|
311
|
-
this.records.set(agent, existing);
|
|
312
|
-
return await this.finish(agent, existing, request.signal);
|
|
313
|
-
}
|
|
314
|
-
const currentGoal = this.deps.goals.get(agent);
|
|
315
|
-
let existingGoal = currentGoal;
|
|
316
|
-
if (currentGoal?.phase === "complete") existingGoal = void 0;
|
|
317
|
-
const manualGoal = existingGoal !== void 0;
|
|
318
|
-
const decision = createScoreDecision(request.assessment, {
|
|
319
|
-
weights: this.config.weights,
|
|
320
|
-
threshold: this.config.threshold,
|
|
321
|
-
manualGoal,
|
|
322
|
-
manualGoalFactor: this.config.manualGoalFactor
|
|
323
|
-
});
|
|
324
|
-
let goal = "none";
|
|
325
|
-
if (decision.decision === "team" && existingGoal === void 0) goal = this.startGoal(agent, request.objective);
|
|
326
|
-
else if (existingGoal !== void 0) goal = "reused";
|
|
327
|
-
const record = {
|
|
328
|
-
score: decision.score,
|
|
329
|
-
threshold: decision.threshold,
|
|
330
|
-
manualGoal,
|
|
331
|
-
decision: decision.decision,
|
|
332
|
-
goal
|
|
333
|
-
};
|
|
334
|
-
this.deps.store.write(agent, record);
|
|
335
|
-
this.records.set(agent, record);
|
|
336
|
-
this.resets.delete(agent);
|
|
337
|
-
return await this.finish(agent, record, request.signal);
|
|
338
|
-
}
|
|
339
|
-
/** Create the auto-started goal and return the reported action. */
|
|
340
|
-
startGoal(agent, objective) {
|
|
341
|
-
const trimmed = objective.trim();
|
|
342
|
-
if (trimmed.length === 0) throw new TypeError("objective is required when auto-team starts a goal");
|
|
343
|
-
this.deps.goals.create(agent, {
|
|
344
|
-
objective: trimmed,
|
|
345
|
-
maxGoalRounds: this.config.goalMaxRounds
|
|
346
|
-
});
|
|
347
|
-
return "started";
|
|
348
|
-
}
|
|
349
|
-
/** Add the reporter result to a frozen record, provisioning it when needed. */
|
|
350
|
-
async finish(agent, record, signal) {
|
|
351
|
-
if (record.decision === "solo") return {
|
|
352
|
-
decision: "solo",
|
|
353
|
-
goal: record.goal
|
|
52
|
+
//#region src/execution.ts
|
|
53
|
+
const leadDenied = "Auto Team: Lead coordinates, reviews, and integrates. Delegate implementation to local execution agents.";
|
|
54
|
+
const worktreeRequired = "Auto Team: use the official Worktree tool in this agent session to create and enter its own worktree before writing. If unavailable, stop writes and ask the Lead to check the profile or arrange a separate workspace.";
|
|
55
|
+
const pathDenied = "Auto Team: write or edit an absolute file_path inside this session’s verified worktree; do not write to the Git worktree marker.";
|
|
56
|
+
const instructionGuidance = "The current workspace’s root AGENTS.md and .agents/ guidance files are exempt from Auto Team’s write checks; .agents/worktrees/ is not. Native permissions still apply.";
|
|
57
|
+
const stateChanged = "Auto Team: the execution or worktree state changed before this write. Recheck the current team, goal, and worktree before retrying.";
|
|
58
|
+
function executionOf(ctx, agent) {
|
|
59
|
+
if (agent === void 0) return;
|
|
60
|
+
try {
|
|
61
|
+
const root = ctx.subagents.rootOf(agent);
|
|
62
|
+
if (!ctx.agentTeams.isEnabled(root)) return;
|
|
63
|
+
const goal = ctx.goals.get(root);
|
|
64
|
+
if (goal?.phase !== "active" || goal.activation !== "armed") return;
|
|
65
|
+
if (root === agent) return {
|
|
66
|
+
root,
|
|
67
|
+
goal,
|
|
68
|
+
role: "lead"
|
|
354
69
|
};
|
|
355
|
-
const reporter = await this.ensureReporter(agent, signal);
|
|
356
70
|
return {
|
|
357
|
-
|
|
358
|
-
goal
|
|
359
|
-
|
|
71
|
+
root,
|
|
72
|
+
goal,
|
|
73
|
+
role: "worker"
|
|
360
74
|
};
|
|
75
|
+
} catch {
|
|
76
|
+
return;
|
|
361
77
|
}
|
|
362
|
-
/** Reuse an existing reporting teammate or spawn one. */
|
|
363
|
-
async ensureReporter(agent, signal) {
|
|
364
|
-
const name = this.config.reportingAgentName;
|
|
365
|
-
const members = await this.deps.team.listMembers(agent, signal);
|
|
366
|
-
signal.throwIfAborted();
|
|
367
|
-
const existing = members.find((member) => member.name === name);
|
|
368
|
-
if (existing !== void 0) return existing.name;
|
|
369
|
-
return (await this.deps.team.spawnReporter(agent, {
|
|
370
|
-
name,
|
|
371
|
-
description: "User-facing status reporter for the auto team",
|
|
372
|
-
prompt: reporterPrompt(),
|
|
373
|
-
provider: this.config.reportingProvider,
|
|
374
|
-
signal
|
|
375
|
-
})).name;
|
|
376
|
-
}
|
|
377
|
-
};
|
|
378
|
-
//#endregion
|
|
379
|
-
//#region src/services.ts
|
|
380
|
-
/** Read the goal service or fail with a plugin-specific message. */
|
|
381
|
-
function goalService(ctx) {
|
|
382
|
-
const service = ctx.get("goals");
|
|
383
|
-
if (service === void 0) throw new Error("auto-team: goals service is unavailable");
|
|
384
|
-
return service;
|
|
385
|
-
}
|
|
386
|
-
/** Read the subagent service without assuming profile load order. */
|
|
387
|
-
function teamService(ctx) {
|
|
388
|
-
const service = ctx.get("subagents");
|
|
389
|
-
if (service === void 0) throw new Error("auto-team: subagents service is unavailable");
|
|
390
|
-
return service;
|
|
391
78
|
}
|
|
392
|
-
|
|
393
|
-
async function observeReporter(ctx, activation) {
|
|
79
|
+
function worktreeOf(ctx, { session }) {
|
|
394
80
|
try {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
81
|
+
const cwd = ctx.workingDirectory.get(session);
|
|
82
|
+
const { worktree = [] } = ctx.sessionProjections.snapshot(session, ["worktree"]).values;
|
|
83
|
+
const matches = worktree.filter((entry) => entry.owned && entry.worktree.path === cwd);
|
|
84
|
+
if (matches.length === 1) return matches[0]?.worktree;
|
|
85
|
+
} catch {}
|
|
86
|
+
}
|
|
87
|
+
function worktreeGuidance(ctx, agent) {
|
|
88
|
+
if (worktreeOf(ctx, agent) !== void 0) return "Use your current owned worktree.";
|
|
89
|
+
if (ctx.tools.get("create_worktree", agent) !== void 0) return "Use create_worktree in your own session with a unique branch name and the agreed local base commit.";
|
|
90
|
+
if (ctx.tools.get("enter_worktree", agent) !== void 0) return "Use enter_worktree in your own session with a unique name and an explicit local from commit; do not enter an existing checkout.";
|
|
91
|
+
return "The official worktree tool is unavailable. Stop before writing implementation files and tell the Lead.";
|
|
92
|
+
}
|
|
93
|
+
function guidance(ctx, agent, restrictWrites) {
|
|
94
|
+
const execution = executionOf(ctx, agent);
|
|
95
|
+
if (execution === void 0 || agent === void 0) return "";
|
|
96
|
+
const condition = "Auto Team applies only while the root session has native Agent Teams enabled and a live, active goal.";
|
|
97
|
+
if (!restrictWrites(execution.root)) {
|
|
98
|
+
const nativeOnly = "Auto Team write restrictions are off; native permissions still apply.";
|
|
99
|
+
if (execution.role === "lead") return `${condition} Coordinate independent tasks, review results, and integrate only as authorized. ${nativeOnly}`;
|
|
100
|
+
return `Coordinate changes with the Lead, avoid overwriting other agents’ work, and report results through native team communication. ${nativeOnly}`;
|
|
398
101
|
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
102
|
+
if (execution.role === "lead") return `${condition} As Lead, assign independent tasks to local execution agents with separate worktrees, a base commit, and acceptance criteria. Coordinate, review their diffs and tests, and integrate only as authorized; do not implement changes yourself or bypass write restrictions with other tools. Do not force parallelism for inseparable work. ${instructionGuidance}`;
|
|
103
|
+
return [
|
|
104
|
+
condition,
|
|
105
|
+
worktreeGuidance(ctx, agent),
|
|
106
|
+
instructionGuidance,
|
|
107
|
+
"Implement and verify only in your own worktree, using absolute file paths; do not bypass write restrictions with shell or other tools. Report the branch, changes, test results, and unresolved issues to the Lead using native team communication."
|
|
108
|
+
].join(" ");
|
|
109
|
+
}
|
|
110
|
+
function isProtected(name) {
|
|
111
|
+
return name === "write" || name === "edit";
|
|
112
|
+
}
|
|
113
|
+
function deny(reason) {
|
|
114
|
+
return {
|
|
115
|
+
isError: true,
|
|
116
|
+
content: [{
|
|
413
117
|
type: "text",
|
|
414
|
-
text:
|
|
118
|
+
text: reason
|
|
415
119
|
}],
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
120
|
+
error: { message: reason }
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function currentDirectory(ctx, agent) {
|
|
420
124
|
try {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}, "auto-team.reporter()");
|
|
425
|
-
} catch (error) {
|
|
426
|
-
await activation.dispose();
|
|
427
|
-
throw error;
|
|
125
|
+
return ctx.workingDirectory.get(agent.session);
|
|
126
|
+
} catch {
|
|
127
|
+
return;
|
|
428
128
|
}
|
|
429
|
-
return { name: request.name };
|
|
430
129
|
}
|
|
431
|
-
|
|
432
|
-
|
|
130
|
+
function writeDenial(ctx, { name, agent, arguments: args }, restrictWrites) {
|
|
131
|
+
if (!isProtected(name) || agent === void 0) return;
|
|
132
|
+
const cwd = currentDirectory(ctx, agent);
|
|
133
|
+
if (cwd !== void 0 && instructionPath(cwd, filePath(args)) !== void 0) return;
|
|
134
|
+
const execution = executionOf(ctx, agent);
|
|
135
|
+
if (execution === void 0 || !restrictWrites(execution.root)) return;
|
|
136
|
+
if (execution.role === "lead") return leadDenied;
|
|
137
|
+
if (worktreeOf(ctx, agent) === void 0) return worktreeRequired;
|
|
138
|
+
}
|
|
139
|
+
async function verifiedInstructionDirectory(ctx, { agent, arguments: args }, signal) {
|
|
140
|
+
const cwd = currentDirectory(ctx, agent);
|
|
141
|
+
const rawPath = filePath(args);
|
|
142
|
+
if (cwd === void 0 || rawPath === void 0) return;
|
|
143
|
+
const candidate = instructionPath(cwd, rawPath);
|
|
144
|
+
if (candidate !== void 0 && await resolvesToInstruction(ctx.fs, {
|
|
145
|
+
rawPath,
|
|
146
|
+
candidate
|
|
147
|
+
}, {
|
|
148
|
+
cwd,
|
|
149
|
+
signal
|
|
150
|
+
})) return cwd;
|
|
151
|
+
}
|
|
152
|
+
function sameExecution(previous, current) {
|
|
153
|
+
return current?.root === previous.root && current.goal.id === previous.goal.id && current.goal.revision === previous.goal.revision;
|
|
154
|
+
}
|
|
155
|
+
function sameWorktree(previous, current) {
|
|
156
|
+
return current?.path === previous.path && current.branch === previous.branch && current.baseCommit === previous.baseCommit;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Share one live execution decision between prompt assembly and the native tool
|
|
160
|
+
* pipeline.
|
|
161
|
+
*/
|
|
162
|
+
function installExecution(ctx, restrictWrites) {
|
|
433
163
|
const lifetime = new AbortController();
|
|
434
164
|
ctx.effect(() => () => {
|
|
435
165
|
lifetime.abort();
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
appendPluginRecord(agent.session, "plugin:auto-team/score", record);
|
|
460
|
-
},
|
|
461
|
-
reset: (agent) => {
|
|
462
|
-
appendPluginRecord(agent.session, "plugin:auto-team/reset", { at: Date.now() });
|
|
463
|
-
}
|
|
166
|
+
});
|
|
167
|
+
ctx.effect(() => ctx.systemPrompt.section({
|
|
168
|
+
name: "auto-team:execution",
|
|
169
|
+
order: ctx.systemPrompt.getSectionOrder("TEAM_POLICY"),
|
|
170
|
+
text: ({ agent }) => guidance(ctx, agent, restrictWrites)
|
|
171
|
+
}));
|
|
172
|
+
ctx.effect(() => ctx.tools.guard((exec) => writeDenial(ctx, exec, restrictWrites)));
|
|
173
|
+
ctx.on("tools/execute", async (exec, next) => {
|
|
174
|
+
const { agent, name, signal } = exec;
|
|
175
|
+
if (!isProtected(name) || agent === void 0 || signal.aborted) return next();
|
|
176
|
+
const execution = executionOf(ctx, agent);
|
|
177
|
+
if (execution === void 0 || !restrictWrites(execution.root)) return next();
|
|
178
|
+
const checkSignal = AbortSignal.any([signal, lifetime.signal]);
|
|
179
|
+
const worktree = worktreeOf(ctx, agent);
|
|
180
|
+
const instructionCwd = await verifiedInstructionDirectory(ctx, {
|
|
181
|
+
agent,
|
|
182
|
+
arguments: exec.arguments
|
|
183
|
+
}, checkSignal);
|
|
184
|
+
if (signal.aborted) return next();
|
|
185
|
+
if (lifetime.signal.aborted) return deny(stateChanged);
|
|
186
|
+
if (instructionCwd !== void 0) {
|
|
187
|
+
if (currentDirectory(ctx, agent) !== instructionCwd) return deny(stateChanged);
|
|
188
|
+
return next();
|
|
464
189
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
*/
|
|
475
|
-
/** Build the one-shot scoring tool for a Lead agent scope. */
|
|
476
|
-
function scoreTool(autoTeam, onScored) {
|
|
477
|
-
return defineTool({
|
|
478
|
-
name: "auto_team_score",
|
|
479
|
-
description: "Record the assessment. Call when no decision exists.",
|
|
480
|
-
parameters: {
|
|
481
|
-
scope: {
|
|
482
|
-
type: "integer",
|
|
483
|
-
required: true,
|
|
484
|
-
enum: [
|
|
485
|
-
1,
|
|
486
|
-
2,
|
|
487
|
-
3,
|
|
488
|
-
4,
|
|
489
|
-
5
|
|
490
|
-
],
|
|
491
|
-
description: "Expected change scope, from 1 low to 5 high."
|
|
492
|
-
},
|
|
493
|
-
parallelism: {
|
|
494
|
-
type: "integer",
|
|
495
|
-
required: true,
|
|
496
|
-
enum: [
|
|
497
|
-
1,
|
|
498
|
-
2,
|
|
499
|
-
3,
|
|
500
|
-
4,
|
|
501
|
-
5
|
|
502
|
-
],
|
|
503
|
-
description: "How much independent work can run in parallel, from 1 low to 5 high."
|
|
504
|
-
},
|
|
505
|
-
ambiguity: {
|
|
506
|
-
type: "integer",
|
|
507
|
-
required: true,
|
|
508
|
-
enum: [
|
|
509
|
-
1,
|
|
510
|
-
2,
|
|
511
|
-
3,
|
|
512
|
-
4,
|
|
513
|
-
5
|
|
514
|
-
],
|
|
515
|
-
description: "How much requirements or design uncertainty remains, from 1 low to 5 high."
|
|
516
|
-
},
|
|
517
|
-
risk: {
|
|
518
|
-
type: "integer",
|
|
519
|
-
required: true,
|
|
520
|
-
enum: [
|
|
521
|
-
1,
|
|
522
|
-
2,
|
|
523
|
-
3,
|
|
524
|
-
4,
|
|
525
|
-
5
|
|
526
|
-
],
|
|
527
|
-
description: "How costly a wrong change would be, from 1 low to 5 high."
|
|
528
|
-
},
|
|
529
|
-
expected_steps: {
|
|
530
|
-
type: "integer",
|
|
531
|
-
required: true,
|
|
532
|
-
enum: [
|
|
533
|
-
1,
|
|
534
|
-
2,
|
|
535
|
-
3,
|
|
536
|
-
4,
|
|
537
|
-
5
|
|
538
|
-
],
|
|
539
|
-
description: "Expected length of the task, from 1 short to 5 long."
|
|
540
|
-
},
|
|
541
|
-
objective: {
|
|
542
|
-
type: "string",
|
|
543
|
-
required: true,
|
|
544
|
-
description: "The task goal in its ordinary, universal meaning. Used only when auto-team starts a goal."
|
|
545
|
-
}
|
|
546
|
-
},
|
|
547
|
-
output: {
|
|
548
|
-
schema: {
|
|
549
|
-
type: "object",
|
|
550
|
-
additionalProperties: false,
|
|
551
|
-
properties: {
|
|
552
|
-
decision: {
|
|
553
|
-
type: "string",
|
|
554
|
-
required: true,
|
|
555
|
-
enum: ["team", "solo"]
|
|
556
|
-
},
|
|
557
|
-
goal: {
|
|
558
|
-
type: "string",
|
|
559
|
-
required: true,
|
|
560
|
-
enum: [
|
|
561
|
-
"started",
|
|
562
|
-
"reused",
|
|
563
|
-
"none"
|
|
564
|
-
]
|
|
565
|
-
},
|
|
566
|
-
reporter: { type: "string" }
|
|
567
|
-
}
|
|
568
|
-
},
|
|
569
|
-
render: (_args, value) => [{
|
|
570
|
-
type: "text",
|
|
571
|
-
text: JSON.stringify(value)
|
|
572
|
-
}]
|
|
573
|
-
},
|
|
574
|
-
async execute(args, exec) {
|
|
575
|
-
const { agent } = exec;
|
|
576
|
-
if (agent === void 0) throw new Error("auto_team_score requires a calling Agent");
|
|
577
|
-
const result = await autoTeam.score(agent, {
|
|
578
|
-
assessment: {
|
|
579
|
-
scope: args.scope,
|
|
580
|
-
parallelism: args.parallelism,
|
|
581
|
-
ambiguity: args.ambiguity,
|
|
582
|
-
risk: args.risk,
|
|
583
|
-
expectedSteps: args.expected_steps
|
|
584
|
-
},
|
|
585
|
-
objective: args.objective,
|
|
586
|
-
signal: exec.signal
|
|
190
|
+
if (execution.role === "lead") return deny(leadDenied);
|
|
191
|
+
if (worktree === void 0) return deny(worktreeRequired);
|
|
192
|
+
const file = absoluteFilePath(exec.arguments);
|
|
193
|
+
if (file === void 0) return deny(pathDenied);
|
|
194
|
+
let inside = false;
|
|
195
|
+
try {
|
|
196
|
+
inside = await insideWorktree(ctx.fs, file, {
|
|
197
|
+
cwd: worktree.path,
|
|
198
|
+
signal: checkSignal
|
|
587
199
|
});
|
|
588
|
-
|
|
589
|
-
|
|
200
|
+
} catch {
|
|
201
|
+
inside = false;
|
|
590
202
|
}
|
|
203
|
+
if (signal.aborted) return next();
|
|
204
|
+
if (lifetime.signal.aborted) return deny(stateChanged);
|
|
205
|
+
const current = executionOf(ctx, agent);
|
|
206
|
+
const currentTree = worktreeOf(ctx, agent);
|
|
207
|
+
if (!sameExecution(execution, current) || !restrictWrites(execution.root) || !sameWorktree(worktree, currentTree)) return deny(stateChanged);
|
|
208
|
+
if (!inside) return deny(pathDenied);
|
|
209
|
+
return next();
|
|
591
210
|
});
|
|
592
211
|
}
|
|
593
|
-
function registerScoreTool(tools, autoTeam) {
|
|
594
|
-
const holder = { dispose: () => void 0 };
|
|
595
|
-
const dispose = () => {
|
|
596
|
-
holder.dispose();
|
|
597
|
-
};
|
|
598
|
-
holder.dispose = tools.register(scoreTool(autoTeam, dispose));
|
|
599
|
-
return holder.dispose;
|
|
600
|
-
}
|
|
601
212
|
//#endregion
|
|
602
|
-
//#region src/
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
const filePath = args.file_path;
|
|
613
|
-
if (typeof filePath === "string") return filePath;
|
|
614
|
-
}
|
|
615
|
-
/** Read the worktree path from a create_worktree canonical value. */
|
|
616
|
-
function worktreePathOf(value) {
|
|
617
|
-
if (typeof value !== "object" || value === null || !("path" in value)) return;
|
|
618
|
-
const worktreePath = value.path;
|
|
619
|
-
if (typeof worktreePath === "string") return worktreePath;
|
|
620
|
-
}
|
|
621
|
-
/** Whether target resolves inside root. */
|
|
622
|
-
function isInside(root, target) {
|
|
623
|
-
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
624
|
-
return relative === "" || !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
625
|
-
}
|
|
626
|
-
/** Require one execution teammate to work inside its own worktree. */
|
|
627
|
-
function installWorkerGuard(agent, root, autoTeam) {
|
|
628
|
-
let hasWorktree = false;
|
|
629
|
-
let worktreePath = void 0;
|
|
630
|
-
const disposers = [];
|
|
631
|
-
const register = (dispose) => {
|
|
632
|
-
disposers.push(dispose);
|
|
633
|
-
};
|
|
213
|
+
//#region src/parallelism.ts
|
|
214
|
+
const minParallelism = 0;
|
|
215
|
+
const maxParallelism = 8;
|
|
216
|
+
const defaults = {
|
|
217
|
+
enabled: false,
|
|
218
|
+
restrictWrites: true,
|
|
219
|
+
lower: minParallelism,
|
|
220
|
+
upper: maxParallelism
|
|
221
|
+
};
|
|
222
|
+
function rootOf(ctx, agent) {
|
|
634
223
|
try {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
kind: "deny",
|
|
639
|
-
reason: "auto-team: create your own worktree with create_worktree before editing files"
|
|
640
|
-
};
|
|
641
|
-
if (worktreePath !== void 0) {
|
|
642
|
-
const filePath = filePathOf(exec.arguments);
|
|
643
|
-
if (filePath === void 0) return {
|
|
644
|
-
kind: "deny",
|
|
645
|
-
reason: "auto-team: write and edit require a file_path so the worktree guard can verify it"
|
|
646
|
-
};
|
|
647
|
-
if (!path.isAbsolute(filePath)) return {
|
|
648
|
-
kind: "deny",
|
|
649
|
-
reason: "auto-team: use an absolute file path inside your registered worktree"
|
|
650
|
-
};
|
|
651
|
-
if (!isInside(worktreePath, filePath)) return {
|
|
652
|
-
kind: "deny",
|
|
653
|
-
reason: `auto-team: edit path is outside your worktree: ${worktreePath}`
|
|
654
|
-
};
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
return await next();
|
|
658
|
-
});
|
|
659
|
-
register(() => {
|
|
660
|
-
pre();
|
|
661
|
-
});
|
|
662
|
-
const post = agent.ctx.on("tools/post-execute", async (exec, result, next) => {
|
|
663
|
-
if (exec.agent === agent && exec.name === "create_worktree" && !result.isError) {
|
|
664
|
-
hasWorktree = true;
|
|
665
|
-
const registered = worktreePathOf(result.value);
|
|
666
|
-
if (registered !== void 0) worktreePath = registered;
|
|
667
|
-
}
|
|
668
|
-
return await next();
|
|
669
|
-
});
|
|
670
|
-
register(() => {
|
|
671
|
-
post();
|
|
672
|
-
});
|
|
673
|
-
} catch (error) {
|
|
674
|
-
for (const dispose of disposers.toReversed()) dispose();
|
|
675
|
-
throw error;
|
|
224
|
+
return ctx.subagents.rootOf(agent);
|
|
225
|
+
} catch {
|
|
226
|
+
return;
|
|
676
227
|
}
|
|
677
|
-
return () => {
|
|
678
|
-
for (const dispose of disposers.toReversed()) dispose();
|
|
679
|
-
};
|
|
680
228
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
229
|
+
function runningIn(ctx, root) {
|
|
230
|
+
let running = 0;
|
|
231
|
+
for (const member of ctx.agents.list()) if (member !== root && member.status === "running" && rootOf(ctx, member) === root) running += 1;
|
|
232
|
+
return running;
|
|
233
|
+
}
|
|
234
|
+
function leadHint(ctx, root, settings) {
|
|
235
|
+
if (!settings.enabled || rootOf(ctx, root) !== root || !ctx.agentTeams.isEnabled(root)) return "";
|
|
236
|
+
const goal = ctx.goals.get(root);
|
|
237
|
+
if (goal?.phase !== "active" || goal.activation !== "armed") return "";
|
|
238
|
+
const running = runningIn(ctx, root);
|
|
239
|
+
if (running < settings.lower) return [
|
|
240
|
+
`Auto Team parallelism: ${running} running local subagents, below the lower bound ${settings.lower}.`,
|
|
241
|
+
"If independent work can be split safely, consider delegating it to additional local subagents",
|
|
242
|
+
"through the native team workflow, within host limits and approvals. Do not force parallelism."
|
|
243
|
+
].join(" ");
|
|
244
|
+
if (running > settings.upper) return [
|
|
245
|
+
`Auto Team parallelism: ${running} running local subagents, above the upper bound ${settings.upper}.`,
|
|
246
|
+
"Let active work continue; do not stop, rush, or archive active tasks.",
|
|
247
|
+
"Review only already completed worktrees and branches for safe cleanup. First verify that no processes use them",
|
|
248
|
+
"and that no uncommitted, untracked, ignored, or unintegrated work would be lost. Retain anything uncertain",
|
|
249
|
+
"and require user authorization and native approvals before any destructive change."
|
|
250
|
+
].join(" ");
|
|
251
|
+
return "";
|
|
252
|
+
}
|
|
253
|
+
function isRecord(value) {
|
|
254
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
255
|
+
}
|
|
256
|
+
function isBound(value) {
|
|
257
|
+
return typeof value === "number" && Number.isInteger(value) && value >= minParallelism && value <= maxParallelism;
|
|
258
|
+
}
|
|
259
|
+
function settingsPatch(payload, current) {
|
|
260
|
+
const { enabled = current.enabled, restrictWrites = current.restrictWrites, lower = current.lower, upper = current.upper } = payload;
|
|
261
|
+
if (typeof enabled !== "boolean" || typeof restrictWrites !== "boolean" || !isBound(lower) || !isBound(upper) || lower > upper) return;
|
|
685
262
|
return {
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
ctx.logger.warn(message);
|
|
691
|
-
}
|
|
263
|
+
enabled,
|
|
264
|
+
restrictWrites,
|
|
265
|
+
lower,
|
|
266
|
+
upper
|
|
692
267
|
};
|
|
693
268
|
}
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
disposers.push(dispose);
|
|
702
|
-
};
|
|
703
|
-
try {
|
|
704
|
-
register(systemPrompt.section({
|
|
705
|
-
name: "auto-team:policy",
|
|
706
|
-
order: systemPrompt.getSectionOrder("TEAM_POLICY"),
|
|
707
|
-
text: policyText(config.reportingAgentName, config.leadDeniedTools)
|
|
708
|
-
}));
|
|
709
|
-
if (!autoTeam.hasScore(agent)) register(registerScoreTool(tools, autoTeam));
|
|
710
|
-
const stop = agent.ctx.on("agent/turn-stopping", (payload) => {
|
|
711
|
-
if (payload.agent !== agent || autoTeam.hasScore(agent)) return;
|
|
712
|
-
agent.steer(createMessage({
|
|
713
|
-
role: "user",
|
|
714
|
-
content: [{
|
|
715
|
-
type: "text",
|
|
716
|
-
text: NO_SCORE_STEER_TEXT
|
|
717
|
-
}],
|
|
718
|
-
source: { kind: "user" }
|
|
719
|
-
}));
|
|
720
|
-
});
|
|
721
|
-
register(() => {
|
|
722
|
-
stop();
|
|
723
|
-
});
|
|
724
|
-
if (config.enforceLeadNoCode) {
|
|
725
|
-
const guard = agent.ctx.on("tools/pre-execute", async (exec, next) => {
|
|
726
|
-
if (exec.agent !== agent || autoTeam.decisionOf(agent) !== "team" || !config.leadDeniedTools.includes(exec.name)) return await next();
|
|
727
|
-
return {
|
|
728
|
-
kind: "deny",
|
|
729
|
-
reason: "auto-team: the Lead must delegate code changes to an execution agent"
|
|
730
|
-
};
|
|
731
|
-
});
|
|
732
|
-
register(() => {
|
|
733
|
-
guard();
|
|
734
|
-
});
|
|
269
|
+
function failure(code, message) {
|
|
270
|
+
return {
|
|
271
|
+
ok: false,
|
|
272
|
+
error: {
|
|
273
|
+
code,
|
|
274
|
+
message,
|
|
275
|
+
details: {}
|
|
735
276
|
}
|
|
736
|
-
} catch (error) {
|
|
737
|
-
for (const dispose of disposers.toReversed()) dispose();
|
|
738
|
-
throw error;
|
|
739
|
-
}
|
|
740
|
-
return () => {
|
|
741
|
-
for (const dispose of disposers.toReversed()) dispose();
|
|
742
277
|
};
|
|
743
278
|
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
ctx.effect(() => registerScoreTool(agent.ctx.tools, autoTeam), "auto-team.reset-score-tool()");
|
|
764
|
-
});
|
|
765
|
-
ctx.effect(() => commands.register(command), "auto-team.reset-command()");
|
|
766
|
-
resetCommandRegistered = true;
|
|
767
|
-
};
|
|
768
|
-
const install = (agent) => {
|
|
769
|
-
if (installed.has(agent)) return;
|
|
770
|
-
if (!servicesReady(ctx)) {
|
|
771
|
-
if (!warned) {
|
|
772
|
-
runtime.warn("auto-team: host services are unavailable; waiting for agent and goal services");
|
|
773
|
-
warned = true;
|
|
774
|
-
}
|
|
775
|
-
return;
|
|
279
|
+
const getEndpoint = "auto-team/parallelism/get";
|
|
280
|
+
const updateEndpoint = "auto-team/parallelism/update";
|
|
281
|
+
const getFields = /* @__PURE__ */ new Set(["sessionId"]);
|
|
282
|
+
const updateFields = /* @__PURE__ */ new Set([
|
|
283
|
+
"sessionId",
|
|
284
|
+
"enabled",
|
|
285
|
+
"restrictWrites",
|
|
286
|
+
"lower",
|
|
287
|
+
"upper"
|
|
288
|
+
]);
|
|
289
|
+
/** Register the root-only advisory and the optional native browser RPC channel. */
|
|
290
|
+
function installParallelism(ctx) {
|
|
291
|
+
const settings = /* @__PURE__ */ new WeakMap();
|
|
292
|
+
ctx.effect(() => ctx.systemPrompt.section({
|
|
293
|
+
name: "auto-team:parallelism",
|
|
294
|
+
order: ctx.systemPrompt.getSectionOrder("TEAM_POLICY"),
|
|
295
|
+
text: ({ agent }) => {
|
|
296
|
+
if (agent === void 0) return "";
|
|
297
|
+
return leadHint(ctx, agent, settings.get(agent.session) ?? defaults);
|
|
776
298
|
}
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
if (root !==
|
|
787
|
-
|
|
788
|
-
|
|
299
|
+
}));
|
|
300
|
+
const dispatch = (endpoint, payload, signal) => {
|
|
301
|
+
if (signal.aborted) return failure("gateway/cancelled", "Parallelism request cancelled.");
|
|
302
|
+
const update = endpoint === updateEndpoint;
|
|
303
|
+
if (!update && endpoint !== getEndpoint) return failure("gateway/not-found", "Unknown parallelism operation.");
|
|
304
|
+
let fields = getFields;
|
|
305
|
+
if (update) fields = updateFields;
|
|
306
|
+
if (!isRecord(payload) || typeof payload.sessionId !== "string" || payload.sessionId === "" || Object.keys(payload).some((key) => !fields.has(key))) return failure("gateway/bad-request", "Expected an explicit session ID and valid parallelism fields.");
|
|
307
|
+
const root = ctx.agents.list().find((agent) => agent.id === payload.sessionId);
|
|
308
|
+
if (root === void 0 || rootOf(ctx, root) !== root) return failure("auto-team/session-unavailable", "The requested session is not a live local root.");
|
|
309
|
+
let next = settings.get(root.session) ?? defaults;
|
|
310
|
+
const running = runningIn(ctx, root);
|
|
311
|
+
if (update) {
|
|
312
|
+
const patch = settingsPatch(payload, next);
|
|
313
|
+
if (patch === void 0) return failure("gateway/bad-request", "Expected boolean switches and ordered integer bounds from 0 to 8.");
|
|
314
|
+
settings.set(root.session, patch);
|
|
315
|
+
next = patch;
|
|
789
316
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
}
|
|
317
|
+
return {
|
|
318
|
+
ok: true,
|
|
319
|
+
value: {
|
|
320
|
+
...next,
|
|
321
|
+
running
|
|
322
|
+
}
|
|
323
|
+
};
|
|
797
324
|
};
|
|
798
|
-
ctx.
|
|
799
|
-
for (const
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
ctx.on("internal/service", (name) => {
|
|
815
|
-
if (name !== "subagents" && name !== "agents" && name !== "goals" && name !== "tools" && name !== "systemPrompt" && name !== "commands") return;
|
|
816
|
-
ensureResetCommand();
|
|
817
|
-
for (const agent of ctx.get("agents")?.list() ?? []) install(agent);
|
|
325
|
+
ctx.inject(["connection"], (web) => {
|
|
326
|
+
for (const endpoint of [getEndpoint, updateEndpoint]) web.connection.fetch.register({
|
|
327
|
+
path: `/api/${endpoint}`,
|
|
328
|
+
methods: ["POST"],
|
|
329
|
+
requestBody: "buffered",
|
|
330
|
+
async fetch(request) {
|
|
331
|
+
const body = await request.json().catch(() => false);
|
|
332
|
+
if (!isRecord(body) || body.type !== "client-request" || body.method !== endpoint || typeof body.rpcId !== "string") return new Response("Invalid request", { status: 400 });
|
|
333
|
+
const result = dispatch(endpoint, body.payload, request.signal);
|
|
334
|
+
return Response.json({
|
|
335
|
+
type: "server-response",
|
|
336
|
+
rpcId: body.rpcId,
|
|
337
|
+
result
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
});
|
|
818
341
|
});
|
|
819
|
-
|
|
820
|
-
for (const agent of ctx.get("agents")?.list() ?? []) install(agent);
|
|
342
|
+
return (root) => (settings.get(root.session) ?? defaults).restrictWrites;
|
|
821
343
|
}
|
|
822
344
|
//#endregion
|
|
823
345
|
//#region src/index.ts
|
|
346
|
+
/** Existing Cordis row identity. */
|
|
347
|
+
const name = "auto-team";
|
|
348
|
+
/** Services supplied by the native DSH profile, never started by this plugin. */
|
|
349
|
+
const inject = [
|
|
350
|
+
"agentTeams",
|
|
351
|
+
"agents",
|
|
352
|
+
"goals",
|
|
353
|
+
"subagents",
|
|
354
|
+
"systemPrompt",
|
|
355
|
+
"tools",
|
|
356
|
+
"sessionProjections",
|
|
357
|
+
"workingDirectory",
|
|
358
|
+
"fs"
|
|
359
|
+
];
|
|
360
|
+
const Config = Schema.object({ message: Schema.string().default("auto-team loaded") });
|
|
824
361
|
/**
|
|
825
|
-
*
|
|
826
|
-
*
|
|
827
|
-
* @module @oh-my-dsh/auto-team
|
|
362
|
+
* Install fiber-owned execution rules without changing native team or goal
|
|
363
|
+
* state.
|
|
828
364
|
*/
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
365
|
+
function apply(ctx, config) {
|
|
366
|
+
installExecution(ctx, installParallelism(ctx));
|
|
367
|
+
ctx.logger.info(config.message);
|
|
368
|
+
}
|
|
833
369
|
//#endregion
|
|
834
370
|
export { Config, apply, inject, name };
|