@principles/pd-cli 1.82.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.
Files changed (65) hide show
  1. package/dist/commands/diagnose.d.ts.map +1 -1
  2. package/dist/commands/diagnose.js +5 -0
  3. package/dist/commands/diagnose.js.map +1 -1
  4. package/dist/commands/pain-retry.d.ts.map +1 -1
  5. package/dist/commands/pain-retry.js +5 -0
  6. package/dist/commands/pain-retry.js.map +1 -1
  7. package/dist/commands/runtime-internalization-run-once.d.ts.map +1 -1
  8. package/dist/commands/runtime-internalization-run-once.js +5 -1
  9. package/dist/commands/runtime-internalization-run-once.js.map +1 -1
  10. package/dist/commands/runtime-uat.d.ts +1 -0
  11. package/dist/commands/runtime-uat.d.ts.map +1 -1
  12. package/dist/commands/runtime-uat.guard.test.d.ts +2 -0
  13. package/dist/commands/runtime-uat.guard.test.d.ts.map +1 -0
  14. package/dist/commands/runtime-uat.guard.test.js +155 -0
  15. package/dist/commands/runtime-uat.guard.test.js.map +1 -0
  16. package/dist/commands/runtime-uat.js +29 -3
  17. package/dist/commands/runtime-uat.js.map +1 -1
  18. package/dist/config-reader.d.ts +25 -0
  19. package/dist/config-reader.d.ts.map +1 -0
  20. package/dist/config-reader.js +109 -0
  21. package/dist/config-reader.js.map +1 -0
  22. package/dist/index.js +2 -0
  23. package/dist/index.js.map +1 -1
  24. package/dist/utils/production-workspace-guard.d.ts +77 -0
  25. package/dist/utils/production-workspace-guard.d.ts.map +1 -0
  26. package/dist/utils/production-workspace-guard.js +170 -0
  27. package/dist/utils/production-workspace-guard.js.map +1 -0
  28. package/dist/utils/production-workspace-guard.test.d.ts +2 -0
  29. package/dist/utils/production-workspace-guard.test.d.ts.map +1 -0
  30. package/dist/utils/production-workspace-guard.test.js +95 -0
  31. package/dist/utils/production-workspace-guard.test.js.map +1 -0
  32. package/package.json +1 -1
  33. package/src/commands/diagnose.ts +8 -1
  34. package/src/commands/pain-retry.ts +8 -1
  35. package/src/commands/runtime-internalization-run-once.ts +6 -2
  36. package/src/commands/runtime-uat.guard.test.ts +177 -0
  37. package/src/commands/runtime-uat.ts +42 -4
  38. package/src/config-reader.ts +122 -0
  39. package/src/index.ts +2 -0
  40. package/src/utils/production-workspace-guard.test.ts +108 -0
  41. package/src/utils/production-workspace-guard.ts +219 -0
  42. package/tests/commands/candidate-audit-repair.test.ts +1 -0
  43. package/tests/commands/candidate-intake.test.ts +1 -0
  44. package/tests/commands/candidate-internalization-backfill.test.ts +1 -0
  45. package/tests/commands/candidate-internalize.test.ts +1 -0
  46. package/tests/commands/candidate-route.test.ts +1 -0
  47. package/tests/commands/diagnose.test.ts +5 -0
  48. package/tests/commands/health.test.ts +1 -0
  49. package/tests/commands/pain-record.test.ts +1 -0
  50. package/tests/commands/pain-retry.test.ts +5 -0
  51. package/tests/commands/runtime-activation.test.ts +1 -0
  52. package/tests/commands/runtime-canary.test.ts +1 -0
  53. package/tests/commands/runtime-diagnostics-export.test.ts +1 -0
  54. package/tests/commands/runtime-health-snapshot.test.ts +1 -0
  55. package/tests/commands/runtime-internalization-enqueue-successors.test.ts +1 -0
  56. package/tests/commands/runtime-internalization-integrity-repair.test.ts +1 -0
  57. package/tests/commands/runtime-internalization-integrity.test.ts +1 -0
  58. package/tests/commands/runtime-internalization-queue.test.ts +1 -0
  59. package/tests/commands/runtime-internalization-run-once.test.ts +5 -0
  60. package/tests/commands/runtime-internalization-wake-once.test.ts +1 -0
  61. package/tests/commands/runtime-pruning.test.ts +1 -0
  62. package/tests/commands/runtime-recovery.test.ts +1 -0
  63. package/tests/commands/runtime.test.ts +1 -0
  64. package/tests/commands/trace.test.ts +1 -0
  65. package/tests/config-reader.test.ts +142 -0
