@principles/pd-cli 1.83.0 → 1.85.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.
Files changed (31) hide show
  1. package/dist/commands/pain-evidence.d.ts +41 -0
  2. package/dist/commands/pain-evidence.d.ts.map +1 -0
  3. package/dist/commands/pain-evidence.js +177 -0
  4. package/dist/commands/pain-evidence.js.map +1 -0
  5. package/dist/commands/runtime-uat.d.ts +1 -0
  6. package/dist/commands/runtime-uat.d.ts.map +1 -1
  7. package/dist/commands/runtime-uat.guard.test.d.ts +2 -0
  8. package/dist/commands/runtime-uat.guard.test.d.ts.map +1 -0
  9. package/dist/commands/runtime-uat.guard.test.js +155 -0
  10. package/dist/commands/runtime-uat.guard.test.js.map +1 -0
  11. package/dist/commands/runtime-uat.js +29 -3
  12. package/dist/commands/runtime-uat.js.map +1 -1
  13. package/dist/index.js +12 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/utils/production-workspace-guard.d.ts +77 -0
  16. package/dist/utils/production-workspace-guard.d.ts.map +1 -0
  17. package/dist/utils/production-workspace-guard.js +170 -0
  18. package/dist/utils/production-workspace-guard.js.map +1 -0
  19. package/dist/utils/production-workspace-guard.test.d.ts +2 -0
  20. package/dist/utils/production-workspace-guard.test.d.ts.map +1 -0
  21. package/dist/utils/production-workspace-guard.test.js +95 -0
  22. package/dist/utils/production-workspace-guard.test.js.map +1 -0
  23. package/package.json +1 -1
  24. package/src/commands/pain-evidence.ts +210 -0
  25. package/src/commands/runtime-uat.guard.test.ts +177 -0
  26. package/src/commands/runtime-uat.ts +42 -4
  27. package/src/index.ts +13 -0
  28. package/src/utils/production-workspace-guard.test.ts +108 -0
  29. package/src/utils/production-workspace-guard.ts +219 -0
  30. package/tests/commands/pain-evidence.test.ts +479 -0
  31. package/tests/commands/pain-retry.test.ts +64 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Production Workspace Guard
