initScheduler wires the scheduler into ChatCLI. Errors are logged, not fatal — a scheduler outage must not break the chat loop.
(parent context.Context)
| 35 | // initScheduler wires the scheduler into ChatCLI. Errors are logged, |
| 36 | // not fatal — a scheduler outage must not break the chat loop. |
| 37 | func (cli *ChatCLI) initScheduler(parent context.Context) { |
| 38 | if cli.logger == nil { |
| 39 | return |
| 40 | } |
| 41 | cfg := scheduler.LoadConfigFromEnv() |
| 42 | if !cfg.Enabled { |
| 43 | cli.logger.Info("scheduler: disabled via CHATCLI_SCHEDULER_ENABLED=false") |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | // Daemon auto-detect. |
| 48 | socket := scheduler.DefaultSocketPath(cfg) |
| 49 | if cfg.DaemonAutoConnect { |
| 50 | if err := scheduler.CheckDaemon(socket); err == nil { |
| 51 | client, derr := scheduler.Dial(socket) |
| 52 | if derr == nil { |
| 53 | cli.schedulerRemote = client |
| 54 | cli.schedulerSocket = socket |
| 55 | // Even in daemon mode, wire the plugin adapter so the |
| 56 | // ReAct loop can still invoke @scheduler. |
| 57 | pluginsSetAdapter(&schedulerPluginAdapter{cli: cli}) |
| 58 | cli.logger.Info("scheduler: connected to daemon", |
| 59 | zap.String("socket", socket)) |
| 60 | return |
| 61 | } |
| 62 | cli.logger.Warn("scheduler: daemon reachable but dial failed", |
| 63 | zap.String("socket", socket), zap.Error(derr)) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | bridge := newSchedulerBridge(cli) |
| 68 | deps := scheduler.SchedulerDeps{ |
| 69 | Hooks: cli.hookManager, |
| 70 | } |
| 71 | s, err := scheduler.New(cfg, bridge, deps, cli.logger) |
| 72 | if err != nil { |
| 73 | cli.logger.Warn("scheduler: New failed — feature disabled for session", zap.Error(err)) |
| 74 | return |
| 75 | } |
| 76 | builtins.RegisterAll(s) |
| 77 | |
| 78 | // The scheduler outlives any single request; its lifecycle is governed |
| 79 | // by schedulerCancel (fired in cleanup). Detach request cancellation |
| 80 | // while inheriting context values. |
| 81 | ctx, cancel := context.WithCancel(context.WithoutCancel(parent)) |
| 82 | cli.schedulerCancel = cancel |
| 83 | if err := s.Start(ctx); err != nil { |
| 84 | cli.logger.Warn("scheduler: Start failed — feature disabled for session", zap.Error(err)) |
| 85 | cancel() |
| 86 | return |
| 87 | } |
| 88 | cli.scheduler = s |
| 89 | cli.schedulerBridge = bridge |
| 90 | // Wire the @scheduler builtin plugin into the ReAct tool registry. |
| 91 | pluginsAdapter := &schedulerPluginAdapter{cli: cli} |
| 92 | pluginsSetAdapter(pluginsAdapter) |
| 93 | |
| 94 | cli.logger.Info("scheduler: running in-process", |
no test coverage detected