Imported from iWorkforces/GogChat (
src/main/utils/ipc/AGENTS.md). Install upstream withnpx skills add iWorkforces/GogChat --skill ipc. Copyright stays with the author.
IPC Utilities Guide
Parent: ../AGENTS.md
This directory owns the main-side IPC safety pipeline. Channel contracts and validation rules are packaging-arch independent.
Pipeline
Every handler should follow:
- Rate limit.
- Validate payload.
- Deduplicate only if safe.
- Handle.
- Catch/log typed failures.
Prefer defineIPC({ kind: 'on' | 'reply' | 'invoke' }) for new handlers. createSecure*Handler in ipcHelper.ts is @deprecated and remains for older tests. Live features (handleNotification, inOnline, passkeySupport) already use defineIPC. Do not add ad-hoc ipcMain.handle / ipcMain.on calls.
Components
defineIPC.ts- current handler factory.ipcHelper.ts- legacy wrappers + shared option types.rateLimiter.ts- per-channel token bucket with 1s windows and stale cleanup. Keys are${channel}:sender:${id}whenevent.sender.idis present so multi-account senders are isolated.ipcDeduplicator.ts- short promise sharing, default 100ms.ipcDeduplicationPatterns.ts- key functions for safe dedup cases.ipcFastPath.ts- sync one-way hotsendchannels only; never forinvoke.ipcCommonValidators.ts- reusable payload validation.benignLogFilter.ts- suppresses expected noisy renderer/subframe errors.
Latency sampling
- IPC latency samples (when recorded) are optional export fields and remain warn-only in the perf budget until a real producer and baseline exist.
- Do not make IPC latency a gated CI metric without that baseline.
Channel contract
- Channel names live in
src/shared/constants.tsunderIPC_CHANNELS. - Payload/response types live in
src/shared/types/ipc.tsand related domain types. - Preload exposes narrow methods from
src/shared/types/bridge.ts. - Never hardcode a channel string.
Existing channel groups
- Renderer → main (
IPC_CHANNELS):UNREAD_COUNT,FAVICON_CHANGED,NOTIFICATION_SHOW,NOTIFICATION_CLICKED,CHECK_IF_ONLINE,PASSKEY_AUTH_FAILED. - Main → renderer:
SEARCH_SHORTCUT,ONLINE_STATUS. - Notification show handlers must validate payloads (including icon allowlist via shared validators), then use
nativeNotification/notificationFocus— not ad-hocnew Notificationoutside those helpers (except the permission probe innotificationAccess).
Anti-patterns
- No raw
ipcMainregistrations without validation and catch handling. - No dedup for mutating or non-idempotent operations. Online checks must not use
deduplicate: true— two senders need isolated probes. Do not put adefineIPCrateLimitonCHECK_IF_ONLINE; a 1/s cap would reject a same-sender replacement before supersession can abort the older probe.inOnlinekeeps one abortable probe per sender and appliesONLINE_FETCH_MIN_INTERVAL_MSafter the handler runs so a tight loop cannot start unboundedgenerate_204fetches. - No raw
ipcRendererexposure from preload. defineIPC.tsis included in Vitest coverage.defineIPC.test.tscovers on/reply/invoke, sender-scoped rate limits, silent drops, channel and payload dedup, and IPCError rethrow from invoke.