{"record":{"id":"079a897faf5049cb","repo":"FoundationAgents/MetaGPT","slug":"invalid-tab-focus-action-step","errorCode":null,"errorMessage":"Invalid tab_focus action {step}","messagePattern":"Invalid tab_focus action (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/a11y_tree.py","lineNumber":85,"sourceCode":"            raise ValueError(f\"Invalid scroll action {step}\")\n        direction = match.group(1)\n        await scroll_page(page, direction)\n    elif func == \"goto\":\n        match = re.search(r\"goto ?\\[(.+)\\]\", step)\n        if not match:\n            raise ValueError(f\"Invalid goto action {step}\")\n        url = match.group(1)\n        await page.goto(url)\n    elif func == \"new_tab\":\n        page = await browser_ctx.new_page()\n    elif func == \"go_back\":\n        await page.go_back()\n    elif func == \"go_forward\":\n        await page.go_forward()\n    elif func == \"tab_focus\":\n        match = re.search(r\"tab_focus ?\\[(\\d+)\\]\", step)\n        if not match:\n            raise ValueError(f\"Invalid tab_focus action {step}\")\n        page_number = int(match.group(1))\n        page = browser_ctx.pages[page_number]\n        await page.bring_to_front()\n    elif func == \"close_tab\":\n        await page.close()\n        if len(browser_ctx.pages) > 0:\n            page = browser_ctx.pages[-1]\n        else:\n            page = await browser_ctx.new_page()\n    elif func == \"stop\":\n        match = re.search(r'stop\\(?\"(.+)?\"\\)', step)\n        answer = match.group(1) if match else \"\"\n        return answer\n    else:\n        raise ValueError\n    await page.wait_for_load_state(\"domcontentloaded\")\n    return page\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/a11y_tree.py#L67-L103","documentation":"MetaGPT's execute_action raises this when a step classified as tab_focus fails to match r'tab_focus ?\\[(\\d+)\\]'. Only a decimal page index inside brackets, e.g. 'tab_focus [0]', is accepted; anything else (non-numeric, missing brackets) fails.","triggerScenarios":"execute_action(page, 'tab_focus [first]') (non-numeric); 'tab_focus 0' without brackets; 'tab_focus [-1]' (\\d+ rejects negative sign); LLM emitting 'tab_focus [1st tab]'.","commonSituations":"LLM refers to tabs by name or ordinal words instead of a zero-based integer index. Also triggered by off-by-one confusion: the value is used directly as browser_ctx.pages[page_number], so the model may emit human-style 1-based indices that are syntactically fine but semantically wrong — and any formatting slip raises this error.","solutions":["Use a zero-based integer index in brackets: 'tab_focus [0]'.","Remember the index is passed straight to browser_ctx.pages[page_number], so index 0 is the first page.","Log the step and the current len(browser_ctx.pages) to pick a valid index.","Prompt the LLM with explicit tab_focus examples including the bracket syntax."],"exampleFix":"# before\nawait execute_action(page, 'tab_focus [1st]')  # raises Invalid tab_focus action\n\n# after\nawait execute_action(page, 'tab_focus [0]')","handlingStrategy":"validation","validationCode":"import re\n\ndef valid_tab_focus_index(step: str, page_count: int):\n    m = re.search(r\"tab_focus ?\\[(\\d+)\\]\", step)\n    if not m:\n        return None\n    idx = int(m.group(1))\n    return idx if 0 <= idx < page_count else None","typeGuard":null,"tryCatchPattern":"try:\n    await execute_action(page, step)\nexcept (ValueError, IndexError) as e:\n    # invalid syntax or out-of-range page index: refresh tabs and re-ask","preventionTips":["Tell the LLM tab indices are zero-based and must fit the current tab list.","Bound-check the index against len(browser_ctx.pages) — an in-range check also prevents the separate IndexError."],"tags":["metagpt","browser-agent","llm-output","regex","tabs"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}