-
Notifications
You must be signed in to change notification settings - Fork 6
feat(learning): add lesson effectiveness telemetry #3803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
djm204
merged 8 commits into
main
from
resolve/issue-1760-feat-learning-add-lesson-effectiveness-telemetry
Aug 9, 2026
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
00c83a3
feat(learning): add lesson effectiveness telemetry
djm204 d7e55e2
fix(learning): harden effectiveness attribution
djm204 0104841
fix(learning): validate lesson injection timing
djm204 01ddb4b
fix(learning): enforce temporal attribution
djm204 2ef7b99
fix(learning): preserve historical lesson scope
djm204 06399b0
Merge remote-tracking branch 'origin/main' into resolve/issue-1760-fe…
djm204 24b53fd
fix(learning): attribute events to injection scope
djm204 35c2190
docs(tasks): record lesson attribution safeguards
djm204 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
320 changes: 320 additions & 0 deletions
320
packages/franken-critique/src/memory/lesson-effectiveness.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,320 @@ | ||
| import type { | ||
| CritiqueLesson, | ||
| LessonEffectivenessEvent, | ||
| LessonEffectivenessOutcome, | ||
| LessonEffectivenessReport, | ||
| LessonEffectivenessTrend, | ||
| LessonInjectionContext, | ||
| LessonLifecycleRecommendation, | ||
| LessonScopeKind, | ||
| LessonScopeMetadata, | ||
| } from '../types/contracts.js'; | ||
| import type { TaskId } from '../types/common.js'; | ||
| import { isLessonApplicable } from './lesson-recorder.js'; | ||
|
|
||
| const LESSON_SCOPE_KINDS: readonly LessonScopeKind[] = [ | ||
| 'global', | ||
| 'repo', | ||
| 'role', | ||
| 'profile', | ||
| 'task', | ||
| ]; | ||
|
|
||
| export interface LessonEffectivenessRecordInput { | ||
| readonly lessonId: string; | ||
| readonly lessonScope: LessonScopeMetadata; | ||
| readonly injectionContext: Omit<LessonInjectionContext, 'now'>; | ||
| readonly injectedAt: string; | ||
| readonly observedAt: string; | ||
| readonly taskSucceeded: boolean; | ||
| readonly blockersBefore: number; | ||
| readonly blockersAfter: number; | ||
| readonly reviewFindingCount: number; | ||
| readonly userCorrection: boolean; | ||
| } | ||
|
|
||
| interface MutableLessonEffectivenessTrend { | ||
| lessonId: string; | ||
| lessonScope: LessonScopeKind; | ||
| observations: number; | ||
| positive: number; | ||
| neutral: number; | ||
| negative: number; | ||
| correctionSignals: number; | ||
| blockerReductions: number; | ||
| blockerRegressions: number; | ||
| } | ||
|
|
||
| /** | ||
| * Records transcript-free outcome signals for injected lessons and aggregates | ||
| * them into deterministic lifecycle trends. Raw prompts, finding text, and | ||
| * correction text are deliberately absent from the input and event schemas. | ||
| */ | ||
| export class LessonEffectivenessTelemetry { | ||
| private readonly events: LessonEffectivenessEvent[] = []; | ||
| private readonly now: () => string; | ||
|
|
||
| constructor(now: () => Date | string = (): Date => new Date()) { | ||
| this.now = (): string => normalizeTimestamp(now(), 'report timestamp'); | ||
| } | ||
|
|
||
| record(input: LessonEffectivenessRecordInput): LessonEffectivenessEvent { | ||
| const lessonId = requireNonEmptyString(input.lessonId, 'lessonId'); | ||
| const lessonScope = normalizeLessonScope(input.lessonScope?.scope); | ||
|
djm204 marked this conversation as resolved.
Outdated
|
||
| const injectedAt = normalizeTimestamp(input.injectedAt, 'injectedAt'); | ||
| const observedAt = normalizeTimestamp(input.observedAt, 'observedAt'); | ||
|
djm204 marked this conversation as resolved.
|
||
| if (Date.parse(observedAt) < Date.parse(injectedAt)) { | ||
| throw new RangeError('observedAt must not precede injectedAt.'); | ||
| } | ||
| const injectionContext = normalizeInjectionContext(input.injectionContext); | ||
| requireApplicableScope( | ||
| lessonId, | ||
| input.lessonScope, | ||
| injectionContext, | ||
| injectedAt, | ||
|
djm204 marked this conversation as resolved.
Outdated
|
||
| ); | ||
| const blockersBefore = requireCount(input.blockersBefore, 'blockersBefore'); | ||
| const blockersAfter = requireCount(input.blockersAfter, 'blockersAfter'); | ||
| const reviewFindingCount = requireCount( | ||
| input.reviewFindingCount, | ||
| 'reviewFindingCount', | ||
| ); | ||
| if (typeof input.taskSucceeded !== 'boolean') { | ||
| throw new TypeError('taskSucceeded must be a boolean.'); | ||
| } | ||
| if (typeof input.userCorrection !== 'boolean') { | ||
| throw new TypeError('userCorrection must be a boolean.'); | ||
| } | ||
|
|
||
| const blockerDelta = blockersAfter - blockersBefore; | ||
| const outcome = attributeOutcome({ | ||
| taskSucceeded: input.taskSucceeded, | ||
| blockerDelta, | ||
| reviewFindingCount, | ||
| userCorrection: input.userCorrection, | ||
| }); | ||
| const event: LessonEffectivenessEvent = { | ||
| schemaVersion: 'lesson-effectiveness-event-v1', | ||
| lessonId, | ||
| lessonScope, | ||
| injectionContext, | ||
| injectedAt, | ||
| observedAt, | ||
| outcome, | ||
| signals: { | ||
| taskSucceeded: input.taskSucceeded, | ||
| blockerDelta, | ||
| blockerReduced: blockerDelta < 0, | ||
| reviewFindingCount, | ||
| userCorrection: input.userCorrection, | ||
| }, | ||
| }; | ||
| this.events.push({ | ||
| ...event, | ||
| injectionContext: { ...event.injectionContext }, | ||
| signals: { ...event.signals }, | ||
| }); | ||
| return event; | ||
| } | ||
|
|
||
| report(): LessonEffectivenessReport { | ||
| const trends = new Map<string, MutableLessonEffectivenessTrend>(); | ||
| for (const event of this.events) { | ||
| const key = `${event.lessonScope}\u0000${event.lessonId}`; | ||
| const trend = trends.get(key) ?? { | ||
| lessonId: event.lessonId, | ||
| lessonScope: event.lessonScope, | ||
| observations: 0, | ||
| positive: 0, | ||
| neutral: 0, | ||
| negative: 0, | ||
| correctionSignals: 0, | ||
| blockerReductions: 0, | ||
| blockerRegressions: 0, | ||
| }; | ||
| trend.observations += 1; | ||
| trend[event.outcome] += 1; | ||
| if (event.signals.userCorrection) trend.correctionSignals += 1; | ||
| if (event.signals.blockerDelta < 0) trend.blockerReductions += 1; | ||
| if (event.signals.blockerDelta > 0) trend.blockerRegressions += 1; | ||
| trends.set(key, trend); | ||
| } | ||
|
|
||
| const lessons = [...trends.values()] | ||
| .sort( | ||
| (left, right) => | ||
| left.lessonId.localeCompare(right.lessonId) || | ||
| left.lessonScope.localeCompare(right.lessonScope), | ||
| ) | ||
| .map(toPublicTrend); | ||
|
|
||
| return { | ||
| schemaVersion: 'lesson-effectiveness-report-v1', | ||
| generatedAt: this.now(), | ||
| totalEvents: this.events.length, | ||
| lessons, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function attributeOutcome(signals: { | ||
| readonly taskSucceeded: boolean; | ||
| readonly blockerDelta: number; | ||
| readonly reviewFindingCount: number; | ||
| readonly userCorrection: boolean; | ||
| }): LessonEffectivenessOutcome { | ||
| if ( | ||
| signals.userCorrection || | ||
| !signals.taskSucceeded || | ||
| signals.blockerDelta > 0 | ||
| ) { | ||
| return 'negative'; | ||
| } | ||
| if ( | ||
| signals.taskSucceeded && | ||
| signals.blockerDelta < 0 && | ||
| signals.reviewFindingCount === 0 | ||
| ) { | ||
| return 'positive'; | ||
| } | ||
| return 'neutral'; | ||
| } | ||
|
|
||
| function toPublicTrend( | ||
| trend: MutableLessonEffectivenessTrend, | ||
| ): LessonEffectivenessTrend { | ||
| const effectivenessScore = roundScore( | ||
| (trend.positive - trend.negative) / trend.observations, | ||
| ); | ||
| return { | ||
| lessonId: trend.lessonId, | ||
| lessonScope: trend.lessonScope, | ||
| observations: trend.observations, | ||
| positive: trend.positive, | ||
| neutral: trend.neutral, | ||
| negative: trend.negative, | ||
| effectivenessScore, | ||
| correctionSignals: trend.correctionSignals, | ||
| blockerReductions: trend.blockerReductions, | ||
| blockerRegressions: trend.blockerRegressions, | ||
| lifecycleRecommendation: recommendLifecycle(trend), | ||
| }; | ||
| } | ||
|
|
||
| function recommendLifecycle( | ||
| trend: MutableLessonEffectivenessTrend, | ||
| ): LessonLifecycleRecommendation { | ||
| if (trend.negative * 2 > trend.observations) return 'retire'; | ||
| if (trend.positive * 2 > trend.observations) return 'promote'; | ||
| return 'monitor'; | ||
| } | ||
|
|
||
| function requireApplicableScope( | ||
| lessonId: string, | ||
| lessonScope: LessonScopeMetadata, | ||
| injectionContext: Omit<LessonInjectionContext, 'now'>, | ||
| injectedAt: string, | ||
| ): void { | ||
| const injectionTime = Date.parse(injectedAt); | ||
| const effectiveAuditTrail = lessonScope.auditTrail.filter((entry) => { | ||
| const changedAt = normalizeTimestamp( | ||
| entry.changedAt, | ||
| 'lessonScope.auditTrail.changedAt', | ||
| ); | ||
| return Date.parse(changedAt) <= injectionTime; | ||
| }); | ||
| const attributionLesson: CritiqueLesson = { | ||
| evaluatorName: 'lesson-effectiveness-attribution', | ||
| failureDescription: lessonId, | ||
| correctionApplied: 'Effectiveness observation', | ||
| taskId: (injectionContext.taskId ?? | ||
| lessonScope.provenance.taskId ?? | ||
| 'lesson-effectiveness-attribution') as TaskId, | ||
| timestamp: injectedAt, | ||
| lifecycleStatus: 'active', | ||
| lessonScope: { | ||
| ...lessonScope, | ||
| auditTrail: effectiveAuditTrail, | ||
|
djm204 marked this conversation as resolved.
|
||
| }, | ||
| }; | ||
| if ( | ||
| !isLessonApplicable(attributionLesson, { | ||
| ...injectionContext, | ||
| now: injectedAt, | ||
| }) | ||
|
djm204 marked this conversation as resolved.
|
||
| ) { | ||
| throw new RangeError( | ||
| 'Lesson effectiveness injection context is outside the reviewed lesson scope.', | ||
| ); | ||
| } | ||
|
djm204 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function normalizeInjectionContext( | ||
| context: Omit<LessonInjectionContext, 'now'>, | ||
| ): Omit<LessonInjectionContext, 'now'> { | ||
| if (!context || typeof context !== 'object') { | ||
| throw new TypeError('injectionContext must be an object.'); | ||
| } | ||
| return { | ||
| ...(context.repo !== undefined | ||
| ? { repo: requireNonEmptyString(context.repo, 'injectionContext.repo') } | ||
| : {}), | ||
| ...(context.role !== undefined | ||
| ? { role: requireNonEmptyString(context.role, 'injectionContext.role') } | ||
| : {}), | ||
| ...(context.profile !== undefined | ||
| ? { | ||
| profile: requireNonEmptyString( | ||
| context.profile, | ||
| 'injectionContext.profile', | ||
| ), | ||
| } | ||
| : {}), | ||
| ...(context.taskId !== undefined | ||
| ? { | ||
| taskId: requireTaskId(context.taskId), | ||
| } | ||
| : {}), | ||
| }; | ||
| } | ||
|
|
||
| function normalizeLessonScope(scope: LessonScopeKind): LessonScopeKind { | ||
| if (!LESSON_SCOPE_KINDS.includes(scope)) { | ||
| throw new RangeError(`Unsupported lesson scope: ${String(scope)}.`); | ||
| } | ||
| return scope; | ||
| } | ||
|
|
||
| function requireNonEmptyString(value: string, label: string): string { | ||
| if (typeof value !== 'string' || !value.trim()) { | ||
| throw new TypeError(`${label} must be a non-empty string.`); | ||
| } | ||
| return value.trim(); | ||
| } | ||
|
|
||
| function requireTaskId( | ||
| value: NonNullable<LessonInjectionContext['taskId']>, | ||
| ): NonNullable<LessonInjectionContext['taskId']> { | ||
| return requireNonEmptyString(value, 'injectionContext.taskId') as NonNullable< | ||
| LessonInjectionContext['taskId'] | ||
| >; | ||
| } | ||
|
|
||
| function requireCount(value: number, label: string): number { | ||
| if (!Number.isSafeInteger(value) || value < 0) { | ||
| throw new RangeError(`${label} must be a non-negative safe integer.`); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| function normalizeTimestamp(value: Date | string, label: string): string { | ||
| const date = value instanceof Date ? value : new Date(value); | ||
| if (!Number.isFinite(date.getTime())) { | ||
| throw new RangeError(`${label} must be a valid timestamp.`); | ||
| } | ||
| return date.toISOString(); | ||
| } | ||
|
|
||
| function roundScore(value: number): number { | ||
| return Math.round(value * 1000) / 1000; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.