wave-agent-sdk 0.0.8 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/dist/agent.d.ts +92 -23
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +351 -137
  4. package/dist/index.d.ts +3 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -0
  7. package/dist/managers/aiManager.d.ts +14 -36
  8. package/dist/managers/aiManager.d.ts.map +1 -1
  9. package/dist/managers/aiManager.js +74 -77
  10. package/dist/managers/backgroundBashManager.d.ts.map +1 -1
  11. package/dist/managers/backgroundBashManager.js +4 -3
  12. package/dist/managers/hookManager.d.ts +3 -8
  13. package/dist/managers/hookManager.d.ts.map +1 -1
  14. package/dist/managers/hookManager.js +39 -29
  15. package/dist/managers/liveConfigManager.d.ts +55 -18
  16. package/dist/managers/liveConfigManager.d.ts.map +1 -1
  17. package/dist/managers/liveConfigManager.js +372 -90
  18. package/dist/managers/lspManager.d.ts +43 -0
  19. package/dist/managers/lspManager.d.ts.map +1 -0
  20. package/dist/managers/lspManager.js +326 -0
  21. package/dist/managers/messageManager.d.ts +8 -16
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +52 -74
  24. package/dist/managers/permissionManager.d.ts +75 -0
  25. package/dist/managers/permissionManager.d.ts.map +1 -0
  26. package/dist/managers/permissionManager.js +368 -0
  27. package/dist/managers/skillManager.d.ts +1 -0
  28. package/dist/managers/skillManager.d.ts.map +1 -1
  29. package/dist/managers/skillManager.js +2 -1
  30. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  31. package/dist/managers/slashCommandManager.js +0 -1
  32. package/dist/managers/subagentManager.d.ts +8 -23
  33. package/dist/managers/subagentManager.d.ts.map +1 -1
  34. package/dist/managers/subagentManager.js +97 -117
  35. package/dist/managers/toolManager.d.ts +38 -1
  36. package/dist/managers/toolManager.d.ts.map +1 -1
  37. package/dist/managers/toolManager.js +66 -2
  38. package/dist/services/aiService.d.ts +3 -1
  39. package/dist/services/aiService.d.ts.map +1 -1
  40. package/dist/services/aiService.js +123 -30
  41. package/dist/services/configurationService.d.ts +116 -0
  42. package/dist/services/configurationService.d.ts.map +1 -0
  43. package/dist/services/configurationService.js +585 -0
  44. package/dist/services/fileWatcher.d.ts.map +1 -1
  45. package/dist/services/fileWatcher.js +5 -6
  46. package/dist/services/hook.d.ts +7 -124
  47. package/dist/services/hook.d.ts.map +1 -1
  48. package/dist/services/hook.js +46 -458
  49. package/dist/services/jsonlHandler.d.ts +24 -15
  50. package/dist/services/jsonlHandler.d.ts.map +1 -1
  51. package/dist/services/jsonlHandler.js +67 -88
  52. package/dist/services/memory.d.ts +0 -9
  53. package/dist/services/memory.d.ts.map +1 -1
  54. package/dist/services/memory.js +2 -49
  55. package/dist/services/session.d.ts +82 -33
  56. package/dist/services/session.d.ts.map +1 -1
  57. package/dist/services/session.js +275 -181
  58. package/dist/tools/bashTool.d.ts.map +1 -1
  59. package/dist/tools/bashTool.js +109 -11
  60. package/dist/tools/deleteFileTool.d.ts.map +1 -1
  61. package/dist/tools/deleteFileTool.js +25 -0
  62. package/dist/tools/editTool.d.ts.map +1 -1
  63. package/dist/tools/editTool.js +30 -6
  64. package/dist/tools/lspTool.d.ts +6 -0
  65. package/dist/tools/lspTool.d.ts.map +1 -0
  66. package/dist/tools/lspTool.js +589 -0
  67. package/dist/tools/multiEditTool.d.ts.map +1 -1
  68. package/dist/tools/multiEditTool.js +26 -7
  69. package/dist/tools/readTool.d.ts.map +1 -1
  70. package/dist/tools/readTool.js +111 -2
  71. package/dist/tools/skillTool.js +2 -2
  72. package/dist/tools/todoWriteTool.d.ts.map +1 -1
  73. package/dist/tools/todoWriteTool.js +23 -0
  74. package/dist/tools/types.d.ts +11 -8
  75. package/dist/tools/types.d.ts.map +1 -1
  76. package/dist/tools/writeTool.d.ts.map +1 -1
  77. package/dist/tools/writeTool.js +25 -9
  78. package/dist/types/commands.d.ts +0 -1
  79. package/dist/types/commands.d.ts.map +1 -1
  80. package/dist/types/config.d.ts +4 -0
  81. package/dist/types/config.d.ts.map +1 -1
  82. package/dist/types/configuration.d.ts +69 -0
  83. package/dist/types/configuration.d.ts.map +1 -0
  84. package/dist/types/configuration.js +8 -0
  85. package/dist/types/core.d.ts +10 -0
  86. package/dist/types/core.d.ts.map +1 -1
  87. package/dist/types/environment.d.ts +41 -0
  88. package/dist/types/environment.d.ts.map +1 -1
  89. package/dist/types/fileSearch.d.ts +5 -0
  90. package/dist/types/fileSearch.d.ts.map +1 -0
  91. package/dist/types/fileSearch.js +1 -0
  92. package/dist/types/hooks.d.ts +11 -2
  93. package/dist/types/hooks.d.ts.map +1 -1
  94. package/dist/types/hooks.js +1 -7
  95. package/dist/types/index.d.ts +5 -0
  96. package/dist/types/index.d.ts.map +1 -1
  97. package/dist/types/index.js +5 -0
  98. package/dist/types/lsp.d.ts +90 -0
  99. package/dist/types/lsp.d.ts.map +1 -0
  100. package/dist/types/lsp.js +4 -0
  101. package/dist/types/messaging.d.ts +6 -11
  102. package/dist/types/messaging.d.ts.map +1 -1
  103. package/dist/types/permissions.d.ts +39 -0
  104. package/dist/types/permissions.d.ts.map +1 -0
  105. package/dist/types/permissions.js +12 -0
  106. package/dist/types/session.d.ts +1 -6
  107. package/dist/types/session.d.ts.map +1 -1
  108. package/dist/types/skills.d.ts +1 -0
  109. package/dist/types/skills.d.ts.map +1 -1
  110. package/dist/types/tools.d.ts +35 -0
  111. package/dist/types/tools.d.ts.map +1 -0
  112. package/dist/types/tools.js +4 -0
  113. package/dist/utils/abortUtils.d.ts +34 -0
  114. package/dist/utils/abortUtils.d.ts.map +1 -0
  115. package/dist/utils/abortUtils.js +92 -0
  116. package/dist/utils/bashHistory.d.ts +4 -0
  117. package/dist/utils/bashHistory.d.ts.map +1 -1
  118. package/dist/utils/bashHistory.js +21 -4
  119. package/dist/utils/bashParser.d.ts +24 -0
  120. package/dist/utils/bashParser.d.ts.map +1 -0
  121. package/dist/utils/bashParser.js +413 -0
  122. package/dist/utils/builtinSubagents.d.ts +7 -0
  123. package/dist/utils/builtinSubagents.d.ts.map +1 -0
  124. package/dist/utils/builtinSubagents.js +65 -0
  125. package/dist/utils/cacheControlUtils.d.ts +8 -33
  126. package/dist/utils/cacheControlUtils.d.ts.map +1 -1
  127. package/dist/utils/cacheControlUtils.js +83 -126
  128. package/dist/utils/constants.d.ts +0 -12
  129. package/dist/utils/constants.d.ts.map +1 -1
  130. package/dist/utils/constants.js +1 -13
  131. package/dist/utils/convertMessagesForAPI.d.ts +2 -1
  132. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
  133. package/dist/utils/convertMessagesForAPI.js +33 -14
  134. package/dist/utils/fileSearch.d.ts +14 -0
  135. package/dist/utils/fileSearch.d.ts.map +1 -0
  136. package/dist/utils/fileSearch.js +88 -0
  137. package/dist/utils/fileUtils.d.ts +14 -2
  138. package/dist/utils/fileUtils.d.ts.map +1 -1
  139. package/dist/utils/fileUtils.js +101 -17
  140. package/dist/utils/globalLogger.d.ts +0 -14
  141. package/dist/utils/globalLogger.d.ts.map +1 -1
  142. package/dist/utils/globalLogger.js +0 -16
  143. package/dist/utils/markdownParser.d.ts.map +1 -1
  144. package/dist/utils/markdownParser.js +1 -17
  145. package/dist/utils/messageOperations.d.ts +1 -11
  146. package/dist/utils/messageOperations.d.ts.map +1 -1
  147. package/dist/utils/messageOperations.js +7 -24
  148. package/dist/utils/pathEncoder.d.ts +4 -0
  149. package/dist/utils/pathEncoder.d.ts.map +1 -1
  150. package/dist/utils/pathEncoder.js +16 -9
  151. package/dist/utils/pathSafety.d.ts +10 -0
  152. package/dist/utils/pathSafety.d.ts.map +1 -0
  153. package/dist/utils/pathSafety.js +23 -0
  154. package/dist/utils/subagentParser.d.ts +2 -2
  155. package/dist/utils/subagentParser.d.ts.map +1 -1
  156. package/dist/utils/subagentParser.js +10 -7
  157. package/package.json +9 -9
  158. package/src/agent.ts +475 -216
  159. package/src/index.ts +3 -0
  160. package/src/managers/aiManager.ts +107 -111
  161. package/src/managers/backgroundBashManager.ts +4 -3
  162. package/src/managers/hookManager.ts +44 -39
  163. package/src/managers/liveConfigManager.ts +524 -138
  164. package/src/managers/lspManager.ts +434 -0
  165. package/src/managers/messageManager.ts +73 -103
  166. package/src/managers/permissionManager.ts +480 -0
  167. package/src/managers/skillManager.ts +3 -1
  168. package/src/managers/slashCommandManager.ts +1 -2
  169. package/src/managers/subagentManager.ts +116 -159
  170. package/src/managers/toolManager.ts +95 -3
  171. package/src/services/aiService.ts +207 -26
  172. package/src/services/configurationService.ts +762 -0
  173. package/src/services/fileWatcher.ts +5 -6
  174. package/src/services/hook.ts +50 -631
  175. package/src/services/jsonlHandler.ts +84 -100
  176. package/src/services/memory.ts +2 -59
  177. package/src/services/session.ts +338 -213
  178. package/src/tools/bashTool.ts +126 -13
  179. package/src/tools/deleteFileTool.ts +36 -0
  180. package/src/tools/editTool.ts +41 -7
  181. package/src/tools/lspTool.ts +760 -0
  182. package/src/tools/multiEditTool.ts +37 -8
  183. package/src/tools/readTool.ts +125 -2
  184. package/src/tools/skillTool.ts +2 -2
  185. package/src/tools/todoWriteTool.ts +33 -1
  186. package/src/tools/types.ts +15 -9
  187. package/src/tools/writeTool.ts +36 -10
  188. package/src/types/commands.ts +0 -1
  189. package/src/types/config.ts +5 -0
  190. package/src/types/configuration.ts +73 -0
  191. package/src/types/core.ts +11 -0
  192. package/src/types/environment.ts +44 -0
  193. package/src/types/fileSearch.ts +4 -0
  194. package/src/types/hooks.ts +14 -11
  195. package/src/types/index.ts +5 -0
  196. package/src/types/lsp.ts +96 -0
  197. package/src/types/messaging.ts +8 -13
  198. package/src/types/permissions.ts +52 -0
  199. package/src/types/session.ts +3 -8
  200. package/src/types/skills.ts +1 -0
  201. package/src/types/tools.ts +38 -0
  202. package/src/utils/abortUtils.ts +118 -0
  203. package/src/utils/bashHistory.ts +28 -4
  204. package/src/utils/bashParser.ts +444 -0
  205. package/src/utils/builtinSubagents.ts +71 -0
  206. package/src/utils/cacheControlUtils.ts +106 -171
  207. package/src/utils/constants.ts +1 -16
  208. package/src/utils/convertMessagesForAPI.ts +38 -14
  209. package/src/utils/fileSearch.ts +107 -0
  210. package/src/utils/fileUtils.ts +114 -19
  211. package/src/utils/globalLogger.ts +0 -17
  212. package/src/utils/markdownParser.ts +1 -19
  213. package/src/utils/messageOperations.ts +7 -35
  214. package/src/utils/pathEncoder.ts +24 -9
  215. package/src/utils/pathSafety.ts +26 -0
  216. package/src/utils/subagentParser.ts +11 -8
  217. package/dist/constants/events.d.ts +0 -28
  218. package/dist/constants/events.d.ts.map +0 -1
  219. package/dist/constants/events.js +0 -27
  220. package/dist/services/configurationWatcher.d.ts +0 -120
  221. package/dist/services/configurationWatcher.d.ts.map +0 -1
  222. package/dist/services/configurationWatcher.js +0 -439
  223. package/dist/services/memoryStore.d.ts +0 -81
  224. package/dist/services/memoryStore.d.ts.map +0 -1
  225. package/dist/services/memoryStore.js +0 -200
  226. package/dist/types/memoryStore.d.ts +0 -82
  227. package/dist/types/memoryStore.d.ts.map +0 -1
  228. package/dist/types/memoryStore.js +0 -7
  229. package/dist/utils/configResolver.d.ts +0 -65
  230. package/dist/utils/configResolver.d.ts.map +0 -1
  231. package/dist/utils/configResolver.js +0 -210
  232. package/src/constants/events.ts +0 -38
  233. package/src/services/configurationWatcher.ts +0 -622
  234. package/src/services/memoryStore.ts +0 -279
  235. package/src/types/memoryStore.ts +0 -94
  236. package/src/utils/configResolver.ts +0 -302
