@principles/host-runtime 3.0.4 → 3.1.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.
@@ -115,6 +115,32 @@ export interface SharedDirectivePacking {
115
115
  dropped: string[];
116
116
  oversized: string[];
117
117
  }
118
+ /**
119
+ * PRI-1140: resolve a configured principle identity to the AT MOST ONE
120
+ * currently-authorized prompt activation of that principle, so a delivery
121
+ * mapping survives Owner revisions (the prompt-replacement flow deactivates
122
+ * the pinned activation) without editing plugin config per revision. Read-only
123
+ * over the SAME candidates reader the shared assembly uses — selection never
124
+ * creates authorization, and the downstream guards in
125
+ * `buildActivePrinciplePromptContext` re-verify authority/content before any
126
+ * injection. Errors fail closed to Native: the caller gets a structured
127
+ * reason, never a guess.
128
+ */
129
+ export type ResolveCurrentAuthorizedActivationResult = {
130
+ ok: true;
131
+ principleId: string;
132
+ artifactId: string;
133
+ activationId: string;
134
+ warnings: string[];
135
+ } | {
136
+ ok: false;
137
+ reason: 'principle_not_currently_authorized' | 'ambiguous_current_activations' | 'candidates_unavailable';
138
+ warnings: string[];
139
+ };
140
+ export declare function resolveCurrentAuthorizedActivationByPrincipleId(input: {
141
+ workspaceDir: string;
142
+ principleId: string;
143
+ }): Promise<ResolveCurrentAuthorizedActivationResult>;
118
144
  /**
119
145
  * PRI-1137 final review P1-5: did the AUTHORIZATION FACTS of the adaptive
120
146
  * priority target change between the native snapshot (first read) and the
@@ -197,6 +223,18 @@ export declare function buildActivePrinciplePromptContext(input: {
197
223
  artifactId: string;
198
224
  activationId: string;
199
225
  };
226
+ /**
227
+ * PRI-1140 (Owner supplementary condition): additive-only adaptive packing.
228
+ * Only meaningful together with `adaptivePriority`. When true, the
229
+ * prioritized repack is accepted ONLY if it keeps every activation the
230
+ * native baseline already assembled (a pure superset — the target may be
231
+ * placed first, but nothing native-included may be squeezed out); otherwise
232
+ * the native baseline is kept and the would-be displacement is recorded via
233
+ * `adaptivePacking.reason='would_displace_native_assembly'` +
234
+ * `displacedActivationIds`. The legacy pinned-identity path is unaffected
235
+ * (its budget-driven displacement semantics are preserved verbatim).
236
+ */
237
+ additiveOnly?: boolean;
200
238
  /** Principles in the native pack that adaptive packing must preserve. */
201
239
  mandatoryPrincipleIds?: string[];
202
240
  /** Request cancellation forwarded to the final read-only target revalidation. */
@@ -95,6 +95,42 @@ export async function readPromptActivationCandidates(input) {
95
95
  }
96
96
  return { principles, warnings, aborted: false, selfReportEnabled };
97
97
  }
