Skip to content

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

Publication prefetch: trace a background check only to the run that started it

Barack Sokullu8f5848321ca6
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index fdf0210ad..994e01729 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -23807,8 +23807,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return parsePublicationJson(response?.content);
}
- async _socialPublicationCheckerFailure(tabId, phase, attempt, error, validation = false) {
- const runId = this.currentRunId.get(tabId);
+ // `runId` is the run the check belongs to: a background check passes the
+ // run that started it, never the tab's current one.
+ async _socialPublicationCheckerFailure(tabId, phase, attempt, error, validation = false, runId = this.currentRunId.get(tabId)) {
if (!runId) return;
if (!validation) {
await this._tracePlannerAttemptFailure(runId, phase, attempt, error, 'publication_checker_failed');
@@ -23995,16 +23996,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
for (let attempt = 0; attempt < 2; attempt++) {
const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
const options = this._socialPublicationChatOptions(provider, attempt > 0, portable);
- const runId = this.currentRunId.get(tabId);
+ // A background compile is traced to the run that started it.
+ const runId = background ? call.runId : this.currentRunId.get(tabId);
const startedAt = Date.now();
let received = false;
try {
+ // In the background, checked before the request is traced and again
+ // right before inference: the run that started the compile may have
+ // stopped or been replaced meanwhile. Its request is traced to that
+ // run and carries that run's signal and allowance, never a later
+ // run's.
+ if (background && (call.signal.aborted || !call.current() || replaced())) return null;
await this._tracePlannerAttemptRequest(runId, 0, provider, requestMessages,
'social_publication_contract', attempt + 1, this._effectiveRunMode(tabId), options);
- // In the background, checked again right before inference: the run
- // that started the compile may have stopped or been replaced while
- // the request was traced. Its request carries that run's signal and
- // allowance, never a later run's.
if (background && (call.signal.aborted || !call.current() || replaced())) return null;
const response = await this._chatWithCostAllowance(provider, requestMessages,
background ? { ...options, signal: call.signal } : options,
@@ -24035,7 +24039,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (background) return null;
throw error;
}
- await this._socialPublicationCheckerFailure(tabId, 'social_publication_contract', attempt + 1, error, received);
+ await this._socialPublicationCheckerFailure(tabId, 'social_publication_contract', attempt + 1, error, received, runId);
if (this._planExecutionGuards.get(tabId) !== guard || replaced() || this._hasPendingSteering(tabId)) return null;
if (!background) {
state.error = 'Failed while reading the publication request. The checker could not return a complete result; the draft was not published.';
@@ -24417,14 +24421,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
for (let attempt = 0; attempt < 2; attempt++) {
const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
const options = this._socialPublicationChatOptions(provider, attempt > 0, portable, true);
- const runId = this.currentRunId.get(tabId);
+ // A background audit is traced to the run that started it.
+ const runId = background ? background.runId : this.currentRunId.get(tabId);
const startedAt = Date.now();
let received = false;
try {
+ // In the background, checked before the request is traced and again
+ // right before inference (see _socialPublicationCompile).
+ if (background && (background.signal.aborted || !background.current() || !isCurrent())) return { status: 'changed' };
await this._tracePlannerAttemptRequest(runId, 0, provider, requestMessages,
'social_publication_authorization', attempt + 1, this._effectiveRunMode(tabId), options);
- // In the background, checked again right before inference (see
- // _socialPublicationCompile).
if (background && (background.signal.aborted || !background.current() || !isCurrent())) return { status: 'changed' };
const response = await this._chatWithCostAllowance(provider, requestMessages,
background ? { ...options, signal: background.signal } : options,
@@ -24456,7 +24462,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// Cancelled: its run stopped, or an audit of a newer draft replaced it.
if (background?.signal.aborted) return { status: 'changed' };
if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
- await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received);
+ await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received, runId);
if (this._planExecutionGuards.get(tabId) !== guard || !isCurrent()
|| social.needsRecompile || this._hasPendingSteering(tabId)
|| guard.taskKey !== taskKey) {
@@ -24579,7 +24585,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const current = () => this._planExecutionGuards.get(tabId) === guard && guard.taskKey === taskKey
&& !this._checkAbort(tabId) && !this._hasPendingSteering(tabId)
&& this._runAbortSignal(tabId) === runSignal && !runSignal?.aborted;
- const binding = { signal: runSignal, costState: this.currentCostState.get(tabId) || null, current };
+ const binding = { signal: runSignal, costState: this.currentCostState.get(tabId) || null, current, runId };
let status = 'skipped';
try {
const pageUrl = await this._currentUrl(tabId);
@@ -24714,6 +24720,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return {
signal: abort.signal,
costState: binding ? binding.costState : (this.currentCostState.get(tabId) || null),
+ // The run whose trace gets the check's requests, responses and failures.
+ runId: binding ? (binding.runId || null) : (this.currentRunId.get(tabId) || null),
current: binding?.current || (() => this._planExecutionGuards.get(tabId) === guard && !this._checkAbort(tabId)),
};
}
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index fb0acf4be..adb29eb9d 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -20990,8 +20990,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return parsePublicationJson(response?.content);
}
- async _socialPublicationCheckerFailure(tabId, phase, attempt, error, validation = false) {
- const runId = this.currentRunId.get(tabId);
+ // `runId` is the run the check belongs to: a background check passes the
+ // run that started it, never the tab's current one.
+ async _socialPublicationCheckerFailure(tabId, phase, attempt, error, validation = false, runId = this.currentRunId.get(tabId)) {
if (!runId) return;
if (!validation) {
await this._tracePlannerAttemptFailure(runId, phase, attempt, error, 'publication_checker_failed');
@@ -21178,16 +21179,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
for (let attempt = 0; attempt < 2; attempt++) {
const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
const options = this._socialPublicationChatOptions(provider, attempt > 0, portable);
- const runId = this.currentRunId.get(tabId);
+ // A background compile is traced to the run that started it.
+ const runId = background ? call.runId : this.currentRunId.get(tabId);
const startedAt = Date.now();
let received = false;
try {
+ // In the background, checked before the request is traced and again
+ // right before inference: the run that started the compile may have
+ // stopped or been replaced meanwhile. Its request is traced to that
+ // run and carries that run's signal and allowance, never a later
+ // run's.
+ if (background && (call.signal.aborted || !call.current() || replaced())) return null;
await this._tracePlannerAttemptRequest(runId, 0, provider, requestMessages,
'social_publication_contract', attempt + 1, this._effectiveRunMode(tabId), options);
- // In the background, checked again right before inference: the run
- // that started the compile may have stopped or been replaced while
- // the request was traced. Its request carries that run's signal and
- // allowance, never a later run's.
if (background && (call.signal.aborted || !call.current() || replaced())) return null;
const response = await this._chatWithCostAllowance(provider, requestMessages,
background ? { ...options, signal: call.signal } : options,
@@ -21218,7 +21222,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (background) return null;
throw error;
}
- await this._socialPublicationCheckerFailure(tabId, 'social_publication_contract', attempt + 1, error, received);
+ await this._socialPublicationCheckerFailure(tabId, 'social_publication_contract', attempt + 1, error, received, runId);
if (this._planExecutionGuards.get(tabId) !== guard || replaced() || this._hasPendingSteering(tabId)) return null;
if (!background) {
state.error = 'Failed while reading the publication request. The checker could not return a complete result; the draft was not published.';
@@ -21600,14 +21604,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
for (let attempt = 0; attempt < 2; attempt++) {
const requestMessages = attempt ? this._socialPublicationRepairMessages(messages) : messages;
const options = this._socialPublicationChatOptions(provider, attempt > 0, portable, true);
- const runId = this.currentRunId.get(tabId);
+ // A background audit is traced to the run that started it.
+ const runId = background ? background.runId : this.currentRunId.get(tabId);
const startedAt = Date.now();
let received = false;
try {
+ // In the background, checked before the request is traced and again
+ // right before inference (see _socialPublicationCompile).
+ if (background && (background.signal.aborted || !background.current() || !isCurrent())) return { status: 'changed' };
await this._tracePlannerAttemptRequest(runId, 0, provider, requestMessages,
'social_publication_authorization', attempt + 1, this._effectiveRunMode(tabId), options);
- // In the background, checked again right before inference (see
- // _socialPublicationCompile).
if (background && (background.signal.aborted || !background.current() || !isCurrent())) return { status: 'changed' };
const response = await this._chatWithCostAllowance(provider, requestMessages,
background ? { ...options, signal: background.signal } : options,
@@ -21639,7 +21645,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// Cancelled: its run stopped, or an audit of a newer draft replaced it.
if (background?.signal.aborted) return { status: 'changed' };
if (this._isCostAllowanceError(error) || this._checkAbort(tabId)) throw error;
- await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received);
+ await this._socialPublicationCheckerFailure(tabId, 'social_publication_authorization', attempt + 1, error, received, runId);
if (this._planExecutionGuards.get(tabId) !== guard || !isCurrent()
|| social.needsRecompile || this._hasPendingSteering(tabId)
|| guard.taskKey !== taskKey) {
@@ -21762,7 +21768,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const current = () => this._planExecutionGuards.get(tabId) === guard && guard.taskKey === taskKey
&& !this._checkAbort(tabId) && !this._hasPendingSteering(tabId)
&& this._runAbortSignal(tabId) === runSignal && !runSignal?.aborted;
- const binding = { signal: runSignal, costState: this.currentCostState.get(tabId) || null, current };
+ const binding = { signal: runSignal, costState: this.currentCostState.get(tabId) || null, current, runId };
let status = 'skipped';
try {
const pageUrl = await this._currentUrl(tabId);
@@ -21897,6 +21903,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return {
signal: abort.signal,
costState: binding ? binding.costState : (this.currentCostState.get(tabId) || null),
+ // The run whose trace gets the check's requests, responses and failures.
+ runId: binding ? (binding.runId || null) : (this.currentRunId.get(tabId) || null),
current: binding?.current || (() => this._planExecutionGuards.get(tabId) === guard && !this._checkAbort(tabId)),
};
}
diff --git a/test/publication-prefetch.mjs b/test/publication-prefetch.mjs
index 4cc577cb1..4db700ced 100644
--- a/test/publication-prefetch.mjs
+++ b/test/publication-prefetch.mjs
@@ -360,6 +360,63 @@ for (const browser of ['chrome', 'firefox']) {
assert.equal(f.pending()?.compiled?.prefetchedAudit, undefined, 'a cancelled audit stores no verdict');
});
+ test(`${browser}: a background check is traced to the run that started it, and not at all once that run moved on`, async () => {
+ // Bound: a repair attempt is traced to the starting run even when the
+ // tab's run id changed while the first attempt ran.
+ {
+ const f = setup();
+ const { agent, tabId } = f;
+ agent._runAbortStates.set(tabId, { controller: new AbortController(), dispose() {} });
+ agent.currentRunId.set(tabId, 'run-A');
+ const traced = [];
+ agent._tracePlannerAttemptRequest = async (runId, _step, _provider, _messages, phase, attempt) => {
+ traced.push([phase, attempt, runId]);
+ };
+ const chat = agent._chatWithCostAllowance;
+ let invalid = true;
+ agent._chatWithCostAllowance = async (...args) => {
+ if (invalid && args[4].generationName === 'social_publication_contract') {
+ invalid = false;
+ agent.currentRunId.set(tabId, 'run-X');
+ return { content: 'not a contract' };
+ }
+ return chat(...args);
+ };
+ assert.equal(await within(f.write(), 'the bound prefetch ends'), 'authorized');
+ assert.deepEqual(traced, [
+ ['social_publication_contract', 1, 'run-A'],
+ ['social_publication_contract', 2, 'run-A'],
+ ['social_publication_authorization', 1, 'run-A'],
+ ]);
+ }
+ // Moved on: when the next run takes the tab right before a request, the
+ // request is neither traced nor sent.
+ for (const stage of ['social_publication_contract', 'social_publication_authorization']) {
+ const f = setup();
+ const { agent, tabId } = f;
+ agent._runAbortStates.set(tabId, { controller: new AbortController(), dispose() {} });
+ agent.currentRunId.set(tabId, 'run-A');
+ const traced = [];
+ agent._tracePlannerAttemptRequest = async (runId, _step, _provider, _messages, phase) => { traced.push({ runId, phase }); };
+ const chatOptions = agent._socialPublicationChatOptions;
+ let replaced = false;
+ agent._socialPublicationChatOptions = function (...args) {
+ if (!replaced && (args[3] === true) === (stage === 'social_publication_authorization')) {
+ replaced = true;
+ agent._runAbortStates.set(tabId, { controller: new AbortController(), dispose() {} });
+ agent.currentRunId.set(tabId, 'run-B');
+ agent._startPlanExecutionGuard(tabId, 'act', { requestKind: 'execute', requiresStateChange: true, requiresSubmission: true });
+ }
+ return chatOptions.apply(this, args);
+ };
+ await within(f.write(), `${stage}: the stale prefetch ends`);
+ assert.equal(replaced, true, stage);
+ assert.equal(traced.some(entry => entry.runId === 'run-B'), false, `${stage}: nothing was traced to the next run`);
+ assert.equal(traced.some(entry => entry.phase === stage), false, `${stage}: the abandoned request was not traced`);
+ assert.equal(f.named(stage).length, 0, `${stage}: the abandoned request was not sent`);
+ }
+ });
+
test(`${browser}: a newer draft cancels the background audit of the older one, and a click awaiting it audits on its own`, async () => {
const f = setup();
const audit = f.hold('social_publication_authorization');