Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
bun run install:chrome
CODEXIFY_TEST_BROWSER_COMPANION_CHROME=1 xvfb-run -a bun test ./tests/chrome-surface.test.ts ./tests/capability-discovery.test.ts ./tests/picker-layouts.test.ts ./tests/account-settings.test.ts
CODEXIFY_TEST_BROWSER_COMPANION_CHROME=1 xvfb-run -a bun test ./tests/conversation-approval.test.ts
CODEXIFY_TEST_BROWSER_COMPANION_CHROME=1 xvfb-run -a bun test ./tests/response-delivery-timeout.test.ts
CODEXIFY_TEST_BROWSER_COMPANION_CHROME=1 xvfb-run -a bun test ./tests/conversation-unavailable.test.ts -t 'transcript API 429s'
CODEXIFY_TEST_BROWSER_COMPANION_CHROME=1 xvfb-run -a bun test ./tests/pairing-submit.test.ts -t 'offline Stop resumes the captured|late Stop dialog during message preparation|Japanese lazy picker|conversation length|conversation-length|task continuation|re-pairing sends'
bun run package
Expand Down
51 changes: 47 additions & 4 deletions browser-companion/src/browser-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,53 @@ const subscriptionFailureAlert = (page: Page): Locator => page

// Error semantics and the support destination are stable across UI languages.
// Scope the fallback to alerts/error surfaces so ordinary answer links do not match.
const RESPONSE_ERROR_SELECTOR = '[role="alert"], [data-testid="conversation-turn-error"], [data-testid="conversation-error"], [data-error-code]';
const terminalErrorAlert = (page: Page): Locator => page
.locator('[role="alert"], [data-testid="conversation-turn-error"], [data-testid="conversation-error"], [data-error-code]')
.locator(RESPONSE_ERROR_SELECTOR)
.filter({hasText:uiLabelPattern("terminalFailure",false)})
.filter({hasText:/help\.openai\.com/i}).filter({visible:true}).last();
.filter({hasText:/help\.openai\.com/i}).filter({visible:true});

// These notices are failures of the previous *response*, not permission to
// resubmit its already accepted user message via the UI's Retry button.
// The supervisor owns any later goal continuation and its idempotency key.
export const deliveryTimeoutText = /(?:message delivery (?:has )?timed out|timed out (?:while )?delivering (?:the )?message|\u010das za dostavo sporo\u010dila se je iztekel)/iu;

async function responseDeliveryTimeoutMatches(page:Page,surfaces:Locator):Promise<boolean[]> {
const url = new URL(page.url());
if(url.origin !== "https://chatgpt.com" || !/^\/(?:g\/[^/]+\/)?c\/[^/]+\/?$/.test(url.pathname))return surfaces.evaluateAll(nodes=>nodes.map(()=>false));
return surfaces.evaluateAll((nodes,{patternSource,errorSelector})=>{
const timeoutPattern=new RegExp(patternSource,"iu");
const content='[data-user-message-bubble], [data-conversation-role], [data-message-author-role], article, [data-turn-key], [data-turn-id], pre, code';
const excluded=content+', [hidden], [inert], [aria-hidden="true"]';
const owned=(node:Element)=>{
if(node.closest(excluded))return false;
for(let parent:Element|null=node;parent;parent=parent.parentElement)if(getComputedStyle(parent).opacity==="0")return false;
return true;
};
return nodes.map(surface=>{
if(!owned(surface))return false;
// An alert can exempt only itself and the Retry control inside it.
const alert=surface.closest(errorSelector);
if(alert)return owned(alert)&&!alert.querySelector(errorSelector)&&!alert.querySelector(content)
&&timeoutPattern.test((alert as HTMLElement).innerText??alert.textContent??'');
if(!surface.matches('[data-testid="regenerate-thread-error-button"]'))return false;
// Some cards have no alert semantics. Their short adjacent text belongs
// to this control only if the container has no other errors or retries.
return [surface.parentElement,surface.parentElement?.parentElement].some(node=>{
if(!node || !owned(node) || node.matches('html, body, main, article, form, [data-app-shell-focus-area]'))return false;
if(node.querySelector(content)||node.querySelector(errorSelector)
||node.querySelectorAll('[data-testid="regenerate-thread-error-button"]').length!==1)return false;
const message=((node as HTMLElement).innerText??node.textContent)?.trim();
return Boolean(message && message.length<=350 && timeoutPattern.test(message));
});
});
},{patternSource:deliveryTimeoutText.source,errorSelector:RESPONSE_ERROR_SELECTOR});
}

