Phase 4: close loot/refund leaks, landing-site garrisons, speed-scaled shipping, update/offsite tooling, broad e2e

Built:
- Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds).
- End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x).
- Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in.
- e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English.

Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented).
NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
jvanvik 2026-10-12 00:58:12 +02:00
parent 9104480463
commit 3c23c51902
92 changed files with 4887 additions and 300 deletions

View file

@ -13,7 +13,8 @@
* bookings (db.tx is tapped too), so only committed settles count. Caps are already inside the stored numbers.
* sinks build/train/research costs (from the command results), Backbone node levels (from landing_sites), cargo that
* did not fit the Rack hall (`cargoLost` in notifications), trade escrow refunds clipped by the Rack hall cap
* (tap on the refund statement)
* (booked from `trade_refund_clipped` notifications: with escrow reserving its room in the Rack hall this is only a hall that was
* shot down below stock + escrow in the meantime, so it is 0 in normal runs)
*
* Anything else is DRIFT: a leak or a duplication in the server (or a missing sink here). Drift is printed and fails the run.
*
@ -28,8 +29,6 @@ const zero = () => ({ fiber: 0, hardware: 0, capital: 0 });
const addRes = (a, b) => { for (const r of RES) a[r] += (b && b[r]) || 0; return a; };
const SETTLE_SQL = 'UPDATE towns SET fiber = ?, hardware = ?, capital = ?, battery = ?, footprint = ?, res_at = ?, loyalty = ?, loyalty_at = ? WHERE id = ?';
const REFUND_RE = /^UPDATE towns SET (fiber|hardware|capital) = \? WHERE id = \?$/;
const CLOSE_PREFIX = 'UPDATE trade_offers SET status = ?, closed_at = ? WHERE id = ?';
class Ledger {
constructor(db) {
@ -38,11 +37,10 @@ class Ledger {
this.sinkCosts = zero(); // build / train / research (command results)
this.sinkLost = zero(); // cargoLost notifications
this.lostEvents = 0;
this.sinkClip = zero(); // trade refund clipped by the Rack hall cap
this.sinkClip = zero(); // trade refund that did not fit (trade_refund_clipped notifications)
this.clipEvents = 0;
this.sourceStart = zero(); // operators' start stock
this.stack = [];
this.closing = null;
this.t = { settles: 0 };
this.installed = false;
}
@ -63,20 +61,6 @@ class Ledger {
if (sql === SETTLE_SQL) {
const prev = db.get('SELECT fiber, hardware, capital FROM towns WHERE id = ?', p[8]);
if (prev) self.book({ fiber: p[0] - prev.fiber, hardware: p[1] - prev.hardware, capital: p[2] - prev.capital }, zero(), 0);
} else if (sql === CLOSE_PREFIX) {
self.closing = p[2];
} else {
const m = REFUND_RE.exec(sql);
if (m && self.closing !== null) {
const prev = db.get('SELECT ' + m[1] + ' AS v FROM towns WHERE id = ?', p[1]);
const off = db.get('SELECT give_amount, give_res FROM trade_offers WHERE id = ?', self.closing);
if (prev && off && off.give_res === m[1]) {
const credit = p[0] - prev.v;
const clip = Math.max(0, off.give_amount - credit);
if (clip > 1e-9) { const c = zero(); c[m[1]] = clip; self.book(zero(), c, 1); }
}
self.closing = null;
}
}
return run(sql, ...p);
};
@ -157,6 +141,7 @@ function aliveUnits(db) {
const add = (k, n) => { out[k] = (out[k] || 0) + n; };
for (const r of db.all('SELECT unit, SUM(count) AS n FROM town_units GROUP BY unit')) add(r.unit, r.n);
for (const r of db.all('SELECT unit, SUM(count) AS n FROM stationed GROUP BY unit')) add(r.unit, r.n);
for (const r of db.all('SELECT unit, SUM(count) AS n FROM site_stationed GROUP BY unit')) add(r.unit, r.n); // garrisons at landing sites (E6)
for (const m of db.all("SELECT units FROM movements WHERE status = 'out' AND units != '{}'")) { const u = JSON.parse(m.units); for (const k of Object.keys(u)) add(k, u[k]); }
return out;
}