( agentName: string, teamName?: string, )
| 277 | * @param teamName - Optional team name |
| 278 | */ |
| 279 | export async function markMessagesAsRead( |
| 280 | agentName: string, |
| 281 | teamName?: string, |
| 282 | ): Promise<void> { |
| 283 | const inboxPath = getInboxPath(agentName, teamName) |
| 284 | logForDebugging( |
| 285 | `[TeammateMailbox] markMessagesAsRead called: agentName=${agentName}, teamName=${teamName}, path=${inboxPath}`, |
| 286 | ) |
| 287 | |
| 288 | const lockFilePath = `${inboxPath}.lock` |
| 289 | |
| 290 | let release: (() => Promise<void>) | undefined |
| 291 | try { |
| 292 | logForDebugging(`[TeammateMailbox] markMessagesAsRead: acquiring lock...`) |
| 293 | release = await lockfile.lock(inboxPath, { |
| 294 | lockfilePath: lockFilePath, |
| 295 | ...LOCK_OPTIONS, |
| 296 | }) |
| 297 | logForDebugging(`[TeammateMailbox] markMessagesAsRead: lock acquired`) |
| 298 | |
| 299 | // Re-read messages after acquiring lock to get the latest state |
| 300 | const messages = await readMailbox(agentName, teamName) |
| 301 | logForDebugging( |
| 302 | `[TeammateMailbox] markMessagesAsRead: read ${messages.length} messages after lock`, |
| 303 | ) |
| 304 | |
| 305 | if (messages.length === 0) { |
| 306 | logForDebugging( |
| 307 | `[TeammateMailbox] markMessagesAsRead: no messages to mark`, |
| 308 | ) |
| 309 | return |
| 310 | } |
| 311 | |
| 312 | const unreadCount = count(messages, m => !m.read) |
| 313 | logForDebugging( |
| 314 | `[TeammateMailbox] markMessagesAsRead: ${unreadCount} unread of ${messages.length} total`, |
| 315 | ) |
| 316 | |
| 317 | // messages comes from jsonParse — fresh, unshared objects safe to mutate |
| 318 | for (const m of messages) m.read = true |
| 319 | |
| 320 | await writeFile(inboxPath, jsonStringify(messages, null, 2), 'utf-8') |
| 321 | logForDebugging( |
| 322 | `[TeammateMailbox] markMessagesAsRead: WROTE ${unreadCount} message(s) as read to ${inboxPath}`, |
| 323 | ) |
| 324 | } catch (error) { |
| 325 | const code = getErrnoCode(error) |
| 326 | if (code === 'ENOENT') { |
| 327 | logForDebugging( |
| 328 | `[TeammateMailbox] markMessagesAsRead: file does not exist at ${inboxPath}`, |
| 329 | ) |
| 330 | return |
| 331 | } |
| 332 | logForDebugging( |
| 333 | `[TeammateMailbox] markMessagesAsRead FAILED for ${agentName}: ${error}`, |
| 334 | ) |
| 335 | logError(error) |
| 336 | } finally { |
no test coverage detected