Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(buddy): db-size prints MongoDB Long values as plain numbers (#2343)

0xken15119f59d0ddba
diff --git a/cli/src/lib/buddyRecipes.spec.ts b/cli/src/lib/buddyRecipes.spec.ts
index c26599566..138445137 100644
--- a/cli/src/lib/buddyRecipes.spec.ts
+++ b/cli/src/lib/buddyRecipes.spec.ts
@@ -128,7 +128,7 @@ describe('harness buddy-read db-size', () => {
describe('the db-size script', () => {
const READ = ['find', 'listCollections', 'listIndexes', 'collStats', 'dbStats', 'killCursors']
type Privilege = { resource: Record<string, unknown>; actions: string[] }
- function database(roles: string[], collections: Record<string, number>, attachmentBytes: number | null, actions: string[] | Privilege[] = READ) {
+ function database(roles: string[], collections: Record<string, unknown>, attachmentBytes: unknown, actions: string[] | Privilege[] = READ) {
const calls: unknown[] = []
const privileges = typeof actions[0] === 'object' ? actions as Privilege[] : [{ resource: { db: '', collection: '' }, actions: actions as string[] }]
const db = {
@@ -155,6 +155,37 @@ describe('the db-size script', () => {
expect(calls).toEqual([URL, { connectionStatus: 1, showPrivileges: true }, [{ $group: { _id: null, bytes: { $sum: '$size' } } }]])
})
+ // MongoDB's 64-bit integer as mongosh hands it over (bson's Long: low and high 32-bit parts, unsigned), with and without its
+ // methods. On Ken's Mac (CLI 0.5.17, 10-30) the attachments' $sum came back as one and printed as {"low":…,"high":…}.
+ class Long {
+ _bsontype = 'Long'
+ constructor(readonly low: number, readonly high: number, readonly unsigned = false) {}
+ toNumber() { return this.unsigned ? (this.high >>> 0) * 2 ** 32 + (this.low >>> 0) : this.high * 2 ** 32 + (this.low >>> 0) }
+ toString() { return String((BigInt(this.unsigned ? this.high >>> 0 : this.high) << 32n) + BigInt(this.low >>> 0)) }
+ }
+ const FIVE_GIB = new Long(1_073_741_824, 1)
+
+ it.each([
+ ['a Long', FIVE_GIB, 5_368_709_120],
+ ['a Long without its methods', { low: 1_073_741_824, high: 1, unsigned: false }, 5_368_709_120],
+ ['a Long whose low part reads as negative', new Long(-1, 0), 4_294_967_295],
+ ['an unsigned Long', new Long(5, 3, true), 3 * 2 ** 32 + 5],
+ ['a 32-bit integer object', { valueOf: (): number => 2048 }, 2048],
+ ['a 128-bit decimal', { _bsontype: 'Decimal128', toString: () => '2048' }, 2048],
+ ['a bigint', 2048n, 2048],
+ ])('prints the attachments\' bytes given as %s as a plain number', (_, bytes, want) => {
+ const { printed, quit } = database(['read'], { SuperTab: new Long(12, 0) }, bytes)
+ expect(quit).toBeNull()
+ expect(printed.join('')).not.toContain('low')
+ expect(JSON.parse(printed.join(''))).toEqual({ database: 'harness', collections: { SuperTab: 12 }, attachmentBytes: want })
+ })
+
+ // Past 2^53 a plain number would be rounded without saying so: the exact value prints as a string of digits instead.
+ it.each([['a Long', new Long(1, 2 ** 21), '9007199254740993'], ['an unsigned Long with its top bit set', new Long(0, -(2 ** 31), true), '9223372036854775808']])(
+ 'prints %s past what a plain number holds exactly as its exact digits', (_, bytes, want) => {
+ expect(JSON.parse(database(['read'], {}, bytes).printed.join('')).attachmentBytes).toBe(want)
+ })
+
it('counts no attachments as 0 bytes', () => {
expect(JSON.parse(database(['read'], {}, null).printed.join('')).attachmentBytes).toBe(0)
})
diff --git a/cli/src/lib/buddyRecipes.ts b/cli/src/lib/buddyRecipes.ts
index 359075c15..bb3111e9b 100644
--- a/cli/src/lib/buddyRecipes.ts
+++ b/cli/src/lib/buddyRecipes.ts
@@ -33,10 +33,21 @@ export const DB_SIZE_SCRIPT = [
'const actions = [].concat(...(auth.authenticatedUserPrivileges || []).map((p) => p.actions || []));',
`if (!roles.length || !actions.length || roles.some((role) => !${JSON.stringify(READ_ROLES)}.includes(role))`
+ ` || actions.some((action) => !${JSON.stringify(READ_ACTIONS)}.includes(action))) { print(JSON.stringify({ refused: 'not read-only', roles })); quit(3) }`,
+ // A number as JSON prints it. MongoDB hands 64-bit integers over as bson Longs (low and high 32-bit parts), which printed
+ // as {"low":…,"high":…} on Ken's Mac (CLI 0.5.17, 10-30); past 2^53 a plain number would round, so those print as digits.
+ 'const plain = (value) => {',
+ ' if (value === null || value === undefined) return 0;',
+ " if (typeof value === 'number') return value;",
+ " if (typeof value === 'object' && value._bsontype === 'Decimal128') return Number(String(value));",
+ " const big = typeof value === 'bigint' ? value : typeof value === 'object' && Number.isInteger(value.low) && Number.isInteger(value.high)",
+ ' ? (BigInt(value.unsigned ? value.high >>> 0 : value.high) << 32n) + BigInt(value.low >>> 0) : null;',
+ ' if (big === null) return Number(value);',
+ ' return big >= BigInt(Number.MIN_SAFE_INTEGER) && big <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(big) : String(big);',
+ '};',
'const collections = {};',
- 'for (const name of db.getCollectionNames().sort()) collections[name] = db.getCollection(name).estimatedDocumentCount();',
+ 'for (const name of db.getCollectionNames().sort()) collections[name] = plain(db.getCollection(name).estimatedDocumentCount());',
`const total = db.SuperTabAttachment.aggregate([{ $group: { _id: null, bytes: { $sum: '$size' } } }]).toArray()[0];`,
- 'print(JSON.stringify({ database: db.getName(), collections, attachmentBytes: total ? total.bytes : 0 }));',
+ 'print(JSON.stringify({ database: db.getName(), collections, attachmentBytes: total ? plain(total.bytes) : 0 }));',
].join('\n')
/** Where the read-only login is kept, in the person's home, and its variable. One fixed place, wherever the command runs:
diff --git a/docs/specs/2026-10-30-buddy-read-access.md b/docs/specs/2026-10-30-buddy-read-access.md
index 8f52a12bf..312b90210 100644
--- a/docs/specs/2026-10-30-buddy-read-access.md
+++ b/docs/specs/2026-10-30-buddy-read-access.md
@@ -131,7 +131,8 @@ The measurements use Juno's read-only methods (Lead, 10-30), nothing wider:
`changeStream`, `collStats`, `dbHash`, `dbStats`, `find`, `killCursors`, `listCollections`, `listIndexes`,
`listSearchIndexes`, `planCacheRead`, and `listDatabases` on the cluster; 8.2's `performRawDataOperations` is left out); `clusterMonitor` is refused, as its actions include setting
changes. It reads `estimatedDocumentCount` per collection and a `$sum` of `size` on
- `SuperTabAttachment`, and prints no connection string, its errors included.
+ `SuperTabAttachment`, and prints no connection string, its errors included. MongoDB's 64-bit integers (bson `Long`)
+ print as plain numbers, or as exact digits past 2^53, never as their low and high parts (Ken's Mac, CLI 0.5.17).
Rulings:
- No recipe for CPU. Why: a fixed command needs no wrapper. Cost if wrong: the Buddy sums the per-pod numbers itself.