app

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

commit 4eb1cf0b7598ea32be41c795d771a089e09a0da2
parent 9f9acc6e68ae84ceff9aeb3ee6b350392a17fc43
Author: triesap <tyson@radroots.org>
Date:   Tue,  6 Oct 2026 14:54:36 +0000

availability: Implement deterministic local food matching (HCP-038)

- Match all plain query terms across admitted active public food fields.
- Normalize search copies while preserving signed text and decimal values.
- Exclude sold, suppressed, unsupported and unavailable head projections.
- Centralize query limits and verify the exact shared compiler chunk.

Diffstat:
Mweb/src/lib/catalog/query-input.ts | 12++++++++----
Aweb/src/lib/catalog/search-match.ts | 43+++++++++++++++++++++++++++++++++++++++++++
Mweb/src/lib/config/budgets.ts | 6++++++
Mweb/tests/provenance/output.test.mjs | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Aweb/tests/unit/search-match.test.ts | 308+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tools/check-output.mjs | 13++++++++++++-
6 files changed, 463 insertions(+), 7 deletions(-)

diff --git a/web/src/lib/catalog/query-input.ts b/web/src/lib/catalog/query-input.ts @@ -1,3 +1,5 @@ +import { PUBLIC_SEARCH_BUDGETS } from '../config/budgets.ts'; + export type PublicQuery = | { readonly ok: true; @@ -13,20 +15,22 @@ const encoder = new TextEncoder(); export function normalizePublicQuery(input: unknown): PublicQuery { if (typeof input !== 'string') return { ok: false, error: 'invalid_query' }; - if (input.length > 512) return { ok: false, error: 'query_too_long' }; + if (input.length > PUBLIC_SEARCH_BUDGETS.queryBytes) + return { ok: false, error: 'query_too_long' }; if (!input.isWellFormed()) return { ok: false, error: 'invalid_query' }; for (const character of input) { const code = character.charCodeAt(0); if (code <= 8 || (code >= 14 && code <= 31) || (code >= 127 && code <= 159)) return { ok: false, error: 'invalid_query' }; } - if (encoder.encode(input).length > 512) + if (encoder.encode(input).length > PUBLIC_SEARCH_BUDGETS.queryBytes) return { ok: false, error: 'query_too_long' }; const normalized = input.normalize('NFKC').toLowerCase().trim(); const terms = normalized === '' ? [] : normalized.split(/\s+/u); - if (terms.length > 12) return { ok: false, error: 'too_many_terms' }; + if (terms.length > PUBLIC_SEARCH_BUDGETS.terms) + return { ok: false, error: 'too_many_terms' }; const text = terms.join(' '); - if (encoder.encode(text).length > 512) + if (encoder.encode(text).length > PUBLIC_SEARCH_BUDGETS.queryBytes) return { ok: false, error: 'query_too_long' }; return { ok: true, text, terms }; } diff --git a/web/src/lib/catalog/search-match.ts b/web/src/lib/catalog/search-match.ts @@ -0,0 +1,43 @@ +import { normalizePublicQuery, type PublicQuery } from './query-input.ts'; +import { + headResolutionSnapshot, + headResolverRetentionState, + type HeadResolver, + type HeadResolution +} from './resolve-head.ts'; + +export type FoodSearch = + | Exclude<PublicQuery, { ok: true }> + | Readonly<{ + ok: true; + query: string; + available: boolean; + rows: readonly HeadResolution[]; + definitiveAbsence: false; + }>; +// Matching follows genuine generic head/deletion/display admission. Partial +// source lookup may still yield a retained last-known row, never assured stock. +export function searchResolvedFood( + resolver: HeadResolver, + input: unknown +): FoodSearch { + const query = normalizePublicQuery(input); + if (!query.ok) return query; + const rows = headResolutionSnapshot(resolver).filter((row) => { + const food = row.state.food; + if (!food || food.status !== 'active') return false; + // Search copies only. No identifier, key, price, image URL or private text. + const text = [food.title, food.summary, food.content, food.location] + .map((field) => field.normalize('NFKC').toLowerCase()) + .join(' '); + return query.terms.every((term) => text.includes(term)); + }); + const available = headResolverRetentionState(resolver).available; + return { + ok: true, + query: query.text, + available, + rows: available ? rows : [], + definitiveAbsence: false + }; +} diff --git a/web/src/lib/config/budgets.ts b/web/src/lib/config/budgets.ts @@ -30,3 +30,9 @@ export const PUBLIC_QUERY_BUDGETS = Object.freeze({ export const PUBLIC_RETENTION_BUDGETS = Object.freeze({ payloadBytes: 33554432 }); + +// Query text is public input. Bound both original and normalized UTF-8 copies. +export const PUBLIC_SEARCH_BUDGETS = Object.freeze({ + queryBytes: 512, + terms: 12 +}); diff --git a/web/tests/provenance/output.test.mjs b/web/tests/provenance/output.test.mjs @@ -114,7 +114,7 @@ await test('actual output qualification', { timeout: 180000 }, async (t) => { const f = await fixture(t); const files = await f.audit(); // Exact eleven-route module/page/static admission; CSS stays once-imported. - assert.equal(files.length, 44); + assert.equal(files.length, 45); // The actual SDK payload crosses the reader scratch boundary; a reused // scratch buffer must still preserve every compiler/static byte exactly. const sizes = await Promise.all( @@ -170,7 +170,7 @@ await test('actual output qualification', { timeout: 180000 }, async (t) => { assert.ok(Buffer.byteLength(expanded) > 64 * 1024); await writeFile(file, expanded); await refresh(f); - assert.equal((await f.audit()).length, 44); + assert.equal((await f.audit()).length, 45); } ); const sdkKey = @@ -432,6 +432,90 @@ await test('actual output qualification', { timeout: 180000 }, async (t) => { } ); await check( + 'actual shared budgets chunk has owned consumers and counterpart', + async (t) => { + const f = await fixture(t); + const manifest = JSON.parse( + await readFile( + path.join(f.web, '.svelte-kit/output/client/.vite/manifest.json'), + 'utf8' + ) + ); + const entries = Object.entries(manifest).filter( + ([, record]) => record.name === 'budgets' + ); + assert.equal(entries.length, 1); + const [key, budget] = entries[0]; + assert.deepEqual(Object.keys(budget).sort(), ['file', 'name']); + for (const name of ['nodes/0', 'routes']) + assert.ok( + Object.values(manifest) + .find((record) => record.name === name) + .imports.includes(key) + ); + assert.deepEqual( + await readFile(path.join(f.web, 'build', budget.file)), + await readFile( + path.join(f.web, '.svelte-kit/output/client', budget.file) + ) + ); + assert.ok((await f.audit()).includes(budget.file)); + } + ); + for (const mutation of [ + 'unknown-name', + 'duplicate', + 'missing-module', + 'extra-field', + 'missing-root-edge', + 'missing-routes-edge', + 'missing-counterpart', + 'changed-counterpart' + ]) { + await check('budgets module fails closed: ' + mutation, async (t) => { + const f = await fixture(t); + const file = path.join( + f.web, + '.svelte-kit/output/client/.vite/manifest.json' + ); + const manifest = JSON.parse(await readFile(file, 'utf8')); + const entry = Object.entries(manifest).find( + ([, record]) => record.name === 'budgets' + ); + assert.ok(entry); + const [key, budget] = entry; + if (mutation === 'unknown-name') budget.name = 'unapproved-budgets'; + if (mutation === 'duplicate') manifest['_HCduplicate.js'] = { ...budget }; + if (mutation === 'missing-module') delete manifest[key]; + if (mutation === 'extra-field') budget.imports = []; + if ( + mutation === 'missing-root-edge' || + mutation === 'missing-routes-edge' + ) { + const name = mutation === 'missing-root-edge' ? 'nodes/0' : 'routes'; + const consumer = Object.values(manifest).find( + (record) => record.name === name + ); + consumer.imports = consumer.imports.filter( + /** @param {string} dependency */ + (dependency) => dependency !== key + ); + } + if (mutation === 'missing-counterpart') + await rm(path.join(f.web, 'build', budget.file)); + if (mutation === 'changed-counterpart') + await writeFile( + path.join(f.web, 'build', budget.file), + 'changed counterpart' + ); + await writeFile(file, JSON.stringify(manifest)); + await assert.rejects( + f.audit, + /Undeclared compiler module|Unresolved compiler import|Invalid owned budgets compiler|Static output inventory mismatch|Static output differs from owned compiler input/ + ); + }); + } + await check( 'actual route-state compiler module has an owned client counterpart', async (t) => { const f = await fixture(t); diff --git a/web/tests/unit/search-match.test.ts b/web/tests/unit/search-match.test.ts @@ -0,0 +1,308 @@ +import { afterAll, describe, expect, it } from 'vitest'; +import { readFileSync } from 'node:fs'; +import { finalizeEvent } from 'applesauce-core/helpers'; +import { normalizePublicQuery } from '../../src/lib/catalog/query-input.ts'; +import { searchResolvedFood } from '../../src/lib/catalog/search-match.ts'; +import { + createPublicHeadCandidate, + publicHeadEnvelope +} from '../../src/lib/catalog/heads.ts'; +import { + createHeadResolver, + resolveHeads, + headResolutionSnapshot +} from '../../src/lib/catalog/resolve-head.ts'; +import { + verifyEnvelope, + verifiedEnvelopeSnapshot +} from '../../src/lib/nostr/verified-envelope.ts'; +import { + createPublicScheduler, + createPublicRun, + openPublicRequest, + closePublicScheduler +} from '../../src/lib/nostr/request-scope.ts'; +import { validateRelayPolicy } from '../../src/lib/config/relays.ts'; +import { + createPublicRetention, + retainPublicEnvelope, + publicRetentionKnown +} from '../../src/lib/catalog/retention.ts'; +const corpus = JSON.parse( + readFileSync( + new URL( + '../../../contracts/interop/food_availability/corpus.v1.json', + import.meta.url + ), + 'utf8' + ) +) as { vectors: { id: string; signed_wires: Record<string, string> }[] }; +const base = JSON.parse( + corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous +) as { tags: string[][]; content: string; created_at: number }; +const cleanup: (() => void)[] = []; +afterAll(() => { + for (const stop of cleanup) stop(); +}); +function fixture() { + const key = crypto.getRandomValues(new Uint8Array(32)); + cleanup.push(() => key.fill(0)); + const proof = ( + time = base.created_at, + tags = base.tags, + content = base.content, + kind = 30402 + ) => { + const p = verifyEnvelope( + JSON.stringify( + finalizeEvent({ kind, created_at: time, tags, content }, key) + ) + ); + if (!p.ok) throw Error('search fixture'); + return p.value; + }; + const head = (p: ReturnType<typeof proof>) => { + const h = createPublicHeadCandidate(p); + if (!h) throw Error('search head'); + return h; + }; + const policy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: [], + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) + )!; + const scheduler = createPublicScheduler({ + now: () => 0, + schedule: () => () => {} + }), + run = createPublicRun(scheduler, policy); + cleanup.push(() => closePublicScheduler(scheduler)); + const retention = createPublicRetention(); + let available = true, + now = base.created_at + 10, + assessments = 0; + const model = createHeadResolver( + run, + (kind, filters, next) => openPublicRequest(run, kind, () => () => {}, next), + { + nowSeconds: () => { + assessments++; + return now; + } + }, + { + retain: (heads) => { + for (const h of heads) + retainPublicEnvelope(retention, publicHeadEnvelope(h)); + return true; + }, + read: (h) => publicRetentionKnown(retention, h), + available: () => available + } + ); + return { + proof, + head, + model, + retention, + add: (p: ReturnType<typeof proof>) => resolveHeads(model, [head(p)]), + retain: (p: ReturnType<typeof proof>) => retainPublicEnvelope(retention, p), + unavailable: () => { + available = false; + }, + clock: (value: number) => { + now = value; + }, + clockSamples: () => assessments + }; +} +describe('deterministic admitted public food matching', () => { + it('requires all terms across title summary description and public location', () => { + const f = fixture(); + f.add(f.proof()); + const result = searchResolvedFood(f.model, 'NANTES fresh week SAANICH'); + expect(result.ok).toBe(true); + if (!result.ok) throw Error('query'); + expect(result.rows).toHaveLength(1); + expect(searchResolvedFood(f.model, 'Nantes missing')).toMatchObject({ + ok: true, + rows: [] + }); + }); + it('normalizes NFKC case and whitespace copies while preserving signed food fields', () => { + const f = fixture(), + tags = base.tags.map((t) => + t[0] === 'title' ? ['title', 'CARROTS'] : [...t] + ), + p = f.proof(base.created_at, tags); + f.add(p); + const before = verifiedEnvelopeSnapshot(p); + const result = searchResolvedFood(f.model, ' carrots\n SAANICH '); + expect(result).toMatchObject({ ok: true, query: 'carrots saanich' }); + if (!result.ok) throw Error('query'); + expect(result.rows).toHaveLength(1); + expect(result.rows[0].state.food!.title).toBe('CARROTS'); + expect(verifiedEnvelopeSnapshot(p)).toEqual(before); + }); + it('empty queries browse only known active supply and preserve last-known uncertainty', () => { + const f = fixture(); + f.add(f.proof()); + const result = searchResolvedFood(f.model, ' \n '); + if (!result.ok) throw Error('query'); + expect(result.rows).toHaveLength(1); + expect(result.rows[0].lastKnown).toBe(true); + expect(result.rows[0].definitiveAbsence).toBe(false); + }); + it('sold revisions exclude the prior active keyword match', () => { + const f = fixture(); + f.add(f.proof()); + f.retain( + f.proof( + base.created_at + 1, + base.tags.map((t) => (t[0] === 'status' ? ['status', 'sold'] : [...t])) + ) + ); + expect(searchResolvedFood(f.model, 'carrots')).toMatchObject({ + ok: true, + rows: [] + }); + expect(searchResolvedFood(f.model, '')).toMatchObject({ + ok: true, + rows: [] + }); + }); + it('unsupported newest heads block older focused matches', () => { + const f = fixture(); + f.add(f.proof()); + f.retain( + f.proof( + base.created_at + 1, + base.tags.map((t) => + t[0] === 'status' ? ['status', 'unknown'] : [...t] + ) + ) + ); + expect(searchResolvedFood(f.model, 'carrots')).toMatchObject({ + ok: true, + rows: [] + }); + }); + it('authorized retained deletion evidence excludes matches without older fallback', () => { + const f = fixture(), + p = f.proof(); + f.add(p); + f.retain( + f.proof( + base.created_at - 1, + [['e', verifiedEnvelopeSnapshot(p)!.id]], + '', + 5 + ) + ); + expect(searchResolvedFood(f.model, 'carrots')).toMatchObject({ + ok: true, + rows: [] + }); + }); + it('future and clock-unavailable heads cannot match or expose older rows', () => { + const f = fixture(); + f.add(f.proof()); + f.retain(f.proof(base.created_at + 1000)); + expect(searchResolvedFood(f.model, '')).toMatchObject({ + ok: true, + rows: [] + }); + const unavailable = fixture(); + unavailable.clock(NaN); + unavailable.add(unavailable.proof()); + expect(headResolutionSnapshot(unavailable.model)[0].state.display).toBe( + 'clock_unavailable' + ); + expect(searchResolvedFood(unavailable.model, 'carrots')).toMatchObject({ + ok: true, + rows: [] + }); + }); + it('terminal projection unavailability differs from a bounded empty match', () => { + const f = fixture(); + f.add(f.proof()); + f.unavailable(); + expect(searchResolvedFood(f.model, 'carrots')).toEqual({ + ok: true, + query: 'carrots', + available: false, + rows: [], + definitiveAbsence: false + }); + }); + it('plain punctuation is literal and does not activate regex stemming or a geocoder', () => { + const f = fixture(); + f.add(f.proof()); + for (const q of ['.*', 'carrot.*', '[a-z]', 'Central.*Saanich', 'carro']) { + const result = searchResolvedFood(f.model, q); + expect(result.ok).toBe(true); + if (!result.ok) throw Error('query'); + expect(result.rows.length).toBe(q === 'carro' ? 1 : 0); + } + expect(searchResolvedFood(f.model, 'carroted')).toMatchObject({ + ok: true, + rows: [] + }); + }); + it('enforces actual512UTF8 bytes and12 normalized terms before matching', () => { + const f = fixture(); + f.add(f.proof()); + expect(searchResolvedFood(f.model, '菜'.repeat(170) + 'aa').ok).toBe(true); + expect(searchResolvedFood(f.model, '菜'.repeat(171))).toEqual({ + ok: false, + error: 'query_too_long' + }); + expect( + searchResolvedFood(f.model, Array(12).fill('carrots').join(' ')).ok + ).toBe(true); + expect( + searchResolvedFood(f.model, Array(13).fill('carrots').join(' ')) + ).toEqual({ ok: false, error: 'too_many_terms' }); + expect(searchResolvedFood(f.model, 'ﷺ'.repeat(100)).ok).toBe(false); + }); + it('rejects malformed query inputs without materializing a projection', () => { + const f = fixture(); + f.add(f.proof()); + const assessments = f.clockSamples(); + for (const input of [undefined, {}, '\ud800', '\u0000']) + expect(searchResolvedFood(f.model, input)).toEqual({ + ok: false, + error: 'invalid_query' + }); + expect(normalizePublicQuery('')).toMatchObject({ + ok: true, + text: '', + terms: [] + }); + expect(f.clockSamples()).toBe(assessments); + }); + it('returns exact decimal and unknown quantity values without conversion or mutation', () => { + const f = fixture(), + tags = base.tags + .filter((t) => t[0] !== 'radroots:quantity') + .map((t) => + t[0] === 'price' ? ['price', '9007199254740993.125', t[2]] : [...t] + ); + f.add(f.proof(base.created_at, tags)); + const result = searchResolvedFood(f.model, 'carrots'); + if (!result.ok) throw Error('query'); + expect(result.rows).toHaveLength(1); + expect(result.rows[0].state.food!.price.amount).toBe( + '9007199254740993.125' + ); + expect(result.rows[0].state.food!.quantity).toBeNull(); + expect(headResolutionSnapshot(f.model)[0].state.food!.price.amount).toBe( + '9007199254740993.125' + ); + }); +}); diff --git a/web/tools/check-output.mjs b/web/tools/check-output.mjs @@ -423,7 +423,8 @@ export async function auditOutput(webDirectory) { names.add(name); } if (hasPublicRuntime) - for (const name of ['public-key', 'dist', 'negentropy']) names.add(name); + for (const name of ['public-key', 'dist', 'negentropy', 'budgets']) + names.add(name); const admitted = new Map([['build-info.json', expectedMetadata]]); const seen = new Set(); for (const [key, record] of Object.entries(manifest)) { @@ -517,6 +518,16 @@ export async function auditOutput(webDirectory) { } const dist = requiredRecord('dist'); const root = requiredRecord('nodes/0')[1]; + const [budgetKey, budget] = requiredRecord('budgets'); + if ( + !/^_[A-Za-z0-9_-]+\.js$/.test(budgetKey) || + Object.keys(budget).some((field) => !['file', 'name'].includes(field)) + ) + throw new Error('Invalid owned budgets compiler identity'); + for (const consumer of ['nodes/0', 'routes']) { + if (!requiredRecord(consumer)[1].imports?.includes(budgetKey)) + throw new Error('Invalid owned budgets compiler edge'); + } const sdk = manifest[sdkNegentropy]; if ( !sdk ||