Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Record the sites a task attempted; replace the per-revision archive
Claude9d0e5d949130
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d67ea762b..24d4c99fd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,7 +7,7 @@ This changelog was generated from the repository Git history and release tags. V## [Unreleased]### Fixed-- Posting on another site after a correction: after a Mastodon post, "ok now on X" no longer leaves WebBrain going back and forth between the two sites, typing the X post again each time. It counted X as the same platform as Mastodon, because it reads account names only on X and Bluesky, and asked to find the Mastodon post on X itself, which can never happen. The site a post was made on now names its platform when no account name was read, so the X post goes ahead once a page has been read since the Mastodon post was sent. Every attempt the task made before the correction counts, not only the latest: an earlier post on the same platform still has to be found first, even when a form on another site came after it, and x.com and twitter.com are one platform.+- Posting on another site after a correction: after a Mastodon post, "ok now on X" no longer leaves WebBrain going back and forth between the two sites, typing the X post again each time. It counted X as the same platform as Mastodon, because it reads account names only on X and Bluesky, and asked to find the Mastodon post on X itself, which can never happen. The site a post was made on now names its platform when no account name was read, and a post on another site goes ahead once a page has been read since the correction, provided the task made no earlier attempt on that site. Each correction now records every site the task had attempted, so this also holds when WebBrain restarted in between. When the task did attempt that site before, or WebBrain has no full record of its earlier attempts, it stops once and says so, instead of searching for a post it cannot find. A search, or a click that sent nothing, is not an attempt, and x.com and twitter.com are one platform.- Chrome: a page's "Leave site?" prompt no longer waits for you when WebBrain navigates away during a task that started on a page the debugger cannot attach to, such as chrome://extensions, or whose debugger connection dropped. WebBrain arms its dialog handling again at the next debugger attach and around each of its own navigations, and answers Leave for its own navigation as it already did on other runs. If you cancel the debugger, prompts stay yours to answer. Firefox offers extensions no way to answer these prompts.- Chat sites such as WhatsApp: a request that points at the open chat instead of naming the person, such as "send some test messages to this one" or "message this user", now sends there instead of asking "which contact, and what text?". The planner never sees the page, and its rules counted only words like "reply here" as the open chat, so it asked the same question every time without reading the page. WebBrain still reads the open chat's header itself and binds every send to that one contact, and stops if no single chat is open. The planner is also told it cannot see the page, no longer reads a note about a screenshot it is never given, and leaves the wording of described messages, such as test messages or a thank-you, to the agent. One request can now cover the open chat and someone named besides it, as in "test messages here, then thank Rustem Bey": the open chat needs no confirmation, the reviewed plan shows the named person, and that person is confirmed once on WebBrain's card before the first send to them. Words that refer back to someone named earlier still mean that person, and on mail sites the people named besides "reply here" are handled as before. On WhatsApp, where a contact is confirmed through its contact drawer, one run still confirms only one chat's contact, so the second chat of such a request needs a request of its own; the agent is now told so, instead of being sent to re-read the page and retry.- A subscription, registration or sign-in wall in front of a news article is now recognized in any language, by a model reading the wall's text once, instead of only when it used English phrases such as "Subscribe to continue reading". A Turkish "Okumaya devam etmek için abone olun" overlay now keeps WebBrain from reading the article hidden behind it, as an English one did, while an offer to subscribe beside text you can still read is not taken for a wall. A wall inside the article is found also after a long preview, and everything after the wall's text stays withheld; the preview before it stays readable, except the short passage just before the wall's text. If a page cannot be read again after its wall was recognized, the read fails rather than return what the wall hides. A wall whose number changes on every read, such as a countdown, stays a wall while its new text is read, and needs no more readings once two of its numbers were read as the wall, while a count that stops being a wall when its number changes reads as what it is once read; a wall whose other text keeps changing makes the read fail rather than leak, and the agent may read the page once more in case the wall went away.diff --git a/docs/architecture.md b/docs/architecture.mdindex 95a5a70fa..92b13ee89 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -98,16 +98,20 @@ record identity is bound by its action and values: an identical resubmission isreported as already made, a corrected in-place save needs the observed earlierresult and authorization under the latest request, and a different target(another form or platform) needs a known earlier outcome. Account names, read-on X and Bluesky only, name a post's platform; a post or form anywhere else,-such as Mastodon, is named by the site it was made on, so "ok now on X" after a-Mastodon post is a post on another platform and is never sent to look for the-Mastodon post on X. Hosts that share their last two labels count as one site,-and x.com and twitter.com as one platform. Every attempt the task dispatched-before the correction counts, not only the latest: an earlier post on the new-post's platform, or an attempt whose site is unknown, holds the new post to that-attempt's own reconciliation, even when a form on another site came after it.-For a post on another platform, a known earlier outcome means a page was read-after the earlier attempt was sent. Whether a correction+on X and Bluesky only, name a post's platform; any other attempt, such as a+Mastodon post or a form, is placed by the site it was made on. Hosts that share+their last two labels count as one site, and x.com and twitter.com as one+platform. A post on another site than the earlier attempt counts as another+platform only when the correction's receipt lists every site the task had+attempted and the post's site is not among them. The agent records the site of+every dispatched attempt that may have changed something (a search, or a send+the browser refused, is not one) for the task, from its first run on; each+correction copies that list into its receipt, and a run rebuilt from the saved+conversation, for example after a restart, reads it there. Without a complete+list, or with an earlier attempt on the post's own site, the post stops once+with a report instead of a search for the other site's post. For a post on+another platform, any page read since the earlier attempt was sent, including+one after the correction, makes its outcome known. Whether a correctionasks to delete the earlier post or record first is a fixed-choice questionthat a model answers from the correction as the user wrote it, in any language(see "Language decisions" below): removing characters, words or hashtags from a@@ -1256,7 +1260,8 @@ A run whose start page the debugger cannot attach to, such aschrome://extensions, or whose debugger connection drops, keeps its wish toanswer dialogs: the next debugger attach arms it, and each navigation andhistory navigation arms it before dispatch and again on the page it reaches.-The person cancelling the debugger ends it for the run.+Stop while a navigation waits for that arming sends nothing. The person+cancelling the debugger ends it for the run.Ask mode and idle tabs do not auto-answer dialogs. Already-observed dialogs areresolved before startup commands. Dialogs opened before debugger attachmentmay need manual dismissal. Startup responds to Stop and times out after fivediff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 5b4e91d9c..135442b03 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -498,6 +498,12 @@ const HUMANIZER_SKILL_SITE_ADAPTERS = new Set(['yandex-mail',]);const SET_CHECKED_VERIFY_DELAY_MS = 80;+// Sites that are one publishing platform under more than one name.+const PUBLICATION_SITE_ALIASES = Object.freeze({+ 'x.com': 'twitter', 'twitter.com': 'twitter', 'bsky.app': 'bluesky',+ 'threads.net': 'threads', 'threads.com': 'threads', 'fb.com': 'facebook', 'facebook.com': 'facebook',+});+const COMPLETION_DOCUMENT_OBSERVATION_TOOLS = new Set(['chat_observe','auto_screenshot',@@ -1059,6 +1065,9 @@ export class Agent extends LoopDetector {this._isPdfTabCache = new Map(); // tabId -> { url, isPdf }this._doneBlockCount = new Map(); // tabId -> consecutive done-blocksthis._completionSubmitStates = new Map(); // tabId -> trusted submit transition metadata+ // tabId -> { key, platforms }: the platforms (sites) of every attempt the+ // current task dispatched, or platforms null when they are not all known.+ this._taskAttemptLedgers = new Map();this._recentSubmitClicks = new Map(); // tabId -> recent submit click timestampsthis._formValidationBlocks = new Map(); // tabId -> validation state that must change before another submitthis._steeringRuns = new Map(); // tabId -> inbox owned by one interactive run@@ -1821,6 +1830,14 @@ export class Agent extends LoopDetector {taskKey: terminalGuard.taskKey,popupLauncherToken: detectedSubmit.popupLauncherToken || '', launcherDispatched: true };}+ // A page read under a correction's guard is a read since the earlier+ // attempt was sent, which was before the correction. It counts whichever+ // page it was and whatever later replaced the live receipt, including in+ // a later run of the same correction.+ if (terminalGuard?.steeringPriorSubmission && COMPLETION_DOCUMENT_OBSERVATION_TOOLS.has(name)+ && next.lastObservation && Number(next.lastObservation.sequence) !== Number(state.lastObservation?.sequence ?? -1)) {+ terminalGuard.steeringPriorReadAfterCorrection = true;+ }let submitState = this._completionSubmitStates.get(tabId);if (submitState?.semanticCommit && Number(next.lastAction?.sequence) > Number(state.lastAction?.sequence || 0)) {submitState = recordSemanticCommitNavigation(submitState, state, next,@@ -1963,7 +1980,16 @@ export class Agent extends LoopDetector {return next;}+ // Every action's result passes here: its submit receipt, if it is one, and+ // what it may have changed, for the task's record of attempted platforms._recordCompletionSubmitAttempt(tabId, detectedSubmit, name, args, beforeUrl, afterUrl, result, beforeDocument = '', afterDocument = '', executionContext = {}) {+ const state = this._recordCompletionSubmitState(tabId, detectedSubmit, name, args, beforeUrl, afterUrl, result,+ beforeDocument, afterDocument, executionContext);+ this._recordTaskAttempt(tabId, name, args, detectedSubmit, result, state);+ return state;+ }++ _recordCompletionSubmitState(tabId, detectedSubmit, name, args, beforeUrl, afterUrl, result, beforeDocument = '', afterDocument = '', executionContext = {}) {// API-backed Chrome Web Store submission is verified by the subsequent// chrome_web_store_status observation. Do not create DOM-form transition// state for a dashboard Chrome intentionally prevents us from inspecting.@@ -2120,12 +2146,6 @@ export class Agent extends LoopDetector {};}this._completionSubmitStates.set(tabId, state);- // Every dispatched attempt of this revision is kept, not only the latest,- // so a correction archives all of them: a post on X followed by a form on- // another site still holds a corrected X post to that X post.- if (state.dispatched && publicationGuard) {- publicationGuard.dispatchedAttempts = [...(publicationGuard.dispatchedAttempts || []), structuredClone(state)].slice(-12);- }if (state.dispatched && publicationGuard?.resourceMutationAuthorization) {const record = state.actionRecord;(publicationGuard.resourceMutationDispatches ||= []).push({ key: publicationGuard.resourceMutationAuthorization.key,@@ -5396,7 +5416,10 @@ export class Agent extends LoopDetector {messages.push({ role: 'user', content: item.text, webbrainSteering: {requestId: run.requestId, messageId: item.id, revision: ++run.revision, parentTaskKey,// A durable attempt receipt is separate from revised completion proof.- ...(priorSubmission ? { priorSubmission: structuredClone(priorSubmission) } : {}),+ // It lists every platform the task attempted up to it, null when not+ // all are known, so a later run, even after a restart, knows them.+ ...(priorSubmission ? { priorSubmission: { ...structuredClone(priorSubmission),+ taskAttemptPlatforms: this._correctionAttemptPlatforms(tabId, messages, priorBinding, priorSubmission) } } : {}),} });onUpdate('steering_applied', item);this._traceSteeringRevision(tabId, 'steering_applied', { revision: run.revision, messageId: item.id });@@ -5481,9 +5504,15 @@ export class Agent extends LoopDetector {// Revised goal counters are separate from the immutable original attempt.const guard = this._startPlanExecutionGuard(tabId, mode, gate, revisionOptions);this._resetCompletionForSteeringRevision(tabId);- guard.steeringPriorSubmission = priorSubmission ? structuredClone(priorSubmission) : null;- guard.steeringMutationReceipts = this._mergeSubmissionReceipts(previous?.steeringMutationReceipts,- [...(previous?.dispatchedAttempts || []), ...(priorSubmission ? [priorSubmission] : [])]);+ guard.steeringPriorSubmission = priorSubmission ? { ...structuredClone(priorSubmission),+ taskAttemptPlatforms: this._correctionAttemptPlatforms(tabId, messages, this._activeTaskBinding(messages), priorSubmission) } : null;+ guard.steeringPriorReadAfterCorrection = false;+ guard.steeringMutationReceipts = structuredClone(previous?.steeringMutationReceipts || []);+ // Action sequences restart with each run, so a receipt is one attempt+ // only within its run.+ if (priorSubmission && !guard.steeringMutationReceipts.some(receipt => receipt.actionSequence === priorSubmission.actionSequence+ && receipt.originatingDocument === priorSubmission.originatingDocument+ && (receipt.runToken || '') === (priorSubmission.runToken || ''))) guard.steeringMutationReceipts.push(structuredClone(priorSubmission));guard.steeringPriorContext = previous?.steeringPriorContext || (priorSubmission ? {siteWorkflow: structuredClone(previous?.siteWorkflow || null),socialPublication: structuredClone(previous?.socialPublication || null),@@ -5602,11 +5631,13 @@ export class Agent extends LoopDetector {// Outcome knowledge that does not depend on the current page: a cached// reconciliation of this exact receipt, a job-verified publication outcome,- // or an explicit page observation after the dispatch.+ // or an explicit page observation after the dispatch, including any page+ // read under the correction, which came after it._steeringPriorEffectKnown(tabId, guard, prior) {const cached = guard?.steeringPriorSubmissionReconciliation;return (cached?.verified === true && cached.actionSequence === prior?.actionSequence)|| this._steeringPriorPublicationOutcome(guard, prior) === 'verified'+ || (!!prior && guard?.steeringPriorSubmission === prior && guard.steeringPriorReadAfterCorrection === true)|| this._steeringPriorEffectObserved(tabId, prior);}@@ -5735,18 +5766,6 @@ export class Agent extends LoopDetector {: !!subject.resourceUrl && entry.resourceUrl === subject.resourceUrl)) || null;}- // Receipts of dispatched attempts in the order they were made, one per- // attempt; a later copy of the same attempt, such as the live receipt with- // its later page reads, replaces the earlier copy in place. Bounded.- _mergeSubmissionReceipts(archived, receipts) {- const merged = new Map();- for (const receipt of [...(Array.isArray(archived) ? archived : []), ...(Array.isArray(receipts) ? receipts : [])]) {- if (!receipt || typeof receipt !== 'object') continue;- merged.set(`${receipt.actionSequence ?? ''}|${receipt.originatingDocument ?? ''}|${receipt.originatingUrl ?? ''}`, structuredClone(receipt));- }- return [...merged.values()].slice(-24);- }-// The platform a post or form belongs to, named by the site it was made on:// X and Bluesky as their account identities name them, any other site by the// last two labels of its host. Hosts that share them count as one site, so@@ -5758,7 +5777,133 @@ export class Agent extends LoopDetector {const labels = host.split('.').filter(Boolean);if (labels.length < 2 || labels.every(label => /^\d+$/.test(label))) return host;const site = labels.slice(-2).join('.');- return site === 'x.com' || site === 'twitter.com' ? 'twitter' : site === 'bsky.app' ? 'bluesky' : site;+ return PUBLICATION_SITE_ALIASES[site] || site;+ }++ // The platforms an attempt may have changed: its account's platform where+ // WebBrain read one (X and Bluesky) and the site of each address it was+ // made from. An attempt with no usable address may have been anywhere, '*'.+ _attemptPlatformKeys(account, urls = []) {+ const keys = new Set();+ const platform = String(account || '').split(':')[0].trim().toLowerCase();+ if (platform) keys.add(platform);+ for (const url of urls) {+ const site = url ? this._publicationSiteOf(url) : '';+ if (site) keys.add(site);+ }+ return keys.size ? [...keys] : ['*'];+ }++ // Where a receipt's attempt was made. The page it went to afterwards, such+ // as the next site of a task, is not where it was made.+ _receiptPlatformKeys(receipt) {+ if (!receipt || typeof receipt !== 'object') return ['*'];+ // A receipt made only of X and Bluesky outcomes, kept when no submission+ // was sent: outcomes that all failed sent nothing anywhere.+ if (receipt.publicationOutcomes && !receipt.originatingUrl && !receipt.currentUrl) {+ return Object.values(receipt.publicationOutcomes).every(outcome => outcome?.status === 'failed') ? [] : ['*'];+ }+ const snapshot = receipt.publicationSnapshot || receipt.workflowBinding?.socialPublication?.snapshot;+ return this._attemptPlatformKeys(snapshot?.account,+ [receipt.originatingUrl || receipt.currentUrl, receipt.iframeSubmission?.currentUrl]);+ }++ // A task is known by its original request, which every correction and+ // clarification of it keeps, in this conversation.+ _taskAttemptLedgerKey(tabId, messages, binding) {+ const root = Number.isInteger(binding?.taskIndex) && binding.taskIndex >= 0 ? messages[binding.taskIndex] : null;+ const text = root ? this._plannerUserAuthoredText(root) : '';+ return text ? `${this.conversationIds?.get(tabId) || ''}|${this._sha256TextSync(text)}` : '';+ }++ // Whether nothing ran after this message: only app state (scratchpad,+ // progress, memory), local status notes, planner questions and the person's+ // own replies follow it. Anything else, including a recovery placeholder or+ // a summary of earlier steps, may stand for an action no longer on record.+ _noTaskRunSince(messages, fromIndex) {+ if (!Number.isInteger(fromIndex) || fromIndex < 0 || fromIndex >= messages.length) return false;+ return messages.slice(fromIndex + 1).every(message => this._isPinnedAgentStateMessage(message)+ || this._isLocalConversationStatusMessage(message)+ || (message?.role === 'assistant' && !!message.webbrainPlannerClarification && !message.tool_calls?.length)+ || (message?.role === 'user' && !message.webbrainAppOwned && !this._isAgentInjectedUserMessage(message)+ && !this._isScheduledResumeTurn(message)));+ }++ // The platforms of every attempt the current task has dispatched, from its+ // first run on, or platforms null when WebBrain cannot know them all. In a+ // session the record follows the task through its runs, corrections and+ // Continue. Without one, for example after WebBrain restarted, only a task+ // that has not run since its request, or since the correction whose receipt+ // lists the platforms before it, is known.+ _taskAttemptLedger(tabId, messages = this.conversations?.get(tabId) || [], binding = this._activeTaskBinding(messages)) {+ const key = this._taskAttemptLedgerKey(tabId, messages, binding);+ const ledgers = this._taskAttemptLedgers ||= new Map();+ const entry = ledgers.get(tabId);+ if (key && entry?.key === key) return entry;+ let platforms = null;+ if (key) {+ const prior = binding.priorSubmission;+ let from = binding.taskIndex;+ if (prior) {+ from = -1;+ for (let index = Math.min(binding.index, messages.length - 1); index > binding.taskIndex; index--) {+ if (messages[index]?.webbrainSteering?.priorSubmission?.dispatched === true) { from = index; break; }+ }+ }+ if (this._noTaskRunSince(messages, from)) {+ if (!prior) platforms = [];+ else if (Array.isArray(prior.taskAttemptPlatforms)) platforms = [...prior.taskAttemptPlatforms];+ }+ }+ const created = { key, platforms };+ ledgers.set(tabId, created);+ return created;+ }++ // Adds what an attempt may have changed to the task's record: a dispatched+ // submission, publication or deletion that commits something, a fetch that+ // sends a mutation, and a store publication. A search commits nothing.+ _recordTaskAttempt(tabId, name, args, detected, result, submitState) {+ try {+ this._addTaskAttempt(tabId, name, args, detected, result, submitState);+ } catch {+ // A record that could not be updated is no longer complete; the action+ // itself is unaffected.+ try { const entry = this._taskAttemptLedgers?.get(tabId); if (entry) entry.platforms = null; } catch {}+ }+ }++ _addTaskAttempt(tabId, name, args, detected, result, submitState) {+ const keys = new Set();+ if (submitState?.dispatched === true && !this._nonCommittingSubmission(detected)) {+ for (const key of this._attemptPlatformKeys(submitState.publicationSnapshot?.account,+ [submitState.originatingUrl, detected?.url, detected?.frameUrl, submitState.iframeSubmission?.currentUrl])) keys.add(key);+ }+ if (isNetworkMutation(name, args) || name === 'chrome_web_store_publish') {+ const notSent = result?.noDispatch === true || result?.denied || result?.skipped || result?.cancelled+ || (result?.dispatched === false && result?.outcomeUnknown !== true && result?.missingToolResponse !== true);+ // A request goes to its own address, not the page's.+ if (!notSent) keys.add(this._publicationSiteOf(name === 'chrome_web_store_publish'+ ? 'https://chromewebstore.google.com/' : args?.url) || '*');+ }+ if (!keys.size) return;+ const ledger = this._taskAttemptLedger(tabId);+ if (!Array.isArray(ledger.platforms)) return;+ const platforms = [...new Set([...ledger.platforms, ...keys])];+ ledger.platforms = platforms.length > 64 ? null : platforms;+ }++ // The platforms of every attempt the task made up to a correction, its+ // receipt included, or null when they are not all known.+ _correctionAttemptPlatforms(tabId, messages, binding, receipt) {+ try {+ const ledger = this._taskAttemptLedger(tabId, messages, binding).platforms;+ if (!Array.isArray(ledger)) return null;+ const platforms = [...new Set([...ledger, ...(receipt ? this._receiptPlatformKeys(receipt) : [])])];+ return platforms.length > 64 ? null : platforms;+ } catch {+ return null;+ }}async _resourceMutationPreSubmitBlock(tabId, name, args, detected, provider = this._activeProvider(tabId)) {@@ -5793,40 +5938,41 @@ export class Agent extends LoopDetector {return blocked('resource_unresolved', 'This Delete or Remove control is not inside one identifiable record (for example a toolbar, bulk, selection or unlinked menu action). Use the Delete or Remove control inside the specific row the user named; toolbar and bulk deletions cannot be bound to a record.');}if (genericCommit && !formCommit && !permalinked) return blocked('resource_unresolved');- const latestPrior = guard.steeringPriorSubmission;+ const prior = guard.steeringPriorSubmission;const task = this._activeTaskBinding(this.conversations.get(tabId) || []);const latest = this._latestAcceptedRequestText(task);+ const priorSnapshot = prior?.publicationSnapshot || prior?.workflowBinding?.socialPublication?.snapshot;// An account belongs to its platform's namespace. A post on another// platform is a separate publication bound to its own contract action,- // not a replacement of the earlier post under a changed account. Account- // names are read on X and Bluesky only; a post or form anywhere else, such- // as Mastodon, is named by the site it was made on. Every attempt the task- // made before the correction counts, not only the latest: an earlier post- // on this platform, or an attempt whose platform is unknown, holds the new- // post to that attempt's own reconciliation, whatever came after it.+ // not a replacement of the earlier post under a changed account.const platformOf = account => String(account || '').split(':')[0];- const snapshotOf = receipt => receipt?.publicationSnapshot || receipt?.workflowBinding?.socialPublication?.snapshot;- const receiptPlatform = receipt => platformOf(snapshotOf(receipt)?.account)- || this._publicationSiteOf(receipt?.originatingUrl || receipt?.currentUrl);- const currentPlatform = platformOf(detected.publicationSnapshot?.account) || this._publicationSiteOf(detected.url);- const earlierAttempts = this._mergeSubmissionReceipts(guard.steeringMutationReceipts, latestPrior ? [latestPrior] : [])- .filter(receipt => receipt.dispatched === true || receipt.outcomeUnknown === true || receipt.nativeInputAcknowledged === true);- const sameNetworkAttempts = publication ? earlierAttempts.filter(receipt => {- const platform = receiptPlatform(receipt);- return !platform || platform === currentPlatform;- }) : [];- const attemptedPlatforms = archive => (archive?.contract?.actions || [])- .filter(action => archive.outcomes?.[action?.id]).map(action => action.platform);- const earlierPostOnPlatform = publication && (- [guard.steeringPriorPublication, guard.steeringPriorContext?.socialPublication]- .some(archive => attemptedPlatforms(archive).includes(currentPlatform))- || (guard.resourceMutationDispatches || []).some(dispatch => dispatch?.mutationKind === 'publish'- && (!dispatch.publicationSnapshot?.account || platformOf(dispatch.publicationSnapshot.account) === currentPlatform)));- const prior = sameNetworkAttempts.at(-1) || latestPrior;- const priorSnapshot = snapshotOf(prior);- const crossPlatform = publication && !!currentPlatform && earlierAttempts.length > 0- && !sameNetworkAttempts.length && !earlierPostOnPlatform;- if (publication && priorSnapshot?.account && detected.publicationSnapshot?.account !== priorSnapshot.account && !crossPlatform) {+ const accountCrossPlatform = publication && !!priorSnapshot?.account && !!detected.publicationSnapshot?.account+ && platformOf(priorSnapshot.account) !== platformOf(detected.publicationSnapshot.account);+ // Account names are read on X and Bluesky only. A post or form anywhere+ // else, such as Mastodon, is placed by the site it was made on. A post on+ // another site than the earlier attempt cannot repeat that attempt, but it+ // may repeat an attempt before it: it counts as another platform only when+ // the correction's receipt lists every platform the task had attempted and+ // this post's platform is not among them.+ const priorPlatforms = prior ? this._receiptPlatformKeys(prior) : ['*'];+ const currentPlatforms = this._attemptPlatformKeys(detected.publicationSnapshot?.account, [detected.url, detected.frameUrl]);+ const otherSite = publication && !priorPlatforms.includes('*') && !currentPlatforms.includes('*')+ && !currentPlatforms.some(platform => priorPlatforms.includes(platform));+ // The receipt's list, checked against the X and Bluesky outcomes and the+ // receipts archived at earlier corrections. A sent-nothing outcome is not+ // an attempt; an attempt of unknown site may have been anywhere.+ const attempted = Array.isArray(prior?.taskAttemptPlatforms) ? prior.taskAttemptPlatforms : null;+ const outcomePlatforms = archive => (archive?.contract?.actions || [])+ .filter(action => action?.id && archive.outcomes?.[action.id] && archive.outcomes[action.id].status !== 'failed')+ .map(action => action.platform || '*');+ const earlierPlatforms = [...(attempted || []),+ ...[guard.steeringPriorPublication, guard.steeringPriorContext?.socialPublication].flatMap(outcomePlatforms),+ ...(guard.steeringMutationReceipts || []).filter(receipt => receipt?.dispatched === true)+ .flatMap(receipt => this._receiptPlatformKeys(receipt))];+ const earlierAttemptHere = earlierPlatforms.some(platform => currentPlatforms.includes(platform));+ const historyKnown = !!attempted && !earlierPlatforms.includes('*');+ const crossPlatform = accountCrossPlatform || (otherSite && historyKnown && !earlierAttemptHere);+ if (publication && priorSnapshot?.account && detected.publicationSnapshot?.account !== priorSnapshot.account && !crossPlatform && !otherSite) {const account = String(detected.publicationSnapshot?.account || '').split(':').slice(1).join(':');if (!account || !latest.toLowerCase().includes(account.toLowerCase())) return blocked('replacement_account_changed');}@@ -5884,6 +6030,17 @@ export class Agent extends LoopDetector {// receipt or adopting a success assertion from a model/tool result.const fresh = await this._detectLikelySubmitAction(tabId, name, args, { mutationReconciliation: true });if (!current() || !fresh || this._resourceMutationActionKey(tabId, name, args, fresh) !== key) return blocked('resource_changed');+ const priorHost = (() => { try { return new URL(prior?.originatingUrl || prior?.currentUrl || '').hostname; } catch { return ''; } })();+ // The latest attempt was on another site, so it can never be found here,+ // and an earlier attempt on this site, or one WebBrain has no record of,+ // may be what this post repeats. Say so once instead of sending the model+ // to look for the other site's post on this page.+ if (prior && publication && otherSite && !crossPlatform) {+ return blocked(earlierAttemptHere ? 'earlier_attempt_on_platform' : 'attempt_history_unknown', earlierAttemptHere+ ? `Nothing was published. Before the latest correction this task already made an attempt on this site, and only the latest attempt${priorHost ? `, on ${priorHost},` : ''} can be checked, so this post could repeat the earlier one or sit beside it. Do not publish it again. Tell the user what was not done and that the earlier attempt on this site may already be there; they can check it and ask again.`+ : `Nothing was published. WebBrain has no full record of what this task attempted before the latest correction (for example after WebBrain restarted), so it cannot rule out an earlier attempt on this site, and the latest attempt${priorHost ? `, on ${priorHost},` : ''} cannot be checked here. Do not publish it again. Tell the user what was not done; they can ask again to post it.`,+ { retryable: false });+ }if (prior) {const matching = this._steeringPriorResourceMatches(tabId, guard, prior, fresh);const reconciled = guard.steeringPriorSubmissionReconciliation;@@ -5914,12 +6071,11 @@ export class Agent extends LoopDetector {&& this._steeringPriorEffectObserved(tabId, prior)) observedKind = 'form_observation';}if (prior.dispatched !== true || (matching.length !== 1 && !deletedVerifiedResource && !observedKind)) {- const priorHost = (() => { try { return new URL(prior.originatingUrl || prior.currentUrl || '').hostname; } catch { return ''; } })();return blocked('prior_effect_unreconciled', publication && !crossPlatform? 'The earlier post from before the latest correction has not been matched yet. Read it on this platform (its permalink or the account\'s recent posts), then retry; do not repeat an uncertain publication.': crossPlatform- // Any page read since it was sent is enough for a post elsewhere,- // so the model is not sent back to the other site.+ // Any page read since the correction is enough for a post on+ // another platform, so the model is not sent back to the other site.? `The earlier submission from before the latest correction${priorHost ? `, on ${priorHost},` : ''} has not been observed yet: no page was read since it was sent. Read the current page, then retry; nothing about it needs to be found here, and do not repeat it.`: 'The earlier submission from before the latest correction has not been observed yet. Read the page that shows its result, then retry; do not repeat an uncertain mutation.');}@@ -31262,6 +31418,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dthis._clarificationAuthorizationGuards.delete(tabId);this._doneBlockCount.delete(tabId);this._completionSubmitStates.delete(tabId);+ this._taskAttemptLedgers.delete(tabId);this._recentSubmitClicks.delete(tabId);this._formValidationBlocks.delete(tabId);// A conversation clear preserves the run guard while the document is@@ -34681,8 +34838,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dsteeringPriorSubmission: carryMatches && carried.steeringPriorSubmission? structuredClone(carried.steeringPriorSubmission): structuredClone(this._activeTaskBinding(this.conversations.get(tabId) || []).priorSubmission || null),+ // A read in an earlier run of the same correction was already a read+ // since its receipt was sent; a receipt read from the conversation needs+ // a read in this run.+ steeringPriorReadAfterCorrection: carryMatches && !!carried.steeringPriorSubmission+ && carried.steeringPriorReadAfterCorrection === true,steeringMutationReceipts: carryMatches ? structuredClone(carried.steeringMutationReceipts || []) : [],- dispatchedAttempts: carryMatches ? structuredClone(carried.dispatchedAttempts || []) : [],steeringPriorContext: carryMatches ? structuredClone(carried.steeringPriorContext || null) : null,steeringPriorSubmissionReconciliation: carryMatches ? structuredClone(carried.steeringPriorSubmissionReconciliation || null) : null,resourceMutationDispatches: carryMatches ? structuredClone(carried.resourceMutationDispatches || []) : [],@@ -34795,6 +34956,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dstaleCancellationRecoveryAttempted: false,};this._planExecutionGuards.set(tabId, state);+ // Start or keep the record of what this task has attempted, before any of+ // this run's actions. Without it the record is incomplete, which only+ // holds posts back.+ try { this._taskAttemptLedger(tabId); } catch {}this._armWorkflowJobRequiredEvidence(tabId, state);const messages = this.conversations.get(tabId);if (messages?.[0]?.role === 'system') {@@ -35210,8 +35375,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dsuccessfulRequiredSchedulingToolCalls: guard.successfulRequiredSchedulingToolCalls,verifiedSubmissionEvidence: guard.verifiedSubmissionEvidence === true,steeringPriorSubmission: guard.steeringPriorSubmission ? structuredClone(guard.steeringPriorSubmission) : null,+ steeringPriorReadAfterCorrection: guard.steeringPriorReadAfterCorrection === true,steeringMutationReceipts: structuredClone(guard.steeringMutationReceipts || []),- dispatchedAttempts: structuredClone(guard.dispatchedAttempts || []),steeringPriorContext: structuredClone(guard.steeringPriorContext || null),steeringPriorSubmissionReconciliation: structuredClone(guard.steeringPriorSubmissionReconciliation || null),resourceMutationDispatches: structuredClone(guard.resourceMutationDispatches || []),@@ -40084,6 +40249,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// connection: arm it again when the run started where none was// possible, such as chrome://extensions, or the connection dropped.await cdpClient.ensureDialogHandling(tabId);+ // Stop while dialog handling was being armed ends the run here,+ // before anything is sent.+ if (this._checkAbort(tabId) || earlyCdpAbortSignal?.aborted) {+ removeNavigationListener();+ return { success: false, dispatched: false, noDispatch: true, cancelled: true,+ error: 'navigate: stopped before the navigation was sent.' };+ }releaseDialogNavigation = cdpClient.authorizeNavigationDialog(tabId, beforeUrl, earlyCdpAbortSignal);await beforePageAgentDispatch(globalThis.chrome, tabId, { kind: 'navigate', url: rawUrl });await chrome.tabs.update(tabId, { url: rawUrl });@@ -40294,6 +40466,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dtry {const delta = direction === 'back' ? -steps : steps;await cdpClient.ensureDialogHandling(tabId);+ // Stop while dialog handling was being armed ends the run here,+ // before anything is sent.+ if (this._checkAbort(tabId) || earlyCdpAbortSignal?.aborted) {+ removeNavigationListeners();+ return { success: false, dispatched: false, noDispatch: true, cancelled: true,+ error: `${name}: stopped before the history navigation was sent.` };+ }await beforePageAgentDispatch(globalThis.chrome, tabId, { kind: 'navigate', history: true });historyDispatchArmed = true;dispatched = true;diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 42e630561..38cdf3fcb 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -337,6 +337,12 @@ function defaultPlanExecutionBlockNudge({const HUMANIZER_SKILL_SITE_ADAPTERS = new Set(['gmail', 'outlook', 'yahoo-mail', 'proton-mail', 'fastmail', 'zoho-mail', 'yandex-mail',]);+// Sites that are one publishing platform under more than one name.+const PUBLICATION_SITE_ALIASES = Object.freeze({+ 'x.com': 'twitter', 'twitter.com': 'twitter', 'bsky.app': 'bluesky',+ 'threads.net': 'threads', 'threads.com': 'threads', 'fb.com': 'facebook', 'facebook.com': 'facebook',+});+const COMPLETION_DOCUMENT_OBSERVATION_TOOLS = new Set(['chat_observe','auto_screenshot',@@ -763,6 +769,9 @@ export class Agent extends LoopDetector {this._isPdfTabCache = new Map();this._doneBlockCount = new Map();this._completionSubmitStates = new Map(); // tabId -> trusted submit transition metadata+ // tabId -> { key, platforms }: the platforms (sites) of every attempt the+ // current task dispatched, or platforms null when they are not all known.+ this._taskAttemptLedgers = new Map();this._recentSubmitClicks = new Map();this._formValidationBlocks = new Map(); // tabId -> validation state that must change before another submitthis._steeringRuns = new Map(); // tabId -> inbox owned by one interactive run@@ -1510,6 +1519,14 @@ export class Agent extends LoopDetector {taskKey: terminalGuard.taskKey,popupLauncherToken: detectedSubmit.popupLauncherToken || '', launcherDispatched: true };}+ // A page read under a correction's guard is a read since the earlier+ // attempt was sent, which was before the correction. It counts whichever+ // page it was and whatever later replaced the live receipt, including in+ // a later run of the same correction.+ if (terminalGuard?.steeringPriorSubmission && COMPLETION_DOCUMENT_OBSERVATION_TOOLS.has(name)+ && next.lastObservation && Number(next.lastObservation.sequence) !== Number(state.lastObservation?.sequence ?? -1)) {+ terminalGuard.steeringPriorReadAfterCorrection = true;+ }let submitState = this._completionSubmitStates.get(tabId);if (submitState?.semanticCommit && Number(next.lastAction?.sequence) > Number(state.lastAction?.sequence || 0)) {submitState = recordSemanticCommitNavigation(submitState, state, next,@@ -1652,7 +1669,16 @@ export class Agent extends LoopDetector {return next;}+ // Every action's result passes here: its submit receipt, if it is one, and+ // what it may have changed, for the task's record of attempted platforms._recordCompletionSubmitAttempt(tabId, detectedSubmit, name, args, beforeUrl, afterUrl, result, beforeDocument = '', afterDocument = '', executionContext = {}) {+ const state = this._recordCompletionSubmitState(tabId, detectedSubmit, name, args, beforeUrl, afterUrl, result,+ beforeDocument, afterDocument, executionContext);+ this._recordTaskAttempt(tabId, name, args, detectedSubmit, result, state);+ return state;+ }++ _recordCompletionSubmitState(tabId, detectedSubmit, name, args, beforeUrl, afterUrl, result, beforeDocument = '', afterDocument = '', executionContext = {}) {// API-backed Chrome Web Store submission is verified by the subsequent// chrome_web_store_status observation. Do not create DOM-form transition// state for a dashboard Chrome intentionally prevents us from inspecting.@@ -1809,12 +1835,6 @@ export class Agent extends LoopDetector {};}this._completionSubmitStates.set(tabId, state);- // Every dispatched attempt of this revision is kept, not only the latest,- // so a correction archives all of them: a post on X followed by a form on- // another site still holds a corrected X post to that X post.- if (state.dispatched && publicationGuard) {- publicationGuard.dispatchedAttempts = [...(publicationGuard.dispatchedAttempts || []), structuredClone(state)].slice(-12);- }if (state.dispatched && publicationGuard?.resourceMutationAuthorization) {const record = state.actionRecord;(publicationGuard.resourceMutationDispatches ||= []).push({ key: publicationGuard.resourceMutationAuthorization.key,@@ -5069,7 +5089,10 @@ export class Agent extends LoopDetector {messages.push({ role: 'user', content: item.text, webbrainSteering: {requestId: run.requestId, messageId: item.id, revision: ++run.revision, parentTaskKey,// A durable attempt receipt is separate from revised completion proof.- ...(priorSubmission ? { priorSubmission: structuredClone(priorSubmission) } : {}),+ // It lists every platform the task attempted up to it, null when not+ // all are known, so a later run, even after a restart, knows them.+ ...(priorSubmission ? { priorSubmission: { ...structuredClone(priorSubmission),+ taskAttemptPlatforms: this._correctionAttemptPlatforms(tabId, messages, priorBinding, priorSubmission) } } : {}),} });onUpdate('steering_applied', item);this._traceSteeringRevision(tabId, 'steering_applied', { revision: run.revision, messageId: item.id });@@ -5154,9 +5177,15 @@ export class Agent extends LoopDetector {// Revised goal counters are separate from the immutable original attempt.const guard = this._startPlanExecutionGuard(tabId, mode, gate, revisionOptions);this._resetCompletionForSteeringRevision(tabId);- guard.steeringPriorSubmission = priorSubmission ? structuredClone(priorSubmission) : null;- guard.steeringMutationReceipts = this._mergeSubmissionReceipts(previous?.steeringMutationReceipts,- [...(previous?.dispatchedAttempts || []), ...(priorSubmission ? [priorSubmission] : [])]);+ guard.steeringPriorSubmission = priorSubmission ? { ...structuredClone(priorSubmission),+ taskAttemptPlatforms: this._correctionAttemptPlatforms(tabId, messages, this._activeTaskBinding(messages), priorSubmission) } : null;+ guard.steeringPriorReadAfterCorrection = false;+ guard.steeringMutationReceipts = structuredClone(previous?.steeringMutationReceipts || []);+ // Action sequences restart with each run, so a receipt is one attempt+ // only within its run.+ if (priorSubmission && !guard.steeringMutationReceipts.some(receipt => receipt.actionSequence === priorSubmission.actionSequence+ && receipt.originatingDocument === priorSubmission.originatingDocument+ && (receipt.runToken || '') === (priorSubmission.runToken || ''))) guard.steeringMutationReceipts.push(structuredClone(priorSubmission));guard.steeringPriorContext = previous?.steeringPriorContext || (priorSubmission ? {siteWorkflow: structuredClone(previous?.siteWorkflow || null),socialPublication: structuredClone(previous?.socialPublication || null),@@ -5275,11 +5304,13 @@ export class Agent extends LoopDetector {// Outcome knowledge that does not depend on the current page: a cached// reconciliation of this exact receipt, a job-verified publication outcome,- // or an explicit page observation after the dispatch.+ // or an explicit page observation after the dispatch, including any page+ // read under the correction, which came after it._steeringPriorEffectKnown(tabId, guard, prior) {const cached = guard?.steeringPriorSubmissionReconciliation;return (cached?.verified === true && cached.actionSequence === prior?.actionSequence)|| this._steeringPriorPublicationOutcome(guard, prior) === 'verified'+ || (!!prior && guard?.steeringPriorSubmission === prior && guard.steeringPriorReadAfterCorrection === true)|| this._steeringPriorEffectObserved(tabId, prior);}@@ -5408,18 +5439,6 @@ export class Agent extends LoopDetector {: !!subject.resourceUrl && entry.resourceUrl === subject.resourceUrl)) || null;}- // Receipts of dispatched attempts in the order they were made, one per- // attempt; a later copy of the same attempt, such as the live receipt with- // its later page reads, replaces the earlier copy in place. Bounded.- _mergeSubmissionReceipts(archived, receipts) {- const merged = new Map();- for (const receipt of [...(Array.isArray(archived) ? archived : []), ...(Array.isArray(receipts) ? receipts : [])]) {- if (!receipt || typeof receipt !== 'object') continue;- merged.set(`${receipt.actionSequence ?? ''}|${receipt.originatingDocument ?? ''}|${receipt.originatingUrl ?? ''}`, structuredClone(receipt));- }- return [...merged.values()].slice(-24);- }-// The platform a post or form belongs to, named by the site it was made on:// X and Bluesky as their account identities name them, any other site by the// last two labels of its host. Hosts that share them count as one site, so@@ -5431,7 +5450,133 @@ export class Agent extends LoopDetector {const labels = host.split('.').filter(Boolean);if (labels.length < 2 || labels.every(label => /^\d+$/.test(label))) return host;const site = labels.slice(-2).join('.');- return site === 'x.com' || site === 'twitter.com' ? 'twitter' : site === 'bsky.app' ? 'bluesky' : site;+ return PUBLICATION_SITE_ALIASES[site] || site;+ }++ // The platforms an attempt may have changed: its account's platform where+ // WebBrain read one (X and Bluesky) and the site of each address it was+ // made from. An attempt with no usable address may have been anywhere, '*'.+ _attemptPlatformKeys(account, urls = []) {+ const keys = new Set();+ const platform = String(account || '').split(':')[0].trim().toLowerCase();+ if (platform) keys.add(platform);+ for (const url of urls) {+ const site = url ? this._publicationSiteOf(url) : '';+ if (site) keys.add(site);+ }+ return keys.size ? [...keys] : ['*'];+ }++ // Where a receipt's attempt was made. The page it went to afterwards, such+ // as the next site of a task, is not where it was made.+ _receiptPlatformKeys(receipt) {+ if (!receipt || typeof receipt !== 'object') return ['*'];+ // A receipt made only of X and Bluesky outcomes, kept when no submission+ // was sent: outcomes that all failed sent nothing anywhere.+ if (receipt.publicationOutcomes && !receipt.originatingUrl && !receipt.currentUrl) {+ return Object.values(receipt.publicationOutcomes).every(outcome => outcome?.status === 'failed') ? [] : ['*'];+ }+ const snapshot = receipt.publicationSnapshot || receipt.workflowBinding?.socialPublication?.snapshot;+ return this._attemptPlatformKeys(snapshot?.account,+ [receipt.originatingUrl || receipt.currentUrl, receipt.iframeSubmission?.currentUrl]);+ }++ // A task is known by its original request, which every correction and+ // clarification of it keeps, in this conversation.+ _taskAttemptLedgerKey(tabId, messages, binding) {+ const root = Number.isInteger(binding?.taskIndex) && binding.taskIndex >= 0 ? messages[binding.taskIndex] : null;+ const text = root ? this._plannerUserAuthoredText(root) : '';+ return text ? `${this.conversationIds?.get(tabId) || ''}|${this._sha256TextSync(text)}` : '';+ }++ // Whether nothing ran after this message: only app state (scratchpad,+ // progress, memory), local status notes, planner questions and the person's+ // own replies follow it. Anything else, including a recovery placeholder or+ // a summary of earlier steps, may stand for an action no longer on record.+ _noTaskRunSince(messages, fromIndex) {+ if (!Number.isInteger(fromIndex) || fromIndex < 0 || fromIndex >= messages.length) return false;+ return messages.slice(fromIndex + 1).every(message => this._isPinnedAgentStateMessage(message)+ || this._isLocalConversationStatusMessage(message)+ || (message?.role === 'assistant' && !!message.webbrainPlannerClarification && !message.tool_calls?.length)+ || (message?.role === 'user' && !message.webbrainAppOwned && !this._isAgentInjectedUserMessage(message)+ && !this._isScheduledResumeTurn(message)));+ }++ // The platforms of every attempt the current task has dispatched, from its+ // first run on, or platforms null when WebBrain cannot know them all. In a+ // session the record follows the task through its runs, corrections and+ // Continue. Without one, for example after WebBrain restarted, only a task+ // that has not run since its request, or since the correction whose receipt+ // lists the platforms before it, is known.+ _taskAttemptLedger(tabId, messages = this.conversations?.get(tabId) || [], binding = this._activeTaskBinding(messages)) {+ const key = this._taskAttemptLedgerKey(tabId, messages, binding);+ const ledgers = this._taskAttemptLedgers ||= new Map();+ const entry = ledgers.get(tabId);+ if (key && entry?.key === key) return entry;+ let platforms = null;+ if (key) {+ const prior = binding.priorSubmission;+ let from = binding.taskIndex;+ if (prior) {+ from = -1;+ for (let index = Math.min(binding.index, messages.length - 1); index > binding.taskIndex; index--) {+ if (messages[index]?.webbrainSteering?.priorSubmission?.dispatched === true) { from = index; break; }+ }+ }+ if (this._noTaskRunSince(messages, from)) {+ if (!prior) platforms = [];+ else if (Array.isArray(prior.taskAttemptPlatforms)) platforms = [...prior.taskAttemptPlatforms];+ }+ }+ const created = { key, platforms };+ ledgers.set(tabId, created);+ return created;+ }++ // Adds what an attempt may have changed to the task's record: a dispatched+ // submission, publication or deletion that commits something, a fetch that+ // sends a mutation, and a store publication. A search commits nothing.+ _recordTaskAttempt(tabId, name, args, detected, result, submitState) {+ try {+ this._addTaskAttempt(tabId, name, args, detected, result, submitState);+ } catch {+ // A record that could not be updated is no longer complete; the action+ // itself is unaffected.+ try { const entry = this._taskAttemptLedgers?.get(tabId); if (entry) entry.platforms = null; } catch {}+ }+ }++ _addTaskAttempt(tabId, name, args, detected, result, submitState) {+ const keys = new Set();+ if (submitState?.dispatched === true && !this._nonCommittingSubmission(detected)) {+ for (const key of this._attemptPlatformKeys(submitState.publicationSnapshot?.account,+ [submitState.originatingUrl, detected?.url, detected?.frameUrl, submitState.iframeSubmission?.currentUrl])) keys.add(key);+ }+ if (isNetworkMutation(name, args) || name === 'chrome_web_store_publish') {+ const notSent = result?.noDispatch === true || result?.denied || result?.skipped || result?.cancelled+ || (result?.dispatched === false && result?.outcomeUnknown !== true && result?.missingToolResponse !== true);+ // A request goes to its own address, not the page's.+ if (!notSent) keys.add(this._publicationSiteOf(name === 'chrome_web_store_publish'+ ? 'https://chromewebstore.google.com/' : args?.url) || '*');+ }+ if (!keys.size) return;+ const ledger = this._taskAttemptLedger(tabId);+ if (!Array.isArray(ledger.platforms)) return;+ const platforms = [...new Set([...ledger.platforms, ...keys])];+ ledger.platforms = platforms.length > 64 ? null : platforms;+ }++ // The platforms of every attempt the task made up to a correction, its+ // receipt included, or null when they are not all known.+ _correctionAttemptPlatforms(tabId, messages, binding, receipt) {+ try {+ const ledger = this._taskAttemptLedger(tabId, messages, binding).platforms;+ if (!Array.isArray(ledger)) return null;+ const platforms = [...new Set([...ledger, ...(receipt ? this._receiptPlatformKeys(receipt) : [])])];+ return platforms.length > 64 ? null : platforms;+ } catch {+ return null;+ }}async _resourceMutationPreSubmitBlock(tabId, name, args, detected, provider = this._activeProvider(tabId)) {@@ -5466,40 +5611,41 @@ export class Agent extends LoopDetector {return blocked('resource_unresolved', 'This Delete or Remove control is not inside one identifiable record (for example a toolbar, bulk, selection or unlinked menu action). Use the Delete or Remove control inside the specific row the user named; toolbar and bulk deletions cannot be bound to a record.');}if (genericCommit && !formCommit && !permalinked) return blocked('resource_unresolved');- const latestPrior = guard.steeringPriorSubmission;+ const prior = guard.steeringPriorSubmission;const task = this._activeTaskBinding(this.conversations.get(tabId) || []);const latest = this._latestAcceptedRequestText(task);+ const priorSnapshot = prior?.publicationSnapshot || prior?.workflowBinding?.socialPublication?.snapshot;// An account belongs to its platform's namespace. A post on another// platform is a separate publication bound to its own contract action,- // not a replacement of the earlier post under a changed account. Account- // names are read on X and Bluesky only; a post or form anywhere else, such- // as Mastodon, is named by the site it was made on. Every attempt the task- // made before the correction counts, not only the latest: an earlier post- // on this platform, or an attempt whose platform is unknown, holds the new- // post to that attempt's own reconciliation, whatever came after it.+ // not a replacement of the earlier post under a changed account.const platformOf = account => String(account || '').split(':')[0];- const snapshotOf = receipt => receipt?.publicationSnapshot || receipt?.workflowBinding?.socialPublication?.snapshot;- const receiptPlatform = receipt => platformOf(snapshotOf(receipt)?.account)- || this._publicationSiteOf(receipt?.originatingUrl || receipt?.currentUrl);- const currentPlatform = platformOf(detected.publicationSnapshot?.account) || this._publicationSiteOf(detected.url);- const earlierAttempts = this._mergeSubmissionReceipts(guard.steeringMutationReceipts, latestPrior ? [latestPrior] : [])- .filter(receipt => receipt.dispatched === true || receipt.outcomeUnknown === true || receipt.nativeInputAcknowledged === true);- const sameNetworkAttempts = publication ? earlierAttempts.filter(receipt => {- const platform = receiptPlatform(receipt);- return !platform || platform === currentPlatform;- }) : [];- const attemptedPlatforms = archive => (archive?.contract?.actions || [])- .filter(action => archive.outcomes?.[action?.id]).map(action => action.platform);- const earlierPostOnPlatform = publication && (- [guard.steeringPriorPublication, guard.steeringPriorContext?.socialPublication]- .some(archive => attemptedPlatforms(archive).includes(currentPlatform))- || (guard.resourceMutationDispatches || []).some(dispatch => dispatch?.mutationKind === 'publish'- && (!dispatch.publicationSnapshot?.account || platformOf(dispatch.publicationSnapshot.account) === currentPlatform)));- const prior = sameNetworkAttempts.at(-1) || latestPrior;- const priorSnapshot = snapshotOf(prior);- const crossPlatform = publication && !!currentPlatform && earlierAttempts.length > 0- && !sameNetworkAttempts.length && !earlierPostOnPlatform;- if (publication && priorSnapshot?.account && detected.publicationSnapshot?.account !== priorSnapshot.account && !crossPlatform) {+ const accountCrossPlatform = publication && !!priorSnapshot?.account && !!detected.publicationSnapshot?.account+ && platformOf(priorSnapshot.account) !== platformOf(detected.publicationSnapshot.account);+ // Account names are read on X and Bluesky only. A post or form anywhere+ // else, such as Mastodon, is placed by the site it was made on. A post on+ // another site than the earlier attempt cannot repeat that attempt, but it+ // may repeat an attempt before it: it counts as another platform only when+ // the correction's receipt lists every platform the task had attempted and+ // this post's platform is not among them.+ const priorPlatforms = prior ? this._receiptPlatformKeys(prior) : ['*'];+ const currentPlatforms = this._attemptPlatformKeys(detected.publicationSnapshot?.account, [detected.url, detected.frameUrl]);+ const otherSite = publication && !priorPlatforms.includes('*') && !currentPlatforms.includes('*')+ && !currentPlatforms.some(platform => priorPlatforms.includes(platform));+ // The receipt's list, checked against the X and Bluesky outcomes and the+ // receipts archived at earlier corrections. A sent-nothing outcome is not+ // an attempt; an attempt of unknown site may have been anywhere.+ const attempted = Array.isArray(prior?.taskAttemptPlatforms) ? prior.taskAttemptPlatforms : null;+ const outcomePlatforms = archive => (archive?.contract?.actions || [])+ .filter(action => action?.id && archive.outcomes?.[action.id] && archive.outcomes[action.id].status !== 'failed')+ .map(action => action.platform || '*');+ const earlierPlatforms = [...(attempted || []),+ ...[guard.steeringPriorPublication, guard.steeringPriorContext?.socialPublication].flatMap(outcomePlatforms),+ ...(guard.steeringMutationReceipts || []).filter(receipt => receipt?.dispatched === true)+ .flatMap(receipt => this._receiptPlatformKeys(receipt))];+ const earlierAttemptHere = earlierPlatforms.some(platform => currentPlatforms.includes(platform));+ const historyKnown = !!attempted && !earlierPlatforms.includes('*');+ const crossPlatform = accountCrossPlatform || (otherSite && historyKnown && !earlierAttemptHere);+ if (publication && priorSnapshot?.account && detected.publicationSnapshot?.account !== priorSnapshot.account && !crossPlatform && !otherSite) {const account = String(detected.publicationSnapshot?.account || '').split(':').slice(1).join(':');if (!account || !latest.toLowerCase().includes(account.toLowerCase())) return blocked('replacement_account_changed');}@@ -5557,6 +5703,17 @@ export class Agent extends LoopDetector {// receipt or adopting a success assertion from a model/tool result.const fresh = await this._detectLikelySubmitAction(tabId, name, args, { mutationReconciliation: true });if (!current() || !fresh || this._resourceMutationActionKey(tabId, name, args, fresh) !== key) return blocked('resource_changed');+ const priorHost = (() => { try { return new URL(prior?.originatingUrl || prior?.currentUrl || '').hostname; } catch { return ''; } })();+ // The latest attempt was on another site, so it can never be found here,+ // and an earlier attempt on this site, or one WebBrain has no record of,+ // may be what this post repeats. Say so once instead of sending the model+ // to look for the other site's post on this page.+ if (prior && publication && otherSite && !crossPlatform) {+ return blocked(earlierAttemptHere ? 'earlier_attempt_on_platform' : 'attempt_history_unknown', earlierAttemptHere+ ? `Nothing was published. Before the latest correction this task already made an attempt on this site, and only the latest attempt${priorHost ? `, on ${priorHost},` : ''} can be checked, so this post could repeat the earlier one or sit beside it. Do not publish it again. Tell the user what was not done and that the earlier attempt on this site may already be there; they can check it and ask again.`+ : `Nothing was published. WebBrain has no full record of what this task attempted before the latest correction (for example after WebBrain restarted), so it cannot rule out an earlier attempt on this site, and the latest attempt${priorHost ? `, on ${priorHost},` : ''} cannot be checked here. Do not publish it again. Tell the user what was not done; they can ask again to post it.`,+ { retryable: false });+ }if (prior) {const matching = this._steeringPriorResourceMatches(tabId, guard, prior, fresh);const reconciled = guard.steeringPriorSubmissionReconciliation;@@ -5587,12 +5744,11 @@ export class Agent extends LoopDetector {&& this._steeringPriorEffectObserved(tabId, prior)) observedKind = 'form_observation';}if (prior.dispatched !== true || (matching.length !== 1 && !deletedVerifiedResource && !observedKind)) {- const priorHost = (() => { try { return new URL(prior.originatingUrl || prior.currentUrl || '').hostname; } catch { return ''; } })();return blocked('prior_effect_unreconciled', publication && !crossPlatform? 'The earlier post from before the latest correction has not been matched yet. Read it on this platform (its permalink or the account\'s recent posts), then retry; do not repeat an uncertain publication.': crossPlatform- // Any page read since it was sent is enough for a post elsewhere,- // so the model is not sent back to the other site.+ // Any page read since the correction is enough for a post on+ // another platform, so the model is not sent back to the other site.? `The earlier submission from before the latest correction${priorHost ? `, on ${priorHost},` : ''} has not been observed yet: no page was read since it was sent. Read the current page, then retry; nothing about it needs to be found here, and do not repeat it.`: 'The earlier submission from before the latest correction has not been observed yet. Read the page that shows its result, then retry; do not repeat an uncertain mutation.');}@@ -29517,6 +29673,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dthis._nytimesPageGateNotified.delete(tabId);this._doneBlockCount.delete(tabId);this._completionSubmitStates.delete(tabId);+ this._taskAttemptLedgers.delete(tabId);this._recentSubmitClicks.delete(tabId);this._formValidationBlocks.delete(tabId);// A conversation clear preserves the run guard while the document is@@ -31960,8 +32117,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dsteeringPriorSubmission: carryMatches && carried.steeringPriorSubmission? structuredClone(carried.steeringPriorSubmission): structuredClone(this._activeTaskBinding(this.conversations.get(tabId) || []).priorSubmission || null),+ // A read in an earlier run of the same correction was already a read+ // since its receipt was sent; a receipt read from the conversation needs+ // a read in this run.+ steeringPriorReadAfterCorrection: carryMatches && !!carried.steeringPriorSubmission+ && carried.steeringPriorReadAfterCorrection === true,steeringMutationReceipts: carryMatches ? structuredClone(carried.steeringMutationReceipts || []) : [],- dispatchedAttempts: carryMatches ? structuredClone(carried.dispatchedAttempts || []) : [],steeringPriorContext: carryMatches ? structuredClone(carried.steeringPriorContext || null) : null,steeringPriorSubmissionReconciliation: carryMatches ? structuredClone(carried.steeringPriorSubmissionReconciliation || null) : null,resourceMutationDispatches: carryMatches ? structuredClone(carried.resourceMutationDispatches || []) : [],@@ -32074,6 +32235,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dstaleCancellationRecoveryAttempted: false,};this._planExecutionGuards.set(tabId, state);+ // Start or keep the record of what this task has attempted, before any of+ // this run's actions. Without it the record is incomplete, which only+ // holds posts back.+ try { this._taskAttemptLedger(tabId); } catch {}this._armWorkflowJobRequiredEvidence(tabId, state);const messages = this.conversations.get(tabId);if (messages?.[0]?.role === 'system') {@@ -32489,8 +32654,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dsuccessfulRequiredSchedulingToolCalls: guard.successfulRequiredSchedulingToolCalls,verifiedSubmissionEvidence: guard.verifiedSubmissionEvidence === true,steeringPriorSubmission: guard.steeringPriorSubmission ? structuredClone(guard.steeringPriorSubmission) : null,+ steeringPriorReadAfterCorrection: guard.steeringPriorReadAfterCorrection === true,steeringMutationReceipts: structuredClone(guard.steeringMutationReceipts || []),- dispatchedAttempts: structuredClone(guard.dispatchedAttempts || []),steeringPriorContext: structuredClone(guard.steeringPriorContext || null),steeringPriorSubmissionReconciliation: structuredClone(guard.steeringPriorSubmissionReconciliation || null),resourceMutationDispatches: structuredClone(guard.resourceMutationDispatches || []),diff --git a/test/browser-dialogs.mjs b/test/browser-dialogs.mjsindex 20e06306a..36e4fa365 100644--- a/test/browser-dialogs.mjs+++ b/test/browser-dialogs.mjs@@ -765,3 +765,53 @@ test('history navigation after a dropped connection answers its own leave promptassert.deepEqual(state.answers(), [{ accept: true }]);});});++// Stop pressed while a navigation waits for dialog handling to be armed, for+// example behind a prompt that already holds the page: nothing is sent.+for (const tool of ['navigate', 'go_back']) {+ test(`Stop while dialog handling is armed for ${tool} sends nothing`, async () => {+ const tabId = tool === 'navigate' ? 4305 : 4306;+ await withNavigationFixture(tabId, 'https://x.com/compose/post', async ({ state, cdpClient, agent }) => {+ const controller = new AbortController();+ await cdpClient.startDialogHandling(tabId, { signal: controller.signal });+ cdpClient._onDebuggerDetach({ tabId }, 'target_closed');+ // Re-arming hangs: Page.enable never answers.+ cdpClient.sendCommand = async (...args) => { state.calls.push(args); return args[1] === 'Page.enable' ? new Promise(() => {}) : {}; };+ let historyRan = false;+ state.chrome.scripting.executeScript = async ({ func }) => {+ if (String(func).includes('history.go')) historyRan = true;+ return [{ result: { attachedFiles: 0, dirtyFields: 0, before: state.url } }];+ };+ const started = Date.now();+ const pending = agent.executeTool(tabId, tool, tool === 'navigate' ? { url: 'https://mastoturk.org/home' } : { force: true });+ setTimeout(() => { agent.abort(tabId); controller.abort(); }, 50);+ const result = await pending;+ assert.equal(result.success, false);+ assert.equal(result.dispatched, false, result.error);+ assert.equal(result.noDispatch, true);+ assert.match(result.error, /stopped before the (?:history )?navigation was sent/);+ assert.equal(state.url, 'https://x.com/compose/post', 'The page was not left');+ assert.equal(historyRan, false, 'history.go never ran');+ assert.ok(Date.now() - started < 1400, 'Stop does not wait for the arming to time out');+ });+ });+}++// The person cancelling the debugger while a run starts stops it, as before.+test('the person cancelling the debugger while a run starts ends its dialog handling as a stop', async () => {+ const { client } = harness();+ let entered;+ const ready = new Promise(resolve => { entered = resolve; });+ client.attach = () => { entered(); return new Promise(() => {}); };+ const startup = client.startDialogHandling(7);+ await ready;+ client._onDebuggerDetach({ tabId: 7 }, 'canceled_by_user');+ await assert.rejects(startup, error => error.name === 'AbortError');+ assert.equal(client.dialogIntents.has(7), false);+ assert.equal(client.dialogRuns.has(7), false);+ client.attach = async id => client.sessions.set(id, { tabId: id });+ assert.equal(await client.ensureDialogHandling(7), false, 'Nothing arms it again');+ await client.attach(7);+ await tick();+ assert.equal(client.dialogRuns.has(7), false);+});diff --git a/test/completion-steering-receipt-authority.mjs b/test/completion-steering-receipt-authority.mjsindex de689d28d..b0cd762e0 100644--- a/test/completion-steering-receipt-authority.mjs+++ b/test/completion-steering-receipt-authority.mjs@@ -113,7 +113,8 @@ for (const build of ['chrome', 'firefox']) {assert.equal(agent._completionDoneBlock(tab, 'done', { outcome: 'success' }), null);const physical = agent._completionSubmitStates.get(tab), guard = agent._planExecutionGuards.get(tab);assert.equal(physical.dispatched, true); assert.equal(physical.taskKey, original.taskKey);- assert.deepEqual(guard.steeringPriorSubmission, original);+ // The correction adds only the list of platforms the task attempted.+ assert.deepEqual(guard.steeringPriorSubmission, { ...original, taskAttemptPlatforms: ['example.test'] });assert.deepEqual(guard.steeringMutationReceipts, [original]);assert.equal(agent._currentCompletionSubmitState(tab), null);const evidence = agent._completionSubmissionEvidence(tab, { relevantFormCount: 1, successMessages: ['Saved successfully.'] }, url);@@ -131,7 +132,7 @@ for (const build of ['chrome', 'firefox']) {assert.equal(fresh.taskKey, guard.taskKey); assert.notEqual(fresh.taskKey, original.taskKey);assert.equal(agent._currentCompletionSubmitState(tab), fresh);assert.equal((await h.done()).done, true); assert.equal(guard.verifiedSubmissionEvidence, true);- assert.equal(h.savedValue, 'Revised'); assert.deepEqual(guard.steeringPriorSubmission, original);+ assert.equal(h.savedValue, 'Revised'); assert.deepEqual(guard.steeringPriorSubmission, { ...original, taskAttemptPlatforms: ['example.test'] });assert.deepEqual(guard.steeringMutationReceipts, [original], 'A new save leaves the prior receipt available for reconciliation.');} finally { h.close(); }});diff --git a/test/guards-steering-receipts.mjs b/test/guards-steering-receipts.mjsindex 3eb821861..f5e8b7d35 100644--- a/test/guards-steering-receipts.mjs+++ b/test/guards-steering-receipts.mjs@@ -17,12 +17,14 @@ for (const build of ['chrome', 'firefox']) {const { resolveAdapterWorkflowJob } = await import(`../src/${build}/src/agent/adapters.js`);const xWorkflow = resolveAdapterWorkflowJob('https://x.com/', 'publish-post');- async function fixture(task, { pageUrl = formUrl, gate: gateOverrides = {} } = {}) {- const tab = nextTab++, options = { detachedRequestId: `guards-${tab}`, askStreamingEnabled: false };+ // A restart is a new Agent given the conversation as it was saved.+ async function fixture(task, { pageUrl = formUrl, gate: gateOverrides = {}, messages = null, tab: fixedTab = null } = {}) {+ const tab = fixedTab ?? nextTab++, options = { detachedRequestId: `guards-${tab}`, askStreamingEnabled: false };const provider = { model: 'owned-offline', supportsVision: false, supportsTools: true, promptTier: 'full',contextWindow: 128000, config: { category: 'local' }, chat() {} };const agent = new Agent({ getActive: () => provider, getProvider: () => provider });- agent.conversations.set(tab, [{ role: 'system', content: 'System' }, { role: 'user', content: task }]);+ agent.conversations.set(tab, messages ? JSON.parse(JSON.stringify(messages))+ : [{ role: 'system', content: 'System' }, { role: 'user', content: task }]);agent.conversationModes.set(tab, 'act');agent._persist = () => {}; agent._persistNow = async () => ({ ok: true });agent._ensureProgressSessionForCurrentTask = async () => {};@@ -53,6 +55,14 @@ for (const build of ['chrome', 'firefox']) {};h.steeringBlock = (detected, name = 'click_ax', args = { ref_id: 'ref_9' }) => agent._steeringPriorSubmissionBlock(tab, name, args, detected);h.close = () => { agent._finishSteeringRun(tab); agent._releaseRunEntry(tab); };+ // WebBrain restarted: only the saved conversation is left.+ h.restart = async (extra = []) => {+ h.close();+ return fixture(task, { pageUrl, gate: gateOverrides, tab, messages: [...agent.conversations.get(tab), ...extra] });+ };+ // A page read, the way the automatic screenshot after an action reads one.+ h.read = (url = pageUrl) => agent._recordCompletionToolResult(tab, 'auto_screenshot', {},+ { success: true, method: 'image_attach', _attachImage: true, pageUrl: url });return h;}@@ -114,7 +124,8 @@ for (const build of ['chrome', 'firefox']) {assert.equal(blocked.actionGuard.reason, variant === 'denied' ? 'latest_request_not_authorized' : 'prior_effect_unreconciled', variant);assert.equal(h.steeringBlock(corrected).noDispatch, true, variant);}- assert.deepEqual(guard.steeringPriorSubmission, archived, 'the original receipt stays immutable');+ // Only the list of platforms the task attempted is added, at the correction.+ assert.deepEqual(guard.steeringPriorSubmission, { ...archived, taskAttemptPlatforms: ['example.test'] }, 'the original receipt stays immutable');} finally { h.close(); }}});@@ -221,89 +232,247 @@ for (const build of ['chrome', 'firefox']) {}});- // An earlier post on the same platform still counts when something on- // another site came after it: "Post 'Hello wrold' on X, then sign me up for- // the newsletter", then "fix the typo in the tweet".- const typoReceipt = () => ({ ...publicationReceipt(), publicationSnapshot: snap('Hello wrold'),- workflowBinding: { preDispatchPublishedResourceIdentities: [], socialPublication: { contractKey: 'contract-1', actionId: 'x-post', snapshot: snap('Hello wrold') } } });- const newsletterReceipt = (urls = true) => ({ dispatched: true, actionSequence: 9, mutationKind: 'submit',- originatingUrl: urls ? 'https://news.example.test/subscribe' : '', currentUrl: urls ? 'https://news.example.test/thanks' : '',- originatingDocument: 'news-doc', submittedActionUrl: urls ? 'https://news.example.test/subscribe/save' : '',- submittedFields: [{ label: 'Email', type: 'email', value: '[email protected]' }], observedAfterSubmit: true });+ // X and Bluesky composers are the publication controls WebBrain reads; a+ // Mastodon composer or a newsletter is an ordinary form.+ const xPublish = (body, account = 'twitter:alice') => ({ ...publish(snap(body, account)), url: 'https://x.com/home', host: 'x.com' });+ const bskyPublish = (body, account = 'bluesky:alice.bsky.social') => ({ ...publish(snap(body, account)), url: 'https://bsky.app/', host: 'bsky.app' });+ const newsletter = () => ({ ...formOn('https://newsletter.test/subscribe'), actionUrl: 'https://newsletter.test/subscribe/save',+ fields: [{ label: 'Email', type: 'email', value: '[email protected]' }] });+ const xSearch = () => ({ ...formOn('https://x.com/explore'), nonCommittingControl: true, method: 'GET',+ fields: [{ label: 'Search', type: 'search', value: 'webbrain' }] });+ // An action's result, recorded the way every action's result is.+ let nextRef = 60;+ const attempt = (h, detected, result = { success: true, dispatched: true }) => h.agent._recordCompletionSubmitAttempt(h.tab,+ detected, 'click_ax', { ref_id: `ref_${nextRef++}` }, detected.url, detected.url, result, `${detected.url}|doc`, `${detected.url}|doc`);+ const refused = { success: false, dispatched: false, noDispatch: true };- test(`${build}: an earlier post on the same platform holds a new one there, whatever came after it`, async () => {+ // "Post 'Hello wrold' on X, then sign me up for the newsletter", then "fix+ // the typo in the tweet": the latest attempt is the newsletter, which can+ // never be found on X, and the X post before it is what a new X post may+ // repeat. WebBrain stops there once, instead of sending the model to look+ // for the newsletter on X until the step limit.+ test(`${build}: after a correction a post is held to an earlier attempt on its own platform, and says so once`, async () => {+ const mastodon = mastodonCompose;const cases = {- // The earlier X post is not on the page: it has to be found first.- 'x-form-x-unread': { earlier: [typoReceipt(), newsletterReceipt()], now: publishOn('https://x.com/home', 'twitter:alice'),- reason: 'prior_effect_unreconciled', guidance: /on this platform/ },- // Found on the page, the X post itself is reconciled, not the form.- 'x-form-x-found': { earlier: [typoReceipt(), newsletterReceipt()], now: { ...publishOn('https://x.com/home', 'twitter:alice'),- reconciliationResources: [record(xPost, 'Hello wrold')] }, allowed: 'resource_identity' },- // Bluesky after an X post and a form elsewhere is another platform.- 'x-form-bluesky': { earlier: [typoReceipt(), newsletterReceipt()], now: publishOn('https://bsky.app/', 'bluesky:alice.bsky.social'),- allowed: 'cross_platform' },- // An attempt whose site is unknown could have been on this platform.- 'unknown-site-x': { earlier: [newsletterReceipt(false)], now: publishOn('https://x.com/home', 'twitter:alice'),- reason: 'prior_effect_unreconciled', guidance: /on this platform/ },+ 'x-form-x': { before: [[xPublish('Hello wrold')], [newsletter()]], correction: 'fix the typo in the tweet',+ now: xPublish('Hello world'), reason: 'earlier_attempt_on_platform' },+ // The same post again is still not repeated.+ 'x-form-same-x': { before: [[xPublish('Hello wrold')], [newsletter()]], correction: 'post it again',+ now: xPublish('Hello wrold'), reason: 'earlier_attempt_on_platform' },+ 'x-form-bluesky': { before: [[xPublish('Hello wrold')], [newsletter()]], correction: 'now post it on Bluesky too',+ now: bskyPublish('Hello world'), allowed: true },+ // A search on X sends nothing and a refused click sent nothing.+ 'x-search-mastodon-x': { before: [[xSearch()], [mastodon, deliveredUnobserved]], correction: 'ok now on X',+ now: xPublish('WebBrain test post', ''), allowed: true },+ 'x-refused-mastodon-x': { before: [[xPublish('WebBrain test post'), refused], [mastodon, deliveredUnobserved]], correction: 'ok now on X',+ now: xPublish('WebBrain test post'), allowed: true },+ // An attempt with no usable address may have been on X.+ 'unknown-mastodon-x': { before: [[{ ...formSubmit('x'), url: '' }], [mastodon, deliveredUnobserved]], correction: 'ok now on X',+ now: xPublish('WebBrain test post', ''), reason: 'attempt_history_unknown' },};for (const [variant, spec] of Object.entries(cases)) {const h = await fixture('Post "Hello wrold" on X, then sign me up for the newsletter.', { pageUrl: spec.now.url });try {- // As the real recorder keeps them: every attempt of the revision, the- // latest also as the live receipt.- h.agent._planExecutionGuards.get(h.tab).dispatchedAttempts = structuredClone(spec.earlier);- h.agent._completionSubmitStates.set(h.tab, structuredClone(spec.earlier.at(-1)));- const guard = await h.steer(variant === 'x-form-bluesky' ? 'now post it on Bluesky too' : 'fix the typo in the tweet');- assert.deepEqual(guard.steeringMutationReceipts.map(receipt => receipt.actionSequence), spec.earlier.map(receipt => receipt.actionSequence), variant);+ for (const [detected, result] of spec.before) attempt(h, detected, result);+ const guard = await h.steer(spec.correction);+ h.read();const blocked = await h.check(spec.now);if (spec.allowed) {assert.equal(blocked, null, `${variant}: ${blocked?.error}`);- assert.equal(guard.steeringPriorSubmissionReconciliation.verificationKind, spec.allowed, variant);- if (spec.allowed === 'resource_identity') {- assert.equal(guard.steeringPriorSubmissionReconciliation.resourceUrl, xPost, variant);- assert.equal(guard.steeringPriorSubmissionReconciliation.actionSequence, 7, `${variant}: the X post, not the form`);- // The deletion question describes the X post, not the form.- assert.deepEqual([h.decisions('deletion_prerequisite').at(-1).page.previous.link,- h.decisions('deletion_prerequisite').at(-1).page.previous.snapshot.account], [xPost, 'twitter:alice'], variant);- }- } else {- assert.equal(blocked?.noDispatch, true, variant);- assert.equal(blocked.actionGuard.reason, spec.reason, `${variant}: ${blocked.error}`);- assert.match(blocked.error, spec.guidance, variant);- assert.equal(h.audits.length, 0, variant);+ assert.equal(guard.steeringPriorSubmissionReconciliation.verificationKind, 'cross_platform', variant);+ continue;}+ assert.equal(blocked?.noDispatch, true, variant);+ assert.equal(blocked.actionGuard.reason, spec.reason, `${variant}: ${blocked.error}`);+ assert.equal(blocked.retryable, false, variant);+ assert.match(blocked.error, /^The consequential action was not dispatched \([a-z_]+\)\. Nothing was published\./, variant);+ assert.match(blocked.error, /Do not publish it again\. Tell the user what was not done/, variant);+ assert.doesNotMatch(blocked.error, /then retry/, `${variant}: no instruction that loops`);+ assert.equal(h.audits.length, 0, variant);+ assert.equal(h.steeringBlock(spec.now)?.noDispatch, true, variant);+ // Reading more pages changes nothing: the same answer, not a new search.+ h.read();+ assert.equal((await h.check(spec.now)).actionGuard.reason, spec.reason, variant);} finally { h.close(); }}});- test(`${build}: every dispatched attempt of a revision is archived at the correction, the live receipt last`, async () => {- const h = await fixture('Post on X, then sign me up for the newsletter.', { pageUrl: 'https://x.com/home' });+ // "Post on X and Bluesky": the X page did not open, so nothing was sent+ // there; the Bluesky post went out; then "now retry the X post".+ test(`${build}: an X attempt that sent nothing does not hold a later X post`, async () => {+ for (const xOutcome of ['failed', 'pending']) {+ for (const account of ['twitter:alice', '']) {+ const variant = `${xOutcome}${account ? '' : '-account-unread'}`;+ const h = await fixture('Post "Hello world" on X and Bluesky.', { pageUrl: 'https://x.com/home' });+ try {+ h.agent._planExecutionGuards.get(h.tab).socialPublication = { key: 'contract-1',+ contract: { actions: [{ id: 'x-post', platform: 'twitter' }, { id: 'bsky-post', platform: 'bluesky' }] },+ outcomes: { 'x-post': xOutcome === 'failed' ? { status: 'failed', cause: 'unavailable' } : { status: 'pending', cause: 'publish_failed' } } };+ attempt(h, bskyPublish('Hello world'));+ await h.steer('now retry the X post');+ h.read();+ const blocked = await h.check(xPublish('Hello world', account));+ // Both accounts read: another platform by account, as before.+ if (xOutcome === 'failed' || account) assert.equal(blocked, null, `${variant}: ${blocked?.error}`);+ else assert.equal(blocked?.actionGuard.reason, 'earlier_attempt_on_platform', variant);+ } finally { h.close(); }+ }+ }+ });++ // "Post on X": the X page did not open and nothing was sent anywhere; then+ // "try again". The receipt is the outcome alone, and it holds nothing.+ test(`${build}: a correction after an attempt that sent nothing lets the post go ahead`, async () => {+ for (const status of ['failed', 'pending']) {+ const h = await fixture('Post "Hello world" on X.', { pageUrl: 'https://x.com/home' });+ try {+ h.agent._planExecutionGuards.get(h.tab).socialPublication = { key: 'contract-1',+ contract: { actions: [{ id: 'x-post', platform: 'twitter' }] },+ outcomes: { 'x-post': status === 'failed' ? { status, cause: 'unavailable' } : { status, cause: 'publish_failed' } } };+ const guard = await h.steer('try again');+ assert.deepEqual(Object.keys(guard.steeringPriorSubmission.publicationOutcomes), ['x-post'], status);+ h.read();+ const blocked = await h.check(xPublish('Hello world'));+ if (status === 'failed') assert.equal(blocked, null, blocked?.error);+ // An outcome that may have been delivered could be on X itself.+ else assert.equal(blocked?.noDispatch, true, status);+ } finally { h.close(); }+ }+ });++ test(`${build}: a correction's receipt lists the platforms the task attempted, so a restart keeps them`, async () => {+ // The trace, with WebBrain restarted after the correction.+ for (const afterCorrection of ['nothing', 'tool steps']) {+ const h = await fixture('go to mastoturk.org and try to post something', { pageUrl: 'https://x.com/home' });+ let r;+ try {+ attempt(h, mastodonCompose, deliveredUnobserved);+ await h.steer('ok now on X');+ r = await h.restart(afterCorrection === 'nothing' ? [] : [+ { role: 'assistant', content: '', tool_calls: [{ id: 'call_nav', type: 'function', function: { name: 'navigate', arguments: '{"url":"https://x.com/home"}' } }] },+ { role: 'tool', tool_call_id: 'call_nav', content: '{"success":true}' }]);+ const guard = r.agent._planExecutionGuards.get(r.tab);+ assert.deepEqual(guard.steeringPriorSubmission?.taskAttemptPlatforms, ['mastoturk.org'], afterCorrection);+ assert.equal(guard.steeringPriorReadAfterCorrection, false, 'a restarted run reads again');+ assert.equal((await r.check(xPublish('WebBrain test post', ''))).actionGuard.reason, 'prior_effect_unreconciled', afterCorrection);+ r.read();+ assert.equal(await r.check(xPublish('WebBrain test post', '')), null, afterCorrection);+ // Only steps after the correction leave the record incomplete, which+ // matters for the next correction.+ attempt(r, xPublish('WebBrain test post'));+ await r.steer('now on Bluesky too');+ r.read();+ const next = await r.check(bskyPublish('WebBrain test post', ''));+ if (afterCorrection === 'nothing') assert.equal(next, null, next?.error);+ else assert.equal(next?.actionGuard.reason, 'attempt_history_unknown', afterCorrection);+ // With both accounts read the platforms differ by account, as before.+ assert.equal(await r.check(bskyPublish('WebBrain test post')), null, afterCorrection);+ } finally { (r || h).close(); }+ }+ // The X post and the newsletter, then "fix the typo in the tweet", then a+ // restart: the X post before the newsletter still holds a new X post.+ {+ const h = await fixture('Post "Hello wrold" on X, then sign me up for the newsletter.', { pageUrl: 'https://x.com/home' });+ let r;+ try {+ attempt(h, xPublish('Hello wrold'));+ attempt(h, newsletter());+ await h.steer('fix the typo in the tweet');+ r = await h.restart();+ r.read();+ assert.equal((await r.check(xPublish('Hello world'))).actionGuard.reason, 'earlier_attempt_on_platform');+ } finally { (r || h).close(); }+ }+ // A conversation saved before the record existed: WebBrain cannot know,+ // so it says so once instead of looking for the Mastodon post on X.+ {+ const h = await fixture('go to mastoturk.org and try to post something', { pageUrl: 'https://x.com/home' });+ let r;+ try {+ attempt(h, mastodonCompose, deliveredUnobserved);+ await h.steer('ok now on X');+ const saved = h.agent.conversations.get(h.tab);+ delete saved.findLast(message => message.webbrainSteering).webbrainSteering.priorSubmission.taskAttemptPlatforms;+ r = await h.restart();+ r.read();+ const blocked = await r.check(xPublish('WebBrain test post', ''));+ assert.equal(blocked.actionGuard.reason, 'attempt_history_unknown', blocked.error);+ assert.equal(blocked.retryable, false);+ } finally { (r || h).close(); }+ }+ });++ // The trace's shape: the Mastodon post, "ok now on X", a page read, a search+ // on X that replaces the live receipt, then the X post.+ test(`${build}: a page read under the correction counts after a later receipt or run replaces the live one`, async () => {+ for (const variant of ['search-after-read', 'no-read', 'continue', 'typed-continue']) {+ const h = await fixture('go to mastoturk.org and try to post something', { pageUrl: 'https://x.com/home' });+ try {+ attempt(h, mastodonCompose, deliveredUnobserved);+ await h.steer('ok now on X');+ if (variant !== 'no-read') h.read();+ if (variant === 'search-after-read') attempt(h, xSearch());+ if (variant === 'continue' || variant === 'typed-continue') {+ h.agent._storeContinuationExecutionEvidence(h.tab);+ h.agent._beginCompletionInvariant(h.tab);+ h.agent._startPlanExecutionGuard(h.tab, 'act', h.gate, variant === 'continue' ? { trustedContinuation: true } : {});+ assert.equal(h.agent._planExecutionGuards.get(h.tab).steeringPriorReadAfterCorrection, variant === 'continue', variant);+ if (variant === 'typed-continue') h.read();+ }+ const blocked = await h.check(xPublish('WebBrain test post'));+ if (variant === 'no-read') {+ assert.equal(blocked.actionGuard.reason, 'prior_effect_unreconciled');+ assert.match(blocked.error, /on mastoturk\.org, has not been observed yet: no page was read since it was sent\. Read the current page, then retry/);+ } else assert.equal(blocked, null, `${variant}: ${blocked?.error}`);+ } finally { h.close(); }+ }+ });++ test(`${build}: the task's record holds what may have changed something, through every correction`, async () => {+ const h = await fixture('Post on X, then sign me up for the newsletter.', { pageUrl: 'https://bsky.app/' });try {- const xNow = body => ({ ...publish(snap(body, 'twitter:alice')), url: 'https://x.com/home', host: 'x.com' });- h.agent._recordCompletionSubmitAttempt(h.tab, xNow('Hello wrold'), 'click_ax', { ref_id: 'ref_1' },- 'https://x.com/home', 'https://x.com/home', { success: true, dispatched: true }, 'x-doc', 'x-doc');- // A preparation the browser refused sends nothing and is not archived.- h.agent._recordCompletionSubmitAttempt(h.tab, formOn('https://news.example.test/subscribe'), 'click_ax', { ref_id: 'ref_2' },- 'https://news.example.test/subscribe', 'https://news.example.test/subscribe', { success: false, dispatched: false, noDispatch: true }, 'news-doc', 'news-doc');- h.agent._recordCompletionSubmitAttempt(h.tab, formOn('https://news.example.test/subscribe'), 'click_ax', { ref_id: 'ref_2' },- 'https://news.example.test/subscribe', 'https://news.example.test/thanks', { success: true, dispatched: true }, 'news-doc', 'news-doc');- assert.deepEqual(h.agent._planExecutionGuards.get(h.tab).dispatchedAttempts.map(receipt => receipt.originatingUrl),- ['https://x.com/home', 'https://news.example.test/subscribe']);- h.agent._completionSubmitStates.set(h.tab, { ...h.agent._completionSubmitStates.get(h.tab), observedAfterSubmit: true });- const guard = await h.steer('fix the typo in the tweet');- assert.deepEqual(guard.steeringMutationReceipts.map(receipt => receipt.originatingUrl),- ['https://x.com/home', 'https://news.example.test/subscribe']);- assert.equal(guard.steeringMutationReceipts.at(-1).observedAfterSubmit, true, 'The live receipt replaces its archived copy');- assert.deepEqual(guard.dispatchedAttempts || [], [], 'The revision starts its own list');- // The corrected post is held to the X post; the same post again is a- // duplicate.- const blocked = await h.check(xNow('Hello world'));- assert.equal(blocked.actionGuard.reason, 'prior_effect_unreconciled', blocked.error);- assert.match(blocked.error, /on this platform/);- assert.equal((await h.check(xNow('Hello wrold'))).actionGuard.reason, 'duplicate_publication');+ attempt(h, xPublish('Hello wrold'));+ attempt(h, newsletter(), refused);+ attempt(h, xSearch());+ attempt(h, newsletter());+ // A request that changes something is recorded at its own address.+ h.agent._recordCompletionSubmitAttempt(h.tab, null, 'fetch_url', { url: 'https://api.example.org/v1/items', method: 'POST' },+ 'https://newsletter.test/thanks', 'https://newsletter.test/thanks', { success: true, status: 201 });+ h.agent._recordCompletionSubmitAttempt(h.tab, null, 'fetch_url', { url: 'https://read.example.net/v1/items' },+ 'https://newsletter.test/thanks', 'https://newsletter.test/thanks', { success: true, status: 200 });+ let guard = await h.steer('fix the typo in the tweet');+ assert.deepEqual(guard.steeringPriorSubmission.taskAttemptPlatforms, ['twitter', 'newsletter.test', 'example.org']);+ assert.deepEqual(h.agent.conversations.get(h.tab).findLast(message => message.webbrainSteering).webbrainSteering+ .priorSubmission.taskAttemptPlatforms, ['twitter', 'newsletter.test', 'example.org'], 'the saved receipt lists them too');+ h.read();+ assert.equal(await h.check(bskyPublish('Hello world')), null);+ attempt(h, bskyPublish('Hello world'));+ guard = await h.steer('and on Mastodon');+ assert.deepEqual(guard.steeringPriorSubmission.taskAttemptPlatforms, ['twitter', 'newsletter.test', 'example.org', 'bluesky']);} finally { h.close(); }});+ test(`${build}: an over-long record counts as unknown, and receipts from two runs stay two`, async () => {+ const h = await fixture('Sign me up on every site.', { pageUrl: 'https://x.com/home' });+ try {+ for (let index = 0; index < 65; index++) attempt(h, formOn(`https://site${index}.test/subscribe`));+ const guard = await h.steer('ok now on X');+ assert.equal(guard.steeringPriorSubmission.taskAttemptPlatforms, null, 'past 64 sites the record is not kept');+ h.read();+ assert.equal((await h.check(xPublish('Hello', ''))).actionGuard.reason, 'attempt_history_unknown');+ } finally { h.close(); }+ // Action sequences restart with each run: the same sequence and page in+ // two runs are two attempts, and both are archived.+ const r = await fixture('Post "Hello world" on X.', { pageUrl: 'https://x.com/home' });+ try {+ r.agent._completionSubmitStates.set(r.tab, { ...publicationReceipt(), runToken: 'completion_run_1' });+ await r.steer('Also send me the link.');+ r.agent._completionSubmitStates.set(r.tab, { ...publicationReceipt(), publicationSnapshot: snap('Hello again'), runToken: 'completion_run_2' });+ const guard = await r.steer('And keep it short.');+ assert.deepEqual(guard.steeringMutationReceipts.map(receipt => receipt.runToken), ['completion_run_1', 'completion_run_2']);+ } finally { r.close(); }+ });+test(`${build}: an unchanged post under a revision reports the earlier publication instead of repeating it`, async () => {for (const variant of ['found-on-page', 'not-found', 'verified-outcome']) {const h = await fixture('Post "Hello world" on X.', { pageUrl: 'https://x.com/home' });