Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/react-compiler-bailout-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@
3 src/renderer/src/hooks/linear-issue-committed-state.ts
1 src/renderer/src/hooks/use-audio-capture.ts
1 src/renderer/src/hooks/use-bundled-skill-install.ts
1 src/renderer/src/hooks/use-monaco-theme.ts
2 src/renderer/src/hooks/useDetectedAgents.ts
2 src/renderer/src/hooks/useGitHubTaskItems.ts
2 src/renderer/src/hooks/useImmediateMutation.ts
Expand Down
90 changes: 90 additions & 0 deletions src/main/runtime/rpc/server/pairing/device-registry-sightings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { withDeviceSighting, type DeviceSightingFraming } from './device-sighting'
import { DeviceRegistryStore } from './device-registry-store'
import type { DeviceEntry } from './device-entry'
import type { NetworkSourceClass } from '../../network-source-class'

// Coalesce reconnect bookkeeping: each Windows save otherwise spawns synchronous ACL PowerShell twice.
const LAST_SEEN_FLUSH_DELAY_MS = 250
// Unpersisted, a restart would refuse and prune a signed-in row as an expired unused offer.
const FIRST_SIGHTING_RETRY_MS = 60_000

/** Device sightings: last-seen bookkeeping, persisted inline on a first sighting and coalesced after. */
export class DeviceRegistrySightings extends DeviceRegistryStore {
/** First sightings this session, kept even when their persist failed. */
protected readonly seenDeviceIds = new Set<string>()
private firstSightingRetryPending = false

updateLastSeen(
deviceId: string,
sourceClass?: NetworkSourceClass,
framing?: DeviceSightingFraming
): void {
// Persist first sighting inline or rotation could delete a paired row still pending on disk.
this.seenDeviceIds.add(deviceId)
const seenAt = Date.now()
const sight = ({ offerExpiresAt: _paired, ...device }: DeviceEntry): DeviceEntry => {
const firstSighting =
device.ticket === true && device.lastSeenAt === 0
? { ...device, ticketFirstSeenAt: seenAt }
: device
return withDeviceSighting(seen(firstSighting, seenAt, sourceClass), seenAt, framing)
}
try {
this.replaceRow(deviceId, sight)
} catch (error) {
// Keep the sighting in memory so any later save writes it, and retry until one lands.
this.devices = this.devices.map((d) => (d.deviceId === deviceId ? sight(d) : d))
this.firstSightingRetryPending = true
this.scheduleLastSeenFlush(FIRST_SIGHTING_RETRY_MS)
throw error
}
}

/** Defer repeat sightings to avoid synchronous Windows ACL saves during E2EE auth.
* First sighting must persist inline so pending-offer rotation cannot revoke a paired device. */
updateLastSeenDeferred(
deviceId: string,
sourceClass?: NetworkSourceClass,
framing?: DeviceSightingFraming
): void {
const existing = this.devices.find((d) => d.deviceId === deviceId)
if (!existing || existing.lastSeenAt === 0) {
return existing ? this.updateLastSeen(deviceId, sourceClass, framing) : undefined
}
const seenAt = Date.now()
this.devices = this.devices.map((device) =>
device === existing
? withDeviceSighting(seen(device, seenAt, sourceClass), seenAt, framing)
: device
)
this.scheduleLastSeenFlush(LAST_SEEN_FLUSH_DELAY_MS)
}

private scheduleLastSeenFlush(delayMs: number): void {
if (!this.pendingLastSeenFlush) {
this.pendingLastSeenFlush = setTimeout(() => this.flushPendingLastSeen(), delayMs)
this.pendingLastSeenFlush.unref?.()
}
}

flushPendingLastSeen(): void {
if (!this.pendingLastSeenFlush) {
return
}
this.cancelPendingLastSeenFlush()
// Failed bookkeeping must not terminate the runtime.
try {
this.devices = this.save(this.devices)
this.firstSightingRetryPending = false
} catch (error) {
console.error('[runtime] Failed to persist device lastSeen:', error)
if (this.firstSightingRetryPending) {
this.scheduleLastSeenFlush(FIRST_SIGHTING_RETRY_MS)
}
}
}
}

