* Write a partial message to disk.
(workspaceId: string, message: MuxMessage)
| 1176 | * Write a partial message to disk. |
| 1177 | */ |
| 1178 | async writePartial(workspaceId: string, message: MuxMessage): Promise<Result<void>> { |
| 1179 | return this.fileLocks.withLock(workspaceId, async () => { |
| 1180 | try { |
| 1181 | const workspaceDir = this.config.getSessionDir(workspaceId); |
| 1182 | await ensurePrivateDir(workspaceDir); |
| 1183 | const partialPath = this.getPartialPath(workspaceId); |
| 1184 | |
| 1185 | const partialMessage: MuxMessage = { |
| 1186 | ...message, |
| 1187 | metadata: { |
| 1188 | ...message.metadata, |
| 1189 | partial: true, |
| 1190 | }, |
| 1191 | }; |
| 1192 | |
| 1193 | // Atomic write: writes to temp file then renames, preventing corruption |
| 1194 | // if app crashes mid-write (prevents "Unexpected end of JSON input" on read) |
| 1195 | await writeFileAtomic(partialPath, JSON.stringify(partialMessage, null, 2)); |
| 1196 | return Ok(undefined); |
| 1197 | } catch (error) { |
| 1198 | const errorMessage = getErrorMessage(error); |
| 1199 | return Err(`Failed to write partial: ${errorMessage}`); |
| 1200 | } |
| 1201 | }); |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * Delete the partial message file for a workspace. |