Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions bin/sync-models.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,39 @@ function resolveTargetProvider(config, targetProvider) {
return { outcome: 'absent' };
}

// Order-insensitive deep equality for the plain JSON values a model
// entry is made of (objects, arrays, scalars). Key order in the parsed
// config follows the file, while the desired state is built in code —
// a key-order difference is not drift and must not cause write churn.
function jsonValueEqual(a, b) {
if (a === b) return true;
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
if (Array.isArray(a) !== Array.isArray(b)) return false;
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) return false;
return aKeys.every((key) => Object.prototype.hasOwnProperty.call(b, key) && jsonValueEqual(a[key], b[key]));
}

// Plan the reconciliation of one existing model entry against its
// desired state (#1799). Returns one update per drifted key: the full
// desired `limit` whenever `context` OR `output` differs from the
// desired limits (or the limit is missing), and the desired `variants`
// whenever they differ from the variants built from the current effort
// set. An already-correct entry plans no updates, so a converged sync
// records no edits. A desired empty variants set plans nothing: the
// sync never strips variants it did not build.
function planModelReconciliation(existing, limits, variants) {
const updates = [];
if (Object.keys(variants).length > 0 && !jsonValueEqual(existing.variants, variants)) {
updates.push({ key: 'variants', value: variants });
}
if (!existing.limit || existing.limit.context !== limits.context || existing.limit.output !== limits.output) {
updates.push({ key: 'limit', value: limits });
}
return updates;
}

// -----------------------------------------------------------------------------
// Main Sync Runner
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -669,18 +702,12 @@ async function run() {
edits.push({ set: [...modelsPath, baseId], value: modelEntry });
totalAdded++;
} else {
let changed = false;
if (Object.keys(variants).length > 0 && !prov.models[baseId].variants) {
prov.models[baseId].variants = variants;
edits.push({ set: [...modelsPath, baseId, 'variants'], value: variants });
changed = true;
}
if (prov.models[baseId].limit?.context !== limits.context) {
prov.models[baseId].limit = limits;
edits.push({ set: [...modelsPath, baseId, 'limit'], value: limits });
changed = true;
const updates = planModelReconciliation(prov.models[baseId], limits, variants);
for (const update of updates) {
prov.models[baseId][update.key] = update.value;
edits.push({ set: [...modelsPath, baseId, update.key], value: update.value });
}
if (changed) {
if (updates.length > 0) {
if (VERBOSE) console.log(`[sync-models] ~ Updated ${targetProvider}/${baseId}`);
totalUpdated++;
}
Expand Down
74 changes: 74 additions & 0 deletions bin/sync-models.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,77 @@ test('resolveTargetProvider: no target and no legacy is absent, never in sync (#
assert.deepEqual(Object.keys(config.provider), ['openai']);
assert.deepEqual(resolveTargetProvider({}, '_antigravity'), { outcome: 'absent' });
});

// --- #1799: existing entries are reconciled on the full desired state —
// limit.output and variants drift survive no sync, and a converged entry
// records no edits (no write churn). Oracle: the plan for an entry is
// exactly the set of keys whose desired value differs from the entry's
// current value under order-insensitive equality. ---

const { planModelReconciliation } = new Function(
`${extractFunction('jsonValueEqual')}\n` +
`${extractFunction('planModelReconciliation')}\n` +
'return { planModelReconciliation };',
)();

const desiredLimits = { context: 1048576, output: 65536 };
const desiredVariants = {
high: { options: { reasoning_effort: 'high' } },
medium: { options: { reasoning_effort: 'medium' } },
};

test('reconciliation corrects output-only drift when context already matches (#1799)', () => {
const existing = { limit: { context: 1048576, output: 9999 }, variants: desiredVariants };
assert.deepEqual(planModelReconciliation(existing, desiredLimits, desiredVariants), [
{ key: 'limit', value: desiredLimits },
]);
});

test('reconciliation corrects variants drift: a dropped effort is removed and an added one appears (#1799)', () => {
const existing = { limit: desiredLimits, variants: { low: { options: { reasoning_effort: 'low' } } } };
assert.deepEqual(planModelReconciliation(existing, desiredLimits, desiredVariants), [
{ key: 'variants', value: desiredVariants },
]);
});

test('reconciliation still corrects context drift and a missing limit (#1799)', () => {
assert.deepEqual(
planModelReconciliation({ limit: { context: 1, output: 65536 }, variants: desiredVariants }, desiredLimits, desiredVariants),
[{ key: 'limit', value: desiredLimits }],
);
assert.deepEqual(
planModelReconciliation({ variants: desiredVariants }, desiredLimits, desiredVariants),
[{ key: 'limit', value: desiredLimits }],
);
});

test('reconciliation adds variants to an entry that has none (#1799)', () => {
const existing = { limit: desiredLimits };
assert.deepEqual(planModelReconciliation(existing, desiredLimits, desiredVariants), [
{ key: 'variants', value: desiredVariants },
]);
});

test('reconciliation plans both updates when limit and variants both drift (#1799)', () => {
const existing = { limit: { context: 1048576, output: 1 }, variants: {} };
assert.deepEqual(planModelReconciliation(existing, desiredLimits, desiredVariants), [
{ key: 'variants', value: desiredVariants },
{ key: 'limit', value: desiredLimits },
]);
});

test('an already-correct entry plans no updates, whatever the key order (#1799)', () => {
const existing = {
limit: { output: 65536, context: 1048576 },
variants: {
medium: { options: { reasoning_effort: 'medium' } },
high: { options: { reasoning_effort: 'high' } },
},
};
assert.deepEqual(planModelReconciliation(existing, desiredLimits, desiredVariants), []);
});

test('an empty desired variants set never strips existing variants (#1799)', () => {
const existing = { limit: desiredLimits, variants: desiredVariants };
assert.deepEqual(planModelReconciliation(existing, desiredLimits, {}), []);
});