Persist remote layout updates using changed workspace parts

This commit is contained in:
Neil
2026-11-09 21:01:25 -08:00
parent 6e445fe60d
commit 3670caa763
25 changed files with 645 additions and 61 deletions
@@ -14,6 +14,7 @@ export type PrimaryStateWriteOperationsRuntime = Pick<
| 'lastWrittenStateHash'
| 'pendingSnapshotFileWork'
| 'pendingAutomationRunsAfter'
| 'pendingRemoteLayoutWorkspaces'
| 'pendingWrite'
| 'profileMaintenancePending'
| 'profileStateAuthority'
@@ -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)
@@ -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<string> | 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 }
@@ -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<void>
assertCurrentRevision(): Promise<void>
writeSerializedDomains(replacements: readonly ProfileStateDomainReplacement[]): Promise<void>
writeSerializedDomainParts?: (
replacements: readonly ProfileStateDomainReplacement[],
update: ProfileStateDomainPartsUpdate,
runs?: readonly AutomationRun[]
) => Promise<void>
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<LegacyPaneKeyAliasEntry, 'ptyId'>
export type ProfileStateAuthorityInitialState<
@@ -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])
@@ -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<string> | 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
}
}
@@ -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()
@@ -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<string, Set<string> | null>
export function serializeRemoteLayoutSnapshotParts(
snapshots: Record<string, RemoteLayoutSnapshot> | 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 ?? [])])
)
}
}
@@ -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<PersistedState, 'githubCache'> {
const { githubCache: _memoryOnly, ...durable } = this.runtime.state
return durable
@@ -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<string> | null = new Set()
pendingAutomationRunsAfter: readonly AutomationRun[] | undefined
pendingRemoteLayoutWorkspaces: PendingRemoteLayoutWorkspaces = new Map()
githubCacheDirty = false
githubCachePersistedAt = Date.now()
githubCachePersistTimer: ReturnType<typeof setTimeout> | null = null
@@ -198,6 +198,8 @@ export function writeProfileStateDomainParts(
payload: string
/** Already-parsed payload, when the caller has it. */
value?: unknown
parts?: readonly ProfileStateDocumentPart[]
changedPartKeys?: ReadonlySet<string>
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) {
@@ -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()
}
})
@@ -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<string>()
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<string>()
const empty = new Set<string>()
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
}
}
@@ -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<string>
}
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(
@@ -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<string, ReturnType<typeof readSplitDomainWriteState>>()
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,
@@ -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<string, unknown> | undefined
): ProfileStateAuthorityInitialState<ProfileStateSqliteAuthority> {
let pending: { revision: number; value: Record<string, unknown> | 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
}
)
}
}
@@ -0,0 +1,17 @@
export function createProfileStateStartupSnapshotReader(
value: Record<string, unknown> | undefined,
assertActive: () => void,
accept: () => void
): () => Record<string, unknown> | undefined {
let pending: { value: Record<string, unknown> | 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
}
}
@@ -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'
)
}
@@ -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'
)
}
@@ -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':
@@ -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<number> {
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<number> {
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
})
}
@@ -40,6 +40,13 @@ async function execute(request: ProfileStateWriterRequest): Promise<ProfileState
}
let exportedRevision: number | null | undefined
switch (request.command) {
case 'write-parts':
authority.writeSerializedDomainParts(
request.replacements,
request.update,
request.runPayloads?.map((payload): unknown => JSON.parse(payload))
)
break
case 'write-state':
authority.writeSerializedState(Buffer.from(request.payload))
break
@@ -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)
@@ -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<StoreRuntimeState, 'state' | 'dirtyProfileStateDomains'>
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
@@ -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]
)
}