Skip to content

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

address review (codex): token-boundary trigger match + live client readiness

Adam Eivy587971d88211
diff --git a/client/src/pages/LoraDatasetDetail.jsx b/client/src/pages/LoraDatasetDetail.jsx
index a218e5dbdb..650a2e18c7 100644
--- a/client/src/pages/LoraDatasetDetail.jsx
+++ b/client/src/pages/LoraDatasetDetail.jsx
@@ -28,6 +28,35 @@ import {
} from '../services/api';
const TRIGGER_RE = /^[a-z0-9_]{2,64}$/;
+// Mirror of server/lib/loraDataset.js MIN_TRAINING_IMAGES + the token-boundary
+// caption match. Kept page-local (UX-advisory only — the server re-validates
+// authoritatively via validateDatasetReady at train time) so the readiness
+// summary + Train gate update the instant a caption is edited or an image
+// deleted, instead of waiting on a server round-trip. Port logic changes here
+// when the server helper changes.
+const MIN_TRAINING_IMAGES = 10;
+const captionHasTriggerWord = (caption, triggerWord) => {
+ const word = (triggerWord || '').trim();
+ const text = (caption || '').trim();
+ if (!text) return false;
+ if (!word) return true;
+ const escaped = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ return new RegExp(`(?:^|[^a-z0-9_])${escaped}(?:[^a-z0-9_]|$)`, 'i').test(text);
+};
+const deriveReadiness = (images, triggerWord) => {
+ const list = Array.isArray(images) ? images : [];
+ const word = (triggerWord || '').trim();
+ const ready = list.filter((img) => img.status === 'ready');
+ const captioned = ready.filter((img) => captionHasTriggerWord(img.caption, word));
+ return {
+ total: list.length,
+ ready: ready.length,
+ captioned: captioned.length,
+ rendering: list.filter((img) => img.status === 'rendering').length,
+ required: MIN_TRAINING_IMAGES,
+ trainable: !!word && captioned.length >= MIN_TRAINING_IMAGES,
+ };
+};
function SliceDialog({ dataset, onClose, onSliced }) {
const [cols, setCols] = useState(3);
@@ -124,8 +153,16 @@ export default function LoraDatasetDetail() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dataset?.character?.universeId, dataset?.character?.entryId]);
+ // Readiness is derived from the live local images (not the server's snapshot
+ // on `dataset.readiness`) so manual caption edits / deletes reflect in the
+ // counts + Train gate immediately, without a refetch per keystroke.
+ const readiness = useMemo(
+ () => deriveReadiness(dataset?.images, dataset?.triggerWord),
+ [dataset?.images, dataset?.triggerWord],
+ );
+
// Poll while any image renders — the server heals stuck images on read.
- const renderingCount = dataset?.readiness?.rendering || 0;
+ const renderingCount = readiness.rendering;
useEffect(() => {
if (!renderingCount) return undefined;
const timer = setInterval(refresh, 5000);
@@ -209,8 +246,6 @@ export default function LoraDatasetDetail() {
return <div className="text-gray-400 flex items-center gap-2"><Loader2 className="w-4 h-4 animate-spin" /> Loading dataset…</div>;
}
- const readiness = dataset.readiness || {};
-
return (
<div className="space-y-4">
<div className="flex items-start justify-between gap-3 flex-wrap">
diff --git a/server/lib/loraDataset.js b/server/lib/loraDataset.js
index 84e62af63d..1c5d23618c 100644
--- a/server/lib/loraDataset.js
+++ b/server/lib/loraDataset.js
@@ -206,19 +206,34 @@ export function buildVariationMatrix({
return tuples;
}
+/**
+ * True when `caption` contains `triggerWord` as a whole token (bounded by a
+ * non-[a-z0-9_] char or a string edge), not merely as a substring. A bare
+ * `.includes()` would count a short trigger like `ai`/`jo` inside unrelated
+ * words (`captain`, `train`), so readiness + the training manifest could
+ * accept captions that don't actually carry the LoRA binding token. The
+ * single source of truth for "does this caption bind the trigger" — used by
+ * both computeDatasetReadiness and the server-side validateDatasetReady so
+ * the gate and the manifest can't drift.
+ */
+export function captionHasTriggerWord(caption, triggerWord) {
+ const word = trim(triggerWord);
+ const text = trim(caption);
+ if (!text) return false;
+ if (!word) return true; // no trigger configured yet — any caption counts
+ return new RegExp(`(?:^|[^a-z0-9_])${escapeRe(word)}(?:[^a-z0-9_]|$)`, 'i').test(text);
+}
+
/**
* Compute dataset readiness for training. Pure — callers pass the sanitized
* record. `trainable` requires a trigger word plus MIN_TRAINING_IMAGES
- * images that are status 'ready' AND carry a caption containing it.
+ * images that are status 'ready' AND carry a caption with the trigger token.
*/
export function computeDatasetReadiness(dataset) {
const images = Array.isArray(dataset?.images) ? dataset.images : [];
const triggerWord = trim(dataset?.triggerWord);
const readyImages = images.filter((img) => img.status === 'ready');
- const captioned = readyImages.filter((img) => {
- const caption = trim(img.caption);
- return caption && (!triggerWord || caption.toLowerCase().includes(triggerWord.toLowerCase()));
- });
+ const captioned = readyImages.filter((img) => captionHasTriggerWord(img.caption, triggerWord));
return {
total: images.length,
ready: readyImages.length,
diff --git a/server/lib/loraDataset.test.js b/server/lib/loraDataset.test.js
index bd4bfe23c0..a7ca925e68 100644
--- a/server/lib/loraDataset.test.js
+++ b/server/lib/loraDataset.test.js
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest';
import {
buildVariationMatrix,
+ captionHasTriggerWord,
computeDatasetReadiness,
deriveTriggerWord,
isValidTriggerWord,
@@ -196,4 +197,30 @@ describe('computeDatasetReadiness', () => {
});
expect(out.captioned).toBe(1);
});
+
+ it('counts the trigger as a whole token, not a substring', () => {
+ // A short trigger (`ai`) must NOT count captions where it only appears
+ // inside other words (`captain`, `train`) — those don't bind the token.
+ const out = computeDatasetReadiness({
+ triggerWord: 'ai',
+ images: [
+ img({ id: 'a', caption: 'a captain on a train' }), // substring only → not counted
+ img({ id: 'b', caption: 'ai, a portrait' }), // real token → counted
+ ],
+ });
+ expect(out.captioned).toBe(1);
+ });
+});
+
+describe('captionHasTriggerWord', () => {
+ it('matches whole tokens, edges, and underscores; rejects substrings', () => {
+ expect(captionHasTriggerWord('ai, portrait', 'ai')).toBe(true);
+ expect(captionHasTriggerWord('a captain', 'ai')).toBe(false);
+ expect(captionHasTriggerWord('the train station', 'ai')).toBe(false);
+ expect(captionHasTriggerWord('kessa_v2 stands tall', 'kessa_v2')).toBe(true);
+ expect(captionHasTriggerWord('ends with token: ai', 'ai')).toBe(true);
+ expect(captionHasTriggerWord('', 'ai')).toBe(false);
+ // No trigger configured → any non-empty caption counts.
+ expect(captionHasTriggerWord('anything', '')).toBe(true);
+ });
});
diff --git a/server/services/loraTraining/dataset.js b/server/services/loraTraining/dataset.js
index e00f9571fe..7aa203f7f8 100644
--- a/server/services/loraTraining/dataset.js
+++ b/server/services/loraTraining/dataset.js
@@ -6,7 +6,7 @@
import { access } from 'fs/promises';
import { ServerError } from '../../lib/errorHandler.js';
-import { computeDatasetReadiness } from '../../lib/loraDataset.js';
+import { captionHasTriggerWord, computeDatasetReadiness } from '../../lib/loraDataset.js';
import { getDataset, datasetImagePath, datasetImagesDir } from '../loraDatasets.js';
/**
@@ -24,11 +24,11 @@ export async function validateDatasetReady(datasetId) {
{ status: 409, code: 'DATASET_NOT_READY' },
);
}
- const trainImages = dataset.images.filter((img) => {
- if (img.status !== 'ready') return false;
- const caption = (img.caption || '').trim();
- return caption && caption.toLowerCase().includes(dataset.triggerWord.toLowerCase());
- });
+ // Same token-boundary predicate as the readiness gate — share the helper so
+ // the manifest can never include a caption the gate counted differently.
+ const trainImages = dataset.images.filter(
+ (img) => img.status === 'ready' && captionHasTriggerWord(img.caption, dataset.triggerWord),
+ );
// Every file must actually exist on disk — a missing file at trainer
// start would fail minutes later with an opaque python traceback.
for (const img of trainImages) {