fix(healthpost): water-floor interop needs distinct reporters + honest device-local tally label
The waterborne->water-floor interop fired on a single resident tapping Report diarrhea 5x (count>=5), letting one person raise everyone's water ration - contradicting Tunnel Safety's distinct-reporter corroboration standard. Added countDistinctSymptomReporters (distinct authorPubkeys) and gated the action on >=3 DISTINCT residents. Relabeled the tally 'Symptom tally (this device)' since symptom reports stay local (not synced). Follow-up: seed a demo outbreak (foreign-signed diarrhea reports) so it is cold-demoable single-device.
This commit is contained in:
1 parent
b57a041922
commit
d62d346151
4 files changed
+82
-13
No files matched your search
@@ -5,8 +5,18 @@ import userEvent from "@testing-library/user-event";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { db } from "../../lib/db.ts";
|
||||
import { init } from "../../lib/identity.ts";
|
||||
import { readAll } from "../../lib/eventlog.ts";
|
||||
import { appendEntry, readAll } from "../../lib/eventlog.ts";
|
||||
import { buildForeignSignedEntry } from "../../admin/seedEvents.ts";
|
||||
import { postSymptomReport } from "./healthpostEvents.ts";
|
||||
|
||||
async function seedForeignDiarrhea(keyByte: number, lamportTs: number): Promise<void> {
|
||||
const entry = await buildForeignSignedEntry(
|
||||
new Uint8Array(32).fill(keyByte),
|
||||
{ type: "healthpost.symptomReport", symptom: "diarrhea" },
|
||||
lamportTs,
|
||||
);
|
||||
await appendEntry(entry);
|
||||
}
|
||||
import { SymptomTallyDashboard } from "./SymptomTallyDashboard.tsx";
|
||||
|
||||
afterEach(cleanup);
|
||||
@@ -67,9 +77,11 @@ describe("SymptomTallyDashboard (QA happy path - seeded reports)", () => {
|
||||
});
|
||||
|
||||
describe("SymptomTallyDashboard - waterborne interop (Health -> Fair Share)", () => {
|
||||
it("offers raising the Fair Share water floor when diarrhea reports cluster, and appends a signed fairshare.floor", async () => {
|
||||
it("offers the water floor only after >=3 DISTINCT residents report diarrhea, then appends a signed fairshare.floor", async () => {
|
||||
const user = userEvent.setup();
|
||||
for (let i = 0; i < 5; i += 1) await postSymptomReport("diarrhea");
|
||||
await seedForeignDiarrhea(11, 101);
|
||||
await seedForeignDiarrhea(12, 102);
|
||||
await seedForeignDiarrhea(13, 103);
|
||||
|
||||
render(<SymptomTallyDashboard />);
|
||||
|
||||
@@ -90,11 +102,11 @@ describe("SymptomTallyDashboard - waterborne interop (Health -> Fair Share)", ()
|
||||
expect((floors[0]!.payload as { resource: string }).resource).toBe("water");
|
||||
});
|
||||
|
||||
it("does not offer the water-floor action below the diarrhea cluster threshold", async () => {
|
||||
await postSymptomReport("diarrhea");
|
||||
it("does NOT offer the water floor for one resident's repeated reports (anti-inflation)", async () => {
|
||||
for (let i = 0; i < 5; i += 1) await postSymptomReport("diarrhea");
|
||||
render(<SymptomTallyDashboard />);
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("healthpost-symptom-count-diarrhea")).toHaveTextContent("1"),
|
||||
expect(screen.getByTestId("healthpost-symptom-count-diarrhea")).toHaveTextContent("5"),
|
||||
);
|
||||
expect(screen.queryByTestId("healthpost-waterborne-interop")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
@@ -7,7 +7,11 @@ import {
|
||||
} from "../../lib/healthpostTally.ts";
|
||||
import { useEventLogEntries } from "../../shell/useEventLogEntries.ts";
|
||||
import { postFairshareFloor } from "../../lib/fairshare.ts";
|
||||
import { extractSymptomReports, postSymptomReport } from "./healthpostEvents.ts";
|
||||
import {
|
||||
countDistinctSymptomReporters,
|
||||
extractSymptomReports,
|
||||
postSymptomReport,
|
||||
} from "./healthpostEvents.ts";
|
||||
import styles from "./SymptomTallyDashboard.module.css";
|
||||
|
||||
/** Protective per-resident water floor a clinician can set when diarrhea reports
|
||||
@@ -15,6 +19,11 @@ import styles from "./SymptomTallyDashboard.module.css";
|
||||
* rehydration. A fixed policy default, never a modeled/forecast value. */
|
||||
const OUTBREAK_WATER_FLOOR_PER_AGENT = 25;
|
||||
|
||||
/** Distinct resident identities that must independently report diarrhea before
|
||||
* the water-floor action is offered - one person re-tapping can never raise
|
||||
* everyone's water ration (mirrors Tunnel Safety's distinct-reporter gate). */
|
||||
const MIN_DISTINCT_DIARRHEA_REPORTERS = 3;
|
||||
|
||||
const SYMPTOM_LABEL: Record<SymptomCategory, string> = {
|
||||
fever: "Fever",
|
||||
cough: "Cough",
|
||||
@@ -47,7 +56,7 @@ export function SymptomTallyDashboard() {
|
||||
const [floorSubmitting, setFloorSubmitting] = useState(false);
|
||||
const [floorStatus, setFloorStatus] = useState<string>();
|
||||
|
||||
const diarrheaRow = tally.bySymptom.find((row) => row.symptom === "diarrhea");
|
||||
const distinctDiarrheaReporters = countDistinctSymptomReporters(entries, "diarrhea");
|
||||
|
||||
async function handleRaiseWaterFloor() {
|
||||
setFloorSubmitting(true);
|
||||
@@ -74,7 +83,7 @@ export function SymptomTallyDashboard() {
|
||||
|
||||
return (
|
||||
<section className={styles.dashboard} aria-label="Health Post dashboard">
|
||||
<h2 className={styles.heading}>Community symptom tally</h2>
|
||||
<h2 className={styles.heading}>Symptom tally (this device)</h2>
|
||||
|
||||
{tally.totalReports === 0 ? (
|
||||
<p className={styles.emptyState} data-testid="healthpost-tally-empty">
|
||||
@@ -115,14 +124,15 @@ export function SymptomTallyDashboard() {
|
||||
))}
|
||||
</ul>
|
||||
|
||||
{diarrheaRow && diarrheaRow.dispatchPriority !== "routine" && (
|
||||
{distinctDiarrheaReporters >= MIN_DISTINCT_DIARRHEA_REPORTERS && (
|
||||
<div
|
||||
data-testid="healthpost-waterborne-interop"
|
||||
style={{ marginTop: 12, padding: 12, borderRadius: 10, background: "#eff6ff", border: "1px solid #bfdbfe" }}
|
||||
>
|
||||
<p style={{ margin: "0 0 8px", fontSize: 13 }}>
|
||||
{diarrheaRow.count} diarrhea reports can signal a waterborne problem. Raising the Fair
|
||||
Share water floor guarantees every resident enough water for oral rehydration.
|
||||
{distinctDiarrheaReporters} different residents have independently reported diarrhea -
|
||||
a corroborated waterborne-outbreak signal. Raising the Fair Share water floor
|
||||
guarantees every resident enough water for oral rehydration.
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { db } from "../../lib/db.ts";
|
||||
import { readAll } from "../../lib/eventlog.ts";
|
||||
import { readAll, type StoredEventLogEntry } from "../../lib/eventlog.ts";
|
||||
import { init } from "../../lib/identity.ts";
|
||||
import type { TriageResult } from "../../lib/healthpost.ts";
|
||||
import {
|
||||
extractReferrals,
|
||||
postReferral,
|
||||
countDistinctSymptomReporters,
|
||||
postSymptomReport,
|
||||
resolveReferral,
|
||||
REFERRAL_PAYLOAD_TYPE,
|
||||
@@ -227,3 +228,24 @@ describe("extractReferrals (QA failure path - malformed/forged payloads never su
|
||||
expect(extractReferrals(malformed as never)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("countDistinctSymptomReporters (corroboration gate)", () => {
|
||||
it("counts distinct signer identities, so one resident reporting many times counts once", () => {
|
||||
const mk = (pub: number, symptom: string) =>
|
||||
({
|
||||
payload: { type: "healthpost.symptomReport", symptom },
|
||||
authorPubkey: new Uint8Array([pub]),
|
||||
}) as unknown as StoredEventLogEntry;
|
||||
const entries = [
|
||||
mk(1, "diarrhea"),
|
||||
mk(1, "diarrhea"),
|
||||
mk(1, "diarrhea"),
|
||||
mk(2, "diarrhea"),
|
||||
mk(3, "diarrhea"),
|
||||
mk(4, "fever"),
|
||||
];
|
||||
expect(countDistinctSymptomReporters(entries, "diarrhea")).toBe(3);
|
||||
expect(countDistinctSymptomReporters(entries, "fever")).toBe(1);
|
||||
expect(countDistinctSymptomReporters([], "diarrhea")).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -61,6 +61,31 @@ export function extractSymptomReports(entries: StoredEventLogEntry[]): SymptomRe
|
||||
return reports;
|
||||
}
|
||||
|
||||
function pubkeyHex(bytes: Uint8Array): string {
|
||||
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Number of DISTINCT signing identities (authorPubkey) that reported a given
|
||||
* symptom. A single resident re-tapping the same report counts once, so a
|
||||
* consequential cross-app action (the waterborne-outbreak -> Fair Share
|
||||
* water-floor interop) requires genuine multi-resident corroboration, mirroring
|
||||
* Tunnel Safety's distinct-reporter corroboration gate - one person can never
|
||||
* raise everyone's water ration alone.
|
||||
*/
|
||||
export function countDistinctSymptomReporters(
|
||||
entries: StoredEventLogEntry[],
|
||||
symptom: SymptomCategory,
|
||||
): number {
|
||||
const reporters = new Set<string>();
|
||||
for (const entry of entries) {
|
||||
if (isSymptomReportPayload(entry.payload) && entry.payload.symptom === symptom) {
|
||||
reporters.add(pubkeyHex(entry.authorPubkey));
|
||||
}
|
||||
}
|
||||
return reporters.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Case-relay-to-clinician loop (plan todo 9 / GAP-8).
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user