@danielstyle/featflow 0.0.0-stage → 0.6.4

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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +249 -2
  3. package/README.zh-CN.md +250 -0
  4. package/bin/featflow.mjs +116 -0
  5. package/package.json +63 -4
  6. package/skills/featflow/SKILL.md +127 -0
  7. package/skills/featflow-issues/SKILL.md +104 -0
  8. package/skills/featflow-regression/SKILL.md +63 -0
  9. package/src/agent-docs.mjs +143 -0
  10. package/src/changed-files.mjs +204 -0
  11. package/src/commands/cleanup.mjs +221 -0
  12. package/src/commands/codegraph.mjs +108 -0
  13. package/src/commands/doctor.mjs +303 -0
  14. package/src/commands/finish.mjs +249 -0
  15. package/src/commands/hook-cmd.mjs +78 -0
  16. package/src/commands/hooks-cmd.mjs +247 -0
  17. package/src/commands/init.mjs +477 -0
  18. package/src/commands/issue.mjs +434 -0
  19. package/src/commands/new.mjs +311 -0
  20. package/src/commands/plan-check.mjs +75 -0
  21. package/src/commands/policy-cmd.mjs +55 -0
  22. package/src/commands/pr-cleanup.mjs +194 -0
  23. package/src/commands/regression.mjs +206 -0
  24. package/src/commands/setup.mjs +343 -0
  25. package/src/commands/size-check.mjs +137 -0
  26. package/src/commands/skills.mjs +181 -0
  27. package/src/commands/status.mjs +100 -0
  28. package/src/commands/sync.mjs +421 -0
  29. package/src/commands/verify.mjs +206 -0
  30. package/src/commands/worktree.mjs +205 -0
  31. package/src/config.mjs +295 -0
  32. package/src/detect.mjs +314 -0
  33. package/src/git.mjs +331 -0
  34. package/src/hook-adapters.mjs +218 -0
  35. package/src/hooks-core.mjs +420 -0
  36. package/src/index.mjs +14 -0
  37. package/src/init-args.mjs +294 -0
  38. package/src/init-hooks.mjs +312 -0
  39. package/src/init-prompts.mjs +191 -0
  40. package/src/issues-helpers.mjs +463 -0
  41. package/src/issues.mjs +521 -0
  42. package/src/plan.mjs +202 -0
  43. package/src/policy.mjs +143 -0
  44. package/src/regression.mjs +338 -0
  45. package/src/size-check.mjs +203 -0
  46. package/src/skills-manager.mjs +348 -0
  47. package/src/tui-select.mjs +378 -0
  48. package/templates/adapters/antigravity/hooks.json +42 -0
  49. package/templates/adapters/claude-code/settings.json +46 -0
  50. package/templates/adapters/codex/config.toml +19 -0
  51. package/templates/adapters/cursor/hooks.json +27 -0
  52. package/templates/agents/codex/codebase-locator.toml +28 -0
  53. package/templates/agents/codex/implementation-scout.toml +28 -0
  54. package/templates/agents/codex/regression-guard-planner.toml +20 -0
  55. package/templates/features/README.md +56 -0
  56. package/templates/features/_program_template.md +79 -0
  57. package/templates/features/_template.md +120 -0
  58. package/templates/features/current-feature.md +18 -0
  59. package/templates/githooks/post-merge +10 -0
  60. package/templates/githooks/pre-commit +14 -0
  61. package/templates/githooks/pre-push +13 -0
  62. package/templates/regression/regression-ledger.yml +23 -0
  63. package/templates/snippets/AGENTS-workflow.md +21 -0
