@amalgm/live 0.2.81-preview.47413265404 → 0.2.81-preview.47428104047

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.
@@ -236,7 +236,7 @@ export type ApplyGuardStage = 'look' | 'final';
236
236
  type MaybePromise<T> = T | Promise<T>;
237
237
  export interface EntityApplyStorePort {
238
238
  readApplyAuthorities(): MaybePromise<readonly string[]>;
239
- readApplyWindow(authorityId: string, before: number | null): MaybePromise<EntityApplyLineStep>;
239
+ readApplyWindow(authorityId: string): MaybePromise<EntityApplyLineStep>;
240
240
  readFetchWindow(unit: string, through: number | null): MaybePromise<EntityApplyLaneStep>;
241
241
  readOpenUnits(): MaybePromise<readonly string[]>;
242
242
  localWorkAfter(authorityId: string, entityId: string, target: number | CloudEntityRecord): MaybePromise<LocalWork | null>;
@@ -22,7 +22,6 @@ export interface ApplyTrack {
22
22
  export interface ApplyLineState extends ApplyTrack {
23
23
  readonly kind: 'line';
24
24
  readonly authorityId: string;
25
- endsBefore: number | null;
26
25
  }
27
26
  export declare function createApplyLine(authorityId: string): ApplyLineState;
28
27
  export declare function releaseApplyLine(line: ApplyTrack, producer: ApplyDependency['producer'], entityIds: readonly string[] | undefined): boolean;
@@ -1 +1 @@
1
- var f=Object.defineProperty;var r=(e,n)=>f(e,"name",{value:n,configurable:!0});import{UUID_PATTERN as l}from"../entities/types.js";function m(e){return{kind:"line",authorityId:e,endsBefore:null,held:null,queued:!1,running:!1,again:!1}}r(m,"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(t=>o.includes(t))?!1:(i.due=!0,!0)}r(k,"releaseApplyLine");function w(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(w,"heldWait");const a=new Set(["detect","apply","capability","send"]),c=r(e=>!!e&&a.has(e.producer)&&Array.isArray(e.entityIds)&&e.entityIds.length>0&&e.entityIds.every(n=>typeof n=="string"&&n.length>0),"namesRelease");function g(e){if(!c(e))throw new Error("Apply dependency must name the entities that can release it")}r(g,"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}`),d(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 d(e,n){(!Array.isArray(e.members)||e.members.length===0)&&u(n);const o=r((t,s)=>Number.isSafeInteger(t)&&t>=(s?0:1),"at");for(const t of e.members)(!l.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")&&(l.test(t.unseen??"")||u(n));const{deliverySequence:i}=e;(i===null?!e.intent:!o(i,e.members[0].work.kept!==null))&&u(n)}r(d,"assertApplyWindow");export{g as assertApplyDependency,b as assertApplyLineStep,d as assertApplyWindow,m as createApplyLine,w as heldWait,u as invalidStep,k as releaseApplyLine};
1
+ var f=Object.defineProperty;var r=(e,n)=>f(e,"name",{value:n,configurable:!0});import{UUID_PATTERN as l}from"../entities/types.js";function m(e){return{kind:"line",authorityId:e,held:null,queued:!1,running:!1,again:!1}}r(m,"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(t=>o.includes(t))?!1:(i.due=!0,!0)}r(k,"releaseApplyLine");function w(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(w,"heldWait");const a=new Set(["detect","apply","capability","send"]),c=r(e=>!!e&&a.has(e.producer)&&Array.isArray(e.entityIds)&&e.entityIds.length>0&&e.entityIds.every(n=>typeof n=="string"&&n.length>0),"namesRelease");function g(e){if(!c(e))throw new Error("Apply dependency must name the entities that can release it")}r(g,"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}`),d(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 d(e,n){(!Array.isArray(e.members)||e.members.length===0)&&u(n);const o=r((t,s)=>Number.isSafeInteger(t)&&t>=(s?0:1),"at");for(const t of e.members)(!l.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")&&(l.test(t.unseen??"")||u(n));const{deliverySequence:i}=e;(i===null?!e.intent:!o(i,e.members[0].work.kept!==null))&&u(n)}r(d,"assertApplyWindow");export{g as assertApplyDependency,b as assertApplyLineStep,d as assertApplyWindow,m as createApplyLine,w as heldWait,u as invalidStep,k as releaseApplyLine};
@@ -2,7 +2,6 @@ import type { ApplyFetchAnswer, ApplyWait, EntityApplyRailOptions } from './cont
2
2
  export declare const ENTITY_APPLY_DEFAULT_CONCURRENCY = 8;
3
3
  export declare const ENTITY_APPLY_RETRY_BASE_MS = 1000;
4
4
  export declare const ENTITY_APPLY_RETRY_MAX_MS = 30000;
5
- export declare const ENTITY_APPLY_SAME_ANSWERS = 3;
6
5
  export declare class EntityApplyRail {
7
6
  #private;
8
7
  constructor(options: EntityApplyRailOptions);
@@ -1 +1 @@
1
- var E=Object.defineProperty;var d=(o,e)=>E(o,"name",{value:e,configurable:!0});import{UUID_PATTERN as u}from"../entities/types.js";import{FetchAhead as b}from"./fetch-ahead.js";import{EntityApplyJourney as P,outcomeOf as g,stepsAside as v}from"./journey.js";import{assertApplyLaneStep as O,createApplyLane as S}from"./lane.js";import{assertApplyDependency as $,assertApplyLineStep as q,createApplyLine as M,heldWait as c,releaseApplyLine as _}from"./line.js";import{windowEndForWait as m}from"./window.js";const x=8,B=1e3,A=3e4,p=3;class C{static{d(this,"EntityApplyRail")}#e;#g;#P;#_;#x;#o;#v;#d=!1;#t=!1;#a=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(e){this.#e=e,this.#g=e.concurrency??x,this.#P=e.laneConcurrency??this.#g,this.#_=e.onBackgroundError??(()=>{}),this.#x=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.#o=new P(e,()=>this.#A,()=>this.#t),this.#v=new b(e.cargo,{sha256Hex:e.sha256Hex,loops:e.loops,deliveryPosition:e.deliveryPosition})}async start(){if(this.#d)throw new Error("entity Apply rail already started");if(this.#t)throw new Error("entity Apply rail is stopped");this.#d=!0;const e=await this.#e.store.readOpenUnits();if(!Array.isArray(e)||new Set(e).size!==e.length||e.some(t=>!u.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.#B()}fetch(e,t){return this.#d?this.#t?Promise.reject(new Error("entity Apply rail is stopped")):u.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=d(()=>this.#e.schedule.schedule(t.stall,()=>{r.waiters.delete(l)&&(r.emptied&&![...r.waiters].some(a=>a.emptied)&&(r.emptied=!1),s(this.#K(r)))}),"arm");let h=n();const l={emptied:t.emptied===!0,answer:d(a=>{h.cancel(),s(a)},"answer"),fail:d(a=>{h.cancel(),i(a)},"fail"),moved:d(()=>{h.cancel(),h=n()},"moved")};r.waiters.add(l),r.waitsOn===null&&this.#T(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(!u.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.#b(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=>!u.test(t)))throw new Error("entity Apply owe needs entity UUIDs");if(!(!this.#d||this.#t)){for(const t of e)this.#S(t);this.#r()}}#I(){!this.#d||this.#t||(this.#n=!0,this.#r())}waiting(){return this.#w().flatMap(e=>c(e.held,y(e))??[])}async flush(){if(!this.#d)throw new Error("entity Apply rail has not started");this.#t||await this.#B()}async stop(){if(this.#t)return;this.#t=!0,this.#M();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.#R()>0||this.#h;)await Promise.allSettled([...this.#l.steps,...this.#u.steps,...this.#h?[this.#h]:[]]);await this.#v.stop();for(const t of this.#m.splice(0))t.resolve()}#B(){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.#J();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.#F(s,e)}if(this.#R()===0&&this.#h===null&&!this.#n&&this.#l.queue.length===0&&this.#u.queue.length===0){if(this.#O.clear(),this.#D(this.#e.now())){this.#r();return}this.#q();for(const e of this.#m.splice(0))e.resolve();this.#x()}}}#R(){return this.#l.steps.size+this.#u.steps.size}#w(){return[...this.#a.values(),...this.#s.values()]}#k(e){return e.kind==="line"?this.#a.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))}}#F(e,t){e.running=!0,e.again=!1;const s=(e.kind==="line"?this.#H(e):this.#V(e)).then(i=>{e.running=!1,t.steps.delete(s),(i||e.again)&&this.#k(e)&&(e.again=!1,this.#i(e)),this.#r()});t.steps.add(s)}async#J(){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.#a)!t.has(s)&&!i.running&&!i.queued&&this.#a.delete(s);for(const s of e){let i=this.#a.get(s);i||(i=M(s),this.#a.set(s,i)),this.#i(i)}}catch(e){this.#p=this.#e.now()+A;const t=this.#m.splice(0);t.length===0&&this.#$(e);for(const s of t)s.reject(e)}}async#H(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,e.endsBefore);if(q(e.authorityId,i),this.#t)return!1;if(i.kind==="end"){e.held=null;const r=e.endsBefore!==null;return e.endsBefore=null,r}return i.window.deliverySequence!==null&&this.#v.advance(e.authorityId,i.window.deliverySequence),await this.#U(e,i.window,s)}catch(t){return this.#C(e,t),!1}}async#V(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(O(e.unit,i),this.#t||!this.#k(e))return!1;if(e.waitsOn=null,i.kind==="unseen")return this.#N(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.#X(e,i.through,i.reason);case"prerequisite":return this.#G(e,i.unit,i.reason,s);case"window":return await this.#U(e,i.window,s)}}catch(t){return this.#C(e,t),!1}}async#X(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.#k(e)?!1:(e.through===t&&e.met.add(e.unit),this.#b(e.unit),!0))}#S(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=S(e,t,s),this.#s.set(e,i),this.#i(i)),i}#G(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.#N(e,{producer:"apply",entityIds:[t]},s,i):!1}#N(e,t,s,i){return e.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#z(t,i),reason:s,dependency:t},e.held.due}#T(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.#k(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.#b(e.unit),this.#c("apply",[e.unit]))}#b(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()}}#K(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#U(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.#Z(e,t))return!1;let r=await g(()=>this.#o.run(t,()=>e.kind==="lane"&&e.emptied?e.unit:null));if(e.kind==="line"&&typeof r=="object"&&r.kind==="park"&&m(t,r.dependency)===null&&this.#Q(e,t,r)&&(r=await g(()=>this.#o.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.#S(n.work.entityId);for(const n of t.members)n.unseen!==void 0&&this.#S(n.unseen);return this.#ee(e,r==="progress"?t.members.map(n=>n.work.entityId):[r.entityId])}if(r==="again")return e.held?.entityId===i&&(e.held=null),!0;if(r.advanced?.length&&this.#j(e,r.advanced),e.kind==="line"){const n=m(t,r.dependency);if(n!==null)return e.endsBefore=n,!0}return this.#te(e,t,r,s),e.kind==="lane"&&this.#T(e),e.held.due}#Q(e,t,s){const i=t.members[0].work.entityId;if(s.cause!==void 0||!v(t,i))return!1;const r=s.dependency?.entityIds??[i];return r.length===0||r.some(n=>n!==i)?!1:s.dependency?.producer==="capability"?!0:this.#Y(e,t)>=p}#Y(e,t){const s=e.held,i=t.members[0].work.entityId;return(s?.entityId===i&&s.target===this.#o.targetOf(t)?s.attempt:0)+1}async#Z(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.#o.targetOf(t)!==s.target||t.cutoff!==s.cutoff?!1:await this.#e.store.localWorkAfter(t.authorityId,i.work.entityId,s.kept??s.target)===null}#ee(e,t){return e.held&&(e.held.entityId===null||t.includes(e.held.entityId))&&(e.held=null),this.#j(e,t),!0}#j(e,t){e.kind==="lane"&&(this.#n=!0,this.#b(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))}#te(e,t,s,i){if(s.dependency&&$(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.#Y(e,t),l=this.#W(h),a=s.again===!0&&h<p,w={entityId:r,target:this.#o.targetOf(t),kept:t.members[0].work.kept,cutoff:t.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#e.now()+(a?0:l),releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#z(s.dependency,s.comparedAt??i):a,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};e.held=w,this.#q();const k=c(n,y(e)),f=c(w,y(e));f&&JSON.stringify(f)!==JSON.stringify(k)&&this.#e.onWaiting?.(f),!(!s.report&&(s.dependency!==void 0||h<p))&&this.#$(new Error(`${I(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 ${l}ms (attempt ${h}): ${s.reason}`,s.cause===void 0?void 0:{cause:s.cause}))}#C(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.#W(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.#q(),this.#$(new Error(`${I(e)} faulted and will retry in ${r}ms (attempt ${i}): ${n}`,{cause:t}))}#$(e){try{this.#_(e)}catch{}}#W(e){const t=Math.min(A,B*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.#O.set(`${e}:${i}`,s);for(const i of this.#w())_(i,e,t)&&this.#i(i)}#z(e,t){return["*",...e.entityIds].some(s=>(this.#O.get(`${e.producer}:${s}`)??0)>t)}#q(){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.#M();return}if(this.#y?.dueAt===e)return;this.#M();const t=Math.max(0,e-this.#e.now()),s=this.#e.schedule.schedule(t,()=>{this.#y=null,this.#D(e),this.#r()});this.#y={dueAt:e,cancel:d(()=>s.cancel(),"cancel")}}#D(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}#M(){this.#y?.cancel(),this.#y=null}}const y=d(o=>o.kind==="line"?o.authorityId:o.unit,"owner"),I=d(o=>o.kind==="line"?`entity Apply line ${o.authorityId}`:`entity Apply fetch of ${o.unit}`,"label");export{x as ENTITY_APPLY_DEFAULT_CONCURRENCY,B as ENTITY_APPLY_RETRY_BASE_MS,A as ENTITY_APPLY_RETRY_MAX_MS,p as ENTITY_APPLY_SAME_ANSWERS,C 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 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"&&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==="again"?(t.held?.entityId===i&&(t.held=null),!0):(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,6 +1,6 @@
1
1
  import type { CloudEntityRecord } from '../entities/cloud.js';
2
2
  import type { AcceptedEntityRecord } from '../records/contracts.js';
3
- import type { ApplyAheadRow, ApplyDependency, ApplyWindow } from './contracts.js';
3
+ import type { ApplyAheadRow } from './contracts.js';
4
4
  import type { LineJudgment } from './settling.js';
5
5
  import type { ApplyPlay, ShownPlace } from './showing.js';
6
6
  export declare const WINDOW_MEMBERS = 4096;
@@ -22,7 +22,6 @@ export interface JudgedWindowMember extends WindowJudgment {
22
22
  readonly member: FoldedMember;
23
23
  }
24
24
  export declare function readWindow(rows: readonly ApplyAheadRow[], judge: (member: FoldedMember) => WindowJudgment, place: (uuid: string) => ShownPlace | undefined, cap?: number): readonly JudgedWindowMember[];
25
- export declare function windowEndForWait(window: ApplyWindow, dependency: ApplyDependency | undefined): number | null;
26
25
  export interface OrderedMember {
27
26
  readonly entityId: string;
28
27
  readonly play: ApplyPlay;
@@ -1 +1 @@
1
- var M=Object.defineProperty;var a=(r,s)=>M(r,"name",{value:s,configurable:!0});import{collisionBases as k}from"../entities/rules.js";import{CONTAINERS as z,isConstructType as $}from"../entities/types.js";const D=4096;function E(r,s=D){const d=new Map;let p=0;for(const u of r){const{entityId:f}=u.record;let l=d.get(f);if(l===void 0){if(d.size===s)break;d.set(f,l={records:[],deliverySequence:u.deliverySequence,through:0})}const t=l.records.at(-1);if(t!==void 0&&t.change.turn===void 0!=(u.record.change.turn===void 0))break;if(t!==void 0&&u.record.globalSequence!==t.globalSequence+1)throw new Error(`entity ${f} has a gap before sequence ${u.record.globalSequence} inside its window`);l.records.push(u.record),l.through=p=u.deliverySequence}return Object.freeze({members:Object.freeze([...d].map(([u,{records:f,deliverySequence:l,through:t}])=>Object.freeze({entityId:u,records:Object.freeze(f),deliverySequence:l,through:t}))),through:p})}a(E,"foldWindow");function B(r,s,d,p=D){const u=new Map;for(let f=r;;){const l=E(f,p).members.map(o=>{const y=`${o.entityId}:${o.records.at(-1).globalSequence}`;let g=u.get(y);return g===void 0&&u.set(y,g=s(o)),{member:o,judgment:g.judgment,result:g.result}}),t=C(l,d);if(t===null)return l;f=f.slice(0,f.findIndex(o=>o.deliverySequence>=t))}}a(B,"readWindow");const W=a((r,s)=>r!==void 0&&(r.root?r.type!==s.type:r.parentUUID!==s.parentUUID||r.name!==s.name||r.type!==s.type),"movesPlace");function C(r,s){const d=new Set,p=new Set;let u=null;for(const{member:n,judgment:e,result:i}of r)e==="leave"?p.add(n.entityId):(e==="enter"||e==="apply"&&W(s(n.entityId),i))&&d.add(n.entityId),e==="enter"&&z.has(i.type)&&n.through<(u?.through??1/0)&&(u=n);if(d.size===0&&p.size===0)return null;const f=new Map(r.map(({member:n})=>[n.entityId,n.deliverySequence])),l=a((n,e)=>n===null?[]:[e,...k(e)].map(i=>`${n}/${i}`),"slotKeys"),t=a(({member:n,result:e})=>e.status==="active"&&!s(n.entityId)?.root?l(e.parentUUID,e.name):[],"goesTo"),o=a(n=>{const e=s(n.member.entityId);return[...e&&!e.root?l(e.parentUUID,e.name):[],...t(n)]},"slotsOf"),y=new Map,g=new Map,j=a((n,e,i)=>{for(const h of e){const c=n.get(h);c===void 0?n.set(h,[i]):c.length<2&&!c.includes(i)&&c.push(i)}},"note");for(const n of r){const{entityId:e}=n.member;d.has(e)&&j(g,t(n),e),(d.has(e)||p.has(e))&&j(y,o(n),e)}const v=a((n,e)=>{let i;for(const h of l(e.parentUUID,e.name)){const[c,w]=g.get(h)??[],I=c===n?w:c;I!==void 0&&(i===void 0||f.get(I)<f.get(i))&&(i=I)}return i},"pusher"),S=a((n,e,i)=>{const h=new Set([n]);for(let c=e;c!==null&&!h.has(c);){h.add(c);const w=s(c),I=i(c,w);if(I!==void 0)return I;c=w.parentUUID}return null},"climbs"),O=a(n=>{const{member:e,judgment:i,result:h}=n;if(i==="superseded"||i==="own"||i==="unseen"||i==="turn"||i==="waiting")return[];if($(h.type))return[];const c=[];for(const U of o(n)){const[m,x]=y.get(U)??[],q=m===e.entityId?x:m;q!==void 0&&c.push(q)}const w=s(e.entityId);if(w&&!w.root){const U=S(e.entityId,w.parentUUID,(m,x)=>d.has(m)?m:!x||x.root?null:v(e.entityId,x));U!==null&&c.push(U)}if(h.status!=="active"||w?.root)return c;const I=S(e.entityId,h.parentUUID,(U,m)=>d.has(U)||p.has(U)?U:m?!m.available||!z.has(m.type)||m.root?null:v(e.entityId,m):u!==null&&e.through>u.through?u.entityId:null);return I!==null&&c.push(I),c},"partnersOf");let b=null;for(const n of r){const e=n.member.deliverySequence;if(b!==null&&e>=b)break;for(const i of O(n)){const h=Math.max(e,f.get(i));(b===null||h<b)&&(b=h)}}return b}a(C,"windowCut");function P(r,s){return r.intent!==null||r.unit!==null||s?.producer!=="apply"?null:r.members.slice(1).find(p=>s.entityIds.includes(p.work.entityId))?.deliverySequence??null}a(P,"windowEndForWait");function A(r){const s=new Map(r.map((t,o)=>[t.entityId,{member:t,index:o}])),d=new Map,p=new Set,u=a((t,o)=>{const y=d.get(t);if(y!==void 0)return y;if(o.has(t)){for(const O of o)p.add(O);return-1}const g=s.get(t);if(!g||g.member.parentUUID===null)return 0;const j=s.get(g.member.parentUUID);if(!j)return d.set(t,0),0;o.add(t);const v=u(j.member.entityId,o);o.delete(t);const S=v<0?-1:v+1;return S>=0&&d.set(t,S),S},"depthOf"),f=[],l=[];for(const[t,o]of r.entries()){const y=u(o.entityId,new Set);if(y<0){p.add(o.entityId);continue}(o.play==="leave"?l:f).push({id:o.entityId,depth:y,index:t})}return f.sort((t,o)=>t.depth-o.depth||t.index-o.index),l.sort((t,o)=>o.depth-t.depth||t.index-o.index),Object.freeze({order:Object.freeze([...f,...l].map(({id:t})=>t)),cycle:Object.freeze([...p])})}a(A,"revealOrder");function F(r,s){let d=0;for(;d<r.length&&s(r[d]);)d+=1;return r.slice(0,d)}a(F,"resolvedPrefix");export{D as WINDOW_MEMBERS,E as foldWindow,B as readWindow,F as resolvedPrefix,A as revealOrder,P as windowEndForWait};
1
+ var M=Object.defineProperty;var a=(o,i)=>M(o,"name",{value:i,configurable:!0});import{collisionBases as $}from"../entities/rules.js";import{CONTAINERS as z,isConstructType as k}from"../entities/types.js";const D=4096;function E(o,i=D){const d=new Map;let p=0;for(const u of o){const{entityId:f}=u.record;let l=d.get(f);if(l===void 0){if(d.size===i)break;d.set(f,l={records:[],deliverySequence:u.deliverySequence,through:0})}const t=l.records.at(-1);if(t!==void 0&&t.change.turn===void 0!=(u.record.change.turn===void 0))break;if(t!==void 0&&u.record.globalSequence!==t.globalSequence+1)throw new Error(`entity ${f} has a gap before sequence ${u.record.globalSequence} inside its window`);l.records.push(u.record),l.through=p=u.deliverySequence}return Object.freeze({members:Object.freeze([...d].map(([u,{records:f,deliverySequence:l,through:t}])=>Object.freeze({entityId:u,records:Object.freeze(f),deliverySequence:l,through:t}))),through:p})}a(E,"foldWindow");function B(o,i,d,p=D){const u=new Map;for(let f=o;;){const l=E(f,p).members.map(r=>{const g=`${r.entityId}:${r.records.at(-1).globalSequence}`;let y=u.get(g);return y===void 0&&u.set(g,y=i(r)),{member:r,judgment:y.judgment,result:y.result}}),t=C(l,d);if(t===null)return l;f=f.slice(0,f.findIndex(r=>r.deliverySequence>=t))}}a(B,"readWindow");const W=a((o,i)=>o!==void 0&&(o.root?o.type!==i.type:o.parentUUID!==i.parentUUID||o.name!==i.name||o.type!==i.type),"movesPlace");function C(o,i){const d=new Set,p=new Set;let u=null;for(const{member:n,judgment:e,result:s}of o)e==="leave"?p.add(n.entityId):(e==="enter"||e==="apply"&&W(i(n.entityId),s))&&d.add(n.entityId),e==="enter"&&z.has(s.type)&&n.through<(u?.through??1/0)&&(u=n);if(d.size===0&&p.size===0)return null;const f=new Map(o.map(({member:n})=>[n.entityId,n.deliverySequence])),l=a((n,e)=>n===null?[]:[e,...$(e)].map(s=>`${n}/${s}`),"slotKeys"),t=a(({member:n,result:e})=>e.status==="active"&&!i(n.entityId)?.root?l(e.parentUUID,e.name):[],"goesTo"),r=a(n=>{const e=i(n.member.entityId);return[...e&&!e.root?l(e.parentUUID,e.name):[],...t(n)]},"slotsOf"),g=new Map,y=new Map,j=a((n,e,s)=>{for(const h of e){const c=n.get(h);c===void 0?n.set(h,[s]):c.length<2&&!c.includes(s)&&c.push(s)}},"note");for(const n of o){const{entityId:e}=n.member;d.has(e)&&j(y,t(n),e),(d.has(e)||p.has(e))&&j(g,r(n),e)}const v=a((n,e)=>{let s;for(const h of l(e.parentUUID,e.name)){const[c,w]=y.get(h)??[],I=c===n?w:c;I!==void 0&&(s===void 0||f.get(I)<f.get(s))&&(s=I)}return s},"pusher"),S=a((n,e,s)=>{const h=new Set([n]);for(let c=e;c!==null&&!h.has(c);){h.add(c);const w=i(c),I=s(c,w);if(I!==void 0)return I;c=w.parentUUID}return null},"climbs"),x=a(n=>{const{member:e,judgment:s,result:h}=n;if(s==="superseded"||s==="own"||s==="unseen"||s==="turn"||s==="waiting")return[];if(k(h.type))return[];const c=[];for(const U of r(n)){const[m,O]=g.get(U)??[],q=m===e.entityId?O:m;q!==void 0&&c.push(q)}const w=i(e.entityId);if(w&&!w.root){const U=S(e.entityId,w.parentUUID,(m,O)=>d.has(m)?m:!O||O.root?null:v(e.entityId,O));U!==null&&c.push(U)}if(h.status!=="active"||w?.root)return c;const I=S(e.entityId,h.parentUUID,(U,m)=>d.has(U)||p.has(U)?U:m?!m.available||!z.has(m.type)||m.root?null:v(e.entityId,m):u!==null&&e.through>u.through?u.entityId:null);return I!==null&&c.push(I),c},"partnersOf");let b=null;for(const n of o){const e=n.member.deliverySequence;if(b!==null&&e>=b)break;for(const s of x(n)){const h=Math.max(e,f.get(s));(b===null||h<b)&&(b=h)}}return b}a(C,"windowCut");function P(o){const i=new Map(o.map((t,r)=>[t.entityId,{member:t,index:r}])),d=new Map,p=new Set,u=a((t,r)=>{const g=d.get(t);if(g!==void 0)return g;if(r.has(t)){for(const x of r)p.add(x);return-1}const y=i.get(t);if(!y||y.member.parentUUID===null)return 0;const j=i.get(y.member.parentUUID);if(!j)return d.set(t,0),0;r.add(t);const v=u(j.member.entityId,r);r.delete(t);const S=v<0?-1:v+1;return S>=0&&d.set(t,S),S},"depthOf"),f=[],l=[];for(const[t,r]of o.entries()){const g=u(r.entityId,new Set);if(g<0){p.add(r.entityId);continue}(r.play==="leave"?l:f).push({id:r.entityId,depth:g,index:t})}return f.sort((t,r)=>t.depth-r.depth||t.index-r.index),l.sort((t,r)=>r.depth-t.depth||t.index-r.index),Object.freeze({order:Object.freeze([...f,...l].map(({id:t})=>t)),cycle:Object.freeze([...p])})}a(P,"revealOrder");function A(o,i){let d=0;for(;d<o.length&&i(o[d]);)d+=1;return o.slice(0,d)}a(A,"resolvedPrefix");export{D as WINDOW_MEMBERS,E as foldWindow,B as readWindow,A as resolvedPrefix,P as revealOrder};
@@ -3,6 +3,9 @@ import type { GraphPageRef } from '../entities/graph-pages.js';
3
3
  import type { OutboxRow } from './outbox.js';
4
4
  import type { AcceptedEntityRecord, EntityRecordInlineCargo, LocalEntityRecord } from './contracts.js';
5
5
  export type MaybePromise<T> = T | Promise<T>;
6
+ export interface RecordedPlace extends UnitPlace {
7
+ readonly gitHomeEntity: string | null;
8
+ }
6
9
  export type PendingEntityRecord = Extract<OutboxRow, {
7
10
  readonly status: 'pending';
8
11
  }> & {
@@ -46,7 +49,7 @@ export interface EntityRecordStorePort {
46
49
  countPending(through?: number): MaybePromise<number>;
47
50
  durableThrough(localSequence: number): MaybePromise<void>;
48
51
  readPackagePage(ref: GraphPageRef): MaybePromise<readonly unknown[]>;
49
- readPlace(uuid: string): MaybePromise<UnitPlace | null>;
52
+ readPlace(uuid: string): MaybePromise<RecordedPlace | null>;
50
53
  stampAccepted(stamps: readonly EntityRecordStamp[]): MaybePromise<void>;
51
54
  readReceipts(authorityId: string, through: number): MaybePromise<EntityRecordReceipts>;
52
55
  readDeliveryCursor(authorityId: string): MaybePromise<number | null>;
@@ -1 +1 @@
1
- var k=Object.defineProperty;var l=(c,t)=>k(c,"name",{value:t,configurable:!0});import{classifyFault as I}from"../faults/classify.js";import{OwnedEffects as O}from"./owned-effects.js";import{RetryClock as S}from"./retry-clock.js";import{ENTITY_RECORD_CARGO_MISSING as T,ENTITY_RECORD_OVER_STORAGE_LIMIT as E,EntityRecordRefusedError as g,acceptedPrefix as P,selectBatch as q}from"./send-batch.js";import{AheadScan as _}from"./send-ahead.js";import{ENTITY_RECORD_LOOKAHEAD_ROWS as p,SendPreparation as D}from"./send-preparation.js";const b=Object.freeze({progressed:!1,failure:null}),m=Object.freeze({progressed:!0,failure:null}),R=l(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");function A(c){if(c instanceof g)return c.code!==E;const t=I(c);return t.kind==="refusal"||t.kind==="need"&&t.need.subject?.type!=="authority"}l(A,"aboutOneRow");class x{static{l(this,"SendLine")}#e;#p;#i;#h;#m;#s=new O(R);#I;#a;#o=null;#b=!1;#l=!1;#n=!1;#r=new Map;#t=null;#c=null;#y=0;#S=!1;#R=0;#O=[];#E=null;#u=null;#k=new Set;#d=0;#f=0;#T=0;#g=0;constructor(t){this.#e=t.store,this.#p=t.authority,this.#I=t.onStamped,this.#a=t.onError,this.#i=new D(t.cargo,t.onError),this.#h=new S(t.schedule),this.#m=new S(t.schedule)}get stamped(){return this.#T}get uncertainThrough(){return this.#b?this.#d:void 0}get paused(){return this.#t?.error??null}get held(){return this.#t?this.#t.error:this.#r.size>0?this.#D():null}async waits(t){if(this.#t)return this.#t.error;const e=this.#c;if(!e)return null;const i=await this.#s.run(()=>this.#e.readPlace(t)),n={type:i?.type??"folder",parentUUID:i?.parentUUID??null,name:""};return await e.behind({entityId:t,facets:[],result:n})?this.held:null}start(){if(!this.#o){const t=l(()=>this.resume(),"resume");this.#O=[this.#p.onStorageChanged(t),this.#p.onDisconnect(t)]}return this.#o??=this.#s.run(()=>this.#e.readLocalSequence()).then(t=>{this.#d=this.#f=t,this.#b=!0,this.wake()}),this.#o}resume(){this.#R+=1,this.#S=!1,this.#t&&(this.#t=null,this.#w(),this.wake())}wake(){this.#l||(this.#n=!0,this.#P())}async settle(){if(!this.#o)throw new Error("entity record Send has not started");const t=this.#s.run(()=>this.#e.readLocalSequence()),e=t.catch(()=>0);this.#k.add(e);try{const[,i]=await Promise.all([this.#o,t]);this.#r.size>0&&this.#_();let n=null,a=!1;for(;this.#g<i;){const[o]=await this.#s.run(()=>this.#e.readPending(1));if(!o||o.localSequence>i)break;if(o.localSequence<=this.#g)continue;if(n)throw n.error;if(this.#t)throw this.#t.error;if(a)throw this.#D();const s=this.#u,h=await(s??this.#q());n=h.failure,a=!s&&!h.progressed&&this.#r.size>0}return i}finally{this.#k.delete(e),this.wake()}}async stop(){this.#l=!0,this.#h.cancel(),this.#m.cancel();for(const t of this.#O)t();await Promise.allSettled([this.#o,this.#E,this.#u,this.#i.stop(),this.#s.stop()])}#P(){if(!this.#b||this.#l||this.#h.armed||this.#E)return;const t=this.#A();this.#E=t,t.then(()=>{this.#E=null,this.#n&&this.#P()})}async#A(){for(;!this.#l&&this.#n&&!this.#h.armed;){this.#n=!1;const t=await(this.#u??this.#q());if(t.failure)return;t.progressed&&(this.#n=!0)}}#q(){this.#h.cancel();const t=this.#N();return this.#u=t,t.then(()=>{this.#u===t&&(this.#u=null)}),t}async#L(){const t=this.#c;if(!t)return this.#s.run(()=>this.#e.readPending(p));for(;t.wanting;){const e=await this.#s.run(()=>this.#e.readPending(p,t.after));if(await t.judge(e),e.length<p)break}return t!==this.#c?null:t.reach!==null?(this.#t=null,this.#y=t.reach,this.#S=!0,this.#w(),null):t.ready}async#N(){let t=[],e=0,i;const n=this.#R,a=this.#y;this.#y=0;const o=this.#c;try{const s=await this.#L();if(s===null)return m;const h=await this.#C(),u=s.filter(r=>r.localSequence<=h),y=this.#f;if(this.#i.retain(u,y),i=u[0],!i||(await this.#i.head(i),a>0&&await this.#i.settledThrough(a),this.#l))return b;const w=q(u,r=>this.#i.prepared(r));await this.#s.run(()=>this.#e.durableThrough(w[w.length-1].row.localSequence)),t=w;const f=await this.#p.submit(t.map(({submission:r})=>r)),d=P(t.map(({row:r})=>r),f);if(d.length>0&&(await this.#e.stampAccepted(d),e=d.length,o?.stamped(d.map(({row:r})=>r)),this.#M(d,o===null),this.#h.succeeded(),o===null&&this.#m.succeeded(),this.#t||(this.#S=!1)),f.refusal){const r=t[e].row;if(r.localSequence>y||f.refusal.code!==T)throw this.#i.forget([r]),i=r,new g(r.record.mutationId,f.refusal,{id:r.record.entityId,type:r.record.change.result.type});this.#f=r.localSequence-1}return m}catch(s){const h=s instanceof g;if(!h&&t.length>e&&(this.#d=Math.max(this.#d,t[t.length-1].row.localSequence)),this.#f=this.#d,h||(this.#y||=a),this.#l)return{progressed:e>0,failure:{error:s}};if(i&&(h||t.length===0)&&(A(s)||h&&this.#t))return await this.#z(i,s),this.#a(s),m;if(h&&s.code===E){const u=this.#R===n;return u&&(this.#t={error:s,row:i},this.#w(),this.#i.retain([],this.#f)),this.#n=!0,u&&a>0||this.#a(s),{progressed:e>0,failure:{error:s}}}return this.#h.arm(()=>this.wake()),this.#a(s),{progressed:e>0,failure:{error:s}}}}async#z(t,e){this.#r.set(t.localSequence,e),this.#i.forget([t]);const i=this.#c;try{i?await i.stepAside(t):this.#w()}catch(n){this.#a(n),this.#w()}this.#m.arm(()=>this.#_()),this.#n=!0}#_(){this.#r.clear(),this.#w(),this.#f=this.#d,this.wake()}#w(){const t=this.#t;this.#c=this.#r.size>0||t?new _({waiting:this.#r,pause:t&&{row:t.row,reached:this.#S},readPage:l(e=>this.#s.run(()=>this.#e.readPackagePage(e)),"readPage"),readPlace:l(e=>this.#s.run(()=>this.#e.readPlace(e)),"readPlace")}):null}#D(){return this.#r.get(Math.min(...this.#r.keys()))??R()}async#C(){for(const t of[...this.#k]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#M(t,e){this.#T+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#i.forget(t.map(({row:i})=>i));try{this.#I(t)}catch(i){this.#a(i)}}}export{x as SendLine};
1
+ var k=Object.defineProperty;var l=(c,t)=>k(c,"name",{value:t,configurable:!0});import{classifyFault as I}from"../faults/classify.js";import{OwnedEffects as O}from"./owned-effects.js";import{RetryClock as S}from"./retry-clock.js";import{ENTITY_RECORD_CARGO_MISSING as T,ENTITY_RECORD_OVER_STORAGE_LIMIT as E,EntityRecordRefusedError as g,acceptedPrefix as P,selectBatch as q}from"./send-batch.js";import{AheadScan as _}from"./send-ahead.js";import{ENTITY_RECORD_LOOKAHEAD_ROWS as p,SendPreparation as D}from"./send-preparation.js";const b=Object.freeze({progressed:!1,failure:null}),m=Object.freeze({progressed:!0,failure:null}),R=l(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");function A(c){if(c instanceof g)return c.code!==E;const t=I(c);return t.kind==="refusal"||t.kind==="need"&&t.need.subject?.type!=="authority"}l(A,"aboutOneRow");class x{static{l(this,"SendLine")}#e;#p;#i;#h;#m;#s=new O(R);#I;#a;#o=null;#b=!1;#l=!1;#n=!1;#r=new Map;#t=null;#c=null;#y=0;#S=!1;#R=0;#O=[];#E=null;#u=null;#k=new Set;#d=0;#f=0;#T=0;#g=0;constructor(t){this.#e=t.store,this.#p=t.authority,this.#I=t.onStamped,this.#a=t.onError,this.#i=new D(t.cargo,t.onError),this.#h=new S(t.schedule),this.#m=new S(t.schedule)}get stamped(){return this.#T}get uncertainThrough(){return this.#b?this.#d:void 0}get paused(){return this.#t?.error??null}get held(){return this.#t?this.#t.error:this.#r.size>0?this.#D():null}async waits(t){if(this.#t)return this.#t.error;const e=this.#c;if(!e)return null;const i=await this.#s.run(()=>this.#e.readPlace(t)),n={type:i?.type??"folder",parentUUID:i?.parentUUID??null,name:"",gitHomeEntity:i?.gitHomeEntity??null};return await e.behind({entityId:t,facets:[],result:n})?this.held:null}start(){if(!this.#o){const t=l(()=>this.resume(),"resume");this.#O=[this.#p.onStorageChanged(t),this.#p.onDisconnect(t)]}return this.#o??=this.#s.run(()=>this.#e.readLocalSequence()).then(t=>{this.#d=this.#f=t,this.#b=!0,this.wake()}),this.#o}resume(){this.#R+=1,this.#S=!1,this.#t&&(this.#t=null,this.#w(),this.wake())}wake(){this.#l||(this.#n=!0,this.#P())}async settle(){if(!this.#o)throw new Error("entity record Send has not started");const t=this.#s.run(()=>this.#e.readLocalSequence()),e=t.catch(()=>0);this.#k.add(e);try{const[,i]=await Promise.all([this.#o,t]);this.#r.size>0&&this.#_();let n=null,a=!1;for(;this.#g<i;){const[o]=await this.#s.run(()=>this.#e.readPending(1));if(!o||o.localSequence>i)break;if(o.localSequence<=this.#g)continue;if(n)throw n.error;if(this.#t)throw this.#t.error;if(a)throw this.#D();const s=this.#u,h=await(s??this.#q());n=h.failure,a=!s&&!h.progressed&&this.#r.size>0}return i}finally{this.#k.delete(e),this.wake()}}async stop(){this.#l=!0,this.#h.cancel(),this.#m.cancel();for(const t of this.#O)t();await Promise.allSettled([this.#o,this.#E,this.#u,this.#i.stop(),this.#s.stop()])}#P(){if(!this.#b||this.#l||this.#h.armed||this.#E)return;const t=this.#A();this.#E=t,t.then(()=>{this.#E=null,this.#n&&this.#P()})}async#A(){for(;!this.#l&&this.#n&&!this.#h.armed;){this.#n=!1;const t=await(this.#u??this.#q());if(t.failure)return;t.progressed&&(this.#n=!0)}}#q(){this.#h.cancel();const t=this.#N();return this.#u=t,t.then(()=>{this.#u===t&&(this.#u=null)}),t}async#L(){const t=this.#c;if(!t)return this.#s.run(()=>this.#e.readPending(p));for(;t.wanting;){const e=await this.#s.run(()=>this.#e.readPending(p,t.after));if(await t.judge(e),e.length<p)break}return t!==this.#c?null:t.reach!==null?(this.#t=null,this.#y=t.reach,this.#S=!0,this.#w(),null):t.ready}async#N(){let t=[],e=0,i;const n=this.#R,a=this.#y;this.#y=0;const o=this.#c;try{const s=await this.#L();if(s===null)return m;const h=await this.#C(),u=s.filter(r=>r.localSequence<=h),y=this.#f;if(this.#i.retain(u,y),i=u[0],!i||(await this.#i.head(i),a>0&&await this.#i.settledThrough(a),this.#l))return b;const w=q(u,r=>this.#i.prepared(r));await this.#s.run(()=>this.#e.durableThrough(w[w.length-1].row.localSequence)),t=w;const f=await this.#p.submit(t.map(({submission:r})=>r)),d=P(t.map(({row:r})=>r),f);if(d.length>0&&(await this.#e.stampAccepted(d),e=d.length,o?.stamped(d.map(({row:r})=>r)),this.#M(d,o===null),this.#h.succeeded(),o===null&&this.#m.succeeded(),this.#t||(this.#S=!1)),f.refusal){const r=t[e].row;if(r.localSequence>y||f.refusal.code!==T)throw this.#i.forget([r]),i=r,new g(r.record.mutationId,f.refusal,{id:r.record.entityId,type:r.record.change.result.type});this.#f=r.localSequence-1}return m}catch(s){const h=s instanceof g;if(!h&&t.length>e&&(this.#d=Math.max(this.#d,t[t.length-1].row.localSequence)),this.#f=this.#d,h||(this.#y||=a),this.#l)return{progressed:e>0,failure:{error:s}};if(i&&(h||t.length===0)&&(A(s)||h&&this.#t))return await this.#z(i,s),this.#a(s),m;if(h&&s.code===E){const u=this.#R===n;return u&&(this.#t={error:s,row:i},this.#w(),this.#i.retain([],this.#f)),this.#n=!0,u&&a>0||this.#a(s),{progressed:e>0,failure:{error:s}}}return this.#h.arm(()=>this.wake()),this.#a(s),{progressed:e>0,failure:{error:s}}}}async#z(t,e){this.#r.set(t.localSequence,e),this.#i.forget([t]);const i=this.#c;try{i?await i.stepAside(t):this.#w()}catch(n){this.#a(n),this.#w()}this.#m.arm(()=>this.#_()),this.#n=!0}#_(){this.#r.clear(),this.#w(),this.#f=this.#d,this.wake()}#w(){const t=this.#t;this.#c=this.#r.size>0||t?new _({waiting:this.#r,pause:t&&{row:t.row,reached:this.#S},readPage:l(e=>this.#s.run(()=>this.#e.readPackagePage(e)),"readPage"),readPlace:l(e=>this.#s.run(()=>this.#e.readPlace(e)),"readPlace")}):null}#D(){return this.#r.get(Math.min(...this.#r.keys()))??R()}async#C(){for(const t of[...this.#k]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#M(t,e){this.#T+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#i.forget(t.map(({row:i})=>i));try{this.#I(t)}catch(i){this.#a(i)}}}export{x as SendLine};
@@ -9,6 +9,7 @@ export interface WaitingChange {
9
9
  readonly type: EntityType;
10
10
  readonly parentUUID: string | null;
11
11
  readonly name: string;
12
+ readonly gitHomeEntity?: string | null;
12
13
  };
13
14
  }
14
15
  export interface WaitingMember {
@@ -1 +1 @@
1
- var u=Object.defineProperty;var a=(n,t)=>u(n,"name",{value:t,configurable:!0});const l=new Set(["create","rename","move","restore"]),h=new Set(["move","restore","delete"]),o=a((n,t)=>n===null?null:`${n}\0${t}`,"slot");class f{static{a(this,"Waits")}#r;#e=new Map;#t=new Set;#i=new Set;#n=new Set;#a=new Set;#l=!1;#s=!1;constructor(t){this.#r=t}get empty(){return this.#t.size===0}async hold(t,i=[]){this.#t.add(t.entityId),this.#o(t.result.parentUUID,t.result.name,t.result.type),t.facets.includes("rename")&&t.result.parentUUID!==null&&this.#a.add(t.result.parentUUID),t.facets.includes("move")&&(this.#l=!0);for(const s of i)this.#t.add(s.uuid),this.#o(s.parentUUID,s.name,s.type);let e=t.result.type==="repo.git"?t.entityId:null;for(const s of await this.#u(t.result.parentUUID))this.#i.add(s),e===null&&(await this.#h(s))?.type==="repo.git"&&(e=s);e!==null&&(this.#t.add(e),this.#s=!0)}async behind(t){if(this.empty)return!1;const{entityId:i,facets:r,result:e}=t;if(this.#t.has(i)||r.some(s=>l.has(s))&&e.parentUUID!==null&&(this.#n.has(o(e.parentUUID,e.name))||this.#a.has(e.parentUUID))||this.#l&&r.some(s=>l.has(s)||s==="delete")||this.#s&&e.type==="repo.git"||this.#i.has(i)&&r.some(s=>h.has(s)))return!0;for(const s of await this.#u(e.parentUUID))if(this.#t.has(s))return!0;return!1}#o(t,i,r){const e=o(t,i);e!==null&&this.#n.add(e),r==="repo.git"&&(this.#s=!0)}async#u(t){const i=[];for(let r=t;r!==null&&!i.includes(r);){i.push(r);const e=await this.#h(r);if(!e)break;r=e.parentUUID}return i}async#h(t){return this.#e.has(t)||this.#e.set(t,await this.#r(t)),this.#e.get(t)}}export{f as Waits};
1
+ var u=Object.defineProperty;var a=(n,t)=>u(n,"name",{value:t,configurable:!0});const l=new Set(["create","rename","move","restore"]),h=new Set(["move","restore","delete"]),o=a((n,t)=>n===null?null:`${n}\0${t}`,"slot");class d{static{a(this,"Waits")}#s;#e=new Map;#t=new Set;#i=new Set;#r=new Set;#n=new Set;#a=!1;constructor(t){this.#s=t}get empty(){return this.#t.size===0}async hold(t,r=[]){this.#t.add(t.entityId),this.#l(t.result.parentUUID,t.result.name),t.facets.includes("rename")&&t.result.parentUUID!==null&&this.#n.add(t.result.parentUUID),t.facets.includes("move")&&(this.#a=!0);for(const e of r)this.#t.add(e.uuid),this.#l(e.parentUUID,e.name);let s=t.result.type==="repo.git"?t.entityId:null;for(const e of await this.#o(t.result.parentUUID))this.#i.add(e),s===null&&(await this.#u(e))?.type==="repo.git"&&(s=e);s!==null&&this.#t.add(s)}async behind(t){if(this.empty)return!1;const{entityId:r,facets:i,result:s}=t;if(this.#t.has(r)||i.some(e=>l.has(e))&&s.parentUUID!==null&&(this.#r.has(o(s.parentUUID,s.name))||this.#n.has(s.parentUUID))||this.#a&&i.some(e=>l.has(e)||e==="delete")||s.gitHomeEntity!=null&&this.#t.has(s.gitHomeEntity)||this.#i.has(r)&&i.some(e=>h.has(e)))return!0;for(const e of await this.#o(s.parentUUID))if(this.#t.has(e))return!0;return!1}#l(t,r){const i=o(t,r);i!==null&&this.#r.add(i)}async#o(t){const r=[];for(let i=t;i!==null&&!r.includes(i);){r.push(i);const s=await this.#u(i);if(!s)break;i=s.parentUUID}return r}async#u(t){return this.#e.has(t)||this.#e.set(t,await this.#s(t)),this.#e.get(t)}}export{d as Waits};
@@ -1 +1 @@
1
- "use strict";var a=Object.defineProperty;var i=(e,n)=>a(e,"name",{value:n,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.invalidStep=void 0,exports.createApplyLine=d,exports.releaseApplyLine=f,exports.heldWait=p,exports.assertApplyDependency=m,exports.assertApplyLineStep=A,exports.assertApplyWindow=s;const u=require("../entities/types.js");function d(e){return{kind:"line",authorityId:e,endsBefore:null,held:null,queued:!1,running:!1,again:!1}}i(d,"createApplyLine");function f(e,n,l){const r=e.held;return!r?.dependency||r.due||r.dependency.producer!==n||l!==void 0&&!r.dependency.entityIds.some(t=>l.includes(t))?!1:(r.due=!0,!0)}i(f,"releaseApplyLine");function p(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(p,"heldWait");const c=new Set(["detect","apply","capability","send"]),y=i(e=>!!e&&c.has(e.producer)&&Array.isArray(e.entityIds)&&e.entityIds.length>0&&e.entityIds.every(n=>typeof n=="string"&&n.length>0),"namesRelease");function m(e){if(!y(e))throw new Error("Apply dependency must name the entities that can release it")}i(m,"assertApplyDependency");function A(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(l=>l.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(A,"assertApplyLineStep");const S=i(e=>{throw new Error(`entity Apply store returned an invalid ${e}`)},"invalidStep");exports.invalidStep=S;function s(e,n){(!Array.isArray(e.members)||e.members.length===0)&&(0,exports.invalidStep)(n);const l=i((t,o)=>Number.isSafeInteger(t)&&t>=(o?0:1),"at");for(const t of e.members)(!u.UUID_PATTERN.test(t.work?.entityId??"")||t.work.authorityId!==e.authorityId||!l(t.deliverySequence,t.work.kept!==null))&&(0,exports.invalidStep)(n),(t.unseen!==void 0||t.judgment==="unseen")&&(u.UUID_PATTERN.test(t.unseen??"")||(0,exports.invalidStep)(n));const{deliverySequence:r}=e;(r===null?!e.intent:!l(r,e.members[0].work.kept!==null))&&(0,exports.invalidStep)(n)}i(s,"assertApplyWindow");
1
+ "use strict";var a=Object.defineProperty;var i=(e,n)=>a(e,"name",{value:n,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.invalidStep=void 0,exports.createApplyLine=d,exports.releaseApplyLine=p,exports.heldWait=f,exports.assertApplyDependency=m,exports.assertApplyLineStep=A,exports.assertApplyWindow=s;const u=require("../entities/types.js");function d(e){return{kind:"line",authorityId:e,held:null,queued:!1,running:!1,again:!1}}i(d,"createApplyLine");function p(e,n,l){const r=e.held;return!r?.dependency||r.due||r.dependency.producer!==n||l!==void 0&&!r.dependency.entityIds.some(t=>l.includes(t))?!1:(r.due=!0,!0)}i(p,"releaseApplyLine");function f(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(f,"heldWait");const c=new Set(["detect","apply","capability","send"]),y=i(e=>!!e&&c.has(e.producer)&&Array.isArray(e.entityIds)&&e.entityIds.length>0&&e.entityIds.every(n=>typeof n=="string"&&n.length>0),"namesRelease");function m(e){if(!y(e))throw new Error("Apply dependency must name the entities that can release it")}i(m,"assertApplyDependency");function A(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(l=>l.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(A,"assertApplyLineStep");const S=i(e=>{throw new Error(`entity Apply store returned an invalid ${e}`)},"invalidStep");exports.invalidStep=S;function s(e,n){(!Array.isArray(e.members)||e.members.length===0)&&(0,exports.invalidStep)(n);const l=i((t,o)=>Number.isSafeInteger(t)&&t>=(o?0:1),"at");for(const t of e.members)(!u.UUID_PATTERN.test(t.work?.entityId??"")||t.work.authorityId!==e.authorityId||!l(t.deliverySequence,t.work.kept!==null))&&(0,exports.invalidStep)(n),(t.unseen!==void 0||t.judgment==="unseen")&&(u.UUID_PATTERN.test(t.unseen??"")||(0,exports.invalidStep)(n));const{deliverySequence:r}=e;(r===null?!e.intent:!l(r,e.members[0].work.kept!==null))&&(0,exports.invalidStep)(n)}i(s,"assertApplyWindow");
@@ -1 +1 @@
1
- "use strict";var E=Object.defineProperty;var a=(d,e)=>E(d,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyRail=exports.ENTITY_APPLY_SAME_ANSWERS=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"),I=require("./fetch-ahead.js"),c=require("./journey.js"),g=require("./lane.js"),l=require("./line.js"),A=require("./window.js");exports.ENTITY_APPLY_DEFAULT_CONCURRENCY=8,exports.ENTITY_APPLY_RETRY_BASE_MS=1e3,exports.ENTITY_APPLY_RETRY_MAX_MS=3e4,exports.ENTITY_APPLY_SAME_ANSWERS=3;class P{static{a(this,"EntityApplyRail")}#e;#g;#k;#O;#v;#d;#T;#a=!1;#t=!1;#o=new Map;#s=new Map;#l={queue:[],steps:new Set};#u={queue:[],steps:new Set};#r=!1;#h=null;#p=1/0;#A=[];#m=0;#S=new Map;#y=null;constructor(e){this.#e=e,this.#g=e.concurrency??exports.ENTITY_APPLY_DEFAULT_CONCURRENCY,this.#k=e.laneConcurrency??this.#g,this.#O=e.onBackgroundError??(()=>{}),this.#v=e.onIdle??(()=>{});for(const[t,s]of[["concurrency",this.#g],["lane concurrency",this.#k]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${t} must be a positive integer`);this.#d=new c.EntityApplyJourney(e,()=>this.#m,()=>this.#t),this.#T=new I.FetchAhead(e.cargo,{sha256Hex:e.sha256Hex,loops:e.loops,deliveryPosition:e.deliveryPosition})}async start(){if(this.#a)throw new Error("entity Apply rail already started");if(this.#t)throw new Error("entity Apply rail is stopped");this.#a=!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.#I(t,null);await this.#M()}fetch(e,t){return this.#a?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.#I(e,null,t.emptied===!0),r=a(()=>this.#e.schedule.schedule(t.stall,()=>{n.waiters.delete(u)&&(n.emptied&&![...n.waiters].some(o=>o.emptied)&&(n.emptied=!1),s(this.#K(n)))}),"arm");let h=r();const u={emptied:t.emptied===!0,answer:a(o=>{h.cancel(),s(o)},"answer"),fail:a(o=>{h.cancel(),i(o)},"fail"),moved:a(()=>{h.cancel(),h=r()},"moved")};n.waiters.add(u),n.waitsOn===null&&this.#$(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.#P(e)}recordsAvailable(){this.#_()}localStateChanged(e){this.#c("detect",e),this.#c("apply",e),this.#_()}sendAccepted(e){this.#c("send",e),this.#_()}capabilityChanged(e){this.#c("capability",e),this.#_()}owe(e){if(e.some(t=>!f.UUID_PATTERN.test(t)))throw new Error("entity Apply owe needs entity UUIDs");if(!(!this.#a||this.#t)){for(const t of e)this.#Y(t);this.#n()}}#_(){!this.#a||this.#t||(this.#r=!0,this.#n())}waiting(){return this.#w().flatMap(e=>(0,l.heldWait)(e.held,y(e))??[])}async flush(){if(!this.#a)throw new Error("entity Apply rail has not started");this.#t||await this.#M()}async stop(){if(this.#t)return;this.#t=!0,this.#N();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.#q()>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.#A.splice(0))t.resolve()}#M(){return new Promise((e,t)=>{this.#A.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.#X();this.#h=e,e.then(()=>{this.#h=null,this.#n()})}for(const[e,t]of[[this.#u,this.#k],[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.#q()===0&&this.#h===null&&!this.#r&&this.#l.queue.length===0&&this.#u.queue.length===0){if(this.#S.clear(),this.#F(this.#e.now())){this.#n();return}this.#b();for(const e of this.#A.splice(0))e.resolve();this.#v()}}}#q(){return this.#l.steps.size+this.#u.steps.size}#w(){return[...this.#o.values(),...this.#s.values()]}#E(e){return e.kind==="line"?this.#o.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.#J(e):this.#H(e)).then(i=>{e.running=!1,t.steps.delete(s),(i||e.again)&&this.#E(e)&&(e.again=!1,this.#i(e)),this.#n()});t.steps.add(s)}async#X(){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.#o)!t.has(s)&&!i.running&&!i.queued&&this.#o.delete(s);for(const s of e){let i=this.#o.get(s);i||(i=(0,l.createApplyLine)(s),this.#o.set(s,i)),this.#i(i)}}catch(e){this.#p=this.#e.now()+exports.ENTITY_APPLY_RETRY_MAX_MS;const t=this.#A.splice(0);t.length===0&&this.#R(e);for(const s of t)s.reject(e)}}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.#m,i=await this.#e.store.readApplyWindow(e.authorityId,e.endsBefore);if((0,l.assertApplyLineStep)(e.authorityId,i),this.#t)return!1;if(i.kind==="end"){e.held=null;const n=e.endsBefore!==null;return e.endsBefore=null,n}return i.window.deliverySequence!==null&&this.#T.advance(e.authorityId,i.window.deliverySequence),await this.#j(e,i.window,s)}catch(t){return this.#C(e,t),!1}}async#H(e){try{const t=e.held;if(t&&t.entityId===null&&!t.due&&t.notBefore>this.#e.now())return!1;const s=this.#m,i=await this.#e.store.readFetchWindow(e.unit,e.toPrefix?null:e.through);if((0,g.assertApplyLaneStep)(e.unit,i),this.#t||!this.#E(e))return!1;if(e.waitsOn=null,i.kind==="unseen")return this.#L(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.#V(e,i.through,i.reason);case"prerequisite":return this.#G(e,i.unit,i.reason,s);case"window":return await this.#j(e,i.window,s)}}catch(t){return this.#C(e,t),!1}}async#V(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(e)?!1:(e.through===t&&e.met.add(e.unit),this.#P(e.unit),!0))}#Y(e){const t=this.#I(e,null);t.toPrefix=!0,t.met.clear(),this.#i(t)}#I(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}#G(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.#U(e,t,s)?this.#L(e,{producer:"apply",entityIds:[t]},s,i):!1}#L(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}#$(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.#U(e,s,t.reason))return}}#U(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.#I(t,e.through)),!0}#f(e,t){if(!this.#E(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.#P(e.unit),this.#c("apply",[e.unit]))}#P(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()}}#K(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.#Z(e,t))return!1;let n=await(0,c.outcomeOf)(()=>this.#d.run(t,()=>e.kind==="lane"&&e.emptied?e.unit:null));if(e.kind==="line"&&typeof n=="object"&&n.kind==="park"&&(0,A.windowEndForWait)(t,n.dependency)===null&&this.#Q(e,t,n)&&(n=await(0,c.outcomeOf)(()=>this.#d.alone(t.members[0]))),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.#Y(r.work.entityId);for(const r of t.members)r.unseen!==void 0&&this.#Y(r.unseen);return this.#ee(e,n==="progress"?t.members.map(r=>r.work.entityId):[n.entityId])}if(n==="again")return e.held?.entityId===i&&(e.held=null),!0;if(n.advanced?.length&&this.#W(e,n.advanced),e.kind==="line"){const r=(0,A.windowEndForWait)(t,n.dependency);if(r!==null)return e.endsBefore=r,!0}return this.#te(e,t,n,s),e.kind==="lane"&&this.#$(e),e.held.due}#Q(e,t,s){const i=t.members[0].work.entityId;if(s.cause!==void 0||!(0,c.stepsAside)(t,i))return!1;const n=s.dependency?.entityIds??[i];return n.length===0||n.some(r=>r!==i)?!1:s.dependency?.producer==="capability"?!0:this.#B(e,t)>=exports.ENTITY_APPLY_SAME_ANSWERS}#B(e,t){const s=e.held,i=t.members[0].work.entityId;return(s?.entityId===i&&s.target===this.#d.targetOf(t)?s.attempt:0)+1}async#Z(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.#d.targetOf(t)!==s.target||t.cutoff!==s.cutoff?!1:await this.#e.store.localWorkAfter(t.authorityId,i.work.entityId,s.kept??s.target)===null}#ee(e,t){return e.held&&(e.held.entityId===null||t.includes(e.held.entityId))&&(e.held=null),this.#W(e,t),!0}#W(e,t){e.kind==="lane"&&(this.#r=!0,this.#P(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))}#te(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].work.entityId,r=e.held,h=this.#B(e,t),u=this.#D(h),o=s.again===!0&&h<exports.ENTITY_APPLY_SAME_ANSWERS,w={entityId:n,target:this.#d.targetOf(t),kept:t.members[0].work.kept,cutoff:t.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#e.now()+(o?0:u),releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#x(s.dependency,s.comparedAt??i):o,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};e.held=w,this.#b();const _=(0,l.heldWait)(r,y(e)),p=(0,l.heldWait)(w,y(e));p&&JSON.stringify(p)!==JSON.stringify(_)&&this.#e.onWaiting?.(p),!(!s.report&&(s.dependency!==void 0||h<exports.ENTITY_APPLY_SAME_ANSWERS))&&this.#R(new Error(`${m(e)} holds at ${n}, 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}))}#C(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.#b(),this.#R(new Error(`${m(e)} faulted and will retry in ${n}ms (attempt ${i}): ${r}`,{cause:t}))}#R(e){try{this.#O(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.#m;for(const i of t??["*"])this.#S.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.#S.get(`${e.producer}:${s}`)??0)>t)}#b(){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.#N();return}if(this.#y?.dueAt===e)return;this.#N();const t=Math.max(0,e-this.#e.now()),s=this.#e.schedule.schedule(t,()=>{this.#y=null,this.#F(e),this.#n()});this.#y={dueAt:e,cancel:a(()=>s.cancel(),"cancel")}}#F(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}#N(){this.#y?.cancel(),this.#y=null}}exports.EntityApplyRail=P;const y=a(d=>d.kind==="line"?d.authorityId:d.unit,"owner"),m=a(d=>d.kind==="line"?`entity Apply line ${d.authorityId}`:`entity Apply fetch of ${d.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){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"&&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==="again"?(e.held?.entityId===i&&(e.held=null),!0):(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 +1 @@
1
- "use strict";var E=Object.defineProperty;var a=(r,s)=>E(r,"name",{value:s,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.WINDOW_MEMBERS=void 0,exports.foldWindow=D,exports.readWindow=_,exports.windowEndForWait=R,exports.revealOrder=k,exports.resolvedPrefix=$;const z=require("../entities/rules.js"),q=require("../entities/types.js");exports.WINDOW_MEMBERS=4096;function D(r,s=exports.WINDOW_MEMBERS){const d=new Map;let h=0;for(const u of r){const{entityId:l}=u.record;let f=d.get(l);if(f===void 0){if(d.size===s)break;d.set(l,f={records:[],deliverySequence:u.deliverySequence,through:0})}const n=f.records.at(-1);if(n!==void 0&&n.change.turn===void 0!=(u.record.change.turn===void 0))break;if(n!==void 0&&u.record.globalSequence!==n.globalSequence+1)throw new Error(`entity ${l} has a gap before sequence ${u.record.globalSequence} inside its window`);f.records.push(u.record),f.through=h=u.deliverySequence}return Object.freeze({members:Object.freeze([...d].map(([u,{records:l,deliverySequence:f,through:n}])=>Object.freeze({entityId:u,records:Object.freeze(l),deliverySequence:f,through:n}))),through:h})}a(D,"foldWindow");function _(r,s,d,h=exports.WINDOW_MEMBERS){const u=new Map;for(let l=r;;){const f=D(l,h).members.map(o=>{const y=`${o.entityId}:${o.records.at(-1).globalSequence}`;let g=u.get(y);return g===void 0&&u.set(y,g=s(o)),{member:o,judgment:g.judgment,result:g.result}}),n=N(f,d);if(n===null)return f;l=l.slice(0,l.findIndex(o=>o.deliverySequence>=n))}}a(_,"readWindow");const x=a((r,s)=>r!==void 0&&(r.root?r.type!==s.type:r.parentUUID!==s.parentUUID||r.name!==s.name||r.type!==s.type),"movesPlace");function N(r,s){const d=new Set,h=new Set;let u=null;for(const{member:t,judgment:e,result:i}of r)e==="leave"?h.add(t.entityId):(e==="enter"||e==="apply"&&x(s(t.entityId),i))&&d.add(t.entityId),e==="enter"&&q.CONTAINERS.has(i.type)&&t.through<(u?.through??1/0)&&(u=t);if(d.size===0&&h.size===0)return null;const l=new Map(r.map(({member:t})=>[t.entityId,t.deliverySequence])),f=a((t,e)=>t===null?[]:[e,...(0,z.collisionBases)(e)].map(i=>`${t}/${i}`),"slotKeys"),n=a(({member:t,result:e})=>e.status==="active"&&!s(t.entityId)?.root?f(e.parentUUID,e.name):[],"goesTo"),o=a(t=>{const e=s(t.member.entityId);return[...e&&!e.root?f(e.parentUUID,e.name):[],...n(t)]},"slotsOf"),y=new Map,g=new Map,v=a((t,e,i)=>{for(const p of e){const c=t.get(p);c===void 0?t.set(p,[i]):c.length<2&&!c.includes(i)&&c.push(i)}},"note");for(const t of r){const{entityId:e}=t.member;d.has(e)&&v(g,n(t),e),(d.has(e)||h.has(e))&&v(y,o(t),e)}const b=a((t,e)=>{let i;for(const p of f(e.parentUUID,e.name)){const[c,I]=g.get(p)??[],m=c===t?I:c;m!==void 0&&(i===void 0||l.get(m)<l.get(i))&&(i=m)}return i},"pusher"),O=a((t,e,i)=>{const p=new Set([t]);for(let c=e;c!==null&&!p.has(c);){p.add(c);const I=s(c),m=i(c,I);if(m!==void 0)return m;c=I.parentUUID}return null},"climbs"),M=a(t=>{const{member:e,judgment:i,result:p}=t;if(i==="superseded"||i==="own"||i==="unseen"||i==="turn"||i==="waiting")return[];if((0,q.isConstructType)(p.type))return[];const c=[];for(const S of o(t)){const[w,j]=y.get(S)??[],W=w===e.entityId?j:w;W!==void 0&&c.push(W)}const I=s(e.entityId);if(I&&!I.root){const S=O(e.entityId,I.parentUUID,(w,j)=>d.has(w)?w:!j||j.root?null:b(e.entityId,j));S!==null&&c.push(S)}if(p.status!=="active"||I?.root)return c;const m=O(e.entityId,p.parentUUID,(S,w)=>d.has(S)||h.has(S)?S:w?!w.available||!q.CONTAINERS.has(w.type)||w.root?null:b(e.entityId,w):u!==null&&e.through>u.through?u.entityId:null);return m!==null&&c.push(m),c},"partnersOf");let U=null;for(const t of r){const e=t.member.deliverySequence;if(U!==null&&e>=U)break;for(const i of M(t)){const p=Math.max(e,l.get(i));(U===null||p<U)&&(U=p)}}return U}a(N,"windowCut");function R(r,s){return r.intent!==null||r.unit!==null||s?.producer!=="apply"?null:r.members.slice(1).find(h=>s.entityIds.includes(h.work.entityId))?.deliverySequence??null}a(R,"windowEndForWait");function k(r){const s=new Map(r.map((n,o)=>[n.entityId,{member:n,index:o}])),d=new Map,h=new Set,u=a((n,o)=>{const y=d.get(n);if(y!==void 0)return y;if(o.has(n)){for(const M of o)h.add(M);return-1}const g=s.get(n);if(!g||g.member.parentUUID===null)return 0;const v=s.get(g.member.parentUUID);if(!v)return d.set(n,0),0;o.add(n);const b=u(v.member.entityId,o);o.delete(n);const O=b<0?-1:b+1;return O>=0&&d.set(n,O),O},"depthOf"),l=[],f=[];for(const[n,o]of r.entries()){const y=u(o.entityId,new Set);if(y<0){h.add(o.entityId);continue}(o.play==="leave"?f:l).push({id:o.entityId,depth:y,index:n})}return l.sort((n,o)=>n.depth-o.depth||n.index-o.index),f.sort((n,o)=>o.depth-n.depth||n.index-o.index),Object.freeze({order:Object.freeze([...l,...f].map(({id:n})=>n)),cycle:Object.freeze([...h])})}a(k,"revealOrder");function $(r,s){let d=0;for(;d<r.length&&s(r[d]);)d+=1;return r.slice(0,d)}a($,"resolvedPrefix");
1
+ "use strict";var E=Object.defineProperty;var a=(o,i)=>E(o,"name",{value:i,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.WINDOW_MEMBERS=void 0,exports.foldWindow=W,exports.readWindow=_,exports.revealOrder=R,exports.resolvedPrefix=$;const z=require("../entities/rules.js"),q=require("../entities/types.js");exports.WINDOW_MEMBERS=4096;function W(o,i=exports.WINDOW_MEMBERS){const d=new Map;let p=0;for(const u of o){const{entityId:f}=u.record;let l=d.get(f);if(l===void 0){if(d.size===i)break;d.set(f,l={records:[],deliverySequence:u.deliverySequence,through:0})}const t=l.records.at(-1);if(t!==void 0&&t.change.turn===void 0!=(u.record.change.turn===void 0))break;if(t!==void 0&&u.record.globalSequence!==t.globalSequence+1)throw new Error(`entity ${f} has a gap before sequence ${u.record.globalSequence} inside its window`);l.records.push(u.record),l.through=p=u.deliverySequence}return Object.freeze({members:Object.freeze([...d].map(([u,{records:f,deliverySequence:l,through:t}])=>Object.freeze({entityId:u,records:Object.freeze(f),deliverySequence:l,through:t}))),through:p})}a(W,"foldWindow");function _(o,i,d,p=exports.WINDOW_MEMBERS){const u=new Map;for(let f=o;;){const l=W(f,p).members.map(r=>{const y=`${r.entityId}:${r.records.at(-1).globalSequence}`;let g=u.get(y);return g===void 0&&u.set(y,g=i(r)),{member:r,judgment:g.judgment,result:g.result}}),t=N(l,d);if(t===null)return l;f=f.slice(0,f.findIndex(r=>r.deliverySequence>=t))}}a(_,"readWindow");const x=a((o,i)=>o!==void 0&&(o.root?o.type!==i.type:o.parentUUID!==i.parentUUID||o.name!==i.name||o.type!==i.type),"movesPlace");function N(o,i){const d=new Set,p=new Set;let u=null;for(const{member:n,judgment:e,result:s}of o)e==="leave"?p.add(n.entityId):(e==="enter"||e==="apply"&&x(i(n.entityId),s))&&d.add(n.entityId),e==="enter"&&q.CONTAINERS.has(s.type)&&n.through<(u?.through??1/0)&&(u=n);if(d.size===0&&p.size===0)return null;const f=new Map(o.map(({member:n})=>[n.entityId,n.deliverySequence])),l=a((n,e)=>n===null?[]:[e,...(0,z.collisionBases)(e)].map(s=>`${n}/${s}`),"slotKeys"),t=a(({member:n,result:e})=>e.status==="active"&&!i(n.entityId)?.root?l(e.parentUUID,e.name):[],"goesTo"),r=a(n=>{const e=i(n.member.entityId);return[...e&&!e.root?l(e.parentUUID,e.name):[],...t(n)]},"slotsOf"),y=new Map,g=new Map,j=a((n,e,s)=>{for(const h of e){const c=n.get(h);c===void 0?n.set(h,[s]):c.length<2&&!c.includes(s)&&c.push(s)}},"note");for(const n of o){const{entityId:e}=n.member;d.has(e)&&j(g,t(n),e),(d.has(e)||p.has(e))&&j(y,r(n),e)}const v=a((n,e)=>{let s;for(const h of l(e.parentUUID,e.name)){const[c,I]=g.get(h)??[],m=c===n?I:c;m!==void 0&&(s===void 0||f.get(m)<f.get(s))&&(s=m)}return s},"pusher"),O=a((n,e,s)=>{const h=new Set([n]);for(let c=e;c!==null&&!h.has(c);){h.add(c);const I=i(c),m=s(c,I);if(m!==void 0)return m;c=I.parentUUID}return null},"climbs"),b=a(n=>{const{member:e,judgment:s,result:h}=n;if(s==="superseded"||s==="own"||s==="unseen"||s==="turn"||s==="waiting")return[];if((0,q.isConstructType)(h.type))return[];const c=[];for(const S of r(n)){const[w,M]=y.get(S)??[],D=w===e.entityId?M:w;D!==void 0&&c.push(D)}const I=i(e.entityId);if(I&&!I.root){const S=O(e.entityId,I.parentUUID,(w,M)=>d.has(w)?w:!M||M.root?null:v(e.entityId,M));S!==null&&c.push(S)}if(h.status!=="active"||I?.root)return c;const m=O(e.entityId,h.parentUUID,(S,w)=>d.has(S)||p.has(S)?S:w?!w.available||!q.CONTAINERS.has(w.type)||w.root?null:v(e.entityId,w):u!==null&&e.through>u.through?u.entityId:null);return m!==null&&c.push(m),c},"partnersOf");let U=null;for(const n of o){const e=n.member.deliverySequence;if(U!==null&&e>=U)break;for(const s of b(n)){const h=Math.max(e,f.get(s));(U===null||h<U)&&(U=h)}}return U}a(N,"windowCut");function R(o){const i=new Map(o.map((t,r)=>[t.entityId,{member:t,index:r}])),d=new Map,p=new Set,u=a((t,r)=>{const y=d.get(t);if(y!==void 0)return y;if(r.has(t)){for(const b of r)p.add(b);return-1}const g=i.get(t);if(!g||g.member.parentUUID===null)return 0;const j=i.get(g.member.parentUUID);if(!j)return d.set(t,0),0;r.add(t);const v=u(j.member.entityId,r);r.delete(t);const O=v<0?-1:v+1;return O>=0&&d.set(t,O),O},"depthOf"),f=[],l=[];for(const[t,r]of o.entries()){const y=u(r.entityId,new Set);if(y<0){p.add(r.entityId);continue}(r.play==="leave"?l:f).push({id:r.entityId,depth:y,index:t})}return f.sort((t,r)=>t.depth-r.depth||t.index-r.index),l.sort((t,r)=>r.depth-t.depth||t.index-r.index),Object.freeze({order:Object.freeze([...f,...l].map(({id:t})=>t)),cycle:Object.freeze([...p])})}a(R,"revealOrder");function $(o,i){let d=0;for(;d<o.length&&i(o[d]);)d+=1;return o.slice(0,d)}a($,"resolvedPrefix");
@@ -1 +1 @@
1
- "use strict";var m=Object.defineProperty;var u=(l,t)=>m(l,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.SendLine=void 0;const O=require("../faults/classify.js"),T=require("./owned-effects.js"),E=require("./retry-clock.js"),a=require("./send-batch.js"),I=require("./send-ahead.js"),g=require("./send-preparation.js"),S=Object.freeze({progressed:!1,failure:null}),_=Object.freeze({progressed:!0,failure:null}),R=u(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");function b(l){if(l instanceof a.EntityRecordRefusedError)return l.code!==a.ENTITY_RECORD_OVER_STORAGE_LIMIT;const t=(0,O.classifyFault)(l);return t.kind==="refusal"||t.kind==="need"&&t.need.subject?.type!=="authority"}u(b,"aboutOneRow");class k{static{u(this,"SendLine")}#e;#p;#s;#n;#_;#i=new T.OwnedEffects(R);#T;#a;#c=null;#R=!1;#o=!1;#h=!1;#r=new Map;#t=null;#u=null;#y=0;#E=!1;#m=0;#I=[];#S=null;#l=null;#O=new Set;#d=0;#f=0;#b=0;#g=0;constructor(t){this.#e=t.store,this.#p=t.authority,this.#T=t.onStamped,this.#a=t.onError,this.#s=new g.SendPreparation(t.cargo,t.onError),this.#n=new E.RetryClock(t.schedule),this.#_=new E.RetryClock(t.schedule)}get stamped(){return this.#b}get uncertainThrough(){return this.#R?this.#d:void 0}get paused(){return this.#t?.error??null}get held(){return this.#t?this.#t.error:this.#r.size>0?this.#D():null}async waits(t){if(this.#t)return this.#t.error;const e=this.#u;if(!e)return null;const s=await this.#i.run(()=>this.#e.readPlace(t)),h={type:s?.type??"folder",parentUUID:s?.parentUUID??null,name:""};return await e.behind({entityId:t,facets:[],result:h})?this.held:null}start(){if(!this.#c){const t=u(()=>this.resume(),"resume");this.#I=[this.#p.onStorageChanged(t),this.#p.onDisconnect(t)]}return this.#c??=this.#i.run(()=>this.#e.readLocalSequence()).then(t=>{this.#d=this.#f=t,this.#R=!0,this.wake()}),this.#c}resume(){this.#m+=1,this.#E=!1,this.#t&&(this.#t=null,this.#w(),this.wake())}wake(){this.#o||(this.#h=!0,this.#k())}async settle(){if(!this.#c)throw new Error("entity record Send has not started");const t=this.#i.run(()=>this.#e.readLocalSequence()),e=t.catch(()=>0);this.#O.add(e);try{const[,s]=await Promise.all([this.#c,t]);this.#r.size>0&&this.#P();let h=null,c=!1;for(;this.#g<s;){const[o]=await this.#i.run(()=>this.#e.readPending(1));if(!o||o.localSequence>s)break;if(o.localSequence<=this.#g)continue;if(h)throw h.error;if(this.#t)throw this.#t.error;if(c)throw this.#D();const i=this.#l,n=await(i??this.#q());h=n.failure,c=!i&&!n.progressed&&this.#r.size>0}return s}finally{this.#O.delete(e),this.wake()}}async stop(){this.#o=!0,this.#n.cancel(),this.#_.cancel();for(const t of this.#I)t();await Promise.allSettled([this.#c,this.#S,this.#l,this.#s.stop(),this.#i.stop()])}#k(){if(!this.#R||this.#o||this.#n.armed||this.#S)return;const t=this.#A();this.#S=t,t.then(()=>{this.#S=null,this.#h&&this.#k()})}async#A(){for(;!this.#o&&this.#h&&!this.#n.armed;){this.#h=!1;const t=await(this.#l??this.#q());if(t.failure)return;t.progressed&&(this.#h=!0)}}#q(){this.#n.cancel();const t=this.#j();return this.#l=t,t.then(()=>{this.#l===t&&(this.#l=null)}),t}async#L(){const t=this.#u;if(!t)return this.#i.run(()=>this.#e.readPending(g.ENTITY_RECORD_LOOKAHEAD_ROWS));for(;t.wanting;){const e=await this.#i.run(()=>this.#e.readPending(g.ENTITY_RECORD_LOOKAHEAD_ROWS,t.after));if(await t.judge(e),e.length<g.ENTITY_RECORD_LOOKAHEAD_ROWS)break}return t!==this.#u?null:t.reach!==null?(this.#t=null,this.#y=t.reach,this.#E=!0,this.#w(),null):t.ready}async#j(){let t=[],e=0,s;const h=this.#m,c=this.#y;this.#y=0;const o=this.#u;try{const i=await this.#L();if(i===null)return _;const n=await this.#N(),d=i.filter(r=>r.localSequence<=n),y=this.#f;if(this.#s.retain(d,y),s=d[0],!s||(await this.#s.head(s),c>0&&await this.#s.settledThrough(c),this.#o))return S;const p=(0,a.selectBatch)(d,r=>this.#s.prepared(r));await this.#i.run(()=>this.#e.durableThrough(p[p.length-1].row.localSequence)),t=p;const w=await this.#p.submit(t.map(({submission:r})=>r)),f=(0,a.acceptedPrefix)(t.map(({row:r})=>r),w);if(f.length>0&&(await this.#e.stampAccepted(f),e=f.length,o?.stamped(f.map(({row:r})=>r)),this.#M(f,o===null),this.#n.succeeded(),o===null&&this.#_.succeeded(),this.#t||(this.#E=!1)),w.refusal){const r=t[e].row;if(r.localSequence>y||w.refusal.code!==a.ENTITY_RECORD_CARGO_MISSING)throw this.#s.forget([r]),s=r,new a.EntityRecordRefusedError(r.record.mutationId,w.refusal,{id:r.record.entityId,type:r.record.change.result.type});this.#f=r.localSequence-1}return _}catch(i){const n=i instanceof a.EntityRecordRefusedError;if(!n&&t.length>e&&(this.#d=Math.max(this.#d,t[t.length-1].row.localSequence)),this.#f=this.#d,n||(this.#y||=c),this.#o)return{progressed:e>0,failure:{error:i}};if(s&&(n||t.length===0)&&(b(i)||n&&this.#t))return await this.#C(s,i),this.#a(i),_;if(n&&i.code===a.ENTITY_RECORD_OVER_STORAGE_LIMIT){const d=this.#m===h;return d&&(this.#t={error:i,row:s},this.#w(),this.#s.retain([],this.#f)),this.#h=!0,d&&c>0||this.#a(i),{progressed:e>0,failure:{error:i}}}return this.#n.arm(()=>this.wake()),this.#a(i),{progressed:e>0,failure:{error:i}}}}async#C(t,e){this.#r.set(t.localSequence,e),this.#s.forget([t]);const s=this.#u;try{s?await s.stepAside(t):this.#w()}catch(h){this.#a(h),this.#w()}this.#_.arm(()=>this.#P()),this.#h=!0}#P(){this.#r.clear(),this.#w(),this.#f=this.#d,this.wake()}#w(){const t=this.#t;this.#u=this.#r.size>0||t?new I.AheadScan({waiting:this.#r,pause:t&&{row:t.row,reached:this.#E},readPage:u(e=>this.#i.run(()=>this.#e.readPackagePage(e)),"readPage"),readPlace:u(e=>this.#i.run(()=>this.#e.readPlace(e)),"readPlace")}):null}#D(){return this.#r.get(Math.min(...this.#r.keys()))??R()}async#N(){for(const t of[...this.#O]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#M(t,e){this.#b+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#s.forget(t.map(({row:s})=>s));try{this.#T(t)}catch(s){this.#a(s)}}}exports.SendLine=k;
1
+ "use strict";var R=Object.defineProperty;var u=(l,t)=>R(l,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.SendLine=void 0;const O=require("../faults/classify.js"),T=require("./owned-effects.js"),E=require("./retry-clock.js"),a=require("./send-batch.js"),I=require("./send-ahead.js"),g=require("./send-preparation.js"),S=Object.freeze({progressed:!1,failure:null}),y=Object.freeze({progressed:!0,failure:null}),m=u(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");function b(l){if(l instanceof a.EntityRecordRefusedError)return l.code!==a.ENTITY_RECORD_OVER_STORAGE_LIMIT;const t=(0,O.classifyFault)(l);return t.kind==="refusal"||t.kind==="need"&&t.need.subject?.type!=="authority"}u(b,"aboutOneRow");class k{static{u(this,"SendLine")}#e;#p;#s;#n;#y;#i=new T.OwnedEffects(m);#T;#a;#o=null;#m=!1;#c=!1;#h=!1;#r=new Map;#t=null;#u=null;#_=0;#E=!1;#R=0;#I=[];#S=null;#l=null;#O=new Set;#d=0;#f=0;#b=0;#g=0;constructor(t){this.#e=t.store,this.#p=t.authority,this.#T=t.onStamped,this.#a=t.onError,this.#s=new g.SendPreparation(t.cargo,t.onError),this.#n=new E.RetryClock(t.schedule),this.#y=new E.RetryClock(t.schedule)}get stamped(){return this.#b}get uncertainThrough(){return this.#m?this.#d:void 0}get paused(){return this.#t?.error??null}get held(){return this.#t?this.#t.error:this.#r.size>0?this.#D():null}async waits(t){if(this.#t)return this.#t.error;const e=this.#u;if(!e)return null;const s=await this.#i.run(()=>this.#e.readPlace(t)),h={type:s?.type??"folder",parentUUID:s?.parentUUID??null,name:"",gitHomeEntity:s?.gitHomeEntity??null};return await e.behind({entityId:t,facets:[],result:h})?this.held:null}start(){if(!this.#o){const t=u(()=>this.resume(),"resume");this.#I=[this.#p.onStorageChanged(t),this.#p.onDisconnect(t)]}return this.#o??=this.#i.run(()=>this.#e.readLocalSequence()).then(t=>{this.#d=this.#f=t,this.#m=!0,this.wake()}),this.#o}resume(){this.#R+=1,this.#E=!1,this.#t&&(this.#t=null,this.#w(),this.wake())}wake(){this.#c||(this.#h=!0,this.#k())}async settle(){if(!this.#o)throw new Error("entity record Send has not started");const t=this.#i.run(()=>this.#e.readLocalSequence()),e=t.catch(()=>0);this.#O.add(e);try{const[,s]=await Promise.all([this.#o,t]);this.#r.size>0&&this.#P();let h=null,o=!1;for(;this.#g<s;){const[c]=await this.#i.run(()=>this.#e.readPending(1));if(!c||c.localSequence>s)break;if(c.localSequence<=this.#g)continue;if(h)throw h.error;if(this.#t)throw this.#t.error;if(o)throw this.#D();const i=this.#l,n=await(i??this.#q());h=n.failure,o=!i&&!n.progressed&&this.#r.size>0}return s}finally{this.#O.delete(e),this.wake()}}async stop(){this.#c=!0,this.#n.cancel(),this.#y.cancel();for(const t of this.#I)t();await Promise.allSettled([this.#o,this.#S,this.#l,this.#s.stop(),this.#i.stop()])}#k(){if(!this.#m||this.#c||this.#n.armed||this.#S)return;const t=this.#A();this.#S=t,t.then(()=>{this.#S=null,this.#h&&this.#k()})}async#A(){for(;!this.#c&&this.#h&&!this.#n.armed;){this.#h=!1;const t=await(this.#l??this.#q());if(t.failure)return;t.progressed&&(this.#h=!0)}}#q(){this.#n.cancel();const t=this.#j();return this.#l=t,t.then(()=>{this.#l===t&&(this.#l=null)}),t}async#L(){const t=this.#u;if(!t)return this.#i.run(()=>this.#e.readPending(g.ENTITY_RECORD_LOOKAHEAD_ROWS));for(;t.wanting;){const e=await this.#i.run(()=>this.#e.readPending(g.ENTITY_RECORD_LOOKAHEAD_ROWS,t.after));if(await t.judge(e),e.length<g.ENTITY_RECORD_LOOKAHEAD_ROWS)break}return t!==this.#u?null:t.reach!==null?(this.#t=null,this.#_=t.reach,this.#E=!0,this.#w(),null):t.ready}async#j(){let t=[],e=0,s;const h=this.#R,o=this.#_;this.#_=0;const c=this.#u;try{const i=await this.#L();if(i===null)return y;const n=await this.#N(),d=i.filter(r=>r.localSequence<=n),_=this.#f;if(this.#s.retain(d,_),s=d[0],!s||(await this.#s.head(s),o>0&&await this.#s.settledThrough(o),this.#c))return S;const p=(0,a.selectBatch)(d,r=>this.#s.prepared(r));await this.#i.run(()=>this.#e.durableThrough(p[p.length-1].row.localSequence)),t=p;const w=await this.#p.submit(t.map(({submission:r})=>r)),f=(0,a.acceptedPrefix)(t.map(({row:r})=>r),w);if(f.length>0&&(await this.#e.stampAccepted(f),e=f.length,c?.stamped(f.map(({row:r})=>r)),this.#M(f,c===null),this.#n.succeeded(),c===null&&this.#y.succeeded(),this.#t||(this.#E=!1)),w.refusal){const r=t[e].row;if(r.localSequence>_||w.refusal.code!==a.ENTITY_RECORD_CARGO_MISSING)throw this.#s.forget([r]),s=r,new a.EntityRecordRefusedError(r.record.mutationId,w.refusal,{id:r.record.entityId,type:r.record.change.result.type});this.#f=r.localSequence-1}return y}catch(i){const n=i instanceof a.EntityRecordRefusedError;if(!n&&t.length>e&&(this.#d=Math.max(this.#d,t[t.length-1].row.localSequence)),this.#f=this.#d,n||(this.#_||=o),this.#c)return{progressed:e>0,failure:{error:i}};if(s&&(n||t.length===0)&&(b(i)||n&&this.#t))return await this.#C(s,i),this.#a(i),y;if(n&&i.code===a.ENTITY_RECORD_OVER_STORAGE_LIMIT){const d=this.#R===h;return d&&(this.#t={error:i,row:s},this.#w(),this.#s.retain([],this.#f)),this.#h=!0,d&&o>0||this.#a(i),{progressed:e>0,failure:{error:i}}}return this.#n.arm(()=>this.wake()),this.#a(i),{progressed:e>0,failure:{error:i}}}}async#C(t,e){this.#r.set(t.localSequence,e),this.#s.forget([t]);const s=this.#u;try{s?await s.stepAside(t):this.#w()}catch(h){this.#a(h),this.#w()}this.#y.arm(()=>this.#P()),this.#h=!0}#P(){this.#r.clear(),this.#w(),this.#f=this.#d,this.wake()}#w(){const t=this.#t;this.#u=this.#r.size>0||t?new I.AheadScan({waiting:this.#r,pause:t&&{row:t.row,reached:this.#E},readPage:u(e=>this.#i.run(()=>this.#e.readPackagePage(e)),"readPage"),readPlace:u(e=>this.#i.run(()=>this.#e.readPlace(e)),"readPlace")}):null}#D(){return this.#r.get(Math.min(...this.#r.keys()))??m()}async#N(){for(const t of[...this.#O]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#M(t,e){this.#b+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#s.forget(t.map(({row:s})=>s));try{this.#T(t)}catch(s){this.#a(s)}}}exports.SendLine=k;
@@ -1 +1 @@
1
- "use strict";var u=Object.defineProperty;var a=(n,t)=>u(n,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.Waits=void 0;const l=new Set(["create","rename","move","restore"]),h=new Set(["move","restore","delete"]),o=a((n,t)=>n===null?null:`${n}\0${t}`,"slot");class p{static{a(this,"Waits")}#r;#e=new Map;#t=new Set;#i=new Set;#n=new Set;#a=new Set;#l=!1;#s=!1;constructor(t){this.#r=t}get empty(){return this.#t.size===0}async hold(t,i=[]){this.#t.add(t.entityId),this.#o(t.result.parentUUID,t.result.name,t.result.type),t.facets.includes("rename")&&t.result.parentUUID!==null&&this.#a.add(t.result.parentUUID),t.facets.includes("move")&&(this.#l=!0);for(const s of i)this.#t.add(s.uuid),this.#o(s.parentUUID,s.name,s.type);let e=t.result.type==="repo.git"?t.entityId:null;for(const s of await this.#u(t.result.parentUUID))this.#i.add(s),e===null&&(await this.#h(s))?.type==="repo.git"&&(e=s);e!==null&&(this.#t.add(e),this.#s=!0)}async behind(t){if(this.empty)return!1;const{entityId:i,facets:r,result:e}=t;if(this.#t.has(i)||r.some(s=>l.has(s))&&e.parentUUID!==null&&(this.#n.has(o(e.parentUUID,e.name))||this.#a.has(e.parentUUID))||this.#l&&r.some(s=>l.has(s)||s==="delete")||this.#s&&e.type==="repo.git"||this.#i.has(i)&&r.some(s=>h.has(s)))return!0;for(const s of await this.#u(e.parentUUID))if(this.#t.has(s))return!0;return!1}#o(t,i,r){const e=o(t,i);e!==null&&this.#n.add(e),r==="repo.git"&&(this.#s=!0)}async#u(t){const i=[];for(let r=t;r!==null&&!i.includes(r);){i.push(r);const e=await this.#h(r);if(!e)break;r=e.parentUUID}return i}async#h(t){return this.#e.has(t)||this.#e.set(t,await this.#r(t)),this.#e.get(t)}}exports.Waits=p;
1
+ "use strict";var u=Object.defineProperty;var a=(n,t)=>u(n,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.Waits=void 0;const l=new Set(["create","rename","move","restore"]),h=new Set(["move","restore","delete"]),o=a((n,t)=>n===null?null:`${n}\0${t}`,"slot");class d{static{a(this,"Waits")}#s;#e=new Map;#t=new Set;#i=new Set;#r=new Set;#n=new Set;#a=!1;constructor(t){this.#s=t}get empty(){return this.#t.size===0}async hold(t,r=[]){this.#t.add(t.entityId),this.#l(t.result.parentUUID,t.result.name),t.facets.includes("rename")&&t.result.parentUUID!==null&&this.#n.add(t.result.parentUUID),t.facets.includes("move")&&(this.#a=!0);for(const e of r)this.#t.add(e.uuid),this.#l(e.parentUUID,e.name);let s=t.result.type==="repo.git"?t.entityId:null;for(const e of await this.#o(t.result.parentUUID))this.#i.add(e),s===null&&(await this.#u(e))?.type==="repo.git"&&(s=e);s!==null&&this.#t.add(s)}async behind(t){if(this.empty)return!1;const{entityId:r,facets:i,result:s}=t;if(this.#t.has(r)||i.some(e=>l.has(e))&&s.parentUUID!==null&&(this.#r.has(o(s.parentUUID,s.name))||this.#n.has(s.parentUUID))||this.#a&&i.some(e=>l.has(e)||e==="delete")||s.gitHomeEntity!=null&&this.#t.has(s.gitHomeEntity)||this.#i.has(r)&&i.some(e=>h.has(e)))return!0;for(const e of await this.#o(s.parentUUID))if(this.#t.has(e))return!0;return!1}#l(t,r){const i=o(t,r);i!==null&&this.#r.add(i)}async#o(t){const r=[];for(let i=t;i!==null&&!r.includes(i);){r.push(i);const s=await this.#u(i);if(!s)break;i=s.parentUUID}return r}async#u(t){return this.#e.has(t)||this.#e.set(t,await this.#s(t)),this.#e.get(t)}}exports.Waits=d;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amalgm/live",
3
- "version": "0.2.81-preview.47413265404",
3
+ "version": "0.2.81-preview.47428104047",
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": "98fb07b41d439be6758938cbc7162d8e15a993b7",
136
+ "gitHead": "6269805c0b1e5a5bc27e0ba7c151bdaba742fa96",
137
137
  "amalgmSource": {
138
138
  "repository": "amalgm-inc/amalgm-live",
139
139
  "branch": "preview",
140
- "commit": "98fb07b41d439be6758938cbc7162d8e15a993b7",
141
- "occurrence": "47413265404"
140
+ "commit": "6269805c0b1e5a5bc27e0ba7c151bdaba742fa96",
141
+ "occurrence": "47428104047"
142
142
  }
143
143
  }