function seen(device: DeviceEntry, seenAt: number, sourceClass?: NetworkSourceClass): DeviceEntry {
return { ...device, lastSeenAt: seenAt, ...(sourceClass ? { lastSourceClass: sourceClass } : {}) }
}
89 changes: 2 additions & 87 deletions src/main/runtime/rpc/server/pairing/device-registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Per-device revocable tokens isolate compromises; persist with hardened metadata-style permissions.
import { withDeviceSighting, type DeviceSightingFraming } from './device-sighting'
import type { DeviceScope } from '../../../../../shared/runtime-session-contracts'
import { mintTokenDevice } from './device-credential'
import { planDeviceKeyFork } from './device-key-fork'
Expand All @@ -10,7 +9,7 @@ import {
promoteExchangedTicket
} from './pairing-ticket-exchange'
import { planIdleEphemeralPrune } from './ephemeral-device-rows'
import { DeviceRegistryStore } from './device-registry-store'
import { DeviceRegistrySightings } from './device-registry-sightings'
import type { RelayDeviceBinding } from '../../../mobile-cloud-relay/relay-revoke-outbox'
import type { MobilePairingConnectionMode } from '../../../../../shared/remote-runtime/pairing/mobile-pairing-connection-mode'
import type { RuntimePairingReach } from '../../../../../shared/runtime-pairing-reach'
Expand Down Expand Up @@ -38,18 +37,8 @@ import {
type TokenDeviceEntry
} from './device-entry'
import type { MobilePushRegistration } from '../../../../../shared/mobile-push-contract'
import type { NetworkSourceClass } from '../../network-source-class'

// Coalesce reconnect bookkeeping: each Windows save otherwise spawns synchronous ACL PowerShell twice.
const LAST_SEEN_FLUSH_DELAY_MS = 250
// Unpersisted, a restart would refuse and prune a signed-in row as an expired unused offer.
const FIRST_SIGHTING_RETRY_MS = 60_000

