@paths.design/caws-cli 3.5.0 → 4.0.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/dist/budget-derivation.d.ts +41 -2
- package/dist/budget-derivation.d.ts.map +1 -1
- package/dist/budget-derivation.js +417 -30
- package/dist/commands/validate.d.ts +1 -0
- package/dist/commands/validate.d.ts.map +1 -1
- package/dist/commands/validate.js +105 -28
- package/dist/index.js +2 -0
- package/dist/policy/PolicyManager.d.ts +104 -0
- package/dist/policy/PolicyManager.d.ts.map +1 -0
- package/dist/policy/PolicyManager.js +399 -0
- package/dist/scaffold/cursor-hooks.d.ts.map +1 -1
- package/dist/scaffold/cursor-hooks.js +15 -0
- package/dist/spec/SpecFileManager.d.ts +146 -0
- package/dist/spec/SpecFileManager.d.ts.map +1 -0
- package/dist/spec/SpecFileManager.js +419 -0
- package/dist/validation/spec-validation.d.ts +14 -0
- package/dist/validation/spec-validation.d.ts.map +1 -1
- package/dist/validation/spec-validation.js +225 -13
- package/package.json +1 -1
- package/templates/.cursor/rules/01-claims-verification.mdc +144 -0
- package/templates/.cursor/rules/02-testing-standards.mdc +315 -0
- package/templates/.cursor/rules/03-infrastructure-standards.mdc +251 -0
- package/templates/.cursor/rules/04-documentation-integrity.mdc +291 -0
- package/templates/.cursor/rules/05-production-readiness-checklist.mdc +214 -0
- package/templates/.cursor/rules/README.md +64 -0
|
@@ -10,10 +10,14 @@ const yaml = require('js-yaml');
|
|
|
10
10
|
const chalk = require('chalk');
|
|
11
11
|
|
|
12
12
|
// Import validation functionality
|
|
13
|
-
const {
|
|
13
|
+
const {
|
|
14
|
+
validateWorkingSpecWithSuggestions,
|
|
15
|
+
getComplianceGrade,
|
|
16
|
+
} = require('../validation/spec-validation');
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* Validate command handler
|
|
20
|
+
* Enhanced with JSON output format support
|
|
17
21
|
* @param {string} specFile - Path to spec file
|
|
18
22
|
* @param {Object} options - Command options
|
|
19
23
|
*/
|
|
@@ -22,55 +26,128 @@ async function validateCommand(specFile, options) {
|
|
|
22
26
|
let specPath = specFile || path.join('.caws', 'working-spec.yaml');
|
|
23
27
|
|
|
24
28
|
if (!fs.existsSync(specPath)) {
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
if (options.format === 'json') {
|
|
30
|
+
console.log(
|
|
31
|
+
JSON.stringify(
|
|
32
|
+
{
|
|
33
|
+
passed: false,
|
|
34
|
+
verdict: 'fail',
|
|
35
|
+
errors: [
|
|
36
|
+
{
|
|
37
|
+
field: 'spec_file',
|
|
38
|
+
message: `Spec file not found: ${specPath}`,
|
|
39
|
+
suggestion: 'Run "caws init" first to create a working spec',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
null,
|
|
44
|
+
2
|
|
45
|
+
)
|
|
46
|
+
);
|
|
47
|
+
} else {
|
|
48
|
+
console.error(chalk.red(`❌ Spec file not found: ${specPath}`));
|
|
49
|
+
console.error(chalk.blue('💡 Run "caws init" first to create a working spec'));
|
|
50
|
+
}
|
|
27
51
|
process.exit(1);
|
|
28
52
|
}
|
|
29
53
|
|
|
30
54
|
const specContent = fs.readFileSync(specPath, 'utf8');
|
|
31
55
|
const spec = yaml.load(specContent);
|
|
32
56
|
|
|
33
|
-
|
|
57
|
+
if (options.format !== 'json') {
|
|
58
|
+
console.log(chalk.cyan('🔍 Validating CAWS working spec...'));
|
|
59
|
+
}
|
|
34
60
|
|
|
35
61
|
const result = validateWorkingSpecWithSuggestions(spec, {
|
|
36
62
|
autoFix: options.autoFix,
|
|
63
|
+
dryRun: options.dryRun,
|
|
37
64
|
suggestions: !options.quiet,
|
|
38
65
|
checkBudget: true,
|
|
39
66
|
projectRoot: path.dirname(specPath),
|
|
40
67
|
});
|
|
41
68
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
69
|
+
// Format output based on requested format
|
|
70
|
+
if (options.format === 'json') {
|
|
71
|
+
// Structured JSON output matching CAWSValidationResult
|
|
72
|
+
const jsonResult = {
|
|
73
|
+
passed: result.valid,
|
|
74
|
+
cawsVersion: '3.4.0',
|
|
75
|
+
timestamp: new Date().toISOString(),
|
|
76
|
+
verdict: result.valid ? 'pass' : 'fail',
|
|
77
|
+
spec: {
|
|
78
|
+
id: spec.id,
|
|
79
|
+
title: spec.title,
|
|
80
|
+
risk_tier: spec.risk_tier,
|
|
81
|
+
mode: spec.mode,
|
|
82
|
+
},
|
|
83
|
+
validation: {
|
|
84
|
+
errors: result.errors || [],
|
|
85
|
+
warnings: result.warnings || [],
|
|
86
|
+
fixes: result.fixes || [],
|
|
87
|
+
},
|
|
88
|
+
budgetCompliance: result.budget_check || null,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
console.log(JSON.stringify(jsonResult, null, 2));
|
|
92
|
+
|
|
93
|
+
if (!result.valid) {
|
|
94
|
+
process.exit(1);
|
|
50
95
|
}
|
|
51
96
|
} else {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
97
|
+
// Human-readable text output
|
|
98
|
+
if (result.valid) {
|
|
99
|
+
console.log(chalk.green('✅ Working spec validation passed'));
|
|
100
|
+
if (!options.quiet) {
|
|
101
|
+
console.log(chalk.gray(` Risk tier: ${spec.risk_tier}`));
|
|
102
|
+
console.log(chalk.gray(` Mode: ${spec.mode}`));
|
|
103
|
+
if (spec.title) {
|
|
104
|
+
console.log(chalk.gray(` Title: ${spec.title}`));
|
|
105
|
+
}
|
|
106
|
+
if (result.complianceScore !== undefined) {
|
|
107
|
+
const grade = getComplianceGrade(result.complianceScore);
|
|
108
|
+
const scorePercent = (result.complianceScore * 100).toFixed(0);
|
|
109
|
+
const scoreColor = result.complianceScore >= 0.9 ? 'green' : result.complianceScore >= 0.7 ? 'yellow' : 'red';
|
|
110
|
+
console.log(chalk[scoreColor](` Compliance: ${scorePercent}% (Grade ${grade})`));
|
|
111
|
+
}
|
|
59
112
|
}
|
|
60
|
-
}
|
|
113
|
+
} else {
|
|
114
|
+
console.log(chalk.red('❌ Working spec validation failed'));
|
|
61
115
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
116
|
+
// Show errors
|
|
117
|
+
result.errors.forEach((error, index) => {
|
|
118
|
+
console.log(` ${index + 1}. ${chalk.red(error.message)}`);
|
|
119
|
+
if (error.suggestion) {
|
|
120
|
+
console.log(` ${chalk.blue('💡 ' + error.suggestion)}`);
|
|
121
|
+
}
|
|
67
122
|
});
|
|
68
|
-
}
|
|
69
123
|
|
|
70
|
-
|
|
124
|
+
// Show warnings
|
|
125
|
+
if (result.warnings && result.warnings.length > 0) {
|
|
126
|
+
console.log(chalk.yellow('\n⚠️ Warnings:'));
|
|
127
|
+
result.warnings.forEach((warning, index) => {
|
|
128
|
+
console.log(` ${index + 1}. ${chalk.yellow(warning.message)}`);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
71
134
|
}
|
|
72
135
|
} catch (error) {
|
|
73
|
-
|
|
136
|
+
if (options.format === 'json') {
|
|
137
|
+
console.log(
|
|
138
|
+
JSON.stringify(
|
|
139
|
+
{
|
|
140
|
+
passed: false,
|
|
141
|
+
verdict: 'fail',
|
|
142
|
+
error: error.message,
|
|
143
|
+
},
|
|
144
|
+
null,
|
|
145
|
+
2
|
|
146
|
+
)
|
|
147
|
+
);
|
|
148
|
+
} else {
|
|
149
|
+
console.error(chalk.red('❌ Error during validation:'), error.message);
|
|
150
|
+
}
|
|
74
151
|
process.exit(1);
|
|
75
152
|
}
|
|
76
153
|
}
|
package/dist/index.js
CHANGED
|
@@ -117,6 +117,8 @@ program
|
|
|
117
117
|
.argument('[spec-file]', 'Path to working spec file (default: .caws/working-spec.yaml)')
|
|
118
118
|
.option('-q, --quiet', 'Suppress suggestions and warnings', false)
|
|
119
119
|
.option('--auto-fix', 'Automatically fix safe validation issues', false)
|
|
120
|
+
.option('--dry-run', 'Preview auto-fixes without applying them', false)
|
|
121
|
+
.option('--format <format>', 'Output format (text, json)', 'text')
|
|
120
122
|
.action(validateCommand);
|
|
121
123
|
|
|
122
124
|
// Status command
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Policy Manager - Handles policy loading with intelligent caching
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - TTL-based caching for performance
|
|
6
|
+
* - Graceful fallback to defaults when policy.yaml missing
|
|
7
|
+
* - Cache inspection and management API
|
|
8
|
+
* - Waiver validation and delta application
|
|
9
|
+
*/
|
|
10
|
+
export class PolicyManager {
|
|
11
|
+
constructor(options?: {});
|
|
12
|
+
enableCaching: any;
|
|
13
|
+
cacheTTL: any;
|
|
14
|
+
policyCache: Map<any, any>;
|
|
15
|
+
/**
|
|
16
|
+
* Load CAWS policy from policy.yaml with caching
|
|
17
|
+
*
|
|
18
|
+
* @param {string} projectRoot - Project root directory
|
|
19
|
+
* @param {Object} options - Loading options
|
|
20
|
+
* @param {boolean} options.useCache - Use cache if available (default: true)
|
|
21
|
+
* @param {number} options.cacheTTL - Cache TTL override in milliseconds
|
|
22
|
+
* @returns {Promise<Object>} Policy object
|
|
23
|
+
*/
|
|
24
|
+
loadPolicy(projectRoot: string, options?: {
|
|
25
|
+
useCache: boolean;
|
|
26
|
+
cacheTTL: number;
|
|
27
|
+
}): Promise<any>;
|
|
28
|
+
/**
|
|
29
|
+
* Load a waiver document by ID
|
|
30
|
+
*
|
|
31
|
+
* @param {string} waiverId - Waiver ID (e.g., WV-0001)
|
|
32
|
+
* @param {string} projectRoot - Project root directory
|
|
33
|
+
* @returns {Promise<Object|null>} Waiver document or null if not found
|
|
34
|
+
*/
|
|
35
|
+
loadWaiver(waiverId: string, projectRoot: string): Promise<any | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Check if a waiver is currently valid
|
|
38
|
+
*
|
|
39
|
+
* @param {Object} waiver - Waiver document
|
|
40
|
+
* @returns {boolean} True if waiver is valid and active
|
|
41
|
+
*/
|
|
42
|
+
isWaiverValid(waiver: any): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Apply waivers to baseline budget
|
|
45
|
+
*
|
|
46
|
+
* @param {Object} baseline - Baseline budget from policy
|
|
47
|
+
* @param {string[]} waiverIds - Array of waiver IDs to apply
|
|
48
|
+
* @param {string} projectRoot - Project root directory
|
|
49
|
+
* @returns {Promise<Object>} Effective budget with waivers applied
|
|
50
|
+
*/
|
|
51
|
+
applyWaivers(baseline: any, waiverIds: string[], projectRoot: string): Promise<any>;
|
|
52
|
+
/**
|
|
53
|
+
* Validate policy structure
|
|
54
|
+
*
|
|
55
|
+
* @param {Object} policy - Policy to validate
|
|
56
|
+
* @throws {Error} If policy is invalid
|
|
57
|
+
*/
|
|
58
|
+
validatePolicy(policy: any): void;
|
|
59
|
+
/**
|
|
60
|
+
* Get default CAWS policy
|
|
61
|
+
*
|
|
62
|
+
* Returns sensible defaults when policy.yaml doesn't exist.
|
|
63
|
+
*
|
|
64
|
+
* @returns {Object} Default policy configuration
|
|
65
|
+
*/
|
|
66
|
+
getDefaultPolicy(): any;
|
|
67
|
+
/**
|
|
68
|
+
* Clear policy cache
|
|
69
|
+
*
|
|
70
|
+
* @param {string} [projectRoot] - Specific project to clear, or all if omitted
|
|
71
|
+
*/
|
|
72
|
+
clearCache(projectRoot?: string): void;
|
|
73
|
+
/**
|
|
74
|
+
* Get cache status for a project
|
|
75
|
+
*
|
|
76
|
+
* @param {string} projectRoot - Project root directory
|
|
77
|
+
* @returns {Object} Cache status information
|
|
78
|
+
*/
|
|
79
|
+
getCacheStatus(projectRoot: string): any;
|
|
80
|
+
/**
|
|
81
|
+
* Reload policy from disk (bypassing cache)
|
|
82
|
+
*
|
|
83
|
+
* @param {string} projectRoot - Project root directory
|
|
84
|
+
* @returns {Promise<Object>} Fresh policy
|
|
85
|
+
*/
|
|
86
|
+
reloadPolicy(projectRoot: string): Promise<any>;
|
|
87
|
+
/**
|
|
88
|
+
* Get all cached projects
|
|
89
|
+
*
|
|
90
|
+
* @returns {string[]} Array of project roots with cached policies
|
|
91
|
+
*/
|
|
92
|
+
getCachedProjects(): string[];
|
|
93
|
+
/**
|
|
94
|
+
* Get cache statistics
|
|
95
|
+
*
|
|
96
|
+
* @returns {Object} Cache statistics
|
|
97
|
+
*/
|
|
98
|
+
getCacheStats(): any;
|
|
99
|
+
}
|
|
100
|
+
export const defaultPolicyManager: PolicyManager;
|
|
101
|
+
export declare function loadPolicy(projectRoot: any, options: any): Promise<any>;
|
|
102
|
+
export declare function clearCache(projectRoot: any): void;
|
|
103
|
+
export declare function getCacheStatus(projectRoot: any): any;
|
|
104
|
+
//# sourceMappingURL=PolicyManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PolicyManager.d.ts","sourceRoot":"","sources":["../../src/policy/PolicyManager.js"],"names":[],"mappings":"AAWA;;;;;;;;GAQG;AACH;IACE,0BAIC;IAHC,mBAAkD;IAClD,cAA0C;IAC1C,2BAA4B;IAG9B;;;;;;;;OAQG;IACH,wBANW,MAAM,YAEd;QAAyB,QAAQ,EAAzB,OAAO;QACS,QAAQ,EAAxB,MAAM;KACd,GAAU,OAAO,KAAQ,CAuE3B;IAED;;;;;;OAMG;IACH,qBAJW,MAAM,eACN,MAAM,GACJ,OAAO,CAAC,MAAO,IAAI,CAAC,CAchC;IAED;;;;;OAKG;IACH,4BAFa,OAAO,CA2BnB;IAED;;;;;;;OAOG;IACH,uCAJW,MAAM,EAAE,eACR,MAAM,GACJ,OAAO,KAAQ,CA2B3B;IAED;;;;;OAKG;IACH,kCA8BC;IAED;;;;;;OAMG;IACH,wBAoDC;IAED;;;;OAIG;IACH,yBAFW,MAAM,QAQhB;IAED;;;;;OAKG;IACH,4BAHW,MAAM,OAmBhB;IAED;;;;;OAKG;IACH,0BAHW,MAAM,GACJ,OAAO,KAAQ,CAK3B;IAED;;;;OAIG;IACH,qBAFa,MAAM,EAAE,CAIpB;IAED;;;;OAIG;IACH,qBA2BC;CACF;AAGD,iDAAiD;AAOnC,iFAA+E;AAC/E,2DAA6D;AACzD,8DAAiE"}
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Policy Manager with Intelligent Caching
|
|
3
|
+
* Manages policy.yaml loading with caching, default fallback, and waiver validation.
|
|
4
|
+
* Ported from agent-agency v2 CAWS integration patterns.
|
|
5
|
+
* @author @darianrosebrook
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const fs = require('fs-extra');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const yaml = require('js-yaml');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Policy Manager - Handles policy loading with intelligent caching
|
|
14
|
+
*
|
|
15
|
+
* Features:
|
|
16
|
+
* - TTL-based caching for performance
|
|
17
|
+
* - Graceful fallback to defaults when policy.yaml missing
|
|
18
|
+
* - Cache inspection and management API
|
|
19
|
+
* - Waiver validation and delta application
|
|
20
|
+
*/
|
|
21
|
+
class PolicyManager {
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.enableCaching = options.enableCaching ?? true;
|
|
24
|
+
this.cacheTTL = options.cacheTTL ?? 300000; // 5 minutes default
|
|
25
|
+
this.policyCache = new Map(); // projectRoot -> { policy, cachedAt, ttl }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Load CAWS policy from policy.yaml with caching
|
|
30
|
+
*
|
|
31
|
+
* @param {string} projectRoot - Project root directory
|
|
32
|
+
* @param {Object} options - Loading options
|
|
33
|
+
* @param {boolean} options.useCache - Use cache if available (default: true)
|
|
34
|
+
* @param {number} options.cacheTTL - Cache TTL override in milliseconds
|
|
35
|
+
* @returns {Promise<Object>} Policy object
|
|
36
|
+
*/
|
|
37
|
+
async loadPolicy(projectRoot, options = {}) {
|
|
38
|
+
const useCache = options.useCache ?? this.enableCaching;
|
|
39
|
+
const cacheTTL = options.cacheTTL ?? this.cacheTTL;
|
|
40
|
+
const startTime = Date.now();
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
// Check cache first
|
|
44
|
+
if (useCache && this.policyCache.has(projectRoot)) {
|
|
45
|
+
const cached = this.policyCache.get(projectRoot);
|
|
46
|
+
const cacheAge = Date.now() - cached.cachedAt;
|
|
47
|
+
|
|
48
|
+
if (cacheAge < cacheTTL) {
|
|
49
|
+
return {
|
|
50
|
+
...cached.policy,
|
|
51
|
+
_cacheHit: true,
|
|
52
|
+
_loadDuration: Date.now() - startTime,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Load from file
|
|
58
|
+
const policyPath = path.join(projectRoot, '.caws', 'policy.yaml');
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const content = await fs.readFile(policyPath, 'utf-8');
|
|
62
|
+
const policy = yaml.load(content);
|
|
63
|
+
|
|
64
|
+
// Validate policy structure
|
|
65
|
+
this.validatePolicy(policy);
|
|
66
|
+
|
|
67
|
+
// Update cache
|
|
68
|
+
if (this.enableCaching) {
|
|
69
|
+
this.policyCache.set(projectRoot, {
|
|
70
|
+
policy,
|
|
71
|
+
cachedAt: Date.now(),
|
|
72
|
+
ttl: cacheTTL,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
...policy,
|
|
78
|
+
_cacheHit: false,
|
|
79
|
+
_loadDuration: Date.now() - startTime,
|
|
80
|
+
};
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (error.code === 'ENOENT') {
|
|
83
|
+
// Policy file doesn't exist - use default
|
|
84
|
+
const defaultPolicy = this.getDefaultPolicy();
|
|
85
|
+
|
|
86
|
+
if (this.enableCaching) {
|
|
87
|
+
this.policyCache.set(projectRoot, {
|
|
88
|
+
policy: defaultPolicy,
|
|
89
|
+
cachedAt: Date.now(),
|
|
90
|
+
ttl: cacheTTL,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
...defaultPolicy,
|
|
96
|
+
_isDefault: true,
|
|
97
|
+
_cacheHit: false,
|
|
98
|
+
_loadDuration: Date.now() - startTime,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
} catch (error) {
|
|
104
|
+
throw new Error(`Policy load failed: ${error.message}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Load a waiver document by ID
|
|
110
|
+
*
|
|
111
|
+
* @param {string} waiverId - Waiver ID (e.g., WV-0001)
|
|
112
|
+
* @param {string} projectRoot - Project root directory
|
|
113
|
+
* @returns {Promise<Object|null>} Waiver document or null if not found
|
|
114
|
+
*/
|
|
115
|
+
async loadWaiver(waiverId, projectRoot) {
|
|
116
|
+
try {
|
|
117
|
+
const waiverPath = path.join(projectRoot, '.caws', 'waivers', `${waiverId}.yaml`);
|
|
118
|
+
|
|
119
|
+
const content = await fs.readFile(waiverPath, 'utf-8');
|
|
120
|
+
return yaml.load(content);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error.code === 'ENOENT') {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
throw new Error(`Failed to load waiver ${waiverId}: ${error.message}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Check if a waiver is currently valid
|
|
131
|
+
*
|
|
132
|
+
* @param {Object} waiver - Waiver document
|
|
133
|
+
* @returns {boolean} True if waiver is valid and active
|
|
134
|
+
*/
|
|
135
|
+
isWaiverValid(waiver) {
|
|
136
|
+
if (!waiver) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Check status
|
|
141
|
+
if (waiver.status !== 'active') {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Check expiry
|
|
146
|
+
if (waiver.expires_at) {
|
|
147
|
+
const expiryDate = new Date(waiver.expires_at);
|
|
148
|
+
const now = new Date();
|
|
149
|
+
if (now > expiryDate) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Check if it has required approvals
|
|
155
|
+
if (!waiver.approvers || waiver.approvers.length === 0) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Apply waivers to baseline budget
|
|
164
|
+
*
|
|
165
|
+
* @param {Object} baseline - Baseline budget from policy
|
|
166
|
+
* @param {string[]} waiverIds - Array of waiver IDs to apply
|
|
167
|
+
* @param {string} projectRoot - Project root directory
|
|
168
|
+
* @returns {Promise<Object>} Effective budget with waivers applied
|
|
169
|
+
*/
|
|
170
|
+
async applyWaivers(baseline, waiverIds, projectRoot) {
|
|
171
|
+
const effective = { ...baseline };
|
|
172
|
+
const applied = [];
|
|
173
|
+
|
|
174
|
+
for (const waiverId of waiverIds) {
|
|
175
|
+
const waiver = await this.loadWaiver(waiverId, projectRoot);
|
|
176
|
+
|
|
177
|
+
if (waiver && this.isWaiverValid(waiver)) {
|
|
178
|
+
// Apply additive delta
|
|
179
|
+
if (waiver.delta) {
|
|
180
|
+
if (waiver.delta.max_files) {
|
|
181
|
+
effective.max_files += waiver.delta.max_files;
|
|
182
|
+
}
|
|
183
|
+
if (waiver.delta.max_loc) {
|
|
184
|
+
effective.max_loc += waiver.delta.max_loc;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
applied.push(waiverId);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
effective,
|
|
193
|
+
applied,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Validate policy structure
|
|
199
|
+
*
|
|
200
|
+
* @param {Object} policy - Policy to validate
|
|
201
|
+
* @throws {Error} If policy is invalid
|
|
202
|
+
*/
|
|
203
|
+
validatePolicy(policy) {
|
|
204
|
+
if (!policy.version) {
|
|
205
|
+
throw new Error('Policy missing version field');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (!policy.risk_tiers) {
|
|
209
|
+
throw new Error('Policy missing risk_tiers configuration');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Validate all tiers have required fields
|
|
213
|
+
for (const tier of [1, 2, 3]) {
|
|
214
|
+
const budget = policy.risk_tiers[tier];
|
|
215
|
+
if (!budget) {
|
|
216
|
+
throw new Error(`Policy missing risk tier ${tier} configuration`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (typeof budget.max_files !== 'number' || typeof budget.max_loc !== 'number') {
|
|
220
|
+
throw new Error(`Risk tier ${tier} missing or invalid budget limits`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Validate edit rules if present
|
|
225
|
+
if (policy.edit_rules) {
|
|
226
|
+
if (typeof policy.edit_rules.policy_and_code_same_pr !== 'boolean') {
|
|
227
|
+
throw new Error('edit_rules.policy_and_code_same_pr must be boolean');
|
|
228
|
+
}
|
|
229
|
+
if (typeof policy.edit_rules.min_approvers_for_budget_raise !== 'number') {
|
|
230
|
+
throw new Error('edit_rules.min_approvers_for_budget_raise must be number');
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Get default CAWS policy
|
|
237
|
+
*
|
|
238
|
+
* Returns sensible defaults when policy.yaml doesn't exist.
|
|
239
|
+
*
|
|
240
|
+
* @returns {Object} Default policy configuration
|
|
241
|
+
*/
|
|
242
|
+
getDefaultPolicy() {
|
|
243
|
+
return {
|
|
244
|
+
version: 1,
|
|
245
|
+
risk_tiers: {
|
|
246
|
+
1: {
|
|
247
|
+
max_files: 25,
|
|
248
|
+
max_loc: 1000,
|
|
249
|
+
description: 'Critical changes requiring manual review',
|
|
250
|
+
},
|
|
251
|
+
2: {
|
|
252
|
+
max_files: 50,
|
|
253
|
+
max_loc: 2000,
|
|
254
|
+
description: 'Standard features with automated gates',
|
|
255
|
+
},
|
|
256
|
+
3: {
|
|
257
|
+
max_files: 100,
|
|
258
|
+
max_loc: 5000,
|
|
259
|
+
description: 'Low-risk changes with minimal oversight',
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
edit_rules: {
|
|
263
|
+
policy_and_code_same_pr: false,
|
|
264
|
+
min_approvers_for_budget_raise: 2,
|
|
265
|
+
require_signed_commits: true,
|
|
266
|
+
},
|
|
267
|
+
gates: {
|
|
268
|
+
budget_limit: {
|
|
269
|
+
enabled: true,
|
|
270
|
+
description: 'Enforce change budget limits',
|
|
271
|
+
},
|
|
272
|
+
spec_completeness: {
|
|
273
|
+
enabled: true,
|
|
274
|
+
description: 'Require complete working specifications',
|
|
275
|
+
},
|
|
276
|
+
contract_compliance: {
|
|
277
|
+
enabled: true,
|
|
278
|
+
description: 'Validate API contracts',
|
|
279
|
+
},
|
|
280
|
+
coverage_threshold: {
|
|
281
|
+
enabled: true,
|
|
282
|
+
description: 'Maintain test coverage requirements',
|
|
283
|
+
},
|
|
284
|
+
mutation_threshold: {
|
|
285
|
+
enabled: true,
|
|
286
|
+
description: 'Require mutation testing for T1/T2 changes',
|
|
287
|
+
},
|
|
288
|
+
security_scan: {
|
|
289
|
+
enabled: true,
|
|
290
|
+
description: 'Run security vulnerability scans',
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Clear policy cache
|
|
298
|
+
*
|
|
299
|
+
* @param {string} [projectRoot] - Specific project to clear, or all if omitted
|
|
300
|
+
*/
|
|
301
|
+
clearCache(projectRoot) {
|
|
302
|
+
if (projectRoot) {
|
|
303
|
+
this.policyCache.delete(projectRoot);
|
|
304
|
+
} else {
|
|
305
|
+
this.policyCache.clear();
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Get cache status for a project
|
|
311
|
+
*
|
|
312
|
+
* @param {string} projectRoot - Project root directory
|
|
313
|
+
* @returns {Object} Cache status information
|
|
314
|
+
*/
|
|
315
|
+
getCacheStatus(projectRoot) {
|
|
316
|
+
const cached = this.policyCache.get(projectRoot);
|
|
317
|
+
|
|
318
|
+
if (!cached) {
|
|
319
|
+
return {
|
|
320
|
+
cached: false,
|
|
321
|
+
ttl: this.cacheTTL,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
cached: true,
|
|
327
|
+
age: Date.now() - cached.cachedAt,
|
|
328
|
+
ttl: cached.ttl,
|
|
329
|
+
remainingTTL: Math.max(0, cached.ttl - (Date.now() - cached.cachedAt)),
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Reload policy from disk (bypassing cache)
|
|
335
|
+
*
|
|
336
|
+
* @param {string} projectRoot - Project root directory
|
|
337
|
+
* @returns {Promise<Object>} Fresh policy
|
|
338
|
+
*/
|
|
339
|
+
async reloadPolicy(projectRoot) {
|
|
340
|
+
this.clearCache(projectRoot);
|
|
341
|
+
return this.loadPolicy(projectRoot, { useCache: false });
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Get all cached projects
|
|
346
|
+
*
|
|
347
|
+
* @returns {string[]} Array of project roots with cached policies
|
|
348
|
+
*/
|
|
349
|
+
getCachedProjects() {
|
|
350
|
+
return Array.from(this.policyCache.keys());
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Get cache statistics
|
|
355
|
+
*
|
|
356
|
+
* @returns {Object} Cache statistics
|
|
357
|
+
*/
|
|
358
|
+
getCacheStats() {
|
|
359
|
+
const projects = this.getCachedProjects();
|
|
360
|
+
const now = Date.now();
|
|
361
|
+
|
|
362
|
+
const stats = {
|
|
363
|
+
totalCached: projects.length,
|
|
364
|
+
validCaches: 0,
|
|
365
|
+
expiredCaches: 0,
|
|
366
|
+
totalAge: 0,
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
for (const project of projects) {
|
|
370
|
+
const cached = this.policyCache.get(project);
|
|
371
|
+
const age = now - cached.cachedAt;
|
|
372
|
+
|
|
373
|
+
stats.totalAge += age;
|
|
374
|
+
|
|
375
|
+
if (age < cached.ttl) {
|
|
376
|
+
stats.validCaches++;
|
|
377
|
+
} else {
|
|
378
|
+
stats.expiredCaches++;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
stats.averageAge = projects.length > 0 ? stats.totalAge / projects.length : 0;
|
|
383
|
+
|
|
384
|
+
return stats;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Export singleton instance with default configuration
|
|
389
|
+
const defaultPolicyManager = new PolicyManager();
|
|
390
|
+
|
|
391
|
+
module.exports = {
|
|
392
|
+
PolicyManager,
|
|
393
|
+
defaultPolicyManager,
|
|
394
|
+
|
|
395
|
+
// Convenience exports for backward compatibility
|
|
396
|
+
loadPolicy: (projectRoot, options) => defaultPolicyManager.loadPolicy(projectRoot, options),
|
|
397
|
+
clearCache: (projectRoot) => defaultPolicyManager.clearCache(projectRoot),
|
|
398
|
+
getCacheStatus: (projectRoot) => defaultPolicyManager.getCacheStatus(projectRoot),
|
|
399
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursor-hooks.d.ts","sourceRoot":"","sources":["../../src/scaffold/cursor-hooks.js"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,gDAHW,MAAM,WACN,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"cursor-hooks.d.ts","sourceRoot":"","sources":["../../src/scaffold/cursor-hooks.js"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,gDAHW,MAAM,WACN,MAAM,EAAE,iBAqJlB"}
|