fix(fairshare): guard power/cooling cycle settlement against remount duplication

Manual browser QA of the previous fix (eb72625) surfaced a real bug: the
"genuinely new value" guard lived only in a useRef, which resets on every
component remount (a page reload, or React remounting ResourceTiles).
Reloading the Fair Share screen re-announced the SAME unchanged power/
cooling total as a brand-new fairshare.allocation.cycle event every time,
corrupting the trend with duplicate flat points (observed first-hand: a
browser QA session produced 4 power cycle points - [100,250,250,250] -
from only 2 real supply changes).

Fix: check the real, already-signed Event Log history
(getAllocationHistory's last point) before settling, not only the
in-memory ref - a ref reset by remount no longer matters once the guard's
source of truth is the persisted log itself. The ref stays as a same-
session fast path to skip a redundant recheck across rapid re-renders.

Added a regression test exercising the exact unmount/remount sequence
that exposed the bug, verifying the event count stays at 1 after a
remount with no real change, and still settles correctly on a genuine
follow-up change.

Re-verified via real-browser QA (chromium, owned/pipe engine): seeded
power twice (100 -> 250) and cooling twice (40 -> 70) through the live
admin panel and Fair Share UI; both TrendCharts now render a real
multi-point SVG trend (screenshots in .omo/evidence/coherence-audit/).
This commit is contained in:
OmO Orchestrator committed 2026-11-20 17:56:17 -05:00
1 parent 70a684c238
commit 21bae7495c
2 files changed
+66 -4

No files matched your search

@@ -248,6 +248,53 @@ describe("ResourceTiles (power/cooling allocation-cycle coherence fix)", () => {
expect(waterCycles).toHaveLength(0);
expect(foodCycles).toHaveLength(0);
});
it("does not re-announce an unchanged power total as a duplicate cycle on remount (e.g. a page reload)", async () => {
await init();
const accountId = toAccountId(await getPublicKey());
await append({ type: "seed.resource.power", resource: "power", value: 100 });
await append({ type: "fairshare.need", agent: accountId, resource: "power", value: 1 });
const firstMount = render(<ResourceTiles />);
await waitFor(() => {
expect(screen.getByTestId("tile-power-allocation")).toHaveTextContent("100");
});
await waitForAllocationCycleCount("power", 1);
// A real app reload (or React remounting this screen) tears down and
// recreates the component instance - any in-memory ref the settlement
// effect used is gone, but the supply/need state in the Event Log is
// completely unchanged. Simulate that exact remount here.
firstMount.unmount();
render(<ResourceTiles />);
await waitFor(() => {
expect(screen.getByTestId("tile-power-allocation")).toHaveTextContent("100");
});
// Give the remounted settlement effect every chance to (wrongly) fire
// again before asserting it didn't: there is no second real state
// change to detect, so the real Event Log must still show exactly the
// one cycle point from before the remount - never a duplicate flat
// point that would corrupt the trend's slope.
await new Promise((resolve) => setTimeout(resolve, 300));
const entries = await readAll();
const history = getAllocationHistory(entries, "power");
expect(history).toHaveLength(1);
expect(history[0].totalAllocated).toBe(100);
// A genuine change after the remount still settles correctly - the fix
// for the remount-duplicate bug didn't also break the real-change path.
await append({ type: "seed.resource.power", resource: "power", value: 300 });
await waitFor(() => {
expect(screen.getByTestId("tile-power-allocation")).toHaveTextContent("300");
});
await waitForAllocationCycleCount("power", 2);
const finalEntries = await readAll();
const finalHistory = getAllocationHistory(finalEntries, "power");
expect(finalHistory.map((p) => p.totalAllocated)).toEqual([100, 300]);
});
});
describe("ResourceTiles (failure path - no ledger balance yet)", () => {
+19 -4
View File
@@ -3,6 +3,7 @@ import { balanceOf, computeBalances } from "../../lib/ledger";
import {
computeFairshareAllocation,
FAIRSHARE_RESOURCES,
getAllocationHistory,
postFairshareNeed,
settleAllocationCycle,
type FairshareResource,
@@ -72,11 +73,19 @@ export function ResourceTiles() {
// Power and cooling trend charts (see `TrendChart.tsx`) are structurally
// stuck at "no trend yet" without this: no code path writes their
// `fairshare.allocation.cycle` event otherwise. Fires once per genuinely
// new re-solved total for an actually-seeded resource - never on a
// re-render echo of the same value (`lastSettledCycleRef`, mirroring
// `FarmsScreen.tsx`'s `lastSettledForecastRef`) and never before the
// new re-solved total for an actually-seeded resource - never before the
// resource has a real seeded/contributed pool and at least one real agent
// (`allocation.supply[resource] > 0 && allocation.agents.length > 0`).
//
// The "genuinely new" guard is checked against the real, already-signed
// Event Log history (`getAllocationHistory`'s last point), never only an
// in-memory ref: a ref alone resets on every remount (a page reload, or
// React re-mounting this screen), which would otherwise re-announce the
// SAME unchanged total as a brand-new cycle on every reload and corrupt
// the trend with duplicate flat points. `lastSettledCycleRef` is kept as
// a same-session fast-path only, to skip a redundant re-check across
// rapid re-renders within one mount - the Event Log check is what makes
// the guard correct across reloads.
const lastSettledCycleRef = useRef<Partial<Record<FairshareResource, number>>>({});
useEffect(() => {
for (const resource of RESOURCES_NEEDING_UI_SETTLEMENT) {
@@ -90,6 +99,12 @@ export function ResourceTiles() {
if (lastSettledCycleRef.current[resource] === totalAllocated) {
continue;
}
const history = getAllocationHistory(entries, resource);
const lastRecorded = history.length > 0 ? history[history.length - 1].totalAllocated : undefined;
if (lastRecorded === totalAllocated) {
lastSettledCycleRef.current[resource] = totalAllocated;
continue;
}
lastSettledCycleRef.current[resource] = totalAllocated;
void settleAllocationCycle(resource)
.then(() => {
@@ -100,7 +115,7 @@ export function ResourceTiles() {
// a transient Event Log failure must never break tile rendering.
});
}
}, [allocation]);
}, [allocation, entries]);
const [needResource, setNeedResource] = useState<FairshareResource>("power");
const [needAmount, setNeedAmount] = useState("");