@bonesofspring/ai-rules 0.2.42 → 0.2.43
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 +44 -0
- package/bin/lib/seed-ai-local-team.js +41 -4
- package/docs/LOCAL-OVERLAY.md +3 -1
- package/docs/specs/INDEX.md +3 -1
- package/docs/specs/_templates/feature.md +6 -0
- package/package.json +1 -1
- package/presets/_shared/assets/docs-specs/_templates/feature.md +6 -0
- package/presets/_shared/core/agent-team/agent-team-orchestrator.md +21 -5
- package/presets/_shared/core/agent-team/plan-graph.md +9 -1
- package/presets/_shared/core/quality/anti-hallucination-contract.md +2 -0
- package/presets/_shared/core/quality/context-and-iteration.md +2 -1
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +1080 -4
- package/presets/claude/android-kotlin/commands/task-continue.md +1 -1
- package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/android-kotlin/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/go/commands/task-continue.md +1 -1
- package/presets/claude/go/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/go/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/ios-swift/commands/task-continue.md +1 -1
- package/presets/claude/ios-swift/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/ios-swift/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/java/commands/task-continue.md +1 -1
- package/presets/claude/java/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/java/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/mcp-ts/commands/task-continue.md +1 -1
- package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/mcp-ts/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/next/REPO_AGENTS.md +1 -1
- package/presets/claude/next/agents/openspec-author.md +26 -2
- package/presets/claude/next/agents/task-analyst.md +15 -6
- package/presets/claude/next/agents/task-router.md +20 -2
- package/presets/{cursor/next/ai-local/rules-library → claude/next/ai-local/rules}/task-levels.md +2 -0
- package/presets/claude/next/commands/task-continue.md +1 -1
- package/presets/claude/next/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/next/rules/tooling-and-review/ai-local-overlay.md +1 -1
- package/presets/claude/next/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/nuxt/agents/README.md +1 -1
- package/presets/claude/nuxt/commands/task-continue.md +1 -1
- package/presets/claude/nuxt/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/nuxt/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/php-hexagonal/commands/task-continue.md +1 -1
- package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/php-hexagonal/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/php-laravel/commands/task-continue.md +1 -1
- package/presets/claude/php-laravel/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/php-laravel/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/claude/svelte/agents/README.md +1 -1
- package/presets/claude/svelte/commands/task-continue.md +1 -1
- package/presets/claude/svelte/hooks/chain-team-phases.sh +1080 -4
- package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +21 -5
- package/presets/claude/svelte/rules/tooling-and-review/anti-hallucination-contract.md +2 -0
- package/presets/cursor/android-kotlin/commands/task-continue.md +1 -1
- package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/android-kotlin/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/go/commands/task-continue.md +1 -1
- package/presets/cursor/go/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/go/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/ios-swift/commands/task-continue.md +1 -1
- package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/ios-swift/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/java/commands/task-continue.md +1 -1
- package/presets/cursor/java/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/java/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/mcp-ts/commands/task-continue.md +1 -1
- package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/mcp-ts/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/next/REPO_AGENTS.md +1 -1
- package/presets/cursor/next/agents/README.md +1 -1
- package/presets/cursor/next/agents/build-verifier.md +2 -0
- package/presets/cursor/next/agents/feature-developer.md +9 -0
- package/presets/cursor/next/agents/openspec-author.md +26 -2
- package/presets/cursor/next/agents/task-analyst.md +20 -6
- package/presets/cursor/next/agents/task-router.md +31 -2
- package/presets/cursor/next/ai-local/conventions.md +20 -0
- package/presets/{mavis/next/ai-local/rules-library → cursor/next/ai-local/rules}/task-levels.md +12 -0
- package/presets/cursor/next/commands/task-continue.md +1 -1
- package/presets/cursor/next/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/next/rules/agent-team-orchestrator.mdc +18 -2
- package/presets/cursor/next/rules/ai-local-overlay.mdc +1 -1
- package/presets/cursor/next/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/nuxt/agents/README.md +1 -1
- package/presets/cursor/nuxt/commands/task-continue.md +1 -1
- package/presets/cursor/nuxt/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/nuxt/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/php-hexagonal/commands/task-continue.md +1 -1
- package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/php-hexagonal/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/php-laravel/commands/task-continue.md +1 -1
- package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/php-laravel/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/cursor/svelte/agents/README.md +1 -1
- package/presets/cursor/svelte/commands/task-continue.md +1 -1
- package/presets/cursor/svelte/hooks/chain-team-phases.sh +1080 -4
- package/presets/cursor/svelte/rules/anti-hallucination-contract.mdc +2 -0
- package/presets/mavis/android-kotlin/adapter-map.json +1 -1
- package/presets/mavis/android-kotlin/commands/task-continue.md +1 -1
- package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/android-kotlin/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/go/adapter-map.json +1 -1
- package/presets/mavis/go/commands/task-continue.md +1 -1
- package/presets/mavis/go/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/go/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/go/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/ios-swift/adapter-map.json +1 -1
- package/presets/mavis/ios-swift/commands/task-continue.md +1 -1
- package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/ios-swift/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/java/adapter-map.json +1 -1
- package/presets/mavis/java/commands/task-continue.md +1 -1
- package/presets/mavis/java/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/java/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/java/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/mcp-ts/adapter-map.json +1 -1
- package/presets/mavis/mcp-ts/commands/task-continue.md +1 -1
- package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/mcp-ts/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/next/REPO_AGENTS.md +1 -1
- package/presets/mavis/next/adapter-map.json +5 -5
- package/presets/mavis/next/agents/build-verifier.md +2 -0
- package/presets/mavis/next/agents/feature-developer.md +9 -0
- package/presets/mavis/next/agents/openspec-author.md +26 -2
- package/presets/mavis/next/agents/task-analyst.md +20 -6
- package/presets/mavis/next/agents/task-router.md +31 -2
- package/presets/mavis/next/ai-local/conventions.md +20 -0
- package/presets/{opencode/next/ai-local/rules-library → mavis/next/ai-local/rules}/task-levels.md +12 -0
- package/presets/mavis/next/commands/task-continue.md +1 -1
- package/presets/mavis/next/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/next/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/next/rules/ai-local-overlay.md +1 -1
- package/presets/mavis/next/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/nuxt/adapter-map.json +1 -1
- package/presets/mavis/nuxt/commands/task-continue.md +1 -1
- package/presets/mavis/nuxt/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/nuxt/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
- package/presets/mavis/php-hexagonal/commands/task-continue.md +1 -1
- package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/php-hexagonal/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/php-laravel/adapter-map.json +1 -1
- package/presets/mavis/php-laravel/commands/task-continue.md +1 -1
- package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/php-laravel/rules/anti-hallucination-contract.md +2 -0
- package/presets/mavis/svelte/adapter-map.json +1 -1
- package/presets/mavis/svelte/commands/task-continue.md +1 -1
- package/presets/mavis/svelte/hooks/chain-team-phases.sh +1080 -4
- package/presets/mavis/svelte/rules/agent-team-orchestrator.md +21 -5
- package/presets/mavis/svelte/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/android-kotlin/adapter-map.json +1 -1
- package/presets/opencode/android-kotlin/commands/task-continue.md +1 -1
- package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/android-kotlin/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/go/adapter-map.json +1 -1
- package/presets/opencode/go/commands/task-continue.md +1 -1
- package/presets/opencode/go/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/go/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/go/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/ios-swift/adapter-map.json +1 -1
- package/presets/opencode/ios-swift/commands/task-continue.md +1 -1
- package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/ios-swift/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/java/adapter-map.json +1 -1
- package/presets/opencode/java/commands/task-continue.md +1 -1
- package/presets/opencode/java/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/java/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/java/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/mcp-ts/adapter-map.json +1 -1
- package/presets/opencode/mcp-ts/commands/task-continue.md +1 -1
- package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/mcp-ts/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/next/REPO_AGENTS.md +1 -1
- package/presets/opencode/next/adapter-map.json +5 -5
- package/presets/opencode/next/agents/build-verifier.md +2 -0
- package/presets/opencode/next/agents/feature-developer.md +9 -0
- package/presets/opencode/next/agents/openspec-author.md +26 -2
- package/presets/opencode/next/agents/task-analyst.md +20 -6
- package/presets/opencode/next/agents/task-router.md +31 -2
- package/presets/opencode/next/ai-local/conventions.md +20 -0
- package/presets/opencode/next/ai-local/rules/task-levels.md +40 -0
- package/presets/opencode/next/commands/task-continue.md +1 -1
- package/presets/opencode/next/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/next/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/next/rules/ai-local-overlay.md +1 -1
- package/presets/opencode/next/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/nuxt/adapter-map.json +1 -1
- package/presets/opencode/nuxt/commands/task-continue.md +1 -1
- package/presets/opencode/nuxt/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/nuxt/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
- package/presets/opencode/php-hexagonal/commands/task-continue.md +1 -1
- package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/php-hexagonal/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/php-laravel/adapter-map.json +1 -1
- package/presets/opencode/php-laravel/commands/task-continue.md +1 -1
- package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/php-laravel/rules/anti-hallucination-contract.md +2 -0
- package/presets/opencode/svelte/adapter-map.json +1 -1
- package/presets/opencode/svelte/commands/task-continue.md +1 -1
- package/presets/opencode/svelte/hooks/chain-team-phases.sh +1080 -4
- package/presets/opencode/svelte/rules/agent-team-orchestrator.md +21 -5
- package/presets/opencode/svelte/rules/anti-hallucination-contract.md +2 -0
|
@@ -272,10 +272,429 @@ function writeSkeletonManifest(manifestPath) {
|
|
|
272
272
|
}
|
|
273
273
|
}
|
|
274
274
|
|
|
275
|
+
function normalizeRepoPath(value) {
|
|
276
|
+
let text = typeof value === 'string' ? value.trim() : '';
|
|
277
|
+
text = text.replace(/\\/g, '/');
|
|
278
|
+
while (text.startsWith('./')) text = text.slice(2);
|
|
279
|
+
if (!text || text === '—' || text === '-' || text === '–') return '';
|
|
280
|
+
if (/[\s()]/.test(text)) return '';
|
|
281
|
+
return text;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function isAppPath(file) {
|
|
285
|
+
const normalized = normalizeRepoPath(file);
|
|
286
|
+
return normalized === 'app' || normalized.startsWith('app/') || normalized.startsWith('app/**') || normalized.startsWith('app/*');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function isSpecFolderPath(file) {
|
|
290
|
+
const normalized = normalizeRepoPath(file);
|
|
291
|
+
return normalized.startsWith('docs/specs/') || normalized.startsWith('docs/specs/**') || normalized.startsWith('openspec/') || normalized.startsWith('openspec/**');
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function listsBothSpecAndApp(files) {
|
|
295
|
+
let app = false;
|
|
296
|
+
let spec = false;
|
|
297
|
+
for (const file of files || []) {
|
|
298
|
+
if (isAppPath(file)) app = true;
|
|
299
|
+
if (isSpecFolderPath(file)) spec = true;
|
|
300
|
+
}
|
|
301
|
+
return app && spec;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function pathInWriteScopes(file, writeScopes) {
|
|
305
|
+
const target = normalizeRepoPath(file);
|
|
306
|
+
if (!target) return false;
|
|
307
|
+
const scopes = Array.isArray(writeScopes) ? writeScopes : [];
|
|
308
|
+
for (const scope of scopes) {
|
|
309
|
+
const normalized = normalizeRepoPath(scope);
|
|
310
|
+
if (!normalized) continue;
|
|
311
|
+
if (normalized === target) return true;
|
|
312
|
+
if (normalized.endsWith('/**')) {
|
|
313
|
+
const prefix = normalized.slice(0, -3);
|
|
314
|
+
if (target === prefix || target.startsWith(prefix + '/')) return true;
|
|
315
|
+
} else if (normalized.endsWith('/*')) {
|
|
316
|
+
const prefix = normalized.slice(0, -2);
|
|
317
|
+
const rest = target.startsWith(prefix + '/') ? target.slice(prefix.length + 1) : '';
|
|
318
|
+
if (rest && !rest.includes('/')) return true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function taskFileSet(task) {
|
|
325
|
+
const all = []
|
|
326
|
+
.concat((task && task.files) || [], (task && task.specFiles) || [], (task && task.appFiles) || [])
|
|
327
|
+
.map((file) => normalizeRepoPath(file))
|
|
328
|
+
.filter(Boolean);
|
|
329
|
+
return new Set(all);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function taskReaches(tasks, startId, targetId) {
|
|
333
|
+
const byId = new Map(tasks.map((task) => [String(task.id), task]));
|
|
334
|
+
const seen = new Set();
|
|
335
|
+
const stack = [String(startId)];
|
|
336
|
+
while (stack.length > 0) {
|
|
337
|
+
const id = stack.pop();
|
|
338
|
+
if (seen.has(id)) continue;
|
|
339
|
+
seen.add(id);
|
|
340
|
+
const task = byId.get(id);
|
|
341
|
+
const deps = task && Array.isArray(task.dependsOn) ? task.dependsOn : [];
|
|
342
|
+
for (const dep of deps) {
|
|
343
|
+
if (String(dep) === String(targetId)) return true;
|
|
344
|
+
stack.push(String(dep));
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function tasksAreOrdered(tasks, left, right) {
|
|
351
|
+
return taskReaches(tasks, left.id, right.id) || taskReaches(tasks, right.id, left.id);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function validateTaskFileLists(tasks, writeScopes) {
|
|
355
|
+
const list = Array.isArray(tasks) ? tasks : [];
|
|
356
|
+
const scopes = Array.isArray(writeScopes) ? writeScopes : [];
|
|
357
|
+
for (const task of list) {
|
|
358
|
+
const id = task && task.id ? task.id : '(unnamed)';
|
|
359
|
+
const files = (task && task.files) || [];
|
|
360
|
+
if (listsBothSpecAndApp(files)) {
|
|
361
|
+
return 'task "' + id + '" lists spec-folder paths and app/** paths in one file list; they are different lists';
|
|
362
|
+
}
|
|
363
|
+
if (scopes.length === 0) continue;
|
|
364
|
+
const combined = files.concat((task && task.specFiles) || [], (task && task.appFiles) || []);
|
|
365
|
+
for (const file of combined) {
|
|
366
|
+
if (!pathInWriteScopes(file, scopes)) {
|
|
367
|
+
const label = normalizeRepoPath(file) || file;
|
|
368
|
+
return 'task "' + id + '" file "' + label + '" is outside the implementing step writeScopes';
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
373
|
+
for (let j = i + 1; j < list.length; j += 1) {
|
|
374
|
+
if (tasksAreOrdered(list, list[i], list[j])) continue;
|
|
375
|
+
const left = taskFileSet(list[i]);
|
|
376
|
+
let shared = '';
|
|
377
|
+
for (const file of taskFileSet(list[j])) {
|
|
378
|
+
if (left.has(file)) {
|
|
379
|
+
shared = file;
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (shared) return 'parallel tasks "' + list[i].id + '" and "' + list[j].id + '" share "' + shared + '"';
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function productionPathsOutsideTaskList(paths, tasks) {
|
|
390
|
+
const allowed = new Set();
|
|
391
|
+
for (const task of tasks || []) {
|
|
392
|
+
for (const file of taskFileSet(task)) allowed.add(file);
|
|
393
|
+
}
|
|
394
|
+
const outside = [];
|
|
395
|
+
for (const cited of paths || []) {
|
|
396
|
+
const normalized = normalizeRepoPath(cited);
|
|
397
|
+
if (normalized && !allowed.has(normalized)) outside.push(normalized);
|
|
398
|
+
}
|
|
399
|
+
return outside;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function splitMarkdownCells(line) {
|
|
403
|
+
const trimmed = String(line || '').trim();
|
|
404
|
+
if (!trimmed.startsWith('|')) return [];
|
|
405
|
+
const cells = trimmed.split('|').map((cell) => cell.trim());
|
|
406
|
+
if (cells.length && cells[0] === '') cells.shift();
|
|
407
|
+
if (cells.length && cells[cells.length - 1] === '') cells.pop();
|
|
408
|
+
return cells;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function isSeparatorRow(cells) {
|
|
412
|
+
return cells.length > 0 && cells.every((cell) => /^:?-{3,}:?$/.test(cell));
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function pathsFromCell(cell) {
|
|
416
|
+
const text = String(cell || '');
|
|
417
|
+
const found = [];
|
|
418
|
+
const pattern = /`([^`]+)`/g;
|
|
419
|
+
let match = pattern.exec(text);
|
|
420
|
+
while (match) {
|
|
421
|
+
const normalized = normalizeRepoPath(match[1]);
|
|
422
|
+
if (normalized && normalized.includes('/')) found.push(normalized);
|
|
423
|
+
match = pattern.exec(text);
|
|
424
|
+
}
|
|
425
|
+
if (found.length > 0) return found;
|
|
426
|
+
return text.split(',').map((part) => normalizeRepoPath(part)).filter((part) => part.includes('/'));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function dependsFromCell(cell) {
|
|
430
|
+
const cleaned = String(cell || '').replace(/[`*]/g, '').trim();
|
|
431
|
+
if (!cleaned || cleaned === '—' || cleaned === '-' || cleaned === '–') return [];
|
|
432
|
+
return cleaned.split(/[,|/]/).map((part) => part.trim()).filter((part) => /^[A-Za-z][A-Za-z0-9._-]*$/.test(part));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function cellIsComplete(cell) {
|
|
436
|
+
const cleaned = String(cell || '').replace(/[`*[\]]/g, '').trim().toLowerCase();
|
|
437
|
+
return cleaned === 'complete' || cleaned === 'completed' || cleaned === 'done' || cleaned === 'x';
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function absorbMarkdownTable(block, byId) {
|
|
441
|
+
const rows = [];
|
|
442
|
+
for (const line of block) {
|
|
443
|
+
const cells = splitMarkdownCells(line);
|
|
444
|
+
if (cells.length === 0 || isSeparatorRow(cells)) continue;
|
|
445
|
+
rows.push(cells);
|
|
446
|
+
}
|
|
447
|
+
if (rows.length < 2) return false;
|
|
448
|
+
const headers = rows[0].map((cell) => cell.replace(/[`*]/g, '').trim());
|
|
449
|
+
const idIndex = headers.findIndex((header) => /^id$/i.test(header));
|
|
450
|
+
if (idIndex < 0) return false;
|
|
451
|
+
const dependsIndex = headers.findIndex((header) => /dependson/i.test(header));
|
|
452
|
+
const specIndex = headers.findIndex((header) => /spec files/i.test(header));
|
|
453
|
+
const appIndex = headers.findIndex((header) => /^app files$/i.test(header));
|
|
454
|
+
const filesIndex = headers.findIndex((header, index) => index !== specIndex && index !== appIndex && /files|paths/i.test(header));
|
|
455
|
+
const statusIndex = headers.findIndex((header) => /^(status|done|state)$/i.test(header));
|
|
456
|
+
const sawFiles = specIndex >= 0 || appIndex >= 0 || filesIndex >= 0;
|
|
457
|
+
for (const cells of rows.slice(1)) {
|
|
458
|
+
const id = String(cells[idIndex] || '').replace(/[`*]/g, '').trim();
|
|
459
|
+
if (!id || /^id$/i.test(id)) continue;
|
|
460
|
+
const task = byId.get(id) || { id, dependsOn: [], files: [], specFiles: [], appFiles: [], tracked: false, rowComplete: false };
|
|
461
|
+
if (dependsIndex >= 0) {
|
|
462
|
+
task.dependsOn = dependsFromCell(cells[dependsIndex] || '');
|
|
463
|
+
task.tracked = true;
|
|
464
|
+
}
|
|
465
|
+
if (statusIndex >= 0) task.rowComplete = cellIsComplete(cells[statusIndex] || '');
|
|
466
|
+
if (filesIndex >= 0) {
|
|
467
|
+
const parsed = pathsFromCell(cells[filesIndex] || '');
|
|
468
|
+
if (parsed.length > 0) task.files = parsed;
|
|
469
|
+
}
|
|
470
|
+
if (specIndex >= 0) {
|
|
471
|
+
const parsed = pathsFromCell(cells[specIndex] || '');
|
|
472
|
+
if (parsed.length > 0) task.specFiles = parsed;
|
|
473
|
+
}
|
|
474
|
+
if (appIndex >= 0) {
|
|
475
|
+
const parsed = pathsFromCell(cells[appIndex] || '');
|
|
476
|
+
if (parsed.length > 0) task.appFiles = parsed;
|
|
477
|
+
}
|
|
478
|
+
byId.set(id, task);
|
|
479
|
+
}
|
|
480
|
+
return sawFiles;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function parseDecompositionTaskFileLists(markdown) {
|
|
484
|
+
const lines = String(markdown || '').split(/\r?\n/);
|
|
485
|
+
const byId = new Map();
|
|
486
|
+
let hasFileColumn = false;
|
|
487
|
+
let index = 0;
|
|
488
|
+
while (index < lines.length) {
|
|
489
|
+
if (!String(lines[index] || '').trim().startsWith('|')) {
|
|
490
|
+
index += 1;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
const block = [];
|
|
494
|
+
while (index < lines.length && String(lines[index] || '').trim().startsWith('|')) {
|
|
495
|
+
block.push(lines[index]);
|
|
496
|
+
index += 1;
|
|
497
|
+
}
|
|
498
|
+
if (absorbMarkdownTable(block, byId)) hasFileColumn = true;
|
|
499
|
+
}
|
|
500
|
+
return { tasks: [...byId.values()], hasFileColumn };
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function parseDecompositionTasks(markdown) {
|
|
504
|
+
const lines = String(markdown || '').split(/\r?\n/);
|
|
505
|
+
const byId = new Map();
|
|
506
|
+
let index = 0;
|
|
507
|
+
while (index < lines.length) {
|
|
508
|
+
if (!String(lines[index] || '').trim().startsWith('|')) {
|
|
509
|
+
index += 1;
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
const block = [];
|
|
513
|
+
while (index < lines.length && String(lines[index] || '').trim().startsWith('|')) {
|
|
514
|
+
block.push(lines[index]);
|
|
515
|
+
index += 1;
|
|
516
|
+
}
|
|
517
|
+
absorbMarkdownTable(block, byId);
|
|
518
|
+
}
|
|
519
|
+
return [...byId.values()].filter((task) => task.tracked === true);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function taskCoveredByCompletedArtifact(taskId, artifacts) {
|
|
523
|
+
const id = String(taskId);
|
|
524
|
+
const list = Array.isArray(artifacts) ? artifacts : [];
|
|
525
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
526
|
+
const artifact = list[index];
|
|
527
|
+
if (!artifact || artifact.status !== 'completed') continue;
|
|
528
|
+
if (String(artifact.id) === id) return true;
|
|
529
|
+
const taskIds = Array.isArray(artifact.taskIds) ? artifact.taskIds : [];
|
|
530
|
+
for (let taskIndex = 0; taskIndex < taskIds.length; taskIndex += 1) {
|
|
531
|
+
if (String(taskIds[taskIndex]) === id) return true;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function computeDecompositionReadySet(tasks, artifacts) {
|
|
538
|
+
const list = (Array.isArray(tasks) ? tasks : []).filter((task) => task && task.id && task.tracked !== false);
|
|
539
|
+
const complete = new Set();
|
|
540
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
541
|
+
const task = list[index];
|
|
542
|
+
if (task.rowComplete === true || taskCoveredByCompletedArtifact(task.id, artifacts)) complete.add(String(task.id));
|
|
543
|
+
}
|
|
544
|
+
const ready = [];
|
|
545
|
+
const blocked = [];
|
|
546
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
547
|
+
const task = list[index];
|
|
548
|
+
const id = String(task.id);
|
|
549
|
+
if (complete.has(id)) continue;
|
|
550
|
+
const deps = Array.isArray(task.dependsOn) ? task.dependsOn.map((dep) => String(dep)) : [];
|
|
551
|
+
const waiting = [];
|
|
552
|
+
const seen = new Set();
|
|
553
|
+
for (let depIndex = 0; depIndex < deps.length; depIndex += 1) {
|
|
554
|
+
const dep = deps[depIndex];
|
|
555
|
+
if (!dep || seen.has(dep) || complete.has(dep)) continue;
|
|
556
|
+
seen.add(dep);
|
|
557
|
+
waiting.push(dep);
|
|
558
|
+
}
|
|
559
|
+
if (waiting.length === 0) ready.push(id);
|
|
560
|
+
else blocked.push({ id, waiting_on: waiting });
|
|
561
|
+
}
|
|
562
|
+
return { ready, blocked };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function readManifestArtifacts(dir) {
|
|
566
|
+
try {
|
|
567
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'artifact-manifest.json'), 'utf8'));
|
|
568
|
+
return Array.isArray(manifest.artifacts) ? manifest.artifacts : [];
|
|
569
|
+
} catch {
|
|
570
|
+
return [];
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function readDecompositionReadySet(dir) {
|
|
575
|
+
let markdown = '';
|
|
576
|
+
try {
|
|
577
|
+
markdown = fs.readFileSync(path.join(dir, 'decomposition.md'), 'utf8');
|
|
578
|
+
} catch {
|
|
579
|
+
markdown = '';
|
|
580
|
+
}
|
|
581
|
+
return computeDecompositionReadySet(parseDecompositionTasks(markdown), readManifestArtifacts(dir));
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function formatDecompositionReadyClause(report, stepsForRows) {
|
|
585
|
+
const source = report && typeof report === 'object' ? report : { ready: [], blocked: [] };
|
|
586
|
+
const stepList = Array.isArray(stepsForRows) ? stepsForRows : [];
|
|
587
|
+
const allow = [];
|
|
588
|
+
for (let index = 0; index < stepList.length; index += 1) {
|
|
589
|
+
const taskIds = stepList[index] && stepList[index].taskIds;
|
|
590
|
+
if (!Array.isArray(taskIds)) continue;
|
|
591
|
+
for (let idIndex = 0; idIndex < taskIds.length; idIndex += 1) {
|
|
592
|
+
if (taskIds[idIndex] != null && String(taskIds[idIndex])) allow.push(String(taskIds[idIndex]));
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
const gate = allow.length > 0 ? new Set(allow) : null;
|
|
596
|
+
const ready = (Array.isArray(source.ready) ? source.ready : []).filter((id) => !gate || gate.has(String(id)));
|
|
597
|
+
const blocked = (Array.isArray(source.blocked) ? source.blocked : []).filter((row) => row && (!gate || gate.has(String(row.id))));
|
|
598
|
+
const readyText = ready.length > 0 ? ready.join(', ') : '(none)';
|
|
599
|
+
let text = ' Decomposition ready set: ' + readyText + '.';
|
|
600
|
+
if (blocked.length > 0) {
|
|
601
|
+
const parts = [];
|
|
602
|
+
for (let index = 0; index < blocked.length; index += 1) {
|
|
603
|
+
const row = blocked[index];
|
|
604
|
+
const waiting = Array.isArray(row.waiting_on) ? row.waiting_on.join(', ') : '';
|
|
605
|
+
parts.push(String(row.id) + ' waiting_on ' + waiting);
|
|
606
|
+
}
|
|
607
|
+
text += ' Blocked: ' + parts.join('; ') + '.';
|
|
608
|
+
}
|
|
609
|
+
return text;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
function decompositionReadyClause(stepsForRows) {
|
|
613
|
+
try {
|
|
614
|
+
return formatDecompositionReadyClause(readDecompositionReadySet(taskDir), stepsForRows);
|
|
615
|
+
} catch {
|
|
616
|
+
return ' Decomposition ready set: (none).';
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function productionCitationRepoPath(cited) {
|
|
621
|
+
const value = typeof cited === 'string' ? cited.trim() : '';
|
|
622
|
+
if (!value) return null;
|
|
623
|
+
const normalized = value.replace(/\\/g, '/');
|
|
624
|
+
if (path.isAbsolute(normalized)) {
|
|
625
|
+
const fromCwd = path.relative(process.cwd(), normalized);
|
|
626
|
+
if (fromCwd && !fromCwd.startsWith('..') && !path.isAbsolute(fromCwd)) return normalizeRepoPath(fromCwd);
|
|
627
|
+
return normalizeRepoPath(normalized) || normalized;
|
|
628
|
+
}
|
|
629
|
+
const taskResolved = path.resolve(taskDir, normalized);
|
|
630
|
+
const taskRelative = path.relative(taskDir, taskResolved);
|
|
631
|
+
const insideTask = taskRelative !== '' && taskRelative !== '..' && !taskRelative.startsWith('..' + path.sep) && !path.isAbsolute(taskRelative);
|
|
632
|
+
if (insideTask) {
|
|
633
|
+
try {
|
|
634
|
+
if (fs.statSync(taskResolved).isFile()) return null;
|
|
635
|
+
} catch {}
|
|
636
|
+
}
|
|
637
|
+
return normalizeRepoPath(normalized);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function implementingStepForTaskIds(taskIds) {
|
|
641
|
+
if (!Array.isArray(steps) || taskIds.length === 0) return null;
|
|
642
|
+
return steps.find((step) => Array.isArray(step && step.taskIds) && taskIds.some((id) => step.taskIds.map((item) => String(item)).includes(String(id)))) || null;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function productionWriteListError(entry) {
|
|
646
|
+
const artifactId = entry && entry.id ? entry.id : '(unnamed)';
|
|
647
|
+
const resume = 'Then resume with: /task-continue ' + slug;
|
|
648
|
+
const fail = (detail) => '[chain-team-phases] ' + slug + ': receipt "' + artifactId + '" ' + detail + ' ' + resume;
|
|
649
|
+
try {
|
|
650
|
+
const evidence = entry && Array.isArray(entry.evidencePaths) ? entry.evidencePaths : [];
|
|
651
|
+
const production = [];
|
|
652
|
+
for (const cited of evidence) {
|
|
653
|
+
const repoPath = productionCitationRepoPath(cited);
|
|
654
|
+
if (repoPath) production.push(repoPath);
|
|
655
|
+
}
|
|
656
|
+
if (production.length === 0) return null;
|
|
657
|
+
let markdown = '';
|
|
658
|
+
try {
|
|
659
|
+
markdown = fs.readFileSync(path.join(taskDir, 'decomposition.md'), 'utf8');
|
|
660
|
+
} catch {
|
|
661
|
+
markdown = '';
|
|
662
|
+
}
|
|
663
|
+
const parsed = parseDecompositionTaskFileLists(markdown);
|
|
664
|
+
if (!parsed.hasFileColumn) return null;
|
|
665
|
+
const tasks = parsed.tasks;
|
|
666
|
+
const taskIds = Array.isArray(entry && entry.taskIds) ? entry.taskIds.map((id) => String(id)) : [];
|
|
667
|
+
const step = implementingStepForTaskIds(taskIds);
|
|
668
|
+
const selectedIds = new Set(taskIds);
|
|
669
|
+
if (step && Array.isArray(step.taskIds)) {
|
|
670
|
+
for (const id of step.taskIds) selectedIds.add(String(id));
|
|
671
|
+
}
|
|
672
|
+
const selected = tasks.filter((task) => selectedIds.has(String(task.id)));
|
|
673
|
+
if (selected.length === 0) {
|
|
674
|
+
return fail('cites production paths but decomposition.md has no file list for ' + (taskIds.join(', ') || '(none)') + '. Record the task file list.');
|
|
675
|
+
}
|
|
676
|
+
const writeScopes = step && Array.isArray(step.writeScopes) ? step.writeScopes : [];
|
|
677
|
+
const listError = validateTaskFileLists(selected, writeScopes);
|
|
678
|
+
if (listError) return fail(listError + '.');
|
|
679
|
+
const owned = selected.filter((task) => taskIds.includes(String(task.id)));
|
|
680
|
+
const citedAgainst = owned.length > 0 ? owned : selected;
|
|
681
|
+
const outside = productionPathsOutsideTaskList(production, citedAgainst);
|
|
682
|
+
if (outside.length > 0) {
|
|
683
|
+
const ids = taskIds.length > 0 ? taskIds.join(', ') : citedAgainst.map((task) => task.id).join(', ');
|
|
684
|
+
return fail('cites production path "' + outside[0] + '", which is not on the decomposition file list for ' + ids + '.');
|
|
685
|
+
}
|
|
686
|
+
return null;
|
|
687
|
+
} catch (error) {
|
|
688
|
+
return fail('production file-list check failed (' + (error && error.message ? error.message : error) + ').');
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
275
692
|
// A2: a `completed` receipt must cite evidence the harness can inspect. Order is
|
|
276
693
|
// the message order — the first failing predicate wins, so the operator sees one
|
|
277
694
|
// actionable line. `authoritativePaths[]` stays task-dir-relative; external
|
|
278
|
-
// references belong in `evidencePaths[]`.
|
|
695
|
+
// references belong in `evidencePaths[]`. The task-folder checks below stay in
|
|
696
|
+
// place. A later production evidencePaths check uses the decomposition file list
|
|
697
|
+
// only when that list column exists.
|
|
279
698
|
function validateCompletedArtifact(entry) {
|
|
280
699
|
const artifactId = entry && entry.id ? entry.id : '(unnamed)';
|
|
281
700
|
const entryStatus = entry && entry.status;
|
|
@@ -307,6 +726,8 @@ function validateCompletedArtifact(entry) {
|
|
|
307
726
|
}
|
|
308
727
|
}
|
|
309
728
|
}
|
|
729
|
+
const productionListError = productionWriteListError(entry);
|
|
730
|
+
if (productionListError) return productionListError;
|
|
310
731
|
return null;
|
|
311
732
|
}
|
|
312
733
|
|
|
@@ -476,6 +897,309 @@ function recordSkippedStep(step, stepIndex) {
|
|
|
476
897
|
}
|
|
477
898
|
}
|
|
478
899
|
|
|
900
|
+
// Living task files and the hook ledger stay put. A completed entry's other markdown is the body that leaves the default read.
|
|
901
|
+
const CLOSED_BODY_KEEP = new Set([
|
|
902
|
+
'brief.md',
|
|
903
|
+
'decomposition.md',
|
|
904
|
+
'goal.md',
|
|
905
|
+
'prompt.md',
|
|
906
|
+
'prompt-log.md',
|
|
907
|
+
'pipeline.json',
|
|
908
|
+
'implementation.md',
|
|
909
|
+
'.memlog.md',
|
|
910
|
+
'focus.md',
|
|
911
|
+
'status.json',
|
|
912
|
+
'metrics.json',
|
|
913
|
+
'artifact-manifest.json',
|
|
914
|
+
'overlay-scope.json',
|
|
915
|
+
]);
|
|
916
|
+
|
|
917
|
+
function closedMarkdownBody(dir, cited) {
|
|
918
|
+
if (typeof cited !== 'string') return null;
|
|
919
|
+
const value = cited.trim();
|
|
920
|
+
if (!value || path.isAbsolute(value)) return null;
|
|
921
|
+
const resolved = path.resolve(dir, value);
|
|
922
|
+
const relative = path.relative(dir, resolved);
|
|
923
|
+
if (!relative || relative === '..' || relative.startsWith('..' + path.sep) || path.isAbsolute(relative)) return null;
|
|
924
|
+
const normalized = relative.split(path.sep).join('/');
|
|
925
|
+
if (!/\.md$/i.test(normalized) || normalized.startsWith('archive/')) return null;
|
|
926
|
+
const base = normalized.slice(normalized.lastIndexOf('/') + 1);
|
|
927
|
+
if (CLOSED_BODY_KEEP.has(base) || CLOSED_BODY_KEEP.has(normalized)) return null;
|
|
928
|
+
return { resolved, base };
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function archiveClosedBody(dir, date, id, body) {
|
|
932
|
+
const archiveDir = path.join(dir, 'archive', date);
|
|
933
|
+
fs.mkdirSync(archiveDir, { recursive: true });
|
|
934
|
+
let name = body.base;
|
|
935
|
+
let destination = path.join(archiveDir, name);
|
|
936
|
+
if (fs.existsSync(destination)) {
|
|
937
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(id)) return null;
|
|
938
|
+
name = id + '--' + body.base;
|
|
939
|
+
destination = path.join(archiveDir, name);
|
|
940
|
+
}
|
|
941
|
+
if (fs.existsSync(destination)) return null;
|
|
942
|
+
fs.renameSync(body.resolved, destination);
|
|
943
|
+
return 'archive/' + date + '/' + name;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
function appendMemlogEvent(dir, text) {
|
|
947
|
+
const memlogPath = path.join(dir, '.memlog.md');
|
|
948
|
+
const line = '- `event`: ' + text + '\n';
|
|
949
|
+
let existing = '';
|
|
950
|
+
try { existing = fs.readFileSync(memlogPath, 'utf8'); } catch { existing = ''; }
|
|
951
|
+
if (existing.includes(text)) return existing;
|
|
952
|
+
const prefix = existing.length > 0 && !existing.endsWith('\n') ? '\n' : '';
|
|
953
|
+
fs.appendFileSync(memlogPath, prefix + line);
|
|
954
|
+
return existing + prefix + line;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
function writeJsonFile(filePath, value) {
|
|
958
|
+
const temporary = filePath + '.' + String(process.pid) + '.tmp';
|
|
959
|
+
const fd = fs.openSync(temporary, 'w');
|
|
960
|
+
try {
|
|
961
|
+
fs.writeSync(fd, JSON.stringify(value, null, 2) + '\n');
|
|
962
|
+
} finally {
|
|
963
|
+
fs.closeSync(fd);
|
|
964
|
+
}
|
|
965
|
+
fs.renameSync(temporary, filePath);
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
function compactClosedManifestBodies(dir, closedOn) {
|
|
969
|
+
const date = typeof closedOn === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(closedOn)
|
|
970
|
+
? closedOn
|
|
971
|
+
: new Date().toISOString().slice(0, 10);
|
|
972
|
+
const manifestPath = path.join(dir, 'artifact-manifest.json');
|
|
973
|
+
let manifest;
|
|
974
|
+
try {
|
|
975
|
+
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
976
|
+
} catch {
|
|
977
|
+
return { moved: [] };
|
|
978
|
+
}
|
|
979
|
+
const slugBefore = manifest.slug;
|
|
980
|
+
const artifacts = Array.isArray(manifest.artifacts) ? manifest.artifacts : [];
|
|
981
|
+
let memlog = '';
|
|
982
|
+
try { memlog = fs.readFileSync(path.join(dir, '.memlog.md'), 'utf8'); } catch { memlog = ''; }
|
|
983
|
+
let manifestChanged = false;
|
|
984
|
+
const moved = [];
|
|
985
|
+
for (let index = 0; index < artifacts.length; index += 1) {
|
|
986
|
+
const entry = artifacts[index];
|
|
987
|
+
if (!entry || entry.status !== 'completed' || typeof entry.id !== 'string' || !entry.id) continue;
|
|
988
|
+
const marker = 'closed manifest artifact ' + entry.id + ';';
|
|
989
|
+
if (memlog.includes(marker)) continue;
|
|
990
|
+
const cited = Array.isArray(entry.authoritativePaths) ? entry.authoritativePaths.slice() : [];
|
|
991
|
+
const relocated = [];
|
|
992
|
+
for (let pathIndex = 0; pathIndex < cited.length; pathIndex += 1) {
|
|
993
|
+
const body = closedMarkdownBody(dir, cited[pathIndex]);
|
|
994
|
+
if (!body) continue;
|
|
995
|
+
let stat = null;
|
|
996
|
+
try { stat = fs.statSync(body.resolved); } catch { stat = null; }
|
|
997
|
+
if (!stat || !stat.isFile()) continue;
|
|
998
|
+
let content = '';
|
|
999
|
+
try { content = fs.readFileSync(body.resolved, 'utf8'); } catch { content = ''; }
|
|
1000
|
+
if (!/\S/.test(content)) continue;
|
|
1001
|
+
const archived = archiveClosedBody(dir, date, entry.id, body);
|
|
1002
|
+
if (!archived) continue;
|
|
1003
|
+
cited[pathIndex] = archived;
|
|
1004
|
+
relocated.push(archived);
|
|
1005
|
+
moved.push(archived);
|
|
1006
|
+
manifestChanged = true;
|
|
1007
|
+
}
|
|
1008
|
+
if (relocated.length > 0) entry.authoritativePaths = cited;
|
|
1009
|
+
let archiveHint = relocated.length > 0 ? 'archive/' + date + '/' : '';
|
|
1010
|
+
if (!archiveHint) {
|
|
1011
|
+
for (let pathIndex = 0; pathIndex < cited.length; pathIndex += 1) {
|
|
1012
|
+
const value = typeof cited[pathIndex] === 'string' ? cited[pathIndex].trim() : '';
|
|
1013
|
+
const match = /^archive\/\d{4}-\d{2}-\d{2}\//.exec(value);
|
|
1014
|
+
if (match) {
|
|
1015
|
+
archiveHint = match[0];
|
|
1016
|
+
break;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
const text = archiveHint
|
|
1021
|
+
? 'closed manifest artifact ' + entry.id + '; body archived under ' + archiveHint
|
|
1022
|
+
: 'closed manifest artifact ' + entry.id + '; no bulky markdown body moved';
|
|
1023
|
+
memlog = appendMemlogEvent(dir, text);
|
|
1024
|
+
}
|
|
1025
|
+
if (manifest.slug !== slugBefore) manifest.slug = slugBefore;
|
|
1026
|
+
if (manifestChanged) writeJsonFile(manifestPath, manifest);
|
|
1027
|
+
return { moved };
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
function projectResumeArtifact(entry) {
|
|
1031
|
+
if (!entry || typeof entry !== 'object') return entry;
|
|
1032
|
+
if (entry.status !== 'completed') return entry;
|
|
1033
|
+
return {
|
|
1034
|
+
id: entry.id,
|
|
1035
|
+
status: entry.status,
|
|
1036
|
+
authoritativePaths: Array.isArray(entry.authoritativePaths) ? entry.authoritativePaths : [],
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
function defaultResumeRead(dir) {
|
|
1041
|
+
const chunks = [];
|
|
1042
|
+
try { chunks.push(fs.readFileSync(path.join(dir, 'focus.md'), 'utf8')); } catch {}
|
|
1043
|
+
try {
|
|
1044
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'artifact-manifest.json'), 'utf8'));
|
|
1045
|
+
const artifacts = Array.isArray(manifest.artifacts) ? manifest.artifacts : [];
|
|
1046
|
+
const projected = [];
|
|
1047
|
+
for (let index = 0; index < artifacts.length; index += 1) {
|
|
1048
|
+
projected.push(projectResumeArtifact(artifacts[index]));
|
|
1049
|
+
}
|
|
1050
|
+
chunks.push(JSON.stringify({ slug: manifest.slug, artifacts: projected }));
|
|
1051
|
+
} catch {}
|
|
1052
|
+
try { chunks.push(fs.readFileSync(path.join(dir, '.memlog.md'), 'utf8')); } catch {}
|
|
1053
|
+
return chunks.join('\n');
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
function archiveIndexLine(dir) {
|
|
1057
|
+
const root = path.join(dir, 'archive');
|
|
1058
|
+
let names = [];
|
|
1059
|
+
try { names = fs.readdirSync(root); } catch { names = []; }
|
|
1060
|
+
const dates = [];
|
|
1061
|
+
for (let index = 0; index < names.length; index += 1) {
|
|
1062
|
+
const name = names[index];
|
|
1063
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(name)) continue;
|
|
1064
|
+
let stat = null;
|
|
1065
|
+
try { stat = fs.statSync(path.join(root, name)); } catch { stat = null; }
|
|
1066
|
+
if (stat && stat.isDirectory()) dates.push(name);
|
|
1067
|
+
}
|
|
1068
|
+
dates.sort();
|
|
1069
|
+
if (dates.length === 0) return '- archive index: (none)';
|
|
1070
|
+
const parts = [];
|
|
1071
|
+
for (let index = 0; index < dates.length; index += 1) parts.push('archive/' + dates[index] + '/');
|
|
1072
|
+
return '- archive index: ' + parts.join(', ');
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
function focusDigestMarkdown(dir, statusSnapshot) {
|
|
1076
|
+
const snapshot = statusSnapshot && typeof statusSnapshot === 'object' ? statusSnapshot : {};
|
|
1077
|
+
const slugValue = typeof snapshot.slug === 'string' ? snapshot.slug : '';
|
|
1078
|
+
const stepIndex = Number.isInteger(snapshot.pipelineIndex) ? snapshot.pipelineIndex : 0;
|
|
1079
|
+
const incomplete = [];
|
|
1080
|
+
try {
|
|
1081
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'artifact-manifest.json'), 'utf8'));
|
|
1082
|
+
const artifacts = Array.isArray(manifest.artifacts) ? manifest.artifacts : [];
|
|
1083
|
+
for (let index = 0; index < artifacts.length; index += 1) {
|
|
1084
|
+
const artifact = artifacts[index];
|
|
1085
|
+
if (!artifact || artifact.status === 'completed') continue;
|
|
1086
|
+
const id = typeof artifact.id === 'string' ? artifact.id : '(no id)';
|
|
1087
|
+
const cited = Array.isArray(artifact.authoritativePaths) ? artifact.authoritativePaths : [];
|
|
1088
|
+
const paths = [];
|
|
1089
|
+
for (let pathIndex = 0; pathIndex < cited.length; pathIndex += 1) {
|
|
1090
|
+
if (typeof cited[pathIndex] === 'string') paths.push(cited[pathIndex]);
|
|
1091
|
+
}
|
|
1092
|
+
incomplete.push({ id, paths });
|
|
1093
|
+
}
|
|
1094
|
+
} catch {}
|
|
1095
|
+
let lastEvent = '(none)';
|
|
1096
|
+
try {
|
|
1097
|
+
const raw = fs.readFileSync(path.join(dir, '.memlog.md'), 'utf8');
|
|
1098
|
+
const lines = raw.split(/\r?\n/);
|
|
1099
|
+
const tickEvent = /`event`:\s*(.*)$/;
|
|
1100
|
+
const bareEvent = /(?:^|\s)event:\s*(.*)$/;
|
|
1101
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
1102
|
+
const line = lines[index];
|
|
1103
|
+
let matched = tickEvent.exec(line);
|
|
1104
|
+
if (!matched) matched = bareEvent.exec(line);
|
|
1105
|
+
if (matched && matched[1].trim()) {
|
|
1106
|
+
lastEvent = matched[1].trim();
|
|
1107
|
+
break;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
} catch {}
|
|
1111
|
+
let nextGate = 'none';
|
|
1112
|
+
if (snapshot.awaitingHumanGate === true || snapshot.state === 'awaiting_approval') {
|
|
1113
|
+
nextGate = 'human gate — /task-continue ' + slugValue;
|
|
1114
|
+
} else {
|
|
1115
|
+
try {
|
|
1116
|
+
const pipeline = JSON.parse(fs.readFileSync(path.join(dir, 'pipeline.json'), 'utf8'));
|
|
1117
|
+
const steps = Array.isArray(pipeline.steps) ? pipeline.steps : [];
|
|
1118
|
+
const gates = Array.isArray(pipeline.humanGates) ? pipeline.humanGates : [];
|
|
1119
|
+
for (let index = stepIndex; index < steps.length; index += 1) {
|
|
1120
|
+
const step = steps[index];
|
|
1121
|
+
const agents = !step ? [] : (Array.isArray(step.agent) ? step.agent : [step.agent]);
|
|
1122
|
+
let hit = '';
|
|
1123
|
+
for (let agentIndex = 0; agentIndex < agents.length; agentIndex += 1) {
|
|
1124
|
+
const agent = agents[agentIndex];
|
|
1125
|
+
if (typeof agent === 'string' && gates.indexOf('after:' + agent) !== -1) {
|
|
1126
|
+
hit = agent;
|
|
1127
|
+
break;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
if (hit) {
|
|
1131
|
+
const stepId = step && typeof step.id === 'string' ? step.id : 'step-' + String(index);
|
|
1132
|
+
nextGate = 'after:' + hit + ' (step ' + stepId + ', index ' + String(index) + ')';
|
|
1133
|
+
break;
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
} catch {}
|
|
1137
|
+
}
|
|
1138
|
+
const pathLines = [];
|
|
1139
|
+
if (incomplete.length === 0) pathLines.push(' - (none)');
|
|
1140
|
+
else {
|
|
1141
|
+
for (let index = 0; index < incomplete.length; index += 1) {
|
|
1142
|
+
const item = incomplete[index];
|
|
1143
|
+
if (item.paths.length === 0) pathLines.push(' - ' + item.id + ': (none)');
|
|
1144
|
+
else {
|
|
1145
|
+
for (let pathIndex = 0; pathIndex < item.paths.length; pathIndex += 1) {
|
|
1146
|
+
pathLines.push(' - ' + item.id + ': ' + item.paths[pathIndex]);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
let readyLine = '- decomposition ready set: (none)';
|
|
1152
|
+
try {
|
|
1153
|
+
const report = readDecompositionReadySet(dir);
|
|
1154
|
+
const ids = Array.isArray(report.ready) ? report.ready : [];
|
|
1155
|
+
readyLine = '- decomposition ready set: ' + (ids.length > 0 ? ids.join(', ') : '(none)');
|
|
1156
|
+
} catch {
|
|
1157
|
+
readyLine = '- decomposition ready set: (none)';
|
|
1158
|
+
}
|
|
1159
|
+
const head = [
|
|
1160
|
+
'# focus.md',
|
|
1161
|
+
'',
|
|
1162
|
+
'View only. `status.json`, `metrics.json`, `artifact-manifest.json`, and `.memlog.md` stay the system of record.',
|
|
1163
|
+
'',
|
|
1164
|
+
'- slug: ' + slugValue,
|
|
1165
|
+
'- pipeline step index: ' + String(stepIndex),
|
|
1166
|
+
readyLine,
|
|
1167
|
+
archiveIndexLine(dir),
|
|
1168
|
+
'- incomplete authoritative paths:',
|
|
1169
|
+
];
|
|
1170
|
+
const tail = [
|
|
1171
|
+
'- last memlog event: ' + lastEvent,
|
|
1172
|
+
'- next gate: ' + nextGate,
|
|
1173
|
+
];
|
|
1174
|
+
let pathBudget = 99 - head.length - tail.length;
|
|
1175
|
+
if (pathBudget < 1) pathBudget = 1;
|
|
1176
|
+
let kept = pathLines;
|
|
1177
|
+
if (pathLines.length > pathBudget) {
|
|
1178
|
+
const room = pathBudget - 1;
|
|
1179
|
+
kept = pathLines.slice(0, room);
|
|
1180
|
+
const omitted = pathLines.length - kept.length;
|
|
1181
|
+
kept.push(' - (' + String(omitted) + ' lines omitted to stay under 100)');
|
|
1182
|
+
}
|
|
1183
|
+
return head.concat(kept, tail).join('\n') + '\n';
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
function writeFocusDigest() {
|
|
1187
|
+
const focusPath = path.join(taskDir, 'focus.md');
|
|
1188
|
+
const temporary = focusPath + '.' + String(process.pid) + '.' + String(Date.now()) + '.tmp';
|
|
1189
|
+
try {
|
|
1190
|
+
const markdown = focusDigestMarkdown(taskDir, status);
|
|
1191
|
+
const fd = fs.openSync(temporary, 'w');
|
|
1192
|
+
try {
|
|
1193
|
+
fs.writeSync(fd, markdown);
|
|
1194
|
+
try { fs.fsyncSync(fd); } catch {}
|
|
1195
|
+
} finally {
|
|
1196
|
+
fs.closeSync(fd);
|
|
1197
|
+
}
|
|
1198
|
+
fs.renameSync(temporary, focusPath);
|
|
1199
|
+
} catch {
|
|
1200
|
+
try { fs.unlinkSync(temporary); } catch {}
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
479
1203
|
|
|
480
1204
|
function writeStatus(next) {
|
|
481
1205
|
status = { ...status, ...next, slug, updatedAt: now };
|
|
@@ -492,6 +1216,11 @@ function writeStatus(next) {
|
|
|
492
1216
|
fs.closeSync(fd);
|
|
493
1217
|
}
|
|
494
1218
|
fs.renameSync(temporary, statusPath);
|
|
1219
|
+
// Closed bodies move after the checkpoint lands. A compaction miss must not roll status.json back.
|
|
1220
|
+
try { compactClosedManifestBodies(taskDir, now.slice(0, 10)); } catch {}
|
|
1221
|
+
// focus.md is a view of this checkpoint. It is written after status.json
|
|
1222
|
+
// lands so a digest failure cannot roll the checkpoint back.
|
|
1223
|
+
writeFocusDigest();
|
|
495
1224
|
}
|
|
496
1225
|
|
|
497
1226
|
function out(message) {
|
|
@@ -654,7 +1383,8 @@ function handoffForWave(units, waveId) {
|
|
|
654
1383
|
`Invoke IN PARALLEL (one message, multiple Task calls): ${labels.join(' AND ')} for slug "${slug}"${waveId ? ` (waveId=${waveId})` : ''}. ` +
|
|
655
1384
|
`Each agent writes its artifact and owned receipt; the hook records slot joins from stops. Do not mutate status.json lifecycle fields. ` +
|
|
656
1385
|
`Artifact inputs: ${inputIds.length > 0 ? inputIds.join(', ') : 'manifest defaults'}; owned output IDs: ${outputIds.join('; ')}. ` +
|
|
657
|
-
`Metrics attemptIds: ${attemptIds.join('; ')}. If the hook cannot identify a parallel stop, append a minimal { attemptId, outcome } receipt to .metrics-receipts.json; do not calculate duration. ${receiptPathClause()}`
|
|
1386
|
+
`Metrics attemptIds: ${attemptIds.join('; ')}. If the hook cannot identify a parallel stop, append a minimal { attemptId, outcome } receipt to .metrics-receipts.json; do not calculate duration. ${receiptPathClause()}` +
|
|
1387
|
+
decompositionReadyClause(units.map((unit) => steps[unit.stepIndex]))
|
|
658
1388
|
);
|
|
659
1389
|
}
|
|
660
1390
|
function blockedNotice(blockedIndex, agent) {
|
|
@@ -690,6 +1420,9 @@ function dispatchReadyWave(prefixMessage) {
|
|
|
690
1420
|
// Slot-filtered dispatch: only unsatisfied (stepIndex, agent) slots are invoked.
|
|
691
1421
|
const units = wave.flatMap((stepIndex) => unsatisfiedAgents(stepIndex).map((agent) => ({ stepIndex, agent })));
|
|
692
1422
|
if (units.length === 0) return false;
|
|
1423
|
+
const unitAgents = units.map((unit) => unit.agent);
|
|
1424
|
+
refuseImplementationStart(unitAgents, (agent) => implementationDebugRetry(agent));
|
|
1425
|
+
recordAllowedDebugRetries(unitAgents);
|
|
693
1426
|
const first = units[0].stepIndex;
|
|
694
1427
|
const waveId = `${status.metricsRunId || slug}:wave-${first}`;
|
|
695
1428
|
for (const unit of units) activateAttempts(steps[unit.stepIndex], unit.stepIndex, units.length > 1 ? waveId : null, [unit.agent]);
|
|
@@ -837,10 +1570,165 @@ function artifactHandoff(step, agent) {
|
|
|
837
1570
|
return ` Artifact inputs: ${inputIds}; owned output ID: ${artifactOutputIdFor(step, agent)}. Bind the terminal receipt to the supplied attemptId. ${receiptPathClause()}`;
|
|
838
1571
|
}
|
|
839
1572
|
|
|
1573
|
+
// <<<implementation-rail>>>
|
|
1574
|
+
// Opt-in. Omitted, "off", and non-production intents stay off. Only "on" applies.
|
|
1575
|
+
function testBeforeHandoffRule() {
|
|
1576
|
+
return 'Include unit tests for mappers/logic before handoff.';
|
|
1577
|
+
}
|
|
1578
|
+
function railOnHandoffClause() {
|
|
1579
|
+
return ' Strict implementation rail: write a failing test before production edits; use a branch or worktree before the first implementation task; run code-reviewer between decomposition tasks; debug retries for this handoff are capped at two.';
|
|
1580
|
+
}
|
|
1581
|
+
function implementationRailApplies(pipeline) {
|
|
1582
|
+
if (!pipeline || typeof pipeline !== 'object') return false;
|
|
1583
|
+
const intent = typeof pipeline.intent === 'string' ? pipeline.intent : '';
|
|
1584
|
+
const level = pipeline.taskLevel;
|
|
1585
|
+
if (level === 'L0' || level === 0 || level === '0') return false;
|
|
1586
|
+
if (intent === 'docs-only' || intent === 'research' || intent === 'preset-only' || intent === 'spike') return false;
|
|
1587
|
+
if (pipeline.presetOnly === true) return false;
|
|
1588
|
+
return pipeline.implementationRail === 'on';
|
|
1589
|
+
}
|
|
1590
|
+
function briefHasOpenClarificationMarker(text) {
|
|
1591
|
+
if (typeof text !== 'string' || text.length === 0) return false;
|
|
1592
|
+
const lines = text.split(/\r?\n/);
|
|
1593
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
1594
|
+
let body = lines[index].trim().replace(/^(?:>\s*)+/, '');
|
|
1595
|
+
body = body.replace(/^(?:[-*+]|\d+[.)])\s+/, '');
|
|
1596
|
+
if (body.startsWith('[NEEDS CLARIFICATION]')) return true;
|
|
1597
|
+
}
|
|
1598
|
+
return false;
|
|
1599
|
+
}
|
|
1600
|
+
function implementationHandoffDecision(input) {
|
|
1601
|
+
const agent = input && input.agent;
|
|
1602
|
+
const applies = agent === 'feature-developer' && implementationRailApplies(input && input.pipeline);
|
|
1603
|
+
if (!applies) {
|
|
1604
|
+
return {
|
|
1605
|
+
start: true,
|
|
1606
|
+
rail: 'off',
|
|
1607
|
+
handoffRule: agent === 'feature-developer' ? testBeforeHandoffRule() : null,
|
|
1608
|
+
constraints: [],
|
|
1609
|
+
};
|
|
1610
|
+
}
|
|
1611
|
+
if (briefHasOpenClarificationMarker(input && input.briefText)) {
|
|
1612
|
+
return { start: false, rail: 'on', reason: 'open-marker', handoffRule: testBeforeHandoffRule(), constraints: [] };
|
|
1613
|
+
}
|
|
1614
|
+
const debugRetries = Number(input && input.debugRetryCount) || 0;
|
|
1615
|
+
if (input && input.debugRetry === true && debugRetries >= 2) {
|
|
1616
|
+
return { start: false, rail: 'on', reason: 'debug-retry-cap', handoffRule: testBeforeHandoffRule(), constraints: [] };
|
|
1617
|
+
}
|
|
1618
|
+
return {
|
|
1619
|
+
start: true,
|
|
1620
|
+
rail: 'on',
|
|
1621
|
+
handoffRule: testBeforeHandoffRule(),
|
|
1622
|
+
constraints: [
|
|
1623
|
+
'failing test before production edits',
|
|
1624
|
+
'branch or worktree before the first implementation task',
|
|
1625
|
+
'code-reviewer between decomposition tasks',
|
|
1626
|
+
'debug retries capped at two',
|
|
1627
|
+
],
|
|
1628
|
+
};
|
|
1629
|
+
}
|
|
1630
|
+
// <<<implementation-rail-end>>>
|
|
1631
|
+
|
|
1632
|
+
function readPipelineRecord() {
|
|
1633
|
+
try {
|
|
1634
|
+
return JSON.parse(fs.readFileSync(pipelinePath, 'utf8'));
|
|
1635
|
+
} catch {
|
|
1636
|
+
return null;
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
function readBriefText() {
|
|
1640
|
+
try {
|
|
1641
|
+
return fs.readFileSync(path.join(taskDir, 'brief.md'), 'utf8');
|
|
1642
|
+
} catch {
|
|
1643
|
+
return '';
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
function countDebugImplementationRetries() {
|
|
1647
|
+
try {
|
|
1648
|
+
const metrics = JSON.parse(fs.readFileSync(METRICS_PATH, 'utf8'));
|
|
1649
|
+
const events = Array.isArray(metrics.events) ? metrics.events : [];
|
|
1650
|
+
return events.filter((event) => event && event.type === 'retry_scheduled' && event.nextAgent === 'feature-developer' && event.retryAfterFix === 'debugger').length;
|
|
1651
|
+
} catch {
|
|
1652
|
+
return 0;
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
function featureDeveloperAlreadyStarted() {
|
|
1656
|
+
try {
|
|
1657
|
+
const metrics = JSON.parse(fs.readFileSync(METRICS_PATH, 'utf8'));
|
|
1658
|
+
const events = Array.isArray(metrics.events) ? metrics.events : [];
|
|
1659
|
+
return events.some((event) => event && event.type === 'attempt_started' && event.agent === 'feature-developer');
|
|
1660
|
+
} catch {
|
|
1661
|
+
return false;
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
function implementationDebugRetry(agent) {
|
|
1665
|
+
return agent === 'feature-developer' && status.currentAgent === 'debugger' && featureDeveloperAlreadyStarted();
|
|
1666
|
+
}
|
|
1667
|
+
function blockImplementationStart(agent, debugRetry) {
|
|
1668
|
+
const decision = implementationHandoffDecision({
|
|
1669
|
+
agent,
|
|
1670
|
+
pipeline: readPipelineRecord(),
|
|
1671
|
+
briefText: readBriefText(),
|
|
1672
|
+
debugRetry: Boolean(debugRetry),
|
|
1673
|
+
debugRetryCount: countDebugImplementationRetries(),
|
|
1674
|
+
});
|
|
1675
|
+
if (decision.start) return null;
|
|
1676
|
+
if (decision.reason === 'debug-retry-cap') {
|
|
1677
|
+
return `Strict implementation rail is on for slug "${slug}". Debug retries for the implementation handoff are capped at two. Do not start implementation.`;
|
|
1678
|
+
}
|
|
1679
|
+
return `Strict implementation rail is on for slug "${slug}" and a [NEEDS CLARIFICATION] marker is open. Do not start implementation.`;
|
|
1680
|
+
}
|
|
1681
|
+
function refuseImplementationStart(agents, debugRetryFor) {
|
|
1682
|
+
const list = Array.isArray(agents) ? agents : [agents];
|
|
1683
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
1684
|
+
const block = blockImplementationStart(list[index], debugRetryFor(list[index]));
|
|
1685
|
+
if (!block) continue;
|
|
1686
|
+
beginHumanGate();
|
|
1687
|
+
writeStatus({
|
|
1688
|
+
state: 'awaiting_approval',
|
|
1689
|
+
phase: 'human_gate',
|
|
1690
|
+
awaitingHumanGate: true,
|
|
1691
|
+
waveSteps: [],
|
|
1692
|
+
waveId: null,
|
|
1693
|
+
retryAfterFix: null,
|
|
1694
|
+
retryAfterFixStepIndex: null,
|
|
1695
|
+
});
|
|
1696
|
+
out(block);
|
|
1697
|
+
process.exit(0);
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
function recordAllowedDebugRetries(agents) {
|
|
1701
|
+
if (!implementationRailApplies(readPipelineRecord())) return;
|
|
1702
|
+
const list = Array.isArray(agents) ? agents : [agents];
|
|
1703
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
1704
|
+
if (!implementationDebugRetry(list[index])) continue;
|
|
1705
|
+
appendMetric({
|
|
1706
|
+
type: 'retry_scheduled',
|
|
1707
|
+
at: now,
|
|
1708
|
+
attemptId: null,
|
|
1709
|
+
nextAgent: 'feature-developer',
|
|
1710
|
+
retryAfterFix: 'debugger',
|
|
1711
|
+
source: 'hook',
|
|
1712
|
+
attributionConfidence: 'high',
|
|
1713
|
+
});
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
function railInstructionClause(agent) {
|
|
1717
|
+
const decision = implementationHandoffDecision({
|
|
1718
|
+
agent,
|
|
1719
|
+
pipeline: readPipelineRecord(),
|
|
1720
|
+
briefText: readBriefText(),
|
|
1721
|
+
debugRetry: false,
|
|
1722
|
+
debugRetryCount: 0,
|
|
1723
|
+
});
|
|
1724
|
+
if (decision.rail !== 'on' || !decision.start) return '';
|
|
1725
|
+
return railOnHandoffClause();
|
|
1726
|
+
}
|
|
1727
|
+
|
|
840
1728
|
function handoffForAgent(agent, scope, step = null) {
|
|
841
1729
|
const fn = AGENT_HANDOFF[agent];
|
|
842
1730
|
const handoff = fn ? fn(slug, scope) : `Invoke ${agent} subagent for slug "${slug}". Upsert the owned terminal receipt in artifact-manifest.json; do not mutate status.json lifecycle fields.`;
|
|
843
|
-
return handoff + artifactHandoff(step, agent) + metricsHandoff(agent);
|
|
1731
|
+
return handoff + railInstructionClause(agent) + artifactHandoff(step, agent) + metricsHandoff(agent) + decompositionReadyClause(step ? [step] : []);
|
|
844
1732
|
}
|
|
845
1733
|
|
|
846
1734
|
function handoffForParallelStep(step) {
|
|
@@ -852,7 +1740,8 @@ function handoffForParallelStep(step) {
|
|
|
852
1740
|
`Invoke IN PARALLEL (one message, multiple Task calls): ${agents.join(' AND ')} for slug "${slug}". ` +
|
|
853
1741
|
`Each agent writes its artifact and owned receipt; the hook records parallelCompleted from stops. Do not mutate status.json lifecycle fields. ` +
|
|
854
1742
|
`Artifact inputs: ${Array.isArray(step.artifactInputIds) ? step.artifactInputIds.join(', ') : 'manifest defaults'}; owned output IDs: ${agents.map((agent) => `${agent}=${artifactOutputIdFor(step, agent)}`).join('; ')}. ` +
|
|
855
|
-
`Metrics attemptIds: ${attemptIds.join('; ')}. If the hook cannot identify a parallel stop, append a minimal { attemptId, outcome } receipt to .metrics-receipts.json; do not calculate duration. ${receiptPathClause()}`
|
|
1743
|
+
`Metrics attemptIds: ${attemptIds.join('; ')}. If the hook cannot identify a parallel stop, append a minimal { attemptId, outcome } receipt to .metrics-receipts.json; do not calculate duration. ${receiptPathClause()}` +
|
|
1744
|
+
decompositionReadyClause([step])
|
|
856
1745
|
);
|
|
857
1746
|
}
|
|
858
1747
|
|
|
@@ -1015,6 +1904,8 @@ if (hookInput.action === 'end_human_gate') {
|
|
|
1015
1904
|
const resumeNext = findNextExecutableStep(resumeSteps, resumeStart, taskDir);
|
|
1016
1905
|
if (resumeNext) {
|
|
1017
1906
|
const resumeAgents = getStepAgents(resumeNext.step);
|
|
1907
|
+
refuseImplementationStart(resumeAgents, (agent) => implementationDebugRetry(agent));
|
|
1908
|
+
recordAllowedDebugRetries(resumeAgents);
|
|
1018
1909
|
activateAttempts(resumeNext.step, resumeNext.index);
|
|
1019
1910
|
writeStatus({
|
|
1020
1911
|
pipelineIndex: resumeNext.index,
|
|
@@ -1171,6 +2062,9 @@ function nearestPrecedingStepIndex(agent, fromIndex, fallback) {
|
|
|
1171
2062
|
return match >= 0 ? match : fallback;
|
|
1172
2063
|
}
|
|
1173
2064
|
function scheduleRetry(agent, retryAfterFix, predecessor, explicitStepIndex = null) {
|
|
2065
|
+
if (agent === 'feature-developer') {
|
|
2066
|
+
refuseImplementationStart([agent], () => retryAfterFix === 'debugger');
|
|
2067
|
+
}
|
|
1174
2068
|
const currentIndex = typeof status.pipelineIndex === 'number' ? status.pipelineIndex : 0;
|
|
1175
2069
|
const explicitOk = Number.isInteger(explicitStepIndex) && getStepAgents(steps[explicitStepIndex]).includes(agent);
|
|
1176
2070
|
const stepIndex = explicitOk ? explicitStepIndex : nearestPrecedingStepIndex(agent, currentIndex, -1);
|
|
@@ -1478,6 +2372,8 @@ if (!next) {
|
|
|
1478
2372
|
const { step: nextStep, index: nextIdx } = next;
|
|
1479
2373
|
const nextAgents = getStepAgents(nextStep);
|
|
1480
2374
|
|
|
2375
|
+
refuseImplementationStart(nextAgents, (agent) => implementationDebugRetry(agent));
|
|
2376
|
+
recordAllowedDebugRetries(nextAgents);
|
|
1481
2377
|
activateAttempts(nextStep, nextIdx);
|
|
1482
2378
|
writeStatus({
|
|
1483
2379
|
pipelineIndex: nextIdx,
|
|
@@ -1506,6 +2402,180 @@ const fs = require('fs');
|
|
|
1506
2402
|
const path = require('path');
|
|
1507
2403
|
const { spawnSync } = require('child_process');
|
|
1508
2404
|
|
|
2405
|
+
// <<<standards-draft>>>
|
|
2406
|
+
// Explicit payload only. This postflight never walks the repo and never
|
|
2407
|
+
// creates `.ai.local/rules/`. The same provenance tag does not rewrite an
|
|
2408
|
+
// accepted Gotchas, Patterns, or Decisions line.
|
|
2409
|
+
function standardsDraftTag(provenance) {
|
|
2410
|
+
return '(`' + provenance + '`)';
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
function standardsDraftLineIndex(content, header) {
|
|
2414
|
+
if (content.startsWith(header) && (content.length === header.length || content.charAt(header.length) === '\n')) return 0;
|
|
2415
|
+
const at = content.indexOf('\n' + header + '\n');
|
|
2416
|
+
if (at !== -1) return at + 1;
|
|
2417
|
+
if (content.endsWith('\n' + header)) return content.length - header.length;
|
|
2418
|
+
return -1;
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
function standardsDraftSection(content, name) {
|
|
2422
|
+
const header = '## ' + name;
|
|
2423
|
+
const start = standardsDraftLineIndex(content, header);
|
|
2424
|
+
if (start === -1) return null;
|
|
2425
|
+
const from = start + header.length;
|
|
2426
|
+
const next = content.indexOf('\n## ', from);
|
|
2427
|
+
const end = next === -1 ? content.length : next;
|
|
2428
|
+
return content.slice(start, end);
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
function standardsDraftProtected(content) {
|
|
2432
|
+
const names = ['Language', 'Gotchas', 'Patterns', 'Decisions', 'Advisory index'];
|
|
2433
|
+
let snapshot = '';
|
|
2434
|
+
for (let index = 0; index < names.length; index += 1) {
|
|
2435
|
+
const body = standardsDraftSection(content, names[index]);
|
|
2436
|
+
snapshot += names[index] + '\n' + (body === null ? '' : body) + '\n';
|
|
2437
|
+
}
|
|
2438
|
+
return snapshot;
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
function standardsDraftNormalize(line) {
|
|
2442
|
+
return line.trim().replace(/\s+/g, ' ').replace(/^-\s+/, '').replace(/\s+\(`[^`]*`\)$/, '').toLowerCase();
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2445
|
+
function standardsDraftAccepted(content, text) {
|
|
2446
|
+
const pending = standardsDraftSection(content, 'Pending');
|
|
2447
|
+
let rest = content;
|
|
2448
|
+
if (pending !== null) {
|
|
2449
|
+
const start = content.indexOf(pending);
|
|
2450
|
+
rest = content.slice(0, start) + content.slice(start + pending.length);
|
|
2451
|
+
}
|
|
2452
|
+
const needle = standardsDraftNormalize(text);
|
|
2453
|
+
if (!needle) return false;
|
|
2454
|
+
const lines = rest.split('\n');
|
|
2455
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
2456
|
+
const line = lines[index];
|
|
2457
|
+
if (!/^-\s+\S/.test(line)) continue;
|
|
2458
|
+
if (standardsDraftNormalize(line) === needle) return true;
|
|
2459
|
+
}
|
|
2460
|
+
return false;
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
function standardsDraftAppendPending(content, line) {
|
|
2464
|
+
const header = '## Pending';
|
|
2465
|
+
const start = standardsDraftLineIndex(content, header);
|
|
2466
|
+
if (start === -1) {
|
|
2467
|
+
const sep = content.endsWith('\n') ? '\n' : '\n\n';
|
|
2468
|
+
return content + sep + header + '\n\n' + line + '\n';
|
|
2469
|
+
}
|
|
2470
|
+
const from = start + header.length;
|
|
2471
|
+
const next = content.indexOf('\n## ', from);
|
|
2472
|
+
const end = next === -1 ? content.length : next;
|
|
2473
|
+
let body = content.slice(from, end);
|
|
2474
|
+
const hasBullet = /(^|\n)-\s+\S/.test(body);
|
|
2475
|
+
if (!hasBullet && /(^|\n)-[ \t]*(?=\n|$)/.test(body)) {
|
|
2476
|
+
body = body.replace(/(^|\n)-[ \t]*(?=\n|$)/, '$1' + line);
|
|
2477
|
+
} else if (body.endsWith('\n\n')) {
|
|
2478
|
+
body = body.slice(0, -1) + line + '\n\n';
|
|
2479
|
+
} else if (body.endsWith('\n')) {
|
|
2480
|
+
body = body + line + '\n';
|
|
2481
|
+
} else {
|
|
2482
|
+
body = body + '\n' + line + '\n';
|
|
2483
|
+
}
|
|
2484
|
+
return content.slice(0, from) + body + content.slice(end);
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
function standardsDraftProvenance(draft) {
|
|
2488
|
+
if (!draft || typeof draft.provenance !== 'string') return '';
|
|
2489
|
+
const provenance = draft.provenance.trim();
|
|
2490
|
+
if (/^incident: [A-Za-z0-9][A-Za-z0-9._:-]{0,80}$/.test(provenance)) return provenance;
|
|
2491
|
+
const read = /^read: ([A-Za-z0-9][A-Za-z0-9._/-]{0,160})$/.exec(provenance);
|
|
2492
|
+
if (!read) return '';
|
|
2493
|
+
const parts = read[1].split('/');
|
|
2494
|
+
if (parts.indexOf('..') !== -1 || parts.indexOf('.') !== -1) return '';
|
|
2495
|
+
if (read[1] === '.ai.local/rules' || read[1].indexOf('.ai.local/rules/') === 0) return '';
|
|
2496
|
+
return provenance;
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
function standardsDraftWrite(target, content) {
|
|
2500
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
2501
|
+
const temporary = target + '.' + String(process.pid) + '.tmp';
|
|
2502
|
+
fs.writeFileSync(temporary, content);
|
|
2503
|
+
fs.renameSync(temporary, target);
|
|
2504
|
+
}
|
|
2505
|
+
|
|
2506
|
+
function appendOptionalStandardsDraft(root, draft) {
|
|
2507
|
+
const provenance = standardsDraftProvenance(draft);
|
|
2508
|
+
if (!provenance) return { wrote: false, reason: 'provenance' };
|
|
2509
|
+
const text = draft && typeof draft.text === 'string' ? draft.text.trim() : '';
|
|
2510
|
+
if (!text || text.length > 400 || text.indexOf('\n') !== -1 || text.indexOf('\r') !== -1 || text.indexOf('## ') !== -1) {
|
|
2511
|
+
return { wrote: false, reason: 'text' };
|
|
2512
|
+
}
|
|
2513
|
+
let destination = 'conventions';
|
|
2514
|
+
if (draft.destination != null) {
|
|
2515
|
+
if (draft.destination === 'conventions' || draft.destination === 'rules-library') destination = draft.destination;
|
|
2516
|
+
else return { wrote: false, reason: 'destination' };
|
|
2517
|
+
}
|
|
2518
|
+
const line = '- ' + text + ' ' + standardsDraftTag(provenance);
|
|
2519
|
+
const overlay = path.join(root, '.ai.local');
|
|
2520
|
+
const rulesDir = path.join(overlay, 'rules');
|
|
2521
|
+
const rulesPrefix = rulesDir + path.sep;
|
|
2522
|
+
|
|
2523
|
+
if (destination === 'rules-library') {
|
|
2524
|
+
const stem = draft && typeof draft.stem === 'string' ? draft.stem : '';
|
|
2525
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+){0,8}$/.test(stem) || stem.length > 48) {
|
|
2526
|
+
return { wrote: false, reason: 'stem' };
|
|
2527
|
+
}
|
|
2528
|
+
const libraryDir = path.join(overlay, 'rules-library');
|
|
2529
|
+
const target = path.join(libraryDir, stem + '.md');
|
|
2530
|
+
const libraryPrefix = libraryDir + path.sep;
|
|
2531
|
+
if (!target.startsWith(libraryPrefix)) return { wrote: false, reason: 'path' };
|
|
2532
|
+
if (target === rulesDir || target.startsWith(rulesPrefix)) return { wrote: false, reason: 'rules' };
|
|
2533
|
+
let existing = null;
|
|
2534
|
+
try {
|
|
2535
|
+
existing = fs.readFileSync(target, 'utf8');
|
|
2536
|
+
} catch (error) {
|
|
2537
|
+
if (!error || error.code !== 'ENOENT') throw error;
|
|
2538
|
+
}
|
|
2539
|
+
if (existing !== null) {
|
|
2540
|
+
if (existing.indexOf(standardsDraftTag(provenance)) !== -1) {
|
|
2541
|
+
return { wrote: false, reason: 'same-provenance', target: target };
|
|
2542
|
+
}
|
|
2543
|
+
const prefix = existing.endsWith('\n') ? '' : '\n';
|
|
2544
|
+
fs.appendFileSync(target, prefix + line + '\n');
|
|
2545
|
+
return { wrote: true, target: target, mode: 'append-provenance' };
|
|
2546
|
+
}
|
|
2547
|
+
standardsDraftWrite(target, '# ' + stem + '\n\nPending draft. Inactive until a human moves this file into `.ai.local/rules/`.\n\n' + line + '\n');
|
|
2548
|
+
return { wrote: true, target: target, mode: 'create' };
|
|
2549
|
+
}
|
|
2550
|
+
|
|
2551
|
+
const conventionsPath = path.join(overlay, 'conventions.md');
|
|
2552
|
+
if (conventionsPath === rulesDir || conventionsPath.startsWith(rulesPrefix)) {
|
|
2553
|
+
return { wrote: false, reason: 'rules' };
|
|
2554
|
+
}
|
|
2555
|
+
let content = null;
|
|
2556
|
+
try {
|
|
2557
|
+
content = fs.readFileSync(conventionsPath, 'utf8');
|
|
2558
|
+
} catch (error) {
|
|
2559
|
+
if (!error || error.code !== 'ENOENT') throw error;
|
|
2560
|
+
}
|
|
2561
|
+
if (content === null) content = '# Living conventions\n';
|
|
2562
|
+
if (content.indexOf(standardsDraftTag(provenance)) !== -1) {
|
|
2563
|
+
return { wrote: false, reason: 'same-provenance', target: conventionsPath };
|
|
2564
|
+
}
|
|
2565
|
+
const before = standardsDraftProtected(content);
|
|
2566
|
+
const next = standardsDraftAppendPending(content, line);
|
|
2567
|
+
if (standardsDraftProtected(next) !== before) {
|
|
2568
|
+
return { wrote: false, reason: 'protected', target: conventionsPath };
|
|
2569
|
+
}
|
|
2570
|
+
standardsDraftWrite(conventionsPath, next);
|
|
2571
|
+
return {
|
|
2572
|
+
wrote: true,
|
|
2573
|
+
target: conventionsPath,
|
|
2574
|
+
mode: standardsDraftAccepted(content, text) ? 'provenance-only' : 'pending',
|
|
2575
|
+
};
|
|
2576
|
+
}
|
|
2577
|
+
// <<<standards-draft-end>>>
|
|
2578
|
+
|
|
1509
2579
|
let input;
|
|
1510
2580
|
try {
|
|
1511
2581
|
input = JSON.parse(process.env.INPUT || '');
|
|
@@ -1538,6 +2608,12 @@ if (pipeline && (pipeline.profile === 'light' || pipeline.intent === 'spike')) {
|
|
|
1538
2608
|
process.exit(0);
|
|
1539
2609
|
}
|
|
1540
2610
|
|
|
2611
|
+
try {
|
|
2612
|
+
appendOptionalStandardsDraft(process.cwd(), input.standardsDraft);
|
|
2613
|
+
} catch {
|
|
2614
|
+
// A draft failure must not fail the stop, and must not add a WARNING.
|
|
2615
|
+
}
|
|
2616
|
+
|
|
1541
2617
|
if (!fs.existsSync(path.join(process.cwd(), '.ai.local', 'scripts', 'run.mjs'))) {
|
|
1542
2618
|
process.stderr.write(
|
|
1543
2619
|
'WARNING: self-learning loop skipped: .ai.local/scripts/run.mjs is missing (install it with `npx @bonesofspring/ai-rules init <tool> --preset <stack>`)\n',
|