@loreai/core 0.15.0 → 0.16.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.
@@ -1,16 +1,17 @@
1
1
  /**
2
- * Dynamic worker model selection.
2
+ * Worker model resolution.
3
3
  *
4
- * Background workers (distillation, curation, query expansion) don't need
5
- * frontier reasoning. This module discovers cheaper models from the same
6
- * provider and validates their quality via a two-phase comparison:
7
- * Phase 1: structural checks (parsability, observation count, token bounds)
8
- * Phase 2: LLM judge (session model rates candidate output vs reference)
4
+ * Background workers (distillation, curation, query expansion) use the session
5
+ * model by default. An explicit `workerModel` config override is supported for
6
+ * cases where the user wants to pin background work to a specific model.
9
7
  *
10
- * Results are persisted in kv_meta and re-evaluated when the model landscape
11
- * changes (new models, session model switch, model deprecation).
8
+ * Previously this module contained dynamic worker model selection with
9
+ * candidate discovery, two-phase validation (structural check + LLM judge),
10
+ * and fingerprint-based staleness detection. That complexity was removed in
11
+ * favor of always using the session model — A/B testing showed the quality
12
+ * gap on complex conversations wasn't worth the infrastructure cost.
12
13
  */
13
- /** Minimal model info needed for worker selection — provider-agnostic. */
14
+ /** Minimal model info kept for downstream consumers. */
14
15
  export type ModelInfo = {
15
16
  id: string;
16
17
  providerID: string;
@@ -26,80 +27,11 @@ export type ModelInfo = {
26
27
  reasoning?: boolean;
27
28
  };
28
29
  };
29
- /** Result of a worker model validation stored in kv_meta. */
30
- export type WorkerModelResult = {
31
- modelID: string;
32
- providerID: string;
33
- fingerprint: string;
34
- validatedAt: number;
35
- judgeScore: number | null;
36
- };
37
- /**
38
- * Select worker model candidates from the available models.
39
- *
40
- * Returns up to 2 candidates: cheapest overall + one tier below the session
41
- * model. The session model itself is included (if it's the cheapest, the list
42
- * has 1 entry and no comparison is needed).
43
- */
44
- export declare function selectWorkerCandidates(sessionModel: {
45
- id: string;
46
- providerID: string;
47
- cost: {
48
- input: number;
49
- };
50
- }, providerModels: ModelInfo[]): ModelInfo[];
51
- /**
52
- * Compute a fingerprint from the model landscape. Changes when:
53
- * - Models are added or removed from the provider
54
- * - The session model changes
55
- */
56
- export declare function computeModelFingerprint(providerID: string, sessionModelID: string, activeModelIDs: string[]): string;
57
- export declare function getValidatedWorkerModel(providerID: string): WorkerModelResult | null;
58
- export declare function storeValidatedWorkerModel(result: WorkerModelResult): void;
59
- /** Clear a stored worker model validation (e.g. when the model is deprecated). */
60
- export declare function clearValidatedWorkerModel(providerID: string): void;
61
- /**
62
- * Check whether the stored validation is stale (fingerprint mismatch).
63
- */
64
- export declare function isValidationStale(stored: WorkerModelResult | null, currentFingerprint: string): boolean;
65
- export type StructuralCheckResult = {
66
- passed: boolean;
67
- observationCount: number;
68
- tokenCount: number;
69
- reason?: string;
70
- };
71
- /**
72
- * Structural quality check: does the candidate distillation output meet
73
- * minimum quality thresholds relative to the reference?
74
- */
75
- export declare function structuralCheck(candidateObservations: string | null, referenceObservations: string): StructuralCheckResult;
76
- export declare const WORKER_JUDGE_SYSTEM = "You are evaluating distillation quality. You will be given a REFERENCE distillation (produced by a capable model) and a CANDIDATE distillation (produced by a cheaper model) of the same conversation segment.\n\nRate the candidate on a scale of 1-5:\n5 = Captures all key facts and decisions, equivalent to reference\n4 = Captures most facts, minor omissions\n3 = Captures the essential facts, some detail loss acceptable\n2 = Missing important facts or technical details\n1 = Significantly incomplete or inaccurate\n\nRespond with ONLY a single digit (1-5).";
77
- export declare function workerJudgeUser(reference: string, candidate: string): string;
78
- /** Parse the judge's score from a response. Returns null on parse failure. */
79
- export declare function parseJudgeScore(response: string): number | null;
80
- import type { LLMClient } from "./types";
81
- export type ValidationInput = {
82
- llm: LLMClient;
83
- providerID: string;
84
- sessionModelID: string;
85
- candidates: ModelInfo[];
86
- /** Recent gen-0 distillation to use as reference (observations text). */
87
- referenceObservations: string;
88
- /** Source messages text for re-running distillation with candidates. */
89
- sourceMessagesText: string;
90
- /** Date string for the distillation prompt. */
91
- date: string;
92
- };
93
- /**
94
- * Run the two-phase quality validation for worker model candidates.
95
- * Returns the cheapest passing candidate, or null if none pass.
96
- */
97
- export declare function runValidation(input: ValidationInput): Promise<WorkerModelResult | null>;
98
30
  /**
99
31
  * Resolve the effective worker model for a given provider.
100
- * Priority: explicit config > validated auto-selection > session model (fallback).
32
+ * Priority: explicit config override > session model (fallback).
101
33
  */
