Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Preserve video duration targets and require measured completion

Codex release-state testa1a28565f51b
diff --git a/package.json b/package.json
index 828642c6f..526533e56 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": "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: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.md
index f870aebad..0dd0584bc 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.
+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.
## The shared prompt
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index f0bb7f210..b9438885b 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -7,6 +7,7 @@ import { DECISION_SETTINGS_KEYS, decisionFeatureEnabled } from './decision-confi
import { 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 { 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';
@@ -1189,7 +1190,7 @@ export class Agent extends LoopDetector {
this._pendingPlans = new Map(); // tabId → (planId → { resolve, ts })
this._planExecutionGuards = new Map(); // tabId → current run's plan-only terminal recovery state
this._unverifiedStepScopes = new Map(); // tabId → { used: Set } 'once' answers the current tool call relies on
- this._continuationExecutionEvidence = new Map(); // tabId → app-owned evidence carried only by continueProcessing()
+ this._continuationExecutionEvidence = new Map(); // tabId → app-owned evidence and pending media obligations carried only by continueProcessing()
this._continuationResponseLanguagePolicies = new Map(); // tabId -> trusted policy carried only by continueProcessing()
// Stale click detection: per-tab last clicked element identity.
this._lastCdpClickIdent = new Map(); // tabId -> string
@@ -2855,10 +2856,23 @@ export class Agent extends LoopDetector {
pageUrl, currentUrl: submit.currentUrl }, text => this._sha256TextSync(text));
}
+ _currentVideoDurationGuard(tabId) {
+ const guard = this._planExecutionGuards.get(tabId);
+ return guard?.enabled && !guard.taskDrifted
+ && guard.taskKey === this._progressTaskKeyHash(tabId)
+ && guard.conversationId === this.conversationIds.get(tabId) ? guard : null;
+ }
+
+ _videoDurationDoneBlock(tabId, outcome = 'success') {
+ return videoDurationCompletionBlock(this._currentVideoDurationGuard(tabId)?.videoDurationContract, outcome);
+ }
+
_completionDoneBlock(tabId, name, args, batchStartState = null, { skip = null } = {}) {
const mode = this._effectiveRunMode(tabId);
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;
const toolbarObligation = this._richTextToolbarGuard.completionAction(tabId);
if (outcome === 'success' && toolbarObligation && skip?.has?.('rich_text_toolbar_target_unresolved') !== true) {
return {
@@ -21622,6 +21636,23 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
this._restorePersistedRichTextToolbarAudit(tabId, entry.richTextToolbarAudit);
this._restoreTaskAttemptLedger(tabId, entry.taskAttemptLedger, attemptStored?.[attemptKey]);
+ const savedVideoObligation = entry.videoDurationObligation;
+ const videoDurationContract = normalizeVideoDurationContract(savedVideoObligation?.contract);
+ if (videoDurationContract && typeof entry.conversationId === 'string' && entry.conversationId
+ && entry.conversationId.length <= 256
+ && savedVideoObligation.conversationId === entry.conversationId
+ && typeof savedVideoObligation.taskKey === 'string' && savedVideoObligation.taskKey
+ && savedVideoObligation.taskKey.length <= 128
+ && savedVideoObligation.taskKey === this._progressTaskKeyHash(tabId)) {
+ // Restore only this app-owned obligation. Serialized tool messages and
+ // scratchpad text never become successful execution evidence.
+ this._continuationExecutionEvidence.set(tabId, {
+ requestKind: 'execute',
+ taskKey: savedVideoObligation.taskKey,
+ conversationId: entry.conversationId,
+ videoDurationContract,
+ });
+ }
const savedDispatch = entry.nativeCaptchaDispatch;
const rootDocument = Array.isArray(savedDispatch?.documents)
? savedDispatch.documents.find(document => document?.frameId === 0) : null;
@@ -21691,6 +21722,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
} catch (e) { /* session storage may be unavailable */ }
}
+ _persistedVideoDurationObligation(tabId) {
+ const guard = this._planExecutionGuards.get(tabId);
+ const source = guard || this._continuationExecutionEvidence.get(tabId);
+ const conversationId = this.conversationIds.get(tabId);
+ if ((guard && (!guard.enabled || guard.taskDrifted)) || source?.requestKind !== 'execute'
+ || typeof conversationId !== 'string' || !conversationId || conversationId.length > 256
+ || source.conversationId !== conversationId
+ || typeof source.taskKey !== 'string' || !source.taskKey || source.taskKey.length > 128
+ || source.taskKey !== this._progressTaskKeyHash(tabId)) return null;
+ const contract = normalizeVideoDurationContract(source.videoDurationContract);
+ return contract ? { taskKey: source.taskKey, conversationId, contract } : null;
+ }
+
_conversationStorageEntry(tabId, options = {}) {
const messages = foldBrowserBatchMessages(this.conversations.get(tabId), { repairPending: true });
const chatWorkflow = this.chatSessions.get(tabId);
@@ -21755,6 +21799,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
sessionSnapshotBytes: serialized.bytes,
conversationId,
chatWorkflow: chatWorkflow ? serializeChatSession(chatWorkflow) : null,
+ videoDurationObligation: this._persistedVideoDurationObligation(tabId),
// A bounded receipt records the task/thread/body digest and latest send
// proved by done. Hydration never turns this receipt into authorization.
nativeChatDeliveryEvidence: this._planExecutionGuards.get(tabId)?.nativeChatDeliveryEvidence
@@ -37309,6 +37354,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
this._standaloneWebgpuRunTabs.delete(tabId);
this._tinyAskRunTabs.delete(tabId);
this._continuationResponseLanguagePolicies.delete(tabId);
+ this._continuationExecutionEvidence.delete(tabId);
+ const clearedExecutionGuard = this._planExecutionGuards.get(tabId);
+ if (clearedExecutionGuard) clearedExecutionGuard.videoDurationContract = null;
this.mastodonStates.delete(tabId);
this.lastAutoScreenshotTs.delete(tabId);
this.autoScreenshotBursts.delete(tabId);
@@ -41252,6 +41300,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
this._progressTaskAnchorText(tabId) || this._latestTaskText(tabId) || this._originalTaskText(tabId),
).slice(0, 1600);
const taskKey = this._progressTaskKeyHash(tabId);
+ const conversationId = this.conversationIds.get(tabId) || null;
const gateApprovedPlanAnchor = this._approvedExecutionPlanAnchor(gateOutcome?.approvedScratchpadText);
const carried = runOptions?.trustedContinuation === true
? this._continuationExecutionEvidence.get(tabId)
@@ -41271,6 +41320,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
&& JSON.stringify(carried.conditionalSiteWorkflow || null) === JSON.stringify(gateOutcome.conditionalSiteWorkflow || null)
&& carried.conversationId === (this.conversationIds.get(tabId) || null);
const carryScopeMatches = carryScopeCompatible && carried.taskKey === taskKey;
+ // A failed first media call still owes its duration. This obligation is
+ // task/conversation scoped, independent of success counters and a later
+ // planner changing unrelated mutation/submission flags on Continue.
+ const carriedVideoDurationContract = enabled && conversationId
+ && carried?.requestKind === 'execute'
+ && carried.taskKey === taskKey && carried.conversationId === conversationId
+ ? normalizeVideoDurationContract(carried.videoDurationContract) : null;
const carrySocialRetryAuthorization = carryScopeCompatible
&& requiresSubmission === true && requiresStateChange === true
&& carried.socialPublication?.retryAuthorizationTaskKey === taskKey
@@ -41323,6 +41379,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
conditionalSiteWorkflow: gateOutcome.conditionalSiteWorkflow || null,
conditionalMutationBlocked: carryMatches && carried.conditionalMutationBlocked === true,
taskKey,
+ conversationId,
+ videoDurationContract: carriedVideoDurationContract,
taskText,
approvedPlanAnchor,
approvedPlanText,
@@ -41833,11 +41891,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_storeContinuationExecutionEvidence(tabId) {
const guard = this._planExecutionGuards.get(tabId);
+ const videoDurationContract = this._persistedVideoDurationObligation(tabId)?.contract || null;
const hasRecipientAuthorization = guard?.requiresSubmission === true && guard?.requiresStateChange === true
&& !!normalizeMessageTarget(guard.messaging)
&& (guard.messageRecipientApprovedAll === true || (guard.approvedRecipients || []).length > 0);
if (guard?.enabled && !guard.taskDrifted && (
hasRecipientAuthorization
+ || !!videoDurationContract
|| guard.socialPublication?.contract?.status === 'ready'
|| guard.multipleDeliverables === true
|| (guard.deliverables || []).length > 1
@@ -41863,6 +41923,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const submit = this._completionSubmitStates.get(tabId);
this._continuationExecutionEvidence.set(tabId, {
requestKind: guard.requestKind,
+ videoDurationContract,
requiresStateChange: guard.requiresStateChange,
requiresSubmission: guard.requiresSubmission,
messaging: guard.messaging,
@@ -42136,6 +42197,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_planOnlyTerminalDecision(tabId, content, { viaDone = false, outcome = null, summaryKind = null } = {}) {
const 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.'}` };
+ }
// The bracketed diagnostic stays in tool results and traces; the chat
// shows a plain-language account of what happened.
const terminal = (kind, failure, status, facts = {}) => ({ failure, status,
@@ -47789,6 +47864,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
if (name === 'done') {
+ const videoBlock = this._videoDurationDoneBlock(tabId, normalizeDoneOutcome(args?.outcome));
+ if (videoBlock) return videoBlock;
const doneStartedAt = Date.now();
const tableBlock = await this._resolveExactTableDone(tabId, args, onUpdate);
if (tableBlock) return tableBlock;
@@ -48211,7 +48288,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// messages are only ever the agent's own readings, never page data.
// By default the incomplete-task judge redirects an honest partial or
// failed ending once per run; later endings are not judged again.
- const incompleteBlock = this._guardHasAdvised(tabId, 'incomplete-task-redirect') ? null : await incompleteCompletionBlock(this, tabId, {
+ // 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 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 || '',
});
@@ -48447,12 +48527,46 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return await fetchUrl(args.url, args, { tabId, signal: executionContext?._contentActionAbortSignal });
}
if (name === 'generate_image') {
- return await generateImage(args, {
+ const guard = this._currentVideoDurationGuard(tabId);
+ const pinned = pinVideoDurationTarget(args, guard?.videoDurationContract);
+ const trackVideo = pinned.args?.media === 'video' && guard && pinned.contract;
+ if (trackVideo) {
+ // Save before dispatch, including when the first attempt will fail
+ // validation. Otherwise a worker restart can lose the user's target.
+ guard.videoDurationContract = pinned.contract;
+ const saved = await this._persistNow(tabId);
+ if (saved !== true && saved?.ok !== true) return { success: false, dispatched: false,
+ error: 'The requested video duration could not be saved. Retry when session storage is available; no video was submitted.' };
+ if (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.' };
+ }
+ const result = await generateImage(trackVideo ? pinned.args : args, {
signal: executionContext?._contentActionAbortSignal,
...(executionContext?.imageGenConfig ? { config: executionContext.imageGenConfig } : {}),
+ verifyVideoDuration: !!trackVideo,
onProgress: message => onUpdate?.('tool_progress', { name, message }),
- confirmVideoLength: details => this._promptVideoLengthConfirmation(tabId, details, onUpdate),
+ confirmVideoLength: async details => {
+ const answer = await this._promptVideoLengthConfirmation(tabId, details, onUpdate);
+ if (answer !== 'once' || !trackVideo) return answer;
+ if (this._currentVideoDurationGuard(tabId) !== guard || this._hasPendingSteering(tabId)
+ || 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 });
+ 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);
+ // 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 {}
+ }
+ return result;
}
if (name === 'read_page_source') {
return await readPageSource(args.url, args, { tabId, signal: executionContext?._contentActionAbortSignal });
@@ -55570,7 +55684,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (previousTinyAskRun) this._tinyAskRunTabs.add(tabId);
else this._tinyAskRunTabs.delete(tabId);
this.responseLanguagePolicies.delete(tabId);
- if (continuationResponseLanguagePolicyStored) {
+ if (continuationResponseLanguagePolicyStored || this._persistedVideoDurationObligation(tabId)) {
try { await this._persistNow(tabId); } catch {}
}
this._resetActiveSkillsForRun(tabId);
@@ -57609,7 +57723,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (previousTinyAskRun) this._tinyAskRunTabs.add(tabId);
else this._tinyAskRunTabs.delete(tabId);
this.responseLanguagePolicies.delete(tabId);
- if (continuationResponseLanguagePolicyStored) {
+ if (continuationResponseLanguagePolicyStored || this._persistedVideoDurationObligation(tabId)) {
try { await this._persistNow(tabId); } catch {}
}
this._resetActiveSkillsForRun(tabId);
diff --git a/src/chrome/src/agent/generative-media.js b/src/chrome/src/agent/generative-media.js
index b728e3a61..fee1ff678 100644
--- a/src/chrome/src/agent/generative-media.js
+++ b/src/chrome/src/agent/generative-media.js
@@ -557,10 +557,12 @@ async function runVideo({ model, entry, text, video, signal, onProgress, confirm
// generating, before anything is submitted. The card is outside the
// generation deadline.
const lengthDetails = { estimatedSeconds: plan.estimated, totalSeconds: plan.total, clipCount: plan.clips.length, maxSeconds: MAX_VIDEO_SECONDS };
+ let approvedDurationSeconds;
if (plan.estimated > MAX_VIDEO_SECONDS) {
const answer = typeof confirmVideoLength === 'function' ? await confirmVideoLength(lengthDetails) : 'unavailable';
signal?.throwIfAborted?.();
if (answer !== 'once') return { declined: true, answer: answer || 'cancelled', model, ...lengthDetails };
+ approvedDurationSeconds = plan.total;
}
onProgress?.(`Generating ${plan.clips.length} video clips…`);
let ready = 0;
@@ -583,7 +585,8 @@ async function runVideo({ model, entry, text, video, signal, onProgress, confirm
}
}
});
- return { clips, model, ...lengthDetails, ...fallback };
+ return { clips, model, ...lengthDetails, ...fallback,
+ ...(approvedDurationSeconds !== undefined ? { approvedDurationSeconds } : {}) };
}
async function runOpenRouter({ text, media, config, video, fetchImpl, signal, timeoutMs, pollMs, onProgress, confirmVideoLength }) {
@@ -820,17 +823,21 @@ const roundSeconds = seconds => (Number.isFinite(seconds) && seconds > 0 ? Math.
function videoDeliveryDetails(result, asset) {
const plannedSeconds = roundSeconds(result.totalSeconds);
if (!plannedSeconds) return {};
+ const estimatedSeconds = roundSeconds(result.estimatedSeconds) || plannedSeconds;
+ const approvedDurationSeconds = roundSeconds(result.approvedDurationSeconds);
+ const requiredSeconds = approvedDurationSeconds || estimatedSeconds;
const actual = asset.durationSeconds;
// Allow a quarter second for container/frame boundary rounding.
const durationStatus = Number.isFinite(actual)
- ? (actual + 0.25 < plannedSeconds ? 'short' : 'enough') : 'unverified';
+ ? (actual + 0.25 < requiredSeconds ? 'short' : 'enough') : 'unverified';
return {
- estimatedSeconds: roundSeconds(result.estimatedSeconds) || plannedSeconds,
+ estimatedSeconds,
plannedSeconds,
+ ...(approvedDurationSeconds ? { approvedDurationSeconds } : {}),
...(result.fallbackClipSeconds ? { fallbackClipSeconds: result.fallbackClipSeconds } : {}),
durationStatus,
...(durationStatus === 'short' ? {
- notice: `Generated ${actual} s of the planned ${plannedSeconds} 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 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.',
} : {}),
@@ -875,7 +882,9 @@ async function storeVideoClips(clips, onProgress) {
* The generate_image tool. `options.onProgress(message)` reports progress of
* a video made of scenes, and `options.confirmVideoLength(details)` asks the
* user when the requested video is longer than WebBrain makes ('once' to go
- * ahead; 'deny', 'unavailable' or null otherwise).
+ * 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.
*/
export async function generateImage(args, options = {}) {
const fetchImpl = typeof options === 'function' ? options : options.fetchImpl || fetch;
@@ -903,7 +912,25 @@ export async function generateImage(args, options = {}) {
return { success: true, model: result.model, provider, ...delivered, ...videoDeliveryDetails(result, delivered) };
}
const { estimatedSeconds, totalSeconds, ...hosted } = result;
- return { success: true, ...hosted, provider, ...videoDeliveryDetails(result, hosted) };
+ let verificationFailed = false;
+ if (options.verifyVideoDuration === true && roundSeconds(totalSeconds)) {
+ try {
+ options.signal?.throwIfAborted?.();
+ const bytes = await downloadClip(hosted.url, { fetchImpl, signal: options.signal,
+ timeoutMs: 15000, label: 'Generated video duration verification' });
+ hosted.durationSeconds = roundSeconds(mp4DurationSeconds(bytes));
+ hosted.mimeType = 'video/mp4';
+ } catch {
+ // Generation already succeeded. A failed verification must not hide
+ // the paid asset or turn into another generation request.
+ verificationFailed = true;
+ }
+ }
+ const delivery = videoDeliveryDetails(result, hosted);
+ 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.';
+ }
+ return { success: true, ...hosted, provider, ...delivery };
} catch (error) { return { success: false, error: error.message }; }
}
diff --git a/src/chrome/src/agent/tools.js b/src/chrome/src/agent/tools.js
index 8bb830a47..c521a9e05 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 joined:false result as incomplete, preserving the generated assets.`;
+- 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.`;
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.
diff --git a/src/chrome/src/agent/video-duration-contract.js b/src/chrome/src/agent/video-duration-contract.js
new file mode 100644
index 000000000..a09b8f16c
--- /dev/null
+++ b/src/chrome/src/agent/video-duration-contract.js
@@ -0,0 +1,67 @@
+// 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.
+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 TOLERANCE_SECONDS = 0.25;
+
+export function normalizeVideoDurationContract(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 (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 videoDurationRequiredSeconds(contract) {
+ return contract.approvedDurationSeconds || contract.requestedSeconds;
+}
+
+export function pinVideoDurationTarget(args, previous) {
+ const prior = normalizeVideoDurationContract(previous);
+ const video = args?.media === 'video' || (args?.media == null && ((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 };
+}
+
+export function recordVideoDurationResult(previous, result) {
+ 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' };
+}
+
+export function videoDurationCompletionBlock(value, outcome = 'success') {
+ const contract = normalizeVideoDurationContract(value);
+ if (!contract || outcome !== 'success' || contract.status === 'enough') 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.'
+ : contract.status === 'unverified' ? 'The delivered video duration could not be verified.'
+ : '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.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.` };
+}
diff --git a/src/chrome/src/run-ui-journal.js b/src/chrome/src/run-ui-journal.js
index a25bd48d5..bad006eb6 100644
--- a/src/chrome/src/run-ui-journal.js
+++ b/src/chrome/src/run-ui-journal.js
@@ -101,7 +101,7 @@ function compactRunUiToolResult(result, name) {
if (typeof source[key] === 'string') compacted[key] = source[key];
}
if (source.inlineMedia === true) compacted.inlineMedia = true;
- for (const key of ['durationSeconds', 'clipCount', 'estimatedSeconds', 'plannedSeconds', 'fallbackClipSeconds']) {
+ for (const key of ['durationSeconds', 'clipCount', 'estimatedSeconds', 'plannedSeconds', 'approvedDurationSeconds', 'fallbackClipSeconds']) {
if (Number.isFinite(source[key])) compacted[key] = source[key];
}
if (['short', 'enough', 'unverified'].includes(source.durationStatus)) compacted.durationStatus = source.durationStatus;
diff --git a/src/firefox/skills/video-scenes.md b/src/firefox/skills/video-scenes.md
index f870aebad..0dd0584bc 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.
+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.
## The shared prompt
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index dc8d55654..0e9c7b940 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -5,6 +5,7 @@ import { DECISION_SETTINGS_KEYS, decisionFeatureEnabled } from './decision-confi
import { 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 { 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';
@@ -845,7 +846,7 @@ export class Agent extends LoopDetector {
this._pendingPlans = new Map();
this._planExecutionGuards = new Map(); // tabId → current run's plan-only terminal recovery state
this._unverifiedStepScopes = new Map(); // tabId → { used: Set } 'once' answers the current tool call relies on
- this._continuationExecutionEvidence = new Map(); // tabId → app-owned evidence carried only by continueProcessing()
+ this._continuationExecutionEvidence = new Map(); // tabId → app-owned evidence and pending media obligations carried only by continueProcessing()
this._continuationResponseLanguagePolicies = new Map(); // tabId -> trusted policy carried only by continueProcessing()
// Strict secret-handling mode — see chrome/agent.js for rationale.
// Defaults to true at extension runtime via background.js and Settings.
@@ -2408,10 +2409,23 @@ export class Agent extends LoopDetector {
pageUrl, currentUrl: submit.currentUrl }, text => this._sha256TextSync(text));
}
+ _currentVideoDurationGuard(tabId) {
+ const guard = this._planExecutionGuards.get(tabId);
+ return guard?.enabled && !guard.taskDrifted
+ && guard.taskKey === this._progressTaskKeyHash(tabId)
+ && guard.conversationId === this.conversationIds.get(tabId) ? guard : null;
+ }
+
+ _videoDurationDoneBlock(tabId, outcome = 'success') {
+ return videoDurationCompletionBlock(this._currentVideoDurationGuard(tabId)?.videoDurationContract, outcome);
+ }
+
_completionDoneBlock(tabId, name, args, batchStartState = null, { skip = null } = {}) {
const mode = this._effectiveRunMode(tabId);
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;
const toolbarObligation = this._richTextToolbarGuard.completionAction(tabId);
if (outcome === 'success' && toolbarObligation && skip?.has?.('rich_text_toolbar_target_unresolved') !== true) {
return {
@@ -7738,6 +7752,23 @@ export class Agent extends LoopDetector {
}
this._restorePersistedRichTextToolbarAudit(tabId, entry.richTextToolbarAudit);
this._restoreTaskAttemptLedger(tabId, entry.taskAttemptLedger, attemptStored?.[attemptKey]);
+ const savedVideoObligation = entry.videoDurationObligation;
+ const videoDurationContract = normalizeVideoDurationContract(savedVideoObligation?.contract);
+ if (videoDurationContract && typeof entry.conversationId === 'string' && entry.conversationId
+ && entry.conversationId.length <= 256
+ && savedVideoObligation.conversationId === entry.conversationId
+ && typeof savedVideoObligation.taskKey === 'string' && savedVideoObligation.taskKey
+ && savedVideoObligation.taskKey.length <= 128
+ && savedVideoObligation.taskKey === this._progressTaskKeyHash(tabId)) {
+ // Restore only this app-owned obligation. Serialized tool messages and
+ // scratchpad text never become successful execution evidence.
+ this._continuationExecutionEvidence.set(tabId, {
+ requestKind: 'execute',
+ taskKey: savedVideoObligation.taskKey,
+ conversationId: entry.conversationId,
+ videoDurationContract,
+ });
+ }
const savedDispatch = entry.nativeCaptchaDispatch;
const rootDocument = Array.isArray(savedDispatch?.documents)
? savedDispatch.documents.find(document => document?.frameId === 0) : null;
@@ -7808,6 +7839,19 @@ export class Agent extends LoopDetector {
} catch (e) { /* session storage may be unavailable */ }
}
+ _persistedVideoDurationObligation(tabId) {
+ const guard = this._planExecutionGuards.get(tabId);
+ const source = guard || this._continuationExecutionEvidence.get(tabId);
+ const conversationId = this.conversationIds.get(tabId);
+ if ((guard && (!guard.enabled || guard.taskDrifted)) || source?.requestKind !== 'execute'
+ || typeof conversationId !== 'string' || !conversationId || conversationId.length > 256
+ || source.conversationId !== conversationId
+ || typeof source.taskKey !== 'string' || !source.taskKey || source.taskKey.length > 128
+ || source.taskKey !== this._progressTaskKeyHash(tabId)) return null;
+ const contract = normalizeVideoDurationContract(source.videoDurationContract);
+ return contract ? { taskKey: source.taskKey, conversationId, contract } : null;
+ }
+
_conversationStorageEntry(tabId, options = {}) {
const messages = foldBrowserBatchMessages(this.conversations.get(tabId), { repairPending: true });
const chatWorkflow = this.chatSessions.get(tabId);
@@ -7872,6 +7916,7 @@ export class Agent extends LoopDetector {
sessionSnapshotBytes: serialized.bytes,
conversationId,
chatWorkflow: chatWorkflow ? serializeChatSession(chatWorkflow) : null,
+ videoDurationObligation: this._persistedVideoDurationObligation(tabId),
// A bounded receipt records the task/thread/body digest and latest send
// proved by done. Hydration never turns this receipt into authorization.
nativeChatDeliveryEvidence: this._planExecutionGuards.get(tabId)?.nativeChatDeliveryEvidence
@@ -33962,6 +34007,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
this.selectionGroundingRestorationPendingTabs.delete(tabId);
this.responseLanguagePolicies.delete(tabId);
this._continuationResponseLanguagePolicies.delete(tabId);
+ this._continuationExecutionEvidence.delete(tabId);
+ const clearedExecutionGuard = this._planExecutionGuards.get(tabId);
+ if (clearedExecutionGuard) clearedExecutionGuard.videoDurationContract = null;
this.mastodonStates.delete(tabId);
this.conversationModes.delete(tabId);
this.conversationIds.delete(tabId);
@@ -37919,6 +37967,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
this._progressTaskAnchorText(tabId) || this._latestTaskText(tabId) || this._originalTaskText(tabId),
).slice(0, 1600);
const taskKey = this._progressTaskKeyHash(tabId);
+ const conversationId = this.conversationIds.get(tabId) || null;
const gateApprovedPlanAnchor = this._approvedExecutionPlanAnchor(gateOutcome?.approvedScratchpadText);
const carried = runOptions?.trustedContinuation === true
? this._continuationExecutionEvidence.get(tabId)
@@ -37938,6 +37987,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
&& JSON.stringify(carried.conditionalSiteWorkflow || null) === JSON.stringify(gateOutcome.conditionalSiteWorkflow || null)
&& carried.conversationId === (this.conversationIds.get(tabId) || null);
const carryScopeMatches = carryScopeCompatible && carried.taskKey === taskKey;
+ // A failed first media call still owes its duration. This obligation is
+ // task/conversation scoped, independent of success counters and a later
+ // planner changing unrelated mutation/submission flags on Continue.
+ const carriedVideoDurationContract = enabled && conversationId
+ && carried?.requestKind === 'execute'
+ && carried.taskKey === taskKey && carried.conversationId === conversationId
+ ? normalizeVideoDurationContract(carried.videoDurationContract) : null;
const carrySocialRetryAuthorization = carryScopeCompatible
&& requiresSubmission === true && requiresStateChange === true
&& carried.socialPublication?.retryAuthorizationTaskKey === taskKey
@@ -37990,6 +38046,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
conditionalSiteWorkflow: gateOutcome.conditionalSiteWorkflow || null,
conditionalMutationBlocked: carryMatches && carried.conditionalMutationBlocked === true,
taskKey,
+ conversationId,
+ videoDurationContract: carriedVideoDurationContract,
taskText,
approvedPlanAnchor,
approvedPlanText,
@@ -38500,11 +38558,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_storeContinuationExecutionEvidence(tabId) {
const guard = this._planExecutionGuards.get(tabId);
+ const videoDurationContract = this._persistedVideoDurationObligation(tabId)?.contract || null;
const hasRecipientAuthorization = guard?.requiresSubmission === true && guard?.requiresStateChange === true
&& !!normalizeMessageTarget(guard.messaging)
&& (guard.messageRecipientApprovedAll === true || (guard.approvedRecipients || []).length > 0);
if (guard?.enabled && !guard.taskDrifted && (
hasRecipientAuthorization
+ || !!videoDurationContract
|| guard.socialPublication?.contract?.status === 'ready'
|| guard.multipleDeliverables === true
|| (guard.deliverables || []).length > 1
@@ -38530,6 +38590,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const submit = this._completionSubmitStates.get(tabId);
this._continuationExecutionEvidence.set(tabId, {
requestKind: guard.requestKind,
+ videoDurationContract,
requiresStateChange: guard.requiresStateChange,
requiresSubmission: guard.requiresSubmission,
messaging: guard.messaging,
@@ -38803,6 +38864,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_planOnlyTerminalDecision(tabId, content, { viaDone = false, outcome = null, summaryKind = null } = {}) {
const 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.'}` };
+ }
// The bracketed diagnostic stays in tool results and traces; the chat
// shows a plain-language account of what happened.
const terminal = (kind, failure, status, facts = {}) => ({ failure, status,
@@ -43171,6 +43246,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
if (name === 'done') {
+ const videoBlock = this._videoDurationDoneBlock(tabId, normalizeDoneOutcome(args?.outcome));
+ if (videoBlock) return videoBlock;
const doneStartedAt = Date.now();
const tableBlock = await this._resolveExactTableDone(tabId, args, onUpdate);
if (tableBlock) return tableBlock;
@@ -43561,7 +43638,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// messages are only ever the agent's own readings, never page data.
// By default the incomplete-task judge redirects an honest partial or
// failed ending once per run; later endings are not judged again.
- const incompleteBlock = this._guardHasAdvised(tabId, 'incomplete-task-redirect') ? null : await incompleteCompletionBlock(this, tabId, {
+ // 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 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 || '',
});
@@ -43787,12 +43867,46 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return await this._restrictedDomainScreenshotFallback(tabId, name, args.url, result);
}
if (name === 'generate_image') {
- return await generateImage(args, {
+ const guard = this._currentVideoDurationGuard(tabId);
+ const pinned = pinVideoDurationTarget(args, guard?.videoDurationContract);
+ const trackVideo = pinned.args?.media === 'video' && guard && pinned.contract;
+ if (trackVideo) {
+ // Save before dispatch, including when the first attempt will fail
+ // validation. Otherwise a worker restart can lose the user's target.
+ guard.videoDurationContract = pinned.contract;
+ const saved = await this._persistNow(tabId);
+ if (saved !== true && saved?.ok !== true) return { success: false, dispatched: false,
+ error: 'The requested video duration could not be saved. Retry when session storage is available; no video was submitted.' };
+ if (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.' };
+ }
+ const result = await generateImage(trackVideo ? pinned.args : args, {
signal: executionContext?._contentActionAbortSignal,
...(executionContext?.imageGenConfig ? { config: executionContext.imageGenConfig } : {}),
+ verifyVideoDuration: !!trackVideo,
onProgress: message => onUpdate?.('tool_progress', { name, message }),
- confirmVideoLength: details => this._promptVideoLengthConfirmation(tabId, details, onUpdate),
+ confirmVideoLength: async details => {
+ const answer = await this._promptVideoLengthConfirmation(tabId, details, onUpdate);
+ if (answer !== 'once' || !trackVideo) return answer;
+ if (this._currentVideoDurationGuard(tabId) !== guard || this._hasPendingSteering(tabId)
+ || 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 });
+ 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);
+ // 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 {}
+ }
+ return result;
}
if (name === 'read_page_source') {
const result = await readPageSource(args.url, args, { tabId, signal: executionContext?._contentActionAbortSignal });
@@ -47032,7 +47146,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (previousStandaloneChatRun) this._standaloneChatRunTabs.add(tabId);
else this._standaloneChatRunTabs.delete(tabId);
this.responseLanguagePolicies.delete(tabId);
- if (continuationResponseLanguagePolicyStored) {
+ if (continuationResponseLanguagePolicyStored || this._persistedVideoDurationObligation(tabId)) {
try { await this._persistNow(tabId); } catch {}
}
this._resetActiveSkillsForRun(tabId);
@@ -48864,7 +48978,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (previousStandaloneChatRun) this._standaloneChatRunTabs.add(tabId);
else this._standaloneChatRunTabs.delete(tabId);
this.responseLanguagePolicies.delete(tabId);
- if (continuationResponseLanguagePolicyStored) {
+ if (continuationResponseLanguagePolicyStored || this._persistedVideoDurationObligation(tabId)) {
try { await this._persistNow(tabId); } catch {}
}
this._resetActiveSkillsForRun(tabId);
diff --git a/src/firefox/src/agent/generative-media.js b/src/firefox/src/agent/generative-media.js
index b728e3a61..fee1ff678 100644
--- a/src/firefox/src/agent/generative-media.js
+++ b/src/firefox/src/agent/generative-media.js
@@ -557,10 +557,12 @@ async function runVideo({ model, entry, text, video, signal, onProgress, confirm
// generating, before anything is submitted. The card is outside the
// generation deadline.
const lengthDetails = { estimatedSeconds: plan.estimated, totalSeconds: plan.total, clipCount: plan.clips.length, maxSeconds: MAX_VIDEO_SECONDS };
+ let approvedDurationSeconds;
if (plan.estimated > MAX_VIDEO_SECONDS) {
const answer = typeof confirmVideoLength === 'function' ? await confirmVideoLength(lengthDetails) : 'unavailable';
signal?.throwIfAborted?.();
if (answer !== 'once') return { declined: true, answer: answer || 'cancelled', model, ...lengthDetails };
+ approvedDurationSeconds = plan.total;
}
onProgress?.(`Generating ${plan.clips.length} video clips…`);
let ready = 0;
@@ -583,7 +585,8 @@ async function runVideo({ model, entry, text, video, signal, onProgress, confirm
}
}
});
- return { clips, model, ...lengthDetails, ...fallback };
+ return { clips, model, ...lengthDetails, ...fallback,
+ ...(approvedDurationSeconds !== undefined ? { approvedDurationSeconds } : {}) };
}
async function runOpenRouter({ text, media, config, video, fetchImpl, signal, timeoutMs, pollMs, onProgress, confirmVideoLength }) {
@@ -820,17 +823,21 @@ const roundSeconds = seconds => (Number.isFinite(seconds) && seconds > 0 ? Math.
function videoDeliveryDetails(result, asset) {
const plannedSeconds = roundSeconds(result.totalSeconds);
if (!plannedSeconds) return {};
+ const estimatedSeconds = roundSeconds(result.estimatedSeconds) || plannedSeconds;
+ const approvedDurationSeconds = roundSeconds(result.approvedDurationSeconds);
+ const requiredSeconds = approvedDurationSeconds || estimatedSeconds;
const actual = asset.durationSeconds;
// Allow a quarter second for container/frame boundary rounding.
const durationStatus = Number.isFinite(actual)
- ? (actual + 0.25 < plannedSeconds ? 'short' : 'enough') : 'unverified';
+ ? (actual + 0.25 < requiredSeconds ? 'short' : 'enough') : 'unverified';
return {
- estimatedSeconds: roundSeconds(result.estimatedSeconds) || plannedSeconds,
+ estimatedSeconds,
plannedSeconds,
+ ...(approvedDurationSeconds ? { approvedDurationSeconds } : {}),
...(result.fallbackClipSeconds ? { fallbackClipSeconds: result.fallbackClipSeconds } : {}),
durationStatus,
...(durationStatus === 'short' ? {
- notice: `Generated ${actual} s of the planned ${plannedSeconds} 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 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.',
} : {}),
@@ -875,7 +882,9 @@ async function storeVideoClips(clips, onProgress) {
* The generate_image tool. `options.onProgress(message)` reports progress of
* a video made of scenes, and `options.confirmVideoLength(details)` asks the
* user when the requested video is longer than WebBrain makes ('once' to go
- * ahead; 'deny', 'unavailable' or null otherwise).
+ * 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.
*/
export async function generateImage(args, options = {}) {
const fetchImpl = typeof options === 'function' ? options : options.fetchImpl || fetch;
@@ -903,7 +912,25 @@ export async function generateImage(args, options = {}) {
return { success: true, model: result.model, provider, ...delivered, ...videoDeliveryDetails(result, delivered) };
}
const { estimatedSeconds, totalSeconds, ...hosted } = result;
- return { success: true, ...hosted, provider, ...videoDeliveryDetails(result, hosted) };
+ let verificationFailed = false;
+ if (options.verifyVideoDuration === true && roundSeconds(totalSeconds)) {
+ try {
+ options.signal?.throwIfAborted?.();
+ const bytes = await downloadClip(hosted.url, { fetchImpl, signal: options.signal,
+ timeoutMs: 15000, label: 'Generated video duration verification' });
+ hosted.durationSeconds = roundSeconds(mp4DurationSeconds(bytes));
+ hosted.mimeType = 'video/mp4';
+ } catch {
+ // Generation already succeeded. A failed verification must not hide
+ // the paid asset or turn into another generation request.
+ verificationFailed = true;
+ }
+ }
+ const delivery = videoDeliveryDetails(result, hosted);
+ 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.';
+ }
+ return { success: true, ...hosted, provider, ...delivery };
} catch (error) { return { success: false, error: error.message }; }
}
diff --git a/src/firefox/src/agent/tools.js b/src/firefox/src/agent/tools.js
index 4a077af90..666e12988 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 joined:false result as incomplete, preserving the generated assets.`;
+- 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.`;
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.
diff --git a/src/firefox/src/agent/video-duration-contract.js b/src/firefox/src/agent/video-duration-contract.js
new file mode 100644
index 000000000..a09b8f16c
--- /dev/null
+++ b/src/firefox/src/agent/video-duration-contract.js
@@ -0,0 +1,67 @@
+// 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.
+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 TOLERANCE_SECONDS = 0.25;
+
+export function normalizeVideoDurationContract(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 (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 videoDurationRequiredSeconds(contract) {
+ return contract.approvedDurationSeconds || contract.requestedSeconds;
+}
+
+export function pinVideoDurationTarget(args, previous) {
+ const prior = normalizeVideoDurationContract(previous);
+ const video = args?.media === 'video' || (args?.media == null && ((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 };
+}
+
+export function recordVideoDurationResult(previous, result) {
+ 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' };
+}
+
+export function videoDurationCompletionBlock(value, outcome = 'success') {
+ const contract = normalizeVideoDurationContract(value);
+ if (!contract || outcome !== 'success' || contract.status === 'enough') 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.'
+ : contract.status === 'unverified' ? 'The delivered video duration could not be verified.'
+ : '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.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.` };
+}
diff --git a/src/firefox/src/run-ui-journal.js b/src/firefox/src/run-ui-journal.js
index a25bd48d5..bad006eb6 100644
--- a/src/firefox/src/run-ui-journal.js
+++ b/src/firefox/src/run-ui-journal.js
@@ -101,7 +101,7 @@ function compactRunUiToolResult(result, name) {
if (typeof source[key] === 'string') compacted[key] = source[key];
}
if (source.inlineMedia === true) compacted.inlineMedia = true;
- for (const key of ['durationSeconds', 'clipCount', 'estimatedSeconds', 'plannedSeconds', 'fallbackClipSeconds']) {
+ for (const key of ['durationSeconds', 'clipCount', 'estimatedSeconds', 'plannedSeconds', 'approvedDurationSeconds', 'fallbackClipSeconds']) {
if (Number.isFinite(source[key])) compacted[key] = source[key];
}
if (['short', 'enough', 'unverified'].includes(source.durationStatus)) compacted.durationStatus = source.durationStatus;
diff --git a/test/generative-media-duration-verification.mjs b/test/generative-media-duration-verification.mjs
new file mode 100644
index 000000000..cacbf3f42
--- /dev/null
+++ b/test/generative-media-duration-verification.mjs
@@ -0,0 +1,184 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import fs from 'node:fs/promises';
+
+const oneSecond = new Uint8Array(await fs.readFile(new URL('./fixtures/video/h264-aac-1.mp4', import.meta.url)));
+const { joinMp4Clips } = await import('../src/chrome/src/agent/video-join.js');
+const sixtyOneSeconds = joinMp4Clips(Array.from({ length: 61 }, () => oneSecond)).bytes;
+const differentSize = new Uint8Array(await fs.readFile(new URL('./fixtures/video/h264-aac-48px.mp4', import.meta.url)));
+
+// Scene delivery stores generated bytes locally. This small in-memory store
+// keeps these tests independent from browser installation and paid providers.
+const assets = new Map();
+globalThis.indexedDB = { open() {
+ const request = { result: { close() {}, transaction() {
+ const transaction = { objectStore: () => ({ put(record) {
+ assets.set(record.id, record);
+ setTimeout(() => transaction.oncomplete?.());
+ } }) };
+ return transaction;
+ } } };
+ setTimeout(() => request.onsuccess?.());
+ return request;
+} };
+
+const model = 'minimax/h3/text-to-video';
+const config = { provider: 'fal', apiKey: 'private-fal-key', videoModel: model };
+const json = (body, status = 200) => new Response(JSON.stringify(body), {
+ status, headers: { 'Content-Type': 'application/json' },
+});
+const openapi = {
+ openapi: '3.0.4', paths: { '/model': { post: { requestBody: { content: {
+ 'application/json': { schema: { type: 'object', properties: {
+ prompt: { type: 'string' }, duration: { type: 'integer', minimum: 1, maximum: 64 },
+ } } },
+ } } } } },
+};
+
+function api({ download = () => new Response(oneSecond, { headers: { 'Content-Type': 'video/mp4' } }) } = {}) {
+ const calls = { submits: [], downloads: [] };
+ const fetchImpl = async (url, init = {}) => {
+ assert.equal(init.credentials, 'omit');
+ if (url.startsWith('https://v3b.fal.media/')) {
+ assert.equal(init.headers, undefined, 'verification never sends the provider key');
+ const index = Number(/clip-(\d+)\.mp4$/.exec(url)[1]);
+ calls.downloads.push(index);
+ return download(index, init);
+ }
+ assert.equal(init.redirect, 'error');
+ assert.equal(init.headers.Authorization, `Key ${config.apiKey}`);
+ if (url.startsWith('https://api.fal.ai/v1/models?')) return json({ models: [{ endpoint_id: model, openapi }] });
+ assert.ok(url.startsWith(`https://queue.fal.run/${model}`), url);
+ if (init.method === 'POST') {
+ const index = calls.submits.push(JSON.parse(init.body)) - 1;
+ const job = `https://queue.fal.run/${model}/requests/job-${index}`;
+ return json({ request_id: `job-${index}`, status_url: `${job}/status`, response_url: job });
+ }
+ const [, index, status] = /\/requests\/job-(\d+)(\/status)?$/.exec(url);
+ return status ? json({ status: 'COMPLETED' })
+ : json({ video: { url: `https://v3b.fal.media/clip-${index}.mp4` } });
+ };
+ return { calls, fetchImpl };
+}
+
+for (const build of ['chrome', 'firefox']) {
+ const { generateImage } = await import(`../src/${build}/src/agent/generative-media.js`);
+ const generate = (mock, args = {}, options = {}) => generateImage({
+ prompt: 'Consistent cast, plain background.', media: 'video', duration: 1, estimated_duration: 1, ...args,
+ }, { config, fetchImpl: mock.fetchImpl, pollIntervalMs: 1, ...options });
+
+ test(`${build}: hosted duration verification is opt-in and preserves the URL`, async () => {
+ const plain = api();
+ const unmeasured = await generate(plain);
+ assert.equal(unmeasured.success, true, unmeasured.error);
+ assert.equal(unmeasured.durationStatus, 'unverified');
+ assert.deepEqual(plain.calls.downloads, []);
+ const mock = api();
+ const result = await generate(mock, {}, { verifyVideoDuration: true });
+ assert.equal(result.success, true, result.error);
+ assert.equal(result.url, 'https://v3b.fal.media/clip-0.mp4');
+ assert.deepEqual([result.durationSeconds, result.estimatedSeconds, result.plannedSeconds, result.durationStatus, result.mimeType],
+ [1, 1, 1, 'enough', 'video/mp4']);
+ assert.equal(result.approvedDurationSeconds, undefined);
+ assert.equal(result.notice, undefined);
+ assert.deepEqual(mock.calls.downloads, [0]);
+ assert.equal(mock.calls.submits.length, 1);
+ });
+
+ test(`${build}: measured hosted output compares against the requested duration, not rounded plan`, async () => {
+ const mock = api({ download: () => new Response(sixtyOneSeconds) });
+ const result = await generate(mock, { duration: 64, estimated_duration: 60 }, { verifyVideoDuration: true });
+ assert.equal(result.success, true, result.error);
+ assert.deepEqual([result.durationSeconds, result.estimatedSeconds, result.plannedSeconds, result.durationStatus],
+ [61, 60, 64, 'enough']);
+ assert.equal(result.notice, undefined);
+ assert.equal(result.approvedDurationSeconds, undefined);
+ });
+
+ test(`${build}: a measured short hosted clip remains a successful paid asset with a shortfall`, async () => {
+ const mock = api();
+ const result = await generate(mock, { duration: 8, estimated_duration: 8 }, { verifyVideoDuration: true });
+ assert.equal(result.success, true, result.error);
+ assert.equal(result.url, 'https://v3b.fal.media/clip-0.mp4');
+ assert.equal(result.durationSeconds, 1);
+ assert.equal(result.durationStatus, 'short');
+ assert.match(result.notice, /accepted target is 8 s/);
+ assert.equal(mock.calls.submits.length, 1);
+ });
+
+ test(`${build}: failed hosted verification keeps the paid URL unverified`, async () => {
+ for (const download of [
+ () => json({ error: 'Unavailable' }, 503),
+ () => new Response('not an MP4'),
+ () => { throw new TypeError('Download failed'); },
+ () => { throw new DOMException('Verification deadline exceeded', 'TimeoutError'); },
+ ]) {
+ const mock = api({ download });
+ const result = await generate(mock, {}, { verifyVideoDuration: true });
+ assert.equal(result.success, true, result.error);
+ assert.equal(result.url, 'https://v3b.fal.media/clip-0.mp4');
+ assert.equal(result.durationStatus, 'unverified');
+ assert.equal(result.durationSeconds, undefined);
+ assert.match(result.notice, /asset was kept/);
+ assert.match(result.notice, /could not be verified/);
+ assert.equal(mock.calls.submits.length, 1, 'verification does not generate another paid clip');
+ }
+ });
+
+ test(`${build}: Stop during hosted verification keeps the completed asset`, async () => {
+ const controller = new AbortController();
+ const mock = api({ download: (_index, init) => {
+ controller.abort(new DOMException('Stopped by user', 'AbortError'));
+ assert.equal(init.signal.aborted, true);
+ throw init.signal.reason;
+ } });
+ const result = await generate(mock, {}, { verifyVideoDuration: true, signal: controller.signal });
+ assert.equal(result.success, true, result.error);
+ assert.equal(result.url, 'https://v3b.fal.media/clip-0.mp4');
+ assert.equal(result.durationStatus, 'unverified');
+ assert.equal(mock.calls.submits.length, 1);
+ });
+
+ test(`${build}: a plain hosted video is not downloaded without a duration target`, async () => {
+ const mock = api();
+ const result = await generate(mock, { duration: undefined, estimated_duration: undefined }, { verifyVideoDuration: true });
+ assert.equal(result.success, true, result.error);
+ assert.deepEqual(mock.calls.downloads, []);
+ assert.equal(result.durationSeconds, undefined);
+ });
+
+ test(`${build}: only an accepted over-limit shortening produces a trusted approved duration`, async () => {
+ const mock = api();
+ let asked;
+ const result = await generate(mock, { duration: undefined, estimated_duration: 330,
+ approvedDurationSeconds: 1, scenes: [{ prompt: 'First part', duration: 1 }, { prompt: 'Second part', duration: 1 }] },
+ { confirmVideoLength: async details => { asked = { ...details }; return 'once'; } });
+ assert.equal(result.success, true, result.error);
+ assert.deepEqual(asked, { estimatedSeconds: 330, totalSeconds: 2, clipCount: 2, maxSeconds: 120 });
+ assert.deepEqual([result.estimatedSeconds, result.approvedDurationSeconds, result.plannedSeconds, result.durationSeconds, result.durationStatus],
+ [330, 2, 2, 2, 'enough']);
+ assert.ok(assets.has(result.mediaId));
+ const withinLimit = api();
+ const ordinary = await generate(withinLimit, { approvedDurationSeconds: 1 }, { verifyVideoDuration: true });
+ assert.equal(ordinary.approvedDurationSeconds, undefined, 'tool arguments cannot forge approval');
+ const declined = api();
+ const stopped = await generate(declined, { duration: undefined, estimated_duration: 330,
+ scenes: [{ prompt: 'Short version', duration: 1 }] }, { confirmVideoLength: async () => 'deny' });
+ assert.equal(stopped.declined, true);
+ assert.equal(stopped.approvedDurationSeconds, undefined);
+ assert.equal(declined.calls.submits.length, 0);
+ });
+
+ test(`${build}: approved duration does not hide an unsuccessful join`, async () => {
+ const mock = api({ download: index => new Response(index ? differentSize : oneSecond) });
+ const result = await generate(mock, { duration: undefined, estimated_duration: 330,
+ scenes: [{ prompt: 'First part', duration: 1 }, { prompt: 'Second part', duration: 1 }] },
+ { confirmVideoLength: async () => 'once' });
+ assert.equal(result.success, true, result.error);
+ assert.equal(result.approvedDurationSeconds, 2);
+ assert.equal(result.durationStatus, 'enough');
+ assert.equal(result.joined, false);
+ assert.equal(result.clips.length, 2);
+ assert.ok(result.clips.every(clip => assets.has(clip.mediaId)));
+ });
+}
diff --git a/test/generative-media-video.mjs b/test/generative-media-video.mjs
index 6907a0517..459be03d3 100644
--- a/test/generative-media-video.mjs
+++ b/test/generative-media-video.mjs
@@ -1251,6 +1251,8 @@ for (const build of ['chrome', 'firefox']) {
assert.deepEqual([approved.estimatedSeconds, approved.plannedSeconds, approved.durationSeconds, approved.durationStatus],
[330, 2, 2, 'enough'], 'delivery is measured against the explicitly approved shorter plan');
assert.equal(approved.notice, undefined);
+ assert.equal(approved.approvedDurationSeconds, 2);
+ assert.equal(journal.compactRunUiData('tool_result', { name: 'generate_image', result: approved }).result.approvedDurationSeconds, 2);
const fal = falApi({ model: h3Max.id, schema: h3Max.schema });
const hosted = await generate({ prompt: 'A short clip', media: 'video', duration: 10, estimated_duration: 10 },
diff --git a/test/video-duration-contract-lifecycle.mjs b/test/video-duration-contract-lifecycle.mjs
new file mode 100644
index 000000000..46e5795ec
--- /dev/null
+++ b/test/video-duration-contract-lifecycle.mjs
@@ -0,0 +1,130 @@
+import assert from 'node:assert/strict';
+import { test } from 'node:test';
+
+// Exercise the real guard, session serializer and hydration. Only browser
+// storage is replaced; these tests never submit provider generation requests.
+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 => `extension://duration-test/${value}` },
+};
+
+const task = 'Generate a one-minute educational video.';
+const gate = { requestKind: 'execute', requiresStateChange: false, requiresSubmission: false };
+let nextTab = 14800;
+
+for (const build of ['chrome', 'firefox']) {
+ const { Agent } = await import(`../src/${build}/src/agent/agent.js`);
+ function fixture() {
+ const agent = new Agent({ getActive: () => ({ config: {} }) });
+ const tab = nextTab++;
+ agent._persist = () => {};
+ agent.conversationIds.set(tab, `duration-conversation-${tab}`);
+ agent.conversationModes.set(tab, 'act');
+ agent.conversations.set(tab, [{ role: 'system', content: 'System' }, { role: 'user', content: task }]);
+ const guard = agent._startPlanExecutionGuard(tab, 'act', gate);
+ return { agent, tab, guard };
+ }
+ async function hydrate(tab, entry) {
+ const cold = new Agent({ getActive: () => ({ config: {} }) });
+ cold._persist = () => {};
+ stored.set(cold._convKey(tab), structuredClone(entry));
+ await cold._hydrateFromSession(tab);
+ return cold;
+ }
+
+ test(`${build}: failed-only video obligation survives trusted Continue without success evidence`, () => {
+ const { agent, tab, guard } = fixture();
+ guard.videoDurationContract = { requestedSeconds: 60, status: 'failed' };
+ assert.equal(guard.successfulTaskToolCalls, 0);
+ assert.equal(guard.evidenceTaskKey, '');
+ assert.equal(guard.conversationId, agent.conversationIds.get(tab));
+ agent._storeContinuationExecutionEvidence(tab);
+ const carried = agent._continuationExecutionEvidence.get(tab);
+ assert.deepEqual(carried.videoDurationContract, guard.videoDurationContract);
+ assert.notEqual(carried.videoDurationContract, guard.videoDurationContract);
+ // A planner flag correction cannot erase a duration already established
+ // by this exact task. It still does not carry unrelated success evidence.
+ const resumed = agent._startPlanExecutionGuard(tab, 'act', { ...gate, requiresStateChange: true }, { trustedContinuation: true });
+ assert.deepEqual(resumed.videoDurationContract, { requestedSeconds: 60, status: 'failed' });
+ assert.equal(resumed.successfulTaskToolCalls, 0);
+ assert.equal(resumed.evidenceTaskKey, '');
+ });
+
+ test(`${build}: active and torn-down video obligations survive actual session storage and cold hydration`, async () => {
+ for (const status of ['pending', 'failed', 'short', 'enough']) {
+ for (const tornDown of [false, true]) {
+ const { agent, tab, guard } = fixture();
+ const contract = status === 'enough'
+ ? { requestedSeconds: 330, approvedDurationSeconds: 90, durationSeconds: 90, status }
+ : { requestedSeconds: 60, ...(status === 'short' ? { durationSeconds: 5 } : {}), status };
+ guard.videoDurationContract = { ...contract, prompt: 'This extra field must never persist.' };
+ 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.deepEqual(entry.videoDurationObligation, {
+ taskKey: guard.taskKey, conversationId: guard.conversationId, contract,
+ });
+ const cold = await hydrate(tab, entry);
+ assert.equal(cold._planExecutionGuards.has(tab), false, 'Hydration never authorizes a run.');
+ const resumed = cold._startPlanExecutionGuard(tab, 'act', gate, { trustedContinuation: true });
+ assert.deepEqual(resumed.videoDurationContract, contract);
+ assert.equal(resumed.successfulTaskToolCalls, 0, 'A persisted media obligation is not generic execution evidence.');
+ }
+ }
+ });
+
+ test(`${build}: new tasks, conversation changes and untrusted starts cannot inherit a video obligation`, () => {
+ for (const change of ['new-task', 'new-conversation', 'new-run', 'steering']) {
+ const { agent, tab, guard } = fixture();
+ guard.videoDurationContract = { requestedSeconds: 60, status: 'failed' };
+ agent._storeContinuationExecutionEvidence(tab);
+ if (change === 'new-task' || change === 'steering') {
+ agent.conversations.set(tab, [{ role: 'system', content: 'System' }, { role: 'user', content: 'Explain the same topic in text.' }]);
+ }
+ if (change === 'new-conversation') agent.conversationIds.set(tab, 'replacement-conversation');
+ const options = change === 'new-run' || change === 'steering'
+ ? { trustedContinuation: false, ...(change === 'steering' ? { steeringRevision: 1 } : {}) }
+ : { trustedContinuation: true };
+ const fresh = agent._startPlanExecutionGuard(tab, 'act', gate, options);
+ assert.equal(fresh.videoDurationContract, null, change);
+ assert.equal(agent._conversationStorageEntry(tab).videoDurationObligation, null, change);
+ }
+ });
+
+ test(`${build}: hydration rejects mismatched or malformed duration records and never reconstructs them from tool text`, async () => {
+ for (const mismatch of ['task', 'conversation', 'contract', 'tool-only']) {
+ const { agent, tab, guard } = fixture();
+ guard.videoDurationContract = { requestedSeconds: 60, status: 'failed' };
+ const entry = JSON.parse(JSON.stringify(agent._conversationStorageEntry(tab)));
+ if (mismatch === 'task') entry.videoDurationObligation.taskKey = 'tk_00000000';
+ if (mismatch === 'conversation') entry.videoDurationObligation.conversationId = 'other-conversation';
+ if (mismatch === 'contract') entry.videoDurationObligation.contract.requestedSeconds = -60;
+ if (mismatch === 'tool-only') {
+ entry.videoDurationObligation = null;
+ entry.messages.push({ role: 'assistant', content: 'Saved note: videoDurationContract={"requestedSeconds":60,"status":"enough","durationSeconds":60}' });
+ }
+ const cold = await hydrate(tab, entry);
+ assert.equal(cold._continuationExecutionEvidence.has(tab), false, mismatch);
+ assert.equal(cold._startPlanExecutionGuard(tab, 'act', gate, { trustedContinuation: true }).videoDurationContract, null, mismatch);
+ }
+ });
+
+ test(`${build}: a cleared conversation drops live and carried media obligations`, async () => {
+ const { agent, tab, guard } = fixture();
+ guard.videoDurationContract = { requestedSeconds: 60, status: 'failed' };
+ agent._storeContinuationExecutionEvidence(tab);
+ await agent.clearConversation(tab, { preserveStorage: true });
+ assert.equal(guard.videoDurationContract, null);
+ assert.equal(agent._continuationExecutionEvidence.has(tab), false);
+ assert.equal(agent._persistedVideoDurationObligation(tab), null);
+ });
+}
diff --git a/test/video-duration-contract.mjs b/test/video-duration-contract.mjs
new file mode 100644
index 000000000..0e5ca3bb1
--- /dev/null
+++ b/test/video-duration-contract.mjs
@@ -0,0 +1,202 @@
+import assert from 'node:assert/strict';
+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 = body => new Response(JSON.stringify(body), { headers: { 'Content-Type': 'application/json' } });
+let nextTab = 7200;
+
+for (const build of ['chrome', 'firefox']) {
+ const { Agent } = await import(`../src/${build}/src/agent/agent.js`);
+ const { normalizeVideoDurationContract: normalize, pinVideoDurationTarget: pin,
+ recordVideoDurationResult: receipt, videoDurationCompletionBlock: block } = await import(`../src/${build}/src/agent/video-duration-contract.js`);
+ const target = () => ({ requestedSeconds: 60, status: 'pending' });
+ const delivered = seconds => ({ success: true, url: 'https://media.example/video.mp4', mimeType: 'video/mp4', durationSeconds: seconds });
+ function fixture() {
+ const agent = new Agent({}), tab = nextTab++;
+ agent.conversations.set(tab, [{ role: 'user', content: 'Explain this in a one minute video.' }]);
+ agent.conversationIds.set(tab, `conversation-${tab}`);
+ agent.conversationModes.set(tab, 'act');
+ agent._persist = () => {};
+ agent._persistNow = async () => ({ ok: true });
+ const guard = agent._startPlanExecutionGuard(tab, 'act', { requestKind: 'execute', requiresStateChange: false });
+ return { agent, tab, guard };
+ }
+
+ test(`${build}: retries cannot drop or reduce the duration, including after failure`, () => {
+ let value = pin({ media: 'video', estimated_duration: 60, prompt: 'Make it.' }).contract;
+ value = receipt(value, { success: false, error: 'Scene too long' });
+ for (const args of [{ prompt: 'Retry the video.' }, { media: 'video' }, { media: 'video', estimated_duration: 5, duration: 5 }]) {
+ const retried = pin(args, value);
+ assert.equal(retried.args.estimated_duration, 60);
+ assert.equal(retried.contract.requestedSeconds, 60);
+ }
+ assert.equal(pin({ media: 'image' }, value).contract.requestedSeconds, 60);
+ assert.equal(pin({ estimated_duration: 60, scenes: [] }).contract.requestedSeconds, 60);
+ assert.equal(pin({ media: 'video' }).contract, null, 'an unspecified standalone clip stays supported');
+ });
+
+ test(`${build}: only one measured video can satisfy the target`, () => {
+ assert.equal(block(receipt(target(), delivered(5))).durationStatus, 'short');
+ assert.equal(block(receipt(target(), delivered(61))), null, 'the requested sixty, not a rounded plan of sixty-four');
+ assert.ok(block(receipt(target(), { ...delivered(64), joined: false })));
+ assert.ok(block(receipt(target(), { success: true, url: 'https://media.example/v.mp4', durationStatus: 'enough', plannedSeconds: 64 })));
+ assert.ok(block(receipt(target(), { success: true, durationSeconds: 64, mimeType: 'video/mp4' })));
+ assert.ok(block(receipt(target(), { ...delivered(64), error: 'failed' })));
+ assert.equal(receipt(receipt(target(), delivered(30)), delivered(30)).durationSeconds, 30, 'do not sum separate retries');
+ assert.equal(receipt(receipt(target(), delivered(64)), { success: false }).status, 'enough', 'keep a completed deliverable');
+ assert.deepEqual(pin({ prompt: 'A thumbnail image' }, receipt(target(), delivered(64))).args, { prompt: 'A thumbnail image' });
+ assert.equal(block(receipt(target(), delivered(5)), 'partial'), null);
+ assert.equal(block(receipt(target(), delivered(5)), 'failed'), null);
+ assert.ok(block(normalize({ requestedSeconds: 60, status: 'enough' })));
+ assert.equal(normalize({ requestedSeconds: Infinity }), null);
+ });
+
+ test(`${build}: only an approved shorter runtime result changes the effective target`, () => {
+ const long = { requestedSeconds: 330, status: 'pending' };
+ 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);
+ });
+
+ test(`${build}: actual trace retry never dispatches a default short paid clip`, async () => {
+ const { agent, tab, guard } = fixture();
+ const originalFetch = globalThis.fetch;
+ let submits = 0, saved = 0;
+ agent._persistNow = async () => { saved++; return { ok: true }; };
+ globalThis.fetch = async (url, init = {}) => {
+ if (init.method === 'POST') { submits++; assert.fail('A target-losing retry must not submit a paid clip'); }
+ if (url.startsWith('https://api.fal.ai/')) return json({ models: [] });
+ assert.ok(url.startsWith('https://fal.ai/api/openapi/queue/openapi.json'));
+ return json({});
+ };
+ try {
+ const context = { imageGenConfig: { provider: 'fal', apiKey: 'fixture', videoModel: 'minimax/h3-max/text-to-video' } };
+ const first = await agent._executeToolImpl(tab, 'generate_image', {
+ prompt: 'Explain the Minkowski sum.', estimated_duration: 60,
+ scenes: Array.from({ length: 4 }, (_, i) => ({ prompt: `Distinct scene ${i}`, duration: 15 })),
+ }, () => {}, context);
+ assert.equal(first.success, false);
+ assert.equal(guard.videoDurationContract.requestedSeconds, 60);
+ assert.match(first.error, /8 s/);
+ for (const retry of [{ prompt: 'Explain the Minkowski sum.' }, { media: 'video', prompt: 'Explain the Minkowski sum.' },
+ { media: 'video', prompt: 'Explain it.', duration: 5, estimated_duration: 5 }]) {
+ const result = await agent._executeToolImpl(tab, 'generate_image', retry, () => {}, context);
+ assert.equal(result.success, false);
+ assert.match(result.error, /estimated_duration: 60/);
+ }
+ assert.equal(submits, 0);
+ assert.equal(saved, 8, 'save target before each attempt and its result afterward');
+ } finally { globalThis.fetch = originalFetch; }
+ });
+
+ test(`${build}: short or unverified results cannot bypass done via advisory, lean, JSON or 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._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']) }));
+ 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);
+ assert.equal(done.videoDurationIncomplete, true);
+ }
+ agent.cloudRunContexts.set(tab, { outputSchema: { type: 'object' } });
+ 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');
+ agent._resolveExactTableDone = async () => null;
+ 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 () => {
+ const { agent, tab, guard } = fixture();
+ guard.videoDurationContract = receipt(target(), delivered(64));
+ agent._resolveExactTableDone = async () => null;
+ agent._leanRunActive = () => true;
+ agent._leanChatReceiptBlock = async () => null;
+ assert.equal(agent._videoDurationDoneBlock(tab), null);
+ assert.equal((await agent._executeToolImpl(tab, 'done', { outcome: 'success', summary: '64 seconds.' }, () => {})).done, true);
+ });
+
+ test(`${build}: an accepted shorter target survives a failed provider request`, async () => {
+ const { agent, tab, guard } = fixture();
+ const originalFetch = globalThis.fetch;
+ let confirmations = 0, submits = 0;
+ agent._promptVideoLengthConfirmation = async () => { confirmations++; return 'once'; };
+ const openapi = { openapi: '3.0.4', paths: { '/model': { post: { requestBody: { content: {
+ 'application/json': { schema: { type: 'object', properties: { prompt: { type: 'string' }, duration: { type: 'integer', enum: [15] } } } },
+ } } } } } };
+ globalThis.fetch = async (url, init = {}) => {
+ if (init.method === 'POST') {
+ submits++;
+ assert.equal(guard.videoDurationContract.approvedDurationSeconds, 120, 'persist approval before the paid request');
+ return new Response('provider rejected', { status: 400 });
+ }
+ assert.ok(url.startsWith('https://api.fal.ai/'));
+ return json({ models: [{ endpoint_id: 'fixture/video', openapi }] });
+ };
+ try {
+ const context = { imageGenConfig: { provider: 'fal', apiKey: 'fixture', videoModel: 'fixture/video' } };
+ const result = await agent._executeToolImpl(tab, 'generate_image', {
+ media: 'video', prompt: 'A long explainer', estimated_duration: 330,
+ scenes: Array.from({ length: 8 }, (_, i) => ({ prompt: `Scene ${i}`, duration: 15 })),
+ }, () => {}, context);
+ assert.equal(result.success, false);
+ assert.ok(submits > 0);
+ assert.equal(confirmations, 1);
+ assert.equal(guard.videoDurationContract.requestedSeconds, 330);
+ assert.equal(guard.videoDurationContract.approvedDurationSeconds, 120);
+ assert.equal(pin({ prompt: 'Retry' }, guard.videoDurationContract).args.estimated_duration, 120);
+ } finally { globalThis.fetch = originalFetch; }
+ });
+
+ test(`${build}: steering queued during persistence prevents a new paid dispatch`, async () => {
+ const { agent, tab } = fixture();
+ agent._persistNow = async () => { agent._hasPendingSteering = () => true; return { ok: true }; };
+ const result = await agent._executeToolImpl(tab, 'generate_image', { media: 'video', estimated_duration: 60, prompt: 'Video' }, () => {});
+ assert.equal(result.dispatched, false);
+ assert.match(result.error, /changed or stopped/);
+ });
+
+ test(`${build}: a late result cannot overwrite a replacement task contract`, async () => {
+ const { agent, tab, guard } = fixture();
+ const originalFetch = globalThis.fetch;
+ let finish, started;
+ const wait = new Promise(resolve => { finish = resolve; });
+ const began = new Promise(resolve => { started = resolve; });
+ globalThis.fetch = async () => { started(); await wait; return json({}); };
+ try {
+ const pending = agent._executeToolImpl(tab, 'generate_image', { media: 'video', estimated_duration: 60, prompt: 'Video' }, () => {},
+ { imageGenConfig: { provider: 'fal', apiKey: 'fixture', videoModel: 'fixture/video' } });
+ await began;
+ const replacement = { ...guard, videoDurationContract: { requestedSeconds: 8, status: 'pending' } };
+ agent._planExecutionGuards.set(tab, replacement);
+ finish();
+ assert.equal((await pending).success, false);
+ assert.deepEqual(replacement.videoDurationContract, { requestedSeconds: 8, status: 'pending' });
+ } finally { finish(); globalThis.fetch = originalFetch; }
+ });
+
+ test(`${build}: storage failure blocks dispatch and changing tasks detaches old receipts`, async () => {
+ const { agent, tab, guard } = fixture();
+ agent._persistNow = async () => ({ ok: false });
+ const result = await agent._executeToolImpl(tab, 'generate_image', { media: 'video', estimated_duration: 60, prompt: 'Video' }, () => {});
+ assert.equal(result.dispatched, false);
+ assert.equal(guard.videoDurationContract.requestedSeconds, 60);
+ agent.conversationIds.set(tab, 'another-conversation');
+ assert.equal(agent._currentVideoDurationGuard(tab), null);
+ });
+}