jackwener/OpenCLI · error · CommandExecutionError

Failed to replace collapsed Xiaohongshu search tab: ${error?

Error message

Failed to replace collapsed Xiaohongshu search tab: ${error?.message ?? String(error)}.${cleanupContext}

What it means

This error is thrown by replaceCollapsedTab() when it fails to swap a collapsed Xiaohongshu search results tab for a fresh one. The browser-side navigation/replacement step raised an error, and the message embeds the original error message plus any cleanup failures that occurred while closing the collapsed tab. It signals that the automated fresh-tab recovery path itself broke, not just that results were collapsed.

Source

Thrown at clis/xiaohongshu/search.js:859

            }
            if (restoredPrevious) {
                try {
                    await page.closeTab(freshPage);
                }
                catch (cleanupError) {
                    cleanupErrors.push(cleanupError?.message ?? String(cleanupError));
                }
            }
            else {
                // If the old target disappeared despite the original error,
                // keep the fresh preferred target bound for --keep-tab.
                page.setActivePage(freshPage);
            }
        }
        const cleanupContext = cleanupErrors.length > 0
            ? ` Cleanup also failed: ${cleanupErrors.join('; ')}.`
            : '';
        throw new CommandExecutionError(
            `Failed to replace collapsed Xiaohongshu search tab: ${error?.message ?? String(error)}.${cleanupContext}`,
        );
    }
}

export const command = cli({
    site: 'xiaohongshu',
    name: 'search',
    access: 'read',
    description: '搜索小红书笔记',
    domain: 'www.xiaohongshu.com',
    strategy: Strategy.COOKIE,
    navigateBefore: false,
    args: [
        { name: 'query', required: true, positional: true, help: 'Search keyword' },
        { name: 'limit', type: 'int', default: 20, help: 'Number of results' },
        { name: 'sort', type: 'string', default: 'comprehensive', choices: ['comprehensive', 'latest', 'most-liked', 'most-commented', 'most-collected'], help: 'Sort order' },
        { name: 'note-type', type: 'string', default: 'all', choices: ['all', 'video', 'image'], help: 'Note type' },

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Check that the browser session backing the page is still alive and the logged-in profile is valid, then retry the search command.
  2. Inspect the embedded `${error.message}` in the message for the underlying browser error and fix that root cause (e.g. navigation timeout, context destroyed).
  3. If cleanup also failed ('Cleanup also failed' suffix), manually close stale tabs/pages before rerunning.
  4. Retry later or with a different logged-in browser session as the recovery path suggests; add your own retry wrapper around the CLI call.
  5. Upgrade/verify the browser automation runtime (chromium/CDP) version matches what the library expects.

Example fix

// before
await replaceCollapsedTab(page, url);
// after
try {
  await replaceCollapsedTab(page, url);
} catch (e) {
  if (!browser.isConnected()) await reopenBrowserAndLogin();
  await replaceCollapsedTab(page, url);
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!browser.isConnected()) throw new Error('browser session closed before search');

Type guard

function isSessionAlive(browser) { return !!browser && typeof browser.isConnected === 'function' && browser.isConnected(); }

Try / catch

try {
  await xhsSearch(query, { limit });
} catch (err) {
  if (String(err.message).includes('Failed to replace collapsed')) {
    await restartBrowserSession();
    return xhsSearch(query, { limit });
  }
  throw err;
}

Prevention

When it happens

Trigger: collectSearchHarvest() detects a collapsed masonry render (isCollapsedRender), replaceCollapsedTab is invoked to open a new tab and re-goto the search URL, and either the new-page creation, page.goto, or page.setActivePage throws; the catch block then attempts to close the broken pages and throws CommandExecutionError with cleanupErrors appended.

Common situations: The logged-in browser session was closed or the connection to the browser dropped mid-run; a popup blocker or browser profile prevents opening a new tab; the page object was invalidated by navigation; cleanup (closing old tabs) fails because the target was already closed.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/0b3aef0e2dd97a97. Report an issue: GitHub.