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
Fork and extend a finished code animation
Claude753ecfbf889a
diff --git a/client/src/pages/CodeAnimation.jsx b/client/src/pages/CodeAnimation.jsxindex b218767092..786534291f 100644--- a/client/src/pages/CodeAnimation.jsx+++ b/client/src/pages/CodeAnimation.jsx@@ -1,5 +1,5 @@import { useCallback, useEffect, useMemo, useRef, useState } from 'react';-import { Copy, FileCode2, Film, Globe, Images, ImagePlus, LoaderCircle, Music2, PenLine, Sparkles, Wand2, X } from 'lucide-react';+import { Copy, FileCode2, GitFork, Film, Globe, Images, ImagePlus, LoaderCircle, Music2, PenLine, Sparkles, Wand2, X } from 'lucide-react';import { useLocation, useNavigate, useParams, useSearchParams } from 'react-router';import PageHeader from '../components/PageHeader';import RouteTabsHeader from '../components/ui/RouteTabsHeader';@@ -50,6 +50,8 @@ const DEFAULT_DRAFT = {format: { durationSeconds: 20, aspectRatio: '16:9', resolution: '1080p', fps: 30 },renderer: 'auto',interactive: false,+ // The finished animation this draft forks and extends: { jobId, title, durationSeconds }.+ base: null,};// Brief fields where a blank from the brief writer means "nothing to add"@@ -58,6 +60,10 @@ const KEEP_ON_BLANK = new Set(['styleNotes', 'cast']);const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);+const readBase = (value) => (isRecord(value) && typeof value.jobId === 'string' && value.jobId+ ? { jobId: value.jobId, title: typeof value.title === 'string' ? value.title : '', durationSeconds: Number.isFinite(value.durationSeconds) ? value.durationSeconds : null }+ : null);+const loadDraft = () => {const stored = safeReadJsonStorage(DRAFT_KEY, null);if (!isRecord(stored)) return DEFAULT_DRAFT;@@ -118,6 +124,7 @@ const loadDraft = () => {},renderer: stringField('renderer'),interactive: typeof stored.interactive === 'boolean' ? stored.interactive : DEFAULT_DRAFT.interactive,+ base: readBase(stored.base),};};@@ -164,6 +171,7 @@ function toBrief(draft) {notes: draft.audio.notes,}: null,+ ...(draft.base ? { baseJobId: draft.base.jobId } : {}),...moodBoardSelection(draft),};}@@ -232,6 +240,7 @@ function draftFromJob(job) {format: { ...DEFAULT_DRAFT.format, ...(input.format || {}) },renderer: input.renderer || 'auto',interactive: input.interactive === true,+ base: readBase(job.base),};}@@ -509,6 +518,18 @@ function FastCodeAnimation() {navigate(`/code-animation/${encodeURIComponent(started.id)}`);};+ // Fork a finished animation: keep its brief, extend its code. The new run is+ // a separate job, so leave the job route; the draft carries everything.+ const handleFork = () => {+ if (!job || job.status !== 'completed') return;+ const forked = draftFromJob(job);+ // A soundtrack generated at the old length would not fit the longer cut.+ const audio = forked.audio?.trackId && forked.audio.trackId === job.soundtrack?.trackId ? null : forked.audio;+ setDraft({ ...forked, audio, base: { jobId: job.id, title: job.title || '', durationSeconds: job.frame?.durationSeconds ?? null } });+ navigate('/code-animation');+ toast.success('Forked. Set the new duration and write what happens next, then Generate animation.');+ };+const handlePreviewPasted = () => {if (!pastedHtml.trim()) return;setPreview({ html: pastedHtml, audioUrl: built?.audioUrl ?? draft.audio?.url ?? null, frame: built?.frame ?? null });@@ -590,6 +611,17 @@ function FastCodeAnimation() {Write brief</button></div>+ {draft.base && (+ <div role="status" className="flex flex-wrap items-center gap-2 rounded-lg border border-port-accent/40 bg-port-accent/10 p-2 text-xs text-gray-200">+ <GitFork className="h-4 w-4 shrink-0 text-port-accent" aria-hidden="true" />+ <span className="min-w-0 flex-1">+ Extending <span className="font-semibold">{draft.base.title || 'an animation'}</span>+ {draft.base.durationSeconds ? ` (${draft.base.durationSeconds}s)` : ''}. Its code is sent with the prompt; set the new duration in Format.+ {draft.base.durationSeconds && draft.format.durationSeconds <= draft.base.durationSeconds ? ' The duration is not longer than the original yet.' : ''}+ </span>+ <button type="button" onClick={() => update({ base: null })} className="text-gray-400 hover:text-white">Stop extending</button>+ </div>+ )}<ProviderModelSelectorproviders={briefProviders}selectedProviderId={briefProviderId}@@ -794,6 +826,11 @@ function FastCodeAnimation() {title={draft.title || draft.concept.slice(0, 40)}/>)}+ {preview?.jobId && job?.id === preview.jobId && job.status === 'completed' && (+ <button type="button" onClick={handleFork} className={buttonSecondary}>+ <GitFork className="h-4 w-4" /> Fork & extend+ </button>+ )}{preview?.jobId && job?.id === preview.jobId && job.status === 'completed' && (<CodeAnimationSoundtrackjobId={job.id}diff --git a/client/src/pages/CodeAnimation.test.jsx b/client/src/pages/CodeAnimation.test.jsxindex 55ddc0c916..aea18b851e 100644--- a/client/src/pages/CodeAnimation.test.jsx+++ b/client/src/pages/CodeAnimation.test.jsx@@ -461,6 +461,28 @@ describe('Code Animation page', () => {expect(screen.getByLabelText(/^title/i)).toHaveValue('Edited after submit');});+ it('forks a finished animation into a new, longer one that sends the original as its base', async () => {+ const user = userEvent.setup();+ getCodeAnimationJob.mockResolvedValue({ ...completedJob, audioUrl: null, input: { ...completedJob.input, format: { durationSeconds: 20 } } });+ startCodeAnimationGeneration.mockResolvedValueOnce({ id: 'job-2', status: 'running', prompt: 'Extended prompt', input: {} });+ await renderPage('/code-animation/job-1', ['/code-animation/job-2']);+ await user.click(await screen.findByRole('button', { name: /fork & extend/i }));+ expect(await screen.findByText(/Extending/)).toHaveTextContent('Extending Example animation (20s)');+ expect(screen.getByText(/not longer than the original yet/)).toBeInTheDocument();+ expect(screen.getByLabelText(/what happens/i)).toHaveValue('A lantern rises');+ const duration = screen.getByLabelText(/duration/i);+ await user.clear(duration);+ await user.type(duration, '120');+ expect(screen.queryByText(/not longer than the original yet/)).not.toBeInTheDocument();+ await user.click(screen.getByRole('button', { name: /generate animation/i }));+ await waitFor(() => expect(startCodeAnimationGeneration).toHaveBeenCalledOnce());+ expect(startCodeAnimationGeneration.mock.calls[0][0]).toMatchObject({ baseJobId: 'job-1', format: { durationSeconds: 120 } });++ // Stop extending sends a fresh animation again.+ await user.click(screen.getByRole('button', { name: 'Stop extending' }));+ expect(screen.queryByText(/Extending/)).not.toBeInTheDocument();+ });+it('picks a film style, shows its moves, and sends the id with the brief and the prompt request', async () => {const user = userEvent.setup();await renderPage();diff --git a/docs/API.md b/docs/API.mdindex 36beb4768a..917877397f 100644--- a/docs/API.md+++ b/docs/API.md@@ -828,7 +828,7 @@ Every mounted API prefix (see `server/index.js` for the authoritative list). Dom| `/api/sprites` | Sprite catalog / export; `POST /import` is host control (it names a host source tree to copy from) || `/api/threejs-models` | Procedural Three.js models || `/api/film-styles` | Read-only film style grammar catalog: picker list (`GET /`), full grammar record (`GET /:id`) and rendered prompt-section preview (`GET /:id/prompt?parts=motion,camera`) |-| `/api/code-animation` | Code Animation: LLM-written briefs, prompt building, persistent jobs gallery with poster thumbnails (`GET`/`POST /jobs/:id/poster`; captured in the jailed preview browser), generated HTML retrieval, frame-exact MP4 export (`POST /:id/export` → HTML-composition media job), a generated soundtrack for a finished animation (`POST /:id/soundtrack` → one local music-engine render at the film length onto a Music-library track that becomes the job audio), the vendored three.js modules the `three` renderer's preview inlines (`GET /vendor/three`; the export stages the same files, hashed, beside the page), portable source download (`GET /:id/package`), and data-only package validation (`POST /packages/validate`; [contract](CODE_ANIMATION_PACKAGES.md)) |+| `/api/code-animation` | Code Animation: LLM-written briefs, prompt building (a `baseJobId` forks a finished animation: its HTML rides the prompt as the cut to extend, refused whole above `baseHtmlMax`), persistent jobs gallery with poster thumbnails (`GET`/`POST /jobs/:id/poster`; captured in the jailed preview browser), generated HTML retrieval, frame-exact MP4 export (`POST /:id/export` → HTML-composition media job), a generated soundtrack for a finished animation (`POST /:id/soundtrack` → one local music-engine render at the film length onto a Music-library track that becomes the job audio), the vendored three.js modules the `three` renderer's preview inlines (`GET /vendor/three`; the export stages the same files, hashed, beside the page), portable source download (`GET /:id/package`), and data-only package validation (`POST /packages/validate`; [contract](CODE_ANIMATION_PACKAGES.md)) || `/api/code-animation/execution` | Code Animation contained production execution: platform sandbox and lane readiness (`GET /`), operator-owned tool paths (`PUT /tools`, host control) and the on-demand adversarial containment check (`POST /probe`, host control) ([contract](CODE_ANIMATION_PACKAGES.md#contained-production-execution)) || `/api/image-to-3d` | Image-to-3D conversion || `/api/rigging` | Auto-skin rigging and animation retargeting for image-to-3D models |diff --git a/server/routes/codeAnimation.js b/server/routes/codeAnimation.jsindex 2cbfd416e3..4fb1f7eb68 100644--- a/server/routes/codeAnimation.js+++ b/server/routes/codeAnimation.js@@ -130,6 +130,8 @@ const briefSchema = z.object({note: z.string().trim().max(L.referenceNoteMax).default(''),}).strict().nullable().optional(),audio: audioSchema,+ // A finished animation this one forks and extends (its HTML rides the prompt).+ baseJobId: z.string().uuid().nullable().optional(),}).strict();const generateSchema = briefSchema.extend({@@ -180,8 +182,8 @@ router.post('/brief', asyncHandler(async (req, res) => {router.post('/prompt', asyncHandler(async (req, res) => {const input = validateRequest(briefSchema, req.body ?? {});- const { prompt, frame, attachments, audioUrl, moodBoardId } = await buildCodeAnimationRequest(input);- res.json({ prompt, frame, attachments, audioUrl, moodBoardId });+ const { prompt, frame, attachments, audioUrl, moodBoardId, base } = await buildCodeAnimationRequest(input);+ res.json({ prompt, frame, attachments, audioUrl, moodBoardId, base });}));router.post('/generate', asyncHandler(async (req, res) => {diff --git a/server/routes/codeAnimation.test.js b/server/routes/codeAnimation.test.jsindex 01d1cf8965..fc6538f307 100644--- a/server/routes/codeAnimation.test.js+++ b/server/routes/codeAnimation.test.js@@ -748,6 +748,39 @@ describe('POST /api/code-animation/generate', () => {await vi.waitFor(() => expect(captureCodeAnimationPoster).toHaveBeenCalledWith(job.id));});+ it('forks a finished animation: its HTML rides the prompt and the new job records what it extends', async () => {+ const baseId = '00000000-0000-4000-8000-000000000004';+ const baseHtml = '<!DOCTYPE html><html><body><script>const DURATION = 20;</script></body></html>';+ codeAnimationRecords.set(baseId, { id: baseId, status: 'completed', title: 'Lantern', frame: { durationSeconds: 20 } });+ codeAnimationHtml.set(baseId, baseHtml);+ getProviderById.mockResolvedValue({ id: 'api-1', type: 'api', enabled: true });+ runPromptThroughProvider.mockResolvedValue({ text: '```html\n<!DOCTYPE html><html></html>\n```', provider: { id: 'api-1' } });+ const app = makeApp();+ const extend = { ...brief, providerId: 'api-1', baseJobId: baseId, format: { durationSeconds: 120 } };+ const res = await request(app).post('/api/code-animation/generate').send(extend);+ expect(res.status).toBe(202);+ expect(res.body.base).toEqual({ jobId: baseId, title: 'Lantern', durationSeconds: 20 });+ expect(res.body.prompt).toContain('BASE ANIMATION');+ expect(res.body.prompt).toContain(baseHtml);+ expect(res.body.prompt).toMatch(/finished 20s cut[\s\S]*120s version/);+ await pollUntilSettled(app, res.body.id);+ expect(runPromptThroughProvider.mock.calls[0][0].prompt).toContain(baseHtml);+ expect(codeAnimationRecords.get(res.body.id).input.baseJobId).toBe(baseId);++ // Unfinished, unknown, and oversized bases are refused before any run.+ runPromptThroughProvider.mockClear();+ const { providerId: _provider, ...forPrompt } = extend;+ codeAnimationRecords.set(baseId, { ...codeAnimationRecords.get(baseId), status: 'running' });+ expect((await request(app).post('/api/code-animation/prompt').send(forPrompt)).body.code).toBe('CODE_ANIMATION_BASE_NOT_COMPLETED');+ codeAnimationRecords.set(baseId, { ...codeAnimationRecords.get(baseId), status: 'completed' });+ codeAnimationHtml.set(baseId, 'x'.repeat(400_001));+ const large = await request(app).post('/api/code-animation/prompt').send(forPrompt);+ expect(large.body.code).toBe('CODE_ANIMATION_BASE_TOO_LARGE');+ expect(large.body.error).toMatch(/400,001 characters.*up to 400,000/);+ expect((await request(app).post('/api/code-animation/prompt').send({ ...forPrompt, baseJobId: '00000000-0000-4000-8000-000000000099' })).body.code).toBe('CODE_ANIMATION_BASE_NOT_FOUND');+ expect(runPromptThroughProvider).not.toHaveBeenCalled();+ });+it('attaches the references for a vision-capable CLI provider instead of only naming their paths', async () => {getProviderById.mockResolvedValue({ id: 'cli-claude', type: 'cli', command: 'claude', enabled: true });runPromptThroughProvider.mockResolvedValue({ text: '```html\n<!DOCTYPE html><html></html>\n```', provider: { id: 'cli-claude', type: 'cli', command: 'claude' } });diff --git a/server/services/codeAnimation/index.js b/server/services/codeAnimation/index.jsindex 3de28715da..f64c9ce924 100644--- a/server/services/codeAnimation/index.js+++ b/server/services/codeAnimation/index.js@@ -212,6 +212,20 @@ async function resolveAudio(audio) {};}+// The finished animation a fork extends: its HTML is sent whole, so an+// oversized file is refused with its size rather than truncated.+async function resolveBaseAnimation(baseJobId) {+ if (!baseJobId) return null;+ const job = isCodeAnimationJobId(baseJobId) ? await getCodeAnimationJobRecord(baseJobId) : null;+ if (!job) throw new ServerError('The animation to extend was not found', { status: 400, code: 'CODE_ANIMATION_BASE_NOT_FOUND' });+ if (job.status !== 'completed') throw new ServerError('Only a finished animation can be extended', { status: 409, code: 'CODE_ANIMATION_BASE_NOT_COMPLETED' });+ const html = await readCodeAnimationHtml(job.id);+ if (html.length > CODE_ANIMATION_LIMITS.baseHtmlMax) {+ throw new ServerError(`The animation to extend is ${html.length.toLocaleString('en-US')} characters of HTML; extending sends it whole, up to ${CODE_ANIMATION_LIMITS.baseHtmlMax.toLocaleString('en-US')}.`, { status: 400, code: 'CODE_ANIMATION_BASE_TOO_LARGE' });+ }+ return { jobId: job.id, title: job.title || '', durationSeconds: job.frame?.durationSeconds ?? null, html };+}+/*** Resolve every configuration a brief references and build its prompt.* `delivery` decides how reference images are described to the model@@ -244,6 +258,7 @@ export async function buildCodeAnimationRequest(input, { delivery = 'copy' } = {: Math.max(0, CODE_ANIMATION_LIMITS.referenceImagesMax - videoImages.length - uploads.length - universeImages.length),});const referenceImages = [...videoImages, ...uploads, ...universeImages, ...boardImages];+ const base = await resolveBaseAnimation(input.baseJobId);const promptInput = {title: input.title,concept: input.concept,@@ -260,6 +275,7 @@ export async function buildCodeAnimationRequest(input, { delivery = 'copy' } = {moodBoard: board,referenceImages,referenceVideo,+ base,};const prompt = buildCodeAnimationPrompt({ ...promptInput, delivery });return {@@ -272,6 +288,7 @@ export async function buildCodeAnimationRequest(input, { delivery = 'copy' } = {// Absolute paths never leave the server — the client gets served URLs only.attachments: referenceImages.map(({ label, origin, url }) => ({ label, origin, url })),audioUrl: audio?.url || null,+ base: base ? { jobId: base.jobId, title: base.title, durationSeconds: base.durationSeconds } : null,referencePaths: referenceImages.map((image) => image.path),};}@@ -475,6 +492,7 @@ export async function startCodeAnimationGeneration(input) {prompt: built.copyPrompt,attachments: built.attachments,moodBoardId: built.moodBoardId,+ ...(built.base ? { base: built.base } : {}),error: null,runId: null,createdAt: now,diff --git a/server/services/codeAnimation/prompt.js b/server/services/codeAnimation/prompt.jsindex 4b3b011438..54ca0ad1df 100644--- a/server/services/codeAnimation/prompt.js+++ b/server/services/codeAnimation/prompt.js@@ -77,6 +77,8 @@ export const CODE_ANIMATION_LIMITS = Object.freeze({referenceVideoImageSlots: 3,referenceNoteMax: 300,audioNotesMax: 1_500,+ // The forked animation's HTML rides the extend prompt whole; a larger file is refused, never cut.+ baseHtmlMax: 400_000,});// The host vendors three.js and a fixed set of addons next to the page and@@ -275,6 +277,7 @@ ${interactive ? '10' : '9'}. Hold ${fps}fps: pre-render static layers and textur* @param {object|null} [input.universe] - `{ name, embrace, avoid, styleNotes, styleReferences }`* @param {object|null} [input.moodBoard] - `collectBoardStyleContext` output* @param {Array<{ label: string, origin: 'upload'|'universe'|'mood-board', note?: string, path?: string }>} [input.referenceImages]+ * @param {{ title?: string, durationSeconds: number, html: string }|null} [input.base] - a finished animation this one extends* @param {'copy'|'api'|'cli'} [input.delivery] - how reference images reach the model* @returns {string}*/@@ -294,6 +297,7 @@ export function buildCodeAnimationPrompt({moodBoard = null,referenceImages = [],referenceVideo = null,+ base = null,delivery = 'copy',}) {const { width, height } = resolveFrameSize(format.aspectRatio, format.resolution);@@ -302,6 +306,7 @@ export function buildCodeAnimationPrompt({`You are the director, animator, rigger, compositor, sound designer, and render engineer of a ${durationSeconds}-second animated short made entirely in code. Write ONE complete, self-contained HTML file that renders it — no image, video, font, or library assets. Every shape, texture, character, and effect is drawn procedurally in the browser, in the art style below. The bar: it looks like a real studio short that people share, not a tech demo.`,`BRIEF${isNonBlankStr(title) ? ` — "${trimTo(title, 200)}"` : ''}:\n${trimTo(concept, CODE_ANIMATION_LIMITS.conceptMax)}`,];+ if (base) sections.push(baseSection(base, durationSeconds));if (isNonBlankStr(cast)) sections.push(castSection(cast));if (isNonBlankStr(onScreenText)) {sections.push(`ON-SCREEN TEXT / NARRATION BEATS (render typography procedurally, timed to the story; system fonts only):\n${trimTo(onScreenText, CODE_ANIMATION_LIMITS.textMax)}`);@@ -322,6 +327,20 @@ export function buildCodeAnimationPrompt({return sections.join('\n\n');}+// A fork extends a finished animation: its HTML is the first cut to build on.+function baseSection({ title, durationSeconds: baseSeconds, html }, durationSeconds) {+ return [+ `BASE ANIMATION — EXTEND THIS FILM${isNonBlankStr(title) ? ` ("${trimTo(title, 200)}")` : ''}: the HTML below is a finished ${baseSeconds}s cut of this film. Write the ${durationSeconds}s version by extending it, not by starting over.`,+ '- Keep its art style, palette, characters, rigs, typography, and code architecture. Reuse and extend its drawing, rig, and timeline code rather than rewriting it, so the extended film is recognisably the same.',+ `- Its existing beats open the film in their original order unless the BRIEF says otherwise; the BRIEF describes the full ${durationSeconds}s story, including what the base already shows.`,+ `- Update ANIMATION_META, the timeline, and every duration constant to ${durationSeconds}s; nothing may stop, loop, or freeze at the old ${baseSeconds}s mark.`,+ '- Everything in the runtime contract below still applies to the extended file.',+ '```html',+ html,+ '```',+ ].join('\n');+}+/*** Pull the HTML document out of a model response: a ```html fence first, then* any fence holding a document, then a bare `<!DOCTYPE html>…</html>` / `<html…>`