MCPcopy
hub / github.com/docker/docker-agent / replaceActiveSession

Method replaceActiveSession

pkg/tui/tui.go:1383–1422  ·  view source on GitHub ↗

replaceActiveSession replaces the current (empty) tab's session with a loaded one in-place. If the loaded session's working directory differs from the runner's current one, a fresh runtime is spawned via the supervisor so that tools operate in the correct directory.

(ctx context.Context, sess *session.Session)

Source from the content-addressed store, hash-verified

1381// If the loaded session's working directory differs from the runner's current one,
1382// a fresh runtime is spawned via the supervisor so that tools operate in the correct directory.
1383func (m *appModel) replaceActiveSession(ctx context.Context, sess *session.Session) (tea.Model, tea.Cmd) {
1384 activeID := m.supervisor.ActiveID()
1385
1386 slog.DebugContext(ctx, "Replacing empty session in-place", "tab_id", activeID, "loaded_session", sess.ID)
1387
1388 // Cleanup old editor for the active session
1389 if ed, ok := m.editors[activeID]; ok {
1390 ed.Cleanup()
1391 }
1392
1393 // If the loaded session's working directory differs from the runner's,
1394 // we need a fresh runtime whose tools operate in the correct directory.
1395 runner := m.supervisor.GetRunner(activeID)
1396 sessWorkingDir := sess.WorkingDir
1397 if sessWorkingDir != "" && runner != nil && sessWorkingDir != runner.WorkingDir {
1398 newApp, _, spawnCleanup, err := m.supervisor.Spawner()(ctx, sessWorkingDir)
1399 if err == nil {
1400 slog.DebugContext(ctx, "Respawning runtime for working dir mismatch",
1401 "tab_id", activeID,
1402 "old_dir", runner.WorkingDir,
1403 "new_dir", sessWorkingDir)
1404 m.supervisor.ReplaceRunnerApp(ctx, activeID, newApp, sessWorkingDir, spawnCleanup)
1405 m.application = newApp
1406 } else {
1407 slog.WarnContext(ctx, "Failed to respawn runtime for working dir, using existing",
1408 "working_dir", sessWorkingDir, "error", err)
1409 }
1410 }
1411
1412 // Replace the session in the app and rebuild all per-session components.
1413 m.application.ReplaceSession(ctx, sess)
1414 m.initSessionComponents(activeID, m.application, sess)
1415
1416 if sess.Title != "" {
1417 m.supervisor.SetRunnerTitle(activeID, sess.Title)
1418 }
1419
1420 cmd := m.initAndFocusComponents()
1421 return m, cmd
1422}
1423
1424// handleClearSession resets the current tab by creating a fresh session
1425// in the same working directory.

Callers 3

initMethod · 0.95
handleLoadSessionMethod · 0.95
handleSwitchTabMethod · 0.95

Calls 9

initSessionComponentsMethod · 0.95
ActiveIDMethod · 0.80
GetRunnerMethod · 0.80
ReplaceRunnerAppMethod · 0.80
ReplaceSessionMethod · 0.80
SetRunnerTitleMethod · 0.80
CleanupMethod · 0.65
SpawnerMethod · 0.65

Tested by

no test coverage detected