@principles/host-runtime 0.1.0 → 0.2.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.
Files changed (35) hide show
  1. package/dist/active-principle-prompt.js +5 -1
  2. package/dist/governance-observation-store.d.ts +196 -0
  3. package/dist/governance-observation-store.js +696 -0
  4. package/dist/governance-signal-admission.d.ts +228 -0
  5. package/dist/governance-signal-admission.js +853 -0
  6. package/dist/host-liveness-contract.d.ts +3 -0
  7. package/dist/host-liveness-contract.js +18 -0
  8. package/dist/index.d.ts +11 -0
  9. package/dist/index.js +17 -0
  10. package/dist/internalization-consumer-cycle.d.ts +43 -0
  11. package/dist/internalization-consumer-cycle.js +468 -0
  12. package/dist/internalization-consumer-governance.d.ts +39 -0
  13. package/dist/internalization-consumer-governance.js +152 -0
  14. package/dist/legacy-rule-contract-symbols.d.ts +20 -0
  15. package/dist/legacy-rule-contract-symbols.js +124 -0
  16. package/dist/product-telemetry/consent-store.d.ts +107 -0
  17. package/dist/product-telemetry/consent-store.js +329 -0
  18. package/dist/product-telemetry/eligibility.d.ts +53 -0
  19. package/dist/product-telemetry/eligibility.js +69 -0
  20. package/dist/product-telemetry/exporter.d.ts +46 -0
  21. package/dist/product-telemetry/exporter.js +71 -0
  22. package/dist/product-telemetry/milestone-readers.d.ts +61 -0
  23. package/dist/product-telemetry/milestone-readers.js +234 -0
  24. package/dist/product-telemetry/service.d.ts +142 -0
  25. package/dist/product-telemetry/service.js +532 -0
  26. package/dist/product-telemetry/workspace-scope.d.ts +38 -0
  27. package/dist/product-telemetry/workspace-scope.js +64 -0
  28. package/dist/production-pain-evidence.d.ts +60 -0
  29. package/dist/production-pain-evidence.js +82 -12
  30. package/dist/production-rulehost-gate.d.ts +0 -1
  31. package/dist/production-rulehost-gate.js +47 -6
  32. package/dist/rule-implementation-runtime.js +0 -2
  33. package/dist/workspace-telemetry-emitter.d.ts +15 -0
  34. package/dist/workspace-telemetry-emitter.js +74 -0
  35. package/package.json +7 -2
@@ -16,6 +16,10 @@ export async function buildActivePrinciplePromptContext(input) {
16
16
  warnings.push('prompt_feature_disabled; nextAction=set features.prompt.enabled=true in .pd/config.yaml');
17
17
  return { additionalContext: '', principleIds: [], activationIds: [], artifactIds: [], warnings, budget: RUNTIME_V2_PRINCIPLE_BUDGET, truncated: false, excludedPrincipleIds, excludedCount: 0, allValidatedPrinciplesExcluded: false };
18
18
  }
19
+ // PRI-532: agent self-report instruction rides the directive template when
20
+ // the flag is on (flag-off keeps the template byte-identical).
21
+ const selfReportEnabled = computeFeatureFlagsFromConfig(config.effective)
22
+ .flags.principle_receipt_self_report?.enabled === true;
19
23
  let connection;
20
24
  const stateDbPath = path.join(input.workspaceDir, '.pd', 'state.db');
