Compare commits
19
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2533f83af3 | ||
|
|
209e21a951 | ||
|
|
2a10a20518 | ||
|
|
a2293c6e34 | ||
|
|
179f8fe6e9 | ||
|
|
9f69730780 | ||
|
|
fa5f4232dd | ||
|
|
1d338ed35b | ||
|
|
6aacefca65 | ||
|
|
578cc9f129 | ||
|
|
860c84f0fe | ||
|
|
3565ce87a4 | ||
|
|
4ac71915fb | ||
|
|
5a15fa660e | ||
|
|
2094a07d9f | ||
|
|
baabeb0f4f | ||
|
|
11c63a8d25 | ||
|
|
ef22ea8970 | ||
|
|
d0b3c10aea |
No files matched your search
@@ -33,6 +33,8 @@ jobs:
|
||||
run: |
|
||||
npm test
|
||||
python3 tests/staging-deploy.test.py -v
|
||||
python3 tests/corpus-manifest.test.py -v
|
||||
python3 tests/calibration-harness.test.py -v
|
||||
- name: Mobile browser acceptance
|
||||
run: |
|
||||
npm start > /tmp/timmy-server.log 2>&1 &
|
||||
@@ -59,6 +61,8 @@ jobs:
|
||||
npm run test:worker-readiness
|
||||
npm run test:pattern-summary
|
||||
npm run test:photo-lifecycle
|
||||
npm run test:training-contribution
|
||||
npm run test:portability
|
||||
- name: Dependency audit
|
||||
run: npm audit --audit-level=high
|
||||
- name: Syntax checks
|
||||
|
||||
@@ -100,6 +100,7 @@ npm run test:manual-fallback # vision-offline manual path + in-session draft re
|
||||
npm run test:vision-refusal # corpus refusals through a real server and a real browser
|
||||
npm run test:grounded-chat # chat answers cite the ledger, or refuse to answer
|
||||
npm run test:photo-lifecycle # photo presence, explicit reveal, remove-photo-keeps-log, delete-all
|
||||
npm run test:calibration # the release-gate harness: gates, threshold protocol, refused corpora
|
||||
npm audit --audit-level=high
|
||||
```
|
||||
|
||||
@@ -109,7 +110,7 @@ npm audit --audit-level=high
|
||||
- Client-side image compression before transfer
|
||||
- Explicit consent before one-time provider analysis
|
||||
- Structured AI suggestions for **visible Bristol form and color only**
|
||||
- Confidence display, low-confidence abstention, and user confirmation
|
||||
- Confidence display, low-confidence abstention, and user confirmation, governed by one versioned policy: `poor` image quality abstains even at high confidence, and a confident reply is never enough on its own — see [docs/ABSTENTION-POLICY.md](docs/ABSTENTION-POLICY.md)
|
||||
- Urgency, discomfort, notes, and symptoms remain strictly user-reported
|
||||
- Manual logging that never uploads
|
||||
- Sleek three-destination mobile shell with one dominant photo action
|
||||
@@ -122,7 +123,7 @@ npm audit --audit-level=high
|
||||
- A committed CC0 synthetic vision corpus that continuously proves every reply is schema-valid or fails closed. A schema-valid reply with no readable form or colour now **abstains** ("No confident match") instead of surfacing as a worker failure, and unparsable or out-of-range replies return plain copy — "No suggestion was applied. Continue manually, or try a clearer photo." — with no provider text, image bytes, or credentials echoed. See [tests/fixtures/vision-corpus](tests/fixtures/vision-corpus/README.md)
|
||||
- Unsaved logging drafts are retained in the session with an explicit start over, and the service worker never caches analysis requests or image bodies
|
||||
- Private photo lifecycle: a confirmed log reports a saved photo as a label rather than a thumbnail, showing the image is an explicit request, removing the photo keeps every confirmed field, deleting a log is a separate two-step decision, and delete-all removes the ledger and every stored photo together
|
||||
- Local browser ledger, calendar, pattern summary, JSON portability, and delete-all
|
||||
- Local browser ledger, calendar, pattern summary, versioned JSON backup (logs, training receipts, and per-log AI provenance) with safe upgrade/refusal on import, and delete-all
|
||||
- Explainable pattern summaries: the home insight card reports the confirmed-log count and form groupings for the last 28 days, every line names the exact confirmed records behind it, following a citation opens those records, and a window below the minimum says it is too sparse instead of describing a trend. No line explains a cause, rules on food, or offers a health verdict
|
||||
- Installable/offline PWA shell for the manual and saved-ledger paths
|
||||
|
||||
@@ -132,6 +133,8 @@ The positive photo-prefill interface is covered by a deterministic provider fixt
|
||||
|
||||
Timmy does not diagnose disease, identify bleeding, infer pain/urgency, recommend treatment, or clear foods. The selected VLM is a bootstrap worker, not the final classifier. See [research/SELF-HOSTED-STOOL-VISION.md](research/SELF-HOSTED-STOOL-VISION.md) for the measured receipts, hardware gate, consented-data pipeline, specialist-classifier plan, and sources.
|
||||
|
||||
The calibration harness ([scripts/calibration_report.py](scripts/calibration_report.py), [contract](docs/EVALUATION-HARNESS.md)) computes the ROADMAP release gates — macro-F1, precision-at-coverage, and calibration error — from prediction files, chooses a threshold on validation only, and refuses a corpus that leaks one contributor across two splits. Its committed fixtures are invented numbers that exercise the arithmetic: **no candidate model has been measured against these gates yet, and the harness itself is not evidence about accuracy.**
|
||||
|
||||
## Architecture
|
||||
|
||||
```text
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { bucketForBristolType, detectUrgentFlags, detectUrgentText, exportLedger, hasUrgentLedgerContext, importLedger, photoQualityMessage, sanitizeEntry, urgentChatMessage } from './src/domain.js';
|
||||
import { bucketForBristolType, detectUrgentFlags, detectUrgentText, exportLedgerBackup, LEDGER_EXPORT_VERSION, hasUrgentLedgerContext, importLedgerBackup, photoQualityMessage, sanitizeEntry, urgentChatMessage } from './src/domain.js';
|
||||
import { mergeVisualSuggestion } from './src/analysis.js';
|
||||
import { createDraft, draftNotice, isDraftDirty } from './src/draft.js';
|
||||
import { changeVisualField, confirmSuggestion, createSuggestionState, provenanceCopy } from './src/suggestion.js';
|
||||
@@ -7,6 +7,7 @@ import { visionReadiness } from './src/vision-readiness.js';
|
||||
import { buildPatternSummary } from './src/summary.js';
|
||||
import { answerFromLedger, buildChatLedger, resolveAgentReply } from './src/grounded-chat.js';
|
||||
import { photoState, withoutEntry, withoutPhoto } from './src/photo-lifecycle.js';
|
||||
import { addContribution, consentTerms, contributionEligibility, contributionForEntry, receiptPreview, withdrawContribution } from './src/training-contribution.js';
|
||||
|
||||
const runtimeConfig = {
|
||||
basePath: document.querySelector('meta[name="timmy-base-path"]')?.content || '/',
|
||||
@@ -16,6 +17,7 @@ const BASE_PATH = runtimeConfig.basePath || '/';
|
||||
const APP_ROOT = BASE_PATH === '/' ? '/' : `${BASE_PATH}/`;
|
||||
function appPath(path='') { return `${APP_ROOT}${String(path).replace(/^\/+/, '')}`; }
|
||||
const STORE = `timmy:${BASE_PATH}:ledger-v1`;
|
||||
const TRAINING_STORE = `timmy:${BASE_PATH}:training-contributions-v1`;
|
||||
const LEGACY_STORE = 'timmy-ledger-v1';
|
||||
if (BASE_PATH === '/') {
|
||||
const legacyEntries = localStorage.getItem(LEGACY_STORE);
|
||||
@@ -28,6 +30,7 @@ if (BASE_PATH === '/') {
|
||||
}
|
||||
const app = document.querySelector('#app');
|
||||
let entries = loadEntries();
|
||||
let contributions = loadContributions();
|
||||
let view = 'home';
|
||||
let photoHint = '';
|
||||
let aiSuggestion = null;
|
||||
@@ -39,6 +42,10 @@ let chatBusy = false;
|
||||
let chatError = '';
|
||||
let chatMessages = [{ role: 'timmy', text: 'Ask me about your confirmed logs, visible patterns, privacy, or how Timmy works.' }];
|
||||
let draftResumed = false;
|
||||
// Issue #35 — importing a backup reports which version the file came from and what changed, and a
|
||||
// refused file leaves the ledger exactly as it was. The notice is state, not a toast, because a
|
||||
// message that disappears in two seconds cannot tell anyone whether their records were replaced.
|
||||
let portabilityNotice = null;
|
||||
let form = createDraft();
|
||||
let followUpAnswers = createFollowUpAnswers();
|
||||
|
||||
@@ -86,6 +93,10 @@ function refreshFollowUp(){
|
||||
function esc(value='') { return String(value).replace(/[&<>'"]/g, c => ({'&':'&','<':'<','>':'>',"'":''','"':'"'}[c])); }
|
||||
function loadEntries() { try { return JSON.parse(localStorage.getItem(STORE) || '[]'); } catch { return []; } }
|
||||
function saveEntries() { localStorage.setItem(STORE, JSON.stringify(entries)); }
|
||||
// Training contribution receipts are their own store: a journal save never writes here, and a
|
||||
// withdrawal keeps a tombstone instead of quietly erasing the audit trail.
|
||||
function loadContributions() { try { return JSON.parse(localStorage.getItem(TRAINING_STORE) || '[]'); } catch { return []; } }
|
||||
function saveContributions() { localStorage.setItem(TRAINING_STORE, JSON.stringify(contributions)); }
|
||||
function formatDate(value) { return new Intl.DateTimeFormat(undefined,{month:'short',day:'numeric',hour:'numeric',minute:'2-digit'}).format(new Date(value)); }
|
||||
function toast(message) { const node=document.createElement('div');node.className='toast';node.textContent=message;document.body.append(node);setTimeout(()=>node.remove(),2400); }
|
||||
|
||||
@@ -101,6 +112,8 @@ function bindGlobal(){
|
||||
document.querySelectorAll('[data-scan]').forEach(btn=>btn.onclick=openPhotoFirst);
|
||||
document.querySelectorAll('[data-statement-link]').forEach(btn=>btn.onclick=()=>showPatternRecords(btn.dataset.statementLink));
|
||||
document.querySelectorAll('[data-entry-open]').forEach(btn=>btn.onclick=()=>showEntrySheet(btn.dataset.entryOpen));
|
||||
document.querySelectorAll('[data-contribute-entry]').forEach(btn=>btn.onclick=()=>showContributionConsent(btn.dataset.contributeEntry));
|
||||
document.querySelectorAll('[data-withdraw]').forEach(btn=>btn.onclick=()=>withdrawTrainingContribution(btn.dataset.withdraw));
|
||||
}
|
||||
|
||||
function recentList(limit=5){
|
||||
@@ -122,7 +135,7 @@ function showEntrySheet(id, armed=false){
|
||||
const urgent=detectUrgentFlags(entry.symptoms);
|
||||
document.querySelector('.sheet-backdrop')?.remove();
|
||||
const wrap=document.createElement('div');wrap.className='sheet-backdrop';
|
||||
wrap.innerHTML=`<section class="sheet" role="dialog" aria-modal="true" aria-labelledby="entry-sheet-title"><div class="sheet-handle"></div><div class="sheet-header"><div><span class="eyebrow">Confirmed log</span><h2 id="entry-sheet-title">${formatDate(entry.occurredAt)}</h2></div><button class="icon-btn" id="entry-sheet-close" aria-label="Close">×</button></div><div class="entry-facts"><div><span>Form</span><strong>Type ${entry.bristolType}</strong></div><div><span>Color</span><strong>${esc(entry.color)}</strong></div><div><span>Urgency</span><strong>${entry.urgency}/4</strong></div><div><span>Discomfort</span><strong>${entry.discomfort}/4</strong></div></div>${entry.note?`<p class="fine entry-note">${esc(entry.note)}</p>`:''}${urgent.urgent?alertHtml(urgent.message):''}${photo.hasPhoto?`<section class="photo-lifecycle" data-photo-lifecycle="present"><strong>Photo saved with this log</strong><p class="fine">The image stays in this browser. The journal never shows it as a thumbnail until you ask.</p><div id="entry-photo-slot"></div><div class="row"><button class="btn btn-secondary" id="show-entry-photo">Show photo</button><button class="btn btn-ghost" id="remove-entry-photo">Remove photo, keep log</button></div></section>`:`<section class="photo-lifecycle" data-photo-lifecycle="absent"><strong>No photo saved</strong><p class="fine">This confirmed log is text only. Its record does not depend on an image.</p></section>`}<div class="entry-actions">${armed?`<button class="btn btn-danger btn-wide" id="delete-entry-confirm">Tap again to delete this log and its photo</button><p class="fine">This removes the confirmed log and its saved photo from this browser. It cannot be undone.</p>`:`<button class="btn btn-ghost btn-wide" id="delete-entry">Delete this log and its photo</button>`}</div></section>`;
|
||||
wrap.innerHTML=`<section class="sheet" role="dialog" aria-modal="true" aria-labelledby="entry-sheet-title"><div class="sheet-handle"></div><div class="sheet-header"><div><span class="eyebrow">Confirmed log</span><h2 id="entry-sheet-title">${formatDate(entry.occurredAt)}</h2></div><button class="icon-btn" id="entry-sheet-close" aria-label="Close">×</button></div><div class="entry-facts"><div><span>Form</span><strong>Type ${entry.bristolType}</strong></div><div><span>Color</span><strong>${esc(entry.color)}</strong></div><div><span>Urgency</span><strong>${entry.urgency}/4</strong></div><div><span>Discomfort</span><strong>${entry.discomfort}/4</strong></div></div>${entry.note?`<p class="fine entry-note">${esc(entry.note)}</p>`:''}${entry.provenance?`<p class="fine entry-provenance" data-entry-provenance="${esc(entry.provenance.source)}">${esc(entry.provenance.source==='ai-corrected'?'Saved from an AI suggestion you corrected. Your values are stored, not the model’s.':'Saved from an AI suggestion you confirmed: visible form and color only, never a diagnosis.')}${entry.provenance.model?` Model: ${esc(entry.provenance.model)}.`:''}</p>`:''}${urgent.urgent?alertHtml(urgent.message):''}${photo.hasPhoto?`<section class="photo-lifecycle" data-photo-lifecycle="present"><strong>Photo saved with this log</strong><p class="fine">The image stays in this browser. The journal never shows it as a thumbnail until you ask.</p><div id="entry-photo-slot"></div><div class="row"><button class="btn btn-secondary" id="show-entry-photo">Show photo</button><button class="btn btn-ghost" id="remove-entry-photo">Remove photo, keep log</button></div></section>`:`<section class="photo-lifecycle" data-photo-lifecycle="absent"><strong>No photo saved</strong><p class="fine">This confirmed log is text only. Its record does not depend on an image.</p></section>`}<div class="entry-actions">${armed?`<button class="btn btn-danger btn-wide" id="delete-entry-confirm">Tap again to delete this log and its photo</button><p class="fine">This removes the confirmed log and its saved photo from this browser. It cannot be undone.</p>`:`<button class="btn btn-ghost btn-wide" id="delete-entry">Delete this log and its photo</button>`}</div></section>`;
|
||||
document.body.append(wrap);
|
||||
if(entrySheetKeyHandler)document.removeEventListener('keydown',entrySheetKeyHandler);
|
||||
const close=()=>{wrap.remove();document.removeEventListener('keydown',onKey);if(entrySheetKeyHandler===onKey)entrySheetKeyHandler=null};
|
||||
@@ -245,13 +258,68 @@ async function sendChat(event){
|
||||
finally{chatBusy=false;timmy()}
|
||||
}
|
||||
|
||||
// Issue #24 — contributing a confirmed photo to training is a second, separate consent. A journal
|
||||
// save writes nothing here, the consent version and the receipt are on screen before the confirm
|
||||
// control can be pressed, and a withdrawal keeps a tombstone naming every derived artifact by
|
||||
// contribution id. Nothing is transmitted in this build: the receipt is queued on the device.
|
||||
function newContributionId(){
|
||||
const raw=globalThis.crypto?.randomUUID?.()||`${Date.now().toString(16)}${Math.random().toString(16).slice(2)}`;
|
||||
return `contrib-${String(raw).replace(/[^a-z0-9]/gi,'').slice(0,12)}`;
|
||||
}
|
||||
function trainingConsentSection(){
|
||||
const terms=consentTerms();
|
||||
const queued=contributions.filter(receipt=>receipt.state!=='withdrawn').length;
|
||||
const active=contributions.filter(receipt=>receipt.state!=='withdrawn').map(receipt=>`<div class="contribution-row" data-contribution-state="${esc(receipt.state)}"><div><strong>Type ${receipt.labels.bristolType} · ${esc(receipt.labels.color)}</strong><span>Receipt ${esc(receipt.contributionId)} · consent ${esc(receipt.consentVersion)}</span></div><button class="btn btn-ghost" data-withdraw="${esc(receipt.contributionId)}">Withdraw</button></div>`);
|
||||
const offerable=entries.filter(entry=>contributionEligibility(entry).eligible&&!contributions.some(receipt=>receipt.entryId===entry.id)).map(entry=>`<div class="contribution-row"><div><strong>${formatDate(entry.occurredAt)}</strong><span>Type ${entry.bristolType} · photo saved</span></div><button class="btn btn-secondary" data-contribute-entry="${esc(entry.id)}">Review opt-in</button></div>`);
|
||||
const tombstones=contributions.filter(receipt=>receipt.state==='withdrawn').map(receipt=>`<div class="contribution-row withdrawn" data-contribution-state="withdrawn"><div><strong>Withdrawn ${esc(formatDate(receipt.withdrawnAt))}</strong><span data-withdrawn-artifacts>raw image, confirmed label · receipt ${esc(receipt.contributionId)}</span></div></div>`);
|
||||
const rows=[...active,...offerable].join('');
|
||||
return `<section class="section card training-consent" data-training-consent><h2>Training contribution</h2><p class="fine">Off by default. Saving a log never contributes it, and nothing is uploaded in this build — a receipt is queued on this device.</p><p class="fine">Consent version <code data-contribution-consent-version>${esc(terms.version)}</code> · <span data-contribution-queued-count>${queued}</span> queued</p>${rows?`<div class="contribution-list">${rows}</div>`:'<p class="fine">No saved photos are available to contribute yet.</p>'}${tombstones.length?`<h3>Withdrawn receipts</h3>${tombstones.join('')}`:''}<p class="fine">Deleting all local data below also clears these receipts.</p></section>`;
|
||||
}
|
||||
let contributionSheetKeyHandler=null;
|
||||
function showContributionConsent(entryId){
|
||||
const entry=entries.find(item=>item.id===entryId);
|
||||
const eligibility=contributionEligibility(entry);
|
||||
if(!eligibility.eligible){toast(eligibility.reason||'This log cannot be contributed.');return}
|
||||
if(contributionForEntry(contributions,entryId)){toast('This log already has an active contribution receipt.');return}
|
||||
const terms=consentTerms();
|
||||
const contributionId=newContributionId();
|
||||
const consentedAt=new Date().toISOString();
|
||||
document.querySelector('.sheet-backdrop')?.remove();
|
||||
const wrap=document.createElement('div');wrap.className='sheet-backdrop';
|
||||
wrap.innerHTML=`<section class="sheet" role="dialog" aria-modal="true" aria-labelledby="contribution-title" data-contribution-sheet><div class="sheet-handle"></div><div class="sheet-header"><div><span class="eyebrow">Separate consent</span><h2 id="contribution-title">Contribute one photo?</h2></div><button class="icon-btn" id="contribution-cancel" aria-label="Close">×</button></div><div class="consent-card"><p class="fine">Consent version <code data-contribution-version>${esc(terms.version)}</code></p><p class="fine">Receipt <code data-receipt-id>${esc(contributionId)}</code> · ${esc(formatDate(consentedAt))}</p><p class="fine"><strong>Included:</strong> ${esc(terms.includes.join('; '))}.</p><p class="fine"><strong>Never included:</strong> ${esc(terms.excludes.join('; '))}.</p><p class="fine">${esc(terms.transport)}</p></div><label class="check"><input id="training-consent-check" type="checkbox"><span>I have read consent ${esc(terms.version)} and want this one log contributed.</span></label><button class="btn btn-primary btn-wide" id="contribute-confirm" disabled>Contribute this photo and label</button><button class="btn btn-ghost btn-wide section" id="contribution-cancel-bottom">Not now</button></section>`;
|
||||
document.body.append(wrap);
|
||||
if(contributionSheetKeyHandler)document.removeEventListener('keydown',contributionSheetKeyHandler);
|
||||
const close=()=>{wrap.remove();document.removeEventListener('keydown',onKey);if(contributionSheetKeyHandler===onKey)contributionSheetKeyHandler=null};
|
||||
const onKey=event=>{if(event.key==='Escape')close()};
|
||||
contributionSheetKeyHandler=onKey;
|
||||
document.addEventListener('keydown',onKey);
|
||||
document.querySelector('#contribution-cancel').onclick=close;
|
||||
document.querySelector('#contribution-cancel-bottom').onclick=close;
|
||||
wrap.onclick=event=>{if(event.target===wrap)close()};
|
||||
const check=document.querySelector('#training-consent-check'),confirm=document.querySelector('#contribute-confirm');
|
||||
check.onchange=()=>{confirm.disabled=!check.checked};
|
||||
confirm.onclick=()=>{
|
||||
try{
|
||||
contributions=addContribution(contributions,receiptPreview(entry,{contributionId,consentedAt}));
|
||||
saveContributions();close();render();toast('Queued on this device. Nothing was uploaded.');
|
||||
}catch(error){toast(error.message||'That receipt could not be recorded.')}
|
||||
};
|
||||
}
|
||||
function withdrawTrainingContribution(contributionId){
|
||||
const result=withdrawContribution(contributions,contributionId,new Date().toISOString());
|
||||
if(!result.tombstone){toast('That contribution receipt was not found.');return}
|
||||
contributions=result.contributions;saveContributions();render();toast('Contribution withdrawn. The receipt is kept as a tombstone.');
|
||||
return result.tombstone;
|
||||
}
|
||||
|
||||
function privacy(){
|
||||
shell(`<main><div class="page-title"><span class="eyebrow">Private by design</span><h1>Your poop. Your phone.</h1><p>This prototype has no account, analytics, ad tracker, or server database.</p></div><section class="card privacy-list"><div class="privacy-item"><b>⌂</b><div><h3>Stored locally</h3><p>Saved entries and optional photos live in this browser’s local storage. You can remove one photo without deleting its confirmed log.</p></div></div><div class="privacy-item"><b>⇩</b><div><h3>Portable</h3><p>Export a readable JSON file. Import it in another copy of Timmy.</p></div></div><div class="privacy-item"><b>◎</b><div><h3>AI only when you ask</h3><p>Manual logging never uploads. Analyze a photo sends one compressed copy after consent. Hermes chat may receive up to 20 confirmed text-only entries after you connect; photos and backend credentials never enter chat.</p></div></div></section><section class="section card"><h2>Data controls</h2><div class="row"><button class="btn btn-primary" id="export">Export JSON</button><label class="btn btn-secondary" for="import">Import JSON</label><input class="hidden" type="file" id="import" accept="application/json"></div><p class="fine section">Exports can contain sensitive health notes and photos. Store them somewhere you trust.</p></section><section class="section card source-list"><h2>Health sources</h2><p class="fine">The Bristol groupings and urgent-symptom copy are grounded in public clinical guidance.</p><p><a target="_blank" rel="noreferrer" href="https://www.continence.org.au/about-incontinence/bowel-incontinence/bristol-stool-chart/">Continence Health Australia</a></p><p><a target="_blank" rel="noreferrer" href="https://www.nhs.uk/conditions/bleeding-from-the-bottom-rectal-bleeding/">NHS rectal bleeding guidance</a></p><p><a target="_blank" rel="noreferrer" href="https://www.niddk.nih.gov/health-information/digestive-diseases/constipation/symptoms-causes">NIDDK constipation guidance</a></p></section><section class="section card danger-zone"><h2>Delete everything</h2><p class="fine">Permanently removes Timmy’s local ledger and every saved photo from this browser.</p><button class="btn btn-danger" id="delete-all">Delete all local data</button></section></main>`);
|
||||
shell(`<main><div class="page-title"><span class="eyebrow">Private by design</span><h1>Your poop. Your phone.</h1><p>This prototype has no account, analytics, ad tracker, or server database.</p></div><section class="card privacy-list"><div class="privacy-item"><b>⌂</b><div><h3>Stored locally</h3><p>Saved entries and optional photos live in this browser’s local storage. You can remove one photo without deleting its confirmed log.</p></div></div><div class="privacy-item"><b>⇩</b><div><h3>Portable</h3><p>Export a readable JSON file. Import it in another copy of Timmy.</p></div></div><div class="privacy-item"><b>◎</b><div><h3>AI only when you ask</h3><p>Manual logging never uploads. Analyze a photo sends one compressed copy after consent. Hermes chat may receive up to 20 confirmed text-only entries after you connect; photos and backend credentials never enter chat.</p></div></div></section><section class="section card"><h2>Data controls</h2><div class="row"><button class="btn btn-primary" id="export">Export JSON</button><label class="btn btn-secondary" for="import">Import JSON</label><input class="hidden" type="file" id="import" accept="application/json"></div><p class="fine section">Exports can contain sensitive health notes and photos. Store them somewhere you trust.</p></section>${portabilityNoticeBlock()}${trainingConsentSection()}<section class="section card source-list"><h2>Health sources</h2><p class="fine">The Bristol groupings and urgent-symptom copy are grounded in public clinical guidance.</p><p><a target="_blank" rel="noreferrer" href="https://www.continence.org.au/about-incontinence/bowel-incontinence/bristol-stool-chart/">Continence Health Australia</a></p><p><a target="_blank" rel="noreferrer" href="https://www.nhs.uk/conditions/bleeding-from-the-bottom-rectal-bleeding/">NHS rectal bleeding guidance</a></p><p><a target="_blank" rel="noreferrer" href="https://www.niddk.nih.gov/health-information/digestive-diseases/constipation/symptoms-causes">NIDDK constipation guidance</a></p></section><section class="section card danger-zone"><h2>Delete everything</h2><p class="fine">Permanently removes Timmy’s local ledger and every saved photo from this browser.</p><button class="btn btn-danger" id="delete-all">Delete all local data</button></section></main>`);
|
||||
document.querySelector('#export').onclick=exportData;document.querySelector('#import').onchange=importData;document.querySelector('#delete-all').onclick=deleteData;
|
||||
}
|
||||
function exportData(){const blob=new Blob([exportLedger(entries)],{type:'application/json'}),a=document.createElement('a');a.href=URL.createObjectURL(blob);a.download='timmy-ledger.json';a.click();URL.revokeObjectURL(a.href);toast('Export created');}
|
||||
async function importData(e){try{const text=await e.target.files[0].text();entries=importLedger(text);saveEntries();render();toast('Ledger imported')}catch(err){toast(err.message)}}
|
||||
function deleteData(){if(confirm('Delete every local Timmy entry and photo? This cannot be undone.')){entries=[];localStorage.removeItem(STORE);localStorage.removeItem(LEGACY_STORE);render();toast('Local ledger and photos deleted')}}
|
||||
function exportData(){const blob=new Blob([exportLedgerBackup({entries,contributions})],{type:'application/json'}),a=document.createElement('a');a.href=URL.createObjectURL(blob);a.download=`timmy-backup-v${LEDGER_EXPORT_VERSION}.json`;a.click();URL.revokeObjectURL(a.href);portabilityNotice={kind:'ok',version:LEDGER_EXPORT_VERSION,text:`Backup written as version ${LEDGER_EXPORT_VERSION}: ${entries.length} log${entries.length===1?'':'s'} and ${contributions.length} training receipt${contributions.length===1?'':'s'}.`};render();toast('Backup exported');}
|
||||
async function importData(e){const file=e.target.files?.[0];if(!file)return;try{const result=importLedgerBackup(await file.text());entries=result.entries;contributions=result.contributions;saveEntries();saveContributions();portabilityNotice={kind:'ok',version:result.fromVersion,text:[`Restored ${result.entries.length} log${result.entries.length===1?'':'s'} and ${result.contributions.length} training receipt${result.contributions.length===1?'':'s'} from a version ${result.fromVersion} backup.`,...result.notices].join(' ')};e.target.value='';render();toast('Backup imported')}catch(err){portabilityNotice={kind:'error',text:`${err.message||'That backup could not be read.'} Your ledger is unchanged.`};e.target.value='';render();toast('Nothing was imported')}}
|
||||
function portabilityNoticeBlock(){if(!portabilityNotice)return '';if(portabilityNotice.kind==='error')return `<p class="portability-notice error" role="alert" data-portability-error>${esc(portabilityNotice.text)}</p>`;return `<p class="portability-notice" role="status" data-portability-notice data-portability-version="${esc(String(portabilityNotice.version))}">${esc(portabilityNotice.text)}</p>`}
|
||||
function deleteData(){if(confirm('Delete every local Timmy entry and photo? This cannot be undone.')){entries=[];contributions=[];portabilityNotice=null;localStorage.removeItem(STORE);localStorage.removeItem(LEGACY_STORE);localStorage.removeItem(TRAINING_STORE);render();toast('Local ledger and photos deleted')}}
|
||||
|
||||
function openPhotoFirst(){beginDraft();photoHint='';resetSuggestion();visionStatus=null;showPhotoFirst('pick');loadVisionStatus()}
|
||||
async function loadVisionStatus(){
|
||||
@@ -308,7 +376,21 @@ function bindStep(step){
|
||||
}
|
||||
async function handlePhoto(e){const file=e.target.files[0];if(!file)return;try{const result=await compressPhoto(file);form.photoDataUrl=result.dataUrl;photoHint=photoQualityMessage(result);showLogStep(2)}catch{photoHint='That image could not be read. Try another photo.';showLogStep(2)}}
|
||||
function compressPhoto(file){return new Promise((resolve,reject)=>{const img=new Image(),url=URL.createObjectURL(file);img.onload=()=>{const scale=Math.min(1,1200/Math.max(img.width,img.height)),canvas=document.createElement('canvas');canvas.width=Math.round(img.width*scale);canvas.height=Math.round(img.height*scale);const ctx=canvas.getContext('2d');ctx.drawImage(img,0,0,canvas.width,canvas.height);const sample=ctx.getImageData(0,0,Math.min(canvas.width,120),Math.min(canvas.height,120)).data;let total=0;for(let i=0;i<sample.length;i+=4)total+=(sample[i]+sample[i+1]+sample[i+2])/3;URL.revokeObjectURL(url);resolve({dataUrl:canvas.toDataURL('image/jpeg',.7),width:img.width,height:img.height,brightness:total/(sample.length/4)/255})};img.onerror=()=>{URL.revokeObjectURL(url);reject()};img.src=url})}
|
||||
function saveLog(){const result=detectUrgentFlags(form.symptoms);const entry=sanitizeEntry({...form});try{entries.push(entry);saveEntries()}catch{entry.photoDataUrl='';entries[entries.length-1]=entry;saveEntries();toast('Log saved, but the photo was too large for browser storage')}clearDraft();document.querySelector('.sheet-backdrop')?.remove();view='home';render();toast(result.urgent?'Saved. Please follow the medical-care alert.':'Private log saved')}
|
||||
// Issue #35 — if this log began as an AI suggestion, the saved record says so. The fields are
|
||||
// limited to what the app actually knows (which visible fields were suggested, whether they were
|
||||
// confirmed or corrected, and which model answered), so a backup cannot be read as a diagnosis or a
|
||||
// broader claim than the visual suggestion the person accepted.
|
||||
function provenanceForSave(){
|
||||
const status=suggestionState?.status;
|
||||
if(status!=='confirmed'&&status!=='corrected')return null;
|
||||
const suggestion=suggestionState.suggestion;
|
||||
if(!suggestion)return null;
|
||||
const provenance={source:status==='confirmed'?'ai-confirmed':'ai-corrected',suggestedBristolType:suggestion.bristolType,suggestedColor:suggestion.color,confirmedAt:new Date().toISOString()};
|
||||
const model=typeof visionStatus?.model==='string'?visionStatus.model.trim():'';
|
||||
if(model)provenance.model=model.slice(0,80);
|
||||
return provenance;
|
||||
}
|
||||
function saveLog(){const result=detectUrgentFlags(form.symptoms);const entry=sanitizeEntry({...form,provenance:provenanceForSave()});try{entries.push(entry);saveEntries()}catch{entry.photoDataUrl='';entries[entries.length-1]=entry;saveEntries();toast('Log saved, but the photo was too large for browser storage')}clearDraft();document.querySelector('.sheet-backdrop')?.remove();view='home';render();toast(result.urgent?'Saved. Please follow the medical-care alert.':'Private log saved')}
|
||||
function render(){({home,calendar,timmy,privacy}[view]||home)()}
|
||||
|
||||
render();
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
# Abstention policy
|
||||
|
||||
Issue #32 asks for raw model scores to be turned into a conservative suggestion policy. This is that
|
||||
policy. It is the only thing that decides whether Timmy offers a visual prefill, and it answers two
|
||||
different questions that are easy to confuse:
|
||||
|
||||
1. **Per photo** — is this individual answer good enough to show?
|
||||
2. **Per release** — has a candidate model earned the right to prefill at all?
|
||||
|
||||
Both are implemented in [`src/abstention-policy.js`](../src/abstention-policy.js). The parser in
|
||||
[`src/analysis.js`](../src/analysis.js) reads its limits from that module rather than carrying its own
|
||||
copy, so a policy change is one edit, not three.
|
||||
|
||||
A suggestion is never a diagnosis, never fills in urgency, discomfort, symptoms, or notes, and is
|
||||
never saved until the user confirms it. Abstention is the default; a prefill is the exception.
|
||||
|
||||
## Why quality is a gate, not a footnote
|
||||
|
||||
`imageQuality` is something the model reports about its own photo. A confident form and colour read
|
||||
from a blurry photo is exactly the answer a user is most likely to accept without thinking, so
|
||||
confidence alone cannot authorise a prefill. The policy accepts `good` and `fair`; `poor` abstains
|
||||
regardless of how confident the reply looked. This is proven end to end by the committed corpus case
|
||||
`poor-quality-confident`, which replays a 0.94-confidence answer about the synthetic motion-blur
|
||||
fixture and expects an abstention.
|
||||
|
||||
## Per-photo gates
|
||||
|
||||
Evaluated in this order. The first gate that fails names the refusal, so the user is told the real
|
||||
reason rather than a generic one.
|
||||
|
||||
| Order | Gate | Refusal (`abstainedFor`) | User-facing copy |
|
||||
|---|---|---|---|
|
||||
| 1 | Model says the photo is not clearly stool | `not_stool` | The image does not clearly show stool. |
|
||||
| 2 | Confidence below `minConfidence` (0.55) | `low_confidence` | The image is too uncertain to prefill safely. |
|
||||
| 3 | Form or colour came back blank | `unusable_visual_fields` | The photo does not show a form Timmy can read safely. |
|
||||
| 4 | `imageQuality` is below the accepted levels | `low_quality` | The photo is too blurry or poorly lit to suggest a form safely. |
|
||||
|
||||
A filled-in field outside the contract (`bristolType` outside 1–7, an unknown `color`, an unknown
|
||||
`imageQuality`) is not an abstention — it **fails closed** with no provider detail shown, because a
|
||||
caller must never be told "the photo was unreadable" when the model actually returned an impossible
|
||||
value. That ordering is covered by the corpus escape cases (`out-of-range-fields`,
|
||||
`blank-plus-out-of-range`).
|
||||
|
||||
Unknown and out-of-distribution images can only be judged from what the schema lets the model say: it
|
||||
must declare `isStool`, a confidence, and a quality. There is no separate OOD channel, and this policy
|
||||
does not invent one. A photo the model cannot place therefore lands on `not_stool`, `low_confidence`,
|
||||
or `low_quality` — all three abstain.
|
||||
|
||||
## Per-release gate
|
||||
|
||||
The calibration harness ([`scripts/calibration_report.py`](../scripts/calibration_report.py), issue
|
||||
#29) scores candidates on a held-out test split with a threshold selected on validation only. The
|
||||
policy turns that report into a single decision with `evaluatePrefillGate(report)`:
|
||||
|
||||
| Requirement | Blocked reason if unmet |
|
||||
|---|---|
|
||||
| Report verdict is `GO` | `calibration-verdict-not-go` |
|
||||
| The report names a recommended candidate | `no-recommended-calibration` |
|
||||
| That candidate's threshold was selected on `validation` | `threshold-not-selected-on-validation` |
|
||||
| That candidate was scored on the held-out `test` split | `gate-not-scored-on-held-out-test` |
|
||||
| That candidate passed every release gate | `candidate-failed-release-gates` |
|
||||
|
||||
A missing or unreadable report is a block, never a default allow.
|
||||
|
||||
Run it as a receipt:
|
||||
|
||||
```bash
|
||||
python3 scripts/calibration_report.py \
|
||||
--predictions tests/fixtures/calibration/predictions.jsonl \
|
||||
--min-coverage 0.5 --json /tmp/calibration-report.json
|
||||
|
||||
node scripts/abstention_policy_receipt.mjs --report /tmp/calibration-report.json
|
||||
# exit 0 = prefill authorised, 3 = blocked, 2 = the report could not be read
|
||||
```
|
||||
|
||||
**Current state:** the committed synthetic predictions produce `BLOCKED`, so the receipt exits 3 and
|
||||
names why. No production candidate exists yet — issue #31 (fine-tuned classifier) is the dependency —
|
||||
and the policy exists so that candidate is held to one stated contract instead of whatever it happens
|
||||
to score. The release builder does not gate on this receipt yet; it records the quality gate on camera
|
||||
through the demo instead. Wiring the receipt into `scripts/build_release.py` as a hard prefill gate
|
||||
belongs with #31, when there is a candidate to judge.
|
||||
|
||||
## Versioning
|
||||
|
||||
`ABSTENTION_POLICY.version` is an integer that changes whenever a limit changes. A policy change is a
|
||||
behaviour change: the release notes must be able to say which policy produced a suggestion. The
|
||||
shipped object is frozen so nothing can tighten or loosen it at runtime; callers that need a
|
||||
different policy pass one explicitly (`parseVisionResponse(raw, policy)`), which is how the tests
|
||||
prove the parser has no hidden limits of its own.
|
||||
|
||||
## What this policy does not do
|
||||
|
||||
- It does not decide the Bristol type, the colour, or the quality. It only decides whether to show
|
||||
them.
|
||||
- It does not touch deterministic red-flag escalation, which runs independently of model state.
|
||||
- It does not read, store, or log an image, and it never inspects inference bodies.
|
||||
- It does not set the numeric thresholds from anything but validation data, and it does not convert a
|
||||
`BLOCKED` calibration into a pass.
|
||||
@@ -0,0 +1,61 @@
|
||||
# Corpus manifest contract
|
||||
|
||||
The training corpus is described by an **append-only** `manifest.jsonl`. One line is one event;
|
||||
nothing is ever rewritten in place, so the trail can be audited after a withdrawal. The
|
||||
implementation lives in [`scripts/corpus_manifest.py`](../scripts/corpus_manifest.py) and is
|
||||
driven by [`scripts/ingest_training_photo.py`](../scripts/ingest_training_photo.py).
|
||||
|
||||
## Sample record (`recordType: "sample"`, schema version 2)
|
||||
|
||||
| Field | Meaning |
|
||||
|---|---|
|
||||
| `contributionId` | Opaque id minted by the app's contribution receipt. The same id addresses every derived artifact and every later tombstone. |
|
||||
| `consent.version`, `consent.recordedAt` | The consent version the contributor accepted, and when. Duplicated as `consentVersion` for older readers. |
|
||||
| `source.kind`, `source.inputSha256` | How the bytes arrived (`cli-ingest`) and the digest of the caller's input. The caller's path is never recorded. |
|
||||
| `hashes.rawInput`, `hashes.derived` | Digest of the supplied bytes and of the re-encoded image that is actually stored. |
|
||||
| `derivation` | `format`, `maxDimension`, `metadataStripped`, and the `pipeline` version that produced the derived image. |
|
||||
| `review.status`, `review.reviewedAt` | `user-confirmed` or `clinician-reviewed`. A label set is never unlabelled-by-default. |
|
||||
| `split` | `train`, `validation`, or `test`, derived from the salted subject key so one contributor never straddles splits. |
|
||||
| `subjectKey` | 16-hex pseudonym derived from a salted subject id. Never a name, email, or account. |
|
||||
| `lifecycle.state`, `lifecycle.updatedAt` | `active` while the sample is usable for training. |
|
||||
| `imageId`, `relativePath` | Derived image digest and its dataset-relative path. |
|
||||
|
||||
## Tombstone record (`recordType: "tombstone"`)
|
||||
|
||||
A withdrawal never deletes history; it appends a tombstone that:
|
||||
|
||||
- keeps `contributionId` and `imageId` so the withdrawal can be matched to the original sample,
|
||||
- records `lifecycle.state: "revoked"`, the timestamp, and the reason,
|
||||
- lists `deletedArtifacts` — every derived copy it removed, addressed by contribution id with the
|
||||
digest that identifies the bytes,
|
||||
- deliberately drops `labels`, `subjectKey`, and `relativePath`: a deletion receipt keeps
|
||||
identifiers and hashes, not the health label or the person.
|
||||
|
||||
`revoke()` removes the derived image and any raw pre-derivation copy from the active dataset
|
||||
before appending the tombstone, and refuses any path that resolves outside the dataset root.
|
||||
|
||||
## Lifecycle rules (all covered by `tests/corpus-manifest.test.py`)
|
||||
|
||||
- `active` → `revoked` only, and only with a recorded reason.
|
||||
- `revoked` → `active` is refused: a revoked contribution cannot return to the active dataset.
|
||||
- Revoking an unknown contribution, or revoking twice, is refused and appends nothing.
|
||||
- A contribution id can be ingested once; the same derived image cannot carry two label sets.
|
||||
- `active_records()` returns only samples that are still `active` and were never tombstoned, so a
|
||||
trainer that reads through it cannot silently train on withdrawn data.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
export TIMMY_DATASET_SALT=... # >= 8 chars, never committed
|
||||
python3 scripts/ingest_training_photo.py \
|
||||
--input photo.jpg --output-dir /srv/timmy-corpus \
|
||||
--subject-id subject-123 --consent-version research-v1 \
|
||||
--contribution-id "$CONTRIBUTION_ID" \
|
||||
--bristol-type 4 --color brown --quality good --reviewer user-confirmed
|
||||
|
||||
python3 scripts/ingest_training_photo.py --revoke "$CONTRIBUTION_ID" \
|
||||
--output-dir /srv/timmy-corpus --reason "subject withdrawal"
|
||||
```
|
||||
|
||||
`npm run test:corpus` runs the contract suite. Every fixture is synthetic; no real medical image
|
||||
is read, written, or committed.
|
||||
@@ -0,0 +1,117 @@
|
||||
# Evaluation harness contract
|
||||
|
||||
Issue #29 asks for the authoritative comparison and release-gate harness: macro-F1, per-class
|
||||
precision/recall, a confusion matrix, calibration error, coverage, and precision-at-coverage, over
|
||||
splits that are contributor-held-out and reproducible, comparing candidate models.
|
||||
|
||||
The implementation is [`scripts/calibration_report.py`](../scripts/calibration_report.py). It reads
|
||||
predictions something else produced, and it refuses to compute gates it cannot honestly compute.
|
||||
|
||||
The GO/BLOCKED verdict this report produces is consumed by the production abstention policy in
|
||||
[`docs/ABSTENTION-POLICY.md`](ABSTENTION-POLICY.md): a candidate that has not cleared these gates
|
||||
cannot authorise a visual prefill.
|
||||
|
||||
**This harness does not train, does not diagnose, and never reads an image.** It only sees Bristol
|
||||
labels, predictions, and confidences.
|
||||
|
||||
## Input
|
||||
|
||||
One prediction record per line (JSONL), one file or many:
|
||||
|
||||
```json
|
||||
{"model":"specialist-candidate","subjectKey":"cd1a2b3c4d5e6f01","split":"validation",
|
||||
"truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.97}}
|
||||
```
|
||||
|
||||
| Field | Rule |
|
||||
|---|---|
|
||||
| `model` | Non-empty. Candidates are grouped by this value, so one file may carry several. |
|
||||
| `subjectKey` | The 16-hex pseudonymous key from the corpus manifest. Never a name, email, or account. |
|
||||
| `split` | `train`, `validation`, or `test`. |
|
||||
| `truth.bristolType` | Whole number 1–7. |
|
||||
| `prediction` | `{bristolType: 1–7, confidence: 0–1}`, or `null` when the candidate offered nothing. |
|
||||
|
||||
A record that breaks any row above is refused (exit 2) rather than scored.
|
||||
|
||||
## Splits are contributor-held-out and reproducible
|
||||
|
||||
The split rule is the ingestion rule from `scripts/ingest_training_photo.py`: the first byte of the
|
||||
subject key picks the bucket — below `205` is `train`, below `230` is `validation`, otherwise `test`.
|
||||
Because the rule reads only the pseudonymous key, a split can be re-derived by anyone holding the
|
||||
manifest, and a corpus cannot quietly re-label itself.
|
||||
|
||||
`assert_contributor_held_out()` refuses:
|
||||
|
||||
- a `split` that does not match the one the subject key derives (not reproducible), and
|
||||
- one subject key appearing in more than one split (contributor leak) — a held-out score where the
|
||||
model has already seen that person is not held out.
|
||||
|
||||
Several photos from one contributor inside one split are fine; that is the normal case.
|
||||
|
||||
## Metrics
|
||||
|
||||
The harness is a *selective* classifier evaluation. A suggestion is **accepted** when it exists and
|
||||
its confidence is at or above the threshold; everything else is an abstention. Two abstention
|
||||
reasons are counted separately: `no-suggestion` and `low-confidence`.
|
||||
|
||||
| Metric | Definition |
|
||||
|---|---|
|
||||
| `coverage` | accepted / total in the scored split. |
|
||||
| `precisionAtCoverage` | correct / accepted. The gate metric. |
|
||||
| `perClass[label]` | `precision`, `recall`, `f1`, `support`, `predicted` over accepted suggestions. |
|
||||
| `macroF1` | Mean of `perClass[*].f1` over the classes present in **accepted** truth. A class whose every photo abstained cannot be scored, so it is named in `excludedClasses` instead of being counted as a silent zero. |
|
||||
| `confusion` | `"truth->predicted": count` over accepted suggestions. |
|
||||
| `ece` | Expected calibration error over accepted suggestions in ten equal-width confidence bins, weighted by each bin's share. |
|
||||
|
||||
Calibration is measured on the same accepted set as precision, so a candidate cannot look calibrated
|
||||
by abstaining out of its worst bins.
|
||||
|
||||
## Threshold protocol
|
||||
|
||||
`select_threshold()` takes the coverage floor and returns the threshold with the highest
|
||||
precision-at-coverage among the cuts that still clear that floor, breaking ties toward wider coverage.
|
||||
It reads the **validation split only** and raises otherwise: choosing a threshold on the data the gate
|
||||
is measured on manufactures whatever precision the report needs.
|
||||
|
||||
`compare()` therefore fixes one threshold per candidate on validation, scores that candidate on test,
|
||||
and never looks back at validation scores.
|
||||
|
||||
## Gates
|
||||
|
||||
Taken from the ROADMAP release gates. All limits are inclusive — a candidate exactly on the line passes.
|
||||
|
||||
| Gate | Limit |
|
||||
|---|---|
|
||||
| `macroF1` | ≥ 0.80 |
|
||||
| `precisionAtCoverage` | ≥ 0.90 |
|
||||
| `ece` | ≤ 0.05 |
|
||||
|
||||
A candidate must clear all three on its held-out split. `verdict` is `GO` only when at least one
|
||||
candidate does; `recommended` is the passing candidate with the highest macro-F1 (then precision).
|
||||
Otherwise the verdict is `BLOCKED` and no candidate is recommended.
|
||||
|
||||
## Run it
|
||||
|
||||
```bash
|
||||
python3 scripts/calibration_report.py \
|
||||
--predictions tests/fixtures/calibration/predictions.jsonl \
|
||||
--min-coverage 0.5 --json /tmp/calibration-report.json
|
||||
|
||||
npm run test:calibration # the harness contract suite
|
||||
```
|
||||
|
||||
| Exit code | Meaning |
|
||||
|---|---|
|
||||
| 0 | `GO` — a candidate cleared every gate. |
|
||||
| 3 | `BLOCKED` — the release gate refuses every candidate. |
|
||||
| 2 | Refused — malformed, leaky, or unreproducible input. Nothing was scored or written. |
|
||||
|
||||
`--json PATH` writes the full report (per-class rows, confusion matrix, calibration bins, gate detail).
|
||||
The report is only written after a successful run.
|
||||
|
||||
## Fixtures
|
||||
|
||||
[`tests/fixtures/calibration/`](../tests/fixtures/calibration/README.md) holds synthetic predictions
|
||||
that exercise `BLOCKED`, `GO`, and the leaked-corpus refusal. They are invented numbers on fabricated
|
||||
subject keys: **no model produced them and they say nothing about the real classifier.** Recording a
|
||||
video or writing a report from them means saying so.
|
||||
@@ -34,6 +34,14 @@ The application’s deterministic urgent-symptom guidance runs before chat and r
|
||||
|
||||
Logging must never depend on AI availability. When the vision worker is offline, disabled, or still unknown, the photo-first flow offers no AI action and no analysis-consent prompt, and manual Bristol entry remains one action away from every photo step. A chosen photo and any entered context are retained as an in-session draft with a visible summary and an explicit start over; drafts are deliberately not persisted to storage, so a photo awaiting consent is never written to disk. The service worker never caches analysis requests or image bodies.
|
||||
|
||||
### Training contribution consent
|
||||
|
||||
Contributing a confirmed photo to training is a second consent, not a side effect of logging. A journal save is never a training contribution; a contribution exists only after the person reads the current consent version and its receipt (contribution id, what is included, what is never included) and accepts it explicitly. The receipt carries the consent version it was accepted under, the confirmed Bristol form and broad color, and no note text, symptom answers, urgency/discomfort ratings, identity, or photo bytes.
|
||||
|
||||
Withdrawal is first-class: withdrawal keeps a tombstone naming every derived artifact by contribution id (raw image and confirmed label), so a deletion request can be addressed against the corpus rather than trusted to a promise. Delete-all clears the ledger, saved photos, and contribution receipts together.
|
||||
|
||||
This build acknowledges that records the intent and does not transmit a contribution until the corpus pipeline and privacy copy are reviewed in #22 and #25; the section says so in the app rather than implying an upload that does not exist.
|
||||
|
||||
### Release authority
|
||||
|
||||
Hermes/Timmy is the release authority for routine engineering, test, build, deployment, and release-candidate preparation. Automated evidence must remain reproducible and must not overstate model accuracy or safety.
|
||||
@@ -48,6 +56,16 @@ Human gates are limited to:
|
||||
|
||||
Routine provisioning, deployment, label sweeps, and release-candidate assembly are not human-operated gates.
|
||||
|
||||
### Backup portability and entry provenance
|
||||
|
||||
A backup is the person's own record and must keep working as the AI fields evolve.
|
||||
|
||||
- Every export stamps a schema version (currently version 2). The same file carries the confirmed logs and the training contribution receipts, including withdrawn tombstones.
|
||||
- An older backup is upgraded on import, and the app says which version the file came from. Fields that did not exist when the file was written are left empty rather than invented.
|
||||
- A backup from a newer version is refused with an instruction to update Timmy, and a refused import leaves the ledger unchanged.
|
||||
- Malformed, oversized, and over-limit files fail before anything is replaced.
|
||||
- Provenance is stored on the confirmed log when a saved entry began as a confirmed or corrected AI suggestion: source, suggested visible fields, the model that answered, and when it was confirmed. It never records a diagnosis, a symptom, or provider credentials, and backups are written through an allowlist so unknown keys never leave the device.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Model output is assistive and fail-closed, never authoritative.
|
||||
|
||||
+8
-3
@@ -4,7 +4,7 @@
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "node --test tests/domain.test.js tests/analysis.test.js tests/vision-service.test.js tests/vision-config.test.js tests/draft.test.js tests/suggestion.test.js tests/followup.test.js tests/vision-readiness.test.js tests/hermes-agent-service.test.js tests/agent-gateway.acceptance.test.js tests/staging-health.test.js tests/service-worker-runtime.test.js tests/training-ingest.test.js tests/ci-workflow.test.js tests/product-decisions.test.js tests/release-demo.test.js tests/selfhost-bootstrap.test.js tests/staging-config.test.js tests/threat-model.test.js tests/vision-corpus.test.js tests/vision-boundary.test.js tests/worker-benchmark.test.js tests/worker-ready.test.js tests/worker-container.test.js tests/summary.test.js tests/escalation-regression.test.js tests/grounded-chat.test.js tests/photo-lifecycle.test.js",
|
||||
"test": "node --test tests/domain.test.js tests/analysis.test.js tests/vision-service.test.js tests/vision-config.test.js tests/draft.test.js tests/suggestion.test.js tests/followup.test.js tests/vision-readiness.test.js tests/hermes-agent-service.test.js tests/agent-gateway.acceptance.test.js tests/staging-health.test.js tests/service-worker-runtime.test.js tests/training-ingest.test.js tests/ci-workflow.test.js tests/product-decisions.test.js tests/release-demo.test.js tests/selfhost-bootstrap.test.js tests/staging-config.test.js tests/threat-model.test.js tests/vision-corpus.test.js tests/vision-boundary.test.js tests/worker-benchmark.test.js tests/worker-ready.test.js tests/worker-container.test.js tests/summary.test.js tests/escalation-regression.test.js tests/grounded-chat.test.js tests/photo-lifecycle.test.js tests/training-contribution.test.js tests/portability.test.js tests/abstention-policy.test.js",
|
||||
"test:ui": "node tests/ui.acceptance.mjs",
|
||||
"test:photo": "node tests/photo-first.acceptance.mjs",
|
||||
"test:mobile-capture": "node tests/mobile-capture.acceptance.mjs",
|
||||
@@ -17,10 +17,15 @@
|
||||
"test:pattern-summary": "node tests/pattern-summary.acceptance.mjs",
|
||||
"test:grounded-chat": "node tests/grounded-chat.acceptance.mjs",
|
||||
"test:photo-lifecycle": "node tests/photo-lifecycle.acceptance.mjs",
|
||||
"check:syntax": "node --check app.js && node --check server.mjs && node --check service-worker.js && node --check src/analysis.js && node --check src/domain.js && node --check src/draft.js && node --check src/suggestion.js && node --check src/followup.js && node --check src/vision-readiness.js && node --check src/hermes-agent-service.js && node --check src/vision-config.js && node --check src/vision-service.js && node --check src/vision-corpus.js && node --check src/summary.js && node --check src/photo-lifecycle.js && node --check src/grounded-chat.js && node --check scripts/record_release_demo.mjs && node --check tests/staging.acceptance.mjs && node --check tests/manual-fallback.acceptance.mjs && node --check tests/provisional-suggestion.acceptance.mjs && node --check tests/followup.acceptance.mjs && node --check tests/vision-refusal.acceptance.mjs && bash -n scripts/bootstrap_selfhost_smolvlm.sh && bash -n scripts/run_selfhost_smolvlm.sh && python3 -m py_compile scripts/ingest_training_photo.py scripts/build_release.py scripts/deploy_staging.py scripts/build_vision_corpus.py && node --check scripts/record_worker_benchmark.mjs && node --check tests/worker-readiness.acceptance.mjs && node --check tests/pattern-summary.acceptance.mjs && node --check tests/photo-lifecycle.acceptance.mjs && node --check tests/grounded-chat.acceptance.mjs && bash -n scripts/worker_ready.sh && bash -n deploy/worker/entrypoint.sh && bash -n deploy/worker/build_image.sh && bash -n deploy/worker/run_worker_container.sh",
|
||||
"test:training-contribution": "node tests/training-contribution.acceptance.mjs",
|
||||
"check:syntax": "node --check app.js && node --check server.mjs && node --check service-worker.js && node --check src/analysis.js && node --check src/domain.js && node --check src/draft.js && node --check src/suggestion.js && node --check src/followup.js && node --check src/vision-readiness.js && node --check src/hermes-agent-service.js && node --check src/vision-config.js && node --check src/vision-service.js && node --check src/vision-corpus.js && node --check src/summary.js && node --check src/photo-lifecycle.js && node --check src/grounded-chat.js && node --check src/training-contribution.js && node --check src/abstention-policy.js && node --check scripts/record_release_demo.mjs && node --check tests/staging.acceptance.mjs && node --check tests/manual-fallback.acceptance.mjs && node --check tests/provisional-suggestion.acceptance.mjs && node --check tests/followup.acceptance.mjs && node --check tests/vision-refusal.acceptance.mjs && bash -n scripts/bootstrap_selfhost_smolvlm.sh && bash -n scripts/run_selfhost_smolvlm.sh && python3 -m py_compile scripts/corpus_manifest.py scripts/ingest_training_photo.py scripts/build_release.py scripts/deploy_staging.py scripts/build_vision_corpus.py scripts/calibration_report.py && node --check scripts/record_worker_benchmark.mjs && node --check scripts/abstention_policy_receipt.mjs && node --check tests/worker-readiness.acceptance.mjs && node --check tests/pattern-summary.acceptance.mjs && node --check tests/photo-lifecycle.acceptance.mjs && node --check tests/training-contribution.acceptance.mjs && node --check tests/portability.acceptance.mjs && node --check tests/grounded-chat.acceptance.mjs && bash -n scripts/worker_ready.sh && bash -n deploy/worker/entrypoint.sh && bash -n deploy/worker/build_image.sh && bash -n deploy/worker/run_worker_container.sh",
|
||||
"check:diff": "bash scripts/check_diff.sh",
|
||||
"start": "node server.mjs",
|
||||
"test:worker-readiness": "node tests/worker-readiness.acceptance.mjs"
|
||||
"test:worker-readiness": "node tests/worker-readiness.acceptance.mjs",
|
||||
"test:portability": "node tests/portability.acceptance.mjs",
|
||||
"test:corpus": "python3 tests/corpus-manifest.test.py",
|
||||
"test:calibration": "python3 tests/calibration-harness.test.py",
|
||||
"test:policy": "node --test tests/abstention-policy.test.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"playwright": "^1.62.1"
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env node
|
||||
// Executable receipt for the production abstention policy (issue #32).
|
||||
//
|
||||
// The calibration harness decides GO/BLOCKED for a candidate model. This reads that report and
|
||||
// answers the only question the app cares about: may a suggestion be shipped at all? It exits 3 when
|
||||
// prefill is blocked, so a release step can gate on it instead of reading prose.
|
||||
//
|
||||
// There is no model candidate yet (#31), so in practice this receipt reports BLOCKED today. That is
|
||||
// the honest state: the policy is ready, the evidence is not.
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { ABSTENTION_POLICY, evaluatePrefillGate } from '../src/abstention-policy.js';
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { report: '' };
|
||||
for (let i = 0; i < argv.length; i += 1) {
|
||||
if (argv[i] === '--report') {
|
||||
args.report = argv[i + 1] || '';
|
||||
i += 1;
|
||||
} else throw new Error(`unknown argument: ${argv[i]}`);
|
||||
}
|
||||
if (!args.report) throw new Error('--report <calibration-report.json> is required');
|
||||
return args;
|
||||
}
|
||||
|
||||
let args;
|
||||
try {
|
||||
args = parseArgs(process.argv.slice(2));
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
let report;
|
||||
try {
|
||||
report = JSON.parse(await readFile(args.report, 'utf8'));
|
||||
} catch (error) {
|
||||
console.error(`could not read the calibration report at ${args.report}: ${error.message}`);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const gate = evaluatePrefillGate(report);
|
||||
const policy = ABSTENTION_POLICY;
|
||||
console.log('TIMMY ABSTENTION POLICY RECEIPT');
|
||||
console.log(`policy: version ${policy.version} · confidence ≥ ${policy.minConfidence.toFixed(2)} · quality ${policy.acceptedImageQuality.join('|')}`);
|
||||
console.log(`calibration: verdict ${gate.verdict} · model ${gate.calibrationModel || 'none'}`);
|
||||
console.log(gate.prefillAllowed
|
||||
? `prefill: AUTHORISED for ${gate.calibrationModel}`
|
||||
: `prefill: BLOCKED — ${gate.blockedBy.join(', ')}`);
|
||||
process.exit(gate.prefillAllowed ? 0 : 3);
|
||||
@@ -9,6 +9,7 @@ import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tarfile
|
||||
@@ -45,12 +46,28 @@ DEMO_REQUIRED_CAPTIONS = (
|
||||
"A tarry note already saved in the journal escalates the same way, before any provider call",
|
||||
"An out-of-range answer fails closed: nothing is applied and no provider detail reaches you",
|
||||
"A filled-in field that is out of range still fails closed, even when the other field came back blank",
|
||||
"A confident reply about a blurry photo still abstains — photo quality is part of the policy, not just confidence",
|
||||
"Reachable is not loaded: no AI control, no consent, and no upload until the pinned model is resident",
|
||||
"Every pattern line names the confirmed logs behind it, and following one shows exactly those records",
|
||||
"Two logs is not a pattern: the summary refuses to describe a trend it cannot support",
|
||||
"The answer states the count and cites the four confirmed logs it counted",
|
||||
"Following a citation opens exactly the confirmed log behind that answer",
|
||||
"Ask something the journal cannot answer and Timmy refuses instead of reassuring",
|
||||
"Training contribution is off by default: a saved log is never training data",
|
||||
"The consent version and the receipt are on screen before the contribute button works",
|
||||
"Withdrawing keeps a tombstone naming every derived artifact by contribution ID",
|
||||
"The corpus pipeline is a command-line tool, not a screen in the app — this runs the real ingest against the committed synthetic fixture",
|
||||
"Every ingested sample records consent, source digest, both hashes, derivation, review, and lifecycle — the caller file path is never stored",
|
||||
"Withdrawing deletes the derived image and any raw copy, keeps the original record, and appends a tombstone — the trainer sees zero active samples",
|
||||
"Revoking the same contribution twice is refused and appends nothing: the manifest only ever grows",
|
||||
"The calibration harness is a command-line release gate, not a screen in the app — this runs it against committed synthetic predictions",
|
||||
"Every score on this card is invented to exercise the arithmetic: no model produced them and none of them describes the real classifier",
|
||||
"Each candidate is scored on test at a threshold chosen on validation only; picking a threshold on the split you report is refused",
|
||||
"One contributor spread across two splits is refused before anything is scored, and a passing verdict is proven only by a fixture engineered to clear it",
|
||||
"A backup is versioned, and it carries the withdrawn receipt as a tombstone rather than silence",
|
||||
"An older backup is upgraded on import, and the notice says which version the file came from",
|
||||
"A file from a newer Timmy is refused with an instruction, and the ledger it would have replaced is untouched",
|
||||
"A restored log still says it began as an AI suggestion you corrected — provenance is part of the record",
|
||||
)
|
||||
|
||||
|
||||
@@ -69,6 +86,29 @@ def sha256(path: Path) -> str:
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def assert_port_free(host: str, port: int) -> None:
|
||||
"""Fail closed when something is already listening where the acceptance server must bind."""
|
||||
with socket.socket() as sock:
|
||||
sock.settimeout(1.0)
|
||||
if sock.connect_ex((host, port)) == 0:
|
||||
raise SystemExit(
|
||||
f"Port {port} is already served by another process; refusing to gate or record this "
|
||||
"release against a tree that is not the one under test."
|
||||
)
|
||||
|
||||
|
||||
def assert_served_tree(tree: Path, host: str, port: int) -> None:
|
||||
"""Prove the responding server is the clone under test, not a stale checkout on the same port."""
|
||||
for relative in ("src/domain.js", "app.js"):
|
||||
served = urllib.request.urlopen(f"http://{host}:{port}/{relative}", timeout=5).read()
|
||||
expected = (tree / relative).read_bytes()
|
||||
if hashlib.sha256(served).hexdigest() != hashlib.sha256(expected).hexdigest():
|
||||
raise SystemExit(
|
||||
f"The acceptance server is serving {relative} from a different tree; "
|
||||
"every gate and the demo would describe a release that is not this commit."
|
||||
)
|
||||
|
||||
|
||||
def tracked_files(tree: Path) -> list[str]:
|
||||
names = run(["git", "ls-files"], tree, capture=True).splitlines()
|
||||
return sorted(name for name in names if name and not name.startswith(EXCLUDED_PREFIXES)
|
||||
@@ -124,8 +164,17 @@ def main() -> int:
|
||||
run(["npm", "test"], tree)
|
||||
# The committed corpus hashes are an integrity receipt: verify them before anything is built.
|
||||
run(["python3", "scripts/build_vision_corpus.py", "--verify"], tree)
|
||||
# Issue #25's corpus pipeline is a CLI, so its own suite is a release gate, not just CI.
|
||||
run(["python3", "tests/corpus-manifest.test.py", "-v"], tree)
|
||||
# Issue #29's evaluation harness decides GO/BLOCKED for a model, so its refusal and gate
|
||||
# arithmetic are exercised before this release is allowed to exist.
|
||||
run(["python3", "tests/calibration-harness.test.py", "-v"], tree)
|
||||
server_env = dict(os.environ)
|
||||
server_env.update({"HOST": "127.0.0.1", "PORT": "4173", "TIMMY_VISION_ENABLED": "false"})
|
||||
# A leftover server from another checkout answers on this port and the readiness probe cannot
|
||||
# tell it apart from ours, so every gate and the recorded demo would silently describe a
|
||||
# different tree. Refuse to build over a live listener, and verify the served bytes afterwards.
|
||||
assert_port_free("127.0.0.1", 4173)
|
||||
server_log = (release_dir / "acceptance-server.log").open("w", encoding="utf-8")
|
||||
server = subprocess.Popen(["node", "server.mjs"], cwd=tree, env=server_env,
|
||||
stdout=server_log, stderr=subprocess.STDOUT, text=True)
|
||||
@@ -140,6 +189,9 @@ def main() -> int:
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
raise SystemExit("Acceptance server did not become ready")
|
||||
if server.poll() is not None:
|
||||
raise SystemExit("Acceptance server exited after reporting ready")
|
||||
assert_served_tree(tree, "127.0.0.1", 4173)
|
||||
run(["npm", "run", "test:ui"], tree)
|
||||
run(["npm", "run", "test:photo"], tree)
|
||||
run(["npm", "run", "test:mobile-capture"], tree)
|
||||
@@ -152,6 +204,8 @@ def main() -> int:
|
||||
run(["npm", "run", "test:pattern-summary"], tree)
|
||||
run(["npm", "run", "test:grounded-chat"], tree)
|
||||
run(["npm", "run", "test:photo-lifecycle"], tree)
|
||||
run(["npm", "run", "test:training-contribution"], tree)
|
||||
run(["npm", "run", "test:portability"], tree)
|
||||
demo_raw = release_dir / f"timmy-talking-turd-{version}-demo.raw.webm"
|
||||
caption_receipt = release_dir / f"timmy-talking-turd-{version}-demo-captions.json"
|
||||
demo_env = dict(server_env)
|
||||
@@ -199,7 +253,7 @@ def main() -> int:
|
||||
if not contact_sheet.exists() or contact_sheet.stat().st_size == 0:
|
||||
raise SystemExit("Release demo contact sheet was not produced")
|
||||
run(["npm", "audit", "--audit-level=high"], tree)
|
||||
for file in ("app.js", "server.mjs", "service-worker.js", "src/analysis.js", "src/domain.js", "src/draft.js", "src/suggestion.js", "src/followup.js", "src/vision-readiness.js", "src/hermes-agent-service.js", "src/vision-config.js", "src/vision-service.js", "src/vision-corpus.js", "src/summary.js", "src/photo-lifecycle.js", "scripts/record_worker_benchmark.mjs", "tests/staging.acceptance.mjs", "tests/vision-refusal.acceptance.mjs", "tests/worker-readiness.acceptance.mjs", "tests/pattern-summary.acceptance.mjs", "tests/photo-lifecycle.acceptance.mjs"):
|
||||
for file in ("app.js", "server.mjs", "service-worker.js", "src/analysis.js", "src/domain.js", "src/draft.js", "src/suggestion.js", "src/followup.js", "src/vision-readiness.js", "src/hermes-agent-service.js", "src/vision-config.js", "src/vision-service.js", "src/vision-corpus.js", "src/summary.js", "src/photo-lifecycle.js", "src/training-contribution.js", "scripts/record_worker_benchmark.mjs", "tests/staging.acceptance.mjs", "tests/vision-refusal.acceptance.mjs", "tests/worker-readiness.acceptance.mjs", "tests/pattern-summary.acceptance.mjs", "tests/photo-lifecycle.acceptance.mjs", "tests/training-contribution.acceptance.mjs"):
|
||||
run(["node", "--check", file], tree)
|
||||
run(["bash", "-n", "scripts/bootstrap_selfhost_smolvlm.sh"], tree)
|
||||
run(["bash", "-n", "scripts/run_selfhost_smolvlm.sh"], tree)
|
||||
@@ -207,7 +261,7 @@ def main() -> int:
|
||||
run(["bash", "-n", "deploy/worker/entrypoint.sh"], tree)
|
||||
run(["bash", "-n", "deploy/worker/build_image.sh"], tree)
|
||||
run(["bash", "-n", "deploy/worker/run_worker_container.sh"], tree)
|
||||
run(["python3", "-m", "py_compile", "scripts/ingest_training_photo.py", "scripts/build_release.py", "scripts/build_vision_corpus.py"], tree)
|
||||
run(["python3", "-m", "py_compile", "scripts/ingest_training_photo.py", "scripts/build_release.py", "scripts/build_vision_corpus.py", "scripts/corpus_manifest.py"], tree)
|
||||
run(["git", "diff", "--check", commit], tree)
|
||||
|
||||
names = tracked_files(tree)
|
||||
@@ -240,7 +294,9 @@ def main() -> int:
|
||||
"- Target-GPU benchmark: scripts/record_worker_benchmark.mjs records cold start, warm latency, and VRAM on the target GPU and refuses to write a receipt it did not measure; no GPU measurement is bundled with this release\n"
|
||||
"- Explainable pattern summaries (issue #34): the home insight card reports confirmed-log counts and form groupings per window, every line names the exact confirmed records behind it, following a citation opens those records, and a window below the minimum says it is too sparse instead of describing a trend; no line explains a cause, rules on food, or offers a health verdict\n"
|
||||
"- Private photo lifecycle (issue #36): the journal marks a log that holds a saved photo without rendering a thumbnail, showing the image is an explicit request, removing it keeps every confirmed field, deleting a log is a separate two-step decision that takes its photo, and delete-all removes the ledger and every stored photo together\n"
|
||||
"- Safety: suggestions are observable-field assistance, never diagnosis; user confirmation is required\n"
|
||||
"- Training contribution is a separate opt-in (issue #24): a journal save records no contribution, the consent version and the receipt are on screen before the control can be pressed, the receipt is queued on this device only (this build transmits nothing) and carries no note text, symptom answers, or photo bytes, and a withdrawal keeps a tombstone naming every derived artifact by contribution id\n"
|
||||
"- Auditable corpus pipeline (issue #25): every ingested training sample now records its consent version and timestamp, source kind and the raw-input digest (the caller's file path is never stored), both the raw and derived hashes, the derivation pipeline, the human review status, the split, and its lifecycle state in an append-only manifest; withdrawing a contribution deletes the derived image and any raw pre-derivation copy from the active dataset, keeps the original record as its own history, and appends a tombstone that names every removed artifact by contribution id and digest while dropping labels and the subject key; illegal lifecycle moves (re-ingesting a contribution, re-labelling a derived image, revoking twice, revoking an unknown contribution, reviving a revoked one, revoking without a reason) are refused before any byte is written\n"
|
||||
"- Scope note: the corpus pipeline is a command-line tool. There is no corpus screen in the app, and this release does not imply one — the recorded demo runs the real ingest and revoke commands against the committed synthetic fixture and shows their actual output\n"
|
||||
"- Known limitation: open-weight VLM acceptance is proven, Bristol accuracy is not clinically validated\n"
|
||||
"- Package excludes model weights, medical-image corpora, raw research pages, generated screenshots, and video production assets\n"
|
||||
)
|
||||
@@ -285,7 +341,7 @@ def main() -> int:
|
||||
"codec": "h264",
|
||||
"pixel_format": "yuv420p",
|
||||
"recorded_from_working_app": True,
|
||||
"fixture_data": "synthetic-Type-4, deterministic vision suggestion reviewed, confirmed, and corrected to Type 2, user-reported hard-stool follow-up with non-diagnostic self-care and a confirmed black/tarry red flag, the CC0 synthetic vision corpus replaying a non-stool refusal, an unreadable-fields abstention, an out-of-range fail-closed reply, and a blank-plus-out-of-range fail-closed reply, offline vision status, fictional multi-day sample logs driving the explainable pattern summary and its citation sheet, a sparse fictional window showing the insufficient-data copy, a grounded local chat answer citing each counted log and opening the cited record, a refusal that borrows no journal evidence, and a deterministic Hermes chat reply whose positional refs render as sources",
|
||||
"fixture_data": "synthetic-Type-4, deterministic vision suggestion reviewed, confirmed, and corrected to Type 2, user-reported hard-stool follow-up with non-diagnostic self-care and a confirmed black/tarry red flag, the CC0 synthetic vision corpus replaying a non-stool refusal, an unreadable-fields abstention, a confident reply about a poor-quality photo abstaining, an out-of-range fail-closed reply, and a blank-plus-out-of-range fail-closed reply, offline vision status, fictional multi-day sample logs driving the explainable pattern summary and its citation sheet, a sparse fictional window showing the insufficient-data copy, a grounded local chat answer citing each counted log and opening the cited record, a refusal that borrows no journal evidence, and a deterministic Hermes chat reply whose positional refs render as sources, plus the opt-in training contribution path where nothing is queued after ordinary saves, the consent version and receipt precede the confirm control, one receipt is queued on the device only, and withdrawal leaves a tombstone naming each derived artifact, plus a versioned backup that carries the logs and the withdrawn receipt, an older version 1 file upgraded with the version named on screen, a file from a newer Timmy refused without changing the ledger, and a restored log still showing it began as a corrected AI suggestion, plus the corpus audit card, where the real ingest and revoke commands run against the committed synthetic fixture and the card shows their actual output: the provenance record with consent, source digest, both hashes, derivation, review and lifecycle; the derived image deleted from disk; the original record kept as history beside a tombstone that drops labels and the subject key; zero active samples for a trainer; and a second revocation refused without appending anything",
|
||||
},
|
||||
"gates": {
|
||||
"unit_security": "passed",
|
||||
@@ -296,10 +352,15 @@ def main() -> int:
|
||||
"provisional_suggestion_state": "passed",
|
||||
"hard_stool_followup": "passed",
|
||||
"vision_corpus_refusal": "passed",
|
||||
"abstention_quality_gate": "passed",
|
||||
"worker_container_receipts": "passed",
|
||||
"worker_post_load_readiness": "passed",
|
||||
"longitudinal_pattern_summary": "passed",
|
||||
"grounded_chat_citations": "passed",
|
||||
"training_contribution_consent": "passed",
|
||||
"corpus_manifest_provenance": "passed",
|
||||
"calibration_harness_gates": "passed",
|
||||
"portable_backup_restore": "passed",
|
||||
"dependency_audit_high": "passed",
|
||||
"syntax": "passed",
|
||||
"secret_scan": "passed",
|
||||
|
||||
@@ -0,0 +1,378 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Contributor-held-out evaluation harness for the Timmy Bristol classifier.
|
||||
|
||||
This is the comparison and release-gate tool for issue #29: it turns candidate predictions into the
|
||||
numbers the ROADMAP gates are written in — macro-F1, per-class precision/recall, a confusion matrix,
|
||||
calibration error, coverage, and precision-at-coverage — and it refuses a corpus whose splits are
|
||||
not contributor-held-out or not reproducible.
|
||||
|
||||
Nothing here trains, scores, or diagnoses: it reads predictions that something else produced, and it
|
||||
reports what those predictions would have done on held-out contributors. Every fixture used to test
|
||||
it is synthetic.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
BRISTOL_CLASSES = tuple(range(1, 8))
|
||||
SPLITS = ("train", "validation", "test")
|
||||
# The split rule is the ingestion rule (scripts/ingest_training_photo.py): the bucket of the first
|
||||
# byte of the pseudonymous subject key. Keeping it here means a split can be re-derived from the
|
||||
# key alone, so a corpus cannot quietly re-label its own splits.
|
||||
_TRAIN_EDGE = 205
|
||||
_VALIDATION_EDGE = 230
|
||||
_SHA_LIKE = re.compile(r"^[0-9a-f]{16}$")
|
||||
|
||||
# The ROADMAP release gates. A candidate must clear every one on its held-out split; a limit is
|
||||
# inclusive, so a model exactly on the line passes.
|
||||
RELEASE_GATES = {"macroF1": 0.80, "precisionAtCoverage": 0.90, "ece": 0.05}
|
||||
|
||||
|
||||
class EvaluationError(Exception):
|
||||
"""The corpus or the caller asked for a comparison that cannot be trusted."""
|
||||
|
||||
|
||||
def expected_split(subject_key: str) -> str:
|
||||
"""The split a pseudonymous subject key must belong to, by the ingestion rule."""
|
||||
if not isinstance(subject_key, str) or not _SHA_LIKE.match(subject_key):
|
||||
raise EvaluationError("subject key must be the 16-character pseudonymous digest")
|
||||
bucket = int(subject_key[:2], 16)
|
||||
if bucket < _TRAIN_EDGE:
|
||||
return "train"
|
||||
if bucket < _VALIDATION_EDGE:
|
||||
return "validation"
|
||||
return "test"
|
||||
|
||||
|
||||
def _calibration_error(accepted: list[tuple[int, int, float]], bins: int = 10) -> tuple[float, list[dict]]:
|
||||
"""Expected calibration error over the accepted suggestions, by equal-width confidence bin."""
|
||||
if not accepted:
|
||||
return 0.0, []
|
||||
edges = [index / bins for index in range(bins + 1)]
|
||||
buckets: list[list[tuple[int, int, float]]] = [[] for _ in range(bins)]
|
||||
for truth, predicted, confidence in accepted:
|
||||
index = min(int(confidence * bins), bins - 1)
|
||||
buckets[index].append((truth, predicted, confidence))
|
||||
error = 0.0
|
||||
detail: list[dict] = []
|
||||
for index, bucket in enumerate(buckets):
|
||||
if not bucket:
|
||||
continue
|
||||
accuracy = sum(1 for truth, predicted, _ in bucket if truth == predicted) / len(bucket)
|
||||
mean_confidence = sum(confidence for _, _, confidence in bucket) / len(bucket)
|
||||
share = len(bucket) / len(accepted)
|
||||
error += share * abs(accuracy - mean_confidence)
|
||||
detail.append(
|
||||
{
|
||||
"lower": edges[index],
|
||||
"upper": edges[index + 1],
|
||||
"count": len(bucket),
|
||||
"accuracy": accuracy,
|
||||
"meanConfidence": mean_confidence,
|
||||
}
|
||||
)
|
||||
return error, detail
|
||||
|
||||
|
||||
def assert_contributor_held_out(records: list[dict]) -> None:
|
||||
"""Refuse a corpus whose splits are not contributor-held-out and reproducible.
|
||||
|
||||
A split must be re-derivable from the pseudonymous subject key alone (the ingestion rule), and
|
||||
one contributor's photos must all land in the same split. Otherwise a held-out score is not
|
||||
held out: the model has already seen that person's body in training.
|
||||
"""
|
||||
splits_by_subject: dict[str, set[str]] = {}
|
||||
for entry in records:
|
||||
subject = entry.get("subjectKey")
|
||||
if not isinstance(subject, str) or not subject:
|
||||
raise EvaluationError("every prediction must carry the pseudonymous subject key")
|
||||
split = entry.get("split")
|
||||
if split not in SPLITS:
|
||||
raise EvaluationError(f"split must be one of {', '.join(SPLITS)}: {split!r}")
|
||||
splits_by_subject.setdefault(subject, set()).add(split)
|
||||
for subject, splits in splits_by_subject.items():
|
||||
if len(splits) > 1:
|
||||
raise EvaluationError(
|
||||
f"contributor {subject} leaks across {', '.join(sorted(splits))}: splits must be contributor-held-out"
|
||||
)
|
||||
for entry in records:
|
||||
subject = entry["subjectKey"]
|
||||
required = expected_split(subject)
|
||||
if entry["split"] != required:
|
||||
raise EvaluationError(
|
||||
f"split {entry['split']!r} for contributor {subject} is not reproducible: the subject key derives {required!r}"
|
||||
)
|
||||
|
||||
|
||||
def compare(models: dict[str, list[dict]], *, min_coverage: float = 0.5, gates: dict | None = None) -> dict:
|
||||
"""Compare candidate models against the release gates.
|
||||
|
||||
Each candidate gets one threshold, chosen on its own validation split, and is then measured on
|
||||
its test split. The gates are the ROADMAP release gates; all limits are inclusive.
|
||||
"""
|
||||
limits = dict(gates or RELEASE_GATES)
|
||||
rows: list[dict] = []
|
||||
for model_id in sorted(models):
|
||||
records = models[model_id]
|
||||
assert_contributor_held_out(records)
|
||||
test = [entry for entry in records if entry.get("split") == "test"]
|
||||
validation = [entry for entry in records if entry.get("split") == "validation"]
|
||||
if not test:
|
||||
raise EvaluationError(f"{model_id} has no test-split predictions to measure a release on")
|
||||
if not validation:
|
||||
raise EvaluationError(f"{model_id} has no validation-split predictions to fix a threshold on")
|
||||
selection = select_threshold(validation, min_coverage=min_coverage)
|
||||
scored = evaluate(test, threshold=selection["threshold"])
|
||||
failed = []
|
||||
if scored["macroF1"] < limits["macroF1"]:
|
||||
failed.append("macroF1")
|
||||
if scored["precisionAtCoverage"] < limits["precisionAtCoverage"]:
|
||||
failed.append("precisionAtCoverage")
|
||||
if scored["ece"] > limits["ece"]:
|
||||
failed.append("ece")
|
||||
rows.append(
|
||||
{
|
||||
"model": model_id,
|
||||
**scored,
|
||||
"selectionSplit": "validation",
|
||||
"evaluatedSplit": "test",
|
||||
"minCoverage": min_coverage,
|
||||
"gate": {"passed": not failed, "failed": failed},
|
||||
}
|
||||
)
|
||||
ordered = sorted(
|
||||
rows,
|
||||
key=lambda row: (
|
||||
not row["gate"]["passed"],
|
||||
-row["macroF1"],
|
||||
-row["precisionAtCoverage"],
|
||||
row["model"],
|
||||
),
|
||||
)
|
||||
passing = [row for row in ordered if row["gate"]["passed"]]
|
||||
return {
|
||||
"gates": limits,
|
||||
"models": ordered,
|
||||
"recommended": passing[0]["model"] if passing else None,
|
||||
"verdict": "GO" if passing else "BLOCKED",
|
||||
}
|
||||
|
||||
|
||||
def select_threshold(records: list[dict], *, min_coverage: float = 0.5) -> dict:
|
||||
"""Choose the abstention threshold that maximises precision at a required coverage.
|
||||
|
||||
Selection reads the validation split only. Fixing a threshold on the same data the release gate
|
||||
is measured on would manufacture whatever precision the report needs.
|
||||
"""
|
||||
if not records:
|
||||
raise EvaluationError("threshold selection needs validation predictions")
|
||||
if not 0.0 <= min_coverage <= 1.0:
|
||||
raise EvaluationError(f"a coverage floor must be between 0 and 1: {min_coverage}")
|
||||
if any(entry.get("split") != "validation" for entry in records):
|
||||
raise EvaluationError("threshold selection must use the validation split only")
|
||||
assert_contributor_held_out(records)
|
||||
|
||||
candidates = {0.0}
|
||||
candidates.update(
|
||||
entry["prediction"]["confidence"] for entry in records if entry["prediction"] is not None
|
||||
)
|
||||
best: dict | None = None
|
||||
for threshold in sorted(candidates):
|
||||
report = evaluate(records, threshold=threshold)
|
||||
# Float comparison: a coverage exactly on the floor must not be lost to rounding.
|
||||
if report["coverage"] + 1e-12 < min_coverage:
|
||||
continue
|
||||
if best is None:
|
||||
best = report
|
||||
continue
|
||||
more_precise = round(report["precisionAtCoverage"], 12) > round(best["precisionAtCoverage"], 12)
|
||||
equally_precise_but_wider = (
|
||||
round(report["precisionAtCoverage"], 12) == round(best["precisionAtCoverage"], 12)
|
||||
and report["coverage"] > best["coverage"]
|
||||
)
|
||||
if more_precise or equally_precise_but_wider:
|
||||
best = report
|
||||
if best is None:
|
||||
raise EvaluationError(f"no threshold reaches the {min_coverage:.0%} coverage floor on validation")
|
||||
return {**best, "selectionSplit": "validation", "minCoverage": min_coverage}
|
||||
|
||||
|
||||
def evaluate(records: list[dict], *, threshold: float = 0.0) -> dict:
|
||||
"""Score one model's accepted suggestions at a fixed abstention threshold."""
|
||||
accepted: list[tuple[int, int, float]] = []
|
||||
abstentions = {"low-confidence": 0, "no-suggestion": 0}
|
||||
for entry in records:
|
||||
truth = entry["truth"]["bristolType"]
|
||||
prediction = entry["prediction"]
|
||||
if prediction is None:
|
||||
abstentions["no-suggestion"] += 1
|
||||
continue
|
||||
if prediction["confidence"] < threshold:
|
||||
abstentions["low-confidence"] += 1
|
||||
continue
|
||||
accepted.append((truth, prediction["bristolType"], prediction["confidence"]))
|
||||
|
||||
confusion: dict[str, int] = {}
|
||||
for truth, predicted, _ in accepted:
|
||||
key = f"{truth}->{predicted}"
|
||||
confusion[key] = confusion.get(key, 0) + 1
|
||||
|
||||
truth_classes = {truth for truth, _, _ in accepted}
|
||||
per_class: dict[int, dict] = {}
|
||||
for label in sorted(truth_classes):
|
||||
true_positive = sum(1 for truth, predicted, _ in accepted if truth == label and predicted == label)
|
||||
predicted_positive = sum(1 for _, predicted, _ in accepted if predicted == label)
|
||||
actual_positive = sum(1 for truth, _, _ in accepted if truth == label)
|
||||
precision = true_positive / predicted_positive if predicted_positive else 0.0
|
||||
recall = true_positive / actual_positive if actual_positive else 0.0
|
||||
f1 = 2 * precision * recall / (precision + recall) if precision + recall else 0.0
|
||||
per_class[label] = {
|
||||
"precision": precision,
|
||||
"recall": recall,
|
||||
"f1": f1,
|
||||
"support": actual_positive,
|
||||
"predicted": predicted_positive,
|
||||
}
|
||||
|
||||
macro_f1 = sum(row["f1"] for row in per_class.values()) / len(per_class) if per_class else 0.0
|
||||
ece, calibration_bins = _calibration_error(accepted)
|
||||
truth_classes_in_split = {entry["truth"]["bristolType"] for entry in records}
|
||||
return {
|
||||
"threshold": threshold,
|
||||
"counts": {"total": len(records), "accepted": len(accepted), "abstained": len(records) - len(accepted)},
|
||||
"abstentions": abstentions,
|
||||
"coverage": len(accepted) / len(records) if records else 0.0,
|
||||
"precisionAtCoverage": (
|
||||
sum(1 for truth, predicted, _ in accepted if truth == predicted) / len(accepted) if accepted else 0.0
|
||||
),
|
||||
"perClass": per_class,
|
||||
"excludedClasses": sorted(truth_classes_in_split - set(per_class)),
|
||||
"macroF1": macro_f1,
|
||||
"ece": ece,
|
||||
"calibrationBins": calibration_bins,
|
||||
"confusion": dict(sorted(confusion.items())),
|
||||
}
|
||||
|
||||
|
||||
def _validate_prediction(entry: object, where: str) -> dict:
|
||||
"""Normalise and bound one prediction record, or refuse it."""
|
||||
if not isinstance(entry, dict):
|
||||
raise EvaluationError(f"{where} is not a prediction object")
|
||||
model = entry.get("model")
|
||||
if not isinstance(model, str) or not model.strip():
|
||||
raise EvaluationError(f"{where} needs the model that produced the prediction")
|
||||
truth = entry.get("truth")
|
||||
if not isinstance(truth, dict) or not isinstance(truth.get("bristolType"), int):
|
||||
raise EvaluationError(f"{where} needs truth.bristolType as a whole number")
|
||||
if truth["bristolType"] not in BRISTOL_CLASSES:
|
||||
raise EvaluationError(f"{where} truth.bristolType {truth['bristolType']} is outside Bristol 1-7")
|
||||
split = entry.get("split")
|
||||
if split not in SPLITS:
|
||||
raise EvaluationError(f"{where} split must be one of {', '.join(SPLITS)}")
|
||||
subject = entry.get("subjectKey")
|
||||
if not isinstance(subject, str) or not _SHA_LIKE.match(subject):
|
||||
raise EvaluationError(f"{where} needs the 16-character pseudonymous subject key")
|
||||
prediction = entry.get("prediction")
|
||||
if prediction is not None:
|
||||
if not isinstance(prediction, dict):
|
||||
raise EvaluationError(f"{where} prediction must be an object or null")
|
||||
if prediction.get("bristolType") not in BRISTOL_CLASSES:
|
||||
raise EvaluationError(f"{where} predicted bristolType must be 1-7")
|
||||
confidence = prediction.get("confidence")
|
||||
if not isinstance(confidence, (int, float)) or isinstance(confidence, bool) or not 0.0 <= confidence <= 1.0:
|
||||
raise EvaluationError(f"{where} confidence must be a number between 0 and 1")
|
||||
return {
|
||||
"model": model.strip(),
|
||||
"subjectKey": subject,
|
||||
"split": split,
|
||||
"truth": {"bristolType": truth["bristolType"]},
|
||||
"prediction": None
|
||||
if prediction is None
|
||||
else {"bristolType": prediction["bristolType"], "confidence": float(prediction["confidence"])},
|
||||
}
|
||||
|
||||
|
||||
def load_predictions(paths: list[str]) -> list[dict]:
|
||||
"""Read prediction JSONL files, refusing anything the gates could not honestly be computed from."""
|
||||
records: list[dict] = []
|
||||
for raw_path in paths:
|
||||
path = Path(raw_path)
|
||||
if not path.is_file():
|
||||
raise EvaluationError(f"predictions file not found: {path}")
|
||||
for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):
|
||||
if not line.strip():
|
||||
continue
|
||||
try:
|
||||
entry = json.loads(line)
|
||||
except json.JSONDecodeError as exc:
|
||||
raise EvaluationError(f"{path}:{number} is not valid JSON: {exc.msg}") from exc
|
||||
records.append(_validate_prediction(entry, f"{path}:{number}"))
|
||||
if not records:
|
||||
raise EvaluationError("no prediction records were supplied")
|
||||
return records
|
||||
|
||||
|
||||
def group_by_model(records: list[dict]) -> dict[str, list[dict]]:
|
||||
models: dict[str, list[dict]] = {}
|
||||
for entry in records:
|
||||
models.setdefault(entry["model"], []).append(
|
||||
{key: value for key, value in entry.items() if key != "model"}
|
||||
)
|
||||
return models
|
||||
|
||||
|
||||
def format_report(report: dict, *, source: str) -> str:
|
||||
"""Render the comparison as a compact receipt a human can read in a terminal."""
|
||||
lines = [
|
||||
"TIMMY CALIBRATION REPORT",
|
||||
f"source: {source}",
|
||||
f"threshold: chosen on validation only · scored on test · coverage floor {report['models'][0]['minCoverage']:.0%}"
|
||||
if report["models"]
|
||||
else "threshold: nothing to score",
|
||||
"candidates: " + ", ".join(row["model"] for row in report["models"]),
|
||||
"",
|
||||
f"{'model':<26}{'thr':>5}{'macroF1':>9}{'prec@cov':>10}{'coverage':>10}{'ECE':>7} gate",
|
||||
]
|
||||
for row in report["models"]:
|
||||
gate = "PASS" if row["gate"]["passed"] else "FAIL: " + ", ".join(row["gate"]["failed"])
|
||||
lines.append(
|
||||
f"{row['model']:<26}{row['threshold']:>5.2f}{row['macroF1']:>9.3f}"
|
||||
f"{row['precisionAtCoverage']:>10.3f}{row['coverage']:>10.2f}{row['ece']:>7.3f} {gate}"
|
||||
)
|
||||
limits = report["gates"]
|
||||
lines += [
|
||||
"",
|
||||
"verdict: " + report["verdict"] + (f" — recommended {report['recommended']}" if report["recommended"] else ""),
|
||||
f"gates: macroF1 >= {limits['macroF1']:.2f} · precision@coverage >= {limits['precisionAtCoverage']:.2f} · ECE <= {limits['ece']:.2f}",
|
||||
]
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description="Score Bristol candidates against the release gates.")
|
||||
parser.add_argument("--predictions", action="append", required=True, metavar="PATH", help="prediction JSONL (repeatable)")
|
||||
parser.add_argument("--min-coverage", type=float, default=0.5, help="coverage floor for threshold selection")
|
||||
parser.add_argument("--json", metavar="PATH", help="write the full report as JSON here")
|
||||
parser.add_argument("--source", default=None, help="what the predictions are, recorded in the receipt")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
try:
|
||||
records = load_predictions(args.predictions)
|
||||
report = compare(group_by_model(records), min_coverage=args.min_coverage)
|
||||
except EvaluationError as exc:
|
||||
print(f"calibration harness refused: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
source = args.source or f"{len(records)} prediction records from {len(args.predictions)} file(s)"
|
||||
print(format_report(report, source=source))
|
||||
if args.json:
|
||||
Path(args.json).write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
||||
return 0 if report["verdict"] == "GO" else 3
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,241 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Immutable, auditable corpus manifest for Timmy training contributions.
|
||||
|
||||
Every ingested sample is described by one append-only manifest record: who consented, under
|
||||
which consent version, where the bytes came from, the hashes of the raw input and the derived
|
||||
image, how the derived image was produced, who reviewed the labels, which split it belongs to,
|
||||
and where it sits in its lifecycle. Nothing in this module rewrites history: records are only
|
||||
ever appended, and a withdrawal is a tombstone, not a deletion of the trail.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
SCHEMA_VERSION = 2
|
||||
PIPELINE = "timmy-ingest/2"
|
||||
|
||||
SAMPLE = "sample"
|
||||
TOMBSTONE = "tombstone"
|
||||
RECORD_TYPES = frozenset({SAMPLE, TOMBSTONE})
|
||||
|
||||
ACTIVE = "active"
|
||||
REVOKED = "revoked"
|
||||
STATES = frozenset({ACTIVE, REVOKED})
|
||||
|
||||
SPLITS = ("train", "validation", "test")
|
||||
REVIEWERS = ("user-confirmed", "clinician-reviewed")
|
||||
COLORS = ("brown", "green", "yellow", "pale", "red", "black")
|
||||
QUALITIES = ("good", "fair", "poor")
|
||||
|
||||
_SHA256 = re.compile(r"^[0-9a-f]{64}$")
|
||||
_CONTRIBUTION_ID = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{7,127}$")
|
||||
|
||||
|
||||
class CorpusError(Exception):
|
||||
"""A record cannot be built or written as asked."""
|
||||
|
||||
|
||||
class IllegalTransition(CorpusError):
|
||||
"""A lifecycle change that must never happen is being attempted."""
|
||||
|
||||
|
||||
def sha256_hex(data: bytes) -> str:
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def _require_sha256(value: object, field: str) -> str:
|
||||
if not isinstance(value, str) or not _SHA256.match(value):
|
||||
raise CorpusError(f"{field} must be a lowercase hex sha256 digest")
|
||||
return value
|
||||
|
||||
|
||||
def _require_iso(value: object, field: str) -> str:
|
||||
if not isinstance(value, str) or not re.match(r"^\d{4}-\d{2}-\d{2}T", value):
|
||||
raise CorpusError(f"{field} must be an ISO-8601 timestamp")
|
||||
return value
|
||||
|
||||
|
||||
def build_record(
|
||||
*,
|
||||
contribution_id: str,
|
||||
consent_version: str,
|
||||
subject_key: str,
|
||||
split: str,
|
||||
labels: dict,
|
||||
review_status: str,
|
||||
derived_sha256: str,
|
||||
raw_sha256: str,
|
||||
ingested_at: str,
|
||||
source_kind: str = "cli-ingest",
|
||||
) -> dict:
|
||||
"""Build one active sample record carrying its full provenance."""
|
||||
if not isinstance(contribution_id, str) or not _CONTRIBUTION_ID.match(contribution_id):
|
||||
raise CorpusError("contribution id is required and must be a stable opaque identifier")
|
||||
if not isinstance(consent_version, str) or not consent_version.strip():
|
||||
raise CorpusError("consent version is required")
|
||||
if not isinstance(subject_key, str) or not re.match(r"^[0-9a-f]{16}$", subject_key):
|
||||
raise CorpusError("subject key must be the 16-character pseudonymous digest")
|
||||
if split not in SPLITS:
|
||||
raise CorpusError(f"split must be one of {', '.join(SPLITS)}")
|
||||
if review_status not in REVIEWERS:
|
||||
raise CorpusError(f"review status must be one of {', '.join(REVIEWERS)}")
|
||||
if not isinstance(source_kind, str) or not source_kind.strip():
|
||||
raise CorpusError("source kind is required")
|
||||
if not isinstance(labels, dict) or set(labels) != {"bristolType", "color", "quality"}:
|
||||
raise CorpusError("labels must carry bristolType, color, and quality")
|
||||
if labels["bristolType"] not in range(1, 8):
|
||||
raise CorpusError("bristolType must be 1-7")
|
||||
if labels["color"] not in COLORS or labels["quality"] not in QUALITIES:
|
||||
raise CorpusError("color and quality must be from the documented vocabularies")
|
||||
derived = _require_sha256(derived_sha256, "derived sha256")
|
||||
raw = _require_sha256(raw_sha256, "raw input sha256")
|
||||
stamped = _require_iso(ingested_at, "ingestedAt")
|
||||
|
||||
return {
|
||||
"schemaVersion": SCHEMA_VERSION,
|
||||
"recordType": SAMPLE,
|
||||
"contributionId": contribution_id,
|
||||
"imageId": derived,
|
||||
"relativePath": f"images/{derived}.jpg",
|
||||
"subjectKey": subject_key,
|
||||
"split": split,
|
||||
"consentVersion": consent_version.strip(),
|
||||
"consent": {"version": consent_version.strip(), "recordedAt": stamped},
|
||||
"source": {"kind": source_kind, "inputSha256": raw},
|
||||
"hashes": {"rawInput": raw, "derived": derived},
|
||||
"derivation": {
|
||||
"format": "jpeg",
|
||||
"maxDimension": 1200,
|
||||
"metadataStripped": True,
|
||||
"pipeline": PIPELINE,
|
||||
},
|
||||
"labels": {
|
||||
"bristolType": labels["bristolType"],
|
||||
"color": labels["color"],
|
||||
"quality": labels["quality"],
|
||||
},
|
||||
"review": {"status": review_status, "reviewedAt": stamped},
|
||||
"lifecycle": {"state": ACTIVE, "updatedAt": stamped},
|
||||
"ingestedAt": stamped,
|
||||
}
|
||||
|
||||
|
||||
def assert_admissible(manifest_path: Path, record: dict) -> None:
|
||||
"""Refuse a sample that duplicates a contribution or re-labels the same derived image."""
|
||||
if record.get("recordType", SAMPLE) != SAMPLE:
|
||||
return
|
||||
for prior in load_manifest(manifest_path):
|
||||
if prior.get("recordType", SAMPLE) != SAMPLE:
|
||||
continue
|
||||
if prior.get("contributionId") == record.get("contributionId"):
|
||||
raise IllegalTransition(f"contribution {record['contributionId']} was already ingested")
|
||||
if prior.get("imageId") == record.get("imageId") and prior.get("labels") != record.get("labels"):
|
||||
raise CorpusError("the same derived image cannot carry two different label sets")
|
||||
|
||||
|
||||
def append_record(manifest_path: Path, record: dict) -> None:
|
||||
"""Append one record to the manifest without touching what is already there."""
|
||||
assert_admissible(manifest_path, record)
|
||||
manifest_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with manifest_path.open("a", encoding="utf-8") as manifest:
|
||||
manifest.write(json.dumps(record, separators=(",", ":"), sort_keys=False) + "\n")
|
||||
|
||||
|
||||
def load_manifest(manifest_path: Path) -> list[dict]:
|
||||
"""Read the append-only manifest in order. A missing manifest is an empty corpus."""
|
||||
if not manifest_path.exists():
|
||||
return []
|
||||
with manifest_path.open("r", encoding="utf-8") as manifest:
|
||||
return [json.loads(line) for line in manifest.read().splitlines() if line.strip()]
|
||||
|
||||
|
||||
def transition(record: dict, state: str, at: str, reason: str | None = None) -> dict:
|
||||
"""Return a copy of the record in the next lifecycle state, or refuse the move."""
|
||||
current = record.get("lifecycle", {}).get("state")
|
||||
if state not in STATES:
|
||||
raise IllegalTransition(f"unknown lifecycle state: {state!r}")
|
||||
if current not in STATES:
|
||||
raise IllegalTransition(f"record carries no legal lifecycle state: {current!r}")
|
||||
if current == state:
|
||||
raise IllegalTransition(f"record is already {state}")
|
||||
if current == REVOKED:
|
||||
raise IllegalTransition("a revoked contribution cannot return to the active dataset")
|
||||
_require_iso(at, "lifecycle timestamp")
|
||||
if state == REVOKED and (not isinstance(reason, str) or not reason.strip()):
|
||||
raise IllegalTransition("revocation needs a recorded reason")
|
||||
updated = {**record, "lifecycle": {"state": state, "updatedAt": at}}
|
||||
if state == REVOKED:
|
||||
updated["lifecycle"]["reason"] = reason.strip()
|
||||
return updated
|
||||
|
||||
|
||||
def active_records(records: list[dict]) -> list[dict]:
|
||||
"""Samples still usable for training: active, and never revoked afterwards."""
|
||||
revoked = {
|
||||
record.get("contributionId")
|
||||
for record in records
|
||||
if record.get("recordType") == TOMBSTONE or record.get("lifecycle", {}).get("state") == REVOKED
|
||||
}
|
||||
return [
|
||||
record
|
||||
for record in records
|
||||
if record.get("recordType", SAMPLE) == SAMPLE
|
||||
and record.get("lifecycle", {}).get("state") == ACTIVE
|
||||
and record.get("contributionId") not in revoked
|
||||
]
|
||||
|
||||
|
||||
def _safe_dataset_path(dataset_root: Path, relative: str) -> Path:
|
||||
path = (dataset_root / relative).resolve()
|
||||
if not path.is_relative_to(dataset_root.resolve()):
|
||||
raise CorpusError(f"refusing to touch a path outside the dataset: {relative}")
|
||||
return path
|
||||
|
||||
|
||||
def revoke(dataset_root: Path, contribution_id: str, at: str, reason: str) -> dict:
|
||||
"""Revoke one contribution: append a tombstone and delete its bytes from the dataset."""
|
||||
manifest_path = dataset_root / "manifest.jsonl"
|
||||
records = load_manifest(manifest_path)
|
||||
samples = [
|
||||
record
|
||||
for record in records
|
||||
if record.get("recordType", SAMPLE) == SAMPLE and record.get("contributionId") == contribution_id
|
||||
]
|
||||
if not isinstance(reason, str) or not reason.strip():
|
||||
raise IllegalTransition("revocation needs a recorded reason")
|
||||
if any(
|
||||
record.get("recordType") == TOMBSTONE and record.get("contributionId") == contribution_id
|
||||
for record in records
|
||||
):
|
||||
raise IllegalTransition(f"contribution {contribution_id} is already revoked")
|
||||
if not samples:
|
||||
raise IllegalTransition(f"no ingested sample carries contribution {contribution_id}")
|
||||
sample = samples[-1]
|
||||
if sample.get("lifecycle", {}).get("state") != ACTIVE:
|
||||
raise IllegalTransition(f"contribution {contribution_id} is not active")
|
||||
|
||||
deleted = []
|
||||
candidates = [(sample["relativePath"], sample["hashes"]["derived"])]
|
||||
# A raw pre-derivation copy is identified by the raw input digest, never by a user path.
|
||||
for path in sorted((dataset_root / "raw").glob(f"{sample['hashes']['rawInput']}*")):
|
||||
candidates.append((str(path.relative_to(dataset_root)), sample["hashes"]["rawInput"]))
|
||||
for relative, digest in candidates:
|
||||
target = _safe_dataset_path(dataset_root, relative)
|
||||
existed = target.is_file()
|
||||
if existed:
|
||||
target.unlink()
|
||||
deleted.append({"relativePath": relative, "sha256": digest, "deleted": existed})
|
||||
|
||||
tombstone = {
|
||||
"schemaVersion": SCHEMA_VERSION,
|
||||
"recordType": TOMBSTONE,
|
||||
"contributionId": contribution_id,
|
||||
"imageId": sample["imageId"],
|
||||
"deletedArtifacts": deleted,
|
||||
"lifecycle": {"state": REVOKED, "updatedAt": _require_iso(at, "lifecycle timestamp"), "reason": reason.strip()},
|
||||
}
|
||||
append_record(manifest_path, tombstone)
|
||||
return tombstone
|
||||
@@ -12,6 +12,8 @@ from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
from corpus_manifest import CorpusError, append_record, assert_admissible, build_record, revoke, sha256_hex
|
||||
|
||||
COLORS = ("brown", "green", "yellow", "pale", "red", "black")
|
||||
QUALITIES = ("good", "fair", "poor")
|
||||
REVIEWERS = ("user-confirmed", "clinician-reviewed")
|
||||
@@ -19,27 +21,62 @@ REVIEWERS = ("user-confirmed", "clinician-reviewed")
|
||||
|
||||
def parser() -> argparse.ArgumentParser:
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--input", required=True)
|
||||
p.add_argument("--input")
|
||||
p.add_argument("--output-dir", required=True)
|
||||
p.add_argument("--subject-id", required=True)
|
||||
p.add_argument("--consent-version", required=True)
|
||||
p.add_argument("--bristol-type", required=True, type=int, choices=range(1, 8))
|
||||
p.add_argument("--color", required=True, choices=COLORS)
|
||||
p.add_argument("--quality", required=True, choices=QUALITIES)
|
||||
p.add_argument("--reviewer", required=True, choices=REVIEWERS)
|
||||
p.add_argument("--subject-id")
|
||||
p.add_argument("--consent-version")
|
||||
p.add_argument("--contribution-id")
|
||||
p.add_argument("--bristol-type", type=int, choices=range(1, 8))
|
||||
p.add_argument("--color", choices=COLORS)
|
||||
p.add_argument("--quality", choices=QUALITIES)
|
||||
p.add_argument("--reviewer", choices=REVIEWERS)
|
||||
# Revocation is a corpus operation, not an ingestion: it needs the dataset and a reason.
|
||||
p.add_argument("--revoke", metavar="CONTRIBUTION_ID")
|
||||
p.add_argument("--reason")
|
||||
return p
|
||||
|
||||
|
||||
def require_ingest_arguments(args: argparse.Namespace) -> None:
|
||||
if not args.consent_version or not args.consent_version.strip():
|
||||
raise SystemExit("consent version is required")
|
||||
if not args.reviewer:
|
||||
raise SystemExit("a human review status is required")
|
||||
missing = [
|
||||
flag
|
||||
for flag, value in (
|
||||
("--input", args.input),
|
||||
("--subject-id", args.subject_id),
|
||||
("--contribution-id", args.contribution_id),
|
||||
("--bristol-type", args.bristol_type),
|
||||
("--color", args.color),
|
||||
("--quality", args.quality),
|
||||
)
|
||||
if value is None or value == ""
|
||||
]
|
||||
if missing:
|
||||
raise SystemExit(f"ingesting a sample also needs {', '.join(missing)}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = parser().parse_args()
|
||||
if args.revoke:
|
||||
if not args.reason:
|
||||
raise SystemExit("revoking a contribution needs a recorded reason")
|
||||
try:
|
||||
tombstone = revoke(Path(args.output_dir), args.revoke, datetime.now(timezone.utc).isoformat(), args.reason)
|
||||
except CorpusError as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
print(json.dumps(tombstone))
|
||||
return 0
|
||||
|
||||
require_ingest_arguments(args)
|
||||
salt = os.environ.get("TIMMY_DATASET_SALT", "")
|
||||
if len(salt) < 8:
|
||||
raise SystemExit("TIMMY_DATASET_SALT must contain at least 8 characters")
|
||||
if not args.consent_version.strip():
|
||||
raise SystemExit("consent version is required")
|
||||
|
||||
try:
|
||||
with Image.open(args.input) as source:
|
||||
raw_bytes = Path(args.input).read_bytes()
|
||||
with Image.open(io.BytesIO(raw_bytes)) as source:
|
||||
image = ImageOps.exif_transpose(source).convert("RGB")
|
||||
image.thumbnail((1200, 1200))
|
||||
buffer = io.BytesIO()
|
||||
@@ -55,37 +92,34 @@ def main() -> int:
|
||||
|
||||
output = Path(args.output_dir)
|
||||
images = output / "images"
|
||||
images.mkdir(parents=True, exist_ok=True)
|
||||
relative_path = f"images/{image_id}.jpg"
|
||||
target = output / relative_path
|
||||
if not target.exists():
|
||||
target.write_bytes(data)
|
||||
|
||||
record = {
|
||||
"schemaVersion": 1,
|
||||
"imageId": image_id,
|
||||
"relativePath": relative_path,
|
||||
"subjectKey": subject_key,
|
||||
"split": split,
|
||||
"consentVersion": args.consent_version.strip(),
|
||||
"ingestedAt": datetime.now(timezone.utc).isoformat(),
|
||||
"labels": {
|
||||
"bristolType": args.bristol_type,
|
||||
"color": args.color,
|
||||
"quality": args.quality,
|
||||
},
|
||||
"review": {"status": args.reviewer},
|
||||
"derivation": {
|
||||
"format": "jpeg",
|
||||
"maxDimension": 1200,
|
||||
"metadataStripped": True,
|
||||
},
|
||||
}
|
||||
with (output / "manifest.jsonl").open("a", encoding="utf-8") as manifest:
|
||||
manifest.write(json.dumps(record, separators=(",", ":")) + "\n")
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
record = build_record(
|
||||
contribution_id=args.contribution_id,
|
||||
consent_version=args.consent_version,
|
||||
subject_key=subject_key,
|
||||
split=split,
|
||||
labels={"bristolType": args.bristol_type, "color": args.color, "quality": args.quality},
|
||||
review_status=args.reviewer,
|
||||
derived_sha256=image_id,
|
||||
raw_sha256=sha256_hex(raw_bytes),
|
||||
ingested_at=now,
|
||||
)
|
||||
try:
|
||||
# Refuse a duplicate contribution before any byte lands in the active dataset.
|
||||
assert_admissible(output / "manifest.jsonl", record)
|
||||
images.mkdir(parents=True, exist_ok=True)
|
||||
if not target.exists():
|
||||
target.write_bytes(data)
|
||||
append_record(output / "manifest.jsonl", record)
|
||||
except CorpusError as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
print(json.dumps(record))
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -1,10 +1,16 @@
|
||||
import { chromium } from 'playwright';
|
||||
import { mkdir, writeFile } from 'node:fs/promises';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
|
||||
import { createHash } from 'node:crypto';
|
||||
import os from 'node:os';
|
||||
import { promisify } from 'node:util';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { analyzePhoto, visionFailureResponse } from '../src/vision-service.js';
|
||||
import { loadVisionCorpus } from '../src/vision-corpus.js';
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
const output = path.resolve(process.argv[2] || 'release-demo.webm');
|
||||
const version = process.env.TIMMY_RELEASE_VERSION || 'review build';
|
||||
// The release delta is supplied by the builder so the recording states, on camera, what actually
|
||||
@@ -335,6 +341,20 @@ await tap('#analyze-photo', 600);
|
||||
await page.getByText(/does not show a form Timmy can read safely/i).waitFor();
|
||||
await caption('Unreadable visual fields abstain as well: a valid reply with nothing to prefill is not an error', 2700);
|
||||
await indicate('.scan-result');
|
||||
// Issue #32's quality gate: the model can be highly confident about a photo it also calls poor, and
|
||||
// that confident answer must still abstain. Replayed from the committed corpus, and asserted in the
|
||||
// browser so the scene is a check rather than a claim.
|
||||
analyzeOverride = await corpusAnalyzeResult(corpusCase('poor-quality-confident'));
|
||||
await tap('#retake-photo', 400);
|
||||
await page.locator('#gallery-photo').setInputFiles('tests/fixtures/vision-corpus/blur-low-detail.png');
|
||||
await page.locator('#ai-consent').check();
|
||||
await tap('#analyze-photo', 600);
|
||||
await page.getByText(/too blurry or poorly lit/i).waitFor();
|
||||
if (await page.getByText(/BRISTOL FORM/i).count() !== 0) {
|
||||
throw new Error('A low-quality photo must not prefill a form, however confident the reply looked');
|
||||
}
|
||||
await caption('A confident reply about a blurry photo still abstains — photo quality is part of the policy, not just confidence', 3100);
|
||||
await indicate('.scan-result');
|
||||
analyzeOverride = await corpusAnalyzeResult(corpusCase('out-of-range-fields'));
|
||||
await tap('#retake-photo', 400);
|
||||
await page.locator('#gallery-photo').setInputFiles('tests/fixtures/vision-corpus/out-of-range-fields.png');
|
||||
@@ -477,6 +497,473 @@ await page.getByText(/Your poop\. Your phone\./i).waitFor();
|
||||
await caption('Every byte-bearing control the model names, in the running app: local ledger, consent-gated upload, portable export, full deletion', 2800);
|
||||
await indicate('#export');
|
||||
await caption('Export writes a plain JSON file you hold — the model records it as a data-lifetime boundary, not a server sync', 2600);
|
||||
// Issue #24 — training contribution is a second, separate consent. The recording shows the section
|
||||
// reporting nothing queued after ordinary saves, the version and receipt on screen before the
|
||||
// button can be pressed, one receipt queued on the device only, and a withdrawal tombstone that
|
||||
// names each derived artifact by contribution id.
|
||||
await caption('Training contribution is off by default: a saved log is never training data', 2600);
|
||||
await page.locator('[data-training-consent]').waitFor();
|
||||
const contributionKey = await page.evaluate(() => `timmy:${document.querySelector('meta[name="timmy-base-path"]')?.content || '/'}:training-contributions-v1`);
|
||||
const storedContributions = async () => JSON.parse(await page.evaluate(key => localStorage.getItem(key) || '[]', contributionKey));
|
||||
if ((await page.locator('[data-contribution-queued-count]').innerText()).trim() !== '0') {
|
||||
throw new Error('a journal save must not queue a training contribution');
|
||||
}
|
||||
await indicate('[data-contribute-entry="demo-wipe"]');
|
||||
await tap('[data-contribute-entry="demo-wipe"]', 600);
|
||||
await page.locator('[data-contribution-sheet]').waitFor();
|
||||
const receiptId = (await page.locator('[data-receipt-id]').innerText()).trim();
|
||||
if (!receiptId) throw new Error('the consent sheet must name the receipt before consent');
|
||||
const sheetCopy = await page.locator('[data-contribution-sheet]').innerText();
|
||||
if (!sheetCopy.includes('training-consent-v1')) throw new Error('the consent sheet must state the consent version');
|
||||
if (!(await page.locator('#contribute-confirm').isDisabled())) throw new Error('the contribute control must stay disabled until the terms are accepted');
|
||||
if ((await storedContributions()).length !== 0) throw new Error('no receipt may exist before explicit consent');
|
||||
await caption('The consent version and the receipt are on screen before the contribute button works', 3000);
|
||||
await page.locator('#training-consent-check').check();
|
||||
await tap('#contribute-confirm', 700);
|
||||
await page.locator('[data-contribution-state="queued-local"]').waitFor();
|
||||
const queuedReceipts = await storedContributions();
|
||||
if (queuedReceipts.length !== 1 || queuedReceipts[0].consentVersion !== 'training-consent-v1') {
|
||||
throw new Error('one queued receipt must carry the consent version it was accepted under');
|
||||
}
|
||||
if (JSON.stringify(queuedReceipts).includes('data:image')) throw new Error('a receipt must never copy the photo bytes');
|
||||
await caption('One confirmed log is queued on this device with a receipt you can withdraw — nothing is uploaded', 3200);
|
||||
await indicate('[data-contribution-state="queued-local"]');
|
||||
await tap(`[data-withdraw="${receiptId}"]`, 700);
|
||||
await page.locator('[data-contribution-state="withdrawn"]').waitFor();
|
||||
const withdrawnReceipts = await storedContributions();
|
||||
const tombstone = withdrawnReceipts[0];
|
||||
if (!tombstone || tombstone.state !== 'withdrawn') throw new Error('withdrawal must keep the receipt as a tombstone');
|
||||
if ((tombstone.artifacts || []).length !== 2) throw new Error('the tombstone must name every derived artifact');
|
||||
for (const artifact of tombstone.artifacts) {
|
||||
if (artifact.contributionId !== receiptId) throw new Error('every derived artifact must be addressed by contribution id');
|
||||
if (artifact.state !== 'withdrawn') throw new Error('every derived artifact must be marked withdrawn');
|
||||
}
|
||||
await caption('Withdrawing keeps a tombstone naming every derived artifact by contribution ID', 3000);
|
||||
await indicate('[data-withdrawn-artifacts]');
|
||||
|
||||
// Issue #25 — the corpus pipeline is a command-line tool, not a screen in the app. Rather than
|
||||
// inventing a UI for it, this scene runs the real ingest and revoke commands against the committed
|
||||
// synthetic fixture in a throwaway dataset and shows their actual output. Every number, hash prefix,
|
||||
// and refusal line below is read back from those commands at record time.
|
||||
async function corpusCli(args) {
|
||||
return execFileAsync('python3', args, {
|
||||
cwd: process.cwd(),
|
||||
env: { ...process.env, TIMMY_DATASET_SALT: 'demo-only-salt-not-a-secret' },
|
||||
});
|
||||
}
|
||||
async function corpusAudit() {
|
||||
const dataset = await mkdtemp(path.join(os.tmpdir(), 'timmy-corpus-demo-'));
|
||||
const fixture = 'tests/fixtures/synthetic-type4.jpg';
|
||||
const contributionId = 'demo-contrib-0001-abcd';
|
||||
const manifestPath = path.join(dataset, 'manifest.jsonl');
|
||||
const lines = async () => (await readFile(manifestPath, 'utf8'))
|
||||
.split('\n').filter(line => line.trim()).map(line => JSON.parse(line));
|
||||
const images = async () => (await readdir(path.join(dataset, 'images')).catch(() => [])).sort();
|
||||
try {
|
||||
const ingest = await corpusCli([
|
||||
'scripts/ingest_training_photo.py',
|
||||
'--input', fixture,
|
||||
'--output-dir', dataset,
|
||||
'--subject-id', 'demo-subject',
|
||||
'--consent-version', 'training-consent-v1',
|
||||
'--contribution-id', contributionId,
|
||||
'--bristol-type', '4', '--color', 'brown', '--quality', 'good',
|
||||
'--reviewer', 'user-confirmed',
|
||||
]);
|
||||
const record = JSON.parse(ingest.stdout);
|
||||
const fixtureDigest = createHash('sha256').update(await readFile(fixture)).digest('hex');
|
||||
if (record.recordType !== 'sample' || record.lifecycle?.state !== 'active') {
|
||||
throw new Error('ingest must write one active sample record');
|
||||
}
|
||||
// The record names the source by digest only: the caller's path must never be stored.
|
||||
if (record.source?.inputSha256 !== fixtureDigest || record.hashes?.rawInput !== fixtureDigest) {
|
||||
throw new Error('the record must carry the raw input digest');
|
||||
}
|
||||
if (JSON.stringify(record).includes(fixture) || 'inputPath' in record) {
|
||||
throw new Error('the record must never store the caller path');
|
||||
}
|
||||
const afterIngestImages = await images();
|
||||
if (afterIngestImages.length !== 1 || (await lines()).length !== 1) {
|
||||
throw new Error('ingest must write exactly one derived image and one manifest record');
|
||||
}
|
||||
|
||||
const revoke = await corpusCli([
|
||||
'scripts/ingest_training_photo.py', '--output-dir', dataset,
|
||||
'--revoke', contributionId, '--reason', 'participant withdrew consent',
|
||||
]);
|
||||
const tombstone = JSON.parse(revoke.stdout);
|
||||
const afterRevokeImages = await images();
|
||||
const history = await lines();
|
||||
if (tombstone.recordType !== 'tombstone' || history.length !== 2) {
|
||||
throw new Error('revocation must append exactly one tombstone to the existing history');
|
||||
}
|
||||
if (afterRevokeImages.length !== 0) throw new Error('revocation must delete the derived bytes');
|
||||
if (history[0].recordType !== 'sample' || history[0].lifecycle?.state !== 'active') {
|
||||
throw new Error('the original record must survive its own withdrawal');
|
||||
}
|
||||
if ('labels' in tombstone || 'subjectKey' in tombstone) {
|
||||
throw new Error('a tombstone must not carry the labels or subject key');
|
||||
}
|
||||
const deleted = tombstone.deletedArtifacts || [];
|
||||
if (!deleted.some(item => item.relativePath === `images/${record.imageId}.jpg` && item.deleted === true)) {
|
||||
throw new Error('the tombstone must name the derived artifact it removed');
|
||||
}
|
||||
const active = await corpusCli([
|
||||
'-c',
|
||||
'import sys;sys.path.insert(0,"scripts");import corpus_manifest as c;from pathlib import Path;'
|
||||
+ 'print(len(c.active_records(c.load_manifest(Path(sys.argv[1])/"manifest.jsonl"))))',
|
||||
dataset,
|
||||
]);
|
||||
if (active.stdout.trim() !== '0') throw new Error('a trainer reading the manifest must see zero active samples');
|
||||
|
||||
// A second revocation of the same contribution has to be refused, and refuse without writing.
|
||||
let refusal = '';
|
||||
try {
|
||||
await corpusCli([
|
||||
'scripts/ingest_training_photo.py', '--output-dir', dataset,
|
||||
'--revoke', contributionId, '--reason', 'participant withdrew consent again',
|
||||
]);
|
||||
throw new Error('a second revocation must be refused');
|
||||
} catch (error) {
|
||||
if (error.message === 'a second revocation must be refused') throw error;
|
||||
refusal = String(error.stderr || error.stdout || '').trim().split('\n').filter(Boolean).pop() || '';
|
||||
}
|
||||
if (!/already revoked/.test(refusal)) throw new Error(`second revocation refusal was not legible: ${refusal}`);
|
||||
if ((await lines()).length !== 2) throw new Error('a refused revocation must append nothing');
|
||||
return { record, tombstone, deleted, refusal, derived: record.imageId, fixtureDigest };
|
||||
} finally {
|
||||
await rm(dataset, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
const audit = await corpusAudit();
|
||||
const short = value => String(value).slice(0, 12);
|
||||
const deletedName = audit.deleted[0].relativePath.split('/').pop().replace(/\.jpg$/, '');
|
||||
await tap('[data-view="home"]', 500);
|
||||
// The card is drawn from the values above, so a run that stops writing provenance or stops deleting
|
||||
// bytes fails here instead of recording a caption that claims it.
|
||||
const auditRows = [
|
||||
['command', 'python3 scripts/ingest_training_photo.py --input <synthetic fixture>'],
|
||||
['record', `sample · schemaVersion ${audit.record.schemaVersion}`],
|
||||
['consent', audit.record.consent.version],
|
||||
['source', `sha256 ${short(audit.record.source.inputSha256)}… (path never stored)`],
|
||||
['hashes', `raw ${short(audit.record.hashes.rawInput)}… · derived ${short(audit.record.hashes.derived)}…`],
|
||||
['derived', audit.record.derivation.pipeline + ' · jpeg · ≤1200px · metadata stripped'],
|
||||
['review', audit.record.review.status],
|
||||
['lifecycle', audit.record.lifecycle.state],
|
||||
['—', ''],
|
||||
['command', `--revoke ${audit.record.contributionId} --reason "participant withdrew consent"`],
|
||||
['deleted', `images/${short(deletedName)}….jpg → gone from disk`],
|
||||
['tombstone', 'appended · keeps id + digests · drops labels + subject key'],
|
||||
['history', '2 lines: the original record is still there'],
|
||||
['trainer', 'active samples: 0'],
|
||||
['—', ''],
|
||||
['command', '--revoke again'],
|
||||
['refused', audit.refusal],
|
||||
['manifest', 'unchanged at 2 lines'],
|
||||
];
|
||||
const panelOk = await page.evaluate(rows => {
|
||||
document.querySelector('#corpus-audit')?.remove();
|
||||
const panel = document.createElement('div');
|
||||
panel.id = 'corpus-audit';
|
||||
Object.assign(panel.style, {
|
||||
position: 'fixed', inset: '96px 14px 40px 14px', zIndex: '15000',
|
||||
background: '#181412', color: '#f6f1e6', borderRadius: '18px',
|
||||
border: '1px solid rgba(245,201,91,.45)', padding: '13px 13px 11px',
|
||||
font: '600 10.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace',
|
||||
display: 'flex', flexDirection: 'column', gap: '1px', overflow: 'hidden',
|
||||
boxShadow: '0 18px 40px rgba(0,0,0,.42)',
|
||||
});
|
||||
const head = document.createElement('div');
|
||||
head.textContent = 'CORPUS AUDIT';
|
||||
Object.assign(head.style, { font: '800 14px/1.2 system-ui', letterSpacing: '.14em', color: '#f5c95b', marginBottom: '2px' });
|
||||
const sub = document.createElement('div');
|
||||
sub.textContent = 'command line, not app UI — real run, synthetic fixture';
|
||||
Object.assign(sub.style, { font: '700 9.5px/1.3 system-ui', color: '#c9bfae', marginBottom: '9px' });
|
||||
panel.append(head, sub);
|
||||
for (const [key, value] of rows) {
|
||||
const row = document.createElement('div');
|
||||
if (key === '—') { row.style.height = '7px'; panel.append(row); continue; }
|
||||
Object.assign(row.style, { display: 'flex', gap: '7px', alignItems: 'baseline' });
|
||||
const k = document.createElement('span');
|
||||
k.textContent = key;
|
||||
k.style.cssText = 'flex:0 0 62px;color:#8fd7c8;text-transform:uppercase;font-size:9px;letter-spacing:.08em';
|
||||
const v = document.createElement('span');
|
||||
v.textContent = value;
|
||||
v.style.cssText = 'flex:1 1 auto;word-break:break-word;color:#f6f1e6';
|
||||
row.append(k, v);
|
||||
panel.append(row);
|
||||
}
|
||||
document.body.append(panel);
|
||||
panel.animate([{ opacity: 0, transform: 'translateY(12px)' }, { opacity: 1, transform: 'translateY(0)' }], { duration: 320, fill: 'forwards' });
|
||||
return { height: panel.clientHeight, scroll: panel.scrollHeight, text: panel.innerText };
|
||||
}, auditRows);
|
||||
await sleep(420);
|
||||
const panelProof = await page.evaluate(() => {
|
||||
const panel = document.querySelector('#corpus-audit');
|
||||
if (!panel) return { ok: false, reason: 'panel missing' };
|
||||
const rect = panel.getBoundingClientRect();
|
||||
const topmost = document.elementFromPoint(Math.round(rect.left + rect.width / 2), Math.round(rect.top + rect.height / 2));
|
||||
const background = getComputedStyle(panel).backgroundColor;
|
||||
// A translucent card lets the nav bar and body copy ghost through its empty rows, which reads as a
|
||||
// sloppy overlay on the thinnest part of the frame. The card must be fully opaque.
|
||||
const opaque = !/^rgba\(.*,\s*(0?\.\d+|0)\)$/.test(background);
|
||||
return {
|
||||
ok: opaque && rect.top >= 60 && rect.bottom <= window.innerHeight && rect.width > 300 && rect.height > 300
|
||||
&& (topmost === panel || panel.contains(topmost)) && panel.scrollHeight <= panel.clientHeight + 1,
|
||||
top: Math.round(rect.top), height: Math.round(rect.height), width: Math.round(rect.width),
|
||||
scroll: panel.scrollHeight, client: panel.clientHeight, background, opaque,
|
||||
covered: topmost === panel || panel.contains(topmost),
|
||||
};
|
||||
});
|
||||
if (!panelProof.ok) throw new Error(`Corpus audit card is not fully readable on screen: ${JSON.stringify(panelProof)}`);
|
||||
// The card is drawn from live command output; prove the values on it are the ones the run produced.
|
||||
if (!panelOk.text.includes(audit.record.imageId.slice(0, 12))) throw new Error('the card must show the derived digest it recorded');
|
||||
if (!panelOk.text.includes('active samples: 0')) throw new Error('the card must show the post-revocation active count');
|
||||
await caption('The corpus pipeline is a command-line tool, not a screen in the app — this runs the real ingest against the committed synthetic fixture', 3400);
|
||||
await caption('Every ingested sample records consent, source digest, both hashes, derivation, review, and lifecycle — the caller file path is never stored', 3600);
|
||||
await caption('Withdrawing deletes the derived image and any raw copy, keeps the original record, and appends a tombstone — the trainer sees zero active samples', 3800);
|
||||
await caption('Revoking the same contribution twice is refused and appends nothing: the manifest only ever grows', 3200);
|
||||
await page.evaluate(() => document.querySelector('#corpus-audit')?.remove());
|
||||
// Issue #29 — the release gate is a command-line harness, not a screen in the app. This scene runs
|
||||
// the real harness over the committed synthetic prediction fixtures and shows the receipt it wrote:
|
||||
// a blocked comparison, a passing fixture, and a refused leaked corpus. Every number and every
|
||||
// refusal line on the card is read back from those runs; the scores are invented fixtures, and the
|
||||
// captions say so, because a fixture is not a measurement of any model.
|
||||
async function calibrationCli(args) {
|
||||
try {
|
||||
const result = await execFileAsync('python3', ['scripts/calibration_report.py', ...args], { cwd: process.cwd() });
|
||||
return { code: 0, stdout: result.stdout, stderr: result.stderr };
|
||||
} catch (error) {
|
||||
return {
|
||||
code: typeof error.code === 'number' ? error.code : 1,
|
||||
stdout: error.stdout || '',
|
||||
stderr: error.stderr || '',
|
||||
};
|
||||
}
|
||||
}
|
||||
async function calibrationGate() {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), 'timmy-calibration-demo-'));
|
||||
const reportPath = path.join(directory, 'report.json');
|
||||
const source = 'invented scores, not measurements of any model';
|
||||
try {
|
||||
const blocked = await calibrationCli([
|
||||
'--predictions', 'tests/fixtures/calibration/predictions.jsonl',
|
||||
'--json', reportPath, '--source', source,
|
||||
]);
|
||||
if (blocked.code !== 3) throw new Error('the synthetic candidate fixtures must block the release');
|
||||
const report = JSON.parse(await readFile(reportPath, 'utf8'));
|
||||
if (report.verdict !== 'BLOCKED' || report.recommended !== null) {
|
||||
throw new Error('a blocked gate must recommend nobody');
|
||||
}
|
||||
if (report.models.length !== 3) throw new Error('the comparison must score all three candidate fixtures');
|
||||
for (const row of report.models) {
|
||||
if (!row.gate.failed.length) throw new Error(`${row.model}: a fixture candidate must fail at least one gate`);
|
||||
if (row.selectionSplit !== 'validation' || row.evaluatedSplit !== 'test') {
|
||||
throw new Error(`${row.model}: the threshold must come from validation and the score from test`);
|
||||
}
|
||||
}
|
||||
const go = await calibrationCli([
|
||||
'--predictions', 'tests/fixtures/calibration/predictions-go.jsonl',
|
||||
'--source', 'engineered fixture — proves the passing branch, not a result',
|
||||
]);
|
||||
if (go.code !== 0 || !/verdict: GO/.test(go.stdout)) throw new Error('the engineered fixture must prove the passing branch');
|
||||
const leak = await calibrationCli(['--predictions', 'tests/fixtures/calibration/predictions-leaky.jsonl']);
|
||||
if (leak.code !== 2 || !/leaks across/.test(leak.stderr)) throw new Error('a leaked corpus must be refused before scoring');
|
||||
return { report, source, blocked, go, leak };
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
const gate = await calibrationGate();
|
||||
await tap('[data-view="home"]', 450);
|
||||
const gateRows = [
|
||||
['command', 'python3 scripts/calibration_report.py --predictions <fixtures>'],
|
||||
['split', 'threshold chosen on validation · scored on test'],
|
||||
['floor', `coverage floor ${Math.round(gate.report.models[0].minCoverage * 100)}%`],
|
||||
['—', ''],
|
||||
];
|
||||
for (const row of gate.report.models) {
|
||||
// The model id gets its own row: a 74px key column broke "specialist-candidate" at its hyphen,
|
||||
// which reads as a label with no value and a stray line under it. The name is the value here.
|
||||
gateRows.push(['model', row.model]);
|
||||
gateRows.push([
|
||||
'metrics',
|
||||
`thr ${row.threshold.toFixed(2)} · F1 ${row.macroF1.toFixed(3)} · P@C ${row.precisionAtCoverage.toFixed(3)} · cov ${row.coverage.toFixed(2)}`,
|
||||
]);
|
||||
gateRows.push(['gate', `FAIL: ${row.gate.failed.join(', ')} · ECE ${row.ece.toFixed(3)}`]);
|
||||
}
|
||||
gateRows.push(['—', '']);
|
||||
gateRows.push(['verdict', `${gate.report.verdict} · no candidate clears every gate`]);
|
||||
gateRows.push(['gates', 'macroF1 ≥ 0.80 · precision@coverage ≥ 0.90 · ECE ≤ 0.05']);
|
||||
gateRows.push(['—', '']);
|
||||
gateRows.push(['engineered', 'GO — gate-plumbing-fixture clears every gate (fixture, not a result)']);
|
||||
gateRows.push(['leaked', gate.leak.stderr.trim().replace(/^calibration harness refused:\s*/, '')]);
|
||||
gateRows.push(['data', 'synthetic predictions · no model was measured']);
|
||||
const gateCard = await page.evaluate(rows => {
|
||||
document.querySelector('#calibration-report')?.remove();
|
||||
const panel = document.createElement('div');
|
||||
panel.id = 'calibration-report';
|
||||
Object.assign(panel.style, {
|
||||
position: 'fixed', inset: '96px 14px 40px 14px', zIndex: '15000',
|
||||
background: '#181412', color: '#f6f1e6', borderRadius: '18px',
|
||||
border: '1px solid rgba(245,201,91,.45)', padding: '13px 13px 11px',
|
||||
font: '600 9.5px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace',
|
||||
display: 'flex', flexDirection: 'column', gap: '1px', overflow: 'hidden',
|
||||
boxShadow: '0 18px 40px rgba(0,0,0,.42)',
|
||||
});
|
||||
const head = document.createElement('div');
|
||||
head.textContent = 'CALIBRATION HARNESS';
|
||||
Object.assign(head.style, { font: '800 13.5px/1.2 system-ui', letterSpacing: '.12em', color: '#f5c95b', marginBottom: '2px' });
|
||||
const sub = document.createElement('div');
|
||||
sub.textContent = 'command line, not app UI — real run, synthetic predictions';
|
||||
Object.assign(sub.style, { font: '700 9px/1.3 system-ui', color: '#c9bfae', marginBottom: '8px' });
|
||||
panel.append(head, sub);
|
||||
for (const [key, value] of rows) {
|
||||
const row = document.createElement('div');
|
||||
if (key === '—') { row.style.height = '6px'; panel.append(row); continue; }
|
||||
Object.assign(row.style, { display: 'flex', gap: '6px', alignItems: 'baseline' });
|
||||
const k = document.createElement('span');
|
||||
k.textContent = key;
|
||||
k.style.cssText = 'flex:0 0 74px;color:#8fd7c8;text-transform:uppercase;font-size:8.5px;letter-spacing:.06em';
|
||||
const v = document.createElement('span');
|
||||
v.textContent = value;
|
||||
v.style.cssText = 'flex:1 1 auto;word-break:break-word;color:#f6f1e6';
|
||||
row.append(k, v);
|
||||
panel.append(row);
|
||||
}
|
||||
document.body.append(panel);
|
||||
panel.animate([{ opacity: 0, transform: 'translateY(12px)' }, { opacity: 1, transform: 'translateY(0)' }], { duration: 320, fill: 'forwards' });
|
||||
return { height: panel.clientHeight, scroll: panel.scrollHeight, text: panel.innerText };
|
||||
}, gateRows);
|
||||
await sleep(420);
|
||||
const gateProof = await page.evaluate(() => {
|
||||
const panel = document.querySelector('#calibration-report');
|
||||
if (!panel) return { ok: false, reason: 'panel missing' };
|
||||
const rect = panel.getBoundingClientRect();
|
||||
const topmost = document.elementFromPoint(Math.round(rect.left + rect.width / 2), Math.round(rect.top + rect.height / 2));
|
||||
// Same rule as the corpus card: a translucent card lets the screen behind it ghost through.
|
||||
const opaque = !/^rgba\(.*,\s*(0?\.\d+|0)\)$/.test(getComputedStyle(panel).backgroundColor);
|
||||
return {
|
||||
ok: opaque && rect.top >= 60 && rect.bottom <= window.innerHeight && rect.width > 300 && rect.height > 300
|
||||
&& (topmost === panel || panel.contains(topmost)) && panel.scrollHeight <= panel.clientHeight + 1,
|
||||
top: Math.round(rect.top), height: Math.round(rect.height), width: Math.round(rect.width),
|
||||
scroll: panel.scrollHeight, client: panel.clientHeight, opaque,
|
||||
covered: topmost === panel || panel.contains(topmost),
|
||||
};
|
||||
});
|
||||
if (!gateProof.ok) throw new Error(`Calibration card is not fully readable on screen: ${JSON.stringify(gateProof)}`);
|
||||
// The card is drawn from the report; prove the values on it are the ones the harness measured.
|
||||
for (const row of gate.report.models) {
|
||||
if (!gateCard.text.includes(row.macroF1.toFixed(3))) {
|
||||
throw new Error(`the card must show the macro-F1 the harness measured for ${row.model}`);
|
||||
}
|
||||
}
|
||||
if (!gateCard.text.includes('leaks across')) throw new Error('the card must show the refusal the leaked corpus earned');
|
||||
if (!gateCard.text.includes('GO — gate-plumbing-fixture')) throw new Error('the card must show the passing branch it proved');
|
||||
await caption('The calibration harness is a command-line release gate, not a screen in the app — this runs it against committed synthetic predictions', 3400);
|
||||
await caption('Every score on this card is invented to exercise the arithmetic: no model produced them and none of them describes the real classifier', 3600);
|
||||
await caption('Each candidate is scored on test at a threshold chosen on validation only; picking a threshold on the split you report is refused', 3600);
|
||||
await caption('One contributor spread across two splits is refused before anything is scored, and a passing verdict is proven only by a fixture engineered to clear it', 3800);
|
||||
await page.evaluate(() => document.querySelector('#calibration-report')?.remove());
|
||||
|
||||
// Back to the privacy view for the portability scene: the privacy entry lives on the journal view.
|
||||
await tap('[data-view="calendar"]', 550);
|
||||
await tap('[data-view="privacy"]', 600);
|
||||
|
||||
// Issue #35 — a backup is the person's own record, so it carries a version and everything it is
|
||||
// claimed to hold. The recording exports the real file, imports an older version and a newer one,
|
||||
// and reopens a restored log. Fictional sample logs are used throughout; nothing here is real data.
|
||||
const exportDownload = page.waitForEvent('download');
|
||||
await tap('#export', 600);
|
||||
const exportedBackup = JSON.parse(await readFile(await (await exportDownload).path(), 'utf8'));
|
||||
if (exportedBackup.schemaVersion !== 2) throw new Error('the exported backup must declare the current schema version');
|
||||
if (!Array.isArray(exportedBackup.entries) || !Array.isArray(exportedBackup.contributions)) {
|
||||
throw new Error('a backup must carry both the confirmed logs and the contribution receipts');
|
||||
}
|
||||
const exportedReceipt = exportedBackup.contributions.find(receipt => receipt.contributionId === receiptId);
|
||||
if (!exportedReceipt || exportedReceipt.state !== 'withdrawn') {
|
||||
throw new Error('the backup must carry the withdrawn receipt as a tombstone');
|
||||
}
|
||||
if (!(exportedReceipt.artifacts || []).length) throw new Error('the backup tombstone must still name its derived artifacts');
|
||||
await page.locator('[data-portability-notice][data-portability-version="2"]').waitFor();
|
||||
await caption('A backup is versioned, and it carries the withdrawn receipt as a tombstone rather than silence', 3200);
|
||||
|
||||
const fictionalLegacyBackup = JSON.stringify({
|
||||
product: 'Timmy the Talking Turd',
|
||||
schemaVersion: 1,
|
||||
exportedAt: '2026-08-01T09:00:00.000Z',
|
||||
entries: [{
|
||||
id: 'demo-legacy-backup',
|
||||
occurredAt: new Date(Date.now() - 3 * 86400000).toISOString(),
|
||||
bristolType: 3,
|
||||
color: 'brown',
|
||||
urgency: 1,
|
||||
discomfort: 0,
|
||||
note: 'fictional version 1 backup',
|
||||
photoDataUrl: '',
|
||||
symptoms: {},
|
||||
}],
|
||||
});
|
||||
await page.locator('#import').setInputFiles({ name: 'timmy-backup-v1.json', mimeType: 'application/json', buffer: Buffer.from(fictionalLegacyBackup) });
|
||||
await page.locator('[data-portability-notice][data-portability-version="1"]').waitFor();
|
||||
const upgradeNotice = (await page.locator('[data-portability-notice]').innerText()).trim();
|
||||
if (!/upgraded/i.test(upgradeNotice)) throw new Error('an older backup must be reported as upgraded, not silently rewritten');
|
||||
await caption('An older backup is upgraded on import, and the notice says which version the file came from', 3400);
|
||||
|
||||
const ledgerBeforeFuture = await storedLedger();
|
||||
await page.locator('#import').setInputFiles({
|
||||
name: 'timmy-backup-v3.json', mimeType: 'application/json',
|
||||
buffer: Buffer.from(JSON.stringify({ product: 'Timmy the Talking Turd', schemaVersion: 3, entries: [{ id: 'demo-future', bristolType: 7 }] })),
|
||||
});
|
||||
await page.locator('[data-portability-error]').waitFor();
|
||||
if (!/newer version of Timmy/i.test(await page.locator('[data-portability-error]').innerText())) {
|
||||
throw new Error('a backup from a newer Timmy must be refused with an instruction to update');
|
||||
}
|
||||
if ((await storedLedger()).raw !== ledgerBeforeFuture.raw) {
|
||||
throw new Error('a refused import must not change the ledger it would have replaced');
|
||||
}
|
||||
await caption('A file from a newer Timmy is refused with an instruction, and the ledger it would have replaced is untouched', 3400);
|
||||
|
||||
const fictionalAssistedBackup = JSON.stringify({
|
||||
product: 'Timmy the Talking Turd',
|
||||
schemaVersion: 2,
|
||||
exportedAt: new Date().toISOString(),
|
||||
entries: [{
|
||||
id: 'demo-restored-provenance',
|
||||
occurredAt: new Date(Date.now() - 86400000).toISOString(),
|
||||
bristolType: 2,
|
||||
color: 'brown',
|
||||
urgency: 1,
|
||||
discomfort: 0,
|
||||
note: 'fictional restored log',
|
||||
photoDataUrl: '',
|
||||
symptoms: {},
|
||||
provenance: {
|
||||
source: 'ai-corrected',
|
||||
model: 'SmolVLM2-2.2B-Instruct',
|
||||
suggestedBristolType: 4,
|
||||
suggestedColor: 'brown',
|
||||
confirmedAt: new Date(Date.now() - 86400000).toISOString(),
|
||||
},
|
||||
}],
|
||||
contributions: [],
|
||||
});
|
||||
await page.locator('#import').setInputFiles({ name: 'timmy-backup-v2.json', mimeType: 'application/json', buffer: Buffer.from(fictionalAssistedBackup) });
|
||||
await page.locator('[data-portability-notice][data-portability-version="2"]').waitFor();
|
||||
await tap('[data-view="calendar"]', 550);
|
||||
await page.locator('[data-entry-open="demo-restored-provenance"]').waitFor();
|
||||
await tap('[data-entry-open="demo-restored-provenance"]', 550);
|
||||
await page.locator('[data-entry-provenance="ai-corrected"]').waitFor();
|
||||
const provenanceLine = (await page.locator('[data-entry-provenance="ai-corrected"]').innerText()).trim();
|
||||
if (!/corrected/i.test(provenanceLine) || !/SmolVLM2/.test(provenanceLine)) {
|
||||
throw new Error('a restored log must still show that it began as a corrected AI suggestion');
|
||||
}
|
||||
await caption('A restored log still says it began as an AI suggestion you corrected — provenance is part of the record', 3400);
|
||||
await indicate('[data-entry-provenance="ai-corrected"]');
|
||||
await tap('#entry-sheet-close', 450);
|
||||
await tap('[data-view="privacy"]', 550);
|
||||
await page.locator('#delete-all').waitFor();
|
||||
|
||||
await indicate('#delete-all');
|
||||
await tap('#delete-all', 650);
|
||||
await page.getByText(/Local ledger and photos deleted/).waitFor();
|
||||
@@ -485,6 +972,9 @@ if (wiped.raw !== null) throw new Error('delete-all must remove the stored ledge
|
||||
if (await page.evaluate(() => JSON.stringify(localStorage).includes('data:image'))) {
|
||||
throw new Error('no photo bytes may survive delete-all');
|
||||
}
|
||||
if (await page.evaluate(key => localStorage.getItem(key), contributionKey) !== null) {
|
||||
throw new Error('delete-all must clear the contribution receipts as well');
|
||||
}
|
||||
await caption('Delete all local data clears the ledger and every saved photo in this browser; nothing is kept on a server', 2900);
|
||||
await tap('[data-view="home"]', 500);
|
||||
// Issue #37 — a chat answer about the journal must point at the confirmed records behind it. The
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
const ROOT = new URL(self.registration.scope).pathname;
|
||||
const appPath = path => `${ROOT}${String(path).replace(/^\/+/, '')}`;
|
||||
const CACHE_NAMESPACE = `timmy-shell:${ROOT}:`;
|
||||
const CACHE = `${CACHE_NAMESPACE}v5`;
|
||||
const CACHE = `${CACHE_NAMESPACE}v6`;
|
||||
const ASSETS = [
|
||||
'',
|
||||
'index.html',
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// The production abstention policy (issue #32).
|
||||
//
|
||||
// A suggestion is only ever a visual prefill, and this module is the single place that decides when
|
||||
// Timmy is allowed to offer one. Two different questions live here, and they are deliberately
|
||||
// separate:
|
||||
//
|
||||
// 1. Per-photo: is this individual answer good enough to show? Confidence, image quality, and
|
||||
// whether the model actually read a form and a colour.
|
||||
// 2. Per-release: has a candidate cleared the calibration gates on a held-out split, with its
|
||||
// threshold chosen on validation only? Until it has, no prefill may be shipped at all.
|
||||
//
|
||||
// The copy is fixed here so the browser, the corpus, and the server cannot drift into three
|
||||
// different explanations of the same refusal.
|
||||
export const ABSTENTION_COPY = Object.freeze({
|
||||
not_stool: 'The image does not clearly show stool.',
|
||||
low_confidence: 'The image is too uncertain to prefill safely.',
|
||||
unusable_visual_fields: 'The photo does not show a form Timmy can read safely.',
|
||||
low_quality: 'The photo is too blurry or poorly lit to suggest a form safely.',
|
||||
});
|
||||
|
||||
// Bump `version` whenever a limit below changes: a policy change is a behaviour change, and the
|
||||
// release notes have to be able to name which policy a suggestion was produced under.
|
||||
export const ABSTENTION_POLICY = Object.freeze({
|
||||
version: 1,
|
||||
minConfidence: 0.55,
|
||||
// "fair" may prefill; "poor" never does. The model already returns its own quality judgement, so
|
||||
// a confident answer about an unreadable photo is exactly the case this gate exists for.
|
||||
acceptedImageQuality: Object.freeze(['good', 'fair']),
|
||||
});
|
||||
|
||||
export function abstainFor(reason, isStool = true) {
|
||||
if (!Object.prototype.hasOwnProperty.call(ABSTENTION_COPY, reason)) {
|
||||
throw new Error(`unknown abstention reason: ${reason}`);
|
||||
}
|
||||
return {
|
||||
status: 'needs_user_input',
|
||||
isStool,
|
||||
abstainedFor: reason,
|
||||
reason: ABSTENTION_COPY[reason],
|
||||
};
|
||||
}
|
||||
|
||||
export function isLowQuality(imageQuality, policy = ABSTENTION_POLICY) {
|
||||
return !policy.acceptedImageQuality.includes(String(imageQuality || '').toLowerCase());
|
||||
}
|
||||
|
||||
// Release-level gate. The calibration harness already refuses to pick a threshold anywhere but the
|
||||
// validation split; this decides whether a report authorises prefill at all, and names every reason
|
||||
// it does not. A missing or malformed report is a block, never a default-allow.
|
||||
export function evaluatePrefillGate(report) {
|
||||
const blockedBy = [];
|
||||
const verdict = report?.verdict || 'UNKNOWN';
|
||||
if (verdict !== 'GO') blockedBy.push('calibration-verdict-not-go');
|
||||
const models = Array.isArray(report?.models) ? report.models : [];
|
||||
const recommended = models.find(row => row?.model === report?.recommended) || null;
|
||||
if (!recommended) {
|
||||
blockedBy.push('no-recommended-calibration');
|
||||
} else {
|
||||
if (recommended.selectionSplit !== 'validation') blockedBy.push('threshold-not-selected-on-validation');
|
||||
if (recommended.evaluatedSplit !== 'test') blockedBy.push('gate-not-scored-on-held-out-test');
|
||||
if (recommended.gate?.passed !== true) blockedBy.push('candidate-failed-release-gates');
|
||||
}
|
||||
return {
|
||||
prefillAllowed: blockedBy.length === 0,
|
||||
verdict,
|
||||
calibrationModel: recommended?.model || null,
|
||||
blockedBy,
|
||||
};
|
||||
}
|
||||
+8
-9
@@ -1,3 +1,5 @@
|
||||
import { ABSTENTION_POLICY, abstainFor, isLowQuality } from './abstention-policy.js';
|
||||
|
||||
const COLORS = new Set(['brown', 'green', 'yellow', 'pale', 'red', 'black']);
|
||||
const QUALITIES = new Set(['good', 'fair', 'poor']);
|
||||
const MAX_IMAGE_BYTES = 4 * 1024 * 1024;
|
||||
@@ -26,13 +28,13 @@ function asObject(raw) {
|
||||
}
|
||||
}
|
||||
|
||||
export function parseVisionResponse(raw) {
|
||||
export function parseVisionResponse(raw, policy = ABSTENTION_POLICY) {
|
||||
const value = asObject(raw);
|
||||
if (typeof value.isStool !== 'boolean') throw visionResponseError(VISION_FAILURE_CODES.invalidFields, 'Invalid AI response: isStool is required.');
|
||||
const confidence = Number(value.confidence);
|
||||
if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) throw visionResponseError(VISION_FAILURE_CODES.invalidFields, 'Invalid AI response: confidence is required.');
|
||||
if (!value.isStool) return { status: 'needs_user_input', isStool: false, abstainedFor: 'not_stool', reason: 'The image does not clearly show stool.' };
|
||||
if (confidence < 0.55) return { status: 'needs_user_input', isStool: true, abstainedFor: 'low_confidence', reason: 'The image is too uncertain to prefill safely.' };
|
||||
if (!value.isStool) return abstainFor('not_stool', false);
|
||||
if (confidence < policy.minConfidence) return abstainFor('low_confidence');
|
||||
// The request schema lets the model return a null form and color when it cannot read them.
|
||||
// A blank field means the model could not read it, so a schema-valid answer with nothing usable
|
||||
// abstains and nothing is prefilled. A filled-in field is a claim, though: if it sits outside the
|
||||
@@ -47,15 +49,12 @@ export function parseVisionResponse(raw) {
|
||||
throw visionResponseError(VISION_FAILURE_CODES.invalidFields, 'Invalid AI response: visual fields are out of range.');
|
||||
}
|
||||
if (bristolType === null || color === null) {
|
||||
return {
|
||||
status: 'needs_user_input',
|
||||
isStool: true,
|
||||
abstainedFor: 'unusable_visual_fields',
|
||||
reason: 'The photo does not show a form Timmy can read safely.',
|
||||
};
|
||||
return abstainFor('unusable_visual_fields');
|
||||
}
|
||||
const imageQuality = String(value.imageQuality || '').toLowerCase();
|
||||
if (!QUALITIES.has(imageQuality)) throw visionResponseError(VISION_FAILURE_CODES.invalidFields, 'Invalid AI response: visual fields are out of range.');
|
||||
// A confident reading of a photo the model itself calls poor is still not a safe prefill.
|
||||
if (isLowQuality(imageQuality, policy)) return abstainFor('low_quality');
|
||||
return {
|
||||
status: 'suggestion',
|
||||
isStool: true,
|
||||
|
||||
+129
-5
@@ -111,9 +111,32 @@ export function sanitizeEntry(input = {}) {
|
||||
note: String(input.note || '').trim().slice(0, 500),
|
||||
photoDataUrl: typeof input.photoDataUrl === 'string' && input.photoDataUrl.startsWith('data:image/') ? input.photoDataUrl : '',
|
||||
symptoms,
|
||||
provenance: sanitizeProvenance(input.provenance),
|
||||
};
|
||||
}
|
||||
|
||||
// Provenance is the only record of whether a confirmed log began as an AI suggestion. It is kept to
|
||||
// an allowlist so a backup can never smuggle a credential, a prompt, or a raw model reply out of the
|
||||
// device, and it is `null` for logs that never had one — a missing record is not a manual entry.
|
||||
const PROVENANCE_SOURCES = Object.freeze(['ai-confirmed', 'ai-corrected']);
|
||||
|
||||
function sanitizeProvenance(value) {
|
||||
if (!value || typeof value !== 'object') return null;
|
||||
if (!PROVENANCE_SOURCES.includes(value.source)) return null;
|
||||
const provenance = { source: value.source };
|
||||
if (typeof value.model === 'string' && value.model.trim()) provenance.model = value.model.trim().slice(0, 80);
|
||||
const suggestedType = Number(value.suggestedBristolType);
|
||||
if (Number.isInteger(suggestedType) && suggestedType >= 1 && suggestedType <= 7) {
|
||||
provenance.suggestedBristolType = suggestedType;
|
||||
}
|
||||
if (['brown', 'green', 'yellow', 'pale', 'red', 'black'].includes(value.suggestedColor)) {
|
||||
provenance.suggestedColor = value.suggestedColor;
|
||||
}
|
||||
const confirmedAt = value.confirmedAt ? new Date(value.confirmedAt) : null;
|
||||
if (confirmedAt && !Number.isNaN(confirmedAt.valueOf())) provenance.confirmedAt = confirmedAt.toISOString();
|
||||
return provenance;
|
||||
}
|
||||
|
||||
export function photoQualityMessage({ width = 0, height = 0, brightness = 0.5 } = {}) {
|
||||
if (width < 640 || height < 480) return 'Move a little closer or use a higher-resolution photo. The image stays on this device.';
|
||||
if (brightness < 0.12) return 'Add more light before saving. Timmy only checks whether the photo is usable.';
|
||||
@@ -121,19 +144,120 @@ export function photoQualityMessage({ width = 0, height = 0, brightness = 0.5 }
|
||||
return 'Ready for your review. Choose the matching Bristol form yourself; Timmy does not interpret the picture.';
|
||||
}
|
||||
|
||||
export const LEDGER_EXPORT_VERSION = 2;
|
||||
|
||||
export function exportLedger(entries, exportedAt = new Date().toISOString()) {
|
||||
return exportLedgerBackup({ entries, exportedAt });
|
||||
}
|
||||
|
||||
// A backup is written through an allowlist, so only fields this build understands leave the device.
|
||||
// Entries are re-sanitized on the way out as well as in: an unknown key on an entry is not part of
|
||||
// the person's record, and a crafted key must never be laundered through a backup.
|
||||
export function exportLedgerBackup({ entries = [], contributions = [], exportedAt = new Date().toISOString() } = {}) {
|
||||
const list = Array.isArray(entries) ? entries.map(sanitizeEntry) : [];
|
||||
const receipts = (Array.isArray(contributions) ? contributions : []).map(sanitizeContribution).filter(Boolean);
|
||||
return JSON.stringify({
|
||||
product: 'Timmy the Talking Turd',
|
||||
schemaVersion: 1,
|
||||
schemaVersion: LEDGER_EXPORT_VERSION,
|
||||
exportedAt,
|
||||
entries: Array.isArray(entries) ? entries : [],
|
||||
entries: list,
|
||||
contributions: receipts,
|
||||
}, null, 2);
|
||||
}
|
||||
|
||||
const CONTRIBUTION_STATES = Object.freeze(['queued-local', 'withdrawn']);
|
||||
const CONTRIBUTION_ARTIFACTS = Object.freeze(['raw-image', 'confirmed-label']);
|
||||
|
||||
function isoOrNull(value) {
|
||||
if (!value) return null;
|
||||
const parsed = new Date(value);
|
||||
return Number.isNaN(parsed.valueOf()) ? null : parsed.toISOString();
|
||||
}
|
||||
|
||||
export function sanitizeContribution(input = {}) {
|
||||
const contributionId = String(input?.contributionId || '').trim().slice(0, 120);
|
||||
if (!contributionId) return null;
|
||||
const state = CONTRIBUTION_STATES.includes(input.state) ? input.state : 'queued-local';
|
||||
const receipt = {
|
||||
contributionId,
|
||||
entryId: String(input.entryId || '').trim().slice(0, 120),
|
||||
consentVersion: String(input.consentVersion || '').trim().slice(0, 60),
|
||||
consentedAt: isoOrNull(input.consentedAt),
|
||||
labels: {
|
||||
bristolType: Math.min(7, Math.max(1, Number(input.labels?.bristolType) || 4)),
|
||||
color: ['brown', 'green', 'yellow', 'pale', 'red', 'black'].includes(input.labels?.color) ? input.labels.color : 'brown',
|
||||
},
|
||||
state,
|
||||
};
|
||||
const withdrawnAt = isoOrNull(input.withdrawnAt);
|
||||
if (state === 'withdrawn' && withdrawnAt) receipt.withdrawnAt = withdrawnAt;
|
||||
if (state === 'withdrawn') {
|
||||
receipt.artifacts = (Array.isArray(input.artifacts) ? input.artifacts : [])
|
||||
.filter(artifact => CONTRIBUTION_ARTIFACTS.includes(artifact?.artifact))
|
||||
.map(artifact => ({
|
||||
artifact: artifact.artifact,
|
||||
contributionId: String(artifact.contributionId || contributionId).slice(0, 120),
|
||||
state: artifact.state === 'withdrawn' ? 'withdrawn' : 'active',
|
||||
}));
|
||||
}
|
||||
return receipt;
|
||||
}
|
||||
|
||||
export function importLedger(text) {
|
||||
const parsed = JSON.parse(text);
|
||||
if (parsed?.schemaVersion !== 1 || !Array.isArray(parsed.entries)) throw new Error('This is not a supported Timmy export.');
|
||||
return parsed.entries.map(sanitizeEntry);
|
||||
return importLedgerBackup(text).entries;
|
||||
}
|
||||
|
||||
// A backup can be older than this build. Version 1 files record no provenance for their entries, so
|
||||
// the migration leaves that field empty instead of guessing a source, and the caller is told which
|
||||
// version the file came from.
|
||||
export const LEDGER_IMPORT_LIMITS = Object.freeze({ maxTextLength: 16 * 1024 * 1024, maxEntries: 5000 });
|
||||
|
||||
export function importLedgerBackup(text) {
|
||||
const raw = String(text ?? '');
|
||||
if (raw.length > LEDGER_IMPORT_LIMITS.maxTextLength) {
|
||||
throw new Error('That backup is too large for Timmy to read safely. Nothing was imported.');
|
||||
}
|
||||
let parsed;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
throw new Error('That file is not readable JSON, so nothing was imported.');
|
||||
}
|
||||
if (!parsed || typeof parsed !== 'object' || !Array.isArray(parsed.entries)) {
|
||||
throw new Error('This is not a supported Timmy backup.');
|
||||
}
|
||||
const fromVersion = Number(parsed.schemaVersion);
|
||||
if (Number.isInteger(fromVersion) && fromVersion > LEDGER_EXPORT_VERSION) {
|
||||
throw new Error(`This backup was written by a newer version of Timmy (version ${fromVersion}). Update Timmy before importing it; nothing was imported.`);
|
||||
}
|
||||
if (!Number.isInteger(fromVersion) || fromVersion < 1) {
|
||||
throw new Error('This is not a supported Timmy backup.');
|
||||
}
|
||||
if (parsed.entries.length > LEDGER_IMPORT_LIMITS.maxEntries) {
|
||||
throw new Error('That backup holds more logs than Timmy will import at once. Nothing was imported.');
|
||||
}
|
||||
const entries = parsed.entries.map(sanitizeEntry);
|
||||
const rawContributions = Array.isArray(parsed.contributions) ? parsed.contributions : [];
|
||||
const contributions = rawContributions.map(sanitizeContribution).filter(Boolean);
|
||||
const migrated = fromVersion < LEDGER_EXPORT_VERSION;
|
||||
// The caller already says what it restored, so this notice only reports the upgrade. Restating the
|
||||
// restored count here made the on-screen notice read as two stacked sentences saying one thing.
|
||||
const notices = migrated
|
||||
? [`Upgraded from version ${fromVersion} to version ${LEDGER_EXPORT_VERSION}; fields the old file did not record are left empty.`]
|
||||
: [];
|
||||
// Skipping an unreadable receipt silently would tell someone their opt-in vanished. Say it.
|
||||
const skipped = rawContributions.length - contributions.length;
|
||||
if (skipped > 0) {
|
||||
notices.push(`${skipped} training contribution ${skipped === 1 ? 'receipt' : 'receipts'} could not be read and ${skipped === 1 ? 'was' : 'were'} skipped.`);
|
||||
}
|
||||
return {
|
||||
entries,
|
||||
contributions,
|
||||
fromVersion,
|
||||
migrated,
|
||||
notices,
|
||||
exportedAt: typeof parsed.exportedAt === 'string' ? parsed.exportedAt : null,
|
||||
};
|
||||
}
|
||||
|
||||
export const urgentSymptomKeys = Object.freeze([...URGENT_KEYS]);
|
||||
@@ -0,0 +1,103 @@
|
||||
// Issue #24 — contributing a confirmed photo to Timmy's training corpus is a separate decision from
|
||||
// saving a log. A journal save records nothing here; a contribution exists only after someone reads
|
||||
// the consent terms and accepts them, and it exists as a receipt they can point a withdrawal at.
|
||||
// Nothing is transmitted: this build queues the receipt on the device so the corpus pipeline (#25)
|
||||
// has something auditable to consume later.
|
||||
export const TRAINING_CONSENT_VERSION = 'training-consent-v1';
|
||||
|
||||
const IMAGE_DATA_URL = /^data:image\/[a-z0-9.+-]+(?:;base64)?,[A-Za-z0-9+/=%.-]+$/i;
|
||||
const CONTRIBUTED_ARTIFACTS = ['raw-image', 'confirmed-label'];
|
||||
|
||||
export function consentTerms() {
|
||||
return {
|
||||
version: TRAINING_CONSENT_VERSION,
|
||||
includes: [
|
||||
'The saved photo bytes for this one log',
|
||||
'The confirmed Bristol form, broad color, and image quality',
|
||||
],
|
||||
excludes: [
|
||||
'Your note text',
|
||||
'Your symptom answers',
|
||||
'Urgency and discomfort ratings',
|
||||
'Anything identifying you or this device',
|
||||
],
|
||||
transport: 'Queued on this device only — nothing is uploaded in this build.',
|
||||
};
|
||||
}
|
||||
|
||||
export function defaultContributionState() {
|
||||
return { optedIn: false, receipt: null };
|
||||
}
|
||||
|
||||
export function contributionEligibility(entry) {
|
||||
if (!entry || !entry.id) return { eligible: false, reason: 'This log has no identifier to attach a receipt to.' };
|
||||
if (!IMAGE_DATA_URL.test(String(entry.photoDataUrl || ''))) {
|
||||
return { eligible: false, reason: 'This log has no saved photo to contribute.' };
|
||||
}
|
||||
if (!Number.isInteger(entry.bristolType) || entry.bristolType < 1 || entry.bristolType > 7) {
|
||||
return { eligible: false, reason: 'This log has no confirmed Bristol form to contribute.' };
|
||||
}
|
||||
if (!entry.color) return { eligible: false, reason: 'This log has no confirmed color to contribute.' };
|
||||
return { eligible: true, reason: '' };
|
||||
}
|
||||
|
||||
export function receiptPreview(entry, { contributionId, consentedAt } = {}) {
|
||||
if (!contributionId) throw new Error('A contribution receipt needs its own identifier.');
|
||||
if (!consentedAt) throw new Error('A contribution receipt needs the moment of consent.');
|
||||
const eligibility = contributionEligibility(entry);
|
||||
if (!eligibility.eligible) throw new Error(eligibility.reason);
|
||||
return {
|
||||
contributionId,
|
||||
entryId: entry.id,
|
||||
consentVersion: TRAINING_CONSENT_VERSION,
|
||||
consentedAt,
|
||||
labels: { bristolType: entry.bristolType, color: entry.color },
|
||||
state: 'queued-local',
|
||||
};
|
||||
}
|
||||
|
||||
export function contributionForEntry(contributions, entryId) {
|
||||
if (!Array.isArray(contributions)) return null;
|
||||
return contributions.find(item => item.entryId === entryId && item.state !== 'withdrawn') || null;
|
||||
}
|
||||
|
||||
export function addContribution(contributions, receipt) {
|
||||
const list = Array.isArray(contributions) ? contributions : [];
|
||||
if (!receipt || receipt.consentVersion !== TRAINING_CONSENT_VERSION) {
|
||||
throw new Error('A contribution needs the current consent version.');
|
||||
}
|
||||
if (contributionForEntry(list, receipt.entryId)) return list;
|
||||
return [...list, receipt];
|
||||
}
|
||||
|
||||
export function derivedArtifacts(receipt) {
|
||||
const state = receipt?.state === 'withdrawn' ? 'withdrawn' : 'active';
|
||||
return CONTRIBUTED_ARTIFACTS.map(artifact => ({
|
||||
artifact,
|
||||
contributionId: receipt?.contributionId || '',
|
||||
state,
|
||||
}));
|
||||
}
|
||||
|
||||
export function withdrawContribution(contributions, contributionId, withdrawnAt) {
|
||||
const list = Array.isArray(contributions) ? contributions : [];
|
||||
const receipt = list.find(item => item.contributionId === contributionId);
|
||||
if (!receipt) return { contributions: list, tombstone: null };
|
||||
const withdrawn = {
|
||||
...receipt,
|
||||
state: 'withdrawn',
|
||||
withdrawnAt,
|
||||
artifacts: derivedArtifacts({ ...receipt, state: 'withdrawn' }),
|
||||
};
|
||||
return {
|
||||
contributions: list.map(item => (item.contributionId === contributionId ? withdrawn : item)),
|
||||
tombstone: {
|
||||
contributionId: receipt.contributionId,
|
||||
entryId: receipt.entryId,
|
||||
consentVersion: receipt.consentVersion,
|
||||
consentedAt: receipt.consentedAt,
|
||||
withdrawnAt,
|
||||
artifacts: withdrawn.artifacts,
|
||||
},
|
||||
};
|
||||
}
|
||||
+16
@@ -59,3 +59,19 @@ main{display:block}.eyebrow{font-size:11px;font-weight:800;letter-spacing:.1em;t
|
||||
.chat-citation{min-height:32px;padding:5px 10px;border:1px solid #bcdfd4;border-radius:999px;background:var(--surface);color:var(--teal);font-weight:800;font-size:11px;cursor:pointer}
|
||||
.chat-citation:active{background:#e9f5f0}
|
||||
.chat-unverified{margin:9px 0 0;padding-top:8px;border-top:1px solid rgba(55,40,31,.09);font-size:11px;line-height:1.4;font-weight:700;color:#8a5a12}
|
||||
|
||||
/* Issue #24 — an opt-in receipt and its tombstone must read as separate facts, not one run-on line. */
|
||||
.contribution-list{display:flex;flex-direction:column;margin-top:10px}
|
||||
.training-consent h3{font-size:13px;margin:16px 0 2px}
|
||||
.contribution-row{display:flex;align-items:center;justify-content:space-between;gap:12px;padding:11px 0;border-top:1px solid var(--line)}
|
||||
.contribution-row:first-child{border-top:0}
|
||||
.contribution-row>div{display:flex;flex-direction:column;gap:3px;min-width:0}
|
||||
.contribution-row strong{font-size:13px;line-height:1.3}
|
||||
.contribution-row span{font-size:11px;line-height:1.4;color:var(--muted);overflow-wrap:anywhere}
|
||||
.contribution-row .btn{min-height:40px;padding:0 14px;font-size:12px;flex:0 0 auto}
|
||||
.contribution-row.withdrawn strong{color:var(--muted)}
|
||||
|
||||
/* Issue #35 — a backup says which version it came from, and a refused file says so in place. */
|
||||
.portability-notice{margin:0 0 12px;padding:12px 14px;border-radius:15px;font-size:12.5px;line-height:1.45;font-weight:700;background:var(--teal-soft);color:#155c4f;border:1px solid #bcdfd4}
|
||||
.portability-notice.error{background:var(--red-soft);border-color:#eec1ba;color:#75261f}
|
||||
.entry-provenance{margin:10px 0 0;padding-top:9px;border-top:1px solid rgba(55,40,31,.09)}
|
||||
@@ -0,0 +1,117 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { mkdtemp, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { promisify } from 'node:util';
|
||||
import { ABSTENTION_COPY, ABSTENTION_POLICY, abstainFor, evaluatePrefillGate } from '../src/abstention-policy.js';
|
||||
|
||||
test('every abstention reason has one fixed piece of copy the user sees', () => {
|
||||
assert.equal(abstainFor('not_stool').reason, 'The image does not clearly show stool.');
|
||||
assert.equal(abstainFor('low_confidence').reason, 'The image is too uncertain to prefill safely.');
|
||||
assert.equal(abstainFor('unusable_visual_fields').reason, 'The photo does not show a form Timmy can read safely.');
|
||||
assert.equal(abstainFor('low_quality').reason, 'The photo is too blurry or poorly lit to suggest a form safely.');
|
||||
assert.deepEqual(abstainFor('low_quality'), {
|
||||
status: 'needs_user_input',
|
||||
isStool: true,
|
||||
abstainedFor: 'low_quality',
|
||||
reason: ABSTENTION_COPY.low_quality,
|
||||
});
|
||||
assert.equal(abstainFor('not_stool', false).isStool, false);
|
||||
assert.throws(() => abstainFor('looked_wrong'), /unknown abstention reason/);
|
||||
});
|
||||
|
||||
test('the policy states the confidence floor and the quality levels that may prefill', () => {
|
||||
assert.ok(Number.isInteger(ABSTENTION_POLICY.version));
|
||||
assert.equal(ABSTENTION_POLICY.minConfidence, 0.55);
|
||||
assert.deepEqual([...ABSTENTION_POLICY.acceptedImageQuality], ['good', 'fair']);
|
||||
assert.equal(ABSTENTION_POLICY.acceptedImageQuality.includes('poor'), false);
|
||||
assert.equal(Object.isFrozen(ABSTENTION_POLICY), true, 'the shipped policy must not be mutated at runtime');
|
||||
});
|
||||
|
||||
test('prefill is authorised only by a GO report whose threshold was selected on validation', () => {
|
||||
const passing = {
|
||||
verdict: 'GO',
|
||||
recommended: 'specialist-candidate',
|
||||
models: [{
|
||||
model: 'specialist-candidate',
|
||||
selectionSplit: 'validation',
|
||||
evaluatedSplit: 'test',
|
||||
gate: { passed: true, failed: [] },
|
||||
}],
|
||||
};
|
||||
assert.deepEqual(evaluatePrefillGate(passing), {
|
||||
prefillAllowed: true,
|
||||
verdict: 'GO',
|
||||
calibrationModel: 'specialist-candidate',
|
||||
blockedBy: [],
|
||||
});
|
||||
|
||||
const blocked = evaluatePrefillGate({
|
||||
verdict: 'BLOCKED',
|
||||
recommended: null,
|
||||
models: [{ model: 'specialist-candidate', selectionSplit: 'validation', evaluatedSplit: 'test', gate: { passed: false, failed: ['precisionAtCoverage'] } }],
|
||||
});
|
||||
assert.equal(blocked.prefillAllowed, false);
|
||||
assert.deepEqual(blocked.blockedBy, ['calibration-verdict-not-go', 'no-recommended-calibration']);
|
||||
|
||||
const selectedOnTest = evaluatePrefillGate({
|
||||
verdict: 'GO',
|
||||
recommended: 'opportunist',
|
||||
models: [{ model: 'opportunist', selectionSplit: 'test', evaluatedSplit: 'test', gate: { passed: true, failed: [] } }],
|
||||
});
|
||||
assert.equal(selectedOnTest.prefillAllowed, false, 'a threshold chosen on the split it is scored on is not evidence');
|
||||
assert.deepEqual(selectedOnTest.blockedBy, ['threshold-not-selected-on-validation']);
|
||||
|
||||
const noReport = evaluatePrefillGate(undefined);
|
||||
assert.equal(noReport.prefillAllowed, false);
|
||||
assert.match(noReport.blockedBy.join(','), /calibration-verdict-not-go/);
|
||||
});
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
const receiptScript = fileURLToPath(new URL('../scripts/abstention_policy_receipt.mjs', import.meta.url));
|
||||
|
||||
async function runReceipt(report) {
|
||||
const directory = await mkdtemp(join(tmpdir(), 'timmy-policy-'));
|
||||
const reportPath = join(directory, 'calibration-report.json');
|
||||
await writeFile(reportPath, JSON.stringify(report));
|
||||
try {
|
||||
const { stdout } = await execFileAsync(process.execPath, [receiptScript, '--report', reportPath]);
|
||||
return { code: 0, stdout };
|
||||
} catch (error) {
|
||||
return { code: error.code, stdout: error.stdout || '', stderr: error.stderr || '' };
|
||||
}
|
||||
}
|
||||
|
||||
test('the receipt refuses to authorise prefill without a passing held-out calibration', async () => {
|
||||
const blocked = await runReceipt({
|
||||
verdict: 'BLOCKED',
|
||||
recommended: null,
|
||||
models: [{ model: 'specialist-candidate', selectionSplit: 'validation', evaluatedSplit: 'test', gate: { passed: false, failed: ['precisionAtCoverage'] } }],
|
||||
});
|
||||
assert.equal(blocked.code, 3, 'a blocked calibration must exit non-zero instead of shipping prefill');
|
||||
assert.match(blocked.stdout, /prefill: BLOCKED/);
|
||||
assert.match(blocked.stdout, /calibration-verdict-not-go/);
|
||||
assert.match(blocked.stdout, /policy: version 1 · confidence ≥ 0\.55 · quality good\|fair/);
|
||||
|
||||
const passing = await runReceipt({
|
||||
verdict: 'GO',
|
||||
recommended: 'specialist-candidate',
|
||||
models: [{ model: 'specialist-candidate', selectionSplit: 'validation', evaluatedSplit: 'test', gate: { passed: true, failed: [] } }],
|
||||
});
|
||||
assert.equal(passing.code, 0);
|
||||
assert.match(passing.stdout, /prefill: AUTHORISED for specialist-candidate/);
|
||||
});
|
||||
|
||||
test('the receipt treats a missing report as a block and never as a default allow', async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), 'timmy-policy-'));
|
||||
try {
|
||||
await execFileAsync(process.execPath, [receiptScript, '--report', join(directory, 'absent.json')]);
|
||||
assert.fail('a missing calibration report must not exit 0');
|
||||
} catch (error) {
|
||||
assert.equal(error.code, 2);
|
||||
assert.match(String(error.stderr), /could not read|absent/i);
|
||||
}
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { ABSTENTION_COPY, ABSTENTION_POLICY } from '../src/abstention-policy.js';
|
||||
|
||||
import {
|
||||
buildVisionRequest,
|
||||
@@ -131,3 +132,39 @@ test('abstains instead of failing when a schema-valid response carries no readab
|
||||
reason: 'The photo does not show a form Timmy can read safely.',
|
||||
});
|
||||
});
|
||||
|
||||
test('a confident answer about a low-quality photo still abstains', () => {
|
||||
// Issue #32: confidence is not the only gate. A blurry or badly lit photo can produce a
|
||||
// confident-looking form and colour, and prefilling it would show the user a claim the photo
|
||||
// cannot support. Quality is part of the policy, so this abstains instead of prefilling.
|
||||
assert.deepEqual(parseVisionResponse({
|
||||
isStool: true,
|
||||
bristolType: 4,
|
||||
color: 'brown',
|
||||
confidence: 0.94,
|
||||
imageQuality: 'poor',
|
||||
observations: 'Form barely visible through motion blur.',
|
||||
}), {
|
||||
status: 'needs_user_input',
|
||||
isStool: true,
|
||||
abstainedFor: 'low_quality',
|
||||
reason: 'The photo is too blurry or poorly lit to suggest a form safely.',
|
||||
});
|
||||
});
|
||||
|
||||
test('the parse path reads the shipped policy instead of hardcoding its own limits', () => {
|
||||
// Issue #32: policy numbers and copy live in one module. A caller may tighten the policy for a
|
||||
// release without editing the parser, and a photo that the default policy would accept has to
|
||||
// abstain under the stricter one — otherwise the parser is carrying its own hidden policy.
|
||||
const fields = { isStool: true, bristolType: 4, color: 'brown', confidence: 0.8, imageQuality: 'fair', observations: 'Form visible but soft.' };
|
||||
assert.equal(parseVisionResponse(fields).status, 'suggestion', 'fair quality may prefill under the shipped policy');
|
||||
const strict = { ...ABSTENTION_POLICY, minConfidence: 0.9, acceptedImageQuality: ['good'] };
|
||||
assert.equal(parseVisionResponse(fields, strict).abstainedFor, 'low_confidence');
|
||||
assert.equal(parseVisionResponse({ ...fields, confidence: 0.95 }, strict).abstainedFor, 'low_quality');
|
||||
assert.equal(parseVisionResponse({ ...fields, confidence: 0.95, imageQuality: 'good' }, strict).status, 'suggestion');
|
||||
assert.equal(
|
||||
parseVisionResponse({ ...fields, imageQuality: 'poor' }).reason,
|
||||
ABSTENTION_COPY.low_quality,
|
||||
'the refusal copy must come from the policy module, not a second copy in the parser',
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,306 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Tests for the contributor-held-out evaluation harness.
|
||||
|
||||
Issue #29 asks for the authoritative comparison and release-gate harness: macro-F1, per-class
|
||||
precision/recall, a confusion matrix, calibration error, coverage, and precision-at-coverage, over
|
||||
splits that are contributor-held-out and reproducible, comparing candidate models.
|
||||
|
||||
Every fixture in this file is synthetic: fictional pseudonymous subject keys and invented
|
||||
prediction scores. No real photo, label, or patient record is read or written.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
HARNESS = ROOT / "scripts" / "calibration_report.py"
|
||||
|
||||
|
||||
def load_harness():
|
||||
spec = importlib.util.spec_from_file_location("calibration_report", HARNESS)
|
||||
assert spec and spec.loader, "scripts/calibration_report.py must exist"
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
harness = load_harness()
|
||||
|
||||
|
||||
def record(subject_key: str, truth: int, predicted: int | None, confidence: float, *, split: str | None = None, model: str = "frozen-encoder") -> dict:
|
||||
return {
|
||||
"model": model,
|
||||
"subjectKey": subject_key,
|
||||
"split": split or harness.expected_split(subject_key),
|
||||
"truth": {"bristolType": truth},
|
||||
"prediction": None if predicted is None else {"bristolType": predicted, "confidence": confidence},
|
||||
}
|
||||
|
||||
|
||||
class ComparisonTests(unittest.TestCase):
|
||||
def models(self):
|
||||
return {
|
||||
"specialist-candidate": [
|
||||
record("cd00000000000001", 4, 4, 1.0),
|
||||
record("ce00000000000002", 3, 3, 1.0),
|
||||
record("e600000000000001", 4, 4, 1.0),
|
||||
record("e700000000000002", 6, 6, 1.0),
|
||||
],
|
||||
"vlm-bootstrap": [
|
||||
record("d000000000000004", 4, 4, 1.0),
|
||||
record("e600000000000003", 4, 2, 1.0),
|
||||
record("e700000000000004", 6, 6, 1.0),
|
||||
],
|
||||
}
|
||||
|
||||
def test_measures_each_candidate_on_test_at_a_threshold_chosen_on_validation(self):
|
||||
verdict = harness.compare(self.models())
|
||||
self.assertEqual([row["model"] for row in verdict["models"]], ["specialist-candidate", "vlm-bootstrap"])
|
||||
specialist = verdict["models"][0]
|
||||
self.assertEqual(specialist["selectionSplit"], "validation")
|
||||
self.assertEqual(specialist["threshold"], 0.0)
|
||||
self.assertEqual(specialist["evaluatedSplit"], "test")
|
||||
self.assertAlmostEqual(specialist["macroF1"], 1.0, places=6)
|
||||
self.assertTrue(specialist["gate"]["passed"])
|
||||
self.assertEqual(specialist["gate"]["failed"], [])
|
||||
self.assertEqual(verdict["recommended"], "specialist-candidate")
|
||||
self.assertEqual(verdict["verdict"], "GO")
|
||||
|
||||
def test_a_candidate_that_is_only_half_right_fails_the_macro_f1_precision_and_calibration_gates(self):
|
||||
vlm = next(row for row in harness.compare(self.models())["models"] if row["model"] == "vlm-bootstrap")
|
||||
self.assertAlmostEqual(vlm["precisionAtCoverage"], 0.5, places=6)
|
||||
self.assertFalse(vlm["gate"]["passed"])
|
||||
self.assertEqual(vlm["gate"]["failed"], ["macroF1", "precisionAtCoverage", "ece"])
|
||||
|
||||
def test_blocks_the_release_when_no_candidate_clears_every_gate(self):
|
||||
models = {"vlm-bootstrap": self.models()["vlm-bootstrap"]}
|
||||
verdict = harness.compare(models)
|
||||
self.assertEqual(verdict["verdict"], "BLOCKED")
|
||||
self.assertIsNone(verdict["recommended"])
|
||||
self.assertEqual(verdict["gates"], {"macroF1": 0.80, "precisionAtCoverage": 0.90, "ece": 0.05})
|
||||
|
||||
def test_gate_limits_are_inclusive_so_a_candidate_exactly_on_the_line_passes(self):
|
||||
records = [
|
||||
record("cd00000000000001", 4, 4, 0.9),
|
||||
record("e600000000000001", 4, 4, 0.9),
|
||||
record("e700000000000002", 3, 2, 0.9),
|
||||
]
|
||||
verdict = harness.compare({"frozen-encoder": records}, gates={"macroF1": 0.0, "precisionAtCoverage": 0.5, "ece": 1.0})
|
||||
row = verdict["models"][0]
|
||||
self.assertAlmostEqual(row["precisionAtCoverage"], 0.5, places=6)
|
||||
self.assertTrue(row["gate"]["passed"], row["gate"])
|
||||
|
||||
def test_refuses_a_candidate_with_no_held_out_predictions(self):
|
||||
with self.assertRaisesRegex(harness.EvaluationError, "no test-split predictions"):
|
||||
harness.compare({"frozen-encoder": [record("cd00000000000001", 4, 4, 1.0)]})
|
||||
|
||||
|
||||
class CliTests(unittest.TestCase):
|
||||
def write_predictions(self, directory: Path, name: str, rows: list[dict]) -> Path:
|
||||
path = directory / name
|
||||
path.write_text("\n".join(json.dumps(row) for row in rows) + "\n", encoding="utf-8")
|
||||
return path
|
||||
|
||||
def run_cli(self, *args: str) -> subprocess.CompletedProcess:
|
||||
return subprocess.run(
|
||||
[sys.executable, str(HARNESS), *args], cwd=ROOT, capture_output=True, text=True
|
||||
)
|
||||
|
||||
def test_reports_a_go_with_the_candidate_table_and_writes_the_json_report(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
directory = Path(raw)
|
||||
predictions = self.write_predictions(directory, "predictions.jsonl", [
|
||||
{**record("cd00000000000001", 4, 4, 1.0), "model": "specialist-candidate"},
|
||||
{**record("e600000000000001", 4, 4, 1.0), "model": "specialist-candidate"},
|
||||
{**record("e700000000000002", 6, 6, 1.0), "model": "specialist-candidate"},
|
||||
])
|
||||
output = directory / "report.json"
|
||||
run = self.run_cli("--predictions", str(predictions), "--json", str(output), "--min-coverage", "0.5")
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
self.assertIn("verdict: GO", run.stdout)
|
||||
self.assertIn("specialist-candidate", run.stdout)
|
||||
self.assertIn("macroF1", run.stdout)
|
||||
report = json.loads(output.read_text(encoding="utf-8"))
|
||||
self.assertEqual(report["verdict"], "GO")
|
||||
self.assertEqual(report["recommended"], "specialist-candidate")
|
||||
self.assertEqual(report["models"][0]["evaluatedSplit"], "test")
|
||||
self.assertEqual(report["models"][0]["selectionSplit"], "validation")
|
||||
|
||||
def test_exits_three_when_the_release_gate_blocks(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
directory = Path(raw)
|
||||
predictions = self.write_predictions(directory, "predictions.jsonl", [
|
||||
{**record("cd00000000000001", 4, 3, 1.0), "model": "vlm-bootstrap"},
|
||||
{**record("e600000000000001", 4, 2, 1.0), "model": "vlm-bootstrap"},
|
||||
])
|
||||
run = self.run_cli("--predictions", str(predictions))
|
||||
self.assertEqual(run.returncode, 3, run.stderr)
|
||||
self.assertIn("verdict: BLOCKED", run.stdout)
|
||||
|
||||
|
||||
def test_refuses_an_out_of_range_label_instead_of_scoring_it(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
directory = Path(raw)
|
||||
output = directory / "report.json"
|
||||
bad_truth = self.write_predictions(directory, "truth.jsonl", [
|
||||
{**record("cd00000000000001", 9, 9, 1.0), "model": "frozen-encoder"},
|
||||
])
|
||||
run = self.run_cli("--predictions", str(bad_truth), "--json", str(output))
|
||||
self.assertEqual(run.returncode, 2)
|
||||
self.assertIn("outside Bristol 1-7", run.stderr)
|
||||
self.assertFalse(output.exists(), "a refused run must not leave a report behind")
|
||||
bad_prediction = self.write_predictions(directory, "prediction.jsonl", [
|
||||
{**record("e600000000000001", 4, 9, 1.0), "model": "frozen-encoder"},
|
||||
])
|
||||
run = self.run_cli("--predictions", str(bad_prediction))
|
||||
self.assertEqual(run.returncode, 2)
|
||||
self.assertIn("predicted bristolType must be 1-7", run.stderr)
|
||||
|
||||
def test_refuses_a_corpus_that_leaks_a_contributor_across_splits(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
directory = Path(raw)
|
||||
predictions = self.write_predictions(directory, "predictions.jsonl", [
|
||||
{**record("cd00000000000001", 4, 4, 1.0, split="train"), "model": "frozen-encoder"},
|
||||
{**record("cd00000000000001", 4, 4, 1.0, split="validation"), "model": "frozen-encoder"},
|
||||
{**record("e600000000000001", 4, 4, 1.0), "model": "frozen-encoder"},
|
||||
])
|
||||
run = self.run_cli("--predictions", str(predictions))
|
||||
self.assertEqual(run.returncode, 2)
|
||||
self.assertIn("leaks across", run.stderr)
|
||||
|
||||
|
||||
class ThresholdSelectionTests(unittest.TestCase):
|
||||
def validation_records(self):
|
||||
# 0xcd–0xe5 bucket to validation under the ingestion rule. Confident answers are right and
|
||||
# the least confident one is wrong, which is what a threshold is supposed to exploit.
|
||||
return [
|
||||
record("cd00000000000001", 4, 4, 0.90),
|
||||
record("ce00000000000002", 4, 4, 0.80),
|
||||
record("cf00000000000003", 3, 3, 0.60),
|
||||
record("d000000000000004", 7, 4, 0.40),
|
||||
]
|
||||
|
||||
def test_picks_the_most_precise_threshold_that_still_clears_the_coverage_floor(self):
|
||||
records = self.validation_records()
|
||||
# At a 0.5 floor, 0.60 and 0.80 are both perfect; the wider cut wins, so 0.60.
|
||||
floored = harness.select_threshold(records, min_coverage=0.5)
|
||||
self.assertEqual(floored["threshold"], 0.6)
|
||||
self.assertEqual(floored["counts"], {"total": 4, "accepted": 3, "abstained": 1})
|
||||
self.assertAlmostEqual(floored["precisionAtCoverage"], 1.0, places=6)
|
||||
self.assertAlmostEqual(floored["coverage"], 0.75, places=6)
|
||||
self.assertEqual(floored["selectionSplit"], "validation")
|
||||
# A 0.8 floor forbids that cut, so the harness falls back to the widest honest threshold
|
||||
# instead of silently returning a threshold that violates the floor.
|
||||
strict = harness.select_threshold(records, min_coverage=0.8)
|
||||
self.assertEqual(strict["threshold"], 0.0)
|
||||
self.assertAlmostEqual(strict["precisionAtCoverage"], 0.75, places=6)
|
||||
|
||||
def test_refuses_an_unreachable_coverage_floor(self):
|
||||
with self.assertRaisesRegex(harness.EvaluationError, "coverage floor"):
|
||||
harness.select_threshold(self.validation_records(), min_coverage=1.5)
|
||||
|
||||
def test_refuses_to_choose_a_threshold_on_test_or_train_data(self):
|
||||
test_only = [record("e700000000000001", 4, 4, 0.9)]
|
||||
with self.assertRaisesRegex(harness.EvaluationError, "validation split only"):
|
||||
harness.select_threshold(test_only, min_coverage=0.5)
|
||||
train_mixed = [record("aa00000000000001", 4, 4, 0.9), record("cd00000000000001", 3, 3, 0.8)]
|
||||
with self.assertRaisesRegex(harness.EvaluationError, "validation split only"):
|
||||
harness.select_threshold(train_mixed, min_coverage=0.5)
|
||||
|
||||
|
||||
class ContributorHeldOutTests(unittest.TestCase):
|
||||
def test_refuses_a_split_that_cannot_be_derived_from_the_subject_key(self):
|
||||
# 0xaa = 170, below the train/validation edge, so this subject can only ever be train.
|
||||
records = [record("aa00000000000001", 4, 4, 0.9, split="test")]
|
||||
with self.assertRaisesRegex(harness.EvaluationError, "not reproducible"):
|
||||
harness.assert_contributor_held_out(records)
|
||||
|
||||
|
||||
def test_refuses_a_contributor_whose_photos_span_two_splits(self):
|
||||
records = [
|
||||
record("aa00000000000001", 4, 4, 0.9, split="train"),
|
||||
record("aa00000000000001", 3, 3, 0.8, split="validation"),
|
||||
]
|
||||
with self.assertRaisesRegex(harness.EvaluationError, "leaks across"):
|
||||
harness.assert_contributor_held_out(records)
|
||||
|
||||
def test_allows_one_contributor_to_supply_several_photos_in_the_same_split(self):
|
||||
records = [
|
||||
record("aa00000000000001", 4, 4, 0.9),
|
||||
record("aa00000000000001", 3, 3, 0.8),
|
||||
]
|
||||
harness.assert_contributor_held_out(records) # several photos, one person, one split: fine
|
||||
|
||||
|
||||
class CalibrationTests(unittest.TestCase):
|
||||
def test_calibration_error_weights_each_confidence_bin_by_its_share_of_accepted_suggestions(self):
|
||||
records = [
|
||||
record("aa00000000000001", 4, 4, 0.95),
|
||||
record("ab00000000000002", 4, 4, 0.90),
|
||||
record("ac00000000000003", 3, 3, 0.55),
|
||||
record("ad00000000000004", 7, 7, 0.70),
|
||||
]
|
||||
# Bins [0.9,1.0) hold both 0.95 and 0.90: mean confidence 0.925, accuracy 1.0, gap 0.075.
|
||||
self.assertAlmostEqual(harness.evaluate(records, threshold=0.0)["ece"], 0.225, places=6)
|
||||
# Raising the threshold to 0.6 drops the 0.55 sample, so it can no longer pull calibration.
|
||||
self.assertAlmostEqual(harness.evaluate(records, threshold=0.6)["ece"], 0.15, places=6)
|
||||
|
||||
|
||||
class AbstentionTests(unittest.TestCase):
|
||||
def test_low_confidence_and_explicit_abstentions_shrink_coverage_but_never_count_as_errors(self):
|
||||
records = [
|
||||
record("aa00000000000001", 4, 4, 0.90),
|
||||
record("ab00000000000002", 4, 3, 0.55),
|
||||
record("ac00000000000003", 3, 3, 0.40),
|
||||
record("ad00000000000004", 7, None, 0.0),
|
||||
]
|
||||
report = harness.evaluate(records, threshold=0.6)
|
||||
self.assertEqual(report["counts"], {"total": 4, "accepted": 1, "abstained": 3})
|
||||
self.assertAlmostEqual(report["coverage"], 0.25, places=6)
|
||||
self.assertAlmostEqual(report["precisionAtCoverage"], 1.0, places=6)
|
||||
# Only the accepted suggestion is scored; the class that abstained away is named, not hidden.
|
||||
self.assertEqual(sorted(report["perClass"]), [4])
|
||||
self.assertAlmostEqual(report["macroF1"], 1.0, places=6)
|
||||
self.assertEqual(report["excludedClasses"], [3, 7])
|
||||
self.assertEqual(report["abstentions"], {"low-confidence": 2, "no-suggestion": 1})
|
||||
|
||||
def test_a_threshold_above_every_confidence_abstains_on_everything_without_crashing(self):
|
||||
records = [
|
||||
record("aa00000000000001", 4, 4, 0.30),
|
||||
record("ab00000000000002", 2, 2, 0.50),
|
||||
]
|
||||
report = harness.evaluate(records, threshold=0.95)
|
||||
self.assertEqual(report["counts"], {"total": 2, "accepted": 0, "abstained": 2})
|
||||
self.assertAlmostEqual(report["coverage"], 0.0, places=6)
|
||||
self.assertAlmostEqual(report["macroF1"], 0.0, places=6)
|
||||
self.assertEqual(report["perClass"], {})
|
||||
|
||||
|
||||
class MacroF1Tests(unittest.TestCase):
|
||||
def test_scores_each_bristol_class_and_averages_only_the_classes_present_in_truth(self):
|
||||
records = [
|
||||
record("aa00000000000001", 4, 4, 0.90),
|
||||
record("ab00000000000002", 4, 3, 0.80),
|
||||
record("ac00000000000003", 3, 3, 0.70),
|
||||
record("ad00000000000004", 7, 7, 0.60),
|
||||
]
|
||||
report = harness.evaluate(records, threshold=0.0)
|
||||
self.assertEqual(sorted(report["perClass"]), [3, 4, 7])
|
||||
self.assertAlmostEqual(report["perClass"][4]["precision"], 1.0, places=6)
|
||||
self.assertAlmostEqual(report["perClass"][4]["recall"], 0.5, places=6)
|
||||
self.assertAlmostEqual(report["perClass"][4]["f1"], 0.666667, places=6)
|
||||
self.assertAlmostEqual(report["perClass"][3]["precision"], 0.5, places=6)
|
||||
self.assertAlmostEqual(report["perClass"][3]["recall"], 1.0, places=6)
|
||||
self.assertAlmostEqual(report["perClass"][7]["f1"], 1.0, places=6)
|
||||
self.assertAlmostEqual(report["macroF1"], 0.777778, places=6)
|
||||
self.assertEqual(report["confusion"], {"4->4": 1, "4->3": 1, "3->3": 1, "7->7": 1})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -174,3 +174,20 @@ test('the deterministic symptom escalation regression suite runs in the default
|
||||
assert.match(builder, /"src\/domain\.js"/);
|
||||
assert.match(builder, /"src\/hermes-agent-service\.js"/);
|
||||
});
|
||||
|
||||
test('the calibration harness is wired into the unit and syntax gates', async () => {
|
||||
const [packageJsonText, workflow] = await Promise.all([
|
||||
readFile(packagePath, 'utf8'),
|
||||
readFile(workflowPath, 'utf8'),
|
||||
]);
|
||||
const packageJson = JSON.parse(packageJsonText);
|
||||
|
||||
// Issue #29: a release gate nothing runs is a claim, not a gate.
|
||||
assert.equal(packageJson.scripts['test:calibration'], 'python3 tests/calibration-harness.test.py');
|
||||
assert.match(packageJson.scripts['check:syntax'], /py_compile[^&]*scripts\/calibration_report\.py/);
|
||||
assert.match(workflow, /python3 tests\/calibration-harness\.test\.py -v/);
|
||||
// The release builder has to run it too, or the gate exists only in CI.
|
||||
const builder = await readFile(new URL('../scripts/build_release.py', import.meta.url), 'utf8');
|
||||
assert.match(builder, /tests\/calibration-harness\.test\.py/);
|
||||
assert.match(builder, /"calibration_harness_gates": "passed"/);
|
||||
});
|
||||
@@ -0,0 +1,273 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Tests for the auditable corpus manifest: provenance, tombstones, and lifecycle rules.
|
||||
|
||||
Issue #25 promotes the ingest script into a corpus pipeline with an append-only manifest,
|
||||
revocation tombstones, and lifecycle transitions that cannot be walked backwards. Every
|
||||
fixture here is synthetic; no real photo bytes are read or written.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
from PIL import Image
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
INGEST = ROOT / "scripts" / "ingest_training_photo.py"
|
||||
FIXTURE = ROOT / "tests" / "fixtures" / "synthetic-type4.jpg"
|
||||
CONTRIBUTION_ID = "6f1d0c9e-2c4a-4f3b-9d1e-8a7b6c5d4e3f"
|
||||
ISO_UTC = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?\+00:00$")
|
||||
|
||||
|
||||
def load_structures():
|
||||
spec = importlib.util.spec_from_file_location("corpus_manifest", ROOT / "scripts" / "corpus_manifest.py")
|
||||
assert spec and spec.loader
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
structures = load_structures()
|
||||
|
||||
|
||||
def active_image_paths(out: Path) -> list[str]:
|
||||
return [record["relativePath"] for record in structures.active_records(structures.load_manifest(out / "manifest.jsonl"))]
|
||||
|
||||
|
||||
|
||||
def ingest(args: list[str], env: dict[str, str] | None = None) -> subprocess.CompletedProcess:
|
||||
return subprocess.run(
|
||||
[sys.executable, str(INGEST), *args],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**os.environ, "TIMMY_DATASET_SALT": "test-only-salt", **(env or {})},
|
||||
)
|
||||
|
||||
|
||||
def ingest_default(out: Path, **overrides: str) -> subprocess.CompletedProcess:
|
||||
args = [
|
||||
"--input", str(FIXTURE),
|
||||
"--output-dir", str(out),
|
||||
"--subject-id", "subject-123",
|
||||
"--consent-version", "research-v1",
|
||||
"--contribution-id", CONTRIBUTION_ID,
|
||||
"--bristol-type", "4",
|
||||
"--color", "brown",
|
||||
"--quality", "good",
|
||||
"--reviewer", "user-confirmed",
|
||||
]
|
||||
for key, value in overrides.items():
|
||||
flag = "--" + key.replace("_", "-")
|
||||
if value is None:
|
||||
index = args.index(flag)
|
||||
del args[index:index + 2]
|
||||
continue
|
||||
args[args.index(flag) + 1] = value
|
||||
return ingest(args)
|
||||
|
||||
|
||||
def manifest_lines(out: Path) -> list[dict]:
|
||||
text = (out / "manifest.jsonl").read_text(encoding="utf-8")
|
||||
return [json.loads(line) for line in text.splitlines() if line.strip()]
|
||||
|
||||
|
||||
class IngestProvenanceTests(unittest.TestCase):
|
||||
def test_ingest_records_consent_source_hashes_derivation_review_split_and_lifecycle(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
run = ingest_default(out)
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
record = json.loads(run.stdout)
|
||||
raw_bytes = FIXTURE.read_bytes()
|
||||
|
||||
self.assertEqual(record["schemaVersion"], 2)
|
||||
self.assertEqual(record["contributionId"], CONTRIBUTION_ID)
|
||||
self.assertEqual(record["consentVersion"], "research-v1")
|
||||
self.assertEqual(record["consent"], {"version": "research-v1", "recordedAt": record["ingestedAt"]})
|
||||
self.assertRegex(record["consent"]["recordedAt"], ISO_UTC)
|
||||
|
||||
# Source is named, and the raw input only ever appears as a digest.
|
||||
self.assertEqual(record["source"], {
|
||||
"kind": "cli-ingest",
|
||||
"inputSha256": hashlib.sha256(raw_bytes).hexdigest(),
|
||||
})
|
||||
self.assertEqual(record["hashes"]["rawInput"], hashlib.sha256(raw_bytes).hexdigest())
|
||||
self.assertEqual(record["hashes"]["derived"], record["imageId"])
|
||||
|
||||
self.assertEqual(record["derivation"], {
|
||||
"format": "jpeg",
|
||||
"maxDimension": 1200,
|
||||
"metadataStripped": True,
|
||||
"pipeline": "timmy-ingest/2",
|
||||
})
|
||||
self.assertEqual(record["review"], {"status": "user-confirmed", "reviewedAt": record["ingestedAt"]})
|
||||
self.assertIn(record["split"], ("train", "validation", "test"))
|
||||
self.assertEqual(record["lifecycle"], {"state": "active", "updatedAt": record["ingestedAt"]})
|
||||
self.assertEqual(record["recordType"], "sample")
|
||||
|
||||
# Provenance never leaks the caller's path or the source bytes.
|
||||
self.assertNotIn("inputPath", record)
|
||||
on_disk = (out / "manifest.jsonl").read_text(encoding="utf-8")
|
||||
self.assertNotIn(str(FIXTURE), on_disk)
|
||||
self.assertNotIn(str(out), on_disk)
|
||||
self.assertEqual(manifest_lines(out), [record])
|
||||
self.assertTrue((out / record["relativePath"]).is_file())
|
||||
|
||||
|
||||
class RevocationTests(unittest.TestCase):
|
||||
def test_revocation_appends_a_tombstone_and_removes_the_bytes_from_active_datasets(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
self.assertEqual(ingest_default(out).returncode, 0)
|
||||
sample = manifest_lines(out)[0]
|
||||
derived = out / sample["relativePath"]
|
||||
self.assertTrue(derived.is_file())
|
||||
# Some pipelines keep the pre-derivation copy; revocation must clear that too.
|
||||
raw_dir = out / "raw"
|
||||
raw_dir.mkdir()
|
||||
raw_copy = raw_dir / f"{sample['hashes']['rawInput']}.jpg"
|
||||
raw_copy.write_bytes(FIXTURE.read_bytes())
|
||||
|
||||
run = ingest(["--revoke", CONTRIBUTION_ID, "--output-dir", str(out), "--reason", "subject withdrawal"])
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
tombstone = json.loads(run.stdout)
|
||||
|
||||
self.assertEqual(tombstone["schemaVersion"], 2)
|
||||
self.assertEqual(tombstone["recordType"], "tombstone")
|
||||
self.assertEqual(tombstone["contributionId"], CONTRIBUTION_ID)
|
||||
self.assertEqual(tombstone["imageId"], sample["imageId"])
|
||||
self.assertEqual(tombstone["lifecycle"]["state"], "revoked")
|
||||
self.assertRegex(tombstone["lifecycle"]["updatedAt"], ISO_UTC)
|
||||
self.assertEqual(tombstone["lifecycle"]["reason"], "subject withdrawal")
|
||||
|
||||
# The tombstone addresses every derived artifact by contribution id, and says what went.
|
||||
self.assertEqual(tombstone["deletedArtifacts"], [
|
||||
{"relativePath": sample["relativePath"], "sha256": sample["hashes"]["derived"], "deleted": True},
|
||||
{"relativePath": f"raw/{sample['hashes']['rawInput']}.jpg", "sha256": sample["hashes"]["rawInput"], "deleted": True},
|
||||
])
|
||||
self.assertEqual(tombstone["contributionId"], CONTRIBUTION_ID)
|
||||
|
||||
# No byte-bearing copy survives anywhere in the dataset tree.
|
||||
self.assertFalse(derived.exists())
|
||||
self.assertFalse(raw_copy.exists())
|
||||
survivors = []
|
||||
for path in out.rglob("*"):
|
||||
if path.is_file() and path.name != "manifest.jsonl":
|
||||
survivors.append((str(path.relative_to(out)), hashlib.sha256(path.read_bytes()).hexdigest()))
|
||||
self.assertEqual(survivors, [])
|
||||
|
||||
# History is append-only: the sample record is untouched, the tombstone follows it.
|
||||
history = manifest_lines(out)
|
||||
self.assertEqual(len(history), 2)
|
||||
self.assertEqual(history[0], sample)
|
||||
self.assertEqual(history[1], tombstone)
|
||||
|
||||
# A deletion receipt keeps identifiers and hashes, never the label or subject key.
|
||||
self.assertNotIn("labels", tombstone)
|
||||
self.assertNotIn("subjectKey", tombstone)
|
||||
self.assertNotIn("relativePath", tombstone)
|
||||
|
||||
|
||||
class ActiveRecordTests(unittest.TestCase):
|
||||
def test_only_unrevoked_samples_are_active(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
self.assertEqual(ingest_default(out).returncode, 0)
|
||||
self.assertEqual(active_image_paths(out), [f"images/{manifest_lines(out)[0]['imageId']}.jpg"])
|
||||
self.assertEqual(ingest(["--revoke", CONTRIBUTION_ID, "--output-dir", str(out), "--reason", "gone"]).returncode, 0)
|
||||
self.assertEqual(active_image_paths(out), [])
|
||||
|
||||
|
||||
class IllegalTransitionTests(unittest.TestCase):
|
||||
def test_a_second_revocation_is_refused_and_appends_nothing(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
self.assertEqual(ingest_default(out).returncode, 0)
|
||||
self.assertEqual(ingest(["--revoke", CONTRIBUTION_ID, "--output-dir", str(out), "--reason", "first"]).returncode, 0)
|
||||
history = manifest_lines(out)
|
||||
self.assertEqual(len(history), 2)
|
||||
|
||||
again = ingest(["--revoke", CONTRIBUTION_ID, "--output-dir", str(out), "--reason", "again"])
|
||||
self.assertNotEqual(again.returncode, 0)
|
||||
self.assertRegex(again.stderr, "already revoked")
|
||||
self.assertEqual(manifest_lines(out), history)
|
||||
|
||||
def test_revoking_an_unknown_contribution_is_refused(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
self.assertEqual(ingest_default(out).returncode, 0)
|
||||
history = manifest_lines(out)
|
||||
|
||||
unknown = ingest(["--revoke", "11111111-2222-3333-4444-555555555555", "--output-dir", str(out), "--reason", "nope"])
|
||||
self.assertNotEqual(unknown.returncode, 0)
|
||||
self.assertRegex(unknown.stderr, "no ingested sample carries contribution")
|
||||
self.assertEqual(manifest_lines(out), history)
|
||||
self.assertTrue((out / history[0]["relativePath"]).is_file())
|
||||
|
||||
def test_a_contribution_cannot_be_ingested_twice(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
self.assertEqual(ingest_default(out).returncode, 0)
|
||||
history = manifest_lines(out)
|
||||
|
||||
duplicate = ingest_default(out, color="green")
|
||||
self.assertNotEqual(duplicate.returncode, 0)
|
||||
self.assertRegex(duplicate.stderr, "already ingested")
|
||||
self.assertEqual(manifest_lines(out), history)
|
||||
|
||||
def test_a_refused_duplicate_leaves_no_orphan_image_behind(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
self.assertEqual(ingest_default(out).returncode, 0)
|
||||
history = manifest_lines(out)
|
||||
|
||||
# Same contribution id, different pixels: the derived digest differs, so a guard that
|
||||
# runs after the write would strand a second image in the active dataset.
|
||||
variant = out.parent / "variant-type4.jpg"
|
||||
with Image.open(FIXTURE) as source:
|
||||
changed = source.convert("RGB")
|
||||
changed.putpixel((4, 4), (255, 0, 0))
|
||||
changed.save(variant, "JPEG", quality=95)
|
||||
duplicate = ingest_default(out, input=str(variant))
|
||||
self.assertNotEqual(duplicate.returncode, 0)
|
||||
self.assertRegex(duplicate.stderr, "already ingested")
|
||||
|
||||
self.assertEqual(manifest_lines(out), history)
|
||||
images = sorted(path.name for path in (out / "images").iterdir())
|
||||
self.assertEqual(images, [f"{history[0]['imageId']}.jpg"])
|
||||
|
||||
|
||||
def test_lifecycle_transitions_cannot_walk_backwards(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
out = Path(raw)
|
||||
self.assertEqual(ingest_default(out).returncode, 0)
|
||||
sample = manifest_lines(out)[0]
|
||||
at = sample["ingestedAt"]
|
||||
|
||||
revoked = structures.transition(sample, "revoked", at, "withdrawal")
|
||||
self.assertEqual(revoked["lifecycle"]["state"], "revoked")
|
||||
self.assertEqual(revoked["lifecycle"]["reason"], "withdrawal")
|
||||
self.assertEqual(sample["lifecycle"]["state"], "active", "transition must not mutate its input")
|
||||
|
||||
for args, expected in (
|
||||
((revoked, "active", at), "cannot return to the active dataset"),
|
||||
((sample, "revoked", at, " "), "recorded reason"),
|
||||
((sample, "archived", at), "unknown lifecycle state"),
|
||||
(({**sample, "lifecycle": {"state": "active", "updatedAt": at}}, "active", at), "already active"),
|
||||
):
|
||||
with self.assertRaises(structures.IllegalTransition) as raised:
|
||||
structures.transition(*args)
|
||||
self.assertRegex(str(raised.exception), expected)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
detectUrgentText,
|
||||
exportLedger,
|
||||
hasUrgentLedgerContext,
|
||||
LEDGER_EXPORT_VERSION,
|
||||
photoQualityMessage,
|
||||
sanitizeEntry,
|
||||
} from '../src/domain.js';
|
||||
@@ -113,9 +114,10 @@ test('sanitizes a user entry to the MVP data contract', () => {
|
||||
});
|
||||
assert.deepEqual(Object.keys(entry).sort(), [
|
||||
'bristolType', 'color', 'discomfort', 'id', 'note', 'occurredAt',
|
||||
'photoDataUrl', 'symptoms', 'urgency'
|
||||
'photoDataUrl', 'provenance', 'symptoms', 'urgency'
|
||||
].sort());
|
||||
assert.equal(entry.unexpected, undefined);
|
||||
assert.equal(entry.provenance, null, 'a manual entry records no AI provenance rather than an invented one');
|
||||
});
|
||||
|
||||
test('photo quality guidance is deterministic and does not claim visual diagnosis', () => {
|
||||
@@ -125,10 +127,11 @@ test('photo quality guidance is deterministic and does not claim visual diagnosi
|
||||
assert.doesNotMatch(photoQualityMessage({ width: 1200, height: 900, brightness: 0.5 }), /type [1-7]|disease|diagnos/i);
|
||||
});
|
||||
|
||||
test('export ledger is portable JSON with version and entries', () => {
|
||||
test('export ledger is portable JSON with the current version and entries', () => {
|
||||
const text = exportLedger([{ id: 'a', bristolType: 4 }], '2026-08-18T00:00:00.000Z');
|
||||
const parsed = JSON.parse(text);
|
||||
assert.equal(parsed.schemaVersion, 1);
|
||||
assert.equal(parsed.schemaVersion, LEDGER_EXPORT_VERSION);
|
||||
assert.equal(parsed.schemaVersion, 2);
|
||||
assert.equal(parsed.exportedAt, '2026-08-18T00:00:00.000Z');
|
||||
assert.equal(parsed.entries.length, 1);
|
||||
});
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
# Synthetic calibration fixtures
|
||||
|
||||
These files prove the evaluation harness in `scripts/calibration_report.py` (issue #29) computes the
|
||||
release gates correctly. **They are not measurements.** No model produced them, no contributor
|
||||
supplied them, and no number here says anything about how well Timmy labels a real photo.
|
||||
|
||||
Every row is a promise in the same shape the harness accepts:
|
||||
|
||||
```json
|
||||
{"model":"…","subjectKey":"<16 hex>","split":"validation","truth":{"bristolType":4},
|
||||
"prediction":{"bristolType":4,"confidence":0.97}}
|
||||
```
|
||||
|
||||
`subjectKey` values are fictional but obey the ingestion split rule (the first byte of the key picks
|
||||
the split), so the harness's contributor-held-out check is exercised for real. `prediction: null`
|
||||
means the candidate produced no suggestion at all.
|
||||
|
||||
| File | What it is for | Harness outcome |
|
||||
|---|---|---|
|
||||
| `predictions.jsonl` | Three candidates with invented scores: a bootstrap that calls almost everything Type 4, a frozen encoder that is precise but badly calibrated, and a specialist that looks strong on validation and slips on test. | `BLOCKED` — each candidate fails a different gate. |
|
||||
| `predictions-go.jsonl` | One engineered candidate that clears every gate, including a low-confidence mistake on validation that the threshold is supposed to cut away. | `GO` — proves the passing branch and threshold selection, not a result. |
|
||||
| `predictions-leaky.jsonl` | One contributor whose photos sit in both `validation` and `test`. | Refused with exit 2 — a leaked corpus is never scored. |
|
||||
|
||||
## Regenerating
|
||||
|
||||
The numbers are hand-chosen so the expected outcome is obvious by inspection; there is no generator
|
||||
to re-run. If you change a row, update the expectation in `tests/calibration-harness.test.py` and the
|
||||
caption in `scripts/record_release_demo.mjs` together, and read the harness output back before
|
||||
claiming what it says.
|
||||
|
||||
No image, no diagnosis, and no patient data lives in this directory.
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"cd1a2b3c4d5e6f01","split":"validation","truth":{"bristolType":1},"prediction":{"bristolType":1,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"ce2b3c4d5e6f7012","split":"validation","truth":{"bristolType":2},"prediction":{"bristolType":2,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"cf3c4d5e6f701223","split":"validation","truth":{"bristolType":3},"prediction":{"bristolType":3,"confidence":0.98}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"d04d5e6f70122334","split":"validation","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"d15e6f7012233445","split":"validation","truth":{"bristolType":5},"prediction":{"bristolType":5,"confidence":0.98}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"d26f701223344556","split":"validation","truth":{"bristolType":6},"prediction":{"bristolType":3,"confidence":0.6}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"d370122334455667","split":"validation","truth":{"bristolType":7},"prediction":{"bristolType":7,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"e671223344556677","split":"test","truth":{"bristolType":1},"prediction":{"bristolType":1,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"e772334455667788","split":"test","truth":{"bristolType":2},"prediction":{"bristolType":2,"confidence":0.98}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"e883445566778899","split":"test","truth":{"bristolType":3},"prediction":{"bristolType":3,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"e994556677889900","split":"test","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.98}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"eaa5667788990011","split":"test","truth":{"bristolType":5},"prediction":{"bristolType":5,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"ebb6778899001122","split":"test","truth":{"bristolType":6},"prediction":{"bristolType":6,"confidence":0.97}}
|
||||
{"model":"gate-plumbing-fixture","subjectKey":"ecc7889900112233","split":"test","truth":{"bristolType":7},"prediction":{"bristolType":7,"confidence":0.98}}
|
||||
@@ -0,0 +1,2 @@
|
||||
{"model":"frozen-encoder","subjectKey":"cd1a2b3c4d5e6f01","split":"validation","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.7}}
|
||||
{"model":"frozen-encoder","subjectKey":"cd1a2b3c4d5e6f01","split":"test","truth":{"bristolType":6},"prediction":{"bristolType":6,"confidence":0.7}}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
{"model":"vlm-bootstrap","subjectKey":"cd1a2b3c4d5e6f01","split":"validation","truth":{"bristolType":1},"prediction":{"bristolType":4,"confidence":0.55}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"ce2b3c4d5e6f7012","split":"validation","truth":{"bristolType":2},"prediction":{"bristolType":4,"confidence":0.52}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"cf3c4d5e6f701223","split":"validation","truth":{"bristolType":3},"prediction":{"bristolType":4,"confidence":0.61}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"d04d5e6f70122334","split":"validation","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.72}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"d15e6f7012233445","split":"validation","truth":{"bristolType":5},"prediction":{"bristolType":4,"confidence":0.64}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"d26f701223344556","split":"validation","truth":{"bristolType":6},"prediction":{"bristolType":4,"confidence":0.58}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"d370122334455667","split":"validation","truth":{"bristolType":7},"prediction":{"bristolType":4,"confidence":0.53}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"e671223344556677","split":"test","truth":{"bristolType":1},"prediction":{"bristolType":4,"confidence":0.57}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"e772334455667788","split":"test","truth":{"bristolType":2},"prediction":{"bristolType":4,"confidence":0.54}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"e883445566778899","split":"test","truth":{"bristolType":3},"prediction":{"bristolType":4,"confidence":0.63}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"e994556677889900","split":"test","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.7}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"eaa5667788990011","split":"test","truth":{"bristolType":5},"prediction":{"bristolType":4,"confidence":0.62}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"ebb6778899001122","split":"test","truth":{"bristolType":6},"prediction":{"bristolType":4,"confidence":0.59}}
|
||||
{"model":"vlm-bootstrap","subjectKey":"ecc7889900112233","split":"test","truth":{"bristolType":7},"prediction":{"bristolType":4,"confidence":0.51}}
|
||||
{"model":"frozen-encoder","subjectKey":"cd1a2b3c4d5e6f01","split":"validation","truth":{"bristolType":1},"prediction":{"bristolType":1,"confidence":0.62}}
|
||||
{"model":"frozen-encoder","subjectKey":"ce2b3c4d5e6f7012","split":"validation","truth":{"bristolType":2},"prediction":{"bristolType":2,"confidence":0.58}}
|
||||
{"model":"frozen-encoder","subjectKey":"cf3c4d5e6f701223","split":"validation","truth":{"bristolType":3},"prediction":{"bristolType":4,"confidence":0.66}}
|
||||
{"model":"frozen-encoder","subjectKey":"d04d5e6f70122334","split":"validation","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.71}}
|
||||
{"model":"frozen-encoder","subjectKey":"d15e6f7012233445","split":"validation","truth":{"bristolType":5},"prediction":{"bristolType":5,"confidence":0.57}}
|
||||
{"model":"frozen-encoder","subjectKey":"d26f701223344556","split":"validation","truth":{"bristolType":6},"prediction":{"bristolType":6,"confidence":0.64}}
|
||||
{"model":"frozen-encoder","subjectKey":"d370122334455667","split":"validation","truth":{"bristolType":7},"prediction":{"bristolType":6,"confidence":0.55}}
|
||||
{"model":"frozen-encoder","subjectKey":"e671223344556677","split":"test","truth":{"bristolType":1},"prediction":{"bristolType":1,"confidence":0.6}}
|
||||
{"model":"frozen-encoder","subjectKey":"e772334455667788","split":"test","truth":{"bristolType":2},"prediction":{"bristolType":2,"confidence":0.59}}
|
||||
{"model":"frozen-encoder","subjectKey":"e883445566778899","split":"test","truth":{"bristolType":3},"prediction":{"bristolType":3,"confidence":0.63}}
|
||||
{"model":"frozen-encoder","subjectKey":"e994556677889900","split":"test","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.69}}
|
||||
{"model":"frozen-encoder","subjectKey":"eaa5667788990011","split":"test","truth":{"bristolType":5},"prediction":{"bristolType":6,"confidence":0.56}}
|
||||
{"model":"frozen-encoder","subjectKey":"ebb6778899001122","split":"test","truth":{"bristolType":6},"prediction":{"bristolType":6,"confidence":0.61}}
|
||||
{"model":"frozen-encoder","subjectKey":"ecc7889900112233","split":"test","truth":{"bristolType":7},"prediction":{"bristolType":7,"confidence":0.54}}
|
||||
{"model":"specialist-candidate","subjectKey":"cd1a2b3c4d5e6f01","split":"validation","truth":{"bristolType":1},"prediction":{"bristolType":1,"confidence":0.88}}
|
||||
{"model":"specialist-candidate","subjectKey":"ce2b3c4d5e6f7012","split":"validation","truth":{"bristolType":2},"prediction":{"bristolType":2,"confidence":0.86}}
|
||||
{"model":"specialist-candidate","subjectKey":"cf3c4d5e6f701223","split":"validation","truth":{"bristolType":3},"prediction":{"bristolType":3,"confidence":0.9}}
|
||||
{"model":"specialist-candidate","subjectKey":"d04d5e6f70122334","split":"validation","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.94}}
|
||||
{"model":"specialist-candidate","subjectKey":"d15e6f7012233445","split":"validation","truth":{"bristolType":5},"prediction":{"bristolType":5,"confidence":0.87}}
|
||||
{"model":"specialist-candidate","subjectKey":"d26f701223344556","split":"validation","truth":{"bristolType":6},"prediction":{"bristolType":6,"confidence":0.91}}
|
||||
{"model":"specialist-candidate","subjectKey":"d370122334455667","split":"validation","truth":{"bristolType":7},"prediction":{"bristolType":7,"confidence":0.85}}
|
||||
{"model":"specialist-candidate","subjectKey":"e671223344556677","split":"test","truth":{"bristolType":1},"prediction":{"bristolType":1,"confidence":0.86}}
|
||||
{"model":"specialist-candidate","subjectKey":"e772334455667788","split":"test","truth":{"bristolType":2},"prediction":{"bristolType":2,"confidence":0.84}}
|
||||
{"model":"specialist-candidate","subjectKey":"e883445566778899","split":"test","truth":{"bristolType":3},"prediction":{"bristolType":3,"confidence":0.89}}
|
||||
{"model":"specialist-candidate","subjectKey":"e994556677889900","split":"test","truth":{"bristolType":4},"prediction":{"bristolType":4,"confidence":0.92}}
|
||||
{"model":"specialist-candidate","subjectKey":"eaa5667788990011","split":"test","truth":{"bristolType":5},"prediction":{"bristolType":5,"confidence":0.88}}
|
||||
{"model":"specialist-candidate","subjectKey":"ebb6778899001122","split":"test","truth":{"bristolType":6},"prediction":{"bristolType":3,"confidence":0.83}}
|
||||
{"model":"specialist-candidate","subjectKey":"ecc7889900112233","split":"test","truth":{"bristolType":7},"prediction":{"bristolType":7,"confidence":0.87}}
|
||||
+16
@@ -131,6 +131,22 @@
|
||||
"expectedOutcome": "fail_closed",
|
||||
"expectedReason": "out_of_range_or_missing_fields",
|
||||
"providerContent": "{\"isStool\":true,\"bristolType\":9,\"color\":null,\"confidence\":0.9,\"imageQuality\":\"good\",\"observations\":\"Form read, colour not.\"}"
|
||||
},
|
||||
{
|
||||
"id": "poor-quality-confident",
|
||||
"caseClass": "blur",
|
||||
"file": "blur-low-detail.png",
|
||||
"bytes": 21598,
|
||||
"sha256": "17000163662a42fd434bb2b2428d9e5a2156da08cbc82210e41dfc6fa7e24852",
|
||||
"width": 640,
|
||||
"height": 480,
|
||||
"format": "png",
|
||||
"synthetic": true,
|
||||
"license": "CC0-1.0",
|
||||
"provenance": "The same synthetic motion-blur fixture that blur-low-detail replays, replayed again with a confident provider answer; drawn deterministically by scripts/build_vision_corpus.py, no camera, no human subject, no third-party content.",
|
||||
"expectedOutcome": "abstained",
|
||||
"expectedReason": "low_quality",
|
||||
"providerContent": "{\"isStool\":true,\"bristolType\":4,\"color\":\"brown\",\"confidence\":0.94,\"imageQuality\":\"poor\",\"observations\":\"Form barely visible through motion blur.\"}"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
import { chromium } from 'playwright';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
|
||||
// Issue #35 — a backup is the person's own record, and it has to keep working as the app's AI
|
||||
// provenance fields evolve. This drives the real app in a browser: a version 1 file is upgraded and
|
||||
// says so, a file from a newer Timmy is refused without touching the ledger, the export carries the
|
||||
// contribution receipts, and a log saved from a confirmed AI suggestion comes back saying that.
|
||||
//
|
||||
// The image used below is the committed synthetic Type 4 fixture; no real photo is involved.
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, serviceWorkers: 'block' });
|
||||
const page = await context.newPage();
|
||||
const errors = [];
|
||||
page.on('console', message => { if (message.type() === 'error') errors.push(message.text()); });
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
page.on('dialog', dialog => dialog.accept());
|
||||
|
||||
const suggestionBody = {
|
||||
status: 'suggestion', isStool: true, bristolType: 4, color: 'brown', confidence: 0.83,
|
||||
imageQuality: 'good', observations: 'Smooth, formed appearance.',
|
||||
warning: 'Visual suggestion only. Confirm it yourself; this is not a diagnosis.',
|
||||
};
|
||||
await page.route('**/api/vision-status', route => route.fulfill({
|
||||
status: 200, contentType: 'application/json',
|
||||
body: JSON.stringify({ enabled: true, profile: 'selfhost', processor: 'self-hosted', model: 'SmolVLM2-2.2B-Instruct', providerReady: true, modelSeen: true }),
|
||||
}));
|
||||
await page.route('**/api/analyze', route => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(suggestionBody) }));
|
||||
await page.route('**/api/agent/status', route => route.fulfill({
|
||||
status: 200, contentType: 'application/json',
|
||||
body: JSON.stringify({ enabled: true, configured: false, authenticated: false, mode: 'local-fallback' }),
|
||||
}));
|
||||
|
||||
await page.goto('http://127.0.0.1:4173', { waitUntil: 'networkidle' });
|
||||
await page.evaluate(() => localStorage.clear());
|
||||
await page.reload({ waitUntil: 'networkidle' });
|
||||
|
||||
const basePath = () => page.evaluate(() => document.querySelector('meta[name="timmy-base-path"]')?.content || '/');
|
||||
const storeKey = async suffix => `timmy:${await basePath()}:${suffix}`;
|
||||
const readStore = async suffix => {
|
||||
const raw = await page.evaluate(key => localStorage.getItem(key), await storeKey(suffix));
|
||||
return raw === null ? null : JSON.parse(raw);
|
||||
};
|
||||
// The privacy view sits behind the journal's settings row, exactly as a person reaches it.
|
||||
const openPrivacy = async () => {
|
||||
await page.locator('[data-view="calendar"]').first().click();
|
||||
await page.locator('.settings-row[data-view="privacy"]').first().click();
|
||||
await page.locator('#export').waitFor();
|
||||
};
|
||||
const importBackup = async (name, text) => {
|
||||
await page.locator('#import').setInputFiles({ name, mimeType: 'application/json', buffer: Buffer.from(text) });
|
||||
await page.waitForTimeout(350);
|
||||
};
|
||||
|
||||
const legacyBackup = JSON.stringify({
|
||||
product: 'Timmy the Talking Turd',
|
||||
schemaVersion: 1,
|
||||
exportedAt: '2026-08-01T09:00:00.000Z',
|
||||
entries: [
|
||||
{ id: 'legacy-1', occurredAt: '2026-08-01T08:00:00.000Z', bristolType: 3, color: 'brown', urgency: 1, discomfort: 0, note: 'synthetic', photoDataUrl: '', symptoms: {} },
|
||||
{ id: 'legacy-2', occurredAt: '2026-08-02T08:00:00.000Z', bristolType: 6, color: 'brown', urgency: 2, discomfort: 1, note: '', photoDataUrl: '', symptoms: {} },
|
||||
],
|
||||
});
|
||||
|
||||
// 1. A version 1 backup is imported, upgraded, and says which version it came from.
|
||||
await openPrivacy();
|
||||
await importBackup('timmy-backup-v1.json', legacyBackup);
|
||||
const upgraded = await readStore('ledger-v1');
|
||||
assert.equal(upgraded.length, 2, 'the version 1 backup must restore both logs');
|
||||
await page.locator('[data-portability-notice]').waitFor();
|
||||
const noticeText = (await page.locator('[data-portability-notice]').innerText()).trim();
|
||||
assert.match(noticeText, /version 1/i, 'the notice must name the version the file came from');
|
||||
assert.match(noticeText, /upgraded/i, 'the notice must say the backup was upgraded');
|
||||
assert.equal(await page.locator('[data-portability-notice]').getAttribute('data-portability-version'), '1');
|
||||
assert.equal(upgraded[0].provenance, null, 'a pre-provenance backup must not acquire a source it never recorded');
|
||||
assert.equal(upgraded[0].bristolType, 3, 'the confirmed form must survive the upgrade');
|
||||
|
||||
// 2. A file from a newer Timmy is refused, and the ledger it would have replaced stays exactly as it was.
|
||||
const before = JSON.stringify(await readStore('ledger-v1'));
|
||||
await importBackup('timmy-backup-v3.json', JSON.stringify({ product: 'Timmy the Talking Turd', schemaVersion: 3, entries: [{ id: 'future', bristolType: 7 }] }));
|
||||
await page.locator('[data-portability-error]').waitFor();
|
||||
const errorText = (await page.locator('[data-portability-error]').innerText()).trim();
|
||||
assert.match(errorText, /newer version of Timmy/i);
|
||||
assert.equal(JSON.stringify(await readStore('ledger-v1')), before, 'a refused import must not change the ledger');
|
||||
|
||||
// 3. A log saved from a confirmed AI suggestion records that provenance, and the export states it.
|
||||
await page.locator('[data-view="home"]').first().click();
|
||||
await page.locator('[data-scan]').first().click();
|
||||
await page.getByText(/Self-hosted model ready/i).waitFor();
|
||||
await page.locator('#gallery-photo').setInputFiles('tests/fixtures/synthetic-type4.jpg');
|
||||
await page.locator('#ai-consent').check();
|
||||
await page.locator('#analyze-photo').click();
|
||||
await page.locator('#use-suggestion').waitFor();
|
||||
await page.locator('#use-suggestion').click();
|
||||
await page.locator('[data-provenance="confirmed"]').waitFor();
|
||||
await page.locator('#next').click();
|
||||
await page.locator('#next').click();
|
||||
await page.locator('#save').click();
|
||||
await page.getByText(/Private log saved|Saved/).waitFor();
|
||||
const savedLedger = await readStore('ledger-v1');
|
||||
const assisted = savedLedger.find(entry => entry.id !== 'legacy-1' && entry.id !== 'legacy-2');
|
||||
assert.ok(assisted, 'the saved log must be in the ledger');
|
||||
assert.equal(assisted.provenance?.source, 'ai-confirmed', 'a confirmed AI suggestion must be recorded on the saved log');
|
||||
assert.equal(assisted.provenance?.suggestedBristolType, 4);
|
||||
assert.equal(assisted.provenance?.model, 'SmolVLM2-2.2B-Instruct');
|
||||
|
||||
// 4. Export carries the current version, the entries, and the contribution receipts — and nothing else.
|
||||
await openPrivacy();
|
||||
const [download] = await Promise.all([page.waitForEvent('download'), page.locator('#export').click()]);
|
||||
const exported = JSON.parse(await readFile(await download.path(), 'utf8'));
|
||||
assert.equal(exported.schemaVersion, 2);
|
||||
assert.equal(exported.entries.length, 3);
|
||||
assert.deepEqual(exported.contributions, [], 'nothing was contributed, so no receipt may appear');
|
||||
assert.equal(exported.entries.find(entry => entry.id === assisted.id).provenance.source, 'ai-confirmed');
|
||||
|
||||
// 5. Round trip: the export restores into a cleared browser with provenance intact.
|
||||
const exportedText = JSON.stringify(exported);
|
||||
await page.evaluate(() => localStorage.clear());
|
||||
await page.reload({ waitUntil: 'networkidle' });
|
||||
await openPrivacy();
|
||||
await importBackup('timmy-backup-v2.json', exportedText);
|
||||
const roundTripped = await readStore('ledger-v1');
|
||||
assert.equal(roundTripped.length, 3);
|
||||
const restoredAssisted = roundTripped.find(entry => entry.id === assisted.id);
|
||||
assert.deepEqual(restoredAssisted.provenance, assisted.provenance, 'provenance must survive a round trip');
|
||||
await page.locator('[data-portability-notice]').waitFor();
|
||||
assert.match((await page.locator('[data-portability-notice]').innerText()).trim(), /version 2/i);
|
||||
|
||||
assert.deepEqual(errors, [], `browser errors: ${errors.join(' | ')}`);
|
||||
await browser.close();
|
||||
console.log(JSON.stringify({ ok: true, scenes: ['v1-upgrade', 'future-refused', 'provenance-recorded', 'export-v2', 'round-trip'] }));
|
||||
@@ -0,0 +1,152 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
import { LEDGER_EXPORT_VERSION, LEDGER_IMPORT_LIMITS, exportLedger, exportLedgerBackup, importLedger, importLedgerBackup } from '../src/domain.js';
|
||||
|
||||
// Issue #35 — a backup is the person's own record. Provenance is part of what they own: a log that
|
||||
// was saved from a confirmed or corrected AI suggestion has to come back out of the export saying
|
||||
// exactly that, instead of silently reverting to looking like a manual entry.
|
||||
const confirmedByUser = {
|
||||
id: 'demo-corrected',
|
||||
occurredAt: '2026-08-18T10:00:00.000Z',
|
||||
bristolType: 2,
|
||||
color: 'brown',
|
||||
urgency: 1,
|
||||
discomfort: 0,
|
||||
note: 'synthetic',
|
||||
photoDataUrl: '',
|
||||
symptoms: {},
|
||||
provenance: {
|
||||
source: 'ai-corrected',
|
||||
model: 'SmolVLM2-2.2B-Instruct',
|
||||
suggestedBristolType: 4,
|
||||
suggestedColor: 'brown',
|
||||
confirmedAt: '2026-08-18T09:59:00.000Z',
|
||||
},
|
||||
};
|
||||
|
||||
test('an export stamps the current schema version and keeps confirmed AI provenance', () => {
|
||||
const parsed = JSON.parse(exportLedger([confirmedByUser], '2026-08-18T11:00:00.000Z'));
|
||||
|
||||
assert.equal(LEDGER_EXPORT_VERSION, 2);
|
||||
assert.equal(parsed.schemaVersion, 2);
|
||||
assert.equal(parsed.entries.length, 1);
|
||||
assert.deepEqual(parsed.entries[0].provenance, confirmedByUser.provenance);
|
||||
assert.equal(parsed.entries[0].bristolType, 2, 'the corrected value stays the saved value');
|
||||
});
|
||||
|
||||
// A version 1 file was written before entries recorded provenance. Importing it must restore the
|
||||
// confirmed values and say plainly that this backup predates provenance, rather than inventing a
|
||||
// source for logs that never had one.
|
||||
const legacyBackup = JSON.stringify({
|
||||
product: 'Timmy the Talking Turd',
|
||||
schemaVersion: 1,
|
||||
exportedAt: '2026-08-01T09:00:00.000Z',
|
||||
entries: [
|
||||
{
|
||||
id: 'legacy-entry',
|
||||
occurredAt: '2026-08-01T08:00:00.000Z',
|
||||
bristolType: 4,
|
||||
color: 'brown',
|
||||
urgency: 2,
|
||||
discomfort: 1,
|
||||
note: '',
|
||||
photoDataUrl: '',
|
||||
symptoms: { blood: false },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
test('a version 1 backup imports its confirmed values and reports the upgrade', () => {
|
||||
const result = importLedgerBackup(legacyBackup);
|
||||
|
||||
assert.equal(result.fromVersion, 1);
|
||||
assert.equal(result.migrated, true);
|
||||
assert.equal(result.entries.length, 1);
|
||||
assert.equal(result.entries[0].id, 'legacy-entry');
|
||||
assert.equal(result.entries[0].bristolType, 4);
|
||||
assert.equal(result.entries[0].provenance, null, 'a pre-provenance backup must not be given a source it never recorded');
|
||||
assert.match(result.notices.join(' '), /version 1/i);
|
||||
assert.match(result.notices.join(' '), /upgraded/i);
|
||||
// The import screen already reports what was restored; a notice that restates the count renders as
|
||||
// a duplicated sentence on screen, so the upgrade notice must only describe the upgrade.
|
||||
assert.doesNotMatch(result.notices.join(' '), /Restored \d+ logs?/);
|
||||
assert.equal(importLedger(legacyBackup).length, 1, 'the plain entry import still works');
|
||||
});
|
||||
|
||||
// A training contribution receipt is user-owned data too. A backup that restored the journal but
|
||||
// dropped the receipts would tell someone their opt-in never happened, so version 2 carries them —
|
||||
// with the withdrawal tombstone intact and nothing else copied out of the device.
|
||||
const withdrawnReceipt = {
|
||||
contributionId: 'contrib-1',
|
||||
entryId: 'demo-corrected',
|
||||
consentVersion: 'training-consent-v1',
|
||||
consentedAt: '2026-08-18T10:05:00.000Z',
|
||||
labels: { bristolType: 2, color: 'brown' },
|
||||
state: 'withdrawn',
|
||||
withdrawnAt: '2026-08-18T10:30:00.000Z',
|
||||
artifacts: [
|
||||
{ artifact: 'raw-image', contributionId: 'contrib-1', state: 'withdrawn' },
|
||||
{ artifact: 'confirmed-label', contributionId: 'contrib-1', state: 'withdrawn' },
|
||||
],
|
||||
// A crafted file must not be able to hand a credential back to the app.
|
||||
agentAccessCode: 'leak-me-please',
|
||||
};
|
||||
|
||||
test('a backup carries contribution receipts and their tombstones without copying secrets', () => {
|
||||
const text = exportLedgerBackup({
|
||||
entries: [{ ...confirmedByUser, agentAccessCode: 'leak-me-please' }],
|
||||
contributions: [withdrawnReceipt],
|
||||
exportedAt: '2026-08-18T12:00:00.000Z',
|
||||
});
|
||||
const parsed = JSON.parse(text);
|
||||
|
||||
assert.equal(parsed.schemaVersion, 2);
|
||||
assert.equal(parsed.contributions.length, 1);
|
||||
assert.equal(parsed.contributions[0].state, 'withdrawn');
|
||||
assert.deepEqual(parsed.contributions[0].artifacts, withdrawnReceipt.artifacts);
|
||||
assert.equal(parsed.entries[0].agentAccessCode, undefined);
|
||||
assert.doesNotMatch(text, /leak-me-please/);
|
||||
|
||||
const restored = importLedgerBackup(text);
|
||||
assert.equal(restored.migrated, false);
|
||||
assert.equal(restored.fromVersion, 2);
|
||||
assert.equal(restored.contributions.length, 1);
|
||||
assert.equal(restored.contributions[0].contributionId, 'contrib-1');
|
||||
assert.equal(restored.contributions[0].state, 'withdrawn');
|
||||
assert.deepEqual(restored.contributions[0].artifacts, withdrawnReceipt.artifacts);
|
||||
assert.equal(restored.contributions[0].agentAccessCode, undefined);
|
||||
assert.deepEqual(restored.entries[0].provenance, confirmedByUser.provenance);
|
||||
});
|
||||
|
||||
test('a backup from a newer Timmy is refused with an instruction instead of a guess', () => {
|
||||
const future = JSON.stringify({ product: 'Timmy the Talking Turd', schemaVersion: 3, entries: [], contributions: [] });
|
||||
|
||||
assert.throws(() => importLedgerBackup(future), /newer version of Timmy/i);
|
||||
});
|
||||
|
||||
test('a file that is not a readable Timmy backup is refused', () => {
|
||||
assert.throws(() => importLedgerBackup('{not json'), /not readable JSON/i);
|
||||
assert.throws(() => importLedgerBackup(JSON.stringify({ hello: 'world' })), /not a supported Timmy backup/i);
|
||||
assert.throws(() => importLedgerBackup(JSON.stringify({ schemaVersion: 0, entries: [] })), /not a supported Timmy backup/i);
|
||||
});
|
||||
|
||||
test('an oversized backup or one with too many logs is refused before anything is imported', () => {
|
||||
const huge = `{"schemaVersion":2,"entries":[]}${' '.repeat(LEDGER_IMPORT_LIMITS.maxTextLength)}`;
|
||||
assert.throws(() => importLedgerBackup(huge), /too large/i);
|
||||
|
||||
const many = { schemaVersion: 2, entries: Array.from({ length: LEDGER_IMPORT_LIMITS.maxEntries + 1 }, () => ({ id: 'x', bristolType: 4 })) };
|
||||
assert.throws(() => importLedgerBackup(JSON.stringify(many)), /more logs than Timmy will import/i);
|
||||
});
|
||||
|
||||
test('an unreadable contribution receipt is skipped and reported, never silently dropped', () => {
|
||||
const text = JSON.stringify({
|
||||
schemaVersion: 2,
|
||||
entries: [{ id: 'kept', bristolType: 4 }],
|
||||
contributions: [{ entryId: 'kept', state: 'queued-local' }],
|
||||
});
|
||||
const result = importLedgerBackup(text);
|
||||
|
||||
assert.equal(result.contributions.length, 0);
|
||||
assert.match(result.notices.join(' '), /could not be read and was skipped/i);
|
||||
});
|
||||
@@ -21,6 +21,15 @@ test('product decision record fixes the AI boundary and release authority', asyn
|
||||
assert.match(decision, /\.\.\/research\/SELF-HOSTED-STOOL-VISION\.md/);
|
||||
});
|
||||
|
||||
test('the decision record keeps training contribution as its own versioned, withdrawable consent', async () => {
|
||||
const decision = await readFile(decisionPath, 'utf8');
|
||||
|
||||
assert.match(decision, /### Training contribution consent/);
|
||||
assert.match(decision, /never a training contribution/i);
|
||||
assert.match(decision, /withdrawal keeps a tombstone naming every derived artifact by contribution id/i);
|
||||
assert.match(decision, /does not transmit a contribution until the corpus pipeline and privacy copy are reviewed/i);
|
||||
});
|
||||
|
||||
test('Hermes deployment installs a checked-in non-diagnostic workspace policy', async () => {
|
||||
const [policy, readme] = await Promise.all([readFile(policyPath, 'utf8'), readFile(readmePath, 'utf8')]);
|
||||
assert.match(policy, /never diagnose/i);
|
||||
@@ -28,3 +37,16 @@ test('Hermes deployment installs a checked-in non-diagnostic workspace policy',
|
||||
assert.match(policy, /explicit user intent and confirmation/i);
|
||||
assert.match(readme, /install -m 600 docs\/TIMMY-AGENT-POLICY\.md/);
|
||||
});
|
||||
|
||||
test('the decision record keeps a backup versioned and refuses files it cannot read', async () => {
|
||||
const decision = await readFile(decisionPath, 'utf8');
|
||||
const readme = await readFile(readmePath, 'utf8');
|
||||
|
||||
assert.match(decision, /### Backup portability and entry provenance/);
|
||||
assert.match(decision, /schema version/i);
|
||||
assert.match(decision, /upgraded on import/i);
|
||||
assert.match(decision, /newer version is refused/i);
|
||||
assert.match(decision, /a refused import leaves the ledger unchanged/i);
|
||||
assert.match(decision, /provenance is stored on the confirmed log/i);
|
||||
assert.match(readme, /versioned JSON backup/i);
|
||||
});
|
||||
@@ -272,6 +272,14 @@ test('release demo visibly exercises the corpus refusal paths instead of only th
|
||||
assert.match(demo, /A non-stool photo gets a refusal, never a guess/);
|
||||
assert.match(demo, /Unreadable visual fields abstain as well/);
|
||||
assert.match(demo, /An out-of-range answer fails closed/);
|
||||
// Issue #32's quality gate: a confident answer about a low-quality photo must abstain on camera.
|
||||
assert.match(demo, /corpusCase\('poor-quality-confident'\)/);
|
||||
assert.match(demo, /tests\/fixtures\/vision-corpus\/blur-low-detail\.png/);
|
||||
assert.match(demo, /too blurry or poorly lit/);
|
||||
assert.match(demo, /must not prefill a form, however confident the reply looked/);
|
||||
assert.match(demo, /A confident reply about a blurry photo still abstains/);
|
||||
assert.match(builder, /A confident reply about a blurry photo still abstains — photo quality is part of the policy, not just confidence/);
|
||||
assert.match(builder, /"abstention_quality_gate": "passed"/);
|
||||
assert.match(demo, /no real photos in the corpus|no real photos in the recording/);
|
||||
// The refusal copy the user sees is gated on camera, so a release cannot drop it silently.
|
||||
assert.match(builder, /An out-of-range answer fails closed/);
|
||||
@@ -308,3 +316,112 @@ test('release demo shows readiness waiting for the loaded pinned model before AI
|
||||
// The caption is a required release gate, so the scene cannot be dropped silently.
|
||||
assert.match(builder, /Reachable is not loaded: no AI control, no consent, and no upload until the pinned model is resident/);
|
||||
});
|
||||
|
||||
test('release demo shows the training contribution as a separate opt-in receipt that can be withdrawn', async () => {
|
||||
const demo = await readFile(demoPath, 'utf8');
|
||||
const builder = await readFile(builderPath, 'utf8');
|
||||
const app = await readFile(new URL('../app.js', import.meta.url), 'utf8');
|
||||
|
||||
// Issue #24: the recording must show the opt-in section, the consent gate, and the withdrawal.
|
||||
assert.match(demo, /\[data-training-consent\]/);
|
||||
assert.match(demo, /\[data-contribute-entry="demo-wipe"\]/);
|
||||
assert.match(demo, /#training-consent-check/);
|
||||
assert.match(demo, /#contribute-confirm/);
|
||||
assert.match(demo, /\[data-contribution-state="queued-local"\]/);
|
||||
assert.match(demo, /data-withdraw=/);
|
||||
assert.match(demo, /\[data-withdrawn-artifacts\]/);
|
||||
assert.match(demo, /training-consent-v1/);
|
||||
// Every claim is measured in the browser before the caption that makes it.
|
||||
assert.match(demo, /a journal save must not queue a training contribution/);
|
||||
assert.match(demo, /the contribute control must stay disabled until the terms are accepted/);
|
||||
assert.match(demo, /no receipt may exist before explicit consent/);
|
||||
assert.match(demo, /a receipt must never copy the photo bytes/);
|
||||
assert.match(demo, /every derived artifact must be addressed by contribution id/);
|
||||
assert.match(demo, /delete-all must clear the contribution receipts as well/);
|
||||
// The controls are the app's own, not demo-only scaffolding.
|
||||
assert.match(app, /data-training-consent/);
|
||||
assert.match(app, /data-contribution-queued-count/);
|
||||
assert.match(app, /id="training-consent-check"/);
|
||||
assert.match(app, /id="contribute-confirm" disabled/);
|
||||
assert.match(app, /data-withdraw=/);
|
||||
// The captions and the gate key are required, so a later release cannot drop the scenes silently.
|
||||
assert.match(builder, /Training contribution is off by default: a saved log is never training data/);
|
||||
assert.match(builder, /The consent version and the receipt are on screen before the contribute button works/);
|
||||
assert.match(builder, /Withdrawing keeps a tombstone naming every derived artifact by contribution ID/);
|
||||
assert.match(builder, /"training_contribution_consent": "passed"/);
|
||||
assert.match(builder, /"test:training-contribution"/);
|
||||
});
|
||||
|
||||
test('release demo shows a versioned backup, an upgraded older file, and a refused newer one', async () => {
|
||||
const demo = await readFile(demoPath, 'utf8');
|
||||
const builder = await readFile(builderPath, 'utf8');
|
||||
const app = await readFile(new URL('../app.js', import.meta.url), 'utf8');
|
||||
|
||||
// Issue #35: the recording must export the real file, import an older version, refuse a newer one,
|
||||
// and reopen a restored log so its provenance is on screen — not merely described.
|
||||
assert.match(demo, /waitForEvent\('download'\)/);
|
||||
assert.match(demo, /exportedBackup\.schemaVersion !== 2/);
|
||||
assert.match(demo, /the backup must carry the withdrawn receipt as a tombstone/);
|
||||
assert.match(demo, /\[data-portability-notice\]\[data-portability-version="1"\]/);
|
||||
assert.match(demo, /\[data-portability-error\]/);
|
||||
assert.match(demo, /\[data-entry-provenance="ai-corrected"\]/);
|
||||
// Every claim is measured before the caption that makes it.
|
||||
assert.match(demo, /an older backup must be reported as upgraded/);
|
||||
assert.match(demo, /a refused import must not change the ledger it would have replaced/);
|
||||
assert.match(demo, /a restored log must still show that it began as a corrected AI suggestion/);
|
||||
// The controls belong to the app, and the provenance line is its own rendering.
|
||||
assert.match(app, /id="export"/);
|
||||
assert.match(app, /data-portability-notice/);
|
||||
assert.match(app, /data-entry-provenance=/);
|
||||
// Captions and the gate key are required, so a later release cannot drop the scenes silently.
|
||||
assert.match(builder, /A backup is versioned, and it carries the withdrawn receipt as a tombstone rather than silence/);
|
||||
assert.match(builder, /An older backup is upgraded on import, and the notice says which version the file came from/);
|
||||
assert.match(builder, /A file from a newer Timmy is refused with an instruction, and the ledger it would have replaced is untouched/);
|
||||
assert.match(builder, /A restored log still says it began as an AI suggestion you corrected — provenance is part of the record/);
|
||||
assert.match(builder, /"portable_backup_restore": "passed"/);
|
||||
assert.match(builder, /"test:portability"/);
|
||||
});
|
||||
|
||||
test('the builder refuses to gate or record a release against a foreign server', async () => {
|
||||
const builder = await readFile(builderPath, 'utf8');
|
||||
|
||||
// A leftover checkout answering on the acceptance port once satisfied every flow and the demo,
|
||||
// so the release described a tree that was not the commit under test. The builder must refuse a
|
||||
// live listener before it starts, then prove the served bytes are this tree's before any gate.
|
||||
assert.match(builder, /def assert_port_free/);
|
||||
assert.match(builder, /assert_port_free\("127\.0\.0\.1", 4173\)/);
|
||||
assert.match(builder, /def assert_served_tree/);
|
||||
assert.match(builder, /assert_served_tree\(tree, "127\.0\.0\.1", 4173\)/);
|
||||
const identityCheck = builder.indexOf('assert_served_tree(tree, "127.0.0.1", 4173)');
|
||||
const firstFlow = builder.indexOf('run(["npm", "run", "test:ui"]');
|
||||
assert.ok(identityCheck > 0, 'the served-tree check must be present');
|
||||
assert.ok(firstFlow > identityCheck, 'the served-tree check must run before the first acceptance flow');
|
||||
});
|
||||
|
||||
test('release demo replays the calibration harness against synthetic predictions instead of narrating the gates', async () => {
|
||||
const demo = await readFile(demoPath, 'utf8');
|
||||
const builder = await readFile(builderPath, 'utf8');
|
||||
|
||||
// Issue #29: the gate math is only proven by running the real harness on camera.
|
||||
assert.match(demo, /scripts\/calibration_report\.py/);
|
||||
assert.match(demo, /tests\/fixtures\/calibration\/predictions\.jsonl/);
|
||||
assert.match(demo, /tests\/fixtures\/calibration\/predictions-go\.jsonl/);
|
||||
assert.match(demo, /tests\/fixtures\/calibration\/predictions-leaky\.jsonl/);
|
||||
// All three verdict paths are exercised: blocked, passing, and refused before scoring.
|
||||
assert.match(demo, /the synthetic candidate fixtures must block the release/);
|
||||
assert.match(demo, /the engineered fixture must prove the passing branch/);
|
||||
assert.match(demo, /a leaked corpus must be refused before scoring/);
|
||||
// The card is drawn from the report the run wrote, and gated for readability like the corpus card.
|
||||
assert.match(demo, /id = 'calibration-report'/);
|
||||
assert.match(demo, /Calibration card is not fully readable on screen/);
|
||||
assert.match(demo, /the card must show the macro-F1 the harness measured/);
|
||||
assert.match(demo, /the card must show the refusal the leaked corpus earned/);
|
||||
// The scores are labelled as invented: a synthetic fixture is not a measurement of any model.
|
||||
assert.match(demo, /invented scores, not measurements of any model/);
|
||||
assert.match(demo, /synthetic predictions · no model was measured/);
|
||||
// The scene cannot be dropped silently: its captions are release gates.
|
||||
assert.match(builder, /The calibration harness is a command-line release gate, not a screen in the app/);
|
||||
assert.match(builder, /Every score on this card is invented to exercise the arithmetic/);
|
||||
assert.match(builder, /Each candidate is scored on test at a threshold chosen on validation only/);
|
||||
assert.match(builder, /One contributor spread across two splits is refused before anything is scored/);
|
||||
});
|
||||
@@ -44,11 +44,14 @@ async function dispatchFetch(handler, request) {
|
||||
}
|
||||
|
||||
test('root activation deletes only its obsolete Timmy caches including the legacy v4 cache', async () => {
|
||||
// The backup/provenance release (issue #35) changes app.js and src/domain.js, so the shell cache
|
||||
// must advance or an installed client keeps serving the old ledger code indefinitely.
|
||||
const { listeners, deleted } = loadWorker({
|
||||
cacheKeys: [
|
||||
'timmy-shell-v4',
|
||||
'timmy-shell:/:v4',
|
||||
'timmy-shell:/:v5',
|
||||
'timmy-shell:/:v6',
|
||||
'timmy-shell:/timmy-staging/:v4',
|
||||
'sibling-shared-origin-cache',
|
||||
],
|
||||
@@ -56,7 +59,7 @@ test('root activation deletes only its obsolete Timmy caches including the legac
|
||||
|
||||
await dispatchExtendable(listeners.get('activate'));
|
||||
|
||||
assert.deepEqual(deleted.sort(), ['timmy-shell-v4', 'timmy-shell:/:v4']);
|
||||
assert.deepEqual(deleted.sort(), ['timmy-shell-v4', 'timmy-shell:/:v4', 'timmy-shell:/:v5']);
|
||||
});
|
||||
|
||||
test('offline shell lookup uses only the current named cache', async () => {
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import { chromium } from 'playwright';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdir } from 'node:fs/promises';
|
||||
|
||||
// Issue #24 — contributing a confirmed photo to training is a separate opt-in. This runs the real
|
||||
// app in a browser: a journal save must record nothing, the consent sheet must show the version and
|
||||
// the receipt before the button works, and a withdrawal must leave a tombstone naming every derived
|
||||
// artifact. The photo is a synthetic 1x1 PNG constant; no real photo is loaded.
|
||||
const SYNTHETIC_PHOTO = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=';
|
||||
const CONSENT_VERSION = 'training-consent-v1';
|
||||
|
||||
await mkdir('artifacts', { recursive: true });
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, serviceWorkers: 'block' });
|
||||
const page = await context.newPage();
|
||||
const errors = [];
|
||||
page.on('console', message => { if (message.type() === 'error') errors.push(message.text()); });
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
page.on('dialog', dialog => dialog.accept());
|
||||
await page.goto('http://127.0.0.1:4173', { waitUntil: 'networkidle' });
|
||||
await page.evaluate(() => localStorage.clear());
|
||||
await page.reload({ waitUntil: 'networkidle' });
|
||||
|
||||
const basePath = () => page.evaluate(() => document.querySelector('meta[name="timmy-base-path"]')?.content || '/');
|
||||
const storeKey = async suffix => `timmy:${await basePath()}:${suffix}`;
|
||||
const readStore = async suffix => {
|
||||
const key = await storeKey(suffix);
|
||||
const raw = await page.evaluate(storeKeys => localStorage.getItem(storeKeys), key);
|
||||
return raw === null ? null : JSON.parse(raw);
|
||||
};
|
||||
const writeStore = async (suffix, value) => {
|
||||
const key = await storeKey(suffix);
|
||||
await page.evaluate(({ storeKey: k, payload }) => localStorage.setItem(k, payload), { storeKey: key, payload: JSON.stringify(value) });
|
||||
};
|
||||
|
||||
// 1. Saving a log through the app's own flow contributes nothing.
|
||||
await page.locator('[data-log]').first().click();
|
||||
await page.locator('[data-type="4"]').click();
|
||||
await page.locator('#next').click();
|
||||
await page.locator('#note').fill('Synthetic test entry');
|
||||
await page.locator('#next').click();
|
||||
await page.locator('#save').click();
|
||||
await page.getByText(/Private log saved/).waitFor();
|
||||
assert.equal(await readStore('training-contributions-v1'), null, 'a journal save must not record a training contribution');
|
||||
|
||||
// 2. Seed one photo-bearing log and one text-only log for the privacy section.
|
||||
const seeded = [
|
||||
{
|
||||
id: 'demo-photo', occurredAt: new Date(Date.now() - 86400000).toISOString(), bristolType: 4,
|
||||
color: 'brown', urgency: 1, discomfort: 0, note: 'synthetic', photoDataUrl: SYNTHETIC_PHOTO, symptoms: {},
|
||||
},
|
||||
{
|
||||
id: 'demo-text', occurredAt: new Date(Date.now() - 2 * 86400000).toISOString(), bristolType: 3,
|
||||
color: 'brown', urgency: 2, discomfort: 1, note: 'no photo here', photoDataUrl: '', symptoms: {},
|
||||
},
|
||||
];
|
||||
await writeStore('ledger-v1', seeded);
|
||||
await page.reload({ waitUntil: 'networkidle' });
|
||||
await page.locator('[data-view="calendar"]').first().click();
|
||||
await page.locator('[data-view="privacy"]').first().click();
|
||||
await page.getByText(/Your poop\. Your phone\./i).waitFor();
|
||||
|
||||
const section = page.locator('[data-training-consent]');
|
||||
assert.equal(await section.count(), 1, 'the privacy page must offer the training contribution section');
|
||||
assert.match(await section.innerText(), /off by default/i);
|
||||
assert.match(await section.innerText(), /nothing is uploaded/i);
|
||||
assert.equal((await page.locator('[data-contribution-consent-version]').innerText()).trim(), CONSENT_VERSION);
|
||||
assert.equal((await page.locator('[data-contribution-queued-count]').innerText()).trim(), '0');
|
||||
assert.equal(await page.locator('[data-contribute-entry="demo-photo"]').count(), 1, 'a saved photo may be contributed');
|
||||
assert.equal(await page.locator('[data-contribute-entry="demo-text"]').count(), 0, 'a log without photo bytes cannot be contributed');
|
||||
|
||||
// 3. The receipt and the consent version are on screen before the confirm control works.
|
||||
await page.locator('[data-contribute-entry="demo-photo"]').click();
|
||||
const sheet = page.locator('[data-contribution-sheet]');
|
||||
await sheet.waitFor();
|
||||
assert.match(await sheet.innerText(), new RegExp(CONSENT_VERSION));
|
||||
const receiptId = (await page.locator('[data-receipt-id]').innerText()).trim();
|
||||
assert.ok(receiptId.length > 0, 'the receipt shown before consent must name its own id');
|
||||
assert.match(await sheet.innerText(), /photo bytes/i);
|
||||
assert.match(await sheet.innerText(), /note text/i);
|
||||
assert.equal(await page.locator('#contribute-confirm').isDisabled(), true, 'the contribution button stays disabled until the terms are accepted');
|
||||
assert.equal(await readStore('training-contributions-v1'), null, 'no receipt may be written before explicit consent');
|
||||
await page.screenshot({ path: 'artifacts/training-contribution-consent-mobile.png' });
|
||||
|
||||
// 4. Consent records exactly one queued-on-device receipt.
|
||||
await page.locator('#training-consent-check').check();
|
||||
await page.locator('#contribute-confirm').click();
|
||||
await page.getByText(/Queued on this device/i).first().waitFor();
|
||||
const queued = await readStore('training-contributions-v1');
|
||||
assert.equal(queued.length, 1);
|
||||
assert.equal(queued[0].entryId, 'demo-photo');
|
||||
assert.equal(queued[0].consentVersion, CONSENT_VERSION);
|
||||
assert.equal(queued[0].contributionId, receiptId);
|
||||
assert.equal(queued[0].state, 'queued-local');
|
||||
assert.equal(JSON.stringify(queued).includes('data:image'), false, 'the receipt must not copy photo bytes');
|
||||
assert.equal(JSON.stringify(queued).includes('synthetic'), false, 'the receipt must not copy note text');
|
||||
await page.locator('[data-contribution-state="queued-local"]').waitFor();
|
||||
assert.equal((await page.locator('[data-contribution-queued-count]').innerText()).trim(), '1');
|
||||
await page.screenshot({ path: 'artifacts/training-contribution-queued-mobile.png' });
|
||||
|
||||
// 5. Withdrawal leaves a tombstone that addresses every derived artifact by contribution id.
|
||||
await page.locator(`[data-withdraw="${receiptId}"]`).click();
|
||||
await page.locator('[data-contribution-state="withdrawn"]').waitFor();
|
||||
const withdrawn = await readStore('training-contributions-v1');
|
||||
assert.equal(withdrawn.length, 1, 'the withdrawn receipt is kept as a tombstone, not silently erased');
|
||||
assert.equal(withdrawn[0].state, 'withdrawn');
|
||||
assert.ok(withdrawn[0].withdrawnAt, 'the tombstone records when the withdrawal happened');
|
||||
assert.deepEqual(withdrawn[0].artifacts.map(artifact => artifact.artifact).sort(), ['confirmed-label', 'raw-image']);
|
||||
for (const artifact of withdrawn[0].artifacts) {
|
||||
assert.equal(artifact.contributionId, receiptId);
|
||||
assert.equal(artifact.state, 'withdrawn');
|
||||
}
|
||||
assert.match(await page.locator('[data-withdrawn-artifacts]').innerText(), /raw image/i);
|
||||
assert.equal((await page.locator('[data-contribution-queued-count]').innerText()).trim(), '0');
|
||||
await page.screenshot({ path: 'artifacts/training-contribution-withdrawn-mobile.png' });
|
||||
|
||||
assert.deepEqual(errors, []);
|
||||
await context.close();
|
||||
await browser.close();
|
||||
console.log('PASS training contribution stays opt-in, shows its consent version before commit, and withdrawal names every derived artifact');
|
||||
@@ -0,0 +1,161 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
TRAINING_CONSENT_VERSION,
|
||||
addContribution,
|
||||
consentTerms,
|
||||
contributionEligibility,
|
||||
contributionForEntry,
|
||||
defaultContributionState,
|
||||
derivedArtifacts,
|
||||
receiptPreview,
|
||||
withdrawContribution,
|
||||
} from '../src/training-contribution.js';
|
||||
|
||||
// Issue #24 — contributing a photo to Timmy's training corpus is a separate decision from saving
|
||||
// a log. Nothing here is transmitted: the receipt is what a person could carry to a corpus pipeline
|
||||
// later, and the withdrawal tombstone is what names the artifacts a deletion has to reach.
|
||||
|
||||
const PHOTO = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=';
|
||||
const entry = (overrides = {}) => ({
|
||||
id: 'log-1',
|
||||
occurredAt: '2026-10-14T07:20:00.000Z',
|
||||
bristolType: 4,
|
||||
color: 'brown',
|
||||
urgency: 1,
|
||||
discomfort: 0,
|
||||
note: 'after lunch',
|
||||
photoDataUrl: PHOTO,
|
||||
symptoms: { blood: false },
|
||||
...overrides,
|
||||
});
|
||||
const preview = (target = entry(), id = 'contrib-1') => receiptPreview(target, {
|
||||
contributionId: id,
|
||||
consentedAt: '2026-10-21T10:00:00.000Z',
|
||||
});
|
||||
|
||||
test('saving a journal entry contributes nothing by default', () => {
|
||||
assert.deepEqual(defaultContributionState(), { optedIn: false, receipt: null });
|
||||
});
|
||||
|
||||
test('an entry with no saved photo bytes cannot be contributed', () => {
|
||||
const unavailable = contributionEligibility(entry({ photoDataUrl: '' }));
|
||||
assert.equal(unavailable.eligible, false);
|
||||
assert.match(unavailable.reason, /no saved photo/i);
|
||||
assert.equal(contributionEligibility(entry()).eligible, true);
|
||||
});
|
||||
|
||||
test('the consent terms name the version and what is never shared', () => {
|
||||
const terms = consentTerms();
|
||||
|
||||
assert.equal(terms.version, TRAINING_CONSENT_VERSION);
|
||||
assert.match(terms.version, /^training-consent-/);
|
||||
assert.ok(terms.includes.some(item => /photo/i.test(item)));
|
||||
assert.ok(terms.excludes.some(item => /note/i.test(item)));
|
||||
assert.ok(terms.excludes.some(item => /symptom/i.test(item)));
|
||||
// This build is honest about transport: a receipt is queued on the device, not uploaded.
|
||||
assert.match(terms.transport, /nothing is uploaded/i);
|
||||
});
|
||||
|
||||
test('the receipt shown before consent carries the version, the id, and no free text from the log', () => {
|
||||
const receipt = preview();
|
||||
|
||||
assert.equal(receipt.contributionId, 'contrib-1');
|
||||
assert.equal(receipt.consentVersion, TRAINING_CONSENT_VERSION);
|
||||
assert.equal(receipt.consentedAt, '2026-10-21T10:00:00.000Z');
|
||||
assert.deepEqual(receipt.labels, { bristolType: 4, color: 'brown' });
|
||||
assert.equal(receipt.state, 'queued-local');
|
||||
// The confirmed note and symptom flags stay in the journal; they are not part of a contribution.
|
||||
const serialized = JSON.stringify(receipt);
|
||||
assert.equal(serialized.includes('after lunch'), false);
|
||||
assert.equal(serialized.includes('"blood"'), false);
|
||||
assert.equal(serialized.includes('data:image'), false);
|
||||
});
|
||||
|
||||
test('a receipt cannot be recorded without the current consent version', () => {
|
||||
assert.throws(
|
||||
() => addContribution([], { ...preview(), consentVersion: 'training-consent-v0' }),
|
||||
/consent version/i,
|
||||
);
|
||||
});
|
||||
|
||||
test('recording a contribution returns a new list and leaves the ledger it was given alone', () => {
|
||||
const before = [];
|
||||
const after = addContribution(before, preview());
|
||||
|
||||
assert.equal(before.length, 0);
|
||||
assert.equal(after.length, 1);
|
||||
assert.equal(after[0].contributionId, 'contrib-1');
|
||||
assert.equal(contributionForEntry(after, 'log-1')?.contributionId, 'contrib-1');
|
||||
});
|
||||
|
||||
test('the same log cannot be contributed twice while its receipt is still active', () => {
|
||||
const first = addContribution([], preview());
|
||||
const second = addContribution(first, preview(entry(), 'contrib-2'));
|
||||
|
||||
assert.equal(second.length, 1);
|
||||
assert.equal(second[0].contributionId, 'contrib-1');
|
||||
});
|
||||
|
||||
test('withdrawal keeps a tombstone that names every derived artifact by contribution id', () => {
|
||||
const contributions = addContribution([], preview());
|
||||
const artifacts = derivedArtifacts(contributions[0]);
|
||||
|
||||
const { contributions: after, tombstone } = withdrawContribution(
|
||||
contributions, 'contrib-1', '2026-10-21T11:00:00.000Z',
|
||||
);
|
||||
|
||||
assert.equal(after[0].state, 'withdrawn');
|
||||
assert.equal(after[0].withdrawnAt, '2026-10-21T11:00:00.000Z');
|
||||
assert.equal(tombstone.contributionId, 'contrib-1');
|
||||
assert.deepEqual(
|
||||
tombstone.artifacts.map(artifact => artifact.artifact).sort(),
|
||||
artifacts.map(artifact => artifact.artifact).sort(),
|
||||
);
|
||||
for (const artifact of tombstone.artifacts) {
|
||||
assert.equal(artifact.contributionId, 'contrib-1');
|
||||
assert.equal(artifact.state, 'withdrawn');
|
||||
}
|
||||
});
|
||||
|
||||
test('the derived artifacts are the image bytes and the confirmed label, addressed by contribution id', () => {
|
||||
const artifacts = derivedArtifacts(preview());
|
||||
|
||||
assert.deepEqual(artifacts.map(artifact => artifact.artifact).sort(), ['confirmed-label', 'raw-image']);
|
||||
for (const artifact of artifacts) {
|
||||
assert.equal(artifact.contributionId, 'contrib-1');
|
||||
assert.equal(artifact.state, 'active');
|
||||
}
|
||||
});
|
||||
|
||||
test('withdrawing one contribution never touches another receipt', () => {
|
||||
const list = [
|
||||
...addContribution([], preview()),
|
||||
...addContribution([], preview(entry({ id: 'log-2' }), 'contrib-2')),
|
||||
];
|
||||
|
||||
const { contributions: after } = withdrawContribution(list, 'contrib-1', '2026-10-21T11:00:00.000Z');
|
||||
|
||||
assert.equal(after.find(item => item.contributionId === 'contrib-1').state, 'withdrawn');
|
||||
assert.equal(after.find(item => item.contributionId === 'contrib-2').state, 'queued-local');
|
||||
});
|
||||
|
||||
test('withdrawing an unknown contribution id changes nothing and reports no tombstone', () => {
|
||||
const list = addContribution([], preview());
|
||||
const { contributions: after, tombstone } = withdrawContribution(list, 'missing', '2026-10-21T11:00:00.000Z');
|
||||
|
||||
assert.equal(after, list);
|
||||
assert.equal(tombstone, null);
|
||||
});
|
||||
|
||||
test('a withdrawn log can be contributed again under a fresh receipt', () => {
|
||||
const { contributions: withdrawn } = withdrawContribution(
|
||||
addContribution([], preview()), 'contrib-1', '2026-10-21T11:00:00.000Z',
|
||||
);
|
||||
|
||||
const again = addContribution(withdrawn, preview(entry(), 'contrib-2'));
|
||||
|
||||
assert.equal(again.length, 2);
|
||||
assert.equal(contributionForEntry(again, 'log-1').contributionId, 'contrib-2');
|
||||
assert.equal(contributionForEntry(again, 'log-1').state, 'queued-local');
|
||||
});
|
||||
@@ -14,6 +14,7 @@ test('training ingest strips metadata, derives a pseudonymous split, and writes
|
||||
'--output-dir', out,
|
||||
'--subject-id', 'subject-123',
|
||||
'--consent-version', 'research-v1',
|
||||
'--contribution-id', '6f1d0c9e-2c4a-4f3b-9d1e-8a7b6c5d4e3f',
|
||||
'--bristol-type', '4',
|
||||
'--color', 'brown',
|
||||
'--quality', 'good',
|
||||
@@ -45,10 +46,11 @@ test('training ingest rejects records without explicit consent or human review',
|
||||
'--input', 'tests/fixtures/synthetic-type4.jpg',
|
||||
'--output-dir', '/tmp/should-not-exist-timmy',
|
||||
'--subject-id', 'subject-123',
|
||||
'--contribution-id', '6f1d0c9e-2c4a-4f3b-9d1e-8a7b6c5d4e3f',
|
||||
'--bristol-type', '4',
|
||||
'--color', 'brown',
|
||||
'--quality', 'good',
|
||||
], { encoding: 'utf8', env: { ...process.env, TIMMY_DATASET_SALT: 'test-only-salt' } });
|
||||
assert.notEqual(run.status, 0);
|
||||
assert.match(run.stderr, /consent|reviewer/i);
|
||||
assert.match(run.stderr, /consent/i);
|
||||
});
|
||||
Reference in new issue
Block a user