Skip to content

fix(agent-core-v2): keep invalidated OAuth flows observable during login - #1663

Merged
7Sageer merged 3 commits into
mainfrom
fix/v2-oauth-login-invalidation
Jul 14, 2026
Merged

fix(agent-core-v2): keep invalidated OAuth flows observable during login#1663
7Sageer merged 3 commits into
mainfrom
fix/v2-oauth-login-invalidation

Conversation

@7Sageer

@7Sageer 7Sageer commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

When the provider configuration changed while a device-code OAuth login was in flight (e.g. a managed-provider model refresh rewriting the persisted config mid-login), OAuthService.invalidateFlows() deleted the flow from the map outright. Two consequences:

  1. Terminal flows lost their TERMINAL_RETENTION_MS observability window.
  2. If the invalidation landed in the microtask gap after toolkit.login resolved but before handleSuccess ran, the state.status !== 'pending' guards there could not observe it (an abort is inert once the final poll returned). setTerminal then wrote authenticated onto an orphaned state that getFlow could never return — the login hung after a successful browser authorization.

A related trigger: a KIMI_CODE_BASE_URL override with a trailing slash was persisted verbatim by provision but rewritten normalized by the model refresh, and the deep-equal diff between the two shapes fired a spurious providers-changed event during login.

What changed

  • invalidateFlows() now transitions affected pending flows to a visible cancelled terminal status (with an explanatory error_message) instead of deleting them, mirroring the existing abortExisting() behavior; non-pending flows keep their retention window.
  • kimiCodeBaseUrl() strips trailing slashes from the KIMI_CODE_BASE_URL env override at the source, so the persisted and the rewritten provider configs share the same canonical shape.
  • Tests: three new cases in auth.test.ts (provider removed, provider changed, and the post-resolve microtask gap) and a normalization test in managed-usage.test.ts.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

invalidateFlows() deleted flows outright when the provider configuration
changed, including terminal ones. That voided the TERMINAL_RETENTION_MS
window and, worse, left the status guards in handleSuccess /
finalizeAuthentication blind when the invalidation landed in the
microtask gap after toolkit.login resolved: setTerminal then wrote
'authenticated' onto an orphaned state the client could never observe,
so the login hung after a successful browser authorization.

Transition affected pending flows to a visible 'cancelled' terminal
status instead, mirroring abortExisting().

Also normalize the KIMI_CODE_BASE_URL env override at the source in
kimiCodeBaseUrl(): provision persists it verbatim while the model
refresh rewrites it normalized, and the deep-equal diff between the two
shapes fired a spurious providers-changed event mid-login.
@changeset-bot

changeset-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6dc8bb7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@moonshot-ai/agent-core-v2 Patch
@moonshot-ai/kimi-code Patch
@moonshot-ai/kap-server Patch
@moonshot-ai/klient Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6dc8bb7
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6dc8bb7

commit: 6dc8bb7

@7Sageer
7Sageer merged commit 1294a0e into main Jul 14, 2026
14 checks passed
@7Sageer
7Sageer deleted the fix/v2-oauth-login-invalidation branch July 14, 2026 04:16
@github-actions github-actions Bot mentioned this pull request Jul 14, 2026
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