@amalgm/live 0.2.81-preview.47452527575 → 0.2.81-preview.47491680470
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/apply/journey.d.ts +1 -1
- package/dist/apply/journey.js +1 -1
- package/dist/apply/line.js +1 -1
- package/dist/apply/rail.js +1 -1
- package/dist/clash/local-work.d.ts +1 -0
- package/dist/clash/local-work.js +1 -1
- package/dist/detection/package-proposal.js +1 -1
- package/dist/records/contracts.js +1 -1
- package/dist-cjs/apply/journey.js +1 -1
- package/dist-cjs/apply/line.js +1 -1
- package/dist-cjs/apply/rail.js +1 -1
- package/dist-cjs/clash/local-work.js +1 -1
- package/dist-cjs/detection/package-proposal.js +1 -1
- package/dist-cjs/records/contracts.js +1 -1
- package/package.json +4 -4
package/dist/apply/journey.d.ts
CHANGED
|
@@ -21,5 +21,5 @@ export declare class EntityApplyJourney {
|
|
|
21
21
|
constructor(options: EntityApplyRailOptions, wakeVersion: () => number, stopping: () => boolean);
|
|
22
22
|
targetOf(window: ApplyWindow): number | null;
|
|
23
23
|
run(window: ApplyWindow, emptying: () => string | null): Promise<HeadOutcome>;
|
|
24
|
-
alone(
|
|
24
|
+
alone(window: ApplyWindow, head?: WindowMember): Promise<HeadOutcome>;
|
|
25
25
|
}
|
package/dist/apply/journey.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var j=Object.defineProperty;var
|
|
1
|
+
var j=Object.defineProperty;var w=(l,e)=>j(l,"name",{value:e,configurable:!0});import{createApplyIntent as F,createApplyWindowIntent as O}from"./contracts.js";import{planSelectedEntityApply as P}from"./plans.js";import{Recovering as M}from"../faults/recover.js";import{groundMoved as S}from"../faults/classify.js";import{localWorkWins as W}from"../clash/local-work.js";import{selectEntityApply as E}from"./selection.js";import{isSettlement as q}from"./settling.js";import{revealOrder as D}from"./window.js";function H(l,e){return l.unit===null&&l.authorityId!==e}w(H,"stepsAside");const I={kind:"park",reason:"its window ended with its head unplaced and named no wait",releaseOnProgress:!1,report:!1,again:!0},U=w(l=>l.kind==="captured-local"||l.kind==="rejudge","drops");function A(l){return l.kind==="retry"?{kind:"park",reason:l.reason??"machine truth is unsettled",releaseOnProgress:!1,report:!1}:l.kind==="dependency"?{kind:"park",reason:l.reason,releaseOnProgress:l.waitFor===void 0,report:!1,...l.waitFor?{dependency:l.waitFor}:{},...l.blocker?{blocker:l.blocker}:{},..."comparedAt"in l?{comparedAt:l.comparedAt}:{}}:l.kind==="conflict"?{kind:"park",reason:l.reason,releaseOnProgress:!0,report:!0}:null}w(A,"parkFor");async function B(l){try{return await l()}catch(e){return{kind:"park",reason:e instanceof Error?e.message:String(e),releaseOnProgress:!1,report:!S(e),cause:e}}}w(B,"outcomeOf");const v=w(l=>q(l.judgment)?{kind:"settled",settlement:l.judgment}:null,"settled");class C{static{w(this,"EntityApplyJourney")}#e;#r;#a;#n;constructor(e,t,a){this.#e=e,this.#r={sha256Hex:e.sha256Hex,loops:e.loops,deliveryPosition:e.deliveryPosition},this.#a=t,this.#n=a}targetOf(e){const t=e.members[0];return t===void 0?null:e.intent?.members.find(a=>a.entityId===t.work.entityId)?.throughSequence??(t.work.kept===null?t.work.records.at(-1)?.globalSequence:t.work.resolvedThrough)??null}async run(e,t){if(e.intent!==null)return this.#p(e,e.intent);const a={member:null};try{return await this.#d(e,t,a)}catch(i){if(a.member===null||this.#n())throw i;if(S(i))return{kind:"park",reason:i instanceof Error?i.message:String(i),releaseOnProgress:!1,report:!1};if(H(e,a.member.work.entityId))return this.alone(e,a.member);if(i instanceof M)return{kind:"park",reason:i.message,releaseOnProgress:!1,report:!1,dependency:{producer:"capability",entityIds:i.need.subject===null?[]:[i.need.subject.id]}};throw i}}async alone(e,t=e.members[0]){const{target:a}=E(t.work,this.#r),i=await this.#i({authorityId:t.work.authorityId,entityId:t.work.entityId,targetType:a.result.type,kept:a.record===null?a.result:null,throughSequence:a.throughSequence}),n=await this.#t(e.intent,[t],[{kind:"settled",settlement:i?"superseded":"waiting"}]);return n==="progress"&&!i?{kind:"alone",entityId:t.work.entityId}:n}async#d(e,t,a){const i=this.#e.newIntentId(),n=this.#e.now(),o=t(),s=new Map;for(const c of e.members){if(v(c))continue;const{work:f}=c,m=E(f,this.#r),b=F({attemptId:i,work:f,target:m.target,unit:e.unit,cutoff:e.cutoff,createdAt:n,emptied:f.entityId===o&&f.play==="enter"&&(m.target.result.type==="file.text"||m.target.result.type==="file.binary")}),k=P(f,m,this.#e.sha256Hex);s.set(f.entityId,{member:c,intent:b,plan:k,adapter:this.#e.effects.adapterFor(k.target.type),look:"safe"})}const{active:d,blocked:h}=await this.#o(e.members,s,"look",a);if(d.length===0)return h??I;const g=d.flatMap(c=>s.get(c.work.entityId)??[]);if(g.length===0)return this.#t(null,d,d.map(c=>v(c)));const y=O({attemptId:i,authorityId:e.authorityId,unit:e.unit,cutoff:e.cutoff,createdAt:n,members:g.map(c=>c.intent)});try{const c=await this.#u(d,s,a),f=c.active.flatMap(r=>s.get(r.work.entityId)??[]);if(f.length===0)return await this.#e.effects.discard(y),c.active.length===0?c.blocked??I:this.#t(null,c.active,c.active.map(r=>v(r)));const m=f.length===g.length?y:O({...y,members:f.map(r=>r.intent)}),b=await this.#e.effects.hold(m,f.map(r=>r.plan));let k,x;try{k=await this.#o(c.active,s,"final",a),x=await this.#c(m,k,s,t)}finally{b()}return k.unseen!==null&&k.active.length===0?this.#h(k.unseen):x}catch(c){throw await this.#e.effects.discard(y),c}}async#c(e,{active:t,blocked:a},i,n){const o=t.flatMap(r=>i.get(r.work.entityId)??[]);if(o.length===0)return await this.#e.effects.discard(e),t.length===0?a??I:this.#t(null,t,t.map(r=>v(r)));const s=O({...e,members:o.map(r=>r.intent)}),d=o.filter(r=>r.look==="safe");if(d.length>0){if(this.#n()||o.some(p=>p.intent.emptied&&n()!==p.intent.entityId))return await this.#e.effects.discard(e),I;const r=await this.#e.store.bindWindow(s);if(r)return await this.#e.effects.discard(e),r.kind==="stale"?I:A({...r,comparedAt:this.#a()})}const h=new Map,g=new Map,y=w((r,p,u=null)=>{h.set(r,{kind:"owed",reason:p}),u!==null&&g.set(r,u)},"owe"),c=w(r=>A(r.kind==="dependency"?{...r,comparedAt:this.#a()}:r),"waitFrom"),{order:f,cycle:m}=D(d.map(r=>({entityId:r.intent.entityId,play:r.plan.play,type:r.plan.target.type,parentUUID:r.plan.target.parentUUID}))),b="its parent chain closes among the window's members";for(const r of m)y(r,b,{kind:"park",reason:b,releaseOnProgress:!1,report:!0});for(const r of f){const p=i.get(r),u=await p.adapter.apply.reveal(p.intent,p.plan);u&&y(r,u.reason,u.kind==="dependency"?c(u):null)}for(const r of o){const p=r.intent.entityId;if(h.has(p))continue;const u=await r.adapter.verify.verify(r.intent,r.plan);u.kind==="match"?h.set(p,{kind:"materialized"}):u.kind==="local-change"?y(p,"a person's version stands in its place"):u.kind==="rejudge"||u.kind==="missing-target"?y(p,u.kind):y(p,u.reason??u.kind,c(u))}const k=t.map(r=>v(r)??h.get(r.work.entityId));if(d.length>0)return this.#s(s,t,k,g);const x=await this.#s(null,t,k,g);return await this.#e.effects.discard(e),x}async#s(e,t,a,i){const n=await this.#t(e,t,a),o=a[0];return n!=="progress"||o.kind!=="owed"?n:{...i.get(t[0].work.entityId)??{kind:"park",reason:o.reason,releaseOnProgress:!1,report:!1,again:!0},advanced:t.filter((d,h)=>a[h].kind!=="owed").map(d=>d.work.entityId)}}async#o(e,t,a,i){const n=[];for(const o of e){const s=t.get(o.work.entityId);if(!s){n.push(o);continue}let d;try{d=await this.#i(s.intent)?{kind:"captured-local"}:a==="look"?await this.#f(s):await s.adapter.apply.guard(s.intent,s.plan,a)}catch(g){if(n.length>0&&!this.#n())return{active:n,blocked:null,unseen:null};throw i.member=o,g}if(d.kind==="local-change"||d.kind==="unsettled-ground")return{active:n,blocked:n.length===0?I:null,unseen:s};if(U(d))return{active:n,blocked:n.length===0?I:null,unseen:null};const h=A(d);if(h)return{active:n,blocked:h,unseen:null};s.look=d.kind,n.push(o)}return{active:n,blocked:null,unseen:null}}async#u(e,t,a){for(const[i,n]of e.entries()){const o=t.get(n.work.entityId);if(!(!o||o.look!=="safe"))try{const s=await o.adapter.apply.prepare(o.intent,o.plan);if(s)return{active:e.slice(0,i),blocked:A(s)}}catch(s){if(i>0&&!this.#n())return{active:e.slice(0,i),blocked:null};throw a.member=n,s}}return{active:[...e],blocked:null}}async#p(e,t){const a=t.members.map(n=>{const o=e.members.find(d=>d.work.entityId===n.entityId);if(!o)return null;const s=n.kept===null?o.work:{...o.work,records:[],kept:n.kept};return s.records.length===0&&s.kept===null?null:P(s,E(s,this.#r,n),this.#e.sha256Hex)}),i=await this.#e.effects.recover(t,a);if(i.length!==t.members.length)throw new Error("Apply recovery answered for a different window");return this.#t(t,e.members,e.members.map(n=>v(n)??i[t.members.findIndex(o=>o.entityId===n.work.entityId)]??{kind:"owed",reason:"not a member of the bound window"}))}async#t(e,t,a){const i=await this.#e.store.endWindow(e,t,a);if(!i)return"progress";if(i.kind!=="stale"||!i.reason)throw new Error("Apply end commit returned an unknown refusal");return I}async#i(e){return W(e.targetType,await this.#e.store.localWorkAfter(e.authorityId,e.entityId,e.kept??e.throughSequence))}async#f(e){const{intent:t,plan:a,adapter:i}=e,n=await i.apply.guard(t,a,"look");if(n.kind!=="local-change"&&n.kind!=="unsettled-ground")return n;const o=await this.#l(t,a);if(o.kind!=="observed")return o;const s=await i.apply.guard(t,a,"look");return s.kind==="local-change"||s.kind==="unsettled-ground"?{kind:"retry",reason:"Ground still needs observation after Detect; this member cannot reveal."}:s}async#h(e){const t=await this.#l(e.intent,e.plan);return t.kind==="captured-local"?I:A(t.kind==="observed"?{kind:"retry",reason:"Its final guard found ground Detect had not recorded; it is judged again after Detect looks."}:t)}async#l(e,t){if(await this.#i(e))return{kind:"captured-local"};let a,i=!0;const n=await this.#e.effects.captureLocal(e,t,()=>{if(!i||a!==void 0)throw new Error("Apply capture comparison must be marked exactly once");a=this.#a()}).finally(()=>{i=!1});if(!n||!["settled","local-only","retry"].includes(n.kind))throw new Error("Apply capture returned no explicit observation outcome");if(await this.#i(e))return{kind:"captured-local"};if(n.kind==="retry")return n;if(n.kind==="local-only"){if(a===void 0)throw new Error("local-only capture requires a synchronous comparison fence");if(!n.reason||n.entityIds.length===0||n.entityIds.some(o=>!o))throw new Error("local-only capture must name the retained ground and its releasing condition");return{kind:"dependency",reason:n.reason,comparedAt:a,waitFor:{producer:"detect",entityIds:n.entityIds}}}return{kind:"observed"}}}export{C as EntityApplyJourney,B as outcomeOf,H as stepsAside};
|
package/dist/apply/line.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var a=Object.defineProperty;var r=(e,n)=>a(e,"name",{value:n,configurable:!0});import{UUID_PATTERN as f}from"../entities/types.js";function w(e){return{kind:"line",authorityId:e,held:null,queued:!1,running:!1,again:!1}}r(w,"createApplyLine");function k(e,n,o){const i=e.held;return!i?.dependency||i.due||i.dependency.producer!==n||o!==void 0&&!i.dependency.entityIds.some(l=>o.includes(l))?!1:(i.due=!0,!0)}r(k,"releaseApplyLine");function g(e,n){if(e)return e.entityId===null||e.target===null?{entityId:e.entityId??n,reason:e.reason,throughSequence:e.target??e.cutoff??0}:{entityId:e.entityId,reason:e.reason,...e.dependency?{prerequisite:e.dependency}:{},throughSequence:e.target,...e.blocker?{blocker:e.blocker}:{}}}r(g,"heldWait");const d=new Set(["detect","apply","capability","send"]),c=r(e=>!!e&&d.has(e.producer)&&Array.isArray(e.entityIds)&&e.entityIds.length>0&&e.entityIds.every(n=>typeof n=="string"&&n.length>0),"namesRelease");function A(e){if(!c(e))throw new Error("Apply dependency must name the entities that can release it")}r(A,"assertApplyDependency");function b(e,n){(!n||typeof n!="object")&&u(`line step for ${e}`),n.kind!=="end"&&((n.kind!=="window"||n.window?.authorityId!==e||n.window.unit!==null||n.window.cutoff!==null||n.window.members?.some(o=>o.work?.kept!==null)||n.window.intent!==null&&(n.window.intent.unit!==null||n.window.intent.cutoff!==null))&&u(`line step for ${e}`),y(n.window,`line step for ${e}`))}r(b,"assertApplyLineStep");const u=r(e=>{throw new Error(`entity Apply store returned an invalid ${e}`)},"invalidStep");function y(e,n){Array.isArray(e.members)||u(n);const o=r((t,s)=>Number.isSafeInteger(t)&&t>=(s?0:1),"at");for(const t of e.members)(!f.test(t.work?.entityId??"")||t.work.authorityId!==e.authorityId||!o(t.deliverySequence,t.work.kept!==null))&&u(n),(t.unseen!==void 0||t.judgment==="unseen")&&(f.test(t.unseen??"")||u(n));const{deliverySequence:i}=e,l=e.members[0];(i===null?!e.intent:l===void 0||!o(i,l.work.kept!==null))&&u(n)}r(y,"assertApplyWindow");export{A as assertApplyDependency,b as assertApplyLineStep,y as assertApplyWindow,w as createApplyLine,g as heldWait,u as invalidStep,k as releaseApplyLine};
|
package/dist/apply/rail.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var I=Object.defineProperty;var a=(o,t)=>I(o,"name",{value:t,configurable:!0});import{UUID_PATTERN as u}from"../entities/types.js";import{FetchAhead as k}from"./fetch-ahead.js";import{EntityApplyJourney as b,outcomeOf as w,stepsAside as E}from"./journey.js";import{assertApplyLaneStep as P,createApplyLane as v}from"./lane.js";import{assertApplyDependency as O,assertApplyLineStep as $,createApplyLine as S,heldWait as c,releaseApplyLine as q}from"./line.js";const M=8,x=1e3,g=3e4;function _(o,t){return t.cause===void 0&&E(o,o.members[0].work.entityId)}a(_,"stuck");class j{static{a(this,"EntityApplyRail")}#t;#g;#P;#x;#_;#o;#v;#a=!1;#e=!1;#d=new Map;#s=new Map;#l={queue:[],steps:new Set};#u={queue:[],steps:new Set};#n=!1;#h=null;#p=1/0;#m=[];#A=0;#O=new Map;#y=null;constructor(t){this.#t=t,this.#g=t.concurrency??M,this.#P=t.laneConcurrency??this.#g,this.#x=t.onBackgroundError??(()=>{}),this.#_=t.onIdle??(()=>{});for(const[e,s]of[["concurrency",this.#g],["lane concurrency",this.#P]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${e} must be a positive integer`);this.#o=new b(t,()=>this.#A,()=>this.#e),this.#v=new k(t.cargo,{sha256Hex:t.sha256Hex,loops:t.loops,deliveryPosition:t.deliveryPosition})}async start(){if(this.#a)throw new Error("entity Apply rail already started");if(this.#e)throw new Error("entity Apply rail is stopped");this.#a=!0;const t=await this.#t.store.readOpenUnits();if(!Array.isArray(t)||new Set(t).size!==t.length||t.some(e=>!u.test(e)))throw new Error("entity Apply store returned an invalid open unit list");for(const e of t)this.#b(e,null);await this.#R()}fetch(t,e){return this.#a?this.#e?Promise.reject(new Error("entity Apply rail is stopped")):u.test(t)?!Number.isFinite(e.stall)||e.stall<0?Promise.reject(new Error("entity Apply fetch needs a non-negative stall")):e.emptied!==void 0&&typeof e.emptied!="boolean"?Promise.reject(new Error("entity Apply fetch emptied must be a boolean")):new Promise((s,i)=>{const r=this.#b(t,null,e.emptied===!0),n=a(()=>this.#t.schedule.schedule(e.stall,()=>{r.waiters.delete(l)&&(r.emptied&&![...r.waiters].some(d=>d.emptied)&&(r.emptied=!1),s(this.#G(r)))}),"arm");let h=n();const l={emptied:e.emptied===!0,answer:a(d=>{h.cancel(),s(d)},"answer"),fail:a(d=>{h.cancel(),i(d)},"fail"),moved:a(()=>{h.cancel(),h=n()},"moved")};r.waiters.add(l),r.waitsOn===null&&this.#N(r),this.#r()}):Promise.reject(new Error("entity Apply fetch needs a unit UUID")):Promise.reject(new Error("entity Apply rail has not started"))}progressed(t,e){if(!u.test(t))throw new Error("entity Apply progress needs a unit UUID");if(!Number.isSafeInteger(e)||e<0)throw new Error("entity Apply progress needs a byte count");e>0&&!this.#e&&this.#E(t)}recordsAvailable(){this.#I()}localStateChanged(t){this.#c("detect",t),this.#c("apply",t),this.#I()}sendAccepted(t){this.#c("send",t),this.#I()}capabilityChanged(t){this.#c("capability",t),this.#I()}owe(t){if(t.some(e=>!u.test(e)))throw new Error("entity Apply owe needs entity UUIDs");if(!(!this.#a||this.#e)){for(const e of t)this.#$(e);this.#r()}}#I(){!this.#a||this.#e||(this.#n=!0,this.#r())}waiting(){return this.#w().flatMap(t=>c(t.held,p(t))??[])}async flush(){if(!this.#a)throw new Error("entity Apply rail has not started");this.#e||await this.#R()}async stop(){if(this.#e)return;this.#e=!0,this.#M();for(const e of[this.#l,this.#u])for(const s of e.queue.splice(0))s.queued=!1;const t=new Error("entity Apply rail stopped before the fetch answered");for(const e of this.#s.values())for(const s of e.waiters)s.fail(t);for(this.#s.clear();this.#B()>0||this.#h;)await Promise.allSettled([...this.#l.steps,...this.#u.steps,...this.#h?[this.#h]:[]]);await this.#v.stop();for(const e of this.#m.splice(0))e.resolve()}#R(){return new Promise((t,e)=>{this.#m.push({resolve:t,reject:e}),this.#n=!0,this.#r()})}#r(){if(!this.#e){if(this.#n&&this.#h===null){this.#n=!1;const t=this.#F();this.#h=t,t.then(()=>{this.#h=null,this.#r()})}for(const[t,e]of[[this.#u,this.#P],[this.#l,this.#g]])for(;t.steps.size<e&&t.queue.length>0;){const s=t.queue.shift();s.queued=!1,this.#W(s,t)}if(this.#B()===0&&this.#h===null&&!this.#n&&this.#l.queue.length===0&&this.#u.queue.length===0){if(this.#O.clear(),this.#D(this.#t.now())){this.#r();return}this.#q();for(const t of this.#m.splice(0))t.resolve();this.#_()}}}#B(){return this.#l.steps.size+this.#u.steps.size}#w(){return[...this.#d.values(),...this.#s.values()]}#k(t){return t.kind==="line"?this.#d.get(t.authorityId)===t:this.#s.get(t.unit)===t}#i(t){if(!this.#e){if(t.running){t.again=!0;return}t.queued||(t.queued=!0,(t.kind==="line"?this.#l:this.#u).queue.push(t))}}#W(t,e){t.running=!0,t.again=!1;const s=(t.kind==="line"?this.#J(t):this.#H(t)).then(i=>{t.running=!1,e.steps.delete(s),(i||t.again)&&this.#k(t)&&(t.again=!1,this.#i(t)),this.#r()});e.steps.add(s)}async#F(){try{const t=await this.#t.store.readApplyAuthorities();if(!Array.isArray(t)||new Set(t).size!==t.length||t.some(s=>typeof s!="string"||s.length===0))throw new Error("entity Apply store returned an invalid authority list");this.#p=1/0;const e=new Set(t);for(const[s,i]of this.#d)!e.has(s)&&!i.running&&!i.queued&&this.#d.delete(s);for(const s of t){let i=this.#d.get(s);i||(i=S(s),this.#d.set(s,i)),this.#i(i)}}catch(t){this.#p=this.#t.now()+g;const e=this.#m.splice(0);e.length===0&&this.#S(t);for(const s of e)s.reject(t)}}async#J(t){try{const e=t.held;if(e&&e.entityId===null&&!e.due&&e.notBefore>this.#t.now())return!1;const s=this.#A,i=await this.#t.store.readApplyWindow(t.authorityId);return $(t.authorityId,i),this.#e?!1:i.kind==="end"?(t.held=null,!1):(i.window.deliverySequence!==null&&this.#v.advance(t.authorityId,i.window.deliverySequence),await this.#U(t,i.window,s))}catch(e){return this.#Y(t,e),!1}}async#H(t){try{const e=t.held;if(e&&e.entityId===null&&!e.due&&e.notBefore>this.#t.now())return!1;const s=this.#A,i=await this.#t.store.readFetchWindow(t.unit,t.toPrefix?null:t.through);if(P(t.unit,i),this.#e||!this.#k(t))return!1;if(t.waitsOn=null,i.kind==="unseen")return this.#L(t,{producer:"detect",entityIds:[t.unit]},i.reason,s);if(!t.again&&(i.kind!=="window"||i.window.intent===null)&&(t.toPrefix=!1),t.through!==null&&i.through>t.through&&t.met.clear(),t.through=Math.max(t.through??0,i.through),i.kind!=="window"&&(t.held=null),t.again&&(i.kind==="end"||i.kind==="nowhere"))return!0;switch(i.kind){case"end":return this.#f(t,{kind:"placed"}),!1;case"nowhere":return this.#f(t,{kind:"nowhere",reason:i.reason}),!1;case"root":return await this.#V(t,i.through,i.reason);case"prerequisite":return this.#X(t,i.unit,i.reason,s);case"window":return await this.#U(t,i.window,s)}}catch(e){return this.#Y(t,e),!1}}async#V(t,e,s){return t.met.has(t.unit)?(this.#f(t,{kind:"held",unit:t.unit,reason:`${t.unit} entered and still not held: ${s}`}),!1):(await this.#t.effects.enterRoot(t.unit,e),this.#e||!this.#k(t)?!1:(t.through===e&&t.met.add(t.unit),this.#E(t.unit),!0))}#$(t){const e=this.#b(t,null);e.toPrefix=!0,e.met.clear(),this.#i(e)}#b(t,e,s=!1){let i=this.#s.get(t);return i?e!==null&&(i.through===null||i.through<e)&&(i.through=e,i.met.clear(),this.#i(i)):(i=v(t,e,s),this.#s.set(t,i),this.#i(i)),i}#X(t,e,s,i){return t.met.has(e)?(this.#f(t,{kind:"held",unit:e,reason:`${e} placed and still needed: ${s}`}),!1):this.#T(t,e,s)?this.#L(t,{producer:"apply",entityIds:[e]},s,i):!1}#L(t,e,s,i){return t.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#z(e,i),reason:s,dependency:e},t.held.due}#N(t){const e=t.held;if(e?.dependency?.producer==="apply"){for(const s of e.dependency.entityIds)if(s!==t.unit&&!t.met.has(s)&&!this.#T(t,s,e.reason))return}}#T(t,e,s){const i=[t];let r=this.#s.get(e);for(;r&&r!==t&&!i.includes(r);)i.push(r),r=r.waitsOn===null?void 0:this.#s.get(r.waitsOn);if(r===t){for(const n of i)this.#f(n,{kind:"held",unit:e,reason:`prerequisite cycle through ${e}: ${s}`});return!1}return t.waitsOn??=e,this.#i(this.#b(e,t.through)),!0}#f(t,e){if(!this.#k(t))return;for(const i of t.waiters)i.answer(e);t.waiters.clear(),t.waitsOn=null,(e.kind==="placed"||t.held?.entityId==null)&&this.#s.delete(t.unit);const s=e.kind==="placed"||e.kind==="held"?e:{kind:"held",unit:t.unit,reason:`${t.unit} is nowhere here: ${e.reason}`};for(const i of[...this.#s.values()])if(i.waitsOn===t.unit){if(e.kind!=="placed"){this.#f(i,s);continue}i.met.add(t.unit)}e.kind==="placed"&&(this.#E(t.unit),this.#c("apply",[t.unit]))}#E(t){const e=new Map([[t,!0]]);for(const s of this.#s.values()){const i=new Set;let r=s.unit;for(;r!==null&&!e.has(r)&&!i.has(r);)i.add(r),r=this.#s.get(r)?.waitsOn??null;const n=r!==null&&e.get(r)===!0;for(const h of i)e.set(h,n);if(n)for(const h of s.waiters)h.moved()}}#G(t){let e=t;const s=new Set([t]);for(let i=t.waitsOn===null?void 0:this.#s.get(t.waitsOn);i&&!s.has(i);i=i.waitsOn===null?void 0:this.#s.get(i.waitsOn))s.add(i),e=i;return{kind:"held",unit:e.unit,reason:e.held?.reason??"still placing"}}async#U(t,e,s){const i=e.members[0]?.work.entityId??null;if(t.held&&t.held.entityId!==null&&t.held.entityId!==i&&(t.held=null),await this.#Q(t,e))return!1;let r=await w(()=>this.#o.run(e,()=>t.kind==="lane"&&t.emptied?t.unit:null));if(t.kind==="line"&&!this.#e&&typeof r=="object"&&r.kind==="park"&&_(e,r)&&(r=await w(()=>this.#o.alone(e.members[0]))),r==="progress"||typeof r=="object"&&r.kind==="alone"){if(t.kind==="line")for(const n of e.members)(n.judgment==="waiting"||r!=="progress"&&n.work.entityId===r.entityId)&&this.#$(n.work.entityId);for(const n of e.members)n.unseen!==void 0&&this.#$(n.unseen);return this.#Z(t,r==="progress"?e.members.map(n=>n.work.entityId):[r.entityId])}return r.advanced?.length&&this.#j(t,r.advanced),this.#tt(t,e,r,s),t.kind==="lane"&&this.#N(t),t.held.due}#K(t,e){const s=t.held,i=e.members[0].work.entityId;return(s?.entityId===i&&s.target===this.#o.targetOf(e)?s.attempt:0)+1}async#Q(t,e){const s=t.held,i=e.members[0];return!s||i===void 0||s.entityId!==i.work.entityId||s.due||s.notBefore<=this.#t.now()||this.#o.targetOf(e)!==s.target||e.cutoff!==s.cutoff?!1:await this.#t.store.localWorkAfter(e.authorityId,i.work.entityId,s.kept??s.target)===null}#Z(t,e){return t.held&&(t.held.entityId===null||e.includes(t.held.entityId))&&(t.held=null),this.#j(t,e),!0}#j(t,e){t.kind==="lane"&&(this.#n=!0,this.#E(t.unit)),this.#c("apply",e);for(const s of this.#w())s!==t&&s.held?.releaseOnProgress&&!s.held.due&&(s.held.due=!0,this.#i(s))}#tt(t,e,s,i){if(s.dependency&&O(s.dependency),s.blocker&&!s.dependency)throw new Error("Apply blocker must name the dependency that releases it");const r=e.members[0].work.entityId,n=t.held,h=this.#K(t,e),l=this.#C(h),d=s.again===!0&&h===1,y={entityId:r,target:this.#o.targetOf(e),kept:e.members[0].work.kept,cutoff:e.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#t.now()+(d?0:l),releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#z(s.dependency,s.comparedAt??i):d,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};t.held=y,this.#q();const A=c(n,p(t)),f=c(y,p(t));f&&JSON.stringify(f)!==JSON.stringify(A)&&this.#t.onWaiting?.(f),!(!s.report&&(s.dependency!==void 0||h===1))&&this.#S(new Error(`${m(t)} holds at ${r}, which ${s.releaseOnProgress?"is parked on a conflict":s.report?"failed":s.again?"keeps ending unplaced":"stands on ground that keeps moving"}, and will retry in ${l}ms (attempt ${h}): ${s.reason}`,s.cause===void 0?void 0:{cause:s.cause}))}#Y(t,e){if(t.kind==="lane"){for(const h of t.waiters)h.fail(e);t.waiters.clear()}const s=t.held,i=(s?.entityId===null?s.attempt:0)+1,r=this.#C(i),n=e instanceof Error?e.message:String(e);t.held={entityId:null,target:null,kept:null,cutoff:null,attempt:i,notBefore:this.#t.now()+r,releaseOnProgress:!1,due:!1,reason:n},this.#q(),this.#S(new Error(`${m(t)} faulted and will retry in ${r}ms (attempt ${i}): ${n}`,{cause:e}))}#S(t){try{this.#x(t)}catch{}}#C(t){const e=Math.min(g,x*2**Math.min(t-1,16)),s=Math.max(0,Math.min(1,this.#t.schedule.random()));return Math.round(e*(.5+s))}#c(t,e){const s=++this.#A;for(const i of e??["*"])this.#O.set(`${t}:${i}`,s);for(const i of this.#w())q(i,t,e)&&this.#i(i)}#z(t,e){return["*",...t.entityIds].some(s=>(this.#O.get(`${t.producer}:${s}`)??0)>e)}#q(){if(this.#e)return;let t=this.#p;for(const i of this.#w())i.held&&!i.held.due&&(t=Math.min(t,i.held.notBefore));if(t===1/0){this.#M();return}if(this.#y?.dueAt===t)return;this.#M();const e=Math.max(0,t-this.#t.now()),s=this.#t.schedule.schedule(e,()=>{this.#y=null,this.#D(t),this.#r()});this.#y={dueAt:t,cancel:a(()=>s.cancel(),"cancel")}}#D(t){let e=!1;for(const s of this.#w())s.held&&!s.held.due&&s.held.notBefore<=t&&(s.held.due=!0,this.#i(s),e=!0);return this.#p<=t&&(this.#p=1/0,this.#n=!0,e=!0),e}#M(){this.#y?.cancel(),this.#y=null}}const p=a(o=>o.kind==="line"?o.authorityId:o.unit,"owner"),m=a(o=>o.kind==="line"?`entity Apply line ${o.authorityId}`:`entity Apply fetch of ${o.unit}`,"label");export{M as ENTITY_APPLY_DEFAULT_CONCURRENCY,x as ENTITY_APPLY_RETRY_BASE_MS,g as ENTITY_APPLY_RETRY_MAX_MS,j as EntityApplyRail};
|
|
1
|
+
var I=Object.defineProperty;var a=(o,t)=>I(o,"name",{value:t,configurable:!0});import{UUID_PATTERN as u}from"../entities/types.js";import{FetchAhead as k}from"./fetch-ahead.js";import{EntityApplyJourney as b,outcomeOf as w,stepsAside as E}from"./journey.js";import{assertApplyLaneStep as P,createApplyLane as v}from"./lane.js";import{assertApplyDependency as O,assertApplyLineStep as $,createApplyLine as S,heldWait as p,releaseApplyLine as q}from"./line.js";const M=8,x=1e3,g=3e4;function _(o,t){const e=o.members[0];return t.cause===void 0&&e!==void 0&&E(o,e.work.entityId)}a(_,"stuck");class j{static{a(this,"EntityApplyRail")}#t;#g;#P;#x;#_;#o;#v;#a=!1;#e=!1;#d=new Map;#i=new Map;#l={queue:[],steps:new Set};#u={queue:[],steps:new Set};#r=!1;#h=null;#p=1/0;#m=[];#A=0;#O=new Map;#y=null;constructor(t){this.#t=t,this.#g=t.concurrency??M,this.#P=t.laneConcurrency??this.#g,this.#x=t.onBackgroundError??(()=>{}),this.#_=t.onIdle??(()=>{});for(const[e,i]of[["concurrency",this.#g],["lane concurrency",this.#P]])if(!Number.isSafeInteger(i)||i<1)throw new Error(`entity Apply ${e} must be a positive integer`);this.#o=new b(t,()=>this.#A,()=>this.#e),this.#v=new k(t.cargo,{sha256Hex:t.sha256Hex,loops:t.loops,deliveryPosition:t.deliveryPosition})}async start(){if(this.#a)throw new Error("entity Apply rail already started");if(this.#e)throw new Error("entity Apply rail is stopped");this.#a=!0;const t=await this.#t.store.readOpenUnits();if(!Array.isArray(t)||new Set(t).size!==t.length||t.some(e=>!u.test(e)))throw new Error("entity Apply store returned an invalid open unit list");for(const e of t)this.#b(e,null);await this.#R()}fetch(t,e){return this.#a?this.#e?Promise.reject(new Error("entity Apply rail is stopped")):u.test(t)?!Number.isFinite(e.stall)||e.stall<0?Promise.reject(new Error("entity Apply fetch needs a non-negative stall")):e.emptied!==void 0&&typeof e.emptied!="boolean"?Promise.reject(new Error("entity Apply fetch emptied must be a boolean")):new Promise((i,s)=>{const n=this.#b(t,null,e.emptied===!0),r=a(()=>this.#t.schedule.schedule(e.stall,()=>{n.waiters.delete(l)&&(n.emptied&&![...n.waiters].some(d=>d.emptied)&&(n.emptied=!1),i(this.#G(n)))}),"arm");let h=r();const l={emptied:e.emptied===!0,answer:a(d=>{h.cancel(),i(d)},"answer"),fail:a(d=>{h.cancel(),s(d)},"fail"),moved:a(()=>{h.cancel(),h=r()},"moved")};n.waiters.add(l),n.waitsOn===null&&this.#N(n),this.#n()}):Promise.reject(new Error("entity Apply fetch needs a unit UUID")):Promise.reject(new Error("entity Apply rail has not started"))}progressed(t,e){if(!u.test(t))throw new Error("entity Apply progress needs a unit UUID");if(!Number.isSafeInteger(e)||e<0)throw new Error("entity Apply progress needs a byte count");e>0&&!this.#e&&this.#E(t)}recordsAvailable(){this.#I()}localStateChanged(t){this.#c("detect",t),this.#c("apply",t),this.#I()}sendAccepted(t){this.#c("send",t),this.#I()}capabilityChanged(t){this.#c("capability",t),this.#I()}owe(t){if(t.some(e=>!u.test(e)))throw new Error("entity Apply owe needs entity UUIDs");if(!(!this.#a||this.#e)){for(const e of t)this.#$(e);this.#n()}}#I(){!this.#a||this.#e||(this.#r=!0,this.#n())}waiting(){return this.#w().flatMap(t=>p(t.held,y(t))??[])}async flush(){if(!this.#a)throw new Error("entity Apply rail has not started");this.#e||await this.#R()}async stop(){if(this.#e)return;this.#e=!0,this.#M();for(const e of[this.#l,this.#u])for(const i of e.queue.splice(0))i.queued=!1;const t=new Error("entity Apply rail stopped before the fetch answered");for(const e of this.#i.values())for(const i of e.waiters)i.fail(t);for(this.#i.clear();this.#B()>0||this.#h;)await Promise.allSettled([...this.#l.steps,...this.#u.steps,...this.#h?[this.#h]:[]]);await this.#v.stop();for(const e of this.#m.splice(0))e.resolve()}#R(){return new Promise((t,e)=>{this.#m.push({resolve:t,reject:e}),this.#r=!0,this.#n()})}#n(){if(!this.#e){if(this.#r&&this.#h===null){this.#r=!1;const t=this.#F();this.#h=t,t.then(()=>{this.#h=null,this.#n()})}for(const[t,e]of[[this.#u,this.#P],[this.#l,this.#g]])for(;t.steps.size<e&&t.queue.length>0;){const i=t.queue.shift();i.queued=!1,this.#W(i,t)}if(this.#B()===0&&this.#h===null&&!this.#r&&this.#l.queue.length===0&&this.#u.queue.length===0){if(this.#O.clear(),this.#D(this.#t.now())){this.#n();return}this.#q();for(const t of this.#m.splice(0))t.resolve();this.#_()}}}#B(){return this.#l.steps.size+this.#u.steps.size}#w(){return[...this.#d.values(),...this.#i.values()]}#k(t){return t.kind==="line"?this.#d.get(t.authorityId)===t:this.#i.get(t.unit)===t}#s(t){if(!this.#e){if(t.running){t.again=!0;return}t.queued||(t.queued=!0,(t.kind==="line"?this.#l:this.#u).queue.push(t))}}#W(t,e){t.running=!0,t.again=!1;const i=(t.kind==="line"?this.#J(t):this.#H(t)).then(s=>{t.running=!1,e.steps.delete(i),(s||t.again)&&this.#k(t)&&(t.again=!1,this.#s(t)),this.#n()});e.steps.add(i)}async#F(){try{const t=await this.#t.store.readApplyAuthorities();if(!Array.isArray(t)||new Set(t).size!==t.length||t.some(i=>typeof i!="string"||i.length===0))throw new Error("entity Apply store returned an invalid authority list");this.#p=1/0;const e=new Set(t);for(const[i,s]of this.#d)!e.has(i)&&!s.running&&!s.queued&&this.#d.delete(i);for(const i of t){let s=this.#d.get(i);s||(s=S(i),this.#d.set(i,s)),this.#s(s)}}catch(t){this.#p=this.#t.now()+g;const e=this.#m.splice(0);e.length===0&&this.#S(t);for(const i of e)i.reject(t)}}async#J(t){try{const e=t.held;if(e&&e.entityId===null&&!e.due&&e.notBefore>this.#t.now())return!1;const i=this.#A,s=await this.#t.store.readApplyWindow(t.authorityId);return $(t.authorityId,s),this.#e?!1:s.kind==="end"?(t.held=null,!1):(s.window.deliverySequence!==null&&this.#v.advance(t.authorityId,s.window.deliverySequence),await this.#U(t,s.window,i))}catch(e){return this.#Y(t,e),!1}}async#H(t){try{const e=t.held;if(e&&e.entityId===null&&!e.due&&e.notBefore>this.#t.now())return!1;const i=this.#A,s=await this.#t.store.readFetchWindow(t.unit,t.toPrefix?null:t.through);if(P(t.unit,s),this.#e||!this.#k(t))return!1;if(t.waitsOn=null,s.kind==="unseen")return this.#L(t,{producer:"detect",entityIds:[t.unit]},s.reason,i);if(!t.again&&(s.kind!=="window"||s.window.intent===null)&&(t.toPrefix=!1),t.through!==null&&s.through>t.through&&t.met.clear(),t.through=Math.max(t.through??0,s.through),s.kind!=="window"&&(t.held=null),t.again&&(s.kind==="end"||s.kind==="nowhere"))return!0;switch(s.kind){case"end":return this.#f(t,{kind:"placed"}),!1;case"nowhere":return this.#f(t,{kind:"nowhere",reason:s.reason}),!1;case"root":return await this.#V(t,s.through,s.reason);case"prerequisite":return this.#X(t,s.unit,s.reason,i);case"window":return await this.#U(t,s.window,i)}}catch(e){return this.#Y(t,e),!1}}async#V(t,e,i){return t.met.has(t.unit)?(this.#f(t,{kind:"held",unit:t.unit,reason:`${t.unit} entered and still not held: ${i}`}),!1):(await this.#t.effects.enterRoot(t.unit,e),this.#e||!this.#k(t)?!1:(t.through===e&&t.met.add(t.unit),this.#E(t.unit),!0))}#$(t){const e=this.#b(t,null);e.toPrefix=!0,e.met.clear(),this.#s(e)}#b(t,e,i=!1){let s=this.#i.get(t);return s?e!==null&&(s.through===null||s.through<e)&&(s.through=e,s.met.clear(),this.#s(s)):(s=v(t,e,i),this.#i.set(t,s),this.#s(s)),s}#X(t,e,i,s){return t.met.has(e)?(this.#f(t,{kind:"held",unit:e,reason:`${e} placed and still needed: ${i}`}),!1):this.#T(t,e,i)?this.#L(t,{producer:"apply",entityIds:[e]},i,s):!1}#L(t,e,i,s){return t.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#z(e,s),reason:i,dependency:e},t.held.due}#N(t){const e=t.held;if(e?.dependency?.producer==="apply"){for(const i of e.dependency.entityIds)if(i!==t.unit&&!t.met.has(i)&&!this.#T(t,i,e.reason))return}}#T(t,e,i){const s=[t];let n=this.#i.get(e);for(;n&&n!==t&&!s.includes(n);)s.push(n),n=n.waitsOn===null?void 0:this.#i.get(n.waitsOn);if(n===t){for(const r of s)this.#f(r,{kind:"held",unit:e,reason:`prerequisite cycle through ${e}: ${i}`});return!1}return t.waitsOn??=e,this.#s(this.#b(e,t.through)),!0}#f(t,e){if(!this.#k(t))return;for(const s of t.waiters)s.answer(e);t.waiters.clear(),t.waitsOn=null,(e.kind==="placed"||t.held?.entityId==null)&&this.#i.delete(t.unit);const i=e.kind==="placed"||e.kind==="held"?e:{kind:"held",unit:t.unit,reason:`${t.unit} is nowhere here: ${e.reason}`};for(const s of[...this.#i.values()])if(s.waitsOn===t.unit){if(e.kind!=="placed"){this.#f(s,i);continue}s.met.add(t.unit)}e.kind==="placed"&&(this.#E(t.unit),this.#c("apply",[t.unit]))}#E(t){const e=new Map([[t,!0]]);for(const i of this.#i.values()){const s=new Set;let n=i.unit;for(;n!==null&&!e.has(n)&&!s.has(n);)s.add(n),n=this.#i.get(n)?.waitsOn??null;const r=n!==null&&e.get(n)===!0;for(const h of s)e.set(h,r);if(r)for(const h of i.waiters)h.moved()}}#G(t){let e=t;const i=new Set([t]);for(let s=t.waitsOn===null?void 0:this.#i.get(t.waitsOn);s&&!i.has(s);s=s.waitsOn===null?void 0:this.#i.get(s.waitsOn))i.add(s),e=s;return{kind:"held",unit:e.unit,reason:e.held?.reason??"still placing"}}async#U(t,e,i){const s=e.members[0]?.work.entityId??null;if(t.held&&t.held.entityId!==null&&t.held.entityId!==s&&(t.held=null),await this.#Q(t,e))return!1;let n=await w(()=>this.#o.run(e,()=>t.kind==="lane"&&t.emptied?t.unit:null));if(t.kind==="line"&&!this.#e&&typeof n=="object"&&n.kind==="park"&&_(e,n)&&(n=await w(()=>this.#o.alone(e))),n==="progress"||typeof n=="object"&&n.kind==="alone"){if(t.kind==="line")for(const r of e.members)(r.judgment==="waiting"||n!=="progress"&&r.work.entityId===n.entityId)&&this.#$(r.work.entityId);for(const r of e.members)r.unseen!==void 0&&this.#$(r.unseen);return this.#Z(t,n!=="progress"?[n.entityId]:e.intent?.members.map(r=>r.entityId)??e.members.map(r=>r.work.entityId))}return n.advanced?.length&&this.#j(t,n.advanced),this.#tt(t,e,n,i),t.kind==="lane"&&this.#N(t),t.held.due}#K(t,e){const i=t.held,s=e.members[0]?.work.entityId??null;return(i?.entityId===s&&i.target===this.#o.targetOf(e)?i.attempt:0)+1}async#Q(t,e){const i=t.held,s=e.members[0];return!i||s===void 0||i.entityId!==s.work.entityId||i.due||i.notBefore<=this.#t.now()||this.#o.targetOf(e)!==i.target||e.cutoff!==i.cutoff?!1:await this.#t.store.localWorkAfter(e.authorityId,s.work.entityId,i.kept??i.target)===null}#Z(t,e){return t.held&&(t.held.entityId===null||e.includes(t.held.entityId))&&(t.held=null),this.#j(t,e),!0}#j(t,e){t.kind==="lane"&&(this.#r=!0,this.#E(t.unit)),this.#c("apply",e);for(const i of this.#w())i!==t&&i.held?.releaseOnProgress&&!i.held.due&&(i.held.due=!0,this.#s(i))}#tt(t,e,i,s){if(i.dependency&&O(i.dependency),i.blocker&&!i.dependency)throw new Error("Apply blocker must name the dependency that releases it");const n=e.members[0],r=t.held,h=this.#K(t,e),l=this.#C(h),d=i.again===!0&&h===1,f={entityId:n?.work.entityId??null,target:this.#o.targetOf(e),kept:n?.work.kept??null,cutoff:e.cutoff,attempt:h,notBefore:i.dependency?1/0:this.#t.now()+(d?0:l),releaseOnProgress:i.releaseOnProgress,due:i.dependency?this.#z(i.dependency,i.comparedAt??s):d,reason:i.reason,...i.dependency?{dependency:i.dependency}:{},...i.blocker?{blocker:i.blocker}:{}};t.held=f,this.#q();const A=p(r,y(t)),c=p(f,y(t));c&&JSON.stringify(c)!==JSON.stringify(A)&&this.#t.onWaiting?.(c),!(!i.report&&(i.dependency!==void 0||h===1))&&this.#S(new Error(`${m(t)} holds at ${f.entityId??"its open window"}, which ${i.releaseOnProgress?"is parked on a conflict":i.report?"failed":i.again?"keeps ending unplaced":"stands on ground that keeps moving"}, and will retry in ${l}ms (attempt ${h}): ${i.reason}`,i.cause===void 0?void 0:{cause:i.cause}))}#Y(t,e){if(t.kind==="lane"){for(const h of t.waiters)h.fail(e);t.waiters.clear()}const i=t.held,s=(i?.entityId===null?i.attempt:0)+1,n=this.#C(s),r=e instanceof Error?e.message:String(e);t.held={entityId:null,target:null,kept:null,cutoff:null,attempt:s,notBefore:this.#t.now()+n,releaseOnProgress:!1,due:!1,reason:r},this.#q(),this.#S(new Error(`${m(t)} faulted and will retry in ${n}ms (attempt ${s}): ${r}`,{cause:e}))}#S(t){try{this.#x(t)}catch{}}#C(t){const e=Math.min(g,x*2**Math.min(t-1,16)),i=Math.max(0,Math.min(1,this.#t.schedule.random()));return Math.round(e*(.5+i))}#c(t,e){const i=++this.#A;for(const s of e??["*"])this.#O.set(`${t}:${s}`,i);for(const s of this.#w())q(s,t,e)&&this.#s(s)}#z(t,e){return["*",...t.entityIds].some(i=>(this.#O.get(`${t.producer}:${i}`)??0)>e)}#q(){if(this.#e)return;let t=this.#p;for(const s of this.#w())s.held&&!s.held.due&&(t=Math.min(t,s.held.notBefore));if(t===1/0){this.#M();return}if(this.#y?.dueAt===t)return;this.#M();const e=Math.max(0,t-this.#t.now()),i=this.#t.schedule.schedule(e,()=>{this.#y=null,this.#D(t),this.#n()});this.#y={dueAt:t,cancel:a(()=>i.cancel(),"cancel")}}#D(t){let e=!1;for(const i of this.#w())i.held&&!i.held.due&&i.held.notBefore<=t&&(i.held.due=!0,this.#s(i),e=!0);return this.#p<=t&&(this.#p=1/0,this.#r=!0,e=!0),e}#M(){this.#y?.cancel(),this.#y=null}}const y=a(o=>o.kind==="line"?o.authorityId:o.unit,"owner"),m=a(o=>o.kind==="line"?`entity Apply line ${o.authorityId}`:`entity Apply fetch of ${o.unit}`,"label");export{M as ENTITY_APPLY_DEFAULT_CONCURRENCY,x as ENTITY_APPLY_RETRY_BASE_MS,g as ENTITY_APPLY_RETRY_MAX_MS,j as EntityApplyRail};
|
|
@@ -5,6 +5,7 @@ import { type PlayableChange } from './bodmas.js';
|
|
|
5
5
|
export interface LocalWork {
|
|
6
6
|
readonly type: string;
|
|
7
7
|
readonly status: EntityStatus;
|
|
8
|
+
readonly whole: boolean;
|
|
8
9
|
}
|
|
9
10
|
export declare const localWorkWins: (type: string, after: LocalWork | null) => boolean;
|
|
10
11
|
export interface OwnLine {
|
package/dist/clash/local-work.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var l=Object.defineProperty;var n=(t,e)=>l(t,"name",{value:e,configurable:!0});import{assertRecord as u}from"../entities/cloud.js";import{playChange as f}from"./bodmas.js";import{clashResolution as c}from"./resolution.js";const g=n((t,e)=>e!==null&&(e.whole||e.status!=="active"||e.type!==t),"localWorkWins");function y(t,e){if(c(t.change.result.type)==="replace")return e.admittedVersion()===e.playedVersion();const r=t.change.joins!==void 0?t.change.result.version:t.change.basedOnState;return e.lineBefore()===r}n(y,"ownSettles");const p=new Set(["move","rename","trash","restore","delete"]);function S(t,e,r){const a=c(t.type)==="replace";let s=t;for(const o of e)o.joins===void 0&&(s=f(s,{facets:a?o.facets:o.facets.filter(i=>p.has(i)),result:o.result},r).row);return u(s)}n(S,"showAhead");export{g as localWorkWins,y as ownSettles,S as showAhead};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var R=Object.defineProperty;var d=(s,a)=>R(s,"name",{value:a,configurable:!0});import{assertRecord as v}from"../entities/cloud.js";import{anchorsPackage as x,carriers as z,packagePagesSteps as A}from"../entities/package-pages.js";import{CONTAINERS as N}from"../entities/types.js";import{eachStep as p,finishSteps as j}from"../steps.js";import{assertRepositoryReplay as T,CHANGE_PROPOSAL_CONTRACT as U,changeCargo as L,ownChange as D}from"./record-proposal.js";function*C(s,a,i){const c=yield*A(s,a,i);let b;for(const g of c)b??=g.items.find(y=>y.uuid===s.toLowerCase());const f=v(b),o={kind:"package",pages:Object.freeze(c.map(g=>g.ref))},u=Object.freeze({contract:U,origin:"detected",entityId:f.uuid,basedOnState:null,result:f,facets:Object.freeze(["create"]),replay:o,cargo:Object.freeze(L(f,o))}),l=D(u).replay;return l.kind==="repo.git"&&T(l,f),{proposal:u,pages:c}}d(C,"packageProposalSteps");function F(s,a,i){return j(C(s,a,i))}d(F,"packageProposal");function*E(s,a,i=new Map){const c=new Map;yield*p(s,e=>{e.basedOnState===null&&e.result.status==="active"&&c.set(e.entityId,e)});const b=z([...c.values()].map(e=>e.result)),f=d(e=>{const{parentUUID:t}=c.get(e).result;return t===null?b.get(e)?.uuid??null:N.has(c.get(t)?.result.type??"")?t:null},"holderOf"),o=new Map;yield*p([...c.keys()],e=>{const t=[];let n=null;for(let r=e;;){const w=o.get(r);if(w!==void 0){n=w;break}t.push(r);const k=f(r);if(k!==null&&!t.includes(k)){r=k;continue}n=k===null&&x(c.get(r).result.type)?r:null;break}for(const r of t)o.set(r,n)});const u=new Map;yield*p([...o],([e,t])=>{if(t===null)return;const n=u.get(t)??[];n.push(c.get(e).result),u.set(t,n)});const l=new Map;for(const[e,t]of u)for(const n of t)l.set(n.uuid,e);const g=d(e=>e.basedOnState!==null&&e.replay.kind!=="package"&&e.
|
|
1
|
+
var R=Object.defineProperty;var d=(s,a)=>R(s,"name",{value:a,configurable:!0});import{assertRecord as v}from"../entities/cloud.js";import{anchorsPackage as x,carriers as z,packagePagesSteps as A}from"../entities/package-pages.js";import{CONTAINERS as N}from"../entities/types.js";import{eachStep as p,finishSteps as j}from"../steps.js";import{assertRepositoryReplay as T,CHANGE_PROPOSAL_CONTRACT as U,changeCargo as L,ownChange as D}from"./record-proposal.js";function*C(s,a,i){const c=yield*A(s,a,i);let b;for(const g of c)b??=g.items.find(y=>y.uuid===s.toLowerCase());const f=v(b),o={kind:"package",pages:Object.freeze(c.map(g=>g.ref))},u=Object.freeze({contract:U,origin:"detected",entityId:f.uuid,basedOnState:null,result:f,facets:Object.freeze(["create"]),replay:o,cargo:Object.freeze(L(f,o))}),l=D(u).replay;return l.kind==="repo.git"&&T(l,f),{proposal:u,pages:c}}d(C,"packageProposalSteps");function F(s,a,i){return j(C(s,a,i))}d(F,"packageProposal");function*E(s,a,i=new Map){const c=new Map;yield*p(s,e=>{e.basedOnState===null&&e.result.status==="active"&&c.set(e.entityId,e)});const b=z([...c.values()].map(e=>e.result)),f=d(e=>{const{parentUUID:t}=c.get(e).result;return t===null?b.get(e)?.uuid??null:N.has(c.get(t)?.result.type??"")?t:null},"holderOf"),o=new Map;yield*p([...c.keys()],e=>{const t=[];let n=null;for(let r=e;;){const w=o.get(r);if(w!==void 0){n=w;break}t.push(r);const k=f(r);if(k!==null&&!t.includes(k)){r=k;continue}n=k===null&&x(c.get(r).result.type)?r:null;break}for(const r of t)o.set(r,n)});const u=new Map;yield*p([...o],([e,t])=>{if(t===null)return;const n=u.get(t)??[];n.push(c.get(e).result),u.set(t,n)});const l=new Map;for(const[e,t]of u)for(const n of t)l.set(n.uuid,e);const g=d(e=>e.basedOnState!==null&&e.replay.kind!=="package"&&e.turn===void 0,"joinable"),y=new Map;yield*p(s,e=>{const{type:t,payloadVersion:n}=e.result,r=t==="reference"&&n!==null?n.toLowerCase():null;g(e)&&r!==null&&l.get(r)===r&&y.set(e.entityId,r)});const h=new Map(y),I=new Set;yield*p(s,e=>{const{parentUUID:t}=e.result;if(!g(e)||h.has(e.entityId))return;const n=t===null?void 0:l.get(t)??y.get(t);if(n!==void 0){h.set(e.entityId,n);return}const r=i.get(e.entityId);if(r!==void 0&&I.has(r)){h.set(e.entityId,r);return}I.add(e.entityId)});const P=new Set(h.values()),m=new Map;for(const[e,t]of u)(t.length>1||P.has(e))&&m.set(e,yield*C(e,t,a));const O=new Map,S=[],M=new Set;return yield*p(s,e=>{const t=h.get(e.entityId);if(t!==void 0){const w=O.get(t)??[];w.push(e),O.set(t,w);return}const n=o.get(e.entityId),r=n==null?void 0:m.get(n);if(!r){S.push(e);return}M.has(n)||(M.add(n),S.push(r.proposal))}),{proposals:S,together:O,pages:[...m.values()].flatMap(e=>e.pages)}}d(E,"packageProposalsSteps");function J(s,a,i){return j(E(s,a,i))}d(J,"packageProposals");export{F as packageProposal,C as packageProposalSteps,J as packageProposals,E as packageProposalsSteps};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var E=Object.defineProperty;var c=(e,t)=>E(e,"name",{value:t,configurable:!0});import{assertRecord as R}from"../entities/cloud.js";import{assertPageRef as T}from"../entities/graph-pages.js";import{anchorsPackage as
|
|
1
|
+
var E=Object.defineProperty;var c=(e,t)=>E(e,"name",{value:t,configurable:!0});import{assertRecord as R}from"../entities/cloud.js";import{assertPageRef as T}from"../entities/graph-pages.js";import{anchorsPackage as q}from"../entities/package-pages.js";import{CONSTRUCT_TYPES as C,UUID_PATTERN as l}from"../entities/types.js";import{CHANGE_PROPOSAL_CONTRACT as b,assertRepositoryReplay as w,changeCargo as H,isPermanentDeletionShape as j,ownChange as G}from"../detection/record-proposal.js";import{failShape as r,isNonEmptyString as d,isRecord as i,pass as p}from"../contracts/validate.js";const S="amalgm.entity-record@1";function x(e){if(e.together===void 0)return[e];const{together:t,...s}=e;return Object.freeze([Object.freeze(s),...t.map(n=>Object.freeze({contract:e.contract,mutationId:e.mutationId,authorityId:e.authorityId,entityId:n.entityId,basedOnState:n.basedOnState,resultState:n.resultState,lastSeenGlobalSequence:n.lastSeenGlobalSequence,change:n.change,createdAt:e.createdAt}))])}c(x,"channelRecords");function B(e){if(e.together===void 0)return e.change.cargo;const t=new Map;for(const{change:s}of x(e))for(const n of s.cargo)t.has(n.contentHash)||t.set(n.contentHash,n);return Object.freeze([...t.values()])}c(B,"recordCargo");const g=/^[0-9a-f]{64}$/,D=new Set(["create","rename","move","retype","content","mode","time","trash","restore","delete"]),N=new Set(["detected","intentional"]),a='{ contract: "amalgm.entity-record@1", mutationId: "m1", authorityId: "pvr_...", entityId: "UUID", basedOnState: null, resultState: "64-hex", lastSeenGlobalSequence: 0, change: {...}, createdAt: 1 }',f=c((e,t=!1)=>typeof e=="number"&&Number.isSafeInteger(e)&&e>=(t?1:0),"isSequence");function O(e){if(!i(e)||e.contract!==b)return r(a,`change.contract was not ${b}`);const t=e.origin;if(typeof t!="string"||!N.has(t))return r(a,"change.origin was not detected or intentional");if(typeof e.entityId!="string"||!l.test(e.entityId))return r(a,"change.entityId was not an entity UUID");if(e.basedOnState!==null&&(typeof e.basedOnState!="string"||!g.test(e.basedOnState)))return r(a,"change.basedOnState was not null or lowercase SHA-256");const s=e.joins!==void 0;if(s&&(t!=="detected"||typeof e.joins!="string"||!g.test(e.joins)||e.basedOnState!==e.joins))return r(a,"change.joins was not a detected echo naming the SHA-256 of the row it states over");const n=e.turn!==void 0;if(n&&(s||t!=="detected"||!i(e.turn)||!(e.turn.act==="ask"?d(e.turn.event):(e.turn.act==="check"||e.turn.act==="running"||e.turn.act==="end")&&d(e.turn.ask))||e.replay===null||!i(e.replay)||e.replay.kind!=="structure"||!i(e.result)||e.result.type!=="repo.git"||e.basedOnState!==e.result.version))return r(a,"change.turn was not a detected ask, check, running or end restating the repository row it was said on");if(!Array.isArray(e.facets)||e.facets.length===0!==(s||n)||e.facets.some(o=>typeof o!="string"||!D.has(o))||new Set(e.facets).size!==e.facets.length)return r(a,"change.facets was invalid, duplicated, or unknown");if(!i(e.replay)||!d(e.replay.kind))return r(a,"change.replay was not a replay instruction");if(!Array.isArray(e.cargo))return r(a,"change.cargo was not an array");try{const o=R(e.result);if(o.uuid!==e.entityId)return r(a,"change.entityId did not equal change.result.uuid");const u=C.has(o.type);if(!(t==="detected"?!u&&o.status!=="deleted":u||j({origin:"intentional",result:o,facets:e.facets,replay:e.replay,cargo:e.cargo})))return r(a,"change.origin did not match its result: constructs are intentional, files are detected and never deleted, and a file is intentional only as a permanent deletion (delete alone, structure replay, no cargo, inside a root)");if(e.replay.kind==="package"&&P(e,o),e.replay.kind==="repo.git"){if(o.type!=="repo.git")throw new Error("repository replay requires a repository result");w(e.replay,o)}const m=H(o,e.replay),I=e.cargo;if(I.length!==m.length||m.some((y,A)=>{const h=I[A];return!i(h)||h.entityId!==y.entityId||h.entityType!==y.entityType||h.kind!==y.kind||h.contentHash!==y.contentHash}))return r(a,"change.cargo does not exactly name its complete result artifact")}catch(o){return r(a,`change.result was invalid: ${o instanceof Error?o.message:String(o)}`)}return p(e)}c(O,"checkChange");function P(e,t){const s=e.replay;if(Object.keys(s).length!==2||!Array.isArray(s.pages)||s.pages.length===0)throw new Error("package replay must name its pages");for(const o of s.pages)if(T(o),Object.keys(o).length!==4)throw new Error("a package page names only its sha256, bytes, records and first key");if(e.origin!=="detected"||e.basedOnState!==null||e.facets.length!==1||e.facets[0]!=="create"||t.status!=="active"||!q(t.type))throw new Error("a package must create one active container or reference anchor");const n=G({result:t,replay:s,cargo:[]}).replay;n.kind==="repo.git"&&w(n,t)}c(P,"assertPackageChange");function k(e){if(!i(e)||e.contract!==S)return r(a,`contract was not ${S}`);for(const s of["mutationId","authorityId"])if(!d(e[s]))return r(a,`${s} was not a non-empty string`);if(typeof e.entityId!="string"||!l.test(e.entityId))return r(a,"entityId was not an entity UUID");if(e.basedOnState!==null&&(typeof e.basedOnState!="string"||!g.test(e.basedOnState)))return r(a,"basedOnState was not null or lowercase SHA-256");if(typeof e.resultState!="string"||!g.test(e.resultState))return r(a,"resultState was not lowercase SHA-256");if(!f(e.lastSeenGlobalSequence)||!f(e.createdAt))return r(a,"lastSeenGlobalSequence and createdAt must be non-negative safe integers");const t=O(e.change);if(!t.ok)return t;if(t.value.entityId!==e.entityId||t.value.basedOnState!==e.basedOnState||t.value.result.version!==e.resultState)return r(a,"record lineage did not exactly match its Detect proposal");if(e.together!==void 0){const s=_(e.entityId,e.together);if(!s.ok)return s}return p(e)}c(k,"checkLocalEntityRecord");const U=["basedOnState","change","entityId","lastSeenGlobalSequence","resultState"];function _(e,t){if(!Array.isArray(t)||t.length===0)return r(a,"together was not a non-empty array");const s=new Set([e]);for(const n of t){if(!i(n)||Object.keys(n).sort().join()!==U.join())return r(a,"a change made together names only its entity, lineage and change");if(typeof n.entityId!="string"||!l.test(n.entityId)||s.has(n.entityId))return r(a,"a change made together names another entity, each once");if(s.add(n.entityId),typeof n.basedOnState!="string"||!g.test(n.basedOnState)||typeof n.resultState!="string"||!g.test(n.resultState)||!f(n.lastSeenGlobalSequence))return r(a,"a change made together changes an entity that already has a channel");const o=O(n.change);if(!o.ok)return o;if(o.value.entityId!==n.entityId||o.value.basedOnState!==n.basedOnState||o.value.result.version!==n.resultState)return r(a,"a change made together did not exactly match its Detect proposal");if(o.value.replay.kind==="package"||o.value.turn!==void 0)return r(a,"a change made together is never a package or a turn")}return p(t)}c(_,"checkTogether");function J(e){const t=k(e);return t.ok?t.value.together!==void 0?r(a,"an accepted record states one channel: its changes made together are accepted on their own"):!i(e)||!f(e.globalSequence,!0)||!d(e.actorId)||!d(e.originDeviceId)||!f(e.acceptedAt)?r(a,"accepted record lacked a positive sequence or server-derived acceptance fields"):p(e):t}c(J,"checkAcceptedEntityRecord");function Q(e){const t=Object.freeze({contract:S,mutationId:e.mutationId,authorityId:e.authorityId,entityId:e.change.entityId,basedOnState:e.change.basedOnState,resultState:e.change.result.version,lastSeenGlobalSequence:e.lastSeenGlobalSequence,change:e.change,createdAt:e.createdAt,...e.together===void 0||e.together.length===0?{}:{together:Object.freeze([...e.together])}}),s=k(t);if(!s.ok)throw new Error(s.error);return t}c(Q,"createLocalEntityRecord");export{S as ENTITY_RECORD_CONTRACT,x as channelRecords,J as checkAcceptedEntityRecord,k as checkLocalEntityRecord,Q as createLocalEntityRecord,B as recordCargo};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var x=Object.defineProperty;var m=(l,e)=>x(l,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyJourney=void 0,exports.stepsAside=P,exports.outcomeOf=H;const j=require("./contracts.js"),q=require("./plans.js"),S=require("../faults/recover.js"),O=require("../faults/classify.js"),M=require("../clash/local-work.js"),E=require("./selection.js"),W=require("./settling.js"),F=require("./window.js");function P(l,e){return l.unit===null&&l.authorityId!==e}m(P,"stepsAside");const I={kind:"park",reason:"its window ended with its head unplaced and named no wait",releaseOnProgress:!1,report:!1,again:!0},D=m(l=>l.kind==="captured-local"||l.kind==="rejudge","drops");function A(l){return l.kind==="retry"?{kind:"park",reason:l.reason??"machine truth is unsettled",releaseOnProgress:!1,report:!1}:l.kind==="dependency"?{kind:"park",reason:l.reason,releaseOnProgress:l.waitFor===void 0,report:!1,...l.waitFor?{dependency:l.waitFor}:{},...l.blocker?{blocker:l.blocker}:{},..."comparedAt"in l?{comparedAt:l.comparedAt}:{}}:l.kind==="conflict"?{kind:"park",reason:l.reason,releaseOnProgress:!0,report:!0}:null}m(A,"parkFor");async function H(l){try{return await l()}catch(e){return{kind:"park",reason:e instanceof Error?e.message:String(e),releaseOnProgress:!1,report:!(0,O.groundMoved)(e),cause:e}}}m(H,"outcomeOf");const v=m(l=>(0,W.isSettlement)(l.judgment)?{kind:"settled",settlement:l.judgment}:null,"settled");class U{static{m(this,"EntityApplyJourney")}#e;#r;#s;#n;constructor(e,t,
|
|
1
|
+
"use strict";var x=Object.defineProperty;var m=(l,e)=>x(l,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyJourney=void 0,exports.stepsAside=P,exports.outcomeOf=H;const j=require("./contracts.js"),q=require("./plans.js"),S=require("../faults/recover.js"),O=require("../faults/classify.js"),M=require("../clash/local-work.js"),E=require("./selection.js"),W=require("./settling.js"),F=require("./window.js");function P(l,e){return l.unit===null&&l.authorityId!==e}m(P,"stepsAside");const I={kind:"park",reason:"its window ended with its head unplaced and named no wait",releaseOnProgress:!1,report:!1,again:!0},D=m(l=>l.kind==="captured-local"||l.kind==="rejudge","drops");function A(l){return l.kind==="retry"?{kind:"park",reason:l.reason??"machine truth is unsettled",releaseOnProgress:!1,report:!1}:l.kind==="dependency"?{kind:"park",reason:l.reason,releaseOnProgress:l.waitFor===void 0,report:!1,...l.waitFor?{dependency:l.waitFor}:{},...l.blocker?{blocker:l.blocker}:{},..."comparedAt"in l?{comparedAt:l.comparedAt}:{}}:l.kind==="conflict"?{kind:"park",reason:l.reason,releaseOnProgress:!0,report:!0}:null}m(A,"parkFor");async function H(l){try{return await l()}catch(e){return{kind:"park",reason:e instanceof Error?e.message:String(e),releaseOnProgress:!1,report:!(0,O.groundMoved)(e),cause:e}}}m(H,"outcomeOf");const v=m(l=>(0,W.isSettlement)(l.judgment)?{kind:"settled",settlement:l.judgment}:null,"settled");class U{static{m(this,"EntityApplyJourney")}#e;#r;#s;#n;constructor(e,t,s){this.#e=e,this.#r={sha256Hex:e.sha256Hex,loops:e.loops,deliveryPosition:e.deliveryPosition},this.#s=t,this.#n=s}targetOf(e){const t=e.members[0];return t===void 0?null:e.intent?.members.find(s=>s.entityId===t.work.entityId)?.throughSequence??(t.work.kept===null?t.work.records.at(-1)?.globalSequence:t.work.resolvedThrough)??null}async run(e,t){if(e.intent!==null)return this.#p(e,e.intent);const s={member:null};try{return await this.#d(e,t,s)}catch(i){if(s.member===null||this.#n())throw i;if((0,O.groundMoved)(i))return{kind:"park",reason:i instanceof Error?i.message:String(i),releaseOnProgress:!1,report:!1};if(P(e,s.member.work.entityId))return this.alone(e,s.member);if(i instanceof S.Recovering)return{kind:"park",reason:i.message,releaseOnProgress:!1,report:!1,dependency:{producer:"capability",entityIds:i.need.subject===null?[]:[i.need.subject.id]}};throw i}}async alone(e,t=e.members[0]){const{target:s}=(0,E.selectEntityApply)(t.work,this.#r),i=await this.#i({authorityId:t.work.authorityId,entityId:t.work.entityId,targetType:s.result.type,kept:s.record===null?s.result:null,throughSequence:s.throughSequence}),n=await this.#t(e.intent,[t],[{kind:"settled",settlement:i?"superseded":"waiting"}]);return n==="progress"&&!i?{kind:"alone",entityId:t.work.entityId}:n}async#d(e,t,s){const i=this.#e.newIntentId(),n=this.#e.now(),o=t(),a=new Map;for(const c of e.members){if(v(c))continue;const{work:f}=c,w=(0,E.selectEntityApply)(f,this.#r),b=(0,j.createApplyIntent)({attemptId:i,work:f,target:w.target,unit:e.unit,cutoff:e.cutoff,createdAt:n,emptied:f.entityId===o&&f.play==="enter"&&(w.target.result.type==="file.text"||w.target.result.type==="file.binary")}),h=(0,q.planSelectedEntityApply)(f,w,this.#e.sha256Hex);a.set(f.entityId,{member:c,intent:b,plan:h,adapter:this.#e.effects.adapterFor(h.target.type),look:"safe"})}const{active:d,blocked:y}=await this.#o(e.members,a,"look",s);if(d.length===0)return y??I;const g=d.flatMap(c=>a.get(c.work.entityId)??[]);if(g.length===0)return this.#t(null,d,d.map(c=>v(c)));const k=(0,j.createApplyWindowIntent)({attemptId:i,authorityId:e.authorityId,unit:e.unit,cutoff:e.cutoff,createdAt:n,members:g.map(c=>c.intent)});try{const c=await this.#u(d,a,s),f=c.active.flatMap(r=>a.get(r.work.entityId)??[]);if(f.length===0)return await this.#e.effects.discard(k),c.active.length===0?c.blocked??I:this.#t(null,c.active,c.active.map(r=>v(r)));const w=f.length===g.length?k:(0,j.createApplyWindowIntent)({...k,members:f.map(r=>r.intent)}),b=await this.#e.effects.hold(w,f.map(r=>r.plan));let h,_;try{h=await this.#o(c.active,a,"final",s),_=await this.#c(w,h,a,t)}finally{b()}return h.unseen!==null&&h.active.length===0?this.#y(h.unseen):_}catch(c){throw await this.#e.effects.discard(k),c}}async#c(e,{active:t,blocked:s},i,n){const o=t.flatMap(r=>i.get(r.work.entityId)??[]);if(o.length===0)return await this.#e.effects.discard(e),t.length===0?s??I:this.#t(null,t,t.map(r=>v(r)));const a=(0,j.createApplyWindowIntent)({...e,members:o.map(r=>r.intent)}),d=o.filter(r=>r.look==="safe");if(d.length>0){if(this.#n()||o.some(p=>p.intent.emptied&&n()!==p.intent.entityId))return await this.#e.effects.discard(e),I;const r=await this.#e.store.bindWindow(a);if(r)return await this.#e.effects.discard(e),r.kind==="stale"?I:A({...r,comparedAt:this.#s()})}const y=new Map,g=new Map,k=m((r,p,u=null)=>{y.set(r,{kind:"owed",reason:p}),u!==null&&g.set(r,u)},"owe"),c=m(r=>A(r.kind==="dependency"?{...r,comparedAt:this.#s()}:r),"waitFrom"),{order:f,cycle:w}=(0,F.revealOrder)(d.map(r=>({entityId:r.intent.entityId,play:r.plan.play,type:r.plan.target.type,parentUUID:r.plan.target.parentUUID}))),b="its parent chain closes among the window's members";for(const r of w)k(r,b,{kind:"park",reason:b,releaseOnProgress:!1,report:!0});for(const r of f){const p=i.get(r),u=await p.adapter.apply.reveal(p.intent,p.plan);u&&k(r,u.reason,u.kind==="dependency"?c(u):null)}for(const r of o){const p=r.intent.entityId;if(y.has(p))continue;const u=await r.adapter.verify.verify(r.intent,r.plan);u.kind==="match"?y.set(p,{kind:"materialized"}):u.kind==="local-change"?k(p,"a person's version stands in its place"):u.kind==="rejudge"||u.kind==="missing-target"?k(p,u.kind):k(p,u.reason??u.kind,c(u))}const h=t.map(r=>v(r)??y.get(r.work.entityId));if(d.length>0)return this.#a(a,t,h,g);const _=await this.#a(null,t,h,g);return await this.#e.effects.discard(e),_}async#a(e,t,s,i){const n=await this.#t(e,t,s),o=s[0];return n!=="progress"||o.kind!=="owed"?n:{...i.get(t[0].work.entityId)??{kind:"park",reason:o.reason,releaseOnProgress:!1,report:!1,again:!0},advanced:t.filter((d,y)=>s[y].kind!=="owed").map(d=>d.work.entityId)}}async#o(e,t,s,i){const n=[];for(const o of e){const a=t.get(o.work.entityId);if(!a){n.push(o);continue}let d;try{d=await this.#i(a.intent)?{kind:"captured-local"}:s==="look"?await this.#f(a):await a.adapter.apply.guard(a.intent,a.plan,s)}catch(g){if(n.length>0&&!this.#n())return{active:n,blocked:null,unseen:null};throw i.member=o,g}if(d.kind==="local-change"||d.kind==="unsettled-ground")return{active:n,blocked:n.length===0?I:null,unseen:a};if(D(d))return{active:n,blocked:n.length===0?I:null,unseen:null};const y=A(d);if(y)return{active:n,blocked:y,unseen:null};a.look=d.kind,n.push(o)}return{active:n,blocked:null,unseen:null}}async#u(e,t,s){for(const[i,n]of e.entries()){const o=t.get(n.work.entityId);if(!(!o||o.look!=="safe"))try{const a=await o.adapter.apply.prepare(o.intent,o.plan);if(a)return{active:e.slice(0,i),blocked:A(a)}}catch(a){if(i>0&&!this.#n())return{active:e.slice(0,i),blocked:null};throw s.member=n,a}}return{active:[...e],blocked:null}}async#p(e,t){const s=t.members.map(n=>{const o=e.members.find(d=>d.work.entityId===n.entityId);if(!o)return null;const a=n.kept===null?o.work:{...o.work,records:[],kept:n.kept};return a.records.length===0&&a.kept===null?null:(0,q.planSelectedEntityApply)(a,(0,E.selectEntityApply)(a,this.#r,n),this.#e.sha256Hex)}),i=await this.#e.effects.recover(t,s);if(i.length!==t.members.length)throw new Error("Apply recovery answered for a different window");return this.#t(t,e.members,e.members.map(n=>v(n)??i[t.members.findIndex(o=>o.entityId===n.work.entityId)]??{kind:"owed",reason:"not a member of the bound window"}))}async#t(e,t,s){const i=await this.#e.store.endWindow(e,t,s);if(!i)return"progress";if(i.kind!=="stale"||!i.reason)throw new Error("Apply end commit returned an unknown refusal");return I}async#i(e){return(0,M.localWorkWins)(e.targetType,await this.#e.store.localWorkAfter(e.authorityId,e.entityId,e.kept??e.throughSequence))}async#f(e){const{intent:t,plan:s,adapter:i}=e,n=await i.apply.guard(t,s,"look");if(n.kind!=="local-change"&&n.kind!=="unsettled-ground")return n;const o=await this.#l(t,s);if(o.kind!=="observed")return o;const a=await i.apply.guard(t,s,"look");return a.kind==="local-change"||a.kind==="unsettled-ground"?{kind:"retry",reason:"Ground still needs observation after Detect; this member cannot reveal."}:a}async#y(e){const t=await this.#l(e.intent,e.plan);return t.kind==="captured-local"?I:A(t.kind==="observed"?{kind:"retry",reason:"Its final guard found ground Detect had not recorded; it is judged again after Detect looks."}:t)}async#l(e,t){if(await this.#i(e))return{kind:"captured-local"};let s,i=!0;const n=await this.#e.effects.captureLocal(e,t,()=>{if(!i||s!==void 0)throw new Error("Apply capture comparison must be marked exactly once");s=this.#s()}).finally(()=>{i=!1});if(!n||!["settled","local-only","retry"].includes(n.kind))throw new Error("Apply capture returned no explicit observation outcome");if(await this.#i(e))return{kind:"captured-local"};if(n.kind==="retry")return n;if(n.kind==="local-only"){if(s===void 0)throw new Error("local-only capture requires a synchronous comparison fence");if(!n.reason||n.entityIds.length===0||n.entityIds.some(o=>!o))throw new Error("local-only capture must name the retained ground and its releasing condition");return{kind:"dependency",reason:n.reason,comparedAt:s,waitFor:{producer:"detect",entityIds:n.entityIds}}}return{kind:"observed"}}}exports.EntityApplyJourney=U;
|
package/dist-cjs/apply/line.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var d=Object.defineProperty;var i=(e,n)=>d(e,"name",{value:n,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.invalidStep=void 0,exports.createApplyLine=f,exports.releaseApplyLine=p,exports.heldWait=c,exports.assertApplyDependency=S,exports.assertApplyLineStep=m,exports.assertApplyWindow=s;const o=require("../entities/types.js");function f(e){return{kind:"line",authorityId:e,held:null,queued:!1,running:!1,again:!1}}i(f,"createApplyLine");function p(e,n,u){const r=e.held;return!r?.dependency||r.due||r.dependency.producer!==n||u!==void 0&&!r.dependency.entityIds.some(l=>u.includes(l))?!1:(r.due=!0,!0)}i(p,"releaseApplyLine");function c(e,n){if(e)return e.entityId===null||e.target===null?{entityId:e.entityId??n,reason:e.reason,throughSequence:e.target??e.cutoff??0}:{entityId:e.entityId,reason:e.reason,...e.dependency?{prerequisite:e.dependency}:{},throughSequence:e.target,...e.blocker?{blocker:e.blocker}:{}}}i(c,"heldWait");const y=new Set(["detect","apply","capability","send"]),A=i(e=>!!e&&y.has(e.producer)&&Array.isArray(e.entityIds)&&e.entityIds.length>0&&e.entityIds.every(n=>typeof n=="string"&&n.length>0),"namesRelease");function S(e){if(!A(e))throw new Error("Apply dependency must name the entities that can release it")}i(S,"assertApplyDependency");function m(e,n){(!n||typeof n!="object")&&(0,exports.invalidStep)(`line step for ${e}`),n.kind!=="end"&&((n.kind!=="window"||n.window?.authorityId!==e||n.window.unit!==null||n.window.cutoff!==null||n.window.members?.some(u=>u.work?.kept!==null)||n.window.intent!==null&&(n.window.intent.unit!==null||n.window.intent.cutoff!==null))&&(0,exports.invalidStep)(`line step for ${e}`),s(n.window,`line step for ${e}`))}i(m,"assertApplyLineStep");const w=i(e=>{throw new Error(`entity Apply store returned an invalid ${e}`)},"invalidStep");exports.invalidStep=w;function s(e,n){Array.isArray(e.members)||(0,exports.invalidStep)(n);const u=i((t,a)=>Number.isSafeInteger(t)&&t>=(a?0:1),"at");for(const t of e.members)(!o.UUID_PATTERN.test(t.work?.entityId??"")||t.work.authorityId!==e.authorityId||!u(t.deliverySequence,t.work.kept!==null))&&(0,exports.invalidStep)(n),(t.unseen!==void 0||t.judgment==="unseen")&&(o.UUID_PATTERN.test(t.unseen??"")||(0,exports.invalidStep)(n));const{deliverySequence:r}=e,l=e.members[0];(r===null?!e.intent:l===void 0||!u(r,l.work.kept!==null))&&(0,exports.invalidStep)(n)}i(s,"assertApplyWindow");
|
package/dist-cjs/apply/rail.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var I=Object.defineProperty;var o=(a,e)=>I(a,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyRail=exports.ENTITY_APPLY_RETRY_MAX_MS=exports.ENTITY_APPLY_RETRY_BASE_MS=exports.ENTITY_APPLY_DEFAULT_CONCURRENCY=void 0;const f=require("../entities/types.js"),_=require("./fetch-ahead.js"),c=require("./journey.js"),g=require("./lane.js"),l=require("./line.js");exports.ENTITY_APPLY_DEFAULT_CONCURRENCY=8,exports.ENTITY_APPLY_RETRY_BASE_MS=1e3,exports.ENTITY_APPLY_RETRY_MAX_MS=3e4;function E(a,e){return e.cause===void 0&&(0,c.stepsAside)(a,a.members[0].work.entityId)}o(E,"stuck");class k{static{o(this,"EntityApplyRail")}#e;#g;#P;#Y;#M;#a;#T;#o=!1;#t=!1;#d=new Map;#s=new Map;#l={queue:[],steps:new Set};#u={queue:[],steps:new Set};#n=!1;#h=null;#p=1/0;#m=[];#A=0;#b=new Map;#y=null;constructor(e){this.#e=e,this.#g=e.concurrency??exports.ENTITY_APPLY_DEFAULT_CONCURRENCY,this.#P=e.laneConcurrency??this.#g,this.#Y=e.onBackgroundError??(()=>{}),this.#M=e.onIdle??(()=>{});for(const[t,s]of[["concurrency",this.#g],["lane concurrency",this.#P]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${t} must be a positive integer`);this.#a=new c.EntityApplyJourney(e,()=>this.#A,()=>this.#t),this.#T=new _.FetchAhead(e.cargo,{sha256Hex:e.sha256Hex,loops:e.loops,deliveryPosition:e.deliveryPosition})}async start(){if(this.#o)throw new Error("entity Apply rail already started");if(this.#t)throw new Error("entity Apply rail is stopped");this.#o=!0;const e=await this.#e.store.readOpenUnits();if(!Array.isArray(e)||new Set(e).size!==e.length||e.some(t=>!f.UUID_PATTERN.test(t)))throw new Error("entity Apply store returned an invalid open unit list");for(const t of e)this.#E(t,null);await this.#N()}fetch(e,t){return this.#o?this.#t?Promise.reject(new Error("entity Apply rail is stopped")):f.UUID_PATTERN.test(e)?!Number.isFinite(t.stall)||t.stall<0?Promise.reject(new Error("entity Apply fetch needs a non-negative stall")):t.emptied!==void 0&&typeof t.emptied!="boolean"?Promise.reject(new Error("entity Apply fetch emptied must be a boolean")):new Promise((s,i)=>{const r=this.#E(e,null,t.emptied===!0),n=o(()=>this.#e.schedule.schedule(t.stall,()=>{r.waiters.delete(u)&&(r.emptied&&![...r.waiters].some(d=>d.emptied)&&(r.emptied=!1),s(this.#G(r)))}),"arm");let h=n();const u={emptied:t.emptied===!0,answer:o(d=>{h.cancel(),s(d)},"answer"),fail:o(d=>{h.cancel(),i(d)},"fail"),moved:o(()=>{h.cancel(),h=n()},"moved")};r.waiters.add(u),r.waitsOn===null&&this.#U(r),this.#r()}):Promise.reject(new Error("entity Apply fetch needs a unit UUID")):Promise.reject(new Error("entity Apply rail has not started"))}progressed(e,t){if(!f.UUID_PATTERN.test(e))throw new Error("entity Apply progress needs a unit UUID");if(!Number.isSafeInteger(t)||t<0)throw new Error("entity Apply progress needs a byte count");t>0&&!this.#t&&this.#k(e)}recordsAvailable(){this.#I()}localStateChanged(e){this.#c("detect",e),this.#c("apply",e),this.#I()}sendAccepted(e){this.#c("send",e),this.#I()}capabilityChanged(e){this.#c("capability",e),this.#I()}owe(e){if(e.some(t=>!f.UUID_PATTERN.test(t)))throw new Error("entity Apply owe needs entity UUIDs");if(!(!this.#o||this.#t)){for(const t of e)this.#O(t);this.#r()}}#I(){!this.#o||this.#t||(this.#n=!0,this.#r())}waiting(){return this.#w().flatMap(e=>(0,l.heldWait)(e.held,y(e))??[])}async flush(){if(!this.#o)throw new Error("entity Apply rail has not started");this.#t||await this.#N()}async stop(){if(this.#t)return;this.#t=!0,this.#S();for(const t of[this.#l,this.#u])for(const s of t.queue.splice(0))s.queued=!1;const e=new Error("entity Apply rail stopped before the fetch answered");for(const t of this.#s.values())for(const s of t.waiters)s.fail(e);for(this.#s.clear();this.#$()>0||this.#h;)await Promise.allSettled([...this.#l.steps,...this.#u.steps,...this.#h?[this.#h]:[]]);await this.#T.stop();for(const t of this.#m.splice(0))t.resolve()}#N(){return new Promise((e,t)=>{this.#m.push({resolve:e,reject:t}),this.#n=!0,this.#r()})}#r(){if(!this.#t){if(this.#n&&this.#h===null){this.#n=!1;const e=this.#F();this.#h=e,e.then(()=>{this.#h=null,this.#r()})}for(const[e,t]of[[this.#u,this.#P],[this.#l,this.#g]])for(;e.steps.size<t&&e.queue.length>0;){const s=e.queue.shift();s.queued=!1,this.#z(s,e)}if(this.#$()===0&&this.#h===null&&!this.#n&&this.#l.queue.length===0&&this.#u.queue.length===0){if(this.#b.clear(),this.#W(this.#e.now())){this.#r();return}this.#v();for(const e of this.#m.splice(0))e.resolve();this.#M()}}}#$(){return this.#l.steps.size+this.#u.steps.size}#w(){return[...this.#d.values(),...this.#s.values()]}#_(e){return e.kind==="line"?this.#d.get(e.authorityId)===e:this.#s.get(e.unit)===e}#i(e){if(!this.#t){if(e.running){e.again=!0;return}e.queued||(e.queued=!0,(e.kind==="line"?this.#l:this.#u).queue.push(e))}}#z(e,t){e.running=!0,e.again=!1;const s=(e.kind==="line"?this.#X(e):this.#J(e)).then(i=>{e.running=!1,t.steps.delete(s),(i||e.again)&&this.#_(e)&&(e.again=!1,this.#i(e)),this.#r()});t.steps.add(s)}async#F(){try{const e=await this.#e.store.readApplyAuthorities();if(!Array.isArray(e)||new Set(e).size!==e.length||e.some(s=>typeof s!="string"||s.length===0))throw new Error("entity Apply store returned an invalid authority list");this.#p=1/0;const t=new Set(e);for(const[s,i]of this.#d)!t.has(s)&&!i.running&&!i.queued&&this.#d.delete(s);for(const s of e){let i=this.#d.get(s);i||(i=(0,l.createApplyLine)(s),this.#d.set(s,i)),this.#i(i)}}catch(e){this.#p=this.#e.now()+exports.ENTITY_APPLY_RETRY_MAX_MS;const t=this.#m.splice(0);t.length===0&&this.#R(e);for(const s of t)s.reject(e)}}async#X(e){try{const t=e.held;if(t&&t.entityId===null&&!t.due&&t.notBefore>this.#e.now())return!1;const s=this.#A,i=await this.#e.store.readApplyWindow(e.authorityId);return(0,l.assertApplyLineStep)(e.authorityId,i),this.#t?!1:i.kind==="end"?(e.held=null,!1):(i.window.deliverySequence!==null&&this.#T.advance(e.authorityId,i.window.deliverySequence),await this.#j(e,i.window,s))}catch(t){return this.#B(e,t),!1}}async#J(e){try{const t=e.held;if(t&&t.entityId===null&&!t.due&&t.notBefore>this.#e.now())return!1;const s=this.#A,i=await this.#e.store.readFetchWindow(e.unit,e.toPrefix?null:e.through);if((0,g.assertApplyLaneStep)(e.unit,i),this.#t||!this.#_(e))return!1;if(e.waitsOn=null,i.kind==="unseen")return this.#q(e,{producer:"detect",entityIds:[e.unit]},i.reason,s);if(!e.again&&(i.kind!=="window"||i.window.intent===null)&&(e.toPrefix=!1),e.through!==null&&i.through>e.through&&e.met.clear(),e.through=Math.max(e.through??0,i.through),i.kind!=="window"&&(e.held=null),e.again&&(i.kind==="end"||i.kind==="nowhere"))return!0;switch(i.kind){case"end":return this.#f(e,{kind:"placed"}),!1;case"nowhere":return this.#f(e,{kind:"nowhere",reason:i.reason}),!1;case"root":return await this.#H(e,i.through,i.reason);case"prerequisite":return this.#V(e,i.unit,i.reason,s);case"window":return await this.#j(e,i.window,s)}}catch(t){return this.#B(e,t),!1}}async#H(e,t,s){return e.met.has(e.unit)?(this.#f(e,{kind:"held",unit:e.unit,reason:`${e.unit} entered and still not held: ${s}`}),!1):(await this.#e.effects.enterRoot(e.unit,t),this.#t||!this.#_(e)?!1:(e.through===t&&e.met.add(e.unit),this.#k(e.unit),!0))}#O(e){const t=this.#E(e,null);t.toPrefix=!0,t.met.clear(),this.#i(t)}#E(e,t,s=!1){let i=this.#s.get(e);return i?t!==null&&(i.through===null||i.through<t)&&(i.through=t,i.met.clear(),this.#i(i)):(i=(0,g.createApplyLane)(e,t,s),this.#s.set(e,i),this.#i(i)),i}#V(e,t,s,i){return e.met.has(t)?(this.#f(e,{kind:"held",unit:t,reason:`${t} placed and still needed: ${s}`}),!1):this.#L(e,t,s)?this.#q(e,{producer:"apply",entityIds:[t]},s,i):!1}#q(e,t,s,i){return e.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#x(t,i),reason:s,dependency:t},e.held.due}#U(e){const t=e.held;if(t?.dependency?.producer==="apply"){for(const s of t.dependency.entityIds)if(s!==e.unit&&!e.met.has(s)&&!this.#L(e,s,t.reason))return}}#L(e,t,s){const i=[e];let r=this.#s.get(t);for(;r&&r!==e&&!i.includes(r);)i.push(r),r=r.waitsOn===null?void 0:this.#s.get(r.waitsOn);if(r===e){for(const n of i)this.#f(n,{kind:"held",unit:t,reason:`prerequisite cycle through ${t}: ${s}`});return!1}return e.waitsOn??=t,this.#i(this.#E(t,e.through)),!0}#f(e,t){if(!this.#_(e))return;for(const i of e.waiters)i.answer(t);e.waiters.clear(),e.waitsOn=null,(t.kind==="placed"||e.held?.entityId==null)&&this.#s.delete(e.unit);const s=t.kind==="placed"||t.kind==="held"?t:{kind:"held",unit:e.unit,reason:`${e.unit} is nowhere here: ${t.reason}`};for(const i of[...this.#s.values()])if(i.waitsOn===e.unit){if(t.kind!=="placed"){this.#f(i,s);continue}i.met.add(e.unit)}t.kind==="placed"&&(this.#k(e.unit),this.#c("apply",[e.unit]))}#k(e){const t=new Map([[e,!0]]);for(const s of this.#s.values()){const i=new Set;let r=s.unit;for(;r!==null&&!t.has(r)&&!i.has(r);)i.add(r),r=this.#s.get(r)?.waitsOn??null;const n=r!==null&&t.get(r)===!0;for(const h of i)t.set(h,n);if(n)for(const h of s.waiters)h.moved()}}#G(e){let t=e;const s=new Set([e]);for(let i=e.waitsOn===null?void 0:this.#s.get(e.waitsOn);i&&!s.has(i);i=i.waitsOn===null?void 0:this.#s.get(i.waitsOn))s.add(i),t=i;return{kind:"held",unit:t.unit,reason:t.held?.reason??"still placing"}}async#j(e,t,s){const i=t.members[0]?.work.entityId??null;if(e.held&&e.held.entityId!==null&&e.held.entityId!==i&&(e.held=null),await this.#Q(e,t))return!1;let r=await(0,c.outcomeOf)(()=>this.#a.run(t,()=>e.kind==="lane"&&e.emptied?e.unit:null));if(e.kind==="line"&&!this.#t&&typeof r=="object"&&r.kind==="park"&&E(t,r)&&(r=await(0,c.outcomeOf)(()=>this.#a.alone(t.members[0]))),r==="progress"||typeof r=="object"&&r.kind==="alone"){if(e.kind==="line")for(const n of t.members)(n.judgment==="waiting"||r!=="progress"&&n.work.entityId===r.entityId)&&this.#O(n.work.entityId);for(const n of t.members)n.unseen!==void 0&&this.#O(n.unseen);return this.#Z(e,r==="progress"?t.members.map(n=>n.work.entityId):[r.entityId])}return r.advanced?.length&&this.#C(e,r.advanced),this.#ee(e,t,r,s),e.kind==="lane"&&this.#U(e),e.held.due}#K(e,t){const s=e.held,i=t.members[0].work.entityId;return(s?.entityId===i&&s.target===this.#a.targetOf(t)?s.attempt:0)+1}async#Q(e,t){const s=e.held,i=t.members[0];return!s||i===void 0||s.entityId!==i.work.entityId||s.due||s.notBefore<=this.#e.now()||this.#a.targetOf(t)!==s.target||t.cutoff!==s.cutoff?!1:await this.#e.store.localWorkAfter(t.authorityId,i.work.entityId,s.kept??s.target)===null}#Z(e,t){return e.held&&(e.held.entityId===null||t.includes(e.held.entityId))&&(e.held=null),this.#C(e,t),!0}#C(e,t){e.kind==="lane"&&(this.#n=!0,this.#k(e.unit)),this.#c("apply",t);for(const s of this.#w())s!==e&&s.held?.releaseOnProgress&&!s.held.due&&(s.held.due=!0,this.#i(s))}#ee(e,t,s,i){if(s.dependency&&(0,l.assertApplyDependency)(s.dependency),s.blocker&&!s.dependency)throw new Error("Apply blocker must name the dependency that releases it");const r=t.members[0].work.entityId,n=e.held,h=this.#K(e,t),u=this.#D(h),d=s.again===!0&&h===1,w={entityId:r,target:this.#a.targetOf(t),kept:t.members[0].work.kept,cutoff:t.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#e.now()+(d?0:u),releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#x(s.dependency,s.comparedAt??i):d,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};e.held=w,this.#v();const A=(0,l.heldWait)(n,y(e)),p=(0,l.heldWait)(w,y(e));p&&JSON.stringify(p)!==JSON.stringify(A)&&this.#e.onWaiting?.(p),!(!s.report&&(s.dependency!==void 0||h===1))&&this.#R(new Error(`${m(e)} holds at ${r}, which ${s.releaseOnProgress?"is parked on a conflict":s.report?"failed":s.again?"keeps ending unplaced":"stands on ground that keeps moving"}, and will retry in ${u}ms (attempt ${h}): ${s.reason}`,s.cause===void 0?void 0:{cause:s.cause}))}#B(e,t){if(e.kind==="lane"){for(const h of e.waiters)h.fail(t);e.waiters.clear()}const s=e.held,i=(s?.entityId===null?s.attempt:0)+1,r=this.#D(i),n=t instanceof Error?t.message:String(t);e.held={entityId:null,target:null,kept:null,cutoff:null,attempt:i,notBefore:this.#e.now()+r,releaseOnProgress:!1,due:!1,reason:n},this.#v(),this.#R(new Error(`${m(e)} faulted and will retry in ${r}ms (attempt ${i}): ${n}`,{cause:t}))}#R(e){try{this.#Y(e)}catch{}}#D(e){const t=Math.min(exports.ENTITY_APPLY_RETRY_MAX_MS,exports.ENTITY_APPLY_RETRY_BASE_MS*2**Math.min(e-1,16)),s=Math.max(0,Math.min(1,this.#e.schedule.random()));return Math.round(t*(.5+s))}#c(e,t){const s=++this.#A;for(const i of t??["*"])this.#b.set(`${e}:${i}`,s);for(const i of this.#w())(0,l.releaseApplyLine)(i,e,t)&&this.#i(i)}#x(e,t){return["*",...e.entityIds].some(s=>(this.#b.get(`${e.producer}:${s}`)??0)>t)}#v(){if(this.#t)return;let e=this.#p;for(const i of this.#w())i.held&&!i.held.due&&(e=Math.min(e,i.held.notBefore));if(e===1/0){this.#S();return}if(this.#y?.dueAt===e)return;this.#S();const t=Math.max(0,e-this.#e.now()),s=this.#e.schedule.schedule(t,()=>{this.#y=null,this.#W(e),this.#r()});this.#y={dueAt:e,cancel:o(()=>s.cancel(),"cancel")}}#W(e){let t=!1;for(const s of this.#w())s.held&&!s.held.due&&s.held.notBefore<=e&&(s.held.due=!0,this.#i(s),t=!0);return this.#p<=e&&(this.#p=1/0,this.#n=!0,t=!0),t}#S(){this.#y?.cancel(),this.#y=null}}exports.EntityApplyRail=k;const y=o(a=>a.kind==="line"?a.authorityId:a.unit,"owner"),m=o(a=>a.kind==="line"?`entity Apply line ${a.authorityId}`:`entity Apply fetch of ${a.unit}`,"label");
|
|
1
|
+
"use strict";var I=Object.defineProperty;var o=(a,e)=>I(a,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyRail=exports.ENTITY_APPLY_RETRY_MAX_MS=exports.ENTITY_APPLY_RETRY_BASE_MS=exports.ENTITY_APPLY_DEFAULT_CONCURRENCY=void 0;const f=require("../entities/types.js"),_=require("./fetch-ahead.js"),c=require("./journey.js"),g=require("./lane.js"),l=require("./line.js");exports.ENTITY_APPLY_DEFAULT_CONCURRENCY=8,exports.ENTITY_APPLY_RETRY_BASE_MS=1e3,exports.ENTITY_APPLY_RETRY_MAX_MS=3e4;function E(a,e){const t=a.members[0];return e.cause===void 0&&t!==void 0&&(0,c.stepsAside)(a,t.work.entityId)}o(E,"stuck");class k{static{o(this,"EntityApplyRail")}#e;#g;#P;#Y;#M;#a;#T;#o=!1;#t=!1;#d=new Map;#s=new Map;#l={queue:[],steps:new Set};#u={queue:[],steps:new Set};#r=!1;#h=null;#p=1/0;#m=[];#A=0;#b=new Map;#y=null;constructor(e){this.#e=e,this.#g=e.concurrency??exports.ENTITY_APPLY_DEFAULT_CONCURRENCY,this.#P=e.laneConcurrency??this.#g,this.#Y=e.onBackgroundError??(()=>{}),this.#M=e.onIdle??(()=>{});for(const[t,s]of[["concurrency",this.#g],["lane concurrency",this.#P]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${t} must be a positive integer`);this.#a=new c.EntityApplyJourney(e,()=>this.#A,()=>this.#t),this.#T=new _.FetchAhead(e.cargo,{sha256Hex:e.sha256Hex,loops:e.loops,deliveryPosition:e.deliveryPosition})}async start(){if(this.#o)throw new Error("entity Apply rail already started");if(this.#t)throw new Error("entity Apply rail is stopped");this.#o=!0;const e=await this.#e.store.readOpenUnits();if(!Array.isArray(e)||new Set(e).size!==e.length||e.some(t=>!f.UUID_PATTERN.test(t)))throw new Error("entity Apply store returned an invalid open unit list");for(const t of e)this.#E(t,null);await this.#N()}fetch(e,t){return this.#o?this.#t?Promise.reject(new Error("entity Apply rail is stopped")):f.UUID_PATTERN.test(e)?!Number.isFinite(t.stall)||t.stall<0?Promise.reject(new Error("entity Apply fetch needs a non-negative stall")):t.emptied!==void 0&&typeof t.emptied!="boolean"?Promise.reject(new Error("entity Apply fetch emptied must be a boolean")):new Promise((s,i)=>{const n=this.#E(e,null,t.emptied===!0),r=o(()=>this.#e.schedule.schedule(t.stall,()=>{n.waiters.delete(u)&&(n.emptied&&![...n.waiters].some(d=>d.emptied)&&(n.emptied=!1),s(this.#G(n)))}),"arm");let h=r();const u={emptied:t.emptied===!0,answer:o(d=>{h.cancel(),s(d)},"answer"),fail:o(d=>{h.cancel(),i(d)},"fail"),moved:o(()=>{h.cancel(),h=r()},"moved")};n.waiters.add(u),n.waitsOn===null&&this.#U(n),this.#n()}):Promise.reject(new Error("entity Apply fetch needs a unit UUID")):Promise.reject(new Error("entity Apply rail has not started"))}progressed(e,t){if(!f.UUID_PATTERN.test(e))throw new Error("entity Apply progress needs a unit UUID");if(!Number.isSafeInteger(t)||t<0)throw new Error("entity Apply progress needs a byte count");t>0&&!this.#t&&this.#k(e)}recordsAvailable(){this.#I()}localStateChanged(e){this.#c("detect",e),this.#c("apply",e),this.#I()}sendAccepted(e){this.#c("send",e),this.#I()}capabilityChanged(e){this.#c("capability",e),this.#I()}owe(e){if(e.some(t=>!f.UUID_PATTERN.test(t)))throw new Error("entity Apply owe needs entity UUIDs");if(!(!this.#o||this.#t)){for(const t of e)this.#O(t);this.#n()}}#I(){!this.#o||this.#t||(this.#r=!0,this.#n())}waiting(){return this.#w().flatMap(e=>(0,l.heldWait)(e.held,w(e))??[])}async flush(){if(!this.#o)throw new Error("entity Apply rail has not started");this.#t||await this.#N()}async stop(){if(this.#t)return;this.#t=!0,this.#S();for(const t of[this.#l,this.#u])for(const s of t.queue.splice(0))s.queued=!1;const e=new Error("entity Apply rail stopped before the fetch answered");for(const t of this.#s.values())for(const s of t.waiters)s.fail(e);for(this.#s.clear();this.#$()>0||this.#h;)await Promise.allSettled([...this.#l.steps,...this.#u.steps,...this.#h?[this.#h]:[]]);await this.#T.stop();for(const t of this.#m.splice(0))t.resolve()}#N(){return new Promise((e,t)=>{this.#m.push({resolve:e,reject:t}),this.#r=!0,this.#n()})}#n(){if(!this.#t){if(this.#r&&this.#h===null){this.#r=!1;const e=this.#F();this.#h=e,e.then(()=>{this.#h=null,this.#n()})}for(const[e,t]of[[this.#u,this.#P],[this.#l,this.#g]])for(;e.steps.size<t&&e.queue.length>0;){const s=e.queue.shift();s.queued=!1,this.#z(s,e)}if(this.#$()===0&&this.#h===null&&!this.#r&&this.#l.queue.length===0&&this.#u.queue.length===0){if(this.#b.clear(),this.#W(this.#e.now())){this.#n();return}this.#v();for(const e of this.#m.splice(0))e.resolve();this.#M()}}}#$(){return this.#l.steps.size+this.#u.steps.size}#w(){return[...this.#d.values(),...this.#s.values()]}#_(e){return e.kind==="line"?this.#d.get(e.authorityId)===e:this.#s.get(e.unit)===e}#i(e){if(!this.#t){if(e.running){e.again=!0;return}e.queued||(e.queued=!0,(e.kind==="line"?this.#l:this.#u).queue.push(e))}}#z(e,t){e.running=!0,e.again=!1;const s=(e.kind==="line"?this.#X(e):this.#J(e)).then(i=>{e.running=!1,t.steps.delete(s),(i||e.again)&&this.#_(e)&&(e.again=!1,this.#i(e)),this.#n()});t.steps.add(s)}async#F(){try{const e=await this.#e.store.readApplyAuthorities();if(!Array.isArray(e)||new Set(e).size!==e.length||e.some(s=>typeof s!="string"||s.length===0))throw new Error("entity Apply store returned an invalid authority list");this.#p=1/0;const t=new Set(e);for(const[s,i]of this.#d)!t.has(s)&&!i.running&&!i.queued&&this.#d.delete(s);for(const s of e){let i=this.#d.get(s);i||(i=(0,l.createApplyLine)(s),this.#d.set(s,i)),this.#i(i)}}catch(e){this.#p=this.#e.now()+exports.ENTITY_APPLY_RETRY_MAX_MS;const t=this.#m.splice(0);t.length===0&&this.#R(e);for(const s of t)s.reject(e)}}async#X(e){try{const t=e.held;if(t&&t.entityId===null&&!t.due&&t.notBefore>this.#e.now())return!1;const s=this.#A,i=await this.#e.store.readApplyWindow(e.authorityId);return(0,l.assertApplyLineStep)(e.authorityId,i),this.#t?!1:i.kind==="end"?(e.held=null,!1):(i.window.deliverySequence!==null&&this.#T.advance(e.authorityId,i.window.deliverySequence),await this.#j(e,i.window,s))}catch(t){return this.#B(e,t),!1}}async#J(e){try{const t=e.held;if(t&&t.entityId===null&&!t.due&&t.notBefore>this.#e.now())return!1;const s=this.#A,i=await this.#e.store.readFetchWindow(e.unit,e.toPrefix?null:e.through);if((0,g.assertApplyLaneStep)(e.unit,i),this.#t||!this.#_(e))return!1;if(e.waitsOn=null,i.kind==="unseen")return this.#q(e,{producer:"detect",entityIds:[e.unit]},i.reason,s);if(!e.again&&(i.kind!=="window"||i.window.intent===null)&&(e.toPrefix=!1),e.through!==null&&i.through>e.through&&e.met.clear(),e.through=Math.max(e.through??0,i.through),i.kind!=="window"&&(e.held=null),e.again&&(i.kind==="end"||i.kind==="nowhere"))return!0;switch(i.kind){case"end":return this.#f(e,{kind:"placed"}),!1;case"nowhere":return this.#f(e,{kind:"nowhere",reason:i.reason}),!1;case"root":return await this.#H(e,i.through,i.reason);case"prerequisite":return this.#V(e,i.unit,i.reason,s);case"window":return await this.#j(e,i.window,s)}}catch(t){return this.#B(e,t),!1}}async#H(e,t,s){return e.met.has(e.unit)?(this.#f(e,{kind:"held",unit:e.unit,reason:`${e.unit} entered and still not held: ${s}`}),!1):(await this.#e.effects.enterRoot(e.unit,t),this.#t||!this.#_(e)?!1:(e.through===t&&e.met.add(e.unit),this.#k(e.unit),!0))}#O(e){const t=this.#E(e,null);t.toPrefix=!0,t.met.clear(),this.#i(t)}#E(e,t,s=!1){let i=this.#s.get(e);return i?t!==null&&(i.through===null||i.through<t)&&(i.through=t,i.met.clear(),this.#i(i)):(i=(0,g.createApplyLane)(e,t,s),this.#s.set(e,i),this.#i(i)),i}#V(e,t,s,i){return e.met.has(t)?(this.#f(e,{kind:"held",unit:t,reason:`${t} placed and still needed: ${s}`}),!1):this.#L(e,t,s)?this.#q(e,{producer:"apply",entityIds:[t]},s,i):!1}#q(e,t,s,i){return e.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#x(t,i),reason:s,dependency:t},e.held.due}#U(e){const t=e.held;if(t?.dependency?.producer==="apply"){for(const s of t.dependency.entityIds)if(s!==e.unit&&!e.met.has(s)&&!this.#L(e,s,t.reason))return}}#L(e,t,s){const i=[e];let n=this.#s.get(t);for(;n&&n!==e&&!i.includes(n);)i.push(n),n=n.waitsOn===null?void 0:this.#s.get(n.waitsOn);if(n===e){for(const r of i)this.#f(r,{kind:"held",unit:t,reason:`prerequisite cycle through ${t}: ${s}`});return!1}return e.waitsOn??=t,this.#i(this.#E(t,e.through)),!0}#f(e,t){if(!this.#_(e))return;for(const i of e.waiters)i.answer(t);e.waiters.clear(),e.waitsOn=null,(t.kind==="placed"||e.held?.entityId==null)&&this.#s.delete(e.unit);const s=t.kind==="placed"||t.kind==="held"?t:{kind:"held",unit:e.unit,reason:`${e.unit} is nowhere here: ${t.reason}`};for(const i of[...this.#s.values()])if(i.waitsOn===e.unit){if(t.kind!=="placed"){this.#f(i,s);continue}i.met.add(e.unit)}t.kind==="placed"&&(this.#k(e.unit),this.#c("apply",[e.unit]))}#k(e){const t=new Map([[e,!0]]);for(const s of this.#s.values()){const i=new Set;let n=s.unit;for(;n!==null&&!t.has(n)&&!i.has(n);)i.add(n),n=this.#s.get(n)?.waitsOn??null;const r=n!==null&&t.get(n)===!0;for(const h of i)t.set(h,r);if(r)for(const h of s.waiters)h.moved()}}#G(e){let t=e;const s=new Set([e]);for(let i=e.waitsOn===null?void 0:this.#s.get(e.waitsOn);i&&!s.has(i);i=i.waitsOn===null?void 0:this.#s.get(i.waitsOn))s.add(i),t=i;return{kind:"held",unit:t.unit,reason:t.held?.reason??"still placing"}}async#j(e,t,s){const i=t.members[0]?.work.entityId??null;if(e.held&&e.held.entityId!==null&&e.held.entityId!==i&&(e.held=null),await this.#Q(e,t))return!1;let n=await(0,c.outcomeOf)(()=>this.#a.run(t,()=>e.kind==="lane"&&e.emptied?e.unit:null));if(e.kind==="line"&&!this.#t&&typeof n=="object"&&n.kind==="park"&&E(t,n)&&(n=await(0,c.outcomeOf)(()=>this.#a.alone(t))),n==="progress"||typeof n=="object"&&n.kind==="alone"){if(e.kind==="line")for(const r of t.members)(r.judgment==="waiting"||n!=="progress"&&r.work.entityId===n.entityId)&&this.#O(r.work.entityId);for(const r of t.members)r.unseen!==void 0&&this.#O(r.unseen);return this.#Z(e,n!=="progress"?[n.entityId]:t.intent?.members.map(r=>r.entityId)??t.members.map(r=>r.work.entityId))}return n.advanced?.length&&this.#C(e,n.advanced),this.#ee(e,t,n,s),e.kind==="lane"&&this.#U(e),e.held.due}#K(e,t){const s=e.held,i=t.members[0]?.work.entityId??null;return(s?.entityId===i&&s.target===this.#a.targetOf(t)?s.attempt:0)+1}async#Q(e,t){const s=e.held,i=t.members[0];return!s||i===void 0||s.entityId!==i.work.entityId||s.due||s.notBefore<=this.#e.now()||this.#a.targetOf(t)!==s.target||t.cutoff!==s.cutoff?!1:await this.#e.store.localWorkAfter(t.authorityId,i.work.entityId,s.kept??s.target)===null}#Z(e,t){return e.held&&(e.held.entityId===null||t.includes(e.held.entityId))&&(e.held=null),this.#C(e,t),!0}#C(e,t){e.kind==="lane"&&(this.#r=!0,this.#k(e.unit)),this.#c("apply",t);for(const s of this.#w())s!==e&&s.held?.releaseOnProgress&&!s.held.due&&(s.held.due=!0,this.#i(s))}#ee(e,t,s,i){if(s.dependency&&(0,l.assertApplyDependency)(s.dependency),s.blocker&&!s.dependency)throw new Error("Apply blocker must name the dependency that releases it");const n=t.members[0],r=e.held,h=this.#K(e,t),u=this.#D(h),d=s.again===!0&&h===1,p={entityId:n?.work.entityId??null,target:this.#a.targetOf(t),kept:n?.work.kept??null,cutoff:t.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#e.now()+(d?0:u),releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#x(s.dependency,s.comparedAt??i):d,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};e.held=p,this.#v();const A=(0,l.heldWait)(r,w(e)),y=(0,l.heldWait)(p,w(e));y&&JSON.stringify(y)!==JSON.stringify(A)&&this.#e.onWaiting?.(y),!(!s.report&&(s.dependency!==void 0||h===1))&&this.#R(new Error(`${m(e)} holds at ${p.entityId??"its open window"}, which ${s.releaseOnProgress?"is parked on a conflict":s.report?"failed":s.again?"keeps ending unplaced":"stands on ground that keeps moving"}, and will retry in ${u}ms (attempt ${h}): ${s.reason}`,s.cause===void 0?void 0:{cause:s.cause}))}#B(e,t){if(e.kind==="lane"){for(const h of e.waiters)h.fail(t);e.waiters.clear()}const s=e.held,i=(s?.entityId===null?s.attempt:0)+1,n=this.#D(i),r=t instanceof Error?t.message:String(t);e.held={entityId:null,target:null,kept:null,cutoff:null,attempt:i,notBefore:this.#e.now()+n,releaseOnProgress:!1,due:!1,reason:r},this.#v(),this.#R(new Error(`${m(e)} faulted and will retry in ${n}ms (attempt ${i}): ${r}`,{cause:t}))}#R(e){try{this.#Y(e)}catch{}}#D(e){const t=Math.min(exports.ENTITY_APPLY_RETRY_MAX_MS,exports.ENTITY_APPLY_RETRY_BASE_MS*2**Math.min(e-1,16)),s=Math.max(0,Math.min(1,this.#e.schedule.random()));return Math.round(t*(.5+s))}#c(e,t){const s=++this.#A;for(const i of t??["*"])this.#b.set(`${e}:${i}`,s);for(const i of this.#w())(0,l.releaseApplyLine)(i,e,t)&&this.#i(i)}#x(e,t){return["*",...e.entityIds].some(s=>(this.#b.get(`${e.producer}:${s}`)??0)>t)}#v(){if(this.#t)return;let e=this.#p;for(const i of this.#w())i.held&&!i.held.due&&(e=Math.min(e,i.held.notBefore));if(e===1/0){this.#S();return}if(this.#y?.dueAt===e)return;this.#S();const t=Math.max(0,e-this.#e.now()),s=this.#e.schedule.schedule(t,()=>{this.#y=null,this.#W(e),this.#n()});this.#y={dueAt:e,cancel:o(()=>s.cancel(),"cancel")}}#W(e){let t=!1;for(const s of this.#w())s.held&&!s.held.due&&s.held.notBefore<=e&&(s.held.due=!0,this.#i(s),t=!0);return this.#p<=e&&(this.#p=1/0,this.#r=!0,t=!0),t}#S(){this.#y?.cancel(),this.#y=null}}exports.EntityApplyRail=k;const w=o(a=>a.kind==="line"?a.authorityId:a.unit,"owner"),m=o(a=>a.kind==="line"?`entity Apply line ${a.authorityId}`:`entity Apply fetch of ${a.unit}`,"label");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var i=Object.defineProperty;var t=(o,e)=>i(o,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.localWorkWins=void 0,exports.ownSettles=f,exports.showAhead=_;const u=require("../entities/cloud.js"),d=require("./bodmas.js"),c=require("./resolution.js"),h=t((o,e)=>e!==null&&(e.whole||e.status!=="active"||e.type!==o),"localWorkWins");exports.localWorkWins=h;function f(o,e){if((0,c.clashResolution)(o.change.result.type)==="replace")return e.admittedVersion()===e.playedVersion();const n=o.change.joins!==void 0?o.change.result.version:o.change.basedOnState;return e.lineBefore()===n}t(f,"ownSettles");const w=new Set(["move","rename","trash","restore","delete"]);function _(o,e,n){const l=(0,c.clashResolution)(o.type)==="replace";let r=o;for(const s of e)s.joins===void 0&&(r=(0,d.playChange)(r,{facets:l?s.facets:s.facets.filter(a=>w.has(a)),result:s.result},n).row);return(0,u.assertRecord)(r)}t(_,"showAhead");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var v=Object.defineProperty;var l=(r,
|
|
1
|
+
"use strict";var v=Object.defineProperty;var l=(r,c)=>v(r,"name",{value:c,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.packageProposalSteps=I,exports.packageProposal=A,exports.packageProposalsSteps=R,exports.packageProposals=N;const q=require("../entities/cloud.js"),P=require("../entities/package-pages.js"),z=require("../entities/types.js"),u=require("../steps.js"),w=require("./record-proposal.js");function*I(r,c,o){const a=yield*(0,P.packagePagesSteps)(r,c,o);let S;for(const p of a)S??=p.items.find(y=>y.uuid===r.toLowerCase());const f=(0,q.assertRecord)(S),i={kind:"package",pages:Object.freeze(a.map(p=>p.ref))},d=Object.freeze({contract:w.CHANGE_PROPOSAL_CONTRACT,origin:"detected",entityId:f.uuid,basedOnState:null,result:f,facets:Object.freeze(["create"]),replay:i,cargo:Object.freeze((0,w.changeCargo)(f,i))}),g=(0,w.ownChange)(d).replay;return g.kind==="repo.git"&&(0,w.assertRepositoryReplay)(g,f),{proposal:d,pages:a}}l(I,"packageProposalSteps");function A(r,c,o){return(0,u.finishSteps)(I(r,c,o))}l(A,"packageProposal");function*R(r,c,o=new Map){const a=new Map;yield*(0,u.eachStep)(r,e=>{e.basedOnState===null&&e.result.status==="active"&&a.set(e.entityId,e)});const S=(0,P.carriers)([...a.values()].map(e=>e.result)),f=l(e=>{const{parentUUID:t}=a.get(e).result;return t===null?S.get(e)?.uuid??null:z.CONTAINERS.has(a.get(t)?.result.type??"")?t:null},"holderOf"),i=new Map;yield*(0,u.eachStep)([...a.keys()],e=>{const t=[];let n=null;for(let s=e;;){const k=i.get(s);if(k!==void 0){n=k;break}t.push(s);const b=f(s);if(b!==null&&!t.includes(b)){s=b;continue}n=b===null&&(0,P.anchorsPackage)(a.get(s).result.type)?s:null;break}for(const s of t)i.set(s,n)});const d=new Map;yield*(0,u.eachStep)([...i],([e,t])=>{if(t===null)return;const n=d.get(t)??[];n.push(a.get(e).result),d.set(t,n)});const g=new Map;for(const[e,t]of d)for(const n of t)g.set(n.uuid,e);const p=l(e=>e.basedOnState!==null&&e.replay.kind!=="package"&&e.turn===void 0,"joinable"),y=new Map;yield*(0,u.eachStep)(r,e=>{const{type:t,payloadVersion:n}=e.result,s=t==="reference"&&n!==null?n.toLowerCase():null;p(e)&&s!==null&&g.get(s)===s&&y.set(e.entityId,s)});const h=new Map(y),M=new Set;yield*(0,u.eachStep)(r,e=>{const{parentUUID:t}=e.result;if(!p(e)||h.has(e.entityId))return;const n=t===null?void 0:g.get(t)??y.get(t);if(n!==void 0){h.set(e.entityId,n);return}const s=o.get(e.entityId);if(s!==void 0&&M.has(s)){h.set(e.entityId,s);return}M.add(e.entityId)});const m=new Set(h.values()),O=new Map;for(const[e,t]of d)(t.length>1||m.has(e))&&O.set(e,yield*I(e,t,c));const _=new Map,j=[],C=new Set;return yield*(0,u.eachStep)(r,e=>{const t=h.get(e.entityId);if(t!==void 0){const k=_.get(t)??[];k.push(e),_.set(t,k);return}const n=i.get(e.entityId),s=n==null?void 0:O.get(n);if(!s){j.push(e);return}C.has(n)||(C.add(n),j.push(s.proposal))}),{proposals:j,together:_,pages:[...O.values()].flatMap(e=>e.pages)}}l(R,"packageProposalsSteps");function N(r,c,o){return(0,u.finishSteps)(R(r,c,o))}l(N,"packageProposals");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var R=Object.defineProperty;var i=(e,n)=>R(e,"name",{value:n,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.ENTITY_RECORD_CONTRACT=void 0,exports.channelRecords=I,exports.recordCargo=E,exports.checkLocalEntityRecord=p,exports.checkAcceptedEntityRecord=j,exports.createLocalEntityRecord=q;const b=require("../entities/cloud.js"),w=require("../entities/graph-pages.js"),k=require("../entities/package-pages.js"),y=require("../entities/types.js"),c=require("../detection/record-proposal.js"),t=require("../contracts/validate.js");exports.ENTITY_RECORD_CONTRACT="amalgm.entity-record@1";function I(e){if(e.together===void 0)return[e];const{together:n,...s}=e;return Object.freeze([Object.freeze(s),...n.map(r=>Object.freeze({contract:e.contract,mutationId:e.mutationId,authorityId:e.authorityId,entityId:r.entityId,basedOnState:r.basedOnState,resultState:r.resultState,lastSeenGlobalSequence:r.lastSeenGlobalSequence,change:r.change,createdAt:e.createdAt}))])}i(I,"channelRecords");function E(e){if(e.together===void 0)return e.change.cargo;const n=new Map;for(const{change:s}of I(e))for(const r of s.cargo)n.has(r.contentHash)||n.set(r.contentHash,r);return Object.freeze([...n.values()])}i(E,"recordCargo");const d=/^[0-9a-f]{64}$/,A=new Set(["create","rename","move","retype","content","mode","time","trash","restore","delete"]),T=new Set(["detected","intentional"]),a='{ contract: "amalgm.entity-record@1", mutationId: "m1", authorityId: "pvr_...", entityId: "UUID", basedOnState: null, resultState: "64-hex", lastSeenGlobalSequence: 0, change: {...}, createdAt: 1 }',g=i((e,n=!1)=>typeof e=="number"&&Number.isSafeInteger(e)&&e>=(n?1:0),"isSequence");function m(e){if(!(0,t.isRecord)(e)||e.contract!==c.CHANGE_PROPOSAL_CONTRACT)return(0,t.failShape)(a,`change.contract was not ${c.CHANGE_PROPOSAL_CONTRACT}`);const n=e.origin;if(typeof n!="string"||!T.has(n))return(0,t.failShape)(a,"change.origin was not detected or intentional");if(typeof e.entityId!="string"||!y.UUID_PATTERN.test(e.entityId))return(0,t.failShape)(a,"change.entityId was not an entity UUID");if(e.basedOnState!==null&&(typeof e.basedOnState!="string"||!d.test(e.basedOnState)))return(0,t.failShape)(a,"change.basedOnState was not null or lowercase SHA-256");const s=e.joins!==void 0;if(s&&(n!=="detected"||typeof e.joins!="string"||!d.test(e.joins)||e.basedOnState!==e.joins))return(0,t.failShape)(a,"change.joins was not a detected echo naming the SHA-256 of the row it states over");const r=e.turn!==void 0;if(r&&(s||n!=="detected"||!(0,t.isRecord)(e.turn)||!(e.turn.act==="ask"?(0,t.isNonEmptyString)(e.turn.event):(e.turn.act==="check"||e.turn.act==="running"||e.turn.act==="end")&&(0,t.isNonEmptyString)(e.turn.ask))||e.replay===null||!(0,t.isRecord)(e.replay)||e.replay.kind!=="structure"||!(0,t.isRecord)(e.result)||e.result.type!=="repo.git"||e.basedOnState!==e.result.version))return(0,t.failShape)(a,"change.turn was not a detected ask, check, running or end restating the repository row it was said on");if(!Array.isArray(e.facets)||e.facets.length===0!==(s||r)||e.facets.some(o=>typeof o!="string"||!A.has(o))||new Set(e.facets).size!==e.facets.length)return(0,t.failShape)(a,"change.facets was invalid, duplicated, or unknown");if(!(0,t.isRecord)(e.replay)||!(0,t.isNonEmptyString)(e.replay.kind))return(0,t.failShape)(a,"change.replay was not a replay instruction");if(!Array.isArray(e.cargo))return(0,t.failShape)(a,"change.cargo was not an array");try{const o=(0,b.assertRecord)(e.result);if(o.uuid!==e.entityId)return(0,t.failShape)(a,"change.entityId did not equal change.result.uuid");const l=y.CONSTRUCT_TYPES.has(o.type);if(!(n==="detected"?!l&&o.status!=="deleted":l||(0,c.isPermanentDeletionShape)({origin:"intentional",result:o,facets:e.facets,replay:e.replay,cargo:e.cargo})))return(0,t.failShape)(a,"change.origin did not match its result: constructs are intentional, files are detected and never deleted, and a file is intentional only as a permanent deletion (delete alone, structure replay, no cargo, inside a root)");if(e.replay.kind==="package"&&_(e,o),e.replay.kind==="repo.git"){if(o.type!=="repo.git")throw new Error("repository replay requires a repository result");(0,c.assertRepositoryReplay)(e.replay,o)}const S=(0,c.changeCargo)(o,e.replay),u=e.cargo;if(u.length!==S.length||S.some((f,O)=>{const h=u[O];return!(0,t.isRecord)(h)||h.entityId!==f.entityId||h.entityType!==f.entityType||h.kind!==f.kind||h.contentHash!==f.contentHash}))return(0,t.failShape)(a,"change.cargo does not exactly name its complete result artifact")}catch(o){return(0,t.failShape)(a,`change.result was invalid: ${o instanceof Error?o.message:String(o)}`)}return(0,t.pass)(e)}i(m,"checkChange");function _(e,n){const s=e.replay;if(Object.keys(s).length!==2||!Array.isArray(s.pages)||s.pages.length===0)throw new Error("package replay must name its pages");for(const o of s.pages)if((0,w.assertPageRef)(o),Object.keys(o).length!==4)throw new Error("a package page names only its sha256, bytes, records and first key");if(e.origin!=="detected"||e.basedOnState!==null||e.facets.length!==1||e.facets[0]!=="create"||n.status!=="active"||!(0,k.anchorsPackage)(n.type))throw new Error("a package must create one active container or reference anchor");const r=(0,c.ownChange)({result:n,replay:s,cargo:[]}).replay;r.kind==="repo.git"&&(0,c.assertRepositoryReplay)(r,n)}i(_,"assertPackageChange");function p(e){if(!(0,t.isRecord)(e)||e.contract!==exports.ENTITY_RECORD_CONTRACT)return(0,t.failShape)(a,`contract was not ${exports.ENTITY_RECORD_CONTRACT}`);for(const s of["mutationId","authorityId"])if(!(0,t.isNonEmptyString)(e[s]))return(0,t.failShape)(a,`${s} was not a non-empty string`);if(typeof e.entityId!="string"||!y.UUID_PATTERN.test(e.entityId))return(0,t.failShape)(a,"entityId was not an entity UUID");if(e.basedOnState!==null&&(typeof e.basedOnState!="string"||!d.test(e.basedOnState)))return(0,t.failShape)(a,"basedOnState was not null or lowercase SHA-256");if(typeof e.resultState!="string"||!d.test(e.resultState))return(0,t.failShape)(a,"resultState was not lowercase SHA-256");if(!g(e.lastSeenGlobalSequence)||!g(e.createdAt))return(0,t.failShape)(a,"lastSeenGlobalSequence and createdAt must be non-negative safe integers");const n=m(e.change);if(!n.ok)return n;if(n.value.entityId!==e.entityId||n.value.basedOnState!==e.basedOnState||n.value.result.version!==e.resultState)return(0,t.failShape)(a,"record lineage did not exactly match its Detect proposal");if(e.together!==void 0){const s=N(e.entityId,e.together);if(!s.ok)return s}return(0,t.pass)(e)}i(p,"checkLocalEntityRecord");const C=["basedOnState","change","entityId","lastSeenGlobalSequence","resultState"];function N(e,n){if(!Array.isArray(n)||n.length===0)return(0,t.failShape)(a,"together was not a non-empty array");const s=new Set([e]);for(const r of n){if(!(0,t.isRecord)(r)||Object.keys(r).sort().join()!==C.join())return(0,t.failShape)(a,"a change made together names only its entity, lineage and change");if(typeof r.entityId!="string"||!y.UUID_PATTERN.test(r.entityId)||s.has(r.entityId))return(0,t.failShape)(a,"a change made together names another entity, each once");if(s.add(r.entityId),typeof r.basedOnState!="string"||!d.test(r.basedOnState)||typeof r.resultState!="string"||!d.test(r.resultState)||!g(r.lastSeenGlobalSequence))return(0,t.failShape)(a,"a change made together changes an entity that already has a channel");const o=m(r.change);if(!o.ok)return o;if(o.value.entityId!==r.entityId||o.value.basedOnState!==r.basedOnState||o.value.result.version!==r.resultState)return(0,t.failShape)(a,"a change made together did not exactly match its Detect proposal");if(o.value.replay.kind==="package"||o.value.
|
|
1
|
+
"use strict";var R=Object.defineProperty;var i=(e,n)=>R(e,"name",{value:n,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.ENTITY_RECORD_CONTRACT=void 0,exports.channelRecords=I,exports.recordCargo=E,exports.checkLocalEntityRecord=p,exports.checkAcceptedEntityRecord=j,exports.createLocalEntityRecord=q;const b=require("../entities/cloud.js"),w=require("../entities/graph-pages.js"),k=require("../entities/package-pages.js"),y=require("../entities/types.js"),c=require("../detection/record-proposal.js"),t=require("../contracts/validate.js");exports.ENTITY_RECORD_CONTRACT="amalgm.entity-record@1";function I(e){if(e.together===void 0)return[e];const{together:n,...s}=e;return Object.freeze([Object.freeze(s),...n.map(r=>Object.freeze({contract:e.contract,mutationId:e.mutationId,authorityId:e.authorityId,entityId:r.entityId,basedOnState:r.basedOnState,resultState:r.resultState,lastSeenGlobalSequence:r.lastSeenGlobalSequence,change:r.change,createdAt:e.createdAt}))])}i(I,"channelRecords");function E(e){if(e.together===void 0)return e.change.cargo;const n=new Map;for(const{change:s}of I(e))for(const r of s.cargo)n.has(r.contentHash)||n.set(r.contentHash,r);return Object.freeze([...n.values()])}i(E,"recordCargo");const d=/^[0-9a-f]{64}$/,A=new Set(["create","rename","move","retype","content","mode","time","trash","restore","delete"]),T=new Set(["detected","intentional"]),a='{ contract: "amalgm.entity-record@1", mutationId: "m1", authorityId: "pvr_...", entityId: "UUID", basedOnState: null, resultState: "64-hex", lastSeenGlobalSequence: 0, change: {...}, createdAt: 1 }',g=i((e,n=!1)=>typeof e=="number"&&Number.isSafeInteger(e)&&e>=(n?1:0),"isSequence");function m(e){if(!(0,t.isRecord)(e)||e.contract!==c.CHANGE_PROPOSAL_CONTRACT)return(0,t.failShape)(a,`change.contract was not ${c.CHANGE_PROPOSAL_CONTRACT}`);const n=e.origin;if(typeof n!="string"||!T.has(n))return(0,t.failShape)(a,"change.origin was not detected or intentional");if(typeof e.entityId!="string"||!y.UUID_PATTERN.test(e.entityId))return(0,t.failShape)(a,"change.entityId was not an entity UUID");if(e.basedOnState!==null&&(typeof e.basedOnState!="string"||!d.test(e.basedOnState)))return(0,t.failShape)(a,"change.basedOnState was not null or lowercase SHA-256");const s=e.joins!==void 0;if(s&&(n!=="detected"||typeof e.joins!="string"||!d.test(e.joins)||e.basedOnState!==e.joins))return(0,t.failShape)(a,"change.joins was not a detected echo naming the SHA-256 of the row it states over");const r=e.turn!==void 0;if(r&&(s||n!=="detected"||!(0,t.isRecord)(e.turn)||!(e.turn.act==="ask"?(0,t.isNonEmptyString)(e.turn.event):(e.turn.act==="check"||e.turn.act==="running"||e.turn.act==="end")&&(0,t.isNonEmptyString)(e.turn.ask))||e.replay===null||!(0,t.isRecord)(e.replay)||e.replay.kind!=="structure"||!(0,t.isRecord)(e.result)||e.result.type!=="repo.git"||e.basedOnState!==e.result.version))return(0,t.failShape)(a,"change.turn was not a detected ask, check, running or end restating the repository row it was said on");if(!Array.isArray(e.facets)||e.facets.length===0!==(s||r)||e.facets.some(o=>typeof o!="string"||!A.has(o))||new Set(e.facets).size!==e.facets.length)return(0,t.failShape)(a,"change.facets was invalid, duplicated, or unknown");if(!(0,t.isRecord)(e.replay)||!(0,t.isNonEmptyString)(e.replay.kind))return(0,t.failShape)(a,"change.replay was not a replay instruction");if(!Array.isArray(e.cargo))return(0,t.failShape)(a,"change.cargo was not an array");try{const o=(0,b.assertRecord)(e.result);if(o.uuid!==e.entityId)return(0,t.failShape)(a,"change.entityId did not equal change.result.uuid");const l=y.CONSTRUCT_TYPES.has(o.type);if(!(n==="detected"?!l&&o.status!=="deleted":l||(0,c.isPermanentDeletionShape)({origin:"intentional",result:o,facets:e.facets,replay:e.replay,cargo:e.cargo})))return(0,t.failShape)(a,"change.origin did not match its result: constructs are intentional, files are detected and never deleted, and a file is intentional only as a permanent deletion (delete alone, structure replay, no cargo, inside a root)");if(e.replay.kind==="package"&&_(e,o),e.replay.kind==="repo.git"){if(o.type!=="repo.git")throw new Error("repository replay requires a repository result");(0,c.assertRepositoryReplay)(e.replay,o)}const S=(0,c.changeCargo)(o,e.replay),u=e.cargo;if(u.length!==S.length||S.some((f,O)=>{const h=u[O];return!(0,t.isRecord)(h)||h.entityId!==f.entityId||h.entityType!==f.entityType||h.kind!==f.kind||h.contentHash!==f.contentHash}))return(0,t.failShape)(a,"change.cargo does not exactly name its complete result artifact")}catch(o){return(0,t.failShape)(a,`change.result was invalid: ${o instanceof Error?o.message:String(o)}`)}return(0,t.pass)(e)}i(m,"checkChange");function _(e,n){const s=e.replay;if(Object.keys(s).length!==2||!Array.isArray(s.pages)||s.pages.length===0)throw new Error("package replay must name its pages");for(const o of s.pages)if((0,w.assertPageRef)(o),Object.keys(o).length!==4)throw new Error("a package page names only its sha256, bytes, records and first key");if(e.origin!=="detected"||e.basedOnState!==null||e.facets.length!==1||e.facets[0]!=="create"||n.status!=="active"||!(0,k.anchorsPackage)(n.type))throw new Error("a package must create one active container or reference anchor");const r=(0,c.ownChange)({result:n,replay:s,cargo:[]}).replay;r.kind==="repo.git"&&(0,c.assertRepositoryReplay)(r,n)}i(_,"assertPackageChange");function p(e){if(!(0,t.isRecord)(e)||e.contract!==exports.ENTITY_RECORD_CONTRACT)return(0,t.failShape)(a,`contract was not ${exports.ENTITY_RECORD_CONTRACT}`);for(const s of["mutationId","authorityId"])if(!(0,t.isNonEmptyString)(e[s]))return(0,t.failShape)(a,`${s} was not a non-empty string`);if(typeof e.entityId!="string"||!y.UUID_PATTERN.test(e.entityId))return(0,t.failShape)(a,"entityId was not an entity UUID");if(e.basedOnState!==null&&(typeof e.basedOnState!="string"||!d.test(e.basedOnState)))return(0,t.failShape)(a,"basedOnState was not null or lowercase SHA-256");if(typeof e.resultState!="string"||!d.test(e.resultState))return(0,t.failShape)(a,"resultState was not lowercase SHA-256");if(!g(e.lastSeenGlobalSequence)||!g(e.createdAt))return(0,t.failShape)(a,"lastSeenGlobalSequence and createdAt must be non-negative safe integers");const n=m(e.change);if(!n.ok)return n;if(n.value.entityId!==e.entityId||n.value.basedOnState!==e.basedOnState||n.value.result.version!==e.resultState)return(0,t.failShape)(a,"record lineage did not exactly match its Detect proposal");if(e.together!==void 0){const s=N(e.entityId,e.together);if(!s.ok)return s}return(0,t.pass)(e)}i(p,"checkLocalEntityRecord");const C=["basedOnState","change","entityId","lastSeenGlobalSequence","resultState"];function N(e,n){if(!Array.isArray(n)||n.length===0)return(0,t.failShape)(a,"together was not a non-empty array");const s=new Set([e]);for(const r of n){if(!(0,t.isRecord)(r)||Object.keys(r).sort().join()!==C.join())return(0,t.failShape)(a,"a change made together names only its entity, lineage and change");if(typeof r.entityId!="string"||!y.UUID_PATTERN.test(r.entityId)||s.has(r.entityId))return(0,t.failShape)(a,"a change made together names another entity, each once");if(s.add(r.entityId),typeof r.basedOnState!="string"||!d.test(r.basedOnState)||typeof r.resultState!="string"||!d.test(r.resultState)||!g(r.lastSeenGlobalSequence))return(0,t.failShape)(a,"a change made together changes an entity that already has a channel");const o=m(r.change);if(!o.ok)return o;if(o.value.entityId!==r.entityId||o.value.basedOnState!==r.basedOnState||o.value.result.version!==r.resultState)return(0,t.failShape)(a,"a change made together did not exactly match its Detect proposal");if(o.value.replay.kind==="package"||o.value.turn!==void 0)return(0,t.failShape)(a,"a change made together is never a package or a turn")}return(0,t.pass)(n)}i(N,"checkTogether");function j(e){const n=p(e);return n.ok?n.value.together!==void 0?(0,t.failShape)(a,"an accepted record states one channel: its changes made together are accepted on their own"):!(0,t.isRecord)(e)||!g(e.globalSequence,!0)||!(0,t.isNonEmptyString)(e.actorId)||!(0,t.isNonEmptyString)(e.originDeviceId)||!g(e.acceptedAt)?(0,t.failShape)(a,"accepted record lacked a positive sequence or server-derived acceptance fields"):(0,t.pass)(e):n}i(j,"checkAcceptedEntityRecord");function q(e){const n=Object.freeze({contract:exports.ENTITY_RECORD_CONTRACT,mutationId:e.mutationId,authorityId:e.authorityId,entityId:e.change.entityId,basedOnState:e.change.basedOnState,resultState:e.change.result.version,lastSeenGlobalSequence:e.lastSeenGlobalSequence,change:e.change,createdAt:e.createdAt,...e.together===void 0||e.together.length===0?{}:{together:Object.freeze([...e.together])}}),s=p(n);if(!s.ok)throw new Error(s.error);return n}i(q,"createLocalEntityRecord");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amalgm/live",
|
|
3
|
-
"version": "0.2.81-preview.
|
|
3
|
+
"version": "0.2.81-preview.47491680470",
|
|
4
4
|
"description": "Portable realtime SDK for Amalgm — wire contracts, mutation discipline, and delivery state machines behind host-injected adapters.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist-cjs/index.js",
|
|
@@ -133,11 +133,11 @@
|
|
|
133
133
|
"dependencies": {
|
|
134
134
|
"@amalgm/core": "0.5.1-preview.46345295340"
|
|
135
135
|
},
|
|
136
|
-
"gitHead": "
|
|
136
|
+
"gitHead": "62236c2005aaedc683d4a5bf8c0d1ce783d0926e",
|
|
137
137
|
"amalgmSource": {
|
|
138
138
|
"repository": "amalgm-inc/amalgm-live",
|
|
139
139
|
"branch": "preview",
|
|
140
|
-
"commit": "
|
|
141
|
-
"occurrence": "
|
|
140
|
+
"commit": "62236c2005aaedc683d4a5bf8c0d1ce783d0926e",
|
|
141
|
+
"occurrence": "47491680470"
|
|
142
142
|
}
|
|
143
143
|
}
|