web/website/node/alpha-reset-review.mjs
2026-11-27 23:43:05 +00:00

33 lines
4.9 KiB
JavaScript

import {createHash} from 'node:crypto';
const hash=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex');
const sorted=values=>[...new Set(values)].sort();
// Deliberately separate from the historical broad reset proposal. This current
// review has no executor and cannot authorize a key, mutation or startup reset.
export function buildAlphaResetReview(inventory,{ownerId,release,observedAt}={}){
if(!ownerId||!release||!Number.isSafeInteger(observedAt))throw TypeError('Current inventory identity required');
for(const field of ['clans','companies','relations','clanInstitutions','companyInstitutions','claims','foundingKeys','accounts','orders','auctionHolds','auctions','deedPurchases','loans','creditActions','creditInstalments','questRewards','polRewards','listings','kitListings','projectJobs','payrollItems','additionalPayrollItems'])if(!Array.isArray(inventory[field]))throw TypeError('Incomplete current inventory: '+field);
const ownClanIds=inventory.clans.filter(c=>c.owner_id===ownerId).map(c=>c.id),ownCompanyIds=inventory.companies.filter(c=>c.owner_account_id===ownerId||c.founded_by===ownerId).map(c=>c.id);
const protectedObjects=new Set([...inventory.clanInstitutions.map(r=>'clan:'+r.clan_id),...inventory.companyInstitutions.map(r=>'company:'+r.company_id)]);
const ownerNetwork=new Set([...ownClanIds.map(id=>'clan:'+id),...ownCompanyIds.map(id=>'company:'+id)]);
let changed=true;while(changed){changed=false;for(const r of inventory.relations){const parent=r.parent_kind+':'+r.parent_id,child=r.child_kind+':'+r.child_id;if(ownerNetwork.has(parent)&&!ownerNetwork.has(child)){ownerNetwork.add(child);changed=true;}}}
const unresolved={orders:inventory.orders.filter(r=>!['posted','failed','cancelled','rejected'].includes(r.state)),auctionHolds:inventory.auctionHolds.filter(r=>!['released','settled','cancelled'].includes(r.state)),auctions:inventory.auctions.filter(r=>!['completed','cancelled','closed','settled'].includes(r.state)),deedPurchases:inventory.deedPurchases.filter(r=>!['posted','cancelled','failed'].includes(r.state)),loans:inventory.loans.filter(r=>!['repaid','closed','cancelled','rejected','expired'].includes(r.state)),creditActions:inventory.creditActions.filter(r=>!['posted','completed','cancelled','failed','rejected'].includes(r.state)),creditInstalments:inventory.creditInstalments.filter(r=>r.state!=='paid'),questRewards:inventory.questRewards.filter(r=>!['delivered','paid','cancelled'].includes(r.state)),polRewards:inventory.polRewards.filter(r=>!['delivered','paid','cancelled'].includes(r.state)),reservations:inventory.accounts.filter(r=>BigInt(r.reserved_minor)!==0n)};
unresolved.projectJobs=inventory.projectJobs.filter(r=>!['completed','applied','cancelled','failed'].includes(r.state));unresolved.payrollItems=inventory.payrollItems;unresolved.additionalPayrollItems=inventory.additionalPayrollItems;
const obligations=Object.fromEntries(Object.entries(unresolved).map(([kind,rows])=>[kind,{count:rows.length,rows}]));
const proposed={
// Preserve all organisations by default. Latest owner exceptions, descendants
// and previously archived objects are explicit, never inferred from names.
preserveOrganisations:sorted([...inventory.clans.map(r=>'clan:'+r.id),...inventory.companies.map(r=>'company:'+r.id)]),
protectedObjects:sorted(protectedObjects),ownerNetwork:sorted(ownerNetwork),
alreadyArchivedClanIds:sorted(inventory.clans.filter(r=>!Number(r.active)).map(r=>r.id)),
archiveClanIds:[],deleteCompanyIds:[],charterChanges:[],rosterChanges:[],homeBlockChanges:[],claimChanges:[],
retainKitListingIds:sorted(inventory.kitListings.map(r=>r.id)),
nonKitListingIdsForReview:sorted(inventory.listings.filter(r=>r.state==='open').map(r=>r.id)),
closeListingIds:[],balanceZeroAccountIds:sorted(inventory.accounts.map(r=>String(r.id))),
foundingEntitlementChanges:[],newBudget:null,newEconomicReference:null,
preserveAllHistoricalKeys:true,preserveContractsAndFinancialAudit:true,stagingDeletionIds:[]
};
return {schemaVersion:1,ticketId:409,release,observedAt,readOnly:true,executionAuthorized:false,executable:false,inventoryHash:hash(inventory),proposedManifestHash:hash(proposed),inventory,obligations,proposed,
gates:['Confirm this exact manifest hash and the current owner exceptions before any execution.','Re-inventory and reject any stale manifest before issuing a reset key.','Resolve each current obligation through its existing domain adapter; no blanket reservation cancellation.','Approve a funded new budget and economic reference; no test-history or zero-denominator baseline.','No automatic entitlement reactivation. Any later replacement must preserve history and enforce one live entitlement.','No reset at startup, deployment or review generation. No additional staging deletion.'],
snapshotConsistency:'Each database is a separate read-only repeatable-read snapshot. The report does not claim a cross-database atomic snapshot; the final execution gate requires fresh reconciliation.'};
}