@principles/host-runtime 4.9.0 → 4.9.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.
@@ -23,7 +23,6 @@
23
23
  * `@principles/core/runtime` → `behavior-example-pack.ts`.
24
24
  */
25
25
  import { buildRuleHostAction } from '@principles/core/runtime/internalization/rule-host-input-builder';
26
- import { computeBehaviorFacts } from '@principles/core/runtime/internalization/rule-context-v2';
27
26
  import { validateBehaviorExamplePack } from '@principles/core/runtime/internalization/behavior-example-pack';
28
27
  // Factory lives in evidence-readiness.ts (the classification authority); it is
29
28
  // consumed through its registered LEAF subpath. A VALUE import of the
@@ -31,7 +30,7 @@ import { validateBehaviorExamplePack } from '@principles/core/runtime/internaliz
31
30
  // fails the satellite-purity guard (OPT-002, scripts/build/check-satellite-purity.mjs) —
32
31
  // the leaf is on core's exports allowlist for exactly this consumer.
33
32
  import { createOwnerEvidenceScopeError } from '@principles/core/runtime/internalization/evidence-readiness';
34
- import { assembleBehaviorHistory } from './behavior-history.js';
33
+ import { assembleBehaviorHistory, computeConservativeBehaviorFacts } from './behavior-history.js';
35
34
  // ── internal constants ─────────────────────────────────────────────────────
36
35
  const MAX_POSITIVES = 3;
37
36
  const MAX_EVIDENCE_REFS = 5;
@@ -237,7 +236,7 @@ export class BehaviorExamplePackAssembler {
237
236
  const historyResult = readTrajectoryStore(`context history read for session=${row.sessionId} before id=${row.id}`, () => this.db.getRuleHostContextRowsBefore(row.sessionId, row.id, HISTORY_LIMIT));
238
237
  const history = assembleBehaviorHistory(historyResult.rows, historyResult.truncated, { projectDir, toolSemantics: this.toolSemantics });
239
238
  const action = buildRuleHostAction(row.toolName, redaction.redacted, projectDir, { canonicalKind: this.toolSemantics.resolve(row.toolName) });
240
- const ruleContext = { version: 2, history, facts: computeBehaviorFacts(history, action.normalizedPath || null, null) };
239
+ const ruleContext = { version: 2, history, facts: computeConservativeBehaviorFacts(history, { path: action.normalizedPath || null, evidence: action.targetEvidence }, null) };
241
240
  return { ...base, params: redaction.redacted, ruleContext };
242
241
  }
243
242
  }
@@ -1,6 +1,9 @@
1
- import type { RuleHistoryWindow, ToolSemanticRegistry } from '@principles/core/runtime';
1
+ import type { RuleBehaviorFacts, RuleHistoryWindow, ToolSemanticRegistry } from '@principles/core/runtime';
2
+ import type { RuleBehaviorFactsTarget } from '@principles/core/runtime/internalization/rule-context-v2';
2
3
  import type { RuleHostContextRow } from '@principles/core/trajectory-types';
4
+ export declare function computeConservativeBehaviorFacts(history: RuleHistoryWindow, target: string | null | RuleBehaviorFactsTarget, sameActionBlockCount: number | null): RuleBehaviorFacts;
3
5
  export declare function assembleBehaviorHistory(rows: readonly RuleHostContextRow[], truncated: boolean, options: {
4
6
  projectDir: string;
5
7
  toolSemantics: ToolSemanticRegistry;
8
+ currentToolUseId?: string;
6
9
  }): RuleHistoryWindow;
@@ -1,5 +1,6 @@
1
1
  /** Shared validated history assembly; host vocabulary is supplied by its owner. */
2
2
  import { buildRuleHostAction } from '@principles/core/runtime/internalization/rule-host-input-builder';
3
+ import { computeBehaviorFacts } from '@principles/core/runtime/internalization/rule-context-v2';
3
4
  const VALID_OUTCOMES = new Set(['success', 'failure', 'blocked']);
4
5
  function isPlainObject(value) {
5
6
  return typeof value === 'object' && value !== null && !Array.isArray(value);
@@ -15,10 +16,27 @@ function unavailable(reason) {
15
16
  calls: [],
16
17
  };
17
18
  }
