mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* fix(profile-state): repair only the damaged sections instead of rolling back the whole profile One profile-state row whose checksum no longer matches made Orca refuse to open the whole profile, and the only recovery swapped in a whole backup, losing every newer change in every section. Startup now checks each section on its own. When only some sections are damaged, the dialog offers "Repair and Restart": Orca copies the damaged database aside, then replaces just those sections with the newest backup copy that passes its checks, keeping every intact section's current value, in one checked write. Headless starts name the damaged sections and `orca profile state rollback --repair`. Whole-file damage keeps the existing backup rollback. Also turns on checkpoint_fullfsync for the profile database: on macOS plain fsync leaves the drive cache unflushed, which can leave a long row's pages from two versions after a power loss or forced shutdown. Tracked in STA-10893. * test(profile-state): pin the repair dialog's text and buttons * fix(profile-state): list the repair module for the CLI typecheck; keep section labels out of the layout-writer count * fix: validate section repair admission before decoding payloads * fix(profile-state): share backup handles during section repair
93 lines
3.4 KiB
TypeScript
93 lines
3.4 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
export const PROFILE_STATE_RECOVERY_FLAG = '--profile-state-recovery'
|
|
export const PROFILE_STATE_RECOVERY_RESULT_PREFIX = '[profile-state-recovery] '
|
|
/** Desktop variant: applies a startup-dialog choice, then relaunches Orca normally. */
|
|
export const PROFILE_STATE_DESKTOP_RECOVERY_FLAG = '--profile-state-desktop-recovery'
|
|
|
|
const positiveInteger = z.number().int().positive().max(Number.MAX_SAFE_INTEGER)
|
|
const repairSectionSchema = z
|
|
.object({ domain: z.string(), backupId: z.string().min(1).nullable() })
|
|
.strict()
|
|
const selectorSchema = z.discriminatedUnion('kind', [
|
|
z.object({ kind: z.literal('json'), revision: positiveInteger }).strict(),
|
|
z.object({ kind: z.literal('current-json') }).strict(),
|
|
z.object({ kind: z.literal('current-sqlite') }).strict(),
|
|
z.object({ kind: z.literal('latest-json') }).strict(),
|
|
z.object({ kind: z.literal('sqlite'), backupId: z.string().min(1) }).strict(),
|
|
// Sections: the plan the user approved; omitted, the CLI repairs whatever it finds.
|
|
z
|
|
.object({ kind: z.literal('repair'), sections: z.array(repairSectionSchema).optional() })
|
|
.strict()
|
|
])
|
|
|
|
export const profileStateRecoveryRequestSchema = z
|
|
.object({
|
|
userDataPath: z.string().min(1),
|
|
selector: selectorSchema,
|
|
profileId: z
|
|
.string()
|
|
.regex(/^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/)
|
|
.optional()
|
|
})
|
|
.strict()
|
|
|
|
const exportsSchema = z.object({
|
|
profileId: z.string(),
|
|
dataFile: z.string(),
|
|
databaseFile: z.string(),
|
|
exportPaths: z.array(z.string()).readonly(),
|
|
backups: z
|
|
.array(z.object({ id: z.string(), path: z.string(), createdAtMs: positiveInteger }))
|
|
.readonly()
|
|
})
|
|
const rollbackSchema = exportsSchema.extend({
|
|
// Canonical JSON edited outside SQLite has no database revision.
|
|
revision: positiveInteger.nullable(),
|
|
quarantineDirectory: z.string(),
|
|
removedDatabaseFiles: z.array(z.string()).readonly(),
|
|
storage: z.enum(['json', 'sqlite']),
|
|
restoredPath: z.string(),
|
|
backupId: z.string().optional(),
|
|
repairedSections: z
|
|
.array(repairSectionSchema.extend({ backupCreatedAtMs: positiveInteger.optional() }))
|
|
.readonly()
|
|
.optional()
|
|
})
|
|
|
|
export const profileStateRecoveryResponseSchema = z.discriminatedUnion('ok', [
|
|
z.object({ ok: z.literal(true), result: rollbackSchema }),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
code: z.enum(['invalid_argument', 'runtime_error']),
|
|
message: z.string()
|
|
})
|
|
])
|
|
|
|
export type ProfileStateRecoverySelector = z.infer<typeof selectorSchema>
|
|
export type ProfileStateRecoveryRepairSection = z.infer<typeof repairSectionSchema>
|
|
export type ProfileStateRecoveryRequest = z.infer<typeof profileStateRecoveryRequestSchema>
|
|
export type ProfileStateRecoveryResponse = z.infer<typeof profileStateRecoveryResponseSchema>
|
|
export type ProfileStateExportsResult = z.infer<typeof exportsSchema>
|
|
export type ProfileStateRollbackResult = z.infer<typeof rollbackSchema>
|
|
|
|
export class ProfileStateRecoveryCommandError extends Error {
|
|
constructor(
|
|
readonly code: 'invalid_argument' | 'runtime_error',
|
|
message: string
|
|
) {
|
|
super(message)
|
|
this.name = 'ProfileStateRecoveryCommandError'
|
|
}
|
|
}
|
|
|
|
export function isProfileStateRecoveryCommandError(
|
|
error: unknown
|
|
): error is Error & { code: 'invalid_argument' | 'runtime_error' } {
|
|
return (
|
|
error instanceof Error &&
|
|
'code' in error &&
|
|
(error.code === 'invalid_argument' || error.code === 'runtime_error')
|
|
)
|
|
}
|