21
25
  if (!fs.existsSync(stateDbPath)) {
@@ -74,7 +78,7 @@ export async function buildActivePrinciplePromptContext(input) {
74
78
  let truncated = false;
75
79
  for (const principle of principles) {
76
80
  const candidate = [...included, principle];
77
- const candidateContext = renderPrinciplesToDirectives(candidate, new Set(candidate.map((entry) => entry.principleId)), escapeXml);
81
+ const candidateContext = renderPrinciplesToDirectives(candidate, new Set(candidate.map((entry) => entry.principleId)), { escapeFn: escapeXml, selfReportInstruction: selfReportEnabled });
78
82
  if (candidateContext.length > RUNTIME_V2_PRINCIPLE_BUDGET) {
79
83
  truncated = true;
80
84
  break;
@@ -0,0 +1,196 @@
1
+ import Database from 'better-sqlite3';
2
+ export declare const GOVERNANCE_RETENTION_MAX_TURNS = 32;
3
+ export declare const GOVERNANCE_RETENTION_MAX_AGE_MS: number;
4
+ export declare const GOVERNANCE_PROMOTION_PRECEDING_TURNS = 12;
5
+ export declare const GOVERNANCE_PENDING_TAIL_STALE_MS: number;
6
+ export type GovernanceObservationKind = 'user_turn' | 'assistant_turn' | 'tool_call';
7
+ export type GovernanceObservationSource = 'live_hook' | 'transcript';
8
+ export type GovernanceObservationCompleteness = 'complete' | 'partial';
9
+ export type GovernanceRetentionClass = 'operational' | 'promoted' | 'expired' | 'rolled_back';
10
+ export interface GovernanceObservationInput {
11
+ readonly hostKind: 'codex';
12
+ readonly rolloutIdentity: string;
13
+ readonly rootSessionId: string;
14
+ readonly parentRolloutIdentity?: string;
15
+ readonly agentIdentity?: string;
16
+ readonly agentDepth?: number;
17
+ readonly hostTurnId: string;
18
+ readonly kind: GovernanceObservationKind;
19
+ /** Semantic convergence identity (SPEC §6). Built by the host adapter. */
20
+ readonly logicalObservationKey: string;
21
+ /** Physical replay identity: `host|rollout|ordinal` (SPEC §6). */
22
+ readonly transcriptRecordKey?: string;
23
+ /**
24
+ * Byte offset of the source record start — the durable transcript SOURCE
25
+ * ORDER (records are append-only, so byte position is strictly increasing
26
+ * within one rollout). All before/after/last-N/next-assistant logic orders
27
+ * by this value, never by the SQLite insertion id: a live hook row is
28
+ * inserted BEFORE the older history that a later catch-up ingests, so
29
+ * insertion order is not transcript order (PR #1455 review P1).
30
+ */
31
+ readonly recordByteStart?: number;
32
+ /** Per-rollout physical record ordinal (G1 §4) — tiebreaker/telemetry alongside the byte order. */
33
+ readonly recordOrdinal?: number;
34
+ readonly assistantItemId?: string;
35
+ /** assistant phase: 'commentary' | 'final_answer' (assistant_turn only). */
36
+ readonly phase?: string;
37
+ readonly toolUseId?: string;
38
+ /** Transcript model-level call id (call_*) — different id space than toolUseId. */
39
+ readonly transcriptToolCallId?: string;
40
+ readonly visibleText?: string;
41
+ readonly toolFacts?: unknown;
42
+ readonly source: GovernanceObservationSource;
43
+ readonly completeness: GovernanceObservationCompleteness;
44
+ readonly observedAt: string;
45
+ }
46
+ export interface GovernanceCheckpointInput {
47
+ readonly hostKind: 'codex';
48
+ readonly rolloutIdentity: string;
49
+ readonly byteOffset: number;
50
+ readonly lastOrdinal: number;
51
+ readonly cliVersion?: string;
52
+ readonly rootSessionId: string;
53
+ readonly incompleteTail: boolean;
54
+ }
55
+ export interface GovernanceIngestDegradation {
56
+ readonly reason: string;
57
+ readonly ordinal?: number;
58
+ readonly nextAction?: string;
59
+ }
60
+ export interface IngestGovernanceObservationsResult {
61
+ readonly ok: boolean;
62
+ readonly reason?: string;
63
+ readonly nextAction?: string;
64
+ readonly inserted: number;
65
+ readonly enriched: number;
66
+ readonly duplicates: number;
67
+ readonly checkpointCommitted: boolean;
68
+ readonly warnings: readonly string[];
69
+ }
70
+ export interface GovernanceCheckpointRecord {
71
+ readonly hostKind: string;
72
+ readonly rolloutIdentity: string;
73
+ readonly byteOffset: number;
74
+ readonly lastOrdinal: number;
75
+ readonly cliVersion: string | null;
76
+ readonly rootSessionId: string;
77
+ readonly incompleteTail: boolean;
78
+ readonly lastDegradationReason: string | null;
79
+ readonly lastDegradationOrdinal: number | null;
80
+ readonly updatedAt: string;
81
+ }
82
+ export interface GovernanceObservationRecord {
83
+ readonly id: number;
84
+ readonly rolloutIdentity: string;
85
+ readonly rootSessionId: string;
86
+ readonly hostTurnId: string;
87
+ readonly kind: GovernanceObservationKind;
88
+ readonly logicalKey: string;
89
+ readonly transcriptRecordKey: string | null;
90
+ readonly sourceOrder: number | null;
91
+ readonly sourceOrdinal: number | null;
92
+ readonly assistantItemId: string | null;
93
+ readonly phase: string | null;
94
+ readonly toolUseId: string | null;
95
+ readonly transcriptToolCallId: string | null;
96
+ readonly visibleText: string | null;
97
+ readonly sanitizedToolFactsJson: string | null;
98
+ readonly source: GovernanceObservationSource;
99
+ readonly completeness: GovernanceObservationCompleteness;
100
+ readonly retentionClass: GovernanceRetentionClass;
101
+ readonly observedAt: string;
102
+ readonly promotionRef: string | null;
103
+ }
104
+ export interface PromoteGovernanceEvidenceInput {
105
+ readonly workspaceDir: string;
106
+ readonly hostKind: 'codex';
107
+ readonly rolloutIdentity: string;
108
+ readonly triggerLogicalKey: string;
109
+ /** Caller-provided pain reference. Slice A never mints a pain identity (SPEC §18). */
110
+ readonly painRef: string;
111
+ readonly now?: Date;
112
+ readonly databaseFactory?: ObservationDatabaseFactory;
113
+ }
114
+ export type PromoteGovernanceEvidenceResult = {
115
+ ok: true;
116
+ promoted: number;
117
+ tailState: 'completed' | 'pending';
118
+ warnings: readonly string[];
119
+ } | {
120
+ ok: false;
121
+ reason: string;
122
+ nextAction: string;
123
+ };
124
+ export type ObservationDatabaseFactory = (databasePath: string) => Database.Database;
125
+ /**
126
+ * Shared with the sibling governance-signal-admission module (Slice B): both
127
+ * modules own separate governance_* tables in the same trajectory.db, and the
128
+ * admission/reconciliation paths read observation-owned tables (promotion
129
+ * tails). One open must make the whole governance schema ready.
130
+ */
131
+ export declare function ensureGovernanceSchema(db: Database.Database): void;
132
+ type Degradation = {
133
+ ok: false;
134
+ reason: string;
135
+ nextAction: string;
136
+ };
137
+ export interface GovernanceRolloutDescriptor {
138
+ readonly hostKind: 'codex';
139
+ readonly rolloutIdentity: string;
140
+ readonly rootSessionId: string;
141
+ readonly parentRolloutIdentity?: string;
142
+ readonly agentIdentity?: string;
143
+ readonly agentDepth?: number;
144
+ }
145
+ export interface IngestGovernanceObservationsInput {
146
+ readonly workspaceDir: string;
147
+ /** Rollout lineage, established even when a delta contains no projected observations (markers/skips only). */
148
+ readonly rollout?: GovernanceRolloutDescriptor;
149
+ readonly observations: readonly GovernanceObservationInput[];
150
+ readonly checkpoint?: GovernanceCheckpointInput;
151
+ readonly degradations?: readonly GovernanceIngestDegradation[];
152
+ readonly compactionTimestamp?: string;
153
+ /** Sequential rollback markers (num_turns each) decoded from the transcript delta. */
154
+ readonly rollbackTurns?: readonly number[];
155
+ readonly now?: Date;
156
+ readonly databaseFactory?: ObservationDatabaseFactory;
157
+ }
158
+ export declare function ingestGovernanceObservations(input: IngestGovernanceObservationsInput): IngestGovernanceObservationsResult;
159
+ export interface ReadGovernanceCheckpointArgs {
160
+ readonly workspaceDir: string;
161
+ readonly hostKind: 'codex';
162
+ readonly rolloutIdentity: string;
163
+ readonly databaseFactory?: ObservationDatabaseFactory;
164
+ }
165
+ export declare function readGovernanceCheckpoint(args: ReadGovernanceCheckpointArgs): GovernanceCheckpointRecord | null | Degradation;
166
+ export interface ListGovernanceCheckpointsArgs {
167
+ readonly workspaceDir: string;
168
+ readonly hostKind: 'codex';
169
+ readonly databaseFactory?: ObservationDatabaseFactory;
170
+ }
171
+ export type ListGovernanceCheckpointsResult = {
172
+ ok: true;
173
+ checkpoints: readonly GovernanceCheckpointRecord[];
174
+ } | Degradation;
175
+ /**
176
+ * List every checkpoint for a host kind, oldest-updated first (PRI-624).
177
+ * The Slice C worker/CLI catch-up uses this to find rollouts with pending
178
+ * transcript lag — the checkpoint set is exactly the set of rollouts the
179
+ * authenticated hook previously delivered, so this is never session
180
+ * discovery (SPEC §13 / ADR-0020 §11.2).
181
+ */
182
+ export declare function listGovernanceCheckpoints(args: ListGovernanceCheckpointsArgs): ListGovernanceCheckpointsResult;
183
+ export interface ListGovernanceObservationsInput {
184
+ readonly workspaceDir: string;
185
+ readonly rolloutIdentity?: string;
186
+ readonly retentionClass?: GovernanceRetentionClass;
187
+ readonly limit?: number;
188
+ readonly databaseFactory?: ObservationDatabaseFactory;
189
+ }
190
+ export type ListGovernanceObservationsResult = {
191
+ ok: true;
192
+ observations: readonly GovernanceObservationRecord[];
193
+ } | Degradation;
194
+ export declare function listGovernanceObservations(input: ListGovernanceObservationsInput): ListGovernanceObservationsResult;
195
+ export declare function promoteGovernanceEvidence(input: PromoteGovernanceEvidenceInput): PromoteGovernanceEvidenceResult;
196
+ export {};