| [780a7c6] | 1 | /**
|
|---|
| 2 | * Guardianship (FEP-633c §3) — the multi-party handshake state.
|
|---|
| 3 | *
|
|---|
| 4 | * A faithful port of the Shaer test daemon's `Handshake`, persisted per local
|
|---|
| 5 | * site (so the two implementations behave identically and the clients speak
|
|---|
| 6 | * one contract). One row in ap_guardian_offers per offer this instance is a
|
|---|
| 7 | * party to; the accepts accumulate in ap_guardian_offer_accepts.
|
|---|
| 8 | *
|
|---|
| 9 | * The offer commits only when the guardian-candidate returns the handle
|
|---|
| 10 | * (§3.1.3) after ward + candidate + at least one existing guardian have
|
|---|
| 11 | * accepted (§3.1.2). A single Reject from any party voids it (§3.2). This is
|
|---|
| 12 | * the core safety property: no single party creates a guardianship alone, and
|
|---|
| 13 | * no new guardian is added without an existing guardian's consent.
|
|---|
| 14 | */
|
|---|
| 15 | import db from '../../config/database.js';
|
|---|
| 16 |
|
|---|
| 17 | let _s = null;
|
|---|
| 18 | function stmts() {
|
|---|
| 19 | if (!_s) {
|
|---|
| 20 | _s = {
|
|---|
| 21 | insOffer: db.prepare(`INSERT OR IGNORE INTO ap_guardian_offers
|
|---|
| 22 | (offer_id, slug, ward_uri, candidate_uri, existing_guardians, status, ward_handle, candidate_handle, created_at)
|
|---|
| 23 | VALUES (?,?,?,?,?, 'pending', ?, ?, CURRENT_TIMESTAMP)`),
|
|---|
| 24 | getOffer: db.prepare('SELECT * FROM ap_guardian_offers WHERE slug=? AND offer_id=?'),
|
|---|
| 25 | offerAnywhere: db.prepare('SELECT * FROM ap_guardian_offers WHERE offer_id=? LIMIT 1'),
|
|---|
| 26 | setStatus: db.prepare('UPDATE ap_guardian_offers SET status=?, handle=COALESCE(?, handle) WHERE slug=? AND offer_id=?'),
|
|---|
| 27 | listBySlug: db.prepare("SELECT * FROM ap_guardian_offers WHERE slug=? AND status='pending' ORDER BY created_at DESC"),
|
|---|
| 28 | insAccept: db.prepare('INSERT OR IGNORE INTO ap_guardian_offer_accepts (offer_id, slug, party_uri, created_at) VALUES (?,?,?,CURRENT_TIMESTAMP)'),
|
|---|
| 29 | accepts: db.prepare('SELECT party_uri FROM ap_guardian_offer_accepts WHERE slug=? AND offer_id=?'),
|
|---|
| 30 | };
|
|---|
| 31 | }
|
|---|
| 32 | return _s;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | const parties = (o) => [o.ward_uri, o.candidate_uri, ...JSON.parse(o.existing_guardians || '[]')];
|
|---|
| 36 | const isParty = (o, actor) => !!actor && parties(o).includes(actor);
|
|---|
| 37 | const acceptsOf = (o) => stmts().accepts.all(o.slug, o.offer_id).map((r) => r.party_uri);
|
|---|
| 38 |
|
|---|
| 39 | /** ward + candidate + (no existing guardians OR at least one existing) accepted. */
|
|---|
| 40 | export function readyToCommit(o) {
|
|---|
| 41 | if (!o || o.status !== 'pending') return false;
|
|---|
| 42 | const acc = new Set(acceptsOf(o));
|
|---|
| 43 | const existing = JSON.parse(o.existing_guardians || '[]');
|
|---|
| 44 | const existingOk = existing.length === 0 || existing.some((g) => acc.has(g));
|
|---|
| 45 | return acc.has(o.ward_uri) && acc.has(o.candidate_uri) && existingOk;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /** Start tracking an offer on `slug` (idempotent). */
|
|---|
| 49 | export function start(slug, { offerId, ward, candidate, existingGuardians = [], wardHandle = null, candidateHandle = null }) {
|
|---|
| 50 | stmts().insOffer.run(offerId, slug, ward, candidate, JSON.stringify(existingGuardians || []), wardHandle, candidateHandle);
|
|---|
| 51 | return stmts().getOffer.get(slug, offerId);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | export function getOffer(slug, offerId) { return stmts().getOffer.get(slug, offerId); }
|
|---|
| 55 | export function findOfferAnywhere(offerId) { return stmts().offerAnywhere.get(offerId); }
|
|---|
| 56 |
|
|---|
| 57 | /** Record an Accept from one party; ignored if not a party or already resolved. */
|
|---|
| 58 | export function recordAccept(slug, offerId, party) {
|
|---|
| 59 | const o = stmts().getOffer.get(slug, offerId);
|
|---|
| 60 | if (!o || o.status !== 'pending' || !isParty(o, party)) return o;
|
|---|
| 61 | stmts().insAccept.run(offerId, slug, party);
|
|---|
| 62 | return stmts().getOffer.get(slug, offerId);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /** A single Reject from any party voids the handshake (§3.2). */
|
|---|
| 66 | export function recordReject(slug, offerId, party) {
|
|---|
| 67 | const o = stmts().getOffer.get(slug, offerId);
|
|---|
| 68 | if (!o || o.status !== 'pending' || !isParty(o, party)) return o;
|
|---|
| 69 | stmts().setStatus.run('void', null, slug, offerId);
|
|---|
| 70 | return stmts().getOffer.get(slug, offerId);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Commit (only when ready): store the returned handle, mark committed. */
|
|---|
| 74 | export function commit(slug, offerId, handle) {
|
|---|
| 75 | const o = stmts().getOffer.get(slug, offerId);
|
|---|
| 76 | if (!o || o.status !== 'pending' || !readyToCommit(o)) return null;
|
|---|
| 77 | stmts().setStatus.run('committed', handle || null, slug, offerId);
|
|---|
| 78 | return stmts().getOffer.get(slug, offerId);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /** Pending offers where `me` is a party — the offers queue (daemon shape). */
|
|---|
| 82 | export function listForParty(slug, me) {
|
|---|
| 83 | return stmts().listBySlug.get ? stmts().listBySlug.all(slug).filter((o) => isParty(o, me)) : [];
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /** One offer as the offers-queue item the Shaer clients parse. */
|
|---|
| 87 | export function queueItem(o, me) {
|
|---|
| 88 | const acc = acceptsOf(o).sort();
|
|---|
| 89 | return {
|
|---|
| 90 | id: o.offer_id,
|
|---|
| 91 | type: 'Offer',
|
|---|
| 92 | actor: o.candidate_uri,
|
|---|
| 93 | object: { type: 'Relationship', subject: o.ward_uri, relationship: 'shaer:Guardian', object: o.candidate_uri },
|
|---|
| 94 | 'shaer:ward': o.ward_uri,
|
|---|
| 95 | 'shaer:candidate': o.candidate_uri,
|
|---|
| 96 | 'shaer:existingGuardians': JSON.parse(o.existing_guardians || '[]'),
|
|---|
| 97 | 'shaer:acceptedBy': acc,
|
|---|
| 98 | 'shaer:needsMyAccept': !acc.includes(me),
|
|---|
| 99 | 'shaer:readyToCommit': readyToCommit(o),
|
|---|
| 100 | 'shaer:iAmCandidate': me === o.candidate_uri,
|
|---|
| 101 | 'shaer:wardHandle': o.ward_handle || undefined,
|
|---|
| 102 | 'shaer:candidateHandle': o.candidate_handle || undefined,
|
|---|
| 103 | published: o.created_at,
|
|---|
| 104 | };
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | export { parties, isParty, acceptsOf };
|
|---|
| 108 | export default {
|
|---|
| 109 | start, getOffer, findOfferAnywhere, recordAccept, recordReject, commit,
|
|---|
| 110 | readyToCommit, listForParty, queueItem, parties, isParty, acceptsOf,
|
|---|
| 111 | };
|
|---|