create-principles-disciple 1.153.0 → 1.153.2
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/_release/product-identity.json +2 -2
- package/console/dist/server/models/ActivationsConsoleModel.d.ts +8 -0
- package/console/dist/server/models/ActivationsConsoleModel.js +3 -2
- package/console/dist/server/models/ExaminerEvidenceModel.d.ts +6 -2
- package/console/dist/server/models/ExaminerEvidenceModel.js +13 -2
- package/console/dist/server/routes/examiner-executor.d.ts +33 -0
- package/console/dist/server/routes/examiner-executor.js +95 -0
- package/console/dist/server/routes/examiner.js +12 -9
- package/console/dist/web/assets/app.css +8 -10
- package/console/dist/web/assets/app.js +29 -29
- package/console/package.json +2 -2
- package/core/dist/ledger-file-lock.d.ts +38 -0
- package/core/dist/ledger-file-lock.d.ts.map +1 -0
- package/core/dist/ledger-file-lock.js +240 -0
- package/core/dist/ledger-file-lock.js.map +1 -0
- package/core/dist/principle-text-normalization.d.ts +32 -0
- package/core/dist/principle-text-normalization.d.ts.map +1 -0
- package/core/dist/principle-text-normalization.js +34 -0
- package/core/dist/principle-text-normalization.js.map +1 -0
- package/core/dist/principle-tree-ledger.d.ts +22 -21
- package/core/dist/principle-tree-ledger.d.ts.map +1 -1
- package/core/dist/principle-tree-ledger.js +77 -204
- package/core/dist/principle-tree-ledger.js.map +1 -1
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.d.ts +13 -0
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.d.ts.map +1 -1
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.js +90 -1
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.js.map +1 -1
- package/core/dist/runtime/adapter/schema-prompt-adapter.d.ts.map +1 -1
- package/core/dist/runtime/adapter/schema-prompt-adapter.js +21 -30
- package/core/dist/runtime/adapter/schema-prompt-adapter.js.map +1 -1
- package/core/dist/runtime/candidate-intake-service.d.ts +40 -1
- package/core/dist/runtime/candidate-intake-service.d.ts.map +1 -1
- package/core/dist/runtime/candidate-intake-service.js +548 -110
- package/core/dist/runtime/candidate-intake-service.js.map +1 -1
- package/core/dist/runtime/candidate-intake.d.ts +59 -2
- package/core/dist/runtime/candidate-intake.d.ts.map +1 -1
- package/core/dist/runtime/candidate-intake.js +23 -0
- package/core/dist/runtime/candidate-intake.js.map +1 -1
- package/core/dist/runtime/candidate-seed-suppression.d.ts +63 -0
- package/core/dist/runtime/candidate-seed-suppression.d.ts.map +1 -0
- package/core/dist/runtime/candidate-seed-suppression.js +29 -0
- package/core/dist/runtime/candidate-seed-suppression.js.map +1 -0
- package/core/dist/runtime/config/pd-config-agent-binding.d.ts +5 -5
- package/core/dist/runtime/config/pd-config-agent-binding.d.ts.map +1 -1
- package/core/dist/runtime/config/pd-config-agent-binding.js +22 -16
- package/core/dist/runtime/config/pd-config-agent-binding.js.map +1 -1
- package/core/dist/runtime/config/pd-credentials.d.ts.map +1 -1
- package/core/dist/runtime/config/pd-credentials.js +7 -1
- package/core/dist/runtime/config/pd-credentials.js.map +1 -1
- package/core/dist/runtime/diagnostician/distiller-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/diagnostician/distiller-prompt-builder.js +30 -3
- package/core/dist/runtime/diagnostician/distiller-prompt-builder.js.map +1 -1
- package/core/dist/runtime/diagnostician/rootcause-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/diagnostician/rootcause-prompt-builder.js +16 -5
- package/core/dist/runtime/diagnostician/rootcause-prompt-builder.js.map +1 -1
- package/core/dist/runtime/diagnostician/router-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/diagnostician/router-prompt-builder.js +16 -4
- package/core/dist/runtime/diagnostician/router-prompt-builder.js.map +1 -1
- package/core/dist/runtime/index.d.ts +4 -2
- package/core/dist/runtime/index.d.ts.map +1 -1
- package/core/dist/runtime/index.js +3 -1
- package/core/dist/runtime/index.js.map +1 -1
- package/core/dist/runtime/internalization/dreamer-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/internalization/dreamer-prompt-builder.js +177 -29
- package/core/dist/runtime/internalization/dreamer-prompt-builder.js.map +1 -1
- package/core/dist/runtime/internalization/internalization-orchestrator.d.ts +16 -2
- package/core/dist/runtime/internalization/internalization-orchestrator.d.ts.map +1 -1
- package/core/dist/runtime/internalization/internalization-orchestrator.js +48 -14
- package/core/dist/runtime/internalization/internalization-orchestrator.js.map +1 -1
- package/core/dist/runtime/internalization/philosopher-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/internalization/philosopher-prompt-builder.js +7 -6
- package/core/dist/runtime/internalization/philosopher-prompt-builder.js.map +1 -1
- package/core/dist/runtime/internalization/scribe-prompt-builder.d.ts +13 -6
- package/core/dist/runtime/internalization/scribe-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/internalization/scribe-prompt-builder.js +15 -10
- package/core/dist/runtime/internalization/scribe-prompt-builder.js.map +1 -1
- package/core/dist/runtime/pain-signal-bridge.d.ts +3 -0
- package/core/dist/runtime/pain-signal-bridge.d.ts.map +1 -1
- package/core/dist/runtime/pain-signal-bridge.js +224 -93
- package/core/dist/runtime/pain-signal-bridge.js.map +1 -1
- package/core/dist/runtime/pain-submit-capability.d.ts.map +1 -1
- package/core/dist/runtime/pain-submit-capability.js +3 -5
- package/core/dist/runtime/pain-submit-capability.js.map +1 -1
- package/core/dist/runtime/pain-to-principle-service.d.ts.map +1 -1
- package/core/dist/runtime/pain-to-principle-service.js +18 -21
- package/core/dist/runtime/pain-to-principle-service.js.map +1 -1
- package/core/dist/runtime/store/candidate/admission-document.d.ts +129 -0
- package/core/dist/runtime/store/candidate/admission-document.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/admission-document.js +196 -0
- package/core/dist/runtime/store/candidate/admission-document.js.map +1 -1
- package/core/dist/runtime/store/candidate/candidate-store.d.ts +54 -1
- package/core/dist/runtime/store/candidate/candidate-store.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.d.ts +32 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.js +24 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.js.map +1 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.d.ts +32 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.js +76 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.js.map +1 -1
- package/core/dist/runtime/store/runtime-state-manager.d.ts +34 -1
- package/core/dist/runtime/store/runtime-state-manager.d.ts.map +1 -1
- package/core/dist/runtime/store/runtime-state-manager.js +10 -0
- package/core/dist/runtime/store/runtime-state-manager.js.map +1 -1
- package/core/package.json +1 -1
- package/host-runtime/dist/governance-signal-admission.js +1 -5
- package/host-runtime/package.json +1 -1
- package/package.json +3 -3
- package/pd-cli/dist/commands/candidate.d.ts.map +1 -1
- package/pd-cli/dist/commands/candidate.js +243 -28
- package/pd-cli/dist/commands/candidate.js.map +1 -1
- package/pd-cli/dist/commands/diagnose.d.ts.map +1 -1
- package/pd-cli/dist/commands/diagnose.js +110 -7
- package/pd-cli/dist/commands/diagnose.js.map +1 -1
- package/pd-cli/dist/commands/pain-retry.d.ts.map +1 -1
- package/pd-cli/dist/commands/pain-retry.js +3 -0
- package/pd-cli/dist/commands/pain-retry.js.map +1 -1
- package/pd-cli/dist/commands/runtime-internalization-run-once.d.ts.map +1 -1
- package/pd-cli/dist/commands/runtime-internalization-run-once.js +68 -2
- package/pd-cli/dist/commands/runtime-internalization-run-once.js.map +1 -1
- package/pd-cli/dist/program.d.ts.map +1 -1
- package/pd-cli/dist/program.js +11 -1
- package/pd-cli/dist/program.js.map +1 -1
- package/pd-cli/dist/services/admission-write-gate.d.ts.map +1 -1
- package/pd-cli/dist/services/admission-write-gate.js +18 -7
- package/pd-cli/dist/services/admission-write-gate.js.map +1 -1
- package/pd-cli/dist/services/demo-story-a-runner.d.ts.map +1 -1
- package/pd-cli/dist/services/demo-story-a-runner.js +8 -3
- package/pd-cli/dist/services/demo-story-a-runner.js.map +1 -1
- package/pd-cli/dist/services/reuse-park-release.d.ts +142 -0
- package/pd-cli/dist/services/reuse-park-release.d.ts.map +1 -0
- package/pd-cli/dist/services/reuse-park-release.js +201 -0
- package/pd-cli/dist/services/reuse-park-release.js.map +1 -0
- package/pd-cli/package.json +1 -1
- package/plugin/dist/bundle.js +819 -629
- package/plugin/dist/openclaw.plugin.json +1 -1
- package/plugin/openclaw.plugin.json +1 -1
- package/plugin/package.json +1 -1
- package/release-manager/package.json +2 -2
|
@@ -49,6 +49,7 @@ import { isPrincipleLedgerEligibleKind, validateRecommendationKind, } from './st
|
|
|
49
49
|
import { buildReuseProposal, proposalNeedsDecision } from './principle-reuse/reuse-proposal.js';
|
|
50
50
|
import { buildReuseShortlist } from './principle-reuse/reuse-retrieval.js';
|
|
51
51
|
import { validateReuseDecision } from './principle-reuse/reuse-proposal.js';
|
|
52
|
+
import { pendingOwnerDecision } from './store/candidate/admission-document.js';
|
|
52
53
|
/**
|
|
53
54
|
* PRI-1146 / SPEC-PD-RULE-ASSOCIATION-005 v0.6 (V2 Only / Clean Break) — task
|
|
54
55
|
* kinds whose candidates MUST prove a complete DiagnosticianOutputV2 lineage
|
|
@@ -184,6 +185,47 @@ export function extractIntakeRecommendation(candidate, artifact) {
|
|
|
184
185
|
};
|
|
185
186
|
}
|
|
186
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Phase 7.2 P1-03 — the AUTOMATIC-path gate over the persisted governance
|
|
190
|
+
* context, as one helper so the step-1b fast check and the in-lock re-check
|
|
191
|
+
* can never drift. Priority: park FIRST (an uncleared Owner park outranks
|
|
192
|
+
* every replay), then a pending typed Owner decision (an unexecuted Owner
|
|
193
|
+
* verdict outranks every automatic action). Returns the refusal result, or
|
|
194
|
+
* null when the automatic path may proceed.
|
|
195
|
+
*
|
|
196
|
+
* Exemptions: reviewOnly (T7 read-only review surfaces the proposal) and a
|
|
197
|
+
* PRESENTED owner-decision execution context (verified separately — the
|
|
198
|
+
* park is expected to still be there; Phase C lifts it).
|
|
199
|
+
*/
|
|
200
|
+
function automaticGateRefusal(candidateId, context) {
|
|
201
|
+
if (context.park) {
|
|
202
|
+
return {
|
|
203
|
+
outcome: 'refused',
|
|
204
|
+
reason: 'reuse_pending_owner',
|
|
205
|
+
candidateId,
|
|
206
|
+
rawRecommendationKind: null,
|
|
207
|
+
message: `Candidate ${candidateId} is parked for the Owner by a PERSISTED reuse gate decision ` +
|
|
208
|
+
'(admission processing: awaiting_owner / reuse_pending_owner). This automatic intake returned the ' +
|
|
209
|
+
'existing park without re-running the evaluation, without writing the ledger, and without touching ' +
|
|
210
|
+
'the candidate state — including candidates that also carry a recorded reuse resolution, which the ' +
|
|
211
|
+
`Owner park outranks. nextAction: pd candidate review --candidate-id ${candidateId} --decide reuse|create --reason "..."`,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
if (context.pendingDecision !== null) {
|
|
215
|
+
return {
|
|
216
|
+
outcome: 'refused',
|
|
217
|
+
reason: 'owner_decision_pending',
|
|
218
|
+
candidateId,
|
|
219
|
+
rawRecommendationKind: null,
|
|
220
|
+
message: `Candidate ${candidateId} carries a PERSISTED Owner ${context.pendingDecision.decision} decision ` +
|
|
221
|
+
`(${context.pendingDecision.decisionId}, decided ${context.pendingDecision.decidedAt}) whose execution has not completed. ` +
|
|
222
|
+
'The automatic path did not override it — no park, no ledger write, no consumption. The pending decision ' +
|
|
223
|
+
`is resumed by re-running the SAME verdict: pd candidate review --candidate-id ${candidateId} ` +
|
|
224
|
+
`--decide ${context.pendingDecision.decision}${context.pendingDecision.decision === 'reuse' ? ` --principle-id ${context.pendingDecision.selectedPrincipleId}` : ''} --reason "..."`,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
187
229
|
export class CandidateIntakeService {
|
|
188
230
|
#stateManager;
|
|
189
231
|
#ledgerAdapter;
|
|
@@ -221,24 +263,98 @@ export class CandidateIntakeService {
|
|
|
221
263
|
}
|
|
222
264
|
}
|
|
223
265
|
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
266
|
+
* Phase 7.2 B2 (+ Owner P1 follow-up + P1-01) — the persisted governance
|
|
267
|
+
* context of one candidate from the SINGLE business truth source (the
|
|
268
|
+
* admission document): the reuse park and any pending typed Owner decision.
|
|
269
|
+
* Consulted by the step-1b fast gate (unlocked) AND re-read inside the
|
|
270
|
+
* coordination lock (authoritative). An unreadable admission document reads
|
|
271
|
+
* as "no park, no pending decision" here; the CLI write gates own the
|
|
272
|
+
* admission-document validity refusal.
|
|
273
|
+
*/
|
|
274
|
+
async #readAdmissionContext(candidateId) {
|
|
275
|
+
const admission = await this.#stateManager.getAdmissionDocument(candidateId);
|
|
276
|
+
if (!admission.ok)
|
|
277
|
+
return { ok: false, park: false, pendingDecision: null };
|
|
278
|
+
return {
|
|
279
|
+
ok: true,
|
|
280
|
+
park: admission.document?.processing?.state === 'awaiting_owner'
|
|
281
|
+
&& admission.document.processing.blockedReason === 'reuse_pending_owner',
|
|
282
|
+
pendingDecision: pendingOwnerDecision(admission.document ?? null),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Phase 7.2 P1-01 Phase B — verify a PRESENTED owner-decision execution
|
|
287
|
+
* context against the persisted admission document. Fail-loud structured
|
|
288
|
+
* errors (the caller surfaces them); never a silent fall-through to the
|
|
289
|
+
* automatic path. `parkExpected` is informational only.
|
|
290
|
+
*/
|
|
291
|
+
async #verifyOwnerDecisionExecution(candidateId, presented) {
|
|
292
|
+
const refuse = (reason, detail) => new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner decision execution for candidate ${candidateId} refused (${reason}): ${detail}`, { candidateId, decisionId: presented.decisionId, reason });
|
|
293
|
+
const admission = await this.#stateManager.getAdmissionDocument(candidateId);
|
|
294
|
+
if (!admission.ok) {
|
|
295
|
+
throw refuse('admission_unreadable', `the admission document cannot be read (${admission.reason}), so the pending decision cannot be verified`);
|
|
296
|
+
}
|
|
297
|
+
const decisions = admission.document?.ownerDecisions ?? [];
|
|
298
|
+
const record = decisions.find((d) => d.decisionId === presented.decisionId);
|
|
299
|
+
if (record === undefined) {
|
|
300
|
+
throw refuse('owner_decision_not_found', 'no Owner decision with this decisionId is persisted for the candidate');
|
|
301
|
+
}
|
|
302
|
+
if (record.candidateId !== candidateId) {
|
|
303
|
+
throw refuse('owner_decision_candidate_mismatch', `the decision belongs to candidate ${record.candidateId}`);
|
|
304
|
+
}
|
|
305
|
+
if (record.executionStatus === 'applied') {
|
|
306
|
+
throw refuse('owner_decision_already_applied', 'the decision already completed (Phase C ran); report the completed result instead of re-executing');
|
|
307
|
+
}
|
|
308
|
+
if (record.executionStatus === 'superseded') {
|
|
309
|
+
throw refuse('owner_decision_superseded', 'the decision was superseded by a later Owner intent');
|
|
310
|
+
}
|
|
311
|
+
// One governing verdict per candidate: when a DIFFERENT verdict already
|
|
312
|
+
// applied (its durable effect exists — reuse evidence or ledger entry),
|
|
313
|
+
// executing this presented intent would create conflicting authorities
|
|
314
|
+
// on the candidate. Fail closed; the applied decision and its effect
|
|
315
|
+
// remain the governing state (scenario-D stress finding, PRI-1243).
|
|
316
|
+
const appliedOther = decisions.find((d) => d.executionStatus === 'applied'
|
|
317
|
+
&& (d.decision !== record.decision || d.selectedPrincipleId !== record.selectedPrincipleId));
|
|
318
|
+
if (appliedOther !== undefined) {
|
|
319
|
+
throw refuse('owner_decision_conflicts_applied', `a conflicting ${appliedOther.decision} decision (${appliedOther.decisionId}) already applied; `
|
|
320
|
+
+ 'its durable effect is the governing state — report it instead of executing this intent');
|
|
321
|
+
}
|
|
322
|
+
return record;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Phase 7.2 B2 — persist a reuse park on the candidate's admission
|
|
326
|
+
* processing domain (the SINGLE business truth source for the park; the
|
|
327
|
+
* telemetry event stays observability-only). Mirrors the record shape the
|
|
328
|
+
* PainSignalBridge writes, so the bridge's own park write is an idempotent
|
|
329
|
+
* re-statement of the same state.
|
|
228
330
|
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* boundary rejected the candidate's kind. Refusal is a normal
|
|
233
|
-
* disposition, NOT an error.
|
|
234
|
-
* @throws CandidateIntakeError with code:
|
|
235
|
-
* - INPUT_INVALID when candidateId is empty/invalid
|
|
236
|
-
* - CANDIDATE_NOT_FOUND when candidate does not exist
|
|
237
|
-
* - ARTIFACT_NOT_FOUND when artifact is missing or unreadable
|
|
238
|
-
* - LEDGER_WRITE_FAILED when ledger write fails
|
|
331
|
+
* Fail-LOUD: a park that cannot be persisted must not be reported as one —
|
|
332
|
+
* an unpersisted park is exactly the bypass window this method closes. The
|
|
333
|
+
* throw keeps the candidate pending so the caller's error path applies.
|
|
239
334
|
*/
|
|
240
|
-
async
|
|
241
|
-
|
|
335
|
+
async #persistReusePark(candidateId, selectedPrincipleId, confidence) {
|
|
336
|
+
let persisted;
|
|
337
|
+
try {
|
|
338
|
+
persisted = await this.#stateManager.updateAdmissionProcessing(candidateId, {
|
|
339
|
+
state: 'awaiting_owner',
|
|
340
|
+
attempts: 0,
|
|
341
|
+
blockedReason: 'reuse_pending_owner',
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
catch (err) {
|
|
345
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Reuse park for candidate ${candidateId} (suspected duplicate of ${selectedPrincipleId ?? '(unresolved)'}, ` +
|
|
346
|
+
`confidence ${confidence ?? 'n/a'}) could not be persisted on the candidate's admission processing; ` +
|
|
347
|
+
'the intake did NOT succeed and the candidate stays pending. An unpersisted park would be bypassable ' +
|
|
348
|
+
`by the next automatic intake. Cause: ${err instanceof Error ? err.message : String(err)}`, { candidateId, reason: 'reuse_park_persistence_failed', cause: err });
|
|
349
|
+
}
|
|
350
|
+
if (!persisted) {
|
|
351
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Reuse park for candidate ${candidateId} (suspected duplicate of ${selectedPrincipleId ?? '(unresolved)'}, ` +
|
|
352
|
+
'confidence ' + `${confidence ?? 'n/a'}) was not persisted (admission processing update returned false); ` +
|
|
353
|
+
'the intake did NOT succeed and the candidate stays pending.', { candidateId, reason: 'reuse_park_persistence_failed' });
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
async intake(candidateId, opts = {}) {
|
|
357
|
+
return this.#intake(candidateId, false, opts);
|
|
242
358
|
}
|
|
243
359
|
/** Whether the automatic recommendation channel is configured for read-only review. */
|
|
244
360
|
get reuseReviewEnabled() {
|
|
@@ -248,14 +364,56 @@ export class CandidateIntakeService {
|
|
|
248
364
|
async reviewReuse(candidateId) {
|
|
249
365
|
if (!this.reuseReviewEnabled)
|
|
250
366
|
return null;
|
|
251
|
-
return this.#intake(candidateId, true);
|
|
367
|
+
return this.#intake(candidateId, true, {});
|
|
252
368
|
}
|
|
253
|
-
async #intake(candidateId, reviewOnly) {
|
|
369
|
+
async #intake(candidateId, reviewOnly, opts) {
|
|
254
370
|
// 1. Input validation (E-01)
|
|
255
371
|
if (!candidateId || typeof candidateId !== 'string' || candidateId.trim() === '') {
|
|
256
372
|
throw new CandidateIntakeError(INTAKE_ERROR_CODES.INPUT_INVALID, 'candidateId must be a non-empty string', { candidateId });
|
|
257
373
|
}
|
|
258
|
-
// 2.
|
|
374
|
+
// 1b/2. Phase 7.2 B2 (+ Owner P1 follow-up + P1-01/P1-03) — the persisted
|
|
375
|
+
// governance context is checked FIRST, before EVERY idempotency replay
|
|
376
|
+
// below (the ledger-entry replay at step 2 and the reuse-resolution replay
|
|
377
|
+
// at step 2b).
|
|
378
|
+
//
|
|
379
|
+
// Priority (P1-03): an uncleared Owner PARK outranks every replay — for
|
|
380
|
+
// the combined state (a recorded reuseEvidence AND an uncleared park) the
|
|
381
|
+
// evidence replay would let the AUTOMATIC path consume the candidate and
|
|
382
|
+
// complete its processing, silently clearing the park without an Owner
|
|
383
|
+
// decision. Next, a PENDING typed Owner decision outranks every automatic
|
|
384
|
+
// action (P1-01: an unexecuted Owner verdict must not be overridden by a
|
|
385
|
+
// park, a creation, or consumption; it is resumed by re-running the same
|
|
386
|
+
// verdict).
|
|
387
|
+
//
|
|
388
|
+
// Exemptions: reviewOnly (T7: the read-only review must still surface the
|
|
389
|
+
// proposal) and a PRESENTED ownerDecision execution context (P1-01 Phase B
|
|
390
|
+
// — verified against the persisted record; the park is EXPECTED to still
|
|
391
|
+
// be there, Phase C lifts it). A bare `reuseDecision` callback is NO
|
|
392
|
+
// LONGER exempt: an injected callback is not Owner authorization.
|
|
393
|
+
// An unreadable admission document does NOT bypass anything here: the CLI
|
|
394
|
+
// write gates treat it as a refusal; this check defends the park domain.
|
|
395
|
+
let ownerDecisionRecord;
|
|
396
|
+
if (!reviewOnly && opts.ownerDecision) {
|
|
397
|
+
ownerDecisionRecord = await this.#verifyOwnerDecisionExecution(candidateId, opts.ownerDecision);
|
|
398
|
+
}
|
|
399
|
+
else if (!reviewOnly) {
|
|
400
|
+
const fastContext = await this.#readAdmissionContext(candidateId);
|
|
401
|
+
const fastRefusal = automaticGateRefusal(candidateId, fastContext);
|
|
402
|
+
if (fastRefusal !== null)
|
|
403
|
+
return fastRefusal;
|
|
404
|
+
}
|
|
405
|
+
// 2. Idempotency check (E-02, D-10)
|
|
406
|
+
// P1-2 review fix: the generic ledger replay MUST NOT swallow an Owner
|
|
407
|
+
// REUSE execution. When the presented decision is `reuse` and the
|
|
408
|
+
// candidate already has a ledger principle of its OWN (the historical
|
|
409
|
+
// park + derived-entry combination, cf. P1-T02), returning that entry
|
|
410
|
+
// would silently convert the reuse verdict into a create-shaped
|
|
411
|
+
// completion (CLI applies the decision as create, clears the park, and
|
|
412
|
+
// the derived candidate even seeds a chain). Fail closed instead: the
|
|
413
|
+
// conflicting lineage is a repair situation, not a success.
|
|
414
|
+
if (ownerDecisionRecord?.decision === 'reuse' && this.#ledgerAdapter.existsForCandidate(candidateId)) {
|
|
415
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner reuse decision ${ownerDecisionRecord.decisionId} for candidate ${candidateId} conflicts with an EXISTING ledger principle derived from the same candidate; a reuse verdict resolves onto the SELECTED principle via reuseEvidence and cannot complete while its own derived entry exists. Nothing was written and the decision stays pending.`, { candidateId, decisionId: ownerDecisionRecord.decisionId, reason: 'reuse_conflicts_derived_entry' });
|
|
416
|
+
}
|
|
259
417
|
const existing = this.#ledgerAdapter.existsForCandidate(candidateId);
|
|
260
418
|
if (existing) {
|
|
261
419
|
return { outcome: 'ledger_entry', written: false, entry: existing };
|
|
@@ -293,6 +451,9 @@ export class CandidateIntakeService {
|
|
|
293
451
|
if (!candidate) {
|
|
294
452
|
throw new CandidateIntakeError(INTAKE_ERROR_CODES.CANDIDATE_NOT_FOUND, `Candidate ${candidateId} not found`, { candidateId });
|
|
295
453
|
}
|
|
454
|
+
// (The persisted-park replay for automatic shapes ran at step 1b — BEFORE
|
|
455
|
+
// the idempotency replays above, so an uncleared Owner park always
|
|
456
|
+
// outranks a recorded reuse resolution. See the step-1b comment.)
|
|
296
457
|
// 3b. ★ Principle Ledger write boundary (Phase 1 / PR1).
|
|
297
458
|
//
|
|
298
459
|
// SPEC v2.1 §5.1 "Raw Kind Provenance Validation": the decision is made on
|
|
@@ -403,6 +564,16 @@ export class CandidateIntakeService {
|
|
|
403
564
|
// no_candidates / Owner-decision shapes. Returned on the ledger_entry
|
|
404
565
|
// result so the degradation reason survives past the gate.
|
|
405
566
|
let autoRecommendation;
|
|
567
|
+
// Phase 7.2 P1-02 — the auto-gate's PARK decision is an INTENT here; the
|
|
568
|
+
// actual persist runs inside the coordination lock below (park creation
|
|
569
|
+
// follows the same protocol as every write: in-lock re-check, and a
|
|
570
|
+
// persisted pending Owner decision outranks creating a park).
|
|
571
|
+
let parkIntent;
|
|
572
|
+
// Phase 7.2 P1-01 — the owner-decision reuse execution requires the
|
|
573
|
+
// state dir to revalidate the selection against the CURRENT proposal.
|
|
574
|
+
if (ownerDecisionRecord !== undefined && ownerDecisionRecord.decision === 'reuse' && !this.#reuseStateDir) {
|
|
575
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner decision execution is misconfigured: a reuse decision was presented without reuseStateDir; the selection cannot be revalidated against the current proposal and no evidence was written for candidate ${candidateId}. nextAction: pass reuseStateDir with the owner-decision execution.`, { candidateId, reason: 'reuse_gate_misconfigured' });
|
|
576
|
+
}
|
|
406
577
|
if ((this.#reuseDecision || this.#reuseRecommendation) && this.#reuseStateDir) {
|
|
407
578
|
const reuseClaim = {
|
|
408
579
|
text: recommendation.text || candidate.description || '',
|
|
@@ -444,18 +615,37 @@ export class CandidateIntakeService {
|
|
|
444
615
|
if (verdict === 'reuse' &&
|
|
445
616
|
selectedPrincipleId !== undefined &&
|
|
446
617
|
proposal.candidates.some((entry) => entry.principleId === selectedPrincipleId)) {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
618
|
+
// Phase 7.2 B2 — the park is persisted in the SINGLE business
|
|
619
|
+
// truth source (admission processing: awaiting_owner /
|
|
620
|
+
// reuse_pending_owner) BEFORE reporting it, so a park that exists
|
|
621
|
+
// only in the returned result (or in telemetry, which may lose
|
|
622
|
+
// the event) can never be bypassed by the next automatic intake —
|
|
623
|
+
// production evidence 2026-11-11: candidate 2fd0e91b was parked
|
|
624
|
+
// at confidence 0.95 and consumed 33s later by a re-intake that
|
|
625
|
+
// re-rolled the gate. P1-02: the persist itself moved INSIDE the
|
|
626
|
+
// per-candidate coordination lock (see below), where the pending
|
|
627
|
+
// Owner decision is re-checked first — a persisted verdict
|
|
628
|
+
// outranks creating a park. reviewOnly never writes (T7).
|
|
629
|
+
if (!reviewOnly) {
|
|
630
|
+
parkIntent = { selectedPrincipleId, confidence, proposal, recommendation: recommendationOutcome };
|
|
631
|
+
reuseCheck = 'recommended_create';
|
|
632
|
+
}
|
|
633
|
+
else {
|
|
634
|
+
return {
|
|
635
|
+
outcome: 'refused',
|
|
636
|
+
reason: 'reuse_pending_owner',
|
|
637
|
+
candidateId,
|
|
638
|
+
rawRecommendationKind: typeof candidate.rawRecommendationKind === 'string' ? candidate.rawRecommendationKind : null,
|
|
639
|
+
message: `Candidate ${candidateId} is a suspected duplicate of existing Principle ${selectedPrincipleId} ` +
|
|
640
|
+
`(semantic reuse recommendation, confidence ${confidence}). No Principle was created and no evidence ` +
|
|
641
|
+
'was written; the candidate stays PENDING for the Owner (read-only review: the park would be ' +
|
|
642
|
+
'persisted on the candidate\'s admission processing by the writing intake, so automatic re-intake ' +
|
|
643
|
+
'cannot re-roll the evaluation). ' +
|
|
644
|
+
`nextAction: pd candidate review --candidate-id ${candidateId} --decide reuse|create --reason "..."`,
|
|
645
|
+
reuseProposal: proposal,
|
|
646
|
+
reuseRecommendation: recommendationOutcome,
|
|
647
|
+
};
|
|
648
|
+
}
|
|
459
649
|
}
|
|
460
650
|
// PRI-939 review fix (CodeRabbit): SPEC v0.3.3 treats a reuse
|
|
461
651
|
// verdict whose selection is outside the proposal as unavailable
|
|
@@ -509,49 +699,59 @@ export class CandidateIntakeService {
|
|
|
509
699
|
// latter would file a governance error under "bad input".
|
|
510
700
|
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Reuse decision selected principle '${decision.selectedPrincipleId}' which is not in the proposal for candidate ${candidateId}; no Principle was created.`, { candidateId, selectedPrincipleId: decision.selectedPrincipleId, reason: 'selected_principle_not_in_proposal' });
|
|
511
701
|
}
|
|
512
|
-
// PRI-917 PR3B Phase 2
|
|
513
|
-
//
|
|
514
|
-
//
|
|
515
|
-
//
|
|
516
|
-
//
|
|
517
|
-
//
|
|
518
|
-
//
|
|
519
|
-
//
|
|
520
|
-
//
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
702
|
+
// PRI-917 PR3B Phase 2 (+ Phase 7.2 P1-02) — materialise the
|
|
703
|
+
// verdict as durable evidence BEFORE reporting it, UNDER the
|
|
704
|
+
// per-candidate coordination lock with the same in-lock governance
|
|
705
|
+
// re-check as every other write (a park or pending Owner decision
|
|
706
|
+
// that appeared while the callback evaluated outranks this legacy
|
|
707
|
+
// channel). A reuse verdict that is not persisted would leave the
|
|
708
|
+
// resolution invisible: replay would re-ask the Owner, and the
|
|
709
|
+
// SPEC §11/§12 asymmetry (REUSE must be auditable, CREATE's
|
|
710
|
+
// artifact speaks for itself) would silently break. The append
|
|
711
|
+
// itself runs INSIDE the ledger's single-writer lock
|
|
712
|
+
// (appendReuseEvidence) and is fail-closed: an integrity/shape
|
|
713
|
+
// failure propagates and the candidate stays pending — it NEVER
|
|
714
|
+
// degrades into creating a Principle (INV-R03/INV-R08).
|
|
715
|
+
return this.#runCoordinated(candidateId, async () => {
|
|
716
|
+
const context = await this.#readAdmissionContext(candidateId);
|
|
717
|
+
const refusal = automaticGateRefusal(candidateId, context);
|
|
718
|
+
if (refusal !== null)
|
|
719
|
+
return refusal;
|
|
720
|
+
const painId = await this.#resolveReusePainId(candidateId, candidate);
|
|
721
|
+
let evidence;
|
|
722
|
+
try {
|
|
723
|
+
const appended = this.#ledgerAdapter.appendReuseEvidence(decision.selectedPrincipleId, {
|
|
724
|
+
painId,
|
|
725
|
+
candidateId,
|
|
726
|
+
decision: 'reuse',
|
|
727
|
+
actor: decision.actor,
|
|
728
|
+
reason: decision.reason,
|
|
729
|
+
decidedAt: decision.decidedAt,
|
|
730
|
+
});
|
|
731
|
+
// The writer either appended this candidate's entry or returned the
|
|
732
|
+
// already-recorded one (concurrent-intake dedupe) — the entry for
|
|
733
|
+
// THIS candidateId must be present; anything else is a broken
|
|
734
|
+
// adapter and fails loud instead of reporting a fabricated record.
|
|
735
|
+
const recorded = appended.reuseEvidence.find((e) => e.candidateId === candidateId);
|
|
736
|
+
if (!recorded) {
|
|
737
|
+
throw new Error(`appendReuseEvidence returned no evidence entry for candidate ${candidateId} (adapter contract violation)`);
|
|
738
|
+
}
|
|
739
|
+
evidence = recorded;
|
|
539
740
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
};
|
|
741
|
+
catch (evidenceErr) {
|
|
742
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Reuse decision for candidate ${candidateId} could not be recorded on principle '${decision.selectedPrincipleId}'; no Principle was created and the decision is not durable. Cause: ${evidenceErr instanceof Error ? evidenceErr.message : String(evidenceErr)}`, { candidateId, selectedPrincipleId: decision.selectedPrincipleId, cause: evidenceErr });
|
|
743
|
+
}
|
|
744
|
+
return {
|
|
745
|
+
outcome: 'refused',
|
|
746
|
+
reason: 'reuse_selected',
|
|
747
|
+
candidateId,
|
|
748
|
+
rawRecommendationKind: typeof candidate.rawRecommendationKind === 'string' ? candidate.rawRecommendationKind : null,
|
|
749
|
+
message: `Candidate ${candidateId} was resolved to existing Principle ${decision.selectedPrincipleId}; the reuse decision was recorded on that Principle's reuseEvidence and no new Principle was created. This candidate DOES target the Principle Ledger — review the reuse proposal, and do not re-run intake to force a new Principle.`,
|
|
750
|
+
reuseProposal: proposal,
|
|
751
|
+
selectedPrincipleId: decision.selectedPrincipleId,
|
|
752
|
+
reuseEvidence: evidence,
|
|
753
|
+
};
|
|
754
|
+
});
|
|
555
755
|
}
|
|
556
756
|
reuseCheck = 'create_decided';
|
|
557
757
|
}
|
|
@@ -564,40 +764,258 @@ export class CandidateIntakeService {
|
|
|
564
764
|
// ledger write, and the Owner verdict path is excluded by reviewReuse.
|
|
565
765
|
if (reviewOnly)
|
|
566
766
|
return null;
|
|
567
|
-
//
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
767
|
+
// Phase 7.2 P1-02 — the write critical section. The auto-gate's park
|
|
768
|
+
// decision, the owner-decision reuse execution and the create write all
|
|
769
|
+
// run under the PER-CANDIDATE cross-process coordination lock (when the
|
|
770
|
+
// adapter provides it): re-read the admission context, re-confirm the
|
|
771
|
+
// ledger identity, then write. This CLOSES the re-check→write race window
|
|
772
|
+
// (P1-C02) that the previous unlocked post-evaluation park re-read (2c′)
|
|
773
|
+
// could only narrow. The LLM evaluation ran above, OUTSIDE the lock; the
|
|
774
|
+
// section below is bounded file/SQLite I/O only.
|
|
775
|
+
return this.#runCoordinated(candidateId, async () => {
|
|
776
|
+
// 2c″ — in-lock re-verification of the governance context (the
|
|
777
|
+
// authoritative version of the step-1b fast check).
|
|
778
|
+
if (opts.ownerDecision) {
|
|
779
|
+
ownerDecisionRecord = await this.#verifyOwnerDecisionExecution(candidateId, opts.ownerDecision);
|
|
780
|
+
}
|
|
781
|
+
else {
|
|
782
|
+
const context = await this.#readAdmissionContext(candidateId);
|
|
783
|
+
const refusal = automaticGateRefusal(candidateId, context);
|
|
784
|
+
if (refusal !== null)
|
|
785
|
+
return refusal;
|
|
786
|
+
}
|
|
787
|
+
// In-lock idempotency replays (authoritative — a concurrent execution
|
|
788
|
+
// may have landed while this call was evaluating). P1-2: same reuse
|
|
789
|
+
// discrimination as the early check — a reuse-presented decision never
|
|
790
|
+
// takes the ledger_entry replay (only a CREATE verdict's own entry is a
|
|
791
|
+
// legitimate crash-recovery resume).
|
|
792
|
+
const existingLocked = this.#ledgerAdapter.existsForCandidate(candidateId);
|
|
793
|
+
if (existingLocked) {
|
|
794
|
+
if (ownerDecisionRecord?.decision === 'reuse') {
|
|
795
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner reuse decision ${ownerDecisionRecord.decisionId} for candidate ${candidateId} conflicts with an EXISTING ledger principle derived from the same candidate (observed under the coordination lock); the decision was NOT executed and stays pending.`, { candidateId, decisionId: ownerDecisionRecord.decisionId, reason: 'reuse_conflicts_derived_entry' });
|
|
796
|
+
}
|
|
797
|
+
return { outcome: 'ledger_entry', written: false, entry: existingLocked };
|
|
798
|
+
}
|
|
799
|
+
const resolvedLocked = this.#ledgerAdapter.findReuseResolutionForCandidate(candidateId);
|
|
800
|
+
if (resolvedLocked) {
|
|
801
|
+
return {
|
|
802
|
+
outcome: 'refused',
|
|
803
|
+
reason: 'reuse_selected',
|
|
804
|
+
candidateId,
|
|
805
|
+
rawRecommendationKind: null,
|
|
806
|
+
message: `Candidate ${candidateId} was already resolved into existing Principle ${resolvedLocked.principleId} by a ` +
|
|
807
|
+
'recorded reuse decision; the decision was NOT asked again and no new Principle was created (INV-R07 replay).',
|
|
808
|
+
selectedPrincipleId: resolvedLocked.principleId,
|
|
809
|
+
reuseEvidence: resolvedLocked.evidence,
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
// Phase 7.2 P1-01/P1-02 — persist the auto-gate's park INTENT inside
|
|
813
|
+
// the lock. A persisted PENDING Owner decision (or a concurrent intake's
|
|
814
|
+
// park) outranks creating a fresh park here — the context's own refusal
|
|
815
|
+
// is the answer; otherwise the persist is fail-loud (an unpersisted park
|
|
816
|
+
// is exactly the bypass window B2 closed).
|
|
817
|
+
if (parkIntent !== undefined) {
|
|
818
|
+
const parkContext = await this.#readAdmissionContext(candidateId);
|
|
819
|
+
if (parkContext.park || parkContext.pendingDecision !== null) {
|
|
820
|
+
const raced = automaticGateRefusal(candidateId, parkContext);
|
|
821
|
+
if (raced !== null)
|
|
822
|
+
return raced;
|
|
823
|
+
}
|
|
824
|
+
await this.#persistReusePark(candidateId, parkIntent.selectedPrincipleId, parkIntent.confidence);
|
|
825
|
+
return {
|
|
826
|
+
outcome: 'refused',
|
|
827
|
+
reason: 'reuse_pending_owner',
|
|
828
|
+
candidateId,
|
|
829
|
+
rawRecommendationKind: typeof candidate.rawRecommendationKind === 'string' ? candidate.rawRecommendationKind : null,
|
|
830
|
+
message: `Candidate ${candidateId} is a suspected duplicate of existing Principle ${parkIntent.selectedPrincipleId} ` +
|
|
831
|
+
`(semantic reuse recommendation, confidence ${parkIntent.confidence}). No Principle was created and no evidence ` +
|
|
832
|
+
'was written; the candidate stays PENDING for the Owner (the park is persisted on the candidate\'s ' +
|
|
833
|
+
'admission processing, so automatic re-intake cannot re-roll the evaluation). ' +
|
|
834
|
+
`nextAction: pd candidate review --candidate-id ${candidateId} --decide reuse|create --reason "..."`,
|
|
835
|
+
reuseProposal: parkIntent.proposal,
|
|
836
|
+
reuseRecommendation: parkIntent.recommendation,
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
// Phase 7.2 P1-01 Phase B (reuse) — execute the PERSISTED Owner
|
|
840
|
+
// decision. actor/reason/decidedAt come from the record itself, never
|
|
841
|
+
// from the call; the selection is revalidated against the CURRENT
|
|
842
|
+
// proposal (INV-R08) because the ledger may have moved since Phase A;
|
|
843
|
+
// appendReuseEvidence is idempotent per candidateId, so a crash between
|
|
844
|
+
// this append and Phase C resumes without writing a second entry.
|
|
845
|
+
if (ownerDecisionRecord !== undefined && ownerDecisionRecord.decision === 'reuse') {
|
|
846
|
+
const { selectedPrincipleId } = ownerDecisionRecord;
|
|
847
|
+
if (selectedPrincipleId === undefined) {
|
|
848
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Persisted reuse decision ${ownerDecisionRecord.decisionId} for candidate ${candidateId} has no selectedPrincipleId (malformed record); the decision was NOT executed.`, { candidateId, decisionId: ownerDecisionRecord.decisionId, reason: 'owner_decision_reuse_missing_selection' });
|
|
849
|
+
}
|
|
850
|
+
const reuseStateDir = this.#reuseStateDir;
|
|
851
|
+
if (reuseStateDir === undefined) {
|
|
852
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner decision execution for candidate ${candidateId} has no reuseStateDir; the selection cannot be revalidated and the decision was NOT executed.`, { candidateId, decisionId: ownerDecisionRecord.decisionId, reason: 'reuse_gate_misconfigured' });
|
|
853
|
+
}
|
|
854
|
+
const reuseClaim = {
|
|
855
|
+
text: recommendation.text || candidate.description || '',
|
|
856
|
+
triggerPattern: recommendation.triggerPattern ?? '',
|
|
857
|
+
action: recommendation.action ?? '',
|
|
858
|
+
};
|
|
859
|
+
let proposalNow;
|
|
860
|
+
try {
|
|
861
|
+
proposalNow = buildReuseProposal(candidateId, buildReuseShortlist(reuseClaim, reuseStateDir, {
|
|
862
|
+
recommendationKind: candidate.rawRecommendationKind,
|
|
863
|
+
}));
|
|
864
|
+
}
|
|
865
|
+
catch (reuseErr) {
|
|
866
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner decision execution for candidate ${candidateId} could not revalidate the reuse proposal; the decision was NOT executed and stays pending. Cause: ${reuseErr instanceof Error ? reuseErr.message : String(reuseErr)}`, { candidateId, decisionId: ownerDecisionRecord.decisionId, cause: reuseErr });
|
|
867
|
+
}
|
|
868
|
+
if (!proposalNow.candidates.some((entry) => entry.principleId === selectedPrincipleId)) {
|
|
869
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner decision selected principle '${selectedPrincipleId}' which is no longer in the current reuse proposal for candidate ${candidateId}; the decision was NOT executed and stays pending. nextAction: re-run the review with a selection from the current proposal.`, { candidateId, selectedPrincipleId, reason: 'selected_principle_not_in_proposal' });
|
|
870
|
+
}
|
|
871
|
+
const painId = await this.#resolveReusePainId(candidateId, candidate);
|
|
872
|
+
let evidence;
|
|
873
|
+
try {
|
|
874
|
+
const appended = this.#ledgerAdapter.appendReuseEvidence(selectedPrincipleId, {
|
|
875
|
+
painId,
|
|
876
|
+
candidateId,
|
|
877
|
+
decision: 'reuse',
|
|
878
|
+
actor: { kind: 'owner', id: ownerDecisionRecord.ownerId },
|
|
879
|
+
reason: ownerDecisionRecord.reason,
|
|
880
|
+
decidedAt: ownerDecisionRecord.decidedAt,
|
|
881
|
+
decisionId: ownerDecisionRecord.decisionId,
|
|
882
|
+
});
|
|
883
|
+
const recorded = appended.reuseEvidence.find((e) => e.candidateId === candidateId);
|
|
884
|
+
if (!recorded) {
|
|
885
|
+
throw new Error(`appendReuseEvidence returned no evidence entry for candidate ${candidateId} (adapter contract violation)`);
|
|
886
|
+
}
|
|
887
|
+
evidence = recorded;
|
|
888
|
+
}
|
|
889
|
+
catch (evidenceErr) {
|
|
890
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Owner reuse decision ${ownerDecisionRecord.decisionId} for candidate ${candidateId} could not be recorded on principle '${selectedPrincipleId}'; the decision stays pending and no Principle was created. Cause: ${evidenceErr instanceof Error ? evidenceErr.message : String(evidenceErr)}`, { candidateId, selectedPrincipleId, decisionId: ownerDecisionRecord.decisionId, cause: evidenceErr });
|
|
891
|
+
}
|
|
892
|
+
return {
|
|
893
|
+
outcome: 'refused',
|
|
894
|
+
reason: 'reuse_selected',
|
|
895
|
+
candidateId,
|
|
896
|
+
rawRecommendationKind: typeof candidate.rawRecommendationKind === 'string' ? candidate.rawRecommendationKind : null,
|
|
897
|
+
message: `Candidate ${candidateId} was resolved to existing Principle ${selectedPrincipleId} by Owner decision ${ownerDecisionRecord.decisionId}; the decision was recorded on that Principle's reuseEvidence and no new Principle was created.`,
|
|
898
|
+
reuseProposal: proposalNow,
|
|
899
|
+
selectedPrincipleId,
|
|
900
|
+
reuseEvidence: evidence,
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
// 5. Build 11-field LedgerPrincipleEntry (E-06)
|
|
904
|
+
const entry = {
|
|
905
|
+
id: randomUUID(),
|
|
906
|
+
title: candidate.title,
|
|
907
|
+
text: recommendation.text || candidate.description || '',
|
|
908
|
+
triggerPattern: recommendation.triggerPattern,
|
|
909
|
+
action: recommendation.action,
|
|
910
|
+
status: 'probation',
|
|
911
|
+
evaluability: 'weak_heuristic',
|
|
912
|
+
sourceRef: `candidate://${candidateId}`,
|
|
913
|
+
artifactRef: `artifact://${candidate.artifactId}`,
|
|
914
|
+
taskRef: candidate.taskId ? `task://${candidate.taskId}` : undefined,
|
|
915
|
+
createdAt: new Date().toISOString(),
|
|
593
916
|
};
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
917
|
+
// 6. Write to ledger via adapter (E-01, D-09)
|
|
918
|
+
try {
|
|
919
|
+
const written = this.#ledgerAdapter.writeProbationEntry(entry);
|
|
920
|
+
// Phase 7.2 B1 — exact-duplicate resolution. The adapter's result
|
|
921
|
+
// carries `exactDedupResolution` exactly when the ledger's in-lock guard
|
|
922
|
+
// resolved this write onto an EXISTING principle (normalized text +
|
|
923
|
+
// SHA-256 identical); the minted-id comparison stays as a belt-and-braces
|
|
924
|
+
// fallback for adapters predating the field. Two ownership cases
|
|
925
|
+
// (Phase 7.2 P2-01, from the in-lock survivor — never a lock-external
|
|
926
|
+
// re-read):
|
|
927
|
+
//
|
|
928
|
+
// a) survivorAlreadyDerivedFromThisCandidate — a concurrent or
|
|
929
|
+
// retried write of THIS candidate landed on its own principle.
|
|
930
|
+
// This is an idempotent replay of this candidate's own write: NO
|
|
931
|
+
// reuse evidence may be appended (that would record the candidate
|
|
932
|
+
// reusing its own principle), and the result is the plain
|
|
933
|
+
// ledger_entry replay shape.
|
|
934
|
+
//
|
|
935
|
+
// b) a DIFFERENT candidate's principle survived — the candidate IS
|
|
936
|
+
// fully resolved, but its resolution must be audited OUTSIDE the
|
|
937
|
+
// ledger lock: appendReuseEvidence acquires its own mutateLedger
|
|
938
|
+
// lock, and calling it from inside addPrincipleToLedger's callback
|
|
939
|
+
// would self-deadlock the O_EXCL lock. An audit failure fails the
|
|
940
|
+
// ENTIRE intake: the candidate stays pending, is not consumed, and
|
|
941
|
+
// must not seed a Dreamer chain — an unaudited resolution may never
|
|
942
|
+
// be reported as success. Retries are safe: appendReuseEvidence is
|
|
943
|
+
// idempotent per (principleId, candidateId) (INV-R07), so a re-run
|
|
944
|
+
// after a failed audit records the single entry.
|
|
945
|
+
const dedup = written.exactDedupResolution
|
|
946
|
+
?? (written.id !== entry.id
|
|
947
|
+
? { survivingPrincipleId: written.id, survivorAlreadyDerivedFromThisCandidate: false }
|
|
948
|
+
: undefined);
|
|
949
|
+
if (dedup !== undefined) {
|
|
950
|
+
if (dedup.survivorAlreadyDerivedFromThisCandidate) {
|
|
951
|
+
return {
|
|
952
|
+
outcome: 'ledger_entry',
|
|
953
|
+
written: false,
|
|
954
|
+
entry: written,
|
|
955
|
+
reuseCheck,
|
|
956
|
+
...(autoRecommendation !== undefined ? { reuseRecommendation: autoRecommendation } : {}),
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
const painId = await this.#resolveReusePainId(candidateId, candidate, opts.painIdHint);
|
|
960
|
+
try {
|
|
961
|
+
const appended = this.#ledgerAdapter.appendReuseEvidence(dedup.survivingPrincipleId, {
|
|
962
|
+
painId,
|
|
963
|
+
candidateId,
|
|
964
|
+
decision: 'reuse',
|
|
965
|
+
actor: { kind: 'ai_owner', id: 'exact-dedupe-guard' },
|
|
966
|
+
reason: `Exact duplicate guard: normalized text and SHA-256 are identical to this principle at ledger write; ` +
|
|
967
|
+
`candidate ${candidateId} was resolved onto the existing principle instead of creating a new one (Phase 7.2 B1).`,
|
|
968
|
+
decidedAt: new Date().toISOString(),
|
|
969
|
+
});
|
|
970
|
+
const recorded = appended.reuseEvidence.some((e) => e.candidateId === candidateId);
|
|
971
|
+
if (!recorded) {
|
|
972
|
+
throw new Error(`appendReuseEvidence returned no evidence entry for candidate ${candidateId} (adapter contract violation)`);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
catch (evidenceErr) {
|
|
976
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Exact-duplicate resolution for candidate ${candidateId} could not be recorded on principle '${dedup.survivingPrincipleId}'; the intake did NOT succeed, the candidate stays pending and no Dreamer chain may be seeded. Cause: ${evidenceErr instanceof Error ? evidenceErr.message : String(evidenceErr)}`, { candidateId, deduplicatedAgainst: dedup.survivingPrincipleId, cause: evidenceErr });
|
|
977
|
+
}
|
|
978
|
+
return {
|
|
979
|
+
outcome: 'ledger_entry',
|
|
980
|
+
written: false,
|
|
981
|
+
entry: written,
|
|
982
|
+
deduplicatedAgainst: dedup.survivingPrincipleId,
|
|
983
|
+
reuseCheck,
|
|
984
|
+
...(autoRecommendation !== undefined ? { reuseRecommendation: autoRecommendation } : {}),
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
return {
|
|
988
|
+
outcome: 'ledger_entry',
|
|
989
|
+
written: true,
|
|
990
|
+
entry: written,
|
|
991
|
+
reuseCheck,
|
|
992
|
+
// PRI-939 Option A: the hook outcome rides out on the CREATE result so
|
|
993
|
+
// the degradation (unavailable) reason reaches the Owner's output
|
|
994
|
+
// instead of dying at the gate. Undefined unless the auto-shape hook ran.
|
|
995
|
+
...(autoRecommendation !== undefined ? { reuseRecommendation: autoRecommendation } : {}),
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
catch (err) {
|
|
999
|
+
if (err instanceof CandidateIntakeError) {
|
|
1000
|
+
throw err;
|
|
1001
|
+
}
|
|
1002
|
+
throw new CandidateIntakeError(INTAKE_ERROR_CODES.LEDGER_WRITE_FAILED, `Failed to write ledger entry for candidate ${candidateId}`, { candidateId, cause: err });
|
|
598
1003
|
}
|
|
599
|
-
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Phase 7.2 P1-02 — run `section` under the adapter's per-candidate
|
|
1008
|
+
* cross-process coordination lock when the capability exists (the
|
|
1009
|
+
* file-backed adapter). Test doubles without the capability run the section
|
|
1010
|
+
* directly — the in-lock re-checks still hold within the process.
|
|
1011
|
+
*/
|
|
1012
|
+
async #runCoordinated(candidateId, section) {
|
|
1013
|
+
// Structural assignment of the optional generic capability — no assertion.
|
|
1014
|
+
const coordination = this.#ledgerAdapter.withCandidateCoordination;
|
|
1015
|
+
if (coordination !== undefined) {
|
|
1016
|
+
return await coordination.call(this.#ledgerAdapter, candidateId, section);
|
|
600
1017
|
}
|
|
1018
|
+
return await section();
|
|
601
1019
|
}
|
|
602
1020
|
/**
|
|
603
1021
|
* PRI-1146 (SPEC v0.6 §4) — prove a Diagnostician-sourced candidate's
|
|
@@ -725,12 +1143,25 @@ export class CandidateIntakeService {
|
|
|
725
1143
|
* persistence path. Rows for one task are re-diagnoses / mixed attributions,
|
|
726
1144
|
* so the LATEST row wins — ordered by (createdAt, id) for determinism.
|
|
727
1145
|
*
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
*
|
|
1146
|
+
* Phase 7.2 P2-01 review fix — `painIdHint` BACKSTOP: the pain-diagnosis
|
|
1147
|
+
* persistence is flag-gated (`pain_diagnosis_persistence`), so in a
|
|
1148
|
+
* workspace with that flag off the store lookup can NEVER resolve, and the
|
|
1149
|
+
* B1 exact-duplicate audit would fail every dedup resolution (production
|
|
1150
|
+
* evidence: the codex-adapter workspace-worker backlog test — two identical
|
|
1151
|
+
* corrections, second candidate deduped, intake failed
|
|
1152
|
+
* `reuse_pain_unresolvable`). The bridge passes the pain that triggered the
|
|
1153
|
+
* diagnosis as an authoritative in-memory lineage fact (rc-6: a validated
|
|
1154
|
+
* caller context, never an id parsed out of task-id strings). Resolution
|
|
1155
|
+
* order: persisted rows FIRST (they may carry later re-attributions), hint
|
|
1156
|
+
* only when the store has nothing.
|
|
1157
|
+
*
|
|
1158
|
+
* Fail-closed when neither source resolves: a candidate with no resolvable
|
|
1159
|
+
* Pain cannot produce a self-contained evidence entry, so the reuse
|
|
1160
|
+
* decision is refused (`reuse_pain_unresolvable`) instead of writing a
|
|
1161
|
+
* fabricated painId (rc-3). The candidate stays pending; no Principle is
|
|
1162
|
+
* created.
|
|
732
1163
|
*/
|
|
733
|
-
async #resolveReusePainId(candidateId, candidate) {
|
|
1164
|
+
async #resolveReusePainId(candidateId, candidate, painIdHint) {
|
|
734
1165
|
const refuse = (detail) => {
|
|
735
1166
|
throw new CandidateIntakeError(INTAKE_ERROR_CODES.REUSE_CHECK_FAILED, `Reuse decision for candidate ${candidateId} cannot be recorded: ${detail} ` +
|
|
736
1167
|
'The evidence entry requires the Pain that re-validated the Principle. ' +
|
|
@@ -767,7 +1198,14 @@ export class CandidateIntakeService {
|
|
|
767
1198
|
}
|
|
768
1199
|
}
|
|
769
1200
|
if (diagnoses.length === 0) {
|
|
770
|
-
|
|
1201
|
+
// Phase 7.2 P2-01 review fix — the flag-gated store had nothing; the
|
|
1202
|
+
// bridge's authoritative pain context is the backstop (see the method
|
|
1203
|
+
// doc). Only a usable hint counts — anything else keeps the fail-closed
|
|
1204
|
+
// refusal below.
|
|
1205
|
+
if (typeof painIdHint === 'string' && painIdHint.trim() !== '') {
|
|
1206
|
+
return painIdHint;
|
|
1207
|
+
}
|
|
1208
|
+
refuse('no persisted pain diagnosis exists for its task and no authoritative pain context was supplied');
|
|
771
1209
|
}
|
|
772
1210
|
const sorted = [...diagnoses].sort((a, b) => {
|
|
773
1211
|
if (a.createdAt !== b.createdAt)
|