Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): gate resume to mflux, drop UI checkpoint-count requirement
[._.]/ Adam Eivy2c343fffbd9c
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 3989b220db..28ef2cf1c5 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -28,7 +28,7 @@- **LoRA dataset workbench gains a "Re-caption all" button — re-run every caption through a new model** — "Caption all" already skips images that already have a caption (it only fills in the uncaptioned ones), so there was no one-click way to re-run a whole dataset through a freshly-picked vision model. A new **Re-caption all** button next to it sends `overwrite: true`, re-captioning every ready image with the model chosen in the caption-model picker (overwriting existing captions, including manual edits — the button's tooltip says so). The single-image "Re-caption" button already overwrote one image; this extends the same intent to the whole set. (`client/src/pages/LoraDatasetDetail.jsx`)-- **Resume a killed LoRA training run from its last checkpoint — and see a failed run's samples** — A training run that got interrupted (an app restart, a manual cancel, a crash partway through) was a total loss: the panel showed only the error, hid the checkpoint picker (it rendered for *completed* runs only), and the only path forward was to start over from step 0 — re-spending all the GPU time the run had already banked. Two changes recover that work. (1) The Train panel now surfaces the **checkpoint picker for any run that saved a checkpoint**, not just completed ones — so you can scrub the failed run's sample previews to judge how far it got, and **"Use this"** still salvages a partial checkpoint into a usable LoRA. (2) A **Resume training** button re-launches a failed/canceled run from its newest checkpoint via the new `POST /api/lora-training/runs/:id/resume` — it re-enqueues a job against the *same* run id (mflux bakes the output path into its checkpoint zip and restores the optimizer + adapter state and step counter from `--resume <zip>`, so the resumed trainer must write back into the original run dir; a fresh dir would strand its artifacts where the watcher can't see them). New checkpoints and samples append to the existing timeline, and finalize registers the LoRA as usual. The torch FLUX.2 trainer resumes via `--resume-from <dir>` the same way. (`server/services/loraTraining/index.js`, `server/services/loraTraining/checkpoints.js`, `server/routes/loraTraining.js`, `client/src/services/apiLoraTraining.js`, `client/src/components/loraTraining/TrainingPanel.jsx`)+- **Resume a killed LoRA training run from its last checkpoint — and see a failed run's samples** — A training run that got interrupted (an app restart, a manual cancel, a crash partway through) was a total loss: the panel showed only the error, hid the checkpoint picker (it rendered for *completed* runs only), and the only path forward was to start over from step 0 — re-spending all the GPU time the run had already banked. Two changes recover that work. (1) The Train panel now surfaces the **checkpoint picker for any run that saved a checkpoint**, not just completed ones — so you can scrub the failed run's sample previews to judge how far it got, and **"Use this"** still salvages a partial checkpoint into a usable LoRA. (2) A **Resume training** button re-launches a failed/canceled run from its newest checkpoint via the new `POST /api/lora-training/runs/:id/resume` — it re-enqueues a job against the *same* run id (mflux bakes the output path into its checkpoint zip and restores the optimizer + adapter state and step counter from `--resume <zip>`, so the resumed trainer must write back into the original run dir; a fresh dir would strand its artifacts where the watcher can't see them). New checkpoints and samples append to the existing timeline, and finalize registers the LoRA as usual. Resume is **mflux-only** for now: mflux's `--resume <zip>` restores the optimizer state and step counter so training picks up mid-run, whereas the torch FLUX.2 trainer's `--resume-from` only warm-starts the adapter and would re-run the full step count on a fresh optimizer — so the server refuses to resume a FLUX.2 run (start a fresh one) until that trainer learns true optimizer-state resume. (`server/services/loraTraining/index.js`, `server/services/loraTraining/checkpoints.js`, `server/routes/loraTraining.js`, `client/src/services/apiLoraTraining.js`, `client/src/components/loraTraining/TrainingPanel.jsx`)- **LoRA training picks the best checkpoint instead of blindly deploying the final step — and you can promote any checkpoint by eye** — A FLUX.2 character LoRA could finish "successfully" yet render garbage: the trainer always handed back the FINAL step's adapter, but late training **divergence** (a recurring FLUX.2 failure — the loss keeps dropping while the sample image collapses to a near-black frame) made that final step the worst one. Loss is no guide here; on the run that motivated this it was *anti-correlated* with quality (the lowest-loss checkpoint was visually broken). Two changes close the gap: (1) a **collapse guard** at finalize analyzes each checkpoint's preview (sharp entropy + per-channel stdev) and, when the final preview is a degenerate near-black/near-uniform collapse, deploys the latest *healthy* checkpoint instead and flags the run for review — calibrated with a wide margin (a real frame measured entropy 5.5–7.1 vs the collapse's 2.8) so it never vetoes a legitimate image; (2) a new **checkpoint picker** under a completed run (`/media/training`) shows every saved checkpoint as a preview thumbnail with its step + loss, and a **"Use this"** button re-extracts that step's adapter and overwrites the deployed LoRA in place — the human override for subtler degradation the guard leaves alone. Per-checkpoint loss is now captured at checkpoint time, and `lib/zipStream` gains `extractZipEntryToBuffer` to crack one adapter out of an mflux checkpoint zip long after the trainer exited. The final debounced checkpoint/sample now flush to the run record before finalize reads it, so the guard always sees the last artifacts. (`server/services/loraTraining/checkpoints.js`, `server/services/loraTraining/index.js`, `server/lib/zipStream.js`, `server/routes/loraTraining.js`, `client/src/components/loraTraining/CheckpointPicker.jsx`)diff --git a/client/src/components/loraTraining/TrainingPanel.jsx b/client/src/components/loraTraining/TrainingPanel.jsxindex eba9989526..a87daac960 100644--- a/client/src/components/loraTraining/TrainingPanel.jsx+++ b/client/src/components/loraTraining/TrainingPanel.jsx@@ -125,9 +125,14 @@ export default function TrainingPanel({ dataset, readiness, triggerSaving, onRunconst setParam = (key, value) => setParams((prev) => ({ ...prev, [key]: value }));// A killed run keeps its checkpoints — surface the picker (view + salvage a- // partial LoRA) and a Resume button whenever a non-completed run saved one.+ // partial LoRA) whenever a non-completed run recorded one, and a Resume button+ // for any failed/canceled mflux run. Gate Resume on status, NOT the recorded+ // checkpoint count: a crash can kill the run before the debounced record+ // persists its last checkpoint, yet the server resumes from disk — let its+ // 409 (NO_RESUMABLE_CHECKPOINT) handle the genuinely-empty case. mflux only;+ // the FLUX.2 torch trainer's resume restarts the optimizer (server refuses it).const lastRunCheckpoints = lastRun?.artifacts?.checkpoints?.length || 0;- const canResume = lastRun && ['failed', 'canceled'].includes(lastRun.status) && lastRunCheckpoints > 0;+ const canResume = lastRun && ['failed', 'canceled'].includes(lastRun.status) && lastRun.runtime === 'mflux';const disabledReason = !readiness?.trainable? `Needs ${readiness?.required ?? 10} captioned images (have ${readiness?.captioned ?? 0})`@@ -213,7 +218,9 @@ export default function TrainingPanel({ dataset, readiness, triggerSaving, onRun{canResume && (<div className="flex items-center justify-between gap-2 rounded-lg border border-port-accent/30 bg-port-accent/5 px-3 py-2"><span className="text-xs text-gray-300">- {lastRunCheckpoints} checkpoint{lastRunCheckpoints === 1 ? '' : 's'} saved — pick up where it stopped.+ {lastRunCheckpoints > 0+ ? `${lastRunCheckpoints} checkpoint${lastRunCheckpoints === 1 ? '' : 's'} saved — pick up where it stopped.`+ : 'Pick up from the last saved checkpoint.'}</span><buttontype="button"diff --git a/server/services/loraTraining/checkpoints.test.js b/server/services/loraTraining/checkpoints.test.jsindex f5959c3799..c1c57caa41 100644--- a/server/services/loraTraining/checkpoints.test.js+++ b/server/services/loraTraining/checkpoints.test.js@@ -130,6 +130,19 @@ describe('loraTraining/checkpoints', () => {expect(resolveLatestCheckpointArtifact({ id: 'no-such-run', runtime: 'mflux' })).toBeNull();});+ it('resolves the latest flux2 checkpoint dir (the one with adapter weights)', async () => {+ const id = 'run-flux2-fixture';+ const ckpts = join(dataRoot, 'training-runs', id, 'checkpoints');+ mkdirSync(join(ckpts, 'step-000100'), { recursive: true });+ mkdirSync(join(ckpts, 'step-000200'), { recursive: true });+ mkdirSync(join(ckpts, 'step-000300'), { recursive: true }); // no weights yet — must be skipped+ await writeFile(join(ckpts, 'step-000100', 'pytorch_lora_weights.safetensors'), Buffer.from('w1'));+ await writeFile(join(ckpts, 'step-000200', 'pytorch_lora_weights.safetensors'), Buffer.from('w2'));+ const latest = resolveLatestCheckpointArtifact({ id, runtime: 'flux2' });+ expect(latest).toMatchObject({ step: 200 });+ expect(latest.path).toContain(join('checkpoints', 'step-000200'));+ });+it('lists samples as step+url sorted by step (seeds the live gallery)', () => {const samples = listRunSamples(buildRun());expect(samples.map((s) => s.step)).toEqual([250, 500]);diff --git a/server/services/loraTraining/index.js b/server/services/loraTraining/index.jsindex bf1d0061e8..c24e09b045 100644--- a/server/services/loraTraining/index.js+++ b/server/services/loraTraining/index.js@@ -216,6 +216,19 @@ export async function resumeTrainingRun(runId) {status: 409, code: 'RUN_NOT_RESUMABLE',});}+ // mflux only: its `--resume <zip>` restores optimizer state AND the step+ // counter, so training picks up mid-run and finishes at the original total.+ // The torch FLUX.2 trainer's `--resume-from` only warm-starts the adapter+ // weights and then runs a FRESH optimizer loop for the full `steps` again —+ // that would over-train and re-collide checkpoint step numbers, not resume.+ // The arg-builder wiring (buildFlux2TrainArgs `resumeFrom`) is in place for+ // when the trainer learns true optimizer-state resume; until then, refuse it.+ if (run.runtime === TRAINING_RUNTIMES.FLUX2) {+ throw new ServerError(+ 'Resume is only supported for mflux (MLX) runs right now — the FLUX.2 torch trainer restarts its optimizer from step 0, which would over-train. Start a fresh run.',+ { status: 409, code: 'RESUME_UNSUPPORTED_RUNTIME' },+ );+ }const checkpoint = resolveLatestCheckpointArtifact(run);if (!checkpoint) {throw new ServerError(