@danielstyle/featflow 0.0.0-stage → 0.6.3
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/LICENSE +21 -0
- package/README.md +206 -2
- package/README.zh-CN.md +163 -0
- package/bin/featflow.mjs +113 -0
- package/package.json +62 -4
- package/skills/featflow/SKILL.md +127 -0
- package/skills/featflow-issues/SKILL.md +104 -0
- package/skills/featflow-regression/SKILL.md +63 -0
- package/src/agent-docs.mjs +143 -0
- package/src/changed-files.mjs +204 -0
- package/src/commands/cleanup.mjs +221 -0
- package/src/commands/codegraph.mjs +110 -0
- package/src/commands/doctor.mjs +265 -0
- package/src/commands/finish.mjs +249 -0
- package/src/commands/hook-cmd.mjs +78 -0
- package/src/commands/hooks-cmd.mjs +238 -0
- package/src/commands/init.mjs +382 -0
- package/src/commands/issue.mjs +434 -0
- package/src/commands/new.mjs +311 -0
- package/src/commands/plan-check.mjs +75 -0
- package/src/commands/policy-cmd.mjs +51 -0
- package/src/commands/pr-cleanup.mjs +194 -0
- package/src/commands/regression.mjs +206 -0
- package/src/commands/setup.mjs +343 -0
- package/src/commands/size-check.mjs +137 -0
- package/src/commands/status.mjs +100 -0
- package/src/commands/sync.mjs +407 -0
- package/src/commands/verify.mjs +206 -0
- package/src/commands/worktree.mjs +205 -0
- package/src/config.mjs +295 -0
- package/src/detect.mjs +314 -0
- package/src/git.mjs +331 -0
- package/src/hook-adapters.mjs +218 -0
- package/src/hooks-core.mjs +420 -0
- package/src/index.mjs +13 -0
- package/src/init-args.mjs +282 -0
- package/src/init-hooks.mjs +312 -0
- package/src/init-prompts.mjs +191 -0
- package/src/issues-helpers.mjs +463 -0
- package/src/issues.mjs +521 -0
- package/src/plan.mjs +202 -0
- package/src/policy.mjs +143 -0
- package/src/regression.mjs +338 -0
- package/src/size-check.mjs +203 -0
- package/src/tui-select.mjs +378 -0
- package/templates/adapters/antigravity/hooks.json +42 -0
- package/templates/adapters/claude-code/settings.json +46 -0
- package/templates/adapters/codex/config.toml +19 -0
- package/templates/adapters/cursor/hooks.json +27 -0
- package/templates/agents/codex/codebase-locator.toml +28 -0
- package/templates/agents/codex/implementation-scout.toml +28 -0
- package/templates/agents/codex/regression-guard-planner.toml +20 -0
- package/templates/features/README.md +56 -0
- package/templates/features/_program_template.md +79 -0
- package/templates/features/_template.md +120 -0
- package/templates/features/current-feature.md +18 -0
- package/templates/githooks/post-merge +7 -0
- package/templates/githooks/pre-commit +11 -0
- package/templates/githooks/pre-push +10 -0
- package/templates/regression/regression-ledger.yml +23 -0
- package/templates/snippets/AGENTS-workflow.md +21 -0
package/src/plan.mjs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const REQUIRED_PLAN_HEADINGS = [
|
|
5
|
+
"## Branch Plan",
|
|
6
|
+
"## Pre-Implementation Approval",
|
|
7
|
+
"## Requirements",
|
|
8
|
+
"## Acceptance Criteria",
|
|
9
|
+
"## Technical Approach",
|
|
10
|
+
"## Implementation Plan",
|
|
11
|
+
"## Affected Files / Modules",
|
|
12
|
+
"## Test Plan",
|
|
13
|
+
"## Risks",
|
|
14
|
+
"## Open Questions",
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const PLACEHOLDER_PATTERNS = [
|
|
18
|
+
{ label: "template placeholder (`Replace with ...`)", regex: /\bReplace with\b/ },
|
|
19
|
+
{ label: "template placeholder (`AI-drafted...`)", regex: /\bAI-drafted\b/ },
|
|
20
|
+
{ label: "template path placeholder (`path/to/expected...`)", regex: /path\/to\/expected/ },
|
|
21
|
+
{ label: "unresolved owner placeholder (`GIT_USER_NAME`)", regex: /\bGIT_USER_NAME\b/ },
|
|
22
|
+
{ label: "unresolved feature id placeholder", regex: /YYYY-MM-DD-HHMM-short-kebab-title/ },
|
|
23
|
+
{ label: "unresolved timestamp placeholder", regex: /YYYY-MM-DDTHH:MM:SS/ },
|
|
24
|
+
{ label: "unresolved template title (`Short Feature Title`)", regex: /^title:\s*Short Feature Title\s*$/m },
|
|
25
|
+
{
|
|
26
|
+
label: "unresolved approval enum (`pending | approved | skipped-by-user`)",
|
|
27
|
+
regex: /pending\s*\|\s*approved\s*\|\s*skipped-by-user/,
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Strip fenced code blocks and inline code spans so literal examples in
|
|
33
|
+
* documentation or date-formatting specs do not cause false positives.
|
|
34
|
+
*/
|
|
35
|
+
export function stripCodeSpans(markdown) {
|
|
36
|
+
const withoutFences = markdown.replace(/```[\s\S]*?```/g, "").replace(/~~~[\s\S]*?~~~/g, "");
|
|
37
|
+
return withoutFences.replace(/`[^`\n]+`/g, "");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function extractFrontmatter(markdown) {
|
|
41
|
+
const match = markdown.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/);
|
|
42
|
+
if (!match) {
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
const fields = {};
|
|
46
|
+
for (const line of match[1].split(/\r?\n/)) {
|
|
47
|
+
const colon = line.indexOf(":");
|
|
48
|
+
if (colon === -1) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const key = line.slice(0, colon).trim();
|
|
52
|
+
const value = line.slice(colon + 1).trim();
|
|
53
|
+
if (key) {
|
|
54
|
+
fields[key] = value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return fields;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function extractSection(markdown, heading) {
|
|
61
|
+
const escaped = heading.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
62
|
+
const startMatch = markdown.match(new RegExp(`^${escaped}\\s*$`, "m"));
|
|
63
|
+
if (!startMatch || startMatch.index === undefined) {
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
const rest = markdown.slice(startMatch.index + startMatch[0].length);
|
|
67
|
+
const nextHeading = rest.match(/^#{1,2}\s+/m);
|
|
68
|
+
return (nextHeading && nextHeading.index !== undefined ? rest.slice(0, nextHeading.index) : rest).trim();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function listFeatureFiles(repoRoot, featuresDir = "features", subdir = "active") {
|
|
72
|
+
const dir = path.resolve(repoRoot, featuresDir, subdir);
|
|
73
|
+
if (!existsSync(dir)) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
return readdirSync(dir)
|
|
77
|
+
.filter((name) => name.endsWith(".md") && !name.startsWith("_"))
|
|
78
|
+
.sort()
|
|
79
|
+
.map((name) => path.join(featuresDir, subdir, name));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function resolveActiveFeaturePath(repoRoot, featuresDir = "features") {
|
|
83
|
+
const dirsToCheck = featuresDir === ".featflow/features" ? [featuresDir] : [featuresDir, ".featflow/features"];
|
|
84
|
+
|
|
85
|
+
for (const dir of dirsToCheck) {
|
|
86
|
+
const pointerPath = path.resolve(repoRoot, dir, "current-feature.md");
|
|
87
|
+
if (existsSync(pointerPath)) {
|
|
88
|
+
const pointer = readFileSync(pointerPath, "utf8");
|
|
89
|
+
const match = pointer.match(/^active_feature:[ \t]*([^\r\n]*)$/m);
|
|
90
|
+
const rawValue = match ? match[1].trim() : "";
|
|
91
|
+
if (rawValue) {
|
|
92
|
+
return rawValue.startsWith(`${dir}/`) ? rawValue : `${dir}/${rawValue}`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const activeFiles = listFeatureFiles(repoRoot, dir, "active");
|
|
97
|
+
if (activeFiles.length === 1) {
|
|
98
|
+
return activeFiles[0];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function validateFeaturePlanContent(markdown, options = {}) {
|
|
106
|
+
const requireApproved = options.requireApproved ?? true;
|
|
107
|
+
const frontmatter = extractFrontmatter(markdown);
|
|
108
|
+
const prose = stripCodeSpans(markdown);
|
|
109
|
+
|
|
110
|
+
const missingHeadings = REQUIRED_PLAN_HEADINGS.filter((heading) => !markdown.includes(heading));
|
|
111
|
+
|
|
112
|
+
const placeholders = [];
|
|
113
|
+
for (const rule of PLACEHOLDER_PATTERNS) {
|
|
114
|
+
if (rule.regex.test(prose)) {
|
|
115
|
+
placeholders.push(rule.label);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (frontmatter.owner === "GIT_USER_NAME" || frontmatter.owner === "") {
|
|
119
|
+
placeholders.push("frontmatter `owner` is unresolved");
|
|
120
|
+
}
|
|
121
|
+
if (frontmatter.id && frontmatter.id.includes("YYYY-MM-DD")) {
|
|
122
|
+
placeholders.push("frontmatter `id` is unresolved");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let approvalPending = false;
|
|
126
|
+
if (requireApproved) {
|
|
127
|
+
const approvalSection = extractSection(markdown, "## Pre-Implementation Approval");
|
|
128
|
+
const statusMatch = approvalSection.match(/Approval status:\s*`?([a-z-]+)`?/i);
|
|
129
|
+
const status = statusMatch ? statusMatch[1].trim().toLowerCase() : "";
|
|
130
|
+
if (!status || status === "pending") {
|
|
131
|
+
approvalPending = true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const emptySections = [];
|
|
136
|
+
for (const heading of REQUIRED_PLAN_HEADINGS) {
|
|
137
|
+
if (missingHeadings.includes(heading)) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
const body = extractSection(markdown, heading);
|
|
141
|
+
if (!body) {
|
|
142
|
+
emptySections.push(heading);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const errors = [];
|
|
147
|
+
if (missingHeadings.length > 0) {
|
|
148
|
+
errors.push(`Missing required Plan Details headings: ${missingHeadings.join(", ")}`);
|
|
149
|
+
}
|
|
150
|
+
if (emptySections.length > 0) {
|
|
151
|
+
errors.push(`Empty Plan Details sections: ${emptySections.join(", ")}`);
|
|
152
|
+
}
|
|
153
|
+
if (placeholders.length > 0) {
|
|
154
|
+
errors.push(`Unresolved template placeholders: ${[...new Set(placeholders)].join("; ")}`);
|
|
155
|
+
}
|
|
156
|
+
if (approvalPending) {
|
|
157
|
+
errors.push("Pre-Implementation Approval status must be `approved` or `skipped-by-user` before implementation.");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
ok: errors.length === 0,
|
|
162
|
+
frontmatter,
|
|
163
|
+
missingHeadings,
|
|
164
|
+
emptySections,
|
|
165
|
+
placeholders: [...new Set(placeholders)],
|
|
166
|
+
approvalPending,
|
|
167
|
+
errors,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function validateActiveFeaturePlan(repoRoot, options = {}) {
|
|
172
|
+
const featuresDir = options.featuresDir ?? "features";
|
|
173
|
+
const targetRelPath = options.filePath ?? resolveActiveFeaturePath(repoRoot, featuresDir);
|
|
174
|
+
|
|
175
|
+
if (!targetRelPath) {
|
|
176
|
+
return {
|
|
177
|
+
ok: true,
|
|
178
|
+
skipped: true,
|
|
179
|
+
reason: "No active feature record found (lightweight change or idle checkout).",
|
|
180
|
+
activeFeature: null,
|
|
181
|
+
errors: [],
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const absPath = path.resolve(repoRoot, targetRelPath);
|
|
186
|
+
if (!existsSync(absPath)) {
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
skipped: false,
|
|
190
|
+
activeFeature: targetRelPath,
|
|
191
|
+
errors: [`Active feature record does not exist: ${targetRelPath}`],
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const content = readFileSync(absPath, "utf8");
|
|
196
|
+
const result = validateFeaturePlanContent(content, options);
|
|
197
|
+
return {
|
|
198
|
+
...result,
|
|
199
|
+
skipped: false,
|
|
200
|
+
activeFeature: targetRelPath,
|
|
201
|
+
};
|
|
202
|
+
}
|
package/src/policy.mjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import {
|
|
2
|
+
currentBranch,
|
|
3
|
+
gitConfigGet,
|
|
4
|
+
remoteExists,
|
|
5
|
+
remoteTrackingBranchExists,
|
|
6
|
+
runReadSoft,
|
|
7
|
+
} from "./git.mjs";
|
|
8
|
+
import { evaluateFileSizeBudget } from "./size-check.mjs";
|
|
9
|
+
|
|
10
|
+
export function setupInstructions(config) {
|
|
11
|
+
if (config.mode === "developer-branch") {
|
|
12
|
+
return [
|
|
13
|
+
"No developer branch workflow is configured for this checkout.",
|
|
14
|
+
"",
|
|
15
|
+
"Run:",
|
|
16
|
+
" featflow setup <your-branch-name>",
|
|
17
|
+
"",
|
|
18
|
+
"Example:",
|
|
19
|
+
" featflow setup alice",
|
|
20
|
+
].join("\n");
|
|
21
|
+
}
|
|
22
|
+
return [
|
|
23
|
+
"Featflow is not initialized in this repository yet.",
|
|
24
|
+
"",
|
|
25
|
+
"Run:",
|
|
26
|
+
" featflow init",
|
|
27
|
+
].join("\n");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isBootstrapAllowed(env = process.env) {
|
|
31
|
+
return env.FEATFLOW_BOOTSTRAP === "1" || env.FEATFLOW_INTERNAL_FINISH === "1";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function checkWorkflowReady(repoRoot, config, env = process.env) {
|
|
35
|
+
if (isBootstrapAllowed(env)) {
|
|
36
|
+
return { ok: true, developerBranch: gitConfigGet(config.developerBranchConfigKey, repoRoot) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (config.mode === "developer-branch") {
|
|
40
|
+
const devBranch = gitConfigGet(config.developerBranchConfigKey, repoRoot);
|
|
41
|
+
if (!devBranch) {
|
|
42
|
+
return { ok: false, message: setupInstructions(config) };
|
|
43
|
+
}
|
|
44
|
+
const hasRemote = remoteExists(config.remote, repoRoot);
|
|
45
|
+
if (config.requireRemote && hasRemote) {
|
|
46
|
+
if (!remoteTrackingBranchExists(config.remote, config.integrationBranch, repoRoot)) {
|
|
47
|
+
return {
|
|
48
|
+
ok: false,
|
|
49
|
+
message: `Missing ${config.remote}/${config.integrationBranch}. Fetch or bootstrap ${config.integrationBranch} before committing.`,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (!remoteTrackingBranchExists(config.remote, devBranch, repoRoot)) {
|
|
53
|
+
return {
|
|
54
|
+
ok: false,
|
|
55
|
+
message: `Missing ${config.remote}/${devBranch}. Run: featflow setup ${devBranch}`,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { ok: true, developerBranch: devBranch };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { ok: true, developerBranch: "" };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function evaluatePreCommit(repoRoot, config, env = process.env) {
|
|
66
|
+
const ready = checkWorkflowReady(repoRoot, config, env);
|
|
67
|
+
if (!ready.ok) {
|
|
68
|
+
return ready;
|
|
69
|
+
}
|
|
70
|
+
if (isBootstrapAllowed(env)) {
|
|
71
|
+
return { ok: true };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const branch = currentBranch(repoRoot);
|
|
75
|
+
const protectedSet = new Set(config.protectedBranches);
|
|
76
|
+
if (protectedSet.has(branch)) {
|
|
77
|
+
const baseHint =
|
|
78
|
+
config.mode === "developer-branch" && ready.developerBranch
|
|
79
|
+
? `your developer branch (${ready.developerBranch})`
|
|
80
|
+
: config.baseBranch;
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
message: `Refusing to commit directly on protected branch "${branch}". Create a task worktree from ${baseHint} using: featflow worktree <prefix>/<task>`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (config.fileSizeBudget?.enforceInPreCommit && env.FEATFLOW_SKIP_SIZE_CHECK !== "1") {
|
|
88
|
+
const stagedRes = runReadSoft(
|
|
89
|
+
"git",
|
|
90
|
+
["diff", "--cached", "--name-only", "--diff-filter=ACMR"],
|
|
91
|
+
repoRoot,
|
|
92
|
+
);
|
|
93
|
+
const stagedFiles =
|
|
94
|
+
stagedRes.status === 0
|
|
95
|
+
? stagedRes.stdout
|
|
96
|
+
.split(/\r?\n/)
|
|
97
|
+
.map((line) => line.trim())
|
|
98
|
+
.filter(Boolean)
|
|
99
|
+
: [];
|
|
100
|
+
if (stagedFiles.length > 0) {
|
|
101
|
+
const sizeRes = evaluateFileSizeBudget(repoRoot, config, { files: stagedFiles });
|
|
102
|
+
if (!sizeRes.ok) {
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
message: [
|
|
106
|
+
"Refusing to commit because staged file(s) exceed configured fileSizeBudget:",
|
|
107
|
+
...sizeRes.failures.map((f) => ` - ${f.path}: ${f.lines} lines exceeds ${f.label}`),
|
|
108
|
+
"Split oversized files into smaller focused modules (or bypass once with FEATFLOW_SKIP_SIZE_CHECK=1).",
|
|
109
|
+
].join("\n"),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return { ok: true };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function evaluatePrePush(repoRoot, config, stdinText, env = process.env) {
|
|
119
|
+
const ready = checkWorkflowReady(repoRoot, config, env);
|
|
120
|
+
if (!ready.ok) {
|
|
121
|
+
return ready;
|
|
122
|
+
}
|
|
123
|
+
if (isBootstrapAllowed(env)) {
|
|
124
|
+
return { ok: true };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const protectedSet = new Set(config.protectedBranches);
|
|
128
|
+
for (const line of String(stdinText ?? "").split(/\r?\n/).filter(Boolean)) {
|
|
129
|
+
const parts = line.trim().split(/\s+/);
|
|
130
|
+
const remoteRef = parts[2] ?? "";
|
|
131
|
+
const remoteBranch = remoteRef.startsWith("refs/heads/")
|
|
132
|
+
? remoteRef.slice("refs/heads/".length)
|
|
133
|
+
: "";
|
|
134
|
+
if (protectedSet.has(remoteBranch)) {
|
|
135
|
+
return {
|
|
136
|
+
ok: false,
|
|
137
|
+
message: `Refusing to push directly to protected branch "${remoteBranch}". Open a pull request or run featflow finish.`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return { ok: true };
|
|
143
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { changedFileSelection, matchesPattern, normalizeRelPath } from "./changed-files.mjs";
|
|
5
|
+
import { resolveRegressionConfig } from "./config.mjs";
|
|
6
|
+
|
|
7
|
+
export const VALID_REGRESSION_STATUSES = new Set(["guarded", "needs_guard", "retired", "superseded"]);
|
|
8
|
+
const INACTIVE_REGRESSION_STATUSES = new Set(["retired", "superseded"]);
|
|
9
|
+
|
|
10
|
+
function unquoteYamlScalar(raw) {
|
|
11
|
+
const trimmed = String(raw ?? "").trim();
|
|
12
|
+
if (trimmed.length >= 2 && trimmed.startsWith('"') && trimmed.endsWith('"')) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(trimmed);
|
|
15
|
+
} catch {
|
|
16
|
+
return trimmed.slice(1, -1);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (trimmed.length >= 2 && trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
20
|
+
return trimmed.slice(1, -1).replaceAll("''", "'");
|
|
21
|
+
}
|
|
22
|
+
return trimmed;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function parseFlowList(raw) {
|
|
26
|
+
const inner = raw.trim().slice(1, -1).trim();
|
|
27
|
+
if (!inner) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
const items = [];
|
|
31
|
+
let current = "";
|
|
32
|
+
let quote = "";
|
|
33
|
+
for (let i = 0; i < inner.length; i += 1) {
|
|
34
|
+
const ch = inner[i];
|
|
35
|
+
if (quote) {
|
|
36
|
+
current += ch;
|
|
37
|
+
if (ch === quote && inner[i - 1] !== "\\") {
|
|
38
|
+
quote = "";
|
|
39
|
+
}
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (ch === "'" || ch === '"') {
|
|
43
|
+
quote = ch;
|
|
44
|
+
current += ch;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (ch === ",") {
|
|
48
|
+
const val = unquoteYamlScalar(current);
|
|
49
|
+
if (val) items.push(val);
|
|
50
|
+
current = "";
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
current += ch;
|
|
54
|
+
}
|
|
55
|
+
const tail = unquoteYamlScalar(current);
|
|
56
|
+
if (tail) items.push(tail);
|
|
57
|
+
return items;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function foldBlockLines(lines, style) {
|
|
61
|
+
const isLiteral = style.startsWith("|");
|
|
62
|
+
const stripTrailing = style.endsWith("-");
|
|
63
|
+
if (isLiteral) {
|
|
64
|
+
const joined = lines.join("\n");
|
|
65
|
+
return stripTrailing ? joined.replace(/\n+$/, "") : joined;
|
|
66
|
+
}
|
|
67
|
+
const out = [];
|
|
68
|
+
let paragraph = [];
|
|
69
|
+
for (const line of lines) {
|
|
70
|
+
if (line.trim() === "") {
|
|
71
|
+
if (paragraph.length > 0) {
|
|
72
|
+
out.push(paragraph.join(" "));
|
|
73
|
+
paragraph = [];
|
|
74
|
+
}
|
|
75
|
+
out.push("");
|
|
76
|
+
} else {
|
|
77
|
+
paragraph.push(line.trim());
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (paragraph.length > 0) {
|
|
81
|
+
out.push(paragraph.join(" "));
|
|
82
|
+
}
|
|
83
|
+
const joined = out.join("\n");
|
|
84
|
+
return stripTrailing ? joined.replace(/\n+$/, "") : joined.trim();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function parseRegressionLedgerYaml(content) {
|
|
88
|
+
const lines = String(content ?? "").split(/\r?\n/);
|
|
89
|
+
const entries = [];
|
|
90
|
+
let current = null;
|
|
91
|
+
|
|
92
|
+
const isTopEntryStart = (line) => /^- [a-zA-Z0-9_-]+:\s*/.test(line);
|
|
93
|
+
const isFieldStart = (line) => /^ [a-zA-Z0-9_-]+:\s*/.test(line);
|
|
94
|
+
|
|
95
|
+
let i = 0;
|
|
96
|
+
while (i < lines.length) {
|
|
97
|
+
const line = lines[i];
|
|
98
|
+
const trimmed = line.trim();
|
|
99
|
+
if (!trimmed || trimmed.startsWith("#")) {
|
|
100
|
+
i += 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let key = "";
|
|
105
|
+
let rawValue = "";
|
|
106
|
+
|
|
107
|
+
const topMatch = line.match(/^- ([a-zA-Z0-9_-]+):\s*(.*)$/);
|
|
108
|
+
if (topMatch) {
|
|
109
|
+
current = {};
|
|
110
|
+
entries.push(current);
|
|
111
|
+
key = topMatch[1];
|
|
112
|
+
rawValue = topMatch[2];
|
|
113
|
+
} else {
|
|
114
|
+
const fieldMatch = line.match(/^ ([a-zA-Z0-9_-]+):\s*(.*)$/);
|
|
115
|
+
if (!fieldMatch || !current) {
|
|
116
|
+
i += 1;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
key = fieldMatch[1];
|
|
120
|
+
rawValue = fieldMatch[2];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
i += 1;
|
|
124
|
+
const valTrim = rawValue.trim();
|
|
125
|
+
|
|
126
|
+
if (/^[|>][+-]?$/.test(valTrim)) {
|
|
127
|
+
const blockLines = [];
|
|
128
|
+
let blockIndent = null;
|
|
129
|
+
while (i < lines.length) {
|
|
130
|
+
const next = lines[i];
|
|
131
|
+
if (next.trim() === "") {
|
|
132
|
+
blockLines.push("");
|
|
133
|
+
i += 1;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const indentMatch = next.match(/^(\s+)/);
|
|
137
|
+
const indent = indentMatch ? indentMatch[1].length : 0;
|
|
138
|
+
if (indent < 4) {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
if (blockIndent === null) {
|
|
142
|
+
blockIndent = indent;
|
|
143
|
+
}
|
|
144
|
+
blockLines.push(next.slice(Math.min(indent, blockIndent)));
|
|
145
|
+
i += 1;
|
|
146
|
+
}
|
|
147
|
+
current[key] = foldBlockLines(blockLines, valTrim);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (valTrim.startsWith("[") && valTrim.endsWith("]")) {
|
|
152
|
+
current[key] = parseFlowList(valTrim);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (valTrim === "") {
|
|
157
|
+
const listItems = [];
|
|
158
|
+
while (i < lines.length) {
|
|
159
|
+
const next = lines[i];
|
|
160
|
+
if (!next.trim() || next.trim().startsWith("#")) {
|
|
161
|
+
i += 1;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (isTopEntryStart(next) || isFieldStart(next)) {
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
const itemMatch = next.match(/^(\s+)-\s+(.*)$/);
|
|
168
|
+
if (itemMatch) {
|
|
169
|
+
const itemIndent = itemMatch[1].length;
|
|
170
|
+
let itemText = unquoteYamlScalar(itemMatch[2]);
|
|
171
|
+
i += 1;
|
|
172
|
+
while (i < lines.length) {
|
|
173
|
+
const cont = lines[i];
|
|
174
|
+
if (!cont.trim() || isTopEntryStart(cont) || isFieldStart(cont)) break;
|
|
175
|
+
const contIndent = (cont.match(/^(\s+)/)?.[1].length ?? 0);
|
|
176
|
+
if (contIndent <= itemIndent || /^\s+-\s+/.test(cont)) break;
|
|
177
|
+
itemText = `${itemText} ${cont.trim()}`;
|
|
178
|
+
i += 1;
|
|
179
|
+
}
|
|
180
|
+
listItems.push(itemText);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
current[key] = listItems;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
let scalar = unquoteYamlScalar(valTrim);
|
|
190
|
+
while (i < lines.length) {
|
|
191
|
+
const next = lines[i];
|
|
192
|
+
if (!next.trim() || next.trim().startsWith("#") || isTopEntryStart(next) || isFieldStart(next)) {
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
const indent = next.match(/^(\s+)/)?.[1].length ?? 0;
|
|
196
|
+
if (indent < 4) {
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
scalar = `${scalar} ${next.trim()}`;
|
|
200
|
+
i += 1;
|
|
201
|
+
}
|
|
202
|
+
current[key] = scalar;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return entries;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function loadRegressionLedger(repoRoot, config, explicitLedgerPath = "") {
|
|
209
|
+
const regConfig = resolveRegressionConfig(config);
|
|
210
|
+
const ledgerRel = explicitLedgerPath || regConfig.ledgerFile;
|
|
211
|
+
const absPath = path.resolve(repoRoot, ledgerRel);
|
|
212
|
+
if (!existsSync(absPath)) {
|
|
213
|
+
throw new Error(`Regression ledger not found: ${absPath}`);
|
|
214
|
+
}
|
|
215
|
+
const content = readFileSync(absPath, "utf8");
|
|
216
|
+
let entries;
|
|
217
|
+
if (absPath.endsWith(".json") || /^\s*[[{]/.test(content)) {
|
|
218
|
+
const parsed = JSON.parse(content);
|
|
219
|
+
entries = Array.isArray(parsed) ? parsed : Array.isArray(parsed?.entries) ? parsed.entries : null;
|
|
220
|
+
if (!entries) {
|
|
221
|
+
throw new Error(`Invalid JSON regression ledger at ${absPath}: expected an array or { "entries": [...] }.`);
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
entries = parseRegressionLedgerYaml(content);
|
|
225
|
+
}
|
|
226
|
+
return { ledgerFile: normalizeRelPath(path.relative(repoRoot, absPath)), absPath, entries };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function isConcreteGuardFile(guardValue) {
|
|
230
|
+
const trimmed = String(guardValue ?? "").trim();
|
|
231
|
+
if (!trimmed || /\s/.test(trimmed) || /[*?]/.test(trimmed)) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return trimmed.includes("/") || /\.[a-z0-9]+$/i.test(trimmed);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function validateRegressionLedger(repoRoot, config, options = {}) {
|
|
238
|
+
const { ledgerFile, entries } = loadRegressionLedger(repoRoot, config, options.ledgerFile);
|
|
239
|
+
const errors = [];
|
|
240
|
+
const warnings = [];
|
|
241
|
+
const seenIds = new Set();
|
|
242
|
+
|
|
243
|
+
for (let idx = 0; idx < entries.length; idx += 1) {
|
|
244
|
+
const entry = entries[idx];
|
|
245
|
+
const label = entry?.id ? `entry "${entry.id}"` : `entry #${idx + 1}`;
|
|
246
|
+
if (!entry?.id || typeof entry.id !== "string") {
|
|
247
|
+
errors.push(`${label}: missing required field "id".`);
|
|
248
|
+
} else if (seenIds.has(entry.id)) {
|
|
249
|
+
errors.push(`${label}: duplicate regression id "${entry.id}".`);
|
|
250
|
+
} else {
|
|
251
|
+
seenIds.add(entry.id);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!entry?.title) errors.push(`${label}: missing required field "title".`);
|
|
255
|
+
if (!VALID_REGRESSION_STATUSES.has(entry?.status)) {
|
|
256
|
+
errors.push(`${label}: invalid status "${entry?.status}" (expected guarded | needs_guard | retired | superseded).`);
|
|
257
|
+
}
|
|
258
|
+
if (!entry?.invariant) errors.push(`${label}: missing required field "invariant".`);
|
|
259
|
+
|
|
260
|
+
const files = Array.isArray(entry?.files) ? entry.files : [];
|
|
261
|
+
const guards = Array.isArray(entry?.guards) ? entry.guards : [];
|
|
262
|
+
|
|
263
|
+
if (!INACTIVE_REGRESSION_STATUSES.has(entry?.status) && files.length === 0) {
|
|
264
|
+
errors.push(`${label}: active entry must list at least one path in "files".`);
|
|
265
|
+
}
|
|
266
|
+
if (entry?.status === "guarded") {
|
|
267
|
+
if (guards.length === 0) {
|
|
268
|
+
errors.push(`${label}: status is "guarded" but "guards" is empty.`);
|
|
269
|
+
} else {
|
|
270
|
+
for (const guard of guards) {
|
|
271
|
+
if (isConcreteGuardFile(guard) && !existsSync(path.resolve(repoRoot, guard))) {
|
|
272
|
+
warnings.push(`${label}: guard path not found on disk: ${guard}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const strictFail = Boolean(options.strict) && warnings.length > 0;
|
|
280
|
+
return {
|
|
281
|
+
ok: errors.length === 0 && !strictFail,
|
|
282
|
+
ledgerFile,
|
|
283
|
+
entryCount: entries.length,
|
|
284
|
+
errors,
|
|
285
|
+
warnings,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function analyzeRegressionImpact(repoRoot, config, options = {}) {
|
|
290
|
+
const { ledgerFile, entries } = loadRegressionLedger(repoRoot, config, options.ledgerFile);
|
|
291
|
+
const selection = changedFileSelection(repoRoot, config, {
|
|
292
|
+
base: options.base,
|
|
293
|
+
head: options.head,
|
|
294
|
+
files: options.files,
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
const impacted = [];
|
|
298
|
+
const commandSet = new Set();
|
|
299
|
+
|
|
300
|
+
for (const entry of entries) {
|
|
301
|
+
if (!entry || INACTIVE_REGRESSION_STATUSES.has(entry.status)) continue;
|
|
302
|
+
const entryFiles = Array.isArray(entry.files) ? entry.files : [];
|
|
303
|
+
const matchedFiles = selection.files.filter((rel) => entryFiles.some((pat) => matchesPattern(rel, pat)));
|
|
304
|
+
if (matchedFiles.length === 0) continue;
|
|
305
|
+
|
|
306
|
+
const guards = Array.isArray(entry.guards) ? entry.guards : [];
|
|
307
|
+
const commands = Array.isArray(entry.commands) ? entry.commands : [];
|
|
308
|
+
const missingGuards = guards.filter((g) => isConcreteGuardFile(g) && !existsSync(path.resolve(repoRoot, g)));
|
|
309
|
+
|
|
310
|
+
for (const cmd of commands) {
|
|
311
|
+
if (cmd) commandSet.add(cmd);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
impacted.push({
|
|
315
|
+
id: entry.id,
|
|
316
|
+
title: entry.title ?? "",
|
|
317
|
+
status: entry.status ?? "unknown",
|
|
318
|
+
invariant: entry.invariant ?? "",
|
|
319
|
+
matchedFiles,
|
|
320
|
+
guards,
|
|
321
|
+
missingGuards,
|
|
322
|
+
commands,
|
|
323
|
+
ownerNotes: entry.owner_notes ?? "",
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const missingGuardEntries = impacted.filter((item) => item.status === "needs_guard" || item.missingGuards.length > 0);
|
|
328
|
+
return {
|
|
329
|
+
ok: missingGuardEntries.length === 0,
|
|
330
|
+
ledgerFile,
|
|
331
|
+
entryCount: entries.length,
|
|
332
|
+
selection,
|
|
333
|
+
changedFiles: selection.files,
|
|
334
|
+
impacted,
|
|
335
|
+
uniqueCommands: [...commandSet],
|
|
336
|
+
missingGuardEntries,
|
|
337
|
+
};
|
|
338
|
+
}
|