mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 19:07:41 +00:00
fix(cloud-sync): lint gates (no setInterval, no casts, scroll on the popover body)
This commit is contained in:
@@ -161,12 +161,23 @@ export function installCloudSync(deps: {
|
||||
}
|
||||
}
|
||||
})
|
||||
const beat = setInterval(() => void sync.reconcile(), BEAT_MS)
|
||||
beat.unref?.()
|
||||
void sync.reconcile()
|
||||
// One reconcile per beat, scheduled after the last one finished (never stacked).
|
||||
let beat: ReturnType<typeof setTimeout> | null = null
|
||||
let stopped = false
|
||||
const scheduleBeat = (): void => {
|
||||
if (stopped) {
|
||||
return
|
||||
}
|
||||
beat = setTimeout(() => void sync.reconcile().finally(scheduleBeat), BEAT_MS)
|
||||
beat.unref?.()
|
||||
}
|
||||
void sync.reconcile().finally(scheduleBeat)
|
||||
|
||||
return () => {
|
||||
clearInterval(beat)
|
||||
stopped = true
|
||||
if (beat) {
|
||||
clearTimeout(beat)
|
||||
}
|
||||
offRuntimes()
|
||||
offPower()
|
||||
for (const channel of [CH.getState, CH.setChoice, CH.openFolder, CH.retry, CH.removalPreview]) {
|
||||
|
||||
@@ -160,3 +160,14 @@ export function planSync(input: {
|
||||
}
|
||||
return { download, remove, editedHere, skippedLarge, overLimit, totalBytes, totalFiles }
|
||||
}
|
||||
|
||||
/**
|
||||
* Safety net for anything the watch missed. The VM's watcher can miss a directory created along
|
||||
* with its parent (`mkdir -p .context/shots`), so small folders are re-checked often (a few
|
||||
* calls each) and large mirrors less often: 30 ms per file, between 30 s and 10 min, and never
|
||||
* more than 5% of the time (a slow pass, e.g. on an image without files.statMany, waits longer).
|
||||
*/
|
||||
export const SAFETY_TICK_MS = 30_000
|
||||
export function safetyIntervalMs(files: number, lastPassMs = 0): number {
|
||||
return Math.min(10 * 60_000, Math.max(SAFETY_TICK_MS, files * 30, lastPassMs * 20))
|
||||
}
|
||||
|
||||
@@ -30,6 +30,11 @@ export class SyncRemoteError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
function isNotFound(error: unknown): boolean {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
return /ENOENT|not.?found|no such/i.test(message)
|
||||
}
|
||||
|
||||
/** Transport answers for a sandbox that is asleep, waking or unreachable right now. */
|
||||
const WAITING_CODES = new Set([
|
||||
'runtime_environment_asleep',
|
||||
@@ -45,11 +50,8 @@ export function isWaitingError(error: unknown): boolean {
|
||||
}
|
||||
|
||||
export function errorCode(error: unknown): string | null {
|
||||
if (error && typeof error === 'object' && 'code' in error) {
|
||||
const code = (error as { code: unknown }).code
|
||||
return typeof code === 'string' ? code : null
|
||||
}
|
||||
return null
|
||||
const code: unknown = error && typeof error === 'object' ? Reflect.get(error, 'code') : undefined
|
||||
return typeof code === 'string' ? code : null
|
||||
}
|
||||
|
||||
const WorktreeRows = z.looseObject({
|
||||
@@ -125,7 +127,7 @@ async function listContext(
|
||||
)
|
||||
} catch (error) {
|
||||
// No .context folder is the common case, not a failure.
|
||||
if (depth === 0 && /ENOENT|not.?found|no such/i.test(String((error as Error)?.message))) {
|
||||
if (depth === 0 && isNotFound(error)) {
|
||||
return
|
||||
}
|
||||
throw error
|
||||
@@ -292,7 +294,7 @@ async function statOneByOne(
|
||||
}
|
||||
} catch (error) {
|
||||
// Deleted between listing and stat: simply not in the manifest.
|
||||
if (!/ENOENT|not.?found|no such/i.test(String((error as Error)?.message))) {
|
||||
if (!isNotFound(error)) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { CloudSyncView } from '../../shared/cloud-sync/cloud-sync-contract'
|
||||
import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes'
|
||||
import { createFakeVm, VM_ROOT } from './cloud-sync-fake-vm.test-support'
|
||||
import { createFakeVm } from './cloud-sync-fake-vm.test-support'
|
||||
import { createCloudSyncService } from './cloud-sync-service'
|
||||
import { createCloudSyncStore } from './cloud-sync-store'
|
||||
|
||||
@@ -25,10 +25,7 @@ function runtime(status: EphemeralVmRuntimeRecord['status']): EphemeralVmRuntime
|
||||
status,
|
||||
cleanupStatus: 'not_started',
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
recipeResult: {
|
||||
connection: { type: 'orca-server', pairingCode: 'x', projectRoot: VM_ROOT }
|
||||
} as unknown as EphemeralVmRuntimeRecord['recipeResult']
|
||||
updatedAt: 1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +201,9 @@ export function createCloudSyncService(deps: CloudSyncServiceDeps) {
|
||||
do {
|
||||
again = false
|
||||
await reconcileOnce().catch((error: unknown) =>
|
||||
log(`[cloud-sync] reconcile failed: ${error instanceof Error ? error.message : error}`)
|
||||
log(
|
||||
`[cloud-sync] reconcile failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
)
|
||||
)
|
||||
} while (again && !disposed)
|
||||
} finally {
|
||||
|
||||
@@ -4,7 +4,8 @@ import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { CloudSyncState } from '../../shared/cloud-sync/cloud-sync-contract'
|
||||
import { createFakeVm, VM_ROOT, type FakeVm } from './cloud-sync-fake-vm.test-support'
|
||||
import { createCloudSyncSession, safetyIntervalMs } from './cloud-sync-session'
|
||||
import { safetyIntervalMs } from './cloud-sync-plan'
|
||||
import { createCloudSyncSession } from './cloud-sync-session'
|
||||
import { createCloudSyncStore, type CloudSyncChoice } from './cloud-sync-store'
|
||||
|
||||
const dirs: string[] = []
|
||||
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
import { isContextPath, isSafeSyncPath } from './cloud-sync-paths'
|
||||
import {
|
||||
emptySyncState,
|
||||
safetyIntervalMs,
|
||||
SAFETY_TICK_MS,
|
||||
limitsFor,
|
||||
limitsInfo,
|
||||
planSync,
|
||||
@@ -38,16 +40,6 @@ import {
|
||||
|
||||
export type { WatchSubscribe, WatchSubscription }
|
||||
|
||||
/**
|
||||
* Safety net for anything the watch missed. The VM's watcher can miss a directory created along
|
||||
* with its parent (`mkdir -p .context/shots`), so small folders are re-checked often (a few
|
||||
* calls each) and large mirrors less often: 30 ms per file, between 30 s and 10 min, and never
|
||||
* more than 5% of the time (a slow pass, e.g. on an image without files.statMany, waits longer).
|
||||
*/
|
||||
const SAFETY_TICK_MS = 30_000
|
||||
export function safetyIntervalMs(files: number, lastPassMs = 0): number {
|
||||
return Math.min(10 * 60_000, Math.max(SAFETY_TICK_MS, files * 30, lastPassMs * 20))
|
||||
}
|
||||
const WATCH_RETRY_MIN_MS = 5_000
|
||||
const WATCH_RETRY_MAX_MS = 5 * 60_000
|
||||
|
||||
@@ -76,7 +68,7 @@ export function createCloudSyncSession(deps: CloudSyncSessionDeps) {
|
||||
let watch: WatchSubscription | null = null
|
||||
let watchRetryMs = WATCH_RETRY_MIN_MS
|
||||
let watchRetryTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let safetyTimer: ReturnType<typeof setInterval> | null = null
|
||||
let safetyTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let lastFullPassAt = 0
|
||||
let lastFullPassMs = 0
|
||||
const abort = new AbortController()
|
||||
@@ -274,6 +266,20 @@ export function createCloudSyncSession(deps: CloudSyncSessionDeps) {
|
||||
}
|
||||
}
|
||||
|
||||
/** One check per tick while running; stop() ends the chain. */
|
||||
function scheduleSafetyCheck(): void {
|
||||
if (stopped) {
|
||||
return
|
||||
}
|
||||
safetyTimer = setTimeout(() => {
|
||||
if (now() - lastFullPassAt >= safetyIntervalMs(state.files, lastFullPassMs)) {
|
||||
void requestFullPass()
|
||||
}
|
||||
scheduleSafetyCheck()
|
||||
}, SAFETY_TICK_MS)
|
||||
safetyTimer.unref?.()
|
||||
}
|
||||
|
||||
function scheduleWatchRetry(): void {
|
||||
if (stopped || watchRetryTimer) {
|
||||
return
|
||||
@@ -295,18 +301,11 @@ export function createCloudSyncSession(deps: CloudSyncSessionDeps) {
|
||||
await loadIndex()
|
||||
await requestFullPass()
|
||||
await openWatch()
|
||||
safetyTimer = setInterval(() => {
|
||||
if (now() - lastFullPassAt >= safetyIntervalMs(state.files, lastFullPassMs)) {
|
||||
void requestFullPass()
|
||||
}
|
||||
}, SAFETY_TICK_MS)
|
||||
safetyTimer.unref?.()
|
||||
scheduleSafetyCheck()
|
||||
},
|
||||
/** This computer woke, or the connection came back: anything may have changed meanwhile. */
|
||||
resync(): Promise<void> {
|
||||
if (!watch) {
|
||||
void openWatch()
|
||||
}
|
||||
void openWatch()
|
||||
return requestFullPass()
|
||||
},
|
||||
setChoice(next: CloudSyncChoice): Promise<void> {
|
||||
@@ -321,14 +320,10 @@ export function createCloudSyncSession(deps: CloudSyncSessionDeps) {
|
||||
watch?.close()
|
||||
watch = null
|
||||
changes.dispose()
|
||||
if (watchRetryTimer) {
|
||||
clearTimeout(watchRetryTimer)
|
||||
}
|
||||
if (safetyTimer) {
|
||||
clearInterval(safetyTimer)
|
||||
}
|
||||
watchRetryTimer = null
|
||||
safetyTimer = null
|
||||
// clearTimeout ignores null.
|
||||
clearTimeout(watchRetryTimer ?? undefined)
|
||||
clearTimeout(safetyTimer ?? undefined)
|
||||
watchRetryTimer = safetyTimer = null
|
||||
return chain
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,8 +131,7 @@ function CloudWorkspaceMarkInner({
|
||||
align="start"
|
||||
sideOffset={10}
|
||||
focusRing
|
||||
// Taller than a short window once the sync section is open: scroll instead of clipping.
|
||||
className="scrollbar-sleek max-h-(--radix-popover-content-available-height) w-72 overflow-y-auto"
|
||||
className="w-72"
|
||||
aria-label={label}
|
||||
tabIndex={-1}
|
||||
onPointerEnter={() => clearTimer()}
|
||||
@@ -147,7 +146,8 @@ function CloudWorkspaceMarkInner({
|
||||
}}
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
>
|
||||
<div className="p-3">
|
||||
{/* Taller than a short window once the sync section is open: scroll instead of clipping. */}
|
||||
<div className="scrollbar-sleek max-h-(--radix-popover-content-available-height) overflow-y-auto p-3">
|
||||
<CloudWorkspaceStatusPanel
|
||||
workspaceId={workspaceId}
|
||||
state={view.state}
|
||||
|
||||
@@ -50,7 +50,6 @@ function PathWithBreaks({ path }: { path: string }): React.JSX.Element {
|
||||
return (
|
||||
<>
|
||||
{parts.map((part, i) => (
|
||||
// oxlint-disable-next-line react/no-array-index-key -- path segments can repeat; their order is the identity
|
||||
<React.Fragment key={i}>
|
||||
{part}
|
||||
{i < parts.length - 1 ? (
|
||||
|
||||
Reference in New Issue
Block a user