diff --git a/src/miniapps/fairshare/ResourceTiles.test.tsx b/src/miniapps/fairshare/ResourceTiles.test.tsx
index 286956f..25bbbf1 100644
--- a/src/miniapps/fairshare/ResourceTiles.test.tsx
+++ b/src/miniapps/fairshare/ResourceTiles.test.tsx
@@ -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();
+ 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();
+ 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)", () => {
diff --git a/src/miniapps/fairshare/ResourceTiles.tsx b/src/miniapps/fairshare/ResourceTiles.tsx
index edac40b..a1cbba5 100644
--- a/src/miniapps/fairshare/ResourceTiles.tsx
+++ b/src/miniapps/fairshare/ResourceTiles.tsx
@@ -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>>({});
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("power");
const [needAmount, setNeedAmount] = useState("");