@bongos/core 1.19.733 → 1.19.734

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,6 +128,12 @@ function discoveredRegistry(opts) {
128
128
  _dir: dir,
129
129
  _provides: manifest.provides || [],
130
130
  _consumes: manifest.consumes || [],
131
+ // The module's own declaration that it METERS money on a caller's behalf
132
+ // (task 1003884). Projected here because src/modules.js refuses to enable
133
+ // such a module where anyone can sign up and nobody is on the hook — and a
134
+ // kernel file may not name a module key (fitness Check 10), so the
135
+ // declaration has to travel WITH the manifest rather than be looked up.
136
+ _spend: manifest.spend || {},
131
137
  };
132
138
  }
133
139
  return out;
@@ -39,6 +39,12 @@
39
39
  // // the APEX ROOT — never both (ADR 0218).
40
40
  // "migrations": true // true => modules/<key>/migrations/ exists (or an array of names)
41
41
  // },
42
+ // "spend": { // OPTIONAL. This module METERS money on a caller's behalf
43
+ // "requiresPayer": true // (task 1003884) — its surfaces can spend real money for
44
+ // }, // whoever calls them, so the core must know WHO PAYS before
45
+ // // it may be enabled where anyone can sign up. src/modules.js
46
+ // // refuses the enablement when the join door is `open` and no
47
+ // // payer is configured.
42
48
  // "provides": ["reward"], // OPTIONAL. seam PORTS this module registers a provider for.
43
49
  // "consumes": ["grade"], // OPTIONAL. seam ports this module resolves (required capabilities).
44
50
  // "prerequisites": { "modules": ["economy"] }, // OPTIONAL. other modules that must be enabled.
@@ -77,6 +83,16 @@ const ATTRIBUTION_KEYS = ['author', 'origin', 'license', 'maintainer'];
77
83
  // optionally a `removeAfter` core version.
78
84
  // orphaned nobody owns it. Still needs a `note`/`successor` so the
79
85
  // next reader knows the situation, not just the label.
86
+ // The `spend` block (task 1003884). A module declaring `requiresPayer: true` is
87
+ // saying its surfaces can spend real money on behalf of whoever calls them — so
88
+ // the core refuses to ENABLE it on an instance whose join door is `open` with no
89
+ // configured payer (src/modules.js). Closed key set for the same reason
90
+ // `contributes` and `maintenance` have one: a typo'd "requirespayer" would
91
+ // silently declare NO spend, and that is the direction that costs money.
92
+ // Deliberately NOT exported: the validator and its own error message are the only
93
+ // readers, and an export nothing imports is dead weight the knip ratchet counts.
94
+ const SPEND_KEYS = ['requiresPayer'];
95
+
80
96
  const MAINTENANCE_STATUSES = ['core-maintained', 'maintained', 'deprecated', 'orphaned'];
81
97
  const MAINTENANCE_KEYS = ['status', 'since', 'removeAfter', 'successor', 'note'];
82
98
  // Statuses that describe a module on its way out — these owe a deprecation path.
@@ -190,6 +206,20 @@ function validateManifest(obj) {
190
206
  }
191
207
  }
192
208
 
209
+ // --- optional spend declaration (task 1003884) ---
210
+ if ('spend' in obj) {
211
+ const sp = obj.spend;
212
+ if (!isObj(sp)) E('spend: must be an object (e.g. { "requiresPayer": true })');
213
+ else {
214
+ for (const k of Object.keys(sp)) {
215
+ if (!SPEND_KEYS.includes(k)) E(`spend.${k}: unknown key (allowed: ${SPEND_KEYS.join(', ')})`);
216
+ }
217
+ if ('requiresPayer' in sp && typeof sp.requiresPayer !== 'boolean') {
218
+ E('spend.requiresPayer: must be a boolean when present');
219
+ }
220
+ }
221
+ }
222
+
193
223
  // --- optional seam declarations ---
194
224
  if ('provides' in obj && !isStrArray(obj.provides)) E('provides: must be an array of non-empty strings (port names)');
195
225
  if ('consumes' in obj && !isStrArray(obj.consumes)) E('consumes: must be an array of non-empty strings (port names)');
package/src/modules.js CHANGED
@@ -118,10 +118,75 @@ function coerceBool(v) {
118
118
  return undefined;
119
119
  }
120
120
 
