app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

commit 4612662f19e4c75e507cf7a55bcc94f76742009e
parent 3844f831c0bce0c9e76829bc6425d513a542dc03
Author: triesap <tyson@radroots.org>
Date:   Wed,  7 Oct 2026 10:18:17 +0000

messages: verify inbox setup through exact readback (HCP-067)

- Bind signed preference readback to actual admitted named discovery sources.
- Retain distinct discovery readback provenance alongside named write acceptance.
- Keep conflicts and stale access from opening setup, send or listing gates.
- Verify real SDK, IndexedDB and Web Locks with an access invalidation regression.

Diffstat:
Mweb/src/lib/contracts/local-records.ts | 3+++
Aweb/src/lib/messaging/inbox-setup-verification.ts | 285+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/src/lib/nostr/inbox-readback.ts | 189+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/src/lib/nostr/request-result.ts | 10++++++++--
Mweb/src/lib/nostr/request-scope.ts | 7++++---
Mweb/src/lib/persistence/artifact-records.ts | 35+++++++++++++++++++++++++----------
Mweb/src/lib/persistence/records.ts | 39+++++++++++++++++++++++++++++----------
Mweb/src/lib/runtime/public-runtime.ts | 6+++---
Aweb/tests/e2e/harness/inbox-readback.ts | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/e2e/inbox-readback.spec.ts | 465+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/integration/inbox-readback.test.ts | 23+++++++++++++++++++++++
Aweb/tests/unit/inbox-readback.test.ts | 247+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tools/check-output.mjs | 2+-
13 files changed, 1359 insertions(+), 29 deletions(-)

