@principles/pd-cli 1.83.0 → 1.84.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,170 @@
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
+ import * as path from 'path';
15
+ import * as os from 'os';
16
+ import { existsSync } from 'fs';
17
+ // ── Production workspace paths ─────────────────────────────────────────────────
18
+ /**
19
+ * List of production workspace paths that should be protected from UAT/test writes.
20
+ * These are the default paths where PD is typically installed and used for real work.
21
+ */
22
+ const PRODUCTION_WORKSPACE_PATHS = [
23
+ // Windows default
24
+ path.resolve('D:\\.openclaw\\workspace'),
25
+ path.resolve('C:\\.openclaw\\workspace'),
26
+ path.resolve('C:\\Users\\Administrator\\.openclaw\\workspace'),
27
+ path.resolve('C:\\Users\\Admin\\.openclaw\\workspace'),
28
+ // Unix-like defaults
29
+ path.resolve(path.join(os.homedir(), '.openclaw', 'workspace')),
30
+ // macOS-specific
31
+ path.resolve(path.join(os.homedir(), '.openclaw', 'workspace')),
32
+ ];
33
+ // ── Resolution helpers ───────────────────────────────────────────────────────
34
+ /**
35
+ * Resolve the workspace path from environment variable or current directory.
36
+ */
37
+ export function resolveWorkspacePath(inputPath) {
38
+ if (!inputPath) {
39
+ // Default to current directory
40
+ return path.resolve(process.cwd());
41
+ }
42
+ return path.resolve(inputPath);
43
+ }
44
+ /**
45
+ * Check if a path is a production workspace.
46
+ *
47
+ * This follows ERR-030: path matching must use segment boundaries (path.sep)
48
+ * to avoid false positives on sibling directories like "workspace-backup".
49
+ *
50
+ * @param resolvedPath - The absolute, normalized workspace path to check
51
+ * @returns true if the path is a production workspace
52
+ */
53
+ export function isProductionWorkspace(resolvedPath) {
54
+ const normalized = resolvedPath.toLowerCase();
55
+ for (const prodPath of PRODUCTION_WORKSPACE_PATHS) {
56
+ const normalizedProd = prodPath.toLowerCase();
57
+ // Exact match
58
+ if (normalized === normalizedProd) {
59
+ return true;
60
+ }
61
+ // Descendant match: must have path separator after prefix
62
+ // ERR-030: "startsWith" without separator matches sibling directories
63
+ if (normalized.startsWith(normalizedProd + path.sep)) {
64
+ return true;
65
+ }
66
+ // Handle Windows path variations
67
+ // D:\.openclaw\workspace should not match D:\.openclaw\workspace-backup
68
+ // Use case-insensitive comparison (already normalized)
69
+ if (path.sep === '\\') {
70
+ // Windows: check both forward and backslash
71
+ if (normalized.startsWith(normalizedProd + '/')) {
72
+ return true;
73
+ }
74
+ if (normalized.startsWith(normalizedProd.replace(/\\/g, '/') + '/')) {
75
+ return true;
76
+ }
77
+ if (normalized.startsWith(normalizedProd.replace(/\\/g, '/') + '\\')) {
78
+ return true;
79
+ }
80
+ }
81
+ }
82
+ return false;
83
+ }
84
+ /**
85
+ * Check if a workspace is protected from UAT/test writes.
86
+ *
87
+ * This is the main guard function for UAT/runtime test commands.
88
+ * It returns a structured result following EP-03/EP-04 requirements.
89
+ *
90
+ * @param inputPath - The workspace path to check (optional, resolves to cwd if not provided)
91
+ * @param commandContext - Context string for the error message (e.g., "pd runtime uat")
92
+ * @returns GuardResult indicating if the operation is allowed or refused
93
+ */
94
+ export function guardUatWorkspace(inputPath, _commandContext) {
95
+ const resolved = resolveWorkspacePath(inputPath);
96
+ // PRI-334: Guard check should happen BEFORE file existence check
97
+ // This prevents production workspace writes even if directory doesn't exist yet
98
+ if (isProductionWorkspace(resolved)) {
99
+ return {
100
+ refused: true,
101
+ workspace: resolved,
102
+ isProduction: true,
103
+ 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.`,
104
+ 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).`,
105
+ };
106
+ }
107
+ if (!existsSync(resolved)) {
108
+ // Non-existent workspace is safe to use (will be created)
109
+ return {
110
+ refused: false,
111
+ workspace: resolved,
112
+ isProduction: false,
113
+ };
114
+ }
115
+ return {
116
+ refused: false,
117
+ workspace: resolved,
118
+ isProduction: false,
119
+ };
120
+ }
121
+ /**
122
+ * Get a safe UAT workspace path in the system temp directory.
123
+ *
124
+ * This follows the safe execution path requirement from PRI-334.
125
+ */
126
+ export function getSafeUatWorkspacePath() {
127
+ const tempDir = os.tmpdir();
128
+ // Create a unique but deterministic path for UAT workspaces
129
+ const uatWorkspace = path.join(tempDir, 'pd-uat-workspace');
130
+ return uatWorkspace;
131
+ }
132
+ /**
133
+ * Check if --allow-production-workspace-for-uat flag is set.
134
+ *
135
+ * This is the escape hatch for cases where the operator explicitly wants to run UAT on production.
136
+ * The flag must be very explicit in both name and output (as required by PRI-334).
137
+ */
138
+ export function isProductionWorkspaceAllowed() {
139
+ // Check if the flag was parsed and passed through opts
140
+ // This will be called from command handlers after Commander parses flags
141
+ return false; // Placeholder; actual check depends on Commander opts
142
+ }
143
+ /**
144
+ * Format guard refusal for console output.
145
+ *
146
+ * Follows EP-03/EP-04: structured reason + nextAction.
147
+ */
148
+ export function formatGuardRefusal(refusal, commandContext, jsonMode = false) {
149
+ if (jsonMode) {
150
+ // JSON mode: output exactly one JSON object with reason and nextAction
151
+ return JSON.stringify({
152
+ status: 'refused',
153
+ reason: refusal.reason,
154
+ nextAction: refusal.nextAction,
155
+ workspace: refusal.workspace,
156
+ isProduction: refusal.isProduction,
157
+ }, null, 2);
158
+ }
159
+ // Text mode: human-readable structured output
160
+ return [
161
+ `[pd-cli] ERROR: ${commandContext} - workspace guard triggered`,
162
+ '',
163
+ `Reason: ${refusal.reason}`,
164
+ `Next Action: ${refusal.nextAction}`,
165
+ `Workspace: ${refusal.workspace}`,
166
+ '',
167
+ 'This guard prevents UAT/runtime test data from polluting your production workspace.',
168
+ ].join('\n');
169
+ }
170
+ //# sourceMappingURL=production-workspace-guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"production-workspace-guard.js","sourceRoot":"","sources":["../../src/utils/production-workspace-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAEhC,kFAAkF;AAElF;;;GAGG;AACH,MAAM,0BAA0B,GAAG;IACjC,kBAAkB;IAClB,IAAI,CAAC,OAAO,CAAC,0BAA0B,CAAC;IACxC,IAAI,CAAC,OAAO,CAAC,0BAA0B,CAAC;IACxC,IAAI,CAAC,OAAO,CAAC,gDAAgD,CAAC;IAC9D,IAAI,CAAC,OAAO,CAAC,wCAAwC,CAAC;IACtD,qBAAqB;IACrB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;IAC/D,iBAAiB;IACjB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;CAChE,CAAC;AAEF,gFAAgF;AAEhF;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,SAAkB;IACrD,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,+BAA+B;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AACjC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,qBAAqB,CAAC,YAAoB;IACxD,MAAM,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A8B;QAC/D,EAAE;QACF,WAAW,OAAO,CAAC,MAAM,EAAE;QAC3B,gBAAgB,OAAO,CAAC,UAAU,EAAE;QACpC,cAAc,OAAO,CAAC,SAAS,EAAE;QACjC,EAAE;QACF,qFAAqF;KACtF,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=production-workspace-guard.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"production-workspace-guard.test.d.ts","sourceRoot":"","sources":["../../src/utils/production-workspace-guard.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,95 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import * as path from 'path';
3
+ import * as os from 'os';
4
+ import { isProductionWorkspace, guardUatWorkspace, getSafeUatWorkspacePath, formatGuardRefusal, } from './production-workspace-guard.js';
5
+ const PRODUCTION_PATHS = [
6
+ 'D:\\.openclaw\\workspace',
7
+ 'C:\\.openclaw\\workspace',
8
+ 'C:\\Users\\Administrator\\.openclaw\\workspace',
9
+ path.join(os.homedir(), '.openclaw', 'workspace'),
10
+ ];
11
+ const SAFE_PATHS = [
12
+ 'D:\\.openclaw\\workspace-test',
13
+ 'D:\\.openclaw\\workspace-backup',
14
+ path.join(os.tmpdir(), 'pd-uat-workspace'),
15
+ path.join(os.tmpdir(), 'pd-test-any'),
16
+ 'C:\\completely-unrelated\\work',
17
+ ];
18
+ describe('isProductionWorkspace', () => {
19
+ it.each(PRODUCTION_PATHS)('detects production: %s', (prodPath) => {
20
+ expect(isProductionWorkspace(path.resolve(prodPath))).toBe(true);
21
+ });
22
+ it.each(SAFE_PATHS)('allows safe: %s', (safePath) => {
23
+ expect(isProductionWorkspace(path.resolve(safePath))).toBe(false);
24
+ });
25
+ it('detects descendant', () => {
26
+ expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace\\sub\\child'))).toBe(true);
27
+ });
28
+ it('rejects sibling workspace-test (ERR-030)', () => {
29
+ expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace-test'))).toBe(false);
30
+ });
31
+ it('rejects sibling workspace-backup (ERR-030)', () => {
32
+ expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace-backup'))).toBe(false);
33
+ });
34
+ });
35
+ describe('guardUatWorkspace', () => {
36
+ describe('refused', () => {
37
+ it.each(PRODUCTION_PATHS)('refuses production: %s', (prodPath) => {
38
+ const r = guardUatWorkspace(prodPath, 'test');
39
+ expect(r.refused).toBe(true);
40
+ if (r.refused) {
41
+ expect(r.reason).toContain('UAT/runtime test commands are not allowed');
42
+ expect(r.nextAction).toContain('temporary workspace');
43
+ }
44
+ });
45
+ it('refuses descendant', () => {
46
+ expect(guardUatWorkspace('D:\\.openclaw\\workspace\\subdir', 'test').refused).toBe(true);
47
+ });
48
+ });
49
+ describe('allowed', () => {
50
+ it.each(SAFE_PATHS)('allows safe: %s', (safePath) => {
51
+ const r = guardUatWorkspace(safePath, 'test');
52
+ expect(r.refused).toBe(false);
53
+ });
54
+ it.each(['D:\\.openclaw\\workspace-test', 'D:\\.openclaw\\workspace-backup'])('allows sibling: %s (ERR-030)', (p) => {
55
+ expect(guardUatWorkspace(p, 'test').refused).toBe(false);
56
+ });
57
+ });
58
+ });
59
+ describe('JSON output (EP-04)', () => {
60
+ it('outputs single object with reason and nextAction', () => {
61
+ const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
62
+ const json = formatGuardRefusal(r, 'test', true);
63
+ const parsed = JSON.parse(json);
64
+ expect(parsed).toMatchObject({
65
+ status: 'refused', reason: expect.any(String), nextAction: expect.any(String),
66
+ workspace: expect.any(String), isProduction: true,
67
+ });
68
+ expect(Array.isArray(parsed)).toBe(false);
69
+ });
70
+ it('no console prefixes in JSON', () => {
71
+ const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
72
+ const json = formatGuardRefusal(r, 'test', true);
73
+ expect(json).not.toContain('[pd-cli]');
74
+ expect(json).not.toContain('ERROR:');
75
+ expect(json.trim().startsWith('{')).toBe(true);
76
+ });
77
+ });
78
+ describe('text output (EP-03)', () => {
79
+ it('includes reason and nextAction', () => {
80
+ const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
81
+ const text = formatGuardRefusal(r, 'test', false);
82
+ expect(text).toContain('Reason:');
83
+ expect(text).toContain('Next Action:');
84
+ });
85
+ });
86
+ describe('getSafeUatWorkspacePath', () => {
87
+ it('returns deterministic temp path', () => {
88
+ const p1 = getSafeUatWorkspacePath();
89
+ const p2 = getSafeUatWorkspacePath();
90
+ expect(p1).toBe(p2);
91
+ expect(p1).toContain(os.tmpdir());
92
+ expect(p1).toContain('pd-uat-workspace');
93
+ });
94
+ });
95
+ //# sourceMappingURL=production-workspace-guard.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"production-workspace-guard.test.js","sourceRoot":"","sources":["../../src/utils/production-workspace-guard.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,uBAAuB,EACvB,kBAAkB,GAEnB,MAAM,iCAAiC,CAAC;AAEzC,MAAM,gBAAgB,GAAG;IACvB,0BAA0B;IAC1B,0BAA0B;IAC1B,gDAAgD;IAChD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,WAAW,CAAC;CAClD,CAAC;AAEF,MAAM,UAAU,GAAG;IACjB,+BAA+B;IAC/B,iCAAiC;IACjC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,kBAAkB,CAAC;IAC1C,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,aAAa,CAAC;IACrC,gCAAgC;CACjC,CAA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;AAEH,QAAQ,CAAC,qBAAqB,EAAE,GAAG,EAAE;IACnC,EAAE,CAAC,gCAAgC,EAAE,GAAG,EAAE;QACxC,MAAM,CAAC,GAAG,iBAAiB,CAAC,0BAA0B,EAAE,MAAM,CAAC,CAAC;QAChE,MAAM,IAAI,GAAG,kBAAkB,CAAC,CAAiB,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;QAClE,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;QAClC,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACvC,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;QACzC,MAAM,EAAE,GAAG,uBAAuB,EAAE,CAAC;QACrC,MAAM,EAAE,GAAG,uBAAuB,EAAE,CAAC;QACrC,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACpB,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC;QAClC,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/pd-cli",
3
- "version": "1.83.0",
3
+ "version": "1.84.0",
4
4
  "description": "PD CLI — Pain recording, sample management, and evolution tasks",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,177 @@
