@shareai-lab/kode 1.0.70 → 1.0.71

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 (253) hide show
  1. package/README.md +202 -76
  2. package/README.zh-CN.md +246 -0
  3. package/cli.js +62 -0
  4. package/package.json +45 -25
  5. package/scripts/postinstall.js +56 -0
  6. package/src/ProjectOnboarding.tsx +180 -0
  7. package/src/Tool.ts +53 -0
  8. package/src/commands/approvedTools.ts +53 -0
  9. package/src/commands/bug.tsx +20 -0
  10. package/src/commands/clear.ts +43 -0
  11. package/src/commands/compact.ts +120 -0
  12. package/src/commands/config.tsx +19 -0
  13. package/src/commands/cost.ts +18 -0
  14. package/src/commands/ctx_viz.ts +209 -0
  15. package/src/commands/doctor.ts +24 -0
  16. package/src/commands/help.tsx +19 -0
  17. package/src/commands/init.ts +37 -0
  18. package/src/commands/listen.ts +42 -0
  19. package/src/commands/login.tsx +51 -0
  20. package/src/commands/logout.tsx +40 -0
  21. package/src/commands/mcp.ts +41 -0
  22. package/src/commands/model.tsx +40 -0
  23. package/src/commands/modelstatus.tsx +20 -0
  24. package/src/commands/onboarding.tsx +34 -0
  25. package/src/commands/pr_comments.ts +59 -0
  26. package/src/commands/refreshCommands.ts +54 -0
  27. package/src/commands/release-notes.ts +34 -0
  28. package/src/commands/resume.tsx +30 -0
  29. package/src/commands/review.ts +49 -0
  30. package/src/commands/terminalSetup.ts +221 -0
  31. package/src/commands.ts +136 -0
  32. package/src/components/ApproveApiKey.tsx +93 -0
  33. package/src/components/AsciiLogo.tsx +13 -0
  34. package/src/components/AutoUpdater.tsx +148 -0
  35. package/src/components/Bug.tsx +367 -0
  36. package/src/components/Config.tsx +289 -0
  37. package/src/components/ConsoleOAuthFlow.tsx +326 -0
  38. package/src/components/Cost.tsx +23 -0
  39. package/src/components/CostThresholdDialog.tsx +46 -0
  40. package/src/components/CustomSelect/option-map.ts +42 -0
  41. package/src/components/CustomSelect/select-option.tsx +52 -0
  42. package/src/components/CustomSelect/select.tsx +143 -0
  43. package/src/components/CustomSelect/use-select-state.ts +414 -0
  44. package/src/components/CustomSelect/use-select.ts +35 -0
  45. package/src/components/FallbackToolUseRejectedMessage.tsx +15 -0
  46. package/src/components/FileEditToolUpdatedMessage.tsx +66 -0
  47. package/src/components/Help.tsx +215 -0
  48. package/src/components/HighlightedCode.tsx +33 -0
  49. package/src/components/InvalidConfigDialog.tsx +113 -0
  50. package/src/components/Link.tsx +32 -0
  51. package/src/components/LogSelector.tsx +86 -0
  52. package/src/components/Logo.tsx +145 -0
  53. package/src/components/MCPServerApprovalDialog.tsx +100 -0
  54. package/src/components/MCPServerDialogCopy.tsx +25 -0
  55. package/src/components/MCPServerMultiselectDialog.tsx +109 -0
  56. package/src/components/Message.tsx +219 -0
  57. package/src/components/MessageResponse.tsx +15 -0
  58. package/src/components/MessageSelector.tsx +211 -0
  59. package/src/components/ModeIndicator.tsx +88 -0
  60. package/src/components/ModelConfig.tsx +301 -0
  61. package/src/components/ModelListManager.tsx +223 -0
  62. package/src/components/ModelSelector.tsx +3208 -0
  63. package/src/components/ModelStatusDisplay.tsx +228 -0
  64. package/src/components/Onboarding.tsx +274 -0
  65. package/src/components/PressEnterToContinue.tsx +11 -0
  66. package/src/components/PromptInput.tsx +710 -0
  67. package/src/components/SentryErrorBoundary.ts +33 -0
  68. package/src/components/Spinner.tsx +129 -0
  69. package/src/components/StructuredDiff.tsx +184 -0
  70. package/src/components/TextInput.tsx +246 -0
  71. package/src/components/TokenWarning.tsx +31 -0
  72. package/src/components/ToolUseLoader.tsx +40 -0
  73. package/src/components/TrustDialog.tsx +106 -0
  74. package/src/components/binary-feedback/BinaryFeedback.tsx +63 -0
  75. package/src/components/binary-feedback/BinaryFeedbackOption.tsx +111 -0
  76. package/src/components/binary-feedback/BinaryFeedbackView.tsx +172 -0
  77. package/src/components/binary-feedback/utils.ts +220 -0
  78. package/src/components/messages/AssistantBashOutputMessage.tsx +22 -0
  79. package/src/components/messages/AssistantLocalCommandOutputMessage.tsx +45 -0
  80. package/src/components/messages/AssistantRedactedThinkingMessage.tsx +19 -0
  81. package/src/components/messages/AssistantTextMessage.tsx +144 -0
  82. package/src/components/messages/AssistantThinkingMessage.tsx +40 -0
  83. package/src/components/messages/AssistantToolUseMessage.tsx +123 -0
  84. package/src/components/messages/UserBashInputMessage.tsx +28 -0
  85. package/src/components/messages/UserCommandMessage.tsx +30 -0
  86. package/src/components/messages/UserKodingInputMessage.tsx +28 -0
  87. package/src/components/messages/UserPromptMessage.tsx +35 -0
  88. package/src/components/messages/UserTextMessage.tsx +39 -0
  89. package/src/components/messages/UserToolResultMessage/UserToolCanceledMessage.tsx +12 -0
  90. package/src/components/messages/UserToolResultMessage/UserToolErrorMessage.tsx +36 -0
  91. package/src/components/messages/UserToolResultMessage/UserToolRejectMessage.tsx +31 -0
  92. package/src/components/messages/UserToolResultMessage/UserToolResultMessage.tsx +57 -0
  93. package/src/components/messages/UserToolResultMessage/UserToolSuccessMessage.tsx +35 -0
  94. package/src/components/messages/UserToolResultMessage/utils.tsx +56 -0
  95. package/src/components/permissions/BashPermissionRequest/BashPermissionRequest.tsx +121 -0
  96. package/src/components/permissions/FallbackPermissionRequest.tsx +155 -0
  97. package/src/components/permissions/FileEditPermissionRequest/FileEditPermissionRequest.tsx +182 -0
  98. package/src/components/permissions/FileEditPermissionRequest/FileEditToolDiff.tsx +75 -0
  99. package/src/components/permissions/FileWritePermissionRequest/FileWritePermissionRequest.tsx +164 -0
  100. package/src/components/permissions/FileWritePermissionRequest/FileWriteToolDiff.tsx +81 -0
  101. package/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx +242 -0
  102. package/src/components/permissions/PermissionRequest.tsx +103 -0
  103. package/src/components/permissions/PermissionRequestTitle.tsx +69 -0
  104. package/src/components/permissions/hooks.ts +44 -0
  105. package/src/components/permissions/toolUseOptions.ts +59 -0
  106. package/src/components/permissions/utils.ts +23 -0
  107. package/src/constants/betas.ts +5 -0
  108. package/src/constants/claude-asterisk-ascii-art.tsx +238 -0
  109. package/src/constants/figures.ts +4 -0
  110. package/src/constants/keys.ts +3 -0
  111. package/src/constants/macros.ts +6 -0
  112. package/src/constants/models.ts +935 -0
  113. package/src/constants/oauth.ts +18 -0
  114. package/src/constants/product.ts +17 -0
  115. package/src/constants/prompts.ts +177 -0
  116. package/src/constants/releaseNotes.ts +7 -0
  117. package/src/context/PermissionContext.tsx +149 -0
  118. package/src/context.ts +278 -0
  119. package/src/cost-tracker.ts +84 -0
  120. package/src/entrypoints/cli.tsx +1498 -0
  121. package/src/entrypoints/mcp.ts +176 -0
  122. package/src/history.ts +25 -0
  123. package/src/hooks/useApiKeyVerification.ts +59 -0
  124. package/src/hooks/useArrowKeyHistory.ts +55 -0
  125. package/src/hooks/useCanUseTool.ts +138 -0
  126. package/src/hooks/useCancelRequest.ts +39 -0
  127. package/src/hooks/useDoublePress.ts +42 -0
  128. package/src/hooks/useExitOnCtrlCD.ts +31 -0
  129. package/src/hooks/useInterval.ts +25 -0
  130. package/src/hooks/useLogMessages.ts +16 -0
  131. package/src/hooks/useLogStartupTime.ts +12 -0
  132. package/src/hooks/useNotifyAfterTimeout.ts +65 -0
  133. package/src/hooks/usePermissionRequestLogging.ts +44 -0
  134. package/src/hooks/useSlashCommandTypeahead.ts +137 -0
  135. package/src/hooks/useTerminalSize.ts +49 -0
  136. package/src/hooks/useTextInput.ts +315 -0
  137. package/src/messages.ts +37 -0
  138. package/src/permissions.ts +268 -0
  139. package/src/query.ts +704 -0
  140. package/src/screens/ConfigureNpmPrefix.tsx +197 -0
  141. package/src/screens/Doctor.tsx +219 -0
  142. package/src/screens/LogList.tsx +68 -0
  143. package/src/screens/REPL.tsx +792 -0
  144. package/src/screens/ResumeConversation.tsx +68 -0
  145. package/src/services/browserMocks.ts +66 -0
  146. package/src/services/claude.ts +1947 -0
  147. package/src/services/customCommands.ts +683 -0
  148. package/src/services/fileFreshness.ts +377 -0
  149. package/src/services/mcpClient.ts +564 -0
  150. package/src/services/mcpServerApproval.tsx +50 -0
  151. package/src/services/notifier.ts +40 -0
  152. package/src/services/oauth.ts +357 -0
  153. package/src/services/openai.ts +796 -0
  154. package/src/services/sentry.ts +3 -0
  155. package/src/services/statsig.ts +171 -0
  156. package/src/services/statsigStorage.ts +86 -0
  157. package/src/services/systemReminder.ts +406 -0
  158. package/src/services/vcr.ts +161 -0
  159. package/src/tools/ArchitectTool/ArchitectTool.tsx +122 -0
  160. package/src/tools/ArchitectTool/prompt.ts +15 -0
  161. package/src/tools/AskExpertModelTool/AskExpertModelTool.tsx +505 -0
  162. package/src/tools/BashTool/BashTool.tsx +270 -0
  163. package/src/tools/BashTool/BashToolResultMessage.tsx +38 -0
  164. package/src/tools/BashTool/OutputLine.tsx +48 -0
  165. package/src/tools/BashTool/prompt.ts +174 -0
  166. package/src/tools/BashTool/utils.ts +56 -0
  167. package/src/tools/FileEditTool/FileEditTool.tsx +316 -0
  168. package/src/tools/FileEditTool/prompt.ts +51 -0
  169. package/src/tools/FileEditTool/utils.ts +58 -0
  170. package/src/tools/FileReadTool/FileReadTool.tsx +371 -0
  171. package/src/tools/FileReadTool/prompt.ts +7 -0
  172. package/src/tools/FileWriteTool/FileWriteTool.tsx +297 -0
  173. package/src/tools/FileWriteTool/prompt.ts +10 -0
  174. package/src/tools/GlobTool/GlobTool.tsx +119 -0
  175. package/src/tools/GlobTool/prompt.ts +8 -0
  176. package/src/tools/GrepTool/GrepTool.tsx +147 -0
  177. package/src/tools/GrepTool/prompt.ts +11 -0
  178. package/src/tools/MCPTool/MCPTool.tsx +106 -0
  179. package/src/tools/MCPTool/prompt.ts +3 -0
  180. package/src/tools/MemoryReadTool/MemoryReadTool.tsx +127 -0
  181. package/src/tools/MemoryReadTool/prompt.ts +3 -0
  182. package/src/tools/MemoryWriteTool/MemoryWriteTool.tsx +89 -0
  183. package/src/tools/MemoryWriteTool/prompt.ts +3 -0
  184. package/src/tools/MultiEditTool/MultiEditTool.tsx +366 -0
  185. package/src/tools/MultiEditTool/prompt.ts +45 -0
  186. package/src/tools/NotebookEditTool/NotebookEditTool.tsx +298 -0
  187. package/src/tools/NotebookEditTool/prompt.ts +3 -0
  188. package/src/tools/NotebookReadTool/NotebookReadTool.tsx +266 -0
  189. package/src/tools/NotebookReadTool/prompt.ts +3 -0
  190. package/src/tools/StickerRequestTool/StickerRequestTool.tsx +93 -0
  191. package/src/tools/StickerRequestTool/prompt.ts +19 -0
  192. package/src/tools/TaskTool/TaskTool.tsx +382 -0
  193. package/src/tools/TaskTool/constants.ts +1 -0
  194. package/src/tools/TaskTool/prompt.ts +56 -0
  195. package/src/tools/ThinkTool/ThinkTool.tsx +56 -0
  196. package/src/tools/ThinkTool/prompt.ts +12 -0
  197. package/src/tools/TodoWriteTool/TodoWriteTool.tsx +289 -0
  198. package/src/tools/TodoWriteTool/prompt.ts +63 -0
  199. package/src/tools/lsTool/lsTool.tsx +269 -0
  200. package/src/tools/lsTool/prompt.ts +2 -0
  201. package/src/tools.ts +63 -0
  202. package/src/types/PermissionMode.ts +120 -0
  203. package/src/types/RequestContext.ts +72 -0
  204. package/src/utils/Cursor.ts +436 -0
  205. package/src/utils/PersistentShell.ts +373 -0
  206. package/src/utils/agentStorage.ts +97 -0
  207. package/src/utils/array.ts +3 -0
  208. package/src/utils/ask.tsx +98 -0
  209. package/src/utils/auth.ts +13 -0
  210. package/src/utils/autoCompactCore.ts +223 -0
  211. package/src/utils/autoUpdater.ts +318 -0
  212. package/src/utils/betas.ts +20 -0
  213. package/src/utils/browser.ts +14 -0
  214. package/src/utils/cleanup.ts +72 -0
  215. package/src/utils/commands.ts +261 -0
  216. package/src/utils/config.ts +771 -0
  217. package/src/utils/conversationRecovery.ts +54 -0
  218. package/src/utils/debugLogger.ts +1123 -0
  219. package/src/utils/diff.ts +42 -0
  220. package/src/utils/env.ts +57 -0
  221. package/src/utils/errors.ts +21 -0
  222. package/src/utils/exampleCommands.ts +108 -0
  223. package/src/utils/execFileNoThrow.ts +51 -0
  224. package/src/utils/expertChatStorage.ts +136 -0
  225. package/src/utils/file.ts +402 -0
  226. package/src/utils/fileRecoveryCore.ts +71 -0
  227. package/src/utils/format.tsx +44 -0
  228. package/src/utils/generators.ts +62 -0
  229. package/src/utils/git.ts +92 -0
  230. package/src/utils/globalLogger.ts +77 -0
  231. package/src/utils/http.ts +10 -0
  232. package/src/utils/imagePaste.ts +38 -0
  233. package/src/utils/json.ts +13 -0
  234. package/src/utils/log.ts +382 -0
  235. package/src/utils/markdown.ts +213 -0
  236. package/src/utils/messageContextManager.ts +289 -0
  237. package/src/utils/messages.tsx +938 -0
  238. package/src/utils/model.ts +836 -0
  239. package/src/utils/permissions/filesystem.ts +118 -0
  240. package/src/utils/ripgrep.ts +167 -0
  241. package/src/utils/sessionState.ts +49 -0
  242. package/src/utils/state.ts +25 -0
  243. package/src/utils/style.ts +29 -0
  244. package/src/utils/terminal.ts +49 -0
  245. package/src/utils/theme.ts +122 -0
  246. package/src/utils/thinking.ts +144 -0
  247. package/src/utils/todoStorage.ts +431 -0
  248. package/src/utils/tokens.ts +43 -0
  249. package/src/utils/toolExecutionController.ts +163 -0
  250. package/src/utils/unaryLogging.ts +26 -0
  251. package/src/utils/user.ts +37 -0
  252. package/src/utils/validate.ts +165 -0
  253. package/cli.mjs +0 -1803
