* Flush any pending partial write and write immediately * Serializes writes to prevent races - waits for any in-flight write before starting new one
(
workspaceId: WorkspaceId,
streamInfo: WorkspaceStreamInfo
)
| 763 | * Serializes writes to prevent races - waits for any in-flight write before starting new one |
| 764 | */ |
| 765 | private async flushPartialWrite( |
| 766 | workspaceId: WorkspaceId, |
| 767 | streamInfo: WorkspaceStreamInfo |
| 768 | ): Promise<void> { |
| 769 | // Wait for any in-flight write to complete first (serialization) |
| 770 | await this.awaitPendingPartialWrite(streamInfo); |
| 771 | |
| 772 | // Clear throttle timer |
| 773 | if (streamInfo.partialWriteTimer) { |
| 774 | clearTimeout(streamInfo.partialWriteTimer); |
| 775 | streamInfo.partialWriteTimer = undefined; |
| 776 | } |
| 777 | |
| 778 | // Start new write and track the promise |
| 779 | streamInfo.partialWritePromise = (async () => { |
| 780 | try { |
| 781 | await this.historyService.writePartial( |
| 782 | workspaceId as string, |
| 783 | this.buildPartialAssistantMessage(streamInfo) |
| 784 | ); |
| 785 | streamInfo.lastPartialWriteTime = Date.now(); |
| 786 | } catch (error) { |
| 787 | log.error("Failed to write partial message:", error); |
| 788 | } finally { |
| 789 | // Clear promise when write completes |
| 790 | streamInfo.partialWritePromise = undefined; |
| 791 | } |
| 792 | })(); |
| 793 | |
| 794 | // Wait for this write to complete |
| 795 | await streamInfo.partialWritePromise; |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * Atomically ensures stream safety by cancelling any existing stream |
no test coverage detected