@principles/pd-cli 1.75.0 → 1.76.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.
@@ -0,0 +1,213 @@
1
+ /**
2
+ * PD Config Loader — PRI-305
3
+ *
4
+ * I/O boundary: reads `.pd/config.yaml`, validates via core, computes effective config.
5
+ * This replaces the old `feature-flag-loader.ts` and `workflows.yaml` reading
6
+ * for CLI production paths.
7
+ *
8
+ * ADR-0016: PD owns exactly one user config file.
9
+ * - Missing config → defaults with nextAction
10
+ * - Malformed config → fail loud with errors and nextAction
11
+ * - No secrets in output
12
+ */
13
+
14
+ import * as fs from 'fs';
15
+ import * as path from 'path';
16
+ import yaml from 'js-yaml';
17
+ import {
18
+ validatePdConfig,
19
+ computeEffectivePdConfig,
20
+ computeFeatureFlagsFromConfig,
21
+ redactPdConfig,
22
+ } from '@principles/core/runtime-v2';
23
+ import type {
24
+ EffectivePdConfig,
25
+ PdConfigValidationResult,
26
+ RedactedPdConfigSummary,
27
+ FeatureFlagsResult,
28
+ } from '@principles/core/runtime-v2';
29
+
30
+ // ── Constants ────────────────────────────────────────────────────────────────
31
+
32
+ export const PD_CONFIG_DIR = '.pd';
33
+ export const PD_CONFIG_FILENAME = 'config.yaml';
34
+
35
+ // ── Types ────────────────────────────────────────────────────────────────────
36
+
37
+ export type ConfigSource = 'defaults' | 'user_config' | 'malformed';
38
+
39
+ export interface PdConfigLoadResultOk {
40
+ ok: true;
41
+ effective: EffectivePdConfig;
42
+ source: ConfigSource;
43
+ configPath: string;
44
+ /** Warnings from config resolution (not errors) */
45
+ warnings: string[];
46
+ /** If legacy files were detected */
47
+ legacyFilesDetected: string[];
48
+ }
49
+
50
+ export interface PdConfigLoadResultErr {
51
+ ok: false;
52
+ source: 'malformed';
53
+ configPath: string;
54
+ errors: { path: string; reason: string; nextAction: string }[];
55
+ /** Fallback defaults are still available */
56
+ defaults: EffectivePdConfig;
57
+ /** Warnings from config resolution */
58
+ warnings: string[];
59
+ legacyFilesDetected: string[];
60
+ }
61
+
62
+ export type PdConfigLoadResult = PdConfigLoadResultOk | PdConfigLoadResultErr;
63
+
64
+ // ── Config Path ──────────────────────────────────────────────────────────────
65
+
66
+ export function getPdConfigPath(workspaceDir: string): string {
67
+ return path.join(workspaceDir, PD_CONFIG_DIR, PD_CONFIG_FILENAME);
68
+ }
69
+
70
+ // ── Legacy File Detection ────────────────────────────────────────────────────
71
+
72
+ function detectLegacyFiles(workspaceDir: string): string[] {
73
+ const detected: string[] = [];
74
+ const legacyPaths = [
75
+ path.join(workspaceDir, PD_CONFIG_DIR, 'feature-flags.yaml'),
76
+ path.join(workspaceDir, '.state', 'workflows.yaml'),
77
+ ];
78
+ for (const p of legacyPaths) {
79
+ if (fs.existsSync(p)) {
80
+ detected.push(p);
81
+ }
82
+ }
83
+ return detected;
84
+ }
85
+
86
+ // ── Load PD Config ───────────────────────────────────────────────────────────
87
+
88
+ /**
89
+ * Load and validate `.pd/config.yaml` from the workspace.
90
+ *
91
+ * - Missing file → returns defaults with source='defaults'
92
+ * - Malformed file → returns error result with defaults fallback
93
+ * - Valid file → returns effective config with source='user_config'
94
+ *
95
+ * Never throws on malformed input. Always provides a usable fallback.
96
+ */
97
+ export function loadPdConfig(workspaceDir: string): PdConfigLoadResult {
98
+ const configPath = getPdConfigPath(workspaceDir);
99
+ const legacyFilesDetected = detectLegacyFiles(workspaceDir);
100
+
101
+ // 1) Config file missing → use defaults
102
+ if (!fs.existsSync(configPath)) {
103
+ const effective = computeEffectivePdConfig(null);
104
+ return {
105
+ ok: true,
106
+ effective,
107
+ source: 'defaults',
108
+ configPath,
109
+ warnings: [
110
+ ...effective.warnings,
111
+ ...(legacyFilesDetected.length > 0
112
+ ? [`Legacy config files detected (${legacyFilesDetected.length}): ${legacyFilesDetected.join(', ')}. PD now uses .pd/config.yaml.`]
113
+ : []),
114
+ ],
115
+ legacyFilesDetected,
116
+ };
117
+ }
118
+
119
+ // 2) Read the file
120
+ let raw: string;
121
+ try {
122
+ raw = fs.readFileSync(configPath, 'utf8');
123
+ } catch (err) {
124
+ const message = err instanceof Error ? err.message : String(err);
125
+ const effective = computeEffectivePdConfig(null);
126
+ return {
127
+ ok: false,
128
+ source: 'malformed',
129
+ configPath,
130
+ errors: [{ path: '', reason: `Failed to read .pd/config.yaml: ${message}`, nextAction: 'Check file permissions for .pd/config.yaml' }],
131
+ warnings: [],
132
+ defaults: effective,
133
+ legacyFilesDetected,
134
+ };
135
+ }
136
+
137
+ // 3) Parse YAML — treat as unknown (ERR-001)
138
+ let parsed: unknown;
139
+ try {
140
+ parsed = yaml.load(raw, { schema: yaml.JSON_SCHEMA });
141
+ } catch (err) {
142
+ const message = err instanceof Error ? err.message : String(err);
143
+ const effective = computeEffectivePdConfig(null);
144
+ return {
145
+ ok: false,
146
+ source: 'malformed',
147
+ configPath,
148
+ errors: [{ path: '', reason: `YAML parse error in .pd/config.yaml: ${message}`, nextAction: 'Fix YAML syntax in .pd/config.yaml' }],
149
+ warnings: [],
150
+ defaults: effective,
151
+ legacyFilesDetected,
152
+ };
153
+ }
154
+
155
+ // 4) Validate via core (ERR-001, ERR-005: no `as` bypasses)
156
+ const validationResult: PdConfigValidationResult = validatePdConfig(parsed);
157
+
158
+ if (!validationResult.ok) {
159
+ const effective = computeEffectivePdConfig(null);
160
+ return {
161
+ ok: false,
162
+ source: 'malformed',
163
+ configPath,
164
+ errors: validationResult.errors.map(e => ({
165
+ path: e.path,
166
+ reason: e.reason,
167
+ nextAction: e.nextAction,
168
+ })),
169
+ warnings: [],
170
+ defaults: effective,
171
+ legacyFilesDetected,
172
+ };
173
+ }
174
+
175
+ // 5) Compute effective config
176
+ const effective = computeEffectivePdConfig(validationResult.value);
177
+
178
+ return {
179
+ ok: true,
180
+ effective,
181
+ source: 'user_config',
182
+ configPath,
183
+ warnings: [
184
+ ...effective.warnings,
185
+ ...(legacyFilesDetected.length > 0
186
+ ? [`Legacy config files detected (${legacyFilesDetected.length}): ${legacyFilesDetected.join(', ')}. PD now uses .pd/config.yaml.`]
187
+ : []),
188
+ ],
189
+ legacyFilesDetected,
190
+ };
191
+ }
192
+
193
+ // ── Feature Flags from Config ────────────────────────────────────────────────
194
+
195
+ /**
196
+ * Compute feature flags from the loaded PD config.
197
+ * Works with both ok and error results (uses defaults for errors).
198
+ */
199
+ export function computeFlagsFromLoadResult(result: PdConfigLoadResult): FeatureFlagsResult {
200
+ const effective = result.ok ? result.effective : result.defaults;
201
+ return computeFeatureFlagsFromConfig(effective);
202
+ }
203
+
204
+ // ── Redacted Summary from Config ─────────────────────────────────────────────
205
+
206
+ /**
207
+ * Produce a redacted summary of the PD config for CLI/Console display.
208
+ * Never includes token/API key values or raw provider objects.
209
+ */
210
+ export function redactLoadResult(result: PdConfigLoadResult): RedactedPdConfigSummary {
211
+ const effective = result.ok ? result.effective : result.defaults;
212
+ return redactPdConfig(effective);
213
+ }