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-analytics.tsmodified+8-7
Changes to packages/registry/src/storage/object-analytics.ts
@@ -1,4 +1,5 @@
11import type { S3Client } from './aws-client'
2import { isS3ObjectNotFound } from './aws-client'
23import { InMemoryAnalytics } from '../analytics'
34
45/**
@@ -16,6 +17,7 @@ import { InMemoryAnalytics } from '../analytics'
1617 * are debounced (counters accumulate in memory between flushes).
1718 */
1819export class ObjectAnalytics extends InMemoryAnalytics {
20 private hydrated = false
1921 private s3: S3Client
2022 private bucket: string
2123 private key: string
@@ -39,16 +41,12 @@ export class ObjectAnalytics extends InMemoryAnalytics {
3941 private async load(): Promise<void> {
4042 try {
4143 const buf = await this.s3.getObjectBuffer(this.bucket, this.key)
42 if (!buf || buf.byteLength === 0)
43 return
4444 this.applyAnalyticsState(JSON.parse(buf.toString('utf-8')))
45 this.hydrated = true
4546 }
4647 catch (err) {
47 const msg = (err as Error).message || ''
48 // A missing object on first run is expected (404/403); start empty otherwise.
49 if (!/\b40[34]\b/.test(msg)) {
50 console.error(`ObjectAnalytics: failed to load ${this.bucket}/${this.key}: ${msg}. Starting empty.`)
51 }
48 if (!isS3ObjectNotFound(err)) throw err
49 this.hydrated = true
5250 }
5351 }
5452
@@ -66,6 +64,8 @@ export class ObjectAnalytics extends InMemoryAnalytics {
6664 }
6765
6866 private async save(): Promise<void> {
67 if (!this.hydrated)
68 throw new Error(`Snapshot not hydrated: ${this.key}`)
6969 while (this.savePromise) await this.savePromise
7070 this.savePromise = this.doSave()
7171 try {
@@ -88,6 +88,7 @@ export class ObjectAnalytics extends InMemoryAnalytics {
8888
8989 /** Flush any pending save immediately (e.g. before shutdown). */
9090 async flush(): Promise<void> {
91 await this.loaded
9192 if (this.saveTimeout) {
9293 clearTimeout(this.saveTimeout)
9394 this.saveTimeout = null