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.

fix(merge): refuse lossy snapshot number conversions [B3,R3,C7]

Corey Olson5c554bd790eb
diff --git a/src/mechanical-merge.js b/src/mechanical-merge.js
index 171118c5..1caab622 100644
--- a/src/mechanical-merge.js
+++ b/src/mechanical-merge.js
@@ -96,6 +96,16 @@ const MISSING = Symbol('missing');
const CONFLICT = Symbol('conflict');
const GENERATED = new Map([['docs/lifecycle.md', ['bin/pullboard.js', 'lifecycle']]]);
+/** Refuse numeric data JavaScript cannot faithfully round-trip instead of silently changing an untouched value. */
+function snapshot(source) {
+ return JSON.parse(source, (_key, value) => {
+ if (typeof value === 'number' && (!Number.isFinite(value) || (Number.isInteger(value) && !Number.isSafeInteger(value)))) {
+ throw new Error('snapshot number cannot round-trip exactly');
+ }
+ return value;
+ });
+}
+
/** Compare JSON values structurally, without confusing absent keys with explicit null. */
function identity(value) {
if (value === MISSING) return MISSING;
@@ -170,7 +180,7 @@ function sourceResolution(path, base, ours, theirs) {
if (GENERATED.has(path)) return { kind: 'generated', keys: [] };
if (path.endsWith('.json')) {
try {
- const merged = jsonValue(JSON.parse(base), JSON.parse(ours), JSON.parse(theirs));
+ const merged = jsonValue(snapshot(base), snapshot(ours), snapshot(theirs));
if (merged === CONFLICT) return null;
const indent = /\n([ \t]+)\S/u.exec(ours)?.[1];
return { kind: 'json', keys: Object.keys(merged ?? {}), source: JSON.stringify(merged, null, indent) + (ours.endsWith('\n') ? '\n' : '') };
diff --git a/test/mechanical-merge.test.js b/test/mechanical-merge.test.js
index fde292dd..758414ec 100644
--- a/test/mechanical-merge.test.js
+++ b/test/mechanical-merge.test.js
@@ -123,6 +123,7 @@ for (const [name, path, base, left, right] of [
['conflicting JSON scalars', 'snapshot.json', '{"value":"base"}\n', '{"value":"left"}\n', '{"value":"right"}\n'],
['a decreased counter', 'snapshot.json', '{"value":2}\n', '{"value":1}\n', '{"value":3}\n'],
['delete versus edit', 'snapshot.json', '{"value":null}\n', '{}\n', '{"value":1}\n'],
+ ['JSON numbers that cannot round-trip', 'snapshot.json', '{"value":1}\n', '{"value":1e400}\n', '{"value":2e400}\n'],
['an invalid JavaScript union', 'src/cli.js', 'export {};\n', 'const value = 1;\nexport {};\n', 'const value = 2;\nexport {};\n'],
['keyed additions with invalid JavaScript context', 'src/api-moves.js',
'export = ;\nconst MOVES = {\n base: { item: true },\n};\n',