@@ -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('');
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Config reader for pd-cli — reads outputLanguage from .pd/config.yaml.
3
+ *
4
+ * This is an I/O boundary module (pd-cli is the I/O layer).
5
+ * Uses core's `resolveOutputLanguage` for validation and degradation.
6
+ *
7
+ * ERR entries:
8
+ * - ERR-001: No `as` bypasses on untrusted parsed YAML — use isRecord type guard
9
+ * - ERR-002: Graceful degradation includes reason + nextAction
10
+ * - ERR-009: Malformed values fail loud with structured warning
11
+ * - ERR-013: Object.hasOwn() for untrusted keys
12
+ */
13
+
14
+ import * as fs from 'fs';
15
+ import * as path from 'path';
16
+ import yaml from 'js-yaml';
17
+ import { resolveOutputLanguage, DEFAULT_OUTPUT_LANGUAGE } from '@principles/core/runtime-v2';
18
+ import type { ResolvedOutputLanguage } from '@principles/core/runtime-v2';
19
+
20
+ /**
21
+ * Type guard: checks that a value is a non-null plain object (Record<string, unknown>).
22
+ * Per ERR-001: use type guards instead of `as` casts at trust boundaries.
23
+ */
24
+ function isRecord(value: unknown): value is Record<string, unknown> {
25
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
26
+ }
27
+
28
+ /**
29
+ * Build a structured degradation warning for config read errors.
30
+ * Per ERR-002/ERR-009: every degraded path must include reason + nextAction.
31
+ */
32
+ function configDegradationWarning(
33
+ reason: 'read_error' | 'yaml_parse_error' | 'invalid_config_root' | 'invalid_principles_structure',
34
+ detail: string,
35
+ ): string {
36
+ const reasons: Record<typeof reason, { msg: string; action: string }> = {
37
+ read_error: {
38
+ msg: `Failed to read .pd/config.yaml: ${detail}`,
39
+ action: 'Check file permissions and ensure .pd/config.yaml is readable',
40
+ },
41
+ yaml_parse_error: {
42
+ msg: `Failed to parse .pd/config.yaml: ${detail}`,
43
+ action: 'Fix YAML syntax errors in .pd/config.yaml',
44
+ },
45
+ invalid_config_root: {
46
+ msg: `.pd/config.yaml root is not an object: ${detail}`,
47
+ action: 'Ensure .pd/config.yaml has a valid YAML mapping at the top level',
48
+ },
49
+ invalid_principles_structure: {
50
+ msg: `.pd/config.yaml principles field is not an object: ${detail}`,
51
+ action: 'Ensure principles field in .pd/config.yaml is a YAML mapping (e.g. principles: { outputLanguage: zh-CN })',
52
+ },
53
+ };
54
+ const entry = reasons[reason];
55
+ return `${entry.msg}. Falling back to default: ${DEFAULT_OUTPUT_LANGUAGE}. nextAction: ${entry.action}`;
56
+ }
57
+
58
+ /**
59
+ * Read principles.outputLanguage from workspace .pd/config.yaml.
60
+ *
61
+ * Returns ResolvedOutputLanguage with:
62
+ * - outputLanguage: the effective language to use
63
+ * - degradationWarning: present if config was malformed or unreadable (ERR-002/ERR-009)
64
+ *
65
+ * Distinguishes between "not configured" (legitimate default, no warning)
66
+ * and "config broken" (degraded with reason + nextAction).
67
+ */
68
+ export function readOutputLanguageFromWorkspace(workspaceDir: string): ResolvedOutputLanguage {
69
+ const configPath = path.join(workspaceDir, '.pd', 'config.yaml');
70
+
71
+ if (!fs.existsSync(configPath)) {
72
+ // No config file → legitimate default, no warning
73
+ return resolveOutputLanguage(undefined);
74
+ }
75
+
76
+ let raw: string;
77
+ try {
78
+ raw = fs.readFileSync(configPath, 'utf8');
79
+ } catch (err) {
80
+ return {
81
+ outputLanguage: DEFAULT_OUTPUT_LANGUAGE,
82
+ degradationWarning: configDegradationWarning('read_error', String(err)),
83
+ };
84
+ }
85
+
86
+ let parsed: unknown;
87
+ try {
88
+ parsed = yaml.load(raw, { schema: yaml.JSON_SCHEMA });
89
+ } catch (err) {
90
+ return {
91
+ outputLanguage: DEFAULT_OUTPUT_LANGUAGE,
92
+ degradationWarning: configDegradationWarning('yaml_parse_error', String(err)),
93
+ };
94
+ }
95
+
96
+ if (!isRecord(parsed)) {
97
+ return {
98
+ outputLanguage: DEFAULT_OUTPUT_LANGUAGE,
99
+ degradationWarning: configDegradationWarning('invalid_config_root', typeof parsed),
100
+ };
101
+ }
102
+
103
+ if (!Object.hasOwn(parsed, 'principles')) {
104
+ // No principles section → legitimate default, no warning
105
+ return resolveOutputLanguage(undefined);
106
+ }
107
+
108
+ const principlesRaw = parsed.principles;
109
+ if (!isRecord(principlesRaw)) {
110
+ return {
111
+ outputLanguage: DEFAULT_OUTPUT_LANGUAGE,
112
+ degradationWarning: configDegradationWarning('invalid_principles_structure', typeof principlesRaw),
113
+ };
114
+ }
115
+
116
+ if (!Object.hasOwn(principlesRaw, 'outputLanguage')) {
117
+ // No outputLanguage key → legitimate default, no warning
118
+ return resolveOutputLanguage(undefined);
119
+ }
120
+
121
+ return resolveOutputLanguage(principlesRaw.outputLanguage);
122
+ }
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
 
