Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Fix video duration scoping, local workflows, and guard policy
Codex release-state test971d39389d02
diff --git a/docs/guards.md b/docs/guards.mdindex 3d4e28506..021dac1a3 100644--- a/docs/guards.md+++ b/docs/guards.md@@ -34,7 +34,8 @@ the action is wrong (`onDecided`) and when it cannot tell (`onCannotDecide`):or unfinished progress rows, unread pages of a whole thread the userasked for, text that went into a formatting toolbar, an exact table thatcan no longer be delivered, a field read back as another value than was- written to it, an arithmetic result claimed before the calculator ran)+ written to it, an arithmetic result claimed before the calculator ran, or+ a requested video that is short, unjoined or undelivered)ends the run as `partial`, not `success`, whether it ended with `done`, a structuredCloud run's `done_json` or a plain answer. Missing evidence alone keepsthe model's outcome.diff --git a/docs/providers-and-models.md b/docs/providers-and-models.mdindex 23f614a28..89fcd7780 100644--- a/docs/providers-and-models.md+++ b/docs/providers-and-models.md@@ -362,7 +362,22 @@ the original estimate (`estimatedSeconds`),accepted plan (`plannedSeconds`), and `durationStatus`: `short`, `enough`,or `unverified`. A measured shortfall keeps the generated asset and reportsincomplete delivery; hosted clips without a measured length stay unverified.-These fields guide reporting, not a hard block on the agent's `done` call.+Each requested video keeps its own duration obligation across retries, Continue,+and worker restart. For multiple videos, use a distinct `video_id` per deliverable+and reuse it on retries; a verified intro cannot satisfy a failed outro. Without+labels, another video requested after a verified result starts a new obligation.+For local ComfyUI, corrected retries omit unsupported video options while WebBrain+retains the target separately for verification. The workflow chooses the output+length; its configured loopback `/view` result is measured without redirects.+Duration-constrained hosted MP4s use a credential-free read capped at 15 seconds.+Verification failures keep the asset and report an unverified duration.++Duration completion checks follow the quality-guard policy: by default they+refuse once, then allow a caveated completion. Known short, unjoined, or undelivered+videos downgrade that completion to partial; unavailable duration verification+alone keeps the model's outcome. Strict checks continue refusing until the video+meets its target or the model reports a partial or failed outcome.+Before writing scenes, the agent can load the default Video scenesskill (`skills/video-scenes.md`), which asks for a shared cast and set sheet in`prompt`, one filmable action per scene, and demonstrations instead of diagramsdiff --git a/package.json b/package.jsonindex 526533e56..bf1941fea 100644--- a/package.json+++ b/package.json@@ -87,7 +87,7 @@"test:cms:browser": "node --test test/cms-browser-smoke.mjs","test:compass-quota": "node --test test/compass-quota.mjs","test:compass-quota:ui": "node test/compass-quota-ui.mjs",- "test:generative-media": "node --test test/generative-media.mjs test/generative-media-availability.mjs test/generative-media-video.mjs test/generative-media-fal-schema.mjs test/generative-media-duration-verification.mjs test/video-duration-contract.mjs test/video-duration-contract-lifecycle.mjs",+ "test:generative-media": "node --test test/generative-media.mjs test/generative-media-availability.mjs test/generative-media-video.mjs test/generative-media-fal-schema.mjs test/generative-media-duration-verification.mjs test/video-duration-contract.mjs test/video-duration-contract-lifecycle.mjs test/generative-media-comfy-duration.mjs test/video-duration-deliverables.mjs test/video-duration-guard-policy.mjs","test:generative-media:ui": "node test/generative-media-ui.mjs","test:generative-media:extension": "node test/generative-media-extension.mjs","test:feedback": "node --test test/feedback-traces.mjs",diff --git a/src/chrome/skills/video-scenes.md b/src/chrome/skills/video-scenes.mdindex 0dd0584bc..af633e5b1 100644--- a/src/chrome/skills/video-scenes.md+++ b/src/chrome/skills/video-scenes.md@@ -10,7 +10,7 @@Use this skill before calling `generate_image` with `scenes`. The video model makes each scene as a separate clip from its text alone, without seeing the other clips; WebBrain then joins the clips into one video.-Keep the user's full requested length in `estimated_duration`. A 60-second video can use four 15-second scenes when the model supports that clip length. The per-clip limit does not limit the whole video, and no separate joining tool is needed. When clip lengths are unavailable, use the provider's stated default or eight 8-second scenes for about 64 seconds. Continue generation and report the measured result; missing metadata alone is not a blocker. If the tool asks for shorter scenes, split them into distinct prompts while keeping the full target. WebBrain retains that target across retries. A short, unverified, or unjoined result is incomplete: preserve and report the generated assets, and use a partial or failed outcome if the full video cannot be delivered or verified. Do not regenerate paid clips just to replace missing verification.+Keep the user's full requested length in `estimated_duration`. A 60-second video can use four 15-second scenes when the model supports that clip length. The per-clip limit does not limit the whole video, and no separate joining tool is needed. When clip lengths are unavailable, use the provider's stated default or eight 8-second scenes for about 64 seconds. Continue generation and report the measured result; missing metadata alone is not a blocker. If the tool asks for shorter scenes, split them into distinct prompts while keeping the full target. WebBrain retains that target across retries. For multiple video deliverables, give each a distinct `video_id` and reuse it when retrying; each video keeps its own target and measured result. Local ComfyUI workflows set their own length: omit unsupported video options on corrected retries while WebBrain retains the target for verification. A short, unverified, or unjoined result is incomplete: preserve and report the generated assets, and use a partial or failed outcome if the full video cannot be delivered or verified. Do not regenerate paid clips just to replace missing verification.## The shared promptdiff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex b9438885b..fa2544f8d 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -7,7 +7,7 @@ import { DECISION_SETTINGS_KEYS, decisionFeatureEnabled } from './decision-confiimport { resolveCurrentDecisionConfig } from './decision-availability.js';import { completionStopError, withCompletionTimeout } from './completion-verifier.js';import { incompleteCompletionBlock } from './incomplete-completion.js';-import { normalizeVideoDurationContract, pinVideoDurationTarget, recordVideoDurationResult, videoDurationCompletionBlock } from './video-duration-contract.js';+import { normalizeVideoDurationContract, pinVideoDurationTarget, recordVideoDurationResult, approveVideoDurationTarget, videoDurationRequiredSeconds, videoDurationCompletionBlock } from './video-duration-contract.js';import { COMPLETION_DOCUMENT_STAMP_SCRIPT, COMPLETION_DOCUMENT_IDENTITY_SCRIPT } from './completion-document.js';import { completionFrameSnapshot } from './completion-frame.js';import { completionResultSnapshot, completionResultPixelRect } from './completion-result.js';@@ -169,7 +169,7 @@ import {} from '../providers/provider-compatibility.js';import { resolveMaxOutputTokens } from '../providers/context-windows.js';import { generateImage, readMediaConfig } from './generative-media.js';-import { GENERATIVE_MEDIA_SETUP_NOTE, imageGenAvailability, isImageGenConfigured, mediaPermissionUrl, validateMediaConfig } from './media-config.js';+import { GENERATIVE_MEDIA_SETUP_NOTE, imageGenAvailability, isImageGenConfigured, mediaPermissionUrl, mediaProvider, validateMediaConfig } from './media-config.js';import { extractFirstJsonObject } from './json-extract.js';import { repairAssistantDisplayText, repairClarifyText, sanitizeText as sanitizePlannerText } from './text-sanitize.js';import { clarifyQuestionIsHard, clarifyTimedAnswer, clarifyTimeoutChoice, normalizeClarifyConsequence, normalizeClarifyTimeoutPolicy } from './clarify-timeout.js';@@ -2872,7 +2872,7 @@ export class Agent extends LoopDetector {if (!this._isActionMode(mode)) return null;const outcome = name === 'done_json' ? 'success' : String(args?.outcome || '').trim().toLowerCase();const videoBlock = this._videoDurationDoneBlock(tabId, outcome);- if (videoBlock) return videoBlock;+ if (videoBlock && skip?.has?.('video_duration_incomplete') !== true) return videoBlock;const toolbarObligation = this._richTextToolbarGuard.completionAction(tabId);if (outcome === 'success' && toolbarObligation && skip?.has?.('rich_text_toolbar_target_unresolved') !== true) {return {@@ -5838,7 +5838,22 @@ export class Agent extends LoopDetector {// The guard behind a completion-invariant refusal, with the sentence the// user sees when a completion is accepted over it.- _completionInvariantGuard(tabId, reason) {+ _completionInvariantGuard(tabId, reason, block = null) {+ if (reason === 'video_duration_incomplete') {+ const detail = block || this._videoDurationDoneBlock(tabId);+ const requiredSeconds = detail?.requiredSeconds;+ const status = detail?.durationStatus;+ const videoLabel = detail?.videoId ? `video (${detail.videoId})` : 'video';+ const decided = status !== 'unverified';+ return { id: 'done-video-duration', decided, downgrade: decided,+ caveat: status === 'short'+ ? `The generated ${videoLabel} is ${detail.durationSeconds} seconds long; ${requiredSeconds} seconds were requested.`+ : status === 'unjoined'+ ? `The generated clips were not joined into the requested ${requiredSeconds}-second ${videoLabel}.`+ : status === 'unverified'+ ? `The generated ${videoLabel}'s requested ${requiredSeconds}-second duration could not be verified.`+ : `The requested ${requiredSeconds}-second ${videoLabel} was not delivered.` };+ }if (reason === 'rich_text_toolbar_target_unresolved') {return { id: 'done-rich-text-toolbar-obligation', decided: true,caveat: 'The last text may have gone into a formatting toolbar instead of the editor.' };@@ -5869,8 +5884,8 @@ export class Agent extends LoopDetector {}_completionInvariantRefuses(tabId, block) {- const guard = this._completionInvariantGuard(tabId, block?.reason);- return !guard || this._guardRefusesOnce(tabId, guard.id, { decided: guard.decided, caveat: guard.caveat });+ const guard = this._completionInvariantGuard(tabId, block?.reason, block);+ return !guard || this._guardRefusesOnce(tabId, guard.id, { decided: guard.decided, caveat: guard.caveat, downgrade: guard.downgrade });}// A plain final in an Act/Dev run that acted. The "end with done, not@@ -42198,18 +42213,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst state = this._planExecutionGuards.get(tabId);if (!state?.enabled) return null;const videoBlock = this._videoDurationDoneBlock(tabId, viaDone ? outcome : 'success');- if (videoBlock) {- // Plain-text completion and advisory/lean mode cannot bypass the- // measured media receipt. One recovery turn permits honest reporting.- if (!state.videoDurationTerminalRecoveryAttempted) {- state.videoDurationTerminalRecoveryAttempted = true;- return { retry: true, retryAssistantContent: null, nudge: videoBlock.error };- }- return { failure: videoBlock.error, status: 'video_duration_incomplete', outcome: 'partial',- userMessage: `The requested ${videoBlock.requiredSeconds}-second video is incomplete. ${videoBlock.durationSeconds- ? `The delivered video measures ${videoBlock.durationSeconds} seconds.`- : videoBlock.durationStatus === 'unjoined' ? 'The generated clips could not be joined.'- : 'A completed video of the requested length could not be verified.'}` };+ if (videoBlock && this._completionInvariantRefuses(tabId, videoBlock)) {+ // Share the same one-refusal policy with done, including when a model+ // switches between a tool completion and plain text after the note.+ return { retry: true, retryAssistantContent: null, nudge: videoBlock.error };}// The bracketed diagnostic stays in tool results and traces; the chat// shows a plain-language account of what happened.@@ -47865,7 +47872,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (name === 'done') {const videoBlock = this._videoDurationDoneBlock(tabId, normalizeDoneOutcome(args?.outcome));- if (videoBlock) return videoBlock;+ if (videoBlock && this._completionInvariantRefuses(tabId, videoBlock)) return videoBlock;const doneStartedAt = Date.now();const tableBlock = await this._resolveExactTableDone(tabId, args, onUpdate);if (tableBlock) return tableBlock;@@ -48290,7 +48297,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// failed ending once per run; later endings are not judged again.// A paid asset with a short, unjoined or unverified result may be// reported honestly without the generic judge forcing regeneration.- const mediaDeliveryLimited = ['short', 'unjoined', 'unverified'].includes(this._currentVideoDurationGuard(tabId)?.videoDurationContract?.status);+ const mediaDeliveryLimited = ['short', 'unjoined', 'unverified'].includes(this._videoDurationDoneBlock(tabId)?.durationStatus);const incompleteBlock = mediaDeliveryLimited || this._guardHasAdvised(tabId, 'incomplete-task-redirect') ? null : await incompleteCompletionBlock(this, tabId, {terminalOnly: Number.isFinite(executionContext?.traceStep) && executionContext.traceStep >= this.maxSteps,outcome, summary: args.summary, pageState, pageUrl: probe?.url || '',@@ -48529,7 +48536,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (name === 'generate_image') {const guard = this._currentVideoDurationGuard(tabId);const pinned = pinVideoDurationTarget(args, guard?.videoDurationContract);- const trackVideo = pinned.args?.media === 'video' && guard && pinned.contract;+ if (pinned.error) return { success: false, dispatched: false, error: pinned.error };+ const trackVideo = pinned.trackDuration && guard && pinned.contract;+ const deliverableId = pinned.contract?.deliverableId;if (trackVideo) {// Save before dispatch, including when the first attempt will fail// validation. Otherwise a worker restart can lose the user's target.@@ -48541,10 +48550,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d|| this._checkAbort(tabId) || executionContext?._contentActionAbortSignal?.aborted) return { success: false, dispatched: false,error: 'The task changed or stopped before video generation. Reconsider the current request.' };}- const result = await generateImage(trackVideo ? pinned.args : args, {+ const config = executionContext?.imageGenConfig || (trackVideo ? await readMediaConfig() : undefined);+ if (trackVideo && (this._currentVideoDurationGuard(tabId) !== guard || this._hasPendingSteering(tabId)+ || this._checkAbort(tabId) || executionContext?._contentActionAbortSignal?.aborted)) return { success: false, dispatched: false,+ error: 'The task changed or stopped before video generation. Reconsider the current request.' };+ // Local workflows set their own length. Keep the obligation for output+ // verification without reinserting options the provider asked to omit.+ const providerArgs = trackVideo+ ? mediaProvider(config) === 'comfyui' ? { ...args, media: 'video' } : pinned.args+ : args;+ const result = await generateImage(providerArgs, {signal: executionContext?._contentActionAbortSignal,- ...(executionContext?.imageGenConfig ? { config: executionContext.imageGenConfig } : {}),+ ...(config !== undefined ? { config } : {}),verifyVideoDuration: !!trackVideo,+ ...(trackVideo ? { videoDurationTarget: videoDurationRequiredSeconds(pinned.contract) } : {}),onProgress: message => onUpdate?.('tool_progress', { name, message }),confirmVideoLength: async details => {const answer = await this._promptVideoLengthConfirmation(tabId, details, onUpdate);@@ -48553,15 +48572,14 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d|| this._checkAbort(tabId)) return 'unavailable';// The user's accepted shorter target survives a provider failure or// restart too. Model-written arguments never supply this approval.- guard.videoDurationContract = normalizeVideoDurationContract({ ...guard.videoDurationContract,- approvedDurationSeconds: details.totalSeconds });+ guard.videoDurationContract = approveVideoDurationTarget(guard.videoDurationContract, details.totalSeconds, deliverableId);const saved = await this._persistNow(tabId);return (saved === true || saved?.ok === true) && this._currentVideoDurationGuard(tabId) === guard&& !this._hasPendingSteering(tabId) && !this._checkAbort(tabId) ? answer : 'unavailable';},});if (trackVideo && this._currentVideoDurationGuard(tabId) === guard) {- guard.videoDurationContract = recordVideoDurationResult(guard.videoDurationContract, result);+ guard.videoDurationContract = recordVideoDurationResult(guard.videoDurationContract, result, deliverableId);// The generated asset must still reach the chat if saving its receipt// fails; the pre-dispatch obligation remains durable for recovery.try { await this._persistNow(tabId); } catch {}diff --git a/src/chrome/src/agent/generative-media.js b/src/chrome/src/agent/generative-media.jsindex fee1ff678..343482ba4 100644--- a/src/chrome/src/agent/generative-media.js+++ b/src/chrome/src/agent/generative-media.js@@ -494,13 +494,17 @@ async function videoBytes(response, label) {// A finished clip as MP4 bytes to join: inline media is decoded, a hosted file// downloaded within the deadline. No key is sent with the download, so// redirects are allowed.-async function downloadClip(url, { fetchImpl, signal, timeoutMs, label }) {+async function downloadClip(url, { fetchImpl, signal, timeoutMs, label, localBaseUrl }) {let bytes = dataUrlBytes(url)?.bytes;if (!bytes) {- if (!safeMediaUrl(url)) throw lasting(`${label} returned an unsupported media URL.`);+ // Only the configured workflow's /view endpoint may use loopback HTTP.+ // Refuse redirects so a local output cannot turn verification into a read+ // from another host. Cloud media keeps its ordinary HTTPS URL policy.+ const localView = localBaseUrl && url.startsWith(`${comfyBaseUrl(localBaseUrl)}/view?`);+ if (!localView && !safeMediaUrl(url)) throw lasting(`${label} returned an unsupported media URL.`);const operation = operationSignal(signal, timeoutMs);try {- const res = await fetchImpl(url, { signal: operation.signal, credentials: 'omit' });+ const res = await fetchImpl(url, { signal: operation.signal, credentials: 'omit', ...(localView ? { redirect: 'error' } : {}) });if (!res.ok) throw Object.assign(new Error(`${label} clip download failed (HTTP ${res.status}).`), { status: res.status });bytes = new Uint8Array(await res.arrayBuffer());} catch (error) { throw operation.signal.aborted ? operation.signal.reason : error; }@@ -820,10 +824,10 @@ const roundSeconds = seconds => (Number.isFinite(seconds) && seconds > 0 ? Math.// Keep the declared target, accepted plan and measured output distinct. Paid// assets remain successful results even if the provider delivered too little;// the agent must report the shortfall instead of calling the whole task ready.-function videoDeliveryDetails(result, asset) {+function videoDeliveryDetails(result, asset, targetSeconds) {const plannedSeconds = roundSeconds(result.totalSeconds);- if (!plannedSeconds) return {};- const estimatedSeconds = roundSeconds(result.estimatedSeconds) || plannedSeconds;+ const estimatedSeconds = roundSeconds(targetSeconds) || roundSeconds(result.estimatedSeconds) || plannedSeconds;+ if (!estimatedSeconds) return {};const approvedDurationSeconds = roundSeconds(result.approvedDurationSeconds);const requiredSeconds = approvedDurationSeconds || estimatedSeconds;const actual = asset.durationSeconds;@@ -832,12 +836,12 @@ function videoDeliveryDetails(result, asset) {? (actual + 0.25 < requiredSeconds ? 'short' : 'enough') : 'unverified';return {estimatedSeconds,- plannedSeconds,+ ...(plannedSeconds ? { plannedSeconds } : {}),...(approvedDurationSeconds ? { approvedDurationSeconds } : {}),...(result.fallbackClipSeconds ? { fallbackClipSeconds: result.fallbackClipSeconds } : {}),durationStatus,...(durationStatus === 'short' ? {- notice: `Generated ${actual} s of the planned ${plannedSeconds} s. The accepted target is ${requiredSeconds} s. Keep the generated asset, but report the duration shortfall; the requested video is incomplete. Do not claim it is complete.`,+ notice: `Generated ${actual} s${plannedSeconds ? ` of the planned ${plannedSeconds} s` : ''}. The accepted target is ${requiredSeconds} s. Keep the generated asset, but report the duration shortfall; the requested video is incomplete. Do not claim it is complete.`,} : durationStatus === 'unverified' ? {notice: 'The output duration has not been measured. Do not report plannedSeconds as the actual video length.',} : {}),@@ -884,7 +888,9 @@ async function storeVideoClips(clips, onProgress) {* user when the requested video is longer than WebBrain makes ('once' to go* ahead; 'deny', 'unavailable' or null otherwise). `verifyVideoDuration`* measures a duration-constrained hosted clip, keeping its URL if verification- * fails. The additional read is credential-free and limited to 15 seconds.+ * fails. `videoDurationTarget` is a runtime-only verification target, separate+ * from provider arguments because local workflows set their own duration.+ * The additional read is credential-free and limited to 15 seconds.*/export async function generateImage(args, options = {}) {const fetchImpl = typeof options === 'function' ? options : options.fetchImpl || fetch;@@ -892,6 +898,7 @@ export async function generateImage(args, options = {}) {const config = Object.hasOwn(options, 'config') ? options.config : await readMediaConfig();if (!config) throw new Error('Configure Generative Media in Providers → Generative.');const provider = mediaProvider(config);+ const targetSeconds = roundSeconds(options.videoDurationTarget);const result = await runMediaGeneration({prompt: args?.prompt, media: args?.media ?? null, video: videoArguments(args), config, fetchImpl, signal: options.signal,pollIntervalMs: options.pollIntervalMs, onProgress: options.onProgress, confirmVideoLength: options.confirmVideoLength,@@ -901,7 +908,7 @@ export async function generateImage(args, options = {}) {}if (Array.isArray(result.clips)) {const asset = await storeVideoClips(result.clips, options.onProgress);- return { success: true, model: result.model, provider, ...asset, ...videoDeliveryDetails(result, asset) };+ return { success: true, model: result.model, provider, ...asset, ...videoDeliveryDetails(result, asset, targetSeconds) };}if (result.url.startsWith('data:')) {const asset = await storeGeneratedMedia(result.url);@@ -909,15 +916,16 @@ export async function generateImage(args, options = {}) {const video = asset.mimeType === 'video/mp4' ? dataUrlBytes(result.url) : null;const durationSeconds = video ? roundSeconds(mp4DurationSeconds(video.bytes)) : undefined;const delivered = { ...asset, ...(durationSeconds ? { durationSeconds } : {}) };- return { success: true, model: result.model, provider, ...delivered, ...videoDeliveryDetails(result, delivered) };+ return { success: true, model: result.model, provider, ...delivered, ...videoDeliveryDetails(result, delivered, targetSeconds) };}const { estimatedSeconds, totalSeconds, ...hosted } = result;let verificationFailed = false;- if (options.verifyVideoDuration === true && roundSeconds(totalSeconds)) {+ if (options.verifyVideoDuration === true && (targetSeconds || roundSeconds(totalSeconds))) {try {options.signal?.throwIfAborted?.();const bytes = await downloadClip(hosted.url, { fetchImpl, signal: options.signal,- timeoutMs: 15000, label: 'Generated video duration verification' });+ timeoutMs: 15000, label: 'Generated video duration verification',+ ...(provider === 'comfyui' ? { localBaseUrl: comfyBaseUrl(config.baseUrl) } : {}) });hosted.durationSeconds = roundSeconds(mp4DurationSeconds(bytes));hosted.mimeType = 'video/mp4';} catch {@@ -926,7 +934,7 @@ export async function generateImage(args, options = {}) {verificationFailed = true;}}- const delivery = videoDeliveryDetails(result, hosted);+ const delivery = videoDeliveryDetails(result, hosted, targetSeconds);if (verificationFailed && delivery.durationStatus === 'unverified') {delivery.notice = 'The generated asset was kept, but its duration could not be verified. Do not report plannedSeconds as the actual video length or claim the duration requirement is complete.';}diff --git a/src/chrome/src/agent/guard-policy.js b/src/chrome/src/agent/guard-policy.jsindex 31b4f230e..63a8ff2fb 100644--- a/src/chrome/src/agent/guard-policy.js+++ b/src/chrome/src/agent/guard-policy.js@@ -66,6 +66,9 @@ export const GUARDS = Object.freeze({summary: "done(success) or a plain final while the last text entry hit a rich-text formatting toolbar and no verified editor-body edit has released the obligation." },"done-summary-meta-only": { class: "quality", family: "completion", decides: "knows", onDecided: "advise", onCannotDecide: "advise",summary: "The done summary only says that an answer was given (\"Explained how...\") without the answer (judge summary_kind or done_summary reading): refused for a fresh answer turn." },+ "done-video-duration": { class: "quality", family: "completion", decides: "mixed", onDecided: "advise", onCannotDecide: "advise",+ summary: "A requested video is shorter than its target, unjoined or undelivered, or its duration could not be verified: completion is refused once, then accepted with a caveat; known incomplete delivery ends as partial.",+ mustNotBlock: "test/video-duration-guard-policy.mjs#a generated video with unavailable duration verification can finish after one note" },"done-verification-debt": { class: "quality", family: "completion", decides: "no-evidence", onDecided: "advise", onCannotDecide: "advise",summary: "done(success) or a plain final after a consequential action with no later successful explicit observation (downloads need list_downloads/read_downloaded_file)." },"exact-table-done": { class: "quality", family: "completion", decides: "mixed", onDecided: "advise", onCannotDecide: "advise",diff --git a/src/chrome/src/agent/tools.js b/src/chrome/src/agent/tools.jsindex c521a9e05..2ae935cc1 100644--- a/src/chrome/src/agent/tools.js+++ b/src/chrome/src/agent/tools.js@@ -37,7 +37,7 @@ const DONE_REQUIRED_WITH_OUTCOME = ['summary', 'outcome'];export const SYSTEM_PROMPT_GENERATIVE_MEDIA = `GENERATIVE MEDIA:- generate_image: Create an image, or with media "video" a video, directly from a text prompt through the user's configured image and video models. When the user asks to GENERATE media ("generate an image of a red apple", "make a logo", "create a video clip"), call this tool — do NOT navigate to third-party image sites (Midjourney, DALL·E, Bing Images, etc.). Not available in Ask mode.- Videos: set media to "video" and estimate how long the requested video should run. One that needs more than one clip (an explainer, a story) is one call with scenes, at most 120 s in total, and estimated_duration set to the full length it needs; WebBrain joins the clips into one video and asks the user first when that estimate is longer than 120 s. You never see the result: report its length and clip count from the tool result, not its contents.-- A model's clip limit applies to each scene. Joining is built into generate_image; no separate joining tool is needed. For a one-minute request keep estimated_duration:60 and plan scenes covering 60 seconds, for example four 15-second scenes when supported. If clip lengths are unavailable, use the provider's default or eight 8-second scenes (about 64 seconds), and continue generation. If the tool requests shorter scenes, split them into distinct prompts and retry while keeping the full target. A fallback length is an attempt, not a confirmed model limit; report actual provider rejections rather than claiming missing metadata prevents videos. Report any durationStatus "short" or "unverified", or joined:false result as incomplete, preserving the generated assets. WebBrain retains the requested duration across retries and requires a measured completed video before success; use done outcome partial or failed if that duration cannot be delivered or verified.`;+- A model's clip limit applies to each scene. Joining is built into generate_image; no separate joining tool is needed. For a one-minute request keep estimated_duration:60 and plan scenes covering 60 seconds, for example four 15-second scenes when supported. If clip lengths are unavailable, use the provider's default or eight 8-second scenes (about 64 seconds), and continue generation. If the tool requests shorter scenes, split them into distinct prompts and retry while keeping the full target. A fallback length is an attempt, not a confirmed model limit; report actual provider rejections rather than claiming missing metadata prevents videos. Report any durationStatus "short" or "unverified", or joined:false result as incomplete, preserving the generated assets. WebBrain retains each video’s requested duration across retries. For multiple videos, use a distinct video_id for each deliverable and reuse it on retries. Local ComfyUI workflows set their own length: omit unsupported video options on corrected retries; WebBrain retains the verification target. Use done outcome partial or failed if the requested duration cannot be delivered or verified.`;export const SYSTEM_PROMPT_BATCHING = `BATCHING:- Use browser_batch for predictable steps on the current page with targets you already observed, such as filling known fields or opening a menu and choosing a known item. A navigation followed by a page read can also share one batch.@@ -1495,6 +1495,7 @@ export const AGENT_TOOLS = [properties: {prompt: { type: 'string', description: 'Text prompt describing the media to generate. With scenes: the look, subject and style every clip shares.' },media: { type: 'string', enum: ['image', 'video'], description: '"image" (default) uses the image model; "video" uses the video model. The video options below imply "video".' },+ video_id: { type: 'string', maxLength: 80, pattern: '^[A-Za-z0-9][A-Za-z0-9_-]{0,79}$', description: 'Video only: stable label for this deliverable, such as intro or outro. For multiple videos use distinct labels; reuse the same label and full target on retries. For a new label give estimated_duration or duration.' },estimated_duration: { type: 'integer', minimum: 1, description: 'Video only: full seconds the user requested, or an estimate when unspecified, before fitting it to the 120 s maximum. Keep this target when retrying; never replace it with the per-clip limit. Required with scenes.' },duration: { type: 'integer', minimum: 1, description: 'Video only: seconds for a single clip.' },aspect_ratio: { type: 'string', description: 'Video only, for example "16:9" or "9:16".' },diff --git a/src/chrome/src/agent/video-duration-contract.js b/src/chrome/src/agent/video-duration-contract.jsindex a09b8f16c..fce8cdd97 100644--- a/src/chrome/src/agent/video-duration-contract.js+++ b/src/chrome/src/agent/video-duration-contract.js@@ -1,60 +1,132 @@-// App-owned duration receipt. Tool arguments can establish a target but cannot-// erase it on retry; only the runtime's user-confirmation result can lower it.+// Duration belongs to a video deliverable, not to the whole conversation.+// Unfinished retries keep their target; named videos can be worked on in any+// order without one video's receipt satisfying another video's request.const STATES = new Set(['pending', 'enough', 'short', 'unverified', 'unjoined', 'failed']);const seconds = value => typeof value === 'number' && Number.isFinite(value) && value > 0 && value <= 86400 ? value : null;+const videoId = value => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_-]{0,79}$/.test(value) ? value : null;const TOLERANCE_SECONDS = 0.25;+const MAX_DELIVERABLES = 24;-export function normalizeVideoDurationContract(value) {+function normalizeReceipt(value) {const requestedSeconds = seconds(value?.requestedSeconds);if (!requestedSeconds) return null;const approved = seconds(value?.approvedDurationSeconds);const measured = seconds(value?.durationSeconds);const contract = { requestedSeconds, status: STATES.has(value.status) ? value.status : 'pending' };+ if (Number.isSafeInteger(value.deliverableId) && value.deliverableId > 0) contract.deliverableId = value.deliverableId;+ if (videoId(value.videoId)) contract.videoId = value.videoId;if (approved && approved <= requestedSeconds) contract.approvedDurationSeconds = approved;if (measured) contract.durationSeconds = measured;if (contract.status === 'short' && !measured) contract.status = 'unverified';- // A status label without a sufficient measured receipt is never proof.if (contract.status === 'enough' && (!measured || measured + TOLERANCE_SECONDS < videoDurationRequiredSeconds(contract))) {contract.status = measured ? 'short' : 'unverified';}return contract;}+export function normalizeVideoDurationContract(value) {+ const active = normalizeReceipt(value);+ if (!active) return null;+ const ids = new Set([active.deliverableId || 0]);+ const names = new Set(active.videoId ? [active.videoId] : []);+ const others = (Array.isArray(value.otherDeliverables) ? value.otherDeliverables : []).slice(0, MAX_DELIVERABLES - 1)+ .map(normalizeReceipt).filter(receipt => {+ if (!receipt || ids.has(receipt.deliverableId || 0) || (receipt.videoId && names.has(receipt.videoId))) return false;+ ids.add(receipt.deliverableId || 0);+ if (receipt.videoId) names.add(receipt.videoId);+ return true;+ });+ return others.length ? { ...active, otherDeliverables: others } : active;+}+export function videoDurationRequiredSeconds(contract) {return contract.approvedDurationSeconds || contract.requestedSeconds;}+function receipts(contract) {+ if (!contract) return [];+ const { otherDeliverables, ...active } = contract;+ return [active, ...(otherDeliverables || [])];+}++function activate(all, active) {+ const others = all.filter(receipt => receipt !== active);+ return others.length ? { ...active, otherDeliverables: others } : { ...active };+}+export function pinVideoDurationTarget(args, previous) {const prior = normalizeVideoDurationContract(previous);- const video = args?.media === 'video' || (args?.media == null && ((prior && prior.status !== 'enough')+ const requestedId = videoId(args?.video_id);+ const video = args?.media === 'video' || (args?.media == null && (requestedId || (prior && prior.status !== 'enough')|| ['duration', 'estimated_duration', 'scenes', 'aspect_ratio', 'resolution'].some(key => args?.[key] !== undefined)));- if (!video) return { args, contract: prior };- const contract = prior- || normalizeVideoDurationContract({ requestedSeconds: seconds(args.estimated_duration) || seconds(args.duration), status: 'pending' });- return { args: contract ? { ...args, media: 'video', estimated_duration: videoDurationRequiredSeconds(contract) } : args, contract };+ const unchanged = { args, contract: prior, trackDuration: false };+ if (args?.video_id !== undefined && (!requestedId || args.media === 'image')) {+ return { ...unchanged, error: 'video_id is video-only and must be 1–80 letters, numbers, hyphens or underscores, starting with a letter or number.' };+ }+ if (!video) return unchanged;+ const all = receipts(prior);+ const declaredSeconds = seconds(args?.estimated_duration) || seconds(args?.duration);+ // An unlabeled first call can acquire its stable label on a retry. Once+ // labeled, different ids always name distinct deliverables.+ let active = requestedId ? all.find(receipt => receipt.videoId === requestedId)+ || (prior && !prior.videoId && prior.status !== 'enough'+ && (!declaredSeconds || declaredSeconds === prior.requestedSeconds || declaredSeconds === videoDurationRequiredSeconds(prior)) ? all[0] : null)+ : prior && prior.status !== 'enough' ? all[0] : null;+ if (!active) {+ const requestedSeconds = declaredSeconds;+ if (!requestedSeconds) return requestedId+ ? { ...unchanged, error: 'For a new video_id, give estimated_duration or duration. Reuse the same video_id when retrying that video.' }+ : unchanged;+ if (all.length >= MAX_DELIVERABLES) return { ...unchanged, error: 'This task already tracks 24 video deliverables. Finish or retry an existing video_id before requesting another task.' };+ active = { requestedSeconds, status: 'pending', deliverableId: Math.max(0, ...all.map(receipt => receipt.deliverableId || 0)) + 1 };+ all.push(active);+ }+ if (!active.deliverableId) active.deliverableId = Math.max(0, ...all.map(receipt => receipt.deliverableId || 0)) + 1;+ if (requestedId) active.videoId = requestedId;+ const contract = activate(all, active);+ return { args: { ...args, media: 'video', estimated_duration: videoDurationRequiredSeconds(active) }, contract, trackDuration: true };}-export function recordVideoDurationResult(previous, result) {+function updateReceipt(previous, deliverableId, update) {const contract = normalizeVideoDurationContract(previous);if (!contract) return null;- // Keep an already verified deliverable if a subsequent attempt fails. Do- // not add the durations of separate attempts to manufacture one long video.- if (contract.status === 'enough') return contract;- const approved = seconds(result?.approvedDurationSeconds);- if (approved && approved <= contract.requestedSeconds) contract.approvedDurationSeconds = approved;- delete contract.durationSeconds;- if (result?.success !== true || result.error) return { ...contract, status: 'failed' };- if (result.joined === false) return { ...contract, status: 'unjoined' };- const video = result.mediaType === 'video' || /^video\//.test(result.mimeType || '');- const measured = video && (result.mediaId || result.url) ? seconds(result.durationSeconds) : null;- if (!measured) return { ...contract, status: 'unverified' };- return { ...contract, durationSeconds: measured,- status: measured + TOLERANCE_SECONDS >= videoDurationRequiredSeconds(contract) ? 'enough' : 'short' };+ const all = receipts(contract);+ const index = deliverableId === undefined ? 0 : all.findIndex(receipt => receipt.deliverableId === deliverableId);+ if (index < 0) return contract; // A late result never recreates a discarded obligation.+ all[index] = update(all[index]);+ return activate(all, all[0]);+}++export function approveVideoDurationTarget(previous, approvedSeconds, deliverableId) {+ return updateReceipt(previous, deliverableId, receipt => normalizeReceipt({ ...receipt, approvedDurationSeconds: approvedSeconds }));+}++export function recordVideoDurationResult(previous, result, deliverableId) {+ return updateReceipt(previous, deliverableId, contract => {+ // An existing verified artifact for this same video still satisfies its+ // request. A new video always starts a separate pending receipt.+ if (contract.status === 'enough') return contract;+ const approved = seconds(result?.approvedDurationSeconds);+ if (approved && approved <= contract.requestedSeconds) contract.approvedDurationSeconds = approved;+ delete contract.durationSeconds;+ if (result?.success !== true || result.error) return { ...contract, status: 'failed' };+ if (result.joined === false) return { ...contract, status: 'unjoined' };+ const video = result.mediaType === 'video' || /^video\//.test(result.mimeType || '');+ const measured = video && (result.mediaId || result.url) ? seconds(result.durationSeconds) : null;+ if (!measured) return { ...contract, status: 'unverified' };+ return { ...contract, durationSeconds: measured,+ status: measured + TOLERANCE_SECONDS >= videoDurationRequiredSeconds(contract) ? 'enough' : 'short' };+ });}export function videoDurationCompletionBlock(value, outcome = 'success') {- const contract = normalizeVideoDurationContract(value);- if (!contract || outcome !== 'success' || contract.status === 'enough') return null;+ if (outcome !== 'success') return null;+ const all = receipts(normalizeVideoDurationContract(value));+ // A merely unverified active video must not hide another video's known+ // shortfall when the completion policy chooses its outcome and caveat.+ const contract = all.find(receipt => !['enough', 'unverified'].includes(receipt.status))+ || all.find(receipt => receipt.status === 'unverified');+ if (!contract) return null;const requiredSeconds = videoDurationRequiredSeconds(contract);const detail = contract.status === 'short' ? `The delivered video measures ${contract.durationSeconds} seconds.`: contract.status === 'unjoined' ? 'The clips have not been joined into one video.'@@ -62,6 +134,7 @@ export function videoDurationCompletionBlock(value, outcome = 'success') {: 'No completed video with a verified duration has been delivered.';return { success: false, blockedDone: true, videoDurationIncomplete: true,reason: 'video_duration_incomplete', requiredSeconds, durationStatus: contract.status,+ ...(contract.videoId ? { videoId: contract.videoId } : {}),...(contract.durationSeconds ? { durationSeconds: contract.durationSeconds } : {}),- error: `The requested video requires ${requiredSeconds} seconds. ${detail} Keep estimated_duration:${requiredSeconds} on retries. Success requires one joined video with a measured duration meeting that target. Preserve and report existing assets; if the target cannot be delivered or verified, finish with outcome:"partial" or outcome:"failed" and explain the limitation. Do not regenerate paid clips merely to replace missing verification.` };+ error: `The requested video${contract.videoId ? ` (${contract.videoId})` : ''} requires ${requiredSeconds} seconds. ${detail} Keep this video's full target on retries; for local ComfyUI omit unsupported video options while WebBrain retains its verification target. Preserve and report existing assets and any limitation. If the target cannot be delivered or verified, use outcome:"partial" or outcome:"failed". Do not regenerate paid clips merely to replace missing verification.` };}diff --git a/src/firefox/skills/video-scenes.md b/src/firefox/skills/video-scenes.mdindex 0dd0584bc..af633e5b1 100644--- a/src/firefox/skills/video-scenes.md+++ b/src/firefox/skills/video-scenes.md@@ -10,7 +10,7 @@Use this skill before calling `generate_image` with `scenes`. The video model makes each scene as a separate clip from its text alone, without seeing the other clips; WebBrain then joins the clips into one video.-Keep the user's full requested length in `estimated_duration`. A 60-second video can use four 15-second scenes when the model supports that clip length. The per-clip limit does not limit the whole video, and no separate joining tool is needed. When clip lengths are unavailable, use the provider's stated default or eight 8-second scenes for about 64 seconds. Continue generation and report the measured result; missing metadata alone is not a blocker. If the tool asks for shorter scenes, split them into distinct prompts while keeping the full target. WebBrain retains that target across retries. A short, unverified, or unjoined result is incomplete: preserve and report the generated assets, and use a partial or failed outcome if the full video cannot be delivered or verified. Do not regenerate paid clips just to replace missing verification.+Keep the user's full requested length in `estimated_duration`. A 60-second video can use four 15-second scenes when the model supports that clip length. The per-clip limit does not limit the whole video, and no separate joining tool is needed. When clip lengths are unavailable, use the provider's stated default or eight 8-second scenes for about 64 seconds. Continue generation and report the measured result; missing metadata alone is not a blocker. If the tool asks for shorter scenes, split them into distinct prompts while keeping the full target. WebBrain retains that target across retries. For multiple video deliverables, give each a distinct `video_id` and reuse it when retrying; each video keeps its own target and measured result. Local ComfyUI workflows set their own length: omit unsupported video options on corrected retries while WebBrain retains the target for verification. A short, unverified, or unjoined result is incomplete: preserve and report the generated assets, and use a partial or failed outcome if the full video cannot be delivered or verified. Do not regenerate paid clips just to replace missing verification.## The shared promptdiff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 0e9c7b940..dae08c8c9 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -5,7 +5,7 @@ import { DECISION_SETTINGS_KEYS, decisionFeatureEnabled } from './decision-confiimport { resolveCurrentDecisionConfig } from './decision-availability.js';import { completionStopError, withCompletionTimeout } from './completion-verifier.js';import { incompleteCompletionBlock } from './incomplete-completion.js';-import { normalizeVideoDurationContract, pinVideoDurationTarget, recordVideoDurationResult, videoDurationCompletionBlock } from './video-duration-contract.js';+import { normalizeVideoDurationContract, pinVideoDurationTarget, recordVideoDurationResult, approveVideoDurationTarget, videoDurationRequiredSeconds, videoDurationCompletionBlock } from './video-duration-contract.js';import { COMPLETION_DOCUMENT_STAMP_SCRIPT, COMPLETION_DOCUMENT_IDENTITY_SCRIPT } from './completion-document.js';import { completionFrameSnapshot } from './completion-frame.js';import { completionResultSnapshot, completionResultPixelRect } from './completion-result.js';@@ -161,7 +161,7 @@ import {} from '../providers/provider-compatibility.js';import { resolveMaxOutputTokens } from '../providers/context-windows.js';import { generateImage, readMediaConfig } from './generative-media.js';-import { GENERATIVE_MEDIA_SETUP_NOTE, imageGenAvailability, isImageGenConfigured, mediaPermissionUrl, validateMediaConfig } from './media-config.js';+import { GENERATIVE_MEDIA_SETUP_NOTE, imageGenAvailability, isImageGenConfigured, mediaPermissionUrl, mediaProvider, validateMediaConfig } from './media-config.js';import { extractFirstJsonObject } from './json-extract.js';import { repairAssistantDisplayText, repairClarifyText, sanitizeText as sanitizePlannerText } from './text-sanitize.js';import { clarifyQuestionIsHard, clarifyTimedAnswer, clarifyTimeoutChoice, normalizeClarifyConsequence, normalizeClarifyTimeoutPolicy } from './clarify-timeout.js';@@ -2425,7 +2425,7 @@ export class Agent extends LoopDetector {if (!this._isActionMode(mode)) return null;const outcome = name === 'done_json' ? 'success' : String(args?.outcome || '').trim().toLowerCase();const videoBlock = this._videoDurationDoneBlock(tabId, outcome);- if (videoBlock) return videoBlock;+ if (videoBlock && skip?.has?.('video_duration_incomplete') !== true) return videoBlock;const toolbarObligation = this._richTextToolbarGuard.completionAction(tabId);if (outcome === 'success' && toolbarObligation && skip?.has?.('rich_text_toolbar_target_unresolved') !== true) {return {@@ -5386,7 +5386,22 @@ export class Agent extends LoopDetector {// The guard behind a completion-invariant refusal, with the sentence the// user sees when a completion is accepted over it.- _completionInvariantGuard(tabId, reason) {+ _completionInvariantGuard(tabId, reason, block = null) {+ if (reason === 'video_duration_incomplete') {+ const detail = block || this._videoDurationDoneBlock(tabId);+ const requiredSeconds = detail?.requiredSeconds;+ const status = detail?.durationStatus;+ const videoLabel = detail?.videoId ? `video (${detail.videoId})` : 'video';+ const decided = status !== 'unverified';+ return { id: 'done-video-duration', decided, downgrade: decided,+ caveat: status === 'short'+ ? `The generated ${videoLabel} is ${detail.durationSeconds} seconds long; ${requiredSeconds} seconds were requested.`+ : status === 'unjoined'+ ? `The generated clips were not joined into the requested ${requiredSeconds}-second ${videoLabel}.`+ : status === 'unverified'+ ? `The generated ${videoLabel}'s requested ${requiredSeconds}-second duration could not be verified.`+ : `The requested ${requiredSeconds}-second ${videoLabel} was not delivered.` };+ }if (reason === 'rich_text_toolbar_target_unresolved') {return { id: 'done-rich-text-toolbar-obligation', decided: true,caveat: 'The last text may have gone into a formatting toolbar instead of the editor.' };@@ -5417,8 +5432,8 @@ export class Agent extends LoopDetector {}_completionInvariantRefuses(tabId, block) {- const guard = this._completionInvariantGuard(tabId, block?.reason);- return !guard || this._guardRefusesOnce(tabId, guard.id, { decided: guard.decided, caveat: guard.caveat });+ const guard = this._completionInvariantGuard(tabId, block?.reason, block);+ return !guard || this._guardRefusesOnce(tabId, guard.id, { decided: guard.decided, caveat: guard.caveat, downgrade: guard.downgrade });}// A plain final in an Act/Dev run that acted. The "end with done, not@@ -38865,18 +38880,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst state = this._planExecutionGuards.get(tabId);if (!state?.enabled) return null;const videoBlock = this._videoDurationDoneBlock(tabId, viaDone ? outcome : 'success');- if (videoBlock) {- // Plain-text completion and advisory/lean mode cannot bypass the- // measured media receipt. One recovery turn permits honest reporting.- if (!state.videoDurationTerminalRecoveryAttempted) {- state.videoDurationTerminalRecoveryAttempted = true;- return { retry: true, retryAssistantContent: null, nudge: videoBlock.error };- }- return { failure: videoBlock.error, status: 'video_duration_incomplete', outcome: 'partial',- userMessage: `The requested ${videoBlock.requiredSeconds}-second video is incomplete. ${videoBlock.durationSeconds- ? `The delivered video measures ${videoBlock.durationSeconds} seconds.`- : videoBlock.durationStatus === 'unjoined' ? 'The generated clips could not be joined.'- : 'A completed video of the requested length could not be verified.'}` };+ if (videoBlock && this._completionInvariantRefuses(tabId, videoBlock)) {+ // Share the same one-refusal policy with done, including when a model+ // switches between a tool completion and plain text after the note.+ return { retry: true, retryAssistantContent: null, nudge: videoBlock.error };}// The bracketed diagnostic stays in tool results and traces; the chat// shows a plain-language account of what happened.@@ -43247,7 +43254,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (name === 'done') {const videoBlock = this._videoDurationDoneBlock(tabId, normalizeDoneOutcome(args?.outcome));- if (videoBlock) return videoBlock;+ if (videoBlock && this._completionInvariantRefuses(tabId, videoBlock)) return videoBlock;const doneStartedAt = Date.now();const tableBlock = await this._resolveExactTableDone(tabId, args, onUpdate);if (tableBlock) return tableBlock;@@ -43640,7 +43647,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// failed ending once per run; later endings are not judged again.// A paid asset with a short, unjoined or unverified result may be// reported honestly without the generic judge forcing regeneration.- const mediaDeliveryLimited = ['short', 'unjoined', 'unverified'].includes(this._currentVideoDurationGuard(tabId)?.videoDurationContract?.status);+ const mediaDeliveryLimited = ['short', 'unjoined', 'unverified'].includes(this._videoDurationDoneBlock(tabId)?.durationStatus);const incompleteBlock = mediaDeliveryLimited || this._guardHasAdvised(tabId, 'incomplete-task-redirect') ? null : await incompleteCompletionBlock(this, tabId, {terminalOnly: Number.isFinite(executionContext?.traceStep) && executionContext.traceStep >= this.maxSteps,outcome, summary: args.summary, pageState, pageUrl: pageState?.url || '',@@ -43869,7 +43876,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (name === 'generate_image') {const guard = this._currentVideoDurationGuard(tabId);const pinned = pinVideoDurationTarget(args, guard?.videoDurationContract);- const trackVideo = pinned.args?.media === 'video' && guard && pinned.contract;+ if (pinned.error) return { success: false, dispatched: false, error: pinned.error };+ const trackVideo = pinned.trackDuration && guard && pinned.contract;+ const deliverableId = pinned.contract?.deliverableId;if (trackVideo) {// Save before dispatch, including when the first attempt will fail// validation. Otherwise a worker restart can lose the user's target.@@ -43881,10 +43890,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d|| this._checkAbort(tabId) || executionContext?._contentActionAbortSignal?.aborted) return { success: false, dispatched: false,error: 'The task changed or stopped before video generation. Reconsider the current request.' };}- const result = await generateImage(trackVideo ? pinned.args : args, {+ const config = executionContext?.imageGenConfig || (trackVideo ? await readMediaConfig() : undefined);+ if (trackVideo && (this._currentVideoDurationGuard(tabId) !== guard || this._hasPendingSteering(tabId)+ || this._checkAbort(tabId) || executionContext?._contentActionAbortSignal?.aborted)) return { success: false, dispatched: false,+ error: 'The task changed or stopped before video generation. Reconsider the current request.' };+ // Local workflows set their own length. Keep the obligation for output+ // verification without reinserting options the provider asked to omit.+ const providerArgs = trackVideo+ ? mediaProvider(config) === 'comfyui' ? { ...args, media: 'video' } : pinned.args+ : args;+ const result = await generateImage(providerArgs, {signal: executionContext?._contentActionAbortSignal,- ...(executionContext?.imageGenConfig ? { config: executionContext.imageGenConfig } : {}),+ ...(config !== undefined ? { config } : {}),verifyVideoDuration: !!trackVideo,+ ...(trackVideo ? { videoDurationTarget: videoDurationRequiredSeconds(pinned.contract) } : {}),onProgress: message => onUpdate?.('tool_progress', { name, message }),confirmVideoLength: async details => {const answer = await this._promptVideoLengthConfirmation(tabId, details, onUpdate);@@ -43893,15 +43912,14 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d|| this._checkAbort(tabId)) return 'unavailable';// The user's accepted shorter target survives a provider failure or// restart too. Model-written arguments never supply this approval.- guard.videoDurationContract = normalizeVideoDurationContract({ ...guard.videoDurationContract,- approvedDurationSeconds: details.totalSeconds });+ guard.videoDurationContract = approveVideoDurationTarget(guard.videoDurationContract, details.totalSeconds, deliverableId);const saved = await this._persistNow(tabId);return (saved === true || saved?.ok === true) && this._currentVideoDurationGuard(tabId) === guard&& !this._hasPendingSteering(tabId) && !this._checkAbort(tabId) ? answer : 'unavailable';},});if (trackVideo && this._currentVideoDurationGuard(tabId) === guard) {- guard.videoDurationContract = recordVideoDurationResult(guard.videoDurationContract, result);+ guard.videoDurationContract = recordVideoDurationResult(guard.videoDurationContract, result, deliverableId);// The generated asset must still reach the chat if saving its receipt// fails; the pre-dispatch obligation remains durable for recovery.try { await this._persistNow(tabId); } catch {}diff --git a/src/firefox/src/agent/generative-media.js b/src/firefox/src/agent/generative-media.jsindex fee1ff678..343482ba4 100644--- a/src/firefox/src/agent/generative-media.js+++ b/src/firefox/src/agent/generative-media.js@@ -494,13 +494,17 @@ async function videoBytes(response, label) {// A finished clip as MP4 bytes to join: inline media is decoded, a hosted file// downloaded within the deadline. No key is sent with the download, so// redirects are allowed.-async function downloadClip(url, { fetchImpl, signal, timeoutMs, label }) {+async function downloadClip(url, { fetchImpl, signal, timeoutMs, label, localBaseUrl }) {let bytes = dataUrlBytes(url)?.bytes;if (!bytes) {- if (!safeMediaUrl(url)) throw lasting(`${label} returned an unsupported media URL.`);+ // Only the configured workflow's /view endpoint may use loopback HTTP.+ // Refuse redirects so a local output cannot turn verification into a read+ // from another host. Cloud media keeps its ordinary HTTPS URL policy.+ const localView = localBaseUrl && url.startsWith(`${comfyBaseUrl(localBaseUrl)}/view?`);+ if (!localView && !safeMediaUrl(url)) throw lasting(`${label} returned an unsupported media URL.`);const operation = operationSignal(signal, timeoutMs);try {- const res = await fetchImpl(url, { signal: operation.signal, credentials: 'omit' });+ const res = await fetchImpl(url, { signal: operation.signal, credentials: 'omit', ...(localView ? { redirect: 'error' } : {}) });if (!res.ok) throw Object.assign(new Error(`${label} clip download failed (HTTP ${res.status}).`), { status: res.status });bytes = new Uint8Array(await res.arrayBuffer());} catch (error) { throw operation.signal.aborted ? operation.signal.reason : error; }@@ -820,10 +824,10 @@ const roundSeconds = seconds => (Number.isFinite(seconds) && seconds > 0 ? Math.// Keep the declared target, accepted plan and measured output distinct. Paid// assets remain successful results even if the provider delivered too little;// the agent must report the shortfall instead of calling the whole task ready.-function videoDeliveryDetails(result, asset) {+function videoDeliveryDetails(result, asset, targetSeconds) {const plannedSeconds = roundSeconds(result.totalSeconds);- if (!plannedSeconds) return {};- const estimatedSeconds = roundSeconds(result.estimatedSeconds) || plannedSeconds;+ const estimatedSeconds = roundSeconds(targetSeconds) || roundSeconds(result.estimatedSeconds) || plannedSeconds;+ if (!estimatedSeconds) return {};const approvedDurationSeconds = roundSeconds(result.approvedDurationSeconds);const requiredSeconds = approvedDurationSeconds || estimatedSeconds;const actual = asset.durationSeconds;@@ -832,12 +836,12 @@ function videoDeliveryDetails(result, asset) {? (actual + 0.25 < requiredSeconds ? 'short' : 'enough') : 'unverified';return {estimatedSeconds,- plannedSeconds,+ ...(plannedSeconds ? { plannedSeconds } : {}),...(approvedDurationSeconds ? { approvedDurationSeconds } : {}),...(result.fallbackClipSeconds ? { fallbackClipSeconds: result.fallbackClipSeconds } : {}),durationStatus,...(durationStatus === 'short' ? {- notice: `Generated ${actual} s of the planned ${plannedSeconds} s. The accepted target is ${requiredSeconds} s. Keep the generated asset, but report the duration shortfall; the requested video is incomplete. Do not claim it is complete.`,+ notice: `Generated ${actual} s${plannedSeconds ? ` of the planned ${plannedSeconds} s` : ''}. The accepted target is ${requiredSeconds} s. Keep the generated asset, but report the duration shortfall; the requested video is incomplete. Do not claim it is complete.`,} : durationStatus === 'unverified' ? {notice: 'The output duration has not been measured. Do not report plannedSeconds as the actual video length.',} : {}),@@ -884,7 +888,9 @@ async function storeVideoClips(clips, onProgress) {* user when the requested video is longer than WebBrain makes ('once' to go* ahead; 'deny', 'unavailable' or null otherwise). `verifyVideoDuration`* measures a duration-constrained hosted clip, keeping its URL if verification- * fails. The additional read is credential-free and limited to 15 seconds.+ * fails. `videoDurationTarget` is a runtime-only verification target, separate+ * from provider arguments because local workflows set their own duration.+ * The additional read is credential-free and limited to 15 seconds.*/export async function generateImage(args, options = {}) {const fetchImpl = typeof options === 'function' ? options : options.fetchImpl || fetch;@@ -892,6 +898,7 @@ export async function generateImage(args, options = {}) {const config = Object.hasOwn(options, 'config') ? options.config : await readMediaConfig();if (!config) throw new Error('Configure Generative Media in Providers → Generative.');const provider = mediaProvider(config);+ const targetSeconds = roundSeconds(options.videoDurationTarget);const result = await runMediaGeneration({prompt: args?.prompt, media: args?.media ?? null, video: videoArguments(args), config, fetchImpl, signal: options.signal,pollIntervalMs: options.pollIntervalMs, onProgress: options.onProgress, confirmVideoLength: options.confirmVideoLength,@@ -901,7 +908,7 @@ export async function generateImage(args, options = {}) {}if (Array.isArray(result.clips)) {const asset = await storeVideoClips(result.clips, options.onProgress);- return { success: true, model: result.model, provider, ...asset, ...videoDeliveryDetails(result, asset) };+ return { success: true, model: result.model, provider, ...asset, ...videoDeliveryDetails(result, asset, targetSeconds) };}if (result.url.startsWith('data:')) {const asset = await storeGeneratedMedia(result.url);@@ -909,15 +916,16 @@ export async function generateImage(args, options = {}) {const video = asset.mimeType === 'video/mp4' ? dataUrlBytes(result.url) : null;const durationSeconds = video ? roundSeconds(mp4DurationSeconds(video.bytes)) : undefined;const delivered = { ...asset, ...(durationSeconds ? { durationSeconds } : {}) };- return { success: true, model: result.model, provider, ...delivered, ...videoDeliveryDetails(result, delivered) };+ return { success: true, model: result.model, provider, ...delivered, ...videoDeliveryDetails(result, delivered, targetSeconds) };}const { estimatedSeconds, totalSeconds, ...hosted } = result;let verificationFailed = false;- if (options.verifyVideoDuration === true && roundSeconds(totalSeconds)) {+ if (options.verifyVideoDuration === true && (targetSeconds || roundSeconds(totalSeconds))) {try {options.signal?.throwIfAborted?.();const bytes = await downloadClip(hosted.url, { fetchImpl, signal: options.signal,- timeoutMs: 15000, label: 'Generated video duration verification' });+ timeoutMs: 15000, label: 'Generated video duration verification',+ ...(provider === 'comfyui' ? { localBaseUrl: comfyBaseUrl(config.baseUrl) } : {}) });hosted.durationSeconds = roundSeconds(mp4DurationSeconds(bytes));hosted.mimeType = 'video/mp4';} catch {@@ -926,7 +934,7 @@ export async function generateImage(args, options = {}) {verificationFailed = true;}}- const delivery = videoDeliveryDetails(result, hosted);+ const delivery = videoDeliveryDetails(result, hosted, targetSeconds);if (verificationFailed && delivery.durationStatus === 'unverified') {delivery.notice = 'The generated asset was kept, but its duration could not be verified. Do not report plannedSeconds as the actual video length or claim the duration requirement is complete.';}diff --git a/src/firefox/src/agent/guard-policy.js b/src/firefox/src/agent/guard-policy.jsindex 31b4f230e..63a8ff2fb 100644--- a/src/firefox/src/agent/guard-policy.js+++ b/src/firefox/src/agent/guard-policy.js@@ -66,6 +66,9 @@ export const GUARDS = Object.freeze({summary: "done(success) or a plain final while the last text entry hit a rich-text formatting toolbar and no verified editor-body edit has released the obligation." },"done-summary-meta-only": { class: "quality", family: "completion", decides: "knows", onDecided: "advise", onCannotDecide: "advise",summary: "The done summary only says that an answer was given (\"Explained how...\") without the answer (judge summary_kind or done_summary reading): refused for a fresh answer turn." },+ "done-video-duration": { class: "quality", family: "completion", decides: "mixed", onDecided: "advise", onCannotDecide: "advise",+ summary: "A requested video is shorter than its target, unjoined or undelivered, or its duration could not be verified: completion is refused once, then accepted with a caveat; known incomplete delivery ends as partial.",+ mustNotBlock: "test/video-duration-guard-policy.mjs#a generated video with unavailable duration verification can finish after one note" },"done-verification-debt": { class: "quality", family: "completion", decides: "no-evidence", onDecided: "advise", onCannotDecide: "advise",summary: "done(success) or a plain final after a consequential action with no later successful explicit observation (downloads need list_downloads/read_downloaded_file)." },"exact-table-done": { class: "quality", family: "completion", decides: "mixed", onDecided: "advise", onCannotDecide: "advise",diff --git a/src/firefox/src/agent/tools.js b/src/firefox/src/agent/tools.jsindex 666e12988..c85688089 100644--- a/src/firefox/src/agent/tools.js+++ b/src/firefox/src/agent/tools.js@@ -37,7 +37,7 @@ const DONE_REQUIRED_WITH_OUTCOME = ['summary', 'outcome'];export const SYSTEM_PROMPT_GENERATIVE_MEDIA = `GENERATIVE MEDIA:- generate_image: Create an image, or with media "video" a video, directly from a text prompt through the user's configured image and video models. When the user asks to GENERATE media ("generate an image of a red apple", "make a logo", "create a video clip"), call this tool — do NOT navigate to third-party image sites (Midjourney, DALL·E, Bing Images, etc.). Not available in Ask mode.- Videos: set media to "video" and estimate how long the requested video should run. One that needs more than one clip (an explainer, a story) is one call with scenes, at most 120 s in total, and estimated_duration set to the full length it needs; WebBrain joins the clips into one video and asks the user first when that estimate is longer than 120 s. You never see the result: report its length and clip count from the tool result, not its contents.-- A model's clip limit applies to each scene. Joining is built into generate_image; no separate joining tool is needed. For a one-minute request keep estimated_duration:60 and plan scenes covering 60 seconds, for example four 15-second scenes when supported. If clip lengths are unavailable, use the provider's default or eight 8-second scenes (about 64 seconds), and continue generation. If the tool requests shorter scenes, split them into distinct prompts and retry while keeping the full target. A fallback length is an attempt, not a confirmed model limit; report actual provider rejections rather than claiming missing metadata prevents videos. Report any durationStatus "short" or "unverified", or joined:false result as incomplete, preserving the generated assets. WebBrain retains the requested duration across retries and requires a measured completed video before success; use done outcome partial or failed if that duration cannot be delivered or verified.`;+- A model's clip limit applies to each scene. Joining is built into generate_image; no separate joining tool is needed. For a one-minute request keep estimated_duration:60 and plan scenes covering 60 seconds, for example four 15-second scenes when supported. If clip lengths are unavailable, use the provider's default or eight 8-second scenes (about 64 seconds), and continue generation. If the tool requests shorter scenes, split them into distinct prompts and retry while keeping the full target. A fallback length is an attempt, not a confirmed model limit; report actual provider rejections rather than claiming missing metadata prevents videos. Report any durationStatus "short" or "unverified", or joined:false result as incomplete, preserving the generated assets. WebBrain retains each video’s requested duration across retries. For multiple videos, use a distinct video_id for each deliverable and reuse it on retries. Local ComfyUI workflows set their own length: omit unsupported video options on corrected retries; WebBrain retains the verification target. Use done outcome partial or failed if the requested duration cannot be delivered or verified.`;export const SYSTEM_PROMPT_BATCHING = `BATCHING:- Use browser_batch for predictable steps on the current page with targets you already observed, such as filling known fields or opening a menu and choosing a known item. A navigation followed by a page read can also share one batch.@@ -1350,6 +1350,7 @@ export const AGENT_TOOLS = [properties: {prompt: { type: 'string', description: 'Text prompt describing the media to generate. With scenes: the look, subject and style every clip shares.' },media: { type: 'string', enum: ['image', 'video'], description: '"image" (default) uses the image model; "video" uses the video model. The video options below imply "video".' },+ video_id: { type: 'string', maxLength: 80, pattern: '^[A-Za-z0-9][A-Za-z0-9_-]{0,79}$', description: 'Video only: stable label for this deliverable, such as intro or outro. For multiple videos use distinct labels; reuse the same label and full target on retries. For a new label give estimated_duration or duration.' },estimated_duration: { type: 'integer', minimum: 1, description: 'Video only: full seconds the user requested, or an estimate when unspecified, before fitting it to the 120 s maximum. Keep this target when retrying; never replace it with the per-clip limit. Required with scenes.' },duration: { type: 'integer', minimum: 1, description: 'Video only: seconds for a single clip.' },aspect_ratio: { type: 'string', description: 'Video only, for example "16:9" or "9:16".' },diff --git a/src/firefox/src/agent/video-duration-contract.js b/src/firefox/src/agent/video-duration-contract.jsindex a09b8f16c..fce8cdd97 100644--- a/src/firefox/src/agent/video-duration-contract.js+++ b/src/firefox/src/agent/video-duration-contract.js@@ -1,60 +1,132 @@-// App-owned duration receipt. Tool arguments can establish a target but cannot-// erase it on retry; only the runtime's user-confirmation result can lower it.+// Duration belongs to a video deliverable, not to the whole conversation.+// Unfinished retries keep their target; named videos can be worked on in any+// order without one video's receipt satisfying another video's request.const STATES = new Set(['pending', 'enough', 'short', 'unverified', 'unjoined', 'failed']);const seconds = value => typeof value === 'number' && Number.isFinite(value) && value > 0 && value <= 86400 ? value : null;+const videoId = value => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_-]{0,79}$/.test(value) ? value : null;const TOLERANCE_SECONDS = 0.25;+const MAX_DELIVERABLES = 24;-export function normalizeVideoDurationContract(value) {+function normalizeReceipt(value) {const requestedSeconds = seconds(value?.requestedSeconds);if (!requestedSeconds) return null;const approved = seconds(value?.approvedDurationSeconds);const measured = seconds(value?.durationSeconds);const contract = { requestedSeconds, status: STATES.has(value.status) ? value.status : 'pending' };+ if (Number.isSafeInteger(value.deliverableId) && value.deliverableId > 0) contract.deliverableId = value.deliverableId;+ if (videoId(value.videoId)) contract.videoId = value.videoId;if (approved && approved <= requestedSeconds) contract.approvedDurationSeconds = approved;if (measured) contract.durationSeconds = measured;if (contract.status === 'short' && !measured) contract.status = 'unverified';- // A status label without a sufficient measured receipt is never proof.if (contract.status === 'enough' && (!measured || measured + TOLERANCE_SECONDS < videoDurationRequiredSeconds(contract))) {contract.status = measured ? 'short' : 'unverified';}return contract;}+export function normalizeVideoDurationContract(value) {+ const active = normalizeReceipt(value);+ if (!active) return null;+ const ids = new Set([active.deliverableId || 0]);+ const names = new Set(active.videoId ? [active.videoId] : []);+ const others = (Array.isArray(value.otherDeliverables) ? value.otherDeliverables : []).slice(0, MAX_DELIVERABLES - 1)+ .map(normalizeReceipt).filter(receipt => {+ if (!receipt || ids.has(receipt.deliverableId || 0) || (receipt.videoId && names.has(receipt.videoId))) return false;+ ids.add(receipt.deliverableId || 0);+ if (receipt.videoId) names.add(receipt.videoId);+ return true;+ });+ return others.length ? { ...active, otherDeliverables: others } : active;+}+export function videoDurationRequiredSeconds(contract) {return contract.approvedDurationSeconds || contract.requestedSeconds;}+function receipts(contract) {+ if (!contract) return [];+ const { otherDeliverables, ...active } = contract;+ return [active, ...(otherDeliverables || [])];+}++function activate(all, active) {+ const others = all.filter(receipt => receipt !== active);+ return others.length ? { ...active, otherDeliverables: others } : { ...active };+}+export function pinVideoDurationTarget(args, previous) {const prior = normalizeVideoDurationContract(previous);- const video = args?.media === 'video' || (args?.media == null && ((prior && prior.status !== 'enough')+ const requestedId = videoId(args?.video_id);+ const video = args?.media === 'video' || (args?.media == null && (requestedId || (prior && prior.status !== 'enough')|| ['duration', 'estimated_duration', 'scenes', 'aspect_ratio', 'resolution'].some(key => args?.[key] !== undefined)));- if (!video) return { args, contract: prior };- const contract = prior- || normalizeVideoDurationContract({ requestedSeconds: seconds(args.estimated_duration) || seconds(args.duration), status: 'pending' });- return { args: contract ? { ...args, media: 'video', estimated_duration: videoDurationRequiredSeconds(contract) } : args, contract };+ const unchanged = { args, contract: prior, trackDuration: false };+ if (args?.video_id !== undefined && (!requestedId || args.media === 'image')) {+ return { ...unchanged, error: 'video_id is video-only and must be 1–80 letters, numbers, hyphens or underscores, starting with a letter or number.' };+ }+ if (!video) return unchanged;+ const all = receipts(prior);+ const declaredSeconds = seconds(args?.estimated_duration) || seconds(args?.duration);+ // An unlabeled first call can acquire its stable label on a retry. Once+ // labeled, different ids always name distinct deliverables.+ let active = requestedId ? all.find(receipt => receipt.videoId === requestedId)+ || (prior && !prior.videoId && prior.status !== 'enough'+ && (!declaredSeconds || declaredSeconds === prior.requestedSeconds || declaredSeconds === videoDurationRequiredSeconds(prior)) ? all[0] : null)+ : prior && prior.status !== 'enough' ? all[0] : null;+ if (!active) {+ const requestedSeconds = declaredSeconds;+ if (!requestedSeconds) return requestedId+ ? { ...unchanged, error: 'For a new video_id, give estimated_duration or duration. Reuse the same video_id when retrying that video.' }+ : unchanged;+ if (all.length >= MAX_DELIVERABLES) return { ...unchanged, error: 'This task already tracks 24 video deliverables. Finish or retry an existing video_id before requesting another task.' };+ active = { requestedSeconds, status: 'pending', deliverableId: Math.max(0, ...all.map(receipt => receipt.deliverableId || 0)) + 1 };+ all.push(active);+ }+ if (!active.deliverableId) active.deliverableId = Math.max(0, ...all.map(receipt => receipt.deliverableId || 0)) + 1;+ if (requestedId) active.videoId = requestedId;+ const contract = activate(all, active);+ return { args: { ...args, media: 'video', estimated_duration: videoDurationRequiredSeconds(active) }, contract, trackDuration: true };}-export function recordVideoDurationResult(previous, result) {+function updateReceipt(previous, deliverableId, update) {const contract = normalizeVideoDurationContract(previous);if (!contract) return null;- // Keep an already verified deliverable if a subsequent attempt fails. Do- // not add the durations of separate attempts to manufacture one long video.- if (contract.status === 'enough') return contract;- const approved = seconds(result?.approvedDurationSeconds);- if (approved && approved <= contract.requestedSeconds) contract.approvedDurationSeconds = approved;- delete contract.durationSeconds;- if (result?.success !== true || result.error) return { ...contract, status: 'failed' };- if (result.joined === false) return { ...contract, status: 'unjoined' };- const video = result.mediaType === 'video' || /^video\//.test(result.mimeType || '');- const measured = video && (result.mediaId || result.url) ? seconds(result.durationSeconds) : null;- if (!measured) return { ...contract, status: 'unverified' };- return { ...contract, durationSeconds: measured,- status: measured + TOLERANCE_SECONDS >= videoDurationRequiredSeconds(contract) ? 'enough' : 'short' };+ const all = receipts(contract);+ const index = deliverableId === undefined ? 0 : all.findIndex(receipt => receipt.deliverableId === deliverableId);+ if (index < 0) return contract; // A late result never recreates a discarded obligation.+ all[index] = update(all[index]);+ return activate(all, all[0]);+}++export function approveVideoDurationTarget(previous, approvedSeconds, deliverableId) {+ return updateReceipt(previous, deliverableId, receipt => normalizeReceipt({ ...receipt, approvedDurationSeconds: approvedSeconds }));+}++export function recordVideoDurationResult(previous, result, deliverableId) {+ return updateReceipt(previous, deliverableId, contract => {+ // An existing verified artifact for this same video still satisfies its+ // request. A new video always starts a separate pending receipt.+ if (contract.status === 'enough') return contract;+ const approved = seconds(result?.approvedDurationSeconds);+ if (approved && approved <= contract.requestedSeconds) contract.approvedDurationSeconds = approved;+ delete contract.durationSeconds;+ if (result?.success !== true || result.error) return { ...contract, status: 'failed' };+ if (result.joined === false) return { ...contract, status: 'unjoined' };+ const video = result.mediaType === 'video' || /^video\//.test(result.mimeType || '');+ const measured = video && (result.mediaId || result.url) ? seconds(result.durationSeconds) : null;+ if (!measured) return { ...contract, status: 'unverified' };+ return { ...contract, durationSeconds: measured,+ status: measured + TOLERANCE_SECONDS >= videoDurationRequiredSeconds(contract) ? 'enough' : 'short' };+ });}export function videoDurationCompletionBlock(value, outcome = 'success') {- const contract = normalizeVideoDurationContract(value);- if (!contract || outcome !== 'success' || contract.status === 'enough') return null;+ if (outcome !== 'success') return null;+ const all = receipts(normalizeVideoDurationContract(value));+ // A merely unverified active video must not hide another video's known+ // shortfall when the completion policy chooses its outcome and caveat.+ const contract = all.find(receipt => !['enough', 'unverified'].includes(receipt.status))+ || all.find(receipt => receipt.status === 'unverified');+ if (!contract) return null;const requiredSeconds = videoDurationRequiredSeconds(contract);const detail = contract.status === 'short' ? `The delivered video measures ${contract.durationSeconds} seconds.`: contract.status === 'unjoined' ? 'The clips have not been joined into one video.'@@ -62,6 +134,7 @@ export function videoDurationCompletionBlock(value, outcome = 'success') {: 'No completed video with a verified duration has been delivered.';return { success: false, blockedDone: true, videoDurationIncomplete: true,reason: 'video_duration_incomplete', requiredSeconds, durationStatus: contract.status,+ ...(contract.videoId ? { videoId: contract.videoId } : {}),...(contract.durationSeconds ? { durationSeconds: contract.durationSeconds } : {}),- error: `The requested video requires ${requiredSeconds} seconds. ${detail} Keep estimated_duration:${requiredSeconds} on retries. Success requires one joined video with a measured duration meeting that target. Preserve and report existing assets; if the target cannot be delivered or verified, finish with outcome:"partial" or outcome:"failed" and explain the limitation. Do not regenerate paid clips merely to replace missing verification.` };+ error: `The requested video${contract.videoId ? ` (${contract.videoId})` : ''} requires ${requiredSeconds} seconds. ${detail} Keep this video's full target on retries; for local ComfyUI omit unsupported video options while WebBrain retains its verification target. Preserve and report existing assets and any limitation. If the target cannot be delivered or verified, use outcome:"partial" or outcome:"failed". Do not regenerate paid clips merely to replace missing verification.` };}diff --git a/test/generative-media-comfy-duration.mjs b/test/generative-media-comfy-duration.mjsnew file mode 100644index 000000000..58dcd98ee--- /dev/null+++ b/test/generative-media-comfy-duration.mjs@@ -0,0 +1,126 @@+import assert from 'node:assert/strict';+import fs from 'node:fs/promises';+import test from 'node:test';++const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };+globalThis.chrome = globalThis.browser = { storage: { local: area, session: area },+ runtime: { getURL: x => x, sendMessage: async () => ({}) } };+const json = value => new Response(JSON.stringify(value), { headers: { 'Content-Type': 'application/json' } });+const video = await fs.readFile(new URL('./fixtures/video/h264-aac-1.mp4', import.meta.url));+const config = { provider: 'comfyui', baseUrl: 'http://localhost:8188/comfy', videoWorkflow: {+ '6': { class_type: 'CLIPTextEncode', inputs: { text: '{{prompt}}' } },+ '9': { class_type: 'SaveVideo', inputs: { images: ['6', 0], seconds: 1 } },+} };+let nextTab = 9300;++function localWorkflow({ failRead = false } = {}) {+ const calls = { submits: [], reads: [] };+ const fetchImpl = async (input, init = {}) => {+ const url = new URL(input);+ assert.equal(url.origin, 'http://localhost:8188');+ assert.equal(init.credentials, 'omit');+ assert.equal(init.redirect, 'error');+ assert.equal(init.headers?.Authorization, undefined);+ if (url.pathname === '/comfy/prompt') {+ assert.equal(init.method, 'POST');+ const body = JSON.parse(init.body);+ calls.submits.push(body);+ assert.deepEqual(Object.keys(body).sort(), ['client_id', 'prompt']);+ assert.deepEqual(body.prompt['9'], config.videoWorkflow['9'], 'verification never rewrites the local workflow');+ return json({ prompt_id: 'local-video' });+ }+ if (url.pathname === '/comfy/history/local-video') return json({ 'local-video': {+ status: { completed: true }, outputs: { '9': { videos: [{ filename: 'clip.mp4', type: 'output' }] } },+ } });+ assert.equal(url.pathname, '/comfy/view');+ assert.equal(url.searchParams.get('filename'), 'clip.mp4');+ calls.reads.push(input);+ if (failRead) throw new DOMException('Verification timed out.', 'TimeoutError');+ return new Response(video, { headers: { 'Content-Type': 'video/mp4' } });+ };+ return { calls, fetchImpl };+}++for (const build of ['chrome', 'firefox']) {+ const { Agent } = await import(`../src/${build}/src/agent/agent.js`);+ const { generateImage } = await import(`../src/${build}/src/agent/generative-media.js`);++ test(`${build}: local duration obligation verifies output without becoming a workflow argument`, async () => {+ const api = localWorkflow();+ const result = await generateImage({ media: 'video', prompt: 'Make a short clip.' }, {+ config, fetchImpl: api.fetchImpl, pollIntervalMs: 1, verifyVideoDuration: true, videoDurationTarget: 60,+ });+ assert.equal(result.success, true);+ assert.equal(result.durationStatus, 'short');+ assert.equal(result.estimatedSeconds, 60);+ assert.equal(result.durationSeconds, 1);+ assert.equal(result.plannedSeconds, undefined, 'a local workflow has no runtime duration plan');+ assert.match(result.url, /^http:\/\/localhost:8188\/comfy\/view\?/);+ assert.equal(api.calls.submits.length, 1);+ assert.equal(api.calls.reads.length, 1);+ });++ test(`${build}: local measured output can satisfy a separately stored target`, async () => {+ const api = localWorkflow();+ const result = await generateImage({ media: 'video', prompt: 'Make a one second clip.' }, {+ config, fetchImpl: api.fetchImpl, pollIntervalMs: 1, verifyVideoDuration: true, videoDurationTarget: 1,+ });+ assert.equal(result.success, true);+ assert.equal(result.durationStatus, 'enough');+ assert.equal(result.durationSeconds, 1);+ });++ test(`${build}: local verification failure preserves the output and unmet verification target`, async () => {+ const api = localWorkflow({ failRead: true });+ const result = await generateImage({ media: 'video', prompt: 'Make a video.' }, {+ config, fetchImpl: api.fetchImpl, pollIntervalMs: 1, verifyVideoDuration: true, videoDurationTarget: 60,+ });+ assert.equal(result.success, true);+ assert.match(result.url, /\/view\?/);+ assert.equal(result.estimatedSeconds, 60);+ assert.equal(result.durationStatus, 'unverified');+ assert.equal(result.plannedSeconds, undefined);+ assert.match(result.notice, /asset was kept/);+ assert.equal(api.calls.submits.length, 1);+ });++ test(`${build}: explicit unsupported local options remain rejected before submission`, async () => {+ const result = await generateImage({ media: 'video', prompt: 'Make a video.', estimated_duration: 60 }, {+ config, fetchImpl: async () => assert.fail('unsupported workflow arguments must not submit'),+ verifyVideoDuration: true, videoDurationTarget: 60,+ });+ assert.equal(result.success, false);+ assert.match(result.error, /do not work with a local ComfyUI workflow/);+ });++ for (const savedConfig of [false, true]) test(`${build}: a corrected local retry reaches ComfyUI with ${savedConfig ? 'saved' : 'dispatch'} settings`, async () => {+ const agent = new Agent({}), tab = nextTab++;+ agent.conversations.set(tab, [{ role: 'user', content: 'Make a one minute video.' }]);+ agent.conversationIds.set(tab, `comfy-duration-${tab}`);+ agent.conversationModes.set(tab, 'act');+ agent._persist = () => {};+ agent._persistNow = async () => ({ ok: true });+ agent._startPlanExecutionGuard(tab, 'act', { requestKind: 'execute', requiresStateChange: false });+ const api = localWorkflow(), originalFetch = globalThis.fetch, originalGet = area.get;+ globalThis.fetch = api.fetchImpl;+ if (savedConfig) area.get = async () => ({ imageGenModel: config });+ try {+ const context = savedConfig ? {} : { imageGenConfig: config };+ const refused = await agent._executeToolImpl(tab, 'generate_image', {+ media: 'video', prompt: 'Make a one minute video.', estimated_duration: 60,+ }, () => {}, context);+ assert.equal(refused.success, false);+ assert.match(refused.error, /do not work with a local ComfyUI workflow/);+ assert.equal(api.calls.submits.length, 0);+ const retried = await agent._executeToolImpl(tab, 'generate_image', {+ ...(savedConfig ? {} : { media: 'video' }), prompt: 'Run the configured workflow.',+ }, () => {}, context);+ assert.equal(retried.success, true, retried.error);+ assert.equal(retried.estimatedSeconds, 60);+ assert.equal(retried.durationStatus, 'short');+ assert.equal(retried.durationSeconds, 1);+ assert.equal(api.calls.submits.length, 1);+ assert.equal(api.calls.reads.length, 1);+ } finally { globalThis.fetch = originalFetch; area.get = originalGet; }+ });+}diff --git a/test/video-duration-contract.mjs b/test/video-duration-contract.mjsindex 0e5ca3bb1..84763b3da 100644--- a/test/video-duration-contract.mjs+++ b/test/video-duration-contract.mjs@@ -54,13 +54,13 @@ for (const build of ['chrome', 'firefox']) {});test(`${build}: only an approved shorter runtime result changes the effective target`, () => {- const long = { requestedSeconds: 330, status: 'pending' };+ const long = { requestedSeconds: 330, status: 'pending', videoId: 'explainer' };assert.ok(block(receipt(long, delivered(120))));const approved = receipt(long, { ...delivered(120), approvedDurationSeconds: 120 });assert.equal(approved.requestedSeconds, 330);assert.equal(approved.approvedDurationSeconds, 120);assert.equal(block(approved), null);- assert.equal(pin({ media: 'video', estimated_duration: 5 }, approved).args.estimated_duration, 120);+ assert.equal(pin({ media: 'video', video_id: 'explainer', estimated_duration: 5 }, approved).args.estimated_duration, 120);});test(`${build}: actual trace retry never dispatches a default short paid clip`, async () => {@@ -94,16 +94,16 @@ for (const build of ['chrome', 'firefox']) {} finally { globalThis.fetch = originalFetch; }});- test(`${build}: short or unverified results cannot bypass done via advisory, lean, JSON or plain text`, async () => {+ test(`${build}: Strict checks refuse short or unverified results through done, JSON and plain text`, async () => {for (const result of [delivered(5), { success: true, url: 'https://media.example/v.mp4' }, { ...delivered(64), joined: false }]) {const { agent, tab, guard } = fixture();guard.videoDurationContract = receipt(target(), result);guard.successfulTaskToolCalls = guard.successfulConsequentialToolCalls = 99;guard.evidenceTaskKey = guard.taskKey;- agent.strictGuards = false;- agent._leanRunActive = () => true;+ agent.strictGuards = true;+ agent._leanRunActive = () => false;agent._resolveExactTableDone = async () => assert.fail('video guard must precede table and lean completion');- assert.ok(agent._completionDoneBlock(tab, 'done', { outcome: 'success' }, null, { skip: new Set(['video_duration_incomplete']) }));+ assert.ok(agent._completionDoneBlock(tab, 'done', { outcome: 'success' }));for (let i = 0; i < 2; i++) {const done = await agent._executeToolImpl(tab, 'done', { outcome: 'success', summary: 'The video is ready.' }, () => {});assert.equal(done.blockedDone, true);@@ -113,15 +113,16 @@ for (const build of ['chrome', 'firefox']) {const structured = await agent._executeToolImpl(tab, 'done_json', { result: {}, summary: 'Complete.' }, () => {});assert.equal(structured.videoDurationIncomplete, true);assert.equal(agent._planOnlyTerminalDecision(tab, 'Here is the full minute.').retry, true);- assert.equal(agent._planOnlyTerminalDecision(tab, 'Here is the full minute.').status, 'video_duration_incomplete');+ assert.equal(agent._planOnlyTerminalDecision(tab, 'Here is the full minute.').retry, true);agent._resolveExactTableDone = async () => null;+ agent._leanRunActive = () => true;const partial = await agent._executeToolImpl(tab, 'done', { outcome: 'partial', summary: 'Only a short clip is available.' }, () => {});assert.equal(partial.done, true);assert.equal(partial.outcome, 'partial');}});- test(`${build}: a measured sufficient receipt passes the hard check`, async () => {+ test(`${build}: a measured sufficient receipt passes the duration check`, async () => {const { agent, tab, guard } = fixture();guard.videoDurationContract = receipt(target(), delivered(64));agent._resolveExactTableDone = async () => null;diff --git a/test/video-duration-deliverables.mjs b/test/video-duration-deliverables.mjsnew file mode 100644index 000000000..62c172ffa--- /dev/null+++ b/test/video-duration-deliverables.mjs@@ -0,0 +1,244 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { readFile } from 'node:fs/promises';++// Real Agent dispatch, duration measurements and session serialization with+// fixture provider responses. No paid video generation is submitted.+const stored = new Map();+const area = {+ get: async key => ({ [key]: stored.get(key) }),+ set: async values => { for (const [key, value] of Object.entries(values)) stored.set(key, structuredClone(value)); },+ remove: async key => { stored.delete(key); },+};+globalThis.chrome = globalThis.browser = {+ storage: { local: area, session: area },+ runtime: { getURL: value => value, sendMessage: async () => ({}) },+};+const json = (body, status = 200) => new Response(JSON.stringify(body), {+ status, headers: { 'Content-Type': 'application/json' },+});+const oneSecond = new Uint8Array(await readFile(new URL('./fixtures/video/h264-aac-1.mp4', import.meta.url)));+const { joinMp4Clips } = await import('../src/chrome/src/agent/video-join.js');+const fiveSeconds = joinMp4Clips(Array(5).fill(oneSecond)).bytes;+const model = 'fixture/multiple-videos';+const config = { provider: 'fal', apiKey: 'fixture-key', videoModel: model };+const gate = { requestKind: 'execute', requiresStateChange: false };+const schema = { openapi: '3.0.4', paths: { '/model': { post: { requestBody: { content: {+ 'application/json': { schema: { type: 'object', properties: {+ prompt: { type: 'string' }, duration: { type: 'integer', enum: [1, 5] },+ } } },+} } } } } };+const deliveries = contract => [contract, ...(contract?.otherDeliverables || [])].filter(Boolean);+const delivered = durationSeconds => ({ success: true, mimeType: 'video/mp4',+ url: 'https://v3b.fal.media/files/fixture.mp4', durationSeconds });+let nextTab = 18200;++function fal({ fail = () => false, beforeDownload = async () => {} } = {}) {+ const submits = [];+ return { submits, fetch: async (url, init = {}) => {+ if (url.startsWith('https://api.fal.ai/v1/models?')) {+ return json({ models: [{ endpoint_id: model, openapi: schema }] });+ }+ if (url.startsWith('https://v3b.fal.media/')) {+ const index = Number(/clip-(\d+)\.mp4$/.exec(url)[1]);+ assert.equal(init.headers?.Authorization, undefined);+ await beforeDownload(index);+ return new Response(submits[index].duration === 5 ? fiveSeconds : oneSecond,+ { headers: { 'Content-Type': 'video/mp4' } });+ }+ assert.equal(init.headers?.Authorization, 'Key fixture-key');+ if (init.method === 'POST') {+ assert.equal(url, `https://queue.fal.run/${model}`);+ const body = JSON.parse(init.body);+ const index = submits.push(body) - 1;+ if (fail(index)) return json({ detail: 'The requested clip was rejected.' }, 400);+ const job = `https://queue.fal.run/${model}/requests/req-${index}`;+ return json({ request_id: `req-${index}`, status_url: `${job}/status`, response_url: job });+ }+ const match = /\/requests\/req-(\d+)(\/status)?$/.exec(url);+ assert.ok(match, url);+ return match[2] ? json({ status: 'COMPLETED' })+ : json({ video: { url: `https://v3b.fal.media/files/clip-${match[1]}.mp4` } });+ } };+}++for (const build of ['chrome', 'firefox']) {+ const { Agent } = await import(`../src/${build}/src/agent/agent.js`);+ const { normalizeVideoDurationContract: normalize, pinVideoDurationTarget: pin,+ recordVideoDurationResult: receipt, approveVideoDurationTarget: approve,+ videoDurationCompletionBlock: block } = await import(`../src/${build}/src/agent/video-duration-contract.js`);++ function fixture() {+ const agent = new Agent({ getActive: () => ({ config: {} }) });+ const tab = nextTab++;+ agent._persist = () => {};+ agent.conversations.set(tab, [{ role: 'system', content: 'Fixture system message.' },+ { role: 'user', content: 'Create a five-second intro and a one-second outro as separate videos.' }]);+ agent.conversationIds.set(tab, `video-deliverables-${tab}`);+ agent.conversationModes.set(tab, 'act');+ const guard = agent._startPlanExecutionGuard(tab, 'act', gate);+ const generate = args => agent._executeToolImpl(tab, 'generate_image', args, () => {}, { imageGenConfig: config });+ return { agent, tab, guard, generate };+ }++ for (const named of [false, true]) {+ test(`${build}: separate ${named ? 'named' : 'legacy'} videos keep their own durations and failed receipts`, async () => {+ const { agent, tab, guard, generate } = fixture();+ const api = fal({ fail: index => index === 1 });+ const originalFetch = globalThis.fetch;+ globalThis.fetch = api.fetch;+ try {+ const intro = await generate({ media: 'video', ...(named ? { video_id: 'intro' } : {}),+ estimated_duration: 5, duration: 5, prompt: 'An intro' });+ assert.equal(intro.success, true);+ assert.equal(intro.durationSeconds, 5);+ assert.equal(guard.videoDurationContract.status, 'enough');+ const outro = await generate({ media: 'video', ...(named ? { video_id: 'outro' } : {}),+ estimated_duration: 1, duration: 1, prompt: 'An outro' });+ assert.equal(outro.success, false);+ assert.deepEqual(api.submits.map(body => body.duration), [5, 1], 'the outro reaches the provider with its own length');+ assert.equal(guard.videoDurationContract.requestedSeconds, 1);+ assert.equal(guard.videoDurationContract.status, 'failed', 'the intro receipt cannot mask the failed outro');+ assert.deepEqual(deliveries(guard.videoDurationContract).map(value => [value.requestedSeconds, value.status]), [[1, 'failed'], [5, 'enough']]);+ assert.equal(block(guard.videoDurationContract).requiredSeconds, 1);+ const retried = await generate({ media: 'video', ...(named ? { video_id: 'outro' } : {}), prompt: 'Retry the outro' });+ assert.equal(retried.success, true);+ assert.equal(retried.durationSeconds, 1);+ assert.deepEqual(api.submits.map(body => body.duration), [5, 1, 1]);+ assert.equal(agent._videoDurationDoneBlock(tab), null);+ } finally { globalThis.fetch = originalFetch; }+ });+ }++ test(`${build}: interleaved named retries retain every unfinished duration`, () => {+ let contract = pin({ media: 'video', video_id: 'lesson', estimated_duration: 60 }).contract;+ contract = receipt(contract, { success: false, error: 'Provider unavailable' });+ contract = pin({ media: 'video', video_id: 'intro', estimated_duration: 5 }, contract).contract;+ contract = receipt(contract, delivered(5));+ assert.equal(block(contract).requiredSeconds, 60, 'completing the intro does not complete the lesson');+ const retry = pin({ media: 'video', video_id: 'lesson', estimated_duration: 1 }, contract);+ assert.equal(retry.args.estimated_duration, 60);+ contract = receipt(retry.contract, delivered(5));+ assert.equal(block(contract).durationStatus, 'short');+ const intro = pin({ media: 'video', video_id: 'intro' }, contract);+ assert.equal(intro.args.estimated_duration, 5);+ assert.equal(intro.contract.status, 'enough');+ assert.equal(block(intro.contract).requiredSeconds, 60);+ const lesson = pin({ media: 'video', video_id: 'lesson' }, intro.contract);+ contract = receipt(lesson.contract, delivered(60));+ assert.equal(block(contract), null);+ assert.equal(deliveries(contract).length, 2);+ });++ test(`${build}: an unknown video id without a target cannot erase another deliverable`, () => {+ const pending = pin({ media: 'video', video_id: 'lesson', estimated_duration: 60 }).contract;+ const attempted = pin({ media: 'video', video_id: 'outro', prompt: 'A separate outro' }, pending);+ assert.ok(attempted.error);+ assert.deepEqual(attempted.contract, pending);+ assert.equal(block(attempted.contract).requiredSeconds, 60);+ });++ test(`${build}: an active unverified video cannot hide another video's measured shortfall`, () => {+ let contract = pin({ media: 'video', video_id: 'lesson', estimated_duration: 60 }).contract;+ contract = receipt(contract, delivered(5));+ contract = pin({ media: 'video', video_id: 'outro', estimated_duration: 1 }, contract).contract;+ contract = receipt(contract, { success: true, url: 'https://v3b.fal.media/files/outro.mp4', mimeType: 'video/mp4' });+ assert.equal(contract.status, 'unverified');+ assert.equal(block(contract).durationStatus, 'short');+ assert.equal(block(contract).requiredSeconds, 60);+ contract = receipt(contract, { success: false }, deliveries(contract).find(value => value.videoId === 'lesson').deliverableId);+ assert.equal(block(contract).durationStatus, 'failed');+ });++ test(`${build}: a new named target is separate while an unlabeled retry can acquire its name`, () => {+ const intro = pin({ media: 'video', estimated_duration: 5 }).contract;+ const outro = pin({ media: 'video', video_id: 'outro', estimated_duration: 1 }, intro);+ assert.equal(outro.args.estimated_duration, 1);+ assert.equal(outro.contract.requestedSeconds, 1);+ assert.equal(deliveries(outro.contract).length, 2);+ assert.equal(deliveries(outro.contract).find(value => value.deliverableId === intro.deliverableId).requestedSeconds, 5);+ for (const duration of [undefined, 5]) {+ const retry = pin({ media: 'video', video_id: 'intro', ...(duration === undefined ? {} : { estimated_duration: duration }) }, intro);+ assert.equal(retry.args.estimated_duration, 5);+ assert.equal(retry.contract.deliverableId, intro.deliverableId);+ assert.equal(retry.contract.videoId, 'intro');+ assert.equal(deliveries(retry.contract).length, 1);+ }+ });++ test(`${build}: receipts and explicit shorter approvals apply to their captured deliverable`, () => {+ const lesson = pin({ media: 'video', video_id: 'lesson', estimated_duration: 330 }).contract;+ const lessonId = lesson.deliverableId;+ let contract = pin({ media: 'video', video_id: 'intro', estimated_duration: 5 }, lesson).contract;+ const introId = contract.deliverableId;+ contract = approve(contract, 120, lessonId);+ contract = receipt(contract, delivered(5), introId);+ assert.equal(block(contract).requiredSeconds, 120);+ contract = receipt(contract, delivered(120), lessonId);+ assert.equal(block(contract), null);+ const savedLesson = deliveries(contract).find(value => value.deliverableId === lessonId);+ const savedIntro = deliveries(contract).find(value => value.deliverableId === introId);+ assert.deepEqual([savedLesson.requestedSeconds, savedLesson.approvedDurationSeconds, savedLesson.durationSeconds], [330, 120, 120]);+ assert.deepEqual([savedIntro.requestedSeconds, savedIntro.approvedDurationSeconds, savedIntro.durationSeconds], [5, undefined, 5]);+ });++ test(`${build}: concurrent same-task generations attach measured receipts to their own video`, async () => {+ const { agent, tab, guard, generate } = fixture();+ let releaseIntro, introReady;+ const waitForIntro = new Promise(resolve => { releaseIntro = resolve; });+ const downloadStarted = new Promise(resolve => { introReady = resolve; });+ const api = fal({ beforeDownload: async index => {+ if (index === 0) { introReady(); await waitForIntro; }+ } });+ const originalFetch = globalThis.fetch;+ globalThis.fetch = api.fetch;+ let intro;+ try {+ intro = generate({ media: 'video', video_id: 'intro', estimated_duration: 5, duration: 5, prompt: 'Intro' });+ await downloadStarted;+ const outro = await generate({ media: 'video', video_id: 'outro', estimated_duration: 1, duration: 1, prompt: 'Outro' });+ assert.equal(outro.success, true);+ assert.equal(block(guard.videoDurationContract).requiredSeconds, 5);+ releaseIntro();+ assert.equal((await intro).success, true);+ assert.equal(agent._videoDurationDoneBlock(tab), null);+ assert.deepEqual(deliveries(guard.videoDurationContract).map(value => [value.requestedSeconds, value.durationSeconds]).sort((a, b) => a[0] - b[0]), [[1, 1], [5, 5]]);+ } finally {+ releaseIntro();+ if (intro) await intro;+ globalThis.fetch = originalFetch;+ }+ });++ test(`${build}: all deliverable obligations survive cold storage and trusted Continue`, async () => {+ for (const tornDown of [false, true]) {+ const { agent, tab, guard } = fixture();+ let contract = pin({ media: 'video', video_id: 'intro', estimated_duration: 5 }).contract;+ contract = receipt(contract, delivered(5));+ contract = pin({ media: 'video', video_id: 'lesson', estimated_duration: 60 }, contract).contract;+ contract = receipt(contract, { success: false });+ contract = pin({ media: 'video', video_id: 'outro', estimated_duration: 1 }, contract).contract;+ guard.videoDurationContract = contract;+ if (tornDown) {+ agent._storeContinuationExecutionEvidence(tab);+ agent._planExecutionGuards.delete(tab);+ }+ assert.equal((await agent._persistNow(tab)).ok, true);+ const entry = stored.get(agent._convKey(tab));+ assert.ok(entry.videoDurationObligation);+ assert.deepEqual(entry.videoDurationObligation.contract, normalize(contract));+ const cold = new Agent({ getActive: () => ({ config: {} }) });+ cold._persist = () => {};+ await cold._hydrateFromSession(tab);+ const resumed = cold._startPlanExecutionGuard(tab, 'act', gate, { trustedContinuation: true });+ assert.deepEqual(resumed.videoDurationContract, normalize(contract));+ assert.equal(deliveries(resumed.videoDurationContract).length, 3);+ const lesson = pin({ media: 'video', video_id: 'lesson' }, resumed.videoDurationContract);+ assert.equal(lesson.args.estimated_duration, 60);+ const lessonDone = receipt(lesson.contract, delivered(60));+ assert.equal(block(lessonDone).requiredSeconds, 1);+ const outro = pin({ media: 'video', video_id: 'outro' }, lessonDone);+ assert.equal(block(receipt(outro.contract, delivered(1))), null);+ }+ });+}diff --git a/test/video-duration-guard-policy.mjs b/test/video-duration-guard-policy.mjsnew file mode 100644index 000000000..f84212e9f--- /dev/null+++ b/test/video-duration-guard-policy.mjs@@ -0,0 +1,143 @@+import assert from 'node:assert/strict';+import test from 'node:test';++const PAGE = 'https://owned.example.test/video';+const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };+globalThis.chrome = globalThis.browser = {+ storage: { local: area, session: area },+ runtime: { getURL: value => value, sendMessage: async () => ({}) },+ tabs: { get: async id => ({ id, url: PAGE, title: 'Video', status: 'complete' }) },+};+let nextTab = 18100, nextCall = 1;+const builds = await Promise.all(['chrome', 'firefox'].map(async build => ({+ build, Agent: (await import(`../src/${build}/src/agent/agent.js`)).Agent,+})));++function fixture(Agent, { strict = false, status = 'unverified' } = {}) {+ const agent = new Agent({ getActive: () => ({ model: 'test', supportsTools: true, supportsVision: false }), getVisionProvider: async () => null });+ const tab = nextTab++;+ agent.strictGuards = strict;+ agent.conversationIds.set(tab, `video-${tab}`);+ agent.conversationModes.set(tab, 'act');+ agent.conversations.set(tab, [{ role: 'system', content: 'sys' }, { role: 'user', content: 'Generate a sixty-second video.' }]);+ agent._ensureGateSetting = async () => {};+ agent._skipPermissionGate = true;+ agent._currentUrl = async () => PAGE;+ agent._persist = () => {};+ agent._persistNow = async () => ({ ok: true });+ const guard = agent._startPlanExecutionGuard(tab, 'act', { requestKind: 'execute', requiresStateChange: false });+ guard.videoDurationContract = { requestedSeconds: 60, status, ...(status === 'short' ? { durationSeconds: 5 } : {}) };+ guard.evidenceTaskKey = guard.taskKey;+ guard.successfulTaskToolCalls = guard.successfulConsequentialToolCalls = 1;+ agent.executeTool = async (_tab, name, args) => {+ assert.ok(['done', 'done_json'].includes(name));+ return name === 'done_json'+ ? { done: true, doneJson: true, summary: args.summary }+ : { done: true, summary: args.summary, outcome: args.outcome };+ };+ const batch = async (name = 'done', outcome = 'success') => {+ const calls = [{ id: `done_${nextCall++}`, function: { name, arguments: JSON.stringify({ outcome, summary: 'Here is the generated video.' }) } }];+ const messages = agent.conversations.get(tab);+ messages.push({ role: 'assistant', content: null, tool_calls: calls });+ return agent._executeToolBatch(tab, calls, messages, () => {}, { supportsVision: false }, null,+ new Set(['done', 'done_json']), 1, {}, null);+ };+ return { agent, tab, guard, batch };+}++test('a generated video with unavailable duration verification can finish after one note', async () => {+ for (const { build, Agent } of builds) {+ const { agent, tab, batch } = fixture(Agent);+ agent._resolveExactTableDone = async (_tab, args) => ({ done: true, summary: args.summary, outcome: args.outcome });+ agent.executeTool = (_tab, name, args) => agent._executeToolImpl(_tab, name, args, () => {});+ assert.equal((await batch()).action, 'continue', `${build}: first completion leaves its note`);+ const final = await batch();+ assert.equal(final.action, 'return', `${build}: verification timeout does not trap completion`);+ assert.equal(final.status, undefined, `${build}: missing evidence does not prove failure`);+ assert.match(final.value, /WebBrain could not verify everything:.*60-second duration could not be verified/);+ assert.equal(agent._guardRunState(tab).advised.has('done-video-duration'), true);+ }+});++for (const { build, Agent } of builds) {+ test(`${build}: duration quality guard applies strict checks and permits honest partial outcomes`, async () => {+ const { agent, tab, batch } = fixture(Agent, { strict: true });+ for (let i = 0; i < 3; i++) assert.equal((await batch()).action, 'continue');+ assert.equal(agent._guardRunState(tab).advised.has('done-video-duration'), false);+ for (const outcome of ['partial', 'failed']) {+ assert.equal(agent._videoDurationDoneBlock(tab, outcome), null);+ assert.equal((await batch('done', outcome)).action, 'return');+ }+ });++ test(`${build}: known short or unfinished videos finish as partial with their caveat`, async () => {+ for (const status of ['short', 'unjoined', 'failed', 'pending']) {+ const { agent, tab, batch } = fixture(Agent, { status });+ assert.equal((await batch()).action, 'continue');+ const final = await batch();+ assert.equal(final.action, 'return', status);+ assert.equal(final.endStatus, 'partial', status);+ assert.match(final.value, /WebBrain could not verify everything:/);+ assert.equal(agent._completionOutcomeDowngraded(tab), true);+ }+ });++ test(`${build}: batch skip, direct done and plain final share the same duration note`, async () => {+ const { agent, tab } = fixture(Agent);+ const firstBlock = agent._completionDoneBlock(tab, 'done', { outcome: 'success' });+ assert.equal(agent._completionInvariantRefuses(tab, firstBlock), true);+ assert.equal(agent._completionDoneBlock(tab, 'done', { outcome: 'success' }, null,+ { skip: new Set(['video_duration_incomplete']) }), null, 'accepted batch invariant is skipped');+ let reached = 0;+ agent._resolveExactTableDone = async () => { reached++; return { done: true, outcome: 'success' }; };+ const direct = await agent._executeToolImpl(tab, 'done', { outcome: 'success', summary: 'Video ready.' }, () => {});+ assert.equal(direct.done, true);+ assert.equal(reached, 1, 'the direct handler does not refuse the already-noted check');+ agent._guardRunState(tab).advised.add('plan-plain-final-protocol');+ assert.equal(agent._planOnlyTerminalDecision(tab, 'Here is the generated video.'), null);+ assert.match(agent._completionCaveatLine(tab), /60-second duration could not be verified/);+ assert.equal(agent._completionOutcomeDowngraded(tab), false);+ });++ test(`${build}: direct done gets one quality refusal and respects strict or lean modes`, async () => {+ for (const strict of [false, true]) {+ const { agent, tab } = fixture(Agent, { strict });+ agent._resolveExactTableDone = async () => ({ done: true, outcome: 'success' });+ assert.equal((await agent._executeToolImpl(tab, 'done', { outcome: 'success' }, () => {})).videoDurationIncomplete, true);+ const next = await agent._executeToolImpl(tab, 'done', { outcome: 'success' }, () => {});+ assert.equal(next.videoDurationIncomplete === true, strict);+ assert.equal(next.done === true, !strict);+ }+ const { agent, tab } = fixture(Agent, { strict: true });+ agent._leanRunActive = () => true;+ agent._resolveExactTableDone = async () => null;+ agent._leanChatReceiptBlock = async () => null;+ const lean = await agent._executeToolImpl(tab, 'done', { outcome: 'success', summary: 'Video ready.' }, () => {});+ assert.equal(lean.done, true, 'lean mode follows its existing quality-guard policy');+ assert.equal(agent._completionCaveatLine(tab), '');+ });++ test(`${build}: plain final gives one duration note while strict checks continue refusing`, () => {+ for (const strict of [false, true]) {+ const { agent, tab } = fixture(Agent, { strict });+ // Isolate duration from the separate plain-final protocol note.+ agent._guardRunState(tab).advised.add('plan-plain-final-protocol');+ assert.equal(agent._planOnlyTerminalDecision(tab, 'Here is the generated video.').retry, true);+ const second = agent._planOnlyTerminalDecision(tab, 'Here is the generated video.');+ assert.equal(second?.retry === true, strict);+ if (!strict) {+ assert.equal(second, null);+ assert.match(agent._completionCaveatLine(tab), /60-second duration could not be verified/);+ assert.equal(agent._completionOutcomeDowngraded(tab), false);+ }+ }+ });++ test(`${build}: structured done completion carries a duration caveat after one note`, async () => {+ const { batch } = fixture(Agent);+ assert.equal((await batch('done_json')).action, 'continue');+ const final = await batch('done_json');+ assert.equal(final.action, 'return');+ assert.match(final.value, /60-second duration could not be verified/);+ });+}