-
Notifications
You must be signed in to change notification settings - Fork 87
M2: Daemon Recording Control + Deterministic Mapping #8696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jasonnnz
merged 2 commits into
feature/standalone-screen-rec
from
swarm/standalone-screen-rec/task-7
Feb 25, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import * as net from 'node:net'; | ||
| import { v4 as uuid } from 'uuid'; | ||
| import type { RecordingStatus, RecordingOptions } from '../ipc-protocol.js'; | ||
| import { log, findSocketForSession, defineHandlers, type HandlerContext } from './shared.js'; | ||
|
|
||
| // ─── Deterministic maps ────────────────────────────────────────────────────── | ||
| // These ensure stop resolves the exact active recording for a conversation, | ||
| // prevent ambiguous cross-thread stop behavior, and maintain conversation | ||
| // linkage for future file attachment (M4). | ||
|
|
||
| /** Maps recordingId -> conversationId. */ | ||
| const standaloneRecordingConversationId = new Map<string, string>(); | ||
|
|
||
| /** Maps conversationId -> recordingId (active recording). */ | ||
| const recordingOwnerByConversation = new Map<string, string>(); | ||
|
|
||
| // ─── Start ─────────────────────────────────────────────────────────────────── | ||
|
|
||
| /** | ||
| * Initiate a standalone recording for a conversation. | ||
| * Generates a unique recording ID, stores deterministic mappings, and sends | ||
| * a `recording_start` message to the client. | ||
| */ | ||
| export function handleRecordingStart( | ||
| conversationId: string, | ||
| options: RecordingOptions | undefined, | ||
| socket: net.Socket, | ||
| ctx: HandlerContext, | ||
| ): string { | ||
| const recordingId = uuid(); | ||
|
|
||
| const existingRecordingId = recordingOwnerByConversation.get(conversationId); | ||
| if (existingRecordingId) { | ||
| log.warn({ conversationId, existingRecordingId }, 'Overwriting active recording for conversation'); | ||
| standaloneRecordingConversationId.delete(existingRecordingId); | ||
| } | ||
|
|
||
| standaloneRecordingConversationId.set(recordingId, conversationId); | ||
| recordingOwnerByConversation.set(conversationId, recordingId); | ||
|
Jasonnnz marked this conversation as resolved.
|
||
|
|
||
| ctx.send(socket, { | ||
| type: 'recording_start', | ||
| recordingId, | ||
| attachToConversationId: conversationId, | ||
| options, | ||
| }); | ||
|
|
||
| log.info({ recordingId, conversationId }, 'Standalone recording started'); | ||
| return recordingId; | ||
| } | ||
|
|
||
| // ─── Stop ──────────────────────────────────────────────────────────────────── | ||
|
|
||
| /** | ||
| * Stop the active standalone recording for a conversation. | ||
| * Looks up the recording ID from `recordingOwnerByConversation` and sends | ||
| * a `recording_stop` message to the client. | ||
| * | ||
| * Returns the recording ID if a stop was sent, or `undefined` if no active | ||
| * recording was found for the conversation. | ||
| */ | ||
| export function handleRecordingStop( | ||
| conversationId: string, | ||
| ctx: HandlerContext, | ||
| ): string | undefined { | ||
| const recordingId = recordingOwnerByConversation.get(conversationId); | ||
| if (!recordingId) { | ||
| log.debug({ conversationId }, 'No active standalone recording to stop for conversation'); | ||
| return undefined; | ||
| } | ||
|
|
||
| // Look up the socket currently bound to the conversation so we can send | ||
| // the stop command to the correct client connection. | ||
| const socket = findSocketForSession(conversationId, ctx); | ||
| if (!socket) { | ||
| log.warn({ conversationId, recordingId }, 'Cannot send recording_stop: no socket bound to conversation'); | ||
| standaloneRecordingConversationId.delete(recordingId); | ||
| recordingOwnerByConversation.delete(conversationId); | ||
| return undefined; | ||
| } | ||
|
Jasonnnz marked this conversation as resolved.
|
||
|
|
||
| ctx.send(socket, { | ||
| type: 'recording_stop', | ||
| recordingId, | ||
| }); | ||
|
|
||
| log.info({ recordingId, conversationId }, 'Standalone recording stop sent'); | ||
| return recordingId; | ||
| } | ||
|
|
||
| // ─── Status (client → server lifecycle updates) ───────────────────────────── | ||
|
|
||
| function handleRecordingStatus( | ||
| msg: RecordingStatus, | ||
| _socket: net.Socket, | ||
| _ctx: HandlerContext, | ||
| ): void { | ||
| const recordingId = msg.sessionId; | ||
| const conversationId = standaloneRecordingConversationId.get(recordingId) | ||
| ?? msg.attachToConversationId; | ||
|
|
||
| switch (msg.status) { | ||
| case 'started': | ||
| log.info({ recordingId, conversationId }, 'Standalone recording confirmed started by client'); | ||
| break; | ||
|
|
||
| case 'stopped': { | ||
| log.info( | ||
| { recordingId, conversationId, filePath: msg.filePath, durationMs: msg.durationMs }, | ||
| 'Standalone recording stopped — file ready', | ||
| ); | ||
| // Clean up deterministic maps. Full finalization (attaching the file | ||
| // to the conversation as a message) is M4 scope. | ||
| standaloneRecordingConversationId.delete(recordingId); | ||
| if (conversationId) { | ||
| const current = recordingOwnerByConversation.get(conversationId); | ||
| if (current === recordingId) { | ||
| recordingOwnerByConversation.delete(conversationId); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case 'failed': { | ||
| log.warn( | ||
| { recordingId, conversationId, error: msg.error }, | ||
| 'Standalone recording failed', | ||
| ); | ||
| standaloneRecordingConversationId.delete(recordingId); | ||
| if (conversationId) { | ||
| const current = recordingOwnerByConversation.get(conversationId); | ||
| if (current === recordingId) { | ||
| recordingOwnerByConversation.delete(conversationId); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ─── Export handler group ──────────────────────────────────────────────────── | ||
|
|
||
| export const recordingHandlers = defineHandlers({ | ||
| recording_status: handleRecordingStatus, | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.