@@ -3,85 +3,168 @@
3
3
  *
4
4
  * Orchestrates live configuration reload functionality including:
5
5
  * - Hook configuration watching and reloading
6
- * - Memory store management for AGENTS.md files
6
+ * - Configuration file watching for settings.json files
7
7
  * - Coordination between file watchers and configuration updates
8
8
  */
9
9
 
10
+ import { existsSync } from "fs";
10
11
  import type { Logger } from "../types/index.js";
11
12
  import {
12
- ConfigurationWatcher,
13
- type ConfigurationChangeEvent,
14
- } from "../services/configurationWatcher.js";
15
-
16
- import { type FileWatchEvent } from "../services/fileWatcher.js";
17
- import { configResolver } from "../utils/configResolver.js";
18
- import { join } from "path";
13
+ FileWatcherService,
14
+ type FileWatchEvent,
15
+ } from "../services/fileWatcher.js";
16
+ import type { HookManager } from "./hookManager.js";
17
+ import type { PermissionManager } from "./permissionManager.js";
19
18
  import {
20
- getUserConfigPaths,
21
19
  getProjectConfigPaths,
22
- } from "../utils/configPaths.js";
23
- import { CONFIGURATION_EVENTS } from "../constants/events.js";
20
+ getUserConfigPaths,
21
+ } from "@/utils/configPaths.js";
22
+ import type { WaveConfiguration, ValidationResult } from "../types/hooks.js";
23
+ import { isValidHookEvent, isValidHookEventConfig } from "../types/hooks.js";
24
+ import { ConfigurationService } from "../services/configurationService.js";
25
+ import { ensureGlobalGitIgnore } from "../utils/fileUtils.js";
26
+
27
+ import type { ConfigurationLoadResult } from "../types/configuration.js";
24
28
 
