@principles/host-runtime 2.0.1 → 2.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.
- package/dist/active-principle-prompt.d.ts +28 -0
- package/dist/active-principle-prompt.js +163 -50
- package/dist/adaptive-journal-reconciliation.d.ts +205 -0
- package/dist/adaptive-journal-reconciliation.js +1050 -0
- package/dist/adaptive-principle-revalidation.d.ts +8 -0
- package/dist/adaptive-principle-revalidation.js +52 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +35 -0
- package/dist/prompt-activation-authority.d.ts +7 -0
- package/dist/prompt-activation-authority.js +16 -0
- package/package.json +3 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type PromptActivationCandidates } from './active-principle-prompt.js';
|
|
2
|
+
import type { ActivatedPrinciple } from '@principles/core/runtime-v2';
|
|
3
|
+
export type AdaptivePrincipleReader = (input: {
|
|
4
|
+
workspaceDir: string;
|
|
5
|
+
}) => Promise<PromptActivationCandidates>;
|
|
6
|
+
/** Bind a test or host-specific reader while keeping the production call signature minimal. */
|
|
7
|
+
export declare function createAdaptivePrincipleRevalidator(reader: AdaptivePrincipleReader): (workspaceDir: string, expected: ActivatedPrinciple, signal: AbortSignal) => Promise<boolean>;
|
|
8
|
+
export declare function revalidateAdaptivePrinciple(workspaceDir: string, expected: ActivatedPrinciple, signal: AbortSignal): Promise<boolean>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { readPromptActivationCandidates, } from './active-principle-prompt.js';
|
|
2
|
+
/**
|
|
3
|
+
* Rechecks the rendered principle's identity, body, and authorization at the
|
|
4
|
+
* adaptive-intervention return boundary. A full artifact digest is required;
|
|
5
|
+
* legacy snapshots without it cannot certify a stable artifact revision.
|
|
6
|
+
*/
|
|
7
|
+
async function revalidateWithReader(input, reader) {
|
|
8
|
+
const { workspaceDir, expected, signal } = input;
|
|
9
|
+
if (signal.aborted || !expected.artifactContentSha256 || !/^[a-f0-9]{64}$/.test(expected.artifactContentSha256))
|
|
10
|
+
return false;
|
|
11
|
+
let onAbort;
|
|
12
|
+
const aborted = new Promise((resolve) => {
|
|
13
|
+
onAbort = () => resolve(null);
|
|
14
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
15
|
+
if (signal.aborted)
|
|
16
|
+
onAbort();
|
|
17
|
+
});
|
|
18
|
+
try {
|
|
19
|
+
const result = await Promise.race([reader({ workspaceDir }), aborted]);
|
|
20
|
+
if (!result || signal.aborted || result.aborted || result.warnings.length > 0)
|
|
21
|
+
return false;
|
|
22
|
+
const matches = result.principles.filter((candidate) => candidate.artifactId === expected.artifactId && candidate.activationId === expected.activationId);
|
|
23
|
+
if (matches.length !== 1)
|
|
24
|
+
return false;
|
|
25
|
+
const [current] = matches;
|
|
26
|
+
if (!current)
|
|
27
|
+
return false;
|
|
28
|
+
// Duplicate principle identity is ambiguous even if one activation pair matches.
|
|
29
|
+
if (result.principles.filter((candidate) => candidate.principleId === expected.principleId).length !== 1)
|
|
30
|
+
return false;
|
|
31
|
+
return current.principleId === expected.principleId
|
|
32
|
+
&& current.artifactContentSha256 === expected.artifactContentSha256
|
|
33
|
+
&& current.text === expected.text
|
|
34
|
+
&& current.authority === expected.authority
|
|
35
|
+
&& current.approvedBy === expected.approvedBy;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
if (onAbort)
|
|
42
|
+
signal.removeEventListener('abort', onAbort);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Bind a test or host-specific reader while keeping the production call signature minimal. */
|
|
46
|
+
export function createAdaptivePrincipleRevalidator(reader) {
|
|
47
|
+
return (workspaceDir, expected, signal) => revalidateWithReader({ workspaceDir, expected, signal }, reader);
|
|
48
|
+
}
|
|
49
|
+
export function revalidateAdaptivePrinciple(workspaceDir, expected, signal) {
|
|
50
|
+
const readWithAuthority = ({ workspaceDir: currentWorkspaceDir }) => readPromptActivationCandidates({ workspaceDir: currentWorkspaceDir, includeAuthority: true });
|
|
51
|
+
return revalidateWithReader({ workspaceDir, expected, signal }, readWithAuthority);
|
|
52
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { RuleImplementationRuntime } from './rule-implementation-runtime.js
|
|
|
5
5
|
import { type PainDatabaseFactory, type PainEnrichmentProvider } from './production-pain-evidence.js';
|
|
6
6
|
import type { GovernanceHostKind, ToolSemanticRegistry } from '@principles/core/runtime-v2';
|
|
7
7
|
export * from './active-principle-prompt.js';
|
|
8
|
+
export * from './prompt-activation-authority.js';
|
|
8
9
|
export * from './prompt-injection-projection.js';
|
|
9
10
|
export * from './prompt-activation-deliverability.js';
|
|
10
11
|
export * from './prompt-injection-status.js';
|
|
@@ -76,6 +77,20 @@ export declare function createProductionHostRuntime(options?: Partial<Pick<HostR
|
|
|
76
77
|
projectDir?: string;
|
|
77
78
|
/** PRI-634-F: host-declared tool semantics supplied by the constructing host adapter. */
|
|
78
79
|
toolSemantics?: ToolSemanticRegistry;
|
|
80
|
+
/** Optional, already-scoped single prompt target from the host adapter. Errors fail closed to Native. */
|
|
81
|
+
adaptivePriorityProvider?: (event: HostEvent) => {
|
|
82
|
+
artifactId: string;
|
|
83
|
+
activationId: string;
|
|
84
|
+
hookInvocationId?: string;
|
|
85
|
+
signal?: AbortSignal;
|
|
86
|
+
deadlineAt?: number;
|
|
87
|
+
} | undefined | Promise<{
|
|
88
|
+
artifactId: string;
|
|
89
|
+
activationId: string;
|
|
90
|
+
hookInvocationId?: string;
|
|
91
|
+
signal?: AbortSignal;
|
|
92
|
+
deadlineAt?: number;
|
|
93
|
+
} | undefined>;
|
|
79
94
|
/**
|
|
80
95
|
* PRI-750: optional event emission port. When present, the shared-path
|
|
81
96
|
* handlers record injection/tool events carrying the host's natural
|
|
@@ -96,3 +111,5 @@ export * from './explicit-owner-statement-prefix.js';
|
|
|
96
111
|
export { listObservedDesignGateSessions } from './workspace-behavior-evidence.js';
|
|
97
112
|
export { readDesignGateSpecSnapshot, describeCodexRuleContextFact } from './design-gate-facts.js';
|
|
98
113
|
export { createCodexArtificerTrustedFactsPort } from './codex-artificer-trusted-facts.js';
|
|
114
|
+
export { revalidateAdaptivePrinciple, createAdaptivePrincipleRevalidator, type AdaptivePrincipleReader } from './adaptive-principle-revalidation.js';
|
|
115
|
+
export * from './adaptive-journal-reconciliation.js';
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import { buildActivePrinciplePromptContext } from './active-principle-prompt.js'
|
|
|
6
6
|
import { createProductionRuleHostGate } from './production-rulehost-gate.js';
|
|
7
7
|
import { createProductionPainEvidenceHandler } from './production-pain-evidence.js';
|
|
8
8
|
export * from './active-principle-prompt.js';
|
|
9
|
+
export * from './prompt-activation-authority.js';
|
|
9
10
|
export * from './prompt-injection-projection.js';
|
|
10
11
|
// SPEC PD_PROMPT_CAPACITY_V1: route-aware single-artifact deliverability
|
|
11
12
|
// precheck (B1 write gate + A candidate fit) and the per-live-activation
|
|
@@ -225,8 +226,35 @@ export function createProductionHostRuntime(options = {}) {
|
|
|
225
226
|
const declaredWindow = promptBudgetHostKind === undefined
|
|
226
227
|
? undefined
|
|
227
228
|
: operatorDeclaredContextWindow(loadPdConfigForPlugin(event.context.workspaceDir).effective.config.promptBudget, promptBudgetHostKind);
|
|
229
|
+
let adaptivePriority;
|
|
230
|
+
let skippedPriority;
|
|
231
|
+
if (options.adaptivePriorityProvider) {
|
|
232
|
+
try {
|
|
233
|
+
adaptivePriority = await options.adaptivePriorityProvider(event);
|
|
234
|
+
if (adaptivePriority && (typeof adaptivePriority.artifactId !== 'string' || !adaptivePriority.artifactId.trim()
|
|
235
|
+
|| typeof adaptivePriority.activationId !== 'string' || !adaptivePriority.activationId.trim()
|
|
236
|
+
|| (adaptivePriority.deadlineAt !== undefined && !Number.isFinite(adaptivePriority.deadlineAt)))) {
|
|
237
|
+
adaptivePriority = undefined;
|
|
238
|
+
}
|
|
239
|
+
if (adaptivePriority?.signal?.aborted
|
|
240
|
+
|| (adaptivePriority?.deadlineAt !== undefined && Date.now() >= adaptivePriority.deadlineAt)) {
|
|
241
|
+
skippedPriority = adaptivePriority;
|
|
242
|
+
adaptivePriority = undefined;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
// An optional adaptive provider can never alter the ordinary Native path on failure.
|
|
247
|
+
adaptivePriority = undefined;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
228
250
|
const prompt = await buildActivePrinciplePromptContext({
|
|
229
251
|
workspaceDir: event.context.workspaceDir,
|
|
252
|
+
...(adaptivePriority ? {
|
|
253
|
+
adaptivePriority: { artifactId: adaptivePriority.artifactId, activationId: adaptivePriority.activationId },
|
|
254
|
+
...(adaptivePriority.signal ? { signal: adaptivePriority.signal } : {}),
|
|
255
|
+
...(adaptivePriority.deadlineAt !== undefined ? { deadlineAt: adaptivePriority.deadlineAt } : {}),
|
|
256
|
+
...(adaptivePriority.hookInvocationId ? { hookInvocationId: adaptivePriority.hookInvocationId } : {}),
|
|
257
|
+
} : {}),
|
|
230
258
|
promptBudget: resolvePromptBudget({
|
|
231
259
|
// SPEC-PD-MODEL-AWARE-PROMPT-BUDGET-001: resolve ONCE per request
|
|
232
260
|
// from the host's own hook facts (OpenClaw effective window /
|
|
@@ -250,6 +278,11 @@ export function createProductionHostRuntime(options = {}) {
|
|
|
250
278
|
catalog: getPromptModelCatalog(),
|
|
251
279
|
}),
|
|
252
280
|
});
|
|
281
|
+
if (skippedPriority?.hookInvocationId)
|
|
282
|
+
prompt.adaptivePacking = {
|
|
283
|
+
status: 'unavailable', reason: 'deadline', targetActivationId: skippedPriority.activationId,
|
|
284
|
+
targetArtifactId: skippedPriority.artifactId, hookInvocationId: skippedPriority.hookInvocationId, displacedActivationIds: [],
|
|
285
|
+
};
|
|
253
286
|
// PRI-750: record the injection event on the shared path with the host
|
|
254
287
|
// turn id (Codex turn_id → runId) so receipt events carry a turn-level
|
|
255
288
|
// anchor in the same events_*.jsonl format as the OpenClaw path. The
|
|
@@ -326,3 +359,5 @@ export * from './explicit-owner-statement-prefix.js';
|
|
|
326
359
|
export { listObservedDesignGateSessions } from './workspace-behavior-evidence.js';
|
|
327
360
|
export { readDesignGateSpecSnapshot, describeCodexRuleContextFact } from './design-gate-facts.js';
|
|
328
361
|
export { createCodexArtificerTrustedFactsPort } from './codex-artificer-trusted-facts.js';
|
|
362
|
+
export { revalidateAdaptivePrinciple, createAdaptivePrincipleRevalidator } from './adaptive-principle-revalidation.js';
|
|
363
|
+
export * from './adaptive-journal-reconciliation.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SqliteConnection } from '@principles/core/runtime-v2';
|
|
2
|
+
export interface PromptActivationAuthority {
|
|
3
|
+
authority?: 'owner' | 'system_policy';
|
|
4
|
+
approvedBy?: string;
|
|
5
|
+
}
|
|
6
|
+
/** Read the existing approval row without changing how activation is granted. */
|
|
7
|
+
export declare function readPromptActivationAuthority(sqliteConn: Pick<SqliteConnection, 'getDb'>, artifactId: string, channel: string): PromptActivationAuthority;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Read the existing approval row without changing how activation is granted. */
|
|
2
|
+
export function readPromptActivationAuthority(sqliteConn, artifactId, channel) {
|
|
3
|
+
const row = sqliteConn.getDb().prepare(`
|
|
4
|
+
SELECT decided_by FROM approvals
|
|
5
|
+
WHERE artifact_id = ? AND channel = ? AND status = 'approved'
|
|
6
|
+
ORDER BY decided_at DESC LIMIT 1
|
|
7
|
+
`).get(artifactId, channel);
|
|
8
|
+
if (row !== null && typeof row === 'object' && Object.hasOwn(row, 'decided_by')) {
|
|
9
|
+
const decidedBy = row.decided_by;
|
|
10
|
+
return {
|
|
11
|
+
authority: 'owner',
|
|
12
|
+
approvedBy: typeof decidedBy === 'string' && decidedBy.length > 0 ? decidedBy : undefined,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
return { authority: 'system_policy' };
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@principles/host-runtime",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.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": "^4.0
|
|
34
|
+
"@principles/core": "^4.1.0",
|
|
35
35
|
"@principles/install-layout": "^0.4.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": "
|
|
52
|
+
"gitHead": "6e4734ae4b5acfc949feeaa3cd7b1f2c30366efd"
|
|
53
53
|
}
|