@principles/host-runtime 0.14.6 → 0.15.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.
@@ -1,4 +1,4 @@
1
- import { type SignalCollectorOutput, type UnifiedKeywordStore } from '@principles/core/runtime-v2';
1
+ import { type LlmClassificationResult, type SignalCollectorOutput, type Stage2GateVerdict, type UnifiedKeywordStore } from '@principles/core/runtime-v2';
2
2
  import { type ObservationDatabaseFactory } from './governance-observation-store.js';
3
3
  export declare const GOVERNANCE_STRONG_PAIN_SCORE = 70;
4
4
  export declare const GOVERNANCE_STRONG_RATE_LIMIT_PER_HOUR = 5;
@@ -10,10 +10,13 @@ type Degradation = {
10
10
  };
11
11
  /** seed/owner_promoted 词进入高精度 deterministic path 的权重阈值 */
12
12
  export declare const HIGH_PRECISION_LEARNED_WEIGHT = 0.7;
13
- /** PRI-788 G3: llm 学习词 earned precision 的 TP 门槛(且要求零 FP) */
14
- export declare const EARNED_PRECISION_MIN_TP = 3;
15
13
  /** 高精度纠正短语 overlay(已验证的确定性 STRONG 路径,不属于 learner seed 集) */
16
14
  export declare const HIGH_PRECISION_CORRECTION_OVERLAY: readonly (readonly [string, number])[];
15
+ /**
16
+ * SPEC v0.3 §3.2.1 降级词:口语高频、可被嵌入引用或否定句,不构成 high。
17
+ * 它们仍是候选(进入 Stage 2),但永不直达 STRONG。
18
+ */
19
+ export declare const AMBIGUOUS_CORRECTION_OVERLAY: readonly (readonly [string, number])[];
17
20
  /** empathy seed overlay(检测行为不变) */
18
21
  export declare const EMPATHY_SEED_OVERLAY: readonly (readonly [string, number])[];
19
22
  export declare function buildSharedSeedKeywordStore(): UnifiedKeywordStore;
@@ -85,7 +88,7 @@ export interface GovernanceCorrectionCandidate {
85
88
  * Owner words bound to the host-written live turn context). Audit-only:
86
89
  * admission, rate limit and idempotency are identical either way.
87
90
  */
88
- readonly explicitChannel?: 'codex_explicit_prefix' | 'codex_explicit_agent';
91
+ readonly explicitChannel?: 'codex_explicit_prefix' | 'codex_explicit_agent' | 'workbuddy_explicit_prefix';
89
92
  }
