From 6e92da3ba58260af5a6057bc2bd3737cefe715e9 Mon Sep 17 00:00:00 2001 From: Paul Bakaus Date: Mon, 13 Jul 2026 12:43:36 -0700 Subject: [PATCH] Cut Live first review latency AI-assisted implementation under maintainer direction. --- .../scripts/live/codex-worker-supervisor.mjs | 65 +++++--- skill/scripts/live/codex-worker.mjs | 144 +++++++++++------- tests/live-codex-worker-supervisor.test.mjs | 24 ++- tests/live-codex-worker.test.mjs | 130 +++++++++++++++- 4 files changed, 278 insertions(+), 85 deletions(-) diff --git a/skill/scripts/live/codex-worker-supervisor.mjs b/skill/scripts/live/codex-worker-supervisor.mjs index eac8e231e..188bb130d 100644 --- a/skill/scripts/live/codex-worker-supervisor.mjs +++ b/skill/scripts/live/codex-worker-supervisor.mjs @@ -157,6 +157,9 @@ export class CodexLiveWorkerSupervisor { const handled = await this.handleAccept(event, this.base, this.token, { deferReply: event.type === 'accept', }); + if (handled?._acceptResult?.handled !== true) { + this.log(`${event.type} ${event.id} source update failed: ${handled?._acceptResult?.error || 'unhandled'}`); + } if (event.type === 'accept' && handled?._acceptResult?.carbonize === true) { await this.postCleanup(this.base, this.token, { id: event.id, @@ -326,6 +329,7 @@ export class CodexLiveWorkerSupervisor { let publishedFromMessage = false; let publicationPromise = null; let earlyCandidateError = null; + let durableCandidate = null; const publishCandidate = async (answer) => { if (publishedFromMessage || this.isCanceled(event.id)) return; if (!publicationPromise) { @@ -335,34 +339,49 @@ export class CodexLiveWorkerSupervisor { phase: generationPhaseName(phase, 'validating'), durationMs: Date.now() - phaseStartedAt, }); - const applied = applyCodexWorkerOutput({ - output: answer, - prepared, - phase, - expectedVariants: Number(event.count || arrivedVariants), - sessionId: event.id, - scaffold: event.scaffold, - cwd: this.cwd, - maxBytes: this.config.maxArtifactBytes, - }); - if (!prepared.previewMode && (phase === 'second' || phase === 'final')) { + if (!durableCandidate) { const candidatePath = path.resolve(this.cwd, prepared.artifactFile); - const reconciled = reconcilePublishedSourceVariants({ - current: artifact.content, - candidate: fs.readFileSync(candidatePath, 'utf-8'), - priorArrived: Math.max(1, arrivedVariants - 1), + if (!prepared.previewMode && (phase === 'first' || phase === 'second')) { + // A structured agent message and the final turn result can contain + // the same delta. Always apply against the immutable phase input so + // a failed publication/checkpoint retry cannot double-insert it. + fs.writeFileSync(candidatePath, artifact.content, 'utf-8'); + } + const applied = applyCodexWorkerOutput({ + output: answer, + prepared, + phase, + expectedVariants: Number(event.count || arrivedVariants), + sessionId: event.id, + scaffold: event.scaffold, + cwd: this.cwd, + maxBytes: this.config.maxArtifactBytes, }); - if (!reconciled.ok) throw supervisorError(`reconcile_${reconciled.error}`); - fs.writeFileSync(candidatePath, reconciled.content, 'utf-8'); + if (!prepared.previewMode && (phase === 'second' || phase === 'final')) { + const reconciled = reconcilePublishedSourceVariants({ + current: artifact.content, + candidate: fs.readFileSync(candidatePath, 'utf-8'), + priorArrived: Math.max(1, arrivedVariants - 1), + }); + if (!reconciled.ok) throw supervisorError(`reconcile_${reconciled.error}`); + fs.writeFileSync(candidatePath, reconciled.content, 'utf-8'); + } + if (this.isCanceled(event.id)) return; + const published = publishCodexWorkerPhase({ event, prepared, arrivedVariants, cwd: this.cwd }); + durableCandidate = { applied, published, planRecorded: false }; } - if (applied.plan) { - this.sessionStore.appendEvent({ type: 'variant_plan', id: event.id, plan: applied.plan }); + if (durableCandidate.applied.plan && !durableCandidate.planRecorded) { + this.sessionStore.appendEvent({ + type: 'variant_plan', + id: event.id, + plan: durableCandidate.applied.plan, + }); + durableCandidate.planRecorded = true; } if (this.isCanceled(event.id)) return; - const published = publishCodexWorkerPhase({ event, prepared, arrivedVariants, cwd: this.cwd }); await this.publishCheckpoint(this.base, this.token, { event, - published, + published: durableCandidate.published, scaffold: event.scaffold, arrivedVariants, }); @@ -373,7 +392,7 @@ export class CodexLiveWorkerSupervisor { try { await pendingPublication; } catch (error) { - earlyCandidateError = error; + if (!earlyCandidateError) earlyCandidateError = error; } finally { if (publicationPromise === pendingPublication) publicationPromise = null; } @@ -384,7 +403,7 @@ export class CodexLiveWorkerSupervisor { outputSchema: codexWorkerOutputSchemaForPhase( phase, Number(event.count || arrivedVariants), - { sourceDelta: phase === 'second' && !prepared.previewMode }, + { sourceDelta: (phase === 'first' || phase === 'second') && !prepared.previewMode }, ), onAgentMessage: publishCandidate, eventId: event.id, diff --git a/skill/scripts/live/codex-worker.mjs b/skill/scripts/live/codex-worker.mjs index 0693fff96..8d3827b1d 100644 --- a/skill/scripts/live/codex-worker.mjs +++ b/skill/scripts/live/codex-worker.mjs @@ -57,31 +57,13 @@ export const CODEX_WORKER_OUTPUT_SCHEMA = Object.freeze({ required: ['files'], additionalProperties: false, }); -const CODEX_SOURCE_DELTA_OUTPUT_SCHEMA = Object.freeze({ - type: 'object', - properties: { - sourceDelta: { - type: 'object', - properties: { - variantId: { type: 'integer', minimum: 2, maximum: 2 }, - markup: { type: 'string', minLength: 1 }, - css: { type: 'string', minLength: 1 }, - }, - required: ['variantId', 'markup', 'css'], - additionalProperties: false, - }, - }, - required: ['sourceDelta'], - additionalProperties: false, -}); - export function codexWorkerOutputSchemaForPhase( phase, expectedVariants = 3, { sourceDelta = false } = {}, ) { - if (sourceDelta) return CODEX_SOURCE_DELTA_OUTPUT_SCHEMA; const requirePlan = Number(expectedVariants) > 1 && (phase === 'first' || phase === 'atomic'); + if (sourceDelta) return codexSourceDeltaOutputSchema(phase, requirePlan); return { ...CODEX_WORKER_OUTPUT_SCHEMA, properties: requirePlan @@ -91,6 +73,28 @@ export function codexWorkerOutputSchemaForPhase( }; } +function codexSourceDeltaOutputSchema(phase, requirePlan) { + const variantId = phase === 'first' ? 1 : 2; + const sourceDelta = { + type: 'object', + properties: { + variantId: { type: 'integer', minimum: variantId, maximum: variantId }, + markup: { type: 'string', minLength: 1 }, + css: { type: 'string', minLength: 1 }, + }, + required: ['variantId', 'markup', 'css'], + additionalProperties: false, + }; + return { + type: 'object', + properties: requirePlan + ? { sourceDelta, plan: VARIANT_PLAN_SCHEMA } + : { sourceDelta }, + required: requirePlan ? ['sourceDelta', 'plan'] : ['sourceDelta'], + additionalProperties: false, + }; +} + export function resolveCodexWorkerConfig({ env = process.env, liveConfig = {} } = {}) { const configured = liveConfig.experimentalCodexWorker || liveConfig.codexWorker || {}; const envEnabled = parseBoolean(env.IMPECCABLE_LIVE_CODEX_WORKER); @@ -161,7 +165,8 @@ export function buildGenerationTurnInput({ const first = phase === 'first'; const second = phase === 'second'; const component = Boolean(prepared.previewMode); - const sourceDelta = second && !component; + const sourceDelta = !component && (first || second); + const sourceDeltaVariant = first ? 1 : 2; const actionRules = event.action === 'bolder' && count > 1 ? [ 'For /bolder, keep variant 1 low-risk: preserve the selected root’s high-level layout and create impact through controlled hierarchy, proportion, or rhythm. Reserve root recomposition for variant 2 or 3.', @@ -199,12 +204,12 @@ export function buildGenerationTurnInput({ ...phaseRules, ...actionRules, sourceDelta - ? 'Return exactly sourceDelta for variant 2. markup is only the selected root replacement, without an outer data-impeccable wrapper. css is only the complete fenced CSS for variant 2, following event.scaffold.cssAuthoring.' + ? `Return exactly sourceDelta for variant ${sourceDeltaVariant}${first && count > 1 ? ' plus the complete variant plan' : ''}. markup is only the selected root replacement, without an outer data-impeccable wrapper. css is only the complete fenced CSS for variant ${sourceDeltaVariant}, following event.scaffold.cssAuthoring.` : component ? `Return staged component files relative to componentDir. Allowed variant extension: .${artifact.componentExtension}. The supervisor updates manifest.json.` : `Return exactly one file whose path is ${JSON.stringify(prepared.artifactFile)} and whose content is the complete staged source artifact.`, sourceDelta - ? 'Do not repeat the staged artifact, variant 1, style tags, wrapper comments, or any data-impeccable attributes. The supervisor merges and validates this delta transactionally.' + ? `Do not repeat the staged artifact${second ? ', variant 1' : ''}, style tags, wrapper comments, or any data-impeccable attributes. The supervisor merges and validates this delta transactionally.` : component ? 'For the final/atomic phase include params.json keyed by variant number. Never include manifest.json or paths outside componentDir.' : 'Keep the existing session wrapper and markers intact. Add only valid variant blocks and preview CSS inside that wrapper.', @@ -300,18 +305,24 @@ export function applyCodexWorkerOutput({ maxBytes = 2_000_000, }) { const parsed = typeof output === 'string' ? parseWorkerJson(output) : output; - if (!prepared.previewMode && phase === 'second') { + const requirePlan = Number(expectedVariants) > 1 && (phase === 'first' || phase === 'atomic'); + if (requirePlan && !parsed?.plan) throw workerError('worker_output_plan_missing'); + const plan = parsed?.plan ? normalizeVariantPlan(parsed.plan, expectedVariants) : null; + if (!prepared.previewMode && (phase === 'first' || phase === 'second')) { const artifactPath = resolveInside(cwd, prepared.artifactFile); if (!artifactPath) throw workerError('artifact_path_outside_project'); const content = applyCodexSourceDelta({ source: fs.readFileSync(artifactPath, 'utf-8'), delta: parsed?.sourceDelta, sessionId, + expectedVariantId: phase === 'first' ? 1 : 2, styleMode: scaffold?.styleMode || scaffold?.cssAuthoring?.mode || 'scoped', + styleTag: scaffold?.styleTag, + jsx: scaffold?.commentSyntax?.open === '{/*', }); if (Buffer.byteLength(content) > maxBytes) throw workerError('worker_output_too_large'); fs.writeFileSync(artifactPath, content, 'utf-8'); - return { files: [prepared.artifactFile], plan: null, sourceDelta: true }; + return { files: [prepared.artifactFile], plan, sourceDelta: true }; } if (!Array.isArray(parsed?.files) || parsed.files.length === 0) { throw workerError('worker_output_files_missing'); @@ -327,10 +338,6 @@ export function applyCodexWorkerOutput({ totalBytes += Buffer.byteLength(file.content); } if (totalBytes > maxBytes) throw workerError('worker_output_too_large'); - const requirePlan = Number(expectedVariants) > 1 && (phase === 'first' || phase === 'atomic'); - if (requirePlan && !parsed.plan) throw workerError('worker_output_plan_missing'); - const plan = parsed.plan ? normalizeVariantPlan(parsed.plan, expectedVariants) : null; - if (!prepared.previewMode) { if (parsed.files.length !== 1 || parsed.files[0].path !== prepared.artifactFile) { throw workerError('worker_output_source_path_invalid'); @@ -390,12 +397,18 @@ export function applyCodexSourceDelta({ source, delta, sessionId, + expectedVariantId = 2, styleMode = 'scoped', + styleTag = null, + jsx = false, }) { if (!delta || typeof delta !== 'object' || Array.isArray(delta)) { throw workerError('worker_output_source_delta_missing'); } - if (Number(delta.variantId) !== 2) throw workerError('worker_output_source_delta_variant_invalid'); + const variantId = Number(expectedVariantId); + if (![1, 2].includes(variantId) || Number(delta.variantId) !== variantId) { + throw workerError('worker_output_source_delta_variant_invalid'); + } const markup = String(delta.markup || '').trim(); const css = String(delta.css || '').trim(); if (!markup || !css) throw workerError('worker_output_source_delta_empty'); @@ -407,11 +420,12 @@ export function applyCodexSourceDelta({ } const cssVariantRefs = [...css.matchAll(/\[data-impeccable-variant=(?:"([^"]+)"|'([^']+)')\]/g)] .map((match) => match[1] || match[2]); - if (cssVariantRefs.length === 0 || cssVariantRefs.some((variant) => variant !== '2')) { + if (cssVariantRefs.length === 0 || cssVariantRefs.some((variant) => variant !== String(variantId))) { throw workerError('worker_output_source_delta_css_unfenced'); } const astroGlobal = styleMode === 'astro-global-prefixed'; - if (astroGlobal ? /@scope\b/.test(css) : !/@scope\s*\(\s*\[data-impeccable-variant=(?:"2"|'2')\]\s*\)/.test(css)) { + const scopePattern = new RegExp(`@scope\\s*\\(\\s*\\[data-impeccable-variant=(?:"${variantId}"|'${variantId}')\\]\\s*\\)`); + if (astroGlobal ? /@scope\b/.test(css) : !scopePattern.test(css)) { throw workerError('worker_output_source_delta_css_strategy_invalid'); } @@ -419,48 +433,65 @@ export function applyCodexSourceDelta({ if (!id) throw workerError('worker_output_source_delta_session_missing'); const wrapper = findSessionWrapper(source, id); if (!wrapper) throw workerError('worker_output_source_delta_wrapper_missing'); - if (extractSourceVariantBlock(source, 2)) throw workerError('worker_output_source_delta_variant_exists'); + const wrapperSource = source.slice(wrapper.openStart, wrapper.closeEnd); + if (extractSourceVariantBlock(wrapperSource, variantId)) throw workerError('worker_output_source_delta_variant_exists'); const escapedId = escapeRegExp(id); const styleOpen = new RegExp(`]*\\bdata-impeccable-css=(?:"${escapedId}"|'${escapedId}')[^>]*>`, 'i'); const styleMatch = styleOpen.exec(source); - if (!styleMatch) throw workerError('worker_output_source_delta_style_missing'); - const styleContentStart = styleMatch.index + styleMatch[0].length; - const styleClose = source.indexOf('', styleContentStart); - if (styleClose < 0 || styleClose > wrapper.closeEnd) { - throw workerError('worker_output_source_delta_style_invalid'); - } - const styleContent = source.slice(styleContentStart, styleClose); - let nextStyleContent; - const firstTick = styleContent.indexOf('`'); - const lastTick = styleContent.lastIndexOf('`'); - if (firstTick >= 0 || lastTick >= 0) { - if (firstTick < 0 || lastTick <= firstTick) { + let merged = source; + let newStyleBlock = null; + if (styleMatch) { + const styleContentStart = styleMatch.index + styleMatch[0].length; + const styleClose = source.indexOf('', styleContentStart); + if (styleClose < 0 || styleClose > wrapper.closeEnd) { throw workerError('worker_output_source_delta_style_invalid'); } - nextStyleContent = styleContent.slice(0, lastTick).trimEnd() - + '\n' + css + '\n' - + styleContent.slice(lastTick); + const styleContent = source.slice(styleContentStart, styleClose); + let nextStyleContent; + const firstTick = styleContent.indexOf('`'); + const lastTick = styleContent.lastIndexOf('`'); + if (firstTick >= 0 || lastTick >= 0) { + if (firstTick < 0 || lastTick <= firstTick) { + throw workerError('worker_output_source_delta_style_invalid'); + } + nextStyleContent = styleContent.slice(0, lastTick).trimEnd() + + '\n' + css + '\n' + + styleContent.slice(lastTick); + } else { + nextStyleContent = styleContent.trimEnd() + '\n' + css + '\n'; + } + merged = source.slice(0, styleContentStart) + nextStyleContent + source.slice(styleClose); } else { - nextStyleContent = styleContent.trimEnd() + '\n' + css + '\n'; + if (variantId !== 1) throw workerError('worker_output_source_delta_style_missing'); + const openingTag = String(styleTag || `'].join('\n') + : [openingTag, css, ''].join('\n'); } - let merged = source.slice(0, styleContentStart) + nextStyleContent + source.slice(styleClose); const nextWrapper = findSessionWrapper(merged, id); if (!nextWrapper) throw workerError('worker_output_source_delta_wrapper_missing'); + const endMarker = findSessionEndMarker(merged, id, nextWrapper); const closeLineStart = merged.lastIndexOf('\n', nextWrapper.closeStart) + 1; const closeLinePrefix = merged.slice(closeLineStart, nextWrapper.closeStart); - const childIndent = nextWrapper.indent + ' '; + const childIndent = endMarker?.indent || nextWrapper.indent + ' '; const contentIndent = childIndent + ' '; const indentedMarkup = markup.split('\n') .map((line) => line.trim() ? contentIndent + line : '') .join('\n'); const variantBlock = [ - `${childIndent}
`, + ...(newStyleBlock + ? newStyleBlock.split('\n').map((line) => childIndent + line) + : []), + `${childIndent}
`, indentedMarkup, `${childIndent}
`, ].join('\n'); - if (/^\s*$/.test(closeLinePrefix)) { + if (endMarker) { + merged = merged.slice(0, endMarker.lineStart) + variantBlock + '\n' + merged.slice(endMarker.lineStart); + } else if (/^\s*$/.test(closeLinePrefix)) { merged = merged.slice(0, closeLineStart) + variantBlock + '\n' + merged.slice(closeLineStart); } else { merged = merged.slice(0, nextWrapper.closeStart) @@ -470,6 +501,15 @@ export function applyCodexSourceDelta({ return merged; } +function findSessionEndMarker(source, sessionId, wrapper) { + const marker = `impeccable-variants-end ${sessionId}`; + const markerAt = source.indexOf(marker, wrapper.openStart); + if (markerAt < 0 || markerAt >= wrapper.closeStart) return null; + const lineStart = source.lastIndexOf('\n', markerAt) + 1; + const indent = source.slice(lineStart, markerAt).match(/^\s*/)?.[0] || ''; + return { lineStart, indent }; +} + function normalizeVariantPlan(plan, expectedVariants) { if (!plan || typeof plan !== 'object' || Array.isArray(plan)) { throw workerError('worker_output_plan_invalid'); diff --git a/tests/live-codex-worker-supervisor.test.mjs b/tests/live-codex-worker-supervisor.test.mjs index b84cf7b22..79713454d 100644 --- a/tests/live-codex-worker-supervisor.test.mjs +++ b/tests/live-codex-worker-supervisor.test.mjs @@ -454,7 +454,6 @@ describe('Codex Live worker supervisor ownership and lifecycle', () => { count: 3, generationEpoch: 1, }); - const first = '

Original

One

'; const final = '

Original

Mutated One again

Mutated Two

Three

'; const client = fakeClient(); let turn = 0; @@ -472,19 +471,21 @@ describe('Codex Live worker supervisor ownership and lifecycle', () => { onStarted?.(`turn-${turn}`); const prompt = input.find((item) => item.type === 'text').text; prompts.push(prompt); - const message = turn === 2 + const message = turn <= 2 ? JSON.stringify({ sourceDelta: { - variantId: 2, - markup: '

Two

', - css: '@scope ([data-impeccable-variant="2"]) { h1 { color: green; } }', + variantId: turn, + markup: turn === 1 ? '

One

' : '

Two

', + css: turn === 1 + ? '@scope ([data-impeccable-variant="1"]) { h1 { color: red; } }' + : '@scope ([data-impeccable-variant="2"]) { h1 { color: green; } }', }, + ...(turn === 1 ? { plan } : {}), }) : (() => { const artifactPath = JSON.parse(prompt.match(/Return exactly one file whose path is ("[^"]+")/)[1]); return JSON.stringify({ - files: [{ path: artifactPath, content: turn === 1 ? first : final }], - ...(turn === 1 ? { plan } : {}), + files: [{ path: artifactPath, content: final }], }); })(); await Promise.all([ @@ -496,6 +497,7 @@ describe('Codex Live worker supervisor ownership and lifecycle', () => { const replies = []; const checkpoints = []; const phases = []; + let checkpointAttempts = 0; const supervisor = new CodexLiveWorkerSupervisor({ cwd, base: 'http://localhost:1', @@ -505,7 +507,11 @@ describe('Codex Live worker supervisor ownership and lifecycle', () => { statePath: path.join(cwd, '.impeccable/live/codex-worker.json'), scriptsDir: path.join(cwd, 'skill/scripts'), reply: async (_base, _token, value) => { replies.push(value); }, - publishCheckpoint: async (_base, _token, value) => { checkpoints.push(value); }, + publishCheckpoint: async (_base, _token, value) => { + checkpointAttempts += 1; + if (checkpointAttempts === 1) throw new Error('transient checkpoint transport failure'); + checkpoints.push(value); + }, publishPhase: async (_base, _token, value) => { phases.push(value); }, }); supervisor.thread = { id: 'live-worker-thread' }; @@ -524,6 +530,7 @@ describe('Codex Live worker supervisor ownership and lifecycle', () => { assert.deepEqual(phases.map((item) => item.phase), [ 'first_variant_generating', 'first_variant_validating', + 'first_variant_validating', 'second_variant_generating', 'second_variant_validating', 'remaining_variants_generating', @@ -539,6 +546,7 @@ describe('Codex Live worker supervisor ownership and lifecycle', () => { assert.equal(snapshot.arrivedVariants, 3); assert.equal(snapshot.publishedRevision, 3); assert.deepEqual(snapshot.variantPlan, plan); + assert.equal(checkpointAttempts, 4, 'the durable first publication only retries its checkpoint'); assert.match(prompts[1], /"name": "Composition"/); }); }); diff --git a/tests/live-codex-worker.test.mjs b/tests/live-codex-worker.test.mjs index 822259c28..66148bf27 100644 --- a/tests/live-codex-worker.test.mjs +++ b/tests/live-codex-worker.test.mjs @@ -238,6 +238,10 @@ describe('Codex Live worker structured artifact boundary', () => { codexWorkerOutputSchemaForPhase('second', 3, { sourceDelta: true }).required, ['sourceDelta'], ); + assert.deepEqual( + codexWorkerOutputSchemaForPhase('first', 3, { sourceDelta: true }).required, + ['sourceDelta', 'plan'], + ); }); it('writes only the prepared source artifact path', () => { @@ -250,7 +254,7 @@ describe('Codex Live worker structured artifact boundary', () => { applyCodexWorkerOutput({ output: { files: [{ path: prepared.artifactFile, content: 'after' }], plan: variantPlan() }, prepared, - phase: 'first', + phase: 'atomic', expectedVariants: 3, cwd, }); @@ -259,7 +263,7 @@ describe('Codex Live worker structured artifact boundary', () => { () => applyCodexWorkerOutput({ output: { files: [{ path: 'src/App.jsx', content: 'unsafe' }], plan: variantPlan() }, prepared, - phase: 'first', + phase: 'atomic', expectedVariants: 3, cwd, }), @@ -267,6 +271,58 @@ describe('Codex Live worker structured artifact boundary', () => { ); }); + it('creates the JSX preview style and variant 1 from a fenced first delta', () => { + const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-first-delta-')); + const artifact = path.join(cwd, '.impeccable/live/artifacts/session-r1.jsx'); + mkdirSync(path.dirname(artifact), { recursive: true }); + writeFileSync(artifact, [ + '', + '
', + '
', + ' {/* Original */}', + '

Original

', + ' {/* Variants: insert below this line */}', + ' {/* impeccable-variants-end session */}', + '
', + '
', + ].join('\n')); + const result = applyCodexWorkerOutput({ + output: { + sourceDelta: { + variantId: 1, + markup: '

One

', + css: '@scope ([data-impeccable-variant="1"]) { :scope > .one { color: red; } }', + }, + plan: variantPlan(), + }, + prepared: { artifactFile: '.impeccable/live/artifacts/session-r1.jsx' }, + phase: 'first', + expectedVariants: 3, + sessionId: 'session', + scaffold: { + styleMode: 'scoped', + styleTag: '', '

Original

', '

Immutable

', + ' {/* impeccable-variants-end session */}', '
', '', ].join('\n'); @@ -307,6 +364,9 @@ describe('Codex Live worker structured artifact boundary', () => { assert.match(after, /

Two<\/h1><\/article>/); assert.match(after, /@scope \(\[data-impeccable-variant="2"\]\)/); assert.equal((after.match(/data-impeccable-variant="1"/g) || []).length, 2); + assert.ok( + after.indexOf('
') < after.indexOf('impeccable-variants-end session'), + ); assert.throws(() => applyCodexWorkerOutput({ output: { @@ -325,6 +385,70 @@ describe('Codex Live worker structured artifact boundary', () => { }), /worker_output_source_delta_css_unfenced/); }); + it('keeps progressive deltas inside the deterministic early-Accept boundary', () => { + const cwd = mkdtempSync(path.join(tmpdir(), 'codex-worker-delta-accept-')); + const artifact = path.join(cwd, 'App.jsx'); + writeFileSync(artifact, [ + 'export default function App() {', + ' return
', + '
', + ' {/* impeccable-variants-start session */}', + '
Original
', + ' {/* Variants: insert below this line */}', + ' {/* impeccable-variants-end session */}', + '
', + '
;', + '}', + ].join('\n')); + const prepared = { artifactFile: 'App.jsx' }; + applyCodexWorkerOutput({ + output: { + sourceDelta: { + variantId: 1, + markup: '
One
', + css: '@scope ([data-impeccable-variant="1"]) { :scope > .one { color: red; } }', + }, + plan: variantPlan(), + }, + prepared, + phase: 'first', + expectedVariants: 3, + sessionId: 'session', + scaffold: { + styleMode: 'scoped', + styleTag: '', '

Original

', '

One

', + ' ', '
', ' ', '', @@ -364,6 +489,7 @@ describe('Codex Live worker structured artifact boundary', () => { assert.match(after, /
/); assert.doesNotMatch(after, /@scope/); assert.match(after, //); + assert.ok(after.indexOf('
') < after.indexOf('impeccable-variants-end session')); }); it('never lets a final component turn rewrite arrived variant 1', () => {