@@ -0,0 +1,108 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import * as path from 'path';
3
+ import * as os from 'os';
4
+ import {
5
+ isProductionWorkspace,
6
+ guardUatWorkspace,
7
+ getSafeUatWorkspacePath,
8
+ formatGuardRefusal,
9
+ type GuardRefusal,
10
+ } from './production-workspace-guard.js';
11
+
12
+ const PRODUCTION_PATHS = [
13
+ 'D:\\.openclaw\\workspace',
14
+ 'C:\\.openclaw\\workspace',
15
+ 'C:\\Users\\Administrator\\.openclaw\\workspace',
16
+ path.join(os.homedir(), '.openclaw', 'workspace'),
17
+ ];
18
+
19
+ const SAFE_PATHS = [
20
+ 'D:\\.openclaw\\workspace-test',
21
+ 'D:\\.openclaw\\workspace-backup',
22
+ path.join(os.tmpdir(), 'pd-uat-workspace'),
23
+ path.join(os.tmpdir(), 'pd-test-any'),
24
+ 'C:\\completely-unrelated\\work',
25
+ ];
26
+
27
+ describe('isProductionWorkspace', () => {
28
+ it.each(PRODUCTION_PATHS)('detects production: %s', (prodPath) => {
29
+ expect(isProductionWorkspace(path.resolve(prodPath))).toBe(true);
30
+ });
31
+ it.each(SAFE_PATHS)('allows safe: %s', (safePath) => {
32
+ expect(isProductionWorkspace(path.resolve(safePath))).toBe(false);
33
+ });
34
+ it('detects descendant', () => {
35
+ expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace\\sub\\child'))).toBe(true);
36
+ });
37
+ it('rejects sibling workspace-test (ERR-030)', () => {
38
+ expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace-test'))).toBe(false);
39
+ });
40
+ it('rejects sibling workspace-backup (ERR-030)', () => {
41
+ expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace-backup'))).toBe(false);
42
+ });
43
+ });
44
+
45
+ describe('guardUatWorkspace', () => {
46
+ describe('refused', () => {
47
+ it.each(PRODUCTION_PATHS)('refuses production: %s', (prodPath) => {
48
+ const r = guardUatWorkspace(prodPath, 'test');
49
+ expect(r.refused).toBe(true);
50
+ if (r.refused) {
51
+ expect(r.reason).toContain('UAT/runtime test commands are not allowed');
52
+ expect(r.nextAction).toContain('temporary workspace');
53
+ }
54
+ });
55
+ it('refuses descendant', () => {
56
+ expect(guardUatWorkspace('D:\\.openclaw\\workspace\\subdir', 'test').refused).toBe(true);
57
+ });
58
+ });
59
+ describe('allowed', () => {
60
+ it.each(SAFE_PATHS)('allows safe: %s', (safePath) => {
61
+ const r = guardUatWorkspace(safePath, 'test');
62
+ expect(r.refused).toBe(false);
63
+ });
64
+ it.each(['D:\\.openclaw\\workspace-test', 'D:\\.openclaw\\workspace-backup'])(
65
+ 'allows sibling: %s (ERR-030)', (p) => {
66
+ expect(guardUatWorkspace(p, 'test').refused).toBe(false);
67
+ });
68
+ });
69
+ });
70
+
71
+ describe('JSON output (EP-04)', () => {
72
+ it('outputs single object with reason and nextAction', () => {
73
+ const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
74
+ const json = formatGuardRefusal(r as GuardRefusal, 'test', true);
75
+ const parsed = JSON.parse(json);
76
+ expect(parsed).toMatchObject({
77
+ status: 'refused', reason: expect.any(String), nextAction: expect.any(String),
78
+ workspace: expect.any(String), isProduction: true,
79
+ });
80
+ expect(Array.isArray(parsed)).toBe(false);
81
+ });
82
+ it('no console prefixes in JSON', () => {
83
+ const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
84
+ const json = formatGuardRefusal(r as GuardRefusal, 'test', true);
85
+ expect(json).not.toContain('[pd-cli]');
86
+ expect(json).not.toContain('ERROR:');
87
+ expect(json.trim().startsWith('{')).toBe(true);
88
+ });
89
+ });
90
+
91
+ describe('text output (EP-03)', () => {
92
+ it('includes reason and nextAction', () => {
93
+ const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
94
+ const text = formatGuardRefusal(r as GuardRefusal, 'test', false);
95
+ expect(text).toContain('Reason:');
96
+ expect(text).toContain('Next Action:');
97
+ });
98
+ });
99
+
100
+ describe('getSafeUatWorkspacePath', () => {
101
+ it('returns deterministic temp path', () => {
102
+ const p1 = getSafeUatWorkspacePath();
103
+ const p2 = getSafeUatWorkspacePath();
104
+ expect(p1).toBe(p2);
105
+ expect(p1).toContain(os.tmpdir());
106
+ expect(p1).toContain('pd-uat-workspace');
107
+ });
108
+ });
@@ -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
+ }
@@ -93,6 +93,7 @@ vi.mock('@principles/core/runtime-v2', () => ({
93
93
  RuntimeStateManager: MockRuntimeStateManager,
94
94
  loadLedger: mockLoadLedger,
95
95
  getLedgerFilePathPublic: mockGetLedgerFilePath,
96
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
96
97
  }));
