@principles/host-runtime 4.10.0 → 4.11.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.
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Idle Turn Lightweight Audit — SPEC-PD-PAIN-DETECTION-UPGRADE-001 R5 (W6).
3
+ *
4
+ * Performs a bounded, budget-protected post-turn review of idle sessions
5
+ * (`>= 5` turns and idle `>= 5` minutes after true turn completion) and routes
6
+ * verified principle deviations strictly to Pain Inbox
7
+ * (`governance_signal_admissions`, `signal_kind = 'idle_audit_deviation'`,
8
+ * `decision = 'inbox_candidate'`). Never writes directly to `pain_events`.
9
+ *
10
+ * Architectural invariants (TD-PD-PAIN-DETECTION-UPGRADE-001 §2.6 / W6):
11
+ * 1. Accurate turn-end trigger (zero new background services):
12
+ * - Codex / WorkBuddy update `idle_audit_session_activity` on `Stop`.
13
+ * - OpenClaw registers native `agent_end` (never relying solely on
14
+ * `llm_output`). Legacy hosts without `agent_end` require `has_active_run = 0`.
15
+ * 2. Real prompt injection evidence + immutable artifact version + lifecycle:
16
+ * - Visibility is proved ONLY from `runtime_v2_prompt_activations_injected`
17
+ * events (`principleIds`, `activationIds`, `artifactIds`, `runId`,
18
+ * `hostKind`, `eventId`) — never from `environment_snapshots`.
19
+ * - `principleVersion` is resolved strictly from immutable `pi_artifacts`
20
+ * via `artifactIds`.
21
+ * - Because `activations` uses `INSERT OR REPLACE` and `PRI-932`
22
+ * (`activation_lifecycle_events`) is still DRAFT, if `activations` shows
23
+ * reactivation overwrite (`activated_at > observedAt`), missing lifecycle
24
+ * bounds, or missing immutable `principleVersion`, MUST return
25
+ * `verdict: 'INCONCLUSIVE'` — never infer historical state from current
26
+ * state and never fabricate a `deviationAnchor`.
27
+ * 3. Atomic daily budget persistence (`idle_audit_daily_budgets`):
28
+ * - Stored inside existing `trajectory.db`.
29
+ * - Reserved via `BEGIN IMMEDIATE` transaction BEFORE calling the LLM
30
+ * (default 10 calls / workspace / UTC day). Timeouts (`<= 25s`) keep the
31
+ * 1 consumed unit (never refunded or double-charged).
32
+ */
33
+ import Database from 'better-sqlite3';
34
+ import type { ObservationDatabaseFactory } from './governance-observation-store.js';
35
+ export declare const DEFAULT_IDLE_AUDIT_MIN_TURNS = 5;
36
+ export declare const DEFAULT_IDLE_AUDIT_IDLE_MS: number;
37
+ export declare const DEFAULT_IDLE_AUDIT_DAILY_LIMIT = 10;
38
+ export declare const DEFAULT_IDLE_AUDIT_TIMEOUT_MS = 25000;
39
+ export type IdleAuditHostKind = 'codex' | 'workbuddy' | 'openclaw';
40
+ export type IdleTurnEndSource = 'agent_end' | 'stop_hook' | 'legacy_idle_check';
41
+ export interface RecordTurnCompletedInput {
42
+ readonly workspaceDir: string;
43
+ readonly hostKind: IdleAuditHostKind;
44
+ readonly sessionId: string;
45
+ readonly turnEndSource: IdleTurnEndSource;
46
+ readonly completedAt?: Date;
47
+ readonly hasActiveRun?: boolean;
48
+ readonly databaseFactory?: ObservationDatabaseFactory;
49
+ }
50
+ export interface ReserveDailyBudgetInput {
51
+ readonly workspaceDir: string;
52
+ readonly dailyLimit?: number;
53
+ readonly now?: Date;
54
+ readonly databaseFactory?: ObservationDatabaseFactory;
55
+ }
56
+ export interface ReserveDailyBudgetResult {
57
+ readonly reserved: boolean;
58
+ readonly workspaceKey: string;
59
+ readonly utcDay: string;
60
+ readonly usedCount: number;
61
+ readonly dailyLimit: number;
62
+ readonly reason?: 'daily_budget_exhausted' | 'db_error';
63
+ }
64
+ export interface VerifiedDeviationAnchor {
65
+ readonly principleId: string;
66
+ readonly principleVersion: string;
67
+ readonly activationId: string;
68
+ readonly artifactId: string;
69
+ readonly eventId: string | null;
70
+ }
71
+ export type PrincipleVisibilityVerdict = {
72
+ readonly verdict: 'PROVEN';
73
+ readonly deviationAnchor: VerifiedDeviationAnchor;
74
+ } | {
75
+ readonly verdict: 'INCONCLUSIVE';
76
+ readonly reason: 'no_logs' | 'missing_injection_event' | 'missing_state_db' | 'missing_immutable_principle_version' | 'activation_lifecycle_overwritten_or_out_of_window' | 'verification_error';
77
+ };
78
+ export interface VerifyPrincipleVisibilityInput {
79
+ readonly workspaceDir: string;
80
+ readonly stateDir?: string;
81
+ readonly eventLogStateDir?: string;
82
+ readonly hostKind: IdleAuditHostKind;
83
+ readonly sessionId: string;
84
+ readonly principleId: string;
85
+ readonly observedAt: string;
86
+ readonly runId?: string;
87
+ readonly stateDbFactory?: ObservationDatabaseFactory;
88
+ }
89
+ export interface IdleAuditSessionCandidate {
90
+ readonly hostKind: IdleAuditHostKind;
91
+ readonly sessionId: string;
92
+ readonly rolloutIdentity: string;
93
+ readonly turnCount: number;
94
+ readonly lastObservationId: string;
95
+ readonly lastTurnCompletedAt: string;
96
+ readonly turnEndSource: string;
97
+ }
98
+ export interface IdleAuditLlmFinding {
99
+ readonly detected: boolean;
100
+ readonly principleId?: string;
101
+ readonly observedAt?: string;
102
+ readonly runId?: string;
103
+ readonly excerpt?: string;
104
+ readonly confidence?: number;
105
+ readonly reasoning?: string;
106
+ }
107
+ export type IdleTurnAuditor = (candidate: IdleAuditSessionCandidate) => Promise<IdleAuditLlmFinding | null>;
108
+ export interface RunIdleTurnAuditCycleInput {
109
+ readonly workspaceDir: string;
110
+ readonly stateDir?: string;
111
+ readonly eventLogStateDir?: string;
112
+ readonly auditor: IdleTurnAuditor | null;
113
+ readonly minTurns?: number;
114
+ readonly idleThresholdMs?: number;
115
+ readonly dailyLimit?: number;
116
+ readonly auditTimeoutMs?: number;
117
+ readonly now?: Date;
118
+ readonly databaseFactory?: ObservationDatabaseFactory;
119
+ readonly stateDbFactory?: ObservationDatabaseFactory;
120
+ }
121
+ export interface RunIdleTurnAuditCycleReport {
122
+ readonly ok: boolean;
123
+ readonly auditedSessions: number;
124
+ readonly admittedInboxCandidates: number;
125
+ readonly inconclusiveCount: number;
126
+ readonly timedOutCount: number;
127
+ readonly budgetReserved: boolean;
128
+ readonly skippedReason?: string;
129
+ readonly idempotencyKey?: string;
130
+ }
131
+ export declare function ensureIdleAuditSchema(db: Database.Database): void;
132
+ /**
133
+ * Record an accurate turn-end timestamp (`agent_end` for OpenClaw, `stop_hook`
134
+ * for Codex/WorkBuddy) into `idle_audit_session_activity`.
135
+ */
136
+ export declare function recordIdleAuditTurnCompleted(input: RecordTurnCompletedInput): {
137
+ ok: boolean;
138
+ reason?: string;
139
+ };
140
+ /**
141
+ * Atomically reserve 1 daily LLM audit slot for `(workspace_key, utc_day)`
142
+ * using a SQLite `BEGIN IMMEDIATE` transaction (W6).
143
+ */
144
+ export declare function reserveIdleAuditDailyBudget(input: ReserveDailyBudgetInput): ReserveDailyBudgetResult;
145
+ /**
146
+ * Verify that `principleId` was genuinely injected into the turn, resolve its
147
+ * immutable `principleVersion` via `artifactIds`, and verify `activations`
148
+ * lifecycle contains `observedAt` without overwrite (W6).
149
+ */
150
+ export declare function verifyPrincipleVisibilityForAudit(input: VerifyPrincipleVisibilityInput): PrincipleVisibilityVerdict;
151
+ /**
152
+ * Run one bounded Idle Turn Audit cycle (at most 1 eligible idle session per
153
+ * worker tick, zero new background daemons).
154
+ */
155
+ export declare function runIdleTurnAuditCycle(input: RunIdleTurnAuditCycleInput): Promise<RunIdleTurnAuditCycleReport>;