98
+ export async function resolveCurrentAuthorizedActivationByPrincipleId(input) {
99
+ const principleId = typeof input.principleId === 'string' ? input.principleId.trim() : '';
100
+ if (!principleId) {
101
+ return { ok: false, reason: 'principle_not_currently_authorized', warnings: [] };
102
+ }
103
+ // Mirrors the adaptive authority re-read (noBusyWait): an optional
104
+ // enhancement must not hold the host behind the synchronous busy wait; a
105
+ // read warning means the candidate set itself is not trustworthy, so the
106
+ // resolution fails closed instead of selecting from a degraded list.
107
+ const candidates = await readPromptActivationCandidates({
108
+ workspaceDir: input.workspaceDir,
109
+ includeAuthority: true,
110
+ noBusyWait: true,
111
+ });
112
+ if (candidates.aborted || candidates.warnings.length > 0) {
113
+ return { ok: false, reason: 'candidates_unavailable', warnings: candidates.warnings };
114
+ }
115
+ const matches = candidates.principles.filter((principle) => principle.principleId === principleId);
116
+ if (matches.length === 0) {
117
+ return { ok: false, reason: 'principle_not_currently_authorized', warnings: candidates.warnings };
118
+ }
119
+ if (matches.length > 1) {
120
+ return { ok: false, reason: 'ambiguous_current_activations', warnings: candidates.warnings };
121
+ }
122
+ const [resolved] = matches;
123
+ if (!resolved) {
124
+ return { ok: false, reason: 'principle_not_currently_authorized', warnings: candidates.warnings };
125
+ }
126
+ return {
127
+ ok: true,
128
+ principleId: resolved.principleId,
129
+ artifactId: resolved.artifactId,
130
+ activationId: resolved.activationId,
131
+ warnings: candidates.warnings,
132
+ };
133
+ }
98
134
  /**
99
135
  * PRI-1137 final review P1-5: did the AUTHORIZATION FACTS of the adaptive
100
136
  * priority target change between the native snapshot (first read) and the
@@ -390,13 +426,23 @@ export async function buildActivePrinciplePromptContext(input) {
390
426
  || mandatoryIds.some((id) => !prioritizedPacking.included.some((principle) => principle.principleId === id))) {
391
427
  return { ...nativeResult, adaptivePacking: report('unavailable', 'mandatory_constraint_displaced') };
392
428
  }
393
- const stillValid = await revalidateAdaptivePrinciple(input.workspaceDir, authorizedTarget, signal);
394
- if (!stillValid)
395
- return { ...nativeResult, adaptivePacking: report('unavailable', 'target_changed') };
396
429
  const prioritizedActivationIds = new Set(prioritizedPacking.included.map((principle) => principle.activationId));
397
430
  const displacedActivationIds = nativePacking.included
398
431
  .filter((principle) => !prioritizedActivationIds.has(principle.activationId))
399
432
  .map((principle) => principle.activationId);
433
+ // PRI-1140: an additive-only target may never squeeze out content the
434
+ // native baseline already assembled. Decided BEFORE the revalidation read so
435
+ // a refused placement costs no further I/O; the would-be displacement is the
436
+ // recorded reason (bounded like the other diagnostic id lists).
437
+ if (input.additiveOnly === true && displacedActivationIds.length > 0) {
438
+ return {
439
+ ...nativeResult,
440
+ adaptivePacking: report('unavailable', 'would_displace_native_assembly', displacedActivationIds.slice(0, MAX_SHARED_DIAGNOSTIC_IDS)),
441
+ };
442
+ }
443
+ const stillValid = await revalidateAdaptivePrinciple(input.workspaceDir, authorizedTarget, signal);
444
+ if (!stillValid)
445
+ return { ...nativeResult, adaptivePacking: report('unavailable', 'target_changed') };
400
446
  const result = makeResult(prioritizedPacking, principles, prepared('prioritized', displacedActivationIds));
401
447
  // Adaptive assembly is deliberately limited to the existing renderer's Native budget cap.
402
448
  return result;
package/dist/index.d.ts CHANGED
@@ -91,12 +91,23 @@ export declare function createProductionHostRuntime(options?: Partial<Pick<HostR
91
91
  adaptivePriorityProvider?: (event: HostEvent) => {
92
92
  artifactId: string;
93
93
  activationId: string;
94
+ /**
95
+ * PRI-1140: true when the provider resolved the target by CURRENT
96
+ * principle identity (revision-resilient selection). The shared
97
+ * assembly then runs its prioritized repack additively — it may place
98
+ * the target first, but must never squeeze out content the native
99
+ * baseline already assembled (otherwise the native baseline is kept and
100
+ * `would_displace_native_assembly` is recorded). Absent/false keeps the
101
+ * legacy pinned-identity semantics untouched.
102
+ */
103
+ additiveOnly?: boolean;
94
104
  hookInvocationId?: string;
95
105
  signal?: AbortSignal;
96
106
  deadlineAt?: number;
