@@ -10,7 +10,7 @@ local M = {
1010 actions = {},
1111}
1212
13- local session_subcommands = { ' new' , ' select' , ' navigate' , ' compact' , ' share' , ' unshare' , ' agents_init' , ' rename' }
13+ local session_subcommands = { ' new' , ' select' , ' navigate' , ' compact' , ' share' , ' unshare' , ' agents_init' , ' rename' , ' toggle_lock ' }
1414
1515--- @param message string
1616local function invalid_arguments (message )
@@ -94,8 +94,27 @@ function M.actions.open_input_new_session_with_title(title)
9494end
9595
9696--- @param parent_id ? string
97- function M .actions .select_session (parent_id )
98- session_runtime .select_session (parent_id )
97+ --- @param scope ? ' project' | ' global' defaults to global when session is locked , project otherwise
98+ function M .actions .select_session (parent_id , scope )
99+ if scope == nil then
100+ scope = session_runtime .is_session_locked () and ' global' or ' project'
101+ end
102+ session_runtime .select_session (parent_id , scope )
103+ end
104+
105+ --- @param value ? boolean if nil toggle , otherwise set to value
106+ function M .actions .toggle_session_lock (value )
107+ local new_value
108+ if value == nil then
109+ new_value = session_runtime .toggle_session_lock ()
110+ else
111+ new_value = session_runtime .set_session_lock (value and true or false )
112+ end
113+ vim .notify (
114+ ' Session lock ' .. (new_value and ' enabled (session preserved across cwd changes)' or ' disabled' ),
115+ vim .log .levels .INFO
116+ )
117+ return new_value
99118end
100119
101120local NAV_DIRECTIONS = { parent = true , child = true , sibling = true , forward = true , backward = true }
@@ -189,7 +208,7 @@ function M.actions.navigate_session_tree(direction, interaction, wrap, empty_pol
189208 return
190209 end
191210 if interaction == ' picker' then
192- return session_runtime .select_session (direction )
211+ return session_runtime .select_session (direction , ' project ' )
193212 end
194213 return session_runtime .switch_session (direction )
195214 end
@@ -207,15 +226,15 @@ function M.actions.navigate_session_tree(direction, interaction, wrap, empty_pol
207226 local target_id = dir .get_target (active )
208227 if not target_id then
209228 if direction == ' sibling' then
210- return session_runtime .select_session (nil )
229+ return session_runtime .select_session (nil , ' project ' )
211230 end
212231 if empty_policy == ' notify' then
213232 vim .notify (' No ' .. direction , vim .log .levels .INFO )
214233 end
215234 return
216235 end
217236 if interaction == ' picker' or not dir .allow_direct then
218- return session_runtime .select_session (target_id )
237+ return session_runtime .select_session (target_id , ' project ' )
219238 end
220239 return session_runtime .switch_session (target_id )
221240 end
@@ -575,11 +594,25 @@ local session_subcommand_actions = {
575594 agents_init = function ()
576595 return M .actions .initialize ()
577596 end ,
597+ toggle_lock = function (args )
598+ local raw = args [2 ]
599+ local value
600+ if raw == nil or raw == ' ' then
601+ value = nil
602+ elseif raw == ' true' or raw == ' on' or raw == ' 1' then
603+ value = true
604+ elseif raw == ' false' or raw == ' off' or raw == ' 0' then
605+ value = false
606+ else
607+ invalid_arguments (' Invalid toggle_lock argument: ' .. tostring (raw ))
608+ end
609+ return M .actions .toggle_session_lock (value )
610+ end ,
578611}
579612
580613M .command_defs = {
581614 session = {
582- desc = ' Manage sessions (new/select/navigate/compact/share/unshare/rename)' ,
615+ desc = ' Manage sessions (new/select/navigate/compact/share/unshare/rename/toggle_lock )' ,
583616 completions = session_subcommands ,
584617 nested_subcommand = { allow_empty = false },
585618 execute = function (args )
@@ -593,6 +626,12 @@ M.command_defs = {
593626 },
594627 -- action name aliases for keymap compatibility
595628 open_input_new_session = { desc = ' Open input (new session)' , execute = M .actions .open_input_new_session },
629+ toggle_session_lock = {
630+ desc = ' Toggle session lock (preserve active session across cwd changes)' ,
631+ execute = function (args )
632+ return M .actions .toggle_session_lock (args [1 ])
633+ end ,
634+ },
596635 select_session = {
597636 desc = ' Select session' ,
598637 execute = function ()
0 commit comments