@principles/pd-cli 1.128.2 → 1.129.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.
- package/dist/commands/runtime-internalization-run-rulehost.d.ts.map +1 -1
- package/dist/commands/runtime-internalization-run-rulehost.js +61 -7
- package/dist/commands/runtime-internalization-run-rulehost.js.map +1 -1
- package/dist/services/__tests__/rulehost-readiness.test.d.ts +2 -0
- package/dist/services/__tests__/rulehost-readiness.test.d.ts.map +1 -0
- package/dist/services/__tests__/rulehost-readiness.test.js +314 -0
- package/dist/services/__tests__/rulehost-readiness.test.js.map +1 -0
- package/dist/services/rulehost-readiness.d.ts +62 -0
- package/dist/services/rulehost-readiness.d.ts.map +1 -0
- package/dist/services/rulehost-readiness.js +214 -0
- package/dist/services/rulehost-readiness.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/runtime-internalization-run-rulehost.ts +68 -6
- package/src/services/__tests__/rulehost-readiness.test.ts +366 -0
- package/src/services/rulehost-readiness.ts +326 -0
- package/tests/commands/run-rulehost-handler.test.ts +277 -0
- package/tests/services/rulehost-pipeline-e2e.test.ts +71 -61
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for RuleHost readiness resolver (PRI-461).
|
|
3
|
+
*
|
|
4
|
+
* Tests the three readiness statuses (ready / text_principle_only / refused)
|
|
5
|
+
* against various config combinations, including the default installed config
|
|
6
|
+
* pattern from create-principles-disciple.
|
|
7
|
+
*
|
|
8
|
+
* ERR refs:
|
|
9
|
+
* - EP-02 / ERR-024, ERR-025: tests exercise the real config resolution path
|
|
10
|
+
* - EP-03: refused/text_principle_only include reason + nextAction
|
|
11
|
+
* - EP-07: readiness uses the same config source as the pipeline
|
|
12
|
+
* - EP-09: tests cover the default installed config, not just hand-written happy paths
|
|
13
|
+
*/
|
|
14
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
15
|
+
import * as fs from 'node:fs';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
import * as os from 'node:os';
|
|
18
|
+
import * as yaml from 'js-yaml';
|
|
19
|
+
import { resolveRuleHostReadiness } from '../rulehost-readiness.js';
|
|
20
|
+
|
|
21
|
+
// ── workspace helpers ─────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
function mkTmpDir(): string {
|
|
24
|
+
return fs.mkdtempSync(path.join(os.tmpdir(), 'pd-readiness-'));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface ConfigOptions {
|
|
28
|
+
/** Override agent enabled flags. Defaults: all pi-ai agents enabled. */
|
|
29
|
+
readonly agentEnabled?: Partial<Record<'dreamer' | 'philosopher' | 'scribe' | 'artificer' | 'evaluator', boolean>>;
|
|
30
|
+
/** Override runtime profile type. Default: 'pi-ai'. */
|
|
31
|
+
readonly profileType?: 'pi-ai' | 'openclaw';
|
|
32
|
+
/** Override API key env var name. Default: 'TEST_API_KEY'. */
|
|
33
|
+
readonly apiKeyEnv?: string;
|
|
34
|
+
/** Override code_rule_capability feature flag. Default: true. */
|
|
35
|
+
readonly codeRuleCapabilityEnabled?: boolean;
|
|
36
|
+
/** Override profile fields. */
|
|
37
|
+
readonly profileProvider?: string;
|
|
38
|
+
readonly profileModel?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function writeConfig(workspaceDir: string, opts: ConfigOptions = {}): void {
|
|
42
|
+
const configDir = path.join(workspaceDir, '.pd');
|
|
43
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
44
|
+
|
|
45
|
+
const profileType = opts.profileType ?? 'pi-ai';
|
|
46
|
+
const apiKeyEnv = opts.apiKeyEnv ?? 'TEST_API_KEY';
|
|
47
|
+
const profileId = profileType === 'pi-ai' ? 'pi-ai.default' : 'openclaw.default';
|
|
48
|
+
|
|
49
|
+
const profile: Record<string, unknown> = profileType === 'pi-ai'
|
|
50
|
+
? {
|
|
51
|
+
type: 'pi-ai',
|
|
52
|
+
provider: opts.profileProvider ?? 'anthropic',
|
|
53
|
+
model: opts.profileModel ?? 'claude-sonnet',
|
|
54
|
+
apiKeyEnv,
|
|
55
|
+
}
|
|
56
|
+
: {
|
|
57
|
+
type: 'openclaw',
|
|
58
|
+
source: 'default',
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const defaultEnabled: Record<string, boolean> = {
|
|
62
|
+
dreamer: true,
|
|
63
|
+
philosopher: true,
|
|
64
|
+
scribe: true,
|
|
65
|
+
artificer: true,
|
|
66
|
+
evaluator: true,
|
|
67
|
+
};
|
|
68
|
+
const agentEnabled = { ...defaultEnabled, ...opts.agentEnabled };
|
|
69
|
+
|
|
70
|
+
const cfg = {
|
|
71
|
+
version: 1,
|
|
72
|
+
features: {
|
|
73
|
+
prompt: { category: 'core', enabled: true },
|
|
74
|
+
code_tool_hook: { category: 'core', enabled: true },
|
|
75
|
+
defer_archive: { category: 'core', enabled: true },
|
|
76
|
+
code_rule_capability: { category: 'core', enabled: opts.codeRuleCapabilityEnabled ?? true },
|
|
77
|
+
},
|
|
78
|
+
runtimeProfiles: {
|
|
79
|
+
[profileId]: profile,
|
|
80
|
+
},
|
|
81
|
+
internalAgents: {
|
|
82
|
+
defaultRuntime: profileId,
|
|
83
|
+
agents: {
|
|
84
|
+
diagnostician: { enabled: true, runtimeProfile: profileId },
|
|
85
|
+
dreamer: { enabled: agentEnabled.dreamer, runtimeProfile: profileId },
|
|
86
|
+
philosopher: { enabled: agentEnabled.philosopher, runtimeProfile: profileId },
|
|
87
|
+
scribe: { enabled: agentEnabled.scribe, runtimeProfile: profileId },
|
|
88
|
+
artificer: { enabled: agentEnabled.artificer, runtimeProfile: profileId },
|
|
89
|
+
evaluator: { enabled: agentEnabled.evaluator, runtimeProfile: profileId },
|
|
90
|
+
rolloutReviewer: { enabled: false, runtimeProfile: profileId },
|
|
91
|
+
correctionObserver: { enabled: false, runtimeProfile: profileId },
|
|
92
|
+
empathyObserver: { enabled: false, runtimeProfile: profileId },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
ui: { diagnostics: { mode: 'simple' } },
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
fs.writeFileSync(path.join(configDir, 'config.yaml'), yaml.dump(cfg), 'utf8');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Write the default installed config pattern from create-principles-disciple.
|
|
103
|
+
* This uses openclaw profiles and has philosopher/evaluator disabled.
|
|
104
|
+
*/
|
|
105
|
+
function writeDefaultInstalledConfig(workspaceDir: string): void {
|
|
106
|
+
const configDir = path.join(workspaceDir, '.pd');
|
|
107
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
108
|
+
|
|
109
|
+
const cfg = {
|
|
110
|
+
version: 1,
|
|
111
|
+
features: {
|
|
112
|
+
prompt: { category: 'core', enabled: true },
|
|
113
|
+
code_tool_hook: { category: 'core', enabled: true },
|
|
114
|
+
defer_archive: { category: 'core', enabled: true },
|
|
115
|
+
code_rule_capability: { category: 'core', enabled: true },
|
|
116
|
+
},
|
|
117
|
+
runtimeProfiles: {
|
|
118
|
+
'openclaw.default': { type: 'openclaw', source: 'default' },
|
|
119
|
+
},
|
|
120
|
+
internalAgents: {
|
|
121
|
+
defaultRuntime: 'openclaw.default',
|
|
122
|
+
agents: {
|
|
123
|
+
diagnostician: { enabled: true, runtimeProfile: 'openclaw.default' },
|
|
124
|
+
dreamer: { enabled: true, runtimeProfile: 'openclaw.default' },
|
|
125
|
+
philosopher: { enabled: false, runtimeProfile: 'openclaw.default' },
|
|
126
|
+
scribe: { enabled: true, runtimeProfile: 'openclaw.default' },
|
|
127
|
+
artificer: { enabled: true, runtimeProfile: 'openclaw.default' },
|
|
128
|
+
evaluator: { enabled: false, runtimeProfile: 'openclaw.default' },
|
|
129
|
+
rolloutReviewer: { enabled: false, runtimeProfile: 'openclaw.default' },
|
|
130
|
+
correctionObserver: { enabled: false, runtimeProfile: 'openclaw.default' },
|
|
131
|
+
empathyObserver: { enabled: false, runtimeProfile: 'openclaw.default' },
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
ui: { diagnostics: { mode: 'simple' } },
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
fs.writeFileSync(path.join(configDir, 'config.yaml'), yaml.dump(cfg), 'utf8');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function writeMalformedConfig(workspaceDir: string): void {
|
|
141
|
+
const configDir = path.join(workspaceDir, '.pd');
|
|
142
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
143
|
+
fs.writeFileSync(path.join(configDir, 'config.yaml'), 'this: is: not: valid: yaml: [', 'utf8');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function envLookup(env: Record<string, string | undefined>): (name: string) => string | undefined {
|
|
147
|
+
return (name) => env[name];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ── tests ─────────────────────────────────────────────────────────────────
|
|
151
|
+
|
|
152
|
+
describe('resolveRuleHostReadiness', () => {
|
|
153
|
+
let workspaceDir: string;
|
|
154
|
+
|
|
155
|
+
beforeEach(() => {
|
|
156
|
+
workspaceDir = mkTmpDir();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
afterEach(() => {
|
|
160
|
+
try { fs.rmSync(workspaceDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// ── refused: config malformed ───────────────────────────────────────────
|
|
164
|
+
|
|
165
|
+
it('returns refused when config is malformed', () => {
|
|
166
|
+
writeMalformedConfig(workspaceDir);
|
|
167
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({}));
|
|
168
|
+
expect(result.status).toBe('refused');
|
|
169
|
+
expect(result.reason).toMatch(/config/i);
|
|
170
|
+
expect(result.nextAction).toBeDefined();
|
|
171
|
+
expect(result.nextAction.length).toBeGreaterThan(0);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// ── refused: required agent issues ──────────────────────────────────────
|
|
175
|
+
|
|
176
|
+
it('returns refused when dreamer is disabled', () => {
|
|
177
|
+
writeConfig(workspaceDir, { agentEnabled: { dreamer: false } });
|
|
178
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
179
|
+
expect(result.status).toBe('refused');
|
|
180
|
+
expect(result.reason).toMatch(/dreamer/i);
|
|
181
|
+
expect(result.nextAction).toBeDefined();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('returns refused when philosopher is disabled', () => {
|
|
185
|
+
writeConfig(workspaceDir, { agentEnabled: { philosopher: false } });
|
|
186
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
187
|
+
expect(result.status).toBe('refused');
|
|
188
|
+
expect(result.reason).toMatch(/philosopher/i);
|
|
189
|
+
expect(result.nextAction).toBeDefined();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('returns refused when scribe is disabled', () => {
|
|
193
|
+
writeConfig(workspaceDir, { agentEnabled: { scribe: false } });
|
|
194
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
195
|
+
expect(result.status).toBe('refused');
|
|
196
|
+
expect(result.reason).toMatch(/scribe/i);
|
|
197
|
+
expect(result.nextAction).toBeDefined();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('returns refused when dreamer uses openclaw profile (not pi-ai)', () => {
|
|
201
|
+
writeConfig(workspaceDir, { profileType: 'openclaw' });
|
|
202
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({}));
|
|
203
|
+
expect(result.status).toBe('refused');
|
|
204
|
+
expect(result.reason).toMatch(/pi-ai|openclaw|profile/i);
|
|
205
|
+
expect(result.nextAction).toBeDefined();
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('returns refused when API key env var is not set', () => {
|
|
209
|
+
writeConfig(workspaceDir, { apiKeyEnv: 'MISSING_API_KEY' });
|
|
210
|
+
// Do NOT set MISSING_API_KEY in env
|
|
211
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({}));
|
|
212
|
+
expect(result.status).toBe('refused');
|
|
213
|
+
expect(result.reason).toMatch(/API key|apiKeyEnv|MISSING_API_KEY/i);
|
|
214
|
+
expect(result.nextAction).toBeDefined();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('returns refused when API key env var is set but empty', () => {
|
|
218
|
+
writeConfig(workspaceDir, { apiKeyEnv: 'EMPTY_API_KEY' });
|
|
219
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ EMPTY_API_KEY: '' }));
|
|
220
|
+
expect(result.status).toBe('refused');
|
|
221
|
+
expect(result.reason).toMatch(/API key|apiKeyEnv|EMPTY_API_KEY/i);
|
|
222
|
+
expect(result.nextAction).toBeDefined();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// ── text_principle_only: code-rule capability off ───────────────────────
|
|
226
|
+
|
|
227
|
+
it('returns text_principle_only when code_rule_capability flag is OFF', () => {
|
|
228
|
+
writeConfig(workspaceDir, { codeRuleCapabilityEnabled: false });
|
|
229
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
230
|
+
expect(result.status).toBe('text_principle_only');
|
|
231
|
+
expect(result.reason).toMatch(/code_rule_capability|feature flag/i);
|
|
232
|
+
expect(result.nextAction).toBeDefined();
|
|
233
|
+
expect(result.codeRuleCapability.enabled).toBe(false);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('returns text_principle_only when evaluator is disabled', () => {
|
|
237
|
+
writeConfig(workspaceDir, { agentEnabled: { evaluator: false } });
|
|
238
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
239
|
+
expect(result.status).toBe('text_principle_only');
|
|
240
|
+
expect(result.reason).toMatch(/evaluator/i);
|
|
241
|
+
expect(result.nextAction).toBeDefined();
|
|
242
|
+
expect(result.codeRuleCapability.enabled).toBe(false);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('returns text_principle_only when artificer is disabled', () => {
|
|
246
|
+
writeConfig(workspaceDir, { agentEnabled: { artificer: false } });
|
|
247
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
248
|
+
expect(result.status).toBe('text_principle_only');
|
|
249
|
+
expect(result.reason).toMatch(/artificer/i);
|
|
250
|
+
expect(result.nextAction).toBeDefined();
|
|
251
|
+
expect(result.codeRuleCapability.enabled).toBe(false);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('returns text_principle_only when artificer API key is missing', () => {
|
|
255
|
+
// Use a config where artificer has a different profile with a missing API key
|
|
256
|
+
const configDir = path.join(workspaceDir, '.pd');
|
|
257
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
258
|
+
const cfg = {
|
|
259
|
+
version: 1,
|
|
260
|
+
features: {
|
|
261
|
+
prompt: { category: 'core', enabled: true },
|
|
262
|
+
code_tool_hook: { category: 'core', enabled: true },
|
|
263
|
+
defer_archive: { category: 'core', enabled: true },
|
|
264
|
+
code_rule_capability: { category: 'core', enabled: true },
|
|
265
|
+
},
|
|
266
|
+
runtimeProfiles: {
|
|
267
|
+
'pi-ai.main': { type: 'pi-ai', provider: 'anthropic', model: 'claude-sonnet', apiKeyEnv: 'MAIN_API_KEY' },
|
|
268
|
+
'pi-ai.artificer': { type: 'pi-ai', provider: 'openrouter', model: 'gpt-4', apiKeyEnv: 'ARTIFICER_API_KEY' },
|
|
269
|
+
},
|
|
270
|
+
internalAgents: {
|
|
271
|
+
defaultRuntime: 'pi-ai.main',
|
|
272
|
+
agents: {
|
|
273
|
+
diagnostician: { enabled: true, runtimeProfile: 'pi-ai.main' },
|
|
274
|
+
dreamer: { enabled: true, runtimeProfile: 'pi-ai.main' },
|
|
275
|
+
philosopher: { enabled: true, runtimeProfile: 'pi-ai.main' },
|
|
276
|
+
scribe: { enabled: true, runtimeProfile: 'pi-ai.main' },
|
|
277
|
+
artificer: { enabled: true, runtimeProfile: 'pi-ai.artificer' },
|
|
278
|
+
evaluator: { enabled: true, runtimeProfile: 'pi-ai.main' },
|
|
279
|
+
rolloutReviewer: { enabled: false, runtimeProfile: 'pi-ai.main' },
|
|
280
|
+
correctionObserver: { enabled: false, runtimeProfile: 'pi-ai.main' },
|
|
281
|
+
empathyObserver: { enabled: false, runtimeProfile: 'pi-ai.main' },
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
ui: { diagnostics: { mode: 'simple' } },
|
|
285
|
+
};
|
|
286
|
+
fs.writeFileSync(path.join(configDir, 'config.yaml'), yaml.dump(cfg), 'utf8');
|
|
287
|
+
|
|
288
|
+
// Do NOT set ARTIFICER_API_KEY
|
|
289
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ MAIN_API_KEY: 'sk-main' }));
|
|
290
|
+
expect(result.status).toBe('text_principle_only');
|
|
291
|
+
expect(result.reason).toMatch(/artificer|ARTIFICER_API_KEY/i);
|
|
292
|
+
expect(result.nextAction).toBeDefined();
|
|
293
|
+
expect(result.codeRuleCapability.enabled).toBe(false);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// ── ready: all conditions met ───────────────────────────────────────────
|
|
297
|
+
|
|
298
|
+
it('returns ready when all agents enabled with pi-ai profiles and API keys set', () => {
|
|
299
|
+
writeConfig(workspaceDir, {});
|
|
300
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
301
|
+
expect(result.status).toBe('ready');
|
|
302
|
+
expect(result.codeRuleCapability.enabled).toBe(true);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// ── default installed config (EP-09) ────────────────────────────────────
|
|
306
|
+
|
|
307
|
+
it('returns refused for the default installed config (openclaw profiles, philosopher/evaluator disabled)', () => {
|
|
308
|
+
writeDefaultInstalledConfig(workspaceDir);
|
|
309
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({}));
|
|
310
|
+
// Default config uses openclaw profiles — dreamer/scribe can't get pi-ai adapters.
|
|
311
|
+
// Philosopher is also disabled. So the status is refused.
|
|
312
|
+
expect(result.status).toBe('refused');
|
|
313
|
+
expect(result.reason).toBeDefined();
|
|
314
|
+
expect(result.reason.length).toBeGreaterThan(0);
|
|
315
|
+
expect(result.nextAction).toBeDefined();
|
|
316
|
+
expect(result.nextAction.length).toBeGreaterThan(0);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// ── result structure ────────────────────────────────────────────────────
|
|
320
|
+
|
|
321
|
+
it('includes per-agent statuses in the result', () => {
|
|
322
|
+
writeConfig(workspaceDir, {});
|
|
323
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
324
|
+
expect(result.agentStatuses).toBeDefined();
|
|
325
|
+
expect(result.agentStatuses.dreamer).toBeDefined();
|
|
326
|
+
expect(result.agentStatuses.philosopher).toBeDefined();
|
|
327
|
+
expect(result.agentStatuses.scribe).toBeDefined();
|
|
328
|
+
expect(result.agentStatuses.artificer).toBeDefined();
|
|
329
|
+
expect(result.agentStatuses.evaluator).toBeDefined();
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('includes codeRuleCapability in the result', () => {
|
|
333
|
+
writeConfig(workspaceDir, { codeRuleCapabilityEnabled: false });
|
|
334
|
+
const result = resolveRuleHostReadiness(workspaceDir, envLookup({ TEST_API_KEY: 'sk-test' }));
|
|
335
|
+
expect(result.codeRuleCapability).toBeDefined();
|
|
336
|
+
expect(result.codeRuleCapability.enabled).toBe(false);
|
|
337
|
+
expect(result.codeRuleCapability.disabledReason).toBeDefined();
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// ── env var injection ───────────────────────────────────────────────────
|
|
341
|
+
|
|
342
|
+
it('uses injected getEnvVar callback instead of process.env', () => {
|
|
343
|
+
writeConfig(workspaceDir, { apiKeyEnv: 'INJECTED_KEY' });
|
|
344
|
+
// Do NOT set INJECTED_KEY in process.env
|
|
345
|
+
const result = resolveRuleHostReadiness(workspaceDir, (name) => {
|
|
346
|
+
if (name === 'INJECTED_KEY') return 'sk-injected';
|
|
347
|
+
return undefined;
|
|
348
|
+
});
|
|
349
|
+
expect(result.status).toBe('ready');
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// ── Runtime Contract #9: graceful degradation with reason ────────────────
|
|
353
|
+
|
|
354
|
+
it('returns refused when getEnvVar throws (Runtime Contract #9: never throws)', () => {
|
|
355
|
+
writeConfig(workspaceDir, { apiKeyEnv: 'TEST_KEY' });
|
|
356
|
+
const throwingGetEnv = (): string => {
|
|
357
|
+
throw new Error('env access failed');
|
|
358
|
+
};
|
|
359
|
+
const result = resolveRuleHostReadiness(workspaceDir, throwingGetEnv);
|
|
360
|
+
expect(result.status).toBe('refused');
|
|
361
|
+
expect(result.reason).toContain('readiness_resolution_failed');
|
|
362
|
+
expect(result.reason).toContain('env access failed');
|
|
363
|
+
expect(result.nextAction).toBeDefined();
|
|
364
|
+
expect(result.codeRuleCapability.enabled).toBe(false);
|
|
365
|
+
});
|
|
366
|
+
});
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RuleHost Readiness Resolver — PRI-461
|
|
3
|
+
*
|
|
4
|
+
* Checks all preconditions for `run-rulehost` BEFORE constructing adapters,
|
|
5
|
+
* returning one of three user-visible statuses:
|
|
6
|
+
* - `ready`: all agents enabled with pi-ai profiles and API keys; code-rule capability ON
|
|
7
|
+
* - `text_principle_only`: dreamer/philosopher/scribe ready, but code-rule capability OFF
|
|
8
|
+
* (flag disabled, or artificer/evaluator not ready)
|
|
9
|
+
* - `refused`: dreamer/philosopher/scribe chain broken (disabled, wrong profile, missing API key,
|
|
10
|
+
* or config malformed)
|
|
11
|
+
*
|
|
12
|
+
* This module NEVER throws. It always returns a structured result with reason + nextAction,
|
|
13
|
+
* so the CLI handler can emit a clear status instead of an opaque adapter-resolution failure.
|
|
14
|
+
*
|
|
15
|
+
* ERR refs:
|
|
16
|
+
* - EP-02 / ERR-024, ERR-025: wired into the production handler path (runtime-internalization-run-rulehost.ts)
|
|
17
|
+
* - EP-03: refused/text_principle_only include reason + nextAction
|
|
18
|
+
* - EP-07: uses the same config source as the pipeline (resolveRuntimeFromPdConfig)
|
|
19
|
+
* - EP-09: tests cover the default installed config pattern
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
resolveAgentRuntimeBinding,
|
|
24
|
+
checkAgentRuntimeReadiness,
|
|
25
|
+
computeFeatureFlagsFromConfig,
|
|
26
|
+
isFeatureEnabled,
|
|
27
|
+
} from '@principles/core/runtime-v2';
|
|
28
|
+
import type {
|
|
29
|
+
EffectivePdConfig,
|
|
30
|
+
RuntimeProfile,
|
|
31
|
+
} from '@principles/core/runtime-v2';
|
|
32
|
+
import { resolveRuntimeFromPdConfig } from './resolve-runtime-from-pd-config.js';
|
|
33
|
+
|
|
34
|
+
// ── Types ────────────────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
export type RuleHostReadinessStatus = 'ready' | 'text_principle_only' | 'refused';
|
|
37
|
+
|
|
38
|
+
export interface AgentReadiness {
|
|
39
|
+
readonly status: 'ready' | 'disabled' | 'not_ready' | 'needs_setup' | 'wrong_profile_type';
|
|
40
|
+
readonly reason?: string;
|
|
41
|
+
readonly nextAction?: string;
|
|
42
|
+
readonly profileId?: string;
|
|
43
|
+
readonly profileType?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface CodeRuleCapabilityReadiness {
|
|
47
|
+
readonly enabled: boolean;
|
|
48
|
+
readonly disabledReason?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface RuleHostReadinessResult {
|
|
52
|
+
readonly status: RuleHostReadinessStatus;
|
|
53
|
+
readonly reason: string;
|
|
54
|
+
readonly nextAction: string;
|
|
55
|
+
readonly agentStatuses: {
|
|
56
|
+
readonly dreamer: AgentReadiness;
|
|
57
|
+
readonly philosopher: AgentReadiness;
|
|
58
|
+
readonly scribe: AgentReadiness;
|
|
59
|
+
readonly artificer: AgentReadiness;
|
|
60
|
+
readonly evaluator: AgentReadiness;
|
|
61
|
+
};
|
|
62
|
+
readonly codeRuleCapability: CodeRuleCapabilityReadiness;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The 5 internal agents that RuleHost readiness checks.
|
|
67
|
+
* Narrowed from InternalAgentName so array iteration indexes a known-shape map.
|
|
68
|
+
*/
|
|
69
|
+
type RuleHostAgentName = 'dreamer' | 'philosopher' | 'scribe' | 'artificer' | 'evaluator';
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Mutable builder type for agentStatuses during construction.
|
|
73
|
+
* The readonly RuleHostReadinessResult['agentStatuses'] is assigned from this.
|
|
74
|
+
*/
|
|
75
|
+
interface AgentStatusesMap {
|
|
76
|
+
dreamer: AgentReadiness;
|
|
77
|
+
philosopher: AgentReadiness;
|
|
78
|
+
scribe: AgentReadiness;
|
|
79
|
+
artificer: AgentReadiness;
|
|
80
|
+
evaluator: AgentReadiness;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ── Constants ─────────────────────────────────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Agents required for the text-principle path (dreamer → philosopher → scribe).
|
|
87
|
+
* If any of these is not ready, the pipeline cannot produce even text principles.
|
|
88
|
+
*/
|
|
89
|
+
const REQUIRED_AGENTS: readonly RuleHostAgentName[] = ['dreamer', 'philosopher', 'scribe'];
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Agents required for the code-rule capability (artificer + evaluator).
|
|
93
|
+
* Both must be ready for the adversarial loop to run.
|
|
94
|
+
*/
|
|
95
|
+
const CODE_RULE_AGENTS: readonly RuleHostAgentName[] = ['artificer', 'evaluator'];
|
|
96
|
+
|
|
97
|
+
// ── Implementation ────────────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Check a single agent's readiness for the RuleHost pipeline.
|
|
101
|
+
*
|
|
102
|
+
* Checks (in order):
|
|
103
|
+
* 1. Agent enabled (via resolveAgentRuntimeBinding)
|
|
104
|
+
* 2. Profile exists (via resolveAgentRuntimeBinding)
|
|
105
|
+
* 3. Profile type is 'pi-ai' (RuleHost needs PiAiRuntimeAdapter)
|
|
106
|
+
* 4. Profile is ready (via checkAgentRuntimeReadiness — provider/model/apiKeyEnv set, env var exists)
|
|
107
|
+
*
|
|
108
|
+
* Returns a structured AgentReadiness result. Never throws.
|
|
109
|
+
*/
|
|
110
|
+
function checkAgentReadiness(
|
|
111
|
+
effective: EffectivePdConfig,
|
|
112
|
+
agentName: RuleHostAgentName,
|
|
113
|
+
getEnvVar: (name: string) => string | undefined,
|
|
114
|
+
): AgentReadiness {
|
|
115
|
+
const binding = resolveAgentRuntimeBinding(effective, agentName);
|
|
116
|
+
|
|
117
|
+
if (!binding.ok) {
|
|
118
|
+
return {
|
|
119
|
+
status: binding.readiness === 'disabled' ? 'disabled' : binding.readiness === 'needs_setup' ? 'needs_setup' : 'not_ready',
|
|
120
|
+
reason: binding.reason,
|
|
121
|
+
nextAction: binding.nextAction,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const profile: RuntimeProfile = binding.profile;
|
|
126
|
+
const profileType = profile.type;
|
|
127
|
+
|
|
128
|
+
// RuleHost pipeline constructs PiAiRuntimeAdapter instances, so the profile
|
|
129
|
+
// must be pi-ai. OpenClaw profiles delegate to OpenClaw's own runtime, which
|
|
130
|
+
// is not available in the RuleHost pipeline context.
|
|
131
|
+
if (profileType !== 'pi-ai') {
|
|
132
|
+
return {
|
|
133
|
+
status: 'wrong_profile_type',
|
|
134
|
+
reason: `Agent '${agentName}' uses profile '${binding.profileId}' with type '${profileType}', but RuleHost requires pi-ai profile type`,
|
|
135
|
+
nextAction: `Add a pi-ai runtime profile to .pd/config.yaml and assign it to ${agentName} via internalAgents.agents.${agentName}.runtimeProfile`,
|
|
136
|
+
profileId: binding.profileId,
|
|
137
|
+
profileType,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const readiness = checkAgentRuntimeReadiness(profile, getEnvVar);
|
|
142
|
+
if (readiness.readiness !== 'ready') {
|
|
143
|
+
return {
|
|
144
|
+
status: readiness.readiness === 'needs_setup' ? 'needs_setup' : 'not_ready',
|
|
145
|
+
reason: readiness.reason,
|
|
146
|
+
nextAction: readiness.nextAction,
|
|
147
|
+
profileId: binding.profileId,
|
|
148
|
+
profileType,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
status: 'ready',
|
|
154
|
+
profileId: binding.profileId,
|
|
155
|
+
profileType,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ── Helpers (defined before public function to satisfy no-use-before-define) ──
|
|
160
|
+
|
|
161
|
+
interface ReadinessResultParts {
|
|
162
|
+
readonly agentStatuses: AgentStatusesMap;
|
|
163
|
+
readonly codeRuleCapability: CodeRuleCapabilityReadiness;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function emptyAgentStatuses(): AgentStatusesMap {
|
|
167
|
+
const empty: AgentReadiness = { status: 'not_ready', reason: 'not checked' };
|
|
168
|
+
return {
|
|
169
|
+
dreamer: empty,
|
|
170
|
+
philosopher: empty,
|
|
171
|
+
scribe: empty,
|
|
172
|
+
artificer: empty,
|
|
173
|
+
evaluator: empty,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function buildRefusedResult(
|
|
178
|
+
reason: string,
|
|
179
|
+
nextAction: string,
|
|
180
|
+
parts: ReadinessResultParts,
|
|
181
|
+
): RuleHostReadinessResult {
|
|
182
|
+
return {
|
|
183
|
+
status: 'refused',
|
|
184
|
+
reason,
|
|
185
|
+
nextAction,
|
|
186
|
+
agentStatuses: parts.agentStatuses,
|
|
187
|
+
codeRuleCapability: parts.codeRuleCapability,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function buildTextPrincipleOnlyResult(
|
|
192
|
+
reason: string,
|
|
193
|
+
nextAction: string,
|
|
194
|
+
parts: ReadinessResultParts,
|
|
195
|
+
): RuleHostReadinessResult {
|
|
196
|
+
return {
|
|
197
|
+
status: 'text_principle_only',
|
|
198
|
+
reason,
|
|
199
|
+
nextAction,
|
|
200
|
+
agentStatuses: parts.agentStatuses,
|
|
201
|
+
codeRuleCapability: parts.codeRuleCapability,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function resolveRuleHostReadinessUnchecked(
|
|
206
|
+
workspaceDir: string,
|
|
207
|
+
getEnvVar: (name: string) => string | undefined,
|
|
208
|
+
): RuleHostReadinessResult {
|
|
209
|
+
// ── Step 1: Load config ──
|
|
210
|
+
const { configLoadResult } = resolveRuntimeFromPdConfig(workspaceDir, getEnvVar);
|
|
211
|
+
|
|
212
|
+
if (!configLoadResult.ok) {
|
|
213
|
+
const [firstError] = configLoadResult.errors;
|
|
214
|
+
const reason = `config_malformed: ${firstError?.reason ?? 'unknown config error'}`;
|
|
215
|
+
const nextAction = firstError?.nextAction ?? 'Fix .pd/config.yaml syntax and retry';
|
|
216
|
+
return buildRefusedResult(reason, nextAction, {
|
|
217
|
+
agentStatuses: emptyAgentStatuses(),
|
|
218
|
+
codeRuleCapability: { enabled: false, disabledReason: 'config_malformed' },
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const { effective } = configLoadResult;
|
|
223
|
+
|
|
224
|
+
// ── Step 2: Check required agents (dreamer, philosopher, scribe) ──
|
|
225
|
+
// Start with all 5 keys set to 'not checked'; updated as each agent is checked.
|
|
226
|
+
const agentStatuses: AgentStatusesMap = emptyAgentStatuses();
|
|
227
|
+
const requiredFailures: string[] = [];
|
|
228
|
+
|
|
229
|
+
for (const agentName of REQUIRED_AGENTS) {
|
|
230
|
+
const readiness = checkAgentReadiness(effective, agentName, getEnvVar);
|
|
231
|
+
agentStatuses[agentName] = readiness;
|
|
232
|
+
if (readiness.status !== 'ready') {
|
|
233
|
+
requiredFailures.push(`${agentName}: ${readiness.reason ?? readiness.status}`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (requiredFailures.length > 0) {
|
|
238
|
+
const reason = `required_agents_not_ready: ${requiredFailures.join('; ')}`;
|
|
239
|
+
const nextAction = `Fix the following agent issues in .pd/config.yaml: ${requiredFailures.join('; ')}. RuleHost requires dreamer, philosopher, and scribe agents to be enabled with pi-ai runtime profiles and valid API keys.`;
|
|
240
|
+
return buildRefusedResult(reason, nextAction, {
|
|
241
|
+
agentStatuses,
|
|
242
|
+
codeRuleCapability: { enabled: false, disabledReason: 'required_agents_not_ready' },
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// ── Step 3: Check code_rule_capability feature flag ──
|
|
247
|
+
const featureFlags = computeFeatureFlagsFromConfig(effective);
|
|
248
|
+
if (!isFeatureEnabled(featureFlags, 'code_rule_capability')) {
|
|
249
|
+
const reason = 'code_rule_capability feature flag is disabled';
|
|
250
|
+
const nextAction = "Enable code_rule_capability in .pd/config.yaml features.code_rule_capability.enabled to run the full adversarial pipeline. Text-principle-only mode is available.";
|
|
251
|
+
// Still check artificer/evaluator for reporting, but they don't affect the status
|
|
252
|
+
for (const agentName of CODE_RULE_AGENTS) {
|
|
253
|
+
agentStatuses[agentName] = checkAgentReadiness(effective, agentName, getEnvVar);
|
|
254
|
+
}
|
|
255
|
+
return buildTextPrincipleOnlyResult(reason, nextAction, {
|
|
256
|
+
agentStatuses,
|
|
257
|
+
codeRuleCapability: { enabled: false, disabledReason: reason },
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ── Step 4: Check code-rule agents (artificer, evaluator) ──
|
|
262
|
+
const codeRuleFailures: string[] = [];
|
|
263
|
+
|
|
264
|
+
for (const agentName of CODE_RULE_AGENTS) {
|
|
265
|
+
const readiness = checkAgentReadiness(effective, agentName, getEnvVar);
|
|
266
|
+
agentStatuses[agentName] = readiness;
|
|
267
|
+
if (readiness.status !== 'ready') {
|
|
268
|
+
codeRuleFailures.push(`${agentName}: ${readiness.reason ?? readiness.status}`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (codeRuleFailures.length > 0) {
|
|
273
|
+
const reason = `code_rule_agents_not_ready: ${codeRuleFailures.join('; ')}`;
|
|
274
|
+
const nextAction = `Fix the following agent issues to enable code-rule capability: ${codeRuleFailures.join('; ')}. Text-principle-only mode is available with the current configuration.`;
|
|
275
|
+
return buildTextPrincipleOnlyResult(reason, nextAction, {
|
|
276
|
+
agentStatuses,
|
|
277
|
+
codeRuleCapability: { enabled: false, disabledReason: reason },
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ── Step 5: All checks pass → ready ──
|
|
282
|
+
return {
|
|
283
|
+
status: 'ready',
|
|
284
|
+
reason: 'All agents ready with pi-ai profiles and valid API keys. Code-rule capability is ON.',
|
|
285
|
+
nextAction: 'Pass --confirm to run the full pipeline.',
|
|
286
|
+
agentStatuses,
|
|
287
|
+
codeRuleCapability: { enabled: true },
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Resolve RuleHost readiness from the workspace's .pd/config.yaml.
|
|
293
|
+
*
|
|
294
|
+
* This is the production entry point called by the `run-rulehost` handler
|
|
295
|
+
* BEFORE constructing any adapters. It returns a structured result so the
|
|
296
|
+
* handler can emit a clear status instead of an opaque adapter-resolution failure.
|
|
297
|
+
*
|
|
298
|
+
* This function NEVER throws. Any unexpected exception from config loading,
|
|
299
|
+
* feature-flag computation, agent checks, or getEnvVar is caught and converted
|
|
300
|
+
* to a `refused` result with reason + nextAction (Runtime Contract #9).
|
|
301
|
+
*
|
|
302
|
+
* @param workspaceDir - The workspace directory containing .pd/config.yaml
|
|
303
|
+
* @param getEnvVar - Env var accessor, defaults to process.env. Injected for testability.
|
|
304
|
+
* @returns Structured readiness result. Never throws.
|
|
305
|
+
*/
|
|
306
|
+
export function resolveRuleHostReadiness(
|
|
307
|
+
workspaceDir: string,
|
|
308
|
+
getEnvVar: (name: string) => string | undefined = (name) => process.env[name],
|
|
309
|
+
): RuleHostReadinessResult {
|
|
310
|
+
try {
|
|
311
|
+
return resolveRuleHostReadinessUnchecked(workspaceDir, getEnvVar);
|
|
312
|
+
} catch (error: unknown) {
|
|
313
|
+
const message =
|
|
314
|
+
error instanceof Error && error.message.length > 0
|
|
315
|
+
? error.message
|
|
316
|
+
: 'unknown readiness resolution error';
|
|
317
|
+
return buildRefusedResult(
|
|
318
|
+
`readiness_resolution_failed: ${message}`,
|
|
319
|
+
'Fix the readiness resolution error and retry. Run `pd config doctor` for diagnostics.',
|
|
320
|
+
{
|
|
321
|
+
agentStatuses: emptyAgentStatuses(),
|
|
322
|
+
codeRuleCapability: { enabled: false, disabledReason: 'readiness_resolution_failed' },
|
|
323
|
+
},
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
}
|