fix(tunnelsafety): resolve hazards by a stable cross-device id, not the local Dexie id

Identical bug to the healthpost referral one: tunnelsafety.hazard.resolve joined on StoredEventLogEntry.id (a per-device Dexie autoincrement), and hazards sync cross-device - so a hazard resolved on one device showed 'active' forever on every other device and stayed falsely corroborated, and routing kept avoiding a cleared segment. Each hazard report now carries an intrinsic reportId (UUID) in its signed payload; resolveHazard + computeHazardReports join on it. Verified by a cross-device test (resolve matches despite differing entry ids). The convergence review confirmed this was the last such join bug - theclimb/fairdraw/governance/fairshare/assembly all already use intrinsic ids.
This commit is contained in:
OmO Orchestrator committed 2026-11-20 05:52:51 -05:00
1 parent d630a0c9a7
commit 1045b2b3f3
6 files changed
+65 -28

No files matched your search

+1
View File
@@ -407,6 +407,7 @@ export async function buildDemoCorroboratingHazardEntry(
): Promise<UnsignedEventLogEntry> {
const payload: HazardReportPayload = {
type: "tunnelsafety.hazard.report",
reportId: crypto.randomUUID(),
segmentId: HOLLOW_DEMO_CORROBORATING_HAZARD.segmentId,
status: HOLLOW_DEMO_CORROBORATING_HAZARD.status,
reportedAt: Date.now(),
+1 -1
View File
@@ -124,7 +124,7 @@ describe("findRoute (accessibleOnly excludes stair segments)", () => {
describe("closedSegmentIdsFromReports", () => {
function report(overrides: Partial<HazardReport>): HazardReport {
return {
id: 1,
id: "r1",
lamportTs: 1,
segmentId: "seg-bc",
status: "flooded",
+33 -6
View File
@@ -2,7 +2,7 @@ import * as ed from "@noble/ed25519";
import { beforeEach, describe, expect, it } from "vitest";
import { db } from "./db";
import { getPublicKey, init } from "./identity";
import { append, appendEntry, readAll } from "./eventlog";
import { type StoredEventLogEntry, append, appendEntry, readAll } from "./eventlog";
import {
activeHazardsForSegment,
computeHazardReports,
@@ -59,7 +59,7 @@ describe("reportHazard (happy path)", () => {
status: "blocked",
});
await resolveHazard({ id: reportEntry.id, segmentId: "segment-3" });
await resolveHazard({ id: (reportEntry.payload as HazardReportPayload).reportId, segmentId: "segment-3" });
const entries = await readAll();
expect(entries).toHaveLength(2);
@@ -75,7 +75,7 @@ describe("reportHazard (happy path)", () => {
expect(resolveEntry?.payload).toMatchObject({
type: "tunnelsafety.hazard.resolve",
segmentId: "segment-3",
reportId: reportEntry.id,
reportId: (reportEntry.payload as HazardReportPayload).reportId,
});
const reports = computeHazardReports(entries);
@@ -97,7 +97,7 @@ describe("reportHazard (happy path)", () => {
expect(activeHazardsForSegment(entries, "segment-a")).toHaveLength(1);
expect(activeHazardsForSegment(entries, "segment-b")).toHaveLength(1);
await resolveHazard({ id: toResolve.id, segmentId: "segment-b" });
await resolveHazard({ id: (toResolve.payload as HazardReportPayload).reportId, segmentId: "segment-b" });
entries = await readAll();
expect(activeHazardsForSegment(entries, "segment-a")).toHaveLength(1);
@@ -117,6 +117,7 @@ describe("computeSegmentCorroboration (real crowd corroboration by distinct sign
) {
const payload: HazardReportPayload = {
type: "tunnelsafety.hazard.report",
reportId: crypto.randomUUID(),
segmentId,
status: "flooded",
reportedAt: Date.now(),
@@ -177,7 +178,7 @@ describe("computeSegmentCorroboration (real crowd corroboration by distinct sign
).toMatchObject({ distinctReporterCount: 2, corroborated: true });
// Resolve alice's report - only bob's remains open.
await resolveHazard({ id: aliceReport.id, segmentId: "seg-flood" });
await resolveHazard({ id: (aliceReport.payload as HazardReportPayload).reportId, segmentId: "seg-flood" });
expect(
computeSegmentCorroboration(computeHazardReports(await readAll())).get("seg-flood"),
@@ -187,7 +188,7 @@ describe("computeSegmentCorroboration (real crowd corroboration by distinct sign
it("leaves a segment with no open reports entirely absent from the map", async () => {
const alice = await ed.keygenAsync();
const report = await appendEntry(await signedReport(alice, "seg-flood", 0));
await resolveHazard({ id: report.id, segmentId: "seg-flood" });
await resolveHazard({ id: (report.payload as HazardReportPayload).reportId, segmentId: "seg-flood" });
const corroboration = computeSegmentCorroboration(computeHazardReports(await readAll()));
expect(corroboration.has("seg-flood")).toBe(false);
@@ -264,3 +265,29 @@ describe("computeHazardReports", () => {
);
});
});
describe("computeHazardReports (cross-device resolve by stable id)", () => {
it("resolves a report by its stable reportId, not the device-local entry id", () => {
const entries = [
{
id: 42,
lamportTs: 1,
authorPubkey: new Uint8Array([1]),
signature: new Uint8Array([1]),
payload: { type: "tunnelsafety.hazard.report", reportId: "h-1", segmentId: "seg-x", status: "flooded", reportedAt: 0 },
},
{
id: 3,
lamportTs: 2,
authorPubkey: new Uint8Array([2]),
signature: new Uint8Array([2]),
payload: { type: "tunnelsafety.hazard.resolve", segmentId: "seg-x", reportId: "h-1", resolvedAt: 5 },
},
] as unknown as StoredEventLogEntry[];
const reports = computeHazardReports(entries);
expect(reports).toHaveLength(1);
expect(reports[0]!.id).toBe("h-1");
// Resolved despite the report entry id (42) and resolve entry id (3) differing.
expect(reports[0]!.resolved).toBe(true);
});
});
+17 -9
View File
@@ -35,6 +35,10 @@ export interface HazardReportInput {
export interface HazardReportPayload {
type: "tunnelsafety.hazard.report";
/** Stable, device-independent id minted when the report is created, so a
* resolve event targets the SAME report on every device after sync (the
* local Dexie `StoredEventLogEntry.id` differs per device). */
reportId: string;
segmentId: string;
status: HazardStatus;
reportedAt: number;
@@ -43,16 +47,18 @@ export interface HazardReportPayload {
export interface HazardResolvePayload {
type: "tunnelsafety.hazard.resolve";
segmentId: string;
/** Event Log id (`StoredEventLogEntry.id`) of the report being resolved. */
reportId: number;
/** Stable `HazardReportPayload.reportId` of the report being resolved - NOT
* the local Dexie id, so a resolution matches the same report on every
* device after sync. */
reportId: string;
resolvedAt: number;
}
/** A hazard report as read back via replay: the original report fields
* plus whether a later resolving event has since closed it out. */
export interface HazardReport {
/** Event Log id of the original `tunnelsafety.hazard.report` entry. */
id: number;
/** Stable cross-device report id (`HazardReportPayload.reportId`), the resolve target. */
id: string;
lamportTs: number;
segmentId: string;
status: HazardStatus;
@@ -106,6 +112,7 @@ function isHazardReportPayload(
const p = payload as Record<string, unknown>;
return (
p.type === "tunnelsafety.hazard.report" &&
typeof p.reportId === "string" &&
typeof p.segmentId === "string" &&
p.segmentId.trim().length > 0 &&
isHazardStatus(p.status) &&
@@ -121,7 +128,7 @@ function isHazardResolvePayload(
return (
p.type === "tunnelsafety.hazard.resolve" &&
typeof p.segmentId === "string" &&
typeof p.reportId === "number" &&
typeof p.reportId === "string" &&
typeof p.resolvedAt === "number"
);
}
@@ -143,6 +150,7 @@ export async function reportHazard(
validateHazardReportInput(input);
const payload: HazardReportPayload = {
type: "tunnelsafety.hazard.report",
reportId: crypto.randomUUID(),
segmentId: input.segmentId,
status: input.status,
reportedAt: Date.now(),
@@ -178,13 +186,13 @@ export function computeHazardReports(
entries: readonly StoredEventLogEntry[],
): HazardReport[] {
const ordered = sortForReplay(entries);
const reports = new Map<number, HazardReport>();
const resolutions = new Map<number, number>();
const reports = new Map<string, HazardReport>();
const resolutions = new Map<string, number>();
for (const entry of ordered) {
if (isHazardReportPayload(entry.payload)) {
reports.set(entry.id, {
id: entry.id,
reports.set(entry.payload.reportId, {
id: entry.payload.reportId,
lamportTs: entry.lamportTs,
segmentId: entry.payload.segmentId,
status: entry.payload.status,
+2 -2
View File
@@ -23,7 +23,7 @@ function CorroborationBadge({
reportId,
corroboration,
}: {
reportId: number;
reportId: string;
corroboration: SegmentCorroboration | undefined;
}) {
if (corroboration?.corroborated) {
@@ -47,7 +47,7 @@ function CorroborationBadge({
}
export function HazardFeed({ reports, corroborationBySegment }: HazardFeedProps) {
const [resolvingId, setResolvingId] = useState<number | undefined>(undefined);
const [resolvingId, setResolvingId] = useState<string | undefined>(undefined);
const ordered = [...reports].sort((a, b) => b.lamportTs - a.lamportTs);
async function handleResolve(report: HazardReport) {
@@ -17,6 +17,7 @@ async function reportAsDistinctResident(segmentId: string, lamportTs: number) {
const { secretKey, publicKey } = await ed.keygenAsync();
const payload: HazardReportPayload = {
type: "tunnelsafety.hazard.report",
reportId: crypto.randomUUID(),
segmentId,
status: "flooded",
reportedAt: Date.now(),
@@ -78,14 +79,14 @@ describe("TunnelSafetyScreen (QA happy path - hazard report reroutes the live ma
await waitFor(() => {
expect(
screen.getByTestId(`tunnel-safety-hazard-${stored.id}`),
screen.getByTestId(`tunnel-safety-hazard-${(stored.payload as HazardReportPayload).reportId}`),
).toBeInTheDocument();
});
await userEvent.click(screen.getByTestId(`tunnel-safety-resolve-${stored.id}`));
await userEvent.click(screen.getByTestId(`tunnel-safety-resolve-${(stored.payload as HazardReportPayload).reportId}`));
await waitFor(() => {
expect(screen.getByTestId(`tunnel-safety-hazard-${stored.id}-status`)).toHaveTextContent(
expect(screen.getByTestId(`tunnel-safety-hazard-${(stored.payload as HazardReportPayload).reportId}-status`)).toHaveTextContent(
"Resolved",
);
});
@@ -125,11 +126,11 @@ describe("TunnelSafetyScreen (crowd corroboration badge - distinct resident repo
const first = await reportHazard({ segmentId: "seg-market-residential", status: "flooded" });
await waitFor(() => {
expect(
screen.getByTestId(`tunnel-safety-hazard-${first.id}-unconfirmed`),
screen.getByTestId(`tunnel-safety-hazard-${(first.payload as HazardReportPayload).reportId}-unconfirmed`),
).toBeInTheDocument();
});
expect(
screen.queryByTestId(`tunnel-safety-hazard-${first.id}-corroborated`),
screen.queryByTestId(`tunnel-safety-hazard-${(first.payload as HazardReportPayload).reportId}-corroborated`),
).not.toBeInTheDocument();
// A second, DISTINCT resident reports the same segment -> crowd-corroborated.
@@ -137,11 +138,11 @@ describe("TunnelSafetyScreen (crowd corroboration badge - distinct resident repo
await waitFor(() => {
expect(
screen.getByTestId(`tunnel-safety-hazard-${first.id}-corroborated`),
screen.getByTestId(`tunnel-safety-hazard-${(first.payload as HazardReportPayload).reportId}-corroborated`),
).toHaveTextContent("Confirmed by 2 residents");
});
expect(
screen.queryByTestId(`tunnel-safety-hazard-${first.id}-unconfirmed`),
screen.queryByTestId(`tunnel-safety-hazard-${(first.payload as HazardReportPayload).reportId}-unconfirmed`),
).not.toBeInTheDocument();
});
@@ -152,13 +153,13 @@ describe("TunnelSafetyScreen (crowd corroboration badge - distinct resident repo
const second = await reportHazard({ segmentId: "seg-market-residential", status: "flooded" });
await waitFor(() => {
expect(screen.getByTestId(`tunnel-safety-hazard-${second.id}`)).toBeInTheDocument();
expect(screen.getByTestId(`tunnel-safety-hazard-${(second.payload as HazardReportPayload).reportId}`)).toBeInTheDocument();
});
expect(
screen.getByTestId(`tunnel-safety-hazard-${first.id}-unconfirmed`),
screen.getByTestId(`tunnel-safety-hazard-${(first.payload as HazardReportPayload).reportId}-unconfirmed`),
).toBeInTheDocument();
expect(
screen.queryByTestId(`tunnel-safety-hazard-${first.id}-corroborated`),
screen.queryByTestId(`tunnel-safety-hazard-${(first.payload as HazardReportPayload).reportId}-corroborated`),
).not.toBeInTheDocument();
});
});