@guilhermednobre/agent-kit 0.1.0 → 0.3.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/CHANGELOG.md +49 -0
- package/README.md +102 -47
- package/bin/cli.js +79 -38
- package/package.json +12 -4
- package/src/cli/tools.js +51 -0
- package/src/cli/wf.js +400 -0
- package/src/core/briefing.js +188 -0
- package/src/core/config.js +321 -0
- package/src/core/dag.js +103 -0
- package/src/core/engine.js +505 -0
- package/src/core/policy.js +145 -0
- package/src/core/report.js +104 -0
- package/src/core/states.js +59 -0
- package/src/core/store.js +60 -0
- package/src/core/verify.js +54 -0
- package/src/detect.js +6 -0
- package/src/generate.js +55 -0
- package/src/install.js +22 -14
- package/src/render.js +5 -7
- package/src/runtimes/index.js +24 -0
- package/src/runtimes/orca.js +61 -0
- package/src/runtimes/t3.js +87 -0
- package/src/trackers/index.js +34 -0
- package/src/trackers/linear-t3.js +138 -0
- package/src/trackers/linear.js +209 -0
- package/templates/AGENTS.md +111 -110
- package/templates/CLAUDE.md +185 -306
- package/templates/agent-kit/hooks/pretooluse.mjs +53 -0
- package/templates/agent-kit/hooks/session-context.mjs +41 -0
- package/templates/agent-kit/runtime/orca.md +68 -0
- package/templates/agent-kit/runtime/t3.md +109 -0
- package/templates/claude/commands/analisar-consistencia.md +111 -0
- package/templates/claude/commands/avaliar-ideia.md +63 -0
- package/templates/claude/commands/configurar-worker.md +116 -0
- package/templates/claude/commands/criar-prd.md +16 -1
- package/templates/claude/commands/criar-tasks.md +40 -3
- package/templates/claude/commands/criar-techspec.md +12 -1
- package/templates/claude/commands/executar-bugfix.md +11 -11
- package/templates/claude/commands/executar-qa.md +14 -15
- package/templates/claude/commands/executar-release.md +20 -0
- package/templates/claude/commands/executar-review.md +51 -31
- package/templates/claude/commands/executar-task.md +14 -23
- package/templates/claude/commands/executar-workflow.md +101 -0
- package/templates/claude/commands/fechar-ciclo.md +88 -0
- package/templates/claude/commands/replanejar-workflow.md +32 -0
- package/templates/claude/commands/retomar-workflow.md +30 -0
- package/templates/claude/commands/status-workflow.md +19 -0
- package/templates/claude/rules/README.md +2 -2
- package/templates/claude/settings.json +25 -0
- package/templates/claude/templates/bugs-template.md +2 -2
- package/templates/claude/templates/plan-template.json +33 -0
- package/templates/claude/templates/prd-template.md +65 -61
- package/templates/claude/templates/task-template.md +8 -8
- package/templates/claude/templates/tasks-template.md +1 -1
- package/templates/claude/templates/techspec-template.md +129 -129
- package/templates/claude/templates/worker-brief-template.md +11 -7
- package/templates/docs/agents/worker.md +94 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { DEFAULT_PROTECTED_ACTIONS } from "./policy.js";
|
|
5
|
+
import { TASK_STATES } from "./states.js";
|
|
6
|
+
|
|
7
|
+
export const CONFIG_DIR = ".agent-kit";
|
|
8
|
+
export const RUNTIMES = ["t3", "orca"];
|
|
9
|
+
export const TRACKERS = ["local", "linear"];
|
|
10
|
+
export const ROLES = [
|
|
11
|
+
"pm",
|
|
12
|
+
"architect",
|
|
13
|
+
"implementation",
|
|
14
|
+
"docs",
|
|
15
|
+
"test",
|
|
16
|
+
"debug",
|
|
17
|
+
"research",
|
|
18
|
+
"design",
|
|
19
|
+
"qa",
|
|
20
|
+
"system",
|
|
21
|
+
"uiux",
|
|
22
|
+
"security",
|
|
23
|
+
"product-owner",
|
|
24
|
+
"review",
|
|
25
|
+
"release",
|
|
26
|
+
];
|
|
27
|
+
export const PROFILE_NAMES = ["coordinator", "fast", "standard", "advanced", "validator"];
|
|
28
|
+
const COORDINATOR = "coordinator";
|
|
29
|
+
|
|
30
|
+
export function defaultConfig({ runtime = "t3", checks = [] } = {}) {
|
|
31
|
+
return {
|
|
32
|
+
version: 1,
|
|
33
|
+
runtime,
|
|
34
|
+
tracker: {
|
|
35
|
+
kind: "local",
|
|
36
|
+
linear: {
|
|
37
|
+
via: runtime === "t3" ? "t3" : "api",
|
|
38
|
+
apiKeyEnv: "LINEAR_API_KEY",
|
|
39
|
+
teamKey: null,
|
|
40
|
+
projectId: null,
|
|
41
|
+
labels: ["agent-kit"],
|
|
42
|
+
stateMap: {
|
|
43
|
+
pending: "unstarted",
|
|
44
|
+
ready: "unstarted",
|
|
45
|
+
running: "started",
|
|
46
|
+
testing: "started",
|
|
47
|
+
review: "started",
|
|
48
|
+
blocked: "started",
|
|
49
|
+
failed: "started",
|
|
50
|
+
completed: "completed",
|
|
51
|
+
cancelled: "canceled",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
checks,
|
|
56
|
+
profiles: {
|
|
57
|
+
fast: workerProfile("Cheap, simple work: docs, boilerplate, mechanical edits"),
|
|
58
|
+
standard: workerProfile("Everyday implementation and tests"),
|
|
59
|
+
advanced: workerProfile("Complex implementation and debugging"),
|
|
60
|
+
validator: workerProfile("Independent validation gates: QA, system, UI/UX, security, product owner"),
|
|
61
|
+
coordinator: {
|
|
62
|
+
provider: COORDINATOR,
|
|
63
|
+
model: null,
|
|
64
|
+
options: {},
|
|
65
|
+
description: "The coordinator session itself (Claude Opus): runs the step, no delegation",
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
roles: {
|
|
69
|
+
pm: "coordinator",
|
|
70
|
+
architect: "coordinator",
|
|
71
|
+
implementation: "standard",
|
|
72
|
+
docs: "fast",
|
|
73
|
+
test: "standard",
|
|
74
|
+
debug: "advanced",
|
|
75
|
+
research: "fast",
|
|
76
|
+
design: "standard",
|
|
77
|
+
qa: "validator",
|
|
78
|
+
system: "validator",
|
|
79
|
+
uiux: "validator",
|
|
80
|
+
security: "validator",
|
|
81
|
+
"product-owner": "validator",
|
|
82
|
+
review: "validator",
|
|
83
|
+
release: "coordinator",
|
|
84
|
+
},
|
|
85
|
+
runtimes: {
|
|
86
|
+
t3: { isolation: "shared" },
|
|
87
|
+
orca: { launchCommand: null, readiness: null },
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function workerProfile(description) {
|
|
93
|
+
return { provider: "antigravity", model: null, options: {}, description };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function defaultAutonomy() {
|
|
97
|
+
return {
|
|
98
|
+
version: 1,
|
|
99
|
+
level: "autonomous",
|
|
100
|
+
maxWorkers: 3,
|
|
101
|
+
maxAttemptsPerTask: 3,
|
|
102
|
+
maxFixCycles: 3,
|
|
103
|
+
taskTimeoutMinutes: 60,
|
|
104
|
+
retry: {
|
|
105
|
+
strategy: "escalate-profile",
|
|
106
|
+
ladder: ["fast", "standard", "advanced"],
|
|
107
|
+
},
|
|
108
|
+
escalation: {
|
|
109
|
+
onBudgetExhausted: "coordinator-takeover",
|
|
110
|
+
askUserFor: [
|
|
111
|
+
"greenfield-stack",
|
|
112
|
+
"product-scope-ambiguity",
|
|
113
|
+
"business-rule-conflict",
|
|
114
|
+
"protected-action",
|
|
115
|
+
"budget-exhausted-after-takeover",
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
permissions: {
|
|
119
|
+
installDevDependencies: true,
|
|
120
|
+
localCommits: false,
|
|
121
|
+
push: false,
|
|
122
|
+
denyWrite: [".env", ".env.*", "**/*.pem", "**/*.key", "**/secrets/**", "**/credentials*.json"],
|
|
123
|
+
},
|
|
124
|
+
protectedActions: {
|
|
125
|
+
decision: "ask",
|
|
126
|
+
allow: [],
|
|
127
|
+
categories: DEFAULT_PROTECTED_ACTIONS,
|
|
128
|
+
},
|
|
129
|
+
completion: {
|
|
130
|
+
requireAllTasks: true,
|
|
131
|
+
requireFinalChecks: true,
|
|
132
|
+
requireIndependentReview: true,
|
|
133
|
+
requireTaskChecks: false,
|
|
134
|
+
},
|
|
135
|
+
recovery: {
|
|
136
|
+
reconcileBeforeRedispatch: true,
|
|
137
|
+
preservePartialResults: true,
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function readJson(path) {
|
|
143
|
+
if (!existsSync(path)) return null;
|
|
144
|
+
const text = await readFile(path, "utf8");
|
|
145
|
+
try {
|
|
146
|
+
return JSON.parse(text);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
throw new Error(`${path} is not valid JSON: ${error.message}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function merge(base, override) {
|
|
153
|
+
if (!isObject(base) || !isObject(override)) return override === undefined ? base : override;
|
|
154
|
+
const result = { ...base };
|
|
155
|
+
for (const [key, value] of Object.entries(override)) result[key] = merge(base[key], value);
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function isObject(value) {
|
|
160
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function defaultWorkflow() {
|
|
164
|
+
return {
|
|
165
|
+
version: 1,
|
|
166
|
+
phases: [
|
|
167
|
+
{ id: "spec", role: "pm", commands: ["/criar-prd"], output: "tasks/prd-<slug>/prd.md" },
|
|
168
|
+
{ id: "plan", role: "architect", commands: ["/criar-techspec", "/criar-tasks", "/analisar-consistencia"], output: "tasks/prd-<slug>/plan.json" },
|
|
169
|
+
{ id: "implement", role: "implementation", commands: ["/executar-task", "/executar-bugfix"] },
|
|
170
|
+
{ id: "validate", gates: ["qa", "system", "uiux", "security"] },
|
|
171
|
+
{ id: "accept", gates: ["acceptance"] },
|
|
172
|
+
{ id: "release", role: "release", commands: ["/executar-release"], requiresApproval: true },
|
|
173
|
+
],
|
|
174
|
+
gates: [
|
|
175
|
+
{ id: "qa", role: "qa", when: "always", focus: "Tests and behaviour: run the task's tests, exercise edge cases and, when there is a UI, the browser flows" },
|
|
176
|
+
{ id: "system", role: "system", when: "always", focus: "Consistency with the techspec, architecture, contracts and repository conventions" },
|
|
177
|
+
{ id: "uiux", role: "uiux", when: "tag:ui", focus: "UI states, responsiveness, accessibility (WCAG 2.2), consistency with the design system" },
|
|
178
|
+
{ id: "security", role: "security", when: "tag:security", focus: "Authentication, authorization, input validation, secrets, PII and LGPD" },
|
|
179
|
+
{ id: "acceptance", role: "product-owner", when: "always", after: ["qa", "system", "uiux", "security"], focus: "Every acceptance criterion, one by one, with evidence" },
|
|
180
|
+
],
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function loadSettings(root) {
|
|
185
|
+
const dir = join(root, CONFIG_DIR);
|
|
186
|
+
const rawConfig = await readJson(join(dir, "config.json"));
|
|
187
|
+
const rawAutonomy = await readJson(join(dir, "autonomy.json"));
|
|
188
|
+
const rawWorkflow = await readJson(join(dir, "workflow.json"));
|
|
189
|
+
const config = merge(defaultConfig({ runtime: rawConfig?.runtime }), rawConfig || {});
|
|
190
|
+
const autonomy = merge(defaultAutonomy(), rawAutonomy || {});
|
|
191
|
+
const workflow = rawWorkflow || defaultWorkflow();
|
|
192
|
+
return {
|
|
193
|
+
config,
|
|
194
|
+
autonomy,
|
|
195
|
+
workflow,
|
|
196
|
+
found: { config: Boolean(rawConfig), autonomy: Boolean(rawAutonomy), workflow: Boolean(rawWorkflow) },
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function validateWorkflow(workflow, config) {
|
|
201
|
+
const errors = [];
|
|
202
|
+
const gates = workflow?.gates;
|
|
203
|
+
if (!Array.isArray(gates) || gates.length === 0) return { errors: ["gates must be a non-empty array"], warnings: [] };
|
|
204
|
+
const ids = new Set(gates.map((gate) => gate.id));
|
|
205
|
+
if (ids.size !== gates.length) errors.push("gate ids must be unique");
|
|
206
|
+
for (const gate of gates) {
|
|
207
|
+
if (!gate.id || !gate.role) errors.push("every gate needs an id and a role");
|
|
208
|
+
if (gate.role && !ROLES.includes(gate.role)) errors.push(`gate "${gate.id}" has unknown role "${gate.role}"`);
|
|
209
|
+
if (config && gate.role && !config.roles?.[gate.role]) errors.push(`gate "${gate.id}": role "${gate.role}" has no profile in config.roles`);
|
|
210
|
+
if (!/^(always|tag:[a-z0-9-]+)$/.test(gate.when || "always")) errors.push(`gate "${gate.id}" has invalid when "${gate.when}"`);
|
|
211
|
+
for (const dep of gate.after || []) if (!ids.has(dep)) errors.push(`gate "${gate.id}" runs after unknown gate "${dep}"`);
|
|
212
|
+
}
|
|
213
|
+
return { errors, warnings: [] };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function validateConfig(config) {
|
|
217
|
+
const errors = [];
|
|
218
|
+
const warnings = [];
|
|
219
|
+
if (!RUNTIMES.includes(config.runtime)) errors.push(`runtime must be one of ${RUNTIMES.join(", ")}`);
|
|
220
|
+
if (!TRACKERS.includes(config.tracker?.kind)) errors.push(`tracker.kind must be one of ${TRACKERS.join(", ")}`);
|
|
221
|
+
if (config.tracker?.kind === "linear") validateLinear(config.tracker.linear || {}, errors, config.runtime);
|
|
222
|
+
if (!Array.isArray(config.checks)) errors.push("checks must be an array");
|
|
223
|
+
for (const [index, check] of (config.checks || []).entries()) {
|
|
224
|
+
if (!check?.name || !check?.command) errors.push(`checks[${index}] needs name and command`);
|
|
225
|
+
}
|
|
226
|
+
for (const [name, profile] of Object.entries(config.profiles || {})) {
|
|
227
|
+
if (!profile?.provider) errors.push(`profiles.${name}.provider is required`);
|
|
228
|
+
else if (profile.provider !== COORDINATOR && !profile.model) {
|
|
229
|
+
warnings.push(`profiles.${name}.model is not set — run /configurar-worker before dispatching`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (const [role, profile] of Object.entries(config.roles || {})) {
|
|
233
|
+
if (!config.profiles?.[profile]) errors.push(`roles.${role} points to unknown profile "${profile}"`);
|
|
234
|
+
}
|
|
235
|
+
if (config.runtime === "orca" && !config.runtimes?.orca?.launchCommand) {
|
|
236
|
+
warnings.push("runtimes.orca.launchCommand is not set — run /configurar-worker");
|
|
237
|
+
}
|
|
238
|
+
return { errors, warnings };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function validateLinear(linear, errors, runtime) {
|
|
242
|
+
const via = linear.via || "api";
|
|
243
|
+
if (!["api", "t3"].includes(via)) errors.push('tracker.linear.via must be "api" or "t3"');
|
|
244
|
+
if (via === "t3" && runtime !== "t3") errors.push('tracker.linear.via "t3" needs runtime "t3"; use "api" with Orca');
|
|
245
|
+
if (via === "api" && !linear.teamKey) errors.push('tracker.linear.teamKey is required when tracker.linear.via is "api"');
|
|
246
|
+
if (via === "api" && !linear.apiKeyEnv) errors.push("tracker.linear.apiKeyEnv is required");
|
|
247
|
+
for (const state of Object.keys(linear.stateMap || {})) {
|
|
248
|
+
if (!TASK_STATES.includes(state)) errors.push(`tracker.linear.stateMap has unknown task state "${state}"`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function validateAutonomy(autonomy, config) {
|
|
253
|
+
const errors = [];
|
|
254
|
+
for (const key of ["maxWorkers", "maxAttemptsPerTask", "maxFixCycles", "taskTimeoutMinutes"]) {
|
|
255
|
+
if (!Number.isInteger(autonomy[key]) || autonomy[key] < 1) errors.push(`${key} must be a positive integer`);
|
|
256
|
+
}
|
|
257
|
+
if (!["autonomous", "supervised"].includes(autonomy.level)) {
|
|
258
|
+
errors.push('level must be "autonomous" or "supervised"');
|
|
259
|
+
}
|
|
260
|
+
if (!["escalate-profile", "same-profile"].includes(autonomy.retry?.strategy)) {
|
|
261
|
+
errors.push('retry.strategy must be "escalate-profile" or "same-profile"');
|
|
262
|
+
}
|
|
263
|
+
for (const name of autonomy.retry?.ladder || []) {
|
|
264
|
+
if (config && !config.profiles?.[name]) errors.push(`retry.ladder references unknown profile "${name}"`);
|
|
265
|
+
}
|
|
266
|
+
if (!["coordinator-takeover", "ask-user", "fail"].includes(autonomy.escalation?.onBudgetExhausted)) {
|
|
267
|
+
errors.push('escalation.onBudgetExhausted must be "coordinator-takeover", "ask-user" or "fail"');
|
|
268
|
+
}
|
|
269
|
+
if (!["ask", "deny"].includes(autonomy.protectedActions?.decision)) {
|
|
270
|
+
errors.push('protectedActions.decision must be "ask" or "deny"');
|
|
271
|
+
}
|
|
272
|
+
for (const category of autonomy.protectedActions?.categories || []) {
|
|
273
|
+
for (const pattern of category.patterns || []) {
|
|
274
|
+
try {
|
|
275
|
+
new RegExp(pattern, "i");
|
|
276
|
+
} catch {
|
|
277
|
+
errors.push(`protectedActions category "${category.id}" has an invalid pattern: ${pattern}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return { errors, warnings: [] };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function validatePlan(plan) {
|
|
285
|
+
const errors = [];
|
|
286
|
+
if (!plan || !Array.isArray(plan.tasks) || plan.tasks.length === 0) {
|
|
287
|
+
return ["plan.tasks must be a non-empty array"];
|
|
288
|
+
}
|
|
289
|
+
for (const [index, task] of plan.tasks.entries()) {
|
|
290
|
+
const label = task?.id ? `task "${task.id}"` : `tasks[${index}]`;
|
|
291
|
+
if (!task?.id || typeof task.id !== "string") errors.push(`${label} needs a string id`);
|
|
292
|
+
if (!task?.title) errors.push(`${label} needs a title`);
|
|
293
|
+
if (task?.role && !ROLES.includes(task.role)) errors.push(`${label} has unknown role "${task.role}"`);
|
|
294
|
+
if (!Array.isArray(task?.files) || task.files.length === 0) {
|
|
295
|
+
errors.push(`${label} must list the files it owns (files)`);
|
|
296
|
+
}
|
|
297
|
+
if (!Array.isArray(task?.acceptance) || task.acceptance.length === 0) {
|
|
298
|
+
errors.push(`${label} needs at least one acceptance criterion`);
|
|
299
|
+
}
|
|
300
|
+
for (const field of ["dependsOn", "validation", "context", "tags"]) {
|
|
301
|
+
if (task?.[field] !== undefined && !Array.isArray(task[field])) errors.push(`${label}.${field} must be an array`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return errors;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function profileForTask(task, config, override) {
|
|
308
|
+
const name = override || task.profile || config.roles?.[task.role || "implementation"] || "standard";
|
|
309
|
+
const profile = config.profiles?.[name];
|
|
310
|
+
if (!profile) throw new Error(`Unknown profile "${name}" for task ${task.id}`);
|
|
311
|
+
return { name, ...profile };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function executorId(profile) {
|
|
315
|
+
if (profile.provider === COORDINATOR) return COORDINATOR;
|
|
316
|
+
return `${profile.provider}:${profile.model || "unset"}`;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function isCoordinatorProfile(profile) {
|
|
320
|
+
return profile.provider === COORDINATOR;
|
|
321
|
+
}
|
package/src/core/dag.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
const GLOB_CHAR = /[*?[{]/;
|
|
2
|
+
|
|
3
|
+
export function validateGraph(tasks) {
|
|
4
|
+
const errors = [];
|
|
5
|
+
const ids = new Set();
|
|
6
|
+
for (const task of tasks) {
|
|
7
|
+
if (ids.has(task.id)) errors.push(`duplicate task id "${task.id}"`);
|
|
8
|
+
ids.add(task.id);
|
|
9
|
+
}
|
|
10
|
+
for (const task of tasks) {
|
|
11
|
+
for (const dep of task.dependsOn || []) {
|
|
12
|
+
if (!ids.has(dep)) errors.push(`task "${task.id}" depends on unknown task "${dep}"`);
|
|
13
|
+
if (dep === task.id) errors.push(`task "${task.id}" depends on itself`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (errors.length === 0) {
|
|
17
|
+
const cycle = findCycle(tasks);
|
|
18
|
+
if (cycle) errors.push(`dependency cycle: ${cycle.join(" -> ")}`);
|
|
19
|
+
}
|
|
20
|
+
return errors;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function findCycle(tasks) {
|
|
24
|
+
const byId = new Map(tasks.map((task) => [task.id, task]));
|
|
25
|
+
const state = new Map();
|
|
26
|
+
const stack = [];
|
|
27
|
+
const visit = (id) => {
|
|
28
|
+
state.set(id, "visiting");
|
|
29
|
+
stack.push(id);
|
|
30
|
+
for (const dep of byId.get(id).dependsOn || []) {
|
|
31
|
+
if (state.get(dep) === "visiting") return [...stack.slice(stack.indexOf(dep)), dep];
|
|
32
|
+
if (!state.has(dep)) {
|
|
33
|
+
const found = visit(dep);
|
|
34
|
+
if (found) return found;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
stack.pop();
|
|
38
|
+
state.set(id, "done");
|
|
39
|
+
return null;
|
|
40
|
+
};
|
|
41
|
+
for (const task of tasks) {
|
|
42
|
+
if (!state.has(task.id)) {
|
|
43
|
+
const found = visit(task.id);
|
|
44
|
+
if (found) return found;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function executionWaves(tasks) {
|
|
51
|
+
const remaining = new Map(tasks.map((task) => [task.id, new Set(task.dependsOn || [])]));
|
|
52
|
+
const waves = [];
|
|
53
|
+
while (remaining.size > 0) {
|
|
54
|
+
const wave = [...remaining].filter(([, deps]) => deps.size === 0).map(([id]) => id);
|
|
55
|
+
if (wave.length === 0) throw new Error("dependency cycle: cannot order tasks");
|
|
56
|
+
waves.push(wave);
|
|
57
|
+
for (const id of wave) remaining.delete(id);
|
|
58
|
+
for (const deps of remaining.values()) for (const id of wave) deps.delete(id);
|
|
59
|
+
}
|
|
60
|
+
return waves;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function staticPrefix(pattern) {
|
|
64
|
+
const normalized = pattern.replace(/\\/g, "/").replace(/^\.\//, "");
|
|
65
|
+
const segments = normalized.split("/");
|
|
66
|
+
const fixed = [];
|
|
67
|
+
for (const segment of segments) {
|
|
68
|
+
if (GLOB_CHAR.test(segment)) return { segments: fixed, open: true };
|
|
69
|
+
fixed.push(segment);
|
|
70
|
+
}
|
|
71
|
+
return { segments: fixed, open: false };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function isSegmentPrefix(shorter, longer) {
|
|
75
|
+
return shorter.every((segment, index) => longer[index] === segment);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function patternsOverlap(a, b) {
|
|
79
|
+
const left = staticPrefix(a);
|
|
80
|
+
const right = staticPrefix(b);
|
|
81
|
+
if (!left.open && !right.open) return left.segments.join("/") === right.segments.join("/");
|
|
82
|
+
if (left.open && isSegmentPrefix(left.segments, right.segments)) return true;
|
|
83
|
+
if (right.open && isSegmentPrefix(right.segments, left.segments)) return true;
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function filesConflict(filesA = [], filesB = []) {
|
|
88
|
+
return filesA.some((a) => filesB.some((b) => patternsOverlap(a, b)));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function selectDispatchable(candidates, holders, slots) {
|
|
92
|
+
const selected = [];
|
|
93
|
+
const owned = holders.map((task) => task.files || []);
|
|
94
|
+
for (const task of candidates) {
|
|
95
|
+
if (selected.length >= slots) break;
|
|
96
|
+
const files = task.files || [];
|
|
97
|
+
if (files.length === 0 && (holders.length > 0 || selected.length > 0)) continue;
|
|
98
|
+
if (owned.some((other) => other.length === 0 || filesConflict(files, other))) continue;
|
|
99
|
+
selected.push(task);
|
|
100
|
+
owned.push(files);
|
|
101
|
+
}
|
|
102
|
+
return selected;
|
|
103
|
+
}
|