Skip to content

Commit b087b6f

Browse files
committed
Re-read session id at reconnect ready-time to avoid stale reload
Re-initialization after a reconnect is async; capturing session_id up front could reload a session that was replaced during the window, clobbering newer state. Read it inside the when_ready callback so the current session is reloaded instead.
1 parent b4a548c commit b087b6f

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

lua/agentic/session_manager.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,8 +1392,6 @@ end
13921392
--- Note: the agent process is shared per provider, so this also resets other
13931393
--- tabs' sessions on the same provider; they re-establish on their next use.
13941394
function SessionManager:reconnect()
1395-
local session_id = self.session_id
1396-
13971395
-- Reset the local UI immediately. The respawn also drains the dead request
13981396
-- callbacks, but don't make the user wait for that to see the spinner stop.
13991397
self.is_generating = false
@@ -1404,6 +1402,11 @@ function SessionManager:reconnect()
14041402
self.agent:reconnect()
14051403

14061404
self.agent:when_ready(function()
1405+
-- Re-read the session at ready-time rather than capturing it earlier:
1406+
-- re-initialization is async, so the current session may have changed
1407+
-- meanwhile and a captured id could be stale. Reloading whatever is
1408+
-- current avoids clobbering a newer session.
1409+
local session_id = self.session_id
14071410
local caps = self.agent.agent_capabilities
14081411
if session_id and caps and caps.loadSession then
14091412
self:load_acp_session(session_id)

lua/agentic/session_manager.test.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,5 +1826,39 @@ describe("agentic.SessionManager", function()
18261826
assert.is_nil(calls.loaded_with)
18271827
assert.is_true(calls.new)
18281828
end)
1829+
1830+
it(
1831+
"reloads the current session, not a stale one, when ready fires late",
1832+
function()
1833+
local calls = { loaded_with = nil }
1834+
local ready_cb
1835+
local session = {
1836+
session_id = "sess-old",
1837+
is_generating = true,
1838+
status_animation = { stop = function() end },
1839+
agent = {
1840+
reconnect = function() end,
1841+
-- Defer the ready callback so the session can change
1842+
-- before re-initialization completes.
1843+
when_ready = function(_self, cb)
1844+
ready_cb = cb
1845+
end,
1846+
agent_capabilities = { loadSession = true },
1847+
},
1848+
reconnect = SessionManager.reconnect,
1849+
load_acp_session = function(_self, id)
1850+
calls.loaded_with = id
1851+
end,
1852+
new_session = function() end,
1853+
} --[[@as agentic.SessionManager]]
1854+
1855+
session:reconnect()
1856+
-- The session is swapped while the agent is still re-initializing.
1857+
session.session_id = "sess-new"
1858+
ready_cb()
1859+
1860+
assert.equal("sess-new", calls.loaded_with)
1861+
end
1862+
)
18291863
end)
18301864
end)

0 commit comments

Comments
 (0)