Desktop agent capture and sync
The desktop agent is the local runtime that turns a user's tracked work into time logs, activity data, and optional screenshots. Its Electron main process owns the OS-facing work, keeps a local copy while the server is unavailable, and sends accepted data to the Gauzy API when the user is signed in and the service is reachable. This page complements desktop time tracking and relates to the shared Electron model in Electron desktop applications and the common desktop services in desktop shared libraries.
Main-process boundary
Runtime coordination
The Electron main process is the control layer between the Angular user interface, the operating system, local storage, and the Gauzy server. apps/…/index.ts creates the single application instance, protocol handling, trusted navigation rules, and provider sign-in handoff. apps/…/app.ts then prepares local storage, windows, the tray, authentication, tracking, and power events. The renderer uses the preload bridge and IPC instead of directly owning these OS actions.
The process uses a shared event channel to connect independent concerns. A timer action, authentication change, power event, or capture result becomes a main-process event. IPC handlers also let the renderer request changes to setup, authentication, tracking, settings, and synchronization views. The event handler then updates tracking, windows, tray state, or shutdown behavior without making those parts call each other directly. This keeps the always-running agent active even when no full application window is open.
Startup also enables shared logging and error reporting. A main-process error can be recorded and shown to the user without making the renderer responsible for recovery.
References: apps/…/index.ts• L18-71 apps/…/index.ts• L87-95 apps/…/index.ts• L8-15 apps/…/app.ts• L290-373 apps/…/ipcMain.ts• L109-248 apps/…/event-handler.ts• L222-261 apps/…/logger.ts• L8-45
Desktop update channel
The desktop update channel keeps the installed agent aligned with published desktop releases. The updater selects a stable or prerelease channel and a package for the running platform, then gives Electron the release feed for download and install. The GitHub-backed channel resolves the current release tag before each update check, so a long-running agent can see a release published after startup.
The release lookup has a bounded wait and keeps the last usable tag when GitHub cannot answer. If no tag has been resolved, it falls back to the running version's v-prefixed tag. A release is preferred only when its platform update metadata is available. The local update strategy remains separate from the GitHub lookup, which lets local development or distribution use its own update source.
References: packages/…/cdn-update.ts• L8-14 packages/…/cdn-update.ts• L30-73 packages/…/github-cdn.ts• L5-25 packages/…/desktop-updater.spec.ts• L151-276
Application state
The agent has three broad states: not set up, set up but not authenticated, and ready for tracking. Setup selects the server and writes local configuration. Authentication adds the user context needed for tenant and organization scope. Only the ready state starts the activity consumer and capture flow.
The local store holds setup, account, application, and selected project context. The main process also creates the local database and a screenshot directory before it starts the normal application flow. A splash step checks platform permissions and refreshes the employee policy when the server is available. These local records let the agent restore its state and protect captured work from short network outages.
References: apps/…/app.ts• L139-200 apps/…/app.ts• L233-241 apps/…/splash.ts• L10-49 apps/…/util.ts• L87-142 apps/…/main.ts• L87-134
Authentication and policy
Account session
Authentication is both a sign-in gate and a data scope. The agent reads a stored access token and its refresh token from local storage. It checks the server when the network is available. If the server cannot be reached, it keeps the local session so that tracking can continue and synchronization can wait. If an authenticated request receives an unrecoverable 401, the agent clears the active session path and raises a logout event.
Every API request carries the bearer token and the tenant and organization identifiers from the signed-in employee. The same identifiers are passed to the capture and queue workers so that local records cannot lose their account scope while they wait for upload. A successful login stores the user context, confirms the employee settings, and starts the tracking workers; logout reverses that path and stops them. Concurrent unauthorized requests share one refresh operation because the server can rotate refresh tokens. A second unauthorized response after that refresh ends the session path. Requests that opt into a deadline apply that limit to the initial call, the shared refresh, and the retry. The renderer also starts proactive token refresh when a stored token pair is available.
Provider sign-in follows the same authenticated path as password and e-mail-code sign-in. The provider first supplies an access token. The agent checks that the token names an employee, then requests a workspace-scoped access and refresh token pair. If the address has several workspaces, the sign-in window sends a one-time code to the workspace picker through navigation state. The selected workspace completes through the agent's AUTH_SUCCESS channel, not the timer application's lower-case channel.
References: apps/…/auth.ts• L11-45 apps/…/ipcMain.ts• L167-223 apps/…/api.ts• L173-200 apps/…/api.ts• L341-409 apps/…/api.ts• L459-547 apps/…/app.component.ts• L50-75 apps/…/app.component.ts• L119-249 apps/…/app.component.social-sign-in.spec.ts• L78-151 apps/…/api.spec.ts• L46-159
Employee policy
The server supplies employee settings that control what the agent may capture and how the user may control it. The agent maps those settings into local application settings after sign-in and refreshes them during use. The policy covers keyboard and mouse tracking, screenshot capture, display scope, random screenshot timing, logout, and application exit.
Screen capture requires approval from both the employee and the employee's organization. The agent does not capture before it has read the organization setting, and a later organization change can turn capture off or on during a running session. This policy is separate from the local user's display preferences. For example, a local notification preference changes how the agent reports a screenshot, while the employee and organization policy decides whether the screenshot may be captured at all. The mapping also gives capture and tray behavior safe disabled defaults when the server does not provide optional capture flags.
References: apps/…/util.ts• L21-31 apps/…/util.ts• L166-210 apps/…/event-handler.ts• L120-140 apps/…/pull-activities.ts• L621-644 apps/…/pull-activities.ts• L740-755 apps/…/util.spec.ts• L42-108
Screenshot frequency
The organization controls the normal screenshot period through its timer settings. The agent reads that value from the signed-in employee's organization, stores it as the local capture period, and applies it to a running capture schedule. A usable organization value applies even when the organization does not enforce the setting because the agent does not offer a local period selector.
The agent reads the setting when tracking starts or resumes and refreshes it every 15 minutes during a running session. The request has a 30-second deadline, and a response is used only when it still belongs to the signed-in account, organization, and tracking session. An absent or unreadable value leaves the current period in place; a missing local period uses a five-minute default.
References: apps/…/api.ts• L358-371 apps/…/api.spec.ts• L46-106 apps/…/screenshot-interval.ts• L13-53 apps/…/util.ts• L212-227 apps/…/pull-activities.ts• L50-57 apps/…/pull-activities.ts• L583-669 apps/…/pull-activities.spec.ts• L468-629
Capture pipeline
Tracking session
PullActivities is the capture worker. Despite its name, it primarily pulls activity from the local operating system into local records; it does not pull activity from the server. A tracking session starts a local timer, enables the configured keyboard and mouse listener, observes the active application or URL, and records active or idle periods.
The worker creates a local timer record and queues its start or stop state before it flushes each activity slice to local storage. A record contains the time range, keyboard and mouse measures, active-window data, AFK duration, account scope, selected project context, and any screenshot paths. The local record remains available until its server time slot succeeds. Idle time is recorded without a screenshot and is split into bounded periods so that a long idle span remains usable as activity data.
The capture schedule uses the organization period or its local fallback and can add random screenshot timing. Tracking transitions are serialized so that a stop, start, sleep, or wake-up cannot attach activity to the wrong timer. A transition that waits too long yields to the next transition while the local work remains durable.
References: apps/…/pull-activities.ts• L152-215 apps/…/pull-activities.ts• L264-348 apps/…/pull-activities.ts• L462-526 apps/…/pull-activities.ts• L583-669 apps/…/pull-activities.ts• L775-819 apps/…/pull-activities.ts• L844-877 apps/…/pull-activities.spec.ts• L188-465
Screen capture
Screen capture is an optional part of an active time slice. When both employee and organization policy allow it, the agent captures either all displays or one display chosen from the active pointer location. It keeps a small preview for user feedback and writes the full image to the local screenshot area. The activity record stores the local image path rather than embedding the image in the activity payload.
For active-only capture, the agent first matches the active display by its reported display ID. Some Linux systems do not provide that ID, so the agent falls back to the display at the active display's index or to the first available display. If pointer detection finds no display, the agent uses the primary display and its index. This keeps active-only capture limited to one display instead of silently producing no screenshot or capturing every display.
The preview can appear as a native notice or as a short-lived capture window. The full image is uploaded only after its time slot has a server identifier. A successful upload removes the temporary file. A failed upload keeps enough local state for the screenshot retry path to try again.
The capture path depends on platform permission. On macOS, activity tracking waits for trusted accessibility access. A user can still run the agent while that permission is pending, but keyboard, mouse, and related activity collection does not start until the permission is granted.
References: apps/…/screenshot.ts• L29-87 apps/…/util.ts• L107-131 apps/…/util.ts• L184-210 apps/…/pull-activities.ts• L690-757 apps/…/pull-activities.ts• L775-807 apps/…/app.ts• L203-231 apps/…/splash.ts• L10-18
Capture and sync flow
The following flow shows how a tracking session moves from local capture to server records and then to screenshot upload.
flowchart LR
A[Tracking session] --> B[OS activity and active window data]
A --> C[Optional screen capture]
B --> D[Local activity record]
C --> D
D --> E[Time-slot queue]
E --> F[Create server time slot]
F --> G[Upload linked screenshots]
G --> H[Mark local records complete]
E -. network or API failure .-> I[Persistent retry queue]
I --> F
References: apps/…/pull-activities.ts• L775-818 apps/…/push-activities.ts• L737-809
Queue and synchronization model
Activity queues
The agent uses durable local queues for three data types: timers, activity time slots, and screenshots. Each type has a normal lane and a retry lane. The queues are backed by local storage, so a process restart or a temporary server outage does not require the user to repeat the work.
WorkerQueue connects these lanes to the push worker and to the audit view. A queue item moves through waiting, running, succeeded, or failed states. The dashboard can show these transitions through the audit queue, which gives support staff a view of work that is still waiting or needs attention.
References: apps/…/worker-queue.ts• L17-69 packages/…/desktop-queue.ts• L10-62 apps/…/audit-queue.ts• L25-117
Push worker
PushActivities drains local records and maps them to the server's time, activity, and screenshot contracts. It sends timer start and stop changes, creates time slots from keyboard, mouse, active-window, and AFK data, and uploads screenshots as files. A time slot is marked locally after its server record is created; linked screenshots then run as separate queue jobs. Local activity is removed after that time-slot handoff and is not removed when the server call fails.
The worker also repairs common state gaps. It can create a time log when a timer was recorded locally but has no remote log, update a known remote log when a stop arrives late, and move failed work into a retry lane. A selected task is moved to an in-progress state when timer work reaches the server. Before timer or activity work is sent, the worker compares stored ownership with the signed-in user, tenant, and organization. A row that belongs to another user stays local instead of being sent under the current session.
References: apps/…/push-activities.ts• L87-177 apps/…/push-activities.ts• L253-337 apps/…/push-activities.ts• L443-467 apps/…/push-activities.ts• L572-735 apps/…/push-activities.ts• L737-809
Pull worker
SyncManager finds local records that are not synchronized and turns them back into queue jobs. It checks timers, activity records, and screenshots in that order. A first check runs shortly after the worker starts. Later checks run as the queue worker ticks, which allows records created during an outage to re-enter the normal upload flow.
The queue system checks both general network access and the configured server. When either check fails, retry lanes pause. When both checks pass again, the retry lanes resume. This separates local capture from server availability and prevents repeated failed requests from becoming the main tracking path.
References: apps/…/sync-manager.ts• L6-49 apps/…/sync-manager.ts• L52-131 packages/…/desktop-queue.ts• L319-357
Tray and window behavior
Tray control surface
The tray is the agent's persistent control surface. It exposes the dashboard, settings, timer start and stop actions, help, plug-ins, and exit. It also shows the current tracking, AFK, keyboard and mouse, and network state without requiring the dashboard window to stay open.
Main-process events update the tray title and menu. Timer state controls which timer action is enabled. The tray also states whether the running agent captures time only, screenshots, or input activity. Employee policy controls whether exit is visible. This makes the tray a live summary of both local activity state and data collection policy.
References: apps/…/tray.ts• L53-163 apps/…/tray.ts• L182-225 apps/…/tray-notify.ts• L28-68 apps/…/event-handler.ts• L120-140
Window lifecycle
The window manager creates only the views needed for the current state. Setup and authentication windows guide first use. Settings and the dashboard open from the tray. The always-on view stays visible when enabled. A short-lived screen capture window presents a preview without taking focus from the user's work.
Closing a normal view hides or destroys that view rather than stopping the agent. The process remains available through the tray. A real exit first stops tracking and queue work, unless employee policy requires a confirmation dialog. macOS also keeps the application process available when its windows close.
The capture preview has one owner for its close and auto-hide behavior. A close-button action returns to the window manager, which cancels the current auto-hide timer before destroying the preview. A preview that closes or is replaced while it is still loading is not shown later.
References: apps/…/window-manager.ts• L30-69 apps/…/window-manager.ts• L118-193 apps/…/window-manager.ts• L239-311 apps/…/window-manager.spec.ts• L46-96 apps/…/screen-capture-window.ts• L1-33 apps/…/screen-capture-window.spec.ts• L21-50 apps/…/event-handler.ts• L199-220
Power transitions
The main process treats suspend, resume, and shutdown as tracking events. Suspend pauses only tracking that was running and keeps its timer open. Resume records the sleep as idle activity in bounded chunks, then resumes that same tracking session. A person who stopped tracking before sleep stays stopped, and a repeated wake-up does not record the same sleep twice.
If the person stops tracking while sleep is paused or being recorded, the stop cancels the pending resume and closes the paused timer at the recorded boundary. This keeps idle activity from extending past the timer that owns it. Shutdown stops the timer path and gives local queues a chance to preserve the last known state.
References: apps/…/app.ts• L259-287 apps/…/event-handler.ts• L156-171 apps/…/pull-activities.ts• L154-218 apps/…/pull-activities.ts• L462-526 apps/…/pull-activities.ts• L844-877 apps/…/pull-activities.spec.ts• L188-344
Change checklist
When these files or sections are changed, remember to consider these:
- Provider sign-in event names must stay aligned across the navigation guard, sign-in window, workspace picker, renderer, and main-process authentication path.
- A provider access token must still become a workspace-scoped token pair when possible, while the access-token-only fallback remains usable.
- Authentication shape changes must update API headers, worker account context, token refresh, concurrent refresh handling, proactive refresh, and logout handling together.
- Organization screenshot settings must keep the API query, request deadline, account and session checks, local period mapping, and running timer update aligned.
- Employee and organization capture flags must keep the capture gate, local settings, periodic refresh, and tray monitoring summary aligned.
- Active-only display capture must keep its display-ID match, primary-display fallback, and single-display fallback aligned with the active display index supplied by screen selection.
- Timer, time-slot, and screenshot queue names must match their enqueue paths, filters, retry paths, and worker handlers.
- Local record fields such as synchronization flags, remote IDs, owner identifiers, and screenshot paths must still support the order of time-slot creation followed by image upload.
- IPC channel or window-route changes must update both the main-process handler and the renderer-side bridge or caller.
- Screenshot notification close paths must keep the window manager as the owner of destruction and auto-hide cancellation.
- Sleep, wake, stop, and start changes must preserve ordered transitions, the same-timer resume rule, and cancellation of sleep activity after a person stops.
- Power and network state changes must preserve the distinction between pausing capture and retaining unsent local work.
- Desktop release changes must keep the channel setting, release tag format, platform update-info file, and Electron feed URL aligned across the release lookup and update strategy.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No stored token at startup | The agent opens sign-in and does not start authenticated tracking. | checkUserAuthentication opens the auth window before the activity consumer starts. |
| Provider token names no employee | Provider sign-in cannot create an employee session. | The renderer checks the decoded employee identity before storing the session. |
| A provider account has several workspaces | Sign-in pauses at workspace choice instead of selecting an unsafe default. | The sign-in window passes a one-time code to the workspace picker through navigation state. |
| Invalid token and failed refresh | The API call stops and the main process emits the logout flow. | The API service shares concurrent refresh work, bounds the refresh wait, retries once, then handles an unauthorized response. |
| Organization settings request hangs | A running session could keep waiting and miss later setting refreshes. | The settings request and its token-refresh path have a bounded deadline. |
| Settings response belongs to another account, organization, or tracking session | A stale response must not change the active screenshot period or capture permission. | The capture worker checks the request scope before applying the response. |
| A queued row belongs to another signed-in user, tenant, or organization | The row is not sent under the current account and remains local. | The push worker compares stored owner fields with the current auth scope before syncing. |
| macOS accessibility permission is not granted | Keyboard and mouse activity does not start. | The main process checks the permission and waits before starting the listener. |
| Server or network is unavailable | New work stays local and retry queues remain paused until service checks pass. | Durable queues and the online/service checks separate capture from upload. |
| Employee or organization screenshot policy is disabled or not yet known | The time slice still records activity, but no screenshot is captured. | Capture requires both effective policy flags to be true. |
| Active display ID is unavailable on a Linux system | Active-only mode cannot identify the display by ID. | The capture path uses the active display index or the first source and still selects one display. |
| Pointer display detection finds no display | Active-only capture could lose its display target. | The display helper falls back to the primary monitor and its display index. |
| Screen capture fails | The current capture produces no screenshot while the tracking path can continue. | The capture operation catches the error and returns an empty result. |
| Device wakes after tracking was manually stopped | The agent must not restart work that the person stopped. | The worker resumes only a sleep pause that it created. |
| Person stops while sleep activity is being recorded | A late wake-up could add activity after the timer stopped. | Ordered transitions cancel the pause and close the timer at the recorded boundary. |
| Temporary screenshot file is missing or unreadable | That image cannot be uploaded and its local image state is removed from the failed path. | Upload checks file existence and read access before sending the file. |
| Capture preview closes while it is loading or before auto-hide runs | A stale wait or timer could show or destroy the next preview. | The window manager checks replacement state and owns close and timeout cleanup. |
| Exit is forbidden by employee policy | The agent stays open instead of silently stopping tracking. | The exit event shows a confirmation dialog unless policy or setup state allows exit. |
| GitHub release lookup is rate-limited or unreachable | The updater keeps the last resolved release or uses the running version instead of building a malformed feed URL. | The lookup has a timeout, validates the response shape, and falls back to a v-prefixed tag. |
| A release is published before its platform update metadata is available | The updater can remain on an older release until the package metadata is ready. | Release selection prefers a matching platform update-info file. |
Key files
| File | Why to read it |
|---|---|
| apps/…/index.ts | Application entry point, single-instance behavior, deep-link routing, navigation safety, and provider sign-in handoff. |
| apps/…/app.component.ts | Renderer-side authentication events, workspace selection, session storage, and proactive token refresh. |
| apps/…/app.ts | Startup order, local database preparation, authentication handoff, tracking start, and power events. |
| apps/…/ipcMain.ts | Renderer-to-main contracts for authentication, tracking, settings, timer state, offline state, and queue audit data. |
| apps/…/splash.ts | Startup readiness checks for platform permissions and the latest employee policy. |
| apps/…/logger.ts | Main-process logging, error reporting, and user-facing error recovery. |
| apps/…/event-handler.ts | Central state changes for tracking, logout, pause/resume, tray updates, and exit. |
| apps/…/auth.ts and apps/…/api.ts | Session checks, token refresh, request scope and deadlines, timer APIs, time-slot APIs, and screenshot upload. |
| apps/…/screenshot-interval.ts and apps/…/util.ts | Organization screenshot period mapping, combined capture policy, display fallback, and shared desktop-agent settings. |
| apps/…/pull-activities.ts | Local capture session, AFK handling, screenshot scheduling, organization setting refresh, and sleep transitions. |
| apps/…/push-activities.ts | Conversion of local records into server data, account-scope protection, and recovery of failed timer, activity, and screenshot work. |
| apps/…/worker-queue.ts and apps/…/sync-manager.ts | Queue wiring, unsynchronized-record discovery, retry routing, and periodic recovery. |
| packages/…/desktop-queue.ts | Shared durable queue behavior and network-aware retry control. |
| apps/…/window-manager.ts and apps/…/tray.ts | Window lifecycle, screenshot preview ownership, and the persistent tray control surface. |
| apps/…/screen-capture-window.ts | Screenshot preview callbacks and close behavior. |
| apps/…/screenshot.ts | Display selection, active-display fallback, image capture, and local screenshot files. |
| packages/…/cdn-update.ts | Release channel lookup, fallback behavior, and platform update metadata selection. |
| packages/…/github-cdn.ts | GitHub feed construction and release re-resolution before update checks. |
| packages/…/desktop-updater.spec.ts | Contract coverage for release lookup, platform assets, recurring checks, manual checks, and local update isolation. |