1
+ /**
2
+ * PRI-334: Runtime UAT guard — command-level integration tests.
3
+ *
4
+ * These tests verify the guard is wired into the real Commander command
5
+ * and that refused paths do NOT call execFileSync (mutation prevention).
6
+ */
7
+ import { describe, it, expect, vi, beforeEach, afterAll, beforeAll } from 'vitest';
8
+ import { Command } from 'commander';
9
+ import * as path from 'path';
10
+ import * as os from 'os';
11
+
12
+ // ─── Mocks ─────────────────────────────────────────────────────────────────
13
+ const mockExecFileSync = vi.fn();
14
+ vi.mock('child_process', () => ({
15
+ execFileSync: mockExecFileSync,
16
+ }));
17
+
18
+ const { handleRuntimeUat } = await import('./runtime-uat.js');
19
+ const { guardUatWorkspace } = await import('../utils/production-workspace-guard.js');
20
+
21
+ // ─── Test Setup ─────────────────────────────────────────────────────────────
22
+ const capturedStderr: string[] = [];
23
+ const capturedStdout: string[] = [];
24
+ let capturedExitCode: number | null = null;
25
+
26
+ const originalExit = process.exit;
27
+ const originalError = console.error;
28
+ const originalLog = console.log;
29
+ const originalEnv = { ...process.env };
30
+
31
+ beforeAll(() => {
32
+ process.exit = vi.fn(((code?: number) => {
33
+ capturedExitCode = code ?? 0;
34
+ }) as typeof process.exit);
35
+ console.error = vi.fn((...args: unknown[]) => { capturedStderr.push(args.join(' ')); });
36
+ console.log = vi.fn((...args: unknown[]) => { capturedStdout.push(args.join(' ')); });
37
+ });
38
+
39
+ afterAll(() => {
40
+ process.exit = originalExit;
41
+ console.error = originalError;
42
+ console.log = originalLog;
43
+ process.env = originalEnv;
44
+ });
45
+
46
+ beforeEach(() => {
47
+ capturedExitCode = null;
48
+ capturedStderr.length = 0;
49
+ capturedStdout.length = 0;
50
+ mockExecFileSync.mockReset();
51
+ process.env = { ...process.env, MINIMAX_CN_API_KEY: 'test-key-for-pri-334' };
52
+ });
53
+
54
+ // ─── Tests ──────────────────────────────────────────────────────────────────
55
+ describe('PRI-334: production workspace refusal', () => {
56
+ it('refuses D: workspace with exit code 1', async () => {
57
+ mockExecFileSync.mockClear();
58
+ await handleRuntimeUat({ workspace: 'D:\\.openclaw\\workspace', count: 1 });
59
+ expect(capturedExitCode).toBe(1);
60
+ expect(mockExecFileSync).not.toHaveBeenCalled();
61
+ });
62
+
63
+ it('refuses C: workspace with exit code 1', async () => {
64
+ mockExecFileSync.mockClear();
65
+ await handleRuntimeUat({ workspace: 'C:\\.openclaw\\workspace', count: 1 });
66
+ expect(capturedExitCode).toBe(1);
67
+ expect(mockExecFileSync).not.toHaveBeenCalled();
68
+ });
69
+
70
+ it('refuses production workspace subdirectory', async () => {
71
+ mockExecFileSync.mockClear();
72
+ await handleRuntimeUat({ workspace: 'D:\\.openclaw\\workspace\\sub\\path', count: 1 });
73
+ expect(capturedExitCode).toBe(1);
74
+ expect(mockExecFileSync).not.toHaveBeenCalled();
75
+ });
76
+ });
77
+
78
+ describe('PRI-334: allowed paths (guard level)', () => {
79
+ it('allows temp workspace', () => {
80
+ const tempPath = path.join(os.tmpdir(), 'pd-test-' + Date.now());
81
+ expect(guardUatWorkspace(tempPath, 'test').refused).toBe(false);
82
+ });
83
+
84
+ it('allows workspace-test sibling (ERR-030)', () => {
85
+ expect(guardUatWorkspace('D:\\.openclaw\\workspace-test', 'test').refused).toBe(false);
86
+ });
87
+
88
+ it('allows workspace-backup sibling (ERR-030)', () => {
89
+ expect(guardUatWorkspace('D:\\.openclaw\\workspace-backup', 'test').refused).toBe(false);
90
+ });
91
+ });
92
+
93
+ describe('PRI-334: JSON output (EP-04)', () => {
94
+ it('outputs single JSON object with reason and nextAction', async () => {
95
+ mockExecFileSync.mockClear();
96
+ capturedStdout.length = 0;
97
+ await handleRuntimeUat({ workspace: 'D:\\.openclaw\\workspace', count: 1, json: true });
98
+ expect(capturedExitCode).toBe(1);
99
+ expect(mockExecFileSync).not.toHaveBeenCalled();
100
+
101
+ const jsonOutput = capturedStdout.join('');
102
+ const parsed = JSON.parse(jsonOutput);
103
+ expect(parsed).toMatchObject({
104
+ status: 'refused', reason: expect.any(String), nextAction: expect.any(String),
105
+ workspace: expect.any(String), isProduction: true,
106
+ });
107
+ expect(Array.isArray(parsed)).toBe(false);
108
+ });
109
+ });
110
+
111
+ describe('PRI-334: escape hatch', () => {
112
+ it('warns when allow-production-workspace-for-uat flag is set', async () => {
113
+ mockExecFileSync.mockClear();
114
+ mockExecFileSync.mockImplementation(() => '{"painId":"test","taskId":"t1","runId":"r1","candidateIds":["c1"],"ledgerEntryIds":["l1"]}');
115
+ capturedStderr.length = 0;
116
+ capturedExitCode = null;
117
+
118
+ await handleRuntimeUat({ workspace: 'D:\\.openclaw\\workspace', count: 1, allowProductionWorkspaceForUat: true });
119
+ const stderrText = capturedStderr.join('\n');
120
+ expect(stderrText).toContain('WARNING');
121
+ expect(stderrText).toContain('--allow-production-workspace-for-uat');
122
+ });
123
+ });
124
+
125
+ describe('PRI-334: mutation prevention', () => {
126
+ it('does not call execFileSync after guard refusal', async () => {
127
+ mockExecFileSync.mockClear();
128
+ await handleRuntimeUat({ workspace: 'D:\\.openclaw\\workspace', count: 1 });
129
+ expect(mockExecFileSync).not.toHaveBeenCalled();
130
+ });
131
+ });
132
+
133
+ describe('PRI-334: Commander flag wiring (ERR-063)', () => {
134
+ it('parses --allow-production-workspace-for-uat', () => {
135
+ const program = new Command();
136
+ const rt = program.command('runtime');
137
+ rt.command('uat')
138
+ .option('--allow-production-workspace-for-uat', 'Escape hatch')
139
+ .action(() => { /* noop */ });
140
+ const cmd = rt.commands.find((c) => c.name() === 'uat') as Command;
141
+ const opt = cmd.options.find((o) => o.long === '--allow-production-workspace-for-uat');
142
+ expect(opt).toBeDefined();
143
+ expect(opt?.long).toBe('--allow-production-workspace-for-uat');
144
+ });
145
+
146
+ it('negated form is NOT registered', () => {
147
+ const program = new Command();
148
+ const rt = program.command('runtime');
149
+ rt.command('uat')
150
+ .option('--allow-production-workspace-for-uat', 'Escape hatch')
151
+ .action(() => { /* noop */ });
152
+ const cmd = rt.commands.find((c) => c.name() === 'uat') as Command;
153
+ const noForm = cmd.options.find((o) => o.long === '--no-allow-production-workspace-for-uat');
154
+ expect(noForm).toBeUndefined();
155
+ });
156
+ });
157
+
158
+ describe('PRI-334: shouldExitWithError exit path (EP-04)', () => {
159
+ it('exits 1 and does not print ALL CHECKS PASSED when shouldExitWithError is true', async () => {
160
+ const tempWorkspace = path.join(os.tmpdir(), 'pd-exit-test-' + Date.now());
161
+ mockExecFileSync.mockClear();
162
+ // Make all iterations fail (execFileSync throws) so shouldExitWithError returns true
163
+ mockExecFileSync.mockRejectedValue(new Error('simulated failure'));
164
+ capturedStderr.length = 0;
165
+ capturedExitCode = null;
166
+
167
+ await handleRuntimeUat({
168
+ workspace: tempWorkspace,
169
+ count: 1,
170
+ minSuccessRate: 1.0,
171
+ });
172
+
173
+ expect(capturedExitCode).toBe(1);
174
+ const stderrText = capturedStderr.join('\n');
175
+ expect(stderrText).not.toContain('ALL CHECKS PASSED');
176
+ });
177
+ });
@@ -17,6 +17,12 @@ import { execFileSync } from 'child_process';
17
17
  import { fileURLToPath } from 'node:url';
