Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

refactor(relay): bound the snapshot size module [H5,H17]

Corey Olson316394c84296
diff --git a/src/relay-snapshot-size.js b/src/relay-snapshot-size.js
new file mode 100644
index 00000000..7d852e5b
--- /dev/null
+++ b/src/relay-snapshot-size.js
@@ -0,0 +1,33 @@
+/** Measure and format sealed snapshot size refusals without transport or link state [H5,H17]. */
+import { SEALED_SNAPSHOT_BYTES as SEALED_SNAPSHOT_LIMIT } from './relay-limits.js';
+
+/** Measure a pending sealed snapshot against the relay's decoded journal limit. */
+export function snapshotSize(state) {
+ if (state?.snapshotApiVersion === 2) return null;
+ const sealed = state?.snapshot?.sealed ?? state?.checkpoint?.sealed;
+ if (typeof sealed !== 'string') return null;
+ const bytes = Buffer.from(sealed, 'base64url');
+ if (bytes.toString('base64url') !== sealed || bytes.byteLength <= SEALED_SNAPSHOT_LIMIT) return null;
+ return { board: state.board, sealedBytes: bytes.byteLength, limit: SEALED_SNAPSHOT_LIMIT };
+}
+
+/** Accept a structured relay limit only when its measurement matches the exact refused ciphertext. */
+export function reportedSnapshotSize(error, state) {
+ const value = error.snapshotLimit;
+ if (!value || Object.keys(value).sort().join(',') !== 'limit,sealedBytes'
+ || !Number.isSafeInteger(value.sealedBytes) || !Number.isSafeInteger(value.limit)
+ || value.limit < 1 || value.limit > SEALED_SNAPSHOT_LIMIT || value.sealedBytes <= value.limit
+ || ![400, 413].includes(error.httpStatus) || typeof state.snapshot?.sealed !== 'string') return null;
+ const bytes = Buffer.from(state.snapshot.sealed, 'base64url');
+ if (bytes.toString('base64url') !== state.snapshot.sealed || bytes.byteLength !== value.sealedBytes) return null;
+ return { sealedBytes: bytes.byteLength, limit: value.limit };
+}
+
+/** Format a shared status line for an oversized sealed snapshot. */
+export function formatSnapshotLimit(problem, { paused = false } = {}) {
+ const size = `${(problem.sealedBytes / 1_000_000).toFixed(1)} MB`;
+ const limit = `${(problem.limit / 1_000_000).toFixed(1)} MB`;
+ if (paused) return `relay paused for ${problem.board}: snapshot ${size} over the relay limit ${limit}; it resumes when the relay supports bounded snapshots`;
+ return `relay snapshot for ${problem.board}: sealed snapshot ${size} over the relay limit ${limit}`;
+}
+
diff --git a/src/relay.js b/src/relay.js
index cc70eacb..60f9b160 100644
--- a/src/relay.js
+++ b/src/relay.js
@@ -1,3 +1,9 @@
+import {
+ formatSnapshotLimit,
+ reportedSnapshotSize,
+ snapshotSize,
+} from './relay-snapshot-size.js';
+export { formatSnapshotLimit };
import { beginCommandPhase } from './command-phase.js';
import { recordedMoveRefusal } from './recorded-move.js';
import {
@@ -454,28 +460,6 @@ function summary(root, state, { pairing = false } = {}) {
...(state.checkpointRefusal ? { refusedCheckpoint: checkpointProblem(state) } : {}) };
}
-/** Measure a pending sealed snapshot against the relay's decoded journal limit. */
-function snapshotSize(state) {
- if (state?.snapshotApiVersion === 2) return null;
- const sealed = state?.snapshot?.sealed ?? state?.checkpoint?.sealed;
- if (typeof sealed !== 'string') return null;
- const bytes = Buffer.from(sealed, 'base64url');
- if (bytes.toString('base64url') !== sealed || bytes.byteLength <= SEALED_SNAPSHOT_LIMIT) return null;
- return { board: state.board, sealedBytes: bytes.byteLength, limit: SEALED_SNAPSHOT_LIMIT };
-}
-
-/** Accept a structured relay limit only when its measurement matches the exact refused ciphertext. */
-function reportedSnapshotSize(error, state) {
- const value = error.snapshotLimit;
- if (!value || Object.keys(value).sort().join(',') !== 'limit,sealedBytes'
- || !Number.isSafeInteger(value.sealedBytes) || !Number.isSafeInteger(value.limit)
- || value.limit < 1 || value.limit > SEALED_SNAPSHOT_LIMIT || value.sealedBytes <= value.limit
- || ![400, 413].includes(error.httpStatus) || typeof state.snapshot?.sealed !== 'string') return null;
- const bytes = Buffer.from(state.snapshot.sealed, 'base64url');
- if (bytes.toString('base64url') !== state.snapshot.sealed || bytes.byteLength !== value.sealedBytes) return null;
- return { sealedBytes: bytes.byteLength, limit: value.limit };
-}
-
/** Publish only an actually received, measured refusal, sealed to the existing board key. */
async function publishSnapshotSize(state, io) {
const pause = state.baselinePause;
@@ -497,14 +481,6 @@ export function pendingSnapshotSize(root) {
return snapshotSize(loadLink(linkFile(root)));
}
-/** Format a shared status line for an oversized sealed snapshot. */
-export function formatSnapshotLimit(problem, { paused = false } = {}) {
- const size = `${(problem.sealedBytes / 1_000_000).toFixed(1)} MB`;
- const limit = `${(problem.limit / 1_000_000).toFixed(1)} MB`;
- if (paused) return `relay paused for ${problem.board}: snapshot ${size} over the relay limit ${limit}; it resumes when the relay supports bounded snapshots`;
- return `relay snapshot for ${problem.board}: sealed snapshot ${size} over the relay limit ${limit}`;
-}
-
/** Keep a permanent refusal only as a bounded, credential-free diagnostic. */
function safePauseMessage(error) {
const message = String(error.relayMessage ?? error.message ?? 'the relay refused the baseline upload')