19
+ export function computeConservativeBehaviorFacts(history, target, sameActionBlockCount) {
20
+ const incomplete = history.truncated || history.calls.some((call) => call.canonicalKind === 'execute' || call.canonicalKind === 'other');
21
+ if (incomplete) {
22
+ return {
23
+ priorReadOfTarget: 'unknown',
24
+ readCount: null,
25
+ writeCount: null,
26
+ uniqueWritePathCount: null,
27
+ sameActionBlockCount: null,
28
+ };
29
+ }
30
+ return target !== null && typeof target === 'object'
31
+ ? computeBehaviorFacts(history, target, sameActionBlockCount)
32
+ : computeBehaviorFacts(history, target, sameActionBlockCount);
33
+ }
18
34
  export function assembleBehaviorHistory(rows, truncated, options) {
19
- const { projectDir, toolSemantics } = options;
35
+ const { projectDir, toolSemantics, currentToolUseId } = options;
20
36
  const records = [];
21
37
  for (const row of rows) {
38
+ if (typeof currentToolUseId === 'string' && currentToolUseId.length > 0 && row.toolUseId === currentToolUseId)
39
+ continue;
22
40
  // ── validate tool_name (ERR-001: unknown) ──
23
41
  const { toolName } = row;
24
42
  if (typeof toolName !== 'string' || toolName.length === 0) {
@@ -53,14 +71,21 @@ export function assembleBehaviorHistory(rows, truncated, options) {
53
71
  const canonicalKind = toolSemantics.resolve(toolName);
54
72
  // Use the shared action builder so history and live/replay preserve the
55
73
  // same distinction between extracted paths and opaque shell commands.
56
- const { normalizedPath } = buildRuleHostAction(toolName, parsedParams, projectDir, { canonicalKind });
74
+ const action = buildRuleHostAction(toolName, parsedParams, projectDir, { canonicalKind });
75
+ const callIdentityRelation = typeof currentToolUseId === 'string' && currentToolUseId.length > 0
76
+ && typeof row.toolUseId === 'string' && row.toolUseId.length > 0
77
+ && row.toolUseId !== currentToolUseId
78
+ ? 'distinct_from_current'
79
+ : 'unproven';
57
80
  records.push({
58
81
  sequenceId: row.id,
59
82
  toolName,
60
83
  canonicalKind,
61
- normalizedPath: normalizedPath === '' ? null : normalizedPath,
84
+ normalizedPath: action.normalizedPath === '' ? null : action.normalizedPath,
62
85
  paramsSummary: parsedParams,
63
86
  outcome,
87
+ callIdentityRelation,
88
+ targetEvidence: action.targetEvidence,
64
89
  });
65
90
  }
66
91
  return {
@@ -1,6 +1,6 @@
1
1
  import path from 'node:path';
2
2
  import { computeBehaviorFacts, } from '@principles/core/runtime';
3
- import { assembleBehaviorHistory } from './behavior-history.js';
3
+ import { assembleBehaviorHistory, computeConservativeBehaviorFacts } from './behavior-history.js';
4
4
  import { readWorkspaceRuleContextRowsBefore } from './workspace-behavior-evidence.js';
5
5
  import { readLatestCodexDesignGateDeclaration, resolveCodexDesignGateFact } from './design-gate-facts.js';
6
6
  const HISTORY_LIMIT = 100;
@@ -134,6 +134,10 @@ export function createCodexRuleContextProvider(options) {
134
134
  return unavailable('trajectory_project_scope_incomplete', designGate);
135
135
  if (historyRows.rows.length === 0)
136
136
  return unavailable('no_prior_session_history', designGate);
137
+ // Deliberately do not pass currentToolUseId here. Unlike WorkBuddy,
138
+ // this reader does not yet prove strict same-host attribution for every
139
+ // historical row, so Codex call identity conservatively remains
140
+ // `unproven` rather than borrowing WorkBuddy's stronger capability.
137
141
  const history = assembleBehaviorHistory(historyRows.rows, historyRows.truncated, {
138
142
  projectDir,
139
143
  toolSemantics: options.toolSemantics,
@@ -142,17 +146,7 @@ export function createCodexRuleContextProvider(options) {
142
146
  // absent from this host's semantic registry. Either may read or mutate
143
147
  // files, so neither target-read absence nor exact action counts can be
144
148
  // asserted from this partial semantic view.
145
- const factsIncomplete = history.truncated || history.calls.some((call) => call.canonicalKind === 'execute' || call.canonicalKind === 'other');
146
- const computedFacts = computeBehaviorFacts(history, factsIncomplete ? null : request.targetPath, null);
147
- const facts = factsIncomplete
148
- ? {
149
- priorReadOfTarget: 'unknown',
150
- readCount: null,
151
- writeCount: null,
152
- uniqueWritePathCount: null,
153
- sameActionBlockCount: null,
154
- }
155
- : computedFacts;
149
+ const facts = computeConservativeBehaviorFacts(history, { path: request.targetPath, evidence: request.targetEvidence }, null);
156
150
  return {
157
151
  version: 2,
158
152
  history,
@@ -1,4 +1,5 @@
1
1
  import { type RuleHostResult, type ToolSemanticRegistry, type GovernanceHostKind } from '@principles/core/runtime';
2
+ import type { RuleTargetEvidence } from '@principles/core/runtime/internalization/rule-context-v2';
2
3
  import type { HostEvent, HostEventResult } from '@principles/core/host';
3
4
  import { type RuleImplementationRuntime } from './rule-implementation-runtime.js';
4
5
  /**
@@ -27,6 +28,8 @@ export interface ProductionRuleContextRequest {
27
28
  targetPath: string;
28
29
  toolName: string;
29
30
  rawPayload: unknown;
31
+ /** Provenance of targetPath when produced by the shared action builder; absent only for legacy/test providers. */
32
+ targetEvidence?: RuleTargetEvidence;
30
33
  }
31
34
  export type RuleContextProvider = (request: ProductionRuleContextRequest) => unknown | Promise<unknown>;
32
35
  export interface RuleInputEnrichment {
@@ -224,6 +224,7 @@ export function createProductionRuleHostGate(options = {}) {
224
224
  const request = {
225
225
  workspaceDir: event.context.workspaceDir, sessionId: event.context.sessionId,
226
226
  targetPath: action.normalizedPath, toolName: input.toolName, rawPayload: event.rawPayload,
227
+ targetEvidence: action.targetEvidence,
227
228
  };
228
229
  if (options.ruleInputEnrichmentProvider) {
229
230
  try {
@@ -1,6 +1,6 @@
1
1
  import path from 'node:path';
2
2
  import { computeBehaviorFacts, } from '@principles/core/runtime';
3
- import { assembleBehaviorHistory } from './behavior-history.js';
3
+ import { assembleBehaviorHistory, computeConservativeBehaviorFacts } from './behavior-history.js';
4
4
  import { readWorkspaceRuleContextRowsBefore } from './workspace-behavior-evidence.js';
5
5
  const HISTORY_LIMIT = 100;
6
6
  const MAX_REASON_LENGTH = 240;
@@ -96,23 +96,14 @@ export function createWorkbuddyRuleContextProvider(options) {
96
96
  const history = assembleBehaviorHistory(historyRows.rows, historyRows.truncated, {
97
97
  projectDir,
98
98
  toolSemantics: options.toolSemantics,
99
+ currentToolUseId: options.currentToolCallId,
99
100
  });
100
101
  // WorkBuddy exposes its shell as the Bash tool, and MCP-style tools may
101
102
  // resolve to 'other' absent from this host's semantic registry. Either
102
103
  // may read or mutate files, so target-read absence and exact action
103
104
  // counts cannot be asserted from this partial semantic view (same
104
105
  // conservative rule as the Codex provider).
105
- const factsIncomplete = history.truncated || history.calls.some((call) => call.canonicalKind === 'execute' || call.canonicalKind === 'other');
106
- const computedFacts = computeBehaviorFacts(history, factsIncomplete ? null : request.targetPath, null);
107
- const facts = factsIncomplete
108
- ? {
109
- priorReadOfTarget: 'unknown',
110
- readCount: null,
111
- writeCount: null,
112
- uniqueWritePathCount: null,
113
- sameActionBlockCount: null,
114
- }
115
- : computedFacts;
106
+ const facts = computeConservativeBehaviorFacts(history, { path: request.targetPath, evidence: request.targetEvidence }, null);
116
107
  return {
117
108
  version: 2,
118
109
  history,
@@ -21,7 +21,14 @@ function contextRow(value) {
21
21
  if (Object.keys(params).some((key) => key === '__proto__' || key === 'constructor' || key === 'prototype')) {
22
22
  throw new Error('Unsafe keys in persisted trajectory params');
23
23
  }
24
- return { id: value.id, toolName: value.tool_name, outcome: value.outcome, paramsJson: value.params_json };
24
+ const toolUseId = typeof value.tool_use_id === 'string' && value.tool_use_id.length > 0 ? value.tool_use_id : undefined;
25
+ return {
26
+ id: value.id,
27
+ toolName: value.tool_name,
28
+ outcome: value.outcome,
29
+ paramsJson: value.params_json,
30
+ ...(toolUseId !== undefined ? { toolUseId } : {}),
31
+ };
25
32
  }
26
33
  function source(db) {
27
34
  return {
@@ -102,7 +109,7 @@ export function readWorkspaceRuleContextRowsBefore(workspaceDir, input) {
102
109
  return db.transaction(() => {
103
110
  // Attribution-aware read: fetch one extra slot beyond the limit so the
104
111
  // partition counts stay truthful about a truncated window's tail.
105
- const selectColumns = includeHostKind ? 'id, tool_name, outcome, params_json, host_kind' : 'id, tool_name, outcome, params_json';
112
+ const selectColumns = includeHostKind ? 'id, tool_name, outcome, params_json, host_kind, tool_use_id' : 'id, tool_name, outcome, params_json, tool_use_id';
106
113
  const fetched = db.prepare(`
107
114
  SELECT ${selectColumns}
108
115
  FROM tool_calls
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principles/host-runtime",
3
- "version": "4.9.0",
3
+ "version": "4.9.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",
@@ -39,7 +39,7 @@
39
39
  "lint": "eslint \"src/**/*.ts\""
40
40
  },
41
41
  "dependencies": {
42
- "@principles/core": "^8.5.0",
42
+ "@principles/core": "^8.5.1",
43
43
  "@principles/install-layout": "^0.12.0",
44
44
  "better-sqlite3": "^13.0.3",
45
45
  "js-yaml": "^5.4.3"
@@ -57,5 +57,5 @@
57
57
  "url": "git+https://github.com/csuzngjh/principles.git"
58
58
  },
59
59
  "license": "Apache-2.0",
60
- "gitHead": "bca2c15209efd155e651e3b7802f8be5287a1d4e"
60
+ "gitHead": "655a074ce2dc4bf776885303e04776f60996099c"
61
61
  }