@@ -18,10 +18,16 @@ use codex_config::types::MarketplaceConfig;
1818use codex_config:: types:: MarketplaceSourceType ;
1919use codex_plugin:: validate_plugin_segment;
2020use codex_utils_absolute_path:: AbsolutePathBuf ;
21+ use std:: fs:: File ;
2122use std:: path:: Path ;
2223use std:: time:: Duration ;
24+ use std:: time:: SystemTime ;
25+ use tracing:: warn;
2326
2427const MARKETPLACE_UPGRADE_GIT_TIMEOUT : Duration = Duration :: from_secs ( 30 ) ;
28+ const MARKETPLACE_UPGRADE_LOCK_FILE : & str = ".tmp/marketplaces.upgrade.lock" ;
29+ // This exceeds the complete bounded Git sequence while still reclaiming interrupted upgrades.
30+ const MARKETPLACE_UPGRADE_STALE_TEMP_DIR_MAX_AGE : Duration = Duration :: from_secs ( 10 * 60 ) ;
2531
2632#[ derive( Debug , Clone , PartialEq , Eq ) ]
2733pub struct ConfiguredMarketplaceUpgradeError {
@@ -95,6 +101,27 @@ pub fn upgrade_configured_git_marketplaces(
95101 . collect :: < Vec < _ > > ( ) ;
96102 selected_marketplaces. sort_unstable ( ) ;
97103 selected_marketplaces. dedup ( ) ;
104+ let _upgrade_guard = match lock_configured_marketplace_upgrades ( codex_home) {
105+ Ok ( upgrade_guard) => upgrade_guard,
106+ Err ( message) => {
107+ errors. extend ( marketplaces. iter ( ) . map ( |marketplace| {
108+ ConfiguredMarketplaceUpgradeError {
109+ marketplace_name : marketplace. name . clone ( ) ,
110+ message : message. clone ( ) ,
111+ }
112+ } ) ) ;
113+ return ConfiguredMarketplaceUpgradeOutcome {
114+ selected_marketplaces,
115+ upgraded_roots : Vec :: new ( ) ,
116+ errors,
117+ } ;
118+ }
119+ } ;
120+ let stale_before = SystemTime :: now ( )
121+ . checked_sub ( MARKETPLACE_UPGRADE_STALE_TEMP_DIR_MAX_AGE )
122+ . unwrap_or ( SystemTime :: UNIX_EPOCH ) ;
123+ remove_stale_marketplace_upgrade_dirs ( & install_root. join ( ".staging" ) , stale_before) ;
124+
98125 let mut upgraded_roots = Vec :: new ( ) ;
99126 let policy = MarketplacePolicy :: from_requirements ( config_layer_stack. requirements ( ) ) ;
100127 for marketplace in marketplaces {
@@ -109,6 +136,17 @@ pub fn upgrade_configured_git_marketplaces(
109136 continue ;
110137 }
111138 } ;
139+ match read_configured_git_marketplace ( codex_home, & marketplace. name ) {
140+ Ok ( Some ( current) ) if current == marketplace => { }
141+ Ok ( _) => continue ,
142+ Err ( message) => {
143+ errors. push ( ConfiguredMarketplaceUpgradeError {
144+ marketplace_name : marketplace. name ,
145+ message,
146+ } ) ;
147+ continue ;
148+ }
149+ }
112150 match upgrade_configured_git_marketplace (
113151 codex_home,
114152 & install_root,
@@ -280,6 +318,82 @@ fn upgrade_configured_git_marketplace(
280318 . map ( Some )
281319 . map_err ( |err| format ! ( "upgraded marketplace path is not absolute: {err}" ) )
282320}
321+
322+ fn lock_configured_marketplace_upgrades ( codex_home : & Path ) -> Result < File , String > {
323+ let lock_path = codex_home. join ( MARKETPLACE_UPGRADE_LOCK_FILE ) ;
324+ let Some ( lock_parent) = lock_path. parent ( ) else {
325+ return Err ( format ! (
326+ "failed to determine configured marketplace upgrade lock parent for {}" ,
327+ lock_path. display( )
328+ ) ) ;
329+ } ;
330+ std:: fs:: create_dir_all ( lock_parent) . map_err ( |err| {
331+ format ! (
332+ "failed to create configured marketplace upgrade lock directory {}: {err}" ,
333+ lock_parent. display( )
334+ )
335+ } ) ?;
336+ let lock_file = File :: options ( )
337+ . write ( true )
338+ . create ( true )
339+ . truncate ( false )
340+ . open ( & lock_path)
341+ . map_err ( |err| {
342+ format ! (
343+ "failed to open configured marketplace upgrade lock {}: {err}" ,
344+ lock_path. display( )
345+ )
346+ } ) ?;
347+ lock_file. lock ( ) . map_err ( |err| {
348+ format ! (
349+ "failed to lock configured marketplace upgrades at {}: {err}" ,
350+ lock_path. display( )
351+ )
352+ } ) ?;
353+ Ok ( lock_file)
354+ }
355+
356+ fn remove_stale_marketplace_upgrade_dirs ( staging_parent : & Path , stale_before : SystemTime ) {
357+ let entries = match std:: fs:: read_dir ( staging_parent) {
358+ Ok ( entries) => entries,
359+ Err ( err) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => return ,
360+ Err ( err) => {
361+ warn ! (
362+ error = %err,
363+ path = %staging_parent. display( ) ,
364+ "failed to list configured marketplace staging directory"
365+ ) ;
366+ return ;
367+ }
368+ } ;
369+
370+ for entry in entries. flatten ( ) {
371+ let path = entry. path ( ) ;
372+ let is_upgrade_dir = path
373+ . file_name ( )
374+ . and_then ( |name| name. to_str ( ) )
375+ . is_some_and ( |name| name. starts_with ( "marketplace-upgrade-" ) ) ;
376+ let Ok ( file_type) = entry. file_type ( ) else {
377+ continue ;
378+ } ;
379+ if !is_upgrade_dir || !file_type. is_dir ( ) || file_type. is_symlink ( ) {
380+ continue ;
381+ }
382+ let Ok ( modified) = entry. metadata ( ) . and_then ( |metadata| metadata. modified ( ) ) else {
383+ continue ;
384+ } ;
385+ if modified > stale_before {
386+ continue ;
387+ }
388+ if let Err ( err) = std:: fs:: remove_dir_all ( & path) {
389+ warn ! (
390+ error = %err,
391+ path = %path. display( ) ,
392+ "failed to remove stale configured marketplace upgrade directory"
393+ ) ;
394+ }
395+ }
396+ }
283397fn ensure_configured_git_marketplace_unchanged (
284398 codex_home : & Path ,
285399 expected : & ConfiguredGitMarketplace ,
0 commit comments