Skip to content

Commit 0e5fa59

Browse files
committed
fix(scripts): engine-double-contract vetoes a native Mongo Collection fake by its own members
A fake of MongoDB's native Collection (insertOne/insertMany/updateOne/ findOne/find) held two engine siblings and a two-parameter findOne, so the gate read it as an engine findOne double. NATIVE_COLLECTION_ONLY_MEMBERS is positive evidence of that shape, consulted under the same object-name guard as the scoped-repository veto: an engine double whose findOne takes the object name first stays in scope, and insertMany stays engine evidence. Claude-Session: https://claude.ai/code/session_01Utp6CXoPqLaG33jM98mL1R Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5852d49 commit 0e5fa59

1 file changed

Lines changed: 146 additions & 15 deletions

File tree

‎scripts/check-engine-double-contract.mjs‎

Lines changed: 146 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080
// is this gate's whole value (see the baseline's `$comment`: shrink-only, hand
8181
// reviewed).
8282
//
83+
// A fourth shape sits one layer BELOW the driver (#29859): MongoDB's native
84+
// `Collection`, faked by driver-mongodb tests to record what the driver sends.
85+
// `findOne(filter, options)` takes no object name, and `find`/`insertMany`
86+
// read as engine siblings, so it is vetoed by NATIVE_COLLECTION_ONLY_MEMBERS
87+
// under the same object-name guard -- out of scan scope, for the same reason.
88+
//
8389
// Deliberately NOT covered, and why (each wants its own slice, not a vaguer
8490
// version of these -- a gate whose scope is fuzzy is indistinguishable from
8591
// no gate to everyone downstream of it):
@@ -506,6 +512,39 @@ const ENGINE_ONLY_MEMBERS = new Set(['insert', 'insertMany', 'aggregate', 'getSc
506512
*/
507513
const REPOSITORY_ONLY_MEMBERS = new Set(['updateById', 'deleteById']);
508514

515+
/**
516+
* The fourth shape (#29859): MongoDB's native `Collection`, which a driver-
517+
* mongodb test fakes to record what the DRIVER sends (`(driver as any).db =
518+
* { collection: () => collection }`). Its read and batch-write names collide
519+
* with the engine's -- `find`, `findOne`, `insertMany`, `aggregate` -- so a
520+
* fake declaring `insertOne`/`insertMany`/`updateOne`/`findOne`/`find` holds
521+
* two engine siblings and reads as an engine `findOne` double whose
522+
* `findOne(filter, options)` takes no object name at all. It is not a looser
523+
* copy of `ObjectQL.findOne`; it is the driver's DEPENDENCY, one layer below
524+
* the driver, and has no dispatch contract to be looser than.
525+
*
526+
* `insertMany` stays engine evidence: `ObjectQL.insertMany(object, rows)` is a
527+
* real member of the engine class, and an engine double spelling it must keep
528+
* reading as one. The fix is positive evidence of the OTHER shape, not less
529+
* evidence of this one.
530+
*
531+
* Verified member by member on the tree this landed against: none of these
532+
* names is declared in `packages/objectql/src/engine.ts`, nor on `IDataEngine`,
533+
* `IDataDriver` or `IScopedObjectRepository` (`packages/spec/src/contracts/`).
534+
* Kept to the names only the native Collection speaks -- its single-document
535+
* writes, its atomic find-and-modify forms, its counts and `bulkWrite`. Its
536+
* `updateMany`/`deleteMany` are absent because DRIVER_ONLY_MEMBERS already
537+
* vetoes them at any arity; `distinct`/`createIndex`/`watch` are absent because
538+
* those names are too generic to answer "Mongo" in a repo with caches and
539+
* routers. Consulted under the same object-name guard as the repository veto,
540+
* so `findOne(objectName, query)` beside an `insertOne` helper stays in scope.
541+
*/
542+
const NATIVE_COLLECTION_ONLY_MEMBERS = new Set([
543+
'insertOne', 'updateOne', 'deleteOne', 'replaceOne', 'bulkWrite',
544+
'countDocuments', 'estimatedDocumentCount',
545+
'findOneAndUpdate', 'findOneAndDelete', 'findOneAndReplace',
546+
]);
547+
509548
/**
510549
* Parameter names that mean "this position holds an OBJECT NAME" — the
511550
* complement of ID_PARAM, and the first position rather than the second.
@@ -1016,6 +1055,16 @@ function isEngineVerbShape(fn, memberNames = new Set(), opts = {}) {
10161055
if (!opts.skipRepositoryVeto && !takesObjectNameFirst(fn)) {
10171056
for (const n of memberNames) if (REPOSITORY_ONLY_MEMBERS.has(n)) return false;
10181057
}
1058+
// The NATIVE COLLECTION veto (#29859): the same two halves for the same
1059+
// reasons. The member is the evidence (a name only Mongo's `Collection`
1060+
// speaks); the object-name test keeps an engine double that hangs an
1061+
// `insertOne` helper off its side in scope. It must decide here, before the
1062+
// arity paths below: `findOne(filter, options)` has a second parameter that is
1063+
// neither an id nor a string, so the two-parameter path would admit it on its
1064+
// own, and the short-arity path reads `insertMany` as engine evidence.
1065+
if (!opts.skipCollectionVeto && !takesObjectNameFirst(fn)) {
1066+
for (const n of memberNames) if (NATIVE_COLLECTION_ONLY_MEMBERS.has(n)) return false;
1067+
}
10191068
if (params.length < 2) {
10201069
let engineEvidence = false;
10211070
for (const n of memberNames) {
@@ -1651,23 +1700,30 @@ function censusRecognizer() {
16511700
// off and diff. The prose used to assert "the pair moves exactly 2, and 0 of
16521701
// the PINNED doubles" as a constant; this recomputes it on today's corpus,
16531702
// which is the only form of that claim worth having.
1654-
const vetoed = [];
1655-
for (const slice of SLICES) {
1656-
for (const abs of files) {
1657-
const rel = relative(ROOT, abs).split(sep).join('/');
1658-
const text = readFileSync(abs, 'utf8');
1659-
if (!mentionsVerb(text, slice.verb)) continue;
1660-
const withVeto = scanSource(abs, text, slice);
1661-
const without = scanSource(abs, text, slice, { skipRepositoryVeto: true });
1662-
if (without.length === withVeto.length) continue;
1663-
const kept = new Set(withVeto.map((d) => d.line));
1664-
for (const d of without) {
1665-
if (!kept.has(d.line)) vetoed.push({ verb: slice.verb, file: rel, line: d.line, pinned: d.pinned });
1703+
// The NATIVE_COLLECTION_ONLY_MEMBERS veto (#29859) is ablated the same way,
1704+
// separately, so each veto's reach is its own reading.
1705+
const ablate = (skip) => {
1706+
const moved = [];
1707+
for (const slice of SLICES) {
1708+
for (const abs of files) {
1709+
const rel = relative(ROOT, abs).split(sep).join('/');
1710+
const text = readFileSync(abs, 'utf8');
1711+
if (!mentionsVerb(text, slice.verb)) continue;
1712+
const withVeto = scanSource(abs, text, slice);
1713+
const without = scanSource(abs, text, slice, { [skip]: true });
1714+
if (without.length === withVeto.length) continue;
1715+
const kept = new Set(withVeto.map((d) => d.line));
1716+
for (const d of without) {
1717+
if (!kept.has(d.line)) moved.push({ verb: slice.verb, file: rel, line: d.line, pinned: d.pinned });
1718+
}
16661719
}
16671720
}
1668-
}
1721+
return moved;
1722+
};
1723+
const vetoed = ablate('skipRepositoryVeto');
1724+
const collectionVetoed = ablate('skipCollectionVeto');
16691725

1670-
return { files: files.length, members, byKind, byCallee, notWidened, assignSites, vetoed };
1726+
return { files: files.length, members, byKind, byCallee, notWidened, assignSites, vetoed, collectionVetoed };
16711727
}
16721728

16731729
function censusReport() {
@@ -1716,6 +1772,11 @@ function censusReport() {
17161772
+ `${c.vetoed.filter((v) => v.pinned).length} of them pinned:`,
17171773
);
17181774
for (const v of c.vetoed) console.log(` [${v.verb}] ${v.file}:${v.line}${v.pinned ? ' (pinned)' : ''}`);
1775+
console.log(
1776+
`\n NATIVE_COLLECTION_ONLY_MEMBERS veto moves ${c.collectionVetoed.length} construct(s); `
1777+
+ `${c.collectionVetoed.filter((v) => v.pinned).length} of them pinned:`,
1778+
);
1779+
for (const v of c.collectionVetoed) console.log(` [${v.verb}] ${v.file}:${v.line}${v.pinned ? ' (pinned)' : ''}`);
17191780
console.log('');
17201781
}
17211782

@@ -3696,6 +3757,7 @@ const SELF_TEST_BATTERIES = Object.freeze({
36963757
'Arity: a fake omits the parameters it ignores (#5629).': 9,
36973758
'The `update` slice (#5480).': 15,
36983759
'The SCOPED REPOSITORY, the third shape (#6327, from #5945).': 11,
3760+
'The NATIVE COLLECTION, the fourth shape (#29859).': 7,
36993761
'The CONSUMER SEAMS (#8194)': 20,
37003762
'The ratchet-remedy authority convention (#8435)': 2,
37013763
'The INHERITANCE clause (#8553)': 5,
@@ -3726,7 +3788,7 @@ const SELF_TEST_BATTERIES = Object.freeze({
37263788

37273789
// DELETING an entry silences that battery's floor exactly as effectively as
37283790
// zeroing it, so the roster's own size is pinned too.
3729-
const SELF_TEST_BATTERY_FLOOR = 34;
3791+
const SELF_TEST_BATTERY_FLOOR = 35;
37303792

37313793
// The key an assertion is filed under when no battery is open. It is not a
37323794
// declared battery, so it reds by the same set difference rather than silently
@@ -4306,6 +4368,75 @@ const engine = {
43064368
.some((f) => f.file === 'packages/plugins/plugin-sharing/src/sharing-rule.test.ts'),
43074369
);
43084370

4371+
// ── The NATIVE COLLECTION, the fourth shape (#29859) ──────────────────────
4372+
//
4373+
// A/B pairs again, for the same reason as the repository battery: every
4374+
// "out of scope" below is the SAME literal with one thing changed beside a
4375+
// control that IS discovered, so a veto that silenced everything fails here.
4376+
battery('The NATIVE COLLECTION, the fourth shape (#29859).');
4377+
const F = SLICES.find((s) => s.verb === 'findOne');
4378+
const collectionBody = `
4379+
insertMany: async (many: Doc[]) => ({}),
4380+
findOne: async (filter: Doc, options: { projection?: Record<string, number> }) => null,
4381+
find: (filter: Doc, options?: any) => ({ toArray: async () => [] }),`;
4382+
const nativeCollection = `const collection = {
4383+
insertOne: async (doc: Doc) => ({}),${collectionBody}
4384+
updateOne: async (filter: Doc, update: { $set?: Doc }) => ({ matchedCount: 0 }),
4385+
};
4386+
`;
4387+
const sameCollectionWithoutMarker = `const collection = {${collectionBody}
4388+
};
4389+
`;
4390+
expect('a five-member native Mongo Collection fake is out of scope for the findOne slice',
4391+
scanSource('nc.test.ts', nativeCollection, F).length === 0);
4392+
expect('…and the SAME literal without insertOne/updateOne is still discovered',
4393+
scanSource('nc.test.ts', sameCollectionWithoutMarker, F).length === 1);
4394+
4395+
// Both arity paths the veto has to outrank: a one-parameter `findOne(filter)`
4396+
// reaches the short-arity path, where `insertMany` alone is engine evidence.
4397+
const oneArityCollection = `const collection = {
4398+
insertOne: async (doc: any) => ({}),
4399+
insertMany: async (many: any[]) => ({}),
4400+
findOne: async (filter: any) => null,
4401+
find: (filter: any) => ({ toArray: async () => [] }),
4402+
};
4403+
`;
4404+
expect('a one-parameter native findOne beside insertMany is out of scope',
4405+
scanSource('nc1.test.ts', oneArityCollection, F).length === 0);
4406+
expect('…and the same without insertOne is discovered',
4407+
scanSource('nc1.test.ts', oneArityCollection.replace(' insertOne: async (doc: any) => ({}),\n', ''), F)
4408+
.length === 1);
4409+
4410+
// The control the card names: a real engine double holding `find` and
4411+
// `insertMany` plus engine members is still attributed, still unpinned.
4412+
const engineWithInsertMany = `
4413+
const engine = {
4414+
async find(objectName: string, q?: any) { return []; },
4415+
async insertMany(objectName: string, rows: any[]) { return rows; },
4416+
async insert(objectName: string, data: any) { return data; },
4417+
async findOne(objectName: string, q?: any) { return null; },
4418+
};
4419+
`;
4420+
d = scanSource('em.test.ts', engineWithInsertMany, F);
4421+
expect('an engine double with find/insertMany is still a findOne double',
4422+
d.length === 1 && d[0].pinned === false);
4423+
4424+
// The member is not a licence by itself: an engine double that takes the
4425+
// object name first stays in scope however many Mongo-named helpers it adds.
4426+
const engineWithInsertOneHelper = engineWithInsertMany.replace(
4427+
' async insert(',
4428+
' async insertOne(doc: any) { return doc; },\n async insert(',
4429+
);
4430+
d = scanSource('eo.test.ts', engineWithInsertOneHelper, F);
4431+
expect('an engine double that ALSO declares insertOne stays in scope',
4432+
engineWithInsertOneHelper.includes('insertOne') && d.length === 1 && d[0].pinned === false);
4433+
4434+
// Exact-set form: both shapes in one file, only the engine reported.
4435+
const mixedCollection = `${nativeCollection}${engineWithInsertMany}`;
4436+
const mixedCollectionFound = scanSource('mc.test.ts', mixedCollection, F);
4437+
expect('a mixed fixture reports EXACTLY the engine double, not the native Collection',
4438+
mixedCollectionFound.length === 1 && mixedCollectionFound[0].line === 9);
4439+
43094440
// ── The CONSUMER SEAMS (#8194) ────────────────────────────────────────────
43104441
//
43114442
// Driven on both sides of all four decisions the seam scan makes — the three

0 commit comments

Comments
 (0)