diff --git a/web/src/lib/contracts/local-records.ts b/web/src/lib/contracts/local-records.ts @@ -72,6 +72,9 @@ export type PublicTargetReceipt = Readonly<{ status: 'accepted' | 'refused' | 'timed_out' | 'unknown' | 'stopped'; observedAtMilliseconds: number; readbackWire: string | null; + // Preference-only actual discovery source; origin remains the ACK write target. + // Absence is legacy metadata, never an inferred cross-source observation. + readbackOrigin?: string; }>; export type PublicOperationRecord = Readonly<{ schema: 1; diff --git a/web/src/lib/messaging/inbox-setup-verification.ts b/web/src/lib/messaging/inbox-setup-verification.ts @@ -0,0 +1,285 @@ +import { + readRelayPolicy, + publicRelayTargets, + type RelayPolicy +} from '../config/relays.ts'; +import { publicRecordSnapshot } from '../persistence/records.ts'; +import { + loadPreferenceOperation, + commitPublicOperationTransition, + observePublicOperationTransition, + publicQuotaOwner, + type PublicQuotaRepository +} from '../persistence/quota.ts'; +import { preparePublicReceiptTransition } from '../persistence/artifact-records.ts'; +import { preferencePolicyFingerprint } from '../nostr/inbox-preference-publication.ts'; +import { + inboxReadbackSnapshot, + inboxReadbackEvidence, + useInboxReadback, + type InboxReadback +} from '../nostr/inbox-readback.ts'; +import { + identityMessagingOwnership, + recheckIdentityOwner, + type IdentitySession +} from '../runtime/identity-session.ts'; +import { + privateSessionOwnership, + type PrivateSession +} from '../runtime/private-session.ts'; +import { + inboxResolverCurrentAfterSample, + type InboxResolver +} from './resolve-inbox.ts'; +import { + messagingReadinessSnapshot, + type ObserveInboxAccess, + type InboxAccessObservation +} from './readiness.ts'; +type Peer = Readonly<{ author: unknown; resolver: InboxResolver }>; +type Status = + | 'invalid' + | 'unknown' + | 'stopped' + | 'busy' + | 'storage_failed' + | 'awaiting_acceptance' + | 'awaiting_readback' + | 'conflict' + | 'access_unavailable' + | 'verified' + | 'compatible_existing'; +export type InboxSetupVerificationResult = Readonly<{ + status: Status; + setupComplete: boolean; + newListingReady: boolean; + sendReady: boolean; + knownHead?: Readonly<{ id: string; createdAt: number }>; +}>; +function blocked( + status: Status, + knownHead?: InboxSetupVerificationResult['knownHead'] +): InboxSetupVerificationResult { + return { + status, + setupComplete: false, + newListingReady: false, + sendReady: false, + ...(knownHead ? { knownHead } : {}) + }; +} +// Existing compatible configuration does not create a local command or perform +// a redundant write. This is detached information, never effect permission. +export function existingInboxSetupSnapshot( + identity: IdentitySession, + session: PrivateSession | undefined, + policy: RelayPolicy, + own: InboxResolver, + observeAccess?: ObserveInboxAccess, + peer?: Peer +): InboxSetupVerificationResult { + const ready = messagingReadinessSnapshot( + identity, + session, + policy, + own, + peer, + observeAccess + ); + if (ready.ownAccess !== 'qualified_exercised') + return blocked('access_unavailable'); + return { + status: 'compatible_existing', + setupComplete: true, + newListingReady: + readRelayPolicy(policy).postingEnabled && ready.newListingReady, + sendReady: ready.sendReady + }; +} +// Explicit verification of a chosen durable operation. ACK and exact readback +// remain separate facts. No EVENT, extension signing, AUTH or auto replacement. +export async function verifyInboxSetupOperation( + repository: PublicQuotaRepository, + identity: IdentitySession, + session: PrivateSession | undefined, + policy: RelayPolicy, + reader: InboxReadback, + observeAccess?: ObserveInboxAccess, + peer?: Peer +): Promise<InboxSetupVerificationResult> { + const initial = inboxReadbackSnapshot(reader); + if (!initial) return blocked('invalid'); + if (!useInboxReadback(reader)) return blocked('stopped', initial.knownHead); + try { + if (typeof window === 'undefined' || !navigator.locks?.request) + return blocked('unknown'); + await recheckIdentityOwner(identity); + const owner = identityMessagingOwnership(identity); + if ( + !owner || + owner.owner !== initial.owner || + publicQuotaOwner(repository) !== owner.owner + ) + return blocked('stopped'); + return await navigator.locks.request( + 'harvestcircle:owner:' + owner.owner, + { mode: 'exclusive', ifAvailable: true }, + async (lock) => { + if (!lock) return blocked('busy'); + const evidence = inboxReadbackEvidence(reader); + if (!evidence || !owner.current()) + return blocked('unknown', initial.knownHead); + const original = publicRecordSnapshot( + evidence.record, + owner.owner, + evidence.id + ); + const read = await loadPreferenceOperation(repository, evidence.id); + if (!read.ok) return blocked('storage_failed'); + const row = publicRecordSnapshot(read.value, owner.owner, evidence.id); + if ( + row?.family !== 'preference_operation' || + original?.family !== 'preference_operation' || + !row.artifact || + row.artifact.wire !== original.artifact?.wire || + JSON.stringify(row.capture) !== JSON.stringify(original.capture) || + JSON.stringify(row.source) !== JSON.stringify(original.source) || + row.consent !== original.consent || + evidence.policy !== policy || + row.capture.policyFingerprint !== + (await preferencePolicyFingerprint(policy)) || + JSON.stringify(row.capture.targets) !== + JSON.stringify(publicRelayTargets(policy, 'write')) || + !owner.current() || + !evidence.current() + ) + return blocked('stopped'); + const accepted = row.receipts.filter( + (fact) => + fact.status === 'accepted' && fact.eventId === row.artifact!.eventId + ); + if (accepted.length === 0) + return blocked('awaiting_acceptance', evidence.knownHead); + // Named ACK and discovery readback may come from different fixed relays. + // Each readback retains its own actual source, never an ACK at that source. + const observed = evidence.wires; + if (observed.length === 0) + return blocked( + evidence.status === 'conflict' ? 'conflict' : 'awaiting_readback', + evidence.knownHead + ); + // A fresh readback action retains prior ACK facts and records the actual + // read observation; it never claims a new EVENT or new relay ACK. + const actionId = crypto.randomUUID(); + for (const proof of observed) { + const prior = + accepted.find((fact) => fact.origin === proof.origin) ?? + accepted[0]; + if (!owner.current() || !evidence.current()) + return blocked('stopped'); + const current = await loadPreferenceOperation( + repository, + evidence.id + ); + if (!current.ok) return blocked('storage_failed'); + const stored = publicRecordSnapshot( + current.value, + owner.owner, + evidence.id + ); + if (stored?.family !== 'preference_operation') + return blocked('storage_failed'); + if ( + stored.receipts.some( + (fact) => + fact.origin === prior.origin && + fact.readbackOrigin === proof.origin && + fact.status === 'accepted' && + fact.readbackWire === proof.wire + ) + ) + continue; + const transition = preparePublicReceiptTransition( + current.value, + owner.owner, + evidence.id, + JSON.stringify({ + actionId, + origin: prior.origin, + readbackOrigin: proof.origin, + role: 'preference', + attempt: prior.attempt, + eventId: row.artifact.eventId, + status: 'accepted', + observedAtMilliseconds: Date.now(), + readbackWire: proof.wire + }) + ); + if (!owner.current() || !evidence.current()) + return blocked('stopped'); + if ( + !transition.ok || + !( + await commitPublicOperationTransition( + repository, + transition.value + ) + ).ok || + ( + await observePublicOperationTransition( + repository, + transition.value + ) + ).state !== 'committed' + ) + return blocked('storage_failed'); + } + const privateOwner = session && privateSessionOwnership(session); + let observedAccess: InboxAccessObservation | undefined; + const ready = messagingReadinessSnapshot( + identity, + session, + policy, + evidence.resolver, + peer, + observeAccess + ? (context) => { + const observation = observeAccess(context); + observedAccess = observation; + return observation; + } + : undefined + ); + // The last head sample can invalidate exercised access. Recheck the + // retained access proof before the final clock-free ownership fences. + const final = inboxReadbackSnapshot(reader); + const accessCurrent = observedAccess?.current() ?? false; + if ( + !final || + !owner.current() || + !privateOwner?.current() || + !evidence.current() || + (peer && !inboxResolverCurrentAfterSample(peer.resolver)) + ) + return blocked('stopped', final?.knownHead); + if (final.status === 'conflict') + return blocked('conflict', final.knownHead); + if (final.status !== 'readback' || !final.complete) + return blocked('unknown', final.knownHead); + if (ready.ownAccess !== 'qualified_exercised' || !accessCurrent) + return blocked('access_unavailable', final.knownHead); + return { + status: 'verified', + setupComplete: true, + newListingReady: + readRelayPolicy(policy).postingEnabled && ready.newListingReady, + sendReady: ready.sendReady, + knownHead: final.knownHead + }; + } + ); + } catch { + return blocked('unknown'); + } +} diff --git a/web/src/lib/nostr/inbox-readback.ts b/web/src/lib/nostr/inbox-readback.ts @@ -0,0 +1,189 @@ +import { publicRelayTargets, type RelayPolicy } from '../config/relays.ts'; +import { + publicRecordSnapshot, + type PublicRecordHandle +} from '../persistence/records.ts'; +import { + verifyEnvelope, + verifiedEnvelopeSnapshot, + verifiedEnvelopeWire +} from './verified-envelope.ts'; +import { + publicViewRunCurrent, + publicViewOwnsRun, + subscribeInboxPreference, + type PublicView +} from '../runtime/public-runtime.ts'; +import { publicRequestScopeSnapshot, type PublicRun } from './request-scope.ts'; +import { + createInboxResolver, + inboxResolutionSnapshot, + inboxResolverCurrentAfterSample, + closeInboxResolver, + type InboxResolver +} from '../messaging/resolve-inbox.ts'; +declare const readbackBrand: unique symbol; +export type InboxReadback = Readonly<{ [readbackBrand]: true }>; +type Owner = Readonly<{ + owner: string; + id: string; + record: PublicRecordHandle; + policy: RelayPolicy; + resolver: InboxResolver; + wires: Map<string, string>; + current(): boolean; + closed(): boolean; + close(): void; + use(): boolean; +}>; +const owners = new WeakMap<InboxReadback, Owner>(); +const reserved = new WeakMap<PublicRun, true>(); +// Read only through the existing bounded anonymous inbox request. No raw SDK, +// arbitrary relay/filter, event publication or inferred seen-relay provenance. +export function resolveInboxReadback( + view: PublicView, + run: PublicRun, + record: PublicRecordHandle, + owner: string, + id: string, + policy: RelayPolicy +): InboxReadback | undefined { + try { + if (typeof window === 'undefined' || reserved.has(run)) return undefined; + reserved.set(run, true); + const row = publicRecordSnapshot(record, owner, id); + if ( + row?.family !== 'preference_operation' || + !row.artifact || + row.revision < 2 || + !publicViewRunCurrent(view, run) + ) + return undefined; + const verified = verifyEnvelope(row.artifact.wire); + if (!verified.ok) return undefined; + const exact = JSON.stringify(verifiedEnvelopeSnapshot(verified.value)); + const sources = publicRelayTargets(policy, 'read'), + wires = new Map<string, string>(); + let closed = false, + used = false; + const resolver = createInboxResolver( + run, + owner, + (next) => + subscribeInboxPreference(view, run, owner, (proof, source) => { + if (closed || !publicViewOwnsRun(view, run)) return; + // This source comes from admitted request dispatch, bound to this exact + // proof, not a journal that knows only an ID or a local artifact copy. + if ( + source !== undefined && + sources.includes(source) && + JSON.stringify(verifiedEnvelopeSnapshot(proof)) === exact + ) { + const wire = verifiedEnvelopeWire(proof); + if (wire !== undefined) wires.set(source, wire); + } + next(proof); + }), + () => !closed && publicViewOwnsRun(view, run) + ); + const token = Object.freeze({}) as InboxReadback; + owners.set(token, { + owner, + id, + record, + policy, + resolver, + wires, + current: () => + !closed && + publicViewOwnsRun(view, run) && + inboxResolverCurrentAfterSample(resolver), + closed: () => closed, + close() { + closed = true; + closeInboxResolver(resolver); + }, + use() { + if (closed || used) return false; + used = true; + return true; + } + }); + return token; + } catch { + return undefined; + } +} +export function inboxReadbackSnapshot(token: InboxReadback) { + try { + const saved = owners.get(token); + if (!saved || saved.closed()) return undefined; + const row = publicRecordSnapshot(saved.record, saved.owner, saved.id); + if (row?.family !== 'preference_operation' || !row.artifact) + return undefined; + const resolution = inboxResolutionSnapshot(saved.resolver), + scope = publicRequestScopeSnapshot(resolution.request); + const sources = publicRelayTargets(saved.policy, 'read'); + const complete = + resolution.coverage === 'bounded-eose' && + scope.inboxSettled === true && + resolution.author === saved.owner && + sources.length > 0 && + sources.length === resolution.sources.length && + sources.every((source) => + resolution.sources.some( + (row) => row.source === source && row.state === 'eose' + ) + ); + const knownHead = resolution.knownBase && { + id: resolution.knownBase.id, + createdAt: resolution.knownBase.createdAt + }; + const current = saved.current(); + return { + owner: saved.owner, + id: saved.id, + artifactId: row.artifact.eventId, + knownHead, + status: !current + ? ('unknown' as const) + : scope.state === 'active' + ? ('pending' as const) + : !complete + ? ('unknown' as const) + : knownHead && knownHead.id !== row.artifact.eventId + ? ('conflict' as const) + : knownHead?.id === row.artifact.eventId && saved.wires.size > 0 + ? ('readback' as const) + : ('missing' as const), + sources: Array.from(saved.wires.keys()), + complete: complete && current, + coverage: resolution.coverage, + definitiveAbsence: false as const + }; + } catch { + return undefined; + } +} +// Internal genuine owner observation. Wire is public, independently verified +// exact signed readback; this confers no signing or network permission. +export function inboxReadbackEvidence(token: InboxReadback) { + const saved = owners.get(token), + snapshot = inboxReadbackSnapshot(token); + if (!saved || !snapshot || !snapshot.complete || !saved.current()) + return undefined; + return { + ...snapshot, + record: saved.record, + policy: saved.policy, + resolver: saved.resolver, + wires: Array.from(saved.wires, ([origin, wire]) => ({ origin, wire })), + current: saved.current + }; +} +export function useInboxReadback(token: InboxReadback): boolean { + return owners.get(token)?.use() ?? false; +} +export function closeInboxReadback(token: InboxReadback): void { + owners.get(token)?.close(); +} diff --git a/web/src/lib/nostr/request-result.ts b/web/src/lib/nostr/request-result.ts @@ -51,7 +51,11 @@ export type InboxSourceHead = Readonly<{ createdAt: number; }>; export type RequestMessageResult = - | Readonly<{ status: 'accepted' | 'duplicate'; value: VerifiedEnvelope }> + | Readonly<{ + status: 'accepted' | 'duplicate'; + value: VerifiedEnvelope; + inboxSource?: string; + }> | Readonly<{ status: | 'rejected' @@ -238,7 +242,9 @@ function createResult( source.admit(admitted.status === 'duplicate'); // Record exact source evidence BEFORE any caller inserts/deduplicates into // EventStore. Only verified proof leaves this boundary, never SDK metadata. - return admitted; + return inboxAuthor === undefined + ? admitted + : { ...admitted, inboxSource: origin }; } if (!source) return { status: 'source_unknown' }; if (message.type === 'EOSE') source.control('eose'); diff --git a/web/src/lib/nostr/request-scope.ts b/web/src/lib/nostr/request-scope.ts @@ -67,7 +67,7 @@ interface RunOwner { readonly open: ( kind: RequestKind, open: (next: (message: PublicPoolMessage) => void) => () => void, - onVerified: (event: VerifiedEnvelope) => void, + onVerified: (event: VerifiedEnvelope, inboxSource?: string) => void, sampleSource?: string, inboxAuthor?: string ) => PublicRequest; @@ -320,7 +320,8 @@ export function createPublicRun( let callbackFailed = false; if (outcome.status === 'accepted' || outcome.status === 'duplicate') { try { - onVerified(outcome.value); + if (outcome.inboxSource === undefined) onVerified(outcome.value); + else onVerified(outcome.value, outcome.inboxSource); } catch { finish('error'); callbackFailed = true; @@ -397,7 +398,7 @@ export function openInboxRequest( run: PublicRun, author: unknown, open: (next: (message: PublicPoolMessage) => void) => () => void, - onVerified: (event: VerifiedEnvelope) => void + onVerified: (event: VerifiedEnvelope, inboxSource?: string) => void ): PublicRequest { const key = canonicalPublicKey(author); if (!key) throw new Error('inbox_author_invalid'); diff --git a/web/src/lib/persistence/artifact-records.ts b/web/src/lib/persistence/artifact-records.ts @@ -187,6 +187,7 @@ function sameFact(a: PublicTargetReceipt, b: PublicTargetReceipt): boolean { return ( a.actionId === b.actionId && a.origin === b.origin && + a.readbackOrigin === b.readbackOrigin && a.role === b.role && a.attempt === b.attempt && a.eventId === b.eventId && @@ -215,16 +216,30 @@ export function preparePublicReceiptTransition( const fact: unknown = JSON.parse(receiptWire); if ( JSON.stringify(fact) !== receiptWire || - !exactLocalFields(fact, [ - 'actionId', - 'origin', - 'role', - 'attempt', - 'eventId', - 'status', - 'observedAtMilliseconds', - 'readbackWire' - ]) + !( + exactLocalFields(fact, [ + 'actionId', + 'origin', + 'role', + 'attempt', + 'eventId', + 'status', + 'observedAtMilliseconds', + 'readbackWire' + ]) || + (base.row.family === 'preference_operation' && + exactLocalFields(fact, [ + 'actionId', + 'origin', + 'role', + 'attempt', + 'eventId', + 'status', + 'observedAtMilliseconds', + 'readbackWire', + 'readbackOrigin' + ])) + ) ) return failed('invalid_record'); // Validate the proposed fact through the existing complete typed codec before diff --git a/web/src/lib/persistence/records.ts b/web/src/lib/persistence/records.ts @@ -244,16 +244,35 @@ function operation(row: Record<string, unknown>): boolean { return false; for (const receipt of row.receipts as unknown[]) { if ( - !exact(receipt, [ - 'actionId', - 'origin', - 'role', - 'attempt', - 'eventId', - 'status', - 'observedAtMilliseconds', - 'readbackWire' - ]) || + !( + exact(receipt, [ + 'actionId', + 'origin', + 'role', + 'attempt', + 'eventId', + 'status', + 'observedAtMilliseconds', + 'readbackWire' + ]) || + (preference && + exact(receipt, [ + 'actionId', + 'origin', + 'role', + 'attempt', + 'eventId', + 'status', + 'observedAtMilliseconds', + 'readbackWire', + 'readbackOrigin' + ])) + ) || + ('readbackOrigin' in receipt && + (receipt.readbackWire === null || + typeof receipt.readbackOrigin !== 'string' || + canonicalRelayOrigin(receipt.readbackOrigin) !== + receipt.readbackOrigin)) || !canonicalLocalId(receipt.actionId) || receipt.role !== (preference ? 'preference' : 'publication') || !(prepared.targets as unknown[]).includes(receipt.origin) || diff --git a/web/src/lib/runtime/public-runtime.ts b/web/src/lib/runtime/public-runtime.ts @@ -377,7 +377,7 @@ export function subscribeInboxPreference( view: PublicView, run: PublicRun, author: unknown, - onVerified: (event: VerifiedEnvelope) => void + onVerified: (event: VerifiedEnvelope, inboxSource?: string) => void ): PublicRequest { const owner = viewOf(view), filters = inboxPreferenceQueries(author); @@ -386,8 +386,8 @@ export function subscribeInboxPreference( run, author, (next) => subscribePublicPool(owner.runtime.pool, filters, next), - (event) => { - if (owner.current(run)) onVerified(event); + (event, inboxSource) => { + if (owner.current(run)) onVerified(event, inboxSource); } ); } diff --git a/web/tests/e2e/harness/inbox-readback.ts b/web/tests/e2e/harness/inbox-readback.ts @@ -0,0 +1,77 @@ +export * from './inbox-preference-publisher.ts'; +export { + createPublicRuntimeContext, + mountPublicRuntime, + createPublicView, + beginPublicViewRun, + disposePublicView, + closePublicRuntime +} from '../../../src/lib/runtime/public-runtime.ts'; +export { + createPrivateSession, + closePrivateSession +} from '../../../src/lib/runtime/private-session.ts'; +export { + resolveInboxReadback, + inboxReadbackSnapshot, + inboxReadbackEvidence, + closeInboxReadback +} from '../../../src/lib/nostr/inbox-readback.ts'; +export { + verifyInboxSetupOperation, + existingInboxSetupSnapshot +} from '../../../src/lib/messaging/inbox-setup-verification.ts'; + +import type { RequestClock } from '../../../src/lib/nostr/request-scope.ts'; +export function makeReadbackClock() { + const state: { next?: () => void } = {}; + const clock: RequestClock = { + now: () => { + const next = state.next; + state.next = undefined; + next?.(); + return performance.now(); + }, + schedule: (callback, delay) => { + const timer = setTimeout(callback, delay); + return () => clearTimeout(timer); + } + }; + return { + clock, + once: (callback: () => void) => { + state.next = callback; + } + }; +} + +import { makePublisherFixture } from './inbox-preference-publisher.ts'; +import { + validateRelayPolicy, + readRelayPolicy +} from '../../../src/lib/config/relays.ts'; +export async function makeDisjointReadbackFixture() { + const fixture = await makePublisherFixture(); + const manifest = readRelayPolicy(fixture.policy); + const policy = validateRelayPolicy( + JSON.stringify({ + ...manifest, + public: [ + { + origin: 'wss://discovery.example.org', + read: true, + write: false, + nip50: false + }, + { + origin: 'wss://publisher.example.org', + read: false, + write: true, + nip50: false + } + ] + }) + ); + if (!policy) throw new Error('invalid actual disjoint policy'); + return { ...fixture, policy }; +} diff --git a/web/tests/e2e/inbox-readback.spec.ts b/web/tests/e2e/inbox-readback.spec.ts @@ -0,0 +1,465 @@ +import { test, expect, type Page } from '@playwright/test'; +import { build } from 'vite'; +import { fileURLToPath } from 'node:url'; +import { createHash } from 'node:crypto'; +import { once } from 'node:events'; +import { WebSocketServer } from 'ws'; +import { createStaticHarness } from '../integration/harness/static.ts'; +import type * as Fixture from './harness/inbox-readback.ts'; +import type { PublicQuotaRepository } from '../../src/lib/persistence/quota.ts'; +import type { BrowserDatabase } from '../../src/lib/persistence/database.ts'; +import type { PrivateSession } from '../../src/lib/runtime/private-session.ts'; +import type { + PublicRuntimeContext, + PublicView +} from '../../src/lib/runtime/public-runtime.ts'; +import type { InboxReadback } from '../../src/lib/nostr/inbox-readback.ts'; +declare global { + interface Window { + hcp067: typeof Fixture; + hcp067Clock: ReturnType<typeof Fixture.makeReadbackClock>; + hcp067Fixture: Awaited<ReturnType<typeof Fixture.makePublisherFixture>>; + hcp067Repo: PublicQuotaRepository; + hcp067Db: BrowserDatabase; + hcp067Id: string; + hcp067Private: PrivateSession | undefined; + hcp067Context: PublicRuntimeContext; + hcp067View: PublicView; + hcp067Readback: InboxReadback; + hcp067Restore?: () => void; + hcp067Trace: ReturnType<typeof Fixture.installEffectTrace>; + } +} +let server: Awaited<ReturnType<typeof createStaticHarness>>, + socketServer: WebSocketServer, + bundle: string, + endpoint: string; +let frames: string[] = [], + artifact: unknown, + competing: unknown; +let response: 'artifact' | 'missing' | 'competing' | 'partial' | 'wrong_wire' = + 'artifact'; +test.beforeAll(async () => { + server = await createStaticHarness(); + socketServer = new WebSocketServer({ + host: '127.0.0.1', + port: 0, + maxPayload: 65536 + }); + await once(socketServer, 'listening'); + const address = socketServer.address(); + if (!address || typeof address === 'string') + throw new Error('missing loopback'); + endpoint = 'ws://127.0.0.1:' + address.port; + socketServer.on('connection', (socket) => + socket.on('message', (bytes) => { + const raw = Buffer.isBuffer(bytes) + ? bytes.toString() + : Array.isArray(bytes) + ? Buffer.concat(bytes).toString() + : Buffer.from(bytes).toString(), + frame = JSON.parse(raw) as unknown[]; + if (frame[0] === 'EVENT') { + frames.push(raw); + artifact = frame[1]; + const event = artifact as { id: string }; + socket.send(JSON.stringify(['OK', event.id, true, ''])); + } else if (frame[0] === 'REQ') { + if (response !== 'missing') { + const event = + response === 'wrong_wire' + ? { + ...(artifact as Record<string, unknown>), + content: 'altered unverified' + } + : artifact; + socket.send(JSON.stringify(['EVENT', frame[1], event])); + } + if (response === 'competing') + socket.send(JSON.stringify(['EVENT', frame[1], competing])); + if (response !== 'partial') + socket.send(JSON.stringify(['EOSE', frame[1]])); + } + }) + ); + const result = await build({ + configFile: false, + logLevel: 'silent', + build: { + write: false, + minify: false, + lib: { + entry: fileURLToPath( + new URL('./harness/inbox-readback.ts', import.meta.url) + ), + name: 'hcp067', + formats: ['iife'] + } + } + }); + const output = Array.isArray(result) ? result[0] : result; + if (!('output' in output)) throw new Error('missing output'); + const chunks = output.output.filter((x) => x.type === 'chunk'); + expect(chunks).toHaveLength(1); + bundle = chunks[0].code; + console.log( + JSON.stringify({ + fixture: 'HCP067_ACTUAL_SDK_REAL_IDB_WEBLOCK_LOOPBACK', + bundleSha256: createHash('sha256').update(bundle).digest('hex'), + moduleIds: Object.keys(chunks[0].modules), + qualification: + 'controlled local source only; no real extension/operator/retention/client qualification' + }) + ); +}); +test.afterAll(async () => { + for (const socket of socketServer.clients) socket.terminate(); + await new Promise<void>((resolve) => socketServer.close(() => resolve())); + await server.close(); +}); +test.beforeEach(async ({ page }, testInfo) => { + frames = []; + response = 'artifact'; + artifact = undefined; + competing = undefined; + await page.addInitScript( + ({ endpoint }) => { + const Native = window.WebSocket; + class LoopbackSocket extends Native { + constructor(url: string | URL, protocols?: string | string[]) { + if ( + ![ + 'wss://discovery.example.org/', + 'wss://publisher.example.org/' + ].includes(String(url)) + ) + throw new Error('unexpected fixture destination'); + super(endpoint, protocols); + } + } + window.WebSocket = LoopbackSocket; + }, + { endpoint } + ); + await page.goto(server.url + '/search'); + await page.addScriptTag({ content: bundle }); + await page.evaluate(async (disjoint) => { + const s = window.hcp067, + f = disjoint + ? await s.makeDisjointReadbackFixture() + : await s.makePublisherFixture(); + window.hcp067Fixture = f; + const opened = await s.openBrowserDatabase(); + if (opened.state !== 'ready') throw new Error(opened.reason); + window.hcp067Db = opened.owner; + const repo = s.createPublicQuotaRepository(opened.owner, f.owner); + if (!repo) throw new Error('missing repo'); + window.hcp067Repo = repo; + const reviewed = await s.reviewInboxSetup( + f.identity, + f.resolve(), + f.policy, + s.setupInput(), + () => 101 + ); + if (reviewed.status !== 'review') throw new Error(reviewed.reason); + const id = crypto.randomUUID(); + window.hcp067Id = id; + await s.saveInboxPreferenceOperation( + repo, + reviewed.review, + f.resolve(), + id, + 'reviewed_global_inbox_replacement' + ); + const action = await s.beginInboxPreferenceOperation( + repo, + f.identity, + f.policy, + reviewed.review, + id, + () => Promise.resolve(f.resolve()), + 'reviewed_global_inbox_replacement' + ); + if (action.status !== 'prepared') throw new Error(action.status); + const published = await s.runInboxPreferenceOperation(action.action); + if (published.status !== 'completed') throw new Error(published.status); + window.hcp067Private = await s.createPrivateSession( + f.identity, + 'reviewed_private_session' + ); + const context = s.createPublicRuntimeContext(), + clock = s.makeReadbackClock(), + runtime = s.mountPublicRuntime(context, f.policy, clock.clock); + window.hcp067Clock = clock; + if (!runtime) throw new Error('missing runtime'); + window.hcp067Context = context; + window.hcp067View = s.createPublicView(runtime); + }, testInfo.title.includes('distinct discovery and publication origins')); +}); +test.afterEach(async ({ page }) => { + if (!page.isClosed()) + await page.evaluate(() => { + window.hcp067Restore?.(); + window.hcp067Trace?.restore(); + if (window.hcp067Readback) + window.hcp067.closeInboxReadback(window.hcp067Readback); + if (window.hcp067Context) + window.hcp067.closePublicRuntime(window.hcp067Context); + if (window.hcp067Private) + window.hcp067.closePrivateSession(window.hcp067Private); + window.hcp067Fixture?.close(); + if (window.hcp067Db) window.hcp067.closeBrowserDatabase(window.hcp067Db); + }); +}); + +async function openReadback(page: Page, settle = true) { + await page.evaluate(async () => { + const s = window.hcp067, + f = window.hcp067Fixture, + read = await s.loadPreferenceOperation( + window.hcp067Repo, + window.hcp067Id + ); + if (!read.ok) throw new Error(read.reason); + const run = s.beginPublicViewRun(window.hcp067View); + const reader = s.resolveInboxReadback( + window.hcp067View, + run, + read.value, + f.owner, + window.hcp067Id, + f.policy + ); + if (!reader) throw new Error('missing reader'); + window.hcp067Readback = reader; + }); + if (settle) + await page.waitForFunction( + () => + window.hcp067.inboxReadbackSnapshot(window.hcp067Readback)?.status !== + 'pending' + ); +} +async function verify(page: Page, qualified = true) { + return await page.evaluate(async (qualified) => { + const s = window.hcp067, + f = window.hcp067Fixture; + const result = await s.verifyInboxSetupOperation( + window.hcp067Repo, + f.identity, + window.hcp067Private, + f.policy, + window.hcp067Readback, + qualified + ? (context) => ({ + ...context, + receive: 'qualified_exercised', + archive: 'qualified_exercised', + current: () => true + }) + : undefined + ); + const read = await s.loadPreferenceOperation( + window.hcp067Repo, + window.hcp067Id + ), + row = read.ok + ? s.publicRecordSnapshot(read.value, f.owner, window.hcp067Id) + : undefined; + if (row?.family !== 'preference_operation') + throw new Error('missing operation'); + return { result, row, signs: f.signs() }; + }, qualified); +} +test('named ACK alone leaves setup closed; exact actual relay readback is durable before setup projection opens', async ({ + page +}) => { + response = 'missing'; + await openReadback(page); + let result = await verify(page); + expect(result.result.setupComplete).toBe(false); + expect(result.row?.receipts.every((f) => f.readbackWire === null)).toBe(true); + response = 'artifact'; + await openReadback(page); + result = await verify(page); + expect(result.result.status).toBe('verified'); + expect(result.result.setupComplete).toBe(true); + expect(result.result.newListingReady).toBe(true); + expect(result.result.sendReady).toBe(false); + expect(result.signs).toBe(1); + expect( + result.row?.receipts.filter((f) => f.readbackWire !== null) + ).toHaveLength(1); + expect(frames).toHaveLength(1); +}); +test('exact readback without qualified exercised access does not claim setup or perpetual availability', async ({ + page +}) => { + await openReadback(page); + const result = await verify(page, false); + expect(result.result.status).toBe('access_unavailable'); + expect(result.result.setupComplete).toBe(false); + expect(result.result.newListingReady).toBe(false); + expect(frames).toHaveLength(1); +}); +test('competing newest preference remains visible while exact artifact readback is retained separately', async ({ + page +}) => { + competing = await page.evaluate(() => window.hcp067Fixture.competing()); + response = 'competing'; + await openReadback(page); + const result = await verify(page); + expect(result.result.status).toBe('conflict'); + expect(result.result.setupComplete).toBe(false); + expect(result.result.knownHead?.id).toBe((competing as { id: string }).id); + expect( + result.row?.receipts.filter((f) => f.readbackWire !== null) + ).toHaveLength(1); + expect(result.signs).toBe(1); + expect(frames).toHaveLength(1); +}); +test('real IDB readback receipt failure prevents setup completion', async ({ + page +}) => { + await openReadback(page); + await page.evaluate(() => { + window.hcp067Restore = window.hcp067.failPreferenceWrite(4); + }); + const result = await verify(page); + expect(result.result.status).toBe('storage_failed'); + expect(result.result.setupComplete).toBe(false); + expect(result.row?.revision).toBe(3); + expect(result.row?.receipts.every((f) => f.readbackWire === null)).toBe(true); + expect(frames).toHaveLength(1); +}); +test('partial discovery and invalid signed readback cannot open setup gates', async ({ + page +}) => { + response = 'partial'; + await openReadback(page, false); + let result = await verify(page); + expect(result.result.setupComplete).toBe(false); + expect(result.result.newListingReady).toBe(false); + response = 'wrong_wire'; + await openReadback(page); + result = await verify(page); + expect(result.result.setupComplete).toBe(false); + expect(result.row?.receipts.every((f) => f.readbackWire === null)).toBe(true); + expect(frames).toHaveLength(1); +}); +test('fresh extension owner mismatch stops readback verification without another write or signature', async ({ + page +}) => { + await openReadback(page); + await page.evaluate(() => window.hcp067Fixture.changeOwner()); + const result = await verify(page); + expect(result.result.setupComplete).toBe(false); + expect(result.row?.revision).toBe(3); + expect(result.signs).toBe(1); + expect(frames).toHaveLength(1); +}); +test('already compatible configuration projects readiness without a redundant preference write', async ({ + page +}) => { + await openReadback(page); + const result = await page.evaluate(() => { + const s = window.hcp067, + f = window.hcp067Fixture; + window.hcp067Trace = s.installEffectTrace(); + const own = s.inboxReadbackEvidence(window.hcp067Readback)?.resolver; + if (!own) throw new Error('missing actual compatible resolver'); + const result = s.existingInboxSetupSnapshot( + f.identity, + window.hcp067Private, + f.policy, + own, + (context) => ({ + ...context, + receive: 'qualified_exercised', + archive: 'qualified_exercised', + current: () => true + }) + ); + return { result, events: window.hcp067Trace.events(), signs: f.signs() }; + }); + expect(result.result.setupComplete).toBe(true); + expect(result.events).toEqual([]); + expect(result.signs).toBe(1); + expect(frames).toHaveLength(1); +}); + +test('final readback clock invalidating exercised access leaves setup and listing gates closed', async ({ + page +}) => { + await openReadback(page); + const observed = await page.evaluate(async () => { + const s = window.hcp067, + f = window.hcp067Fixture; + let qualified = true, + invalidated = false; + const result = await s.verifyInboxSetupOperation( + window.hcp067Repo, + f.identity, + window.hcp067Private, + f.policy, + window.hcp067Readback, + (context) => ({ + ...context, + receive: 'qualified_exercised', + archive: 'qualified_exercised', + current: () => { + window.hcp067Clock.once(() => { + qualified = false; + invalidated = true; + }); + return qualified; + } + }) + ); + return { result, invalidated, qualified, signs: f.signs() }; + }); + expect(observed.invalidated).toBe(true); + expect(observed.qualified).toBe(false); + expect(observed.result.status).toBe('access_unavailable'); + expect(observed.result.setupComplete).toBe(false); + expect(observed.result.newListingReady).toBe(false); + expect(observed.result.sendReady).toBe(false); + expect(observed.signs).toBe(1); + expect(frames).toHaveLength(1); +}); + +test('distinct discovery and publication origins retain separate ACK and readback provenance before setup opens', async ({ + page +}) => { + await openReadback(page); + const observed = await verify(page); + expect(observed.result.status).toBe('verified'); + expect(observed.result.setupComplete).toBe(true); + expect(observed.result.newListingReady).toBe(true); + expect(observed.result.sendReady).toBe(false); + expect(observed.signs).toBe(1); + expect(frames).toHaveLength(1); + expect( + observed.row.receipts.some( + (fact) => + fact.origin === 'wss://publisher.example.org' && + fact.status === 'accepted' && + fact.readbackWire === null + ) + ).toBe(true); + const reads = observed.row.receipts.filter( + (fact) => fact.readbackWire !== null + ); + expect(reads).toHaveLength(1); + expect(reads[0]).toMatchObject({ + origin: 'wss://publisher.example.org', + readbackOrigin: 'wss://discovery.example.org', + status: 'accepted' + }); + expect( + observed.row.receipts.every( + (fact) => fact.origin === 'wss://publisher.example.org' + ) + ).toBe(true); +}); diff --git a/web/tests/integration/inbox-readback.test.ts b/web/tests/integration/inbox-readback.test.ts @@ -0,0 +1,23 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { + inboxReadbackSnapshot, + closeInboxReadback +} from '../../src/lib/nostr/inbox-readback.ts'; +import { verifyInboxSetupOperation } from '../../src/lib/messaging/inbox-setup-verification.ts'; +await test('SSR and forged readback controls grant no setup or publication authority', async () => { + assert.equal(typeof window, 'undefined'); + assert.equal(inboxReadbackSnapshot({} as never), undefined); + assert.doesNotThrow(() => closeInboxReadback({} as never)); + const result = await verifyInboxSetupOperation( + {} as never, + {} as never, + undefined, + {} as never, + {} as never + ); + assert.equal(result.status, 'invalid'); + assert.equal(result.setupComplete, false); + assert.equal(result.newListingReady, false); + assert.equal(result.sendReady, false); +}); diff --git a/web/tests/unit/inbox-readback.test.ts b/web/tests/unit/inbox-readback.test.ts @@ -0,0 +1,247 @@ +import { it, expect } from 'vitest'; +import { makeFixture } from '../e2e/harness/approved-signing.ts'; +import { fixturePolicy, discovery } from '../e2e/harness/inbox-setup.ts'; +import { + createPublicScheduler, + createPublicRun, + openInboxRequest, + openPublicRequest, + closePublicScheduler +} from '../../src/lib/nostr/request-scope.ts'; +it('forwards the actual canonical admitted inbox source with the verified proof', () => { + const f = makeFixture(10050), + scheduler = createPublicScheduler({ + now: () => 0, + schedule: () => () => {} + }); + const seen: (string | undefined)[] = []; + try { + const run = createPublicRun(scheduler, fixturePolicy()); + openInboxRequest( + run, + f.owner, + (sink) => { + sink({ + type: 'EVENT', + from: discovery + '/', + id: 'owned', + event: f.sign(f.template) + }); + sink({ type: 'EOSE', from: discovery + '/', id: 'owned' }); + return () => {}; + }, + (_proof, source?: string) => { + seen.push(source); + } + ); + expect(seen).toEqual([discovery]); + } finally { + closePublicScheduler(scheduler); + f.close(); + } +}); +it('unknown inbox sources emit no proof and ordinary public callbacks retain their original arity', () => { + const f = makeFixture(10050), + publicFixture = makeFixture(30402), + scheduler = createPublicScheduler({ + now: () => 0, + schedule: () => () => {} + }); + const inbox: unknown[] = [], + publicArguments: number[] = []; + try { + openInboxRequest( + createPublicRun(scheduler, fixturePolicy()), + f.owner, + (sink) => { + sink({ + type: 'EVENT', + from: 'wss://foreign.example.org', + id: 'owned', + event: f.sign(f.template) + }); + return () => {}; + }, + (...args) => { + inbox.push(args); + } + ); + openPublicRequest( + createPublicRun(scheduler, fixturePolicy()), + 'head', + (sink) => { + sink({ + type: 'EVENT', + from: discovery, + id: 'owned', + event: publicFixture.sign(publicFixture.template) + }); + return () => {}; + }, + (...args) => { + publicArguments.push(args.length); + } + ); + expect(inbox).toEqual([]); + expect(publicArguments).toEqual([1]); + } finally { + closePublicScheduler(scheduler); + f.close(); + publicFixture.close(); + } +}); + +import { + decodePublicRecord, + publicRecordSnapshot, + publicRecordWire +} from '../../src/lib/persistence/records.ts'; +import { + preparePublicReceiptTransition, + publicTransitionSnapshot +} from '../../src/lib/persistence/artifact-records.ts'; +function receiptFixture(kind: 10050 | 30402 = 10050) { + const f = makeFixture(kind); + const row = publicRecordSnapshot(f.record, f.owner, f.id); + if (!row || row.family === 'public_draft') + throw new Error('missing operation'); + const event = f.sign(f.template), + wire = JSON.stringify(event); + const fact = { + actionId: crypto.randomUUID(), + origin: row.capture.targets[0], + role: kind === 10050 ? 'preference' : 'publication', + attempt: 1, + eventId: event.id, + status: 'accepted', + observedAtMilliseconds: 102, + readbackWire: wire + }; + const record = { + ...row, + revision: 2, + artifact: { eventId: event.id, wire }, + receipts: [] + }; + return { ...f, record, fact, wire }; +} +it('preference readback names its actual discovery origin without changing the acknowledged write target', () => { + const f = receiptFixture(); + try { + const row = { + ...f.record, + receipts: [{ ...f.fact, readbackOrigin: 'wss://discovery.example.org' }] + }; + const raw = JSON.stringify(row), + decoded = decodePublicRecord(raw, f.owner, f.id); + expect(decoded.ok).toBe(true); + if (!decoded.ok) throw new Error(decoded.reason); + expect(publicRecordWire(decoded.value, f.owner, f.id)).toBe(raw); + } finally { + f.close(); + } +}); +it('legacy ACK and legacy readback receipts roundtrip unchanged without an invented discovery source', () => { + const f = receiptFixture(); + try { + for (const readbackWire of [null, f.wire]) { + const raw = JSON.stringify({ + ...f.record, + receipts: [{ ...f.fact, readbackWire }] + }); + const decoded = decodePublicRecord(raw, f.owner, f.id); + expect(decoded.ok).toBe(true); + if (!decoded.ok) throw new Error(decoded.reason); + expect(publicRecordWire(decoded.value, f.owner, f.id)).toBe(raw); + } + } finally { + f.close(); + } +}); +it('readback source rejects null wire, noncanonical origins, arbitrary fields, invalid signatures and ordinary publication rows', () => { + const f = receiptFixture(), + publicFixture = receiptFixture(30402); + try { + for (const fact of [ + { + ...f.fact, + readbackOrigin: 'wss://discovery.example.org', + readbackWire: null + }, + { ...f.fact, readbackOrigin: 'wss://discovery.example.org/' }, + { ...f.fact, readbackOrigin: 'ws://127.0.0.1' }, + { ...f.fact, readbackOrigin: 'wss://discovery.example.org', extra: true }, + { + ...f.fact, + readbackOrigin: 'wss://discovery.example.org', + readbackWire: JSON.stringify({ + ...JSON.parse(f.wire), + content: 'forged' + }) + } + ]) + expect( + decodePublicRecord( + JSON.stringify({ ...f.record, receipts: [fact] }), + f.owner, + f.id + ).ok + ).toBe(false); + expect( + decodePublicRecord( + JSON.stringify({ + ...publicFixture.record, + receipts: [ + { + ...publicFixture.fact, + readbackOrigin: 'wss://discovery.example.org' + } + ] + }), + publicFixture.owner, + publicFixture.id + ).ok + ).toBe(false); + } finally { + f.close(); + publicFixture.close(); + } +}); +it('distinct discovery sources remain separate typed receipt facts within the same readback action', () => { + const f = receiptFixture(); + try { + const decoded = decodePublicRecord(JSON.stringify(f.record), f.owner, f.id); + if (!decoded.ok) throw new Error(decoded.reason); + const first = preparePublicReceiptTransition( + decoded.value, + f.owner, + f.id, + JSON.stringify({ + ...f.fact, + readbackOrigin: 'wss://discovery.example.org' + }) + ); + expect(first.ok).toBe(true); + if (!first.ok) throw new Error(first.reason); + const transition = publicTransitionSnapshot(first.value, f.owner, f.id); + const next = decodePublicRecord(transition?.nextWire, f.owner, f.id); + if (!next.ok) throw new Error(next.reason); + const second = preparePublicReceiptTransition( + next.value, + f.owner, + f.id, + JSON.stringify({ ...f.fact, readbackOrigin: 'wss://second.example.org' }) + ); + expect(second.ok).toBe(true); + if (!second.ok) throw new Error(second.reason); + const settled = publicTransitionSnapshot(second.value, f.owner, f.id); + const both = decodePublicRecord(settled?.nextWire, f.owner, f.id); + if (!both.ok) throw new Error(both.reason); + const row = publicRecordSnapshot(both.value, f.owner, f.id); + expect(row && row.family !== 'public_draft' && row.receipts).toHaveLength( + 2 + ); + } finally { + f.close(); + } +}); diff --git a/web/tools/check-output.mjs b/web/tools/check-output.mjs @@ -415,7 +415,7 @@ async function productPresentationAdmission(web) { 'src/lib/navigation-copy.ts': '1a17eac4b355d8a3fb2b55b3fe1bc856cc3b3bce07434836d708b68407686f20', 'src/lib/runtime/public-runtime.ts': - '7098e5333fc25b6c6238676d177e2f38d7862c679f52b16926aace7cbff367b8', + 'fa2e27d72b82b6bef73693cf93f19614e61ea51ebd11eb40d8ca4ec723f0c14e', 'src/lib/catalog/publishers.ts': 'a6795a35ebbf97246e22235df1ca868432e2cccfb26a38f0ebdf4524911192e6', 'src/lib/catalog/resolve-head.ts':