jackwener/OpenCLI · error · CommandExecutionError

Failed to add @${username} to list ${listId}: malformed muta

Error message

Failed to add @${username} to list ${listId}: malformed mutation response envelope

What it means

The ListAddMember mutation runs via page.evaluate and returns a JSON-stringified tuple envelope [httpOk, status, member_count, is_member, errors, raw, fetchError]. The command unwraps it with unwrapBrowserResult and JSON.parse; if the parsed string is not valid JSON, the in-page/result plumbing was corrupted and the mutation outcome is unknowable, so it throws CommandExecutionError('malformed mutation response envelope').

Source

Thrown at clis/twitter/list-add-core.js:212

                return JSON.stringify([
                    r.ok,
                    r.status,
                    list ? list.member_count : null,
                    list ? list.is_member : null,
                    body && body.errors ? body.errors : null,
                    raw,
                    null,
                ]);
            } catch (e) {
                return JSON.stringify([false, 0, null, null, null, null, String(e)]);
            }
        }`);
        const addResultJson = unwrapBrowserResult(addResultJsonRaw);
        let addResultTuple;
        try {
            addResultTuple = JSON.parse(addResultJson);
        } catch {
            throw new CommandExecutionError(`Failed to add @${username} to list ${listId}: malformed mutation response envelope`);
        }
        const addResult = Object.create(null);
        addResult.httpOk = Boolean(addResultTuple?.[0]);
        addResult.status = Number(addResultTuple?.[1]) || 0;
        addResult.mc = addResultTuple?.[2];
        addResult.isMember = addResultTuple?.[3];
        addResult.errors = addResultTuple?.[4];
        addResult.raw = addResultTuple?.[5];
        addResult.fetchError = addResultTuple?.[6];

    return [buildListAddMemberRow({ addResult, memberCountBefore, listId, username, userId })];
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Check your @jackwener/opencli version: >=1.7.x wraps evaluate results as {session,data}; ensure unwrapBrowserResult matches your installed version (upgrade or downgrade so they agree).
  2. Re-run the command once — a transient page-context interruption can corrupt a single evaluate result.
  3. Add logging around unwrapBrowserResult's raw return to inspect what actually came back from page.evaluate.
  4. If reproducible, verify no page navigation/race occurs during the evaluate (keep the tab on x.com).

Example fix

// before: mixed versions
npm ls @jackwener/opencli  # 1.6.x installed, unwrapBrowserResult expects 1.7.x envelope
// after
npm install @jackwener/opencli@latest  # evaluate wrapper and unwrapBrowserResult in sync
Defensive patterns

Strategy: type-guard

Type guard

function isMutationEnvelope(v) {
  if (typeof v !== 'string') return false;
  try {
    const t = JSON.parse(v);
    return Array.isArray(t) && typeof t[0] === 'boolean' && typeof t[1] === 'number';
  } catch { return false; }
}

Try / catch

try {
  const result = await listAddUser(page, { listId, username });
} catch (e) {
  if (/malformed mutation response envelope/.test(e.message)) {
    console.error('page.evaluate result did not parse — check opencli version vs unwrapBrowserResult; retrying once.');
    return retryOnce();
  }
  throw e;
}

Prevention

When it happens

Trigger: unwrapBrowserResult returns a value that is not the JSON.stringify'd tuple the in-page async function produced — e.g. the evaluate result was stringified as '[object Object]', a serialization wrapper mismatch (opencli >=1.7.x {session,data} envelope handled differently), or the string was truncated/corrupted in transit.

Common situations: Running against an older/newer opencli version whose page.evaluate return-value wrapping differs from what unwrapBrowserResult expects; the page navigated or the context was destroyed mid-evaluate; a proxy/interceptor mangled the evaluate result.

Understand the failure class

Related errors


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