Skip to content

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

Prepare the X/Bluesky publication check before the click on Post

Barack Sokulluaff136578ec9
diff --git a/docs/privacy-and-data-flow.md b/docs/privacy-and-data-flow.md
index 520d229ce..c9a08556c 100644
--- a/docs/privacy-and-data-flow.md
+++ b/docs/privacy-and-data-flow.md
@@ -372,6 +372,11 @@ counts, outcome categories, and safety-status booleans. URL, target labels,
accessibility references, and other page content are excluded in both tiers,
including lossless traces.
+`predispatch_timing` trace notes retain only bounded per-stage milliseconds of
+one tool call's pre-dispatch pipeline, the tool name and an outcome category.
+`publication_prefetch` notes retain only a status label and a duration. Page,
+draft and model content are excluded in both tiers, including lossless traces.
+
`done_timing` trace notes retain only the milliseconds a completion check spent
preparing its evidence, reading the page's status texts and reaching a verdict,
with the verdict's outcome and engine names. Completion verification attempts
diff --git a/docs/social-publication-contract.md b/docs/social-publication-contract.md
index 1621628e1..4f3828147 100644
--- a/docs/social-publication-contract.md
+++ b/docs/social-publication-contract.md
@@ -14,6 +14,8 @@ The contract records destinations, accounts, ordered post bodies, media constrai
A ready contract normally also requires an independent audit by the selected provider against the concrete composer snapshot. Approval is cached only for that contract, action, payload, account, page, and document/frame. The runtime rereads the composer after the audit and immediately before execution. A changed snapshot needs a new audit. Rejections retain the checker's reason; invalid/mismatched output and request failures are separate technical failures. Arbitrary JavaScript, bundled editing/submission, keyboard submission, and opaque callbacks cannot substitute for the observed publish control.
+Both checks can run before the click instead of during it. When the agent writes into X's or Bluesky's own post editor (X's `tweetTextarea_N`, Bluesky's ProseMirror editor) in a run whose plan submits something and is not bound to another site job, the runtime starts compiling the contract in the background as the write begins. After the write succeeds it runs the click's own probe against that composer's publish control, audits the observed draft for the exact binding key the click would compute, and reads the control captions the click's checks will ask about. An upload into the composer last written repeats the audit. Background work changes nothing in the run: a contract compiled ahead is installed only by the next caller that would compile exactly the same input (sources, context, task, prior state), and a correction drops it. At the click, a verdict stored for the key computed from the click's fresh probe decides once, exactly as the click's own audit would (a denial blocks and offers the same Retry). An audit still running for that key is awaited. Any other key, or no verdict, runs the audit at the click. The post-audit reread, Retry exemption and every other check are unchanged. Stop, steering, a changed task and navigation discard background results; the prefetch writes no transcript, emits only trace notes, and its model calls go through the run's cost allowance.
+
Compilation and audit use 2,048 output tokens initially and 4,096 for one format repair, capped by the selected provider's limit. Supported provider controls reduce reasoning and request structured JSON; an explicit unsupported-control rejection gets a portable prompt-only repair. Repairs keep the original user input and binding frozen and add format instructions to the system message. The audit accepts one JSON object, optionally fenced, with exact key/action identifiers, a boolean verdict, and a string reason; extra metadata is harmless and displayed reasons are limited to 600 characters. Empty, malformed, conflicting, or mismatched results cannot authorize dispatch. Trace exports include each request/reply, output budget, finish/token diagnostics, and the precise validation failure.
An exhausted publication check stops the run with a clear failure message and app-owned Give up / Retry controls. Give up leaves the draft untouched. A direct user Retry grants one audit exemption for this unchanged task, action, account, payload, page, and document/frame. It skips the independent model audit for one dispatch attempt while keeping intent compilation, deterministic preflight, fresh observations, capability permissions, and duplicate-delivery checks. Model-authored arguments, alternate click strategies, and ordinary Continue cannot grant this exemption. Changes or steering invalidate it; it is consumed immediately before the actual guarded tool invocation and cannot be replayed, including after an uncertain or failed invocation. The frozen contract and user grant survive trusted Continue without being counted as successful execution.
diff --git a/docs/trace-format-compatibility.md b/docs/trace-format-compatibility.md
index b2e0ace99..f9845ec7a 100644
--- a/docs/trace-format-compatibility.md
+++ b/docs/trace-format-compatibility.md
@@ -18,6 +18,8 @@ Planner alignment and publication checks record their auxiliary model requests a
Recoverable failures may include a `failure_recovery_offered` note with the failed stage and retry mode. Chat controls use an opaque runtime recovery ID bound to the exact task and conversation. Starting another run, changing the task, stopping, or choosing Give up expires the offer. Model, response-generation and interaction failures resume through normal continuation checks. For `action_context_unverified`, `action_monitoring_relaxed` identifies one explicitly accepted Retry run on the same task and document; publication Retry remains the separately bound, one-use audit exemption described in the publication contract. Metadata-only `page_action_validation` notes include a finite `certificationFailure` or dispatch `predicate` and a `monitoringRelaxed` boolean. Private grant tokens and target/page content are excluded. Metadata-only `page_traversal_attribution` notes record which attribution a same-document Back/Forward update finally kept (`outcome`: `click_effect`, `page` or `user`) and a finite `reason`; no URL is recorded.
+A consequential tool call may add one `predispatch_timing` note: per-stage milliseconds of its pre-dispatch pipeline (for example `submitProbeMs`, `workflowDispatchMs`, `publicationContractMs`, `publicationAuditMs` or `publicationAuditReusedMs`, `recipientGuardMs`, `checkpointMs`, `executeToolMs`, `totalMs`), the tool name, and an `outcome` of `dispatched`, `not_dispatched` or `blocked`. Stage names vary with the path a call took; readers should treat unknown stages as additional durations. A `publication_prefetch` note records the `status` (`skipped`, `contract_ready`, `authorized`, `denied`, `cached`, `changed` or `failed`) and `durationMs` of a background publication check.
+
Tool events may include an optional `data.outcome` summary containing bounded dispatch, application, provider, error, and CAPTCHA-gate diagnostics. In lossless recordings, this summary survives the content-budget marker, while answer tokens, cookies, provider keys, and form inputs are excluded from the summary. Readers should still honor `_truncated` and `losslessBudgetOmitted`; the summary does not reconstruct discarded content.
`/export --traces --full` exports the stored conversation through the existing `webbrain-trace/1` session envelope, including stored screenshots. It adds no Markdown preview or turn-count truncation. Recording-time omissions and privacy projections remain explicit. The side panel reads the shared trace database and assembles the download locally; only the session identity crosses runtime messaging, so screenshot-heavy exports do not hit the browser message-size limit.
diff --git a/package.json b/package.json
index 4e4bf1163..35bfe23ef 100644
--- a/package.json
+++ b/package.json
@@ -62,7 +62,7 @@
"bump": "node scripts/bump-version.mjs",
"release": "node scripts/bump-version.mjs --release",
"benchmark:relevance": "node scripts/benchmark-offline-relevance.mjs --verbose",
- "test:social-contract": "node --test test/social-publish-contract.mjs test/publication-quote-markup.mjs",
+ "test:social-contract": "node --test test/social-publish-contract.mjs test/publication-quote-markup.mjs test/publication-prefetch.mjs",
"test:social-contract:dom": "node test/social-publish-contract-dom.mjs",
"test:runtime-lifecycle": "node test/bot-memory-dom.mjs && node --test test/agent-lifecycle.mjs test/runtime-startup.mjs test/run-ui-scheduled-lifecycle.mjs test/transport-deadlines.mjs test/cdp-worker-restart.mjs test/bot-memory.mjs test/bot-memory-registries.mjs test/bot-memory-cloud-controller.mjs test/cdp-clear-receipt.mjs test/cdp-field-submit-recovery.mjs test/conversation-persistence.mjs test/history-recovery-state.mjs test/history-recovery-scheduler.mjs test/history-recovery-atomic.mjs test/context-live-tool-group.mjs test/integration-races.mjs test/watch-completion-checkpoint.mjs test/runtime-model-identity.mjs test/provider-message-privacy.mjs test/runtime-task-authority.mjs test/planner-repair-authority.mjs test/runtime-progress-continuation.mjs test/chat-resume-chain.mjs test/chat-resume-completion.mjs test/native-chat-execution-ledger.mjs test/chat-send-dispatch-result.mjs test/chat-reply-dedup-race.mjs test/memcode-lifecycle.mjs test/runtime-peer-review.mjs test/read-continuation-schema.mjs test/completion-submit-acknowledgement.mjs test/completion-live-status.mjs test/completion-iframe-submit.mjs test/form-validation-alert-readings.mjs test/control-effect-readings.mjs test/trusted-retry-readings.mjs test/completion-semantic-commit.mjs test/completion-diagnostics.mjs test/mastodon-publication.mjs test/calculator.mjs test/chat-recovery-protocol.mjs test/chat-pending-reconciliation.mjs test/semantic-page-retry.mjs test/pdf-detection.mjs test/runtime-lifecycle.mjs test/collection-read-progress.mjs test/network-lifecycle.mjs test/text-write-cancellation.mjs test/guard-advisory-writes.mjs test/trusted-field-preparation.mjs test/draftjs-text.mjs test/codemirror-text.mjs test/rich-text-frameworks.mjs test/cdp-managed-text-selection.mjs test/firefox-rich-text-readback.mjs test/contenteditable-beforeinput.mjs test/trusted-field-preparation-ownership.mjs test/typing-preparation-geometry.mjs test/chat-steering.mjs test/lifecycle-steering-restart.mjs test/resource-mutation-guards.mjs test/completion-steering-receipt-authority.mjs test/steering-revision-recovery.mjs test/pending-clarification-hydration.mjs test/clarify-timeout-policy.mjs test/guard-ask-cards.mjs test/permission-steering-wakeup.mjs test/pure-calculation-reply.mjs test/pure-calculation-tools.mjs test/page-feedback.mjs test/page-feedback-progress.mjs test/page-feedback-unbindable-target.mjs test/guard-advisory-actions.mjs test/rich-text-preflight-page-change.mjs test/keyboard-receiver-frame.mjs test/ask-response-scope.mjs test/ask-completion-churn.mjs test/page-feedback-policy.mjs test/research-escalation-page-feedback.mjs test/page-feedback-transport-races.mjs test/page-feedback-recovery.mjs test/telegram-read-preparation-agent.mjs test/media-download-agent-handler.mjs test/code-scanning-url-recipient.mjs test/apocalypse-comm-parsing.mjs test/apocalypse-scalar-decoding.mjs test/bitgpu-chat-security.mjs test/social-media-url-upgrades.mjs test/downloaded-file-saved-bytes.mjs test/downloaded-file-local-probe.mjs test/formless-verification-recovery.mjs test/file-upload-accept.mjs test/file-upload-accept-agent.mjs test/workflow-route-generic-completion.mjs test/release-publication-completion.mjs test/workflow-implicit-metadata.mjs test/workflow-field-readings.mjs test/workflow-field-anchors.mjs test/user-turn-kind.mjs test/follow-up-task-context.mjs test/completion-current-execution-evidence.mjs test/monitor-intervention-contract.mjs test/monitor-owned-geometry.mjs test/monitor-viewport-remainder.mjs test/monitor-scroll-churn.mjs test/monitor-local-attribution.mjs test/monitor-lifecycle-leaks.mjs test/feedback-automatic-churn.mjs test/feedback-navigation-idle.mjs test/feedback-run-boundaries.mjs test/feedback-ask-viewing.mjs test/lifecycle-terminal-done.mjs test/incomplete-completion-recovery.mjs test/guard-advisory-completion.mjs test/guard-policy.mjs test/lean-mode.mjs test/lifecycle-planner-digest.mjs test/lifecycle-vision-fallback.mjs test/guards-control-classification.mjs test/guards-unbound-deletion.mjs test/guards-steering-receipts.mjs && node test/ui-content-lifecycle.mjs && node --test test/native-telegram-chat.mjs test/native-telegram-recipient.mjs test/telegram-native-baseline.mjs test/telegram-native-latest.mjs test/native-chat-pre-enter-draft.mjs test/chat-draft-dispatch-ownership.mjs test/native-telegram-alias-preparation.mjs test/telegram-read-preparation-content.mjs test/queued-composer-ownership.mjs test/onboarding-provider-ready.mjs test/workflow-native-targets.mjs test/native-whatsapp-chat.mjs test/native-chat-context-identity.mjs test/native-whatsapp-contact.mjs test/whatsapp-native-bootstrap.mjs test/whatsapp-drawer-identity.mjs test/whatsapp-read-latest.mjs test/whatsapp-read-preparation.mjs test/whatsapp-chat-completion.mjs test/whatsapp-chat-restart.mjs test/chat-goal-lifecycle.mjs test/chat-completion.mjs test/chat-generic-dispatch.mjs test/completion-live-status-browser.mjs test/native-confirmation-visibility.mjs test/read-continuation-browser.mjs test/iframe-preparation-focus.mjs test/contenteditable-literal.mjs test/native-chat-empty-editor.mjs test/native-chat-trusted-clear.mjs test/chat-steering-ui.mjs test/recipient-confirmation-ui.mjs test/assistant-message-visibility-ui.mjs test/history-recovery.mjs test/history-recovery-ui.mjs test/page-monitor-ui.mjs test/x-compose-trusted-typing.mjs test/page-monitor-dialog.mjs test/page-monitor-dialog-native.mjs test/page-monitor-inline-post.mjs test/bluesky-composer-context.mjs test/page-monitor-anchored.mjs test/page-monitor-upload-owner.mjs test/page-monitor-upload-release.mjs test/page-monitor-native-controls.mjs test/page-monitor-native-preparation.mjs test/native-preparation-cold.mjs test/page-monitor-navigation.mjs test/page-monitor-main-owner.mjs test/page-monitor-collateral-controls.mjs test/accessibility-tree-scoped-budget.mjs test/ax-reference-origin.mjs test/page-monitor-decorative-hit.mjs test/social-ui-navigation.mjs test/native-resource-deletion-binding.mjs test/native-mastodon-confirmation.mjs test/dropdown-menu-inventory.mjs test/page-monitor-passive-progress.mjs test/page-monitor-feed-items.mjs test/media-download-binding.mjs test/media-download-capture-integration.mjs test/social-media-downloader-reinjection.mjs test/upload-file-local-state-guidance.mjs && node test/message-recipient-upload.mjs && node test/message-recipient-clarify-binding.mjs && node --test test/message-recipient-whatsapp.mjs test/message-control-readings.mjs && node test/quality/integration-mv3.mjs --full-form && node test/quality/literal-calculate-midrun-mv3.mjs && node test/quality/ask-completion-churn-mv3.mjs && node test/quality/ask-person-activity-mv3.mjs && node test/quality/telegram-cold-preparation-mv3.mjs && node test/quality/native-chat-mv3.mjs && node test/quality/native-chat-presubmit-mv3.mjs && node test/quality/cloud-bot-memory-mv3.mjs && node test/quality/whatsapp-contact-matrix.mjs && node test/quality/pdf-detection-mv3.mjs && node test/quality/download-upload-mv3.mjs && node test/quality/upload-release-binding-mv3.mjs && node test/quality/release-review-world-mv3.mjs && node test/quality/downloaded-file-saved-bytes-mv3.mjs && node test/quality/file-upload-accept-mv3.mjs && node test/quality/bulk-resume-mv3.mjs && node test/quality/literal-frame-mv3.mjs && node test/quality/mastodon-publication-mv3.mjs && node test/quality/mastodon-reply-route-mv3.mjs && node test/quality/mastodon-deletion-mv3.mjs && node test/quality/mastodon-delete-confirmation-mv3.mjs && node test/quality/mastodon-delete-confirmation-mv3.mjs --menu-read && node test/quality/mastodon-delete-confirmation-mv3.mjs --vague && node test/quality/unbound-deletion-mv3.mjs && node test/quality/recipient-clarify-mv3.mjs && node test/quality/open-chat-recipients-mv3.mjs && node test/quality/correction-cross-site-mv3.mjs && node test/quality/anchored-action-mv3.mjs && node test/quality/inline-social-composer-mv3.mjs && node test/quality/x-compose-dialog-mv3.mjs && node test/quality/bluesky-composer-mv3.mjs && node test/quality/dropdown-menu-matrix.mjs && node test/quality/mastodon-deletion-navigation-mv3.mjs && node test/quality/semantic-navigation-matrix.mjs && node test/quality/workflow-replay-mv3.mjs && node test/quality/generative-media-mv3.mjs && node test/quality/cdp-worker-restart-mv3.mjs && node test/quality/steering-worker-restart-mv3.mjs",
"test:page-feedback": "node --test test/trusted-field-preparation.mjs test/guard-advisory-writes.mjs test/draftjs-text.mjs test/codemirror-text.mjs test/rich-text-frameworks.mjs test/cdp-managed-text-selection.mjs test/firefox-rich-text-readback.mjs test/contenteditable-beforeinput.mjs test/trusted-field-preparation-ownership.mjs test/typing-preparation-geometry.mjs test/pure-calculation-reply.mjs test/pure-calculation-tools.mjs test/page-feedback.mjs test/page-feedback-progress.mjs test/page-feedback-unbindable-target.mjs test/guard-advisory-actions.mjs test/rich-text-preflight-page-change.mjs test/keyboard-receiver-frame.mjs test/ask-response-scope.mjs test/ask-completion-churn.mjs test/page-feedback-policy.mjs test/research-escalation-page-feedback.mjs test/page-feedback-transport-races.mjs test/page-feedback-recovery.mjs test/telegram-read-preparation-agent.mjs test/media-download-agent-handler.mjs test/page-monitor-ui.mjs test/x-compose-trusted-typing.mjs test/page-monitor-dialog.mjs test/page-monitor-dialog-native.mjs test/page-monitor-inline-post.mjs test/bluesky-composer-context.mjs test/page-monitor-anchored.mjs test/page-monitor-upload-owner.mjs test/page-monitor-upload-release.mjs test/page-monitor-native-controls.mjs test/page-monitor-passive-progress.mjs test/page-monitor-feed-items.mjs test/media-download-binding.mjs test/media-download-capture-integration.mjs test/monitor-intervention-contract.mjs test/monitor-owned-geometry.mjs test/monitor-viewport-remainder.mjs test/monitor-scroll-churn.mjs test/monitor-local-attribution.mjs test/monitor-lifecycle-leaks.mjs test/feedback-automatic-churn.mjs test/feedback-navigation-idle.mjs test/feedback-run-boundaries.mjs test/feedback-ask-viewing.mjs test/delivery-click-ax-response-loss.mjs test/delivery-stop-compound.mjs test/delivery-cdp-input.mjs test/delivery-click-locks.mjs test/delivery-native-button-geometry.mjs test/delivery-hover-transition.mjs test/delivery-native-button-route.mjs test/delivery-dispatch-boundary.mjs && node test/message-recipient-upload.mjs && node test/message-recipient-clarify-binding.mjs && node --test test/message-recipient-whatsapp.mjs",
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 913347319..27c6336c9 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -9282,6 +9282,45 @@ export class Agent extends LoopDetector {
if (runId) try { trace.recordNote(runId, 0, 'done_timing', timing); } catch {}
}
+ // Where one tool call spent its time before dispatch, for traces: one
+ // predispatch_timing note per consequential call, milliseconds per stage of
+ // the pipeline, never page or model content. `lap(stage)` charges the time
+ // since the previous lap to that stage. Checks nested in a stage add their
+ // own sub-steps with _predispatchStage while the call's record is open.
+ _predispatchTimer(tabId) {
+ const stages = {};
+ const startedAt = Date.now();
+ let last = startedAt;
+ (this._predispatchTimings ||= new Map()).set(tabId, stages);
+ const add = (stage, ms) => {
+ if (Number.isFinite(ms) && ms >= 0) stages[stage] = (stages[stage] || 0) + ms;
+ };
+ return {
+ lap: stage => { const now = Date.now(); add(stage, now - last); last = now; },
+ add,
+ // The whole pipeline up to this point, e.g. until dispatch begins.
+ since: stage => add(stage, Date.now() - startedAt),
+ emit: (step, extra = {}) => {
+ if (this._predispatchTimings?.get(tabId) === stages) this._predispatchTimings.delete(tabId);
+ this._recordPredispatchTiming(tabId, step, { ...stages, totalMs: Date.now() - startedAt, ...extra });
+ },
+ };
+ }
+
+ _recordPredispatchTiming(tabId, step, timing) {
+ const runId = this.currentRunId.get(tabId);
+ if (!runId) return;
+ try {
+ const pending = trace.recordNote(runId, Number.isInteger(step) ? step : null, 'predispatch_timing', timing);
+ if (pending?.catch) pending.catch(() => {});
+ } catch {}
+ }
+
+ _predispatchStage(tabId, stage, ms) {
+ const stages = this._predispatchTimings?.get(tabId);
+ if (stages && Number.isFinite(ms) && ms >= 0) stages[stage] = (stages[stage] || 0) + ms;
+ }
+
recordSystemOneVerdict(tabId, metadata, context = {}) {
const runId = context.runId || this.currentRunId.get(tabId);
if (runId) try { trace.recordNote(runId, 0, 'system_one', metadata); } catch {}
@@ -15425,12 +15464,17 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// executeTool(). Keep the failure in the ordinary result pipeline below
// so tracing, loop handling, and trusted recovery notes still behave
// exactly like other tool results.
+ const predispatch = this._predispatchTimer(tabId);
const protectedPageFailure = await this._chromeProtectedPageFailure(tabId, fnName);
if (!protectedPageFailure && fnName !== 'done' && !Agent.NAV_TOOLS.has(fnName)) {
try {
await this._adoptLiveSocialPublishWorkflow(tabId, provider);
} catch {}
}
+ // Writing into a post composer starts compiling the publication
+ // contract in the background (see _socialPublicationContractPrefetch).
+ if (!protectedPageFailure) this._socialPublicationContractPrefetch(tabId, fnName, fnArgs, provider);
+ predispatch.lap('workflowAdoptMs');
const recordPagePreparationTimeout = async (error, stage) => {
const timeoutResult = this._contentActionPreparationTimeoutResult(fnName, error, stage);
@@ -15481,6 +15525,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const captchaGateBlock = protectedPageFailure
? null
: this._captchaGateBlockResult(tabId, fnName, fnArgs);
+ predispatch.lap('captchaGateMs');
if (captchaGateBlock) {
// This path returns before normal tool observation. Retain the signup
// checkpoint even when the first detected challenge needs manual help.
@@ -15613,6 +15658,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const missingResponseOutcomeUnknown = capabilities.length > 0 || isStateChangingCall;
const executionMutationEvidence = !protectedPageFailure
&& this._isExecutionMutationEvidence(fnName, fnArgs, capabilities);
+ predispatch.lap('preparationMs');
const clarificationAuthorizationBlock = protectedPageFailure
? null
: this._clarificationAuthorizationBlock(
@@ -15711,6 +15757,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
});
continue;
}
+ predispatch.lap('clarificationMs');
const formValidationCandidate = !protectedPageFailure
&& this._isFormValidationCandidate(fnName, fnArgs);
let formValidationAllFrames = false;
@@ -15744,17 +15791,22 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// The detector owns its supported-tool filtering. Every form
// validation candidate, including set_field({submit:true}), needs
// the same resolved-target evidence before workflow guards run.
+ const probeStarted = Date.now();
const detected = await this._detectLikelySubmitAction(tabId, fnName, fnArgs, {
...(Array.isArray(coordinateFrames)
? { coordinateFrames }
: {}),
+ onCaptionReading: ms => predispatch.add('submitCaptionReadingMs', ms),
});
+ predispatch.add('submitProbeMs', Date.now() - probeStarted);
this._throwIfAborted(abortSignal);
const currentValidationBlock = this._formValidationBlocks.get(tabId) || null;
const obviousSubmit = this._formValidationActionLooksSubmit(fnName, fnArgs, null, detected);
if (!this._leanRunActive(tabId) && (obviousSubmit || detected?.isSubmit === true) && detected && fnName !== 'iframe_click'
&& /^ref_\d+$/.test(String(fnArgs?.ref_id || ''))) {
+ const baselineStarted = Date.now();
const baseline = await this._readCompletionResult(tabId, fnArgs.ref_id);
+ predispatch.add('resultBaselineMs', Date.now() - baselineStarted);
if (baseline?.identity && baseline.scopeRef && typeof baseline.text === 'string') {
detected.resultBaseline = { identity: baseline.identity, scopeRef: baseline.scopeRef,
textSha256: this._sha256TextSync(baseline.text) };
@@ -15765,10 +15817,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// yet, may still turn out to submit: keep the page's state from
// before the click, so a rejection it shows is new.
const caption = this._controlCaption(fnName, fnArgs, null);
+ const validationStarted = Date.now();
const before = !this._leanRunActive(tabId) && (currentValidationBlock || obviousSubmit || detected?.isSubmit === true
|| (caption && this._controlEffectReading(caption) === null))
? await this._captureFormValidationState(tabId, { allFrames })
: [];
+ predispatch.add('validationSnapshotMs', Date.now() - validationStarted);
this._throwIfAborted(abortSignal);
return {
allFrames,
@@ -15789,6 +15843,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
continue;
}
}
+ predispatch.lap('submitPreflightMs');
if (fnName === 'chrome_web_store_publish') {
detectedSubmitAction = {
isSubmit: true,
@@ -15796,13 +15851,25 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
reason: 'submit the configured Chrome Web Store release for review',
};
}
- const runWorkflowPreSubmitChecks = async () => await this._resourceMutationPreSubmitBlock(tabId, fnName, fnArgs, detectedSubmitAction, provider)
+ const timedCheck = async (stage, check) => {
+ const started = Date.now();
+ try {
+ return await check();
+ } finally {
+ predispatch.add(stage, Date.now() - started);
+ }
+ };
+ const runWorkflowPreSubmitChecks = async () => await timedCheck('resourceMutationMs',
+ () => this._resourceMutationPreSubmitBlock(tabId, fnName, fnArgs, detectedSubmitAction, provider))
|| this._steeringPriorSubmissionBlock(tabId, fnName, fnArgs, detectedSubmitAction)
- || await this._conditionalGithubCommitTransition(tabId, fnName, fnArgs, detectedSubmitAction, callState.guardAdvisories)
- || await this._workflowPreSubmitDispatchBlock(
- tabId, fnName, fnArgs, detectedSubmitAction, provider, callState.guardAdvisories,
- )
+ || await timedCheck('githubTransitionMs',
+ () => this._conditionalGithubCommitTransition(tabId, fnName, fnArgs, detectedSubmitAction, callState.guardAdvisories))
+ || await timedCheck('workflowDispatchMs', () => this._workflowPreSubmitDispatchBlock(
+ tabId, fnName, fnArgs, detectedSubmitAction, provider, callState.guardAdvisories,
+ ))
|| this._taskClickLockAsk(tabId, fnName, fnArgs, callState);
+ const predispatchConsequential = () => missingResponseOutcomeUnknown
+ || detectedSubmitAction?.isSubmit === true || detectedSubmitAction?.publicationControl === true;
let workflowPreSubmitBlock = await runWorkflowPreSubmitChecks();
if (workflowPreSubmitBlock?.mutationConfirmation
&& this.scheduledRunPolicies.get(tabId)?.requireConsequentialConfirmation !== false) {
@@ -15824,6 +15891,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { action: 'abort', value };
}
}
+ predispatch.lap('workflowPreSubmitMs');
// A quality guard that cannot verify a commit, publish or submit asks
// the user on WebBrain's card instead of refusing (guard-policy.js
// 'ask'); 'once' lets this one step go ahead with a note for the model.
@@ -15848,8 +15916,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
onUpdate('warning', { message: 'Stopped by user.' });
return { action: 'abort', value };
}
+ predispatch.lap('unverifiedStepCardMs');
}
if (workflowPreSubmitBlock) {
+ if (predispatchConsequential()) predispatch.emit(step, { tool: fnName, outcome: 'blocked' });
onUpdate('tool_call', { name: fnName, args: fnArgs, outcomeUnknown: false });
onUpdate('tool_result', { name: fnName, result: workflowPreSubmitBlock });
messages.push({
@@ -15939,7 +16009,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let submitConfirmation = detectedSubmitAction || groupedSearchForm;
if (!submitConfirmation) {
try {
- submitConfirmation = await detectSubmitWithDeadline();
+ submitConfirmation = await timedCheck('submitDetectionMs', detectSubmitWithDeadline);
} catch (error) {
if (error?.code !== 'content_action_timeout') throw error;
const loopStop = await recordPagePreparationTimeout(error, 'submit-action detection');
@@ -16109,6 +16179,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
}
}
+ predispatch.lap('submitConfirmationMs');
if (capabilities.length && !bypassesConsequentialGates) {
await this.permissions.hydrate();
const curUrl = await this._currentUrl(tabId);
@@ -16239,6 +16310,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
continue;
}
}
+ predispatch.lap('permissionMs');
if (fnName === 'done' || fnName === 'done_json') {
// Each completion invariant gets its own note: one that already advised
@@ -16398,6 +16470,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// Snapshot URL before nav-prone calls. Some tools are conditional:
// Enter and set_field({submit:true}) can navigate, while other key
// presses and ordinary field edits should avoid the URL-check delay.
+ predispatch.lap('completionChecksMs');
const navigationProneCall = !protectedPageFailure
&& this._isNavigationProneToolCall(fnName, fnArgs);
let beforeUrl = '';
@@ -16463,7 +16536,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (this._hasPendingSteering(tabId)) return skipSteeredPermissionWait(toolIndex);
}
}
+ predispatch.lap('recipientGuardMs');
if (messageRecipientBlock) {
+ if (predispatchConsequential()) predispatch.emit(step, { tool: fnName, outcome: 'blocked' });
onUpdate('tool_call', { name: fnName, args: fnArgs, outcomeUnknown: false });
onUpdate('tool_result', { name: fnName, result: messageRecipientBlock });
messages.push({
@@ -16513,6 +16588,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { action: 'return', value, status: 'persistence_degraded' };
}
}
+ predispatch.lap('checkpointMs');
this._throwIfAborted(this._runAbortSignal(tabId));
const _toolStart = Date.now();
let steeringBeforeDispatch = false;
@@ -16537,6 +16613,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const abortSignal = linked.signal;
try {
this._throwIfAborted(abortSignal);
+ predispatch.lap('pipelineWaitMs');
const pipelineToolbarPreflight = await this._preflightRichTextToolbarTarget(
tabId,
fnName,
@@ -16544,6 +16621,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
provider,
{ onUpdate, guardAdvisories: callState.guardAdvisories },
);
+ predispatch.lap('toolbarPreflightMs');
this._throwIfAborted(abortSignal);
// The draft was asked about (and answered) before this deadline
// started. A question that comes up only now, because the draft
@@ -16558,13 +16636,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// Its notes were already given by the first check.
|| this._withoutUnverifiedStepAsk(
await this._githubCommitPreSubmitBlock(tabId, fnName, fnArgs, detectedSubmitAction, [], { recheck: true }));
+ predispatch.lap('pipelinePublicationMs');
this._throwIfAborted(abortSignal);
// Permission, checkpoint and preflight waits can receive a correction.
// Recheck after all preparation, before marking or invoking dispatch.
if (this._hasPendingSteering(tabId) || await this._refreshPageFeedbackForCall(tabId, tc, onUpdate)) return { steered: true };
+ predispatch.lap('feedbackRecheckMs');
const genericChatBlock = pipelineToolbarPreflight.block || socialDispatchBlock ? null
: await this._prepareGenericChatDispatch(tabId, fnName, fnArgs, messageRecipientExecutionContext, abortSignal,
callState.guardAdvisories);
+ predispatch.lap('genericChatMs');
this._throwIfAborted(abortSignal);
if (!pipelineToolbarPreflight.block && !socialDispatchBlock && !genericChatBlock) {
// Save the task's record with this action's sites before it can be
@@ -16579,6 +16660,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { steered: true };
}
}
+ predispatch.lap('attemptCheckpointMs');
let calculationDispatchBlock = this._pureCalculationToolBlock(tabId, fnName, fnArgs, calculationRequestScope);
if (calculationDispatchBlock && this._pureCalculationScopeAdvises(tabId, fnName, fnArgs,
calculationRequestScope, callState.guardAdvisories)) {
@@ -16595,6 +16677,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (fnName === 'calculate') this._rememberPureCalculationAttempt(tabId, fnArgs, calculationRequestScope);
callState.invoked = true;
}
+ predispatch.lap('dispatchPreparationMs');
+ predispatch.since('preDispatchMs');
const pipelineRawToolResult = pipelineToolbarPreflight.block || socialDispatchBlock || genericChatBlock || calculationDispatchBlock || await this.executeTool(
tabId,
fnName,
@@ -16618,6 +16702,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_contentActionDispatchState: actionDispatchState,
},
);
+ predispatch.lap('executeToolMs');
callState.result = this._normalizeToolResult(fnName, pipelineRawToolResult, missingResponseOutcomeUnknown);
if (pipelineRawToolResult?.dispatched === false || pipelineRawToolResult?.noDispatch === true) {
actionDispatchState.started = false;
@@ -16744,6 +16829,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
rawToolResult = pipelineResult.rawToolResult;
toolResult = pipelineResult.toolResult;
}
+ if (predispatchConsequential()) {
+ predispatch.emit(step, { tool: fnName, outcome: steeringBeforeDispatch || attemptRecordNotSaved
+ || rawToolResult?.noDispatch === true || rawToolResult?.dispatched === false ? 'not_dispatched' : 'dispatched' });
+ }
}
if (attemptRecordNotSaved) {
const value = 'Stopped before sending the page action because the record of what this task has posted could not be saved. Retry after extension storage is available.';
@@ -16799,6 +16888,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// That also noted the call in the dispatch record; see the catch in
// _executeToolBatch for a call interrupted before this point.
callState.dispatchNoted = true;
+ // A write into a post composer starts the publication check of its
+ // draft in the background; the click on Post then finds it done.
+ if (!protectedPageFailure) this._socialPublicationPrefetch(tabId, fnName, fnArgs, toolResult, provider);
const readCompletenessBeforeTool = this.readCompletenessStates.get(tabId) || null;
const readCompletenessAfterTool = this._recordReadCompleteness(tabId, fnName, fnArgs, toolResult);
const requiredReadProgress = readCompletenessMadeProgress(
@@ -23653,6 +23745,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
_invalidateSocialPublicationContract(guard) {
+ // A contract compiled in the background before a correction is never
+ // installed after it.
+ this._socialPublicationContractPrefetches?.delete(guard);
const social = guard.socialPublication;
if (!social) return;
social.needsRecompile = true;
@@ -23789,15 +23884,33 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
delete next.deniedAudit;
delete next.deniedAuditKey;
}
+ // A verdict prefetched for the run that ended is never reused by another.
+ delete next.prefetchedAudit;
return next;
}
- async _ensureSocialPublicationContract(tabId, provider = this._activeProvider(tabId)) {
+ // One compile runs per publication state at a time: a caller arriving while
+ // one runs waits for it instead of reading its still empty contract or
+ // compiling a second time (`wait: false` returns null instead of waiting).
+ // A background compile (`background`, the prefetch) changes nothing in the
+ // run: it is kept beside the run's state and never throws. A later caller
+ // that would compile awaits it and installs it only when it compiled exactly
+ // the input that caller would compile from now: the same sources, context,
+ // task and prior state. Anything else compiles as before.
+ async _ensureSocialPublicationContract(tabId, provider = this._activeProvider(tabId), { background = false, wait = true } = {}) {
const guard = this._planExecutionGuards.get(tabId);
if (!guard?.enabled) return null;
// The app-owned guard is new for every user revision. A trusted Continue
// carries its frozen contract, including exact raw source text.
const previous = guard.socialPublication;
+ const compiles = this._socialPublicationCompiles ||= new WeakMap();
+ const running = previous ? compiles.get(previous) : null;
+ if (running) {
+ if (!wait) return null;
+ await running.catch(() => {});
+ if (this._planExecutionGuards.get(tabId) !== guard) return null;
+ return this._ensureSocialPublicationContract(tabId, provider, { background, wait });
+ }
if (previous && (!previous.needsRecompile || Object.keys(previous.outcomes || {}).length)) return previous;
const sources = this._socialPublicationSources(tabId);
// This URL was observed by the app when the user task started, before
@@ -23807,12 +23920,66 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
requestPlatform: SOCIAL_PLATFORMS.includes(requestWorkflow?.adapterName) ? requestWorkflow.adapterName : null,
};
const key = this._sha256TextSync(JSON.stringify({ sources, context, taskKey: guard.taskKey }));
+ const prefetches = this._socialPublicationContractPrefetches ||= new WeakMap();
+ const prefetch = prefetches.get(guard);
+ if (prefetch && prefetch.previous === previous && prefetch.state.key === key) {
+ if (background) return prefetch.done;
+ if (!prefetch.settled) {
+ if (!wait) return null;
+ await prefetch.done;
+ if (this._planExecutionGuards.get(tabId) !== guard) return null;
+ // Checked again: the sources may have changed while it ran.
+ return this._ensureSocialPublicationContract(tabId, provider, { background, wait });
+ }
+ prefetches.delete(guard);
+ if (prefetch.compiled && !this._checkAbort(tabId) && !this._hasPendingSteering(tabId)) {
+ guard.socialPublication = prefetch.compiled;
+ return prefetch.compiled;
+ }
+ } else if (prefetch && !background) {
+ // A background compile of another input is never installed.
+ prefetches.delete(guard);
+ }
const state = { key, sources, context, contract: null, outcomes: {}, actionId: null, dispatch: null, error: '' };
+ if (background) {
+ if (!provider?.chat || JSON.stringify(sources).length > 120000) return null;
+ const entry = { previous, state, settled: false, compiled: null, done: null };
+ prefetches.set(guard, entry);
+ entry.done = this._socialPublicationCompile(tabId, provider, guard, state, previous, true)
+ .catch(() => null)
+ .then(compiled => {
+ entry.settled = true;
+ entry.compiled = compiled || null;
+ if (!compiled && prefetches.get(guard) === entry) prefetches.delete(guard);
+ return entry.compiled;
+ });
+ return entry.done;
+ }
guard.socialPublication = state;
if (!provider?.chat || JSON.stringify(sources).length > 120000) {
state.error = 'Publication intent could not be read completely with the selected provider.';
return state;
}
+ const compile = this._socialPublicationCompile(tabId, provider, guard, state, previous, false);
+ compiles.set(state, compile);
+ let outcome;
+ try {
+ outcome = await compile;
+ } finally {
+ if (compiles.get(state) === compile) compiles.delete(state);
+ }
+ return outcome === 'recompile' ? this._ensureSocialPublicationContract(tabId, provider, { wait }) : outcome;
+ }
+
+ // The compile itself: the compiled state, null when the run or its task
+ // moved on, or 'recompile' when a correction arrived meanwhile. A background
+ // compile stops once the run's state or its own slot moved on, and leaves
+ // the click its own attempts when it cannot finish.
+ async _socialPublicationCompile(tabId, provider, guard, state, previous, background) {
+ const { sources, context } = state;
+ const replaced = () => (background
+ ? guard.socialPublication !== previous || this._socialPublicationContractPrefetches?.get(guard)?.state !== state
+ : guard.socialPublication !== state || state.needsRecompile);
const messages = publicationContractMessages(sources, context);
let portable = false;
for (let attempt = 0; attempt < 2; attempt++) {
@@ -23830,7 +23997,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
await this._tracePlannerAttemptResponse(runId, 0, provider, response,
'social_publication_contract', attempt + 1, startedAt, options);
if (this._planExecutionGuards.get(tabId) !== guard || this._checkAbort(tabId)) return null;
- if (guard.socialPublication !== state || state.needsRecompile) return this._ensureSocialPublicationContract(tabId, provider);
+ if (replaced()) return background ? null : 'recompile';
if (this._hasPendingSteering(tabId)) return null;
state.contract = normalizePublicationContract(this._socialPublicationResponseObject(response, options), sources);
// A clarification invalidates authorization, not the uploads already
@@ -23845,16 +24012,29 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
state.error = '';
return state;
} catch (error) {
- if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
+ if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) {
+ if (background) return null;
+ throw error;
+ }
await this._socialPublicationCheckerFailure(tabId, 'social_publication_contract', attempt + 1, error, received);
- if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== state
- || state.needsRecompile || this._hasPendingSteering(tabId)) return null;
- state.error = 'Failed while reading the publication request. The checker could not return a complete result; the draft was not published.';
- state.errorKind = 'technical';
+ if (this._planExecutionGuards.get(tabId) !== guard || replaced() || this._hasPendingSteering(tabId)) return null;
+ if (!background) {
+ state.error = 'Failed while reading the publication request. The checker could not return a complete result; the draft was not published.';
+ state.errorKind = 'technical';
+ }
if (this._socialPublicationOptionsRejected(error)) portable = true;
}
}
- return state;
+ return background ? null : state;
+ }
+
+ // Whether `social` is the run's publication state, or the one a background
+ // compile holds for it while it could still be installed.
+ _socialPublicationStateCurrent(guard, social) {
+ if (!social) return false;
+ if (guard.socialPublication === social) return true;
+ const prefetch = this._socialPublicationContractPrefetches?.get(guard);
+ return prefetch?.compiled === social && prefetch.previous === guard.socialPublication;
}
_socialPublicationAction(guard) {
@@ -23931,7 +24111,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const liveUrl = await this._currentUrl(tabId);
const live = resolveAdapterWorkflowJob(liveUrl, 'publish-post');
if (!SOCIAL_PLATFORMS.includes(live?.adapterName)) return false;
- const social = await this._ensureSocialPublicationContract(tabId, provider);
+ // Only the click waits for a contract still being compiled; an ordinary
+ // tool call binds on a later call instead of waiting here.
+ const social = await this._ensureSocialPublicationContract(tabId, provider, { wait: !!detected });
if (!social || social.needsRecompile || social.contract?.status !== 'ready') return false;
this._recordSocialPublishTargetSatisfied(guard);
const eligible = publicationProgress(social.contract, social.outcomes).eligible;
@@ -24071,8 +24253,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
if (!['click', 'click_ax', 'iframe_click'].includes(name)) return blocked('Write and verify the draft first, then activate its publish control in a separate click.');
if (detected.publicationControl !== true) return blocked('Publication composer ownership could not be observed. Read the current page and use its resolved publish control.');
+ const contractStarted = Date.now();
await this._adoptLiveSocialPublishWorkflow(tabId, provider, detected);
const social = await this._ensureSocialPublicationContract(tabId, provider);
+ this._predispatchStage(tabId, 'publicationContractMs', Date.now() - contractStarted);
const action = this._socialPublicationAction(guard);
if (social?.needsRecompile || !action || action.platform !== live.adapterName
|| !publicationProgress(social?.contract, social?.outcomes).eligible.includes(action.id)) {
@@ -24152,57 +24336,28 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (social.deniedAuditKey === key && !bypassMatches) return auditBlocked();
if (social.dispatch?.key !== key) {
if (!bypassMatches) {
- const auditProvider = provider || this._activeProvider(tabId);
- const messages = publicationAuditMessages(social.sources, social.contract, action, snapshot, key, social.context);
- let portable = false;
- let authorized = false;
- for (let attempt = 0; attempt < 2; attempt++) {
- const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
- const options = this._socialPublicationChatOptions(auditProvider, attempt > 0, portable, true);
- const runId = this.currentRunId.get(tabId);
- const startedAt = Date.now();
- let received = false;
- try {
- await this._tracePlannerAttemptRequest(runId, 0, auditProvider, requestMessages,
- 'social_publication_authorization', attempt + 1, this._effectiveRunMode(tabId), options);
- const response = await this._chatWithCostAllowance(auditProvider, requestMessages, options,
- this.currentCostState.get(tabId) || null, { tabId, generationName: 'social_publication_authorization' });
- received = true;
- await this._tracePlannerAttemptResponse(runId, 0, auditProvider, response,
- 'social_publication_authorization', attempt + 1, startedAt, options);
- if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social
- || social.needsRecompile || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
- || guard.taskKey !== taskKey) {
- return blocked('Publication context changed during authorization.');
- }
- const verdict = publicationAuditValidation(this._socialPublicationResponseObject(response, options), key, action.id);
- if (!verdict.valid) throw new Error(verdict.failure);
- if (!verdict.authorized) {
- social.deniedAuditKey = key;
- social.deniedAudit = { status: 'denied', reason: verdict.reason };
- return auditBlocked();
- }
- authorized = true;
- delete social.deniedAudit;
- delete social.deniedAuditKey;
- break;
- } catch (error) {
- if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
- await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received);
- if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social
- || social.needsRecompile || this._hasPendingSteering(tabId)
- || guard.taskKey !== taskKey) {
- return blocked('Publication context changed during authorization.');
- }
+ const auditStarted = Date.now();
+ // A background prefetch audited this exact key (or still is): its
+ // verdict decides once, exactly as this audit would have. Any other
+ // key, or no verdict, runs the audit now.
+ const prefetched = await this._socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey);
+ if (prefetched?.changed) return blocked('Publication context changed during authorization.');
+ if (prefetched) {
+ this._predispatchStage(tabId, prefetched.awaited ? 'publicationAuditAwaitedMs' : 'publicationAuditReusedMs', Date.now() - auditStarted);
+ if (!prefetched.authorized) {
social.deniedAuditKey = key;
- social.deniedAudit = { status: received ? 'invalid_response' : 'unavailable',
- reason: received ? 'The publication checker returned an invalid or mismatched authorization response.'
- : 'The selected provider could not complete the publication check.',
- validationFailure: String(error?.message || error).slice(0, 600) };
- if (this._socialPublicationOptionsRejected(error)) portable = true;
+ social.deniedAudit = { status: 'denied', reason: prefetched.reason };
+ return auditBlocked();
}
+ delete social.deniedAudit;
+ delete social.deniedAuditKey;
+ } else {
+ const outcome = await this._socialPublicationAuditRun(tabId, provider || this._activeProvider(tabId),
+ guard, social, action, snapshot, key, taskKey, { record: true });
+ this._predispatchStage(tabId, 'publicationAuditMs', Date.now() - auditStarted);
+ if (outcome.status === 'changed') return blocked('Publication context changed during authorization.');
+ if (outcome.status !== 'authorized') return auditBlocked();
}
- if (!authorized) return auditBlocked();
}
social.dispatch = { key, actionId: action.id, snapshot: JSON.parse(JSON.stringify(snapshot)), pageUrl,
documentIdentity, frameId, ...(bypassMatches ? { auditBypassed: true } : {}),
@@ -24210,7 +24365,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
// The semantic check can take time. Re-read the same target afterward;
// neither a changed draft/account nor a navigated document inherits it.
+ const rereadStarted = Date.now();
const fresh = await this._detectLikelySubmitAction(tabId, name, args);
+ this._predispatchStage(tabId, 'publicationRereadMs', Date.now() - rereadStarted);
if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social || social.needsRecompile
|| this._checkAbort(tabId) || this._hasPendingSteering(tabId) || guard.taskKey !== taskKey
|| fresh?.isSubmit !== true || fresh.publicationControl !== true || fresh.publicationResourceUrlsComplete !== true
@@ -24226,6 +24383,270 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return null;
}
+ // The publication audit of one concrete draft: the same request, schema,
+ // two attempts, format repair and validation for the click and for its
+ // background prefetch. `record` (the click) records each outcome on the
+ // publication state as it goes; the prefetch records nothing here.
+ // `isCurrent` tells whether `social` is still the run's publication state.
+ async _socialPublicationAuditRun(tabId, provider, guard, social, action, snapshot, key, taskKey,
+ { record = false, isCurrent = () => guard.socialPublication === social } = {}) {
+ const messages = publicationAuditMessages(social.sources, social.contract, action, snapshot, key, social.context);
+ let portable = false;
+ let failure = null;
+ for (let attempt = 0; attempt < 2; attempt++) {
+ const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
+ const options = this._socialPublicationChatOptions(provider, attempt > 0, portable, true);
+ const runId = this.currentRunId.get(tabId);
+ const startedAt = Date.now();
+ let received = false;
+ try {
+ await this._tracePlannerAttemptRequest(runId, 0, provider, requestMessages,
+ 'social_publication_authorization', attempt + 1, this._effectiveRunMode(tabId), options);
+ const response = await this._chatWithCostAllowance(provider, requestMessages, options,
+ this.currentCostState.get(tabId) || null, { tabId, generationName: 'social_publication_authorization' });
+ received = true;
+ await this._tracePlannerAttemptResponse(runId, 0, provider, response,
+ 'social_publication_authorization', attempt + 1, startedAt, options);
+ if (this._planExecutionGuards.get(tabId) !== guard || !isCurrent()
+ || social.needsRecompile || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || guard.taskKey !== taskKey) {
+ return { status: 'changed' };
+ }
+ const verdict = publicationAuditValidation(this._socialPublicationResponseObject(response, options), key, action.id);
+ if (!verdict.valid) throw new Error(verdict.failure);
+ if (!verdict.authorized) {
+ if (record) {
+ social.deniedAuditKey = key;
+ social.deniedAudit = { status: 'denied', reason: verdict.reason };
+ }
+ return { status: 'denied', reason: verdict.reason };
+ }
+ if (record) {
+ delete social.deniedAudit;
+ delete social.deniedAuditKey;
+ }
+ return { status: 'authorized', reason: verdict.reason };
+ } catch (error) {
+ if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
+ await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received);
+ if (this._planExecutionGuards.get(tabId) !== guard || !isCurrent()
+ || social.needsRecompile || this._hasPendingSteering(tabId)
+ || guard.taskKey !== taskKey) {
+ return { status: 'changed' };
+ }
+ failure = { status: received ? 'invalid_response' : 'unavailable',
+ reason: received ? 'The publication checker returned an invalid or mismatched authorization response.'
+ : 'The selected provider could not complete the publication check.',
+ validationFailure: String(error?.message || error).slice(0, 600) };
+ if (record) {
+ social.deniedAuditKey = key;
+ social.deniedAudit = failure;
+ }
+ if (this._socialPublicationOptionsRejected(error)) portable = true;
+ }
+ }
+ return { status: 'failed', failure };
+ }
+
+ // The verdict a background prefetch reached for this exact key, awaiting
+ // one still running for it. The click decides here once: a stored verdict is
+ // used at most once, and a prefetch still running for another draft can no
+ // longer store anything. A run, task or correction that moved on meanwhile
+ // is reported as a change, as after the click's own audit.
+ async _socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey) {
+ const audits = this._socialPublicationAuditPrefetches;
+ const running = audits?.get(social);
+ let awaited = false;
+ if (running?.key === key && social.prefetchedAudit?.key !== key) {
+ awaited = true;
+ await running.done;
+ }
+ if (running && audits.get(social) === running) audits.delete(social);
+ const stored = social.prefetchedAudit?.key === key ? social.prefetchedAudit : null;
+ delete social.prefetchedAudit;
+ if (!stored) return null;
+ if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social
+ || social.needsRecompile || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || guard.taskKey !== taskKey) return { changed: true };
+ return { authorized: stored.authorized === true, reason: String(stored.reason || ''), awaited };
+ }
+
+ // After a text write into an X or Bluesky post editor, or a file added to
+ // the composer last written, the click on Post will need the publication
+ // contract, an audit of this exact draft and readings of what the composer's
+ // controls do. They are prepared here in the background so the click does
+ // not wait for them. Nothing here authorizes anything or changes the run:
+ // the contract is installed only by a caller that would compile exactly the
+ // same input, the click recomputes the binding key from its own fresh probe
+ // and uses a verdict only for that exact key, and every later check still
+ // runs. The prefetch writes no transcript and emits no UI event (only trace
+ // notes), and its model calls go through the run's cost allowance.
+ _socialPublicationPrefetch(tabId, name, args = {}, result = null, provider = null) {
+ try {
+ const guard = this._planExecutionGuards.get(tabId);
+ if (!this._socialPublicationPrefetchAllowed(guard) || !provider?.chat
+ || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || !result || typeof result !== 'object' || result.success !== true
+ || result.noDispatch === true || result.outcomeUnknown === true) return null;
+ const hints = this._socialPublicationPrefetchHints ||= new WeakMap();
+ const hint = name === 'upload_file' ? hints.get(guard) || null : this._socialPublicationPrefetchHint(name, args);
+ if (!hint) return null;
+ hints.set(guard, hint);
+ return this._socialPublicationPrefetchRun(tabId, guard, provider, hint).catch(() => null);
+ } catch {
+ return null;
+ }
+ }
+
+ // The contract depends on the request, never on the draft, so its compile
+ // starts as soon as the agent begins a write into a post editor, while that
+ // write is still being prepared. The audit waits for the written draft.
+ _socialPublicationContractPrefetch(tabId, name, args = {}, provider = null) {
+ try {
+ const guard = this._planExecutionGuards.get(tabId);
+ const hint = this._socialPublicationPrefetchHint(name, args);
+ const social = guard?.socialPublication;
+ const pending = this._socialPublicationContractPrefetches?.get(guard);
+ if (!hint || !this._socialPublicationPrefetchAllowed(guard) || !provider?.chat
+ || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || (social && (this._socialPublicationCompiles?.get(social)
+ || !social.needsRecompile || Object.keys(social.outcomes || {}).length))
+ || (pending && pending.previous === social)) return null;
+ return this._socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly: true }).catch(() => null);
+ } catch {
+ return null;
+ }
+ }
+
+ // A write may prepare the click on Post only in a run that executes a plan
+ // submitting something and is not bound to another site job (a message to
+ // send, a file to commit): the planner told those apart from a post.
+ _socialPublicationPrefetchAllowed(guard) {
+ const job = guard?.siteWorkflow?.job;
+ return !!guard?.enabled && guard.requiresSubmission === true
+ && (!job || (job.id === 'publish-post' && SOCIAL_PLATFORMS.includes(guard.siteWorkflow.adapterName)));
+ }
+
+ // The editor a text write targets: its ref, its selector, or the focused
+ // element.
+ _socialPublicationPrefetchHint(name, args = {}) {
+ if ((name === 'type_ax' || (name === 'set_field' && args?.submit !== true))
+ && typeof args?.ref_id === 'string' && args.ref_id) return { editorRef: args.ref_id };
+ if (name !== 'type_text') return null;
+ return typeof args?.selector === 'string' && args.selector.trim()
+ ? { editorSelector: args.selector.trim() } : { focused: true };
+ }
+
+ async _socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly = false } = {}) {
+ const startedAt = Date.now();
+ const runId = this.currentRunId.get(tabId);
+ const taskKey = guard.taskKey;
+ const current = () => this._planExecutionGuards.get(tabId) === guard && guard.taskKey === taskKey
+ && !this._checkAbort(tabId) && !this._hasPendingSteering(tabId);
+ let status = 'skipped';
+ try {
+ const pageUrl = await this._currentUrl(tabId);
+ if (!current() || !SOCIAL_PLATFORMS.includes(resolveAdapterWorkflowJob(pageUrl, 'publish-post')?.adapterName)) return status;
+ // The same probe the click runs, on the publish control of the composer
+ // that holds the post editor written (found by the app's own markers).
+ const detected = await this._detectLikelySubmitAction(tabId, 'click_ax', { __wbPublicationPrefetch: hint }, { captionPass: 'final' });
+ if (!current() || detected?.isSubmit !== true || detected.publicationControl !== true) return status;
+ const readings = this._socialPublicationPrefetchReadings(tabId, detected.publicationPrefetchCaptions, pageUrl);
+ try {
+ const social = await this._ensureSocialPublicationContract(tabId, provider, { background: true });
+ status = contractOnly
+ ? (social?.contract && this._socialPublicationStateCurrent(guard, social) ? 'contract_ready' : 'skipped')
+ : await this._socialPublicationPrefetchAudit(tabId, guard, provider, social, detected, pageUrl, taskKey, current);
+ } finally {
+ await readings;
+ }
+ return status;
+ } catch {
+ return (status = 'failed');
+ } finally {
+ // Noted in the trace of the run that started it.
+ if (runId) {
+ try {
+ const pending = trace.recordNote(runId, 0, 'publication_prefetch', { status, durationMs: Date.now() - startedAt });
+ if (pending?.catch) pending.catch(() => {});
+ } catch {}
+ }
+ }
+ }
+
+ async _socialPublicationPrefetchAudit(tabId, guard, provider, social, detected, pageUrl, taskKey, current) {
+ const isCurrent = () => this._socialPublicationStateCurrent(guard, social);
+ if (!current() || !isCurrent() || social.needsRecompile
+ || social.contract?.status !== 'ready' || social.retryBypass) return 'skipped';
+ // The action the click's own workflow binding chooses for this site.
+ const live = resolveAdapterWorkflowJob(pageUrl, 'publish-post');
+ const eligible = publicationProgress(social.contract, social.outcomes).eligible;
+ const action = social.contract.actions.find(a => a.platform === live?.adapterName && eligible.includes(a.id));
+ if (!action) return 'skipped';
+ // The click audits only a draft that passes its preflight; so does this.
+ const snapshot = this._socialPublicationSnapshot(guard, detected.publicationSnapshot);
+ if (this._socialSnapshotActionIssues(action, snapshot).length
+ || !Array.isArray(detected.publicationResourceUrls) || detected.publicationResourceUrlsComplete !== true) return 'skipped';
+ const documentIdentity = detected.frameDocumentIdentity || '';
+ const frameId = Number.isInteger(detected.frameId) ? detected.frameId : 0;
+ const key = this._sha256TextSync(JSON.stringify({ contract: social.key, action, snapshot, pageUrl, documentIdentity, frameId }));
+ if (social.dispatch?.key === key || social.deniedAuditKey === key || social.prefetchedAudit?.key === key) return 'cached';
+ const audits = this._socialPublicationAuditPrefetches ||= new WeakMap();
+ const entry = { key, done: null };
+ audits.set(social, entry);
+ entry.done = (async () => {
+ try {
+ const outcome = await this._socialPublicationAuditRun(tabId, provider, guard, social, action, snapshot, key, taskKey, { isCurrent });
+ if (outcome.status !== 'authorized' && outcome.status !== 'denied') return outcome.status;
+ // Stored only while nothing moved on: the same run, task, contract
+ // and page, no newer prefetch, and no click that already decided.
+ const unchanged = () => audits.get(social) === entry && current() && isCurrent()
+ && !social.needsRecompile && social.dispatch?.key !== key && social.deniedAuditKey !== key;
+ if (!unchanged()) return 'changed';
+ const liveUrl = await this._currentUrl(tabId);
+ if (!unchanged() || this._normalizeUrl(liveUrl) !== this._normalizeUrl(pageUrl)) return 'changed';
+ social.prefetchedAudit = { key, authorized: outcome.status === 'authorized', reason: outcome.reason, at: Date.now() };
+ return outcome.status;
+ } catch {
+ return 'failed';
+ } finally {
+ if (audits.get(social) === entry) audits.delete(social);
+ }
+ })();
+ return entry.done;
+ }
+
+ // The readings the click's own checks ask for, asked now: what the publish
+ // control and the controls beside it do (the submit probe), and, on a page
+ // whose sends the recipient guard checks, whether activating them sends a
+ // message. Readings are kept per caption, so asking earlier changes no
+ // answer; a caption that differs at the click is read there as before.
+ _socialPublicationPrefetchReadings(tabId, captions, pageUrl) {
+ const list = [...new Set((Array.isArray(captions) ? captions : [])
+ .map(caption => String(caption ?? '').replace(/\s+/g, ' ').trim())
+ .filter(caption => caption && caption.length <= 200))];
+ // A caption an earlier prefetch is still reading is not asked twice.
+ const reading = this._socialPublicationPrefetchReading ||= new Set();
+ const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null
+ && !reading.has(kind + '\u0000' + caption));
+ const read = (kind, texts, request) => {
+ for (const text of texts) reading.add(kind + '\u0000' + text);
+ return Promise.resolve().then(request)
+ .finally(() => { for (const text of texts) reading.delete(kind + '\u0000' + text); });
+ };
+ const reads = [];
+ const effects = unread('control_effect').slice(0, 24);
+ if (effects.length) reads.push(read('control_effect', effects, () => this._readControlEffects(tabId, effects)));
+ let guarded = false;
+ try { guarded = getMessageRecipientGuardPolicy(pageUrl)?.verifyActiveRecipient === true; } catch {}
+ const controls = guarded ? unread('message_control').slice(0, 48) : [];
+ if (controls.length) {
+ reads.push(read('message_control', controls, () => this._readPageTexts(tabId, 'message_control', controls, {},
+ { maxChars: 400, mainTimeoutMs: CONTROL_READING_TIMEOUT_MS })));
+ }
+ return Promise.allSettled(reads);
+ }
+
_socialPublishedContractMatches(binding, guard, pageState) {
const social = guard?.socialPublication;
const dispatch = binding?.socialPublication;
@@ -28059,7 +28480,11 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let result = await check();
for (let pass = 0; pass < 3; pass += 1) {
const unread = Array.isArray(result?.pageTextsNeeded) ? result.pageTextsNeeded : [];
- if (!unread.length || !await this._readUnreadPageTexts(tabId, unread)) break;
+ if (!unread.length) break;
+ const readingStarted = Date.now();
+ const read = await this._readUnreadPageTexts(tabId, unread);
+ this._predispatchStage(tabId, 'recipientReadingMs', Date.now() - readingStarted);
+ if (!read) break;
result = await check();
}
return result;
@@ -30955,11 +31380,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
return results;
};
- let rawResults = await runProbe('collect');
+ // A background publication prefetch reads the captions itself, beside
+ // its other work, so its probe goes straight to the final pass.
+ let rawResults = await runProbe(options.captionPass === 'final' ? 'final' : 'collect');
const captionsNeeded = (Array.isArray(rawResults) ? rawResults : [])
.flatMap(item => (Array.isArray(item?.captionsNeeded) ? item.captionsNeeded : []));
if (captionsNeeded.length) {
+ const readingStarted = Date.now();
await this._readControlEffects(tabId, captionsNeeded);
+ if (typeof options.onCaptionReading === 'function') options.onCaptionReading(Date.now() - readingStarted);
rawResults = await runProbe('final');
}
const detected = (Array.isArray(rawResults) ? rawResults : [])
@@ -31011,6 +31440,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
publicationSnapshot: detected.publicationSnapshot || null,
publicationAccountIdentity: String(detected.publicationAccountIdentity || '').slice(0, 300),
publicationAccountIdentityComplete: detected.publicationAccountIdentityComplete === true,
+ ...(Array.isArray(detected.publicationPrefetchCaptions) ? {
+ publicationPrefetchCaptions: detected.publicationPrefetchCaptions
+ .filter(caption => typeof caption === 'string' && caption.length <= 200).slice(0, 48),
+ } : {}),
transactionOrderIds: Array.isArray(detected.transactionOrderIds)
? detected.transactionOrderIds.slice(0, 20)
: [],
@@ -32109,7 +32542,50 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const interactiveElements = () => Array.from((findTopmostModal() || doc).querySelectorAll(
'a, button, [role="button"], [role="link"], [role="tab"], [role="menuitem"], input:not([type="hidden"]), textarea, select, input[type="button"], input[type="submit"], summary, label, [onclick], [data-action]'
)).filter(isVisible);
+ // A background publication prefetch names no control: it asks for the
+ // publish control of the composer that holds the post editor just
+ // written, found by the app's own publish marker nearest that editor. Any
+ // other field, even one beside a Post button, and a missing, hidden or
+ // ambiguous control resolve to nothing.
+ const publicationPrefetchControl = hint => {
+ const adapterName = socialPublishAdapterName();
+ if (!adapterName || !hint || typeof hint !== 'object') return null;
+ let editor = null;
+ try {
+ if (typeof hint.editorRef === 'string' && typeof window.__wb_ax_lookup === 'function') {
+ editor = window.__wb_ax_lookup(hint.editorRef);
+ } else if (typeof hint.editorSelector === 'string' && hint.editorSelector) {
+ const matches = Array.from(doc.querySelectorAll(hint.editorSelector)).filter(isVisible);
+ editor = matches.length === 1 ? matches[0] : null;
+ } else if (hint.focused === true) {
+ editor = doc.activeElement;
+ }
+ // X marks its post editor tweetTextarea_N; Bluesky's is a ProseMirror
+ // editor (composerTextInput in some builds).
+ if (!editor?.closest?.(adapterName === 'twitter'
+ ? '[data-testid^="tweetTextarea_"]'
+ : '[data-testid="composerTextInput"],.ProseMirror[contenteditable="true"]')) return null;
+ } catch {
+ return null;
+ }
+ if (!editor || editor.nodeType !== 1 || !editor.isConnected) return null;
+ const markers = adapterName === 'twitter'
+ ? '[data-testid="tweetButton"],[data-testid="tweetButtonInline"]'
+ : '[data-testid="composerPublishBtn"],[data-testid="composerPublishButton"]';
+ // The nearest ancestor holding a publish control decides; a control of
+ // another composer is refused below, so the climb can be generous.
+ for (let node = editor.parentElement, depth = 0; node && depth < 24; node = node.parentElement, depth += 1) {
+ let controls = [];
+ try { controls = Array.from(node.querySelectorAll(markers)).filter(isVisible); } catch { return null; }
+ if (!controls.length) continue;
+ if (controls.length !== 1) return null;
+ const composer = socialPublishComposerFor(controls[0]);
+ return composer && composer.contains(editor) ? controls[0] : null;
+ }
+ return null;
+ };
const resolveClickTarget = () => {
+ if (toolName === 'click_ax' && args.__wbPublicationPrefetch) return publicationPrefetchControl(args.__wbPublicationPrefetch);
if (toolName === 'click_ax' || toolName === 'set_field' || toolName === 'type_ax') {
try {
if (typeof window.__wb_ax_lookup === 'function' && typeof args.ref_id === 'string') {
@@ -32616,8 +33092,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// A click is judged by its activated control's captions and by those of
// the control that owns its activation. On a collect pass, unread ones
// are reported before anything else; fields and plain links have none.
- if (args.__wbCaptionPass === 'collect' && ['click', 'click_ax', 'iframe_click'].includes(toolName)
- && target && !isEditableActivationTarget(target)) {
+ const activationCaptions = () => {
const subject = labelControlFor(target) || target;
const subjects = [control, subject.closest?.('button,input,[role="button"],[onclick],[data-action]'),
subject.closest?.('button,input,[role="button"],[role="menuitem"],[role="menuitemradio"],[role="menuitemcheckbox"],[role="tab"],[role="option"],[role="treeitem"],[role="switch"],[role="checkbox"],[role="radio"],summary')];
@@ -32625,8 +33100,11 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// there makes a confirmation ambiguous in any language.
const dialog = subject.closest?.('dialog[open],[role="dialog"],[role="alertdialog"]');
if (dialog) subjects.push(...[...dialog.querySelectorAll('button,input[type="button"],input[type="submit"],[role="button"],[role="menuitem"]')].slice(0, 24));
- const needed = [...new Set(subjects.filter(el => el && !isEditableActivationTarget(el)).flatMap(controlCaptions))]
- .filter(key => !captionReading(key));
+ return [...new Set(subjects.filter(el => el && !isEditableActivationTarget(el)).flatMap(controlCaptions))];
+ };
+ if (args.__wbCaptionPass === 'collect' && ['click', 'click_ax', 'iframe_click'].includes(toolName)
+ && target && !isEditableActivationTarget(target)) {
+ const needed = activationCaptions().filter(key => !captionReading(key));
if (needed.length) return { captionsNeeded: needed.slice(0, 24) };
}
const actionReading = captionsReading(actionWords);
@@ -33208,6 +33686,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const submitter = owner.closest?.('button,input,[role="button"],[onclick],[data-action]') || owner;
if (isVisible(submitter) && ordinaryControlEvidence(submitter, ordinaryLabels(submitter))) info.nonCommittingControl = true;
}
+ // The captions a click on this control would have read, for the
+ // prefetch to read ahead of it.
+ if (args.__wbPublicationPrefetch && toolName === 'click_ax') info.publicationPrefetchCaptions = activationCaptions().slice(0, 48);
return info;
}
if (target && ['click', 'click_ax', 'iframe_click'].includes(toolName)
diff --git a/src/chrome/src/trace/privacy.js b/src/chrome/src/trace/privacy.js
index 47752bc0f..b2ba93b14 100644
--- a/src/chrome/src/trace/privacy.js
+++ b/src/chrome/src/trace/privacy.js
@@ -113,6 +113,32 @@ function projectDoneTiming(data) {
return { step: Number.isFinite(data?.step) ? data.step : 0, note: 'done_timing', extra };
}
+// Where one tool call spent its time before dispatch: the pipeline's stages
+// in milliseconds, the tool's name and the call's outcome, never what the
+// page or the model said.
+const PREDISPATCH_TIMING_MS_FIELDS = [
+ 'workflowAdoptMs', 'captchaGateMs', 'preparationMs', 'clarificationMs', 'submitPreflightMs',
+ 'submitProbeMs', 'submitCaptionReadingMs', 'resultBaselineMs', 'validationSnapshotMs',
+ 'workflowPreSubmitMs', 'resourceMutationMs', 'githubTransitionMs', 'workflowDispatchMs',
+ 'publicationContractMs', 'publicationAuditMs', 'publicationAuditReusedMs', 'publicationAuditAwaitedMs',
+ 'publicationRereadMs', 'submitDetectionMs', 'submitConfirmationMs', 'permissionMs', 'completionChecksMs',
+ 'recipientGuardMs', 'recipientReadingMs', 'checkpointMs', 'pipelineWaitMs', 'toolbarPreflightMs',
+ 'pipelinePublicationMs', 'feedbackRecheckMs', 'genericChatMs', 'attemptCheckpointMs',
+ 'dispatchPreparationMs', 'preDispatchMs', 'executeToolMs', 'totalMs',
+];
+function projectPredispatchTiming(data) {
+ const extra = {};
+ for (const key of PREDISPATCH_TIMING_MS_FIELDS) {
+ const value = data?.extra?.[key];
+ if (typeof value === 'number' && Number.isFinite(value) && value >= 0) extra[key] = Math.min(value, 600_000);
+ }
+ const tool = data?.extra?.tool;
+ if (typeof tool === 'string' && /^[a-z][a-z0-9_]{0,63}$/.test(tool)) extra.tool = tool;
+ const outcome = data?.extra?.outcome;
+ if (['dispatched', 'not_dispatched', 'blocked'].includes(outcome)) extra.outcome = outcome;
+ return { step: Number.isFinite(data?.step) ? data.step : 0, note: 'predispatch_timing', extra };
+}
+
function projectClickAxTiming(data) {
const extra = {};
for (const key of CLICK_AX_TIMING_MS_FIELDS) {
@@ -243,6 +269,9 @@ export function projectTraceEventData(kind, data, { includeContent = false } = {
if (kind === 'note' && data?.note === 'done_timing') {
return projectDoneTiming(data);
}
+ if (kind === 'note' && data?.note === 'predispatch_timing') {
+ return projectPredispatchTiming(data);
+ }
if (includeContent || data == null) return data;
const source = data && typeof data === 'object' && !Array.isArray(data) ? data : {};
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index e19c772d6..826e39c71 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -8995,6 +8995,45 @@ export class Agent extends LoopDetector {
if (runId) try { trace.recordNote(runId, 0, 'done_timing', timing); } catch {}
}
+ // Where one tool call spent its time before dispatch, for traces: one
+ // predispatch_timing note per consequential call, milliseconds per stage of
+ // the pipeline, never page or model content. `lap(stage)` charges the time
+ // since the previous lap to that stage. Checks nested in a stage add their
+ // own sub-steps with _predispatchStage while the call's record is open.
+ _predispatchTimer(tabId) {
+ const stages = {};
+ const startedAt = Date.now();
+ let last = startedAt;
+ (this._predispatchTimings ||= new Map()).set(tabId, stages);
+ const add = (stage, ms) => {
+ if (Number.isFinite(ms) && ms >= 0) stages[stage] = (stages[stage] || 0) + ms;
+ };
+ return {
+ lap: stage => { const now = Date.now(); add(stage, now - last); last = now; },
+ add,
+ // The whole pipeline up to this point, e.g. until dispatch begins.
+ since: stage => add(stage, Date.now() - startedAt),
+ emit: (step, extra = {}) => {
+ if (this._predispatchTimings?.get(tabId) === stages) this._predispatchTimings.delete(tabId);
+ this._recordPredispatchTiming(tabId, step, { ...stages, totalMs: Date.now() - startedAt, ...extra });
+ },
+ };
+ }
+
+ _recordPredispatchTiming(tabId, step, timing) {
+ const runId = this.currentRunId.get(tabId);
+ if (!runId) return;
+ try {
+ const pending = trace.recordNote(runId, Number.isInteger(step) ? step : null, 'predispatch_timing', timing);
+ if (pending?.catch) pending.catch(() => {});
+ } catch {}
+ }
+
+ _predispatchStage(tabId, stage, ms) {
+ const stages = this._predispatchTimings?.get(tabId);
+ if (stages && Number.isFinite(ms) && ms >= 0) stages[stage] = (stages[stage] || 0) + ms;
+ }
+
recordSystemOneVerdict(tabId, metadata, context = {}) {
const runId = context.runId || this.currentRunId.get(tabId);
if (runId) try { trace.recordNote(runId, 0, 'system_one', metadata); } catch {}
@@ -13776,11 +13815,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
continue;
}
fnArgs = coordinates.args;
+ const predispatch = this._predispatchTimer(tabId);
if (fnName !== 'done' && !Agent.NAV_TOOLS.has(fnName)) {
try {
await this._adoptLiveSocialPublishWorkflow(tabId, provider);
} catch {}
}
+ // Writing into a post composer starts compiling the publication
+ // contract in the background (see _socialPublicationContractPrefetch).
+ this._socialPublicationContractPrefetch(tabId, fnName, fnArgs, provider);
+ predispatch.lap('workflowAdoptMs');
const recordPagePreparationTimeout = async (error, stage) => {
const timeoutResult = this._contentActionPreparationTimeoutResult(fnName, error, stage);
@@ -13827,6 +13871,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
if (captchaPreflight) onUpdate('captcha_gate', captchaPreflight);
const captchaGateBlock = this._captchaGateBlockResult(tabId, fnName, fnArgs);
+ predispatch.lap('captchaGateMs');
if (captchaGateBlock) {
// This path returns before normal tool observation. Retain the signup
// checkpoint even when the first detected challenge needs manual help.
@@ -13938,6 +13983,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// the side effect may have completed before its reply was lost.
const missingResponseOutcomeUnknown = capabilities.length > 0 || Agent.STATE_CHANGE_TOOLS.has(fnName);
const executionMutationEvidence = this._isExecutionMutationEvidence(fnName, fnArgs, capabilities);
+ predispatch.lap('preparationMs');
const clarificationAuthorizationBlock = this._clarificationAuthorizationBlock(
tabId,
fnName,
@@ -14034,6 +14080,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
});
continue;
}
+ predispatch.lap('clarificationMs');
const formValidationCandidate = this._isFormValidationCandidate(fnName, fnArgs);
let formValidationAllFrames = false;
let detectedSubmitAction = null;
@@ -14047,13 +14094,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// The detector owns its supported-tool filtering. Every form
// validation candidate, including set_field({submit:true}), needs
// the same resolved-target evidence before workflow guards run.
- const detected = await this._detectLikelySubmitAction(tabId, fnName, fnArgs);
+ const probeStarted = Date.now();
+ const detected = await this._detectLikelySubmitAction(tabId, fnName, fnArgs, {
+ onCaptionReading: ms => predispatch.add('submitCaptionReadingMs', ms),
+ });
+ predispatch.add('submitProbeMs', Date.now() - probeStarted);
this._throwIfAborted(abortSignal);
const currentValidationBlock = this._formValidationBlocks.get(tabId) || null;
const obviousSubmit = this._formValidationActionLooksSubmit(fnName, fnArgs, null, detected);
if (!this._leanRunActive(tabId) && (obviousSubmit || detected?.isSubmit === true) && detected && fnName !== 'iframe_click'
&& /^ref_\d+$/.test(String(fnArgs?.ref_id || ''))) {
+ const baselineStarted = Date.now();
const baseline = await this._readCompletionResult(tabId, fnArgs.ref_id);
+ predispatch.add('resultBaselineMs', Date.now() - baselineStarted);
if (baseline?.identity && baseline.scopeRef && typeof baseline.text === 'string') {
detected.resultBaseline = { identity: baseline.identity, scopeRef: baseline.scopeRef,
textSha256: this._sha256TextSync(baseline.text) };
@@ -14064,10 +14117,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// yet, may still turn out to submit: keep the page's state from
// before the click, so a rejection it shows is new.
const caption = this._controlCaption(fnName, fnArgs, null);
+ const validationStarted = Date.now();
const before = !this._leanRunActive(tabId) && (currentValidationBlock || obviousSubmit || detected?.isSubmit === true
|| (caption && this._controlEffectReading(caption) === null))
? await this._captureFormValidationState(tabId, { allFrames })
: [];
+ predispatch.add('validationSnapshotMs', Date.now() - validationStarted);
this._throwIfAborted(abortSignal);
return {
allFrames,
@@ -14088,6 +14143,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
continue;
}
}
+ predispatch.lap('submitPreflightMs');
if (fnName === 'chrome_web_store_publish') {
detectedSubmitAction = {
isSubmit: true,
@@ -14095,12 +14151,24 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
reason: 'submit the configured Chrome Web Store release for review',
};
}
- const runWorkflowPreSubmitChecks = async () => await this._resourceMutationPreSubmitBlock(tabId, fnName, fnArgs, detectedSubmitAction, provider)
+ const timedCheck = async (stage, check) => {
+ const started = Date.now();
+ try {
+ return await check();
+ } finally {
+ predispatch.add(stage, Date.now() - started);
+ }
+ };
+ const runWorkflowPreSubmitChecks = async () => await timedCheck('resourceMutationMs',
+ () => this._resourceMutationPreSubmitBlock(tabId, fnName, fnArgs, detectedSubmitAction, provider))
|| this._steeringPriorSubmissionBlock(tabId, fnName, fnArgs, detectedSubmitAction)
- || await this._conditionalGithubCommitTransition(tabId, fnName, fnArgs, detectedSubmitAction, callState.guardAdvisories)
- || await this._workflowPreSubmitDispatchBlock(
- tabId, fnName, fnArgs, detectedSubmitAction, provider, callState.guardAdvisories,
- );
+ || await timedCheck('githubTransitionMs',
+ () => this._conditionalGithubCommitTransition(tabId, fnName, fnArgs, detectedSubmitAction, callState.guardAdvisories))
+ || await timedCheck('workflowDispatchMs', () => this._workflowPreSubmitDispatchBlock(
+ tabId, fnName, fnArgs, detectedSubmitAction, provider, callState.guardAdvisories,
+ ));
+ const predispatchConsequential = () => missingResponseOutcomeUnknown
+ || detectedSubmitAction?.isSubmit === true || detectedSubmitAction?.publicationControl === true;
let workflowPreSubmitBlock = await runWorkflowPreSubmitChecks();
if (workflowPreSubmitBlock?.mutationConfirmation
&& this.scheduledRunPolicies.get(tabId)?.requireConsequentialConfirmation !== false) {
@@ -14122,6 +14190,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { action: 'abort', value };
}
}
+ predispatch.lap('workflowPreSubmitMs');
// A quality guard that cannot verify a commit, publish or submit asks
// the user on WebBrain's card instead of refusing (guard-policy.js
// 'ask'); 'once' lets this one step go ahead with a note for the model.
@@ -14146,8 +14215,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
onUpdate('warning', { message: 'Stopped by user.' });
return { action: 'abort', value };
}
+ predispatch.lap('unverifiedStepCardMs');
}
if (workflowPreSubmitBlock) {
+ if (predispatchConsequential()) predispatch.emit(step, { tool: fnName, outcome: 'blocked' });
onUpdate('tool_call', { name: fnName, args: fnArgs, outcomeUnknown: false });
onUpdate('tool_result', { name: fnName, result: workflowPreSubmitBlock });
messages.push({
@@ -14230,7 +14301,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let submitConfirmation = detectedSubmitAction || groupedSearchForm;
if (!submitConfirmation) {
try {
- submitConfirmation = await detectSubmitWithDeadline();
+ submitConfirmation = await timedCheck('submitDetectionMs', detectSubmitWithDeadline);
} catch (error) {
if (error?.code !== 'content_action_timeout') throw error;
const loopStop = await recordPagePreparationTimeout(error, 'submit-action detection');
@@ -14401,6 +14472,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
}
}
+ predispatch.lap('submitConfirmationMs');
if (capabilities.length && !this._skipPermissionGate && !scheduledBypassesGate) {
await this.permissions.hydrate();
const curUrl = await this._currentUrl(tabId);
@@ -14531,6 +14603,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
continue;
}
}
+ predispatch.lap('permissionMs');
if (fnName === 'done' || fnName === 'done_json') {
// Each completion invariant gets its own note: one that already advised
@@ -14690,6 +14763,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// Snapshot URL before nav-prone calls. Some tools are conditional:
// Enter and set_field({submit:true}) can navigate, while other key
// presses and ordinary field edits should avoid the URL-check delay.
+ predispatch.lap('completionChecksMs');
const navigationProneCall = this._isNavigationProneToolCall(fnName, fnArgs);
let beforeUrl = '';
let afterUrl = '';
@@ -14752,7 +14826,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
if (this._hasPendingSteering(tabId)) return skipSteeredPermissionWait(toolIndex);
}
+ predispatch.lap('recipientGuardMs');
if (messageRecipientBlock) {
+ if (predispatchConsequential()) predispatch.emit(step, { tool: fnName, outcome: 'blocked' });
onUpdate('tool_call', { name: fnName, args: fnArgs, outcomeUnknown: false });
onUpdate('tool_result', { name: fnName, result: messageRecipientBlock });
messages.push({
@@ -14802,6 +14878,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { action: 'return', value, status: 'persistence_degraded' };
}
}
+ predispatch.lap('checkpointMs');
this._throwIfAborted(this._runAbortSignal(tabId));
const _toolStart = Date.now();
let steeringBeforeDispatch = false;
@@ -14816,6 +14893,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const abortSignal = linked.signal;
try {
this._throwIfAborted(abortSignal);
+ predispatch.lap('pipelineWaitMs');
const pipelineToolbarPreflight = await this._preflightRichTextToolbarTarget(
tabId,
fnName,
@@ -14823,6 +14901,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
provider,
{ onUpdate, guardAdvisories: callState.guardAdvisories },
);
+ predispatch.lap('toolbarPreflightMs');
this._throwIfAborted(abortSignal);
// The draft was asked about (and answered) before this deadline
// started. A question that comes up only now, because the draft
@@ -14837,13 +14916,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// Its notes were already given by the first check.
|| this._withoutUnverifiedStepAsk(
await this._githubCommitPreSubmitBlock(tabId, fnName, fnArgs, detectedSubmitAction, [], { recheck: true }));
+ predispatch.lap('pipelinePublicationMs');
this._throwIfAborted(abortSignal);
// Permission, checkpoint and preflight waits can receive a correction.
// Recheck after all preparation, before marking or invoking dispatch.
if (this._hasPendingSteering(tabId) || await this._refreshPageFeedbackForCall(tabId, tc, onUpdate)) return { steered: true };
+ predispatch.lap('feedbackRecheckMs');
const genericChatBlock = pipelineToolbarPreflight.block || socialDispatchBlock ? null
: await this._prepareGenericChatDispatch(tabId, fnName, fnArgs, messageRecipientExecutionContext, abortSignal,
callState.guardAdvisories);
+ predispatch.lap('genericChatMs');
this._throwIfAborted(abortSignal);
if (!pipelineToolbarPreflight.block && !socialDispatchBlock && !genericChatBlock) {
// Save the task's record with this action's sites before it can be
@@ -14858,6 +14940,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { steered: true };
}
}
+ predispatch.lap('attemptCheckpointMs');
let calculationDispatchBlock = this._pureCalculationToolBlock(tabId, fnName, fnArgs, calculationRequestScope);
if (calculationDispatchBlock && this._pureCalculationScopeAdvises(tabId, fnName, fnArgs,
calculationRequestScope, callState.guardAdvisories)) {
@@ -14874,6 +14957,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (fnName === 'calculate') this._rememberPureCalculationAttempt(tabId, fnArgs, calculationRequestScope);
callState.invoked = true;
}
+ predispatch.lap('dispatchPreparationMs');
+ predispatch.since('preDispatchMs');
const pipelineRawToolResult = pipelineToolbarPreflight.block || socialDispatchBlock || genericChatBlock || calculationDispatchBlock || await this.executeTool(
tabId,
fnName,
@@ -14895,6 +14980,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
_contentActionDispatchState: actionDispatchState,
},
);
+ predispatch.lap('executeToolMs');
callState.result = this._normalizeToolResult(fnName, pipelineRawToolResult, missingResponseOutcomeUnknown);
if (pipelineRawToolResult?.dispatched === false || pipelineRawToolResult?.noDispatch === true) {
actionDispatchState.started = false;
@@ -15014,6 +15100,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
rawToolResult = pipelineResult.rawToolResult;
toolResult = pipelineResult.toolResult;
}
+ if (predispatchConsequential()) {
+ predispatch.emit(step, { tool: fnName, outcome: steeringBeforeDispatch || attemptRecordNotSaved
+ || rawToolResult?.noDispatch === true || rawToolResult?.dispatched === false ? 'not_dispatched' : 'dispatched' });
+ }
if (attemptRecordNotSaved) {
const value = 'Stopped before sending the page action because the record of what this task has posted could not be saved. Retry after extension storage is available.';
this._appendSyntheticToolResults(tabId, toolCalls, toolIndex, messages, onUpdate, step, () => ({
@@ -15068,6 +15158,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// That also noted the call in the dispatch record; see the catch in
// _executeToolBatch for a call interrupted before this point.
callState.dispatchNoted = true;
+ // A write into a post composer starts the publication check of its
+ // draft in the background; the click on Post then finds it done.
+ this._socialPublicationPrefetch(tabId, fnName, fnArgs, toolResult, provider);
const readCompletenessBeforeTool = this.readCompletenessStates.get(tabId) || null;
const readCompletenessAfterTool = this._recordReadCompleteness(tabId, fnName, fnArgs, toolResult);
const requiredReadProgress = readCompletenessMadeProgress(
@@ -20835,6 +20928,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
_invalidateSocialPublicationContract(guard) {
+ // A contract compiled in the background before a correction is never
+ // installed after it.
+ this._socialPublicationContractPrefetches?.delete(guard);
const social = guard.socialPublication;
if (!social) return;
social.needsRecompile = true;
@@ -20971,15 +21067,33 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
delete next.deniedAudit;
delete next.deniedAuditKey;
}
+ // A verdict prefetched for the run that ended is never reused by another.
+ delete next.prefetchedAudit;
return next;
}
- async _ensureSocialPublicationContract(tabId, provider = this._activeProvider(tabId)) {
+ // One compile runs per publication state at a time: a caller arriving while
+ // one runs waits for it instead of reading its still empty contract or
+ // compiling a second time (`wait: false` returns null instead of waiting).
+ // A background compile (`background`, the prefetch) changes nothing in the
+ // run: it is kept beside the run's state and never throws. A later caller
+ // that would compile awaits it and installs it only when it compiled exactly
+ // the input that caller would compile from now: the same sources, context,
+ // task and prior state. Anything else compiles as before.
+ async _ensureSocialPublicationContract(tabId, provider = this._activeProvider(tabId), { background = false, wait = true } = {}) {
const guard = this._planExecutionGuards.get(tabId);
if (!guard?.enabled) return null;
// The app-owned guard is new for every user revision. A trusted Continue
// carries its frozen contract, including exact raw source text.
const previous = guard.socialPublication;
+ const compiles = this._socialPublicationCompiles ||= new WeakMap();
+ const running = previous ? compiles.get(previous) : null;
+ if (running) {
+ if (!wait) return null;
+ await running.catch(() => {});
+ if (this._planExecutionGuards.get(tabId) !== guard) return null;
+ return this._ensureSocialPublicationContract(tabId, provider, { background, wait });
+ }
if (previous && (!previous.needsRecompile || Object.keys(previous.outcomes || {}).length)) return previous;
const sources = this._socialPublicationSources(tabId);
// This URL was observed by the app when the user task started, before
@@ -20989,12 +21103,66 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
requestPlatform: SOCIAL_PLATFORMS.includes(requestWorkflow?.adapterName) ? requestWorkflow.adapterName : null,
};
const key = this._sha256TextSync(JSON.stringify({ sources, context, taskKey: guard.taskKey }));
+ const prefetches = this._socialPublicationContractPrefetches ||= new WeakMap();
+ const prefetch = prefetches.get(guard);
+ if (prefetch && prefetch.previous === previous && prefetch.state.key === key) {
+ if (background) return prefetch.done;
+ if (!prefetch.settled) {
+ if (!wait) return null;
+ await prefetch.done;
+ if (this._planExecutionGuards.get(tabId) !== guard) return null;
+ // Checked again: the sources may have changed while it ran.
+ return this._ensureSocialPublicationContract(tabId, provider, { background, wait });
+ }
+ prefetches.delete(guard);
+ if (prefetch.compiled && !this._checkAbort(tabId) && !this._hasPendingSteering(tabId)) {
+ guard.socialPublication = prefetch.compiled;
+ return prefetch.compiled;
+ }
+ } else if (prefetch && !background) {
+ // A background compile of another input is never installed.
+ prefetches.delete(guard);
+ }
const state = { key, sources, context, contract: null, outcomes: {}, actionId: null, dispatch: null, error: '' };
+ if (background) {
+ if (!provider?.chat || JSON.stringify(sources).length > 120000) return null;
+ const entry = { previous, state, settled: false, compiled: null, done: null };
+ prefetches.set(guard, entry);
+ entry.done = this._socialPublicationCompile(tabId, provider, guard, state, previous, true)
+ .catch(() => null)
+ .then(compiled => {
+ entry.settled = true;
+ entry.compiled = compiled || null;
+ if (!compiled && prefetches.get(guard) === entry) prefetches.delete(guard);
+ return entry.compiled;
+ });
+ return entry.done;
+ }
guard.socialPublication = state;
if (!provider?.chat || JSON.stringify(sources).length > 120000) {
state.error = 'Publication intent could not be read completely with the selected provider.';
return state;
}
+ const compile = this._socialPublicationCompile(tabId, provider, guard, state, previous, false);
+ compiles.set(state, compile);
+ let outcome;
+ try {
+ outcome = await compile;
+ } finally {
+ if (compiles.get(state) === compile) compiles.delete(state);
+ }
+ return outcome === 'recompile' ? this._ensureSocialPublicationContract(tabId, provider, { wait }) : outcome;
+ }
+
+ // The compile itself: the compiled state, null when the run or its task
+ // moved on, or 'recompile' when a correction arrived meanwhile. A background
+ // compile stops once the run's state or its own slot moved on, and leaves
+ // the click its own attempts when it cannot finish.
+ async _socialPublicationCompile(tabId, provider, guard, state, previous, background) {
+ const { sources, context } = state;
+ const replaced = () => (background
+ ? guard.socialPublication !== previous || this._socialPublicationContractPrefetches?.get(guard)?.state !== state
+ : guard.socialPublication !== state || state.needsRecompile);
const messages = publicationContractMessages(sources, context);
let portable = false;
for (let attempt = 0; attempt < 2; attempt++) {
@@ -21012,7 +21180,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
await this._tracePlannerAttemptResponse(runId, 0, provider, response,
'social_publication_contract', attempt + 1, startedAt, options);
if (this._planExecutionGuards.get(tabId) !== guard || this._checkAbort(tabId)) return null;
- if (guard.socialPublication !== state || state.needsRecompile) return this._ensureSocialPublicationContract(tabId, provider);
+ if (replaced()) return background ? null : 'recompile';
if (this._hasPendingSteering(tabId)) return null;
state.contract = normalizePublicationContract(this._socialPublicationResponseObject(response, options), sources);
// A clarification invalidates authorization, not the uploads already
@@ -21027,16 +21195,29 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
state.error = '';
return state;
} catch (error) {
- if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
+ if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) {
+ if (background) return null;
+ throw error;
+ }
await this._socialPublicationCheckerFailure(tabId, 'social_publication_contract', attempt + 1, error, received);
- if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== state
- || state.needsRecompile || this._hasPendingSteering(tabId)) return null;
- state.error = 'Failed while reading the publication request. The checker could not return a complete result; the draft was not published.';
- state.errorKind = 'technical';
+ if (this._planExecutionGuards.get(tabId) !== guard || replaced() || this._hasPendingSteering(tabId)) return null;
+ if (!background) {
+ state.error = 'Failed while reading the publication request. The checker could not return a complete result; the draft was not published.';
+ state.errorKind = 'technical';
+ }
if (this._socialPublicationOptionsRejected(error)) portable = true;
}
}
- return state;
+ return background ? null : state;
+ }
+
+ // Whether `social` is the run's publication state, or the one a background
+ // compile holds for it while it could still be installed.
+ _socialPublicationStateCurrent(guard, social) {
+ if (!social) return false;
+ if (guard.socialPublication === social) return true;
+ const prefetch = this._socialPublicationContractPrefetches?.get(guard);
+ return prefetch?.compiled === social && prefetch.previous === guard.socialPublication;
}
_socialPublicationAction(guard) {
@@ -21113,7 +21294,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const liveUrl = await this._currentUrl(tabId);
const live = resolveAdapterWorkflowJob(liveUrl, 'publish-post');
if (!SOCIAL_PLATFORMS.includes(live?.adapterName)) return false;
- const social = await this._ensureSocialPublicationContract(tabId, provider);
+ // Only the click waits for a contract still being compiled; an ordinary
+ // tool call binds on a later call instead of waiting here.
+ const social = await this._ensureSocialPublicationContract(tabId, provider, { wait: !!detected });
if (!social || social.needsRecompile || social.contract?.status !== 'ready') return false;
this._recordSocialPublishTargetSatisfied(guard);
const eligible = publicationProgress(social.contract, social.outcomes).eligible;
@@ -21253,8 +21436,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
if (!['click', 'click_ax', 'iframe_click'].includes(name)) return blocked('Write and verify the draft first, then activate its publish control in a separate click.');
if (detected.publicationControl !== true) return blocked('Publication composer ownership could not be observed. Read the current page and use its resolved publish control.');
+ const contractStarted = Date.now();
await this._adoptLiveSocialPublishWorkflow(tabId, provider, detected);
const social = await this._ensureSocialPublicationContract(tabId, provider);
+ this._predispatchStage(tabId, 'publicationContractMs', Date.now() - contractStarted);
const action = this._socialPublicationAction(guard);
if (social?.needsRecompile || !action || action.platform !== live.adapterName
|| !publicationProgress(social?.contract, social?.outcomes).eligible.includes(action.id)) {
@@ -21334,57 +21519,28 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (social.deniedAuditKey === key && !bypassMatches) return auditBlocked();
if (social.dispatch?.key !== key) {
if (!bypassMatches) {
- const auditProvider = provider || this._activeProvider(tabId);
- const messages = publicationAuditMessages(social.sources, social.contract, action, snapshot, key, social.context);
- let portable = false;
- let authorized = false;
- for (let attempt = 0; attempt < 2; attempt++) {
- const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
- const options = this._socialPublicationChatOptions(auditProvider, attempt > 0, portable, true);
- const runId = this.currentRunId.get(tabId);
- const startedAt = Date.now();
- let received = false;
- try {
- await this._tracePlannerAttemptRequest(runId, 0, auditProvider, requestMessages,
- 'social_publication_authorization', attempt + 1, this._effectiveRunMode(tabId), options);
- const response = await this._chatWithCostAllowance(auditProvider, requestMessages, options,
- this.currentCostState.get(tabId) || null, { tabId, generationName: 'social_publication_authorization' });
- received = true;
- await this._tracePlannerAttemptResponse(runId, 0, auditProvider, response,
- 'social_publication_authorization', attempt + 1, startedAt, options);
- if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social
- || social.needsRecompile || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
- || guard.taskKey !== taskKey) {
- return blocked('Publication context changed during authorization.');
- }
- const verdict = publicationAuditValidation(this._socialPublicationResponseObject(response, options), key, action.id);
- if (!verdict.valid) throw new Error(verdict.failure);
- if (!verdict.authorized) {
- social.deniedAuditKey = key;
- social.deniedAudit = { status: 'denied', reason: verdict.reason };
- return auditBlocked();
- }
- authorized = true;
- delete social.deniedAudit;
- delete social.deniedAuditKey;
- break;
- } catch (error) {
- if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
- await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received);
- if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social
- || social.needsRecompile || this._hasPendingSteering(tabId)
- || guard.taskKey !== taskKey) {
- return blocked('Publication context changed during authorization.');
- }
+ const auditStarted = Date.now();
+ // A background prefetch audited this exact key (or still is): its
+ // verdict decides once, exactly as this audit would have. Any other
+ // key, or no verdict, runs the audit now.
+ const prefetched = await this._socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey);
+ if (prefetched?.changed) return blocked('Publication context changed during authorization.');
+ if (prefetched) {
+ this._predispatchStage(tabId, prefetched.awaited ? 'publicationAuditAwaitedMs' : 'publicationAuditReusedMs', Date.now() - auditStarted);
+ if (!prefetched.authorized) {
social.deniedAuditKey = key;
- social.deniedAudit = { status: received ? 'invalid_response' : 'unavailable',
- reason: received ? 'The publication checker returned an invalid or mismatched authorization response.'
- : 'The selected provider could not complete the publication check.',
- validationFailure: String(error?.message || error).slice(0, 600) };
- if (this._socialPublicationOptionsRejected(error)) portable = true;
+ social.deniedAudit = { status: 'denied', reason: prefetched.reason };
+ return auditBlocked();
}
+ delete social.deniedAudit;
+ delete social.deniedAuditKey;
+ } else {
+ const outcome = await this._socialPublicationAuditRun(tabId, provider || this._activeProvider(tabId),
+ guard, social, action, snapshot, key, taskKey, { record: true });
+ this._predispatchStage(tabId, 'publicationAuditMs', Date.now() - auditStarted);
+ if (outcome.status === 'changed') return blocked('Publication context changed during authorization.');
+ if (outcome.status !== 'authorized') return auditBlocked();
}
- if (!authorized) return auditBlocked();
}
social.dispatch = { key, actionId: action.id, snapshot: JSON.parse(JSON.stringify(snapshot)), pageUrl,
documentIdentity, frameId, ...(bypassMatches ? { auditBypassed: true } : {}),
@@ -21392,7 +21548,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
// The semantic check can take time. Re-read the same target afterward;
// neither a changed draft/account nor a navigated document inherits it.
+ const rereadStarted = Date.now();
const fresh = await this._detectLikelySubmitAction(tabId, name, args);
+ this._predispatchStage(tabId, 'publicationRereadMs', Date.now() - rereadStarted);
if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social || social.needsRecompile
|| this._checkAbort(tabId) || this._hasPendingSteering(tabId) || guard.taskKey !== taskKey
|| fresh?.isSubmit !== true || fresh.publicationControl !== true || fresh.publicationResourceUrlsComplete !== true
@@ -21408,6 +21566,270 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return null;
}
+ // The publication audit of one concrete draft: the same request, schema,
+ // two attempts, format repair and validation for the click and for its
+ // background prefetch. `record` (the click) records each outcome on the
+ // publication state as it goes; the prefetch records nothing here.
+ // `isCurrent` tells whether `social` is still the run's publication state.
+ async _socialPublicationAuditRun(tabId, provider, guard, social, action, snapshot, key, taskKey,
+ { record = false, isCurrent = () => guard.socialPublication === social } = {}) {
+ const messages = publicationAuditMessages(social.sources, social.contract, action, snapshot, key, social.context);
+ let portable = false;
+ let failure = null;
+ for (let attempt = 0; attempt < 2; attempt++) {
+ const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
+ const options = this._socialPublicationChatOptions(provider, attempt > 0, portable, true);
+ const runId = this.currentRunId.get(tabId);
+ const startedAt = Date.now();
+ let received = false;
+ try {
+ await this._tracePlannerAttemptRequest(runId, 0, provider, requestMessages,
+ 'social_publication_authorization', attempt + 1, this._effectiveRunMode(tabId), options);
+ const response = await this._chatWithCostAllowance(provider, requestMessages, options,
+ this.currentCostState.get(tabId) || null, { tabId, generationName: 'social_publication_authorization' });
+ received = true;
+ await this._tracePlannerAttemptResponse(runId, 0, provider, response,
+ 'social_publication_authorization', attempt + 1, startedAt, options);
+ if (this._planExecutionGuards.get(tabId) !== guard || !isCurrent()
+ || social.needsRecompile || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || guard.taskKey !== taskKey) {
+ return { status: 'changed' };
+ }
+ const verdict = publicationAuditValidation(this._socialPublicationResponseObject(response, options), key, action.id);
+ if (!verdict.valid) throw new Error(verdict.failure);
+ if (!verdict.authorized) {
+ if (record) {
+ social.deniedAuditKey = key;
+ social.deniedAudit = { status: 'denied', reason: verdict.reason };
+ }
+ return { status: 'denied', reason: verdict.reason };
+ }
+ if (record) {
+ delete social.deniedAudit;
+ delete social.deniedAuditKey;
+ }
+ return { status: 'authorized', reason: verdict.reason };
+ } catch (error) {
+ if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
+ await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received);
+ if (this._planExecutionGuards.get(tabId) !== guard || !isCurrent()
+ || social.needsRecompile || this._hasPendingSteering(tabId)
+ || guard.taskKey !== taskKey) {
+ return { status: 'changed' };
+ }
+ failure = { status: received ? 'invalid_response' : 'unavailable',
+ reason: received ? 'The publication checker returned an invalid or mismatched authorization response.'
+ : 'The selected provider could not complete the publication check.',
+ validationFailure: String(error?.message || error).slice(0, 600) };
+ if (record) {
+ social.deniedAuditKey = key;
+ social.deniedAudit = failure;
+ }
+ if (this._socialPublicationOptionsRejected(error)) portable = true;
+ }
+ }
+ return { status: 'failed', failure };
+ }
+
+ // The verdict a background prefetch reached for this exact key, awaiting
+ // one still running for it. The click decides here once: a stored verdict is
+ // used at most once, and a prefetch still running for another draft can no
+ // longer store anything. A run, task or correction that moved on meanwhile
+ // is reported as a change, as after the click's own audit.
+ async _socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey) {
+ const audits = this._socialPublicationAuditPrefetches;
+ const running = audits?.get(social);
+ let awaited = false;
+ if (running?.key === key && social.prefetchedAudit?.key !== key) {
+ awaited = true;
+ await running.done;
+ }
+ if (running && audits.get(social) === running) audits.delete(social);
+ const stored = social.prefetchedAudit?.key === key ? social.prefetchedAudit : null;
+ delete social.prefetchedAudit;
+ if (!stored) return null;
+ if (this._planExecutionGuards.get(tabId) !== guard || guard.socialPublication !== social
+ || social.needsRecompile || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || guard.taskKey !== taskKey) return { changed: true };
+ return { authorized: stored.authorized === true, reason: String(stored.reason || ''), awaited };
+ }
+
+ // After a text write into an X or Bluesky post editor, or a file added to
+ // the composer last written, the click on Post will need the publication
+ // contract, an audit of this exact draft and readings of what the composer's
+ // controls do. They are prepared here in the background so the click does
+ // not wait for them. Nothing here authorizes anything or changes the run:
+ // the contract is installed only by a caller that would compile exactly the
+ // same input, the click recomputes the binding key from its own fresh probe
+ // and uses a verdict only for that exact key, and every later check still
+ // runs. The prefetch writes no transcript and emits no UI event (only trace
+ // notes), and its model calls go through the run's cost allowance.
+ _socialPublicationPrefetch(tabId, name, args = {}, result = null, provider = null) {
+ try {
+ const guard = this._planExecutionGuards.get(tabId);
+ if (!this._socialPublicationPrefetchAllowed(guard) || !provider?.chat
+ || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || !result || typeof result !== 'object' || result.success !== true
+ || result.noDispatch === true || result.outcomeUnknown === true) return null;
+ const hints = this._socialPublicationPrefetchHints ||= new WeakMap();
+ const hint = name === 'upload_file' ? hints.get(guard) || null : this._socialPublicationPrefetchHint(name, args);
+ if (!hint) return null;
+ hints.set(guard, hint);
+ return this._socialPublicationPrefetchRun(tabId, guard, provider, hint).catch(() => null);
+ } catch {
+ return null;
+ }
+ }
+
+ // The contract depends on the request, never on the draft, so its compile
+ // starts as soon as the agent begins a write into a post editor, while that
+ // write is still being prepared. The audit waits for the written draft.
+ _socialPublicationContractPrefetch(tabId, name, args = {}, provider = null) {
+ try {
+ const guard = this._planExecutionGuards.get(tabId);
+ const hint = this._socialPublicationPrefetchHint(name, args);
+ const social = guard?.socialPublication;
+ const pending = this._socialPublicationContractPrefetches?.get(guard);
+ if (!hint || !this._socialPublicationPrefetchAllowed(guard) || !provider?.chat
+ || this._checkAbort(tabId) || this._hasPendingSteering(tabId)
+ || (social && (this._socialPublicationCompiles?.get(social)
+ || !social.needsRecompile || Object.keys(social.outcomes || {}).length))
+ || (pending && pending.previous === social)) return null;
+ return this._socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly: true }).catch(() => null);
+ } catch {
+ return null;
+ }
+ }
+
+ // A write may prepare the click on Post only in a run that executes a plan
+ // submitting something and is not bound to another site job (a message to
+ // send, a file to commit): the planner told those apart from a post.
+ _socialPublicationPrefetchAllowed(guard) {
+ const job = guard?.siteWorkflow?.job;
+ return !!guard?.enabled && guard.requiresSubmission === true
+ && (!job || (job.id === 'publish-post' && SOCIAL_PLATFORMS.includes(guard.siteWorkflow.adapterName)));
+ }
+
+ // The editor a text write targets: its ref, its selector, or the focused
+ // element.
+ _socialPublicationPrefetchHint(name, args = {}) {
+ if ((name === 'type_ax' || (name === 'set_field' && args?.submit !== true))
+ && typeof args?.ref_id === 'string' && args.ref_id) return { editorRef: args.ref_id };
+ if (name !== 'type_text') return null;
+ return typeof args?.selector === 'string' && args.selector.trim()
+ ? { editorSelector: args.selector.trim() } : { focused: true };
+ }
+
+ async _socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly = false } = {}) {
+ const startedAt = Date.now();
+ const runId = this.currentRunId.get(tabId);
+ const taskKey = guard.taskKey;
+ const current = () => this._planExecutionGuards.get(tabId) === guard && guard.taskKey === taskKey
+ && !this._checkAbort(tabId) && !this._hasPendingSteering(tabId);
+ let status = 'skipped';
+ try {
+ const pageUrl = await this._currentUrl(tabId);
+ if (!current() || !SOCIAL_PLATFORMS.includes(resolveAdapterWorkflowJob(pageUrl, 'publish-post')?.adapterName)) return status;
+ // The same probe the click runs, on the publish control of the composer
+ // that holds the post editor written (found by the app's own markers).
+ const detected = await this._detectLikelySubmitAction(tabId, 'click_ax', { __wbPublicationPrefetch: hint }, { captionPass: 'final' });
+ if (!current() || detected?.isSubmit !== true || detected.publicationControl !== true) return status;
+ const readings = this._socialPublicationPrefetchReadings(tabId, detected.publicationPrefetchCaptions, pageUrl);
+ try {
+ const social = await this._ensureSocialPublicationContract(tabId, provider, { background: true });
+ status = contractOnly
+ ? (social?.contract && this._socialPublicationStateCurrent(guard, social) ? 'contract_ready' : 'skipped')
+ : await this._socialPublicationPrefetchAudit(tabId, guard, provider, social, detected, pageUrl, taskKey, current);
+ } finally {
+ await readings;
+ }
+ return status;
+ } catch {
+ return (status = 'failed');
+ } finally {
+ // Noted in the trace of the run that started it.
+ if (runId) {
+ try {
+ const pending = trace.recordNote(runId, 0, 'publication_prefetch', { status, durationMs: Date.now() - startedAt });
+ if (pending?.catch) pending.catch(() => {});
+ } catch {}
+ }
+ }
+ }
+
+ async _socialPublicationPrefetchAudit(tabId, guard, provider, social, detected, pageUrl, taskKey, current) {
+ const isCurrent = () => this._socialPublicationStateCurrent(guard, social);
+ if (!current() || !isCurrent() || social.needsRecompile
+ || social.contract?.status !== 'ready' || social.retryBypass) return 'skipped';
+ // The action the click's own workflow binding chooses for this site.
+ const live = resolveAdapterWorkflowJob(pageUrl, 'publish-post');
+ const eligible = publicationProgress(social.contract, social.outcomes).eligible;
+ const action = social.contract.actions.find(a => a.platform === live?.adapterName && eligible.includes(a.id));
+ if (!action) return 'skipped';
+ // The click audits only a draft that passes its preflight; so does this.
+ const snapshot = this._socialPublicationSnapshot(guard, detected.publicationSnapshot);
+ if (this._socialSnapshotActionIssues(action, snapshot).length
+ || !Array.isArray(detected.publicationResourceUrls) || detected.publicationResourceUrlsComplete !== true) return 'skipped';
+ const documentIdentity = detected.frameDocumentIdentity || '';
+ const frameId = Number.isInteger(detected.frameId) ? detected.frameId : 0;
+ const key = this._sha256TextSync(JSON.stringify({ contract: social.key, action, snapshot, pageUrl, documentIdentity, frameId }));
+ if (social.dispatch?.key === key || social.deniedAuditKey === key || social.prefetchedAudit?.key === key) return 'cached';
+ const audits = this._socialPublicationAuditPrefetches ||= new WeakMap();
+ const entry = { key, done: null };
+ audits.set(social, entry);
+ entry.done = (async () => {
+ try {
+ const outcome = await this._socialPublicationAuditRun(tabId, provider, guard, social, action, snapshot, key, taskKey, { isCurrent });
+ if (outcome.status !== 'authorized' && outcome.status !== 'denied') return outcome.status;
+ // Stored only while nothing moved on: the same run, task, contract
+ // and page, no newer prefetch, and no click that already decided.
+ const unchanged = () => audits.get(social) === entry && current() && isCurrent()
+ && !social.needsRecompile && social.dispatch?.key !== key && social.deniedAuditKey !== key;
+ if (!unchanged()) return 'changed';
+ const liveUrl = await this._currentUrl(tabId);
+ if (!unchanged() || this._normalizeUrl(liveUrl) !== this._normalizeUrl(pageUrl)) return 'changed';
+ social.prefetchedAudit = { key, authorized: outcome.status === 'authorized', reason: outcome.reason, at: Date.now() };
+ return outcome.status;
+ } catch {
+ return 'failed';
+ } finally {
+ if (audits.get(social) === entry) audits.delete(social);
+ }
+ })();
+ return entry.done;
+ }
+
+ // The readings the click's own checks ask for, asked now: what the publish
+ // control and the controls beside it do (the submit probe), and, on a page
+ // whose sends the recipient guard checks, whether activating them sends a
+ // message. Readings are kept per caption, so asking earlier changes no
+ // answer; a caption that differs at the click is read there as before.
+ _socialPublicationPrefetchReadings(tabId, captions, pageUrl) {
+ const list = [...new Set((Array.isArray(captions) ? captions : [])
+ .map(caption => String(caption ?? '').replace(/\s+/g, ' ').trim())
+ .filter(caption => caption && caption.length <= 200))];
+ // A caption an earlier prefetch is still reading is not asked twice.
+ const reading = this._socialPublicationPrefetchReading ||= new Set();
+ const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null
+ && !reading.has(kind + '\u0000' + caption));
+ const read = (kind, texts, request) => {
+ for (const text of texts) reading.add(kind + '\u0000' + text);
+ return Promise.resolve().then(request)
+ .finally(() => { for (const text of texts) reading.delete(kind + '\u0000' + text); });
+ };
+ const reads = [];
+ const effects = unread('control_effect').slice(0, 24);
+ if (effects.length) reads.push(read('control_effect', effects, () => this._readControlEffects(tabId, effects)));
+ let guarded = false;
+ try { guarded = getMessageRecipientGuardPolicy(pageUrl)?.verifyActiveRecipient === true; } catch {}
+ const controls = guarded ? unread('message_control').slice(0, 48) : [];
+ if (controls.length) {
+ reads.push(read('message_control', controls, () => this._readPageTexts(tabId, 'message_control', controls, {},
+ { maxChars: 400, mainTimeoutMs: CONTROL_READING_TIMEOUT_MS })));
+ }
+ return Promise.allSettled(reads);
+ }
+
_socialPublishedContractMatches(binding, guard, pageState) {
const social = guard?.socialPublication;
const dispatch = binding?.socialPublication;
@@ -25172,7 +25594,11 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let result = await check();
for (let pass = 0; pass < 3; pass += 1) {
const unread = Array.isArray(result?.pageTextsNeeded) ? result.pageTextsNeeded : [];
- if (!unread.length || !await this._readUnreadPageTexts(tabId, unread)) break;
+ if (!unread.length) break;
+ const readingStarted = Date.now();
+ const read = await this._readUnreadPageTexts(tabId, unread);
+ this._predispatchStage(tabId, 'recipientReadingMs', Date.now() - readingStarted);
+ if (!read) break;
result = await check();
}
return result;
@@ -28065,11 +28491,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
return results;
};
- let rawResults = await runProbe('collect');
+ // A background publication prefetch reads the captions itself, beside
+ // its other work, so its probe goes straight to the final pass.
+ let rawResults = await runProbe(options.captionPass === 'final' ? 'final' : 'collect');
const captionsNeeded = (Array.isArray(rawResults) ? rawResults : [])
.flatMap(item => (Array.isArray(item?.captionsNeeded) ? item.captionsNeeded : []));
if (captionsNeeded.length) {
+ const readingStarted = Date.now();
await this._readControlEffects(tabId, captionsNeeded);
+ if (typeof options.onCaptionReading === 'function') options.onCaptionReading(Date.now() - readingStarted);
rawResults = await runProbe('final');
}
const detected = (Array.isArray(rawResults) ? rawResults : [])
@@ -28121,6 +28551,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
publicationSnapshot: detected.publicationSnapshot || null,
publicationAccountIdentity: String(detected.publicationAccountIdentity || '').slice(0, 300),
publicationAccountIdentityComplete: detected.publicationAccountIdentityComplete === true,
+ ...(Array.isArray(detected.publicationPrefetchCaptions) ? {
+ publicationPrefetchCaptions: detected.publicationPrefetchCaptions
+ .filter(caption => typeof caption === 'string' && caption.length <= 200).slice(0, 48),
+ } : {}),
transactionOrderIds: Array.isArray(detected.transactionOrderIds)
? detected.transactionOrderIds.slice(0, 20)
: [],
@@ -29131,7 +29565,50 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const interactiveElements = () => Array.from((findTopmostModal() || doc).querySelectorAll(
'a, button, [role="button"], [role="link"], [role="tab"], [role="menuitem"], input:not([type="hidden"]), textarea, select, input[type="button"], input[type="submit"], summary, label, [onclick], [data-action]'
)).filter(isVisible);
+ // A background publication prefetch names no control: it asks for the
+ // publish control of the composer that holds the post editor just
+ // written, found by the app's own publish marker nearest that editor. Any
+ // other field, even one beside a Post button, and a missing, hidden or
+ // ambiguous control resolve to nothing.
+ const publicationPrefetchControl = hint => {
+ const adapterName = socialPublishAdapterName();
+ if (!adapterName || !hint || typeof hint !== 'object') return null;
+ let editor = null;
+ try {
+ if (typeof hint.editorRef === 'string' && typeof window.__wb_ax_lookup === 'function') {
+ editor = window.__wb_ax_lookup(hint.editorRef);
+ } else if (typeof hint.editorSelector === 'string' && hint.editorSelector) {
+ const matches = Array.from(doc.querySelectorAll(hint.editorSelector)).filter(isVisible);
+ editor = matches.length === 1 ? matches[0] : null;
+ } else if (hint.focused === true) {
+ editor = doc.activeElement;
+ }
+ // X marks its post editor tweetTextarea_N; Bluesky's is a ProseMirror
+ // editor (composerTextInput in some builds).
+ if (!editor?.closest?.(adapterName === 'twitter'
+ ? '[data-testid^="tweetTextarea_"]'
+ : '[data-testid="composerTextInput"],.ProseMirror[contenteditable="true"]')) return null;
+ } catch {
+ return null;
+ }
+ if (!editor || editor.nodeType !== 1 || !editor.isConnected) return null;
+ const markers = adapterName === 'twitter'
+ ? '[data-testid="tweetButton"],[data-testid="tweetButtonInline"]'
+ : '[data-testid="composerPublishBtn"],[data-testid="composerPublishButton"]';
+ // The nearest ancestor holding a publish control decides; a control of
+ // another composer is refused below, so the climb can be generous.
+ for (let node = editor.parentElement, depth = 0; node && depth < 24; node = node.parentElement, depth += 1) {
+ let controls = [];
+ try { controls = Array.from(node.querySelectorAll(markers)).filter(isVisible); } catch { return null; }
+ if (!controls.length) continue;
+ if (controls.length !== 1) return null;
+ const composer = socialPublishComposerFor(controls[0]);
+ return composer && composer.contains(editor) ? controls[0] : null;
+ }
+ return null;
+ };
const resolveClickTarget = () => {
+ if (toolName === 'click_ax' && args.__wbPublicationPrefetch) return publicationPrefetchControl(args.__wbPublicationPrefetch);
if (toolName === 'click_ax' || toolName === 'set_field' || toolName === 'type_ax') {
try {
if (typeof window.__wb_ax_lookup === 'function' && typeof args.ref_id === 'string') {
@@ -29603,8 +30080,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// A click is judged by its activated control's captions and by those of
// the control that owns its activation. On a collect pass, unread ones
// are reported before anything else; fields and plain links have none.
- if (args.__wbCaptionPass === 'collect' && ['click', 'click_ax', 'iframe_click'].includes(toolName)
- && target && !isEditableActivationTarget(target)) {
+ const activationCaptions = () => {
const subject = labelControlFor(target) || target;
const subjects = [control, subject.closest?.('button,input,[role="button"],[onclick],[data-action]'),
subject.closest?.('button,input,[role="button"],[role="menuitem"],[role="menuitemradio"],[role="menuitemcheckbox"],[role="tab"],[role="option"],[role="treeitem"],[role="switch"],[role="checkbox"],[role="radio"],summary')];
@@ -29612,8 +30088,11 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// there makes a confirmation ambiguous in any language.
const dialog = subject.closest?.('dialog[open],[role="dialog"],[role="alertdialog"]');
if (dialog) subjects.push(...[...dialog.querySelectorAll('button,input[type="button"],input[type="submit"],[role="button"],[role="menuitem"]')].slice(0, 24));
- const needed = [...new Set(subjects.filter(el => el && !isEditableActivationTarget(el)).flatMap(controlCaptions))]
- .filter(key => !captionReading(key));
+ return [...new Set(subjects.filter(el => el && !isEditableActivationTarget(el)).flatMap(controlCaptions))];
+ };
+ if (args.__wbCaptionPass === 'collect' && ['click', 'click_ax', 'iframe_click'].includes(toolName)
+ && target && !isEditableActivationTarget(target)) {
+ const needed = activationCaptions().filter(key => !captionReading(key));
if (needed.length) return { captionsNeeded: needed.slice(0, 24) };
}
const actionReading = captionsReading(actionWords);
@@ -30195,6 +30674,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const submitter = owner.closest?.('button,input,[role="button"],[onclick],[data-action]') || owner;
if (isVisible(submitter) && ordinaryControlEvidence(submitter, ordinaryLabels(submitter))) info.nonCommittingControl = true;
}
+ // The captions a click on this control would have read, for the
+ // prefetch to read ahead of it.
+ if (args.__wbPublicationPrefetch && toolName === 'click_ax') info.publicationPrefetchCaptions = activationCaptions().slice(0, 48);
return info;
}
if (target && ['click', 'click_ax', 'iframe_click'].includes(toolName)
diff --git a/src/firefox/src/trace/privacy.js b/src/firefox/src/trace/privacy.js
index 47752bc0f..b2ba93b14 100644
--- a/src/firefox/src/trace/privacy.js
+++ b/src/firefox/src/trace/privacy.js
@@ -113,6 +113,32 @@ function projectDoneTiming(data) {
return { step: Number.isFinite(data?.step) ? data.step : 0, note: 'done_timing', extra };
}
+// Where one tool call spent its time before dispatch: the pipeline's stages
+// in milliseconds, the tool's name and the call's outcome, never what the
+// page or the model said.
+const PREDISPATCH_TIMING_MS_FIELDS = [
+ 'workflowAdoptMs', 'captchaGateMs', 'preparationMs', 'clarificationMs', 'submitPreflightMs',
+ 'submitProbeMs', 'submitCaptionReadingMs', 'resultBaselineMs', 'validationSnapshotMs',
+ 'workflowPreSubmitMs', 'resourceMutationMs', 'githubTransitionMs', 'workflowDispatchMs',
+ 'publicationContractMs', 'publicationAuditMs', 'publicationAuditReusedMs', 'publicationAuditAwaitedMs',
+ 'publicationRereadMs', 'submitDetectionMs', 'submitConfirmationMs', 'permissionMs', 'completionChecksMs',
+ 'recipientGuardMs', 'recipientReadingMs', 'checkpointMs', 'pipelineWaitMs', 'toolbarPreflightMs',
+ 'pipelinePublicationMs', 'feedbackRecheckMs', 'genericChatMs', 'attemptCheckpointMs',
+ 'dispatchPreparationMs', 'preDispatchMs', 'executeToolMs', 'totalMs',
+];
+function projectPredispatchTiming(data) {
+ const extra = {};
+ for (const key of PREDISPATCH_TIMING_MS_FIELDS) {
+ const value = data?.extra?.[key];
+ if (typeof value === 'number' && Number.isFinite(value) && value >= 0) extra[key] = Math.min(value, 600_000);
+ }
+ const tool = data?.extra?.tool;
+ if (typeof tool === 'string' && /^[a-z][a-z0-9_]{0,63}$/.test(tool)) extra.tool = tool;
+ const outcome = data?.extra?.outcome;
+ if (['dispatched', 'not_dispatched', 'blocked'].includes(outcome)) extra.outcome = outcome;
+ return { step: Number.isFinite(data?.step) ? data.step : 0, note: 'predispatch_timing', extra };
+}
+
function projectClickAxTiming(data) {
const extra = {};
for (const key of CLICK_AX_TIMING_MS_FIELDS) {
@@ -243,6 +269,9 @@ export function projectTraceEventData(kind, data, { includeContent = false } = {
if (kind === 'note' && data?.note === 'done_timing') {
return projectDoneTiming(data);
}
+ if (kind === 'note' && data?.note === 'predispatch_timing') {
+ return projectPredispatchTiming(data);
+ }
if (includeContent || data == null) return data;
const source = data && typeof data === 'object' && !Array.isArray(data) ? data : {};
diff --git a/test/publication-prefetch.mjs b/test/publication-prefetch.mjs
new file mode 100644
index 000000000..9cf1b1bfb
--- /dev/null
+++ b/test/publication-prefetch.mjs
@@ -0,0 +1,522 @@
+import { strict as assert } from 'node:assert';
+import { test } from 'node:test';
+
+// The X/Bluesky publication contract and draft audit start in the background
+// after the draft is written, so the click on Post does not wait for them.
+// These tests drive the real Agent with a fake checker model that records
+// every call with its start and end, and can hold a call open until released.
+
+const ref = (text, source = 'request') => ({ source, start: text, end: text });
+const count = (type = 'any', min = 0, max = 0, format = null) => ({ kind: 'count', type, format, min, max });
+const rawAction = (id, platform, text = 'Hello', kind = 'exact') => ({ id, platform, account: null,
+ posts: [{ body: { kind, source: ref(text) }, media: count(), context: { kind: 'post', target: null } }] });
+const rawContract = (actions = [rawAction('p1', 'twitter')], requirements = 'p1') => ({
+ version: 1, status: 'ready', actions, requirements, prohibited: [], reason: 'User requested publication.',
+});
+const snapshot = (bodyText = 'Hello', account = 'twitter:alice') => ({ complete: true, account,
+ posts: [{ complete: true, bodyText, attachments: [], context: { kind: 'post', target: null } }] });
+const deferred = () => {
+ let release;
+ const promise = new Promise(resolve => { release = resolve; });
+ return { promise, release };
+};
+const settle = async (turns = 20) => { for (let i = 0; i < turns; i++) await new Promise(resolve => setImmediate(resolve)); };
+const until = async (predicate, message) => {
+ for (let i = 0; i < 400; i++) {
+ if (predicate()) return;
+ await new Promise(resolve => setImmediate(resolve));
+ }
+ assert.fail(message);
+};
+
+for (const browser of ['chrome', 'firefox']) {
+ const { Agent } = await import(`../src/${browser}/src/agent/agent.js`);
+ const { projectTraceEventData } = await import(`../src/${browser}/src/trace/privacy.js`);
+ const { resolveAdapterWorkflowJob } = await import(`../src/${browser}/src/agent/adapters.js`);
+
+ function setup({ request = 'Post Hello on X', contract = rawContract(), authorize = () => true } = {}) {
+ const provider = { model: 'fake-checker', chat: async () => ({ content: '{}' }) };
+ const agent = new Agent({ getActive: () => provider });
+ const tabId = 811;
+ const f = { agent, tabId, provider, contract, authorize, calls: [], probes: [], readings: [], holds: {},
+ pageUrl: 'https://x.com/compose/post' };
+ agent.useSiteAdapters = true;
+ agent._persist = () => {};
+ agent._currentUrl = async () => f.pageUrl;
+ agent.conversations.set(tabId, [{ role: 'system', content: 'system' }, { role: 'user', content: request }]);
+ f.guard = agent._startPlanExecutionGuard(tabId, 'act', { requestKind: 'execute', requiresStateChange: true, requiresSubmission: true });
+ agent._chatWithCostAllowance = async (usedProvider, messages, _options, _cost, meta) => {
+ assert.equal(usedProvider, provider);
+ const call = { name: meta.generationName, startedAt: Date.now(), endedAt: null, done: false };
+ f.calls.push(call);
+ const hold = f.holds[meta.generationName];
+ if (hold) {
+ delete f.holds[meta.generationName];
+ hold.started = true;
+ await hold.promise;
+ }
+ call.endedAt = Date.now();
+ call.done = true;
+ const input = JSON.parse(messages[1].content);
+ if (meta.generationName === 'social_publication_authorization') {
+ call.key = input.key;
+ const authorized = f.authorize(input);
+ return { content: JSON.stringify({ key: input.key, actionId: input.action.id, authorized,
+ reason: authorized ? 'The draft matches the request.' : 'The draft does not match the request.' }) };
+ }
+ return { content: JSON.stringify(f.contract) };
+ };
+ f.detected = { isSubmit: true, publicationControl: true, publicationResourceUrls: [], publicationResourceUrlsComplete: true,
+ frameDocumentIdentity: 'https://x.com/compose/post|fixture-document-1',
+ publicationAccountIdentity: 'twitter:alice', publicationAccountIdentityComplete: true, publicationSnapshot: snapshot() };
+ // The same probe answers the prefetch (its publish-control hint) and the click.
+ agent._detectLikelySubmitAction = async (_tabId, name, args, options = {}) => {
+ f.probes.push({ name, args: structuredClone(args), captionPass: options.captionPass || 'collect' });
+ return structuredClone(f.detected);
+ };
+ agent._readPageTexts = async (_tabId, kind, texts) => { f.readings.push({ kind, texts: [...texts] }); };
+ f.hold = name => { const gate = deferred(); f.holds[name] = gate; return gate; };
+ f.named = name => f.calls.filter(call => call.name === name);
+ f.write = (args = { ref_id: 'ref_7', text: 'Hello', clear: true }) => agent._socialPublicationPrefetch(
+ tabId, 'type_ax', args, { success: true, verified: true }, provider);
+ f.click = () => agent._workflowPreSubmitDispatchBlock(tabId, 'click_ax', { ref_id: 'ref_9' }, f.detected, provider);
+ f.pending = () => agent._socialPublicationContractPrefetches?.get(f.guard) || null;
+ f.applySteering = text => {
+ agent._runningTabs.add(tabId);
+ agent._beginSteeringRun(tabId, () => {}, { detachedRequestId: 'prefetch-steering' });
+ assert.equal(agent.steerMessage(tabId, text, { requestId: 'prefetch-steering', messageId: 'revision' }).accepted, true);
+ assert.equal(agent._applyPendingSteering(tabId, agent.conversations.get(tabId), () => {}), true);
+ agent._finishSteeringRun(tabId);
+ agent._runningTabs.delete(tabId);
+ };
+ return f;
+ }
+
+ test(`${browser}: the contract is compiled after the write, before the click, which then runs no checker`, async () => {
+ const f = setup();
+ const status = await f.write();
+ assert.equal(status, 'authorized');
+ assert.deepEqual(f.calls.map(call => call.name), ['social_publication_contract', 'social_publication_authorization']);
+ // The compiled contract waits beside the run; the run itself is unchanged.
+ const compiled = f.pending().compiled;
+ assert.equal(compiled.contract.status, 'ready');
+ assert.equal(f.guard.socialPublication, null);
+ // The probe asked for the composer's own publish control, beside the
+ // editor that was written, and went straight to the final pass.
+ assert.deepEqual(f.probes[0], { name: 'click_ax', args: { __wbPublicationPrefetch: { editorRef: 'ref_7' } }, captionPass: 'final' });
+ const clickAt = Date.now();
+ assert(f.calls.every(call => call.done && call.endedAt <= clickAt));
+ assert.equal(await f.click(), null);
+ assert.equal(f.calls.length, 2, 'the click compiled nothing and audited nothing');
+ assert.equal(f.guard.socialPublication, compiled, 'the click installed the contract it would have compiled');
+ assert.equal(f.pending(), null);
+ assert.equal(f.guard.socialPublication.dispatch.key, f.named('social_publication_authorization')[0].key);
+ assert.equal(f.guard.socialPublication.prefetchedAudit, undefined, 'a verdict decides one click only');
+ });
+
+ test(`${browser}: the prefetched contract stays out of the run until the click installs it`, async () => {
+ const f = setup();
+ assert.equal(await f.write(), 'authorized');
+ // Ordinary tool calls before the click neither bind the publish workflow
+ // nor see a publication target, exactly as when nothing was prefetched.
+ assert.equal(await f.agent._adoptLiveSocialPublishWorkflow(f.tabId, f.provider), false);
+ assert.equal(f.guard.siteWorkflow, null);
+ assert.equal(f.guard.socialPublication, null);
+ assert.deepEqual(f.agent._missingSocialPublishTargets(f.guard), []);
+ assert.equal(await f.click(), null);
+ assert.equal(f.guard.siteWorkflow.adapterName, 'twitter');
+ assert.equal(f.guard.siteWorkflow.job.id, 'publish-post');
+ assert.equal(f.calls.length, 2);
+ });
+
+ test(`${browser}: the compile starts when a composer write begins, and the draft audit joins it`, async () => {
+ const f = setup();
+ const contract = f.hold('social_publication_contract');
+ // The write is still being prepared: only the contract can start.
+ const early = f.agent._socialPublicationContractPrefetch(f.tabId, 'type_ax', { ref_id: 'ref_7', text: 'Hello' }, f.provider);
+ await until(() => contract.started, 'the compile started before the write finished');
+ assert.equal(f.named('social_publication_authorization').length, 0);
+ assert.equal(f.agent._socialPublicationContractPrefetch(f.tabId, 'type_ax', { ref_id: 'ref_7', text: 'Hello' }, f.provider), null,
+ 'a compile in flight is not started twice');
+ // The write finished: the draft audit waits for the same compile.
+ const afterWrite = f.write();
+ await settle();
+ assert.equal(f.named('social_publication_authorization').length, 0, 'the audit needs the contract first');
+ contract.release();
+ assert.equal(await early, 'contract_ready');
+ assert.equal(await afterWrite, 'authorized');
+ assert.deepEqual(f.calls.map(call => call.name), ['social_publication_contract', 'social_publication_authorization']);
+ assert.equal(await f.click(), null);
+ assert.equal(f.calls.length, 2);
+ });
+
+ test(`${browser}: a click during the background compile waits for it instead of compiling again`, async () => {
+ const f = setup();
+ const contract = f.hold('social_publication_contract');
+ const prefetch = f.write();
+ await until(() => contract.started, 'the background compile started');
+ let clicked = false;
+ const click = f.click().then(result => { clicked = true; return result; });
+ await settle();
+ assert.equal(clicked, false, 'the click waits for the compile in flight');
+ assert.equal(f.guard.socialPublication, null, 'nothing is installed while the background compile runs');
+ contract.release();
+ assert.equal(await click, null);
+ assert.equal(await prefetch, 'authorized');
+ assert.equal(f.named('social_publication_contract').length, 1, 'one compile for prefetch and click');
+ assert.equal(f.named('social_publication_authorization').length, 1, 'the click awaited the prefetched audit');
+ assert.equal(f.guard.socialPublication.dispatch.key, f.named('social_publication_authorization')[0].key);
+ });
+
+ test(`${browser}: a click during the background audit of the same draft awaits it`, async () => {
+ const f = setup();
+ const audit = f.hold('social_publication_authorization');
+ const prefetch = f.write();
+ await until(() => audit.started, 'the background audit started');
+ let clicked = false;
+ const click = f.click().then(result => { clicked = true; return result; });
+ await settle();
+ assert.equal(clicked, false, 'the click waits for the audit in flight');
+ audit.release();
+ assert.equal(await click, null);
+ await prefetch;
+ assert.deepEqual(f.calls.map(call => call.name), ['social_publication_contract', 'social_publication_authorization']);
+ });
+
+ test(`${browser}: a changed draft, account, page or document after the prefetch is audited again at the click`, async () => {
+ for (const change of ['draft', 'account', 'page', 'document']) {
+ // A composed body lets the changed draft pass the click's preflight, so
+ // only the binding key tells it apart from the audited one.
+ const f = setup({ request: 'Tweet about my blog post',
+ contract: rawContract([rawAction('p1', 'twitter', 'about my blog post', 'compose')]) });
+ assert.equal(await f.write(), 'authorized', change);
+ const prefetchedKey = f.named('social_publication_authorization')[0].key;
+ if (change === 'draft') f.detected.publicationSnapshot.posts[0].bodyText = 'A different post about the blog';
+ if (change === 'account') f.detected.publicationSnapshot.account = 'twitter:bob';
+ if (change === 'page') f.pageUrl = 'https://x.com/home';
+ if (change === 'document') f.detected.frameDocumentIdentity = 'https://x.com/compose/post|fixture-document-2';
+ assert.equal(await f.click(), null, change);
+ const audits = f.named('social_publication_authorization');
+ assert.equal(audits.length, 2, `${change}: the click ran its own audit`);
+ assert.notEqual(audits[1].key, prefetchedKey, change);
+ assert.equal(f.guard.socialPublication.dispatch.key, audits[1].key, change);
+ assert.equal(f.named('social_publication_contract').length, 1, change);
+ }
+ });
+
+ test(`${browser}: a denied prefetched verdict blocks exactly like a denial at the click`, async () => {
+ const deny = () => false;
+ const prefetched = setup({ authorize: deny });
+ assert.equal(await prefetched.write(), 'denied');
+ const denied = await prefetched.click();
+ assert.equal(prefetched.named('social_publication_authorization').length, 1, 'the stored verdict decided');
+ const live = setup({ authorize: deny });
+ const expected = await live.click();
+ assert.deepEqual(denied, expected);
+ assert.equal(denied.noDispatch, true);
+ assert.equal(denied.failureRecovery.code, 'publication_audit_denied');
+ assert.equal(denied.failureRecovery.retryMode, 'publication_audit_bypass');
+ for (const key of ['deniedAuditKey', 'deniedAudit', 'dispatch']) {
+ assert.deepEqual(prefetched.guard.socialPublication[key], live.guard.socialPublication[key], key);
+ }
+ // The cached denial holds for another attempt, and the user's Retry
+ // grant applies to it exactly as to a live denial.
+ assert.deepEqual(await prefetched.click(), denied);
+ assert.equal(prefetched.calls.length, 2);
+ assert.equal(prefetched.agent._grantSocialPublicationAuditRetry(prefetched.tabId, denied.failureRecovery), true);
+ assert.equal(await prefetched.click(), null);
+ assert.equal(prefetched.guard.socialPublication.dispatch.auditBypassed, true);
+ assert.equal(prefetched.calls.length, 2);
+ });
+
+ test(`${browser}: steering or Stop during the background audit leaves no usable verdict`, async () => {
+ for (const interruption of ['steering', 'stop']) {
+ const f = setup();
+ const audit = f.hold('social_publication_authorization');
+ const prefetch = f.write();
+ await until(() => audit.started, 'the background audit started');
+ const audited = f.pending().compiled;
+ if (interruption === 'steering') f.applySteering('Actually post Goodbye instead');
+ else f.agent.abort(f.tabId);
+ audit.release();
+ assert.equal(await prefetch, 'changed', interruption);
+ assert.equal(audited.prefetchedAudit, undefined, interruption);
+ if (interruption === 'steering') {
+ assert.equal(f.pending(), null, 'the correction dropped the background contract');
+ assert.equal(f.guard.socialPublication, null);
+ // The corrected task is compiled and audited again at the click.
+ f.contract = rawContract([rawAction('p2', 'twitter', 'Goodbye', 'exact')], 'p2');
+ f.contract.actions[0].posts[0].body.source = ref('Goodbye', 'steering_message0');
+ f.detected.publicationSnapshot.posts[0].bodyText = 'Goodbye';
+ assert.equal(await f.click(), null);
+ assert.equal(f.named('social_publication_contract').length, 2);
+ assert.equal(f.named('social_publication_authorization').length, 2);
+ }
+ }
+ });
+
+ test(`${browser}: steering or Stop during the background compile leaves no contract behind`, async () => {
+ for (const interruption of ['steering', 'stop']) {
+ const f = setup();
+ const contract = f.hold('social_publication_contract');
+ const prefetch = f.write();
+ await until(() => contract.started, 'the background compile started');
+ if (interruption === 'steering') f.applySteering('Actually post Goodbye instead');
+ else f.agent.abort(f.tabId);
+ contract.release();
+ assert.equal(await prefetch, 'skipped', interruption);
+ assert.equal(f.guard.socialPublication, null, `${interruption}: the guard is left as the prefetch found it`);
+ assert.equal(f.pending(), null, interruption);
+ assert.equal(f.named('social_publication_authorization').length, 0, interruption);
+ }
+ });
+
+ test(`${browser}: a failed background compile leaves the click its own two attempts`, async () => {
+ const f = setup();
+ let failures = 2;
+ const chat = f.agent._chatWithCostAllowance;
+ f.agent._chatWithCostAllowance = async (...args) => {
+ if (args[4].generationName === 'social_publication_contract' && failures-- > 0) {
+ f.calls.push({ name: 'social_publication_contract', failed: true });
+ throw new Error('HTTP 503: temporarily unavailable');
+ }
+ return chat(...args);
+ };
+ assert.equal(await f.write(), 'skipped');
+ assert.equal(f.guard.socialPublication, null);
+ assert.equal(f.pending(), null);
+ assert.equal(await f.click(), null);
+ assert.equal(f.named('social_publication_contract').length, 3);
+ });
+
+ test(`${browser}: a contract whose sources changed after the prefetch is compiled again at the click`, async () => {
+ const f = setup();
+ assert.equal(await f.write(), 'authorized');
+ const prefetchedState = f.pending().compiled;
+ // A plain assistant message later in the run is a draft source the click
+ // would have compiled from.
+ f.agent.conversations.get(f.tabId).push({ role: 'assistant', content: 'Draft: Hello' });
+ assert.equal(await f.click(), null);
+ assert.notEqual(f.guard.socialPublication, prefetchedState);
+ assert.equal(f.guard.socialPublication.sources.draft0, 'Draft: Hello');
+ assert.equal(f.named('social_publication_contract').length, 2);
+ assert.equal(f.named('social_publication_authorization').length, 2, 'the new contract is audited again');
+ });
+
+ test(`${browser}: drafts, ordinary pages and failed writes start no prefetch`, async () => {
+ const cases = [
+ ['no submission planned', f => { f.guard.requiresSubmission = false; }],
+ ['not a social composer page', f => { f.pageUrl = 'https://example.com/editor'; }],
+ ['no publish control beside the editor', f => { f.detected = { isSubmit: false, resolvedEditableTarget: true }; }],
+ ['failed write', f => { f.result = { success: false, noDispatch: true }; f.atStart = false; }],
+ ['a submitting field write', f => { f.name = 'set_field'; f.args = { ref_id: 'ref_7', text: 'Hello', submit: true }; }],
+ // The planner bound the run to another job on the site: not a post.
+ ['a run bound to another site job', f => { f.guard.siteWorkflow = { adapterName: 'twitter', job: { id: 'send-message' } }; }],
+ ];
+ for (const [label, prepare] of cases) {
+ const f = setup();
+ f.name = 'type_ax'; f.args = { ref_id: 'ref_7', text: 'Hello' }; f.result = { success: true };
+ prepare(f);
+ // The contract compile can start with the write; the audit needs its result.
+ if (f.atStart !== false) await f.agent._socialPublicationContractPrefetch(f.tabId, f.name, f.args, f.provider);
+ await f.agent._socialPublicationPrefetch(f.tabId, f.name, f.args, f.result, f.provider);
+ assert.equal(f.calls.length, 0, label);
+ assert.equal(f.guard.socialPublication, null, label);
+ assert.equal(f.pending(), null, label);
+ }
+ });
+
+ test(`${browser}: a run bound to the publish job audits the draft against the contract compiled at its start`, async () => {
+ const f = setup();
+ f.guard.siteWorkflow = resolveAdapterWorkflowJob('https://x.com/compose/post', 'publish-post');
+ f.guard.siteWorkflowUrl = 'https://x.com/compose/post';
+ const installed = await f.agent._ensureSocialPublicationContract(f.tabId, f.provider);
+ assert.equal(installed.contract.status, 'ready');
+ assert.equal(await f.write(), 'authorized');
+ assert.equal(f.pending(), null, 'nothing to compile in the background');
+ assert.equal(await f.click(), null);
+ assert.equal(f.guard.socialPublication, installed);
+ assert.deepEqual(f.calls.map(call => call.name), ['social_publication_contract', 'social_publication_authorization']);
+ // A clarification asks for a new contract: it is compiled in the
+ // background from the clarified sources and installed by the next click.
+ f.agent._recordSocialPublicationClarification(f.tabId, f.guard, 'Which account?', 'alice', 'user');
+ assert.equal(installed.needsRecompile, true);
+ f.detected.frameDocumentIdentity = 'https://x.com/compose/post|fixture-document-2';
+ assert.equal(await f.write(), 'authorized');
+ assert.equal(f.guard.socialPublication, installed, 'the run keeps its state until a caller installs the new one');
+ const recompiled = f.pending().compiled;
+ assert.equal(recompiled.sources.clarification_answer0, 'alice');
+ assert.equal(await f.click(), null);
+ assert.equal(f.guard.socialPublication, recompiled);
+ assert.deepEqual(f.calls.map(call => call.name), ['social_publication_contract', 'social_publication_authorization',
+ 'social_publication_contract', 'social_publication_authorization']);
+ });
+
+ test(`${browser}: the prefetch reads the composer's captions the click and recipient guard would read`, async () => {
+ const f = setup();
+ f.detected.publicationPrefetchCaptions = ['Post', 'Close', 'Choose audience'];
+ assert.equal(await f.agent._socialPublicationPrefetch(f.tabId, 'type_text',
+ { selector: '[data-testid="tweetTextarea_0"]', text: 'Hello', clear: true }, { success: true }, f.provider), 'authorized');
+ assert.deepEqual(f.probes[0].args, { __wbPublicationPrefetch: { editorSelector: '[data-testid="tweetTextarea_0"]' } });
+ // x.com/compose/post is a page whose sends the recipient guard checks.
+ assert.deepEqual(f.readings.map(reading => reading.kind).sort(), ['control_effect', 'message_control']);
+ for (const reading of f.readings) assert.deepEqual(reading.texts, ['Post', 'Close', 'Choose audience']);
+ });
+
+ test(`${browser}: an upload into the composer last written audits the draft again`, async () => {
+ const f = setup({ request: 'Tweet about my blog post',
+ contract: rawContract([rawAction('p1', 'twitter', 'about my blog post', 'compose')]) });
+ assert.equal(await f.write(), 'authorized');
+ f.detected.frameDocumentIdentity = 'https://x.com/compose/post|fixture-document-after-upload';
+ assert.equal(await f.agent._socialPublicationPrefetch(f.tabId, 'upload_file', { selector: 'input[type=file]' },
+ { success: true, attachmentState: 'page_consumed' }, f.provider), 'authorized');
+ assert.deepEqual(f.probes.at(-1).args, { __wbPublicationPrefetch: { editorRef: 'ref_7' } });
+ assert.equal(await f.click(), null);
+ assert.equal(f.named('social_publication_authorization').length, 2);
+ });
+
+ test(`${browser}: a consequential click records predispatch_timing with stage durations only`, async () => {
+ const f = setup();
+ const { agent, tabId, provider } = f;
+ assert.equal(await f.write(), 'authorized');
+ agent.currentRunId.set(tabId, 'prefetch-run');
+ agent._skipPermissionGate = true;
+ agent._ensureGateSetting = async () => true;
+ agent._recordProgressObservation = async () => null;
+ agent._autoRecordProgressAction = () => null;
+ agent._progressWarningForAction = () => '';
+ agent._captureFormValidationState = async () => [];
+ agent._waitForFormValidationFailure = async () => null;
+ agent._readCompletionResult = async () => null;
+ agent._messageRecipientGuardBlock = async () => null;
+ agent._captchaMutationPreflight = async () => null;
+ const dispatched = [];
+ agent.executeTool = async (_tabId, name) => { dispatched.push(name); return { success: true, dispatched: true }; };
+ const notes = [];
+ agent._recordPredispatchTiming = (_tabId, step, timing) => notes.push({ step, timing });
+ const messages = [];
+ await agent._executeToolBatch(tabId, [{ id: 'post', function: { name: 'click_ax', arguments: JSON.stringify({ ref_id: 'ref_9' }) } }],
+ messages, () => {}, provider, '', new Set(['click_ax']), 4);
+ assert.deepEqual(dispatched, ['click_ax']);
+ assert.equal(f.calls.length, 2, 'the click reused the prefetched contract and verdict');
+ assert.equal(notes.length, 1);
+ const [{ step, timing }] = notes;
+ assert.equal(step, 4);
+ assert.equal(timing.tool, 'click_ax');
+ assert.equal(timing.outcome, 'dispatched');
+ for (const stage of ['captchaGateMs', 'submitPreflightMs', 'submitProbeMs', 'workflowPreSubmitMs', 'workflowDispatchMs',
+ 'publicationContractMs', 'publicationAuditReusedMs', 'publicationRereadMs', 'recipientGuardMs', 'checkpointMs',
+ 'pipelinePublicationMs', 'attemptCheckpointMs', 'preDispatchMs', 'executeToolMs', 'totalMs']) {
+ assert.equal(typeof timing[stage], 'number', stage);
+ assert(timing[stage] >= 0, stage);
+ }
+ assert.equal(timing.publicationAuditMs, undefined, 'no audit ran at the click');
+ const projected = projectTraceEventData('note', { step, note: 'predispatch_timing',
+ extra: { ...timing, tool: 'click_ax', outcome: 'dispatched', pageUrl: 'https://PRIVATE.invalid', draft: 'PRIVATE' } });
+ assert.equal(projected.note, 'predispatch_timing');
+ assert.equal(projected.extra.tool, 'click_ax');
+ assert.equal(projected.extra.publicationAuditReusedMs, timing.publicationAuditReusedMs);
+ assert.equal(JSON.stringify(projected).includes('PRIVATE'), false);
+ assert(Object.values(projected.extra).every(value => typeof value === 'number' || ['click_ax', 'dispatched'].includes(value)));
+ });
+}
+
+// The prefetch probe, in a real page: it must find the composer's own Post
+// button from the editor just written, and report the same binding snapshot
+// and document as the click on that button will, so their keys match.
+test('the prefetch probe resolves the written composer\'s Post button and binds the same draft as the click', async () => {
+ const { chromium } = await import('playwright');
+ const fs = await import('node:fs');
+ const browser = await chromium.launch({ headless: true });
+ try {
+ const context = await browser.newContext();
+ await context.route('**/*', route => route.fulfill({ status: 200, contentType: 'text/html', body: '<!doctype html><html><body></body></html>' }));
+ const page = await context.newPage();
+ for (const build of ['chrome', 'firefox']) {
+ const { Agent } = await import(`../src/${build}/src/agent/agent.js`);
+ await page.goto('https://x.com/compose/post');
+ await page.setContent(`<nav><a data-testid="AppTabBar_Profile_Link" href="/alice">Profile</a></nav>
+ <main><div id="inline"><div><div contenteditable="true" role="textbox" aria-label="Post text" data-testid="tweetTextarea_0">Inline draft</div></div>
+ <div><button type="button" data-testid="tweetButtonInline">Post</button></div></div></main>
+ <div role="dialog" aria-modal="true" id="dialog"><button type="button" aria-label="Close">x</button>
+ <div><div><div><div contenteditable="true" role="textbox" aria-label="Post text" data-testid="tweetTextarea_0">Hello</div></div></div></div>
+ <nav aria-label="Composer tools"><button type="button" aria-label="Add photos or video">Media</button></nav>
+ <div><button type="button" data-testid="tweetButton">Post</button></div></div>`);
+ await page.addScriptTag({ content: fs.readFileSync(`src/${build}/src/content/accessibility-tree.js`, 'utf8') });
+ const refs = await page.evaluate(() => ({
+ dialogEditor: window.__wb_ax_ref(document.querySelector('#dialog [role="textbox"]')),
+ dialogPost: window.__wb_ax_ref(document.querySelector('#dialog [data-testid="tweetButton"]')),
+ inlineEditor: window.__wb_ax_ref(document.querySelector('#inline [role="textbox"]')),
+ inlinePost: window.__wb_ax_ref(document.querySelector('#inline [data-testid="tweetButtonInline"]')),
+ }));
+ const probe = args => page.evaluate(({ source, args }) => Function('return (' + source + ')')()('click_ax', args),
+ { source: Agent._submitActionProbe.toString(), args: { __wbCaptionPass: 'final', ...args } });
+ for (const [editor, post, body] of [[refs.dialogEditor, refs.dialogPost, 'Hello'], [refs.inlineEditor, refs.inlinePost, 'Inline draft']]) {
+ const prefetch = await probe({ __wbPublicationPrefetch: { editorRef: editor } });
+ const click = await probe({ ref_id: post });
+ assert.equal(prefetch?.isSubmit, true, build);
+ assert.equal(prefetch.publicationControl, true, build);
+ assert.equal(click.publicationControl, true, build);
+ assert.deepEqual(prefetch.publicationSnapshot, click.publicationSnapshot, build);
+ assert.equal(prefetch.frameDocumentIdentity, click.frameDocumentIdentity, build);
+ assert.deepEqual(prefetch.publicationSnapshot.posts.map(post => post.bodyText), [body], build);
+ assert.equal(prefetch.publicationSnapshot.account, 'twitter:alice', build);
+ assert(Array.isArray(prefetch.publicationPrefetchCaptions) && prefetch.publicationPrefetchCaptions.includes('Post'), build);
+ assert.equal(click.publicationPrefetchCaptions, undefined, 'only the prefetch asks for captions');
+ }
+ // The dialog's other controls are among the captions the click reads.
+ const dialogCaptions = (await probe({ __wbPublicationPrefetch: { editorRef: refs.dialogEditor } })).publicationPrefetchCaptions;
+ for (const caption of ['Close', 'Add photos or video']) assert(dialogCaptions.includes(caption), `${build}: ${caption}`);
+ // A selector naming both editors, or two publish controls beside the
+ // editor, resolve to nothing rather than to a guess.
+ assert.equal(await probe({ __wbPublicationPrefetch: { editorSelector: '[data-testid="tweetTextarea_0"]' } }), null, build);
+ await page.evaluate(() => {
+ const second = document.createElement('button');
+ second.type = 'button'; second.dataset.testid = 'tweetButton'; second.textContent = 'Post all';
+ document.querySelector('#dialog').append(second);
+ });
+ assert.equal(await probe({ __wbPublicationPrefetch: { editorRef: refs.dialogEditor } }), null, build);
+ // Only X's own post editor prepares a post: a form's text area or an
+ // unmarked editable beside a Post button does not.
+ for (const field of ['<textarea id="field">Draft</textarea>', '<div id="field" contenteditable="true" role="textbox">Draft</div>']) {
+ await page.setContent(`<nav><a data-testid="AppTabBar_Profile_Link" href="/alice">Profile</a></nav>
+ <form>${field}<button type="button" data-testid="tweetButtonInline">Post</button></form>`);
+ await page.addScriptTag({ content: fs.readFileSync(`src/${build}/src/content/accessibility-tree.js`, 'utf8') });
+ const fieldRef = await page.evaluate(() => window.__wb_ax_ref(document.querySelector('#field')));
+ const postRef = await page.evaluate(() => window.__wb_ax_ref(document.querySelector('button')));
+ assert.equal((await probe({ ref_id: postRef })).publicationControl, true, `${build}: the click itself stays guarded`);
+ assert.equal(await probe({ __wbPublicationPrefetch: { editorRef: fieldRef } }), null, `${build}: ${field}`);
+ }
+ // Bluesky's post editor is its ProseMirror editor.
+ await page.goto('https://bsky.app/');
+ await page.setContent(`<nav><a href="/profile/alice.bsky.social">Profile</a></nav>
+ <div role="dialog" aria-modal="true"><div class="ProseMirror" contenteditable="true" role="textbox"><p>Hello</p></div>
+ <button type="button" data-testid="composerPublishBtn">Post</button></div>`);
+ await page.addScriptTag({ content: fs.readFileSync(`src/${build}/src/content/accessibility-tree.js`, 'utf8') });
+ const bluesky = await page.evaluate(() => ({ editor: window.__wb_ax_ref(document.querySelector('.ProseMirror')),
+ post: window.__wb_ax_ref(document.querySelector('[data-testid="composerPublishBtn"]')) }));
+ const prefetch = await probe({ __wbPublicationPrefetch: { editorRef: bluesky.editor } });
+ const click = await probe({ ref_id: bluesky.post });
+ assert.equal(prefetch?.publicationControl, true, build);
+ assert.deepEqual(prefetch.publicationSnapshot, click.publicationSnapshot, build);
+ assert.equal(prefetch.publicationSnapshot.account, 'bluesky:alice.bsky.social', build);
+ assert.deepEqual(prefetch.publicationSnapshot.posts.map(post => post.bodyText), ['Hello'], build);
+ assert.equal(await probe({ __wbPublicationPrefetch: { editorSelector: '.ProseMirror' } }).then(result => result?.publicationControl), true, build);
+ }
+ } finally {
+ await browser.close();
+ }
+});
+
+test('Chrome and Firefox share the publication prefetch', async () => {
+ const { Agent: Chrome } = await import('../src/chrome/src/agent/agent.js');
+ const { Agent: Firefox } = await import('../src/firefox/src/agent/agent.js');
+ for (const name of ['_socialPublicationPrefetch', '_socialPublicationContractPrefetch', '_socialPublicationPrefetchHint',
+ '_socialPublicationPrefetchAllowed', '_socialPublicationStateCurrent', '_socialPublicationPrefetchRun', '_socialPublicationPrefetchAudit',
+ '_socialPublicationPrefetchReadings', '_socialPublicationPrefetchedVerdict', '_socialPublicationAuditRun',
+ '_socialPublicationCompile', '_ensureSocialPublicationContract', '_predispatchTimer', '_predispatchStage',
+ '_recordPredispatchTiming']) {
+ assert.equal(typeof Chrome.prototype[name], 'function', name);
+ assert.equal(Chrome.prototype[name].toString(), Firefox.prototype[name].toString(), name);
+ }
+});