export class DeviceRegistry extends DeviceRegistryStore {
/** First sightings this session, kept even when their persist failed. */
private readonly seenDeviceIds = new Set<string>()
private firstSightingRetryPending = false

export class DeviceRegistry extends DeviceRegistrySightings {
addDevice(
name: string,
scope: DeviceScope = 'mobile',
Expand Down Expand Up @@ -295,78 +284,4 @@ export class DeviceRegistry extends DeviceRegistryStore {
this.adoptExternalEdits()
return findPrincipalDevice(this.devices, principal)
}

updateLastSeen(
deviceId: string,
sourceClass?: NetworkSourceClass,
framing?: DeviceSightingFraming
): void {
// Persist first sighting inline or rotation could delete a paired row still pending on disk.
this.seenDeviceIds.add(deviceId)
const seenAt = Date.now()
const sight = ({ offerExpiresAt: _paired, ...device }: DeviceEntry): DeviceEntry => {
const firstSighting =
device.ticket === true && device.lastSeenAt === 0
? { ...device, ticketFirstSeenAt: seenAt }
: device
return withDeviceSighting(seen(firstSighting, seenAt, sourceClass), seenAt, framing)
}
try {
this.replaceRow(deviceId, sight)
} catch (error) {
// Keep the sighting in memory so any later save writes it, and retry until one lands.
this.devices = this.devices.map((d) => (d.deviceId === deviceId ? sight(d) : d))
this.firstSightingRetryPending = true
this.scheduleLastSeenFlush(FIRST_SIGHTING_RETRY_MS)
throw error
}
}

/** Defer repeat sightings to avoid synchronous Windows ACL saves during E2EE auth.
* First sighting must persist inline so pending-offer rotation cannot revoke a paired device. */
updateLastSeenDeferred(
deviceId: string,
sourceClass?: NetworkSourceClass,
framing?: DeviceSightingFraming
): void {
const existing = this.getDevice(deviceId)
if (!existing || existing.lastSeenAt === 0) {
return existing ? this.updateLastSeen(deviceId, sourceClass, framing) : undefined
}
const seenAt = Date.now()
this.devices = this.devices.map((device) =>
device === existing
? withDeviceSighting(seen(device, seenAt, sourceClass), seenAt, framing)
: device
)
this.scheduleLastSeenFlush(LAST_SEEN_FLUSH_DELAY_MS)
}

private scheduleLastSeenFlush(delayMs: number): void {
if (!this.pendingLastSeenFlush) {
this.pendingLastSeenFlush = setTimeout(() => this.flushPendingLastSeen(), delayMs)
this.pendingLastSeenFlush.unref?.()
}
}

flushPendingLastSeen(): void {
if (!this.pendingLastSeenFlush) {
return
}
this.cancelPendingLastSeenFlush()
// Failed bookkeeping must not terminate the runtime.
try {
this.devices = this.save(this.devices)
this.firstSightingRetryPending = false
} catch (error) {
console.error('[runtime] Failed to persist device lastSeen:', error)
if (this.firstSightingRetryPending) {
this.scheduleLastSeenFlush(FIRST_SIGHTING_RETRY_MS)
}
}
}
}

function seen(device: DeviceEntry, seenAt: number, sourceClass?: NetworkSourceClass): DeviceEntry {
return { ...device, lastSeenAt: seenAt, ...(sourceClass ? { lastSourceClass: sourceClass } : {}) }
}
29 changes: 18 additions & 11 deletions src/renderer/src/hooks/use-monaco-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import { useCodeEditorThemeStore } from '@/store/code-editor-theme'

const definedThemes = new Set<string>()

type PluginMonacoTheme = NonNullable<ReturnType<typeof useCodeEditorThemeStore.getState>['monaco']>

// Why outside the hook: the React compiler cannot lower dynamic imports inside a hook.
async function definePluginMonacoTheme(theme: PluginMonacoTheme): Promise<void> {
// Why lazy: surfaces that never show an editor must not load Monaco for a theme.
const [monaco, data] = await Promise.all([
import('monaco-editor'),
import('@/lib/monaco-languages/markdown-monaco-theme-rules').then(
({ monacoThemeDataWithMarkdown }) => monacoThemeDataWithMarkdown(theme.data),
// Why: markdown colors are extra; a failed chunk must not keep the theme undefined.
() => theme.data
)
])
monaco.editor.defineTheme(theme.name, data)
definedThemes.add(theme.name)
}

/** The Monaco theme every editor surface uses: the editor or app theme's, else the built-in.
* `isDarkOverride` passes an already resolved app mode; a separate editor theme's base wins. */
export function useMonacoTheme(isDarkOverride?: boolean): string {
Expand All @@ -18,17 +35,7 @@ export function useMonacoTheme(isDarkOverride?: boolean): string {
return
}
let current = true
// Why lazy: surfaces that never show an editor must not load Monaco for a theme.
void Promise.all([
import('monaco-editor'),
import('@/lib/monaco-languages/markdown-monaco-theme-rules').then(
({ monacoThemeDataWithMarkdown }) => monacoThemeDataWithMarkdown(pluginTheme.data),
// Why: markdown colors are extra; a failed chunk must not keep the theme undefined.
() => pluginTheme.data
)
]).then(([monaco, data]) => {
monaco.editor.defineTheme(pluginTheme.name, data)
definedThemes.add(pluginTheme.name)
void definePluginMonacoTheme(pluginTheme).then(() => {
if (current) {
setDefined(new Set(definedThemes))
}
Expand Down
Loading