25
29
  export interface LiveConfigManagerOptions {
26
30
  workdir: string;
27
31
  logger?: Logger;
28
- onConfigurationChanged?: () => void; // Callback for when configuration changes
29
- onMemoryStoreFileChanged?: (
30
- filePath: string,
31
- changeType: "add" | "change" | "unlink",
32
- ) => Promise<void>; // Callback for memory store file changes
32
+ hookManager?: HookManager;
33
+ permissionManager?: PermissionManager;
34
+ configurationService?: ConfigurationService;
33
35
  }
34
36
 
35
37
  export class LiveConfigManager {
36
38
  private readonly workdir: string;
37
39
  private readonly logger?: Logger;
38
- private readonly onConfigurationChanged?: () => void;
39
- private readonly onMemoryStoreFileChanged?: (
40
- filePath: string,
41
- changeType: "add" | "change" | "unlink",
42
- ) => Promise<void>;
43
- private configurationWatcher?: ConfigurationWatcher;
40
+ private readonly hookManager?: HookManager;
41
+ private readonly permissionManager?: PermissionManager;
44
42
  private isInitialized: boolean = false;
43
+ private readonly configurationService: ConfigurationService;
44
+
45
+ // Configuration state
46
+ private currentConfiguration: WaveConfiguration | null = null;
47
+ private lastValidConfiguration: WaveConfiguration | null = null;
48
+
49
+ // File watching state
50
+ private fileWatcher: FileWatcherService;
51
+ private userConfigPaths?: string[];
52
+ private projectConfigPaths?: string[];
53
+ private isWatching: boolean = false;
54
+ private reloadInProgress: boolean = false;
45
55
 
46
56
  constructor(options: LiveConfigManagerOptions) {
47
57
  this.workdir = options.workdir;
48
58
  this.logger = options.logger;
49
- this.onConfigurationChanged = options.onConfigurationChanged;
50
- this.onMemoryStoreFileChanged = options.onMemoryStoreFileChanged;
59
+ this.hookManager = options.hookManager;
60
+ this.permissionManager = options.permissionManager;
61
+ this.configurationService =
62
+ options.configurationService || new ConfigurationService();
63
+ this.fileWatcher = new FileWatcherService(this.logger);
64
+ this.setupFileWatcherEvents();
51
65
  }
52
66
 
53
67
  /**
54
- * Initialize live configuration management
68
+ * Initialize configuration watching
69
+ * Maps to FR-004: System MUST watch settings.json files
70
+ * Supports watching multiple file paths (e.g., settings.local.json and settings.json)
71
+ */
72
+ private async initializeWatching(
73
+ userPaths: string[],
74
+ projectPaths?: string[],
75
+ ): Promise<void> {
76
+ try {
77
+ this.logger?.info("Live Config: Initializing configuration watching...");
78
+
79
+ this.userConfigPaths = userPaths;
80
+ this.projectConfigPaths = projectPaths;
81
+
82
+ // Load initial configuration
83
+ await this.reloadConfiguration();
84
+
85
+ // Start watching user configs that exist
86
+ for (const userPath of userPaths) {
87
+ if (existsSync(userPath)) {
88
+ this.logger?.debug(
89
+ `Live Config: Starting to watch user config: ${userPath}`,
90
+ );
91
+ await this.fileWatcher.watchFile(userPath, (event) =>
92
+ this.handleFileChange(event, "user"),
93
+ );
94
+ } else {
95
+ this.logger?.debug(
96
+ `Live Config: User config file does not exist: ${userPath}`,
97
+ );
98
+ }
99
+ }
100
+
101
+ // Start watching project configs that exist
102
+ if (projectPaths) {
103
+ for (const projectPath of projectPaths) {
104
+ if (existsSync(projectPath)) {
105
+ if (projectPath.endsWith("settings.local.json")) {
106
+ await ensureGlobalGitIgnore("**/.wave/settings.local.json");
107
+ }
108
+ this.logger?.debug(
109
+ `Live Config: Starting to watch project config: ${projectPath}`,
110
+ );
111
+ await this.fileWatcher.watchFile(projectPath, (event) =>
112
+ this.handleFileChange(event, "project"),
113
+ );
114
+ } else {
115
+ this.logger?.debug(
116
+ `Live Config: Project config file does not exist: ${projectPath}`,
117
+ );
118
+ }
119
+ }
120
+ }
121
+
122
+ this.isWatching = true;
123
+ this.logger?.info(
124
+ "Live Config: Configuration watching initialized successfully",
125
+ );
126
+ } catch (error) {
127
+ const errorMessage = `Failed to initialize configuration watching: ${(error as Error).message}`;
128
+ this.logger?.error(`Live Config: ${errorMessage}`);
129
+ throw new Error(errorMessage);
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Get current configuration
135
+ */
136
+ getCurrentConfiguration(): WaveConfiguration | null {
137
+ return this.currentConfiguration ? { ...this.currentConfiguration } : null;
138
+ }
139
+
140
+ /**
141
+ * Initialize configuration management with file watching
55
142
  */
56
143
  async initialize(): Promise<void> {
57
144
  if (this.isInitialized) {
58
- this.logger?.debug("[LiveConfigManager] Already initialized");
145
+ this.logger?.debug("Already initialized");
59
146
  return;
60
147
  }
61
148
 
62
149
  try {
63
- // Initialize configuration watcher for hook settings
64
- await this.initializeConfigurationWatcher();
150
+ // Get configuration file paths
151
+ const { userPaths, projectPaths } = this.getConfigurationPaths();
65
152
 
66
- // Initialize memory store watching for AGENTS.md if callback is available
67
- if (this.onMemoryStoreFileChanged) {
68
- await this.initializeMemoryStoreWatching();
69
- }
153
+ // Initialize configuration watching
154
+ await this.initializeWatching(userPaths, projectPaths);
70
155
 
71
156
  this.isInitialized = true;
72
157
  this.logger?.info(
73
- "Live Config: Live configuration management initialized successfully",
158
+ "Live configuration management initialized with file watching",
74
159
  );
75
160
  } catch (error) {
76
- this.logger?.error(
77
- `Live Config: Failed to initialize: ${(error as Error).message}`,
78
- );
161
+ this.logger?.error(`Failed to initialize: ${(error as Error).message}`);
79
162
  throw error;
80
163
  }
81
164
  }
82
165
 
83
166
  /**
84
- * Shutdown live configuration management
167
+ * Shutdown configuration management and cleanup resources
85
168
  */
86
169
  async shutdown(): Promise<void> {
87
170
  if (!this.isInitialized) {
@@ -89,148 +172,451 @@ export class LiveConfigManager {
89
172
  }
90
173
 
91
174
  try {
92
- if (this.configurationWatcher) {
93
- await this.configurationWatcher.shutdown();
94
- this.configurationWatcher = undefined;
95
- }
175
+ this.logger?.info("Live Config: Shutting down configuration manager...");
176
+
177
+ this.isWatching = false;
178
+
179
+ // Cleanup file watcher
180
+ await this.fileWatcher.cleanup();
181
+
182
+ // Clean up state
183
+ this.currentConfiguration = null;
184
+ this.lastValidConfiguration = null;
96
185
 
97
186
  this.isInitialized = false;
98
- this.logger?.info(
99
- "Live Config: Live configuration management shutdown completed",
100
- );
187
+ this.logger?.info("Live configuration management shutdown completed");
101
188
  } catch (error) {
102
- this.logger?.error(
103
- `Live Config: Error during shutdown: ${(error as Error).message}`,
104
- );
189
+ this.logger?.error(`Error during shutdown: ${(error as Error).message}`);
105
190
  throw error;
106
191
  }
107
192
  }
108
193
 
109
194
  /**
110
- * Initialize configuration watcher for hook settings
195
+ * Reload configuration from files
196
+ * Maps to FR-008: Continue with previous valid configuration on errors
111
197
  */
112
- private async initializeConfigurationWatcher(): Promise<void> {
113
- this.configurationWatcher = new ConfigurationWatcher(
114
- this.workdir,
115
- this.logger,
116
- );
198
+ private async reloadConfiguration(): Promise<WaveConfiguration> {
199
+ if (this.reloadInProgress) {
200
+ this.logger?.debug("Live Config: Reload already in progress, skipping");
201
+ return this.currentConfiguration || {};
202
+ }
117
203
 
118
- // Set up configuration change handler using EventEmitter pattern
119
- this.configurationWatcher.on(
120
- CONFIGURATION_EVENTS.CONFIGURATION_CHANGE,
121
- (event: ConfigurationChangeEvent) => {
122
- this.handleConfigurationChange(event);
123
- },
124
- );
204
+ this.reloadInProgress = true;
125
205
 
126
- // Initialize watching for user and project settings
127
- const { userPaths, projectPaths } = this.getConfigurationPaths();
128
- await this.configurationWatcher.initializeWatching(userPaths, projectPaths);
129
- this.logger?.info("Live Config: Configuration watching initialized");
130
- }
206
+ try {
207
+ this.logger?.debug("Live Config: Reloading configuration from files...");
131
208
 
132
- /**
133
- * Initialize memory store watching for AGENTS.md files
134
- */
135
- private async initializeMemoryStoreWatching(): Promise<void> {
136
- if (!this.onMemoryStoreFileChanged || !this.configurationWatcher) {
137
- this.logger?.debug(
138
- "Live Config: Memory store callback or configuration watcher not available, skipping AGENTS.md watching",
139
- );
140
- return;
141
- }
209
+ // Load merged configuration using ConfigurationService
210
+ const loadResult: ConfigurationLoadResult =
211
+ await this.configurationService.loadMergedConfiguration(this.workdir);
212
+ const newConfig = loadResult.configuration;
142
213
 
143
- try {
144
- const agentsFilePath = join(this.workdir, "AGENTS.md");
145
-
146
- // Add AGENTS.md to file watcher
147
- await this.configurationWatcher.watchAdditionalFile(
148
- agentsFilePath,
149
- async (event: FileWatchEvent) => {
150
- await this.handleMemoryStoreFileChange(event);
151
- },
152
- );
214
+ // Check for errors during loading
215
+ if (!loadResult.success) {
216
+ const errorMessage =
217
+ loadResult.error || "Configuration loading failed with unknown error";
218
+ this.logger?.error(
219
+ `Live Config: Configuration loading failed: ${errorMessage}`,
220
+ );
153
221
 
154
- this.logger?.info("Live Config: AGENTS.md file watching initialized");
155
- } catch (error) {
156
- this.logger?.warn(
157
- `Live Config: Failed to initialize AGENTS.md watching: ${(error as Error).message}`,
158
- );
159
- // Don't throw - memory optimization is not critical for core functionality
160
- }
161
- }
222
+ // Log warnings if any
223
+ if (loadResult.warnings && loadResult.warnings.length > 0) {
224
+ this.logger?.warn(
225
+ `Live Config: Configuration warnings: ${loadResult.warnings.join("; ")}`,
226
+ );
227
+ }
162
228
 
163
- /**
164
- * Handle configuration change events
165
- */
166
- private handleConfigurationChange(event: ConfigurationChangeEvent): void {
167
- this.logger?.info(
168
- `Live Config: Configuration change detected: ${event.type} at ${event.path}`,
169
- );
229
+ // Use fallback configuration if available
230
+ if (this.lastValidConfiguration) {
231
+ this.logger?.info(
232
+ "Live Config: Using previous valid configuration due to loading errors",
233
+ );
234
+ this.currentConfiguration = this.lastValidConfiguration;
170
235
 
171
- // Invalidate and refresh configuration cache for live environment variable updates
172
- configResolver.invalidateCache(this.workdir);
173
- configResolver.refreshCache(this.workdir);
236
+ // Apply environment variables to configuration service if configured
237
+ if (this.lastValidConfiguration.env) {
238
+ this.configurationService.setEnvironmentVars(
239
+ this.lastValidConfiguration.env,
240
+ );
241
+ }
174
242
 
175
- // Trigger Agent configuration update callback if provided
176
- if (this.onConfigurationChanged) {
177
- try {
178
- this.logger?.info("Live Config: Triggering Agent configuration update");
179
- this.onConfigurationChanged();
180
- } catch (error) {
181
- this.logger?.error(
182
- `Live Config: Error in configuration change callback: ${(error as Error).message}`,
243
+ // Update hook manager if available
244
+ if (this.hookManager) {
245
+ this.hookManager.loadConfigurationFromWaveConfig(
246
+ this.lastValidConfiguration,
247
+ );
248
+ }
249
+
250
+ return this.currentConfiguration;
251
+ } else {
252
+ this.logger?.warn(
253
+ "Live Config: No previous valid configuration available, using empty config",
254
+ );
255
+ this.currentConfiguration = {};
256
+ return this.currentConfiguration;
257
+ }
258
+ }
259
+
260
+ // Log success with detailed information
261
+ if (newConfig) {
262
+ this.logger?.info(
263
+ `Live Config: Configuration loaded successfully from ${loadResult.sourcePath || "merged sources"}`,
264
+ );
265
+
266
+ // Log detailed configuration info
267
+ const hookCount = Object.keys(newConfig.hooks || {}).length;
268
+ const envCount = Object.keys(newConfig.env || {}).length;
269
+ this.logger?.debug(
270
+ `Live Config: Loaded ${hookCount} hook events and ${envCount} environment variables`,
271
+ );
272
+ } else {
273
+ this.logger?.info(
274
+ "Live Config: No configuration found (using empty configuration)",
183
275
  );
184
276
  }
185
- }
186
277
 
187
- // Log cache status after refresh
188
- const cacheStatus = configResolver.getCacheStatus();
189
- if (cacheStatus) {
278
+ // Log warnings from successful loading
279
+ if (loadResult.warnings && loadResult.warnings.length > 0) {
280
+ this.logger?.warn(
281
+ `Live Config: Configuration warnings: ${loadResult.warnings.join("; ")}`,
282
+ );
283
+ }
284
+
285
+ // Validate new configuration if it exists
286
+ if (newConfig) {
287
+ const validation = this.validateConfiguration(newConfig);
288
+ if (!validation.valid) {
289
+ const errorMessage = `Configuration validation failed: ${validation.errors.join(", ")}`;
290
+ this.logger?.error(`Live Config: ${errorMessage}`);
291
+
292
+ // Use previous valid configuration for error recovery
293
+ if (this.lastValidConfiguration) {
294
+ this.logger?.info(
295
+ "Live Config: Using previous valid configuration due to validation errors",
296
+ );
297
+ this.currentConfiguration = this.lastValidConfiguration;
298
+
299
+ // Apply environment variables to configuration service if configured
300
+ if (this.lastValidConfiguration.env) {
301
+ this.configurationService.setEnvironmentVars(
302
+ this.lastValidConfiguration.env,
303
+ );
304
+ }
305
+
306
+ // Update hook manager if available
307
+ if (this.hookManager) {
308
+ this.hookManager.loadConfigurationFromWaveConfig(
309
+ this.lastValidConfiguration,
310
+ );
311
+ }
312
+
313
+ return this.currentConfiguration;
314
+ } else {
315
+ this.logger?.warn(
316
+ "Live Config: No previous valid configuration available, using empty config",
317
+ );
318
+ this.currentConfiguration = {};
319
+ return this.currentConfiguration;
320
+ }
321
+ }
322
+ }
323
+
324
+ // Detect changes between old and new configuration
325
+ this.detectChanges(this.currentConfiguration, newConfig);
326
+
327
+ // Update current configuration
328
+ this.currentConfiguration = newConfig || {};
329
+
330
+ // Save as last valid configuration if it's valid and not empty
331
+ if (newConfig && (newConfig.hooks || newConfig.env)) {
332
+ this.lastValidConfiguration = { ...newConfig };
333
+ this.logger?.debug(
334
+ "Live Config: Saved current configuration as last valid backup",
335
+ );
336
+ }
337
+
338
+ // Note: Environment variables are already applied by loadMergedConfiguration()
339
+ // No need to set them again here as currentConfiguration === newConfig
340
+
341
+ // Update hook manager if available
342
+ if (this.hookManager) {
343
+ this.hookManager.loadConfigurationFromWaveConfig(
344
+ this.currentConfiguration,
345
+ );
346
+ }
347
+
348
+ // Update permission manager if available
349
+ if (this.permissionManager) {
350
+ if (this.currentConfiguration.defaultMode) {
351
+ this.permissionManager.updateConfiguredDefaultMode(
352
+ this.currentConfiguration.defaultMode,
353
+ );
354
+ }
355
+ if (this.currentConfiguration.permissions?.allow) {
356
+ this.permissionManager.updateAllowedRules(
357
+ this.currentConfiguration.permissions.allow,
358
+ );
359
+ }
360
+ }
361
+
190
362
  this.logger?.info(
191
- `Live Config: Configuration cache refreshed - ${cacheStatus.envVarCount} environment variables loaded`,
363
+ `Live Config: Configuration reload completed successfully with ${Object.keys(newConfig?.hooks || {}).length} event types and ${Object.keys(newConfig?.env || {}).length} environment variables`,
192
364
  );
365
+
366
+ return this.currentConfiguration;
367
+ } catch (error) {
368
+ const errorMessage = `Configuration reload failed with exception: ${(error as Error).message}`;
369
+ this.logger?.error(`Live Config: ${errorMessage}`);
370
+
371
+ // Use previous valid configuration for error recovery
372
+ if (this.lastValidConfiguration) {
373
+ this.logger?.info(
374
+ "Live Config: Using previous valid configuration due to reload exception",
375
+ );
376
+ this.currentConfiguration = this.lastValidConfiguration;
377
+
378
+ // Apply environment variables to configuration service if configured
379
+ if (this.lastValidConfiguration.env) {
380
+ this.configurationService.setEnvironmentVars(
381
+ this.lastValidConfiguration.env,
382
+ );
383
+ }
384
+
385
+ // Update hook manager if available
386
+ if (this.hookManager) {
387
+ this.hookManager.loadConfigurationFromWaveConfig(
388
+ this.lastValidConfiguration,
389
+ );
390
+ }
391
+ } else {
392
+ this.logger?.warn(
393
+ "Live Config: No previous valid configuration available, using empty config",
394
+ );
395
+ this.currentConfiguration = {};
396
+ }
397
+
398
+ return this.currentConfiguration;
399
+ } finally {
400
+ this.reloadInProgress = false;
193
401
  }
194
402
  }
195
403
 
196
404
  /**
197
- * Handle AGENTS.md file change events
405
+ * Reload configuration from files (public method)
198
406
  */
199
- private async handleMemoryStoreFileChange(
407
+ async reload(): Promise<WaveConfiguration> {
408
+ this.logger?.info("Manually reloading configuration...");
409
+ return await this.reloadConfiguration();
410
+ }
411
+
412
+ /**
413
+ * Check if watching is active
414
+ */
415
+ isWatchingActive(): boolean {
416
+ return this.isWatching;
417
+ }
418
+
419
+ /**
420
+ * Get watcher status for monitoring
421
+ */
422
+ getWatcherStatus() {
423
+ const statuses = this.fileWatcher.getAllWatcherStatuses();
424
+ return {
425
+ isActive: this.isWatching,
426
+ configurationLoaded: this.currentConfiguration !== null,
427
+ hasValidConfiguration: this.lastValidConfiguration !== null,
428
+ reloadInProgress: this.reloadInProgress,
429
+ watchedFiles: statuses.map((s) => ({
430
+ path: s.path,
431
+ isActive: s.isActive,
432
+ method: s.method,
433
+ errorCount: s.errorCount,
434
+ })),
435
+ };
436
+ }
437
+
438
+ private setupFileWatcherEvents(): void {
439
+ this.fileWatcher.on("watcherError", (error: Error) => {
440
+ this.logger?.error(`Live Config: File watcher error: ${error.message}`);
441
+ });
442
+ }
443
+
444
+ private async handleFileChange(
200
445
  event: FileWatchEvent,
446
+ source: "user" | "project",
201
447
  ): Promise<void> {
202
- if (!this.onMemoryStoreFileChanged) {
203
- return;
204
- }
448
+ this.logger?.debug(
449
+ `Live Config: File ${event.type} detected for ${source} config: ${event.path}`,
450
+ );
205
451
 
206
452
  try {
207
- this.logger?.info(
208
- `Live Config: AGENTS.md ${event.type} detected: ${event.path}`,
209
- );
453
+ // Handle file deletion
454
+ if (event.type === "delete") {
455
+ this.logger?.info(
456
+ `Live Config: ${source} config file deleted: ${event.path}`,
457
+ );
458
+ // Reload configuration without the deleted file
459
+ await this.reloadConfiguration();
460
+ return;
461
+ }
210
462
 
211
- const changeType: "add" | "change" | "unlink" =
212
- event.type === "delete"
213
- ? "unlink"
214
- : event.type === "create"
215
- ? "add"
216
- : "change";
217
- await this.onMemoryStoreFileChanged(event.path, changeType);
463
+ // Handle file creation or modification
464
+ if (event.type === "change" || event.type === "create") {
465
+ this.logger?.info(
466
+ `Live Config: ${source} config file ${event.type}: ${event.path}`,
467
+ );
218
468
 
219
- this.logger?.info(
220
- `Live Config: Memory store updated for AGENTS.md ${event.type}`,
221
- );
469
+ if (
470
+ source === "project" &&
471
+ event.path.endsWith("settings.local.json") &&
472
+ event.type === "create"
473
+ ) {
474
+ await ensureGlobalGitIgnore("**/.wave/settings.local.json");
475
+ }
476
+
477
+ // Add small delay to ensure file write is complete
478
+ await new Promise((resolve) => setTimeout(resolve, 50));
479
+
480
+ // Reload configuration
481
+ await this.reloadConfiguration();
482
+ }
222
483
  } catch (error) {
223
484
  this.logger?.error(
224
- `Live Config: Failed to handle AGENTS.md file change: ${(error as Error).message}`,
485
+ `Live Config: Error handling file change for ${source} config: ${(error as Error).message}`,
225
486
  );
226
487
  }
227
488
  }
228
489
 
229
490
  /**
230
- * Get initialization status
491
+ * Validate configuration structure and content
231
492
  */
232
- get initialized(): boolean {
233
- return this.isInitialized;
493
+ private validateConfiguration(config: WaveConfiguration): ValidationResult {
494
+ const errors: string[] = [];
495
+
496
+ if (!config || typeof config !== "object") {
497
+ return { valid: false, errors: ["Configuration must be an object"] };
498
+ }
499
+
500
+ // Validate defaultMode if present
501
+ if (config.defaultMode !== undefined) {
502
+ if (
503
+ config.defaultMode !== "default" &&
504
+ config.defaultMode !== "bypassPermissions" &&
505
+ config.defaultMode !== "acceptEdits"
506
+ ) {
507
+ errors.push(
508
+ `Invalid defaultMode: "${config.defaultMode}". Must be "default", "bypassPermissions" or "acceptEdits"`,
509
+ );
510
+ }
511
+ }
512
+
513
+ // Validate hooks if present
514
+ if (config.hooks) {
515
+ if (typeof config.hooks !== "object") {
516
+ errors.push("hooks property must be an object");
517
+ } else {
518
+ // Validate each hook event
519
+ for (const [eventName, eventConfigs] of Object.entries(config.hooks)) {
520
+ // Validate event name
521
+ if (!isValidHookEvent(eventName)) {
522
+ errors.push(`Invalid hook event: ${eventName}`);
523
+ continue;
524
+ }
525
+
526
+ // Validate event configurations
527
+ if (!Array.isArray(eventConfigs)) {
528
+ errors.push(
529
+ `Hook event ${eventName} must be an array of configurations`,
530
+ );
531
+ continue;
532
+ }
533
+
534
+ eventConfigs.forEach((eventConfig, index) => {
535
+ if (!isValidHookEventConfig(eventConfig)) {
536
+ errors.push(
537
+ `Invalid hook event configuration at ${eventName}[${index}]`,
538
+ );
539
+ }
540
+ });
541
+ }
542
+ }
543
+ }
544
+
545
+ // Validate environment variables if present
546
+ if (config.env) {
547
+ if (typeof config.env !== "object" || Array.isArray(config.env)) {
548
+ errors.push("env property must be an object");
549
+ } else {
550
+ for (const [key, value] of Object.entries(config.env)) {
551
+ if (typeof key !== "string" || key.trim() === "") {
552
+ errors.push(`Invalid environment variable key: ${key}`);
553
+ }
554
+ if (typeof value !== "string") {
555
+ errors.push(`Environment variable ${key} must have a string value`);
556
+ }
557
+ }
558
+ }
559
+ }
560
+
561
+ return {
562
+ valid: errors.length === 0,
563
+ errors,
564
+ };
565
+ }
566
+
567
+ private detectChanges(
568
+ oldConfig: WaveConfiguration | null,
569
+ newConfig: WaveConfiguration | null,
570
+ ): {
571
+ added: string[];
572
+ modified: string[];
573
+ removed: string[];
574
+ } {
575
+ const added: string[] = [];
576
+ const modified: string[] = [];
577
+ const removed: string[] = [];
578
+
579
+ // Handle environment variables changes
580
+ const oldEnv = oldConfig?.env || {};
581
+ const newEnv = newConfig?.env || {};
582
+
583
+ for (const key of Object.keys(newEnv)) {
584
+ if (!(key in oldEnv)) {
585
+ added.push(`env.${key}`);
586
+ } else if (oldEnv[key] !== newEnv[key]) {
587
+ modified.push(`env.${key}`);
588
+ }
589
+ }
590
+
591
+ for (const key of Object.keys(oldEnv)) {
592
+ if (!(key in newEnv)) {
593
+ removed.push(`env.${key}`);
594
+ }
595
+ }
596
+
597
+ // Handle hooks changes (simplified)
598
+ const oldHooks = oldConfig?.hooks || {};
599
+ const newHooks = newConfig?.hooks || {};
600
+
601
+ for (const event of Object.keys(newHooks)) {
602
+ if (isValidHookEvent(event)) {
603
+ if (!(event in oldHooks)) {
604
+ added.push(`hooks.${event}`);
605
+ } else if (
606
+ JSON.stringify(oldHooks[event]) !== JSON.stringify(newHooks[event])
607
+ ) {
608
+ modified.push(`hooks.${event}`);
609
+ }
610
+ }
611
+ }
612
+
613
+ for (const event of Object.keys(oldHooks)) {
614
+ if (isValidHookEvent(event) && !(event in newHooks)) {
615
+ removed.push(`hooks.${event}`);
616
+ }
617
+ }
618
+
619
+ return { added, modified, removed };
234
620
  }
235
621
 
236
622
  /**