@@ -104,6 +104,31 @@ function isSameKeybindingRule(left: KeybindingRule, right: KeybindingRule): bool
104104 ) ;
105105}
106106
107+ // Default rules added to a command after startup sync had already persisted
108+ // that command. Backfill skips commands a config already has, so startup adds
109+ // each rule once per config, only next to the untouched earlier default
110+ // (`alongside`), and records its id. Customized commands and later removals
111+ // stay as the user set them.
112+ const LATE_DEFAULT_KEYBINDINGS : ReadonlyArray < {
113+ readonly id : string ;
114+ readonly alongside : KeybindingRule ;
115+ readonly rule : KeybindingRule ;
116+ } > = [
117+ {
118+ id : "composer.sendBackground:mod+enter" ,
119+ alongside : {
120+ key : "mod+alt+enter" ,
121+ command : "composer.sendBackground" ,
122+ when : "composerFocus && draftThreadRoute" ,
123+ } ,
124+ rule : {
125+ key : "mod+enter" ,
126+ command : "composer.sendBackground" ,
127+ when : "composerFocus && draftThreadRoute" ,
128+ } ,
129+ } ,
130+ ] ;
131+
107132function keybindingShortcutContext ( rule : KeybindingRule ) : string | null {
108133 const parsed = parseKeybindingShortcut ( rule . key ) ;
109134 if ( ! parsed ) return null ;
@@ -399,6 +424,32 @@ const make = Effect.gen(function* () {
399424 return { keybindings, issues } ;
400425 } ) ;
401426
427+ // One applied migration id per line, next to the config file.
428+ const appliedMigrationsPath = path . join (
429+ path . dirname ( keybindingsConfigPath ) ,
430+ "keybindings-migrations" ,
431+ ) ;
432+ const readAppliedMigrationIds = fs . readFileString ( appliedMigrationsPath ) . pipe (
433+ Effect . map ( ( contents ) => new Set ( contents . split ( "\n" ) . filter ( ( line ) => line . length > 0 ) ) ) ,
434+ Effect . orElseSucceed ( ( ) => new Set < string > ( ) ) ,
435+ ) ;
436+ const recordAppliedMigrations = ( ids : Iterable < string > ) =>
437+ writeFileStringAtomically ( {
438+ filePath : appliedMigrationsPath ,
439+ contents : `${ [ ...new Set ( ids ) ] . join ( "\n" ) } \n` ,
440+ } ) . pipe (
441+ Effect . provideService ( FileSystem . FileSystem , fs ) ,
442+ Effect . provideService ( Path . Path , path ) ,
443+ Effect . mapError (
444+ ( cause ) =>
445+ new KeybindingsConfigError ( {
446+ configPath : keybindingsConfigPath ,
447+ detail : "failed to record keybinding migrations" ,
448+ cause,
449+ } ) ,
450+ ) ,
451+ ) ;
452+
402453 const writeConfigAtomically = ( rules : readonly KeybindingRule [ ] ) => {
403454 return encodeKeybindingsConfigPrettyJson ( rules ) . pipe (
404455 Effect . map ( ( encoded ) => `${ encoded } \n` ) ,
@@ -450,9 +501,19 @@ const make = Effect.gen(function* () {
450501
451502 const syncDefaultKeybindingsOnStartup = upsertSemaphore . withPermits ( 1 ) (
452503 Effect . gen ( function * ( ) {
504+ const appliedMigrationIds = yield * readAppliedMigrationIds ;
505+ const pendingLateDefaults = LATE_DEFAULT_KEYBINDINGS . filter (
506+ ( late ) => ! appliedMigrationIds . has ( late . id ) ,
507+ ) ;
453508 const configExists = yield * readConfigExists ;
454509 if ( ! configExists ) {
455510 yield * writeConfigAtomically ( DEFAULT_KEYBINDINGS ) ;
511+ if ( pendingLateDefaults . length > 0 ) {
512+ yield * recordAppliedMigrations ( [
513+ ...appliedMigrationIds ,
514+ ...LATE_DEFAULT_KEYBINDINGS . map ( ( late ) => late . id ) ,
515+ ] ) ;
516+ }
456517 yield * Cache . invalidate ( resolvedConfigCache , resolvedConfigCacheKey ) ;
457518 return ;
458519 }
@@ -496,6 +557,16 @@ const make = Effect.gen(function* () {
496557 }
497558 missingDefaults . push ( defaultRule ) ;
498559 }
560+ // The loop above backfills whole missing commands. Late defaults join
561+ // an untouched earlier default, when their shortcut is still free.
562+ for ( const { alongside, rule } of pendingLateDefaults ) {
563+ if (
564+ customConfig . some ( ( entry ) => isSameKeybindingRule ( entry , alongside ) ) &&
565+ ! customConfig . some ( ( entry ) => hasSameShortcutContext ( entry , rule ) )
566+ ) {
567+ missingDefaults . push ( rule ) ;
568+ }
569+ }
499570 for ( const conflict of shortcutConflictWarnings ) {
500571 yield * Effect . logWarning ( "skipping default keybinding due to shortcut conflict" , {
501572 path : keybindingsConfigPath ,
@@ -506,21 +577,18 @@ const make = Effect.gen(function* () {
506577 reason : "shortcut context already used by existing rule" ,
507578 } ) ;
508579 }
509- if ( missingDefaults . length === 0 ) {
510- yield * Cache . invalidate ( resolvedConfigCache , resolvedConfigCacheKey ) ;
511- return ;
512- }
513-
514- const matchingDefaults = Array . filterMap ( DEFAULT_KEYBINDINGS , ( defaultRule ) =>
515- customConfig . some ( ( entry ) => isSameKeybindingRule ( entry , defaultRule ) )
516- ? Result . succeed ( defaultRule . command )
517- : Result . failVoid ,
518- ) ;
519- if ( matchingDefaults . length > 0 ) {
520- yield * Effect . logWarning ( "default keybinding rule already defined in user config" , {
521- path : keybindingsConfigPath ,
522- commands : matchingDefaults ,
523- } ) ;
580+ if ( missingDefaults . length > 0 ) {
581+ const matchingDefaults = Array . filterMap ( DEFAULT_KEYBINDINGS , ( defaultRule ) =>
582+ customConfig . some ( ( entry ) => isSameKeybindingRule ( entry , defaultRule ) )
583+ ? Result . succeed ( defaultRule . command )
584+ : Result . failVoid ,
585+ ) ;
586+ if ( matchingDefaults . length > 0 ) {
587+ yield * Effect . logWarning ( "default keybinding rule already defined in user config" , {
588+ path : keybindingsConfigPath ,
589+ commands : matchingDefaults ,
590+ } ) ;
591+ }
524592 }
525593
526594 // Startup backfill must never evict persisted user rules: append only
@@ -535,12 +603,19 @@ const make = Effect.gen(function* () {
535603 commands : skippedDefaults . map ( ( rule ) => rule . command ) ,
536604 } ) ;
537605 }
538- if ( defaultsToAppend . length === 0 ) {
539- yield * Cache . invalidate ( resolvedConfigCache , resolvedConfigCacheKey ) ;
540- return ;
606+ if ( defaultsToAppend . length > 0 ) {
607+ yield * writeConfigAtomically ( [ ...customConfig , ...defaultsToAppend ] ) ;
608+ }
609+ // A late default skipped at max entries stays pending for a later start.
610+ const settledLateDefaults = pendingLateDefaults . filter (
611+ ( late ) => ! skippedDefaults . includes ( late . rule ) ,
612+ ) ;
613+ if ( settledLateDefaults . length > 0 ) {
614+ yield * recordAppliedMigrations ( [
615+ ...appliedMigrationIds ,
616+ ...settledLateDefaults . map ( ( late ) => late . id ) ,
617+ ] ) ;
541618 }
542-
543- yield * writeConfigAtomically ( [ ...customConfig , ...defaultsToAppend ] ) ;
544619 yield * Cache . invalidate ( resolvedConfigCache , resolvedConfigCacheKey ) ;
545620 } ) ,
546621 ) ;
0 commit comments