function createHumanGates(options = {}) { const storage = options.storage || window.localStorage; const getLogin = options.getLogin || (() => ''); const getAccountKey = options.getAccountKey || getLogin; const isOnline = options.isOnline || (() => navigator.onLine); const location = options.location || window.location; const fetchJson = options.fetchJson; const nodes = options.nodes || {}; let queue = { pending_count: 0, items: [] }; let reviewSnapshot = []; let reviewIndex = -1; let loadedAccountKey = ''; let loadEpoch = 0; const decisionKeys = new Map(); let decisionFlight = null; const escape = value => String(value ?? '').replace(/[&<>"']/g, character => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', })[character]); const cacheKey = () => 'stackchain.human-gates.v1:' + String(getAccountKey() || '').trim().toLowerCase(); const setText = (node, value) => { if (node) node.textContent = value; }; const setHtml = (node, value) => { if (node) node.innerHTML = value; }; const publish = state => options.onChange?.(JSON.parse(JSON.stringify(queue)), state); function validSnapshot(value) { return value && Number.isInteger(value.pending_count) && Array.isArray(value.items) ? value : null; } function restore() { if (!getLogin()) return null; try { return validSnapshot(JSON.parse(storage.getItem(cacheKey()) || 'null')); } catch (_) { return null; } } function save(value) { if (!getLogin()) return; try { storage.setItem(cacheKey(), JSON.stringify(value)); } catch (_) {} } function render() { setText(nodes.count, String(queue.pending_count)); if (!queue.pending_count) { setHtml(nodes.list, '
Inbox zeroNo release candidates need your decision.
'); setText(nodes.status, 'Human Gates inbox zero.'); return; } setText(nodes.status, queue.pending_count + (queue.pending_count === 1 ? ' gate pending.' : ' gates pending.')); setHtml(nodes.list, queue.items.map(item => '' ).join('')); } function renderDetail(item) { if (!item) { setHtml(nodes.detail, '
Inbox zeroFixed review snapshot complete.
'); return; } const checks = (item.checks || []).map(check => '
  • ' + escape(check.name) + ' · ' + escape(check.state) + (check.required ? ' · required' : '') + '
  • ' ).join(''); const artifacts = (item.artifacts || []).map(artifact => '
  • ' + escape(artifact.name) + '
  • ').join(''); const links = (item.links || []).map(link => '
  • ' + escape(link.label) + '
  • ').join(''); const provenance = Object.entries(item.provenance || {}).map(([key, value]) => '
  • ' + escape(key) + ' · ' + escape(value) + '
  • ').join(''); const history = (item.history || []).map(event => '
  • ' + escape(event.action) + ' · ' + escape(event.at) + '
  • ').join(''); setHtml(nodes.detail, '

    ' + escape(item.title) + '

    ' + '

    Project ' + escape(item.project) + '

    ' + '

    Exact candidate ' + escape(item.candidate_hash) + '

    ' + '

    Score ' + escape(item.score?.value ?? 'not supplied') + ' · ' + escape(item.score?.provenance || '') + '

    ' + '

    Artifacts

    Links

    ' + '

    Checks

    Provenance

    ' + '

    History

    ' + '' + '' + '' + '' + '' + '
    ' + '
    ' ); } async function load() { const epoch = ++loadEpoch; const accountKey = String(getAccountKey() || '').trim().toLowerCase(); if (accountKey !== loadedAccountKey) { loadedAccountKey = accountKey; queue = { pending_count: 0, items: [] }; reviewSnapshot = []; reviewIndex = -1; render(); } const cached = restore(); if (cached) { queue = cached; render(); publish({available:true, authoritative:false, cached:true}); } try { const live = validSnapshot(await fetchJson('api/v1/human-gates')); if (epoch !== loadEpoch || accountKey !== loadedAccountKey) return queue; if (!live) throw new Error('Human Gates response is invalid.'); queue = { pending_count: live.pending_count, items: live.items.slice() }; save(queue); render(); publish({available:true, authoritative:true}); return queue; } catch (error) { if (epoch !== loadEpoch || accountKey !== loadedAccountKey) return queue; if (!cached) { publish({available:false, authoritative:false}); throw error; } setText(nodes.status, 'Offline cached gate list · reconnect before deciding.'); return queue; } } function reviewNext() { if (reviewIndex < 0) { reviewSnapshot = queue.items.slice(); reviewIndex = 0; } const item = reviewSnapshot[reviewIndex] || null; renderDetail(item); if (item) { const index = reviewIndex; fetchJson('api/v1/human-gates/' + encodeURIComponent(item.id)).then(detail => { if (!detail || detail.id !== item.id) throw new Error('Gate detail is invalid.'); if (reviewIndex !== index || reviewSnapshot[index]?.id !== item.id) return; reviewSnapshot[index] = detail; renderDetail(detail); }).catch(() => { setText(nodes.status, 'Gate detail is unavailable. Retry while online.'); }); } return item; } function select(gateId) { if (reviewIndex < 0) reviewSnapshot = queue.items.slice(); const index = reviewSnapshot.findIndex(item => item.id === gateId); if (index < 0) throw new Error('Gate is not in the current review snapshot.'); reviewIndex = index; return reviewNext(); } function current() { return reviewIndex < 0 ? null : (reviewSnapshot[reviewIndex] || null); } function idempotencyKey(item, decision, payload) { const operation = item.id + ':' + item.revision + ':' + decision + ':' + JSON.stringify(payload); if (decisionKeys.has(operation)) return { operation, key: decisionKeys.get(operation) }; const nonce = globalThis.crypto?.randomUUID?.() || (Date.now().toString(36) + '-' + Math.random().toString(36).slice(2)); const key = 'human-gate:' + item.id + ':' + item.revision + ':' + decision + ':' + nonce; decisionKeys.set(operation, key); return { operation, key }; } async function decideAndNext(decision, values = {}) { const item = current(); if (!item) throw new Error('No gate is selected.'); if (!isOnline()) throw new Error('Human Gate decisions require an online connection.'); if (!String(getLogin() || '').trim()) throw new Error('Authenticated account identity is required.'); const checklist = values.checklist || {}; const complete = ['exact_hash', 'artifacts_reviewed', 'provenance_reviewed'].every(key => checklist[key] === true); if (decision === 'release' && !complete) throw new Error('Complete the release checklist before deciding.'); const unmet = (item.checks || []).filter(check => check.required && check.state !== 'success'); if (decision === 'release' && unmet.length && !String(values.override_reason || '').trim()) { throw new Error('An explicit override reason is required for unmet required checks.'); } if (decision === 'hold' && !String(values.reason || '').trim()) throw new Error('A hold reason is required.'); const payload = { expected_revision: item.revision, decision, reason: String(values.reason || '').trim(), override_reason: String(values.override_reason || '').trim(), checklist, }; if (decisionFlight) return decisionFlight; const operation = (async () => { const decisionKey = idempotencyKey(item, decision, payload); const receipt = await fetchJson('api/v1/human-gates/' + encodeURIComponent(item.id) + '/decision', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Idempotency-Key': decisionKey.key }, body: JSON.stringify(payload), }); decisionKeys.delete(decisionKey.operation); queue.items = queue.items.filter(candidate => candidate.id !== item.id); queue.pending_count = Math.max(0, queue.pending_count - 1); save(queue); render(); publish({available:true, authoritative:true, decision:true}); reviewIndex += 1; const next = current(); if (next) reviewNext(); else renderDetail(null); setText(nodes.status, next ? (decision === 'release' ? 'Released. Reviewing next gate.' : 'Held. Reviewing next gate.') : 'Decision saved. Human Gates review snapshot complete.'); return { receipt, next }; })(); decisionFlight = operation; try { return await operation; } finally { if (decisionFlight === operation) decisionFlight = null; } } function open() { location.hash = '#/my-work/human-gates'; if (nodes.panel) nodes.panel.hidden = false; return load().then(() => reviewNext()); } return { load, open, reviewNext, select, decideAndNext, current, restoreCached: restore, snapshot: () => JSON.parse(JSON.stringify(queue)), route: () => location.hash, }; } if (typeof module !== 'undefined') module.exports = createHumanGates; if (typeof window !== 'undefined') window.createHumanGates = createHumanGates;