Skip to content

fix(acp): resolve cold-start race with belt-and-suspenders defense#1172

Open
chaodu-agent wants to merge 1 commit into
XiaomiMiMo:mainfrom
chaodu-agent:fix/acp-cold-start-belt-and-suspenders
Open

fix(acp): resolve cold-start race with belt-and-suspenders defense#1172
chaodu-agent wants to merge 1 commit into
XiaomiMiMo:mainfrom
chaodu-agent:fix/acp-cold-start-belt-and-suspenders

Conversation

@chaodu-agent

Copy link
Copy Markdown

Summary

Belt-and-suspenders fix for the ACP cold-start race condition where DB migration delays provider loading, causing defaultModel() to fall through to Provider.sort() and pick a paid model.

Supersedes #1170 and #1171 (combined into one PR with unit tests).

Proactive Fix (acp.ts)

Poll providers endpoint (max 10× at 500ms intervals) before accepting requests:

  • Ensures providers are loaded after DB migration completes
  • Logs warning if providers don't load within 5s (observable timeout)

Defensive Fix (agent.ts)

When user specifies a model in config.json but:

  • Provider not yet in the providers list → return user's explicit choice
  • Provider loaded but model not indexed → return user's explicit choice

Instead of falling through to Provider.sort() which may pick an unintended paid model.

Tests

10 new unit tests covering both paths:

  • test/acp/default-model.test.ts (6 tests): resolution logic for all provider/model states
  • test/acp/wait-for-providers.test.ts (4 tests): polling retry, immediate success, timeout
bun test test/acp/
# 21 pass, 0 fail (includes existing acp tests)

Design

Extracted testable pure functions:

  • resolve-model.ts — stateless model resolution (no SDK, no side effects)
  • wait-for-providers.ts — configurable polling with log.warn on timeout

Verification

  • bun test test/acp/ — ✅ 21 pass
  • bun run typecheck — ✅ clean

Fixes #865, Fixes #866

Two complementary fixes for the ACP cold-start race condition where
DB migration delays provider loading, causing defaultModel() to fall
through to Provider.sort() and pick a paid model.

Proactive fix (acp.ts):
- Poll providers endpoint (max 10x, 500ms intervals) before accepting
  requests, ensuring providers are loaded post-migration
- Log warning if providers don't load within 5s

Defensive fix (agent.ts):
- When user specifies a model in config.json but the provider is not
  yet loaded (or provider loaded but model not indexed), return the
  user's explicit choice instead of falling through to Provider.sort()

Extracted testable modules:
- resolve-model.ts: pure resolution logic (6 unit tests)
- wait-for-providers.ts: polling loop with configurable retry (4 unit tests)

Fixes XiaomiMiMo#865, Fixes XiaomiMiMo#866
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.

First ACP request after cold start returns empty (DB migration race) ACP mode ignores default model set by mimo auth login

1 participant