-
Notifications
You must be signed in to change notification settings - Fork 331
fix(claude-code): preflight subagent model access #3043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MagicLizi
merged 1 commit into
makecindy:main
from
ZJPex:fix/issue-2915-claude-subagent-model-preflight
Aug 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
apps/desktop/src/main/maker-host/__tests__/subagentModelAccess.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { classifyClaudeSubagentModelAccess } from '../subagent-model-access-policy.js'; | ||
|
|
||
| const xdModel = (id: string) => ({ id, agents: ['claude-code' as const] }); | ||
|
|
||
| function classify(overrides: Partial<Parameters<typeof classifyClaudeSubagentModelAccess>[0]> = {}) { | ||
| return classifyClaudeSubagentModelAccess({ | ||
| providerId: 'xd', | ||
| credentialMode: 'gateway-key', | ||
| model: 'sonnet', | ||
| gatewayKeyAvailable: true, | ||
| xdSnapshot: { authoritative: true, models: [xdModel('codex/gpt-5.6-sol')] }, | ||
| providers: [], | ||
| ...overrides, | ||
| }); | ||
| } | ||
|
|
||
| describe('Claude subagent model access', () => { | ||
| it('denies an absent alias only from the current authoritative XD snapshot', () => { | ||
| expect(classify()).toEqual({ status: 'denied' }); | ||
| expect(classify({ | ||
| xdSnapshot: { authoritative: false, models: [xdModel('claude-sonnet-4-6')] }, | ||
| })) | ||
| .toEqual({ status: 'unknown' }); | ||
| }); | ||
|
|
||
| it('accepts aliases, full ids, and the [1m] suffix from the XD snapshot', () => { | ||
| const snapshot = { | ||
| authoritative: true, | ||
| models: [xdModel('claude-sonnet-4-6'), xdModel('xai/grok-4.6')], | ||
| }; | ||
| expect(classify({ model: 'sonnet', xdSnapshot: snapshot })).toEqual({ status: 'allowed' }); | ||
| expect(classify({ model: 'CLAUDE-SONNET-4-6[1m]', xdSnapshot: snapshot })) | ||
| .toEqual({ status: 'allowed' }); | ||
| expect(classify({ model: 'xai/grok-4.6', xdSnapshot: snapshot })) | ||
| .toEqual({ status: 'allowed' }); | ||
| }); | ||
|
|
||
| it('does not treat a static or stale non-XD catalog absence as denial', () => { | ||
| expect(classify({ | ||
| providerId: 'anthropic', | ||
| credentialMode: 'oauth-bearer', | ||
| gatewayKeyAvailable: true, | ||
| providers: [{ | ||
| id: 'anthropic', | ||
| connected: true, | ||
| models: { 'claude-code': [] }, | ||
| }], | ||
| })).toEqual({ status: 'unknown' }); | ||
| }); | ||
|
|
||
| it('re-evaluates sequential account snapshots instead of retaining a denial', () => { | ||
| expect(classify()).toEqual({ status: 'denied' }); | ||
| expect(classify({ | ||
| xdSnapshot: { authoritative: true, models: [xdModel('claude-sonnet-4-6')] }, | ||
| })).toEqual({ status: 'allowed' }); | ||
| }); | ||
|
|
||
| it('uses the default XD route for OAuth spawn when a gateway key is active', () => { | ||
| expect(classify({ | ||
| providerId: null, | ||
| credentialMode: 'oauth-bearer', | ||
| gatewayKeyAvailable: true, | ||
| })).toEqual({ status: 'denied' }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
apps/desktop/src/main/maker-host/subagent-model-access-policy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import type { | ||
| AgentCredentialMode, | ||
| ClaudeSubagentModelAccessResult, | ||
| } from '@cindy/maker-core'; | ||
| import type { ProviderView } from '@cindy/model-providers'; | ||
|
|
||
| const CLAUDE_ALIASES = ['sonnet', 'opus', 'haiku', 'fable'] as const; | ||
|
|
||
| function normalizeModel(model: string): string { | ||
| const normalized = model.trim().toLowerCase(); | ||
| return normalized.endsWith('[1m]') | ||
| ? normalized.slice(0, -'[1m]'.length) | ||
| : normalized; | ||
| } | ||
|
|
||
| function modelMatches(requested: string, candidate: string): boolean { | ||
| const normalizedCandidate = normalizeModel(candidate); | ||
| if (requested === normalizedCandidate) return true; | ||
| return CLAUDE_ALIASES.some( | ||
| (alias) => requested === alias && normalizedCandidate.includes(`claude-${alias}-`), | ||
| ); | ||
| } | ||
|
|
||
| interface XdAccessModel { | ||
| id: string; | ||
| agents?: readonly string[]; | ||
| } | ||
|
|
||
| interface XdAccessSnapshot { | ||
| authoritative: boolean; | ||
| models: readonly XdAccessModel[]; | ||
| } | ||
|
|
||
| function xdClaudeCodeModels(models: readonly XdAccessModel[]): readonly string[] { | ||
| return models | ||
| .filter((model) => model.agents?.includes('claude-code')) | ||
| .map((model) => model.id); | ||
| } | ||
|
|
||
| export function classifyClaudeSubagentModelAccess(input: { | ||
| providerId?: string | null; | ||
| credentialMode?: AgentCredentialMode; | ||
| model: string; | ||
| gatewayKeyAvailable: boolean; | ||
| xdSnapshot: XdAccessSnapshot; | ||
| providers: readonly Pick<ProviderView, 'id' | 'connected' | 'suspended' | 'models'>[]; | ||
| }): ClaudeSubagentModelAccessResult { | ||
| const requested = normalizeModel(input.model); | ||
| if (!requested) return { status: 'unknown' }; | ||
|
|
||
| const providerId = input.providerId?.trim() || null; | ||
| const usesXdGateway = providerId === 'xd' | ||
| || (!providerId && input.credentialMode === 'gateway-key') | ||
| // OAuth spawn 有 XD key 时,本地 compat proxy 的默认路由同样换成网关 key。 | ||
| || (!providerId && input.gatewayKeyAvailable); | ||
|
|
||
| if (usesXdGateway) { | ||
| if (!input.xdSnapshot.authoritative) return { status: 'unknown' }; | ||
| return xdClaudeCodeModels(input.xdSnapshot.models).some((id) => modelMatches(requested, id)) | ||
| ? { status: 'allowed' } | ||
| : { status: 'denied' }; | ||
| } | ||
|
|
||
| // 非 XD 来源没有统一的权威“负清单”:当前连接来源的正命中可以放行, | ||
| // 缺席只能说明目录未发现/过期,绝不能据此硬阻断。 | ||
| const candidates = providerId | ||
| ? input.providers.filter((provider) => provider.id === providerId) | ||
| : input.providers; | ||
| const positivelyAvailable = candidates.some((provider) => | ||
| provider.connected | ||
| && !provider.suspended | ||
| && (provider.models['claude-code'] ?? []).some( | ||
| (model) => !model.disabled && modelMatches(requested, model.id), | ||
| ), | ||
| ); | ||
| return positivelyAvailable ? { status: 'allowed' } : { status: 'unknown' }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { | ||
| AgentCredentialMode, | ||
| ClaudeSubagentModelAccessResult, | ||
| } from '@cindy/maker-core'; | ||
|
|
||
| import { readClaudeApiKey } from './auth-adapters.js'; | ||
| import { | ||
| getActiveCatalog, | ||
| getXdGatewayModelAccessSnapshot, | ||
| } from './active-catalog.js'; | ||
| import { getDesktopProviderService } from './createDesktopProviderService.js'; | ||
| import { classifyClaudeSubagentModelAccess } from './subagent-model-access-policy.js'; | ||
|
|
||
| /** | ||
| * ClaudeCodeAgent 的 host 注入入口。每次 PreToolUse 都现场读取 key、权威 XD | ||
| * 快照和 ProviderView,因此账号切换不会复用会话启动时冻结的权限结论。 | ||
| */ | ||
| export async function resolveDesktopClaudeSubagentModelAccess(context: { | ||
| providerId?: string | null; | ||
| parentModel: string; | ||
| credentialMode?: AgentCredentialMode; | ||
| model: string; | ||
| }): Promise<ClaudeSubagentModelAccessResult> { | ||
| try { | ||
| const providers = await getDesktopProviderService().listProviders({ | ||
| allowSideEffects: false, | ||
| catalog: getActiveCatalog(), | ||
| }); | ||
| return classifyClaudeSubagentModelAccess({ | ||
| providerId: context.providerId, | ||
| credentialMode: context.credentialMode, | ||
| model: context.model, | ||
| gatewayKeyAvailable: Boolean(readClaudeApiKey()), | ||
| xdSnapshot: getXdGatewayModelAccessSnapshot(), | ||
| providers, | ||
| }); | ||
| } catch { | ||
| return { status: 'unknown' }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.