121
+ // WHO PAYS for metered module spend on this instance (task 1003884). Two
122
+ // answers are a payer and the absence of one is not: `project` puts the bill on
123
+ // whoever runs the instance, `builder` on the caller who spent it. Anything else
124
+ // — unset, empty, a typo, a value from a newer core — is NO payer, which is the
125
+ // reading that costs nobody money by accident.
126
+ const PAYERS = Object.freeze(['project', 'builder']);
127
+
128
+ // Does this registry entry declare that its surfaces spend real money on behalf
129
+ // of whoever calls them? The declaration lives in the module's own module.json
130
+ // (`"spend": { "requiresPayer": true }`) and rides through the loader as `_spend`,
131
+ // because fitness Check 10 forbids the kernel from naming a module key — the
132
+ // guard below has to be true of any module that says this about itself, not of
133
+ // one the resolver has heard of.
134
+ const metersSpend = (entry) => !!(entry && entry._spend && entry._spend.requiresPayer === true);
135
+
136
+ // The spend guard's own posture. `joinDoorOpen` is "any GitHub account becomes a
137
+ // builder seconds after signing up" — the composed join door, not the
138
+ // `joinability` knob alone (src/bongos/project-door.js).
139
+ const NO_SPEND_RISK = Object.freeze({ joinDoorOpen: false, payer: null });
140
+
141
+ // WHICH enabled modules must be REFUSED, and why this is a config-load decision.
142
+ //
143
+ // A module that meters spend for its callers is safe in exactly two situations:
144
+ // the people who can call it were let in deliberately (the door is not open), or
145
+ // somebody is on the hook for the bill (a payer is configured, and the module's
146
+ // own per-payer ceiling bounds it). With an OPEN door and NO payer, an account
147
+ // created seconds ago can spend the instance's money with nobody to bill and no
148
+ // cap to stop it — so the enablement itself is refused.
149
+ //
150
+ // AT CONFIG LOAD, NOT AT FIRST CALL. A per-request check would let the module
151
+ // mount, advertise its routes in the manifest, run its migrations and appear
152
+ // enabled everywhere the UI reads `modules.enabled` — and then refuse one caller
153
+ // at a time, which is a broken feature rather than a closed door. Refusing the
154
+ // enablement means the surface never exists.
155
+ //
156
+ // PURE, and exported for direct unit testing: the fail-closed reading of an
157
+ // UNKNOWN posture belongs to loadModules, which is where "unknown" can arise.
158
+ function spendRefusals(resolved, registry, { joinDoorOpen = false, payer = null } = {}) {
159
+ if (!joinDoorOpen) return [];
160
+ if (PAYERS.includes(payer)) return [];
161
+ return Object.keys(resolved).filter((k) => resolved[k] === true && metersSpend(registry[k]));
162
+ }
163
+
164
+ // The refusal is LOUD. A module silently resolving to off is how an operator
165
+ // spends an afternoon wondering why their routes 404 — and this one is refusing
166
+ // something they explicitly asked for, so it has to say what to do about it.
167
+ // `log` is injectable for the same reason instance-config's legacy-env warning
168
+ // injects it: a test asserts the warning fired, not only that the value resolved.
169
+ function warnSpendRefusal(key, envPrefix, log = console) {
170
+ log.warn(
171
+ `[bongos] module "${key}" is enabled in config but was REFUSED: it spends money on behalf of `
172
+ + `whoever calls it, this project's join door is \`open\` (anyone may sign up), and no payer is `
173
+ + `configured — so a stranger's spend would have nobody to bill. Set ${envPrefix}_SPEND_PAYER to `
174
+ + `${PAYERS.join(' or ')} (the platform pushes it from the project's \`spend_payer\` setting), or `
175
+ + `close the join door. The module stays OFF until then.`,
176
+ );
177
+ }
178
+
121
179
  // Pure resolver — all inputs injected, so tests drive it with zero I/O.
122
180
  // Returns a frozen { <moduleKey>: boolean } covering EVERY registry key, plus a
123
181
  // non-enumerable resolution that throws on an unknown module key (typo guard).
