From 3670caa763572b45e348c5901fae5b6fdf082de5 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 21:01:25 -0800 Subject: [PATCH] Persist remote layout updates using changed workspace parts --- .../primary-state-write-runtime.ts | 1 + .../loading-store/primary-state-write-sync.ts | 3 + .../primary-state-write-worker.ts | 21 ++- .../loading-store/profile-state-authority.ts | 18 +++ ...profile-state-delayed-authority-fixture.ts | 17 +++ .../profile-state-selective-write.ts | 23 ++- .../profile-state-worker-coordination.spec.ts | 57 ++++++++ .../remote-layout-snapshot-serialization.ts | 68 +++++++++ .../state-serialization-secret-handling.ts | 8 ++ .../loading-store/store-runtime-state.ts | 2 + .../profile-state-document-parts-storage.ts | 17 ++- .../profile-state-domain-parts-update.test.ts | 89 ++++++++++++ .../profile-state-domain-parts-update.ts | 131 ++++++++++++++++++ .../profile-state-domain-write-validation.ts | 14 +- .../profile-state-domain-writes.ts | 11 ++ .../profile-state-sqlite-authority.ts | 36 ++--- .../profile-state-startup-snapshot.ts | 17 +++ .../profile-state-worker-authority.ts | 28 ++-- .../profile-state-worker-lifecycle.ts | 18 +++ .../profile-state-writer-protocol.ts | 28 +++- .../profile-state-writer-worker-client.ts | 38 +++-- .../profile-state-writer-worker-entry.ts | 7 + .../profile-state-writer-worker.test.ts | 30 ++++ .../remote-layout-snapshot-persistence.ts | 16 ++- .../remote-layout-snapshot-recorder.ts | 8 +- 25 files changed, 645 insertions(+), 61 deletions(-) create mode 100644 src/main/persistence/loading-store/remote-layout-snapshot-serialization.ts create mode 100644 src/main/persistence/profile-state/profile-state-domain-parts-update.test.ts create mode 100644 src/main/persistence/profile-state/profile-state-domain-parts-update.ts create mode 100644 src/main/persistence/profile-state/profile-state-startup-snapshot.ts create mode 100644 src/main/persistence/profile-state/profile-state-worker-lifecycle.ts diff --git a/src/main/persistence/loading-store/primary-state-write-runtime.ts b/src/main/persistence/loading-store/primary-state-write-runtime.ts index 4c2373f24da..d0f70c1031f 100644 --- a/src/main/persistence/loading-store/primary-state-write-runtime.ts +++ b/src/main/persistence/loading-store/primary-state-write-runtime.ts @@ -14,6 +14,7 @@ export type PrimaryStateWriteOperationsRuntime = Pick< | 'lastWrittenStateHash' | 'pendingSnapshotFileWork' | 'pendingAutomationRunsAfter' + | 'pendingRemoteLayoutWorkspaces' | 'pendingWrite' | 'profileMaintenancePending' | 'profileStateAuthority' diff --git a/src/main/persistence/loading-store/primary-state-write-sync.ts b/src/main/persistence/loading-store/primary-state-write-sync.ts index c1e8480063d..8f13488ea3c 100644 --- a/src/main/persistence/loading-store/primary-state-write-sync.ts +++ b/src/main/persistence/loading-store/primary-state-write-sync.ts @@ -42,6 +42,7 @@ export function writeToDiskSync( runtime.pendingAutomationRunsAfter = undefined } runtime.lastWrittenStateHash = null + runtime.pendingRemoteLayoutWorkspaces.clear() runtime.protectedSecrets.commitRetentionUpdates(selective.protectedSecretUpdates) markPrimaryStateWriteDurable(runtime, opts.expectedGeneration ?? runtime.writeGeneration) authority.scheduleBackup?.() @@ -58,6 +59,7 @@ export function writeToDiskSync( if (canReuseDurableProfileState(runtime, stateHash)) { runtime.dirtyProfileStateDomains = new Set() runtime.pendingAutomationRunsAfter = undefined + runtime.pendingRemoteLayoutWorkspaces.clear() markPrimaryStateWriteDurable(runtime, opts.expectedGeneration ?? runtime.writeGeneration) return true } @@ -68,6 +70,7 @@ export function writeToDiskSync( } runtime.dirtyProfileStateDomains = new Set() runtime.pendingAutomationRunsAfter = undefined + runtime.pendingRemoteLayoutWorkspaces.clear() if (!isCurrent || isCurrent()) { runtime.lastWrittenStateHash = stateHash runtime.protectedSecrets.commitRetentionUpdates(protectedSecretUpdates) diff --git a/src/main/persistence/loading-store/primary-state-write-worker.ts b/src/main/persistence/loading-store/primary-state-write-worker.ts index 6a67aa73cb2..f23fa9880d4 100644 --- a/src/main/persistence/loading-store/primary-state-write-worker.ts +++ b/src/main/persistence/loading-store/primary-state-write-worker.ts @@ -2,6 +2,10 @@ import type { AsyncProfileStateAuthority } from './profile-state-authority' import type { PrimaryStateWriteOperationsContext } from './primary-state-write-context' import { markPrimaryStateWriteDurable } from './primary-state-write-runtime' import { prepareSelectiveProfileStateWrite } from './profile-state-selective-write' +import { + restoreRemoteLayoutWorkspaces, + type PendingRemoteLayoutWorkspaces +} from './remote-layout-snapshot-serialization' // Capture intent now; edits made while this operation waits belong to the next write. export async function writeProfileStateInWorker( @@ -12,8 +16,10 @@ export async function writeProfileStateInWorker( const generation = runtime.writeGeneration const dirtyDomains = runtime.dirtyProfileStateDomains const automationRuns = runtime.pendingAutomationRunsAfter + const remoteWorkspaces = runtime.pendingRemoteLayoutWorkspaces runtime.dirtyProfileStateDomains = new Set() runtime.pendingAutomationRunsAfter = undefined + runtime.pendingRemoteLayoutWorkspaces = new Map() const restoreIntent = (): void => { if (dirtyDomains === null) { @@ -24,6 +30,7 @@ export async function writeProfileStateInWorker( } } runtime.pendingAutomationRunsAfter ??= automationRuns + restoreRemoteLayoutWorkspaces(runtime.pendingRemoteLayoutWorkspaces, remoteWorkspaces) } try { @@ -32,6 +39,7 @@ export async function writeProfileStateInWorker( serialization, dirtyDomains, automationRuns, + remoteWorkspaces, runtime.lastWrittenStateHash, () => runtime.writeGeneration === generation ) @@ -69,6 +77,7 @@ function beginWrite( serialization: PrimaryStateWriteOperationsContext['serialization'], dirtyDomains: ReadonlySet | null, automationRuns: PrimaryStateWriteOperationsContext['runtime']['pendingAutomationRunsAfter'], + remoteWorkspaces: PendingRemoteLayoutWorkspaces, lastWrittenStateHash: string | null, isCurrent: () => boolean ) { @@ -76,14 +85,20 @@ function beginWrite( authority, serialization, dirtyDomains, - automationRuns + automationRuns, + remoteWorkspaces ) if (selective) { if (!isCurrent()) { return undefined } - const completion = - selective.automationRuns !== undefined + const completion = selective.partsUpdate + ? authority.writeSerializedDomainParts!( + selective.replacements, + selective.partsUpdate, + selective.automationRuns + ) + : selective.automationRuns !== undefined ? authority.writeSerializedAutomationRuns(selective.replacements, selective.automationRuns) : authority.writeSerializedDomains(selective.replacements) return { completion, stateHash: null, protectedSecretUpdates: selective.protectedSecretUpdates } diff --git a/src/main/persistence/loading-store/profile-state-authority.ts b/src/main/persistence/loading-store/profile-state-authority.ts index b39ba4357bc..89492cc9dba 100644 --- a/src/main/persistence/loading-store/profile-state-authority.ts +++ b/src/main/persistence/loading-store/profile-state-authority.ts @@ -17,6 +17,12 @@ export type ProfileStateAuthority = { /** Without selective domain writes, callers replace the full document. */ writeSerializedDomains?: (replacements: readonly ProfileStateDomainReplacement[]) => void + writeSerializedDomainParts?: ( + replacements: readonly ProfileStateDomainReplacement[], + update: ProfileStateDomainPartsUpdate, + runs?: readonly AutomationRun[] + ) => void + /** Commit definitions and run projections atomically without serializing unrelated domains. */ writeSerializedAutomationRuns?: ( replacements: readonly ProfileStateDomainReplacement[], @@ -48,6 +54,7 @@ export type AsyncProfileStateAuthority = Omit< | 'asynchronous' | 'assertCurrentRevision' | 'writeSerializedDomains' + | 'writeSerializedDomainParts' | 'writeSerializedAutomationRuns' | 'writeSerializedState' | 'writeCompleteSerializedDomains' @@ -60,6 +67,11 @@ export type AsyncProfileStateAuthority = Omit< abort(): Promise assertCurrentRevision(): Promise writeSerializedDomains(replacements: readonly ProfileStateDomainReplacement[]): Promise + writeSerializedDomainParts?: ( + replacements: readonly ProfileStateDomainReplacement[], + update: ProfileStateDomainPartsUpdate, + runs?: readonly AutomationRun[] + ) => Promise writeSerializedAutomationRuns( replacements: readonly ProfileStateDomainReplacement[], runs: readonly AutomationRun[] @@ -85,6 +97,12 @@ export type ProfileStateDomainReplacement = { payload: string | null } +export type ProfileStateDomainPartsUpdate = { + domain: 'remoteLayoutSnapshots' + order: readonly string[] + parts: readonly { key: string; payload: string }[] +} + export type ProfileStateStartupPaneAlias = Omit export type ProfileStateAuthorityInitialState< diff --git a/src/main/persistence/loading-store/profile-state-delayed-authority-fixture.ts b/src/main/persistence/loading-store/profile-state-delayed-authority-fixture.ts index c0a5bf9554b..793aa068798 100644 --- a/src/main/persistence/loading-store/profile-state-delayed-authority-fixture.ts +++ b/src/main/persistence/loading-store/profile-state-delayed-authority-fixture.ts @@ -8,6 +8,7 @@ import { ProfileStateSqliteAuthority } from '../profile-state/profile-state-sqli import { writeVersionedProfileStateExport } from '../profile-state/legacy-json/profile-state-versioned-export' import type { AsyncProfileStateAuthority, + ProfileStateDomainPartsUpdate, ProfileStateDomainReplacement } from './profile-state-authority' import { Store } from './store' @@ -63,6 +64,22 @@ export class DelayedAuthority implements AsyncProfileStateAuthority { this.captures.push([...captured]) await this.dispatch(() => this.inner.writeSerializedDomains(captured), true) } + async writeSerializedDomainParts( + replacements: readonly ProfileStateDomainReplacement[], + update: ProfileStateDomainPartsUpdate, + runs?: readonly AutomationRun[] + ) { + const captured = structuredClone({ replacements, update, runs }) + await this.dispatch( + () => + this.inner.writeSerializedDomainParts( + captured.replacements, + captured.update, + captured.runs + ), + true + ) + } async writeCompleteSerializedDomains(replacements: readonly ProfileStateDomainReplacement[]) { const captured = structuredClone(replacements) this.captures.push([...captured]) diff --git a/src/main/persistence/loading-store/profile-state-selective-write.ts b/src/main/persistence/loading-store/profile-state-selective-write.ts index d5e481fbd1b..3cfd665a729 100644 --- a/src/main/persistence/loading-store/profile-state-selective-write.ts +++ b/src/main/persistence/loading-store/profile-state-selective-write.ts @@ -2,10 +2,12 @@ import type { ProtectedSecretRetentionUpdate } from '../../credentials/protected import type { ProfileStateAuthority, ProfileStateDomainReplacement, + ProfileStateDomainPartsUpdate, ProfileStatePersistenceAuthority } from './profile-state-authority' import type { StateSerializationSecretHandlingOperations } from './state-serialization-secret-handling' import type { AutomationRun } from '../../../shared/automations/history/automation-run' +import type { PendingRemoteLayoutWorkspaces } from './remote-layout-snapshot-serialization' export type SelectiveProfileStateWriteResult = { handled: boolean @@ -19,6 +21,7 @@ export type PreparedSelectiveProfileStateWrite = { automationRuns: readonly AutomationRun[] | undefined consumedAutomationRuns: boolean protectedSecretUpdates: ProtectedSecretRetentionUpdate[] + partsUpdate?: ProfileStateDomainPartsUpdate } export function writeSelectiveProfileState( @@ -68,7 +71,8 @@ export function prepareSelectiveProfileStateWrite( authority: ProfileStatePersistenceAuthority | undefined, serialization: StateSerializationSecretHandlingOperations, dirtyDomains: ReadonlySet | null, - pendingAutomationRunsAfter: readonly AutomationRun[] | undefined + pendingAutomationRunsAfter: readonly AutomationRun[] | undefined, + pendingRemoteLayoutWorkspaces?: PendingRemoteLayoutWorkspaces ): PreparedSelectiveProfileStateWrite | undefined { if ( !authority || @@ -81,9 +85,19 @@ export function prepareSelectiveProfileStateWrite( const useAutomationDelta = pendingAutomationRunsAfter !== undefined && authority.writeSerializedAutomationRuns !== undefined - const serializableDomains = useAutomationDelta - ? new Set([...dirtyDomains].filter((domain) => domain !== 'automationRuns')) - : dirtyDomains + const partsUpdate = + dirtyDomains.has('remoteLayoutSnapshots') && + authority.writeSerializedDomainParts && + pendingRemoteLayoutWorkspaces + ? serialization.buildRemoteLayoutSnapshotParts(pendingRemoteLayoutWorkspaces) + : undefined + const serializableDomains = new Set( + [...dirtyDomains].filter( + (domain) => + !(useAutomationDelta && domain === 'automationRuns') && + !(partsUpdate && domain === 'remoteLayoutSnapshots') + ) + ) const built = serialization.buildStateDomainsToSave(serializableDomains) if (built === undefined) { return undefined @@ -93,6 +107,7 @@ export function prepareSelectiveProfileStateWrite( replacements, automationRuns: useAutomationDelta ? pendingAutomationRunsAfter : undefined, consumedAutomationRuns: pendingAutomationRunsAfter !== undefined, + partsUpdate, protectedSecretUpdates } } diff --git a/src/main/persistence/loading-store/profile-state-worker-coordination.spec.ts b/src/main/persistence/loading-store/profile-state-worker-coordination.spec.ts index 339074dc428..178943c1002 100644 --- a/src/main/persistence/loading-store/profile-state-worker-coordination.spec.ts +++ b/src/main/persistence/loading-store/profile-state-worker-coordination.spec.ts @@ -1,8 +1,65 @@ import { describe, expect, it, vi } from 'vitest' import { fixture } from './profile-state-delayed-authority-fixture' +import { recordRemoteLayoutFrame } from '../../remote-layout-snapshot/remote-layout-snapshot-recorder' export function registerProfileStateWorkerCoordinationTests(): void { describe('worker-owned Store writes', () => { + it('retains a later remote workspace edit after an older snapshot commit', async () => { + const { store, authority, readState } = await fixture() + const record = (title: string) => + recordRemoteLayoutFrame(store, 'env', 'session.tabs.subscribeAll', { + id: 'stream', + ok: true, + _meta: { runtimeId: 'test' }, + result: { + type: 'updated', + worktree: 'workspace', + tabs: [{ type: 'terminal', id: 'tab', title }] + } + }) + record('first') + const gate = authority.pause() + const first = store.flushPendingOrThrowAsync({ drainToStableGeneration: false }) + await gate.started.promise + record('second') + gate.finish.resolve() + await first + expect(readState().remoteLayoutSnapshots.env.workspaces.workspace.tabs).toEqual([ + { type: 'terminal', id: 'tab', title: 'first' } + ]) + await store.flushPendingOrThrowAsync() + expect(readState().remoteLayoutSnapshots.env.workspaces.workspace.tabs).toEqual([ + { type: 'terminal', id: 'tab', title: 'second' } + ]) + }) + + it('restores failed remote snapshot intent alongside a newer edit', async () => { + const { store, authority, readState } = await fixture() + vi.spyOn(console, 'error').mockImplementation(() => {}) + recordRemoteLayoutFrame(store, 'env', 'session.tabs.subscribeAll', { + id: 'stream', + ok: true, + _meta: { runtimeId: 'test' }, + result: { type: 'snapshots', snapshots: [{ worktree: 'a', tabs: [] }] } + }) + const gate = authority.pause() + const first = store.flushPendingOrThrowAsync({ drainToStableGeneration: false }) + const rejected = expect(first).rejects.toThrow('disk refused') + await gate.started.promise + recordRemoteLayoutFrame(store, 'env', 'session.tabs.subscribeAll', { + id: 'stream', + ok: true, + _meta: { runtimeId: 'test' }, + result: { type: 'updated', worktree: 'b', tabs: [] } + }) + gate.finish.reject(new Error('disk refused')) + await rejected + await store.flushPendingOrThrowAsync() + expect(readState().remoteLayoutSnapshots).toEqual({ + env: { workspaces: { a: { worktree: 'a', tabs: [] }, b: { worktree: 'b', tabs: [] } } } + }) + }) + it('retains a newer edit after an older write is acknowledged', async () => { const { store, authority, readState } = await fixture() const gate = authority.pause() diff --git a/src/main/persistence/loading-store/remote-layout-snapshot-serialization.ts b/src/main/persistence/loading-store/remote-layout-snapshot-serialization.ts new file mode 100644 index 00000000000..91d1213c6c2 --- /dev/null +++ b/src/main/persistence/loading-store/remote-layout-snapshot-serialization.ts @@ -0,0 +1,68 @@ +import type { RemoteLayoutSnapshot } from '../../../shared/remote-layout-snapshot' +import type { ProfileStateDomainPartsUpdate } from './profile-state-authority' +import { serializeSelectiveProfileStateDomains } from './profile-state-authority-writes' + +export type PendingRemoteLayoutWorkspaces = Map | null> + +export function serializeRemoteLayoutSnapshotParts( + snapshots: Record | undefined, + pending: PendingRemoteLayoutWorkspaces +): ProfileStateDomainPartsUpdate | undefined { + if (pending.size === 0 || snapshots === undefined) { + return undefined + } + const order: string[] = [] + const parts: { key: string; payload: string }[] = [] + for (const [environment, snapshot] of Object.entries(snapshots)) { + if ( + !snapshot || + Object.keys(snapshot).length !== 1 || + !snapshot.workspaces || + typeof snapshot.workspaces !== 'object' || + Array.isArray(snapshot.workspaces) + ) { + return undefined + } + const keys = Object.keys(snapshot.workspaces) + const dirty = pending.get(environment) + if (keys.length === 0) { + const key = JSON.stringify([environment, 'workspaces']) + order.push(key) + if (pending.has(environment)) { + parts.push({ key, payload: '{}' }) + } + } + for (const worktree of keys) { + const key = JSON.stringify([environment, 'workspaces', worktree]) + order.push(key) + if (dirty === null || dirty?.has(worktree)) { + const [replacement] = serializeSelectiveProfileStateDomains( + { [worktree]: snapshot.workspaces[worktree] }, + new Set([worktree]) + ) + if (replacement?.payload == null) { + return undefined + } + parts.push({ key, payload: replacement.payload }) + } + } + } + if (order.length === 0) { + order.push('[]') + parts.push({ key: '[]', payload: '{}' }) + } + return { domain: 'remoteLayoutSnapshots', order, parts } +} + +export function restoreRemoteLayoutWorkspaces( + pending: PendingRemoteLayoutWorkspaces, + captured: PendingRemoteLayoutWorkspaces +): void { + for (const [environment, keys] of captured) { + const current = pending.get(environment) + pending.set( + environment, + keys === null || current === null ? null : new Set([...keys, ...(current ?? [])]) + ) + } +} diff --git a/src/main/persistence/loading-store/state-serialization-secret-handling.ts b/src/main/persistence/loading-store/state-serialization-secret-handling.ts index 05d859c8cae..1b8a6858756 100644 --- a/src/main/persistence/loading-store/state-serialization-secret-handling.ts +++ b/src/main/persistence/loading-store/state-serialization-secret-handling.ts @@ -22,6 +22,10 @@ import { type SecretSentinelSubstitution } from './secret-sentinel-substitution' import type { StoreRuntimeState } from './store-runtime-state' +import { + serializeRemoteLayoutSnapshotParts, + type PendingRemoteLayoutWorkspaces +} from './remote-layout-snapshot-serialization' type StateSerializationSecretHandlingOperationsRuntime = Pick< StoreRuntimeState, @@ -31,6 +35,10 @@ type StateSerializationSecretHandlingOperationsRuntime = Pick< export class StateSerializationSecretHandlingOperations { constructor(private readonly runtime: StateSerializationSecretHandlingOperationsRuntime) {} + buildRemoteLayoutSnapshotParts(pending: PendingRemoteLayoutWorkspaces) { + return serializeRemoteLayoutSnapshotParts(this.runtime.state.remoteLayoutSnapshots, pending) + } + getDurableState(): Omit { const { githubCache: _memoryOnly, ...durable } = this.runtime.state return durable diff --git a/src/main/persistence/loading-store/store-runtime-state.ts b/src/main/persistence/loading-store/store-runtime-state.ts index 951187da47b..0795ac7854f 100644 --- a/src/main/persistence/loading-store/store-runtime-state.ts +++ b/src/main/persistence/loading-store/store-runtime-state.ts @@ -29,6 +29,7 @@ import type { } from '../scheduling-automations/automation-owner-projection' import type { ProfileStatePersistenceAuthority } from './profile-state-authority' import type { AutomationRun } from '../../../shared/automations/history/automation-run' +import type { PendingRemoteLayoutWorkspaces } from './remote-layout-snapshot-serialization' import { resolveNewPaneShellPath } from '../migrating-orcad-catalog/orcad-shell-history-installer' import { OrcadTerminalRestoreHintStore } from '../migrating-orcad-catalog/orcad-terminal-restore-hint-store' @@ -82,6 +83,7 @@ export class StoreRuntimeState { /** Null requests complete-document fallback. */ dirtyProfileStateDomains: Set | null = new Set() pendingAutomationRunsAfter: readonly AutomationRun[] | undefined + pendingRemoteLayoutWorkspaces: PendingRemoteLayoutWorkspaces = new Map() githubCacheDirty = false githubCachePersistedAt = Date.now() githubCachePersistTimer: ReturnType | null = null diff --git a/src/main/persistence/profile-state/profile-state-document-parts-storage.ts b/src/main/persistence/profile-state/profile-state-document-parts-storage.ts index 9641f09e0fc..49d24f1f550 100644 --- a/src/main/persistence/profile-state/profile-state-document-parts-storage.ts +++ b/src/main/persistence/profile-state/profile-state-document-parts-storage.ts @@ -198,6 +198,8 @@ export function writeProfileStateDomainParts( payload: string /** Already-parsed payload, when the caller has it. */ value?: unknown + parts?: readonly ProfileStateDocumentPart[] + changedPartKeys?: ReadonlySet payloadHash: string domainVersion: number revision: number @@ -215,10 +217,12 @@ export function writeProfileStateDomainParts( if (depth === undefined) { return false } - const parts = splitProfileStateDomainValue( - args.value === undefined ? JSON.parse(args.payload) : args.value, - depth - ) + const parts = + args.parts ?? + splitProfileStateDomainValue( + args.value === undefined ? JSON.parse(args.payload) : args.value, + depth + ) // Non-canonical JSON (whitespace, escapes) would not round-trip byte-for-byte. if (assembleProfileStateDomainPayload(parts) !== args.payload) { deleteProfileStateDomainParts(db, args.domain) @@ -267,7 +271,10 @@ export function writeProfileStateDomainParts( seen.add(part.key) const ordinal = ordinals[index]! const previous = existing.get(part.key) - const contentHash = hashProfileStatePart(part.payload) + const contentHash = + previous && args.changedPartKeys && !args.changedPartKeys.has(part.key) + ? previous.contentHash + : hashProfileStatePart(part.payload) if (!previous) { insert.run(args.domain, part.key, ordinal, part.payload, contentHash) } else if (previous.contentHash !== contentHash) { diff --git a/src/main/persistence/profile-state/profile-state-domain-parts-update.test.ts b/src/main/persistence/profile-state/profile-state-domain-parts-update.test.ts new file mode 100644 index 00000000000..74b6969b607 --- /dev/null +++ b/src/main/persistence/profile-state/profile-state-domain-parts-update.test.ts @@ -0,0 +1,89 @@ +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, expect, it } from 'vitest' +import { openProfileStateDatabase, profileStateDatabaseFile } from './profile-state-database' +import { exportProfileStateJson, importProfileStateJson } from './profile-state-documents' +import { writeProfileStateDomains } from './profile-state-domain-writes' + +const directories: string[] = [] +afterEach(() => { + for (const path of directories.splice(0)) { + rmSync(path, { recursive: true, force: true }) + } +}) +function open() { + const path = mkdtempSync(join(tmpdir(), 'orca-remote-parts-')) + directories.push(path) + return openProfileStateDatabase(profileStateDatabaseFile(path), 'parts-test').db +} + +it('migrates a legacy snapshot, retains unrelated workspaces, and applies ordered removals and empty maps', () => { + const db = open() + try { + importProfileStateJson( + db, + '{"remoteLayoutSnapshots":{"env":{"workspaces":{"a":{"tabs":[{"title":"old"}]},"b":{"tabs":[]}}}},"other":1}' + ) + writeProfileStateDomains(db, { + expectedRevision: 1, + replacements: [], + partsUpdate: { + domain: 'remoteLayoutSnapshots', + order: ['["env","workspaces","b"]', '["env","workspaces","a"]'], + parts: [{ key: '["env","workspaces","a"]', payload: '{"tabs":[{"title":"new"}]}' }] + } + }) + expect(exportProfileStateJson(db)).toBe( + '{"remoteLayoutSnapshots":{"env":{"workspaces":{"b":{"tabs":[]},"a":{"tabs":[{"title":"new"}]}}}},"other":1}' + ) + writeProfileStateDomains(db, { + expectedRevision: 2, + replacements: [], + partsUpdate: { + domain: 'remoteLayoutSnapshots', + order: ['["env","workspaces"]'], + parts: [{ key: '["env","workspaces"]', payload: '{}' }] + } + }) + expect(exportProfileStateJson(db)).toBe( + '{"remoteLayoutSnapshots":{"env":{"workspaces":{}}},"other":1}' + ) + writeProfileStateDomains(db, { + expectedRevision: 3, + replacements: [], + partsUpdate: { + domain: 'remoteLayoutSnapshots', + order: ['[]'], + parts: [{ key: '[]', payload: '{}' }] + } + }) + expect(exportProfileStateJson(db)).toBe('{"remoteLayoutSnapshots":{},"other":1}') + } finally { + db.close() + } +}) + +it('rolls back the snapshot and other domains together when its commit fails', () => { + const db = open() + try { + importProfileStateJson(db, '{"remoteLayoutSnapshots":{},"other":1}') + db.exec( + "CREATE TRIGGER refuse_parts BEFORE INSERT ON profile_state_document_parts BEGIN SELECT RAISE(ABORT, 'disk refused'); END" + ) + expect(() => + writeProfileStateDomains(db, { + expectedRevision: 1, + replacements: [{ domain: 'other', payload: '2' }], + partsUpdate: { + domain: 'remoteLayoutSnapshots', + order: ['["env","workspaces","a"]'], + parts: [{ key: '["env","workspaces","a"]', payload: '{"tabs":[]}' }] + } + }) + ).toThrow('disk refused') + expect(exportProfileStateJson(db)).toBe('{"remoteLayoutSnapshots":{},"other":1}') + } finally { + db.close() + } +}) diff --git a/src/main/persistence/profile-state/profile-state-domain-parts-update.ts b/src/main/persistence/profile-state/profile-state-domain-parts-update.ts new file mode 100644 index 00000000000..5bd6329d1af --- /dev/null +++ b/src/main/persistence/profile-state/profile-state-domain-parts-update.ts @@ -0,0 +1,131 @@ +import type Database from '../../sqlite/sync-database' +import type { ProfileStateDomainPartsUpdate } from '../loading-store/profile-state-authority' +import type { PreparedProfileStateMutation } from './profile-state-domain-write-validation' +import { + assembleProfileStateDomainPayload, + splitProfileStateDomainValue, + PROFILE_STATE_DOCUMENT_PARTS_TABLE, + type ProfileStateDocumentPart +} from './profile-state-document-parts' +import { readSplitDomainWriteState } from './profile-state-document-parts-storage' +import { + hashProfileStatePayload, + isRecord, + ProfileStateDocumentCorruptionError, + validateProfileStateDocumentRow +} from './profile-state-document-validation' +import { assertProfileStateDocumentRevision } from './profile-state-revision' + +export function prepareProfileStateDomainPartsUpdate( + db: Database.Database, + update: ProfileStateDomainPartsUpdate, + revision: number +): PreparedProfileStateMutation { + const split = readSplitDomainWriteState(db, update.domain) + let previous: ProfileStateDocumentPart[] = [] + if (split.kind === 'parts') { + assertProfileStateDocumentRevision(split.revision, revision, update.domain) + previous = db + .prepare( + `SELECT part_key, payload FROM ${PROFILE_STATE_DOCUMENT_PARTS_TABLE} WHERE domain = ? ORDER BY ordinal` + ) + .all(update.domain) + .map((row) => { + if (!isRecord(row) || typeof row.part_key !== 'string' || typeof row.payload !== 'string') { + throw new ProfileStateDocumentCorruptionError( + 'Profile state delta base is invalid', + update.domain + ) + } + return { key: row.part_key, payload: row.payload } + }) + if ( + hashProfileStatePayload(assembleProfileStateDomainPayload(previous)) !== split.contentHash + ) { + throw new ProfileStateDocumentCorruptionError( + 'Profile state delta base hash mismatch', + update.domain + ) + } + } else { + const row = db + .prepare( + 'SELECT domain, payload, domain_version, revision, updated_at, content_hash FROM profile_state_documents WHERE domain = ?' + ) + .get(update.domain) + if (row !== undefined) { + const document = validateProfileStateDocumentRow(row) + assertProfileStateDocumentRevision(document.revision, revision, update.domain) + previous = splitProfileStateDomainValue(JSON.parse(document.payload), 3) + } + } + const payloads = new Map(previous.map((part) => [part.key, part.payload])) + const keys = new Set(update.order) + if (keys.size !== update.order.length || keys.size === 0) { + throw new Error('Invalid profile state part order') + } + const changed = new Set() + for (const part of update.parts) { + if (!keys.has(part.key) || changed.has(part.key)) { + throw new Error('Invalid profile state changed part') + } + JSON.parse(part.payload) + payloads.set(part.key, part.payload) + changed.add(part.key) + } + const closed = new Set() + const empty = new Set() + let environment: string | undefined + let hasPartInEnvironment = false + const parts = update.order.map((key) => { + const path: unknown = JSON.parse(key) + if ( + !Array.isArray(path) || + !path.every((segment) => typeof segment === 'string') || + JSON.stringify(path) !== key || + !(path.length === 0 || ((path.length === 2 || path.length === 3) && path[1] === 'workspaces')) + ) { + throw new Error('Invalid remote layout snapshot part key') + } + const payload = payloads.get(key) + if (payload === undefined) { + throw new Error('Missing profile state part') + } + if (path.length === 0) { + if (keys.size !== 1 || payload !== '{}') { + throw new Error('Invalid empty remote layout snapshot') + } + } else { + if (environment !== path[0]) { + if (environment !== undefined) { + closed.add(environment) + } + if (closed.has(path[0])) { + throw new Error('Repeated remote layout snapshot environment') + } + environment = path[0] + hasPartInEnvironment = false + } + if (empty.has(environment!)) { + throw new Error('Overlapping remote layout snapshot parts') + } + if (path.length === 2) { + if (payload !== '{}' || hasPartInEnvironment) { + throw new Error('Invalid empty remote layout workspaces') + } + empty.add(environment!) + } + hasPartInEnvironment = true + } + return { key, payload } + }) + const payload = assembleProfileStateDomainPayload(parts) + return { + domain: update.domain, + payload, + payloadHash: hashProfileStatePayload(payload), + domainVersion: 1, + serializedParts: parts, + changedPartKeys: changed + } +} diff --git a/src/main/persistence/profile-state/profile-state-domain-write-validation.ts b/src/main/persistence/profile-state/profile-state-domain-write-validation.ts index b899e477496..154df93be22 100644 --- a/src/main/persistence/profile-state/profile-state-domain-write-validation.ts +++ b/src/main/persistence/profile-state/profile-state-domain-write-validation.ts @@ -2,6 +2,7 @@ import { PROFILE_STATE_DOCUMENT_VERSION } from './profile-state-database-schema' import { isProfileStateSplitDomain } from './profile-state-document-parts' import { hashProfileStateJson } from './profile-state-documents' import type { AutomationRunsWritePreparation } from './profile-state-automation-runs' +import type { ProfileStateDocumentPart } from './profile-state-document-parts' import type { ProfileStateDomainMutation, ProfileStateDomainTransaction @@ -15,6 +16,8 @@ export type PreparedProfileStateMutation = ProfileStateDomainMutation & { /** Parsed payload of a split domain, so storing its parts does not parse it again. */ splitDomainValue?: unknown automationRuns?: AutomationRunsWritePreparation + serializedParts?: readonly ProfileStateDocumentPart[] + changedPartKeys?: ReadonlySet } export function validateProfileStateDomainTransaction( @@ -25,7 +28,9 @@ export function validateProfileStateDomainTransaction( } if ( !Array.isArray(transaction.replacements) || - (transaction.replacements.length === 0 && transaction.automationRunsAfter === undefined) + (transaction.replacements.length === 0 && + transaction.automationRunsAfter === undefined && + transaction.partsUpdate === undefined) ) { throw new Error('Profile state domain transaction requires at least one replacement') } @@ -40,6 +45,13 @@ export function validateProfileStateDomainTransaction( if (transaction.automationRunsAfter !== undefined && domains.has('automationRuns')) { throw new Error('Profile state automationRuns delta repeats domain: automationRuns') } + if ( + transaction.partsUpdate && + (transaction.partsUpdate.domain !== 'remoteLayoutSnapshots' || + domains.has(transaction.partsUpdate.domain)) + ) { + throw new Error('Profile state parts update repeats or names an unsupported domain') + } } export function prepareProfileStateDomainMutation( diff --git a/src/main/persistence/profile-state/profile-state-domain-writes.ts b/src/main/persistence/profile-state/profile-state-domain-writes.ts index 44314d9ab3b..ee9f6476264 100644 --- a/src/main/persistence/profile-state/profile-state-domain-writes.ts +++ b/src/main/persistence/profile-state/profile-state-domain-writes.ts @@ -1,5 +1,7 @@ import { withProfileStateWriteTransaction } from './profile-state-write-transaction' import type Database from '../../sqlite/sync-database' +import type { ProfileStateDomainPartsUpdate } from '../loading-store/profile-state-authority' +import { prepareProfileStateDomainPartsUpdate } from './profile-state-domain-parts-update' import { readCurrentAutomationRunsState } from './profile-state-automation-runs-storage' import { PROFILE_STATE_DOCUMENT_VERSION, @@ -49,6 +51,7 @@ export type ProfileStateDomainTransaction = { replacements: readonly ProfileStateDomainMutation[] /** Enable selective run-row updates from Store's changed projection. */ automationRunsAfter?: readonly unknown[] + partsUpdate?: ProfileStateDomainPartsUpdate } export type ProfileStateDomainWriteResult = { @@ -96,6 +99,11 @@ export function writeProfileStateDomains( const currentAutomationRuns = readCurrentAutomationRunsState(db, actualRevision) const updates: PreparedProfileStateMutation[] = [] + if (transaction.partsUpdate) { + prepared.push( + prepareProfileStateDomainPartsUpdate(db, transaction.partsUpdate, actualRevision) + ) + } const splitStates = new Map>() for (const mutation of prepared) { const splitState = readSplitDomainWriteState(db, mutation.domain) @@ -202,6 +210,8 @@ export function writeProfileStateDomains( domain: mutation.domain, payload: mutation.payload, value: mutation.splitDomainValue, + parts: mutation.serializedParts, + changedPartKeys: mutation.changedPartKeys, payloadHash: mutation.payloadHash!, domainVersion: mutation.domainVersion, revision: nextRevision, @@ -236,6 +246,7 @@ export function writeProfileStateDomains( domain: mutation.domain, payload: mutation.payload, value: mutation.splitDomainValue, + parts: mutation.serializedParts, payloadHash: mutation.payloadHash!, domainVersion: mutation.domainVersion, revision: nextRevision, diff --git a/src/main/persistence/profile-state/profile-state-sqlite-authority.ts b/src/main/persistence/profile-state/profile-state-sqlite-authority.ts index bd15fa2545a..178beef3677 100644 --- a/src/main/persistence/profile-state/profile-state-sqlite-authority.ts +++ b/src/main/persistence/profile-state/profile-state-sqlite-authority.ts @@ -1,8 +1,10 @@ +import { createProfileStateStartupSnapshotReader } from './profile-state-startup-snapshot' import { existsSync } from 'node:fs' import type { ProfileStateAuthority, ProfileStateAuthorityInitialState, ProfileStateDomainReplacement, + ProfileStateDomainPartsUpdate, ProfileStateMaintenance } from '../loading-store/profile-state-authority' import { @@ -143,7 +145,8 @@ export class ProfileStateSqliteAuthority implements ProfileStateAuthority { writeSerializedDomains( replacements: readonly ProfileStateDomainReplacement[], - automationRunsAfter?: readonly unknown[] + automationRunsAfter?: readonly unknown[], + partsUpdate?: ProfileStateDomainPartsUpdate ): void { this.assertActive() if (this.observedRevision === undefined) { @@ -155,7 +158,8 @@ export class ProfileStateSqliteAuthority implements ProfileStateAuthority { this.observedRevision = writeProfileStateDomains(opened.db, { expectedRevision: this.observedRevision ?? 0, replacements, - automationRunsAfter + automationRunsAfter, + partsUpdate }).revision this.legacyDocuments.afterWrite(() => this.writableDatabase?.db) } @@ -167,11 +171,14 @@ export class ProfileStateSqliteAuthority implements ProfileStateAuthority { } } - writeSerializedAutomationRuns( + readonly writeSerializedAutomationRuns = this.writeSerializedDomains.bind(this) + + writeSerializedDomainParts( replacements: readonly ProfileStateDomainReplacement[], - runs: readonly unknown[] + update: ProfileStateDomainPartsUpdate, + runs?: readonly unknown[] ): void { - this.writeSerializedDomains(replacements, runs) + this.writeSerializedDomains(replacements, runs, update) } writeSerializedState(payload: Buffer): void { @@ -285,23 +292,16 @@ export class ProfileStateSqliteAuthority implements ProfileStateAuthority { revision: number, value: Record | undefined ): ProfileStateAuthorityInitialState { - let pending: { revision: number; value: Record | undefined } | undefined = { - revision, - value - } this.observedRevision = revision return { authority: this, - takeParsedState: () => { - this.assertActive() - const snapshot = pending - if (snapshot === undefined) { - throw new Error('Profile state startup snapshot has already been consumed') + takeParsedState: createProfileStateStartupSnapshotReader( + value, + () => this.assertActive(), + () => { + this.observedRevision = revision } - pending = undefined - this.observedRevision = snapshot.revision - return snapshot.value - } + ) } } diff --git a/src/main/persistence/profile-state/profile-state-startup-snapshot.ts b/src/main/persistence/profile-state/profile-state-startup-snapshot.ts new file mode 100644 index 00000000000..2802fa16b66 --- /dev/null +++ b/src/main/persistence/profile-state/profile-state-startup-snapshot.ts @@ -0,0 +1,17 @@ +export function createProfileStateStartupSnapshotReader( + value: Record | undefined, + assertActive: () => void, + accept: () => void +): () => Record | undefined { + let pending: { value: Record | undefined } | undefined = { value } + return () => { + assertActive() + const snapshot = pending + if (snapshot === undefined) { + throw new Error('Profile state startup snapshot has already been consumed') + } + pending = undefined + accept() + return snapshot.value + } +} diff --git a/src/main/persistence/profile-state/profile-state-worker-authority.ts b/src/main/persistence/profile-state/profile-state-worker-authority.ts index 49ba60ce74a..24034afdd63 100644 --- a/src/main/persistence/profile-state/profile-state-worker-authority.ts +++ b/src/main/persistence/profile-state/profile-state-worker-authority.ts @@ -17,14 +17,11 @@ import { reportTerminalReopenFailure } from './profile-state-writer-reopen-policy' -export type ProfileStateWorkerAuthorityOptions = { - workerPath?: string - backupWorkerPath?: string - onFailure?: (error: Error) => void - onSaveDelayChanged?: (delayed: boolean) => void - /** Close the writer thread after this long without a command; the next one reopens it (~22 ms). */ - writerIdleStopMs?: number -} +export type { ProfileStateWorkerAuthorityOptions } from './profile-state-worker-lifecycle' +import { + closedWriterError, + type ProfileStateWorkerAuthorityOptions +} from './profile-state-worker-lifecycle' /** Main owns backup scheduling; the worker owns every live SQL command while it runs. */ export class ProfileStateWorkerAuthority implements AsyncProfileStateAuthority { @@ -91,6 +88,13 @@ export class ProfileStateWorkerAuthority implements AsyncProfileStateAuthority { return this.run((writer) => writer.writeSerializedDomains(replacements)).then(() => {}) } + writeSerializedDomainParts: NonNullable< + AsyncProfileStateAuthority['writeSerializedDomainParts'] + > = (replacements, update, runs) => + this.run((writer) => writer.writeSerializedDomainParts(replacements, update, runs)).then( + () => {} + ) + writeSerializedAutomationRuns( replacements: readonly ProfileStateDomainReplacement[], runs: readonly AutomationRun[] @@ -335,11 +339,3 @@ export class ProfileStateWorkerAuthority implements AsyncProfileStateAuthority { return this.stoppedRevision } } - -function closedWriterError(): ProfileStateWriterError { - return new ProfileStateWriterError( - 'profile-state-writer-closed', - 'Profile state writer is closing', - 'known-failure' - ) -} diff --git a/src/main/persistence/profile-state/profile-state-worker-lifecycle.ts b/src/main/persistence/profile-state/profile-state-worker-lifecycle.ts new file mode 100644 index 00000000000..bc753210820 --- /dev/null +++ b/src/main/persistence/profile-state/profile-state-worker-lifecycle.ts @@ -0,0 +1,18 @@ +import { ProfileStateWriterError } from './profile-state-writer-errors' + +export type ProfileStateWorkerAuthorityOptions = { + workerPath?: string + backupWorkerPath?: string + onFailure?: (error: Error) => void + onSaveDelayChanged?: (delayed: boolean) => void + /** Close the writer thread after this long without a command; the next one reopens it (~22 ms). */ + writerIdleStopMs?: number +} + +export function closedWriterError(): ProfileStateWriterError { + return new ProfileStateWriterError( + 'profile-state-writer-closed', + 'Profile state writer is closing', + 'known-failure' + ) +} diff --git a/src/main/persistence/profile-state/profile-state-writer-protocol.ts b/src/main/persistence/profile-state/profile-state-writer-protocol.ts index fc6dd1f262d..89ac56c7a18 100644 --- a/src/main/persistence/profile-state/profile-state-writer-protocol.ts +++ b/src/main/persistence/profile-state/profile-state-writer-protocol.ts @@ -1,4 +1,7 @@ -import type { ProfileStateDomainReplacement } from '../loading-store/profile-state-authority' +import type { + ProfileStateDomainReplacement, + ProfileStateDomainPartsUpdate +} from '../loading-store/profile-state-authority' import { isRecord } from './profile-state-document-validation' export type ProfileStateWriterInitialization = { @@ -8,6 +11,12 @@ export type ProfileStateWriterInitialization = { } export type ProfileStateWriterCommand = + | { + command: 'write-parts' + replacements: readonly ProfileStateDomainReplacement[] + update: ProfileStateDomainPartsUpdate + runPayloads?: readonly string[] + } | { command: 'write-state'; payload: Uint8Array } | { command: 'write-complete' | 'write-domains' @@ -70,6 +79,23 @@ export function isProfileStateWriterRequest(value: unknown): value is ProfileSta return false } switch (value.command) { + case 'write-parts': + return ( + Array.isArray(value.replacements) && + value.replacements.every(isReplacement) && + isRecord(value.update) && + value.update.domain === 'remoteLayoutSnapshots' && + Array.isArray(value.update.order) && + value.update.order.every((key) => typeof key === 'string') && + Array.isArray(value.update.parts) && + value.update.parts.every( + (part) => + isRecord(part) && typeof part.key === 'string' && typeof part.payload === 'string' + ) && + (value.runPayloads === undefined || + (Array.isArray(value.runPayloads) && + value.runPayloads.every((payload) => typeof payload === 'string'))) + ) case 'write-state': return value.payload instanceof Uint8Array case 'assert-revision': diff --git a/src/main/persistence/profile-state/profile-state-writer-worker-client.ts b/src/main/persistence/profile-state/profile-state-writer-worker-client.ts index f8226bdff7f..cf8e0e90a23 100644 --- a/src/main/persistence/profile-state/profile-state-writer-worker-client.ts +++ b/src/main/persistence/profile-state/profile-state-writer-worker-client.ts @@ -1,4 +1,7 @@ -import type { ProfileStateDomainReplacement } from '../loading-store/profile-state-authority' +import type { + ProfileStateDomainReplacement, + ProfileStateDomainPartsUpdate +} from '../loading-store/profile-state-authority' import { ProfileStateWriterConnection } from './profile-state-writer-connection' export { resolveProfileStateWriterWorkerPath } from './profile-state-writer-worker-path' export { @@ -8,6 +11,21 @@ export { export type { ProfileStateWriterInitialization } from './profile-state-writer-protocol' export class ProfileStateWriteWorkerClient extends ProfileStateWriterConnection { + writeSerializedDomainParts( + replacements: readonly ProfileStateDomainReplacement[], + update: ProfileStateDomainPartsUpdate, + runs?: readonly unknown[] + ): Promise { + try { + this.assertDispatchable() + const runPayloads = runs === undefined ? undefined : serializeAutomationRunPayloads(runs) + return this.dispatch({ command: 'write-parts', replacements, update, runPayloads }).then( + (response) => response.revision + ) + } catch (error) { + return Promise.reject(error) + } + } assertWritable(): void { this.assertDispatchable() } @@ -36,13 +54,7 @@ export class ProfileStateWriteWorkerClient extends ProfileStateWriterConnection ): Promise { try { this.assertDispatchable() - const runPayloads = runs.map((run) => { - const payload = JSON.stringify(run) - if (payload === undefined) { - throw new Error('Automation run is not serializable') - } - return payload - }) + const runPayloads = serializeAutomationRunPayloads(runs) return this.dispatch({ command: 'write-automation', replacements, runPayloads }).then( (response) => response.revision ) @@ -67,3 +79,13 @@ export class ProfileStateWriteWorkerClient extends ProfileStateWriterConnection ) } } + +function serializeAutomationRunPayloads(runs: readonly unknown[]): string[] { + return runs.map((run) => { + const payload = JSON.stringify(run) + if (payload === undefined) { + throw new Error('Automation run is not serializable') + } + return payload + }) +} diff --git a/src/main/persistence/profile-state/profile-state-writer-worker-entry.ts b/src/main/persistence/profile-state/profile-state-writer-worker-entry.ts index b037ad87952..c7016681310 100644 --- a/src/main/persistence/profile-state/profile-state-writer-worker-entry.ts +++ b/src/main/persistence/profile-state/profile-state-writer-worker-entry.ts @@ -40,6 +40,13 @@ async function execute(request: ProfileStateWriterRequest): Promise JSON.parse(payload)) + ) + break case 'write-state': authority.writeSerializedState(Buffer.from(request.payload)) break diff --git a/src/main/persistence/profile-state/profile-state-writer-worker.test.ts b/src/main/persistence/profile-state/profile-state-writer-worker.test.ts index 265e90fb320..6c5d2b90bac 100644 --- a/src/main/persistence/profile-state/profile-state-writer-worker.test.ts +++ b/src/main/persistence/profile-state/profile-state-writer-worker.test.ts @@ -83,6 +83,36 @@ function script(root: string, source: string): string { } describe('persistent profile state write worker', () => { + it('delivers captured snapshot parts with other domains and automation history in one commit', async () => { + const f = fixture() + const client = clientFor(f.initialization) + await client.ready + const update = { + domain: 'remoteLayoutSnapshots' as const, + order: ['["env","workspaces","a"]'], + parts: [{ key: '["env","workspaces","a"]', payload: '{"tabs":[]}' }] + } + const write = client.writeSerializedDomainParts( + [{ domain: 'settings', payload: '{"theme":"light"}' }], + update, + [{ id: 'run-1' }] + ) + update.parts[0]!.payload = '{"tabs":["edited after capture"]}' + await write + const exported = join(f.root, 'parts.json') + await client.writeJsonExport(exported) + expect(JSON.parse(readFileSync(exported, 'utf8'))).toEqual({ + settings: { theme: 'light' }, + remoteLayoutSnapshots: { env: { workspaces: { a: { tabs: [] } } } }, + automationRuns: [{ id: 'run-1' }] + }) + await client.close() + expect(readState(f.path, f.profileId)).toEqual({ + settings: { theme: 'light' }, + remoteLayoutSnapshots: { env: { workspaces: { a: { tabs: [] } } } }, + automationRuns: [{ id: 'run-1' }] + }) + }) it('commits full, selective, automation and byte payloads, exports and releases the database', async () => { const f = fixture() const client = clientFor(f.initialization) diff --git a/src/main/remote-layout-snapshot/remote-layout-snapshot-persistence.ts b/src/main/remote-layout-snapshot/remote-layout-snapshot-persistence.ts index 04560570a45..3f587d0edf1 100644 --- a/src/main/remote-layout-snapshot/remote-layout-snapshot-persistence.ts +++ b/src/main/remote-layout-snapshot/remote-layout-snapshot-persistence.ts @@ -3,7 +3,10 @@ import type { StoreRuntimeState } from '../persistence/loading-store/store-runti import type { WriteSchedulingOperations } from '../persistence/loading-store/write-scheduling' import { scheduleSave } from '../persistence/loading-store/write-scheduling' -type RemoteLayoutSnapshotRuntime = Pick +type RemoteLayoutSnapshotRuntime = Pick< + StoreRuntimeState, + 'state' | 'dirtyProfileStateDomains' | 'pendingRemoteLayoutWorkspaces' +> const remoteLayoutSnapshotPersistenceContext = Symbol('RemoteLayoutSnapshotPersistence') type RemoteLayoutSnapshotPersistenceContext = { @@ -40,7 +43,8 @@ export class RemoteLayoutSnapshotPersistence { setRemoteLayoutSnapshot( environmentId: string, snapshot: RemoteLayoutSnapshot | null, - save: 'now' | 'later' + save: 'now' | 'later', + changedWorkspaces?: readonly string[] ): void { const { runtime, scheduling } = this[remoteLayoutSnapshotPersistenceContext] const { [environmentId]: previous, ...others } = runtime.state.remoteLayoutSnapshots ?? {} @@ -50,6 +54,14 @@ export class RemoteLayoutSnapshotPersistence { runtime.state.remoteLayoutSnapshots = snapshot ? { ...others, [environmentId]: snapshot } : others + const pending = runtime.pendingRemoteLayoutWorkspaces + const current = pending.get(environmentId) + pending.set( + environmentId, + changedWorkspaces === undefined || current === null + ? null + : new Set([...(current ?? []), ...changedWorkspaces]) + ) if (save === 'later') { runtime.dirtyProfileStateDomains?.add('remoteLayoutSnapshots') return diff --git a/src/main/remote-layout-snapshot/remote-layout-snapshot-recorder.ts b/src/main/remote-layout-snapshot/remote-layout-snapshot-recorder.ts index 36bec681970..bb474b6dded 100644 --- a/src/main/remote-layout-snapshot/remote-layout-snapshot-recorder.ts +++ b/src/main/remote-layout-snapshot/remote-layout-snapshot-recorder.ts @@ -12,7 +12,8 @@ export type RemoteLayoutSnapshotStore = { setRemoteLayoutSnapshot( environmentId: string, snapshot: RemoteLayoutSnapshot | null, - save: 'now' | 'later' + save: 'now' | 'later', + changedWorkspaces?: readonly string[] ): void } @@ -80,14 +81,15 @@ export function recordRemoteLayoutFrame( const { [frame.worktree]: previous, ...others } = store.getRemoteLayoutSnapshot(environmentId)?.workspaces ?? {} if (removed) { - store.setRemoteLayoutSnapshot(environmentId, { workspaces: others }, 'now') + store.setRemoteLayoutSnapshot(environmentId, { workspaces: others }, 'now', [frame.worktree]) return } const workspace = displayed(result) store.setRemoteLayoutSnapshot( environmentId, { workspaces: { ...others, [frame.worktree]: workspace } }, - structure(previous) === structure(workspace) ? 'later' : 'now' + structure(previous) === structure(workspace) ? 'later' : 'now', + [frame.worktree] ) }