mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
fix(packing,journey,server): address review findings on the three fixes
An adversarial review of the diff found sixteen defects that survived verification. The substantive ones: - The new `packing:bag-totals` event was never classified in wsEventPolicy, so the registry-parity ratchet was red. It belongs in HANDLED_OUTSIDE_TRIP_STORE: it carries no state to apply, which is the point of it. - The Journey Settings tracks switch called onSaved(), which closes the dialog. That destroyed the switch the moment it was flipped AND skipped the unsaved-changes guard, so a title typed but not saved was silently lost. It refreshes instead. - Deleting a bag sends no totals ping, yet packing_items.bag_id is ON DELETE SET NULL: everything in the bag lands in the unassigned pile and both figures move. The comment claiming otherwise was wrong. - Only the REST handlers pinged. The MCP tools and the plugin RPC are two more live write paths, and since the displayed number now comes from the server rather than from the item events, weights simply stopped updating for anything written through them. - Every item write pinged, including checking an item off — the most frequent packing write there is — and clearing twelve checked items sent twelve. The ping now fires only when a weight-relevant field changed, and the clients coalesce bursts and never overlap two refetches. - Bags have no repo and no Dexie table, so offline the server totals froze at the last online read and went blind to the mutation queue. A stale absolute number measured against an airline limit is worse than an honest partial one, so offline the surfaces sum what they can see. - SIGTERM arriving mid-bootstrap called server.close() on undefined, and the fire-and-forget promise turned that into an unhandled rejection: on Node 22 a crash, which is the opposite of the fix. - Promise.allSettled swallowed a rejected nestApp.close(), reporting a clean exit 0 while the crons and the plugin children were never torn down. This bug class is diagnosed from container logs. Also: the track fetch gate read a stale `current` during journey-to-journey navigation and fetched the previous journey's tracks; listBags ran the weight aggregate twice per request, on a route the ping now fires constantly; the grand total counted unassigned weight whose row was not rendered; the client declared show_trip_tracks boolean while the wire carries SQLite 0|1; and the six broadcast calls had no test, so removing all of them left the suite green. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S5VwmiBQ9sPCJwfmpv64r3
This commit is contained in:
@@ -151,7 +151,12 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Bag not found' }, 400);
|
||||
}
|
||||
this.packing.broadcastUpdate(tripId, id, updated as PackingItemRow, !!before?.is_private, socketId);
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
// Only when the write could actually move a weight. Checking an item off is
|
||||
// the most frequent packing write there is, and every ping costs every
|
||||
// connected client a listBags round trip.
|
||||
if (['weight_grams', 'quantity', 'bag_id'].some(k => Object.keys(body).includes(k))) {
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
}
|
||||
return { item: updated };
|
||||
}
|
||||
|
||||
@@ -256,10 +261,7 @@ export class PackingController {
|
||||
// unassigned_weight_grams rides along so the "no bag" pile and the grand
|
||||
// total follow the same rule as the bags themselves (#2191) — a screen
|
||||
// mixing true totals with per-viewer ones would be worse than either.
|
||||
return {
|
||||
bags: this.packing.listBags(tripId),
|
||||
unassigned_weight_grams: this.packing.unassignedWeightGrams(tripId),
|
||||
};
|
||||
return this.packing.listBagsWithWeights(tripId);
|
||||
}
|
||||
|
||||
@RequirePermission('packing_edit')
|
||||
@@ -311,6 +313,9 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Bag not found' }, 404);
|
||||
}
|
||||
this.packing.broadcast(tripId, 'packing:bag-deleted', { bagId: Number(bagId) }, socketId);
|
||||
// bag_id is ON DELETE SET NULL, so everything that was in it just landed in
|
||||
// the unassigned pile — both figures moved.
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
|
||||
@@ -99,6 +99,7 @@ export class PackingMcp {
|
||||
// A restricted item (#858) reaches its owner and recipients only; a Common
|
||||
// one answers null here and goes to the whole room.
|
||||
this.guards.safeBroadcast(tripId, 'packing:created', { item }, this.packing.viewersOf(item));
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return ok({ item });
|
||||
}
|
||||
|
||||
@@ -146,6 +147,7 @@ export class PackingMcp {
|
||||
// deleteItem hands back the row it removed, so the delete can be scoped to
|
||||
// the same people the item was ever visible to (#1976).
|
||||
this.guards.safeBroadcast(tripId, 'packing:deleted', { itemId }, this.packing.viewersOf(deleted));
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return ok({ success: true });
|
||||
}
|
||||
|
||||
@@ -207,6 +209,7 @@ export class PackingMcp {
|
||||
}
|
||||
// Newly common: the members who never had the row need it created, not updated.
|
||||
if (wasPrivate) this.guards.safeBroadcast(tripId, 'packing:created', { item });
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
this.guards.safeBroadcast(tripId, 'packing:updated', { item });
|
||||
}
|
||||
|
||||
@@ -237,6 +240,7 @@ export class PackingMcp {
|
||||
// it back to whoever may see it now, as the REST route does.
|
||||
this.guards.safeBroadcast(tripId, 'packing:deleted', { itemId });
|
||||
this.guards.safeBroadcast(tripId, 'packing:created', { item }, this.packing.viewersOf(item));
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return ok({ item });
|
||||
}
|
||||
|
||||
@@ -439,6 +443,7 @@ export class PackingMcp {
|
||||
const items = this.packing.applyTemplate(tripId, templateId);
|
||||
if (items === null) return errorResult('Template not found.');
|
||||
this.guards.safeBroadcast(tripId, 'packing:template-applied', { items });
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return ok({ items, count: items.length });
|
||||
}
|
||||
|
||||
@@ -527,6 +532,9 @@ export class PackingMcp {
|
||||
for (const item of created) {
|
||||
this.guards.safeBroadcast(tripId, 'packing:created', { item }, this.packing.viewersOf(item));
|
||||
}
|
||||
// Once for the whole import, not once per item: the ping is content-free and
|
||||
// every one of them costs each connected client a listBags round trip.
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return ok({ items: created, count: created.length });
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ export class PackingRpc {
|
||||
// A referenced bag must exist on this trip (#2154), as on the REST route.
|
||||
if (isInvalidBagRef(item)) throw new BadParams(`no packing bag ${parsed.data.bag_id} on trip ${tripId}`);
|
||||
this.packing.emitToViewers(String(tripId), 'packing:created', { item }, item, undefined);
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return item;
|
||||
}
|
||||
|
||||
@@ -71,6 +72,9 @@ export class PackingRpc {
|
||||
// A referenced bag must exist on this trip (#2154), as on the REST route.
|
||||
if (isInvalidBagRef(updated)) throw new BadParams(`no packing bag ${parsed.data.bag_id} on trip ${tripId}`);
|
||||
this.packing.broadcastUpdate(String(tripId), itemId, updated as PrivacyItem, !!before?.is_private, undefined);
|
||||
if (['weight_grams', 'quantity', 'bag_id'].some(k => Object.keys(input).includes(k))) {
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
@@ -83,6 +87,7 @@ export class PackingRpc {
|
||||
const deleted = this.packing.deleteItem(String(tripId), String(itemId), actor) as PrivacyItem | null;
|
||||
if (!deleted) throw new ForbiddenResource(`no packing item ${itemId} on trip ${tripId}`);
|
||||
this.packing.emitToViewers(String(tripId), 'packing:deleted', { itemId }, deleted, undefined);
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return { deleted: true };
|
||||
}
|
||||
|
||||
@@ -132,6 +137,8 @@ export class PackingRpc {
|
||||
throw new ForbiddenResource(`no packing bag ${bagId} on trip ${tripId}`);
|
||||
}
|
||||
this.realtime.broadcast(tripId, 'packing:bag-deleted', { bagId }, undefined);
|
||||
// bag_id is ON DELETE SET NULL — its items just moved to the unassigned pile.
|
||||
this.packing.broadcastBagTotals(String(tripId));
|
||||
return { deleted: true };
|
||||
}
|
||||
|
||||
|
||||
@@ -120,8 +120,10 @@ export class PackingService {
|
||||
* own client cannot recompute a server-side total from the item it just
|
||||
* wrote either. Everyone refetches, everyone gets numbers.
|
||||
*
|
||||
* Called for item writes, not bag writes — a bag's own create/update already
|
||||
* broadcasts the row, and neither changes what is inside it.
|
||||
* Called for item writes, and for a bag DELETE: packing_items.bag_id is
|
||||
* ON DELETE SET NULL, so deleting a bag moves everything in it to the
|
||||
* unassigned pile and moves both figures. A bag create or rename does not,
|
||||
* and already broadcasts its own row.
|
||||
*/
|
||||
broadcastBagTotals(tripId: string): void {
|
||||
this.broadcast(tripId, 'packing:bag-totals', {}, undefined);
|
||||
@@ -537,9 +539,25 @@ export class PackingService {
|
||||
return this.bagWeightTotals(tripId).get(null) ?? 0;
|
||||
}
|
||||
|
||||
listBags(tripId: string | number) {
|
||||
const bags = this.db.all<any>('SELECT * FROM packing_bags WHERE trip_id = ? ORDER BY sort_order, id', tripId);
|
||||
/**
|
||||
* The bags plus the unassigned pile, from ONE pass over the aggregate.
|
||||
*
|
||||
* The REST list route wants both, and the WS ping (#2191) makes that route
|
||||
* fire on every item write for every connected client — running the same
|
||||
* SUM…GROUP BY twice per request is not a cost worth paying for a nicer
|
||||
* method list.
|
||||
*/
|
||||
listBagsWithWeights(tripId: string | number): { bags: unknown[]; unassigned_weight_grams: number } {
|
||||
const totals = this.bagWeightTotals(tripId);
|
||||
return { bags: this.decorateBags(tripId, totals), unassigned_weight_grams: totals.get(null) ?? 0 };
|
||||
}
|
||||
|
||||
listBags(tripId: string | number) {
|
||||
return this.decorateBags(tripId, this.bagWeightTotals(tripId));
|
||||
}
|
||||
|
||||
private decorateBags(tripId: string | number, totals: Map<number | null, number>) {
|
||||
const bags = this.db.all<any>('SELECT * FROM packing_bags WHERE trip_id = ? ORDER BY sort_order, id', tripId);
|
||||
const members = this.db.all<{ bag_id: number; user_id: number; username: string; avatar: string | null }>(`
|
||||
SELECT bm.bag_id, bm.user_id, COALESCE(u.display_name, u.username) AS username, u.avatar
|
||||
FROM packing_bag_members bm
|
||||
|
||||
@@ -158,7 +158,15 @@ export async function runShutdown(signal: string, deps: ShutdownDeps): Promise<v
|
||||
}, drainMs);
|
||||
drain.unref?.();
|
||||
|
||||
await Promise.allSettled([httpClosed, closeNestApp()]);
|
||||
const [, nestResult] = await Promise.allSettled([httpClosed, closeNestApp()]);
|
||||
// allSettled never rejects, so a throwing onModuleDestroy would otherwise
|
||||
// vanish and the process would report a clean exit while the crons and the
|
||||
// plugin children were never torn down. This bug class is diagnosed from
|
||||
// container logs; that is the one line worth having.
|
||||
if (nestResult.status === 'rejected') {
|
||||
const err: unknown = nestResult.reason;
|
||||
logError(`nest shutdown failed during shutdown: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
clearTimeout(drain);
|
||||
clearTimeout(forced);
|
||||
finish(0, 'Shutdown complete');
|
||||
|
||||
Reference in New Issue
Block a user