@@ -0,0 +1,377 @@
1
+ import { statSync, existsSync, watchFile, unwatchFile } from 'fs'
2
+ import {
3
+ emitReminderEvent,
4
+ systemReminderService,
5
+ } from '../services/systemReminder'
6
+ import { getAgentFilePath } from '../utils/agentStorage'
7
+ import { logEvent } from '../services/statsig'
8
+
9
+ interface FileTimestamp {
10
+ path: string
11
+ lastRead: number
12
+ lastModified: number
13
+ size: number
14
+ lastAgentEdit?: number // Track when Agent last edited this file
15
+ }
16
+
17
+ interface FileFreshnessState {
18
+ readTimestamps: Map<string, FileTimestamp>
19
+ editConflicts: Set<string>
20
+ sessionFiles: Set<string>
21
+ watchedTodoFiles: Map<string, string> // agentId -> filePath
22
+ }
23
+
24
+ class FileFreshnessService {
25
+ private state: FileFreshnessState = {
26
+ readTimestamps: new Map(),
27
+ editConflicts: new Set(),
28
+ sessionFiles: new Set(),
29
+ watchedTodoFiles: new Map(),
30
+ }
31
+
32
+ constructor() {
33
+ this.setupEventListeners()
34
+ }
35
+
36
+ /**
37
+ * Setup event listeners for session management
38
+ */
39
+ private setupEventListeners(): void {
40
+ // Listen for session startup events through the SystemReminderService
41
+ systemReminderService.addEventListener(
42
+ 'session:startup',
43
+ (context: any) => {
44
+ // Reset session state on startup
45
+ this.resetSession()
46
+
47
+ // Log session startup
48
+ logEvent('file_freshness_session_startup', {
49
+ agentId: context.agentId || 'default',
50
+ timestamp: context.timestamp,
51
+ })
52
+ },
53
+ )
54
+ }
55
+
56
+ /**
57
+ * Record file read operation with timestamp tracking
58
+ */
59
+ public recordFileRead(filePath: string): void {
60
+ try {
61
+ if (!existsSync(filePath)) {
62
+ return
63
+ }
64
+
65
+ const stats = statSync(filePath)
66
+ const timestamp: FileTimestamp = {
67
+ path: filePath,
68
+ lastRead: Date.now(),
69
+ lastModified: stats.mtimeMs,
70
+ size: stats.size,
71
+ }
72
+
73
+ this.state.readTimestamps.set(filePath, timestamp)
74
+ this.state.sessionFiles.add(filePath)
75
+
76
+ // Emit file read event for system reminders
77
+ emitReminderEvent('file:read', {
78
+ filePath,
79
+ timestamp: timestamp.lastRead,
80
+ size: timestamp.size,
81
+ modified: timestamp.lastModified,
82
+ })
83
+ } catch (error) {
84
+ console.error(`Error recording file read for ${filePath}:`, error)
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Check if file has been modified since last read
90
+ */
91
+ public checkFileFreshness(filePath: string): {
92
+ isFresh: boolean
93
+ lastRead?: number
94
+ currentModified?: number
95
+ conflict: boolean
96
+ } {
97
+ const recorded = this.state.readTimestamps.get(filePath)
98
+
99
+ if (!recorded) {
100
+ return { isFresh: true, conflict: false }
101
+ }
102
+
103
+ try {
104
+ if (!existsSync(filePath)) {
105
+ return { isFresh: false, conflict: true }
106
+ }
107
+
108
+ const currentStats = statSync(filePath)
109
+ const isFresh = currentStats.mtimeMs <= recorded.lastModified
110
+ const conflict = !isFresh
111
+
112
+ if (conflict) {
113
+ this.state.editConflicts.add(filePath)
114
+
115
+ // Emit file conflict event
116
+ emitReminderEvent('file:conflict', {
117
+ filePath,
118
+ lastRead: recorded.lastRead,
119
+ lastModified: recorded.lastModified,
120
+ currentModified: currentStats.mtimeMs,
121
+ sizeDiff: currentStats.size - recorded.size,
122
+ })
123
+ }
124
+
125
+ return {
126
+ isFresh,
127
+ lastRead: recorded.lastRead,
128
+ currentModified: currentStats.mtimeMs,
129
+ conflict,
130
+ }
131
+ } catch (error) {
132
+ console.error(`Error checking freshness for ${filePath}:`, error)
133
+ return { isFresh: false, conflict: true }
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Record file edit operation by Agent
139
+ */
140
+ public recordFileEdit(filePath: string, content?: string): void {
141
+ try {
142
+ const now = Date.now()
143
+
144
+ // Update recorded timestamp after edit
145
+ if (existsSync(filePath)) {
146
+ const stats = statSync(filePath)
147
+ const existing = this.state.readTimestamps.get(filePath)
148
+
149
+ if (existing) {
150
+ existing.lastModified = stats.mtimeMs
151
+ existing.size = stats.size
152
+ existing.lastAgentEdit = now // Mark this as Agent-initiated edit
153
+ this.state.readTimestamps.set(filePath, existing)
154
+ } else {
155
+ // Create new record for Agent-edited file
156
+ const timestamp: FileTimestamp = {
157
+ path: filePath,
158
+ lastRead: now,
159
+ lastModified: stats.mtimeMs,
160
+ size: stats.size,
161
+ lastAgentEdit: now,
162
+ }
163
+ this.state.readTimestamps.set(filePath, timestamp)
164
+ }
165
+ }
166
+
167
+ // Remove from conflicts since we just edited it
168
+ this.state.editConflicts.delete(filePath)
169
+
170
+ // Emit file edit event
171
+ emitReminderEvent('file:edited', {
172
+ filePath,
173
+ timestamp: now,
174
+ contentLength: content?.length || 0,
175
+ source: 'agent',
176
+ })
177
+ } catch (error) {
178
+ console.error(`Error recording file edit for ${filePath}:`, error)
179
+ }
180
+ }
181
+
182
+ public generateFileModificationReminder(filePath: string): string | null {
183
+ const recorded = this.state.readTimestamps.get(filePath)
184
+
185
+ if (!recorded) {
186
+ return null
187
+ }
188
+
189
+ try {
190
+ if (!existsSync(filePath)) {
191
+ return `Note: ${filePath} was deleted since last read.`
192
+ }
193
+
194
+ const currentStats = statSync(filePath)
195
+ const isModified = currentStats.mtimeMs > recorded.lastModified
196
+
197
+ if (!isModified) {
198
+ return null
199
+ }
200
+
201
+ // Check if this was an Agent-initiated change
202
+ // Use small time tolerance to handle filesystem timestamp precision issues
203
+ const TIME_TOLERANCE_MS = 100
204
+ if (
205
+ recorded.lastAgentEdit &&
206
+ recorded.lastAgentEdit >= recorded.lastModified - TIME_TOLERANCE_MS
207
+ ) {
208
+ // Agent modified this file recently, no reminder needed
209
+ // (context already contains before/after content)
210
+ return null
211
+ }
212
+
213
+ // External modification detected - generate reminder
214
+ return `Note: ${filePath} was modified externally since last read. The file may have changed outside of this session.`
215
+ } catch (error) {
216
+ console.error(`Error checking modification for ${filePath}:`, error)
217
+ return null
218
+ }
219
+ }
220
+
221
+ public getConflictedFiles(): string[] {
222
+ return Array.from(this.state.editConflicts)
223
+ }
224
+
225
+ public getSessionFiles(): string[] {
226
+ return Array.from(this.state.sessionFiles)
227
+ }
228
+
229
+ public resetSession(): void {
230
+ // Clean up existing todo file watchers
231
+ this.state.watchedTodoFiles.forEach(filePath => {
232
+ try {
233
+ unwatchFile(filePath)
234
+ } catch (error) {
235
+ console.error(`Error unwatching file ${filePath}:`, error)
236
+ }
237
+ })
238
+
239
+ this.state = {
240
+ readTimestamps: new Map(),
241
+ editConflicts: new Set(),
242
+ sessionFiles: new Set(),
243
+ watchedTodoFiles: new Map(),
244
+ }
245
+ }
246
+
247
+ /**
248
+ * Start watching todo file for an agent
249
+ */
250
+ public startWatchingTodoFile(agentId: string): void {
251
+ try {
252
+ const filePath = getAgentFilePath(agentId)
253
+
254
+ // Don't watch if already watching
255
+ if (this.state.watchedTodoFiles.has(agentId)) {
256
+ return
257
+ }
258
+
259
+ this.state.watchedTodoFiles.set(agentId, filePath)
260
+
261
+ // Record initial state if file exists
262
+ if (existsSync(filePath)) {
263
+ this.recordFileRead(filePath)
264
+ }
265
+
266
+ // Start watching for changes
267
+ watchFile(filePath, { interval: 1000 }, (curr, prev) => {
268
+ // Check if this was an external modification
269
+ const reminder = this.generateFileModificationReminder(filePath)
270
+ if (reminder) {
271
+ // File was modified externally, emit todo change reminder
272
+ emitReminderEvent('todo:file_changed', {
273
+ agentId,
274
+ filePath,
275
+ reminder,
276
+ timestamp: Date.now(),
277
+ currentStats: { mtime: curr.mtime, size: curr.size },
278
+ previousStats: { mtime: prev.mtime, size: prev.size },
279
+ })
280
+ }
281
+ })
282
+ } catch (error) {
283
+ console.error(
284
+ `Error starting todo file watch for agent ${agentId}:`,
285
+ error,
286
+ )
287
+ }
288
+ }
289
+
290
+ /**
291
+ * Stop watching todo file for an agent
292
+ */
293
+ public stopWatchingTodoFile(agentId: string): void {
294
+ try {
295
+ const filePath = this.state.watchedTodoFiles.get(agentId)
296
+ if (filePath) {
297
+ unwatchFile(filePath)
298
+ this.state.watchedTodoFiles.delete(agentId)
299
+ }
300
+ } catch (error) {
301
+ console.error(
302
+ `Error stopping todo file watch for agent ${agentId}:`,
303
+ error,
304
+ )
305
+ }
306
+ }
307
+
308
+ public getFileInfo(filePath: string): FileTimestamp | null {
309
+ return this.state.readTimestamps.get(filePath) || null
310
+ }
311
+
312
+ public isFileTracked(filePath: string): boolean {
313
+ return this.state.readTimestamps.has(filePath)
314
+ }
315
+
316
+ /**
317
+ * Retrieves files prioritized for recovery during conversation compression
318
+ *
319
+ * Selects recently accessed files based on:
320
+ * - File access recency (most recent first)
321
+ * - File type relevance (excludes dependencies, build artifacts)
322
+ * - Development workflow importance
323
+ *
324
+ * Used to maintain coding context when conversation history is compressed
325
+ */
326
+ public getImportantFiles(maxFiles: number = 5): Array<{
327
+ path: string
328
+ timestamp: number
329
+ size: number
330
+ }> {
331
+ return Array.from(this.state.readTimestamps.entries())
332
+ .map(([path, info]) => ({
333
+ path,
334
+ timestamp: info.lastRead,
335
+ size: info.size,
336
+ }))
337
+ .filter(file => this.isValidForRecovery(file.path))
338
+ .sort((a, b) => b.timestamp - a.timestamp) // Newest first
339
+ .slice(0, maxFiles)
340
+ }
341
+
342
+ /**
343
+ * Determines which files are suitable for automatic recovery
344
+ *
345
+ * Excludes files that are typically not relevant for development context:
346
+ * - Build artifacts and generated files
347
+ * - Dependencies and cached files
348
+ * - Temporary files and system directories
349
+ */
350
+ private isValidForRecovery(filePath: string): boolean {
351
+ return (
352
+ !filePath.includes('node_modules') &&
353
+ !filePath.includes('.git') &&
354
+ !filePath.startsWith('/tmp') &&
355
+ !filePath.includes('.cache') &&
356
+ !filePath.includes('dist/') &&
357
+ !filePath.includes('build/')
358
+ )
359
+ }
360
+ }
361
+
362
+ export const fileFreshnessService = new FileFreshnessService()
363
+
364
+ export const recordFileRead = (filePath: string) =>
365
+ fileFreshnessService.recordFileRead(filePath)
366
+ export const recordFileEdit = (filePath: string, content?: string) =>
367
+ fileFreshnessService.recordFileEdit(filePath, content)
368
+ export const checkFileFreshness = (filePath: string) =>
369
+ fileFreshnessService.checkFileFreshness(filePath)
370
+ export const generateFileModificationReminder = (filePath: string) =>
371
+ fileFreshnessService.generateFileModificationReminder(filePath)
372
+ export const resetFileFreshnessSession = () =>
373
+ fileFreshnessService.resetSession()
374
+ export const startWatchingTodoFile = (agentId: string) =>
375
+ fileFreshnessService.startWatchingTodoFile(agentId)
376
+ export const stopWatchingTodoFile = (agentId: string) =>
377
+ fileFreshnessService.stopWatchingTodoFile(agentId)