@principles/host-runtime 3.5.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,27 @@ const WARNING_LIMIT = 500;
10
10
  const MAX_WARNINGS = 16;
11
11
  const GATE_DEADLINE_MS = 3_000;
12
12
  const ARTIFACT_CONTENT_BYTES = 512 * 1024;
13
+ /**
14
+ * Production RuleHost gate — the shared decision path for every supported host.
15
+ *
16
+ * PRI-1187 (Owner decision 2026-11-12): rules are NEVER filtered by
17
+ * `affectedTools`. Over-blocking (a write-only rule also observing exec) is
18
+ * accepted noise; under-blocking is not. `affectedTools` is the DECLARED
19
+ * SCOPE used by activation (`validateRuleReliability`: golden-trace tool
20
+ * families must be a subset of the declared families) and by approval/audit
21
+ * surfaces — it is NOT an execution bound.
22
+ *
23
+ * DO NOT re-add a runtime filter here. Three review rounds each found a way to
24
+ * skip a call the rule was validated to block: the reserved `host-liveness:`
25
+ * prefix, a narrow declaration with a broad implementation, and a guard that
26
+ * only inspected the golden trace. The decision and the follow-up ticket live
27
+ * in docs/proposals/PRI-1187-write-only-rule-exec-false-positive.md.
28
+ *
29
+ * The only affectedTools runtime code allowed in this file is the
30
+ * observation-only mismatch signal below: it logs, it never changes the
31
+ * evaluation set or the verdict.
32
+ */
33
+ const AFFECTED_TOOLS_OBSERVABILITY_ENV = 'PD_AFFECTED_TOOLS_OBSERVABILITY';
13
34
  // Check the actual imported builder, not an installed-package version string:
14
35
  // a mixed host-runtime/core deployment must not claim input-v2 support.
15
36
  const RECOVERY_INPUT_V2_AVAILABLE = RECOVERY_CONTROL_SURFACES
@@ -82,9 +103,73 @@ if (!baselineToolSemanticsResult.ok) {
82
103
  throw new Error(`baseline tool semantic registry failed to build: ${baselineToolSemanticsResult.errors.join('; ')}`);
83
104
  }
84
105
  const BASELINE_TOOL_SEMANTICS = baselineToolSemanticsResult.registry;
