ReviewOS

also looking at this

pantry-pm/pantry

fix: hydrate durable registry snapshots before serving writes

#244
Merged chris wants to merge codex/wildloop-registry-rollout into main
11 files +202 -35
packages/registry/src/storage/object-snapshot.tsmodified+9-9
Changes to packages/registry/src/storage/object-snapshot.ts
@@ -1,4 +1,5 @@
11import type { S3Client } from './aws-client'
2import { isS3ObjectNotFound } from './aws-client'
23
34/**
45 * Reusable debounced JSON-snapshot persistence to an S3-compatible bucket.
@@ -13,6 +14,7 @@ import type { S3Client } from './aws-client'
1314 * the bucket under their own keys by the owning store.
1415 */
1516export class ObjectSnapshot {
17 private hydrated = false
1618 private saveTimeout: ReturnType<typeof setTimeout> | null = null
1719 private savePromise: Promise<void> | null = null
1820
@@ -27,17 +29,13 @@ export class ObjectSnapshot {
2729 async load(): Promise<unknown | null> {
2830 try {
2931 const buf = await this.s3.getObjectBuffer(this.bucket, this.key)
30 if (!buf || buf.byteLength === 0)
31 return null
32 return JSON.parse(buf.toString('utf-8'))
32 const state = JSON.parse(buf.toString('utf-8'))
33 this.hydrated = true
34 return state
3335 }
3436 catch (err) {
35 // A missing object on first run is expected (404/403). Anything else we
36 // surface but still start empty rather than block boot — and we do NOT
37 // overwrite the remote object until the next real save.
38 const msg = (err as Error).message || ''
39 if (!/\b40[34]\b/.test(msg))
40 console.error(`ObjectSnapshot: failed to load ${this.bucket}/${this.key}: ${msg}. Starting empty.`)
37 if (!isS3ObjectNotFound(err)) throw err
38 this.hydrated = true
4139 return null
4240 }
4341 }
@@ -53,6 +51,8 @@ export class ObjectSnapshot {
5351 }
5452
5553 private async save(): Promise<void> {
54 if (!this.hydrated)
55 throw new Error(`Snapshot not hydrated: ${this.key}`)
5656 // Serialize saves so a slow upload can't be overtaken by a newer one.
5757 while (this.savePromise) await this.savePromise
5858 this.savePromise = this.s3.putObject({