97
98
 
98
99
  vi.mock('../../src/principle-tree-ledger-adapter.js', () => ({
@@ -70,6 +70,7 @@ vi.mock('@principles/core/runtime-v2', () => ({
70
70
  CandidateIntakeService: MockCandidateIntakeService,
71
71
  CandidateIntakeError: MockCandidateIntakeError,
72
72
  RuntimeStateManager: MockRuntimeStateManager,
73
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
73
74
  }));
74
75
 
75
76
  vi.mock('../../src/principle-tree-ledger-adapter.js', () => ({
@@ -89,6 +89,7 @@ vi.mock('@principles/core/runtime-v2', async (importOriginal) => {
89
89
  warnings: input.warnings ?? [],
90
90
  })),
91
91
  remediationAction: vi.fn((input: unknown) => input),
92
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
92
93
  };
93
94
  });
94
95
 
@@ -27,6 +27,7 @@ vi.mock('@principles/core/runtime-v2', async (importOriginal) => {
27
27
  ...original,
28
28
  RuntimeStateManager: MockRuntimeStateManager,
29
29
  decideInternalizationRoute: vi.fn(),
30
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
30
31
  };
31
32
  });
32
33
 
@@ -31,6 +31,7 @@ const { mockStateManager, MockRuntimeStateManager } = vi.hoisted(() => {
31
31
  vi.mock('@principles/core/runtime-v2', () => ({
32
32
  RuntimeStateManager: MockRuntimeStateManager,
33
33
  decideInternalizationRoute: vi.fn(),
34
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
34
35
  }));
35
36
 
36
37
  vi.mock('../../src/resolve-workspace.js', () => ({
@@ -78,6 +78,7 @@ vi.mock('@principles/core/runtime-v2', () => {
78
78
  agentId: 'main',
79
79
  }),
80
80
  isRuntimeConfigError: vi.fn().mockReturnValue(false),
81
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
81
82
  run: vi.fn().mockResolvedValue({
82
83
  status: 'succeeded',
83
84
  taskId: 'test-task-1',
@@ -101,6 +102,10 @@ vi.mock('../../src/principle-tree-ledger-adapter.js', () => ({
101
102
  PrincipleTreeLedgerAdapter: MockPrincipleTreeLedgerAdapter,
102
103
  }));
103
104
 
105
+ vi.mock('../../src/config-reader.js', () => ({
106
+ readOutputLanguageFromWorkspace: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
107
+ }));
108
+
104
109
  import { handleDiagnoseRun, type DiagnoseRunOptions } from '../../src/commands/diagnose.js';
105
110
 
106
111
  const SUCCEEDED_RESULT = {
@@ -38,6 +38,7 @@ vi.mock('@principles/core/runtime-v2', () => ({
38
38
  }),
39
39
  auditCandidateLedgerConsistency: mockAuditCandidateLedgerConsistency,
40
40
  getLedgerFilePathPublic: vi.fn().mockReturnValue('/fake/workspace/.state/principle_training_state.json'),
41
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
41
42
  }));
42
43
 
43
44
  vi.mock('../../src/resolve-workspace.js', () => ({
@@ -34,6 +34,7 @@ vi.mock('@principles/core/runtime-v2', () => ({
34
34
  agentId: 'main',
35
35
  }),
36
36
  isRuntimeConfigError: vi.fn().mockReturnValue(false),
37
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
37
38
  }));
38
39
 
39
40
  import { handlePainRecord } from '../../src/commands/pain-record.js';
@@ -103,6 +103,7 @@ vi.mock('@principles/core/runtime-v2', () => {
103
103
  CandidateIntakeService: MockCandidateIntakeService,
104
104
  resolveRuntimeConfig: mockResolveRuntimeConfig,
105
105
  isRuntimeConfigError: vi.fn().mockReturnValue(false),
106
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
106
107
  run: mockRun,
107
108
  status: vi.fn(),
108
109
  };
@@ -112,6 +113,10 @@ vi.mock('../../src/principle-tree-ledger-adapter.js', () => ({
112
113
  PrincipleTreeLedgerAdapter: MockPrincipleTreeLedgerAdapter,
113
114
  }));
114
115
 
116
+ vi.mock('../../src/config-reader.js', () => ({
117
+ readOutputLanguageFromWorkspace: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
118
+ }));
119
+
115
120
  import { handlePainRetry } from '../../src/commands/pain-retry.js';
116
121
 
117
122
  // ── Test Data ──────────────────────────────────────────────────────────────────
@@ -20,6 +20,7 @@ vi.mock('@principles/core/runtime-v2', async (importOriginal) => {
20
20
  },
21
21
  };
22
22
  }),
23
+ resolveOutputLanguage: vi.fn().mockReturnValue({ outputLanguage: 'zh-CN' }),
23
24
  };
24
25
  });
25
26