102
- export declare function resolveWorkerModel(providerID: string, configWorkerModel?: {
34
+ export declare function resolveWorkerModel(_providerID: string, configWorkerModel?: {
103
35
  providerID: string;
104
36
  modelID: string;
105
37
  }, configModel?: {
@@ -1 +1 @@
1
- {"version":3,"file":"worker-model.d.ts","sourceRoot":"","sources":["../../src/worker-model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAUH,0EAA0E;AAC1E,MAAM,MAAM,SAAS,GAAG;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE;QACZ,KAAK,EAAE;YAAE,IAAI,EAAE,OAAO,CAAA;SAAE,CAAC;QACzB,+DAA+D;QAC/D,SAAS,CAAC,EAAE,OAAO,CAAC;KACrB,CAAC;CACH,CAAC;AAEF,6DAA6D;AAC7D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B,CAAC;AAQF;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,YAAY,EAAE;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,EACzE,cAAc,EAAE,SAAS,EAAE,GAC1B,SA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E,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACpD;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAMrD"}
@@ -29005,208 +29005,10 @@ function importLoreFile(projectPath) {
29005
29005
  // src/worker-model.ts
29006
29006
  var worker_model_exports = {};
29007
29007
  __export(worker_model_exports, {
29008
- WORKER_JUDGE_SYSTEM: () => WORKER_JUDGE_SYSTEM,
29009
- clearValidatedWorkerModel: () => clearValidatedWorkerModel,
29010
- computeModelFingerprint: () => computeModelFingerprint,
29011
- getValidatedWorkerModel: () => getValidatedWorkerModel,
29012
- isValidationStale: () => isValidationStale,
29013
- parseJudgeScore: () => parseJudgeScore,
29014
- resolveWorkerModel: () => resolveWorkerModel,
29015
- runValidation: () => runValidation,
29016
- selectWorkerCandidates: () => selectWorkerCandidates,
29017
- storeValidatedWorkerModel: () => storeValidatedWorkerModel,
29018
- structuralCheck: () => structuralCheck,
29019
- workerJudgeUser: () => workerJudgeUser
29008
+ resolveWorkerModel: () => resolveWorkerModel
29020
29009
  });
29021
- var KV_PREFIX = "lore:worker_model:";
29022
- function selectWorkerCandidates(sessionModel, providerModels) {
29023
- const eligible = providerModels.filter(
29024
- (m) => m.providerID === sessionModel.providerID && m.status === "active" && m.capabilities.input.text
29025
- );
29026
- if (eligible.length === 0) return [];
29027
- const sorted = [...eligible].sort((a, b) => {
29028
- const costDiff = a.cost.input - b.cost.input;
29029
- if (costDiff !== 0) return costDiff;
29030
- const aReasoning = a.capabilities.reasoning ? 1 : 0;
29031
- const bReasoning = b.capabilities.reasoning ? 1 : 0;
29032
- return aReasoning - bReasoning;
29033
- });
29034
- const cheapest = sorted[0];
29035
- const belowSession = sorted.filter((m) => m.cost.input < sessionModel.cost.input).pop();
29036
- const candidates = /* @__PURE__ */ new Map();
29037
- candidates.set(cheapest.id, cheapest);
29038
- if (belowSession && belowSession.id !== cheapest.id) {
29039
- candidates.set(belowSession.id, belowSession);
29040
- }
29041
- if (cheapest.id === sessionModel.id || cheapest.cost.input >= sessionModel.cost.input) {
29042
- return [cheapest];
29043
- }
29044
- return [...candidates.values()];
29045
- }
29046
- function computeModelFingerprint(providerID, sessionModelID, activeModelIDs) {
29047
- const sorted = [...activeModelIDs].sort();
29048
- return sha256(
29049
- JSON.stringify({ providerID, sessionModelID, modelIDs: sorted })
29050
- );
29051
- }
29052
- function getValidatedWorkerModel(providerID) {
29053
- const row = db().query("SELECT value FROM kv_meta WHERE key = ?").get(`${KV_PREFIX}${providerID}`);
29054
- if (!row) return null;
29055
- try {
29056
- return JSON.parse(row.value);
29057
- } catch {
29058
- return null;
29059
- }
29060
- }
29061
- function storeValidatedWorkerModel(result) {
29062
- const key = `${KV_PREFIX}${result.providerID}`;
29063
- const value = JSON.stringify(result);
29064
- db().query(
29065
- "INSERT INTO kv_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = ?"
29066
- ).run(key, value, value);
29067
- }
29068
- function clearValidatedWorkerModel(providerID) {
29069
- db().query("DELETE FROM kv_meta WHERE key = ?").run(`${KV_PREFIX}${providerID}`);
29070
- }
29071
- function isValidationStale(stored, currentFingerprint) {
29072
- if (!stored) return true;
29073
- return stored.fingerprint !== currentFingerprint;
29074
- }
29075
- function structuralCheck(candidateObservations, referenceObservations) {
29076
- if (candidateObservations == null || candidateObservations.length === 0) {
29077
- return { passed: false, observationCount: 0, tokenCount: 0, reason: candidateObservations === null ? "parse_failed" : "empty" };
29078
- }
29079
- const countObs = (text4) => text4.split("\n").filter((l) => l.trim().length > 0).length;
29080
- const refCount = countObs(referenceObservations);
29081
- const candCount = countObs(candidateObservations);
29082
- const candTokens = Math.ceil(candidateObservations.length / 3);
29083
- if (refCount > 0 && (candCount < refCount * 0.5 || candCount > refCount * 1.5)) {
29084
- return {
29085
- passed: false,
29086
- observationCount: candCount,
29087
- tokenCount: candTokens,
29088
- reason: `observation_count_${candCount}_vs_ref_${refCount}`
29089
- };
29090
- }
29091
- const refTokens = Math.ceil(referenceObservations.length / 3);
29092
- if (candTokens === 0) {
29093
- return { passed: false, observationCount: candCount, tokenCount: candTokens, reason: "empty" };
29094
- }
29095
- if (refTokens > 0 && candTokens > refTokens * 3) {
29096
- return {
29097
- passed: false,
29098
- observationCount: candCount,
29099
- tokenCount: candTokens,
29100
- reason: `token_count_${candTokens}_vs_ref_${refTokens}_3x`
29101
- };
29102
- }
29103
- return { passed: true, observationCount: candCount, tokenCount: candTokens };
29104
- }
29105
- var WORKER_JUDGE_SYSTEM = `You are evaluating distillation quality. You will be given a REFERENCE distillation (produced by a capable model) and a CANDIDATE distillation (produced by a cheaper model) of the same conversation segment.
29106
-
29107
- Rate the candidate on a scale of 1-5:
29108
- 5 = Captures all key facts and decisions, equivalent to reference
29109
- 4 = Captures most facts, minor omissions
29110
- 3 = Captures the essential facts, some detail loss acceptable
29111
- 2 = Missing important facts or technical details
29112
- 1 = Significantly incomplete or inaccurate
29113
-
29114
- Respond with ONLY a single digit (1-5).`;
29115
- function workerJudgeUser(reference, candidate) {
29116
- return `<reference>
29117
- ${reference}
29118
- </reference>
29119
-
29120
- <candidate>
29121
- ${candidate}
29122
- </candidate>`;
29123
- }
29124
- function parseJudgeScore(response) {
29125
- const match = response.trim().match(/^([1-5])/);
29126
- if (!match) return null;
29127
- return parseInt(match[1], 10);
29128
- }
29129
- async function runValidation(input) {
29130
- const { llm, candidates, referenceObservations, sourceMessagesText, date: date5 } = input;
29131
- const userPrompt = distillationUser({
29132
- messages: sourceMessagesText,
29133
- date: date5
29134
- });
29135
- for (const candidate of candidates) {
29136
- if (candidate.id === input.sessionModelID) continue;
29137
- let candidateObservations = null;
29138
- try {
29139
- const raw = await llm.prompt(DISTILLATION_SYSTEM, userPrompt, {
29140
- model: { providerID: candidate.providerID, modelID: candidate.id },
29141
- workerID: "lore-distill",
29142
- thinking: false
29143
- });
29144
- if (raw) {
29145
- const match = raw.match(/<observations>([\s\S]*?)<\/observations>/);
29146
- candidateObservations = match ? match[1].trim() : raw.trim();
29147
- }
29148
- } catch (e) {
29149
- warn(`worker model validation: candidate ${candidate.id} failed:`, e);
29150
- continue;
29151
- }
29152
- const structural = structuralCheck(candidateObservations, referenceObservations);
29153
- if (!structural.passed) {
29154
- info(
29155
- `worker model validation: ${candidate.id} failed structural check: ${structural.reason}`
29156
- );
29157
- continue;
29158
- }
29159
- let judgeScore = null;
29160
- try {
29161
- const judgeResponse = await llm.prompt(
29162
- WORKER_JUDGE_SYSTEM,
29163
- workerJudgeUser(referenceObservations, candidateObservations),
29164
- { workerID: "lore-distill", thinking: false }
29165
- // use session model (no model override)
29166
- );
29167
- if (judgeResponse) {
29168
- judgeScore = parseJudgeScore(judgeResponse);
29169
- }
29170
- } catch (e) {
29171
- warn(`worker model validation: judge call failed for ${candidate.id}:`, e);
29172
- }
29173
- if (judgeScore !== null && judgeScore < 3) {
29174
- info(
29175
- `worker model validation: ${candidate.id} failed judge (score=${judgeScore})`
29176
- );
29177
- continue;
29178
- }
29179
- const fingerprint = computeModelFingerprint(
29180
- input.providerID,
29181
- input.sessionModelID,
29182
- candidates.map((c) => c.id)
29183
- );
29184
- const result = {
29185
- modelID: candidate.id,
29186
- providerID: candidate.providerID,
29187
- fingerprint,
29188
- validatedAt: Date.now(),
29189
- judgeScore
29190
- };
29191
- storeValidatedWorkerModel(result);
29192
- info(
29193
- `worker model validated: ${candidate.id} (judge=${judgeScore}) for provider ${input.providerID}`
29194
- );
29195
- return result;
29196
- }
29197
- clearValidatedWorkerModel(input.providerID);
29198
- info(
29199
- `worker model validation: no candidate passed for ${input.providerID} \u2014 cleared stale entry`
29200
- );
29201
- return null;
29202
- }
29203
- function resolveWorkerModel(providerID, configWorkerModel, configModel) {
29010
+ function resolveWorkerModel(_providerID, configWorkerModel, configModel) {
29204
29011
  if (configWorkerModel) return configWorkerModel;
29205
- const validated = getValidatedWorkerModel(providerID);
29206
- const MAX_AGE_MS = 24 * 60 * 60 * 1e3;
29207
- if (validated && Date.now() - validated.validatedAt <= MAX_AGE_MS) {
29208
- return { providerID: validated.providerID, modelID: validated.modelID };
29209
- }
29210
29012
  return configModel;
29211
29013
  }
29212
29014
  export {