export async function previousResponseDeliveryTimedOut(page:Page):Promise<boolean> {
const surfaces=page.locator(RESPONSE_ERROR_SELECTOR).or(page.getByTestId("regenerate-thread-error-button")).filter({visible:true});
return (await responseDeliveryTimeoutMatches(page,surfaces)).some(Boolean);
}

export async function conversationUnavailableVisible(page: Page): Promise<boolean> {
const url = new URL(page.url());
Expand Down Expand Up @@ -113,8 +156,8 @@ export async function throwIfSessionAttention(page: Page): Promise<void> {
if(conversationLoadWasRateLimited(page))throw new BrowserCompanionError("ChatGPT conversation loading is temporarily rate limited; wait before retrying","conversation_load_rate_limited");
throw new BrowserCompanionError("ChatGPT conversation temporarily not accessible", "conversation_unavailable");
}
if (await page.getByTestId("regenerate-thread-error-button").last().isVisible().catch(() => false)
|| await terminalErrorAlert(page).isVisible().catch(() => false)) {
const failures=page.getByTestId("regenerate-thread-error-button").filter({visible:true}).or(terminalErrorAlert(page));
if ((await responseDeliveryTimeoutMatches(page,failures)).some(timeout=>!timeout)) {
throw new BrowserCompanionError("ChatGPT displayed a terminal response error.", "failed");
}
}
6 changes: 5 additions & 1 deletion browser-companion/src/chatgpt-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ async function visibleStopDialogs(page:Page):Promise<Locator[]> {
for(const frame of page.frames()) {
try {
if(await visibleChatGptFrame(frame)) {
dialogs.push(...await frame.locator('[role="dialog"], [role="alertdialog"]').filter({visible:true}).all());
// ChatGPT exposes non-modal suggestion menus and tool popovers with
// role=dialog even after the prior response has finished. They are
// not Stop confirmations. Only a genuinely modal dialog may block
// Stop recovery; all real Stop/approval confirmations remain guarded.
dialogs.push(...await frame.locator('[role="dialog"][aria-modal="true"], [role="alertdialog"][aria-modal="true"]').filter({visible:true}).all());
}
} catch(error) {
if(!frame.isDetached())throw error;
Expand Down
15 changes: 15 additions & 0 deletions browser-companion/tests/pairing-submit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,21 @@ test.skipIf(!native)("offline messages and goals stop lingering generation befor
}
},25_000);

test.skipIf(!native)("finished responses ignore non-modal ChatGPT popovers rather than treating them as Stop confirmation",async()=>{
for(const aria of [' aria-modal="false"',''] as const){
const driver=await fixture();
// Search and tool menus can use role=dialog without modal semantics.
// They must not be interpreted as a Stop approval when no Stop button exists.
await page.evaluate(aria=>document.body.insertAdjacentHTML('beforeend',
'<div role="dialog"'+aria+' data-testid="nonmodal-menu">Suggestions</div>'),aria);
const offline=offlineRequest();
await executeFixture(driver,{request:offline,renderedPrompt:renderActionPrompt(offline.prompt),signal:new AbortController().signal,emit:()=>{}});
expect(await page.locator('body').getAttribute('data-sends')).toBe('1');
expect(await page.locator('body').getAttribute('data-stops')).toBe('0');
expect(await page.locator('[data-testid="nonmodal-menu"]').count()).toBe(1);
}
},18_000);

test.skipIf(!native)("offline Stop confirms an optional localized dialog before sending once",async()=>{
const prompts:ExecuteActionRequest["prompt"][]=[
{kind:"offline_chat",batchId:"dialog-batch",messages:[{id:"dialog-message",markdown:"Continue"}]},
Expand Down
93 changes: 93 additions & 0 deletions browser-companion/tests/response-delivery-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {afterAll, beforeAll, expect, test} from "bun:test";
import {mkdtemp,rm} from "node:fs/promises";
import {tmpdir} from "node:os";
import {join} from "node:path";
import {chromium,type BrowserContext,type Page} from "patchright";
import {privateChromeExecutable} from "../src/chrome-executable";
import {deliveryTimeoutText,previousResponseDeliveryTimedOut,throwIfSessionAttention} from "../src/browser-session";

const localizedTimeouts=[
"Cas za dostavo sporocila se je iztekel. Poskusite znova.", // ASCII is not the real locale; must not match
"\u010cas za dostavo sporo\u010dila se je iztekel. Poskusite znova.",
"Message delivery timed out. Please try again.",
"Message delivery has timed out. Retry.",
] as const;

test("recognized response-delivery timeout text is narrowly localized",()=>{
expect(deliveryTimeoutText.test(localizedTimeouts[0])).toBe(false);
for(const text of localizedTimeouts.slice(1))expect(deliveryTimeoutText.test(text)).toBe(true);
for(const text of [
"Delivery will take longer than expected",
"Your message was not sent. Please retry.",
"The connection timed out while reading the repository.",
"You have reached a conversation limit.",
])expect(deliveryTimeoutText.test(text)).toBe(false);
});

const native=process.env.CODEXIFY_TEST_BROWSER_COMPANION_CHROME==="1";
let context:BrowserContext;
let page:Page;
let profile:string;

beforeAll(async()=>{
if(!native)return;
profile=await mkdtemp(join(tmpdir(),"codexify-delivery-timeout-"));
const executablePath=await privateChromeExecutable(join(import.meta.dirname,"../.browsers",`${process.platform}-${process.arch}`));
context=await chromium.launchPersistentContext(profile,{channel:"chrome",executablePath,headless:true});
page=context.pages()[0]!;
await context.route("https://chatgpt.com/c/timeout-fixture",route=>route.fulfill({contentType:"text/html",body:"<main><form><div contenteditable=true id=prompt-textarea></div></form></main>"}));
await page.goto("https://chatgpt.com/c/timeout-fixture");
},60_000);

afterAll(async()=>{
await context?.close();
if(profile)await rm(profile,{recursive:true,force:true,maxRetries:6,retryDelay:500});
});

test.skipIf(!native)("localized timeout in app-owned alert permits new continuation, never clicks Retry",async()=>{
for(const message of localizedTimeouts.slice(1)){
await page.setContent(`<main><form><div id="prompt-textarea" contenteditable="true"></div></form>
<article data-message-author-role="user">Existing accepted user message</article>
<div role="alert">${message}<button data-testid="regenerate-thread-error-button">Try again</button></div></main>`);
expect(await previousResponseDeliveryTimedOut(page)).toBe(true);
await throwIfSessionAttention(page);
expect(await page.getByTestId("regenerate-thread-error-button").count()).toBe(1);
}
},60_000);

test.skipIf(!native)("quoted user content cannot masquerade as an app-owned delivery timeout",async()=>{
await page.setContent(`<main><form><div id="prompt-textarea" contenteditable="true"></div></form>
<article data-message-author-role="user"><div role="alert">${localizedTimeouts[1]}</div></article>
<button data-testid="regenerate-thread-error-button">Try again</button></main>`);
expect(await previousResponseDeliveryTimedOut(page)).toBe(false);
await expect(throwIfSessionAttention(page)).rejects.toMatchObject({code:"failed"});
},60_000);

test.skipIf(!native)("visible app Retry button and adjacent localized message work without alert role",async()=>{
await page.setContent(`<main><form><div id="prompt-textarea" contenteditable="true"></div></form>
<div class="rounded border"><p>\u010cas za dostavo sporo\u010dila se je iztekel. Poskusite znova.</p>
<button data-testid="regenerate-thread-error-button" onclick="document.body.dataset.clicked='true'">Poskusi znova</button></div></main>`);
expect(await previousResponseDeliveryTimedOut(page)).toBe(true);
await throwIfSessionAttention(page);
expect(await page.locator("body").getAttribute("data-clicked")).toBeNull();
},60_000);

test.skipIf(!native)("unrelated terminal failure remains a blocker",async()=>{
await page.setContent(`<main><form><div id="prompt-textarea" contenteditable="true"></div></form>
<div role="alert">The response failed.</div><button data-testid="regenerate-thread-error-button">Try again</button></main>`);
expect(await previousResponseDeliveryTimedOut(page)).toBe(false);
await expect(throwIfSessionAttention(page)).rejects.toMatchObject({code:"failed"});
},60_000);

const timeoutCard=`<div role="alert">${localizedTimeouts[2]}<button data-testid="regenerate-thread-error-button">Retry old response</button></div>`;
const terminalCard='<div role="alert">Something went wrong. See help.openai.com.<button data-testid="regenerate-thread-error-button">Retry current response</button></div>';
for(const [name,cards] of [
['timeout before terminal',timeoutCard+terminalCard],
['terminal before timeout',terminalCard+timeoutCard],
['separate Retry in a shared wrapper',`<section><div role="alert">${localizedTimeouts[2]}</div><div><button data-testid="regenerate-thread-error-button">Retry unrelated response</button></div></section>`],
['timeout without ARIA alert',`<div><p>${localizedTimeouts[2]}</p><button data-testid="regenerate-thread-error-button">Retry old response</button></div>`+terminalCard],
] as const)test.skipIf(!native)(`a previous timeout never exempts a different error: ${name}`,async()=>{
await page.setContent(`<main><form><div id="prompt-textarea" contenteditable="true"></div></form>${cards}</main>`);
expect(await previousResponseDeliveryTimedOut(page)).toBe(true);
await expect(throwIfSessionAttention(page)).rejects.toMatchObject({code:"failed"});
},30_000);
6 changes: 4 additions & 2 deletions scripts/test-owner-chat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,8 +1423,10 @@ for (const [engineName, engine] of [["Chromium",chromium],["WebKit",webkit]]) {
const dialog=page.getByRole('dialog',{name:'New chat'});
const open=async()=>{
const back=page.locator('#chat-host').getByRole('button',{name:'Back to chats',exact:true});
if(await back.isVisible())await back.click();
await page.getByRole('button',{name:'New chat',exact:true}).click();
// On mobile the sidebar becomes visible only after the chat header hydrates.
const newChat=page.getByRole('button',{name:'New chat',exact:true});
if(!(await newChat.isVisible())){await back.waitFor({state:'visible'});await back.click();}
await newChat.click();
await dialog.getByRole('button',{name:'Existing repo',exact:true}).waitFor();
await dialog.locator('#new-chat-model:enabled').waitFor();
assert.deepEqual(await dialog.getByLabel('Reasoning effort').locator('option').allTextContents(),['Pro','Extra High','High','Medium','Instant']);
Expand Down
8 changes: 2 additions & 6 deletions src/agent_automation/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl AutomationTaskState {
return false;
};
let action = &self.action;
if goal.status != GoalStatus::Active
!(goal.status != GoalStatus::Active
|| action.goal_revision != Some(goal.revision)
|| action.id.is_none()
|| action.status == AutomationActionStatus::Idle
Expand All @@ -437,11 +437,7 @@ impl AutomationTaskState {
|| action.kind == Some(AutomationActionKind::OfflineCombined)
&& action.status != AutomationActionStatus::Cancelled
&& action.send_accepted == Some(true)
&& action.messages_fenced)
{
return false;
}
true
&& action.messages_fenced))
}

pub(crate) fn goal_retry_at_ms(&self) -> Option<u64> {
Expand Down
Loading