Skip to content

fix: enable fast mode patch for API auth#92

Open
siyuan-123 wants to merge 1 commit into
Haleclipse:masterfrom
siyuan-123:codex/fix-api-fast-mode
Open

fix: enable fast mode patch for API auth#92
siyuan-123 wants to merge 1 commit into
Haleclipse:masterfrom
siyuan-123:codex/fix-api-fast-mode

Conversation

@siyuan-123

@siyuan-123 siyuan-123 commented Jun 28, 2026

Copy link
Copy Markdown

Summary

This fixes the Fast mode patch for API-key auth on newer Codex Desktop bundles.

Root cause

The existing scripts/patch-fast-mode.js only handled the older negative auth gate shape inside fast-mode functions:

X.authMethod !== "chatgpt"

Newer upstream bundles now use positive chatgpt checks instead, for both the UI selector and request-time service_tier plumbing, for example:

a?.authMethod === `chatgpt`
n === `chatgpt`

As a result, API-key users (apikey) still cannot use Fast mode even after the old patch runs:

  • the speed selector remains gated to ChatGPT auth in use-service-tier-settings-*
  • request-time service tier evaluation remains gated to ChatGPT auth in read-service-tier-for-request-*

Changes

  • Teach the patcher to detect both string literal and template literal chatgpt comparisons.
  • Keep support for the old !== "chatgpt" gate and continue replacing it with !1.
  • Add support for the new === "chatgpt" / === `chatgpt` gate and expand it to also allow API-key auth:
expr === `chatgpt` || expr === `apikey`
  • Search target chunks by fast_mode + chatgpt instead of requiring the literal text authMethod, because the request-time helper can use a local auth variable such as n.
  • Improve --check output so it reports how many gates would be patched.

Impact

API-key users can access Fast mode UI and the selected Fast service tier is preserved when building requests.

Validation

On the Windows upstream ASAR assets, --check now finds both target gates:

[win] src\win\_asar\webview\assets\read-service-tier-for-request-DODCm1-G.js
  [?] n===`chatgpt` -> n===`chatgpt`||n===`apikey`
[win] src\win\_asar\webview\assets\use-service-tier-settings-Cc2_hi1D.js
  [?] a?.authMethod===`chatgpt` -> a?.authMethod===`chatgpt`||a?.authMethod===`apikey`
[check] 2 auth gate(s) would be patched

Also validated:

node -c scripts/patch-fast-mode.js
node scripts/patch-fast-mode.js win
node scripts/patch-fast-mode.js win --check

After patching, the second --check reports the gates are already patched/absent, confirming the script is idempotent.

Summary by Sourcery

Update the fast-mode patch script to support newer chatgpt-based auth gates and report patched gate counts for API-key users.

Bug Fixes:

  • Ensure fast mode UI and service tier selection work for API-key auth by expanding chatgpt-only auth gates in bundled assets.

Enhancements:

  • Extend the AST-based patcher to handle both old !== "chatgpt" checks and new === "chatgpt"/template literal forms without double-patching.
  • Relax bundle targeting from explicit authMethod references to generic fast_mode + chatgpt patterns across assets.
  • Improve --check mode output to show how many auth gates were detected and would be patched.

@sourcery-ai

sourcery-ai Bot commented Jun 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

Update the fast-mode patch script to handle both old and new auth gate shapes, expand chatgpt-only checks to include apikey, broaden bundle targeting heuristics, and improve check-mode reporting of detected gates.

Sequence diagram for updated fast mode patch script in check mode

sequenceDiagram
  participant CLI
  participant main
  participant bundles as locateBundles
  participant fs
  participant acorn
  participant patcher as collectPatches

  CLI->>main: node scripts/patch-fast-mode.js win --check
  main->>bundles: locateBundles SRC_DIR
  bundles-->>main: bundle list
  loop per bundle
    main->>fs: readFileSync(bundle.path)
    main->>main: src.includes chatgpt and fast_mode
    alt matches markers
      main->>acorn: parse(src, ecmaVersion)
      acorn-->>main: ast
      main->>patcher: collectPatches(ast, src)
      patcher-->>main: patches
      main->>main: totalFound += patches.length
      note over main: isCheck=true so no writeFileSync
    else no markers
      main->>main: skip bundle
    end
  end
  alt totalPatched > 0
    main-->>CLI: [ok] X auth gate(s) removed
  else totalPatched == 0 and totalFound > 0
    main-->>CLI: [check] totalFound auth gate(s) would be patched
  else
    main-->>CLI: [ok] fast_mode auth gates already patched or absent
  end
Loading

File-Level Changes

Change Details Files
Extend AST walking and gate detection to support new chatgpt-based equality checks, including template literals, while avoiding double-patching already-expanded expressions.
  • Add parent-tracking to the AST walk function so visitors receive both child and parent nodes.
  • Introduce helpers to recognize chatgpt string and template literals and to extract the non-literal side of binary expressions for reuse in apikey comparisons.
  • Detect BinaryExpression nodes with === operators where one side is a chatgpt literal, skip those already wrapped in a LogicalExpression containing apikey, and generate patches that expand the comparison to also match apikey.
scripts/patch-fast-mode.js
Maintain support for the legacy !== "chatgpt" auth gate and ensure it continues to be patched to always-false while preventing duplicate patches.
  • Restrict legacy gate handling to BinaryExpression nodes with !== operators whose source contains both authMethod and chatgpt markers.
  • Guard against already-patched expressions (e.g., !1) and skip any BinaryExpression whose start offset is already in the patch list.
  • Record patches for legacy gates with replacement set to !1 and an explicit id for fast_mode_auth_gate.
scripts/patch-fast-mode.js
Broaden bundle targeting and function-level filtering heuristics to match newer assets using chatgpt markers rather than authMethod text.
  • Change per-function filtering to require fast_mode and chatgpt in the function source instead of fast_mode and authMethod.
  • Change bundle scanning to treat a file as a target when it contains both chatgpt and fast_mode markers rather than authMethod and fast_mode.
  • Rely on these broader markers to catch both UI selector and request-time service tier helper chunks regardless of local variable names.
scripts/patch-fast-mode.js
Improve check-mode output by tracking the number of gates found and reporting potential patches when running with --check.
  • Add a totalFound counter alongside totalPatched to track all detected gates across bundles.
  • Increment totalFound by the number of collected patches per bundle, even when not applying them in check mode.
  • Emit a summary line in check mode showing how many auth gates would be patched when totalPatched is zero but totalFound is positive, while preserving the existing idempotency message when nothing is found.
scripts/patch-fast-mode.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@siyuan-123
siyuan-123 marked this pull request as ready for review June 28, 2026 06:05

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • When expanding === 'chatgpt' comparisons, the replacement always uses a template literal for apikey; consider mirroring the original literal style (string vs template) to avoid unexpected formatting changes in minified/bundled code.
  • The expressionSourceForApiKeySide helper assumes the compared expression is a simple AST node and reuses its source slice; if more complex expressions or whitespace/comment variations appear, you may want to normalize or parenthesize the inserted apikey comparison to avoid precedence or formatting issues.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When expanding `=== 'chatgpt'` comparisons, the replacement always uses a template literal for `apikey`; consider mirroring the original literal style (string vs template) to avoid unexpected formatting changes in minified/bundled code.
- The `expressionSourceForApiKeySide` helper assumes the compared expression is a simple AST node and reuses its source slice; if more complex expressions or whitespace/comment variations appear, you may want to normalize or parenthesize the inserted `apikey` comparison to avoid precedence or formatting issues.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant