From 5de3aeeb2ecd7506db5d5dec5d4bdbfa9b3bc084 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 1 Aug 2026 22:26:44 +0000 Subject: [PATCH] OH3: pin snapshot policy constants + simplify benchmark script Co-authored-by: Francisco Solla --- scripts/benchmark-ops-snapshot.mjs | 33 +++++++++++++----------- src/lib/campaignOps/opsSnapshotPolicy.ts | 14 ++++++++++ tests/unit/campaignOps.unit.spec.ts | 9 +++++++ 3 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/lib/campaignOps/opsSnapshotPolicy.ts diff --git a/scripts/benchmark-ops-snapshot.mjs b/scripts/benchmark-ops-snapshot.mjs index e22a76af..407df32c 100644 --- a/scripts/benchmark-ops-snapshot.mjs +++ b/scripts/benchmark-ops-snapshot.mjs @@ -32,13 +32,23 @@ assertLocalDatabase( ) const { OPS_MIRROR_SCHEMA_VERSION } = await import('../src/lib/campaignOps/opsMirrorVersion.ts') +const { serializeOpsSnapshot } = await import('../src/lib/campaignOps/opsContract.ts') +const { + OPS_MUNICIPALITY_UPDATE_LIMIT_PER_MUNICIPALITY, + OPS_SNAPSHOT_GZIP_TARGET_BYTES, + OPS_SNAPSHOT_PROD_GZIP_RATIO_ESTIMATE, + OPS_SNAPSHOT_PROD_MEASURED_JSON_BYTES, +} = await import('../src/lib/campaignOps/opsSnapshotPolicy.ts') const { relationshipId, uniqueRelationshipIds } = await import('../src/lib/relationship.ts') +const { MINIMAL_CAMPAIGN_USERS } = await import('./lib/seed-minimal-manifest.mjs') const payloadConfig = (await import('../src/payload.config.ts')).default /** @typedef {import('../src/lib/campaignOps/opsContract.ts').OpsSnapshot} OpsSnapshot */ -const COORDINATOR_EMAIL = 'seed-coordenador@teqo.invalid' +const COORDINATOR_EMAIL = + MINIMAL_CAMPAIGN_USERS.find((user) => user.role === 'coordinator')?.email ?? + 'seed-coordenador@teqo.invalid' /** Prod row estimates for projection (2026-08-01 ops-hibrido spec + campaign scale). */ const PROD_ROW_ESTIMATES = { @@ -55,12 +65,12 @@ const PROD_ROW_ESTIMATES = { * Measured prod JSON size (2026-08-01, ops-hibrido spec) — anchor for projection sanity. * @see docs/plans/ops-hibrido-rsc-local-spec.md */ -const PROD_MEASURED_JSON_BYTES = 4 * 1024 * 1024 +const PROD_MEASURED_JSON_BYTES = OPS_SNAPSHOT_PROD_MEASURED_JSON_BYTES /** Conservative gzip ratio for prod ops JSON (text-heavy updates; not the sparse-local 6%). */ -const PROD_GZIP_RATIO_ESTIMATE = 0.5 +const PROD_GZIP_RATIO_ESTIMATE = OPS_SNAPSHOT_PROD_GZIP_RATIO_ESTIMATE -const GZIP_TARGET_BYTES = 2 * 1024 * 1024 +const GZIP_TARGET_BYTES = OPS_SNAPSHOT_GZIP_TARGET_BYTES /** * Fallback bytes/row when the minimal seed has no rows (local cannot extrapolate). @@ -79,7 +89,7 @@ const PROD_FALLBACK_BYTES_PER_ROW = { const parseCliArgs = (argv) => { /** @type {Record} */ - const rowLimits = { municipality_update: 50 } + const rowLimits = { municipality_update: OPS_MUNICIPALITY_UPDATE_LIMIT_PER_MUNICIPALITY } for (let index = 0; index < argv.length; index += 1) { const arg = argv[index] @@ -300,7 +310,7 @@ const truncateMunicipalityUpdates = (updates, limitPerMunicipality) => { } const measureJson = (value) => { - const json = JSON.stringify(value) + const json = typeof value === 'string' ? value : JSON.stringify(value) const gzip = gzipSync(json) return { jsonBytes: Buffer.byteLength(json, 'utf8'), gzipBytes: gzip.length } } @@ -565,7 +575,7 @@ const buildSnapshotSections = async (payload, actor, rowLimits) => { goals, } - const totalSize = measureJson(snapshot) + const totalSize = measureJson(serializeOpsSnapshot(snapshot)) const totalQueryMs = sections.reduce((sum, section) => sum + section.queryMs, 0) return { snapshot, sections, totalSize, totalQueryMs } @@ -610,18 +620,11 @@ const projectProdSnapshot = (sections, rowLimits) => { sections.reduce((sum, section) => sum + section.jsonBytes, 0) : PROD_GZIP_RATIO_ESTIMATE - const estimatedGzipBytes = Math.round(projectedJsonBytes * PROD_GZIP_RATIO_ESTIMATE) const prodMeasuredGzipEstimate = Math.round( PROD_MEASURED_JSON_BYTES * PROD_GZIP_RATIO_ESTIMATE, ) - return { - projectedRows, - projectedJsonBytes, - estimatedGzipBytes, - localGzipRatio, - prodMeasuredGzipEstimate, - } + return { projectedRows, projectedJsonBytes, prodMeasuredGzipEstimate, localGzipRatio } } const printReport = (sections, totalSize, totalQueryMs, rowLimits, projection) => { diff --git a/src/lib/campaignOps/opsSnapshotPolicy.ts b/src/lib/campaignOps/opsSnapshotPolicy.ts new file mode 100644 index 00000000..ff34914d --- /dev/null +++ b/src/lib/campaignOps/opsSnapshotPolicy.ts @@ -0,0 +1,14 @@ +/** Latest municipality_update rows per municipality in the ops snapshot (OH3 benchmark). */ +export const OPS_MUNICIPALITY_UPDATE_LIMIT_PER_MUNICIPALITY = 50 + +/** Gzip budget for full ops sync (ops-hibrido spec). */ +export const OPS_SNAPSHOT_GZIP_TARGET_BYTES = 2 * 1024 * 1024 + +/** + * Prod JSON size measured 2026-08-01 (ops-hibrido spec) — anchor for OH3 projection. + * @see docs/plans/ops-hibrido-rsc-local-spec.md + */ +export const OPS_SNAPSHOT_PROD_MEASURED_JSON_BYTES = 4 * 1024 * 1024 + +/** Conservative gzip ratio for prod ops JSON (text-heavy updates). */ +export const OPS_SNAPSHOT_PROD_GZIP_RATIO_ESTIMATE = 0.5 diff --git a/tests/unit/campaignOps.unit.spec.ts b/tests/unit/campaignOps.unit.spec.ts index d7276241..066157d0 100644 --- a/tests/unit/campaignOps.unit.spec.ts +++ b/tests/unit/campaignOps.unit.spec.ts @@ -50,6 +50,15 @@ const snapshot = (partial: Partial = {}): OpsSnapshot => ({ ...partial, }) +describe('opsSnapshotPolicy', () => { + it('pins OH3 truncation and gzip budget for OH4', async () => { + const policy = await import('@/lib/campaignOps/opsSnapshotPolicy') + expect(policy.OPS_MUNICIPALITY_UPDATE_LIMIT_PER_MUNICIPALITY).toBe(50) + expect(policy.OPS_SNAPSHOT_GZIP_TARGET_BYTES).toBe(2 * 1024 * 1024) + expect(policy.OPS_SNAPSHOT_PROD_MEASURED_JSON_BYTES).toBe(4 * 1024 * 1024) + }) +}) + describe('OPS_MIRROR_SCHEMA_VERSION', () => { it('is pinned at 1', () => { expect(OPS_MIRROR_SCHEMA_VERSION).toBe(1)