@principles/host-runtime 3.4.0 → 3.4.1

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.
@@ -71,7 +71,9 @@ export type ValidateSelectionResult = {
71
71
  readonly refValid: boolean | null;
72
72
  readonly digestValid: boolean | null;
73
73
  };
74
- export type AdaptiveFailureCode = 'SELECTION_REFERENCE_INVALID' | 'SELECTION_DIGEST_INVALID' | 'SELECTION_OUTPUT_INVALID' | 'SELECTION_AMBIGUOUS' | 'SELECTION_EXPIRED'
74
+ export type AdaptiveFailureCode = 'SELECTION_REFERENCE_INVALID' | 'SELECTION_DIGEST_INVALID' | 'SELECTION_OUTPUT_INVALID'
75
+ /** The selector's schema ref is not in the adapter registry (configuration fault). */
76
+ | 'SELECTION_SCHEMA_UNAVAILABLE' | 'SELECTION_AMBIGUOUS' | 'SELECTION_EXPIRED'
75
77
  /** The host's outer hook window ended Adaptive; distinct from the model's own timeout. */
76
78
  | 'TURN_DEADLINE_EXCEEDED' | 'TRANSPORT_NOT_SENT' | 'TRANSPORT_FAILED' | 'INPUT_REFUSED';
77
79
  export type AdaptiveSelectionStage = 'idle' | 'admission' | 'generation' | 'validate' | 'revalidate' | 'done';
@@ -15,7 +15,7 @@
15
15
  * - Diagnostics never carry the raw prompt, the raw response, or credentials.
16
16
  */
17
17
  import crypto from 'node:crypto';
18
- import { PiAiRuntimeAdapter, StoreEventEmitter, } from '@principles/core/runtime';
18
+ import { PDRuntimeError, PiAiRuntimeAdapter, StoreEventEmitter, } from '@principles/core/runtime';
19
19
  import { readPromptActivationCandidates } from './active-principle-prompt.js';
20
20
  import { revalidateAdaptivePrinciple } from './adaptive-principle-revalidation.js';
21
21
  export const ADAPTIVE_SELECTION_LIMITS = {
@@ -367,14 +367,43 @@ export function createSingleModelSelector(options) {
367
367
  fetch: guardedFetch,
368
368
  eventEmitter: new StoreEventEmitter(),
369
369
  });
370
- const run = await adapter.startRun({
371
- agentSpec: { agentId: 'adaptive-principle-selector', schemaVersion: 'current' },
372
- inputPayload: input.payload,
373
- contextItems: [],
374
- timeoutMs: remainingAdmission + generationTimeoutMs,
375
- maxTokens,
376
- systemPrompt: ADAPTIVE_SELECTOR_SYSTEM,
377
- });
370
+ let run;
371
+ try {
372
+ run = await adapter.startRun({
373
+ agentSpec: { agentId: 'adaptive-principle-selector', schemaVersion: 'current' },
374
+ inputPayload: input.payload,
375
+ contextItems: [],
376
+ // S5: the last caller that reached the adapter without a schema ref.
377
+ // `adaptive-selection-output` is registered in OUTPUT_SCHEMA_REGISTRY
378
+ // (principles-core), so the structural shape is validated by the
379
+ // shared path instead of relying on the host guard alone.
380
+ // Scope note: this is the ONLY change here. The admission/completion
381
+ // deadline semantics are owned by PRI-1170 (merged as 030d0035,
382
+ // #2513); they are intentionally untouched to avoid a second timing
383
+ // system in this file.
384
+ outputSchemaRef: 'adaptive-selection-output',
385
+ timeoutMs: remainingAdmission + generationTimeoutMs,
386
+ maxTokens,
387
+ systemPrompt: ADAPTIVE_SELECTOR_SYSTEM,
388
+ });
389
+ }
390
+ catch (error) {
391
+ // S5: now that the adapter validates the selection shape, a
392
+ // structurally incomplete answer (e.g. `selected` without a digest)
393
+ // is rejected there — before the host guard could label it
394
+ // SELECTION_DIGEST_INVALID / SELECTION_REFERENCE_INVALID. Map it to
395
+ // the host contract code so the reason stays diagnosable instead of
396
+ // degrading to TRANSPORT_FAILED in runAdaptiveSelection (rc-9).
397
+ if (error instanceof PDRuntimeError && error.category === 'output_invalid') {
398
+ // An unregistered schema ref is a deployment fault, not a malformed
399
+ // answer. Keeping the codes distinct stops operators from debugging
400
+ // the model when the registry is the actual problem.
401
+ fail(error.details?.parseFailureReason === 'unknown_output_schema_ref'
402
+ ? 'SELECTION_SCHEMA_UNAVAILABLE'
403
+ : 'SELECTION_OUTPUT_INVALID');
404
+ }
405
+ throw error;
406
+ }
378
407
  const output = await adapter.fetchOutput(run.runId);
