@principles/pd-cli 1.147.11 → 1.147.13

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,550 @@
1
+ /**
2
+ * PRI-708 — run-once rollout revision routing: cross entry point equivalence.
3
+ *
4
+ * Owner acceptance (P0 A/B/C): the SAME durable state must produce the SAME
5
+ * governance decision whether it travels the production consumer-cycle
6
+ * assembly or the run-once CLI entry. Only the LLM boundary is scripted
7
+ * (PRI-661 parity-test pattern) — the evaluator stage, the rollout reviewer
8
+ * runner, validators, stores and the reopen path are all REAL code against
9
+ * REAL SQLite. No runner.run() mocks, no fabricated verdict objects.
10
+ *
11
+ * P0 A (routing correctness): needs_revision → reopen scribe/artificer,
12
+ * identical targetTaskKind / revisionIteration
13
+ * / revisionCauseId / payload on both entries.
14
+ * P0 B (durable recovery): crash-window resume via the run-once entry —
15
+ * same intent → same effect, LLM not re-asked,
16
+ * no duplicate reopen (causeId materialize).
17
+ * P0 C (exhaustion safety): appliedCount ≥ 2 → rollout_revision_budget_
18
+ * exhausted NHR, decision-capable (eligible /
19
+ * allowedActions non-empty) — wiring must not
20
+ * bypass the revision budget.
21
+ */
22
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
23
+ import * as fs from 'node:fs';
24
+ import * as os from 'node:os';
25
+ import * as path from 'node:path';
26
+ import {
27
+ RuntimeStateManager,
28
+ InternalizationOrchestrator,
29
+ RolloutReviewerRunner,
30
+ DefaultRolloutReviewerValidator,
31
+ storeEmitter,
32
+ hydratePITaskRecord,
33
+ createPITaskDiagnosticJson,
34
+ mergePITaskMetadata,
35
+ reopenTaskForRevision,
36
+ collectOwnerDecisionFacts,
37
+ deriveOwnerDecisionCapability,
38
+ factStoreFromStateManager,
39
+ type PITaskMetadata,
40
+ type PDRuntimeAdapter,
41
+ } from '@principles/core/runtime-v2';
42
+ import { createRolloutGovernanceDeps, saveHostToolDeclaration } from '@principles/host-runtime';
43
+ import { handleRuntimeInternalizationRunOnce } from '../../src/commands/runtime-internalization-run-once.js';
44
+
45
+ // ── LLM boundary (the ONLY scripted seam) ────────────────────────────────────
46
+ // resolveRuntimeAdapterFromConfig is the CLI's adapter seam; mocking it keeps
47
+ // the handler, orchestrator, runners, validators and SQLite stores real while
48
+ // making the reviewer/evaluator verdicts deterministic.
49
+
50
+ const scriptedPayloads: Record<string, unknown> = {};
51
+ const llmCalls: Record<string, number> = {};
52
+
53
+ vi.mock('../../src/services/runtime-adapter-resolver.js', () => ({
54
+ resolveRuntimeAdapterFromConfig: (input: { runnerKind: string }) => scriptedAdapterFor(input.runnerKind),
55
+ }));
56
+
57
+ function scriptedAdapterFor(runnerKind: string): PDRuntimeAdapter {
58
+ return {
59
+ kind: () => 'test-double',
60
+ startRun: async () => {
61
+ llmCalls[runnerKind] = (llmCalls[runnerKind] ?? 0) + 1;
62
+ return {
63
+ runId: `run-${runnerKind}-${llmCalls[runnerKind]}`,
64
+ runtimeKind: 'test-double',
65
+ startedAt: new Date().toISOString(),
66
+ };
67
+ },
68
+ pollRun: async (runId: string) => ({ status: 'succeeded' as const, runId }),
69
+ fetchOutput: async (runId: string) => ({ runId, payload: scriptedPayloads[runnerKind] }),
70
+ cancelRun: async () => undefined,
71
+ } as unknown as PDRuntimeAdapter;
72
+ }
73
+
74
+ // ── fixture: deterministic lineage + evaluation payload ─────────────────────
75
+
76
+ const SCRIBE_ID = 'scribe-parity-1';
77
+ const ARTIFICER_ID = 'artificer-parity-1';
78
+ const EVAL_ID = 'evaluator-parity-1';
79
+ const ROLL_ID_SEED = 'rollout-parity-craft'; // only used by crafted (T3/T4) workspaces
80
+ const SCRIBE_ART = 'pi-art-scribe-parity-1';
81
+ const ARTIFICER_ART = 'pi-art-artificer-parity-1';
82
+ const EVAL_ART = 'pi-art-eval-parity-1';
83
+
84
+ function evaluationApprovedPayload(): Record<string, unknown> {
85
+ return {
86
+ taskId: EVAL_ID,
87
+ sourceArtificerArtifactId: ARTIFICER_ART,
88
+ evaluation: {
89
+ decision: 'approved',
90
+ summary: 'parity fixture evaluation',
91
+ score: 0.85,
92
+ strengths: ['well structured'],
93
+ concerns: [],
94
+ requiredChanges: [],
95
+ },
96
+ sourceTrace: { artificerArtifactId: ARTIFICER_ART },
97
+ risks: [],
98
+ generatedAt: new Date().toISOString(),
99
+ };
100
+ }
101
+
102
+ function rolloutNeedsRevisionPayload(taskId: string, evaluatorArtifactId: string): Record<string, unknown> {
103
+ return {
104
+ taskId,
105
+ sourceEvaluatorArtifactId: evaluatorArtifactId,
106
+ review: {
107
+ decision: 'needs_revision',
108
+ summary: 'parity fixture review',
109
+ confidence: 0.7,
110
+ requiredChanges: ['Fix the principle wording to cover the observed failure'],
111
+ rolloutRisks: ['wording drift'],
112
+ safetyChecks: [],
113
+ },
114
+ sourceTrace: { evaluatorArtifactId },
115
+ risks: [],
116
+ generatedAt: new Date().toISOString(),
117
+ };
118
+ }
119
+
120
+ async function seedWorkspace(workspaceDir: string): Promise<RuntimeStateManager> {
121
+ saveHostToolDeclaration(workspaceDir, {
122
+ version: 1,
123
+ hostKind: 'openclaw',
124
+ mappings: [{ rawToolName: 'Write', canonicalKind: 'write' }],
125
+ declaredAt: new Date().toISOString(),
126
+ });
127
+ const sm = new RuntimeStateManager({ workspaceDir });
128
+ await sm.initialize();
129
+ await sm.piArtifactStore.upsertArtifact({
130
+ artifactId: SCRIBE_ART, artifactKind: 'principle', sourceTaskId: SCRIBE_ID,
131
+ lineageArtifactIds: [], validationStatus: 'validated',
132
+ contentJson: JSON.stringify({ principleDraft: { title: 'parity-p', statement: '原则正文' } }),
133
+ createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
134
+ });
135
+ await sm.piArtifactStore.upsertArtifact({
136
+ artifactId: ARTIFICER_ART, artifactKind: 'rule', sourceTaskId: ARTIFICER_ID,
137
+ lineageArtifactIds: [SCRIBE_ART], validationStatus: 'validated',
138
+ contentJson: JSON.stringify({
139
+ implementationPlan: { summary: 'parity plan', targetSurface: 'src/*.ts', changes: [], tests: [], rolloutNotes: [], confidence: 0.8 },
140
+ sourceTrace: { scribeArtifactId: SCRIBE_ART },
141
+ }),
142
+ createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
143
+ });
144
+ const chain: readonly [string, string, string[], 'succeeded' | 'pending'][] = [
145
+ [SCRIBE_ID, 'scribe', [], 'succeeded'],
146
+ [ARTIFICER_ID, 'artificer', [SCRIBE_ID], 'succeeded'],
147
+ [EVAL_ID, 'evaluator', [ARTIFICER_ID], 'pending'],
148
+ ];
149
+ for (const [id, kind, deps, status] of chain) {
150
+ // 先 pending 建 task → lease → succeeded(lease 门只接受 pending/retry_wait)
151
+ await sm.createTask({
152
+ taskId: id, taskKind: kind, status: 'pending', attemptCount: 0, maxAttempts: 3,
153
+ diagnosticJson: createPITaskDiagnosticJson({
154
+ dependencyTaskIds: deps, channel: 'prompt', timeoutMs: 300_000,
155
+ inputArtifactRefs: [], outputArtifactRefs: [], correlationId: 'parity',
156
+ }),
157
+ });
158
+ if (status === 'succeeded') {
159
+ await sm.acquireLease({ taskId: id, owner: 'parity-seed', runtimeKind: 'test-double' });
160
+ await sm.markTaskSucceeded(id);
161
+ }
162
+ }
163
+ return sm;
164
+ }
165
+
166
+ /** Real evaluator stage through the REAL run-once handler → real evaluator artifact + real rollout successor task. */
167
+ async function driveEvaluatorLeg(workspaceDir: string): Promise<{ rolloutTaskId: string; evaluatorArtifactId: string }> {
168
+ scriptedPayloads['evaluator'] = evaluationApprovedPayload();
169
+ await handleRuntimeInternalizationRunOnce({
170
+ workspace: workspaceDir, runner: 'evaluator', runtime: 'test-double', allowTestDouble: true, json: true,
171
+ });
172
+ const out = JSON.parse(consoleLogSpy.mock.calls.at(-1)![0] as string) as Record<string, unknown>;
173
+ expect(out.decision).toBe('would_lease');
174
+ expect(out.enqueueDecision).toBe('successor_created');
175
+ expect(out.successorKind).toBe('rollout_reviewer');
176
+ return {
177
+ rolloutTaskId: (out.successorTaskIds as string[])[0],
178
+ evaluatorArtifactId: out.artifactId as string,
179
+ };
180
+ }
181
+
182
+ interface GovernanceOutcome {
183
+ rollout: {
184
+ status: string;
185
+ runnerDecision: string | undefined;
186
+ payloadRevisionIteration: number | undefined;
187
+ payloadStatus: string | undefined;
188
+ payloadTargetKind: string | undefined;
189
+ payloadRequiredChanges: readonly string[] | undefined;
190
+ intentStatus: string | undefined;
191
+ humanReviewContext: PITaskMetadata['humanReviewContext'];
192
+ };
193
+ scribe: {
194
+ status: string;
195
+ attemptCount: number;
196
+ revisionCount: number;
197
+ revisionCauseId: string | undefined;
198
+ revisionFeedback: string | undefined;
199
+ };
200
+ }
201
+
202
+ async function readGovernanceOutcome(sm: RuntimeStateManager, rolloutTaskId: string): Promise<GovernanceOutcome> {
203
+ const rolloutRaw = await sm.getTask(rolloutTaskId);
204
+ expect(rolloutRaw).not.toBeNull();
205
+ const rollout = hydratePITaskRecord(rolloutRaw!)!;
206
+ const scribeRaw = await sm.getTask(SCRIBE_ID);
207
+ const scribe = scribeRaw ? hydratePITaskRecord(scribeRaw) : null;
208
+ return {
209
+ rollout: {
210
+ status: rolloutRaw!.status,
211
+ runnerDecision: rollout.runnerDecision,
212
+ payloadRevisionIteration: rollout.rolloutRevisionPayload?.revisionIteration,
213
+ payloadStatus: rollout.rolloutRevisionPayload?.status,
214
+ payloadTargetKind: rollout.rolloutRevisionPayload?.targetTaskKind,
215
+ payloadRequiredChanges: rollout.rolloutRevisionPayload?.requiredChanges,
216
+ intentStatus: rollout.completionIntent?.status,
217
+ humanReviewContext: rollout.humanReviewContext,
218
+ },
219
+ scribe: {
220
+ status: scribeRaw!.status,
221
+ attemptCount: scribeRaw!.attemptCount,
222
+ revisionCount: scribe?.revisionCount ?? 0,
223
+ revisionCauseId: scribe?.revisionCauseId,
224
+ revisionFeedback: scribe?.revisionFeedback,
225
+ },
226
+ };
227
+ }
228
+
229
+ let consoleLogSpy: ReturnType<typeof vi.spyOn>;
230
+
231
+ beforeEach(() => {
232
+ for (const key of Object.keys(scriptedPayloads)) delete scriptedPayloads[key];
233
+ for (const key of Object.keys(llmCalls)) delete llmCalls[key];
234
+ consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
235
+ vi.spyOn(console, 'error').mockImplementation(() => {});
236
+ });
237
+
238
+ afterEach(() => {
239
+ consoleLogSpy.mockRestore();
240
+ vi.restoreAllMocks();
241
+ process.exitCode = 0;
242
+ });
243
+
244
+ // ═══ P0 A — cross entry point equivalence (core AC) ══════════════════════════
245
+
246
+ describe('PRI-708 P0-A cross entry point equivalence', () => {
247
+ const dirs: string[] = [];
248
+ const states: RuntimeStateManager[] = [];
249
+
250
+ function makeWorkspace(prefix: string): string {
251
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
252
+ dirs.push(dir);
253
+ return dir;
254
+ }
255
+
256
+ afterEach(async () => {
257
+ for (const sm of states.splice(0)) await sm.close().catch(() => undefined);
258
+ for (const dir of dirs.splice(0)) {
259
+ try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* temp */ }
260
+ }
261
+ });
262
+
263
+ it('run-once entry: real evaluator→rollout chain, needs_revision reopens the scribe via canonical routing', async () => {
264
+ const ws = makeWorkspace('pd-pri708-runonce-');
265
+ const sm = await seedWorkspace(ws);
266
+ states.push(sm);
267
+
268
+ const { rolloutTaskId, evaluatorArtifactId } = await driveEvaluatorLeg(ws);
269
+ scriptedPayloads['rollout_reviewer'] = rolloutNeedsRevisionPayload(rolloutTaskId, evaluatorArtifactId);
270
+
271
+ await handleRuntimeInternalizationRunOnce({
272
+ workspace: ws, runner: 'rollout_reviewer', runtime: 'test-double', allowTestDouble: true, json: true,
273
+ });
274
+ const out = JSON.parse(consoleLogSpy.mock.calls.at(-1)![0] as string) as Record<string, unknown>;
275
+ expect(out.decision).toBe('would_lease');
276
+ expect((out.runnerResult as Record<string, unknown>).status).toBe('succeeded');
277
+
278
+ const outcome = await readGovernanceOutcome(sm, rolloutTaskId);
279
+ // rollout 侧: governance transition 完成 — 无 NHR、payload applied、intent applied
280
+ expect(outcome.rollout.status).toBe('succeeded');
281
+ expect(outcome.rollout.runnerDecision).toBe('needs_revision');
282
+ expect(outcome.rollout.humanReviewContext).toBeUndefined();
283
+ expect(outcome.rollout.payloadRevisionIteration).toBe(1);
284
+ expect(outcome.rollout.payloadStatus).toBe('applied');
285
+ expect(outcome.rollout.payloadTargetKind).toBe('scribe');
286
+ expect(outcome.rollout.payloadRequiredChanges).toEqual(['Fix the principle wording to cover the observed failure']);
287
+ expect(outcome.rollout.intentStatus).toBe('applied');
288
+ // scribe 侧: 被 reopen(prompt channel → scribe 走到底),反馈携带 requiredChanges
289
+ expect(outcome.scribe.status).toBe('pending');
290
+ expect(outcome.scribe.attemptCount).toBe(0);
291
+ expect(outcome.scribe.revisionCount).toBe(1);
292
+ expect(outcome.scribe.revisionCauseId).toBe(`rollout-${rolloutTaskId}-r1`);
293
+ expect(outcome.scribe.revisionFeedback).toContain('Fix the principle wording');
294
+ });
295
+
296
+ it('production consumer-cycle assembly on the SAME durable input produces the IDENTICAL governance outcome', async () => {
297
+ const ws = makeWorkspace('pd-pri708-prodcycle-');
298
+ const sm = await seedWorkspace(ws);
299
+ states.push(sm);
300
+
301
+ const { rolloutTaskId, evaluatorArtifactId } = await driveEvaluatorLeg(ws);
302
+ scriptedPayloads['rollout_reviewer'] = rolloutNeedsRevisionPayload(rolloutTaskId, evaluatorArtifactId);
303
+
304
+ // internalization-consumer-cycle.ts:536-543 的精确装配公式(同工厂 + 同
305
+ // options 形;cycle 的 orchestrator 同为 dryRun: true)。
306
+ const orchestrator = new InternalizationOrchestrator(
307
+ { stateManager: sm },
308
+ { owner: 'parity-production', runtimeKind: 'test-double', dryRun: true },
309
+ );
310
+ const wake = await orchestrator.wakeOnce('rollout_reviewer');
311
+ expect(wake.decision).toBe('would_lease');
312
+ const runner = new RolloutReviewerRunner(
313
+ {
314
+ stateManager: sm,
315
+ runtimeAdapter: scriptedAdapterFor('rollout_reviewer'),
316
+ eventEmitter: storeEmitter,
317
+ artifactStore: sm.piArtifactStore,
318
+ validator: new DefaultRolloutReviewerValidator(),
319
+ ...createRolloutGovernanceDeps(ws, orchestrator, {}),
320
+ },
321
+ { owner: 'parity-production', runtimeKind: 'test-double', pollIntervalMs: 5, timeoutMs: 15_000 },
322
+ );
323
+ const result = await runner.run(wake.taskId);
324
+ expect(result.status).toBe('succeeded');
325
+ await orchestrator.commitNextTaskProposal(wake.taskId);
326
+
327
+ const outcome = await readGovernanceOutcome(sm, rolloutTaskId);
328
+ expect(outcome.rollout.status).toBe('succeeded');
329
+ expect(outcome.rollout.runnerDecision).toBe('needs_revision');
330
+ expect(outcome.rollout.humanReviewContext).toBeUndefined();
331
+ expect(outcome.rollout.payloadRevisionIteration).toBe(1);
332
+ expect(outcome.rollout.payloadStatus).toBe('applied');
333
+ expect(outcome.rollout.payloadTargetKind).toBe('scribe');
334
+ expect(outcome.rollout.payloadRequiredChanges).toEqual(['Fix the principle wording to cover the observed failure']);
335
+ expect(outcome.rollout.intentStatus).toBe('applied');
336
+ expect(outcome.scribe.status).toBe('pending');
337
+ expect(outcome.scribe.attemptCount).toBe(0);
338
+ expect(outcome.scribe.revisionCount).toBe(1);
339
+ expect(outcome.scribe.revisionCauseId).toBe(`rollout-${rolloutTaskId}-r1`);
340
+ expect(outcome.scribe.revisionFeedback).toContain('Fix the principle wording');
341
+ });
342
+
343
+ it('EQUIVALENCE: both entries yield byte-identical governance fields for the same durable input', async () => {
344
+ const wsA = makeWorkspace('pd-pri708-eq-a-');
345
+ const smA = await seedWorkspace(wsA);
346
+ states.push(smA);
347
+ const wsB = makeWorkspace('pd-pri708-eq-b-');
348
+ const smB = await seedWorkspace(wsB);
349
+ states.push(smB);
350
+
351
+ // 腿 A: run-once(真实 CLI handler)
352
+ const legA = await driveEvaluatorLeg(wsA);
353
+ scriptedPayloads['rollout_reviewer'] = rolloutNeedsRevisionPayload(legA.rolloutTaskId, legA.evaluatorArtifactId);
354
+ await handleRuntimeInternalizationRunOnce({
355
+ workspace: wsA, runner: 'rollout_reviewer', runtime: 'test-double', allowTestDouble: true, json: true,
356
+ });
357
+
358
+ // 腿 B: production consumer-cycle 装配公式
359
+ const legB = await driveEvaluatorLeg(wsB);
360
+ scriptedPayloads['rollout_reviewer'] = rolloutNeedsRevisionPayload(legB.rolloutTaskId, legB.evaluatorArtifactId);
361
+ const orchestrator = new InternalizationOrchestrator(
362
+ { stateManager: smB },
363
+ { owner: 'parity-production', runtimeKind: 'test-double', dryRun: true },
364
+ );
365
+ const wake = await orchestrator.wakeOnce('rollout_reviewer');
366
+ const runner = new RolloutReviewerRunner(
367
+ {
368
+ stateManager: smB,
369
+ runtimeAdapter: scriptedAdapterFor('rollout_reviewer'),
370
+ eventEmitter: storeEmitter,
371
+ artifactStore: smB.piArtifactStore,
372
+ validator: new DefaultRolloutReviewerValidator(),
373
+ ...createRolloutGovernanceDeps(wsB, orchestrator, {}),
374
+ },
375
+ { owner: 'parity-production', runtimeKind: 'test-double', pollIntervalMs: 5, timeoutMs: 15_000 },
376
+ );
377
+ expect((await runner.run(wake.taskId)).status).toBe('succeeded');
378
+ await orchestrator.commitNextTaskProposal(wake.taskId);
379
+
380
+ const outA = await readGovernanceOutcome(smA, legA.rolloutTaskId);
381
+ const outB = await readGovernanceOutcome(smB, legB.rolloutTaskId);
382
+ // rollout 的 causeId 内嵌各自 workspace 的 taskId — 归一后必须逐字段一致
383
+ expect(outA.scribe.revisionCauseId).toBe(`rollout-${legA.rolloutTaskId}-r1`);
384
+ expect(outB.scribe.revisionCauseId).toBe(`rollout-${legB.rolloutTaskId}-r1`);
385
+ const normalized = (o: GovernanceOutcome, rolloutTaskId: string): GovernanceOutcome => ({
386
+ ...o,
387
+ scribe: { ...o.scribe, revisionCauseId: o.scribe.revisionCauseId?.replace(rolloutTaskId, '<rolloutTaskId>') },
388
+ });
389
+ expect(normalized(outA, legA.rolloutTaskId)).toEqual(normalized(outB, legB.rolloutTaskId));
390
+ });
391
+ });
392
+
393
+ // ═══ P0 B — durable recovery (crash window resume) ═══════════════════════════
394
+
395
+ describe('PRI-708 P0-B durable recovery through the run-once entry', () => {
396
+ let ws: string;
397
+ let sm: RuntimeStateManager;
398
+
399
+ beforeEach(async () => {
400
+ ws = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-pri708-recovery-'));
401
+ sm = await seedWorkspace(ws);
402
+ // craft 链的 evaluator 无 evaluator leg 驱动 — 真实 lease→succeed 收尾,
403
+ // 使 rollout 任务的依赖满足(wakeOnce 才会投放)
404
+ await sm.acquireLease({ taskId: EVAL_ID, owner: 'parity-seed', runtimeKind: 'test-double' });
405
+ await sm.markTaskSucceeded(EVAL_ID);
406
+ // craft 后 rollout 任务不需要 evaluator artifact(resume 不走 buildContext)
407
+ await sm.piArtifactStore.upsertArtifact({
408
+ artifactId: EVAL_ART, artifactKind: 'principle', sourceTaskId: EVAL_ID,
409
+ lineageArtifactIds: [ARTIFICER_ART], validationStatus: 'pending',
410
+ contentJson: JSON.stringify({ evaluation: { decision: 'approved', score: 0.9, strengths: [], concerns: [], requiredChanges: [] } }),
411
+ createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
412
+ });
413
+ });
414
+
415
+ afterEach(async () => {
416
+ await sm.close().catch(() => undefined);
417
+ try { fs.rmSync(ws, { recursive: true, force: true }); } catch { /* temp */ }
418
+ });
419
+
420
+ /** craft crash window: verdict+intent durable(pending),effects 未 materialize */
421
+ async function craftPendingIntent(): Promise<string> {
422
+ await sm.createTask({
423
+ taskId: ROLL_ID_SEED, taskKind: 'rollout_reviewer', status: 'pending', attemptCount: 1, maxAttempts: 3,
424
+ diagnosticJson: createPITaskDiagnosticJson({
425
+ dependencyTaskIds: [EVAL_ID], channel: 'prompt', timeoutMs: 300_000,
426
+ inputArtifactRefs: [{ artifactType: 'principle', ref: EVAL_ART }], outputArtifactRefs: [], correlationId: 'parity-recovery',
427
+ }),
428
+ });
429
+ await sm.acquireLease({ taskId: ROLL_ID_SEED, owner: 'craft', runtimeKind: 'test-double' });
430
+ const runs = await sm.getRunsByTask(ROLL_ID_SEED);
431
+ const runId = runs.at(-1)!.runId;
432
+ await sm.updateRunOutput(runId, JSON.stringify(rolloutNeedsRevisionPayload(ROLL_ID_SEED, EVAL_ART)));
433
+ await sm.releaseLease(ROLL_ID_SEED, 'craft');
434
+ const raw = await sm.getTask(ROLL_ID_SEED);
435
+ const pi = hydratePITaskRecord(raw!)!;
436
+ await sm.updateTaskDiagnosticJson(ROLL_ID_SEED, createPITaskDiagnosticJson(mergePITaskMetadata(pi, {
437
+ runnerDecision: 'needs_revision',
438
+ completionIntent: {
439
+ decision: 'needs_revision', sourceRunId: runId, revisionEpoch: 0, status: 'pending', revisionIteration: 1,
440
+ } as PITaskMetadata['completionIntent'],
441
+ })));
442
+ return runId;
443
+ }
444
+
445
+ it('crash-before-reopen window: resume applies the SAME effect (reopen) without re-asking the LLM', async () => {
446
+ await craftPendingIntent();
447
+
448
+ await handleRuntimeInternalizationRunOnce({
449
+ workspace: ws, runner: 'rollout_reviewer', runtime: 'test-double', allowTestDouble: true, json: true,
450
+ });
451
+
452
+ // resume = intent 是 recovery authority — LLM 不得被重新调用
453
+ expect(llmCalls['rollout_reviewer'] ?? 0).toBe(0);
454
+ const outcome = await readGovernanceOutcome(sm, ROLL_ID_SEED);
455
+ expect(outcome.rollout.status).toBe('succeeded');
456
+ expect(outcome.rollout.humanReviewContext).toBeUndefined();
457
+ expect(outcome.rollout.payloadStatus).toBe('applied');
458
+ expect(outcome.rollout.intentStatus).toBe('applied');
459
+ expect(outcome.scribe.status).toBe('pending');
460
+ expect(outcome.scribe.revisionCount).toBe(1);
461
+ expect(outcome.scribe.revisionCauseId).toBe(`rollout-${ROLL_ID_SEED}-r1`);
462
+ });
463
+
464
+ it('crash-after-reopen window: causeId materialize check — no duplicate reopen on resume', async () => {
465
+ await craftPendingIntent();
466
+ // reopen 已 materialize、intent 未标 applied 的窗口: 先真实 reopen
467
+ await reopenTaskForRevision(sm, SCRIBE_ID, {
468
+ revisionFeedback: 'pre-crash reopen', reason: 'rollout_revision_iteration_1',
469
+ revisionCauseId: `rollout-${ROLL_ID_SEED}-r1`,
470
+ });
471
+
472
+ await handleRuntimeInternalizationRunOnce({
473
+ workspace: ws, runner: 'rollout_reviewer', runtime: 'test-double', allowTestDouble: true, json: true,
474
+ });
475
+
476
+ expect(llmCalls['rollout_reviewer'] ?? 0).toBe(0);
477
+ const outcome = await readGovernanceOutcome(sm, ROLL_ID_SEED);
478
+ expect(outcome.rollout.status).toBe('succeeded');
479
+ expect(outcome.rollout.intentStatus).toBe('applied');
480
+ // 不得二次 reopen — revisionCount 仍是 1,原反馈不被覆写
481
+ expect(outcome.scribe.status).toBe('pending');
482
+ expect(outcome.scribe.revisionCount).toBe(1);
483
+ expect(outcome.scribe.revisionFeedback).toBe('pre-crash reopen');
484
+ });
485
+ });
486
+
487
+ // ═══ P0 C — exhaustion safety (decision-capable NHR) ═════════════════════════
488
+
489
+ describe('PRI-708 P0-C exhaustion safety through the run-once entry', () => {
490
+ let ws: string;
491
+ let sm: RuntimeStateManager;
492
+
493
+ beforeEach(async () => {
494
+ ws = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-pri708-budget-'));
495
+ sm = await seedWorkspace(ws);
496
+ // 同 P0-B: evaluator 真实收尾,rollout 依赖满足
497
+ await sm.acquireLease({ taskId: EVAL_ID, owner: 'parity-seed', runtimeKind: 'test-double' });
498
+ await sm.markTaskSucceeded(EVAL_ID);
499
+ await sm.piArtifactStore.upsertArtifact({
500
+ artifactId: EVAL_ART, artifactKind: 'principle', sourceTaskId: EVAL_ID,
501
+ lineageArtifactIds: [ARTIFICER_ART], validationStatus: 'pending',
502
+ contentJson: JSON.stringify({ evaluation: { decision: 'approved', score: 0.9, strengths: [], concerns: [], requiredChanges: [] } }),
503
+ createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
504
+ });
505
+ await sm.createTask({
506
+ taskId: ROLL_ID_SEED, taskKind: 'rollout_reviewer', status: 'pending', attemptCount: 0, maxAttempts: 3,
507
+ diagnosticJson: createPITaskDiagnosticJson({
508
+ dependencyTaskIds: [EVAL_ID], channel: 'prompt', timeoutMs: 300_000,
509
+ inputArtifactRefs: [{ artifactType: 'principle', ref: EVAL_ART }], outputArtifactRefs: [], correlationId: 'parity-budget',
510
+ // 预算证据: 两轮修订已 applied
511
+ rolloutRevisionPayload: {
512
+ requiredChanges: ['前两轮'], revisionIteration: 2, sourceRolloutTaskId: ROLL_ID_SEED,
513
+ sourceArtifactId: EVAL_ART, targetTaskKind: 'scribe', status: 'applied',
514
+ },
515
+ revisionCount: 1,
516
+ }),
517
+ });
518
+ });
519
+
520
+ afterEach(async () => {
521
+ await sm.close().catch(() => undefined);
522
+ try { fs.rmSync(ws, { recursive: true, force: true }); } catch { /* temp */ }
523
+ });
524
+
525
+ it('budget exhausted (appliedCount>=2): rollout_revision_budget_exhausted NHR — decision-capable, NOT recovery', async () => {
526
+ scriptedPayloads['rollout_reviewer'] = rolloutNeedsRevisionPayload(ROLL_ID_SEED, EVAL_ART);
527
+ await handleRuntimeInternalizationRunOnce({
528
+ workspace: ws, runner: 'rollout_reviewer', runtime: 'test-double', allowTestDouble: true, json: true,
529
+ });
530
+
531
+ // 预算不被新接线绕过: scribe 未被 reopen
532
+ const outcome = await readGovernanceOutcome(sm, ROLL_ID_SEED);
533
+ expect(outcome.scribe.revisionCount).toBe(0);
534
+ expect(outcome.scribe.status).toBe('succeeded');
535
+ // 任务落 NHR,reasonCode 规范化为 decision-capable 集合成员
536
+ const raw = await sm.getTask(ROLL_ID_SEED);
537
+ expect(raw!.status).toBe('needs_human_review');
538
+ const pi = hydratePITaskRecord(raw!)!;
539
+ expect(pi.humanReviewContext?.reasonCode).toBe('rollout_revision_budget_exhausted');
540
+
541
+ // Owner 裁决能力: eligible=true、allowedActions 非空(生产同一 capability 函数)
542
+ const facts = await collectOwnerDecisionFacts(factStoreFromStateManager(sm), ROLL_ID_SEED);
543
+ expect(facts).not.toBeNull();
544
+ const capability = deriveOwnerDecisionCapability(facts!);
545
+ expect(capability.eligible).toBe(true);
546
+ expect(capability.attention).toBe('owner_decision');
547
+ expect(capability.allowedActions.length).toBeGreaterThan(0);
548
+ expect(capability.allowedActions).toContain('accept_current');
549
+ });
550
+ });
@@ -174,15 +174,24 @@ vi.mock('../../src/config-reader.js', () => ({
174
174
  // host-runtime builder. Mock just that seam here — the unit tests below prove
175
175
  // dispatch wiring; the real resolver + declaration fixture path is proven by
176
176
  // runtime-internalization-run-once-evaluator-parity.test.ts.
177
- const { mockCreateEvaluatorRuntimeContext } = vi.hoisted(() => {
177
+ // PRI-708: same pattern for the canonical rollout governance factory — unit
178
+ // tests assert the run-once handler passes its reopen callback through to the
179
+ // runner deps; the real factory + real SQLite path is proven by
180
+ // runtime-internalization-run-once-rollout-parity.test.ts.
181
+ const { mockCreateEvaluatorRuntimeContext, mockCreateRolloutGovernanceDeps } = vi.hoisted(() => {
178
182
  const mockCreateEvaluatorRuntimeContext = vi.fn().mockReturnValue({
179
183
  ok: true,
180
184
  gateDeps: { evaluateInSandbox: vi.fn() },
181
185
  });
182
- return { mockCreateEvaluatorRuntimeContext };
186
+ const mockCreateRolloutGovernanceDeps = vi.fn().mockReturnValue({
187
+ dispatchActivation: vi.fn(),
188
+ reopenRevisionTarget: vi.fn(),
189
+ });
190
+ return { mockCreateEvaluatorRuntimeContext, mockCreateRolloutGovernanceDeps };
183
191
  });
184
192
  vi.mock('@principles/host-runtime', () => ({
185
193
  createEvaluatorRuntimeContext: mockCreateEvaluatorRuntimeContext,
194
+ createRolloutGovernanceDeps: mockCreateRolloutGovernanceDeps,
186
195
  }));
187
196
 
188
197
  import { handleRuntimeInternalizationRunOnce } from '../../src/commands/runtime-internalization-run-once.js';
@@ -1341,6 +1350,16 @@ describe('handleRuntimeInternalizationRunOnce', () => {
1341
1350
  expect(RolloutReviewerRunnerMock).toHaveBeenCalled();
1342
1351
  expect(mockRun).toHaveBeenCalledWith('task-rollout-reviewer-001');
1343
1352
 
1353
+ // PRI-708: the run-once rollout entry must wire the canonical revision
1354
+ // routing callback from the ONE host-runtime factory (the same builder the
1355
+ // consumer cycle spreads). dispatchActivation stays deliberately unwired
1356
+ // on this manual entry (P2 follow-up) — approve_rollout keeps its
1357
+ // recovery-only NHR behavior exactly as before this change.
1358
+ expect(mockCreateRolloutGovernanceDeps).toHaveBeenCalledTimes(1);
1359
+ const rolloutDeps = RolloutReviewerRunnerMock.mock.calls[0][0];
1360
+ expect(rolloutDeps.reopenRevisionTarget).toBeTypeOf('function');
1361
+ expect(rolloutDeps.dispatchActivation).toBeUndefined();
1362
+
1344
1363
  const output = JSON.parse(consoleLogSpy.mock.calls[0][0]);
1345
1364
  expect(output.runnerKind).toBe('rollout_reviewer');
1346
1365
  expect(output.taskId).toBe('task-rollout-reviewer-001');