18
18
  import * as path from 'path';
19
19
  import * as fs from 'fs';
20
+ import {
21
+ guardUatWorkspace,
22
+ formatGuardRefusal,
23
+ type GuardResult,
24
+ type GuardRefusal,
25
+ } from '../utils/production-workspace-guard.js';
20
26
 
21
27
  // ── Types ────────────────────────────────────────────────────────────────────
22
28
 
@@ -25,6 +31,7 @@ interface UatOptions {
25
31
  count?: number;
26
32
  minSuccessRate?: number;
27
33
  json?: boolean;
34
+ allowProductionWorkspaceForUat?: boolean;
28
35
  }
29
36
 
30
37
  interface PainRecordResult {
@@ -73,6 +80,8 @@ export function parseUatArgs(argv: string[]): UatOptions {
73
80
  } else if (argv[i] === '--min-success-rate') {
74
81
  const rate = parseFloat(argv[++i] ?? '1.0');
75
82
  args.minSuccessRate = isNaN(rate) ? 1.0 : rate;
83
+ } else if (argv[i] === '--allow-production-workspace-for-uat') {
84
+ args.allowProductionWorkspaceForUat = true;
76
85
  }
77
86
  }
78
87
  return args;
@@ -153,8 +162,6 @@ interface IterationConfig {
153
162
  export function runUatIteration(config: IterationConfig): PainRecordResult {
154
163
  const { iteration, reason, workspace, timeoutMs = 300_000 } = config;
155
164
  const iterStart = Date.now();
156
-
157
- // eslint-disable-next-line @typescript-eslint/init-declarations
158
165
  let recordOutput: string;
159
166
  try {
160
167
  recordOutput = pd(['pain', 'record', '--reason', reason, '--score', '85', '--source', 'manual', '--json'], workspace, timeoutMs);
@@ -173,7 +180,6 @@ export function runUatIteration(config: IterationConfig): PainRecordResult {
173
180
  }
174
181
 
175
182
  const wallTimeMs = Date.now() - iterStart;
176
- // eslint-disable-next-line @typescript-eslint/init-declarations
177
183
  let parsed: Record<string, unknown>;
178
184
  try {
179
185
  parsed = parseJsonOutput(recordOutput) as Record<string, unknown>;
@@ -190,7 +196,6 @@ export function runUatIteration(config: IterationConfig): PainRecordResult {
190
196
  };
191
197
  }
192
198
 
193
- // eslint-disable-next-line @typescript-eslint/init-declarations
194
199
  let auditStatus: string;
195
200
  try {
196
201
  const auditOut = pd(['candidate', 'audit', '--json'], workspace, 30_000);
@@ -276,6 +281,37 @@ export async function handleRuntimeUat(opts: UatOptions): Promise<void> {
276
281
  if (!workspace) {
277
282
  console.error('Error: --workspace <path> is required');
278
283
  process.exit(1);
284
+ return;
285
+ }
286
+
287
+ // PRI-334: Guard against writing to production workspace
288
+ const guardResult: GuardResult = guardUatWorkspace(workspace, 'pd runtime uat');
289
+
290
+ if (guardResult.refused && !opts.allowProductionWorkspaceForUat) {
291
+ // Fail loud with structured reason and nextAction (EP-03/EP-04)
292
+ const refused: GuardRefusal = guardResult;
293
+ const refusalOutput = formatGuardRefusal(
294
+ refused,
295
+ 'pd runtime uat',
296
+ !!opts.json
297
+ );
298
+
299
+ if (opts.json) {
300
+ console.log(refusalOutput);
301
+ } else {
302
+ console.error(refusalOutput);
303
+ }
304
+
305
+ process.exit(1);
306
+ return;
307
+ }
308
+
309
+ if (guardResult.refused && opts.allowProductionWorkspaceForUat) {
310
+ // Escape hatch used: warn but allow
311
+ console.error('[pd-cli] WARNING: --allow-production-workspace-for-uat is set.');
312
+ console.error(' Test/synthetic data will be written to your production workspace.');
313
+ console.error(' This is not recommended and may pollute your real PD state.');
314
+ console.error('');
279
315
  }
280
316
 
281
317
  console.error(`[${new Date().toISOString()}] Runtime V2 Chain UAT — workspace: ${workspace}, count: ${count}`);
@@ -284,6 +320,7 @@ export async function handleRuntimeUat(opts: UatOptions): Promise<void> {
284
320
  if (!process.env.MINIMAX_CN_API_KEY) {
285
321
  console.error('Error: MINIMAX_CN_API_KEY environment variable not set');
286
322
  process.exit(1);
323
+ return;
287
324
  }
288
325
 
289
326
  const results: PainRecordResult[] = [];
@@ -332,6 +369,7 @@ export async function handleRuntimeUat(opts: UatOptions): Promise<void> {
332
369
  console.error(`FAIL: successRate=${summary.successRate} (threshold: ${minSuccessRate}) ` +
333
370
  `ledger=${summary.ledgerConsistencyOk} candidates=${summary.allHaveCandidates} ledger=${summary.allHaveLedger}`);
334
371
  process.exit(1);
372
+ return;
335
373
  }
336
374
 
337
375
  console.error('');
package/src/index.ts CHANGED
@@ -475,12 +475,14 @@ runtimeCmd
475
475
  .option('--count <n>', 'Number of iterations (default: 5, max: 50)', parseInt)
476
476
  .option('--min-success-rate <rate>', 'Minimum success rate threshold (default: 1.0)', parseFloat)
477
477
  .option('--json', 'Output machine-readable JSON summary')
478
+ .option('--allow-production-workspace-for-uat', 'DANGEROUS: Allow UAT to write to production workspace (NOT RECOMMENDED)')
478
479
  .action(async (opts) => {
479
480
  await handleRuntimeUat({
480
481
  workspace: opts.workspace,
481
482
  count: opts.count,
482
483
  minSuccessRate: opts.minSuccessRate,
483
484
  json: opts.json,
485
+ allowProductionWorkspaceForUat: opts.allowProductionWorkspaceForUat,
484
486
  });
485
487
  });
486
488