90
93
  export interface GovernanceToolFailureCandidate {
91
94
  readonly kind: 'tool_failure';
@@ -139,7 +142,46 @@ export type GovernanceAdmissionOutcome = {
139
142
  disposition: 'rate_limited';
140
143
  kind: 'user_correction';
141
144
  logicalObservationKey: string;
145
+ }
146
+ /** Ambiguous Stage-1 candidate persisted for Stage 2 review (SPEC §4.4.8). */
147
+ | {
148
+ disposition: 'pending_llm_review';
149
+ kind: 'user_correction';
150
+ logicalObservationKey: string;
151
+ duplicate: boolean;
152
+ }
153
+ /** Redelivery of a candidate that already reached a non-admitted terminal state. */
154
+ | {
155
+ disposition: 'resolved_none' | 'abandoned';
156
+ kind: 'user_correction';
157
+ logicalObservationKey: string;
158
+ reason: string | null;
142
159
  } | Degradation;
160
+ interface Stage2ReviewRecord {
161
+ readonly verdict: Stage2GateVerdict;
162
+ readonly confidence: number | null;
163
+ readonly reason: string;
164
+ }
165
+ export interface PendingCorrectionCandidate {
166
+ readonly logicalObservationKey: string;
167
+ readonly hostKind: 'codex' | 'workbuddy';
168
+ readonly rolloutIdentity: string;
169
+ readonly rootSessionId: string;
170
+ readonly hostTurnId: string;
171
+ readonly text: string;
172
+ readonly matchedTerms: readonly string[];
173
+ readonly attempts: number;
174
+ readonly claimedAt: string | null;
175
+ readonly claimToken: string | null;
176
+ readonly reviewResult: Stage2ReviewRecord | null;
177
+ readonly createdAt: string;
178
+ }
179
+ /**
180
+ * @deprecated SPEC v0.3 §3.2.3 (D5) retired earned precision: learned keywords
181
+ * are Stage-2 candidates only. This constant is retained ONLY for public API
182
+ * compatibility; no admission code consults it.
183
+ */
184
+ export declare const EARNED_PRECISION_MIN_TP = 3;
143
185
  export interface AdmitGovernanceSignalsInput {
144
186
  readonly workspaceDir: string;
145
187
  readonly candidates: readonly GovernanceSignalCandidate[];
@@ -154,6 +196,93 @@ export type AdmitGovernanceSignalsResult = {
154
196
  } | Degradation;
155
197
  /** Public generic intake stays Codex + explicit WorkBuddy only. */
156
198
  export declare function admitGovernanceSignals(input: AdmitGovernanceSignalsInput): AdmitGovernanceSignalsResult;
199
+ export interface PendingReviewResult {
200
+ readonly ok: true;
201
+ readonly disposition: 'admitted' | 'resolved_none' | 'pending' | 'abandoned' | 'concurrent_loss' | 'not_pending';
202
+ readonly logicalObservationKey: string;
203
+ readonly canonicalPainId?: string;
204
+ readonly taskId?: string;
205
+ readonly attempts: number;
206
+ readonly reason: string | null;
207
+ }
208
+ export type PendingReviewCallResult = PendingReviewResult | Degradation;
209
+ export interface ClaimPendingCandidatesInput {
210
+ readonly workspaceDir: string;
211
+ readonly limit?: number;
212
+ readonly now?: Date;
213
+ readonly databaseFactory?: ObservationDatabaseFactory;
214
+ }
215
+ export type ClaimPendingCandidatesResult = {
216
+ ok: true;
217
+ candidates: readonly PendingCorrectionCandidate[];
218
+ expired: number;
219
+ } | Degradation;
220
+ /**
221
+ * Claim bounded pending candidates with an optimistic CAS on the exact payload
222
+ * text. Rows whose claim is younger than PENDING_REVIEW_CLAIM_TTL_MS are
223
+ * skipped (another worker is reviewing them); TTL-expired rows become
224
+ * `abandoned(ttl_expired)` here, before any LLM work.
225
+ */
226
+ export declare function claimPendingCorrectionCandidates(input: ClaimPendingCandidatesInput): ClaimPendingCandidatesResult;
227
+ export interface ReleasePendingClaimInput {
228
+ readonly workspaceDir: string;
229
+ readonly logicalObservationKey: string;
230
+ /** The fencing token returned by `claimPendingCorrectionCandidates`. */
231
+ readonly claimToken: string;
232
+ readonly databaseFactory?: ObservationDatabaseFactory;
233
+ }
234
+ export type ReleasePendingClaimResult = {
235
+ ok: true;
236
+ released: boolean;
237
+ reason: 'released' | 'not_pending' | 'review_already_persisted' | 'claim_not_owned' | 'claim_changed' | 'claim_absent';
238
+ } | Degradation;
239
+ /**
240
+ * Release THIS worker's claim when no classification started (budget exhausted
241
+ * or classifier unavailable), so the next cycle can re-claim immediately.
242
+ *
243
+ * Guard rails:
244
+ * - only `pending_llm_review` rows;
245
+ * - never when a durable reviewResult exists (that outcome must be consumed by
246
+ * settle, not thrown away);
247
+ * - the persisted claim token must still be the one this worker took;
248
+ * - CAS on the exact payload text, so a concurrent worker's state is untouched.
249
+ */
250
+ export declare function releasePendingCorrectionClaim(input: ReleasePendingClaimInput): ReleasePendingClaimResult;
251
+ export interface CompletePendingReviewInput {
252
+ readonly workspaceDir: string;
253
+ readonly logicalObservationKey: string;
254
+ /** Fencing token from `claimPendingCorrectionCandidates`; required. */
255
+ readonly claimToken: string;
256
+ /** `null` = the classifier produced no usable verdict (timeout/exception). */
257
+ readonly llmResult: LlmClassificationResult | null;
258
+ readonly now?: Date;
259
+ readonly databaseFactory?: ObservationDatabaseFactory;
260
+ }
261
+ /**
262
+ * Durable Phase 1: persist the Stage-2 verdict + attempts into the pending
263
+ * payload (CAS on the payload text). A crash after this commit loses no LLM
264
+ * result; `settlePendingCorrection` finishes the transition without re-calling.
265
+ */
266
+ export declare function recordPendingCorrectionReview(input: CompletePendingReviewInput): PendingReviewCallResult;
267
+ export interface SettlePendingReviewInput {
268
+ readonly workspaceDir: string;
269
+ readonly logicalObservationKey: string;
270
+ /**
271
+ * Fencing token when this worker recorded the verdict in the current cycle.
272
+ * Omitted only for crash recovery of an ALREADY persisted reviewResult.
273
+ */
274
+ readonly claimToken?: string;
275
+ readonly now?: Date;
276
+ readonly databaseFactory?: ObservationDatabaseFactory;
277
+ }
278
+ /**
279
+ * Durable Phase 2: apply the persisted verdict. Correction → admitted (CAS,
280
+ * pain dedup, rate limit, marker swap); confident none → resolved_none;
281
+ * low-confidence/error/empathy → pending until MAX_ATTEMPTS or TTL; rate
282
+ * limiting rolls the transaction back and leaves the row pending without
283
+ * consuming another attempt (no LLM happened here).
284
+ */
285
+ export declare function settlePendingCorrection(input: SettlePendingReviewInput): Promise<PendingReviewCallResult>;
157
286
  /**
158
287
  * AUDIT-P3-01 (2026-10-21): WorkBuddy live-keyword 检测接入共享 live 词库
159
288
  * (`<workspace>/.state/correction_keywords.json`,seed ∪ learned,mtime 刷新,
@@ -169,6 +298,18 @@ export declare function resolveSharedCorrectionKeywordStoreForDetection(workspac
169
298
  * store parameter is how the shared live vocabulary enters (AUDIT-P3-01);
170
299
  * omitted, the check keeps the P0 four-term projection (backward compatible).
171
300
  */
301
+ export type WorkbuddyCorrectionCandidateKind = 'strong' | 'ambiguous' | 'none';
302
+ /**
303
+ * Shared WorkBuddy Stage-1 assessment. `strong` may admit inline (no LLM);
304
+ * `ambiguous` persists a pending Stage-2 candidate (zero LLM in the hook);
305
+ * `none` writes nothing. The startsWith pre-filter (SPEC P-5) is retained
306
+ * until the cross-host negative matrix authorizes its retirement.
307
+ */
308
+ export declare function assessWorkbuddyCorrectionCandidate(text: string, sessionId: string, store?: UnifiedKeywordStore): {
309
+ kind: WorkbuddyCorrectionCandidateKind;
310
+ reason: 'matched' | 'not_a_signal' | 'prompt_too_large';
311
+ };
312
+ /** Backward-compatible boolean view: only `strong` counts as matched. */
172
313
  export declare function assessWorkbuddyLiveKeyword(text: string, sessionId: string, store?: UnifiedKeywordStore): {
173
314
  matched: boolean;
174
315
  reason: 'matched' | 'not_a_signal' | 'prompt_too_large';
@@ -195,6 +336,8 @@ export type EnsureGovernanceTaskResult = {
195
336
  created: boolean;
196
337
  duplicate: boolean;
197
338
  linkRepaired: boolean;
339
+ /** Present when the Owner-manual ingress legitimately degraded (honest empty evidence). */
340
+ ingressWarning?: string;
198
341
  } | Degradation;
199
342
  /**
200
343
  * Ensure exactly one pending Diagnostician task exists for an admitted