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
Hold a corrected post to every earlier attempt on its platform; review fixes
Claudeeb73abb6a83e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c1027b8e9..d67ea762b 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 the earlier post's result is known. A post on the same site still has to find the earlier one first, 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, 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.- 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 97d231a67..95a5a70fa 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -102,7 +102,12 @@ 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 aMastodon post is a post on another platform and is never sent to look for theMastodon post on X. Hosts that share their last two labels count as one site,-and x.com and twitter.com as one platform. Whether a correction+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 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 adiff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 1a6e9f9a3..5b4e91d9c 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -2120,6 +2120,12 @@ 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,@@ -5476,9 +5482,8 @@ export class Agent extends LoopDetector {const guard = this._startPlanExecutionGuard(tabId, mode, gate, revisionOptions);this._resetCompletionForSteeringRevision(tabId);guard.steeringPriorSubmission = priorSubmission ? structuredClone(priorSubmission) : null;- guard.steeringMutationReceipts = structuredClone(previous?.steeringMutationReceipts || []);- if (priorSubmission && !guard.steeringMutationReceipts.some(receipt => receipt.actionSequence === priorSubmission.actionSequence- && receipt.originatingDocument === priorSubmission.originatingDocument)) guard.steeringMutationReceipts.push(structuredClone(priorSubmission));+ guard.steeringMutationReceipts = this._mergeSubmissionReceipts(previous?.steeringMutationReceipts,+ [...(previous?.dispatchedAttempts || []), ...(priorSubmission ? [priorSubmission] : [])]);guard.steeringPriorContext = previous?.steeringPriorContext || (priorSubmission ? {siteWorkflow: structuredClone(previous?.siteWorkflow || null),socialPublication: structuredClone(previous?.socialPublication || null),@@ -5730,6 +5735,18 @@ 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@@ -5776,20 +5793,39 @@ 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 prior = guard.steeringPriorSubmission;+ const latestPrior = 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; for a post or form anywhere else,- // such as Mastodon, the site it was made on names its platform, so a post- // on another site is never sent to look for the earlier one on its own page.+ // 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.const platformOf = account => String(account || '').split(':')[0];- const priorPlatform = platformOf(priorSnapshot?.account) || this._publicationSiteOf(prior?.originatingUrl || prior?.currentUrl);+ 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 crossPlatform = publication && !!priorPlatform && !!currentPlatform && priorPlatform !== currentPlatform;+ 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 account = String(detected.publicationSnapshot?.account || '').split(':').slice(1).join(':');if (!account || !latest.toLowerCase().includes(account.toLowerCase())) return blocked('replacement_account_changed');@@ -5881,7 +5917,11 @@ export class Agent extends LoopDetector {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.'- : `The earlier submission from before the latest correction${crossPlatform && priorHost ? `, on ${priorHost},` : ''} has not been observed yet. Read the page that shows its result, then retry; do not repeat an uncertain mutation.`);+ : 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.+ ? `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.');}if (matching.length === 1 || deletedVerifiedResource) {guard.steeringPriorSubmissionReconciliation = { verified: true, verificationKind: 'resource_identity', taskKey: guard.taskKey,@@ -34642,6 +34682,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d? structuredClone(carried.steeringPriorSubmission): structuredClone(this._activeTaskBinding(this.conversations.get(tabId) || []).priorSubmission || null),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 || []) : [],@@ -35170,6 +35211,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dverifiedSubmissionEvidence: guard.verifiedSubmissionEvidence === true,steeringPriorSubmission: guard.steeringPriorSubmission ? structuredClone(guard.steeringPriorSubmission) : null,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/src/chrome/src/cdp/cdp-client.js b/src/chrome/src/cdp/cdp-client.jsindex f4e332b0c..b3986abf0 100644--- a/src/chrome/src/cdp/cdp-client.js+++ b/src/chrome/src/cdp/cdp-client.js@@ -528,7 +528,7 @@ export class CDPClient {// the run's: the next attach or navigation arms it again. The person// cancelling the debugger ends it for the run.if (reason === 'canceled_by_user') this.stopDialogHandling(tabId);- else this._disarmDialogHandling(tabId);+ else this._disarmDialogHandling(tabId, 'dropped');this.pendingDialogs.delete(tabId);this._dropWebMCPSession(tabId, `Debugger detached: ${reason || 'unknown reason'}`);this.sessions.delete(tabId);@@ -752,9 +752,11 @@ export class CDPClient {}// Arms the run's dialog handling on the tab's current debugger connection.- // A failure leaves the run's wish in place for a later connection.- _armDialogHandling(tabId, intent) {- this._disarmDialogHandling(tabId);+ // A failure leaves the run's wish in place for a later connection. Arming+ // during a run waits less than a run's start: a setup that hangs behind an+ // open prompt must not hold every navigation for long.+ _armDialogHandling(tabId, intent, timeoutMs = intent.timeoutMs ?? 5000) {+ this._disarmDialogHandling(tabId, 'replaced');if (intent.signal?.aborted || this.dialogIntents.get(tabId) !== intent) return Promise.resolve();const owner = { signal: intent.signal, onHandled: intent.onHandled };owner.onAbort = () => {@@ -762,7 +764,7 @@ export class CDPClient {};this.dialogRuns.set(tabId, owner);owner.signal?.addEventListener('abort', owner.onAbort, { once: true });- owner.arming = this._armDialogOwner(tabId, owner, intent.timeoutMs ?? 5000);+ owner.arming = this._armDialogOwner(tabId, owner, timeoutMs);return owner.arming;}@@ -776,10 +778,14 @@ export class CDPClient {}};const interrupted = new Promise((_, reject) => {- owner.cancelStartup = () => {+ owner.cancelStartup = (cause = 'stopped') => {markPendingAttachCancelled();- const error = new Error('Stopped during browser dialog setup');- error.name = 'AbortError';+ // Only Stop, abort and the run's end stop the run. A connection that+ // dropped, or a newer arming, leaves it running without this setup.+ const error = new Error(cause === 'stopped' ? 'Stopped during browser dialog setup'+ : 'The debugger connection changed during browser dialog setup');+ if (cause === 'stopped') error.name = 'AbortError';+ else error.code = cause === 'dropped' ? 'dialog_connection_dropped' : 'dialog_setup_replaced';reject(error);};timer = setTimeout(() => {@@ -817,13 +823,13 @@ export class CDPClient {* leave prompt. Resolves whether it is armed; never throws, so a page the* debugger cannot reach still navigates.*/- async ensureDialogHandling(tabId) {+ async ensureDialogHandling(tabId, { timeoutMs = 1500 } = {}) {const intent = this.dialogIntents.get(tabId);if (!intent || intent.signal?.aborted) return false;const owner = this.dialogRuns.get(tabId);try {if (owner) await owner.arming;- else await this._armDialogHandling(tabId, intent);+ else await this._armDialogHandling(tabId, intent, timeoutMs);} catch {}return this.dialogIntents.get(tabId) === intent && this.dialogRuns.has(tabId);}@@ -833,7 +839,7 @@ export class CDPClient {_resumeDialogHandling(tabId) {const intent = this.dialogIntents.get(tabId);if (!intent || intent.signal?.aborted || this.dialogRuns.has(tabId)) return;- this._armDialogHandling(tabId, intent).catch(() => {});+ this._armDialogHandling(tabId, intent, 1500).catch(() => {});}stopDialogHandling(tabId) {@@ -843,10 +849,10 @@ export class CDPClient {this._disarmDialogHandling(tabId);}- _disarmDialogHandling(tabId) {+ _disarmDialogHandling(tabId, cause = 'stopped') {const owner = this.dialogRuns.get(tabId);owner?.navigation?.release();- owner?.cancelStartup?.();+ owner?.cancelStartup?.(cause);owner?.signal?.removeEventListener('abort', owner.onAbort);this.dialogRuns.delete(tabId);}diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 9653fe68c..42e630561 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -1809,6 +1809,12 @@ 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,@@ -5149,9 +5155,8 @@ export class Agent extends LoopDetector {const guard = this._startPlanExecutionGuard(tabId, mode, gate, revisionOptions);this._resetCompletionForSteeringRevision(tabId);guard.steeringPriorSubmission = priorSubmission ? structuredClone(priorSubmission) : null;- guard.steeringMutationReceipts = structuredClone(previous?.steeringMutationReceipts || []);- if (priorSubmission && !guard.steeringMutationReceipts.some(receipt => receipt.actionSequence === priorSubmission.actionSequence- && receipt.originatingDocument === priorSubmission.originatingDocument)) guard.steeringMutationReceipts.push(structuredClone(priorSubmission));+ guard.steeringMutationReceipts = this._mergeSubmissionReceipts(previous?.steeringMutationReceipts,+ [...(previous?.dispatchedAttempts || []), ...(priorSubmission ? [priorSubmission] : [])]);guard.steeringPriorContext = previous?.steeringPriorContext || (priorSubmission ? {siteWorkflow: structuredClone(previous?.siteWorkflow || null),socialPublication: structuredClone(previous?.socialPublication || null),@@ -5403,6 +5408,18 @@ 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@@ -5449,20 +5466,39 @@ 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 prior = guard.steeringPriorSubmission;+ const latestPrior = 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; for a post or form anywhere else,- // such as Mastodon, the site it was made on names its platform, so a post- // on another site is never sent to look for the earlier one on its own page.+ // 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.const platformOf = account => String(account || '').split(':')[0];- const priorPlatform = platformOf(priorSnapshot?.account) || this._publicationSiteOf(prior?.originatingUrl || prior?.currentUrl);+ 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 crossPlatform = publication && !!priorPlatform && !!currentPlatform && priorPlatform !== currentPlatform;+ 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 account = String(detected.publicationSnapshot?.account || '').split(':').slice(1).join(':');if (!account || !latest.toLowerCase().includes(account.toLowerCase())) return blocked('replacement_account_changed');@@ -5554,7 +5590,11 @@ export class Agent extends LoopDetector {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.'- : `The earlier submission from before the latest correction${crossPlatform && priorHost ? `, on ${priorHost},` : ''} has not been observed yet. Read the page that shows its result, then retry; do not repeat an uncertain mutation.`);+ : 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.+ ? `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.');}if (matching.length === 1 || deletedVerifiedResource) {guard.steeringPriorSubmissionReconciliation = { verified: true, verificationKind: 'resource_identity', taskKey: guard.taskKey,@@ -31921,6 +31961,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d? structuredClone(carried.steeringPriorSubmission): structuredClone(this._activeTaskBinding(this.conversations.get(tabId) || []).priorSubmission || null),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 || []) : [],@@ -32449,6 +32490,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dverifiedSubmissionEvidence: guard.verifiedSubmissionEvidence === true,steeringPriorSubmission: guard.steeringPriorSubmission ? structuredClone(guard.steeringPriorSubmission) : null,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 ba034de08..20e06306a 100644--- a/test/browser-dialogs.mjs+++ b/test/browser-dialogs.mjs@@ -55,13 +55,17 @@ test('inactive tabs, stop, abort, and cleanup never auto-answer dialogs', asyncassert.ok(calls.every(call => call[1] === 'Page.enable'));});-test('failed startup and already-aborted runs do not retain dialog handling', async () => {+test('failed startup and already-aborted runs arm nothing; a failed start keeps only the run\'s wish until it stops', async () => {const { client } = harness();await client.startDialogHandling(7, { signal: AbortSignal.abort() });assert.equal(client.dialogRuns.size, 0);+ assert.equal(client.dialogIntents.size, 0, 'An aborted run has no wish to answer dialogs');client.sendCommand = async () => { throw new Error('detached'); };await assert.rejects(client.startDialogHandling(7));- assert.equal(client.dialogRuns.size, 0);+ assert.equal(client.dialogRuns.size, 0, 'Nothing answers dialogs after a failed start');+ assert.equal(client.dialogIntents.has(7), true, 'The run may arm on a later connection');+ client.stopDialogHandling(7);+ assert.equal(client.dialogIntents.size, 0);});test('dialog races consume protocol failures without retrying the next dialog', async () => {@@ -493,7 +497,7 @@ function navigationFixture(tabId, startUrl) {emit: (...args) => [...listeners].forEach(fn => fn(...args)) };};const events = { committed: event(), errors: event(), updated: event() };- const state = { url: startUrl, leavePrompt: false, calls: [] };+ const state = { url: startUrl, leavePrompt: false, calls: [], events };state.answers = () => state.calls.filter(call => call[1] === 'Page.handleJavaScriptDialog').map(call => call[2]);state.chrome = {storage: { local: area, session: area }, runtime: { getURL: value => value },@@ -518,7 +522,7 @@ function navigationFixture(tabId, startUrl) {return {};},},- webNavigation: { onCommitted: events.committed, onErrorOccurred: events.errors },+ webNavigation: { onCommitted: events.committed, onErrorOccurred: events.errors, onHistoryStateUpdated: event() },scripting: { executeScript: async () => [{ result: { attachedFiles: 0, dirtyFields: 0 } }] },};return state;@@ -646,3 +650,118 @@ test('any later debugger attach arms the dialog handling its run could not arm aglobalThis.chrome = previousChrome;}});++test('mid-run arming behind a hung Page.enable gives up quickly and keeps the run\'s wish', async () => {+ const { client, calls } = harness();+ client.sendCommand = async () => { throw new Error('Cannot access a chrome:// URL'); };+ await assert.rejects(client.startDialogHandling(7));+ // Page.enable never answers, as behind an already open prompt.+ client.sendCommand = (...args) => { calls.push(args); return args[1] === 'Page.enable' ? new Promise(() => {}) : Promise.resolve({}); };+ const started = Date.now();+ assert.equal(await client.ensureDialogHandling(7, { timeoutMs: 40 }), false);+ assert.ok(Date.now() - started < 1000);+ assert.equal(client.dialogRuns.has(7), false);+ assert.equal(client.dialogIntents.has(7), true);+ client.sendCommand = async (...args) => { calls.push(args); return {}; };+ assert.equal(await client.ensureDialogHandling(7), true, 'A later navigation arms it');+ client.stopDialogHandling(7);+});++test('after re-arming, a leave prompt WebBrain did not ask for still stays, and only its own navigation leaves', async () => {+ const { client, calls } = harness();+ await client.startDialogHandling(7);+ client._onDebuggerDetach({ tabId: 7 }, 'target_closed');+ assert.equal(await client.ensureDialogHandling(7), true);+ const emit = () => {+ client._onDebuggerEvent({ tabId: 7 }, 'Page.javascriptDialogOpening', { type: 'beforeunload', url: 'https://x.com/home' });+ client._onDebuggerEvent({ tabId: 7 }, 'Page.javascriptDialogClosed', {});+ };+ emit();+ const release = client.authorizeNavigationDialog(7, 'https://x.com/home');+ emit();+ release();+ assert.deepEqual(calls.filter(call => call[1] === 'Page.handleJavaScriptDialog').map(call => call[2].accept), [false, true]);+ client.stopDialogHandling(7);+});++test('a connection that drops while a run starts does not stop the run', 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 }, 'target_closed');+ await assert.rejects(startup, error => error.name !== 'AbortError' && error.code === 'dialog_connection_dropped');+ assert.equal(client.dialogIntents.has(7), true);+ client.attach = async id => client.sessions.set(id, { tabId: id });+ assert.equal(await client.ensureDialogHandling(7), true);+ // Stop during a start is still a stop.+ client.attach = () => { entered(); return new Promise(() => {}); };+ const stopped = client.startDialogHandling(8);+ client.stopDialogHandling(8);+ await assert.rejects(stopped, error => error.name === 'AbortError');+ client.stopDialogHandling(7);+});++test('an aborted run is not armed again by a later debugger attach', async () => {+ const previousChrome = globalThis.chrome;+ const sent = [];+ let attaches = 0;+ globalThis.chrome = {+ runtime: { getURL: value => value, lastError: undefined },+ debugger: {+ attach: (_target, _version, callback) => {+ const attempt = ++attaches;+ setTimeout(() => {+ if (attempt === 1) globalThis.chrome.runtime.lastError = { message: 'Cannot access a chrome:// URL' };+ callback();+ globalThis.chrome.runtime.lastError = undefined;+ }, 0);+ },+ detach: (_target, callback) => callback?.(),+ sendCommand: (_target, method, params, callback) => { sent.push(method); callback({}); },+ onEvent: { addListener: () => {}, removeListener: () => {} },+ onDetach: { addListener: () => {}, removeListener: () => {} },+ },+ };+ try {+ const client = new CDPClient(), controller = new AbortController();+ await assert.rejects(client.startDialogHandling(61, { signal: controller.signal }), /chrome:\/\//);+ controller.abort();+ assert.equal(client.dialogIntents.has(61), false);+ await client.attach(61);+ await tick();+ assert.equal(client.dialogRuns.has(61), false);+ assert.deepEqual(sent, []);+ } finally {+ globalThis.chrome = previousChrome;+ }+});++test('history navigation after a dropped connection answers its own leave prompt', async () => {+ const tabId = 4304;+ await withNavigationFixture(tabId, 'https://x.com/compose/post', async ({ state, cdpClient, agent }) => {+ await cdpClient.startDialogHandling(tabId);+ cdpClient._onDebuggerDetach({ tabId }, 'target_closed');+ // history.go runs in the page; the prompt comes from the page being left.+ state.chrome.scripting.executeScript = async ({ func }) => {+ if (String(func).includes('history.go')) {+ const before = state.url;+ const answers = state.answers().length;+ state.client._onDebuggerEvent({ tabId }, 'Page.javascriptDialogOpening', { type: 'beforeunload', url: before });+ await new Promise(resolve => setTimeout(resolve, 0));+ state.client._onDebuggerEvent({ tabId }, 'Page.javascriptDialogClosed', {});+ if (state.answers()[answers]?.accept) {+ state.url = 'https://x.com/home';+ state.events.committed.emit({ tabId, frameId: 0, url: state.url, transitionQualifiers: ['forward_back'] });+ }+ return [{ result: { before } }];+ }+ return [{ result: { attachedFiles: 0, dirtyFields: 0 } }];+ };+ const result = await agent.executeTool(tabId, 'go_back', { force: true });+ assert.equal(result.success, true, result.error);+ assert.deepEqual(state.answers(), [{ accept: true }]);+ });+});diff --git a/test/guards-steering-receipts.mjs b/test/guards-steering-receipts.mjsindex dfb130170..3eb821861 100644--- a/test/guards-steering-receipts.mjs+++ b/test/guards-steering-receipts.mjs@@ -221,6 +221,89 @@ 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 });++ test(`${build}: an earlier post on the same platform holds a new one there, whatever came after it`, async () => {+ 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/ },+ };+ 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);+ 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);+ }+ } 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' });+ 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');+ } finally { h.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' });