File size: 10,568 Bytes
9646e24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { nanoid } from "nanoid";
import {
  StrategyEvolutionProposal,
  StrategyGenome,
  StrategyComponent,
  StrategyExecutionPattern,
  StrategyContextCondition,
  EvidenceLevel,
} from "@/types";
import {
  loadStrategyEvolution,
  saveStrategyEvolution,
  saveStrategies,
} from "@/lib/config";
import { ensureStrategiesSeeded } from "@/lib/strategy/library";
import { getStrategyPerformance } from "@/lib/strategy/attribution";

const now = () => new Date().toISOString();

// ─── Evolution Store ───────────────────────────────────────────────

export function listProposals(): StrategyEvolutionProposal[] {
  return loadStrategyEvolution();
}

export function getProposalById(id: string): StrategyEvolutionProposal | undefined {
  return loadStrategyEvolution().find((p) => p.id === id);
}

function saveProposal(proposal: StrategyEvolutionProposal): void {
  const all = loadStrategyEvolution();
  const idx = all.findIndex((p) => p.id === proposal.id);
  if (idx >= 0) {
    all[idx] = proposal;
  } else {
    all.push(proposal);
  }
  saveStrategyEvolution(all);
}

// ─── Component Decomposition ───────────────────────────────────────

export interface DecomposedComponent {
  component: StrategyComponent;
  sourceStrategyId: string;
  sourceStrategyName: string;
  performanceScore: number;
}

export function decomposeStrategy(strategyId: string): DecomposedComponent[] {
  const strategies = ensureStrategiesSeeded();
  const strategy = strategies.find((s) => s.id === strategyId);
  if (!strategy) return [];

  const perf = getStrategyPerformance(strategyId);
  const performanceScore = perf ? perf.averageContribution * perf.successRate : 0;

  return strategy.components.map((component) => ({
    component: { ...component },
    sourceStrategyId: strategy.id,
    sourceStrategyName: strategy.name,
    performanceScore,
  }));
}

// ─── Component Recombination ───────────────────────────────────────

export function proposeRecombination(
  parentStrategyIds: string[],
  rationale: string,
  expectedImprovement: string
): StrategyEvolutionProposal {
  const strategies = ensureStrategiesSeeded();
  const parents = parentStrategyIds
    .map((id) => strategies.find((s) => s.id === id))
    .filter((s): s is StrategyGenome => s !== undefined);

  if (parents.length === 0) {
    throw new Error("No valid parent strategies found for recombination");
  }

  // Decompose all parents and select best-performing components
  const allComponents: DecomposedComponent[] = [];
  for (const parent of parents) {
    allComponents.push(...decomposeStrategy(parent.id));
  }

  // Sort by performance score descending, take top components
  const sortedComponents = allComponents.sort((a, b) => b.performanceScore - a.performanceScore);

  // Select unique components (by name) up to 5
  const seenNames = new Set<string>();
  const selectedComponents: StrategyComponent[] = [];
  for (const dc of sortedComponents) {
    if (!seenNames.has(dc.component.name) && selectedComponents.length < 5) {
      seenNames.add(dc.component.name);
      selectedComponents.push({
        ...dc.component,
        id: nanoid(10),
      });
    }
  }

  // Merge execution patterns from parents
  const mergedPattern: StrategyExecutionPattern = {
    stepOrder: dedupe(parents.flatMap((p) => p.executionPattern.stepOrder)),
    toolsUsed: dedupe(parents.flatMap((p) => p.executionPattern.toolsUsed)),
    timeAllocation: parents[0].executionPattern.timeAllocation,
    decisionRules: dedupe(parents.flatMap((p) => p.executionPattern.decisionRules)),
  };

  // Merge applicable context from parents
  const mergedContext: StrategyContextCondition[] = [];
  const contextKeys = new Set<string>();
  for (const parent of parents) {
    for (const c of parent.applicableContext) {
      const key = `${c.field}_${c.operator}_${JSON.stringify(c.value)}`;
      if (!contextKeys.has(key)) {
        contextKeys.add(key);
        mergedContext.push({ ...c });
      }
    }
  }

  const proposal: StrategyEvolutionProposal = {
    id: nanoid(12),
    parentStrategyIds,
    proposedComponents: selectedComponents,
    proposedExecutionPattern: mergedPattern,
    proposedContext: mergedContext,
    rationale,
    expectedImprovement,
    status: "proposed",
    complianceValidated: false,
    proposedAt: now(),
  };

  saveProposal(proposal);
  return proposal;
}

// ─── Proposal Validation & Deployment ──────────────────────────────