3
+ *
4
+ * Prevents UAT/runtime test commands from writing to production workspaces.
5
+ *
6
+ * Production workspaces are:
7
+ * - D:\.openclaw\workspace
8
+ * - C:\Users\Administrator\.openclaw\workspace
9
+ * - And the default workspace resolved by OpenClaw configuration
10
+ *
11
+ * This module follows ERR-030 (path prefix matching must use segment boundaries)
12
+ * and EP-03/EP-04 (fail loud with structured reason and nextAction).
13
+ */
14
+
15
+ import * as path from 'path';
16
+ import * as os from 'os';
17
+ import { existsSync } from 'fs';
18
+
19
+ // ── Production workspace paths ─────────────────────────────────────────────────
20
+
21
+ /**
22
+ * List of production workspace paths that should be protected from UAT/test writes.
23
+ * These are the default paths where PD is typically installed and used for real work.
24
+ */
25
+ const PRODUCTION_WORKSPACE_PATHS = [
26
+ // Windows default
27
+ path.resolve('D:\\.openclaw\\workspace'),
28
+ path.resolve('C:\\.openclaw\\workspace'),
29
+ path.resolve('C:\\Users\\Administrator\\.openclaw\\workspace'),
30
+ path.resolve('C:\\Users\\Admin\\.openclaw\\workspace'),
31
+ // Unix-like defaults
32
+ path.resolve(path.join(os.homedir(), '.openclaw', 'workspace')),
33
+ // macOS-specific
34
+ path.resolve(path.join(os.homedir(), '.openclaw', 'workspace')),
35
+ ];
36
+
37
+ // ── Resolution helpers ───────────────────────────────────────────────────────
38
+
39
+ /**
40
+ * Resolve the workspace path from environment variable or current directory.
41
+ */
42
+ export function resolveWorkspacePath(inputPath?: string): string {
43
+ if (!inputPath) {
44
+ // Default to current directory
45
+ return path.resolve(process.cwd());
46
+ }
47
+ return path.resolve(inputPath);
48
+ }
49
+
50
+ /**
51
+ * Check if a path is a production workspace.
52
+ *
53
+ * This follows ERR-030: path matching must use segment boundaries (path.sep)
54
+ * to avoid false positives on sibling directories like "workspace-backup".
55
+ *
56
+ * @param resolvedPath - The absolute, normalized workspace path to check
57
+ * @returns true if the path is a production workspace
58
+ */
59
+ export function isProductionWorkspace(resolvedPath: string): boolean {
60
+ const normalized = resolvedPath.toLowerCase();
61
+
62
+ for (const prodPath of PRODUCTION_WORKSPACE_PATHS) {
63
+ const normalizedProd = prodPath.toLowerCase();
64
+
65
+ // Exact match
66
+ if (normalized === normalizedProd) {
67
+ return true;
68
+ }
69
+
70
+ // Descendant match: must have path separator after prefix
71
+ // ERR-030: "startsWith" without separator matches sibling directories
72
+ if (normalized.startsWith(normalizedProd + path.sep)) {
73
+ return true;
74
+ }
75
+
76
+ // Handle Windows path variations
77
+ // D:\.openclaw\workspace should not match D:\.openclaw\workspace-backup
78
+ // Use case-insensitive comparison (already normalized)
79
+ if (path.sep === '\\') {
80
+ // Windows: check both forward and backslash
81
+ if (normalized.startsWith(normalizedProd + '/')) {
82
+ return true;
83
+ }
84
+ if (normalized.startsWith(normalizedProd.replace(/\\/g, '/') + '/')) {
85
+ return true;
86
+ }
87
+ if (normalized.startsWith(normalizedProd.replace(/\\/g, '/') + '\\')) {
88
+ return true;
89
+ }
90
+ }
91
+ }
92
+
93
+ return false;
94
+ }
95
+
96
+ // ── Guard logic ──────────────────────────────────────────────────────────────
97
+
98
+ /**
99
+ * Guard result for UAT/test commands attempting to write to production workspace.
100
+ */
101
+ export interface GuardRefusal {
102
+ refused: true;
103
+ reason: string;
104
+ nextAction: string;
105
+ workspace: string;
106
+ isProduction: true;
107
+ }
108
+
109
+ /**
110
+ * Guard result allowing the operation.
111
+ */
112
+ export interface GuardAllowed {
113
+ refused: false;
114
+ workspace: string;
115
+ isProduction: false;
116
+ }
117
+
118
+ export type GuardResult = GuardRefusal | GuardAllowed;
119
+
120
+ /**
121
+ * Check if a workspace is protected from UAT/test writes.
122
+ *
123
+ * This is the main guard function for UAT/runtime test commands.
124
+ * It returns a structured result following EP-03/EP-04 requirements.
125
+ *
126
+ * @param inputPath - The workspace path to check (optional, resolves to cwd if not provided)
127
+ * @param commandContext - Context string for the error message (e.g., "pd runtime uat")
128
+ * @returns GuardResult indicating if the operation is allowed or refused
129
+ */
130
+ export function guardUatWorkspace(
131
+ inputPath: string | undefined,
132
+ _commandContext: string
133
+ ): GuardResult {
134
+ const resolved = resolveWorkspacePath(inputPath);
135
+
136
+ // PRI-334: Guard check should happen BEFORE file existence check
137
+ // This prevents production workspace writes even if directory doesn't exist yet
138
+ if (isProductionWorkspace(resolved)) {
139
+ return {
140
+ refused: true,
141
+ workspace: resolved,
142
+ isProduction: true,
143
+ reason: `UAT/runtime test commands are not allowed to write to the production workspace (${resolved}). This prevents test/synthetic data from polluting your real PD state.`,
144
+ nextAction: `Use a temporary workspace for testing (recommended: ${os.tmpdir()}/pd-uat-workspace) or explicitly confirm you understand the risk by using --allow-production-workspace-for-uat (not recommended).`,
145
+ };
146
+ }
147
+
148
+ if (!existsSync(resolved)) {
149
+ // Non-existent workspace is safe to use (will be created)
150
+ return {
151
+ refused: false,
152
+ workspace: resolved,
153
+ isProduction: false,
154
+ };
155
+ }
156
+
157
+ return {
158
+ refused: false,
159
+ workspace: resolved,
160
+ isProduction: false,
161
+ };
162
+ }
163
+
164
+ /**
165
+ * Get a safe UAT workspace path in the system temp directory.
166
+ *
167
+ * This follows the safe execution path requirement from PRI-334.
168
+ */
169
+ export function getSafeUatWorkspacePath(): string {
170
+ const tempDir = os.tmpdir();
171
+ // Create a unique but deterministic path for UAT workspaces
172
+ const uatWorkspace = path.join(tempDir, 'pd-uat-workspace');
173
+ return uatWorkspace;
174
+ }
175
+
176
+ /**
177
+ * Check if --allow-production-workspace-for-uat flag is set.
178
+ *
179
+ * This is the escape hatch for cases where the operator explicitly wants to run UAT on production.
180
+ * The flag must be very explicit in both name and output (as required by PRI-334).
181
+ */
182
+ export function isProductionWorkspaceAllowed(): boolean {
183
+ // Check if the flag was parsed and passed through opts
184
+ // This will be called from command handlers after Commander parses flags
185
+ return false; // Placeholder; actual check depends on Commander opts
186
+ }
187
+
188
+ /**
189
+ * Format guard refusal for console output.
190
+ *
191
+ * Follows EP-03/EP-04: structured reason + nextAction.
192
+ */
193
+ export function formatGuardRefusal(refusal: GuardRefusal, commandContext: string, jsonMode = false): string {
194
+ if (jsonMode) {
195
+ // JSON mode: output exactly one JSON object with reason and nextAction
196
+ return JSON.stringify(
197
+ {
198
+ status: 'refused',
199
+ reason: refusal.reason,
200
+ nextAction: refusal.nextAction,
201
+ workspace: refusal.workspace,
202
+ isProduction: refusal.isProduction,
203
+ },
204
+ null,
205
+ 2
206
+ );
207
+ }
208
+
209
+ // Text mode: human-readable structured output
210
+ return [
211
+ `[pd-cli] ERROR: ${commandContext} - workspace guard triggered`,
212
+ '',
213
+ `Reason: ${refusal.reason}`,
214
+ `Next Action: ${refusal.nextAction}`,
215
+ `Workspace: ${refusal.workspace}`,
216
+ '',
217
+ 'This guard prevents UAT/runtime test data from polluting your production workspace.',
218
+ ].join('\n');
219
+ }