From fad057a108f2bb40b652032a6168382bc0ed0a61 Mon Sep 17 00:00:00 2001 From: monkey-w1n5t0n Date: Tue, 7 Apr 2026 20:10:50 +0200 Subject: [PATCH] feat: add undo stack with weight/noise restoration, undo button, and randomize/clear examples validation (f10) - Implement undo stack (max 20 entries) in ML store with pushUndoSnapshot before thumbsDown/randomise - Add undo() method that restores weights within 1e-4 tolerance and noise level - Add reactive undoDepthSignal for UI button state tracking - Add undo button to RLButtons component between thumbs-down and thumbs-up - Update debug probe with undo method - Add 17 e2e tests covering VAL-ML-003, VAL-ML-013, VAL-ML-014, VAL-CROSS-007 - Update architecture docs with undo stack and RLButtons details Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- .factory/library/architecture.md | 11 + src/components/rl/RLButtons.tsx | 25 ++ src/components/rl/rl-buttons.css | 20 + src/probe/debug-probe.ts | 3 + src/stores/ml-store.ts | 42 ++ tests/solidjs/randomize-clear-undo.spec.ts | 441 +++++++++++++++++++++ 6 files changed, 542 insertions(+) create mode 100644 tests/solidjs/randomize-clear-undo.spec.ts diff --git a/.factory/library/architecture.md b/.factory/library/architecture.md index b1c7691..00d322f 100644 --- a/.factory/library/architecture.md +++ b/.factory/library/architecture.md @@ -59,6 +59,7 @@ Unified event system replacing EventBus + CustomEvents. Topics with `equals: fal - `addExample()` — adds current I/O as training example, updates reactive count - `clearAll()` — clears examples, loss history, resets noise to default - `getLossHistory()` — returns copy of loss history array +- Undo stack: ring buffer (max 20 entries) of weight snapshots with noise level. `pushUndoSnapshot()` called before `thumbsDown()` and `randomise()`. `undo()` pops snapshot, restores weights and noise. `undoDepthSignal` — reactive depth for UI button state. - `dispose()` destroys both IML instances (called in App.tsx onCleanup) - Bus topics created: `ml.outputs`, `ml.outputCount`, `mode.output` - Debug probe (`probe/debug-probe.ts`) now wraps MLStore instead of raw WasmIML @@ -117,6 +118,16 @@ Unified event system replacing EventBus + CustomEvents. Topics with `equals: fal - Format: "N examples · loss X.XXXXX · noise X.XXX" or "N examples · untrained · noise X.XXX" - Reactively watches exampleCountSignal, lastLossSignal, noiseLevel signals +### RLButtons Component (`components/rl/RLButtons.tsx`) — IMPLEMENTED +- Three floating circular buttons: thumbs-down (−, key 1), undo (↶, key Z), thumbs-up (+, key 2) +- Positioned bottom-center above status line +- Thumbs-down: push undo snapshot, add noise to weights +- Thumbs-up: add example + async training + decay noise +- Undo: pop undo stack, restore weights + noise level +- Undo button shows muted state when stack empty, cyan accent (`has-undo` class) when entries exist +- Glass morphism styling with per-button accent colors +- Exposed as `#btn-thumbsdown`, `#btn-undo`, `#btn-thumbsup` for e2e tests + ### Dual IML System - `imlJoy`: 2 inputs (joystick), dynamic outputs — warm-started on resize - `imlHand`: 14 inputs (hand tracking), dynamic outputs — fresh on resize diff --git a/src/components/rl/RLButtons.tsx b/src/components/rl/RLButtons.tsx index ccae980..3736041 100644 --- a/src/components/rl/RLButtons.tsx +++ b/src/components/rl/RLButtons.tsx @@ -4,6 +4,7 @@ * Matches the old app's rl-buttons div: * - Thumbs-down (−): explore more (adds noise to weights) * - Thumbs-up (+): keep this (adds example + async training + decays noise) + * - Undo (↶): restore previous weight state from undo stack * * Positioned at bottom-center of viewport, above the status line. * Glass morphism circular buttons with keyboard shortcut hints. @@ -20,6 +21,15 @@ export interface RLButtonsProps { export default function RLButtons(props: RLButtonsProps) { let thumbsUpBtn: HTMLButtonElement | undefined; let thumbsDownBtn: HTMLButtonElement | undefined; + let undoBtn: HTMLButtonElement | undefined; + + // Track undo depth for has-undo class (reactive signal) + createEffect(() => { + const depth = props.mlStore.undoDepthSignal(); + if (undoBtn) { + undoBtn.classList.toggle('has-undo', depth > 0); + } + }); function onThumbsUp(): void { flashButton(thumbsUpBtn); @@ -33,6 +43,11 @@ export default function RLButtons(props: RLButtonsProps) { props.mlStore.thumbsDown(); } + function onUndo(): void { + flashButton(undoBtn); + props.mlStore.undo(); + } + function flashButton(btn: HTMLButtonElement | undefined): void { if (!btn) return; btn.classList.add('flash'); @@ -51,6 +66,16 @@ export default function RLButtons(props: RLButtonsProps) { − 1 +