106
+ /**
107
+ * Resolve a rule's declared scope for OBSERVATION ONLY.
108
+ *
109
+ * Returns `null` when the declaration cannot prove a mismatch (absent, not an
110
+ * array, empty, malformed entry, or a name the registry cannot classify). A
111
+ * `null` declaration therefore emits nothing — the signal must never guess,
112
+ * because a false positive would misdirect the follow-up work that decides
113
+ * whether the no-filter tradeoff stays.
114
+ */
115
+ function resolveDeclaredFamilies(affectedTools, toolSemantics) {
116
+ if (!Array.isArray(affectedTools) || affectedTools.length === 0)
117
+ return null;
118
+ const tools = [];
119
+ const families = new Set();
120
+ for (const declared of affectedTools) {
121
+ if (typeof declared !== 'string' || declared.trim().length === 0)
122
+ return null;
123
+ const kind = toolSemantics.lookup(declared);
124
+ if (kind === null)
125
+ return null;
126
+ tools.push(declared);
127
+ families.add(kind);
128
+ }
129
+ return families.size === 0 ? null : { tools, families: [...families] };
130
+ }
131
+ function affectedToolsObservabilityDisabledByEnv() {
132
+ const raw = process.env[AFFECTED_TOOLS_OBSERVABILITY_ENV]?.trim().toLowerCase();
133
+ return raw === '0' || raw === 'false' || raw === 'off';
134
+ }
135
+ function defaultAffectedToolsSink(event) {
136
+ console.warn(`[PD:RuleHost] affected_tools_mismatch ${JSON.stringify(event)}`);
137
+ }
85
138
  export function createProductionRuleHostGate(options = {}) {
86
139
  const implementationRuntime = options.implementationRuntime ?? createNodeRuleImplementationRuntime();
87
140
  const toolSemantics = options.toolSemantics ?? BASELINE_TOOL_SEMANTICS;
141
+ const observabilityEnabled = options.affectedToolsObservability?.enabled
142
+ ?? !affectedToolsObservabilityDisabledByEnv();
143
+ const observabilitySink = options.affectedToolsObservability?.sink ?? defaultAffectedToolsSink;
144
+ /**
145
+ * PRI-1187 observation-only: record that a declared scope did not cover the
146
+ * action while the rule was still evaluated. MUST NOT skip, reorder, or
147
+ * mutate anything. Sink failures are swallowed for the same reason.
148
+ */
149
+ const emitAffectedToolsMismatch = (candidate, observation) => {
150
+ if (!observabilityEnabled)
151
+ return;
152
+ if (candidate.declaredTools === null || candidate.declaredFamilies === null)
153
+ return;
154
+ if (candidate.declaredFamilies.includes(observation.actionFamily))
155
+ return;
156
+ try {
157
+ observabilitySink({
158
+ ruleId: candidate.ruleId,
159
+ activationId: candidate.implId,
160
+ activationMode: candidate.activationMode,
161
+ toolName: observation.toolName,
162
+ actionFamily: observation.actionFamily,
163
+ declaredTools: candidate.declaredTools,
164
+ declaredFamilies: candidate.declaredFamilies,
165
+ decision: observation.decision,
166
+ ...(options.hostKind !== undefined ? { hostKind: options.hostKind } : {}),
167
+ });
168
+ }
169
+ catch {
170
+ // Observation must never affect enforcement.
171
+ }
172
+ };
88
173
  return async (event) => {
89
174
  const startedAt = Date.now();
90
175
  const warnings = [];
@@ -521,7 +606,10 @@ export function createProductionRuleHostGate(options = {}) {
521
606
  continue;
522
607
  }
523
608
  const fallbackMeta = { name: activationId, version: '1', ruleId, coversCondition: 'all' };
524
- candidates.push({ implId: activationId, ruleId, principleId, meta: isRuleMeta(content.meta) ? content.meta : fallbackMeta, source: content.implementationCode, activationMode });
609
+ // PRI-1187: resolved for the observation-only mismatch signal; it
610
+ // is attached to the candidate but never filters the batch.
611
+ const declaredScope = resolveDeclaredFamilies(content.affectedTools, toolSemantics);
612
+ candidates.push({ implId: activationId, ruleId, principleId, meta: isRuleMeta(content.meta) ? content.meta : fallbackMeta, source: content.implementationCode, activationMode, declaredTools: declaredScope?.tools ?? null, declaredFamilies: declaredScope?.families ?? null });
525
613
  }
526
614
  catch (error) {
527
615
  addWarning(warnings, `implementation_unhealthy: ${error instanceof Error ? error.message : String(error)}`, 'fix the RuleCode and reactivate the rule');
@@ -605,6 +693,7 @@ export function createProductionRuleHostGate(options = {}) {
605
693
  const shadowResult = normalized.matched
606
694
  ? { ...normalized, ruleId: candidate.ruleId, principleId: candidate.principleId }
607
695
  : normalized;
696
+ emitAffectedToolsMismatch(candidate, { decision: shadowResult.decision, actionFamily: canonicalKind, toolName: input.toolName });
608
697
  shadowEvaluations.push({
609
698
  toolName: input.toolName,
610
699
  filePath: action.normalizedPath,
@@ -645,6 +734,7 @@ export function createProductionRuleHostGate(options = {}) {
645
734
  : normalized;
646
735
  implementations.push({ ...candidate, evaluate: () => validatedResult });
647
736
  liveEvaluatedResults.push(validatedResult);
737
+ emitAffectedToolsMismatch(candidate, { decision: validatedResult.decision, actionFamily: canonicalKind, toolName: input.toolName });
648
738
  }
649
739
  const result = mergeDecisions(implementations, hostInput, {
650
740
  warn(message) { addWarning(warnings, message, 'inspect the unhealthy activation and RuleCode output'); },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/host-runtime",
3
- "version": "3.5.0",
3
+ "version": "4.1.0",
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.4.0",
34
+ "@principles/core": "^7.6.0",
35
35
  "@principles/install-layout": "^0.8.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": "5c7389e72a6409efb00891327a63dfc01a2009cb"
52
+ "gitHead": "d70d277260da6f9ef2fcbecb622501cea504a12c"
53
53
  }