| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "reasonix/internal/extension" |
| 7 | "reasonix/internal/lsp" |
| 8 | "reasonix/internal/plugin" |
| 9 | "reasonix/internal/sessiontemp" |
| 10 | "reasonix/internal/skill" |
| 11 | ) |
| 12 | |
| 13 | func closeSkillStores(stores ...*skill.Store) { |
| 14 | for _, store := range stores { |
| 15 | _ = store.Close() |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func closeUnownedSkills(owned *bool, cleanup func()) { |
| 20 | if !*owned { |
| 21 | cleanup() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // wireRuntimeScopeCleanup folds MCP host / LSP / session-temp inventory into |
| 26 | // the RuntimeSet when it already holds generation effects. Empty sets stay |
| 27 | // empty (stage-3a Len()==0). Returns the controller cleanup func. |
| 28 | func wireRuntimeScopeCleanup(runtimeSet *extension.RuntimeSet, cleanup func(), sharedHost *plugin.Host, pluginHost *plugin.Host, lspMgr *lsp.Manager, sessionTemp *sessiontemp.Manager, closeBrowser func()) func() { |
| 29 | if runtimeSet == nil || runtimeSet.Len() == 0 { |
| 30 | return func() { |
| 31 | if cleanup != nil { |
| 32 | cleanup() |
| 33 | } |
| 34 | if closeBrowser != nil { |
| 35 | closeBrowser() |
| 36 | } |
| 37 | _ = runtimeSet.Close() |
| 38 | } |
| 39 | } |
| 40 | // A browser this controller launched must die with it; one the host |
| 41 | // brought registers no closer here because the host still owns it. |
| 42 | if closeBrowser != nil { |
| 43 | _ = extension.TrackControllerCleanup(runtimeSet.Scope(), "browser-backend", func() error { |
| 44 | closeBrowser() |
| 45 | return nil |
| 46 | }) |
| 47 | } |
| 48 | if sharedHost == nil && pluginHost != nil { |
| 49 | host := pluginHost |
| 50 | _ = extension.TrackMCPClient(runtimeSet.Scope(), "plugin-host", extension.FuncCloser(host.Close)) |
| 51 | } |
| 52 | if lspMgr != nil { |
| 53 | mgr := lspMgr |
| 54 | _ = extension.TrackWatcher(runtimeSet.Scope(), "lsp-manager", func() error { |
| 55 | mgr.Close() |
| 56 | return nil |
| 57 | }) |
| 58 | } |
| 59 | if sessionTemp != nil { |
| 60 | _ = extension.TrackBackgroundJob(runtimeSet.Scope(), "session-temp", nil, func(context.Context) error { |
| 61 | return nil |
| 62 | }) |
| 63 | } |
| 64 | _ = extension.TrackControllerCleanup(runtimeSet.Scope(), "session-resources", func() error { |
| 65 | return nil |
| 66 | }) |
| 67 | return func() { _ = runtimeSet.Close() } |
| 68 | } |
| 69 |