@@ -0,0 +1,181 @@
1
+ import { loadConfig } from "../config.mjs";
2
+ import { fail, findRepoRoot } from "../git.mjs";
3
+ import {
4
+ BUNDLED_SKILLS,
5
+ inspectSkillsStatus,
6
+ installGlobalSkills,
7
+ parseGlobalSkillAgents,
8
+ parseSkillNamesList,
9
+ uninstallGlobalSkills,
10
+ } from "../skills-manager.mjs";
11
+
12
+ const usage = `
13
+ Usage:
14
+ featflow skills install [options]
15
+ featflow skills status [options]
16
+ featflow skills uninstall [options]
17
+
18
+ Manages global featflow Agent Skills synced directly from the installed
19
+ @danielstyle/featflow package (offline, version-matched with your CLI).
20
+
21
+ Global skill targets (auto-detected by default):
22
+ - ~/.agents/skills/ (Standard: Antigravity, Codex, Cursor)
23
+ - ~/.claude/skills/ (Claude Code, when ~/.claude exists)
24
+ - ~/.codex/skills/ (OpenAI Codex, when ~/.codex exists)
25
+ - ~/.cursor/skills/ (Cursor, when ~/.cursor exists)
26
+
27
+ Options:
28
+ --only <skills> Comma-separated subset: ${BUNDLED_SKILLS.join(",")} (default: all)
29
+ --agents <list> Comma-separated targets: agents,claude-code,codex,cursor,all,auto (default: auto)
30
+ --dry-run Preview changes without writing or deleting files
31
+ --json Output machine-readable JSON
32
+ -h, --help Show this help
33
+ `.trim();
34
+
35
+ function parseArgs(argv) {
36
+ const action = argv[0] ?? "";
37
+ if (!action || action === "-h" || action === "--help") {
38
+ console.log(usage);
39
+ process.exit(0);
40
+ }
41
+ if (!["install", "sync", "status", "uninstall"].includes(action)) {
42
+ fail(`Unknown skills subcommand "${action}". Expected "install", "status", or "uninstall".`, usage);
43
+ }
44
+
45
+ const options = {
46
+ action: action === "sync" ? "install" : action,
47
+ skillNames: [...BUNDLED_SKILLS],
48
+ agents: null,
49
+ dryRun: false,
50
+ json: false,
51
+ };
52
+
53
+ for (let i = 1; i < argv.length; i += 1) {
54
+ const arg = argv[i];
55
+ if (arg === "-h" || arg === "--help") {
56
+ console.log(usage);
57
+ process.exit(0);
58
+ }
59
+ if (arg === "--only" || arg.startsWith("--only=")) {
60
+ const value = arg.startsWith("--only=") ? arg.slice("--only=".length) : (argv[++i] ?? "");
61
+ try {
62
+ options.skillNames = parseSkillNamesList(value);
63
+ } catch (error) {
64
+ fail(error instanceof Error ? error.message : String(error), usage);
65
+ }
66
+ continue;
67
+ }
68
+ if (arg === "--agents" || arg.startsWith("--agents=")) {
69
+ const value = arg.startsWith("--agents=") ? arg.slice("--agents=".length) : (argv[++i] ?? "");
70
+ try {
71
+ options.agents = parseGlobalSkillAgents(value);
72
+ } catch (error) {
73
+ fail(error instanceof Error ? error.message : String(error), usage);
74
+ }
75
+ continue;
76
+ }
77
+ if (arg === "--dry-run") {
78
+ options.dryRun = true;
79
+ continue;
80
+ }
81
+ if (arg === "--json") {
82
+ options.json = true;
83
+ continue;
84
+ }
85
+ fail(`Unknown option for skills ${action}: ${arg}`, usage);
86
+ }
87
+
88
+ return options;
89
+ }
90
+
91
+ export async function runSkills(argv) {
92
+ const options = parseArgs(argv);
93
+
94
+ if (options.action === "install") {
95
+ const result = installGlobalSkills({
96
+ skillNames: options.skillNames,
97
+ agents: options.agents,
98
+ dryRun: options.dryRun,
99
+ });
100
+ if (options.json) {
101
+ console.log(JSON.stringify(result, null, 2));
102
+ return;
103
+ }
104
+ const verb = options.dryRun ? "Would install/update" : "Synced";
105
+ if (result.installed.length === 0 && result.updated.length === 0) {
106
+ console.log(
107
+ `All global featflow skills are already up to date (${result.upToDate.length} file(s) across ${result.targets.map((t) => t.displayDir).join(", ")}).`,
108
+ );
109
+ return;
110
+ }
111
+ console.log(`${verb} global featflow skills:`);
112
+ for (const item of result.installed) {
113
+ console.log(` + ${item.display}`);
114
+ }
115
+ for (const item of result.updated) {
116
+ console.log(` ~ ${item.display}`);
117
+ }
118
+ if (result.upToDate.length > 0) {
119
+ console.log(` = ${result.upToDate.length} already up to date`);
120
+ }
121
+ return;
122
+ }
123
+
124
+ if (options.action === "uninstall") {
125
+ const result = uninstallGlobalSkills({
126
+ skillNames: options.skillNames,
127
+ agents: options.agents,
128
+ dryRun: options.dryRun,
129
+ });
130
+ if (options.json) {
131
+ console.log(JSON.stringify(result, null, 2));
132
+ return;
133
+ }
134
+ if (result.removed.length === 0) {
135
+ console.log("No global featflow skills found to remove.");
136
+ return;
137
+ }
138
+ const verb = options.dryRun ? "Would remove" : "Removed";
139
+ console.log(`${verb} global featflow skills:`);
140
+ for (const item of result.removed) {
141
+ console.log(` - ${item.display}`);
142
+ }
143
+ return;
144
+ }
145
+
146
+ const repoRoot = findRepoRoot(process.cwd());
147
+ let repoConfig = null;
148
+ if (repoRoot) {
149
+ try {
150
+ repoConfig = loadConfig(repoRoot).config;
151
+ } catch {
152
+ repoConfig = null;
153
+ }
154
+ }
155
+ const report = inspectSkillsStatus({
156
+ repoRoot,
157
+ skillNames: options.skillNames,
158
+ agents: options.agents,
159
+ });
160
+
161
+ if (options.json) {
162
+ console.log(JSON.stringify({ ...report, repoRoot, repoMode: repoConfig?.mode ?? null }, null, 2));
163
+ return;
164
+ }
165
+
166
+ console.log("featflow skills status");
167
+ console.log(`Global target dirs: ${report.targets.map((t) => t.displayDir).join(", ") || "(none)"}`);
168
+ for (const skill of report.skills) {
169
+ const globalSummary = skill.globalTargets.map((t) => `${t.agentId}=${t.status}`).join(", ");
170
+ const repoNote = skill.hasRepoCopy ? ` | repo=.agents/skills/${skill.name} (${skill.repoStatus})` : "";
171
+ console.log(` - ${skill.name}: ${globalSummary}${repoNote}`);
172
+ }
173
+ if (report.missingSkills.length > 0 || report.driftedSkills.length > 0) {
174
+ console.log("\nRun `featflow skills install` to install or refresh global skills.");
175
+ }
176
+ if (report.shadowedSkills.length > 0) {
177
+ console.log(
178
+ `\nNote: repo-level .agents/skills/ copies exist for [${report.shadowedSkills.join(", ")}] and take precedence over global skills in this repo. Remove them to follow global updates automatically, or run \`featflow sync\` to keep them updated.`,
179
+ );
180
+ }
181
+ }
@@ -0,0 +1,100 @@
1
+ import { loadConfig, resolveWorktreeParent } from "../config.mjs";
2
+ import {
3
+ currentBranch,
4
+ findWorktreeForBranch,
5
+ gitConfigGet,
6
+ gitWorktrees,
7
+ localBranchExists,
8
+ remoteBranchExists,
9
+ remoteExists,
10
+ remoteTrackingBranchExists,
11
+ } from "../git.mjs";
12
+ import { listFeatureFiles } from "../plan.mjs";
13
+ import { setupInstructions } from "../policy.mjs";
14
+
15
+ const usage = `
16
+ Usage:
17
+ featflow status
18
+
19
+ Shows featflow mode, repository/branch state, worktrees, active feature records, and next step.
20
+ `.trim();
21
+
22
+ function yesNo(value) {
23
+ return value ? "yes" : "no";
24
+ }
25
+
26
+ export async function runStatus(argv) {
27
+ if (argv.includes("-h") || argv.includes("--help")) {
28
+ console.log(usage);
29
+ return;
30
+ }
31
+
32
+ const { config, hasConfigFile, repoRoot } = loadConfig(process.cwd());
33
+ const current = currentBranch(repoRoot);
34
+ const worktrees = gitWorktrees(repoRoot);
35
+ const hooksPath = gitConfigGet("core.hooksPath", repoRoot);
36
+ const hasRemote = remoteExists(config.remote, repoRoot);
37
+
38
+ console.log("featflow status");
39
+ console.log(`Repository: ${repoRoot}`);
40
+ console.log(`Config file: ${hasConfigFile ? ".featflow.json" : "(defaults — run featflow init)"}`);
41
+ console.log(`Mode: ${config.mode}${config.mode === "trunk" ? ` (finishTarget: ${config.finishTarget})` : ""}`);
42
+ console.log(`Current branch: ${current || "(detached)"}`);
43
+ console.log(`Protected branches: ${config.protectedBranches.join(", ") || "(none)"}`);
44
+ console.log(`Worktree parent: ${resolveWorktreeParent(repoRoot, config)}`);
45
+ console.log(`Git hooks path: ${hooksPath || "(not configured)"}`);
46
+
47
+ const activeFeatures = [
48
+ ...listFeatureFiles(repoRoot, config.featuresDir, "active"),
49
+ ...(config.featuresDir === ".featflow/features" ? [] : listFeatureFiles(repoRoot, ".featflow/features", "active")),
50
+ ];
51
+ const pausedFeatures = [
52
+ ...listFeatureFiles(repoRoot, config.featuresDir, "paused"),
53
+ ...(config.featuresDir === ".featflow/features" ? [] : listFeatureFiles(repoRoot, ".featflow/features", "paused")),
54
+ ];
55
+ console.log(`Active feature records: ${activeFeatures.length > 0 ? activeFeatures.join(", ") : "(none)"}`);
56
+ if (pausedFeatures.length > 0) {
57
+ console.log(`Paused feature records: ${pausedFeatures.join(", ")}`);
58
+ }
59
+
60
+ let targetBranch = config.baseBranch;
61
+ if (config.mode === "developer-branch") {
62
+ const devBranch = gitConfigGet(config.developerBranchConfigKey, repoRoot);
63
+ console.log(`Configured developer branch: ${devBranch || "(not configured)"}`);
64
+ if (hasRemote) {
65
+ console.log(
66
+ `Remote ${config.remote}/${config.integrationBranch}: ${yesNo(
67
+ remoteBranchExists(config.remote, config.integrationBranch, repoRoot),
68
+ )}`,
69
+ );
70
+ }
71
+ if (!devBranch) {
72
+ console.log("");
73
+ console.log(setupInstructions(config));
74
+ return;
75
+ }
76
+ targetBranch = devBranch;
77
+ console.log(`Local developer branch: ${yesNo(localBranchExists(devBranch, repoRoot))}`);
78
+ if (hasRemote) {
79
+ console.log(`Tracking ref ${config.remote}/${devBranch}: ${yesNo(remoteTrackingBranchExists(config.remote, devBranch, repoRoot))}`);
80
+ }
81
+ const devWt = findWorktreeForBranch(worktrees, devBranch);
82
+ console.log(`Developer worktree: ${devWt?.path ?? "(not checked out)"}`);
83
+ }
84
+
85
+ console.log(`Worktrees (${worktrees.length}):`);
86
+ for (const wt of worktrees) {
87
+ const label = wt.branch ? wt.branch.replace(/^refs\/heads\//, "") : "(detached)";
88
+ console.log(` - ${wt.path} [${label}]`);
89
+ }
90
+
91
+ console.log("");
92
+ const protectedSet = new Set(config.protectedBranches);
93
+ if (protectedSet.has(current)) {
94
+ console.log(`Next step: create a task worktree from ${targetBranch} before editing tracked files.`);
95
+ } else if (current === targetBranch) {
96
+ console.log("Next step: create a typed task branch/worktree for the next task with `featflow worktree`.");
97
+ } else {
98
+ console.log(`Next step: finish this task with \`featflow finish ${current || "<task-branch>"}\`.`);
99
+ }
100
+ }
@@ -0,0 +1,421 @@
1
+ import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
2
+ import path from "node:path";
3
+
4
+ import {
5
+ findManagedAgentDocs,
6
+ inspectAgentDocBlock,
7
+ renderAgentWorkflowBlock,
8
+ resolveAgentDocTargets,
9
+ upsertAgentDocBlock,
10
+ } from "../agent-docs.mjs";
11
+ import { loadConfig, packageRoot } from "../config.mjs";
12
+ import { fail, findRepoRoot } from "../git.mjs";
13
+ import { configureGitHooksSafely } from "./hooks-cmd.mjs";
14
+
15
+ const VALID_GROUPS = new Set(["skill", "issues-skill", "regression-skill", "templates", "githooks", "agent-docs"]);
16
+
17
+ const usage = `
18
+ Usage:
19
+ featflow sync [options]
20
+
21
+ Synchronizes featflow-managed files (Skill, optional featflow-issues / featflow-regression Skills,
22
+ feature templates, .githooks, and managed AGENTS.md / CLAUDE.md workflow blocks) from the installed
23
+ featflow package into the current repository without touching .featflow.json, active/done
24
+ feature records, or custom content outside <!-- FEATFLOW_START --> markers.
25
+
26
+ Options:
27
+ --check Verify managed files match the installed featflow version (exit 1 on drift).
28
+ --dry-run Print files that would be created or updated without writing.
29
+ --only <groups> Comma-separated subset: skill,issues-skill,regression-skill,templates,githooks,agent-docs.
30
+ --with-repo-skills Include repo-embedded .agents/skills/ files even if not present yet.
31
+ --with-issues-skill Include .agents/skills/featflow-issues/SKILL.md even if issues is not configured yet.
32
+ --skip-issues-skill Skip .agents/skills/featflow-issues/SKILL.md even if issues is configured.
33
+ --with-regression-skill Include .agents/skills/featflow-regression/SKILL.md.
34
+ --skip-regression-skill Skip .agents/skills/featflow-regression/SKILL.md even if present.
35
+ --with-githooks Include .githooks even if the directory does not exist yet.
36
+ --skip-githooks Skip .githooks even if the directory exists.
37
+ --with-agent-docs Include AGENTS.md / CLAUDE.md even if no managed block exists yet.
38
+ --skip-agent-docs Skip AGENTS.md / CLAUDE.md even if a managed block exists.
39
+ --json Output machine-readable JSON.
40
+ -h, --help Show this help.
41
+ `.trim();
42
+
43
+ function parseArgs(argv) {
44
+ const options = {
45
+ check: false,
46
+ dryRun: false,
47
+ only: null,
48
+ withRepoSkills: false,
49
+ withIssuesSkill: false,
50
+ skipIssuesSkill: false,
51
+ withRegressionSkill: false,
52
+ skipRegressionSkill: false,
53
+ withGithooks: false,
54
+ skipGithooks: false,
55
+ withAgentDocs: false,
56
+ skipAgentDocs: false,
57
+ json: false,
58
+ };
59
+
60
+ for (let i = 0; i < argv.length; i += 1) {
61
+ const arg = argv[i];
62
+ if (arg === "-h" || arg === "--help") {
63
+ console.log(usage);
64
+ process.exit(0);
65
+ }
66
+ if (arg === "--check") {
67
+ options.check = true;
68
+ continue;
69
+ }
70
+ if (arg === "--dry-run") {
71
+ options.dryRun = true;
72
+ continue;
73
+ }
74
+ if (arg === "--with-repo-skills") {
75
+ options.withRepoSkills = true;
76
+ continue;
77
+ }
78
+ if (arg === "--with-issues-skill") {
79
+ options.withIssuesSkill = true;
80
+ continue;
81
+ }
82
+ if (arg === "--skip-issues-skill") {
83
+ options.skipIssuesSkill = true;
84
+ continue;
85
+ }
86
+ if (arg === "--with-regression-skill") {
87
+ options.withRegressionSkill = true;
88
+ continue;
89
+ }
90
+ if (arg === "--skip-regression-skill") {
91
+ options.skipRegressionSkill = true;
92
+ continue;
93
+ }
94
+ if (arg === "--with-githooks") {
95
+ options.withGithooks = true;
96
+ continue;
97
+ }
98
+ if (arg === "--skip-githooks") {
99
+ options.skipGithooks = true;
100
+ continue;
101
+ }
102
+ if (arg === "--with-agent-docs") {
103
+ options.withAgentDocs = true;
104
+ continue;
105
+ }
106
+ if (arg === "--skip-agent-docs") {
107
+ options.skipAgentDocs = true;
108
+ continue;
109
+ }
110
+ if (arg === "--json") {
111
+ options.json = true;
112
+ continue;
113
+ }
114
+ if (arg === "--only") {
115
+ options.only = argv[++i] ?? "";
116
+ continue;
117
+ }
118
+ if (arg.startsWith("--only=")) {
119
+ options.only = arg.slice("--only=".length);
120
+ continue;
121
+ }
122
+ fail(`Unknown option: ${arg}`, usage);
123
+ }
124
+
125
+ return options;
126
+ }
127
+
128
+ function normalizeLineEndings(content) {
129
+ return content.replace(/\r\n/g, "\n");
130
+ }
131
+
132
+ function resolveGroups(options, repoRoot, config) {
133
+ if (options.only !== null) {
134
+ const requested = options.only
135
+ .split(",")
136
+ .map((item) => item.trim())
137
+ .filter(Boolean);
138
+ if (requested.length === 0) {
139
+ fail("--only requires at least one group (skill, issues-skill, regression-skill, templates, githooks, agent-docs).", usage);
140
+ }
141
+ for (const group of requested) {
142
+ if (!VALID_GROUPS.has(group)) {
143
+ fail(`Unknown --only group "${group}". Valid groups: skill, issues-skill, regression-skill, templates, githooks, agent-docs.`, usage);
144
+ }
145
+ }
146
+ return new Set(requested);
147
+ }
148
+
149
+ const hasRepoSkill = existsSync(path.join(repoRoot, ".agents", "skills", "featflow", "SKILL.md"));
150
+ const hasIssuesSkill = existsSync(path.join(repoRoot, ".agents", "skills", "featflow-issues", "SKILL.md"));
151
+ const hasRegressionSkill = existsSync(path.join(repoRoot, ".agents", "skills", "featflow-regression", "SKILL.md"));
152
+ const hasRepoTemplates =
153
+ existsSync(path.join(repoRoot, config.featuresDir, "_template.md")) ||
154
+ existsSync(path.join(repoRoot, config.featuresDir, "README.md")) ||
155
+ existsSync(path.join(repoRoot, config.featuresDir, "_program_template.md"));
156
+ const hasGithooksDir = existsSync(path.join(repoRoot, ".githooks"));
157
+ const managedAgentDocs = findManagedAgentDocs(repoRoot);
158
+ const includeRepoSkills = options.withRepoSkills || hasRepoSkill;
159
+
160
+ const groups = new Set();
161
+ if (includeRepoSkills) {
162
+ groups.add("skill");
163
+ }
164
+ if (hasRepoTemplates) {
165
+ groups.add("templates");
166
+ }
167
+ if (
168
+ !options.skipIssuesSkill &&
169
+ (options.withIssuesSkill || hasIssuesSkill || (includeRepoSkills && Boolean(config.issues)))
170
+ ) {
171
+ groups.add("issues-skill");
172
+ }
173
+ if (
174
+ !options.skipRegressionSkill &&
175
+ (options.withRegressionSkill || hasRegressionSkill || (includeRepoSkills && Boolean(config.regression)))
176
+ ) {
177
+ groups.add("regression-skill");
178
+ }
179
+ if (!options.skipGithooks && (options.withGithooks || hasGithooksDir)) {
180
+ groups.add("githooks");
181
+ }
182
+ if (!options.skipAgentDocs && (options.withAgentDocs || managedAgentDocs.length > 0)) {
183
+ groups.add("agent-docs");
184
+ }
185
+ return groups;
186
+ }
187
+
188
+ export function buildManagedFiles(repoRoot, config, groups) {
189
+ const files = [];
190
+
191
+ if (groups.has("skill")) {
192
+ files.push({
193
+ kind: "file",
194
+ group: "skill",
195
+ src: path.join(packageRoot, "skills", "featflow", "SKILL.md"),
196
+ dest: path.join(repoRoot, ".agents", "skills", "featflow", "SKILL.md"),
197
+ executable: false,
198
+ });
199
+ }
200
+
201
+ if (groups.has("issues-skill")) {
202
+ files.push({
203
+ kind: "file",
204
+ group: "issues-skill",
205
+ src: path.join(packageRoot, "skills", "featflow-issues", "SKILL.md"),
206
+ dest: path.join(repoRoot, ".agents", "skills", "featflow-issues", "SKILL.md"),
207
+ executable: false,
208
+ });
209
+ }
210
+
211
+ if (groups.has("regression-skill")) {
212
+ files.push({
213
+ kind: "file",
214
+ group: "regression-skill",
215
+ src: path.join(packageRoot, "skills", "featflow-regression", "SKILL.md"),
216
+ dest: path.join(repoRoot, ".agents", "skills", "featflow-regression", "SKILL.md"),
217
+ executable: false,
218
+ });
219
+ }
220
+
221
+ if (groups.has("templates")) {
222
+ const srcDir = path.join(packageRoot, "templates", "features");
223
+ const destDir = path.join(repoRoot, config.featuresDir);
224
+ for (const name of ["README.md", "_template.md", "_program_template.md"]) {
225
+ files.push({
226
+ kind: "file",
227
+ group: "templates",
228
+ src: path.join(srcDir, name),
229
+ dest: path.join(destDir, name),
230
+ executable: false,
231
+ });
232
+ }
233
+ }
234
+
235
+ if (groups.has("githooks")) {
236
+ const srcDir = path.join(packageRoot, "templates", "githooks");
237
+ const destDir = path.join(repoRoot, ".githooks");
238
+ for (const hook of ["pre-commit", "pre-push", "post-merge"]) {
239
+ files.push({
240
+ kind: "file",
241
+ group: "githooks",
242
+ src: path.join(srcDir, hook),
243
+ dest: path.join(destDir, hook),
244
+ executable: true,
245
+ });
246
+ }
247
+ }
248
+
249
+ if (groups.has("agent-docs")) {
250
+ const managed = findManagedAgentDocs(repoRoot);
251
+ const targets = managed.length > 0 ? managed : resolveAgentDocTargets(repoRoot);
252
+ for (const rel of targets) {
253
+ files.push({
254
+ kind: "agent-doc",
255
+ group: "agent-docs",
256
+ src: path.join(packageRoot, "templates", "snippets", "AGENTS-workflow.md"),
257
+ dest: path.resolve(repoRoot, rel),
258
+ executable: false,
259
+ });
260
+ }
261
+ }
262
+
263
+ return files;
264
+ }
265
+
266
+ export function inspectSyncState(repoRoot, config, groups) {
267
+ const managed = buildManagedFiles(repoRoot, config, groups);
268
+ const renderedBlock = groups.has("agent-docs") ? renderAgentWorkflowBlock() : "";
269
+ const missing = [];
270
+ const drifted = [];
271
+ const upToDate = [];
272
+
273
+ for (const entry of managed) {
274
+ const rel = path.relative(repoRoot, entry.dest).replaceAll("\\", "/");
275
+ if (entry.kind === "agent-doc") {
276
+ const check = inspectAgentDocBlock(entry.dest, renderedBlock);
277
+ if (check.status === "missing-file" || check.status === "missing-block") {
278
+ missing.push({ ...entry, rel });
279
+ } else if (check.status === "drifted") {
280
+ drifted.push({ ...entry, rel });
281
+ } else {
282
+ upToDate.push({ ...entry, rel });
283
+ }
284
+ continue;
285
+ }
286
+
287
+ if (!existsSync(entry.dest)) {
288
+ missing.push({ ...entry, rel });
289
+ continue;
290
+ }
291
+ const expected = normalizeLineEndings(readFileSync(entry.src, "utf8"));
292
+ const actual = normalizeLineEndings(readFileSync(entry.dest, "utf8"));
293
+ if (expected !== actual) {
294
+ drifted.push({ ...entry, rel });
295
+ } else {
296
+ upToDate.push({ ...entry, rel });
297
+ }
298
+ }
299
+
300
+ return {
301
+ ok: missing.length === 0 && drifted.length === 0,
302
+ missing,
303
+ drifted,
304
+ upToDate,
305
+ };
306
+ }
307
+
308
+ export async function runSync(argv) {
309
+ const options = parseArgs(argv);
310
+ const repoRoot = findRepoRoot(process.cwd());
311
+ if (!repoRoot) {
312
+ fail("Not inside a Git repository.");
313
+ }
314
+
315
+ const { config } = loadConfig(repoRoot);
316
+ const groups = resolveGroups(options, repoRoot, config);
317
+ const state = inspectSyncState(repoRoot, config, groups);
318
+
319
+ if (options.check) {
320
+ const summary = {
321
+ ok: state.ok,
322
+ groups: [...groups],
323
+ missing: state.missing.map((item) => item.rel),
324
+ drifted: state.drifted.map((item) => item.rel),
325
+ upToDate: state.upToDate.map((item) => item.rel),
326
+ };
327
+ if (options.json) {
328
+ console.log(JSON.stringify(summary, null, 2));
329
+ } else if (state.ok) {
330
+ console.log(`featflow sync --check passed (${state.upToDate.length} managed files up to date).`);
331
+ } else {
332
+ console.error("featflow sync --check found out-of-date managed files:");
333
+ for (const item of state.missing) {
334
+ console.error(` [missing] ${item.rel}`);
335
+ }
336
+ for (const item of state.drifted) {
337
+ console.error(` [drifted] ${item.rel}`);
338
+ }
339
+ console.error("\nRun `featflow sync` to update them (your .featflow.json will not be modified).");
340
+ }
341
+ if (!state.ok) {
342
+ process.exit(1);
343
+ }
344
+ return;
345
+ }
346
+
347
+ const pending = [...state.missing, ...state.drifted];
348
+ const created = [];
349
+ const updated = [];
350
+ const renderedBlock = groups.has("agent-docs") ? renderAgentWorkflowBlock() : "";
351
+
352
+ for (const item of pending) {
353
+ if (item.kind === "agent-doc") {
354
+ const result = upsertAgentDocBlock(item.dest, renderedBlock, { dryRun: options.dryRun });
355
+ if (result.action === "created") {
356
+ created.push(item.rel);
357
+ } else if (result.action === "updated") {
358
+ updated.push(item.rel);
359
+ }
360
+ continue;
361
+ }
362
+
363
+ const isNew = !existsSync(item.dest);
364
+ if (!options.dryRun) {
365
+ mkdirSync(path.dirname(item.dest), { recursive: true });
366
+ copyFileSync(item.src, item.dest);
367
+ if (item.executable) {
368
+ chmodSync(item.dest, 0o755);
369
+ }
370
+ }
371
+ if (isNew) {
372
+ created.push(item.rel);
373
+ } else {
374
+ updated.push(item.rel);
375
+ }
376
+ }
377
+
378
+ let hookResult = null;
379
+ if (options.withGithooks && !options.dryRun) {
380
+ hookResult = configureGitHooksSafely(repoRoot);
381
+ }
382
+
383
+ if (options.json) {
384
+ console.log(
385
+ JSON.stringify(
386
+ {
387
+ dryRun: options.dryRun,
388
+ groups: [...groups],
389
+ created,
390
+ updated,
391
+ upToDate: state.upToDate.map((item) => item.rel),
392
+ hookResult,
393
+ },
394
+ null,
395
+ 2,
396
+ ),
397
+ );
398
+ return;
399
+ }
400
+
401
+ if (created.length === 0 && updated.length === 0) {
402
+ console.log(`All ${state.upToDate.length} managed featflow files are already up to date.`);
403
+ } else {
404
+ const verb = options.dryRun ? "Would sync" : "Synced";
405
+ console.log(`${verb} featflow managed files (kept .featflow.json untouched):`);
406
+ for (const rel of created) {
407
+ console.log(` + ${rel}`);
408
+ }
409
+ for (const rel of updated) {
410
+ console.log(` ~ ${rel}`);
411
+ }
412
+ }
413
+
414
+ if (hookResult && !hookResult.configured) {
415
+ console.log("");
416
+ console.log(`Kept existing core.hooksPath=${hookResult.existingHooksPath} (did not overwrite).`);
417
+ } else if (hookResult?.configured) {
418
+ console.log("");
419
+ console.log("Git hooks enabled: core.hooksPath=.githooks");
420
+ }
421
+ }