@@ -16,6 +16,7 @@ import {
1616 type ExtensionMessage ,
1717 type ExtensionState ,
1818 type RooCodeSettings ,
19+ type ModeConfig ,
1920 type WebviewMessage ,
2021 ORGANIZATION_ALLOW_ALL ,
2122 DEFAULT_CHECKPOINT_TIMEOUT_SECONDS ,
@@ -4108,6 +4109,62 @@ describe("ClineProvider", () => {
41084109
41094110 expect ( setValues ) . toHaveBeenCalledWith ( { mode : "code" } )
41104111 } )
4112+
4113+ it ( "initializes a new task with a custom mode passed beside its own slug" , async ( ) => {
4114+ // Row 4509495977: createTask registers the custom modes of a configuration before it
4115+ // calls setValues(), and setValues() only keeps a mode the custom-mode store can already
4116+ // resolve. The double below makes a slug resolvable exactly once registration has stored
4117+ // it, so moving the registration after setValues() drops the mode here.
4118+ const slug = "release-reviewer"
4119+ const registered = new Map < string , ModeConfig > ( )
4120+ const updateCustomMode = vi . fn ( ) . mockImplementation ( async ( modeSlug : string , mode : ModeConfig ) => {
4121+ registered . set ( modeSlug , mode )
4122+ } )
4123+ ; ( provider as MemberSwapTarget ) . customModesManager = {
4124+ updateCustomMode,
4125+ getCustomModes : vi . fn ( ) . mockImplementation ( async ( ) => [ ...registered . values ( ) ] ) ,
4126+ }
4127+ const setValues = vi . spyOn ( provider . contextProxy , "setValues" ) . mockResolvedValue ( undefined )
4128+ vi . spyOn ( provider , "log" ) . mockImplementation ( ( ) => { } )
4129+
4130+ const { getModeBySlug, modes } = await import ( "../../../shared/modes" )
4131+ const resolver = vi . mocked ( getModeBySlug )
4132+ // The harness default answers every slug with the code mode (mock factory above). This test
4133+ // mirrors the real resolver instead: a slug is a mode only while the custom-mode list the
4134+ // provider just read holds it. The finally block re-applies the factory's answer so later
4135+ // tests are unaffected by this override.
4136+ const harnessDefaultMode : ModeConfig = {
4137+ slug : "code" ,
4138+ name : "Code Mode" ,
4139+ roleDefinition : "You are a code assistant" ,
4140+ groups : [ "read" , "edit" ] ,
4141+ }
4142+ resolver . mockImplementation (
4143+ ( modeSlug : string , customModes ?: ModeConfig [ ] ) =>
4144+ customModes ?. find ( ( mode ) => mode . slug === modeSlug ) ?? modes . find ( ( mode ) => mode . slug === modeSlug ) ,
4145+ )
4146+
4147+ try {
4148+ const mode : ModeConfig = {
4149+ slug,
4150+ name : "Release reviewer" ,
4151+ roleDefinition : "You review releases before they ship" ,
4152+ groups : [ "read" ] ,
4153+ }
4154+
4155+ await provider
4156+ . createTask ( "hello" , undefined , undefined , { } , hand ( { customModes : [ mode ] , mode : slug } ) )
4157+ . catch ( ( ) => undefined ) // what happens after the configuration is written is not under test
4158+
4159+ expect ( updateCustomMode ) . toHaveBeenCalledWith ( slug , mode )
4160+ expect ( updateCustomMode . mock . invocationCallOrder [ 0 ] ) . toBeLessThan ( setValues . mock . invocationCallOrder [ 0 ] )
4161+ // The mode the new task initializes with is the one getState() now reports.
4162+ expect ( ( await provider . getState ( ) ) . mode ) . toBe ( slug )
4163+ } finally {
4164+ resolver . mockReset ( )
4165+ resolver . mockReturnValue ( harnessDefaultMode )
4166+ }
4167+ } )
41114168 } )
41124169
41134170 test ( "getState returns the saved destructive command guard setting" , async ( ) => {
@@ -6847,6 +6904,69 @@ describe("ClineProvider", () => {
68476904 await provider . dispose ( )
68486905 } )
68496906
6907+ it ( "keeps the restored profile visible to public readers when the shared store names another profile" , async ( ) => {
6908+ const writer = new ClineProvider (
6909+ mockContext ,
6910+ mockOutputChannel ,
6911+ "sidebar" ,
6912+ new ContextProxy ( mockContext ) ,
6913+ new WebviewFocusTracker ( ) ,
6914+ )
6915+ await writer [ "setViewStateId" ] ( "shared-view" )
6916+ await writer . saveViewState ( "mode" , "architect" )
6917+ await writer . saveViewState ( "currentApiConfigName" , "my-profile" )
6918+
6919+ const provider = new ClineProvider (
6920+ mockContext ,
6921+ mockOutputChannel ,
6922+ "editor" ,
6923+ new ContextProxy ( mockContext ) ,
6924+ new WebviewFocusTracker ( ) ,
6925+ )
6926+ const getProfile = vi . fn ( ) . mockResolvedValue ( {
6927+ name : "my-profile" ,
6928+ apiProvider : providerIdentifiers . openrouter ,
6929+ openRouterModelId : "model-x" ,
6930+ } )
6931+ ; ( provider as MemberSwapTarget ) . providerSettingsManager = { getProfile }
6932+ // The writer has to release the id before this view adopts the entry it persisted.
6933+ await writer . dispose ( )
6934+
6935+ // Another view activated a different profile in the shared store in the meantime, so the
6936+ // shared pair and the pair this view restores disagree on purpose.
6937+ await provider . contextProxy . setValue ( "currentApiConfigName" , "shared-profile" )
6938+ await provider . contextProxy . setProviderSettings ( { apiProvider : providerIdentifiers . anthropic } )
6939+ await provider . contextProxy . setValue (
6940+ "viewStates" ,
6941+ mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
6942+ )
6943+ await provider [ "setViewStateId" ] ( "shared-view" , persistedViewStateClaim ( mockContext , "shared-view" ) )
6944+
6945+ expect ( getProfile ) . toHaveBeenCalledWith ( { name : "my-profile" } )
6946+ expect ( provider [ "viewLocalState" ] ) . toEqual ( {
6947+ mode : "architect" ,
6948+ currentApiConfigName : "my-profile" ,
6949+ apiConfiguration : { apiProvider : providerIdentifiers . openrouter , openRouterModelId : "model-x" } ,
6950+ } )
6951+ // Public reader 1: getValues() merges this view's buffer, so the restored name and the
6952+ // configuration it resolved to are visible outside the instance that loaded them.
6953+ expect ( provider . getValues ( ) . currentApiConfigName ) . toBe ( "my-profile" )
6954+ expect ( provider . getValues ( ) . apiConfiguration ) . toEqual ( {
6955+ apiProvider : providerIdentifiers . openrouter ,
6956+ openRouterModelId : "model-x" ,
6957+ } )
6958+ // Public reader 2: getState() and the state posted to the webview still resolve the
6959+ // shared profile, while the mode they report is already the view-local one. That split is
6960+ // what row 4509495951 asks to close; pinning both sides here makes the fix flip a visible
6961+ // assertion instead of passing unnoticed.
6962+ const state = await provider . getState ( )
6963+ expect ( state . currentApiConfigName ) . toBe ( "shared-profile" )
6964+ expect ( state . apiConfiguration ) . toMatchObject ( { apiProvider : providerIdentifiers . anthropic } )
6965+ expect ( state . mode ) . toBe ( "architect" )
6966+ expect ( ( await provider . getStateToPostToWebview ( ) ) . currentApiConfigName ) . toBe ( "shared-profile" )
6967+ await provider . dispose ( )
6968+ } )
6969+
68506970 it ( "should apply setValue mutations to global state and keep or clear the right buffer fields" , async ( ) => {
68516971 const provider = new ClineProvider (
68526972 mockContext ,
@@ -9934,7 +10054,7 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
993410054 )
993510055 } )
993610056
9937- it ( "logs and posts state when profile persistence fails" , async ( ) => {
10057+ it ( "logs and posts state when the state read fails before the profile write " , async ( ) => {
993810058 vi . spyOn ( provider , "getState" ) . mockRejectedValue ( new Error ( "state unavailable" ) )
993910059 vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
994010060 ; ( provider as MemberSwapTarget ) . providerSettingsManager = {
@@ -9949,6 +10069,42 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
994910069 // State must still be refreshed even when profile persistence fails.
995010070 expect ( provider . postStateToWebview ) . toHaveBeenCalled ( )
995110071 } )
10072+
10073+ it ( "reports the failed profile write and still posts state" , async ( ) => {
10074+ // Row 4509500273: the case above rejects getState(), so it never reaches the write. Here
10075+ // the read succeeds and saveConfig rejects inside persistProviderProfile, which
10076+ // upsertProviderProfile swallows, logs, and reports to the user.
10077+ vi . spyOn ( provider , "getState" ) . mockResolvedValue ( {
10078+ apiConfiguration : { zooGatewayModelId : "anthropic/claude-sonnet-4" } ,
10079+ } as never )
10080+ vi . spyOn ( provider . contextProxy , "getProviderSettings" ) . mockReturnValue ( {
10081+ apiProvider : providerIdentifiers . anthropic ,
10082+ } as never )
10083+ vi . spyOn ( provider . contextProxy , "getValues" ) . mockReturnValue ( {
10084+ currentApiConfigName : "Anthropic" ,
10085+ } as never )
10086+ const postStateToWebview = vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
10087+ const saveConfig = vi . fn ( ) . mockRejectedValue ( new Error ( "saveConfig rejected" ) )
10088+ ; ( provider as MemberSwapTarget ) . providerSettingsManager = {
10089+ listConfig : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
10090+ saveConfig,
10091+ }
10092+
10093+ await provider . handleZooCodeCallback ( "zoo_ext_token" )
10094+
10095+ expect ( saveConfig ) . toHaveBeenCalledWith (
10096+ "Zoo Gateway" ,
10097+ expect . objectContaining ( {
10098+ apiProvider : providerIdentifiers . zooGateway ,
10099+ zooSessionToken : "zoo_ext_token" ,
10100+ } ) ,
10101+ )
10102+ expect ( mockOutputChannel . appendLine ) . toHaveBeenCalledWith (
10103+ expect . stringContaining ( "Error create new api configuration" ) ,
10104+ )
10105+ expect ( vscode . window . showErrorMessage ) . toHaveBeenCalledWith ( "errors.create_api_config" )
10106+ expect ( postStateToWebview ) . toHaveBeenCalled ( )
10107+ } )
995210108 it ( "updates every zoo-gateway profile and activates only the active one" , async ( ) => {
995310109 vi . spyOn ( provider , "getState" ) . mockResolvedValue ( {
995410110 apiConfiguration : { zooGatewayModelId : "anthropic/claude-sonnet-4" } ,
0 commit comments