379
408
  const endedAt = now();
380
409
  diagnostics.generationEndedAt = endedAt;
@@ -673,7 +673,27 @@ export function createProductionRuleHostGate(options = {}) {
673
673
  }
674
674
  return { decision: 'deny', reason: result.reason, source: event.source, ...(warnings.length ? { warnings } : {}), metadata: { evaluatedLiveRules: implementations.length, ruleId: result.ruleId, principleId: result.principleId, evaluations } };
675
675
  }
676
- return { decision: 'allow', source: event.source, ...(warnings.length ? { warnings } : {}), metadata: { evaluatedLiveRules: implementations.length, ruleDecision: result?.decision ?? 'allow', evaluations } };
676
+ // Task 1 (legacy fallback convergence): a non-block verdict now carries
677
+ // the facts the OpenClaw adapter needs to record/apply it WITHOUT a
678
+ // second evaluation: winner identity, matched flag, rule reason and the
679
+ // already-merged (validated) correction proposal. `metadata` is
680
+ // host-neutral; adapters that do not consume these keys ignore them and
681
+ // never forward them into a host schema.
682
+ return {
683
+ decision: 'allow',
684
+ source: event.source,
685
+ ...(warnings.length ? { warnings } : {}),
686
+ metadata: {
687
+ evaluatedLiveRules: implementations.length,
688
+ ruleDecision: result?.decision ?? 'allow',
689
+ matched: result?.matched ?? false,
690
+ ...(result?.ruleId !== undefined ? { ruleId: result.ruleId } : {}),
691
+ ...(result?.principleId !== undefined ? { principleId: result.principleId } : {}),
692
+ ...(result?.reason !== undefined ? { ruleReason: result.reason } : {}),
693
+ ...(result !== undefined && result.correctionProposal !== undefined ? { correctionProposal: result.correctionProposal } : {}),
694
+ evaluations,
695
+ },
696
+ };
677
697
  }
678
698
  catch (error) {
679
699
  addWarning(warnings, `activation_read_failed: ${error instanceof Error ? error.message : String(error)}`, 'inspect state.db schema and integrity');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/host-runtime",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
4
4
  "description": "Shared host-neutral orchestration for Principles Disciple MVP-Core hook paths.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -31,7 +31,7 @@
31
31
  "lint": "eslint \"src/**/*.ts\""
32
32
  },
33
33
  "dependencies": {
34
- "@principles/core": "^7.3.2",
34
+ "@principles/core": "^7.3.3",
35
35
  "@principles/install-layout": "^0.7.0",
36
36
  "better-sqlite3": "^13.0.3",
37
37
  "js-yaml": "^5.4.3"
@@ -49,5 +49,5 @@
49
49
  "url": "git+https://github.com/csuzngjh/principles.git"
50
50
  },
51
51
  "license": "Apache-2.0",
52
- "gitHead": "3d6e2af3cf55ad63b7bb388c005adf4b923eb383"
52
+ "gitHead": "1851ac671ffc4391a0e732af2bca991e199d483e"
53
53
  }