@pcoliveira90/pdd 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,126 +1,126 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
- function ensureDir(filePath) {
5
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
6
- }
7
-
8
- function writeFile(filePath, content) {
9
- ensureDir(filePath);
10
- fs.writeFileSync(filePath, content, 'utf-8');
11
- }
12
-
13
- function slugify(value) {
14
- return value
15
- .toLowerCase()
16
- .replace(/[^a-z0-9]+/g, '-')
17
- .replace(/^-+|-+$/g, '')
18
- .slice(0, 48);
19
- }
20
-
21
- export function generatePatchArtifacts({ issue, baseDir = process.cwd() }) {
22
- const timestamp = Date.now();
23
- const changeId = `change-${timestamp}-${slugify(issue || 'update')}`;
24
- const changeDir = path.join(baseDir, 'changes', changeId);
25
-
26
- const files = [
27
- path.join('changes', changeId, 'delta-spec.md'),
28
- path.join('changes', changeId, 'patch-plan.md'),
29
- path.join('changes', changeId, 'verification-report.md')
30
- ];
31
-
32
- writeFile(
33
- path.join(changeDir, 'delta-spec.md'),
34
- `# Delta Spec
35
-
36
- ## Change ID
37
- ${changeId}
38
-
39
- ## Issue
40
- ${issue}
41
-
42
- ## Type
43
- bugfix | feature | refactor-safe | hotfix
44
-
45
- ## Context
46
-
47
- ## Current Behavior
48
-
49
- ## Expected Behavior
50
-
51
- ## Evidence
52
-
53
- ## Root Cause Hypothesis
54
-
55
- ## Impacted Areas
56
-
57
- ## Constraints
58
-
59
- ## Minimal Safe Delta
60
-
61
- ## Alternatives Considered
62
-
63
- ## Acceptance Criteria
64
-
65
- ## Verification Strategy
66
- `
67
- );
68
-
69
- writeFile(
70
- path.join(changeDir, 'patch-plan.md'),
71
- `# Patch Plan
72
-
73
- ## Change ID
74
- ${changeId}
75
-
76
- ## Issue
77
- ${issue}
78
-
79
- ## Files to Inspect
80
-
81
- ## Files to Change
82
-
83
- ## Execution Steps
84
- 1. Reproduce issue
85
- 2. Confirm root cause
86
- 3. Apply minimal change
87
- 4. Adjust tests
88
- 5. Run validations
89
-
90
- ## Regression Risks
91
-
92
- ## Rollback Strategy
93
- `
94
- );
95
-
96
- writeFile(
97
- path.join(changeDir, 'verification-report.md'),
98
- `# Verification Report
99
-
100
- ## Change ID
101
- ${changeId}
102
-
103
- ## Issue
104
- ${issue}
105
-
106
- ## Reproduction
107
-
108
- ## Changes Made
109
-
110
- ## Tests Run
111
-
112
- ## Manual Validation
113
-
114
- ## Residual Risks
115
-
116
- ## Final Status
117
- pending
118
- `
119
- );
120
-
121
- return {
122
- changeId,
123
- changeDir,
124
- files
125
- };
126
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ function ensureDir(filePath) {
5
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
6
+ }
7
+
8
+ function writeFile(filePath, content) {
9
+ ensureDir(filePath);
10
+ fs.writeFileSync(filePath, content, 'utf-8');
11
+ }
12
+
13
+ function slugify(value) {
14
+ return value
15
+ .toLowerCase()
16
+ .replace(/[^a-z0-9]+/g, '-')
17
+ .replace(/^-+|-+$/g, '')
18
+ .slice(0, 48);
19
+ }
20
+
21
+ export function generatePatchArtifacts({ issue, baseDir = process.cwd() }) {
22
+ const timestamp = Date.now();
23
+ const changeId = `change-${timestamp}-${slugify(issue || 'update')}`;
24
+ const changeDir = path.join(baseDir, 'changes', changeId);
25
+
26
+ const files = [
27
+ path.join('changes', changeId, 'delta-spec.md'),
28
+ path.join('changes', changeId, 'patch-plan.md'),
29
+ path.join('changes', changeId, 'verification-report.md')
30
+ ];
31
+
32
+ writeFile(
33
+ path.join(changeDir, 'delta-spec.md'),
34
+ `# Delta Spec
35
+
36
+ ## Change ID
37
+ ${changeId}
38
+
39
+ ## Issue
40
+ ${issue}
41
+
42
+ ## Type
43
+ bugfix | feature | refactor-safe | hotfix
44
+
45
+ ## Context
46
+
47
+ ## Current Behavior
48
+
49
+ ## Expected Behavior
50
+
51
+ ## Evidence
52
+
53
+ ## Root Cause Hypothesis
54
+
55
+ ## Impacted Areas
56
+
57
+ ## Constraints
58
+
59
+ ## Minimal Safe Delta
60
+
61
+ ## Alternatives Considered
62
+
63
+ ## Acceptance Criteria
64
+
65
+ ## Verification Strategy
66
+ `
67
+ );
68
+
69
+ writeFile(
70
+ path.join(changeDir, 'patch-plan.md'),
71
+ `# Patch Plan
72
+
73
+ ## Change ID
74
+ ${changeId}
75
+
76
+ ## Issue
77
+ ${issue}
78
+
79
+ ## Files to Inspect
80
+
81
+ ## Files to Change
82
+
83
+ ## Execution Steps
84
+ 1. Reproduce issue
85
+ 2. Confirm root cause
86
+ 3. Apply minimal change
87
+ 4. Adjust tests
88
+ 5. Run validations
89
+
90
+ ## Regression Risks
91
+
92
+ ## Rollback Strategy
93
+ `
94
+ );
95
+
96
+ writeFile(
97
+ path.join(changeDir, 'verification-report.md'),
98
+ `# Verification Report
99
+
100
+ ## Change ID
101
+ ${changeId}
102
+
103
+ ## Issue
104
+ ${issue}
105
+
106
+ ## Reproduction
107
+
108
+ ## Changes Made
109
+
110
+ ## Tests Run
111
+
112
+ ## Manual Validation
113
+
114
+ ## Residual Risks
115
+
116
+ ## Final Status
117
+ pending
118
+ `
119
+ );
120
+
121
+ return {
122
+ changeId,
123
+ changeDir,
124
+ files
125
+ };
126
+ }
@@ -1,21 +1,21 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { execSync } from 'child_process';
4
-
5
- function exec(command) {
6
- execSync(command, { stdio: 'inherit' });
7
- }
8
-
9
- export async function openPullRequest({ issue, changeId, changeDir }) {
10
- const branch = `pdd/${changeId}`;
11
- const title = `fix: ${issue}`;
12
-
13
- fs.writeFileSync(path.join(changeDir, 'pr-title.txt'), title);
14
- fs.writeFileSync(path.join(changeDir, 'pr-body.md'), issue);
15
-
16
- exec(`git checkout -b ${branch}`);
17
- exec('git add .');
18
- exec(`git commit -m "${title}"`);
19
-
20
- console.log('PR ready (use IDE to open)');
21
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { execSync } from 'child_process';
4
+
5
+ function exec(command) {
6
+ execSync(command, { stdio: 'inherit' });
7
+ }
8
+
9
+ export async function openPullRequest({ issue, changeId, changeDir }) {
10
+ const branch = `pdd/${changeId}`;
11
+ const title = `fix: ${issue}`;
12
+
13
+ fs.writeFileSync(path.join(changeDir, 'pr-title.txt'), title);
14
+ fs.writeFileSync(path.join(changeDir, 'pr-body.md'), issue);
15
+
16
+ exec(`git checkout -b ${branch}`);
17
+ exec('git add .');
18
+ exec(`git commit -m "${title}"`);
19
+
20
+ console.log('PR ready (use IDE to open)');
21
+ }
@@ -0,0 +1,301 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import readline from 'node:readline/promises';
4
+ import { stdin as input, stdout as output } from 'node:process';
5
+
6
+ const REVIEW_DIR = '.pdd/review';
7
+ const REVIEW_FILE = `${REVIEW_DIR}/project-review.md`;
8
+ const REVIEW_STATUS_FILE = `${REVIEW_DIR}/project-review-status.json`;
9
+ const REVIEW_FEEDBACK_FILE = `${REVIEW_DIR}/project-review-feedback.md`;
10
+
11
+ const IGNORED_DIRS = new Set([
12
+ '.git',
13
+ '.pdd',
14
+ 'node_modules',
15
+ 'dist',
16
+ 'build',
17
+ 'coverage',
18
+ '.next',
19
+ '.turbo',
20
+ '.idea',
21
+ '.vscode'
22
+ ]);
23
+
24
+ function ensureDir(filePath) {
25
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
26
+ }
27
+
28
+ function writeFile(baseDir, relativePath, content) {
29
+ const fullPath = path.join(baseDir, relativePath);
30
+ ensureDir(fullPath);
31
+ fs.writeFileSync(fullPath, content, 'utf-8');
32
+ }
33
+
34
+ function readJson(filePath) {
35
+ try {
36
+ return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
37
+ } catch {
38
+ return null;
39
+ }
40
+ }
41
+
42
+ function safeRead(filePath) {
43
+ try {
44
+ return fs.readFileSync(filePath, 'utf-8');
45
+ } catch {
46
+ return null;
47
+ }
48
+ }
49
+
50
+ function listFiles(rootDir, currentDir, depth = 0, maxDepth = 3) {
51
+ if (depth > maxDepth) return [];
52
+ const entries = fs.readdirSync(currentDir, { withFileTypes: true });
53
+ const files = [];
54
+
55
+ for (const entry of entries) {
56
+ const fullPath = path.join(currentDir, entry.name);
57
+ const relative = path.relative(rootDir, fullPath);
58
+
59
+ if (entry.isDirectory()) {
60
+ if (IGNORED_DIRS.has(entry.name)) continue;
61
+ files.push(...listFiles(rootDir, fullPath, depth + 1, maxDepth));
62
+ continue;
63
+ }
64
+
65
+ files.push(relative);
66
+ }
67
+
68
+ return files;
69
+ }
70
+
71
+ function detectTechnologies(baseDir, files) {
72
+ const tech = new Set();
73
+ const packageJsonPath = path.join(baseDir, 'package.json');
74
+ const pyprojectPath = path.join(baseDir, 'pyproject.toml');
75
+ const requirementsPath = path.join(baseDir, 'requirements.txt');
76
+ const goModPath = path.join(baseDir, 'go.mod');
77
+ const cargoPath = path.join(baseDir, 'Cargo.toml');
78
+
79
+ const packageJson = fs.existsSync(packageJsonPath) ? readJson(packageJsonPath) : null;
80
+ if (packageJson) {
81
+ tech.add('Node.js');
82
+ if (packageJson.type === 'module') {
83
+ tech.add('JavaScript (ESM)');
84
+ }
85
+ const deps = {
86
+ ...(packageJson.dependencies || {}),
87
+ ...(packageJson.devDependencies || {})
88
+ };
89
+
90
+ if (deps.typescript) tech.add('TypeScript');
91
+ if (deps.react) tech.add('React');
92
+ if (deps.next) tech.add('Next.js');
93
+ if (deps.express) tech.add('Express');
94
+ if (deps.vite) tech.add('Vite');
95
+ }
96
+
97
+ if (fs.existsSync(pyprojectPath) || fs.existsSync(requirementsPath)) tech.add('Python');
98
+ if (fs.existsSync(goModPath)) tech.add('Go');
99
+ if (fs.existsSync(cargoPath)) tech.add('Rust');
100
+
101
+ if (files.some(file => file.endsWith('.sql'))) tech.add('SQL');
102
+ if (files.some(file => file.endsWith('.md'))) tech.add('Documentation-heavy');
103
+ if (files.some(file => file.endsWith('.yml') || file.endsWith('.yaml'))) tech.add('CI/Automation');
104
+
105
+ return Array.from(tech);
106
+ }
107
+
108
+ function inferPurpose(baseDir) {
109
+ const packageJson = readJson(path.join(baseDir, 'package.json'));
110
+ if (packageJson?.description) {
111
+ return packageJson.description.trim();
112
+ }
113
+
114
+ const readme = safeRead(path.join(baseDir, 'README.md')) || safeRead(path.join(baseDir, 'README.pt-BR.md'));
115
+ if (!readme) {
116
+ return 'Project purpose not explicitly documented.';
117
+ }
118
+
119
+ const nonEmpty = readme
120
+ .split('\n')
121
+ .map(line => line.trim())
122
+ .find(line => line.length > 0 && !line.startsWith('#'));
123
+
124
+ return nonEmpty || 'Project purpose not explicitly documented.';
125
+ }
126
+
127
+ function pickCoreAreas(files) {
128
+ const rootFolders = new Map();
129
+ for (const file of files) {
130
+ const first = file.split(path.sep)[0];
131
+ if (!first || first.includes('.')) continue;
132
+ rootFolders.set(first, (rootFolders.get(first) || 0) + 1);
133
+ }
134
+
135
+ return Array.from(rootFolders.entries())
136
+ .sort((a, b) => b[1] - a[1])
137
+ .slice(0, 6)
138
+ .map(([name]) => name);
139
+ }
140
+
141
+ function pickKeyFiles(files) {
142
+ const preferred = [
143
+ 'README.md',
144
+ 'README.pt-BR.md',
145
+ 'package.json',
146
+ 'pyproject.toml',
147
+ 'go.mod',
148
+ 'Cargo.toml',
149
+ 'src/cli/index.js',
150
+ 'src/index.js',
151
+ 'src/main.js'
152
+ ];
153
+
154
+ const normalized = new Set(files.map(file => file.replace(/\\/g, '/')));
155
+ const first = preferred.filter(file => normalized.has(file));
156
+ const rest = files
157
+ .map(file => file.replace(/\\/g, '/'))
158
+ .filter(file => !first.includes(file))
159
+ .slice(0, 6);
160
+
161
+ return [...first, ...rest].slice(0, 10);
162
+ }
163
+
164
+ function buildReport({ purpose, technologies, coreAreas, keyFiles }) {
165
+ return [
166
+ '# Initial Project Review',
167
+ '',
168
+ '## What this project appears to be',
169
+ purpose,
170
+ '',
171
+ '## Main technologies',
172
+ ...(technologies.length > 0 ? technologies.map(item => `- ${item}`) : ['- Not detected']),
173
+ '',
174
+ '## Main areas in repository',
175
+ ...(coreAreas.length > 0 ? coreAreas.map(area => `- ${area}`) : ['- Not detected']),
176
+ '',
177
+ '## Key files inspected by review agent',
178
+ ...(keyFiles.length > 0 ? keyFiles.map(file => `- ${file}`) : ['- Not detected']),
179
+ '',
180
+ '## Reviewer checklist',
181
+ '- [ ] Is the project purpose correct?',
182
+ '- [ ] Are the technologies correct?',
183
+ '- [ ] Are there important modules missing in the summary?',
184
+ ''
185
+ ].join('\n');
186
+ }
187
+
188
+ function printReviewSummary({ purpose, technologies, coreAreas, reportPath }) {
189
+ console.log('');
190
+ console.log('🤖 Initial project review agent');
191
+ console.log(`- Purpose: ${purpose}`);
192
+ console.log(`- Technologies: ${technologies.length > 0 ? technologies.join(', ') : 'Not detected'}`);
193
+ console.log(`- Main areas: ${coreAreas.length > 0 ? coreAreas.join(', ') : 'Not detected'}`);
194
+ console.log(`- Full report: ./${reportPath}`);
195
+ }
196
+
197
+ function parseDecision(answer) {
198
+ const value = answer.trim().toLowerCase();
199
+ if (value === '' || value === 'ok' || value === 'y' || value === 'yes' || value === 's' || value === 'sim') {
200
+ return 'approved';
201
+ }
202
+ if (value === 'a' || value === 'ajustes' || value === 'ajuste' || value === 'adjust') {
203
+ return 'needs-adjustments';
204
+ }
205
+ if (value === 'skip' || value === 'n' || value === 'no') {
206
+ return 'skipped';
207
+ }
208
+ return null;
209
+ }
210
+
211
+ async function askReviewDecision() {
212
+ if (!process.stdin.isTTY) {
213
+ return { status: 'skipped', feedback: null };
214
+ }
215
+
216
+ const rl = readline.createInterface({ input, output });
217
+ try {
218
+ console.log('');
219
+ console.log('Project review generated. Please review now:');
220
+ console.log(`- ./${REVIEW_FILE}`);
221
+ console.log('');
222
+ console.log('Type:');
223
+ console.log(' Enter / ok -> approve');
224
+ console.log(' ajustes / a -> request adjustments');
225
+ console.log(' skip / n -> skip this step');
226
+ console.log('');
227
+
228
+ let answer = await rl.question('> ');
229
+ let status = parseDecision(answer);
230
+ while (status === null) {
231
+ console.log('Invalid option. Use Enter, ok, ajustes, a, skip, or n.');
232
+ answer = await rl.question('> ');
233
+ status = parseDecision(answer);
234
+ }
235
+
236
+ if (status !== 'needs-adjustments') {
237
+ return { status, feedback: null };
238
+ }
239
+
240
+ console.log('');
241
+ const feedback = await rl.question('Describe the adjustments you want in this review: ');
242
+ return { status, feedback: feedback.trim() || null };
243
+ } finally {
244
+ rl.close();
245
+ }
246
+ }
247
+
248
+ export async function runInitialProjectReviewAgent(baseDir = process.cwd(), argv = process.argv.slice(2)) {
249
+ const files = listFiles(baseDir, baseDir);
250
+ const purpose = inferPurpose(baseDir);
251
+ const technologies = detectTechnologies(baseDir, files);
252
+ const coreAreas = pickCoreAreas(files);
253
+ const keyFiles = pickKeyFiles(files);
254
+ const report = buildReport({ purpose, technologies, coreAreas, keyFiles });
255
+
256
+ writeFile(baseDir, REVIEW_FILE, report);
257
+ printReviewSummary({ purpose, technologies, coreAreas, reportPath: REVIEW_FILE });
258
+
259
+ if (argv.includes('-y') || argv.includes('--yes') || argv.includes('--no-review-prompt')) {
260
+ writeFile(
261
+ baseDir,
262
+ REVIEW_STATUS_FILE,
263
+ JSON.stringify({ status: 'approved', decidedAt: new Date().toISOString(), feedback: null }, null, 2) + '\n'
264
+ );
265
+ console.log(`✅ Initial review approved automatically: ${REVIEW_FILE}`);
266
+ return { status: 'approved', reportPath: REVIEW_FILE };
267
+ }
268
+
269
+ const decision = await askReviewDecision();
270
+ writeFile(
271
+ baseDir,
272
+ REVIEW_STATUS_FILE,
273
+ JSON.stringify(
274
+ {
275
+ status: decision.status,
276
+ decidedAt: new Date().toISOString(),
277
+ feedback: decision.feedback
278
+ },
279
+ null,
280
+ 2
281
+ ) + '\n'
282
+ );
283
+
284
+ if (decision.feedback) {
285
+ writeFile(
286
+ baseDir,
287
+ REVIEW_FEEDBACK_FILE,
288
+ `# Project Review Feedback\n\n${decision.feedback}\n`
289
+ );
290
+ }
291
+
292
+ if (decision.status === 'approved') {
293
+ console.log(`✅ Initial review approved: ${REVIEW_FILE}`);
294
+ } else if (decision.status === 'needs-adjustments') {
295
+ console.log(`📝 Adjustments requested and saved: ${REVIEW_FEEDBACK_FILE}`);
296
+ } else {
297
+ console.log('⏭️ Initial project review was skipped.');
298
+ }
299
+
300
+ return { status: decision.status, reportPath: REVIEW_FILE };
301
+ }