pi-spine 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/spine-batch.mjs +160 -53
- package/bin/spine-cli/batch.mjs +3 -3
- package/bin/spine-cli/lane-logs.mjs +24 -0
- package/bin/spine-cli/scenarios.mjs +298 -0
- package/bin/spine-cli/wait.mjs +30 -0
- package/bin/spine-cli/watch.mjs +27 -0
- package/bin/spine-issue.mjs +196 -0
- package/bin/spine-journal.mjs +1 -0
- package/bin/spine-run.mjs +30 -0
- package/bin/spine-status.mjs +19 -1
- package/bin/spine-tasks.mjs +16 -2
- package/bin/spine-worker-runner.mjs +10 -10
- package/bin/spine.mjs +49 -1
- package/package.json +1 -1
- package/skills/create-spine-tasks/references/contract-template.md +19 -0
- package/src/batch/abort.mjs +2 -11
- package/src/batch/agent-session-worker.mjs +45 -5
- package/src/batch/attached-engine-handoff.mjs +188 -0
- package/src/batch/attached-runner.mjs +312 -0
- package/src/batch/contract-prelanded.mjs +180 -0
- package/src/batch/contract-verify.mjs +140 -2
- package/src/batch/detached-start.mjs +66 -2
- package/src/batch/diagnosis-alternatives.mjs +46 -0
- package/src/batch/diagnosis-merge-failure.mjs +79 -0
- package/src/batch/diagnosis-retry-limbo.mjs +34 -0
- package/src/batch/diagnosis-stub.mjs +109 -0
- package/src/batch/diagnosis-task-done.mjs +110 -0
- package/src/batch/diagnosis-worker-done-missing.mjs +96 -0
- package/src/batch/diagnosis.mjs +135 -68
- package/src/batch/engine-lanes/merge.mjs +110 -82
- package/src/batch/engine-lanes/queue.mjs +4 -1
- package/src/batch/engine-lanes/watch.mjs +29 -0
- package/src/batch/engine-lanes.mjs +1 -0
- package/src/batch/engine-scope.mjs +11 -1
- package/src/batch/engine.mjs +79 -24
- package/src/batch/evidence.mjs +78 -15
- package/src/batch/gate.mjs +43 -14
- package/src/batch/git-helpers.mjs +96 -0
- package/src/batch/heartbeat.mjs +73 -7
- package/src/batch/integrate.mjs +2 -4
- package/src/batch/journal.mjs +3 -0
- package/src/batch/lane-commit.mjs +90 -5
- package/src/batch/lifecycle.mjs +130 -3
- package/src/batch/macro-phase.mjs +226 -0
- package/src/batch/merge/adoption-doc-merge.mjs +93 -10
- package/src/batch/merge/wave-merge-state.mjs +3 -0
- package/src/batch/metrics.mjs +74 -5
- package/src/batch/orphan-detect.mjs +74 -0
- package/src/batch/pause.mjs +223 -0
- package/src/batch/post-merge-limbo.mjs +264 -8
- package/src/batch/reconcile.mjs +429 -54
- package/src/batch/resume-common.mjs +5 -1
- package/src/batch/resume-lane-reviews.mjs +16 -0
- package/src/batch/resume-multi-lanes.mjs +56 -1
- package/src/batch/resume-multi-validate.mjs +67 -6
- package/src/batch/resume-multi.mjs +9 -4
- package/src/batch/resume.mjs +52 -45
- package/src/batch/retry.mjs +56 -5
- package/src/batch/review-spawn.mjs +8 -2
- package/src/batch/rules-manifest-drift.mjs +157 -0
- package/src/batch/salvage.mjs +2 -1
- package/src/batch/sequence.mjs +454 -0
- package/src/batch/state.mjs +18 -3
- package/src/batch/status-json.mjs +67 -0
- package/src/batch/task-stall-budget.mjs +22 -10
- package/src/batch/worker-host.mjs +95 -7
- package/src/batch/worker-output.mjs +196 -3
- package/src/cli/issue-draft.mjs +180 -0
- package/src/cli/journal-follow.mjs +239 -0
- package/src/cli/lane-logs.mjs +291 -0
- package/src/cli/sequence.mjs +108 -0
- package/src/cli/settings-set.mjs +2 -21
- package/src/cli/wait.mjs +187 -0
- package/src/cli/watch.mjs +160 -0
- package/src/config/agent-model-resolve.mjs +48 -0
- package/src/config/cursor-rules/discover.mjs +3 -21
- package/src/config/defaults.mjs +7 -1
- package/src/config/settings-fields.mjs +48 -0
- package/src/config/spine-init-constants.mjs +1 -0
- package/src/config/spine-preflight-lib.mjs +471 -16
- package/src/dashboard/lane-throughput.mjs +351 -0
- package/src/dashboard/public/dashboard.css +25 -1
- package/src/dashboard/public/dashboard.js +223 -18
- package/src/dashboard/public/index.html +14 -2
- package/src/dashboard/server.mjs +4 -0
- package/src/dashboard/snapshot.mjs +215 -1
- package/src/dashboard/view.mjs +170 -17
- package/src/doctor/agent-model-inherit.mjs +61 -6
- package/src/doctor/run-doctor-checks.mjs +26 -1
- package/src/doctor/sequence-safety.mjs +57 -0
- package/src/doctor/stale-worktrees.mjs +102 -0
- package/src/fixtures/scenario-registry.mjs +194 -0
- package/src/fs/atomic-write.mjs +49 -0
- package/src/planner/file-scope.mjs +246 -0
- package/src/planner/format-plan.mjs +9 -7
- package/src/planner/index.mjs +6 -2
- package/src/planner/wave-scope.mjs +136 -0
- package/src/planner/waves.mjs +133 -0
- package/src/tasks/packet/index.mjs +1 -0
- package/src/tasks/packet/parse-prompt.mjs +42 -1
- package/src/tasks/packet/validate-prompt.mjs +124 -0
- package/templates/spine-config.json +8 -1
|
@@ -22,8 +22,9 @@ import {
|
|
|
22
22
|
} from "./stall-config.mjs";
|
|
23
23
|
import { buildRulesManifestDoctorCheck } from "./rules-manifest.mjs";
|
|
24
24
|
import { buildWorktreeHealthDoctorCheck } from "./worktree-health.mjs";
|
|
25
|
+
import { buildStaleWorktreesDoctorCheck } from "./stale-worktrees.mjs";
|
|
25
26
|
import { buildWorkerBackendDoctorCheck } from "../config/worker-backend.mjs";
|
|
26
|
-
import { buildAgentModelInheritDoctorCheck } from "./agent-model-inherit.mjs";
|
|
27
|
+
import { buildAgentModelInheritDoctorCheck, buildReviewerPerTypePinsDoctorCheck } from "./agent-model-inherit.mjs";
|
|
27
28
|
import { CURSOR_RULES_ROOT_REL } from "../config/cursor-rules/discover.mjs";
|
|
28
29
|
import { RULES_PROFILE_REL_PATH } from "../config/cursor-rules/profile.mjs";
|
|
29
30
|
import {
|
|
@@ -346,6 +347,7 @@ export function runDoctorChecks(projectRoot = process.cwd()) {
|
|
|
346
347
|
);
|
|
347
348
|
checks.push(buildStallConfigDoctorCheck({ config: configResult.config }));
|
|
348
349
|
checks.push(buildPiWorkerTimeoutDoctorCheck({ config: configResult.config }));
|
|
350
|
+
checks.push(buildReviewerPerTypePinsDoctorCheck({ config: configResult.config }));
|
|
349
351
|
checks.push(buildAgentModelInheritDoctorCheck({
|
|
350
352
|
config: configResult.config,
|
|
351
353
|
projectRoot,
|
|
@@ -418,6 +420,8 @@ export function runDoctorChecks(projectRoot = process.cwd()) {
|
|
|
418
420
|
}
|
|
419
421
|
|
|
420
422
|
if (isInsideGitRepo(projectRoot)) {
|
|
423
|
+
checks.push(buildStaleWorktreesDoctorCheck({ projectRoot }));
|
|
424
|
+
|
|
421
425
|
const gitignorePath = path.join(projectRoot, ".gitignore");
|
|
422
426
|
if (!fs.existsSync(gitignorePath)) {
|
|
423
427
|
checks.push({
|
|
@@ -442,6 +446,27 @@ export function runDoctorChecks(projectRoot = process.cwd()) {
|
|
|
442
446
|
suggestedCommand: missing.length > 0 ? "spine init" : undefined,
|
|
443
447
|
});
|
|
444
448
|
}
|
|
449
|
+
|
|
450
|
+
const metricsRelPath = configResult.config?.metrics?.path ?? ".spine/run-metrics.jsonl";
|
|
451
|
+
try {
|
|
452
|
+
const tracked = execFileSync("git", ["ls-files", "--", metricsRelPath], {
|
|
453
|
+
cwd: projectRoot,
|
|
454
|
+
encoding: "utf-8",
|
|
455
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
456
|
+
timeout: 5000,
|
|
457
|
+
}).trim();
|
|
458
|
+
if (tracked) {
|
|
459
|
+
checks.push({
|
|
460
|
+
label: "run-metrics.jsonl not git-tracked",
|
|
461
|
+
ok: true,
|
|
462
|
+
warning: true,
|
|
463
|
+
detail: `${metricsRelPath} is tracked by git — runtime appends break preflight until untracked`,
|
|
464
|
+
suggestedCommand: `git rm --cached -- ${metricsRelPath}`,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
} catch {
|
|
468
|
+
// Git ls-files unavailable; skip tracked-metrics advisory.
|
|
469
|
+
}
|
|
445
470
|
}
|
|
446
471
|
|
|
447
472
|
const rulesRootPath = path.join(projectRoot, CURSOR_RULES_ROOT_REL);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequence auto-approve gate safety (GitHub #54 Tier 2 SP-E).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { isStubWorkerMode } from "./stall-config.mjs";
|
|
6
|
+
|
|
7
|
+
const AUTO_APPROVE_REFUSAL_MESSAGE =
|
|
8
|
+
"--auto-approve-gate bypasses the human integrate gate and is blocked for real pi workers. " +
|
|
9
|
+
"Use SPINE_WORKER_STUB=1 for CI/stub sequences, or pass --force after reviewing the risk.";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {object} [params]
|
|
13
|
+
* @param {boolean} [params.autoApproveGate]
|
|
14
|
+
* @param {boolean} [params.force]
|
|
15
|
+
*/
|
|
16
|
+
export function validateSequenceAutoApproveGate({ autoApproveGate = false, force = false } = {}) {
|
|
17
|
+
if (!autoApproveGate) {
|
|
18
|
+
return { ok: true };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (isStubWorkerMode()) {
|
|
22
|
+
return { ok: true, stubMode: true };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (force) {
|
|
26
|
+
return { ok: true, forced: true };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
error: "auto_approve_gate_unsafe",
|
|
32
|
+
output: `${AUTO_APPROVE_REFUSAL_MESSAGE}\n`,
|
|
33
|
+
suggestedCommand: "SPINE_WORKER_STUB=1 spine run sequence <scope> --auto-approve-gate",
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Doctor advisory for sequence auto-approve gate usage.
|
|
39
|
+
*/
|
|
40
|
+
export function buildSequenceAutoApproveDoctorCheck() {
|
|
41
|
+
if (isStubWorkerMode()) {
|
|
42
|
+
return {
|
|
43
|
+
label: "sequence --auto-approve-gate",
|
|
44
|
+
ok: true,
|
|
45
|
+
detail: "SPINE_WORKER_STUB set — auto gate approval permitted for stub/CI sequences",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
label: "sequence --auto-approve-gate",
|
|
51
|
+
ok: true,
|
|
52
|
+
warning: true,
|
|
53
|
+
detail:
|
|
54
|
+
"real pi — flag blocked unless --force; use manual spine gate approve between waves",
|
|
55
|
+
suggestedCommand: "spine doctor # verify SPINE_WORKER_STUB is unset before production runs",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Doctor check for leftover `.worktrees/spine-<batchId>/` dirs (issue #26, SP-351).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
|
|
8
|
+
import { TERMINAL_BATCH_PHASES } from "../batch/state.mjs";
|
|
9
|
+
|
|
10
|
+
const SPINE_WORKTREE_DIR_PATTERN = /^spine-(.+)$/;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Batch ID from `.spine/batch-state.json` when the batch is not terminal.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} projectRoot
|
|
16
|
+
* @returns {string|null}
|
|
17
|
+
*/
|
|
18
|
+
export function resolveInProgressSpineBatchId(projectRoot) {
|
|
19
|
+
const statePath = path.join(projectRoot, ".spine", "batch-state.json");
|
|
20
|
+
if (!fs.existsSync(statePath)) return null;
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const raw = JSON.parse(fs.readFileSync(statePath, "utf-8"));
|
|
24
|
+
const batchId = String(raw.batchId ?? raw.id ?? "").trim();
|
|
25
|
+
if (!batchId) return null;
|
|
26
|
+
|
|
27
|
+
const phase = String(raw.phase ?? raw.status ?? "").toLowerCase();
|
|
28
|
+
if (TERMINAL_BATCH_PHASES.has(phase)) return null;
|
|
29
|
+
|
|
30
|
+
return batchId;
|
|
31
|
+
} catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* List `.worktrees/spine-<batchId>/` directories with no matching in-progress batch.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} projectRoot
|
|
40
|
+
* @param {string|null} [activeBatchId]
|
|
41
|
+
* @returns {string[]} batch IDs (not full paths)
|
|
42
|
+
*/
|
|
43
|
+
export function listStaleSpineWorktreeBatchIds(projectRoot, activeBatchId = null) {
|
|
44
|
+
const worktreesRoot = path.join(projectRoot, ".worktrees");
|
|
45
|
+
if (!fs.existsSync(worktreesRoot)) return [];
|
|
46
|
+
|
|
47
|
+
/** @type {string[]} */
|
|
48
|
+
const stale = [];
|
|
49
|
+
const entries = fs.readdirSync(worktreesRoot, { withFileTypes: true });
|
|
50
|
+
for (const entry of entries) {
|
|
51
|
+
if (!entry.isDirectory()) continue;
|
|
52
|
+
const match = SPINE_WORKTREE_DIR_PATTERN.exec(entry.name);
|
|
53
|
+
if (!match) continue;
|
|
54
|
+
|
|
55
|
+
const batchId = match[1].trim();
|
|
56
|
+
if (!batchId) continue;
|
|
57
|
+
if (activeBatchId && batchId === activeBatchId) continue;
|
|
58
|
+
|
|
59
|
+
stale.push(batchId);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
stale.sort();
|
|
63
|
+
return stale;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {object} params
|
|
68
|
+
* @param {string} params.projectRoot
|
|
69
|
+
* @param {string|null} [params.activeBatchId]
|
|
70
|
+
* @param {(root: string) => string|null} [params.resolveActiveBatchId]
|
|
71
|
+
*/
|
|
72
|
+
export function buildStaleWorktreesDoctorCheck({
|
|
73
|
+
projectRoot,
|
|
74
|
+
activeBatchId,
|
|
75
|
+
resolveActiveBatchId = resolveInProgressSpineBatchId,
|
|
76
|
+
}) {
|
|
77
|
+
const inProgressBatchId =
|
|
78
|
+
activeBatchId === undefined ? resolveActiveBatchId(projectRoot) : activeBatchId;
|
|
79
|
+
const staleBatchIds = listStaleSpineWorktreeBatchIds(projectRoot, inProgressBatchId);
|
|
80
|
+
|
|
81
|
+
if (staleBatchIds.length === 0) {
|
|
82
|
+
return {
|
|
83
|
+
label: "stale worktrees",
|
|
84
|
+
ok: true,
|
|
85
|
+
detail: inProgressBatchId
|
|
86
|
+
? `none (active batch ${inProgressBatchId})`
|
|
87
|
+
: "none",
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const preview = staleBatchIds.slice(0, 3).map((id) => `spine-${id}`).join(", ");
|
|
92
|
+
const suffix = staleBatchIds.length > 3 ? ` (+${staleBatchIds.length - 3} more)` : "";
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
label: "stale worktrees",
|
|
96
|
+
ok: true,
|
|
97
|
+
warning: true,
|
|
98
|
+
detail: `${staleBatchIds.length} leftover batch dir(s): ${preview}${suffix}`,
|
|
99
|
+
suggestedCommand:
|
|
100
|
+
"After spine batch complete/dismiss, lane worktrees auto-clean (lanes.cleanupWorktreesOnComplete). Remove legacy dirs: git worktree remove --force .worktrees/spine-<batchId>/lane-N; rm -rf .worktrees/spine-<batchId>",
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scenario fixture registry — central catalog for incident, stub, adoption, and recipe fixtures.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
|
|
8
|
+
import { resolvePiSpineRoot } from "../config/pi-spine-root.mjs";
|
|
9
|
+
|
|
10
|
+
export const REGISTRY_SCHEMA_VERSION = 1;
|
|
11
|
+
|
|
12
|
+
/** @type {readonly string[]} */
|
|
13
|
+
export const SCENARIO_KINDS = Object.freeze([
|
|
14
|
+
"incident",
|
|
15
|
+
"stub",
|
|
16
|
+
"adoption",
|
|
17
|
+
"recipe",
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
const REGISTRY_RELATIVE_PATH = path.join("tests", "fixtures", "scenarios", "registry.json");
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} [packageRoot]
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
export function resolveRegistryPath(packageRoot = resolvePiSpineRoot({}, process.cwd(), import.meta.url)) {
|
|
27
|
+
return path.join(packageRoot, REGISTRY_RELATIVE_PATH);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {{ packageRoot?: string }} [options]
|
|
32
|
+
* @returns {{ schemaVersion: number, scenarios: object[] }}
|
|
33
|
+
*/
|
|
34
|
+
export function loadRegistry(options = {}) {
|
|
35
|
+
const packageRoot =
|
|
36
|
+
options.packageRoot ?? resolvePiSpineRoot({}, process.cwd(), import.meta.url);
|
|
37
|
+
const registryPath = resolveRegistryPath(packageRoot);
|
|
38
|
+
const raw = fs.readFileSync(registryPath, "utf-8");
|
|
39
|
+
return JSON.parse(raw);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {{ packageRoot?: string }} [options]
|
|
44
|
+
* @returns {object[]}
|
|
45
|
+
*/
|
|
46
|
+
export function listScenarios(options = {}) {
|
|
47
|
+
const registry = loadRegistry(options);
|
|
48
|
+
return [...registry.scenarios].sort((left, right) =>
|
|
49
|
+
String(left.id).localeCompare(String(right.id)),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {string} id
|
|
55
|
+
* @param {{ packageRoot?: string }} [options]
|
|
56
|
+
* @returns {object | null}
|
|
57
|
+
*/
|
|
58
|
+
export function getScenario(id, options = {}) {
|
|
59
|
+
const registry = loadRegistry(options);
|
|
60
|
+
return registry.scenarios.find((entry) => entry.id === id) ?? null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {unknown} value
|
|
65
|
+
* @returns {value is string}
|
|
66
|
+
*/
|
|
67
|
+
function isNonEmptyString(value) {
|
|
68
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {unknown} value
|
|
73
|
+
* @returns {value is string[]}
|
|
74
|
+
*/
|
|
75
|
+
function isStringArray(value) {
|
|
76
|
+
return Array.isArray(value) && value.every((entry) => isNonEmptyString(entry));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {object} scenario
|
|
81
|
+
* @param {Set<string>} seenIds
|
|
82
|
+
* @param {{ packageRoot: string, checkFixturePaths: boolean }} context
|
|
83
|
+
* @returns {string[]}
|
|
84
|
+
*/
|
|
85
|
+
function validateScenarioEntry(scenario, seenIds, context) {
|
|
86
|
+
/** @type {string[]} */
|
|
87
|
+
const errors = [];
|
|
88
|
+
|
|
89
|
+
if (!scenario || typeof scenario !== "object" || Array.isArray(scenario)) {
|
|
90
|
+
return ["scenario entry must be an object"];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!isNonEmptyString(scenario.id)) {
|
|
94
|
+
errors.push("scenario.id must be a non-empty string");
|
|
95
|
+
} else if (seenIds.has(scenario.id)) {
|
|
96
|
+
errors.push(`duplicate scenario id: ${scenario.id}`);
|
|
97
|
+
} else {
|
|
98
|
+
seenIds.add(scenario.id);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!isNonEmptyString(scenario.kind)) {
|
|
102
|
+
errors.push("scenario.kind must be a non-empty string");
|
|
103
|
+
} else if (!SCENARIO_KINDS.includes(scenario.kind)) {
|
|
104
|
+
errors.push(`scenario.kind must be one of: ${SCENARIO_KINDS.join(", ")}`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!isNonEmptyString(scenario.title)) {
|
|
108
|
+
errors.push("scenario.title must be a non-empty string");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (scenario.description != null && typeof scenario.description !== "string") {
|
|
112
|
+
errors.push("scenario.description must be a string when set");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const requiresFixturePath = scenario.kind === "incident" || scenario.kind === "stub" || scenario.kind === "adoption";
|
|
116
|
+
if (requiresFixturePath && !isNonEmptyString(scenario.fixturePath)) {
|
|
117
|
+
errors.push(`scenario.fixturePath is required for kind ${scenario.kind ?? "(missing)"}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (scenario.fixturePath != null && !isNonEmptyString(scenario.fixturePath)) {
|
|
121
|
+
errors.push("scenario.fixturePath must be a non-empty string when set");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (scenario.batchId != null && !isNonEmptyString(scenario.batchId)) {
|
|
125
|
+
errors.push("scenario.batchId must be a non-empty string when set");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const field of ["tests", "docs", "relatedTasks", "tags"]) {
|
|
129
|
+
if (scenario[field] != null && !isStringArray(scenario[field])) {
|
|
130
|
+
errors.push(`scenario.${field} must be an array of non-empty strings when set`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (
|
|
135
|
+
context.checkFixturePaths &&
|
|
136
|
+
isNonEmptyString(scenario.fixturePath) &&
|
|
137
|
+
isNonEmptyString(scenario.id)
|
|
138
|
+
) {
|
|
139
|
+
const fixturePath = path.join(context.packageRoot, scenario.fixturePath);
|
|
140
|
+
if (!fs.existsSync(fixturePath)) {
|
|
141
|
+
errors.push(`scenario.fixturePath does not exist: ${scenario.fixturePath}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return errors;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @param {object} registry
|
|
150
|
+
* @param {{ packageRoot?: string, checkFixturePaths?: boolean }} [options]
|
|
151
|
+
* @returns {{ ok: boolean, errors: string[] }}
|
|
152
|
+
*/
|
|
153
|
+
export function validateRegistry(registry, options = {}) {
|
|
154
|
+
const packageRoot =
|
|
155
|
+
options.packageRoot ?? resolvePiSpineRoot({}, process.cwd(), import.meta.url);
|
|
156
|
+
const checkFixturePaths = options.checkFixturePaths ?? true;
|
|
157
|
+
/** @type {string[]} */
|
|
158
|
+
const errors = [];
|
|
159
|
+
|
|
160
|
+
if (!registry || typeof registry !== "object" || Array.isArray(registry)) {
|
|
161
|
+
return { ok: false, errors: ["registry must be an object"] };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (registry.schemaVersion !== REGISTRY_SCHEMA_VERSION) {
|
|
165
|
+
errors.push(`schemaVersion must be ${REGISTRY_SCHEMA_VERSION}`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (!Array.isArray(registry.scenarios)) {
|
|
169
|
+
errors.push("scenarios must be an array");
|
|
170
|
+
return { ok: errors.length === 0, errors };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const seenIds = new Set();
|
|
174
|
+
for (const scenario of registry.scenarios) {
|
|
175
|
+
const prefix = isNonEmptyString(scenario?.id) ? `scenario ${scenario.id}:` : "scenario:";
|
|
176
|
+
for (const issue of validateScenarioEntry(scenario, seenIds, {
|
|
177
|
+
packageRoot,
|
|
178
|
+
checkFixturePaths,
|
|
179
|
+
})) {
|
|
180
|
+
errors.push(`${prefix} ${issue}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return { ok: errors.length === 0, errors };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Resolve package root from this module location (for tests and CLI callers).
|
|
189
|
+
*
|
|
190
|
+
* @returns {string}
|
|
191
|
+
*/
|
|
192
|
+
export function scenarioRegistryPackageRoot() {
|
|
193
|
+
return resolvePiSpineRoot({}, process.cwd(), import.meta.url);
|
|
194
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Atomic file writes via temp file + rename (orchestration artifact persistence).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { randomBytes } from "node:crypto";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} targetPath
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
export function makeAtomicTempPath(targetPath) {
|
|
14
|
+
return `${targetPath}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} filePath
|
|
19
|
+
* @param {string} content
|
|
20
|
+
*/
|
|
21
|
+
export function writeTextAtomic(filePath, content) {
|
|
22
|
+
const dir = path.dirname(filePath);
|
|
23
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
24
|
+
|
|
25
|
+
const tmpPath = makeAtomicTempPath(filePath);
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
fs.writeFileSync(tmpPath, content, "utf-8");
|
|
29
|
+
fs.renameSync(tmpPath, filePath);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
try {
|
|
32
|
+
if (fs.existsSync(tmpPath)) {
|
|
33
|
+
fs.unlinkSync(tmpPath);
|
|
34
|
+
}
|
|
35
|
+
} catch {
|
|
36
|
+
// ignore cleanup failure
|
|
37
|
+
}
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {string} filePath
|
|
44
|
+
* @param {unknown} data
|
|
45
|
+
*/
|
|
46
|
+
export function writeJsonAtomic(filePath, data) {
|
|
47
|
+
const content = `${JSON.stringify(data, null, 2)}\n`;
|
|
48
|
+
writeTextAtomic(filePath, content);
|
|
49
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Scope overlap detection for wave planner (SP-352 / issue #31).
|
|
3
|
+
*
|
|
4
|
+
* Detects when two tasks' ## File Scope globs could touch the same paths so
|
|
5
|
+
* parallel lane assignment would risk merge conflicts.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import micromatch from 'micromatch';
|
|
9
|
+
|
|
10
|
+
/** Synthetic basename used when expanding globs into concrete probe paths. */
|
|
11
|
+
const PROBE_BASENAME = '__probe__';
|
|
12
|
+
|
|
13
|
+
/** Extensions probed when expanding directory or wildcard scope entries. */
|
|
14
|
+
const SYNTHETIC_PROBE_EXTENSIONS = Object.freeze([
|
|
15
|
+
'.mjs',
|
|
16
|
+
'.js',
|
|
17
|
+
'.ts',
|
|
18
|
+
'.tsx',
|
|
19
|
+
'.swift',
|
|
20
|
+
'.py',
|
|
21
|
+
'.go',
|
|
22
|
+
'.java',
|
|
23
|
+
'.rs',
|
|
24
|
+
'.md',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} raw: raw File Scope path or glob from PROMPT.md
|
|
29
|
+
* @returns {string|null}
|
|
30
|
+
*/
|
|
31
|
+
export function normalizeFileScopePath(raw) {
|
|
32
|
+
let s = String(raw ?? '').trim();
|
|
33
|
+
if (!s) return null;
|
|
34
|
+
|
|
35
|
+
s = s.replace(/\\/g, '/');
|
|
36
|
+
if (s.startsWith('./')) s = s.slice(2);
|
|
37
|
+
s = s.replace(/\/+$/g, '');
|
|
38
|
+
|
|
39
|
+
// Directory-style globs are treated as path prefixes for overlap checks.
|
|
40
|
+
if (s.endsWith('/*')) s = s.slice(0, -2);
|
|
41
|
+
if (s.endsWith('/**')) s = s.slice(0, -3);
|
|
42
|
+
|
|
43
|
+
return s || null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* True when normalized paths share a directory prefix or are equal.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} a
|
|
50
|
+
* @param {string} b
|
|
51
|
+
* @returns {boolean}
|
|
52
|
+
*/
|
|
53
|
+
export function pathsOverlap(a, b) {
|
|
54
|
+
if (a === b) return true;
|
|
55
|
+
// Only treat directory-prefix matches; a file prefix without a slash should not match.
|
|
56
|
+
return a.startsWith(b + '/') || b.startsWith(a + '/');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {string} raw
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
function normalizeScopeEntry(raw) {
|
|
64
|
+
return String(raw ?? '').trim().replace(/\\/g, '/').replace(/^\.\/+/, '');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Expand a single File Scope entry into concrete paths for glob overlap probes.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} raw
|
|
71
|
+
* @returns {string[]}
|
|
72
|
+
*/
|
|
73
|
+
function expandScopeEntryProbes(raw) {
|
|
74
|
+
const entry = normalizeScopeEntry(raw);
|
|
75
|
+
if (!entry) return [];
|
|
76
|
+
|
|
77
|
+
/** @type {Set<string>} */
|
|
78
|
+
const probes = new Set([entry]);
|
|
79
|
+
|
|
80
|
+
if (entry.endsWith('/**')) {
|
|
81
|
+
const base = entry.slice(0, -3);
|
|
82
|
+
for (const ext of SYNTHETIC_PROBE_EXTENSIONS) {
|
|
83
|
+
probes.add(`${base}/${PROBE_BASENAME}/${PROBE_BASENAME}${ext}`);
|
|
84
|
+
probes.add(`${base}/nested/${PROBE_BASENAME}${ext}`);
|
|
85
|
+
}
|
|
86
|
+
return [...probes];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (entry.endsWith('/*')) {
|
|
90
|
+
const base = entry.slice(0, -2);
|
|
91
|
+
for (const ext of SYNTHETIC_PROBE_EXTENSIONS) {
|
|
92
|
+
probes.add(`${base}/${PROBE_BASENAME}${ext}`);
|
|
93
|
+
}
|
|
94
|
+
return [...probes];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (/[*?[\\]/.test(entry)) {
|
|
98
|
+
probes.add(entry.replace(/\*/g, PROBE_BASENAME).replace(/\?/g, 'x'));
|
|
99
|
+
return [...probes];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const baseName = entry.split('/').pop() ?? '';
|
|
103
|
+
if (!/\.\w[\w\d]*$/.test(baseName)) {
|
|
104
|
+
for (const ext of SYNTHETIC_PROBE_EXTENSIONS) {
|
|
105
|
+
probes.add(`${entry}/${PROBE_BASENAME}${ext}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return [...probes];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {string[]} fileScope
|
|
114
|
+
* @returns {string[]}
|
|
115
|
+
*/
|
|
116
|
+
export function expandFileScopeProbes(fileScope) {
|
|
117
|
+
if (!Array.isArray(fileScope) || fileScope.length === 0) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @type {Set<string>} */
|
|
122
|
+
const probes = new Set();
|
|
123
|
+
|
|
124
|
+
for (const raw of fileScope) {
|
|
125
|
+
for (const probe of expandScopeEntryProbes(raw)) {
|
|
126
|
+
probes.add(probe);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return [...probes];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @param {string} pattern
|
|
135
|
+
* @returns {boolean}
|
|
136
|
+
*/
|
|
137
|
+
function patternHasGlobMeta(pattern) {
|
|
138
|
+
return /[*?[\\]/.test(pattern);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* True when two File Scope patterns could match the same repository path.
|
|
143
|
+
*
|
|
144
|
+
* @param {string} leftPattern
|
|
145
|
+
* @param {string} rightPattern
|
|
146
|
+
* @returns {boolean}
|
|
147
|
+
*/
|
|
148
|
+
export function fileScopePatternsOverlap(leftPattern, rightPattern) {
|
|
149
|
+
const leftEntry = normalizeScopeEntry(leftPattern);
|
|
150
|
+
const rightEntry = normalizeScopeEntry(rightPattern);
|
|
151
|
+
if (!leftEntry || !rightEntry) return false;
|
|
152
|
+
|
|
153
|
+
const leftPrefix = normalizeFileScopePath(leftPattern);
|
|
154
|
+
const rightPrefix = normalizeFileScopePath(rightPattern);
|
|
155
|
+
if (leftPrefix && rightPrefix && pathsOverlap(leftPrefix, rightPrefix)) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const leftProbes = expandScopeEntryProbes(leftPattern);
|
|
160
|
+
const rightProbes = expandScopeEntryProbes(rightPattern);
|
|
161
|
+
const matchOpts = { dot: true };
|
|
162
|
+
|
|
163
|
+
for (const probe of leftProbes) {
|
|
164
|
+
if (micromatch.isMatch(probe, rightEntry, matchOpts)) return true;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
for (const probe of rightProbes) {
|
|
168
|
+
if (micromatch.isMatch(probe, leftEntry, matchOpts)) return true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!patternHasGlobMeta(leftEntry) && micromatch.isMatch(leftEntry, rightEntry, matchOpts)) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!patternHasGlobMeta(rightEntry) && micromatch.isMatch(rightEntry, leftEntry, matchOpts)) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* @param {string[]} left
|
|
184
|
+
* @param {string[]} right
|
|
185
|
+
* @returns {boolean}
|
|
186
|
+
*/
|
|
187
|
+
export function fileScopesOverlap(left, right) {
|
|
188
|
+
const leftPatterns = Array.isArray(left) ? left : [];
|
|
189
|
+
const rightPatterns = Array.isArray(right) ? right : [];
|
|
190
|
+
|
|
191
|
+
for (const a of leftPatterns) {
|
|
192
|
+
for (const b of rightPatterns) {
|
|
193
|
+
if (fileScopePatternsOverlap(a, b)) return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @typedef {{
|
|
202
|
+
* waveIndex: number,
|
|
203
|
+
* taskA: string,
|
|
204
|
+
* taskB: string,
|
|
205
|
+
* scopesA: string[],
|
|
206
|
+
* scopesB: string[],
|
|
207
|
+
* }} FileScopeOverlapPair
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Find overlapping File Scope pairs among tasks scheduled in the same wave.
|
|
212
|
+
*
|
|
213
|
+
* @param {string[][]} waves
|
|
214
|
+
* @param {Record<string, { fileScope?: string[] }>} tasksById
|
|
215
|
+
* @returns {FileScopeOverlapPair[]}
|
|
216
|
+
*/
|
|
217
|
+
export function findWaveFileScopeOverlaps(waves, tasksById) {
|
|
218
|
+
/** @type {FileScopeOverlapPair[]} */
|
|
219
|
+
const overlaps = [];
|
|
220
|
+
|
|
221
|
+
for (let waveIndex = 0; waveIndex < waves.length; waveIndex++) {
|
|
222
|
+
const waveTaskIds = waves[waveIndex];
|
|
223
|
+
if (waveTaskIds.length < 2) continue;
|
|
224
|
+
|
|
225
|
+
for (let i = 0; i < waveTaskIds.length; i++) {
|
|
226
|
+
for (let j = i + 1; j < waveTaskIds.length; j++) {
|
|
227
|
+
const taskA = waveTaskIds[i];
|
|
228
|
+
const taskB = waveTaskIds[j];
|
|
229
|
+
const scopesA = tasksById[taskA]?.fileScope ?? [];
|
|
230
|
+
const scopesB = tasksById[taskB]?.fileScope ?? [];
|
|
231
|
+
|
|
232
|
+
if (!fileScopesOverlap(scopesA, scopesB)) continue;
|
|
233
|
+
|
|
234
|
+
overlaps.push({
|
|
235
|
+
waveIndex,
|
|
236
|
+
taskA,
|
|
237
|
+
taskB,
|
|
238
|
+
scopesA: [...scopesA],
|
|
239
|
+
scopesB: [...scopesB],
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return overlaps;
|
|
246
|
+
}
|