97
107
  } | undefined | Promise<{
98
108
  artifactId: string;
99
109
  activationId: string;
110
+ additiveOnly?: boolean;
100
111
  hookInvocationId?: string;
101
112
  signal?: AbortSignal;
102
113
  deadlineAt?: number;
package/dist/index.js CHANGED
@@ -249,6 +249,7 @@ export function createProductionHostRuntime(options = {}) {
249
249
  adaptivePriority = await options.adaptivePriorityProvider(event);
250
250
  if (adaptivePriority && (typeof adaptivePriority.artifactId !== 'string' || !adaptivePriority.artifactId.trim()
251
251
  || typeof adaptivePriority.activationId !== 'string' || !adaptivePriority.activationId.trim()
252
+ || (adaptivePriority.additiveOnly !== undefined && typeof adaptivePriority.additiveOnly !== 'boolean')
252
253
  || (adaptivePriority.deadlineAt !== undefined && !Number.isFinite(adaptivePriority.deadlineAt)))) {
253
254
  adaptivePriority = undefined;
254
255
  }
@@ -268,6 +269,7 @@ export function createProductionHostRuntime(options = {}) {
268
269
  ...(roundKey !== undefined ? { roundKey } : {}),
269
270
  ...(adaptivePriority ? {
270
271
  adaptivePriority: { artifactId: adaptivePriority.artifactId, activationId: adaptivePriority.activationId },
272
+ ...(adaptivePriority.additiveOnly === true ? { additiveOnly: true } : {}),
271
273
  ...(adaptivePriority.signal ? { signal: adaptivePriority.signal } : {}),
272
274
  ...(adaptivePriority.deadlineAt !== undefined ? { deadlineAt: adaptivePriority.deadlineAt } : {}),
273
275
  ...(adaptivePriority.hookInvocationId ? { hookInvocationId: adaptivePriority.hookInvocationId } : {}),
@@ -3,10 +3,11 @@ import path from 'node:path';
3
3
  import { PROMPT_BUDGET_POLICY_VERSION, RUNTIME_PRINCIPLE_BUDGET, isValidContextWindowTokens, promptBudgetCharsForContextWindow, resolvePromptBudget, operatorDeclaredContextWindow, } from '@principles/core/runtime';
4
4
  import { isPromptInjectionTargetHost, } from './prompt-injection-projection.js';
5
5
  /**
6
- * PRI-1104 Stage 0 (expand): accepted budget policy provenance versions.
6
+ * PRI-1104 Stage 0 (expand) / PRI-1139 (PR-6 cutover): accepted budget policy
7
+ * provenance versions.
7
8
  *
8
9
  * Injection events written to the JSONL event log carry `budgetPolicyVersion`
9
- * as a durable wire literal. PR-6 renames the CURRENT policy id from
10
+ * as a durable wire literal. PR-6 renamed the CURRENT policy id from
10
11
  * `model_aware_v2` to a version-neutral `model_aware`; without this read-side
11
12
  * allowance, every historical event would suddenly be classified as
12
13
  * "computed under a different policy" and silently skipped (and a revert of
@@ -14,13 +15,14 @@ import { isPromptInjectionTargetHost, } from './prompt-injection-projection.js';
14
15
  *
15
16
  * These are FROZEN WIRE LITERALS, not a second policy definition: they exist
16
17
  * only so events remain readable across the PR-6 rename AND its revert. The
17
- * PR-6 target id `model_aware` is listed up front (PR #2367 CodeRabbit #2):
18
- * if PR-6 is reverted after it wrote `model_aware` events, a reader accepting
19
- * only `model_aware_v2` would silently skip those observations and the console
20
- * would fall back to a stale budget.
18
+ * pre-cutover id `model_aware_v2` is listed explicitly (PR #2367 CodeRabbit
19
+ * #2): if PR-6 is reverted, a reader accepting only the then-current
20
+ * `model_aware` would silently skip every historical observation and the
21
+ * console would fall back to a stale budget.
21
22
  *
22
- * Today the writer emits only `model_aware_v2`, so listing the future and
23
- * historical literals does not change any current read result.
23
+ * Since the PRI-1139 cutover the writer emits `model_aware`; both spellings
24
+ * denote the SAME formula, so an accepted event of either id is valid
25
+ * evidence about the current budget math.
24
26
  */
25
27
  const ACCEPTED_BUDGET_POLICY_VERSIONS = new Set([
26
28
  PROMPT_BUDGET_POLICY_VERSION,
@@ -29,8 +29,10 @@ export interface CreateSignalClassifierOptions {
29
29
  * packages/principles-core/src/runtime/adapter/output-schema-registry.ts:
30
30
  * the adapter fails loud (`output_invalid`) on an unregistered ref, and the
31
31
  * catch below would silently degrade Stage 2 to "no classification".
32
+ * PRI-1139: version-neutral canonical key; the pre-cutover `-v2` spelling
33
+ * still resolves through the registry's PRI-1104 alias table.
32
34
  */
33
- export declare const SIGNAL_CLASSIFICATION_SCHEMA_REF = "signal-classification-output-v2";
35
+ export declare const SIGNAL_CLASSIFICATION_SCHEMA_REF = "signal-classification-output";
34
36
  /** Build the classifier from an already-resolved profile (no config I/O). */
35
37
  export declare function createSignalLlmClassifier(profile: SignalClassifierProfile, workspaceDir: string, options?: CreateSignalClassifierOptions): SignalLlmClassifier;
36
38
  export type SignalClassifierResolution = {
@@ -13,7 +13,7 @@
13
13
  * No second model runtime is introduced (ADR-0029: pi-ai is the only backend).
14
14
  */
15
15
  import path from 'node:path';
16
- import { PiAiRuntimeAdapter, SIGNAL_CLASSIFIER_SYSTEM_PROMPT, buildLlmPrompt, resolveLlmClassificationPayload, safeStringifyPreview, } from '@principles/core/runtime';
16
+ import { PiAiRuntimeAdapter, RUNTIME_CONTRACT_SCHEMA_VERSION, SIGNAL_CLASSIFIER_SYSTEM_PROMPT, buildLlmPrompt, resolveLlmClassificationPayload, safeStringifyPreview, } from '@principles/core/runtime';
17
17
  import { loadPdConfigForPlugin } from './pd-config.js';
18
18
  /**
19
19
  * The Stage-2 structured-output contract this classifier requests. It MUST stay
@@ -21,8 +21,10 @@ import { loadPdConfigForPlugin } from './pd-config.js';
21
21
  * packages/principles-core/src/runtime/adapter/output-schema-registry.ts:
22
22
  * the adapter fails loud (`output_invalid`) on an unregistered ref, and the
23
23
  * catch below would silently degrade Stage 2 to "no classification".
24
+ * PRI-1139: version-neutral canonical key; the pre-cutover `-v2` spelling
25
+ * still resolves through the registry's PRI-1104 alias table.
24
26
  */
25
- export const SIGNAL_CLASSIFICATION_SCHEMA_REF = 'signal-classification-output-v2';
27
+ export const SIGNAL_CLASSIFICATION_SCHEMA_REF = 'signal-classification-output';
26
28
  /** Build the classifier from an already-resolved profile (no config I/O). */
27
29
  export function createSignalLlmClassifier(profile, workspaceDir, options = {}) {
28
30
  const timeoutMs = options.timeoutMs ?? profile.timeoutMs;
@@ -46,7 +48,7 @@ export function createSignalLlmClassifier(profile, workspaceDir, options = {}) {
46
48
  const effectiveTimeoutMs = callOptions?.timeoutMs ?? timeoutMs;
47
49
  try {
48
50
  const handle = await adapter.startRun({
49
- agentSpec: { agentId: 'signal-collector', schemaVersion: '1' },
51
+ agentSpec: { agentId: 'signal-collector', schemaVersion: RUNTIME_CONTRACT_SCHEMA_VERSION },
50
52
  inputPayload: { prompt },
51
53
  contextItems: [],
52
54
  timeoutMs: effectiveTimeoutMs,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/host-runtime",
3
- "version": "3.0.4",
3
+ "version": "3.1.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": "^5.2.0",
34
+ "@principles/core": "^6.0.0",
35
35
  "@principles/install-layout": "^0.5.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": "d1b8ff89e487e11a242aa705ee562b75ebc8d209"
52
+ "gitHead": "8a791cfcfb34ec1a70dcdf392f4afc86c1a278c8"
53
53
  }