create-principles-disciple 1.148.9 → 1.148.10

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.
@@ -278,3 +278,10 @@ export function getPdCliEntry(paths, mode) {
278
278
  const root = mode === 'canonical' ? paths.pdCliDir : path.join(paths.openClawExtensionDir, 'pd-cli');
279
279
  return path.join(root, 'dist', 'index.js');
280
280
  }
281
+ /**
282
+ * PD home resolution (PRI-872): production keeps the historical `os.homedir()`
283
+ * derivation; `PD_EXPERIMENT_ROOT` opts into a fail-closed isolated root. This
284
+ * is the ONE authority for "which PD home does this process own" — components
285
+ * must not re-derive it locally.
286
+ */
287
+ export { PD_EXPERIMENT_ROOT_ENV, PD_OPENCLAW_HOME_ENV, PD_HOME_MODES, PD_HOME_FAILURE_REASONS, resolvePdHome, isPathWithinRoot, } from './pd-home.js';
@@ -0,0 +1,55 @@
1
+ /** Opt-in isolation switch. Absolute path to the experiment's own root. */
2
+ export declare const PD_EXPERIMENT_ROOT_ENV = "PD_EXPERIMENT_ROOT";
3
+ /** OpenClaw's own home override (pre-existing; honoured in production mode). */
4
+ export declare const PD_OPENCLAW_HOME_ENV = "OPENCLAW_HOME";
5
+ export declare const PD_HOME_MODES: readonly ['production', 'experiment'];
6
+ export type PdHomeMode = (typeof PD_HOME_MODES)[number];
7
+ export interface PdHomePaths {
8
+ readonly mode: PdHomeMode;
9
+ /**
10
+ * The effective home all layout paths derive from. Production: the real user
11
+ * home. Experiment: the resolved experiment root (a stand-in home, so
12
+ * `getInstallLayoutPaths(homeDir)` keeps working unchanged).
13
+ */
14
+ readonly homeDir: string;
15
+ /** `<homeDir>/.pd` — active record, transactions, bootstrap executor. */
16
+ readonly pdHome: string;
17
+ /** `<homeDir>/.openclaw` (or `OPENCLAW_HOME`). */
18
+ readonly openclawHome: string;
19
+ /** `<homeDir>/.pd-console` — the console's workspace registry. */
20
+ readonly consoleConfigDir: string;
21
+ /** `<homeDir>/.codex` — suggested Codex home for the experiment launcher. */
22
+ readonly codexHome: string;
23
+ /** Present only in experiment mode: the canonical (realpath'd) root. */
24
+ readonly experimentRoot?: string;
25
+ }
26
+ export declare const PD_HOME_FAILURE_REASONS: readonly ['experiment_root_empty', 'experiment_root_not_absolute', 'experiment_root_missing', 'experiment_root_not_directory', 'experiment_root_unresolvable', 'experiment_root_overlaps_production', 'experiment_path_escapes_root', 'openclaw_home_outside_experiment'];
27
+ export type PdHomeFailureReason = (typeof PD_HOME_FAILURE_REASONS)[number];
28
+ export interface PdHomeFailure {
29
+ readonly ok: false;
30
+ readonly reason: PdHomeFailureReason;
31
+ readonly message: string;
32
+ readonly nextAction: string;
33
+ readonly details: Readonly<Record<string, string>>;
34
+ }
35
+ export interface PdHomeSuccess extends PdHomePaths {
36
+ readonly ok: true;
37
+ }
38
+ export type PdHomeResolution = PdHomeSuccess | PdHomeFailure;
39
+ export interface ResolvePdHomeOptions {
40
+ /** Environment to read. Defaults to `process.env`; injected by tests. */
41
+ readonly env?: Readonly<Record<string, string | undefined>>;
42
+ /** Real user home. Defaults to `os.homedir()`; injected by tests. */
43
+ readonly homeDir?: string;
44
+ }
45
+ /**
46
+ * True when `candidate` is inside `root` (or equal to it). Uses
47
+ * `path.relative`, which on win32 compares case-insensitively — the reason not
48
+ * to hand-roll a `startsWith` prefix check.
49
+ */
50
+ export declare function isPathWithinRoot(root: string, candidate: string): boolean;
51
+ /**
52
+ * Resolve the PD home for this process. Callers must treat `ok: false` as a
53
+ * startup refusal — there is no degraded mode.
54
+ */
55
+ export declare function resolvePdHome(options?: ResolvePdHomeOptions): PdHomeResolution;
@@ -0,0 +1,226 @@
1
+ /**
2
+ * PD home resolution — the ONE place that answers "which user-level PD home
3
+ * does THIS process belong to?".
4
+ *
5
+ * Why this exists (PRI-872 / SPEC `install-mjs-dev-prod-isolation-review` §6):
6
+ * before this module every PD component re-derived the home on its own with
7
+ * `os.homedir()` (pd-console had ten such sites plus the bootstrap executor),
8
+ * so a second, experimental instance silently shared the production install:
9
+ * the update page read `~/.pd`, the "apply" action spawned the production
10
+ * bootstrap executor, and the console registry (`~/.pd-console/workspaces.json`)
11
+ * was read and written by both instances.
12
+ *
13
+ * Why an explicit root instead of `HOME`: on Windows `os.homedir()` reads
14
+ * USERPROFILE while the installer's `getHomeDir()` prefers `HOME`
15
+ * (`create-principles-disciple/src/mvp-config.ts`). Pointing `HOME` elsewhere
16
+ * therefore made the installer and the console disagree about the same
17
+ * installation instead of isolating it. The isolation switch is its own
18
+ * variable and the whole derivation hangs off it.
19
+ *
20
+ * FAIL-CLOSED: when `PD_EXPERIMENT_ROOT` is present but unusable (relative,
21
+ * missing, not a directory, unresolvable, or overlapping the real production
22
+ * dirs) this returns a refusal. It never falls back to the real user home —
23
+ * a silent fallback is exactly the failure mode this module exists to remove.
24
+ */
25
+ import * as fs from 'node:fs';
26
+ import * as os from 'node:os';
27
+ import * as path from 'node:path';
28
+ /** Opt-in isolation switch. Absolute path to the experiment's own root. */
29
+ export const PD_EXPERIMENT_ROOT_ENV = 'PD_EXPERIMENT_ROOT';
30
+ /** OpenClaw's own home override (pre-existing; honoured in production mode). */
31
+ export const PD_OPENCLAW_HOME_ENV = 'OPENCLAW_HOME';
32
+ export const PD_HOME_MODES = ['production', 'experiment'];
33
+ export const PD_HOME_FAILURE_REASONS = [
34
+ 'experiment_root_empty',
35
+ 'experiment_root_not_absolute',
36
+ 'experiment_root_missing',
37
+ 'experiment_root_not_directory',
38
+ 'experiment_root_unresolvable',
39
+ 'experiment_root_overlaps_production',
40
+ 'experiment_path_escapes_root',
41
+ 'openclaw_home_outside_experiment',
42
+ ];
43
+ function failure(input) {
44
+ return { ok: false, ...input };
45
+ }
46
+ /**
47
+ * True when `candidate` is inside `root` (or equal to it). Uses
48
+ * `path.relative`, which on win32 compares case-insensitively — the reason not
49
+ * to hand-roll a `startsWith` prefix check.
50
+ */
51
+ export function isPathWithinRoot(root, candidate) {
52
+ const relative = path.relative(path.resolve(root), path.resolve(candidate));
53
+ if (relative === '')
54
+ return true;
55
+ if (path.isAbsolute(relative))
56
+ return false;
57
+ return relative !== '..' && !relative.startsWith(`..${path.sep}`);
58
+ }
59
+ /**
60
+ * Realpath the deepest EXISTING ancestor of `target`, then re-append the
61
+ * missing tail. Unlike `realpathSync(target)` this also answers for paths that
62
+ * do not exist yet (PD creates its own directories), while still resolving any
63
+ * link that already sits on the path.
64
+ */
65
+ function canonicalizeDeepestExisting(target) {
66
+ const missing = [];
67
+ let current = path.resolve(target);
68
+ for (;;) {
69
+ try {
70
+ const real = fs.realpathSync.native(current);
71
+ return missing.length === 0 ? real : path.join(real, ...missing.reverse());
72
+ }
73
+ catch {
74
+ // Missing or broken link — walk up one level and retry.
75
+ }
76
+ const parent = path.dirname(current);
77
+ if (parent === current)
78
+ return null;
79
+ missing.push(path.basename(current));
80
+ current = parent;
81
+ }
82
+ }
83
+ function productionPaths(realHome, env) {
84
+ const openclawOverride = env[PD_OPENCLAW_HOME_ENV]?.trim();
85
+ // Preserved production behaviour: OPENCLAW_HOME wins when set, no realpath
86
+ // rewrite (existing installs rely on the literal resolved path).
87
+ const openclawHome = openclawOverride !== undefined && openclawOverride.length > 0
88
+ ? path.resolve(openclawOverride)
89
+ : path.join(realHome, '.openclaw');
90
+ return {
91
+ ok: true,
92
+ mode: 'production',
93
+ homeDir: realHome,
94
+ pdHome: path.join(realHome, '.pd'),
95
+ openclawHome,
96
+ consoleConfigDir: path.join(realHome, '.pd-console'),
97
+ codexHome: path.join(realHome, '.codex'),
98
+ };
99
+ }
100
+ /**
101
+ * Resolve the PD home for this process. Callers must treat `ok: false` as a
102
+ * startup refusal — there is no degraded mode.
103
+ */
104
+ export function resolvePdHome(options = {}) {
105
+ const env = options.env ?? process.env;
106
+ const realHome = path.resolve(options.homeDir ?? os.homedir());
107
+ const rawRoot = env[PD_EXPERIMENT_ROOT_ENV];
108
+ if (rawRoot === undefined) {
109
+ return productionPaths(realHome, env);
110
+ }
111
+ const declared = rawRoot.trim();
112
+ if (declared.length === 0) {
113
+ return failure({
114
+ reason: 'experiment_root_empty',
115
+ message: `${PD_EXPERIMENT_ROOT_ENV} is set to an empty value.`,
116
+ nextAction: `Set ${PD_EXPERIMENT_ROOT_ENV} to an absolute path outside the production install, or unset it to run the production instance.`,
117
+ details: { [PD_EXPERIMENT_ROOT_ENV]: rawRoot },
118
+ });
119
+ }
120
+ if (!path.isAbsolute(declared)) {
121
+ return failure({
122
+ reason: 'experiment_root_not_absolute',
123
+ message: `${PD_EXPERIMENT_ROOT_ENV} must be an absolute path (got "${declared}").`,
124
+ nextAction: 'Use an absolute path such as D:/pd-labs/pri-872; a relative root would resolve against the current directory and drift.',
125
+ details: { [PD_EXPERIMENT_ROOT_ENV]: rawRoot },
126
+ });
127
+ }
128
+ let stats;
129
+ try {
130
+ stats = fs.statSync(declared);
131
+ }
132
+ catch {
133
+ return failure({
134
+ reason: 'experiment_root_missing',
135
+ message: `Experiment root does not exist: ${declared}`,
136
+ nextAction: 'Create the directory first (an empty directory is enough); PD will populate its own state under it.',
137
+ details: { experimentRoot: declared },
138
+ });
139
+ }
140
+ if (!stats.isDirectory()) {
141
+ return failure({
142
+ reason: 'experiment_root_not_directory',
143
+ message: `Experiment root is not a directory: ${declared}`,
144
+ nextAction: 'Point the root at a directory, not a file or a device.',
145
+ details: { experimentRoot: declared },
146
+ });
147
+ }
148
+ // Canonicalise once: every path below is derived from the REAL root, so a
149
+ // junction/symlink/case difference cannot make later checks disagree with
150
+ // where files actually land.
151
+ let rootReal;
152
+ try {
153
+ rootReal = fs.realpathSync.native(declared);
154
+ }
155
+ catch {
156
+ return failure({
157
+ reason: 'experiment_root_unresolvable',
158
+ message: `Experiment root cannot be resolved to a real path: ${declared}`,
159
+ nextAction: 'Check the path for a broken junction/symlink and retry.',
160
+ details: { experimentRoot: declared },
161
+ });
162
+ }
163
+ // The experiment must be DISJOINT from the production control plane in both
164
+ // directions: the root must not live inside ~/.pd, ~/.pd-console or
165
+ // ~/.openclaw, and it must not be an ancestor of them (e.g. C:/Users/me).
166
+ const productionDirs = [
167
+ ['.pd', path.join(realHome, '.pd')],
168
+ ['.pd-console', path.join(realHome, '.pd-console')],
169
+ ['.openclaw', path.join(realHome, '.openclaw')],
170
+ ];
171
+ for (const [label, productionDir] of productionDirs) {
172
+ const productionReal = canonicalizeDeepestExisting(productionDir) ?? path.resolve(productionDir);
173
+ if (isPathWithinRoot(rootReal, productionReal) || isPathWithinRoot(productionReal, rootReal)) {
174
+ return failure({
175
+ reason: 'experiment_root_overlaps_production',
176
+ message: `Experiment root overlaps the production ${label} directory (${productionReal}).`,
177
+ nextAction: 'Choose a root that contains neither the production state nor sits inside it — for example D:/pd-labs/<task>.',
178
+ details: { experimentRoot: rootReal, productionDir: productionReal },
179
+ });
180
+ }
181
+ }
182
+ const experimentPaths = [
183
+ ['pdHome', path.join(rootReal, '.pd')],
184
+ ['openclawHome', path.join(rootReal, '.openclaw')],
185
+ ['consoleConfigDir', path.join(rootReal, '.pd-console')],
186
+ ['codexHome', path.join(rootReal, '.codex')],
187
+ ];
188
+ for (const [label, candidate] of experimentPaths) {
189
+ // A pre-existing link at (or above) a derived path is the escape route:
190
+ // `<root>/.pd` junctioned to the production `~/.pd` would pass a naive
191
+ // prefix check on the declared path while writing production state.
192
+ const candidateReal = canonicalizeDeepestExisting(candidate);
193
+ if (candidateReal === null || !isPathWithinRoot(rootReal, candidateReal)) {
194
+ return failure({
195
+ reason: 'experiment_path_escapes_root',
196
+ message: `${label} resolves outside the experiment root: ${candidate} -> ${candidateReal ?? 'unresolvable'}`,
197
+ nextAction: 'Remove the junction/symlink at that path (or point it inside the root) and retry.',
198
+ details: { experimentRoot: rootReal, path: candidate, resolved: candidateReal ?? '' },
199
+ });
200
+ }
201
+ }
202
+ const openclawOverride = env[PD_OPENCLAW_HOME_ENV]?.trim();
203
+ let openclawHome = path.join(rootReal, '.openclaw');
204
+ if (openclawOverride !== undefined && openclawOverride.length > 0) {
205
+ const overrideReal = canonicalizeDeepestExisting(openclawOverride) ?? path.resolve(openclawOverride);
206
+ if (!path.isAbsolute(openclawOverride) || !isPathWithinRoot(rootReal, overrideReal)) {
207
+ return failure({
208
+ reason: 'openclaw_home_outside_experiment',
209
+ message: `${PD_OPENCLAW_HOME_ENV} points outside the experiment root: ${openclawOverride} -> ${overrideReal}`,
210
+ nextAction: `Unset ${PD_OPENCLAW_HOME_ENV} (the experiment derives its own openclaw home) or point it inside ${rootReal}.`,
211
+ details: { experimentRoot: rootReal, [PD_OPENCLAW_HOME_ENV]: openclawOverride },
212
+ });
213
+ }
214
+ openclawHome = path.resolve(openclawOverride);
215
+ }
216
+ return {
217
+ ok: true,
218
+ mode: 'experiment',
219
+ homeDir: rootReal,
220
+ pdHome: path.join(rootReal, '.pd'),
221
+ openclawHome,
222
+ consoleConfigDir: path.join(rootReal, '.pd-console'),
223
+ codexHome: path.join(rootReal, '.codex'),
224
+ experimentRoot: rootReal,
225
+ };
226
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/install-layout",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-principles-disciple",
3
- "version": "1.148.9",
3
+ "version": "1.148.10",
4
4
  "description": "Interactive CLI installer for Principles Disciple OpenClaw plugin",
5
5
  "type": "module",
6
6
  "bin": {
@@ -63,7 +63,7 @@
63
63
  "dependencies": {
64
64
  "@earendil-works/pi-agent-core": "1.2.0",
65
65
  "@earendil-works/pi-ai": "1.4.0",
66
- "@principles/install-layout": "^0.5.0",
66
+ "@principles/install-layout": "^0.6.0",
67
67
  "@sinclair/typebox": "^0.34.52",
68
68
  "@tufjs/models": "3.0.1",
69
69
  "@inquirer/prompts": "^8.7.2",
@@ -101,5 +101,5 @@
101
101
  "pd": {
102
102
  "bundledPluginVersion": "3.1.5"
103
103
  },
104
- "gitHead": "7d898b78cdcc789b8102129cfaeb5318d70d9153"
104
+ "gitHead": "80e8f5e27a6b7b395f73d2334477eb34317c3cc0"
105
105
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/pd-cli",
3
- "version": "1.168.1",
3
+ "version": "2.0.0",
4
4
  "description": "PD CLI — Pain recording, sample management, and governance tasks for Principles Disciple",
5
5
  "type": "module",
6
6
  "bin": {