124
- function resolveModules({ neutral = {}, instance = {}, env = {}, envPrefix = FALLBACK_ENV_PREFIX, registry = MODULE_REGISTRY } = {}) {
182
+ function resolveModules({
183
+ neutral = {}, instance = {}, env = {}, envPrefix = FALLBACK_ENV_PREFIX, registry = MODULE_REGISTRY,
184
+ // The spend guard's inputs (task 1003884). Defaulted to "no risk" so every
185
+ // caller that does not know about spend resolves exactly as before; loadModules
186
+ // is the one that reads the real posture, and the one that fails closed when it
187
+ // cannot.
188
+ spendGuard = NO_SPEND_RISK, warn = warnSpendRefusal,
189
+ } = {}) {
125
190
  // The known-key set + defaults come from the passed registry: callers inject
126
191
  // the built-in MODULE_REGISTRY (the default, what the pure unit tests use) or
127
192
  // the effective built-in+discovered registry (what loadModules passes). This
@@ -175,6 +240,14 @@ function resolveModules({ neutral = {}, instance = {}, env = {}, envPrefix = FAL
175
240
  const v = key in merged ? coerceBool(merged[key]) : undefined;
176
241
  resolved[key] = v === undefined ? registry[key].default : v;
177
242
  }
243
+ // THE SPEND GUARD, applied LAST — after config, after env, after the default.
244
+ // It is deliberately not another precedence level: a refusal that env could
245
+ // outrank would be a suggestion, and the whole point is that the one knob an
246
+ // untrusted-but-cheap deploy would reach for cannot turn it back on.
247
+ for (const key of spendRefusals(resolved, registry, spendGuard)) {
248
+ resolved[key] = false;
249
+ warn(key, envPrefix);
250
+ }
178
251
  return Object.freeze(resolved);
179
252
  }
180
253
 
@@ -220,9 +293,37 @@ function loadModules({ neutralPath = NEUTRAL_PATH, instancePath, env = process.e
220
293
  // Reuse the branding env prefix so an instance has ONE prefix, not two.
221
294
  let envPrefix = FALLBACK_ENV_PREFIX;
222
295
  try { envPrefix = require('./branding').branding().envPrefix || envPrefix; } catch { /* fail-to-default */ }
296
+ // THE SPEND POSTURE, read once per load (task 1003884): is this project's
297
+ // composed join door `open`, and has the owner said who pays?
298
+ //
299
+ // `effectiveJoinDoor() === 'open'` is LITERALLY the expression openEnrollmentEnabled
300
+ // is defined as (src/bongos/auth-admission.js), read from the composer rather than
301
+ // through the gate so this file does not pull in the whole sign-in stack. That
302
+ // identity is the point: the guard and the gate that actually admits strangers
303
+ // cannot answer one stranger differently, which is the same argument task 1003579
304
+ // made when it moved the gate onto the composed door.
305
+ //
306
+ // WHAT THE CATCH COVERS, stated precisely rather than generously. A pack that
307
+ // fails to PARSE never reaches here — effectiveJoinDoor swallows that itself and
308
+ // resolves the default door, `apply`, which is closed. That is not a fail-open
309
+ // hole: the real admission gate reads the same broken pack and refuses the same
310
+ // self-enrolment, so a door this guard reads as closed IS closed. The catch is
311
+ // for the harder failure — the require itself throwing, in tooling that has no
312
+ // such file — and THERE the default is the dangerous reading rather than the
313
+ // convenient one, because the thing being guarded is money leaving the account.
314
+ // The blast radius of being wrong is one module resolving OFF, and since every
315
+ // module defaults to off, an instance that never enabled one notices nothing.
316
+ let spendGuard = { joinDoorOpen: true, payer: null };
317
+ try {
318
+ // Both are KERNEL files (fitness Check 12), required lazily for the same
319
+ // reason branding is just above: require-time must never crash tooling.
320
+ const { effectiveJoinDoor } = require('./bongos/project-door'); // eslint-disable-line global-require
321
+ const project = require('./branding').branding().project || {}; // eslint-disable-line global-require
322
+ spendGuard = { joinDoorOpen: effectiveJoinDoor() === 'open', payer: project.spendPayer || null };
323
+ } catch { /* unreadable posture — the fail-closed default above stands */ }
223
324
  // Resolve against the EFFECTIVE registry (built-in + discovered) so a config
224
325
  // flag for a discovered module is honored. No-op until a module exists.
225
- return resolveModules({ neutral, instance, env, envPrefix, registry: effectiveRegistry() });
326
+ return resolveModules({ neutral, instance, env, envPrefix, registry: effectiveRegistry(), spendGuard });
226
327
  }
227
328
 
228
329
  // Resolved singleton for app code; lazy so require-time never crashes tooling.
@@ -282,6 +383,10 @@ module.exports = {
282
383
  effectiveRegistry,
283
384
  knownModules,
284
385
  CORE_DISCIPLINES,
386
+ PAYERS,
387
+ metersSpend,
388
+ spendRefusals,
389
+ warnSpendRefusal,
285
390
  resolveModules,
286
391
  loadModules,
287
392
  modules,
@@ -0,0 +1,272 @@
1
+ // tests/agents_spend_guard.mjs — the spend guard and the per-payer spend ceiling
2
+ // (task 1003884).
3
+ //
4
+ // Two independent walls, tested apart because they fail in different directions:
5
+ //
6
+ // 1. the CONFIG-LOAD guard (src/modules.js) — a module that declares it meters
7
+ // money for its callers is not ENABLED at all on an instance whose join door
8
+ // is `open` with no payer configured;
9
+ // 2. the per-payer MONTHLY CEILING (modules/agents/lib/spend-ceiling.js) — a
10
+ // payer who has spent their month cannot fire, and so, deliberately, cannot
11
+ // anyone whose ceiling could not be established.
12
+ //
13
+ // The second is where task 1001393's defect is pinned: that spend brake read
14
+ // month-to-date spend from a database it could not reach and FAILED OPEN, so it
15
+ // never tripped. Several cases below exist only to assert this one fails closed.
16
+ import assert from 'node:assert/strict';
17
+ import { test } from 'node:test';
18
+ import { readFileSync } from 'node:fs';
19
+ // Imported BY NAME rather than destructured off a default import: knip cannot
20
+ // follow a name through `import mod from '...'` + `const { x } = mod`, so the
21
+ // guard's tested exports would read as dead code and the ratchet would block the
22
+ // merge on a file whose every export is genuinely used right here.
23
+ import { resolveModules, spendRefusals, metersSpend, PAYERS, warnSpendRefusal } from '../src/modules.js';
24
+ import { validateManifest } from '../src/module-loader/manifest-schema.js';
25
+ import ceilingMod from '../modules/agents/lib/spend-ceiling.js';
26
+ import spawnMod from '../modules/agents/spawn.js';
27
+
28
+ const { createSpendCeiling, payerFor, monthStartUtc, DEFAULT_CAP_USD } = ceilingMod;
29
+ const { plannedRow } = spawnMod;
30
+
31
+ // A registry with one metered module and one ordinary one, so every assertion can
32
+ // show that the guard bites the declaring module and NOTHING else.
33
+ const REG = {
34
+ metered: { default: false, contributes: {}, _spend: { requiresPayer: true } },
35
+ plain: { default: false, contributes: {} },
36
+ };
37
+ const BOTH_ON = { modules: { metered: true, plain: true } };
38
+ const silent = () => {};
39
+ const resolve = (spendGuard, extra = {}) =>
40
+ resolveModules({ instance: BOTH_ON, registry: REG, spendGuard, warn: silent, ...extra });
41
+
42
+ // ---------------------------------------------------------------------------
43
+ // 1. the config-load guard
44
+ // ---------------------------------------------------------------------------
45
+
46
+ test('an OPEN join door with NO payer refuses the metered module — and only that one', () => {
47
+ const m = resolve({ joinDoorOpen: true, payer: null });
48
+ assert.equal(m.metered, false, 'anyone can sign up and nobody is on the hook — it must not mount');
49
+ assert.equal(m.plain, true, 'a module that never said it spends money is untouched');
50
+ });
51
+
52
+ test('a CLOSED join door leaves it enabled: the people who can call it were let in deliberately', () => {
53
+ assert.equal(resolve({ joinDoorOpen: false, payer: null }).metered, true);
54
+ });
55
+
56
+ test('an open door WITH a payer leaves it enabled — somebody is on the hook, and the ceiling bounds them', () => {
57
+ for (const payer of PAYERS) {
58
+ assert.equal(resolve({ joinDoorOpen: true, payer }).metered, true, payer);
59
+ }
60
+ });
61
+
62
+ test('anything that is not one of the two payer words is NO payer', () => {
63
+ // The direction that matters: a typo, a stale value, or a word a newer core
64
+ // understands must never read as "somebody is paying".
65
+ for (const bad of ['Project', 'BUILDER', 'none', '', ' project', 'owner', null, undefined, 0, true]) {
66
+ assert.equal(resolve({ joinDoorOpen: true, payer: bad }).metered, false, JSON.stringify(bad));
67
+ }
68
+ });
69
+
70
+ test('the refusal outranks the env override — it is a wall, not another precedence level', () => {
71
+ // <PREFIX>_MODULE_METERED=1 is the highest-precedence way to turn a module on,
72
+ // and it is exactly the knob a cheap deploy reaches for. It must not lift this.
73
+ const m = resolveModules({
74
+ instance: { modules: { metered: false } },
75
+ env: { BONGOS_MODULE_METERED: '1' },
76
+ envPrefix: 'BONGOS',
77
+ registry: REG,
78
+ spendGuard: { joinDoorOpen: true, payer: null },
79
+ warn: silent,
80
+ });
81
+ assert.equal(m.metered, false, 'env cannot buy its way past the guard');
82
+ });
83
+
84
+ test('the refusal is LOUD: it names the module, the two payer words and the env var that lifts it', () => {
85
+ const said = [];
86
+ resolveModules({
87
+ instance: BOTH_ON, registry: REG, envPrefix: 'MERCURY',
88
+ spendGuard: { joinDoorOpen: true, payer: null },
89
+ warn: (key, prefix) => said.push([key, prefix]),
90
+ });
91
+ assert.deepEqual(said, [['metered', 'MERCURY']], 'exactly the refused module, under this pack\'s prefix');
92
+
93
+ // And the default warner writes something an operator can act on, under the
94
+ // instance's OWN prefix rather than a hardcoded one.
95
+ const lines = [];
96
+ warnSpendRefusal('metered', 'MERCURY', { warn: (s) => lines.push(s) });
97
+ assert.equal(lines.length, 1);
98
+ assert.match(lines[0], /metered/);
99
+ assert.match(lines[0], /MERCURY_SPEND_PAYER/, 'names the variable, under the right prefix');
100
+ for (const p of PAYERS) assert.match(lines[0], new RegExp(p), `names the ${p} payer`);
101
+ });
102
+
103
+ test('callers that know nothing about spend resolve exactly as before', () => {
104
+ // Every existing call site passes no spendGuard. The default must change nothing
105
+ // — the fail-closed reading belongs to loadModules, where "unknown" can arise.
106
+ assert.equal(resolveModules({ instance: BOTH_ON, registry: REG }).metered, true);
107
+ });
108
+
109
+ test('metersSpend reads the module\'s own declaration, and only a literal true', () => {
110
+ assert.equal(metersSpend({ _spend: { requiresPayer: true } }), true);
111
+ for (const bad of [undefined, null, {}, { _spend: {} }, { _spend: { requiresPayer: 'true' } }, { _spend: { requiresPayer: 1 } }]) {
112
+ assert.equal(metersSpend(bad), false, JSON.stringify(bad));
113
+ }
114
+ });
115
+
116
+ test('spendRefusals never names a module that was already off', () => {
117
+ const off = { metered: false, plain: false };
118
+ assert.deepEqual(spendRefusals(off, REG, { joinDoorOpen: true, payer: null }), []);
119
+ });
120
+
121
+ test('the manifest declaration is a CLOSED key set — a typo must not read as "no spend"', () => {
122
+ const base = { key: 'x', title: 't', description: 'd', coreVersion: '^1.0.0', contributes: {} };
123
+ assert.equal(validateManifest({ ...base, spend: { requiresPayer: true } }).valid, true);
124
+ assert.equal(validateManifest({ ...base, spend: { requirespayer: true } }).valid, false,
125
+ 'a misspelled key silently declaring NO spend is the direction that costs money');
126
+ assert.equal(validateManifest({ ...base, spend: { requiresPayer: 'yes' } }).valid, false);
127
+ assert.equal(validateManifest({ ...base, spend: true }).valid, false);
128
+ });
129
+
130
+ test('the agents module actually declares it — the guard guards something', () => {
131
+ // The whole chain is inert if the manifest never says so, and that failure is
132
+ // invisible: everything still resolves, nothing is ever refused.
133
+ const manifest = JSON.parse(readFileSync(new URL('../modules/agents/module.json', import.meta.url), 'utf8'));
134
+ assert.equal(manifest.spend && manifest.spend.requiresPayer, true);
135
+ assert.equal(manifest.default, false, 'and it still ships disabled');
136
+ });
137
+
138
+ // ---------------------------------------------------------------------------
139
+ // 2. who pays for one fire
140
+ // ---------------------------------------------------------------------------
141
+
142
+ test('payerFor: `builder` bills the caller, everything else bills the project', () => {
143
+ assert.deepEqual(payerFor('builder', 7), { payer_kind: 'builder', payer_builder_id: 7 });
144
+ assert.deepEqual(payerFor('project', 7), { payer_kind: 'project', payer_builder_id: null });
145
+ assert.deepEqual(payerFor('none', 7), { payer_kind: 'project', payer_builder_id: null });
146
+ assert.deepEqual(payerFor(null, 7), { payer_kind: 'project', payer_builder_id: null });
147
+ });
148
+
149
+ test('an event fire has no caller, so it can only be the project — never nobody', () => {
150
+ // Nobody asked for it, so there is nobody to bill; falling to "no payer" would
151
+ // make the event path the one unbounded route through the ceiling.
152
+ assert.deepEqual(payerFor('builder', null), { payer_kind: 'project', payer_builder_id: null });
153
+ assert.deepEqual(payerFor('builder', undefined), { payer_kind: 'project', payer_builder_id: null });
154
+ });
155
+
156
+ test('every ledger row carries a payer — including a no-go, which cost nothing', () => {
157
+ const go = plannedRow({ definition: { id: 1, name: 'a' }, gate: { decision: 'go' }, requestedByBuilderId: 4, spendPayer: 'builder' });
158
+ assert.equal(go.payer_kind, 'builder');
159
+ assert.equal(go.payer_builder_id, 4);
160
+
161
+ const nogo = plannedRow({ definition: { id: 1, name: 'a' }, gate: { decision: 'no-go', reason: 'x' }, requestedByBuilderId: 4, spendPayer: 'builder' });
162
+ assert.equal(nogo.status, 'skipped');
163
+ assert.equal(nogo.payer_kind, 'builder', 'a refusal is still a fire somebody would have been billed for');
164
+ });
165
+
166
+ test('who ASKED and who PAYS are different columns, and differ under a project payer', () => {
167
+ const row = plannedRow({ definition: { id: 1, name: 'a' }, gate: { decision: 'go' }, requestedByBuilderId: 9, spendPayer: 'project' });
168
+ assert.equal(row.requested_by_builder_id, 9);
169
+ assert.equal(row.payer_kind, 'project');
170
+ assert.equal(row.payer_builder_id, null, 'the project pays; the asker is recorded but not billed');
171
+ });
172
+
173
+ // ---------------------------------------------------------------------------
174
+ // 3. the per-payer monthly ceiling — every branch fails CLOSED
175
+ // ---------------------------------------------------------------------------
176
+
177
+ const poolOf = (spent) => ({ query: async () => ({ rows: [{ spent }] }) });
178
+ const AT = Date.UTC(2026, 8, 13, 12, 0, 0); // 2026-09-13
179
+ const PROJECT = { payer_kind: 'project', payer_builder_id: null };
180
+
181
+ test('under the cap: allowed, with the arithmetic shown', () => {
182
+ const c = createSpendCeiling({ capUsd: 25, now: () => AT });
183
+ return c.check(poolOf('4.5000'), PROJECT).then((r) => {
184
+ assert.equal(r.ok, true);
185
+ assert.equal(r.spentUsd, 4.5);
186
+ assert.equal(r.remainingUsd, 20.5);
187
+ assert.equal(r.reason, null);
188
+ });
189
+ });
190
+
191
+ test('at or past the cap: refused, and the refusal says which', async () => {
192
+ const c = createSpendCeiling({ capUsd: 25, now: () => AT });
193
+ for (const spent of ['25', '25.0001', '900']) {
194
+ const r = await c.check(poolOf(spent), PROJECT);
195
+ assert.equal(r.ok, false, spent);
196
+ assert.equal(r.reason, 'spend_cap_reached', spent);
197
+ assert.equal(r.remainingUsd, 0, spent);
198
+ }
199
+ });
200
+
201
+ test('AN UNREADABLE LEDGER REFUSES — the exact defect task 1001393 was filed about', async () => {
202
+ // That brake read month-to-date spend from a DB it could not reach and fell
203
+ // through to "go ahead". "I cannot tell what has been spent" is the moment a
204
+ // ceiling matters most.
205
+ const throws = { query: async () => { throw new Error('ECONNREFUSED'); } };
206
+ const r = await createSpendCeiling({ capUsd: 25, now: () => AT }).check(throws, PROJECT);
207
+ assert.equal(r.ok, false);
208
+ assert.equal(r.reason, 'ledger_unreadable');
209
+ assert.equal(r.spentUsd, null, 'it must not claim a sum it does not have');
210
+ });
211
+
212
+ test('a missing pool, and a sum that is not a number, are the same "I do not know"', async () => {
213
+ const c = createSpendCeiling({ capUsd: 25, now: () => AT });
214
+ assert.equal((await c.check(null, PROJECT)).reason, 'ledger_unreadable');
215
+ assert.equal((await c.check({}, PROJECT)).reason, 'ledger_unreadable');
216
+ for (const junk of ['abc', null, undefined, -1]) {
217
+ assert.equal((await c.check(poolOf(junk), PROJECT)).ok, false, JSON.stringify(junk));
218
+ }
219
+ });
220
+
221
+ test('NO CAP IS NOT AN INFINITE CAP — an unbounded ceiling is the defect wearing a cap\'s name', async () => {
222
+ for (const cap of [0, -5, Number.NaN, Number.POSITIVE_INFINITY, null, 'lots']) {
223
+ const r = await createSpendCeiling({ capUsd: cap, now: () => AT }).check(poolOf('0'), PROJECT);
224
+ assert.equal(r.ok, false, String(cap));
225
+ assert.equal(r.reason, 'no_spend_cap_configured', String(cap));
226
+ }
227
+ });
228
+
229
+ test('no payer to bill: refused, matching fire-budget\'s reading of an unidentified caller', async () => {
230
+ const c = createSpendCeiling({ capUsd: 25, now: () => AT });
231
+ for (const p of [null, undefined, {}, { payer_kind: 'nobody' }, { payer_kind: 'builder', payer_builder_id: null }]) {
232
+ const r = await c.check(poolOf('0'), p);
233
+ assert.equal(r.ok, false, JSON.stringify(p));
234
+ assert.equal(r.reason, 'no_payer', JSON.stringify(p));
235
+ }
236
+ });
237
+
238
+ test('the window is the calendar month, and the sum is scoped to THIS payer', async () => {
239
+ let captured = null;
240
+ const spy = { query: async (_sql, params) => { captured = params; return { rows: [{ spent: '1' }] }; } };
241
+ await createSpendCeiling({ capUsd: 25, now: () => AT })
242
+ .check(spy, { payer_kind: 'builder', payer_builder_id: 12 });
243
+ assert.deepEqual(captured[0], new Date(Date.UTC(2026, 8, 1)), 'since the 1st, UTC');
244
+ assert.equal(captured[1], 'builder');
245
+ assert.equal(captured[2], 12, 'one builder\'s month, not every builder\'s');
246
+ });
247
+
248
+ test('monthStartUtc is the 1st at midnight UTC, including on the 1st itself', () => {
249
+ assert.deepEqual(monthStartUtc(Date.UTC(2026, 0, 31, 23, 59)), new Date(Date.UTC(2026, 0, 1)));
250
+ assert.deepEqual(monthStartUtc(Date.UTC(2026, 0, 1, 0, 0)), new Date(Date.UTC(2026, 0, 1)));
251
+ });
252
+
253
+ test('the default cap is a real, small, finite number — a default is what an unthinking instance gets', () => {
254
+ assert.ok(Number.isFinite(DEFAULT_CAP_USD) && DEFAULT_CAP_USD > 0);
255
+ assert.ok(DEFAULT_CAP_USD <= 100, 'generous defaults are how an unbounded bill arrives');
256
+ });
257
+
258
+ test('the ceiling RECORDS NOTHING — the ledger is the only tally', async () => {
259
+ // fire-budget.check deliberately records the fire it admits. This one must not:
260
+ // a second tally is the "two disagreeing answers to what did agents cost" that
261
+ // fire-budget's own header refuses to become.
262
+ const c = createSpendCeiling({ capUsd: 25, now: () => AT });
263
+ const writes = [];
264
+ const pool = { query: async (sql) => { writes.push(sql); return { rows: [{ spent: '1' }] }; } };
265
+ await c.check(pool, PROJECT);
266
+ await c.check(pool, PROJECT);
267
+ assert.equal(writes.length, 2, 'two reads');
268
+ for (const sql of writes) {
269
+ assert.match(sql, /^\s*SELECT/i, 'reads only');
270
+ assert.doesNotMatch(sql, /INSERT|UPDATE|DELETE/i);
271
+ }
272
+ });
@@ -42,7 +42,7 @@ const ARMED = Object.freeze({
42
42
  created_at: 'T0', updated_at: 'T0',
43
43
  });
44
44
 
45
- function fakePool({ definition = ARMED, runId = 42 } = {}) {
45
+ function fakePool({ definition = ARMED, runId = 42, spentUsd = '0' } = {}) {
46
46
  const calls = [];
47
47
  return {
48
48
  calls,
@@ -60,6 +60,12 @@ function fakePool({ definition = ARMED, runId = 42 } = {}) {
60
60
  if (/^\s*INSERT INTO agents_definitions/.test(sql)) return { rows: [{ ...ARMED, name: params[0], enabled: false }] };
61
61
  if (/^\s*UPDATE agents_definitions/.test(sql)) return { rows: [{ ...definition, enabled: /enabled = true/.test(sql) }], rowCount: 1 };
62
62
  if (/^\s*DELETE FROM agents_definitions/.test(sql)) return { rows: [], rowCount: 1 };
63
+ // THE SPEND CEILING'S READ (task 1003884), and it must be tested BEFORE the
64
+ // generic agents_runs branch below — the same anchoring lesson that branch's
65
+ // own comment records about SELECT vs DELETE. Matched last, the ceiling's
66
+ // SUM would be answered with a ledger ROW, which has no `spent` column, and
67
+ // the ceiling would correctly refuse every fire in this file as unreadable.
68
+ if (/SUM\(cost_usd\)/.test(sql)) return { rows: [{ spent: spentUsd }] };
63
69
  if (/FROM agents_runs/.test(sql)) {
64
70
  // The ownership filter is params[1]; the fake honours it so the IDOR test
65
71
  // exercises the real WHERE clause rather than a stub that ignores it.
@@ -184,6 +190,50 @@ await t('the persona and the caller\'s input BOTH reach the model', async () =>
184
190
  assert.match(lastPrompt, /why is it so\?/);
185
191
  });
186
192
 
193
+ await t('the fire records WHO PAYS beside who asked (task 1003884)', async () => {
194
+ const pool = fakePool();
195
+ api.pool = pool;
196
+ const res = fakeRes();
197
+ await handler(routes(), 'post', '/agents/:name/invoke')(req({ params: { name: 'historian' } }), res);
198
+ const row = pool.insertedRun();
199
+ assert.equal(row.requested_by_builder_id, 5, 'who asked');
200
+ // The neutral pack ships spend_payer 'none', which attributes to the PROJECT —
201
+ // that is who the money actually comes from, and it is what keeps a closed-door
202
+ // instance working exactly as it did (ADR 0282).
203
+ assert.equal(row.payer_kind, 'project', 'who is on the hook');
204
+ assert.equal(row.payer_builder_id, null);
205
+ });
206
+
207
+ await t('A PAYER AT THE CAP IS REFUSED 402, and nothing is spawned or ledgered', async () => {
208
+ // The ceiling is wired ahead of the definition read, so an exhausted payer costs
209
+ // not even a SELECT. 402 rather than fire-budget's 429 on purpose: "the money is
210
+ // gone" and "too fast, try later" are different operator problems.
211
+ const pool = fakePool({ spentUsd: '9999' });
212
+ api.pool = pool;
213
+ lastPrompt = null;
214
+ const res = fakeRes();
215
+ await handler(routes(), 'post', '/agents/:name/invoke')(req({ params: { name: 'historian' } }), res);
216
+
217
+ assert.equal(res.out.status, 402);
218
+ assert.equal(res.out.failed.code, 'spend_cap_reached');
219
+ assert.equal(res.out.failed.details.reason, 'spend_cap_reached');
220
+ assert.equal(res.out.failed.details.payer_kind, 'project');
221
+ assert.equal(pool.insertedRun(), null, 'a refused fire writes no run');
222
+ assert.equal(lastPrompt, null, 'and calls no model');
223
+ });
224
+
225
+ await t('AN UNREADABLE LEDGER REFUSES THE FIRE — the ceiling fails closed at the route too', async () => {
226
+ // The lib-level case is in tests/agents_spend_guard.mjs; this one proves the
227
+ // route honours it rather than falling through, which is the half that task
228
+ // 1001393's failed-open brake actually got wrong.
229
+ api.pool = { async query() { throw new Error('ECONNREFUSED'); } };
230
+ const res = fakeRes();
231
+ await handler(routes(), 'post', '/agents/:name/invoke')(req({ params: { name: 'historian' } }), res);
232
+ assert.equal(res.out.status, 402);
233
+ assert.equal(res.out.failed.details.reason, 'ledger_unreadable');
234
+ assert.equal(res.out.failed.details.spent_usd, null, 'it claims no sum it does not have');
235
+ });
236
+
187
237
  await t('a body key the schema does not name is refused rather than ignored', async () => {
188
238
  api.pool = fakePool();
189
239
  const res = fakeRes();
@@ -171,7 +171,84 @@ test('publish.yml wires the terminator: guard step present, every armed step gat
171
171
  assert.ok(PUBLISH_YML.includes('id: relhead'), 'the Already released? guard step exists');
172
172
  assert.match(PUBLISH_YML, /isReleaseCommit/, 'the guard delegates to bump-version.js — single source');
173
173
  const gates = (PUBLISH_YML.match(/steps\.relhead\.outputs\.release_head == 'false'/g) || []).length;
174
- assert.ok(gates >= 7, `every post-guard armed step must carry the gate (setup-node, install, resolve, bump, pack, publish, tag) — found ${gates}`);
174
+ assert.ok(gates >= 7, `every post-guard armed step must carry the gate (setup-node, install, resolve, bump, pack, publish, record, tag) — found ${gates}`);
175
+ });
176
+
177
+ // ---- the release ORDER: publish before push (task 1003914) ----
178
+ //
179
+ // The bug this pins: publish.yml used to push the bump commit to main BEFORE
180
+ // packaging and publishing. A failure in either left main permanently declaring
181
+ // a version that never reached npm, and the cascade terminator above then made
182
+ // it unretryable — HEAD was this lane's own release commit, so every following
183
+ // run no-opped and only a fresh work merge moved past the hole. 1.19.716-723
184
+ // were minted and lost that way on 2026-09-13.
185
+ //
186
+ // These are source-order assertions because a workflow's step order IS its
187
+ // source; there is nothing to execute here. They key on the actual COMMANDS,
188
+ // never on prose, so a comment rewrite can't make them pass falsely.
189
+
190
+ const BRANCH_PUSH = ['git', 'push', 'origin', 'HEAD:main'].join(' ');
191
+
192
+ test('the bump commit is pushed only AFTER npm publish — the irreversible step goes last', () => {
193
+ const publishAt = PUBLISH_YML.indexOf('npm publish "$TGZ"');
194
+ const pushAt = PUBLISH_YML.indexOf(BRANCH_PUSH);
195
+ assert.ok(publishAt !== -1, 'publish.yml must publish the packaged tarball');
196
+ assert.ok(pushAt !== -1, 'publish.yml must push the bump commit to main somewhere');
197
+ assert.ok(
198
+ publishAt < pushAt,
199
+ 'the push to main must come AFTER npm publish. Pushing first makes a failed publish burn the version number permanently and unretryably (task 1003914).',
200
+ );
201
+ });
202
+
203
+ test('the push to main happens exactly once, and not inside the bump step', () => {
204
+ const pushes = PUBLISH_YML.split(BRANCH_PUSH).length - 1;
205
+ assert.equal(pushes, 1, `exactly one push of the bump to main — found ${pushes}`);
206
+ // The bump step runs from `id: bump` up to the next step's `- name:`.
207
+ const bumpAt = PUBLISH_YML.indexOf('id: bump');
208
+ assert.ok(bumpAt !== -1, 'the bump step must carry id: bump');
209
+ const nextStepAt = PUBLISH_YML.indexOf('\n - name:', bumpAt);
210
+ const bumpStep = PUBLISH_YML.slice(bumpAt, nextStepAt === -1 ? undefined : nextStepAt);
211
+ assert.ok(
212
+ !bumpStep.includes(BRANCH_PUSH),
213
+ 'the bump step must COMMIT only — its push moved to the record step so a pack/publish failure discards a purely local commit',
214
+ );
215
+ assert.ok(bumpStep.includes('git commit -m'), 'the bump step still commits locally — package-core stamps the version found AT the ref');
216
+ });
217
+
218
+ test('an already-published version is success, not a wedge', () => {
219
+ // Publish-then-failed-push leaves a version on npm with no release commit.
220
+ // The next run reads it as free (laneReleasedVersions is git-based), bumps to
221
+ // it, and npm refuses the duplicate. Without this branch that run fails, and
222
+ // so does every run after it — the lane wedges with no way out but a human.
223
+ const pubAt = PUBLISH_YML.indexOf('id: pub');
224
+ assert.ok(pubAt !== -1, 'the publish step must carry id: pub');
225
+ const pubStep = PUBLISH_YML.slice(pubAt, PUBLISH_YML.indexOf('\n # PUBLISH VERDICT'));
226
+ assert.match(
227
+ pubStep,
228
+ /cannot publish over\|previously published\|EPUBLISHCONFLICT/,
229
+ 'the publish step must recognise npm refusing a duplicate version',
230
+ );
231
+ assert.match(pubStep, /already=true/, 'and must record that it took the already-present branch');
232
+ assert.match(
233
+ pubStep,
234
+ /exit "\$RC"/,
235
+ 'every OTHER publish failure must still fail the step — the tolerance is narrow on purpose',
236
+ );
237
+ });
238
+
239
+ test('the record step gates on a bump actually having been made', () => {
240
+ const recAt = PUBLISH_YML.indexOf('- name: Record the release on main');
241
+ assert.ok(recAt !== -1, 'the record step exists');
242
+ const recStep = PUBLISH_YML.slice(recAt, PUBLISH_YML.indexOf('\n # PUBLISH VERDICT'));
243
+ assert.match(
244
+ recStep,
245
+ /steps\.bump\.outputs\.to != ''/,
246
+ 'nothing to push when no bump was made (taken == false) — the step must skip rather than push an empty HEAD',
247
+ );
248
+ assert.ok(
249
+ !recStep.includes('git rebase'),
250
+ 'no rebase-retry: the tarball was built from the pre-rebase tree, so rebasing would leave source_commit naming a SHA that is not on main',
251
+ );
175
252
  });
176
253
 
177
254
  // ---------------------------------------------------------------------------