export function validateProposal(id: string): StrategyEvolutionProposal | undefined {
  const proposal = getProposalById(id);
  if (!proposal) return undefined;

  // Compliance validation: check that no component involves external communication,
  // claim modification, or patient-level targeting
  const complianceFlags: string[] = [];
  for (const comp of proposal.proposedComponents) {
    const nameLower = comp.name.toLowerCase();
    const descLower = comp.description.toLowerCase();
    if (nameLower.includes("claim") || descLower.includes("claim")) {
      complianceFlags.push(`Component "${comp.name}" may involve claims - requires compliance review.`);
    }
    if (descLower.includes("patient-level") || descLower.includes("patient level")) {
      complianceFlags.push(`Component "${comp.name}" may involve patient-level targeting - prohibited.`);
    }
    if (descLower.includes("unapproved") || descLower.includes("off-label")) {
      complianceFlags.push(`Component "${comp.name}" may involve unapproved/off-label content - prohibited.`);
    }
  }

  const complianceValidated = complianceFlags.length === 0;

  const updated: StrategyEvolutionProposal = {
    ...proposal,
    status: complianceValidated ? "validated" : "rejected",
    complianceValidated,
    validatedAt: now(),
  };

  saveProposal(updated);
  return updated;
}

export function deployProposal(id: string): StrategyGenome | undefined {
  const proposal = getProposalById(id);
  if (!proposal || proposal.status !== "validated" || !proposal.complianceValidated) {
    return undefined;
  }

  const parentStrategies = proposal.parentStrategyIds
    .map((pid) => ensureStrategiesSeeded().find((s) => s.id === pid))
    .filter((s): s is StrategyGenome => s !== undefined);

  if (parentStrategies.length === 0) return undefined;

  // Determine domain from most common parent domain
  const domainCounts: Record<string, number> = {};
  for (const p of parentStrategies) {
    domainCounts[p.domain] = (domainCounts[p.domain] || 0) + 1;
  }
  const domain = Object.entries(domainCounts).sort((a, b) => b[1] - a[1])[0]?.[0] as StrategyGenome["domain"];

  // Merge expected outcomes from parents
  const mergedOutcomes = parentStrategies[0].expectedOutcomes.map((o) => ({ ...o, observed: 0 }));

  const newGenome: StrategyGenome = {
    id: nanoid(12),
    name: `Evolved Strategy from ${parentStrategies.map((p) => p.name).join(" + ")}`,
    description: `Recombined strategy. ${proposal.rationale}`,
    domain,
    strategyClass: "experimental",
    components: proposal.proposedComponents,
    applicableContext: proposal.proposedContext,
    executionPattern: proposal.proposedExecutionPattern,
    expectedOutcomes: mergedOutcomes,
    evidenceLevel: "unresolved",
    evidenceCount: 0,
    parentIds: proposal.parentStrategyIds,
    childIds: [],
    version: 1,
    createdAt: now(),
    updatedAt: now(),
    deprecated: false,
    complianceValidated: true,
    complianceNotes: `Evolved from validated proposal ${proposal.id}. Compliance checked at validation time.`,
  };

  // Save new strategy
  const allStrategies = ensureStrategiesSeeded();
  allStrategies.push(newGenome);

  // Update parent childIds
  for (const pid of proposal.parentStrategyIds) {
    const idx = allStrategies.findIndex((s) => s.id === pid);
    if (idx >= 0) {
      allStrategies[idx].childIds.push(newGenome.id);
    }
  }

  saveStrategies(allStrategies);

  // Update proposal status
  const updatedProposal: StrategyEvolutionProposal = {
    ...proposal,
    status: "deployed",
  };
  saveProposal(updatedProposal);

  return newGenome;
}

export function rejectProposal(id: string): StrategyEvolutionProposal | undefined {
  const proposal = getProposalById(id);
  if (!proposal) return undefined;
  const updated: StrategyEvolutionProposal = {
    ...proposal,
    status: "rejected",
    validatedAt: now(),
  };
  saveProposal(updated);
  return updated;
}

// ─── Helpers ───────────────────────────────────────────────────────

function dedupe<T>(arr: T[]): T[] {
  return [...new Set(arr)];
}

// ─── Auto-Evolution: Find Best Candidates ──────────────────────────

export interface EvolutionCandidate {
  strategyId: string;
  strategyName: string;
  reason: string;
  performanceScore: number;
}

export function findEvolutionCandidates(): EvolutionCandidate[] {
  const strategies = ensureStrategiesSeeded().filter((s) => !s.deprecated);
  const candidates: EvolutionCandidate[] = [];

  for (const strategy of strategies) {
    const perf = getStrategyPerformance(strategy.id);
    if (!perf) continue;

    const score = perf.averageContribution * perf.successRate;

    // High performers: good candidates for recombination with others
    if (score > 0.3 && perf.totalOutcomes >= 3) {
      candidates.push({
        strategyId: strategy.id,
        strategyName: strategy.name,
        reason: `High performance (contribution: ${perf.averageContribution}, success: ${perf.successRate}, outcomes: ${perf.totalOutcomes})`,
        performanceScore: score,
      });
    }

    // Low performers with unresolved evidence: candidates for decomposition and improvement
    if (score < 0.1 && perf.totalOutcomes >= 2 && strategy.evidenceLevel === "unresolved") {
      candidates.push({
        strategyId: strategy.id,
        strategyName: strategy.name,
        reason: `Low performance, needs improvement (contribution: ${perf.averageContribution}, outcomes: ${perf.totalOutcomes})`,
        performanceScore: score,
      });
    }
  }

  return candidates.sort((a, b) => b.performanceScore - a.performanceScore);
}