@amalgm/live 0.2.81-main.46383070700 → 0.2.81-main.46397184323
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/apply/journey.d.ts +3 -0
- package/dist/apply/journey.js +1 -1
- package/dist/apply/lane.d.ts +1 -0
- package/dist/apply/lane.js +1 -1
- package/dist/apply/rail.d.ts +1 -0
- package/dist/apply/rail.js +1 -1
- package/dist/apply/settling.d.ts +2 -1
- package/dist/apply/settling.js +1 -1
- package/dist/apply/window.js +1 -1
- package/dist/entities/cloud.d.ts +3 -1
- package/dist/entities/cloud.js +1 -1
- package/dist/files/commands.d.ts +2 -0
- package/dist/files/commands.js +1 -1
- package/dist/records/receive-loop.js +1 -1
- package/dist/registration/user-ground.d.ts +1 -0
- package/dist/registration/user-ground.js +1 -1
- package/dist-cjs/apply/journey.js +1 -1
- package/dist-cjs/apply/lane.js +1 -1
- package/dist-cjs/apply/rail.js +1 -1
- package/dist-cjs/apply/settling.js +1 -1
- package/dist-cjs/apply/window.js +1 -1
- package/dist-cjs/entities/cloud.js +1 -1
- package/dist-cjs/files/commands.js +1 -1
- package/dist-cjs/records/receive-loop.js +1 -1
- package/dist-cjs/registration/user-ground.js +1 -1
- package/package.json +5 -5
package/dist/apply/journey.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { type ApplyBlocker, type ApplyDependency, type ApplyWindow, type EntityApplyRailOptions } from './contracts.js';
|
|
2
2
|
export type HeadOutcome = 'progress' | 'again' | {
|
|
3
|
+
readonly kind: 'alone';
|
|
4
|
+
readonly entityId: string;
|
|
5
|
+
} | {
|
|
3
6
|
readonly kind: 'park';
|
|
4
7
|
readonly reason: string;
|
|
5
8
|
readonly releaseOnProgress: boolean;
|
package/dist/apply/journey.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var F=Object.defineProperty;var w=(l,e)=>F(l,"name",{value:e,configurable:!0});import{createApplyIntent as M,createApplyWindowIntent as x}from"./contracts.js";import{planSelectedEntityApply as E}from"./plans.js";import{Recovering as W}from"../faults/recover.js";import{groundMoved as O}from"../faults/classify.js";import{localWorkWins as D}from"../clash/local-work.js";import{selectEntityApply as j}from"./selection.js";import{isSettlement as P}from"./settling.js";import{revealOrder as S}from"./window.js";const q=w(l=>l.kind==="captured-local"||l.kind==="rejudge","drops");function v(l){return l.kind==="retry"?{kind:"park",reason:l.reason??"machine truth is unsettled",releaseOnProgress:!1,report:!1}:l.kind==="dependency"?{kind:"park",reason:l.reason,releaseOnProgress:l.waitFor===void 0,report:!1,...l.waitFor?{dependency:l.waitFor}:{},...l.blocker?{blocker:l.blocker}:{},..."comparedAt"in l?{comparedAt:l.comparedAt}:{}}:l.kind==="conflict"?{kind:"park",reason:l.reason,releaseOnProgress:!0,report:!0}:null}w(v,"parkFor");async function _(l){try{return await l()}catch(e){return{kind:"park",reason:e instanceof Error?e.message:String(e),releaseOnProgress:!1,report:!O(e),cause:e}}}w(_,"outcomeOf");const I=w(l=>P(l.judgment)?{kind:"settled",settlement:l.judgment}:null,"settled");class B{static{w(this,"EntityApplyJourney")}#e;#a;#r;#n;constructor(e,t,i){this.#e=e,this.#a={sha256Hex:e.sha256Hex,loops:e.loops},this.#r=t,this.#n=i}targetOf(e){const t=e.members[0];return t===void 0?null:e.intent?.members.find(i=>i.entityId===t.work.entityId)?.throughSequence??(t.work.kept===null?t.work.records.at(-1)?.globalSequence:t.work.resolvedThrough)??null}async run(e,t){if(e.intent!==null)return this.#f(e,e.intent);const i={member:null};try{return await this.#c(e,t,i)}catch(s){if(i.member===null||this.#n()||O(s))throw s;if(e.unit===null)return this.#d(i.member);if(s instanceof W)return{kind:"park",reason:s.message,releaseOnProgress:!1,report:!1,dependency:{producer:"capability",entityIds:s.need.subject===null?[]:[s.need.subject.id]}};throw s}}async#d(e){const t=await this.#t(null,[e],[{kind:"settled",settlement:"waiting"}]);return t==="progress"?{kind:"alone",entityId:e.work.entityId}:t}async#c(e,t,i){const s=this.#e.newIntentId(),n=this.#e.now(),o=t(),a=new Map;for(const d of e.members){if(I(d))continue;const{work:f}=d,m=j(f,this.#a),b=M({attemptId:s,work:f,target:m.target,unit:e.unit,cutoff:e.cutoff,createdAt:n,emptied:f.entityId===o&&f.play==="enter"&&(m.target.result.type==="file.text"||m.target.result.type==="file.binary")}),h=E(f,m,this.#e.sha256Hex);a.set(f.entityId,{member:d,intent:b,plan:h,adapter:this.#e.effects.adapterFor(h.target.type),look:"safe"})}const{active:c,blocked:y}=await this.#o(e.members,a,"look",i);if(c.length===0)return y??"again";const g=c.flatMap(d=>a.get(d.work.entityId)??[]);if(g.length===0)return this.#t(null,c,c.map(d=>I(d)));const k=x({attemptId:s,authorityId:e.authorityId,unit:e.unit,cutoff:e.cutoff,createdAt:n,members:g.map(d=>d.intent)});try{const d=await this.#p(c,a,i),f=d.active.flatMap(r=>a.get(r.work.entityId)??[]);if(f.length===0)return await this.#e.effects.discard(k),d.active.length===0?d.blocked??"again":this.#t(null,d.active,d.active.map(r=>I(r)));const m=f.length===g.length?k:x({...k,members:f.map(r=>r.intent)}),b=await this.#e.effects.hold(m,f.map(r=>r.plan));let h,A;try{h=await this.#o(d.active,a,"final",i),A=await this.#u(m,h,a,t)}finally{b()}return h.unseen!==null&&h.active.length===0?this.#k(h.unseen):A}catch(d){throw await this.#e.effects.discard(k),d}}async#u(e,{active:t,blocked:i},s,n){const o=t.flatMap(r=>s.get(r.work.entityId)??[]);if(o.length===0)return await this.#e.effects.discard(e),t.length===0?i??"again":this.#t(null,t,t.map(r=>I(r)));const a=x({...e,members:o.map(r=>r.intent)}),c=o.filter(r=>r.look==="safe");if(c.length>0){if(this.#n()||o.some(p=>p.intent.emptied&&n()!==p.intent.entityId))return await this.#e.effects.discard(e),"again";const r=await this.#e.store.bindWindow(a);if(r)return await this.#e.effects.discard(e),r.kind==="stale"?"again":v({...r,comparedAt:this.#r()})}const y=new Map,g=new Map,k=w((r,p,u=null)=>{y.set(r,{kind:"owed",reason:p}),u!==null&&g.set(r,u)},"owe"),d=w(r=>v(r.kind==="dependency"?{...r,comparedAt:this.#r()}:r),"waitFrom"),{order:f,cycle:m}=S(c.map(r=>({entityId:r.intent.entityId,play:r.plan.play,type:r.plan.target.type,parentUUID:r.plan.target.parentUUID}))),b="its parent chain closes among the window's members";for(const r of m)k(r,b,{kind:"park",reason:b,releaseOnProgress:!1,report:!0});for(const r of f){const p=s.get(r),u=await p.adapter.apply.reveal(p.intent,p.plan);u&&k(r,u.reason,u.kind==="dependency"?d(u):null)}for(const r of o){const p=r.intent.entityId;if(y.has(p))continue;const u=await r.adapter.verify.verify(r.intent,r.plan);u.kind==="match"?y.set(p,{kind:"materialized"}):u.kind==="local-change"?k(p,"a person's version stands in its place"):u.kind==="rejudge"||u.kind==="missing-target"?k(p,u.kind):k(p,u.reason??u.kind,d(u))}const h=t.map(r=>I(r)??y.get(r.work.entityId));if(c.length>0)return this.#s(a,t,h,g);const A=await this.#s(null,t,h,g);return await this.#e.effects.discard(e),A}async#s(e,t,i,s){const n=await this.#t(e,t,i),o=n==="progress"?s.get(t[0].work.entityId):void 0;return o===void 0?n:{...o,advanced:t.filter((a,c)=>i[c].kind!=="owed").map(a=>a.work.entityId)}}async#o(e,t,i,s){const n=[];for(const o of e){const a=t.get(o.work.entityId);if(!a){n.push(o);continue}let c;try{c=await this.#i(a.intent)?{kind:"captured-local"}:i==="look"?await this.#h(a):await a.adapter.apply.guard(a.intent,a.plan,i)}catch(g){if(n.length>0&&!this.#n())return{active:n,blocked:null,unseen:null};throw s.member=o,g}if(c.kind==="local-change"||c.kind==="unsettled-ground")return{active:n,blocked:n.length===0?"again":null,unseen:a};if(q(c))return{active:n,blocked:n.length===0?"again":null,unseen:null};const y=v(c);if(y)return{active:n,blocked:y,unseen:null};a.look=c.kind,n.push(o)}return{active:n,blocked:null,unseen:null}}async#p(e,t,i){for(const[s,n]of e.entries()){const o=t.get(n.work.entityId);if(!(!o||o.look!=="safe"))try{const a=await o.adapter.apply.prepare(o.intent,o.plan);if(a)return{active:e.slice(0,s),blocked:v(a)}}catch(a){if(s>0&&!this.#n())return{active:e.slice(0,s),blocked:null};throw i.member=n,a}}return{active:[...e],blocked:null}}async#f(e,t){const i=t.members.map(n=>{const o=e.members.find(c=>c.work.entityId===n.entityId);if(!o)return null;const a=n.kept===null?o.work:{...o.work,records:[],kept:n.kept};return a.records.length===0&&a.kept===null?null:E(a,j(a,this.#a,n),this.#e.sha256Hex)}),s=await this.#e.effects.recover(t,i);if(s.length!==t.members.length)throw new Error("Apply recovery answered for a different window");return this.#t(t,e.members,e.members.map(n=>I(n)??s[t.members.findIndex(o=>o.entityId===n.work.entityId)]??{kind:"owed",reason:"not a member of the bound window"}))}async#t(e,t,i){const s=await this.#e.store.endWindow(e,t,i);if(!s)return"progress";if(s.kind!=="stale"||!s.reason)throw new Error("Apply end commit returned an unknown refusal");return"again"}async#i(e){return D(e.targetType,await this.#e.store.localWorkAfter(e.authorityId,e.entityId,e.kept??e.throughSequence))}async#h(e){const{intent:t,plan:i,adapter:s}=e,n=await s.apply.guard(t,i,"look");if(n.kind!=="local-change"&&n.kind!=="unsettled-ground")return n;const o=await this.#l(t,i);if(o.kind!=="observed")return o;const a=await s.apply.guard(t,i,"look");return a.kind==="local-change"||a.kind==="unsettled-ground"?{kind:"retry",reason:"Ground still needs observation after Detect; this member cannot reveal."}:a}async#k(e){const t=await this.#l(e.intent,e.plan);return t.kind==="captured-local"?"again":v(t.kind==="observed"?{kind:"retry",reason:"Its final guard found ground Detect had not recorded; it is judged again after Detect looks."}:t)}async#l(e,t){if(await this.#i(e))return{kind:"captured-local"};let i,s=!0;const n=await this.#e.effects.captureLocal(e,t,()=>{if(!s||i!==void 0)throw new Error("Apply capture comparison must be marked exactly once");i=this.#r()}).finally(()=>{s=!1});if(!n||!["settled","local-only","retry"].includes(n.kind))throw new Error("Apply capture returned no explicit observation outcome");if(await this.#i(e))return{kind:"captured-local"};if(n.kind==="retry")return n;if(n.kind==="local-only"){if(i===void 0)throw new Error("local-only capture requires a synchronous comparison fence");if(!n.reason||n.entityIds.length===0||n.entityIds.some(o=>!o))throw new Error("local-only capture must name the retained ground and its releasing condition");return{kind:"dependency",reason:n.reason,comparedAt:i,waitFor:{producer:"detect",entityIds:n.entityIds}}}return{kind:"observed"}}}export{B as EntityApplyJourney,_ as outcomeOf};
|
package/dist/apply/lane.d.ts
CHANGED
package/dist/apply/lane.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var l=Object.defineProperty;var o=(n,e)=>l(n,"name",{value:e,configurable:!0});import{UUID_PATTERN as a}from"../entities/types.js";import{assertApplyWindow as u,invalidStep as t}from"./line.js";function d(n,e,r){return{kind:"lane",unit:n,emptied:r,through:e,waitsOn:null,met:new Set,waiters:new Set,held:null,queued:!1,running:!1,again:!1}}o(d,"createApplyLane");function
|
|
1
|
+
var l=Object.defineProperty;var o=(n,e)=>l(n,"name",{value:e,configurable:!0});import{UUID_PATTERN as a}from"../entities/types.js";import{assertApplyWindow as u,invalidStep as t}from"./line.js";function d(n,e,r){return{kind:"lane",unit:n,emptied:r,through:e,toPrefix:!1,waitsOn:null,met:new Set,waiters:new Set,held:null,queued:!1,running:!1,again:!1}}o(d,"createApplyLane");function s(n,e){const r=`lane step for ${n}`;switch((!e||typeof e!="object"||!Number.isSafeInteger(e.through)||e.through<0)&&t(r),e.kind){case"end":return;case"nowhere":case"root":(typeof e.reason!="string"||e.reason.length===0)&&t(r);return;case"prerequisite":(!a.test(e.unit)||e.unit===n||typeof e.reason!="string"||e.reason.length===0)&&t(r);return;case"window":(e.window?.unit!==n||e.window.cutoff!==e.through||e.window.intent!==null&&(e.window.intent.unit!==n||e.window.intent.cutoff!==e.through))&&t(r),u(e.window,r),e.window.members.some(i=>i.work.kept!==null)&&f(e.window,r);return;default:t(r)}}o(s,"assertApplyLaneStep");function f(n,e){const[r]=n.members;(n.members.length!==1||r.work.play!=="enter"||r.work.records.length!==0||r.work.materialized!==null||r.deliverySequence!==n.cutoff||n.intent!==null&&n.intent.members.some(i=>i.kept===null))&&t(e)}o(f,"assertKeptWindow");export{s as assertApplyLaneStep,d as createApplyLane};
|
package/dist/apply/rail.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export declare class EntityApplyRail {
|
|
|
16
16
|
localStateChanged(entityIds?: readonly string[]): void;
|
|
17
17
|
sendAccepted(entityIds: readonly string[]): void;
|
|
18
18
|
capabilityChanged(entityIds?: readonly string[]): void;
|
|
19
|
+
owe(entityIds: readonly string[]): void;
|
|
19
20
|
waiting(): readonly ApplyWait[];
|
|
20
21
|
flush(): Promise<void>;
|
|
21
22
|
stop(): Promise<void>;
|
package/dist/apply/rail.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var g=Object.defineProperty;var a=(d,t)=>g(d,"name",{value:t,configurable:!0});import{UUID_PATTERN as f}from"../entities/types.js";import{FetchAhead as m}from"./fetch-ahead.js";import{EntityApplyJourney as A,outcomeOf as I}from"./journey.js";import{assertApplyLaneStep as k,createApplyLane as E}from"./lane.js";import{assertApplyDependency as P,assertApplyLineStep as b,createApplyLine as v,heldWait as c,releaseApplyLine as O}from"./line.js";import{windowEndForWait as $}from"./window.js";const S=8,q=1e3,p=3e4,M=8;class Y{static{a(this,"EntityApplyRail")}#t;#w;#P;#M;#T;#g;#b;#f=!1;#e=!1;#o=new Map;#s=new Map;#a={queue:[],steps:new Set};#d={queue:[],steps:new Set};#r=!1;#n=null;#c=1/0;#m=[];#A=0;#v=new Map;#p=null;constructor(t){this.#t=t,this.#w=t.concurrency??S,this.#P=t.laneConcurrency??this.#w,this.#M=t.onBackgroundError??(()=>{}),this.#T=t.onIdle??(()=>{});for(const[e,s]of[["concurrency",this.#w],["lane concurrency",this.#P]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${e} must be a positive integer`);this.#g=new A(t,()=>this.#A,()=>this.#e),this.#b=new m(t.cargo,{sha256Hex:t.sha256Hex,loops:t.loops})}async start(){if(this.#f)throw new Error("entity Apply rail already started");if(this.#e)throw new Error("entity Apply rail is stopped");this.#f=!0;const t=await this.#t.store.readOpenUnits();if(!Array.isArray(t)||new Set(t).size!==t.length||t.some(e=>!f.test(e)))throw new Error("entity Apply store returned an invalid open unit list");for(const e of t)this.#O(e,null);await this.#_()}fetch(t,e){return this.#f?this.#e?Promise.reject(new Error("entity Apply rail is stopped")):f.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.#O(t,null,e.emptied===!0),n=a(()=>this.#t.schedule.schedule(e.stall,()=>{r.waiters.delete(l)&&(r.emptied&&![...r.waiters].some(o=>o.emptied)&&(r.emptied=!1),s(this.#Q(r)))}),"arm");let h=n();const l={emptied:e.emptied===!0,answer:a(o=>{h.cancel(),s(o)},"answer"),fail:a(o=>{h.cancel(),i(o)},"fail"),moved:a(()=>{h.cancel(),h=n()},"moved")};r.waiters.add(l),r.waitsOn===null&&this.#R(r),this.#h()}):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(!f.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.#u("detect",t),this.#u("apply",t),this.#I()}sendAccepted(t){this.#u("send",t),this.#I()}capabilityChanged(t){this.#u("capability",t),this.#I()}#I(){!this.#f||this.#e||(this.#r=!0,this.#h())}waiting(){return this.#y().flatMap(t=>c(t.held)??[])}async flush(){if(!this.#f)throw new Error("entity Apply rail has not started");this.#e||await this.#_()}async stop(){if(this.#e)return;this.#e=!0,this.#q();for(const e of[this.#a,this.#d])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.#n;)await Promise.allSettled([...this.#a.steps,...this.#d.steps,...this.#n?[this.#n]:[]]);await this.#b.stop();for(const e of this.#m.splice(0))e.resolve()}#_(){return new Promise((t,e)=>{this.#m.push({resolve:t,reject:e}),this.#r=!0,this.#h()})}#h(){if(!this.#e){if(this.#r&&this.#n===null){this.#r=!1;const t=this.#W();this.#n=t,t.then(()=>{this.#n=null,this.#h()})}for(const[t,e]of[[this.#d,this.#P],[this.#a,this.#w]])for(;t.steps.size<e&&t.queue.length>0;){const s=t.queue.shift();s.queued=!1,this.#z(s,t)}if(this.#B()===0&&this.#n===null&&!this.#r&&this.#a.queue.length===0&&this.#d.queue.length===0){if(this.#v.clear(),this.#j(this.#t.now())){this.#h();return}this.#S();for(const t of this.#m.splice(0))t.resolve();this.#T()}}}#B(){return this.#a.steps.size+this.#d.steps.size}#y(){return[...this.#o.values(),...this.#s.values()]}#k(t){return t.kind==="line"?this.#o.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.#a:this.#d).queue.push(t))}}#z(t,e){t.running=!0,t.again=!1;const s=(t.kind==="line"?this.#D(t):this.#F(t)).then(i=>{t.running=!1,e.steps.delete(s),(i||t.again)&&this.#k(t)&&(t.again=!1,this.#i(t)),this.#h()});e.steps.add(s)}async#W(){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.#c=1/0;const e=new Set(t);for(const[s,i]of this.#o)!e.has(s)&&!i.running&&!i.queued&&this.#o.delete(s);for(const s of t){let i=this.#o.get(s);i||(i=v(s),this.#o.set(s,i)),this.#i(i)}}catch(t){this.#c=this.#t.now()+p;const e=this.#m.splice(0);e.length===0&&this.#$(t);for(const s of e)s.reject(t)}}async#D(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,t.endsBefore);if(b(t.authorityId,i),this.#e)return!1;if(i.kind==="end"){t.held=null;const r=t.endsBefore!==null;return t.endsBefore=null,r}return i.window.deliverySequence!==null&&this.#b.advance(t.authorityId,i.window.deliverySequence),await this.#L(t,i.window,s)}catch(e){return this.#Y(t,e),!1}}async#F(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.through);if(k(t.unit,i),this.#e||!this.#k(t))return!1;switch(t.through!==null&&i.through>t.through&&t.met.clear(),t.through=Math.max(t.through??0,i.through),t.waitsOn=null,i.kind!=="window"&&(t.held=null),i.kind){case"end":return this.#l(t,{kind:"placed"}),!1;case"nowhere":return this.#l(t,{kind:"nowhere",reason:i.reason}),!1;case"root":return await this.#J(t,i.through,i.reason);case"prerequisite":return this.#H(t,i.unit,i.reason,s);case"window":return await this.#L(t,i.window,s)}}catch(e){return this.#Y(t,e),!1}}async#J(t,e,s){return t.met.has(t.unit)?(this.#l(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))}#O(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=E(t,e,s),this.#s.set(t,i),this.#i(i)),i}#H(t,e,s,i){if(t.met.has(e))return this.#l(t,{kind:"held",unit:e,reason:`${e} placed and still needed: ${s}`}),!1;if(!this.#x(t,e,s))return!1;const r={producer:"apply",entityIds:[e]};return t.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#C(r,i),reason:s,dependency:r},t.held.due}#R(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.#x(t,s,e.reason))return}}#x(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.#l(n,{kind:"held",unit:e,reason:`prerequisite cycle through ${e}: ${s}`});return!1}return t.waitsOn??=e,this.#i(this.#O(e,t.through)),!0}#l(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.#l(i,s);continue}i.met.add(t.unit)}e.kind==="placed"&&(this.#E(t.unit),this.#u("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()}}#Q(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#L(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.#V(t,e))return!1;const r=await I(()=>this.#g.run(e,()=>t.kind==="lane"&&t.emptied?t.unit:null));if(r==="progress")return this.#X(t,e.members.map(n=>n.work.entityId));if(r==="again")return t.held?.entityId===i&&(t.held=null),!0;if(r.advanced?.length&&this.#N(t,r.advanced),t.kind==="line"){const n=$(e,r.dependency);if(n!==null)return t.endsBefore=n,!0}return this.#G(t,e,r,s),t.kind==="lane"&&this.#R(t),t.held.due}async#V(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.#g.targetOf(e)!==s.target||e.cutoff!==s.cutoff?!1:await this.#t.store.localWorkAfter(e.authorityId,i.work.entityId,s.kept??s.target)===null}#X(t,e){return t.held&&(t.held.entityId===null||e.includes(t.held.entityId))&&(t.held=null),this.#N(t,e),!0}#N(t,e){t.kind==="lane"&&(this.#r=!0,this.#E(t.unit)),this.#u("apply",e);for(const s of this.#y())s!==t&&s.held?.releaseOnProgress&&!s.held.due&&(s.held.due=!0,this.#i(s))}#G(t,e,s,i){if(s.dependency&&P(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=(n?.entityId===r?n.attempt:0)+1,l=this.#U(h),o={entityId:r,target:this.#g.targetOf(e),kept:e.members[0].work.kept,cutoff:e.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#t.now()+l,releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#C(s.dependency,s.comparedAt??i):!1,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};t.held=o,this.#S();const w=c(n),u=c(o);u&&JSON.stringify(u)!==JSON.stringify(w)&&this.#t.onWaiting?.(u),!(!s.report&&h<M)&&this.#$(new Error(`${y(t)} holds at ${r}, which ${s.releaseOnProgress?"is parked on a conflict":s.report?"failed":"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.#U(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.#S(),this.#$(new Error(`${y(t)} faulted and will retry in ${r}ms (attempt ${i}): ${n}`,{cause:e}))}#$(t){try{this.#M(t)}catch{}}#U(t){const e=Math.min(p,q*2**Math.min(t-1,16)),s=Math.max(0,Math.min(1,this.#t.schedule.random()));return Math.round(e*(.5+s))}#u(t,e){const s=++this.#A;for(const i of e??["*"])this.#v.set(`${t}:${i}`,s);for(const i of this.#y())O(i,t,e)&&this.#i(i)}#C(t,e){return["*",...t.entityIds].some(s=>(this.#v.get(`${t.producer}:${s}`)??0)>e)}#S(){if(this.#e)return;let t=this.#c;for(const i of this.#y())i.held&&!i.held.due&&(t=Math.min(t,i.held.notBefore));if(t===1/0){this.#q();return}if(this.#p?.dueAt===t)return;this.#q();const e=Math.max(0,t-this.#t.now()),s=this.#t.schedule.schedule(e,()=>{this.#p=null,this.#j(t),this.#h()});this.#p={dueAt:t,cancel:a(()=>s.cancel(),"cancel")}}#j(t){let e=!1;for(const s of this.#y())s.held&&!s.held.due&&s.held.notBefore<=t&&(s.held.due=!0,this.#i(s),e=!0);return this.#c<=t&&(this.#c=1/0,this.#r=!0,e=!0),e}#q(){this.#p?.cancel(),this.#p=null}}const y=a(d=>d.kind==="line"?`entity Apply line ${d.authorityId}`:`entity Apply fetch of ${d.unit}`,"label");export{S as ENTITY_APPLY_DEFAULT_CONCURRENCY,M as ENTITY_APPLY_QUIET_ATTEMPTS,q as ENTITY_APPLY_RETRY_BASE_MS,p as ENTITY_APPLY_RETRY_MAX_MS,Y as EntityApplyRail};
|
|
1
|
+
var g=Object.defineProperty;var a=(d,t)=>g(d,"name",{value:t,configurable:!0});import{UUID_PATTERN as u}from"../entities/types.js";import{FetchAhead as m}from"./fetch-ahead.js";import{EntityApplyJourney as A,outcomeOf as I}from"./journey.js";import{assertApplyLaneStep as k,createApplyLane as E}from"./lane.js";import{assertApplyDependency as P,assertApplyLineStep as b,createApplyLine as v,heldWait as c,releaseApplyLine as O}from"./line.js";import{windowEndForWait as $}from"./window.js";const S=8,q=1e3,p=3e4,M=8;class U{static{a(this,"EntityApplyRail")}#t;#w;#b;#M;#T;#g;#v;#o=!1;#e=!1;#a=new Map;#s=new Map;#d={queue:[],steps:new Set};#l={queue:[],steps:new Set};#n=!1;#h=null;#c=1/0;#m=[];#A=0;#O=new Map;#p=null;constructor(t){this.#t=t,this.#w=t.concurrency??S,this.#b=t.laneConcurrency??this.#w,this.#M=t.onBackgroundError??(()=>{}),this.#T=t.onIdle??(()=>{});for(const[e,s]of[["concurrency",this.#w],["lane concurrency",this.#b]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${e} must be a positive integer`);this.#g=new A(t,()=>this.#A,()=>this.#e),this.#v=new m(t.cargo,{sha256Hex:t.sha256Hex,loops:t.loops})}async start(){if(this.#o)throw new Error("entity Apply rail already started");if(this.#e)throw new Error("entity Apply rail is stopped");this.#o=!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.#E(e,null);await this.#_()}fetch(t,e){return this.#o?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.#E(t,null,e.emptied===!0),n=a(()=>this.#t.schedule.schedule(e.stall,()=>{r.waiters.delete(l)&&(r.emptied&&![...r.waiters].some(o=>o.emptied)&&(r.emptied=!1),s(this.#V(r)))}),"arm");let h=n();const l={emptied:e.emptied===!0,answer:a(o=>{h.cancel(),s(o)},"answer"),fail:a(o=>{h.cancel(),i(o)},"fail"),moved:a(()=>{h.cancel(),h=n()},"moved")};r.waiters.add(l),r.waitsOn===null&&this.#R(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.#P(t)}recordsAvailable(){this.#I()}localStateChanged(t){this.#f("detect",t),this.#f("apply",t),this.#I()}sendAccepted(t){this.#f("send",t),this.#I()}capabilityChanged(t){this.#f("capability",t),this.#I()}owe(t){if(t.some(e=>!u.test(e)))throw new Error("entity Apply owe needs entity UUIDs");if(!(!this.#o||this.#e)){for(const e of t)this.#B(e);this.#r()}}#I(){!this.#o||this.#e||(this.#n=!0,this.#r())}waiting(){return this.#y().flatMap(t=>c(t.held)??[])}async flush(){if(!this.#o)throw new Error("entity Apply rail has not started");this.#e||await this.#_()}async stop(){if(this.#e)return;this.#e=!0,this.#q();for(const e of[this.#d,this.#l])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.#x()>0||this.#h;)await Promise.allSettled([...this.#d.steps,...this.#l.steps,...this.#h?[this.#h]:[]]);await this.#v.stop();for(const e of this.#m.splice(0))e.resolve()}#_(){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.#D();this.#h=t,t.then(()=>{this.#h=null,this.#r()})}for(const[t,e]of[[this.#l,this.#b],[this.#d,this.#w]])for(;t.steps.size<e&&t.queue.length>0;){const s=t.queue.shift();s.queued=!1,this.#W(s,t)}if(this.#x()===0&&this.#h===null&&!this.#n&&this.#d.queue.length===0&&this.#l.queue.length===0){if(this.#O.clear(),this.#z(this.#t.now())){this.#r();return}this.#S();for(const t of this.#m.splice(0))t.resolve();this.#T()}}}#x(){return this.#d.steps.size+this.#l.steps.size}#y(){return[...this.#a.values(),...this.#s.values()]}#k(t){return t.kind==="line"?this.#a.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.#d:this.#l).queue.push(t))}}#W(t,e){t.running=!0,t.again=!1;const s=(t.kind==="line"?this.#F(t):this.#J(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#D(){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.#c=1/0;const e=new Set(t);for(const[s,i]of this.#a)!e.has(s)&&!i.running&&!i.queued&&this.#a.delete(s);for(const s of t){let i=this.#a.get(s);i||(i=v(s),this.#a.set(s,i)),this.#i(i)}}catch(t){this.#c=this.#t.now()+p;const e=this.#m.splice(0);e.length===0&&this.#$(t);for(const s of e)s.reject(t)}}async#F(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,t.endsBefore);if(b(t.authorityId,i),this.#e)return!1;if(i.kind==="end"){t.held=null;const r=t.endsBefore!==null;return t.endsBefore=null,r}return i.window.deliverySequence!==null&&this.#v.advance(t.authorityId,i.window.deliverySequence),await this.#N(t,i.window,s)}catch(e){return this.#Y(t,e),!1}}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.readFetchWindow(t.unit,t.toPrefix?null:t.through);if(k(t.unit,i),this.#e||!this.#k(t))return!1;switch((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),t.waitsOn=null,i.kind!=="window"&&(t.held=null),i.kind){case"end":return this.#u(t,{kind:"placed"}),!1;case"nowhere":return this.#u(t,{kind:"nowhere",reason:i.reason}),!1;case"root":return await this.#H(t,i.through,i.reason);case"prerequisite":return this.#Q(t,i.unit,i.reason,s);case"window":return await this.#N(t,i.window,s)}}catch(e){return this.#Y(t,e),!1}}async#H(t,e,s){return t.met.has(t.unit)?(this.#u(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.#P(t.unit),!0))}#B(t){const e=this.#E(t,null);e.toPrefix=!0,e.met.clear(),this.#i(e)}#E(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=E(t,e,s),this.#s.set(t,i),this.#i(i)),i}#Q(t,e,s,i){if(t.met.has(e))return this.#u(t,{kind:"held",unit:e,reason:`${e} placed and still needed: ${s}`}),!1;if(!this.#L(t,e,s))return!1;const r={producer:"apply",entityIds:[e]};return t.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#C(r,i),reason:s,dependency:r},t.held.due}#R(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.#L(t,s,e.reason))return}}#L(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.#u(n,{kind:"held",unit:e,reason:`prerequisite cycle through ${e}: ${s}`});return!1}return t.waitsOn??=e,this.#i(this.#E(e,t.through)),!0}#u(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.#u(i,s);continue}i.met.add(t.unit)}e.kind==="placed"&&(this.#P(t.unit),this.#f("apply",[t.unit]))}#P(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()}}#V(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#N(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.#X(t,e))return!1;const r=await I(()=>this.#g.run(e,()=>t.kind==="lane"&&t.emptied?t.unit:null));if(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.#B(n.work.entityId);return this.#G(t,r==="progress"?e.members.map(n=>n.work.entityId):[r.entityId])}if(r==="again")return t.held?.entityId===i&&(t.held=null),!0;if(r.advanced?.length&&this.#U(t,r.advanced),t.kind==="line"){const n=$(e,r.dependency);if(n!==null)return t.endsBefore=n,!0}return this.#K(t,e,r,s),t.kind==="lane"&&this.#R(t),t.held.due}async#X(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.#g.targetOf(e)!==s.target||e.cutoff!==s.cutoff?!1:await this.#t.store.localWorkAfter(e.authorityId,i.work.entityId,s.kept??s.target)===null}#G(t,e){return t.held&&(t.held.entityId===null||e.includes(t.held.entityId))&&(t.held=null),this.#U(t,e),!0}#U(t,e){t.kind==="lane"&&(this.#n=!0,this.#P(t.unit)),this.#f("apply",e);for(const s of this.#y())s!==t&&s.held?.releaseOnProgress&&!s.held.due&&(s.held.due=!0,this.#i(s))}#K(t,e,s,i){if(s.dependency&&P(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=(n?.entityId===r?n.attempt:0)+1,l=this.#j(h),o={entityId:r,target:this.#g.targetOf(e),kept:e.members[0].work.kept,cutoff:e.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#t.now()+l,releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#C(s.dependency,s.comparedAt??i):!1,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};t.held=o,this.#S();const w=c(n),f=c(o);f&&JSON.stringify(f)!==JSON.stringify(w)&&this.#t.onWaiting?.(f),!(!s.report&&h<M)&&this.#$(new Error(`${y(t)} holds at ${r}, which ${s.releaseOnProgress?"is parked on a conflict":s.report?"failed":"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.#j(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.#S(),this.#$(new Error(`${y(t)} faulted and will retry in ${r}ms (attempt ${i}): ${n}`,{cause:e}))}#$(t){try{this.#M(t)}catch{}}#j(t){const e=Math.min(p,q*2**Math.min(t-1,16)),s=Math.max(0,Math.min(1,this.#t.schedule.random()));return Math.round(e*(.5+s))}#f(t,e){const s=++this.#A;for(const i of e??["*"])this.#O.set(`${t}:${i}`,s);for(const i of this.#y())O(i,t,e)&&this.#i(i)}#C(t,e){return["*",...t.entityIds].some(s=>(this.#O.get(`${t.producer}:${s}`)??0)>e)}#S(){if(this.#e)return;let t=this.#c;for(const i of this.#y())i.held&&!i.held.due&&(t=Math.min(t,i.held.notBefore));if(t===1/0){this.#q();return}if(this.#p?.dueAt===t)return;this.#q();const e=Math.max(0,t-this.#t.now()),s=this.#t.schedule.schedule(e,()=>{this.#p=null,this.#z(t),this.#r()});this.#p={dueAt:t,cancel:a(()=>s.cancel(),"cancel")}}#z(t){let e=!1;for(const s of this.#y())s.held&&!s.held.due&&s.held.notBefore<=t&&(s.held.due=!0,this.#i(s),e=!0);return this.#c<=t&&(this.#c=1/0,this.#n=!0,e=!0),e}#q(){this.#p?.cancel(),this.#p=null}}const y=a(d=>d.kind==="line"?`entity Apply line ${d.authorityId}`:`entity Apply fetch of ${d.unit}`,"label");export{S as ENTITY_APPLY_DEFAULT_CONCURRENCY,M as ENTITY_APPLY_QUIET_ATTEMPTS,q as ENTITY_APPLY_RETRY_BASE_MS,p as ENTITY_APPLY_RETRY_MAX_MS,U as EntityApplyRail};
|
package/dist/apply/settling.d.ts
CHANGED
|
@@ -2,12 +2,13 @@ import { type LocalWork, type OwnLine } from '../clash/local-work.js';
|
|
|
2
2
|
import type { AcceptedEntityRecord } from '../records/contracts.js';
|
|
3
3
|
import { type OutboxRow } from '../records/outbox.js';
|
|
4
4
|
import { type ApplyPlay, type ShowingJudgment } from './showing.js';
|
|
5
|
-
export type ApplySettlement = 'superseded' | 'own' | 'neither' | 'listed' | 'turn';
|
|
5
|
+
export type ApplySettlement = 'superseded' | 'own' | 'neither' | 'listed' | 'turn' | 'waiting';
|
|
6
6
|
export type LineJudgment = ApplySettlement | ApplyPlay;
|
|
7
7
|
export interface LineHeadFacts extends OwnLine {
|
|
8
8
|
localWorkAfter(): LocalWork | null;
|
|
9
9
|
outboxRow(): OutboxRow | undefined;
|
|
10
10
|
showing(): ShowingJudgment;
|
|
11
|
+
waiting?(): boolean;
|
|
11
12
|
}
|
|
12
13
|
export declare function judgeLineHead(head: AcceptedEntityRecord, facts: LineHeadFacts): LineJudgment;
|
|
13
14
|
export declare function isSettlement(judgment: LineJudgment): judgment is ApplySettlement;
|
package/dist/apply/settling.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var o=Object.defineProperty;var e=(r,n)=>o(r,"name",{value:n,configurable:!0});import{localWorkWins as i,ownSettles as u}from"../clash/local-work.js";import{isOwnRecord as f}from"../records/outbox.js";import{APPLY_PLAYS as w}from"./showing.js";function g(r,n){if(r.change.turn!==void 0)return"turn";if(n.waiting?.()===!0)return"waiting";if(i(r.change.result.type,n.localWorkAfter()))return"superseded";const t=n.outboxRow();return t!==void 0&&f(t,r)&&u(r,n)?"own":n.showing()}e(g,"judgeLineHead");function s(r){return!w.has(r)}e(s,"isSettlement");export{s as isSettlement,g as judgeLineHead};
|
package/dist/apply/window.js
CHANGED
|
@@ -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
|
|
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==="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};
|
package/dist/entities/cloud.d.ts
CHANGED
|
@@ -37,7 +37,9 @@ export declare function containedRecordsSteps(records: readonly CloudEntityRecor
|
|
|
37
37
|
export declare function containedRecords(records: readonly CloudEntityRecord[], rootUuid: string): readonly CloudEntityRecord[];
|
|
38
38
|
export declare function materializableRecordsSteps(records: readonly CloudEntityRecord[], rootUuid: string): Steps<readonly CloudEntityRecord[]>;
|
|
39
39
|
export declare function materializableRecords(records: readonly CloudEntityRecord[], rootUuid: string): readonly CloudEntityRecord[];
|
|
40
|
-
export declare function
|
|
40
|
+
export declare function waitingEntities(value: unknown, what: string): readonly string[];
|
|
41
|
+
export declare function installedSubtree(installed: readonly unknown[], setAside: readonly CloudEntityRecord[], subtree: readonly CloudEntityRecord[], rootUuid: string, waiting?: readonly string[]): boolean;
|
|
42
|
+
export declare function withoutWaiting(placed: readonly CloudEntityRecord[], root: string, waiting: readonly string[]): readonly CloudEntityRecord[];
|
|
41
43
|
export declare function completeRootsFromRecords(rootIds: readonly string[], records: readonly unknown[]): readonly CloudEntityRecord[];
|
|
42
44
|
export declare function completeRootsFromRecordsSteps(rootIds: readonly string[], records: readonly unknown[]): Steps<readonly CloudEntityRecord[]>;
|
|
43
45
|
export declare function parseSnapshot(bytes: Uint8Array | string): CloudSnapshot;
|
package/dist/entities/cloud.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var V=Object.defineProperty;var p=(e,t)=>V(e,"name",{value:t,configurable:!0});import{CONTAINERS as
|
|
1
|
+
var V=Object.defineProperty;var p=(e,t)=>V(e,"name",{value:t,configurable:!0});import{CONTAINERS as w,DERIVED_PAYLOAD as k,ROOT_TYPES as z,STATUSES as C,TYPES as M,UUID_PATTERN as h}from"./types.js";import{bytesToBase64Url as N,hexToBytes as O,utf8Decode as A}from"./bytes.js";import{isName as _,validFileState as P,validGitHome as $,validLinkTarget as L,validPortableMtime as H,validPortableSize as Y}from"./rules.js";import{clashResolution as F}from"../clash/resolution.js";import{eachStep as f,finishSteps as y,ITEMS_PER_BOUNDARY as E,sortSteps as B}from"../steps.js";import{failure as J}from"../faults/need.js";const j="amalgm-entity-registry@10",S=1,D=/^[0-9a-f]{64}$/;function lt(e,t){return`pvr_${N(O(t(`private-user:${e}:${j}`))).slice(0,32)}`}p(lt,"privateEntityResourceId");function pt(e){return/^pvr_[A-Za-z0-9_-]{32}$/.test(e)}p(pt,"isPrivateEntityResourceId");const I=p(e=>JSON.stringify(e),"stableJson"),ct=p((e,t)=>I(e)===I(t),"sameRecord"),G=["uuid","type","parentUUID","name","status","version","payloadVersion","fileMode","mtime","size","linkTarget","gitHomeEntity","gitHomePath","transportVersion"];function R(e,t){const r=m(e).records,n=m(t).records;if(r.length!==n.length)return!1;for(let o=0;o<r.length;o+=1){const l=r[o],u=n[o];if(l!==u){for(const i of G)if(l[i]!==u[i])return!1}}return!0}p(R,"sameRecords");function W(e,t){const r=new Map(t.map(n=>[n.uuid,n]));return m(e).records.map(n=>{const o=r.get(n.uuid);return o&&F(n.type)==="play"&&o.type===n.type&&o.parentUUID===n.parentUUID&&o.name===n.name&&o.status===n.status?o:n})}p(W,"statedRecords");const T=new WeakSet;function Z(e){if(typeof e=="object"&&e!==null&&T.has(e))return e;const t=e;if(!t||typeof t!="object"||typeof t.uuid!="string"||!h.test(t.uuid)||typeof t.type!="string"||!M.has(t.type)||t.parentUUID!==null&&(typeof t.parentUUID!="string"||!h.test(t.parentUUID))||!_(t.name)||!C.has(t.status)||typeof t.version!="string"||!D.test(t.version)||t.payloadVersion!==null&&typeof t.payloadVersion!="string"||!P(t.type,t.payloadVersion,t.fileMode)||!H(t.type,t.mtime,t.payloadVersion===null&&t.fileMode===null)||!Y(t.type,t.payloadVersion,t.size)||!L(t.type,t.payloadVersion,t.linkTarget)||!$(t.type,t.gitHomeEntity??null,t.gitHomePath??null)||t.transportVersion!==void 0&&t.transportVersion!==null&&(typeof t.transportVersion!="string"||!D.test(t.transportVersion)))throw new Error("cloud entity snapshot has an invalid record");if(t.parentUUID===null&&!z.has(t.type))throw new Error(`cloud entity ${t.uuid} is not a valid tree root`);if(k.has(t.type)&&t.payloadVersion!==null)throw new Error("workspace and folder payloads are derived membership, never transported heads");const r=t.transportVersion??null;if(t.type!=="repo.git"&&r!==null)throw new Error("only a repository may declare a transport head");const n=Object.freeze({uuid:t.uuid.toLowerCase(),type:t.type,parentUUID:t.parentUUID===null?null:t.parentUUID.toLowerCase(),name:t.name,status:t.status,version:t.version,payloadVersion:t.payloadVersion,fileMode:t.fileMode,mtime:t.mtime,size:t.size,linkTarget:t.linkTarget,gitHomeEntity:t.gitHomeEntity??null,gitHomePath:t.gitHomePath??null,transportVersion:r});return T.add(n),n}p(Z,"assertRecord");function*q(e){const t=[],r=new Set;return yield*f(e,n=>{const o=Z(n);if(r.has(o.uuid))throw new Error("cloud entity snapshot has duplicate UUIDs");r.add(o.uuid),t.push(o)}),t}p(q,"assertedRecordsSteps");function*g(e){const t=yield*B(yield*q(e),(r,n)=>r.uuid<n.uuid?-1:r.uuid>n.uuid?1:0);return{schemaVersion:S,kind:"amalgm-entity-registry",records:t}}p(g,"snapshotFromRecordsSteps");function m(e){return y(g(e))}p(m,"snapshotFromRecords");function*b(e,t){const r=new Map;yield*f(e,i=>{if(i.parentUUID===null)return;const s=r.get(i.parentUUID);s?s.push(i):r.set(i.parentUUID,[i])});const n=new Set([t]),o=[t];let l=0;for(;o.length>0;){for(const i of r.get(o.pop())??[])n.has(i.uuid)||(n.add(i.uuid),o.push(i.uuid));++l%E===0&&(yield)}const u=[];return yield*f(e,i=>{n.has(i.uuid)&&u.push(i)}),u}p(b,"containedRecordsSteps");function dt(e,t){return y(b(e,t))}p(dt,"containedRecords");function*K(e,t){const r=yield*b((yield*g(e)).records,t),n=new Map;yield*f(r,a=>n.set(a.uuid,a));const o=n.get(t);if(!o||o.status!=="active")return[];if(!w.has(o.type))throw new Error(`cloud materialization root ${o.uuid} is not a container`);const l=new Map;yield*f(r,a=>{if(a.uuid===o.uuid)return;if(a.parentUUID===null)throw new Error(`cloud materialization entity ${a.uuid} escapes its selected root`);if(!n.get(a.parentUUID))throw new Error(`cloud materialization entity ${a.uuid} has incomplete ancestry`);const U=l.get(a.parentUUID);U?U.push(a):l.set(a.parentUUID,[a])});const u=new Set,i=[o];let s=0;for(;i.length>0;){const a=i.pop();if(!(a.status!=="active"||u.has(a.uuid))&&(u.add(a.uuid),!!w.has(a.type))){for(const d of l.get(a.uuid)??[])d.status==="active"&&i.push(d);++s%E===0&&(yield)}}const c=[];return yield*f(r,a=>{u.has(a.uuid)&&c.push(a)}),c}p(K,"materializableRecordsSteps");function Q(e,t){return y(K(e,t))}p(Q,"materializableRecords");function ft(e,t){const r=e??[];if(!Array.isArray(r)||r.some(o=>typeof o!="string"||!h.test(o)))throw new Error(`${t} names invalid waiting entities`);const n=r.map(o=>o.toLowerCase());if(new Set(n).size!==n.length)throw new Error(`${t} names a waiting entity twice`);return n}p(ft,"waitingEntities");function ht(e,t,r,n,o=[]){const l=W(e,r),u=Q(r,n),i=new Set(u.map(({uuid:s})=>s));return t.some(({uuid:s})=>i.has(s))?!1:R(l,X(u,n,o))&&R([...u,...t],r)}p(ht,"installedSubtree");function X(e,t,r){if(r.length===0)return e;const n=new Map(e.map(i=>[i.uuid,i])),o=new Set(r);for(const i of o)if(i===t||!n.has(i))throw new Error("a waiting entity is not one the placement places beneath its root");const l=new Map,u=p(i=>{const s=[];let c=i,a=!1;for(;c!==void 0;){const d=l.get(c.uuid);if(d!==void 0){a=d;break}if(o.has(c.uuid)){a=!0,s.push(c.uuid);break}s.push(c.uuid),c=c.parentUUID===null?void 0:n.get(c.parentUUID)}for(const d of s)l.set(d,a);return a},"waitsBeneath");return e.filter(i=>!u(i))}p(X,"withoutWaiting");const x=Symbol("parent cycle"),v=Symbol("incomplete ancestry");function tt(e){const t=new Map;return r=>{const n=[],o=new Set;let l=r,u;for(;;){const i=t.get(l.uuid);if(i!==void 0){u=i;break}if(l.parentUUID===null){u=l.uuid;break}if(o.has(l.uuid)){u=x;break}o.add(l.uuid),n.push(l.uuid);const s=e.get(l.parentUUID);if(!s){u=v;break}l=s}for(const i of n)t.set(i,u);return u}}p(tt,"createRootResolver");function yt(e,t){return y(et(e,t))}p(yt,"completeRootsFromRecords");function*et(e,t){if(e.length===0)throw new Error("cloud root set has no roots");const r=e.map(s=>{if(!h.test(s))throw new Error("cloud root set has an invalid root UUID");return s.toLowerCase()}).sort();if(new Set(r).size!==r.length)throw new Error("cloud root set has duplicate roots");const n=yield*g(t),o=new Map;yield*f(n.records,s=>{o.set(s.uuid,s)});const l=new Set(r),u=n.records.filter(s=>s.parentUUID===null).map(s=>s.uuid).sort();if(u.length!==r.length||u.some((s,c)=>s!==r[c]))throw new Error("cloud root set records do not exactly match its roots");const i=tt(o);return yield*f(n.records,s=>{if(s.parentUUID!==null){const a=o.get(s.parentUUID);if(!a||s.status==="active"&&!w.has(a.type))throw new Error(`cloud root set entity ${s.uuid} has no container parent`)}const c=i(s);if(c===x)throw J("cloud root set has a parent cycle","graph-cycle","parent-cycle");if(c===v)throw new Error(`cloud root set entity ${s.uuid} has incomplete ancestry`);if(!l.has(c))throw new Error(`cloud root set entity ${s.uuid} is outside its declared roots`)}),n.records}p(et,"completeRootsFromRecordsSteps");function mt(e){let t;try{t=JSON.parse(typeof e=="string"?e:A(e))}catch{throw new Error("cloud entity snapshot is not JSON")}if(t?.schemaVersion!==S||t?.kind!=="amalgm-entity-registry"||!Array.isArray(t.records))throw new Error("cloud entity snapshot has an unsupported shape");return m(t.records)}p(mt,"parseSnapshot");export{j as ENTITY_CLOUD_CONTRACT,S as ENTITY_CLOUD_SCHEMA_VERSION,Z as assertRecord,q as assertedRecordsSteps,yt as completeRootsFromRecords,et as completeRootsFromRecordsSteps,dt as containedRecords,b as containedRecordsSteps,ht as installedSubtree,pt as isPrivateEntityResourceId,Q as materializableRecords,K as materializableRecordsSteps,mt as parseSnapshot,lt as privateEntityResourceId,ct as sameRecord,R as sameRecords,m as snapshotFromRecords,g as snapshotFromRecordsSteps,I as stableJson,W as statedRecords,ft as waitingEntities,X as withoutWaiting};
|
package/dist/files/commands.d.ts
CHANGED
|
@@ -60,6 +60,7 @@ export interface FilesAddPorts {
|
|
|
60
60
|
readonly syncingReferences: readonly string[];
|
|
61
61
|
readonly listedReferences: readonly string[];
|
|
62
62
|
readonly alreadyMaterialized: boolean;
|
|
63
|
+
readonly waiting?: readonly string[];
|
|
63
64
|
}>;
|
|
64
65
|
coverWorkspace(input: {
|
|
65
66
|
readonly workspaceId: string;
|
|
@@ -78,6 +79,7 @@ export interface FilesAddResult {
|
|
|
78
79
|
readonly alreadyMaterialized: boolean;
|
|
79
80
|
readonly syncing: boolean;
|
|
80
81
|
readonly syncingReferences: readonly string[];
|
|
82
|
+
readonly waiting: readonly string[];
|
|
81
83
|
readonly watcherRoots: number;
|
|
82
84
|
}
|
|
83
85
|
export declare function workspaceGraph(records: readonly CloudEntityRecord[], workspaceId: string): {
|
package/dist/files/commands.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var N=Object.defineProperty;var p=(n,a)=>N(n,"name",{value:a,configurable:!0});import{CONTAINERS as S,UUID_PATTERN as W,containedRecords as F,installedSubtree as U,waitingEntities as $,materializableRecords as A,sameRecords as E,snapshotFromRecords as v}from"../entities/index.js";import{computerAddress as q}from"./computer-address.js";import{ownFolderRefusal as x}from"./own-folders.js";function m(n,a){const e=typeof n=="string"?n.trim().toLowerCase():"";if(!W.test(e))throw new Error(`${a} must be a UUID`);return e}p(m,"uuid");function d(n,a){const e=typeof n=="string"?n.trim():"";if(!e)throw new Error(`${a} is required`);return e}p(d,"required");async function G(n,a){const e=d(n,"workspace path"),r=await a.inspectExternalWorkspace(e),c=d(r.absolutePath,"external workspace path"),i=x("files register",c,r.amalgmFolders);if(i)throw i;const l=d(r.name,"workspace name"),s=r.workspaceId===null?m(a.randomUuid(),"generated workspace id"):m(r.workspaceId,"existing workspace id"),w=await a.placeWorkspaceReference({workspaceId:s,absolutePath:c,address:q(await a.computerFolder(),c)});return{workspaceId:s,workspaceName:l,externalPath:c,referenceName:d(w.referenceName,"reference name"),referencePath:d(w.referencePath,"reference path"),declared:!0,created:w.created===!0}}p(G,"registerFilesWorkspace");function D(n,a){const e=m(a,"workspace id"),r=v(n),c=r.records.find(s=>s.uuid===e);if(!c||c.status!=="active")throw new Error(`workspace ${e} does not exist in this user's cloud registry`);if(!S.has(c.type))throw new Error(`entity ${e} is not a registered workspace boundary`);const i=r.records.filter(s=>s.status==="active"&&s.type==="reference"&&s.payloadVersion===e);if(i.length!==1)throw new Error(`workspace ${e} must have exactly one durable reference`);const l=F(r.records,e);return{workspace:c,reference:i[0],subtree:l,records:A(l,e)}}p(D,"workspaceGraph");async function L(n,a,e){const r=m(n,"workspace id"),c=d(a,"destination directory"),i=await e.lookupRegistry(r),{subtree:l,...s}=D(i.snapshot.records,r),w=await e.inspectDestination({workspaceId:r,destinationParent:c,name:s.workspace.name}),g=x("files add",d(w.absolutePath,"destination path"),w.amalgmFolders);if(g)throw g;const P=new Set(s.records.map(({uuid:t})=>t)),y=l.filter(({uuid:t})=>!P.has(t)),h=i.snapshot.records.filter(t=>t.status==="active"&&t.type==="reference"),o=await e.materializeWorkspace({...s,setAside:y,references:h,destinationParent:c,cloudHead:d(i.checksum,"cloud head"),deliveryWatermark:i.deliveryWatermark}),R=v(o.records||[]).records,f=new Set(o.syncingReferences),k=new Set(o.listedReferences),z=new Set(h.map(({uuid:t})=>t)),I=$(o.waiting,"workspace materialization"),b=U(R,y,l,r,I);if(!Number.isSafeInteger(o.rows)||o.rows!==R.length||!b&&o.alreadyMaterialized!==!0||!E([o.reference],[s.reference])||f.size!==o.syncingReferences.length||k.size!==o.listedReferences.length||[...f].some(t=>!z.has(t))||[...k].some(t=>!z.has(t)||f.has(t))||!E(o.references,h.filter(({uuid:t})=>!f.has(t)&&!k.has(t))))throw new Error("workspace materialization did not persist exactly the selected graph");const u=await e.coverWorkspace({workspaceId:r});if(u.active!==!0||!Number.isSafeInteger(u.roots)||u.roots<1)throw new Error("workspace watcher coverage is not active");return{workspaceId:r,workspaceName:s.workspace.name,path:d(o.path,"materialized workspace path"),rows:o.rows,cloudHead:i.checksum,materialized:!0,alreadyMaterialized:o.alreadyMaterialized===!0,syncing:!b,syncingReferences:[...f].sort(),waiting:[...I].sort(),watcherRoots:u.roots}}p(L,"addFilesWorkspace");export{L as addFilesWorkspace,G as registerFilesWorkspace,D as workspaceGraph};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var f=Object.defineProperty;var
|
|
1
|
+
var f=Object.defineProperty;var s=(h,t)=>f(h,"name",{value:t,configurable:!0});import{ReceiveChannel as n}from"./receive-channel.js";import{receiveEntityPage as u,receiveTailPage as l}from"./receive-pages.js";import{OwnedEffects as d}from"./owned-effects.js";import{RetryClock as y}from"./retry-clock.js";const o=s(()=>new Error("entity record rail stopped before reaching its watermark"),"stopped"),c=s(h=>{if(!Number.isSafeInteger(h)||h<0)throw new Error("entity record watermark must be a non-negative integer")},"checkPosition");class I{static{s(this,"ReceiveLoop")}#o;#v;#c;#a;#f;#u=new d(o);#i;#s=new Map;#r=new Set;#h=new Map;#w;#g=!1;#t=!1;#n=null;#l=[];constructor(t){this.#o=t.authorityId,this.#v=t.store,this.#c=t.authority,this.#f=t.onError,this.#a={store:t.store,authority:t.authority,run:s(e=>this.#u.run(e),"run"),onInboxCommitted:s(()=>{try{t.onInboxCommitted()}catch(e){this.#f(e)}},"onInboxCommitted")},this.#w=t.schedule,this.#i=new n(t.authorityId,null,!0);for(const e of t.channels)this.#y(e).held=!0}start(){this.#g=!0,this.#l=[this.#c.onAvailable(t=>this.ring(t)),this.#c.onDisconnect(()=>{if(!this.#t)for(const t of this.#d())this.#p(t)})];for(const t of this.#d())this.#e(t)}ring(t){if(t.entityId!==null){const e=this.#s.get(t.entityId);e&&this.#e(e)}else t.authorityId===this.#o&&this.#e(this.#i)}hold(t){if(this.#t)return;const e=this.#y(t);e.held||(e.held=!0,this.#e(e))}release(t){const e=this.#s.get(t);e&&(e.held=!1,this.#I(e))}async receiveThrough(t){c(t),await this.#m(this.#i,t)}async receiveCurrent(){if(this.#t)throw o();const t=this.#i.wait(null);this.#e(this.#i),await t}async receiveEntityThrough(t,e){if(c(e),this.#t)throw o();await this.#m(this.#y(t),e)}async coverReceipts(t){const e=await this.#u.run(()=>this.#v.readReceipts(this.#o,t)),r=await Promise.allSettled([this.receiveThrough(e.delivery),...e.entities.map(({entityId:i,globalSequence:a})=>this.receiveEntityThrough(i,a))]);for(const i of r)if(i.status==="rejected")throw i.reason}async stop(){this.#t=!0;for(const t of this.#h.values())t.cancel();for(const t of this.#l)t();this.#l=[],await Promise.allSettled([this.#n,this.#u.stop()]);for(const t of this.#d())t.stop(o())}#d(){return[this.#i,...this.#s.values()]}#y(t){let e=this.#s.get(t);return e||this.#s.set(t,e=new n(t,t,!1)),e}#m(t,e){if(this.#t)return Promise.reject(o());if(t.covers(e))return Promise.resolve();const r=t.wait(e);return this.#e(t),r}#e(t){this.#t||this.#h.get(t)?.armed||(this.#r.add(t),this.#b())}#p(t){this.#r.delete(t);let e=this.#h.get(t);e||this.#h.set(t,e=new y(this.#w)),e.arm(()=>this.#e(t))}#I(t){t.needed||t.entityId===null||(this.#s.delete(t.entityId),this.#r.delete(t),this.#k(t))}#k(t){this.#h.get(t)?.cancel(),this.#h.delete(t)}#b(){if(!this.#g||this.#t||this.#n)return;const t=this.#P();this.#n=t,t.then(()=>{this.#n=null,!this.#t&&this.#r.size>0&&this.#b()})}async#P(){for(let t=this.#E();t&&!this.#t;t=this.#E())await this.#T(t)}#E(){const[t]=this.#r;return t&&this.#r.delete(t),t}async#T(t){const e=t.beginTurn(),r=s(i=>t.observe(i),"observe");try{const i=t.entityId===null?await l(this.#a,this.#o,r):await u(this.#a,t.entityId,r);if(this.#t)return;this.#k(t),i?this.#e(t):t.caughtUp(e)}catch(i){if(this.#t)return;t.failed(e,i),this.#p(t),this.#f(i)}finally{t.endTurn(),this.#I(t)}}}export{I as ReceiveLoop};
|
|
@@ -27,6 +27,7 @@ export interface LocalUserGround {
|
|
|
27
27
|
readonly localRoot: string;
|
|
28
28
|
readonly sqlitePath: string;
|
|
29
29
|
readonly records: readonly CloudEntityRecord[];
|
|
30
|
+
readonly waiting?: readonly string[];
|
|
30
31
|
readonly place?: number;
|
|
31
32
|
readonly head?: UserGroundHead;
|
|
32
33
|
readonly pendingRecords?: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var H=Object.defineProperty;var c=(n,e)=>H(n,"name",{value:e,configurable:!0});import{isLocalOnlyPath as G}from"@amalgm/core/identity";import{containedRecords as E,materializableRecords as k,waitingEntities as L,withoutWaiting as P,graphHeadChecksum as W,privateEntityResourceId as q,sameRecords as C,statedRecords as O,snapshotFromRecords as f,stableJson as p}from"../entities/index.js";import{placedAtHome as z}from"./home-layout.js";const w=/^[0-9a-f]{64}$/;function h(n,e){const o=typeof n=="string"?n.trim():"";if(!o)throw new Error(`user-ground ${e} is required`);return o}c(h,"required");function j(n){return{userId:h(n?.userId,"user id"),userEmail:h(n?.userEmail,"user email").toLowerCase(),deviceId:h(n?.deviceId,"device id")}}c(j,"normalizeUserGroundIdentity");function g(n,e,o){const r=n;if(!r||r.resourceId!==e||!Number.isSafeInteger(r.version)||Number(r.version)<0||!Number.isSafeInteger(r.deliveryWatermark)||Number(r.deliveryWatermark)<0||typeof r.checksum!="string"||!w.test(r.checksum))throw new Error("cloud user-ground head is invalid");const t=f(r.snapshot?.records||[]);if(t.records.length===0)throw new Error("cloud user-ground graph is empty");const s=r.head?W(r.head,o):o(p(t));if(s!==r.checksum)throw new Error("cloud user-ground snapshot checksum mismatch");return{resourceId:e,version:Number(r.version),deliveryWatermark:Number(r.deliveryWatermark),checksum:s,snapshot:t,...r.head?{head:r.head}:{}}}c(g,"assertCloud");function A(n){const e=n;if(!e||!Number.isSafeInteger(e.version)||Number(e.version)<0||typeof e.checksum!="string"||!w.test(e.checksum))throw new Error("local user-ground holds a place but keeps no valid graph head");return{version:Number(e.version),checksum:e.checksum}}c(A,"assertHead");function M(n,e){const o=n,r=o?.checksum;if(typeof r!="string"||!w.test(r)||!o?.snapshot||e(p(o.snapshot))!==r)throw new Error("local user-ground publication journal is invalid");return{checksum:r,snapshot:o.snapshot}}c(M,"assertPublication");function I(n,e,o,r){const t=f(n?.records||[]);if(t.records.length===0)throw new Error("local user-ground graph is empty");const s=L(n.waiting,"local user ground");return{...y(n,e,o,r),records:t.records,...s.length>0?{waiting:s}:{}}}c(I,"assertLocal");function y(n,e,o,r){const t=n;if(!t||t.userId!==e.userId||String(t.userEmail||"").toLowerCase()!==e.userEmail||t.deviceId!==e.deviceId)throw new Error("local user-ground identity does not match login");const s=h(t.localRoot,"local root"),a=h(t.sqlitePath,"SQLite path");if(!G(a))throw new Error("local user-ground SQLite must live inside a local-only folder (.amalgm or .private)");const i=t.place;if(i!==void 0&&(!Number.isSafeInteger(i)||i<0))throw new Error("local user-ground place is invalid");const d=i===void 0?void 0:{place:i,head:A(t.head)},l=t.placing===void 0?void 0:g(t.placing,o,r);if(l!==void 0&&i===void 0)throw new Error("local user-ground pins a placement it never committed");if(t.onDemand!==void 0&&t.onDemand!==!0)throw new Error("local user-ground on-demand mark is invalid");if(t.onDemand===!0&&i===void 0)throw new Error("local user-ground is served on demand but holds no place on the line");if(t.pendingRecords===!0&&i===void 0)throw new Error("local user-ground holds unsent records but no place on the line");const u=t.publication===void 0?void 0:M(t.publication,r);if(u!==void 0&&i!==void 0)throw new Error("local user-ground holds a place and an unanswered publication");return{...e,localRoot:s,sqlitePath:a,...d,...t.pendingRecords===!0?{pendingRecords:!0}:{},...u!==void 0?{publication:u}:{},...l!==void 0?{placing:l}:{},...t.onDemand===!0?{onDemand:!0}:{}}}c(y,"assertGround");function Q(n,e){const o=U(n,e);return k(E(f(n).records,o),o)}c(Q,"userGroundRecords");function $(n,e,o){const r=v(o?.drive),t=o.standing??new Set,s=U(n,e),a=E(f(n).records,s),i=k(a,s),d=r?F(i,s,t):i,l=new Map(d.map(u=>[u.uuid,u]));for(const u of t)if(l.get(u)?.status!=="active")throw new Error("a placed record is not an active record of the home");for(const u of d)if(u.uuid!==s&&(u.parentUUID===null||!l.has(u.parentUUID)))throw new Error("a placed record stands apart from its parent");return{root:s,placed:d,listed:a.filter(({uuid:u})=>!l.has(u))}}c($,"userGroundSelection");function F(n,e,o){const r=new Map;for(const a of n){if(a.parentUUID===null)continue;const i=r.get(a.parentUUID);i?i.push(a):r.set(a.parentUUID,[a])}const t=new Set([e]),s=(r.get(e)??[]).map(a=>({record:a,top:a.name}));for(;s.length>0;){const{record:a,top:i}=s.pop(),d=a.parentUUID!==e;(z(i,d)||o.has(a.uuid))&&t.add(a.uuid);for(const l of r.get(a.uuid)??[])s.push({record:l,top:i})}return n.filter(({uuid:a})=>t.has(a))}c(F,"placedOnDrive");const R="cloud registry must contain exactly one authenticated user-ground root";function J(n,e){return n.status==="active"&&n.parentUUID===null&&n.type==="workspace"&&n.name.toLowerCase()===e}c(J,"isUserGroundRoot");function T(n,e){const o=h(e,"user email").toLowerCase();let r=null;for(const t of n)if(J(t,o)){if(r!==null)throw new Error(R);r=t.uuid}return r}c(T,"findUserGroundRoot");function U(n,e){const o=T(n,e);if(o===null)throw new Error(R);return o}c(U,"userGroundRootId");function v(n){if(typeof n!="boolean")throw new Error("the host must say whether this computer's Amalgm folder is served by its drive");return n}c(v,"assertDrive");function V(n,e,o,r){if(n.place!==e.deliveryWatermark)throw new Error("placed user ground does not hold its graph's place on the line");if(r!==(n.onDemand===!0))throw new Error(r?"placed user ground is not marked as served on demand":"installed user ground is marked as served on demand, but no drive serves this computer's Amalgm folder");const t=O(Q(n.records,o),e.snapshot.records),s=new Set(t.map(({uuid:d})=>d));for(const d of n.waiting??[])s.add(d);const{root:a,placed:i}=$(e.snapshot.records,o,r?{drive:r,standing:s}:{drive:r});if(!C(t,P(i,a,n.waiting??[])))throw new Error(r?"placed user-ground rows are not the runtime's own entries and the standing records exactly as the graph states them":"installed user-ground rows are not the home's active closure exactly as the graph states it")}c(V,"assertHomePlaced");async function b(n,e,o){const r=v(o.local.drive),t=I(await o.local.place(n,e),n,e.resourceId,o.sha256Hex);return V(t,e,n.userEmail,r),{head:D(e),local:t,mode:r?"placed":"materialized"}}c(b,"place");async function _(n,e,o){const r=I(await o.local.initialize(n),n,e,o.sha256Hex),t=f(r.records),s={checksum:o.sha256Hex(p(t)),snapshot:t};return await o.local.journalPublication(n,s),{ground:r,publication:s}}c(_,"capture");async function x(n,e,o,r,t){const s=await t.cloud.publishInitial({resourceId:e,...r}),a=g(s.current,e,t.sha256Hex);return s.created?(await t.local.join(n,0),{head:D(a),local:o,mode:"initialized"}):b(n,a,t)}c(x,"publish");function D(n){return{version:n.version,checksum:n.checksum}}c(D,"headOf");function B(n){const e=n;if(!e||e.active!==!0||!Number.isSafeInteger(e.roots)||Number(e.roots)<1)throw new Error("user-ground watcher coverage is not active");return{active:!0,roots:Number(e.roots)}}c(B,"assertWatch");async function ee(n,e){const o=j(n);v(e.local.drive);const r=q(o.userId,e.sha256Hex),t=await e.local.inspect(o),s=t==null?null:y(t,o,r,e.sha256Hex);let a;if(s?.publication)a=await x(o,r,s,s.publication,e);else if(s?.head!==void 0)a=s.placing?await b(o,s.placing,e):{head:s.head,local:s,mode:s.pendingRecords===!0?"syncing":"current"};else{const m=await e.cloud.lookup(r);if(m==null){const{ground:N,publication:S}=await _(o,r,e);a=await x(o,r,N,S,e)}else a=await b(o,g(m,r,e.sha256Hex),e)}const{head:i,local:d,mode:l}=a,u=B(await e.local.watch(o));return{...o,localRoot:d.localRoot,sqlitePath:d.sqlitePath,cloudResourceId:r,cloudVersion:i.version,cloudHead:i.checksum,mode:l,watcherRoots:u.roots,watching:!0}}c(ee,"convergeUserGround");export{ee as convergeUserGround,T as findUserGroundRoot,J as isUserGroundRoot,j as normalizeUserGroundIdentity,Q as userGroundRecords,$ as userGroundSelection};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var O=Object.defineProperty;var m=(l,e)=>O(l,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyJourney=void 0,exports.outcomeOf=S;const _=require("./contracts.js"),j=require("./plans.js"),x=require("../faults/recover.js"),E=require("../faults/classify.js"),M=require("../clash/local-work.js"),q=require("./selection.js"),W=require("./settling.js"),F=require("./window.js"),P=m(l=>l.kind==="captured-local"||l.kind==="rejudge","drops");function v(l){return l.kind==="retry"?{kind:"park",reason:l.reason??"machine truth is unsettled",releaseOnProgress:!1,report:!1}:l.kind==="dependency"?{kind:"park",reason:l.reason,releaseOnProgress:l.waitFor===void 0,report:!1,...l.waitFor?{dependency:l.waitFor}:{},...l.blocker?{blocker:l.blocker}:{},..."comparedAt"in l?{comparedAt:l.comparedAt}:{}}:l.kind==="conflict"?{kind:"park",reason:l.reason,releaseOnProgress:!0,report:!0}:null}m(v,"parkFor");async function S(l){try{return await l()}catch(e){return{kind:"park",reason:e instanceof Error?e.message:String(e),releaseOnProgress:!1,report:!(0,E.groundMoved)(e),cause:e}}}m(S,"outcomeOf");const I=m(l=>(0,W.isSettlement)(l.judgment)?{kind:"settled",settlement:l.judgment}:null,"settled");class D{static{m(this,"EntityApplyJourney")}#e;#a;#r;#n;constructor(e,t,i){this.#e=e,this.#a={sha256Hex:e.sha256Hex,loops:e.loops},this.#r=t,this.#n=i}targetOf(e){const t=e.members[0];return t===void 0?null:e.intent?.members.find(i=>i.entityId===t.work.entityId)?.throughSequence??(t.work.kept===null?t.work.records.at(-1)?.globalSequence:t.work.resolvedThrough)??null}async run(e,t){if(e.intent!==null)return this.#f(e,e.intent);const i={member:null};try{return await this.#c(e,t,i)}catch(s){if(i.member===null||this.#n()||(0,E.groundMoved)(s))throw s;if(e.unit===null)return this.#d(i.member);if(s instanceof x.Recovering)return{kind:"park",reason:s.message,releaseOnProgress:!1,report:!1,dependency:{producer:"capability",entityIds:s.need.subject===null?[]:[s.need.subject.id]}};throw s}}async#d(e){const t=await this.#t(null,[e],[{kind:"settled",settlement:"waiting"}]);return t==="progress"?{kind:"alone",entityId:e.work.entityId}:t}async#c(e,t,i){const s=this.#e.newIntentId(),n=this.#e.now(),o=t(),a=new Map;for(const d of e.members){if(I(d))continue;const{work:f}=d,w=(0,q.selectEntityApply)(f,this.#a),b=(0,_.createApplyIntent)({attemptId:s,work:f,target:w.target,unit:e.unit,cutoff:e.cutoff,createdAt:n,emptied:f.entityId===o&&f.play==="enter"&&(w.target.result.type==="file.text"||w.target.result.type==="file.binary")}),y=(0,j.planSelectedEntityApply)(f,w,this.#e.sha256Hex);a.set(f.entityId,{member:d,intent:b,plan:y,adapter:this.#e.effects.adapterFor(y.target.type),look:"safe"})}const{active:c,blocked:k}=await this.#o(e.members,a,"look",i);if(c.length===0)return k??"again";const g=c.flatMap(d=>a.get(d.work.entityId)??[]);if(g.length===0)return this.#t(null,c,c.map(d=>I(d)));const h=(0,_.createApplyWindowIntent)({attemptId:s,authorityId:e.authorityId,unit:e.unit,cutoff:e.cutoff,createdAt:n,members:g.map(d=>d.intent)});try{const d=await this.#p(c,a,i),f=d.active.flatMap(r=>a.get(r.work.entityId)??[]);if(f.length===0)return await this.#e.effects.discard(h),d.active.length===0?d.blocked??"again":this.#t(null,d.active,d.active.map(r=>I(r)));const w=f.length===g.length?h:(0,_.createApplyWindowIntent)({...h,members:f.map(r=>r.intent)}),b=await this.#e.effects.hold(w,f.map(r=>r.plan));let y,A;try{y=await this.#o(d.active,a,"final",i),A=await this.#u(w,y,a,t)}finally{b()}return y.unseen!==null&&y.active.length===0?this.#h(y.unseen):A}catch(d){throw await this.#e.effects.discard(h),d}}async#u(e,{active:t,blocked:i},s,n){const o=t.flatMap(r=>s.get(r.work.entityId)??[]);if(o.length===0)return await this.#e.effects.discard(e),t.length===0?i??"again":this.#t(null,t,t.map(r=>I(r)));const a=(0,_.createApplyWindowIntent)({...e,members:o.map(r=>r.intent)}),c=o.filter(r=>r.look==="safe");if(c.length>0){if(this.#n()||o.some(p=>p.intent.emptied&&n()!==p.intent.entityId))return await this.#e.effects.discard(e),"again";const r=await this.#e.store.bindWindow(a);if(r)return await this.#e.effects.discard(e),r.kind==="stale"?"again":v({...r,comparedAt:this.#r()})}const k=new Map,g=new Map,h=m((r,p,u=null)=>{k.set(r,{kind:"owed",reason:p}),u!==null&&g.set(r,u)},"owe"),d=m(r=>v(r.kind==="dependency"?{...r,comparedAt:this.#r()}:r),"waitFrom"),{order:f,cycle:w}=(0,F.revealOrder)(c.map(r=>({entityId:r.intent.entityId,play:r.plan.play,type:r.plan.target.type,parentUUID:r.plan.target.parentUUID}))),b="its parent chain closes among the window's members";for(const r of w)h(r,b,{kind:"park",reason:b,releaseOnProgress:!1,report:!0});for(const r of f){const p=s.get(r),u=await p.adapter.apply.reveal(p.intent,p.plan);u&&h(r,u.reason,u.kind==="dependency"?d(u):null)}for(const r of o){const p=r.intent.entityId;if(k.has(p))continue;const u=await r.adapter.verify.verify(r.intent,r.plan);u.kind==="match"?k.set(p,{kind:"materialized"}):u.kind==="local-change"?h(p,"a person's version stands in its place"):u.kind==="rejudge"||u.kind==="missing-target"?h(p,u.kind):h(p,u.reason??u.kind,d(u))}const y=t.map(r=>I(r)??k.get(r.work.entityId));if(c.length>0)return this.#s(a,t,y,g);const A=await this.#s(null,t,y,g);return await this.#e.effects.discard(e),A}async#s(e,t,i,s){const n=await this.#t(e,t,i),o=n==="progress"?s.get(t[0].work.entityId):void 0;return o===void 0?n:{...o,advanced:t.filter((a,c)=>i[c].kind!=="owed").map(a=>a.work.entityId)}}async#o(e,t,i,s){const n=[];for(const o of e){const a=t.get(o.work.entityId);if(!a){n.push(o);continue}let c;try{c=await this.#i(a.intent)?{kind:"captured-local"}:i==="look"?await this.#y(a):await a.adapter.apply.guard(a.intent,a.plan,i)}catch(g){if(n.length>0&&!this.#n())return{active:n,blocked:null,unseen:null};throw s.member=o,g}if(c.kind==="local-change"||c.kind==="unsettled-ground")return{active:n,blocked:n.length===0?"again":null,unseen:a};if(P(c))return{active:n,blocked:n.length===0?"again":null,unseen:null};const k=v(c);if(k)return{active:n,blocked:k,unseen:null};a.look=c.kind,n.push(o)}return{active:n,blocked:null,unseen:null}}async#p(e,t,i){for(const[s,n]of e.entries()){const o=t.get(n.work.entityId);if(!(!o||o.look!=="safe"))try{const a=await o.adapter.apply.prepare(o.intent,o.plan);if(a)return{active:e.slice(0,s),blocked:v(a)}}catch(a){if(s>0&&!this.#n())return{active:e.slice(0,s),blocked:null};throw i.member=n,a}}return{active:[...e],blocked:null}}async#f(e,t){const i=t.members.map(n=>{const o=e.members.find(c=>c.work.entityId===n.entityId);if(!o)return null;const a=n.kept===null?o.work:{...o.work,records:[],kept:n.kept};return a.records.length===0&&a.kept===null?null:(0,j.planSelectedEntityApply)(a,(0,q.selectEntityApply)(a,this.#a,n),this.#e.sha256Hex)}),s=await this.#e.effects.recover(t,i);if(s.length!==t.members.length)throw new Error("Apply recovery answered for a different window");return this.#t(t,e.members,e.members.map(n=>I(n)??s[t.members.findIndex(o=>o.entityId===n.work.entityId)]??{kind:"owed",reason:"not a member of the bound window"}))}async#t(e,t,i){const s=await this.#e.store.endWindow(e,t,i);if(!s)return"progress";if(s.kind!=="stale"||!s.reason)throw new Error("Apply end commit returned an unknown refusal");return"again"}async#i(e){return(0,M.localWorkWins)(e.targetType,await this.#e.store.localWorkAfter(e.authorityId,e.entityId,e.kept??e.throughSequence))}async#y(e){const{intent:t,plan:i,adapter:s}=e,n=await s.apply.guard(t,i,"look");if(n.kind!=="local-change"&&n.kind!=="unsettled-ground")return n;const o=await this.#l(t,i);if(o.kind!=="observed")return o;const a=await s.apply.guard(t,i,"look");return a.kind==="local-change"||a.kind==="unsettled-ground"?{kind:"retry",reason:"Ground still needs observation after Detect; this member cannot reveal."}:a}async#h(e){const t=await this.#l(e.intent,e.plan);return t.kind==="captured-local"?"again":v(t.kind==="observed"?{kind:"retry",reason:"Its final guard found ground Detect had not recorded; it is judged again after Detect looks."}:t)}async#l(e,t){if(await this.#i(e))return{kind:"captured-local"};let i,s=!0;const n=await this.#e.effects.captureLocal(e,t,()=>{if(!s||i!==void 0)throw new Error("Apply capture comparison must be marked exactly once");i=this.#r()}).finally(()=>{s=!1});if(!n||!["settled","local-only","retry"].includes(n.kind))throw new Error("Apply capture returned no explicit observation outcome");if(await this.#i(e))return{kind:"captured-local"};if(n.kind==="retry")return n;if(n.kind==="local-only"){if(i===void 0)throw new Error("local-only capture requires a synchronous comparison fence");if(!n.reason||n.entityIds.length===0||n.entityIds.some(o=>!o))throw new Error("local-only capture must name the retained ground and its releasing condition");return{kind:"dependency",reason:n.reason,comparedAt:i,waitFor:{producer:"detect",entityIds:n.entityIds}}}return{kind:"observed"}}}exports.EntityApplyJourney=D;
|
package/dist-cjs/apply/lane.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var o=Object.defineProperty;var i=(n,e)=>o(n,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.createApplyLane=u,exports.assertApplyLaneStep=
|
|
1
|
+
"use strict";var o=Object.defineProperty;var i=(n,e)=>o(n,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.createApplyLane=u,exports.assertApplyLaneStep=f;const a=require("../entities/types.js"),r=require("./line.js");function u(n,e,t){return{kind:"lane",unit:n,emptied:t,through:e,toPrefix:!1,waitsOn:null,met:new Set,waiters:new Set,held:null,queued:!1,running:!1,again:!1}}i(u,"createApplyLane");function f(n,e){const t=`lane step for ${n}`;switch((!e||typeof e!="object"||!Number.isSafeInteger(e.through)||e.through<0)&&(0,r.invalidStep)(t),e.kind){case"end":return;case"nowhere":case"root":(typeof e.reason!="string"||e.reason.length===0)&&(0,r.invalidStep)(t);return;case"prerequisite":(!a.UUID_PATTERN.test(e.unit)||e.unit===n||typeof e.reason!="string"||e.reason.length===0)&&(0,r.invalidStep)(t);return;case"window":(e.window?.unit!==n||e.window.cutoff!==e.through||e.window.intent!==null&&(e.window.intent.unit!==n||e.window.intent.cutoff!==e.through))&&(0,r.invalidStep)(t),(0,r.assertApplyWindow)(e.window,t),e.window.members.some(l=>l.work.kept!==null)&&s(e.window,t);return;default:(0,r.invalidStep)(t)}}i(f,"assertApplyLaneStep");function s(n,e){const[t]=n.members;(n.members.length!==1||t.work.play!=="enter"||t.work.records.length!==0||t.work.materialized!==null||t.deliverySequence!==n.cutoff||n.intent!==null&&n.intent.members.some(l=>l.kept===null))&&(0,r.invalidStep)(e)}i(s,"assertKeptWindow");
|
package/dist-cjs/apply/rail.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var A=Object.defineProperty;var d=(o,e)=>A(o,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyRail=exports.ENTITY_APPLY_QUIET_ATTEMPTS=exports.ENTITY_APPLY_RETRY_MAX_MS=exports.ENTITY_APPLY_RETRY_BASE_MS=exports.ENTITY_APPLY_DEFAULT_CONCURRENCY=void 0;const c=require("../entities/types.js"),_=require("./fetch-ahead.js"),p=require("./journey.js"),y=require("./lane.js"),l=require("./line.js"),E=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_QUIET_ATTEMPTS=8;class m{static{d(this,"EntityApplyRail")}#e;#w;#T;#O;#v;#g;#P;#f=!1;#t=!1;#a=new Map;#s=new Map;#d={queue:[],steps:new Set};#l={queue:[],steps:new Set};#r=!1;#n=null;#c=1/0;#A=[];#_=0;#k=new Map;#p=null;constructor(e){this.#e=e,this.#w=e.concurrency??exports.ENTITY_APPLY_DEFAULT_CONCURRENCY,this.#T=e.laneConcurrency??this.#w,this.#O=e.onBackgroundError??(()=>{}),this.#v=e.onIdle??(()=>{});for(const[t,s]of[["concurrency",this.#w],["lane concurrency",this.#T]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${t} must be a positive integer`);this.#g=new p.EntityApplyJourney(e,()=>this.#_,()=>this.#t),this.#P=new _.FetchAhead(e.cargo,{sha256Hex:e.sha256Hex,loops:e.loops})}async start(){if(this.#f)throw new Error("entity Apply rail already started");if(this.#t)throw new Error("entity Apply rail is stopped");this.#f=!0;const e=await this.#e.store.readOpenUnits();if(!Array.isArray(e)||new Set(e).size!==e.length||e.some(t=>!c.UUID_PATTERN.test(t)))throw new Error("entity Apply store returned an invalid open unit list");for(const t of e)this.#Y(t,null);await this.#N()}fetch(e,t){return this.#f?this.#t?Promise.reject(new Error("entity Apply rail is stopped")):c.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.#Y(e,null,t.emptied===!0),n=d(()=>this.#e.schedule.schedule(t.stall,()=>{r.waiters.delete(u)&&(r.emptied&&![...r.waiters].some(a=>a.emptied)&&(r.emptied=!1),s(this.#Q(r)))}),"arm");let h=n();const u={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(u),r.waitsOn===null&&this.#q(r),this.#h()}):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(!c.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.#I(e)}recordsAvailable(){this.#E()}localStateChanged(e){this.#u("detect",e),this.#u("apply",e),this.#E()}sendAccepted(e){this.#u("send",e),this.#E()}capabilityChanged(e){this.#u("capability",e),this.#E()}#E(){!this.#f||this.#t||(this.#r=!0,this.#h())}waiting(){return this.#y().flatMap(e=>(0,l.heldWait)(e.held)??[])}async flush(){if(!this.#f)throw new Error("entity Apply rail has not started");this.#t||await this.#N()}async stop(){if(this.#t)return;this.#t=!0,this.#M();for(const t of[this.#d,this.#l])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.#b()>0||this.#n;)await Promise.allSettled([...this.#d.steps,...this.#l.steps,...this.#n?[this.#n]:[]]);await this.#P.stop();for(const t of this.#A.splice(0))t.resolve()}#N(){return new Promise((e,t)=>{this.#A.push({resolve:e,reject:t}),this.#r=!0,this.#h()})}#h(){if(!this.#t){if(this.#r&&this.#n===null){this.#r=!1;const e=this.#x();this.#n=e,e.then(()=>{this.#n=null,this.#h()})}for(const[e,t]of[[this.#l,this.#T],[this.#d,this.#w]])for(;e.steps.size<t&&e.queue.length>0;){const s=e.queue.shift();s.queued=!1,this.#W(s,e)}if(this.#b()===0&&this.#n===null&&!this.#r&&this.#d.queue.length===0&&this.#l.queue.length===0){if(this.#k.clear(),this.#D(this.#e.now())){this.#h();return}this.#R();for(const e of this.#A.splice(0))e.resolve();this.#v()}}}#b(){return this.#d.steps.size+this.#l.steps.size}#y(){return[...this.#a.values(),...this.#s.values()]}#m(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.#d:this.#l).queue.push(e))}}#W(e,t){e.running=!0,e.again=!1;const s=(e.kind==="line"?this.#z(e):this.#F(e)).then(i=>{e.running=!1,t.steps.delete(s),(i||e.again)&&this.#m(e)&&(e.again=!1,this.#i(e)),this.#h()});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.#c=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=(0,l.createApplyLine)(s),this.#a.set(s,i)),this.#i(i)}}catch(e){this.#c=this.#e.now()+exports.ENTITY_APPLY_RETRY_MAX_MS;const t=this.#A.splice(0);t.length===0&&this.#S(e);for(const s of t)s.reject(e)}}async#z(e){try{const t=e.held;if(t&&t.entityId===null&&!t.due&&t.notBefore>this.#e.now())return!1;const s=this.#_,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 r=e.endsBefore!==null;return e.endsBefore=null,r}return i.window.deliverySequence!==null&&this.#P.advance(e.authorityId,i.window.deliverySequence),await this.#L(e,i.window,s)}catch(t){return this.#B(e,t),!1}}async#F(e){try{const t=e.held;if(t&&t.entityId===null&&!t.due&&t.notBefore>this.#e.now())return!1;const s=this.#_,i=await this.#e.store.readFetchWindow(e.unit,e.through);if((0,y.assertApplyLaneStep)(e.unit,i),this.#t||!this.#m(e))return!1;switch(e.through!==null&&i.through>e.through&&e.met.clear(),e.through=Math.max(e.through??0,i.through),e.waitsOn=null,i.kind!=="window"&&(e.held=null),i.kind){case"end":return this.#o(e,{kind:"placed"}),!1;case"nowhere":return this.#o(e,{kind:"nowhere",reason:i.reason}),!1;case"root":return await this.#X(e,i.through,i.reason);case"prerequisite":return this.#J(e,i.unit,i.reason,s);case"window":return await this.#L(e,i.window,s)}}catch(t){return this.#B(e,t),!1}}async#X(e,t,s){return e.met.has(e.unit)?(this.#o(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.#m(e)?!1:(e.through===t&&e.met.add(e.unit),this.#I(e.unit),!0))}#Y(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,y.createApplyLane)(e,t,s),this.#s.set(e,i),this.#i(i)),i}#J(e,t,s,i){if(e.met.has(t))return this.#o(e,{kind:"held",unit:t,reason:`${t} placed and still needed: ${s}`}),!1;if(!this.#$(e,t,s))return!1;const r={producer:"apply",entityIds:[t]};return e.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#C(r,i),reason:s,dependency:r},e.held.due}#q(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.#$(e,s,t.reason))return}}#$(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.#o(n,{kind:"held",unit:t,reason:`prerequisite cycle through ${t}: ${s}`});return!1}return e.waitsOn??=t,this.#i(this.#Y(t,e.through)),!0}#o(e,t){if(!this.#m(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.#o(i,s);continue}i.met.add(e.unit)}t.kind==="placed"&&(this.#I(e.unit),this.#u("apply",[e.unit]))}#I(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()}}#Q(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#L(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.#H(e,t))return!1;const r=await(0,p.outcomeOf)(()=>this.#g.run(t,()=>e.kind==="lane"&&e.emptied?e.unit:null));if(r==="progress")return this.#V(e,t.members.map(n=>n.work.entityId));if(r==="again")return e.held?.entityId===i&&(e.held=null),!0;if(r.advanced?.length&&this.#U(e,r.advanced),e.kind==="line"){const n=(0,E.windowEndForWait)(t,r.dependency);if(n!==null)return e.endsBefore=n,!0}return this.#G(e,t,r,s),e.kind==="lane"&&this.#q(e),e.held.due}async#H(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.#g.targetOf(t)!==s.target||t.cutoff!==s.cutoff?!1:await this.#e.store.localWorkAfter(t.authorityId,i.work.entityId,s.kept??s.target)===null}#V(e,t){return e.held&&(e.held.entityId===null||t.includes(e.held.entityId))&&(e.held=null),this.#U(e,t),!0}#U(e,t){e.kind==="lane"&&(this.#r=!0,this.#I(e.unit)),this.#u("apply",t);for(const s of this.#y())s!==e&&s.held?.releaseOnProgress&&!s.held.due&&(s.held.due=!0,this.#i(s))}#G(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=(n?.entityId===r?n.attempt:0)+1,u=this.#j(h),a={entityId:r,target:this.#g.targetOf(t),kept:t.members[0].work.kept,cutoff:t.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#e.now()+u,releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#C(s.dependency,s.comparedAt??i):!1,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};e.held=a,this.#R();const g=(0,l.heldWait)(n),f=(0,l.heldWait)(a);f&&JSON.stringify(f)!==JSON.stringify(g)&&this.#e.onWaiting?.(f),!(!s.report&&h<exports.ENTITY_APPLY_QUIET_ATTEMPTS)&&this.#S(new Error(`${w(e)} holds at ${r}, which ${s.releaseOnProgress?"is parked on a conflict":s.report?"failed":"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.#j(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.#R(),this.#S(new Error(`${w(e)} faulted and will retry in ${r}ms (attempt ${i}): ${n}`,{cause:t}))}#S(e){try{this.#O(e)}catch{}}#j(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))}#u(e,t){const s=++this.#_;for(const i of t??["*"])this.#k.set(`${e}:${i}`,s);for(const i of this.#y())(0,l.releaseApplyLine)(i,e,t)&&this.#i(i)}#C(e,t){return["*",...e.entityIds].some(s=>(this.#k.get(`${e.producer}:${s}`)??0)>t)}#R(){if(this.#t)return;let e=this.#c;for(const i of this.#y())i.held&&!i.held.due&&(e=Math.min(e,i.held.notBefore));if(e===1/0){this.#M();return}if(this.#p?.dueAt===e)return;this.#M();const t=Math.max(0,e-this.#e.now()),s=this.#e.schedule.schedule(t,()=>{this.#p=null,this.#D(e),this.#h()});this.#p={dueAt:e,cancel:d(()=>s.cancel(),"cancel")}}#D(e){let t=!1;for(const s of this.#y())s.held&&!s.held.due&&s.held.notBefore<=e&&(s.held.due=!0,this.#i(s),t=!0);return this.#c<=e&&(this.#c=1/0,this.#r=!0,t=!0),t}#M(){this.#p?.cancel(),this.#p=null}}exports.EntityApplyRail=m;const w=d(o=>o.kind==="line"?`entity Apply line ${o.authorityId}`:`entity Apply fetch of ${o.unit}`,"label");
|
|
1
|
+
"use strict";var A=Object.defineProperty;var a=(l,e)=>A(l,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityApplyRail=exports.ENTITY_APPLY_QUIET_ATTEMPTS=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"),m=require("./fetch-ahead.js"),p=require("./journey.js"),y=require("./lane.js"),d=require("./line.js"),_=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_QUIET_ATTEMPTS=8;class E{static{a(this,"EntityApplyRail")}#e;#w;#P;#M;#N;#g;#k;#o=!1;#t=!1;#a=new Map;#s=new Map;#d={queue:[],steps:new Set};#l={queue:[],steps:new Set};#n=!1;#h=null;#c=1/0;#A=[];#m=0;#Y=new Map;#p=null;constructor(e){this.#e=e,this.#w=e.concurrency??exports.ENTITY_APPLY_DEFAULT_CONCURRENCY,this.#P=e.laneConcurrency??this.#w,this.#M=e.onBackgroundError??(()=>{}),this.#N=e.onIdle??(()=>{});for(const[t,s]of[["concurrency",this.#w],["lane concurrency",this.#P]])if(!Number.isSafeInteger(s)||s<1)throw new Error(`entity Apply ${t} must be a positive integer`);this.#g=new p.EntityApplyJourney(e,()=>this.#m,()=>this.#t),this.#k=new m.FetchAhead(e.cargo,{sha256Hex:e.sha256Hex,loops:e.loops})}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.#I(t,null);await this.#O()}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.#I(e,null,t.emptied===!0),n=a(()=>this.#e.schedule.schedule(t.stall,()=>{r.waiters.delete(u)&&(r.emptied&&![...r.waiters].some(o=>o.emptied)&&(r.emptied=!1),s(this.#H(r)))}),"arm");let h=n();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=n()},"moved")};r.waiters.add(u),r.waitsOn===null&&this.#$(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.#T(e)}recordsAvailable(){this.#_()}localStateChanged(e){this.#f("detect",e),this.#f("apply",e),this.#_()}sendAccepted(e){this.#f("send",e),this.#_()}capabilityChanged(e){this.#f("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.#o||this.#t)){for(const t of e)this.#q(t);this.#r()}}#_(){!this.#o||this.#t||(this.#n=!0,this.#r())}waiting(){return this.#y().flatMap(e=>(0,d.heldWait)(e.held)??[])}async flush(){if(!this.#o)throw new Error("entity Apply rail has not started");this.#t||await this.#O()}async stop(){if(this.#t)return;this.#t=!0,this.#b();for(const t of[this.#d,this.#l])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.#v()>0||this.#h;)await Promise.allSettled([...this.#d.steps,...this.#l.steps,...this.#h?[this.#h]:[]]);await this.#k.stop();for(const t of this.#A.splice(0))t.resolve()}#O(){return new Promise((e,t)=>{this.#A.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.#z();this.#h=e,e.then(()=>{this.#h=null,this.#r()})}for(const[e,t]of[[this.#l,this.#P],[this.#d,this.#w]])for(;e.steps.size<t&&e.queue.length>0;){const s=e.queue.shift();s.queued=!1,this.#W(s,e)}if(this.#v()===0&&this.#h===null&&!this.#n&&this.#d.queue.length===0&&this.#l.queue.length===0){if(this.#Y.clear(),this.#x(this.#e.now())){this.#r();return}this.#R();for(const e of this.#A.splice(0))e.resolve();this.#N()}}}#v(){return this.#d.steps.size+this.#l.steps.size}#y(){return[...this.#a.values(),...this.#s.values()]}#E(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.#d:this.#l).queue.push(e))}}#W(e,t){e.running=!0,e.again=!1;const s=(e.kind==="line"?this.#F(e):this.#X(e)).then(i=>{e.running=!1,t.steps.delete(s),(i||e.again)&&this.#E(e)&&(e.again=!1,this.#i(e)),this.#r()});t.steps.add(s)}async#z(){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.#c=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=(0,d.createApplyLine)(s),this.#a.set(s,i)),this.#i(i)}}catch(e){this.#c=this.#e.now()+exports.ENTITY_APPLY_RETRY_MAX_MS;const t=this.#A.splice(0);t.length===0&&this.#S(e);for(const s of t)s.reject(e)}}async#F(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,d.assertApplyLineStep)(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.#k.advance(e.authorityId,i.window.deliverySequence),await this.#L(e,i.window,s)}catch(t){return this.#B(e,t),!1}}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.#m,i=await this.#e.store.readFetchWindow(e.unit,e.toPrefix?null:e.through);if((0,y.assertApplyLaneStep)(e.unit,i),this.#t||!this.#E(e))return!1;switch((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),e.waitsOn=null,i.kind!=="window"&&(e.held=null),i.kind){case"end":return this.#u(e,{kind:"placed"}),!1;case"nowhere":return this.#u(e,{kind:"nowhere",reason:i.reason}),!1;case"root":return await this.#J(e,i.through,i.reason);case"prerequisite":return this.#Q(e,i.unit,i.reason,s);case"window":return await this.#L(e,i.window,s)}}catch(t){return this.#B(e,t),!1}}async#J(e,t,s){return e.met.has(e.unit)?(this.#u(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.#T(e.unit),!0))}#q(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,y.createApplyLane)(e,t,s),this.#s.set(e,i),this.#i(i)),i}#Q(e,t,s,i){if(e.met.has(t))return this.#u(e,{kind:"held",unit:t,reason:`${t} placed and still needed: ${s}`}),!1;if(!this.#U(e,t,s))return!1;const r={producer:"apply",entityIds:[t]};return e.held={entityId:null,target:null,kept:null,cutoff:null,attempt:1,notBefore:1/0,releaseOnProgress:!1,due:this.#D(r,i),reason:s,dependency:r},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 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.#u(n,{kind:"held",unit:t,reason:`prerequisite cycle through ${t}: ${s}`});return!1}return e.waitsOn??=t,this.#i(this.#I(t,e.through)),!0}#u(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.#u(i,s);continue}i.met.add(e.unit)}t.kind==="placed"&&(this.#T(e.unit),this.#f("apply",[e.unit]))}#T(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()}}#H(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#L(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.#V(e,t))return!1;const r=await(0,p.outcomeOf)(()=>this.#g.run(t,()=>e.kind==="lane"&&e.emptied?e.unit:null));if(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.#q(n.work.entityId);return this.#G(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=(0,_.windowEndForWait)(t,r.dependency);if(n!==null)return e.endsBefore=n,!0}return this.#K(e,t,r,s),e.kind==="lane"&&this.#$(e),e.held.due}async#V(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.#g.targetOf(t)!==s.target||t.cutoff!==s.cutoff?!1:await this.#e.store.localWorkAfter(t.authorityId,i.work.entityId,s.kept??s.target)===null}#G(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.#T(e.unit)),this.#f("apply",t);for(const s of this.#y())s!==e&&s.held?.releaseOnProgress&&!s.held.due&&(s.held.due=!0,this.#i(s))}#K(e,t,s,i){if(s.dependency&&(0,d.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=(n?.entityId===r?n.attempt:0)+1,u=this.#C(h),o={entityId:r,target:this.#g.targetOf(t),kept:t.members[0].work.kept,cutoff:t.cutoff,attempt:h,notBefore:s.dependency?1/0:this.#e.now()+u,releaseOnProgress:s.releaseOnProgress,due:s.dependency?this.#D(s.dependency,s.comparedAt??i):!1,reason:s.reason,...s.dependency?{dependency:s.dependency}:{},...s.blocker?{blocker:s.blocker}:{}};e.held=o,this.#R();const g=(0,d.heldWait)(n),c=(0,d.heldWait)(o);c&&JSON.stringify(c)!==JSON.stringify(g)&&this.#e.onWaiting?.(c),!(!s.report&&h<exports.ENTITY_APPLY_QUIET_ATTEMPTS)&&this.#S(new Error(`${w(e)} holds at ${r}, which ${s.releaseOnProgress?"is parked on a conflict":s.report?"failed":"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.#C(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.#R(),this.#S(new Error(`${w(e)} faulted and will retry in ${r}ms (attempt ${i}): ${n}`,{cause:t}))}#S(e){try{this.#M(e)}catch{}}#C(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))}#f(e,t){const s=++this.#m;for(const i of t??["*"])this.#Y.set(`${e}:${i}`,s);for(const i of this.#y())(0,d.releaseApplyLine)(i,e,t)&&this.#i(i)}#D(e,t){return["*",...e.entityIds].some(s=>(this.#Y.get(`${e.producer}:${s}`)??0)>t)}#R(){if(this.#t)return;let e=this.#c;for(const i of this.#y())i.held&&!i.held.due&&(e=Math.min(e,i.held.notBefore));if(e===1/0){this.#b();return}if(this.#p?.dueAt===e)return;this.#b();const t=Math.max(0,e-this.#e.now()),s=this.#e.schedule.schedule(t,()=>{this.#p=null,this.#x(e),this.#r()});this.#p={dueAt:e,cancel:a(()=>s.cancel(),"cancel")}}#x(e){let t=!1;for(const s of this.#y())s.held&&!s.held.due&&s.held.notBefore<=e&&(s.held.due=!0,this.#i(s),t=!0);return this.#c<=e&&(this.#c=1/0,this.#n=!0,t=!0),t}#b(){this.#p?.cancel(),this.#p=null}}exports.EntityApplyRail=E;const w=a(l=>l.kind==="line"?`entity Apply line ${l.authorityId}`:`entity Apply fetch of ${l.unit}`,"label");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var i=Object.defineProperty;var r=(e,n)=>i(e,"name",{value:n,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.judgeLineHead=l,exports.isSettlement=c;const u=require("../clash/local-work.js"),o=require("../records/outbox.js"),s=require("./showing.js");function l(e,n){if(e.change.turn!==void 0)return"turn";if(n.waiting?.()===!0)return"waiting";if((0,u.localWorkWins)(e.change.result.type,n.localWorkAfter()))return"superseded";const t=n.outboxRow();return t!==void 0&&(0,o.isOwnRecord)(t,e)&&(0,u.ownSettles)(e,n)?"own":n.showing()}r(l,"judgeLineHead");function c(e){return!s.APPLY_PLAYS.has(e)}r(c,"isSettlement");
|
package/dist-cjs/apply/window.js
CHANGED
|
@@ -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
|
|
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==="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 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var V=Object.defineProperty;var l=(t,e)=>V(t,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.sameRecord=exports.stableJson=exports.ENTITY_CLOUD_SCHEMA_VERSION=exports.ENTITY_CLOUD_CONTRACT=void 0,exports.privateEntityResourceId=P,exports.isPrivateEntityResourceId=k,exports.sameRecords=E,exports.statedRecords=I,exports.assertRecord=_,exports.assertedRecordsSteps=D,exports.snapshotFromRecordsSteps=w,exports.snapshotFromRecords=m,exports.containedRecordsSteps=S,exports.containedRecords=H,exports.materializableRecordsSteps=N,exports.materializableRecords=b,exports.waitingEntities=j,exports.installedSubtree=F,exports.withoutWaiting=C,exports.completeRootsFromRecords=q,exports.completeRootsFromRecordsSteps=A,exports.parseSnapshot=B;const h=require("./types.js"),U=require("./bytes.js"),y=require("./rules.js"),M=require("../clash/resolution.js"),p=require("../steps.js"),z=require("../faults/need.js");exports.ENTITY_CLOUD_CONTRACT="amalgm-entity-registry@10",exports.ENTITY_CLOUD_SCHEMA_VERSION=1;const g=/^[0-9a-f]{64}$/;function P(t,e){return`pvr_${(0,U.bytesToBase64Url)((0,U.hexToBytes)(e(`private-user:${t}:${exports.ENTITY_CLOUD_CONTRACT}`))).slice(0,32)}`}l(P,"privateEntityResourceId");function k(t){return/^pvr_[A-Za-z0-9_-]{32}$/.test(t)}l(k,"isPrivateEntityResourceId");const L=l(t=>JSON.stringify(t),"stableJson");exports.stableJson=L;const Y=l((t,e)=>(0,exports.stableJson)(t)===(0,exports.stableJson)(e),"sameRecord");exports.sameRecord=Y;const $=["uuid","type","parentUUID","name","status","version","payloadVersion","fileMode","mtime","size","linkTarget","gitHomeEntity","gitHomePath","transportVersion"];function E(t,e){const o=m(t).records,n=m(e).records;if(o.length!==n.length)return!1;for(let s=0;s<o.length;s+=1){const c=o[s],u=n[s];if(c!==u){for(const r of $)if(c[r]!==u[r])return!1}}return!0}l(E,"sameRecords");function I(t,e){const o=new Map(e.map(n=>[n.uuid,n]));return m(t).records.map(n=>{const s=o.get(n.uuid);return s&&(0,M.clashResolution)(n.type)==="play"&&s.type===n.type&&s.parentUUID===n.parentUUID&&s.name===n.name&&s.status===n.status?s:n})}l(I,"statedRecords");const T=new WeakSet;function _(t){if(typeof t=="object"&&t!==null&&T.has(t))return t;const e=t;if(!e||typeof e!="object"||typeof e.uuid!="string"||!h.UUID_PATTERN.test(e.uuid)||typeof e.type!="string"||!h.TYPES.has(e.type)||e.parentUUID!==null&&(typeof e.parentUUID!="string"||!h.UUID_PATTERN.test(e.parentUUID))||!(0,y.isName)(e.name)||!h.STATUSES.has(e.status)||typeof e.version!="string"||!g.test(e.version)||e.payloadVersion!==null&&typeof e.payloadVersion!="string"||!(0,y.validFileState)(e.type,e.payloadVersion,e.fileMode)||!(0,y.validPortableMtime)(e.type,e.mtime,e.payloadVersion===null&&e.fileMode===null)||!(0,y.validPortableSize)(e.type,e.payloadVersion,e.size)||!(0,y.validLinkTarget)(e.type,e.payloadVersion,e.linkTarget)||!(0,y.validGitHome)(e.type,e.gitHomeEntity??null,e.gitHomePath??null)||e.transportVersion!==void 0&&e.transportVersion!==null&&(typeof e.transportVersion!="string"||!g.test(e.transportVersion)))throw new Error("cloud entity snapshot has an invalid record");if(e.parentUUID===null&&!h.ROOT_TYPES.has(e.type))throw new Error(`cloud entity ${e.uuid} is not a valid tree root`);if(h.DERIVED_PAYLOAD.has(e.type)&&e.payloadVersion!==null)throw new Error("workspace and folder payloads are derived membership, never transported heads");const o=e.transportVersion??null;if(e.type!=="repo.git"&&o!==null)throw new Error("only a repository may declare a transport head");const n=Object.freeze({uuid:e.uuid.toLowerCase(),type:e.type,parentUUID:e.parentUUID===null?null:e.parentUUID.toLowerCase(),name:e.name,status:e.status,version:e.version,payloadVersion:e.payloadVersion,fileMode:e.fileMode,mtime:e.mtime,size:e.size,linkTarget:e.linkTarget,gitHomeEntity:e.gitHomeEntity??null,gitHomePath:e.gitHomePath??null,transportVersion:o});return T.add(n),n}l(_,"assertRecord");function*D(t){const e=[],o=new Set;return yield*(0,p.eachStep)(t,n=>{const s=_(n);if(o.has(s.uuid))throw new Error("cloud entity snapshot has duplicate UUIDs");o.add(s.uuid),e.push(s)}),e}l(D,"assertedRecordsSteps");function*w(t){const e=yield*(0,p.sortSteps)(yield*D(t),(o,n)=>o.uuid<n.uuid?-1:o.uuid>n.uuid?1:0);return{schemaVersion:exports.ENTITY_CLOUD_SCHEMA_VERSION,kind:"amalgm-entity-registry",records:e}}l(w,"snapshotFromRecordsSteps");function m(t){return(0,p.finishSteps)(w(t))}l(m,"snapshotFromRecords");function*S(t,e){const o=new Map;yield*(0,p.eachStep)(t,r=>{if(r.parentUUID===null)return;const i=o.get(r.parentUUID);i?i.push(r):o.set(r.parentUUID,[r])});const n=new Set([e]),s=[e];let c=0;for(;s.length>0;){for(const r of o.get(s.pop())??[])n.has(r.uuid)||(n.add(r.uuid),s.push(r.uuid));++c%p.ITEMS_PER_BOUNDARY===0&&(yield)}const u=[];return yield*(0,p.eachStep)(t,r=>{n.has(r.uuid)&&u.push(r)}),u}l(S,"containedRecordsSteps");function H(t,e){return(0,p.finishSteps)(S(t,e))}l(H,"containedRecords");function*N(t,e){const o=yield*S((yield*w(t)).records,e),n=new Map;yield*(0,p.eachStep)(o,a=>n.set(a.uuid,a));const s=n.get(e);if(!s||s.status!=="active")return[];if(!h.CONTAINERS.has(s.type))throw new Error(`cloud materialization root ${s.uuid} is not a container`);const c=new Map;yield*(0,p.eachStep)(o,a=>{if(a.uuid===s.uuid)return;if(a.parentUUID===null)throw new Error(`cloud materialization entity ${a.uuid} escapes its selected root`);if(!n.get(a.parentUUID))throw new Error(`cloud materialization entity ${a.uuid} has incomplete ancestry`);const R=c.get(a.parentUUID);R?R.push(a):c.set(a.parentUUID,[a])});const u=new Set,r=[s];let i=0;for(;r.length>0;){const a=r.pop();if(!(a.status!=="active"||u.has(a.uuid))&&(u.add(a.uuid),!!h.CONTAINERS.has(a.type))){for(const f of c.get(a.uuid)??[])f.status==="active"&&r.push(f);++i%p.ITEMS_PER_BOUNDARY===0&&(yield)}}const d=[];return yield*(0,p.eachStep)(o,a=>{u.has(a.uuid)&&d.push(a)}),d}l(N,"materializableRecordsSteps");function b(t,e){return(0,p.finishSteps)(N(t,e))}l(b,"materializableRecords");function j(t,e){const o=t??[];if(!Array.isArray(o)||o.some(s=>typeof s!="string"||!h.UUID_PATTERN.test(s)))throw new Error(`${e} names invalid waiting entities`);const n=o.map(s=>s.toLowerCase());if(new Set(n).size!==n.length)throw new Error(`${e} names a waiting entity twice`);return n}l(j,"waitingEntities");function F(t,e,o,n,s=[]){const c=I(t,o),u=b(o,n),r=new Set(u.map(({uuid:i})=>i));return e.some(({uuid:i})=>r.has(i))?!1:E(c,C(u,n,s))&&E([...u,...e],o)}l(F,"installedSubtree");function C(t,e,o){if(o.length===0)return t;const n=new Map(t.map(r=>[r.uuid,r])),s=new Set(o);for(const r of s)if(r===e||!n.has(r))throw new Error("a waiting entity is not one the placement places beneath its root");const c=new Map,u=l(r=>{const i=[];let d=r,a=!1;for(;d!==void 0;){const f=c.get(d.uuid);if(f!==void 0){a=f;break}if(s.has(d.uuid)){a=!0,i.push(d.uuid);break}i.push(d.uuid),d=d.parentUUID===null?void 0:n.get(d.parentUUID)}for(const f of i)c.set(f,a);return a},"waitsBeneath");return t.filter(r=>!u(r))}l(C,"withoutWaiting");const O=Symbol("parent cycle"),v=Symbol("incomplete ancestry");function J(t){const e=new Map;return o=>{const n=[],s=new Set;let c=o,u;for(;;){const r=e.get(c.uuid);if(r!==void 0){u=r;break}if(c.parentUUID===null){u=c.uuid;break}if(s.has(c.uuid)){u=O;break}s.add(c.uuid),n.push(c.uuid);const i=t.get(c.parentUUID);if(!i){u=v;break}c=i}for(const r of n)e.set(r,u);return u}}l(J,"createRootResolver");function q(t,e){return(0,p.finishSteps)(A(t,e))}l(q,"completeRootsFromRecords");function*A(t,e){if(t.length===0)throw new Error("cloud root set has no roots");const o=t.map(i=>{if(!h.UUID_PATTERN.test(i))throw new Error("cloud root set has an invalid root UUID");return i.toLowerCase()}).sort();if(new Set(o).size!==o.length)throw new Error("cloud root set has duplicate roots");const n=yield*w(e),s=new Map;yield*(0,p.eachStep)(n.records,i=>{s.set(i.uuid,i)});const c=new Set(o),u=n.records.filter(i=>i.parentUUID===null).map(i=>i.uuid).sort();if(u.length!==o.length||u.some((i,d)=>i!==o[d]))throw new Error("cloud root set records do not exactly match its roots");const r=J(s);return yield*(0,p.eachStep)(n.records,i=>{if(i.parentUUID!==null){const a=s.get(i.parentUUID);if(!a||i.status==="active"&&!h.CONTAINERS.has(a.type))throw new Error(`cloud root set entity ${i.uuid} has no container parent`)}const d=r(i);if(d===O)throw(0,z.failure)("cloud root set has a parent cycle","graph-cycle","parent-cycle");if(d===v)throw new Error(`cloud root set entity ${i.uuid} has incomplete ancestry`);if(!c.has(d))throw new Error(`cloud root set entity ${i.uuid} is outside its declared roots`)}),n.records}l(A,"completeRootsFromRecordsSteps");function B(t){let e;try{e=JSON.parse(typeof t=="string"?t:(0,U.utf8Decode)(t))}catch{throw new Error("cloud entity snapshot is not JSON")}if(e?.schemaVersion!==exports.ENTITY_CLOUD_SCHEMA_VERSION||e?.kind!=="amalgm-entity-registry"||!Array.isArray(e.records))throw new Error("cloud entity snapshot has an unsupported shape");return m(e.records)}l(B,"parseSnapshot");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var _=Object.defineProperty;var u=(
|
|
1
|
+
"use strict";var _=Object.defineProperty;var u=(n,s)=>_(n,"name",{value:s,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.registerFilesWorkspace=N,exports.workspaceGraph=F,exports.addFilesWorkspace=x;const d=require("../entities/index.js"),W=require("./computer-address.js"),E=require("./own-folders.js");function m(n,s){const e=typeof n=="string"?n.trim().toLowerCase():"";if(!d.UUID_PATTERN.test(e))throw new Error(`${s} must be a UUID`);return e}u(m,"uuid");function l(n,s){const e=typeof n=="string"?n.trim():"";if(!e)throw new Error(`${s} is required`);return e}u(l,"required");async function N(n,s){const e=l(n,"workspace path"),r=await s.inspectExternalWorkspace(e),c=l(r.absolutePath,"external workspace path"),i=(0,E.ownFolderRefusal)("files register",c,r.amalgmFolders);if(i)throw i;const w=l(r.name,"workspace name"),a=r.workspaceId===null?m(s.randomUuid(),"generated workspace id"):m(r.workspaceId,"existing workspace id"),p=await s.placeWorkspaceReference({workspaceId:a,absolutePath:c,address:(0,W.computerAddress)(await s.computerFolder(),c)});return{workspaceId:a,workspaceName:w,externalPath:c,referenceName:l(p.referenceName,"reference name"),referencePath:l(p.referencePath,"reference path"),declared:!0,created:p.created===!0}}u(N,"registerFilesWorkspace");function F(n,s){const e=m(s,"workspace id"),r=(0,d.snapshotFromRecords)(n),c=r.records.find(a=>a.uuid===e);if(!c||c.status!=="active")throw new Error(`workspace ${e} does not exist in this user's cloud registry`);if(!d.CONTAINERS.has(c.type))throw new Error(`entity ${e} is not a registered workspace boundary`);const i=r.records.filter(a=>a.status==="active"&&a.type==="reference"&&a.payloadVersion===e);if(i.length!==1)throw new Error(`workspace ${e} must have exactly one durable reference`);const w=(0,d.containedRecords)(r.records,e);return{workspace:c,reference:i[0],subtree:w,records:(0,d.materializableRecords)(w,e)}}u(F,"workspaceGraph");async function x(n,s,e){const r=m(n,"workspace id"),c=l(s,"destination directory"),i=await e.lookupRegistry(r),{subtree:w,...a}=F(i.snapshot.records,r),p=await e.inspectDestination({workspaceId:r,destinationParent:c,name:a.workspace.name}),y=(0,E.ownFolderRefusal)("files add",l(p.absolutePath,"destination path"),p.amalgmFolders);if(y)throw y;const P=new Set(a.records.map(({uuid:t})=>t)),R=w.filter(({uuid:t})=>!P.has(t)),k=i.snapshot.records.filter(t=>t.status==="active"&&t.type==="reference"),o=await e.materializeWorkspace({...a,setAside:R,references:k,destinationParent:c,cloudHead:l(i.checksum,"cloud head"),deliveryWatermark:i.deliveryWatermark}),z=(0,d.snapshotFromRecords)(o.records||[]).records,f=new Set(o.syncingReferences),g=new Set(o.listedReferences),b=new Set(k.map(({uuid:t})=>t)),I=(0,d.waitingEntities)(o.waiting,"workspace materialization"),v=(0,d.installedSubtree)(z,R,w,r,I);if(!Number.isSafeInteger(o.rows)||o.rows!==z.length||!v&&o.alreadyMaterialized!==!0||!(0,d.sameRecords)([o.reference],[a.reference])||f.size!==o.syncingReferences.length||g.size!==o.listedReferences.length||[...f].some(t=>!b.has(t))||[...g].some(t=>!b.has(t)||f.has(t))||!(0,d.sameRecords)(o.references,k.filter(({uuid:t})=>!f.has(t)&&!g.has(t))))throw new Error("workspace materialization did not persist exactly the selected graph");const h=await e.coverWorkspace({workspaceId:r});if(h.active!==!0||!Number.isSafeInteger(h.roots)||h.roots<1)throw new Error("workspace watcher coverage is not active");return{workspaceId:r,workspaceName:a.workspace.name,path:l(o.path,"materialized workspace path"),rows:o.rows,cloudHead:i.checksum,materialized:!0,alreadyMaterialized:o.alreadyMaterialized===!0,syncing:!v,syncingReferences:[...f].sort(),waiting:[...I].sort(),watcherRoots:h.roots}}u(x,"addFilesWorkspace");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var f=Object.defineProperty;var
|
|
1
|
+
"use strict";var f=Object.defineProperty;var s=(h,t)=>f(h,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReceiveLoop=void 0;const n=require("./receive-channel.js"),c=require("./receive-pages.js"),l=require("./owned-effects.js"),d=require("./retry-clock.js"),o=s(()=>new Error("entity record rail stopped before reaching its watermark"),"stopped"),a=s(h=>{if(!Number.isSafeInteger(h)||h<0)throw new Error("entity record watermark must be a non-negative integer")},"checkPosition");class v{static{s(this,"ReceiveLoop")}#o;#y;#c;#a;#u;#f=new l.OwnedEffects(o);#i;#s=new Map;#r=new Set;#h=new Map;#w;#g=!1;#t=!1;#n=null;#l=[];constructor(t){this.#o=t.authorityId,this.#y=t.store,this.#c=t.authority,this.#u=t.onError,this.#a={store:t.store,authority:t.authority,run:s(e=>this.#f.run(e),"run"),onInboxCommitted:s(()=>{try{t.onInboxCommitted()}catch(e){this.#u(e)}},"onInboxCommitted")},this.#w=t.schedule,this.#i=new n.ReceiveChannel(t.authorityId,null,!0);for(const e of t.channels)this.#v(e).held=!0}start(){this.#g=!0,this.#l=[this.#c.onAvailable(t=>this.ring(t)),this.#c.onDisconnect(()=>{if(!this.#t)for(const t of this.#d())this.#_(t)})];for(const t of this.#d())this.#e(t)}ring(t){if(t.entityId!==null){const e=this.#s.get(t.entityId);e&&this.#e(e)}else t.authorityId===this.#o&&this.#e(this.#i)}hold(t){if(this.#t)return;const e=this.#v(t);e.held||(e.held=!0,this.#e(e))}release(t){const e=this.#s.get(t);e&&(e.held=!1,this.#p(e))}async receiveThrough(t){a(t),await this.#m(this.#i,t)}async receiveCurrent(){if(this.#t)throw o();const t=this.#i.wait(null);this.#e(this.#i),await t}async receiveEntityThrough(t,e){if(a(e),this.#t)throw o();await this.#m(this.#v(t),e)}async coverReceipts(t){const e=await this.#f.run(()=>this.#y.readReceipts(this.#o,t)),r=await Promise.allSettled([this.receiveThrough(e.delivery),...e.entities.map(({entityId:i,globalSequence:u})=>this.receiveEntityThrough(i,u))]);for(const i of r)if(i.status==="rejected")throw i.reason}async stop(){this.#t=!0;for(const t of this.#h.values())t.cancel();for(const t of this.#l)t();this.#l=[],await Promise.allSettled([this.#n,this.#f.stop()]);for(const t of this.#d())t.stop(o())}#d(){return[this.#i,...this.#s.values()]}#v(t){let e=this.#s.get(t);return e||this.#s.set(t,e=new n.ReceiveChannel(t,t,!1)),e}#m(t,e){if(this.#t)return Promise.reject(o());if(t.covers(e))return Promise.resolve();const r=t.wait(e);return this.#e(t),r}#e(t){this.#t||this.#h.get(t)?.armed||(this.#r.add(t),this.#k())}#_(t){this.#r.delete(t);let e=this.#h.get(t);e||this.#h.set(t,e=new d.RetryClock(this.#w)),e.arm(()=>this.#e(t))}#p(t){t.needed||t.entityId===null||(this.#s.delete(t.entityId),this.#r.delete(t),this.#I(t))}#I(t){this.#h.get(t)?.cancel(),this.#h.delete(t)}#k(){if(!this.#g||this.#t||this.#n)return;const t=this.#E();this.#n=t,t.then(()=>{this.#n=null,!this.#t&&this.#r.size>0&&this.#k()})}async#E(){for(let t=this.#b();t&&!this.#t;t=this.#b())await this.#P(t)}#b(){const[t]=this.#r;return t&&this.#r.delete(t),t}async#P(t){const e=t.beginTurn(),r=s(i=>t.observe(i),"observe");try{const i=t.entityId===null?await(0,c.receiveTailPage)(this.#a,this.#o,r):await(0,c.receiveEntityPage)(this.#a,t.entityId,r);if(this.#t)return;this.#I(t),i?this.#e(t):t.caughtUp(e)}catch(i){if(this.#t)return;t.failed(e,i),this.#_(t),this.#u(i)}finally{t.endTurn(),this.#p(t)}}}exports.ReceiveLoop=v;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var q=Object.defineProperty;var c=(
|
|
1
|
+
"use strict";var q=Object.defineProperty;var c=(n,e)=>q(n,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.normalizeUserGroundIdentity=b,exports.userGroundRecords=I,exports.userGroundSelection=k,exports.isUserGroundRoot=U,exports.findUserGroundRoot=D,exports.convergeUserGround=F;const P=require("@amalgm/core/identity"),u=require("../entities/index.js"),_=require("./home-layout.js"),w=/^[0-9a-f]{64}$/;function f(n,e){const o=typeof n=="string"?n.trim():"";if(!o)throw new Error(`user-ground ${e} is required`);return o}c(f,"required");function b(n){return{userId:f(n?.userId,"user id"),userEmail:f(n?.userEmail,"user email").toLowerCase(),deviceId:f(n?.deviceId,"device id")}}c(b,"normalizeUserGroundIdentity");function p(n,e,o){const r=n;if(!r||r.resourceId!==e||!Number.isSafeInteger(r.version)||Number(r.version)<0||!Number.isSafeInteger(r.deliveryWatermark)||Number(r.deliveryWatermark)<0||typeof r.checksum!="string"||!w.test(r.checksum))throw new Error("cloud user-ground head is invalid");const t=(0,u.snapshotFromRecords)(r.snapshot?.records||[]);if(t.records.length===0)throw new Error("cloud user-ground graph is empty");const s=r.head?(0,u.graphHeadChecksum)(r.head,o):o((0,u.stableJson)(t));if(s!==r.checksum)throw new Error("cloud user-ground snapshot checksum mismatch");return{resourceId:e,version:Number(r.version),deliveryWatermark:Number(r.deliveryWatermark),checksum:s,snapshot:t,...r.head?{head:r.head}:{}}}c(p,"assertCloud");function L(n){const e=n;if(!e||!Number.isSafeInteger(e.version)||Number(e.version)<0||typeof e.checksum!="string"||!w.test(e.checksum))throw new Error("local user-ground holds a place but keeps no valid graph head");return{version:Number(e.version),checksum:e.checksum}}c(L,"assertHead");function j(n,e){const o=n,r=o?.checksum;if(typeof r!="string"||!w.test(r)||!o?.snapshot||e((0,u.stableJson)(o.snapshot))!==r)throw new Error("local user-ground publication journal is invalid");return{checksum:r,snapshot:o.snapshot}}c(j,"assertPublication");function E(n,e,o,r){const t=(0,u.snapshotFromRecords)(n?.records||[]);if(t.records.length===0)throw new Error("local user-ground graph is empty");const s=(0,u.waitingEntities)(n.waiting,"local user ground");return{...R(n,e,o,r),records:t.records,...s.length>0?{waiting:s}:{}}}c(E,"assertLocal");function R(n,e,o,r){const t=n;if(!t||t.userId!==e.userId||String(t.userEmail||"").toLowerCase()!==e.userEmail||t.deviceId!==e.deviceId)throw new Error("local user-ground identity does not match login");const s=f(t.localRoot,"local root"),a=f(t.sqlitePath,"SQLite path");if(!(0,P.isLocalOnlyPath)(a))throw new Error("local user-ground SQLite must live inside a local-only folder (.amalgm or .private)");const i=t.place;if(i!==void 0&&(!Number.isSafeInteger(i)||i<0))throw new Error("local user-ground place is invalid");const l=i===void 0?void 0:{place:i,head:L(t.head)},h=t.placing===void 0?void 0:p(t.placing,o,r);if(h!==void 0&&i===void 0)throw new Error("local user-ground pins a placement it never committed");if(t.onDemand!==void 0&&t.onDemand!==!0)throw new Error("local user-ground on-demand mark is invalid");if(t.onDemand===!0&&i===void 0)throw new Error("local user-ground is served on demand but holds no place on the line");if(t.pendingRecords===!0&&i===void 0)throw new Error("local user-ground holds unsent records but no place on the line");const d=t.publication===void 0?void 0:j(t.publication,r);if(d!==void 0&&i!==void 0)throw new Error("local user-ground holds a place and an unanswered publication");return{...e,localRoot:s,sqlitePath:a,...l,...t.pendingRecords===!0?{pendingRecords:!0}:{},...d!==void 0?{publication:d}:{},...h!==void 0?{placing:h}:{},...t.onDemand===!0?{onDemand:!0}:{}}}c(R,"assertGround");function I(n,e){const o=G(n,e);return(0,u.materializableRecords)((0,u.containedRecords)((0,u.snapshotFromRecords)(n).records,o),o)}c(I,"userGroundRecords");function k(n,e,o){const r=g(o?.drive),t=o.standing??new Set,s=G(n,e),a=(0,u.containedRecords)((0,u.snapshotFromRecords)(n).records,s),i=(0,u.materializableRecords)(a,s),l=r?z(i,s,t):i,h=new Map(l.map(d=>[d.uuid,d]));for(const d of t)if(h.get(d)?.status!=="active")throw new Error("a placed record is not an active record of the home");for(const d of l)if(d.uuid!==s&&(d.parentUUID===null||!h.has(d.parentUUID)))throw new Error("a placed record stands apart from its parent");return{root:s,placed:l,listed:a.filter(({uuid:d})=>!h.has(d))}}c(k,"userGroundSelection");function z(n,e,o){const r=new Map;for(const a of n){if(a.parentUUID===null)continue;const i=r.get(a.parentUUID);i?i.push(a):r.set(a.parentUUID,[a])}const t=new Set([e]),s=(r.get(e)??[]).map(a=>({record:a,top:a.name}));for(;s.length>0;){const{record:a,top:i}=s.pop(),l=a.parentUUID!==e;((0,_.placedAtHome)(i,l)||o.has(a.uuid))&&t.add(a.uuid);for(const h of r.get(a.uuid)??[])s.push({record:h,top:i})}return n.filter(({uuid:a})=>t.has(a))}c(z,"placedOnDrive");const y="cloud registry must contain exactly one authenticated user-ground root";function U(n,e){return n.status==="active"&&n.parentUUID===null&&n.type==="workspace"&&n.name.toLowerCase()===e}c(U,"isUserGroundRoot");function D(n,e){const o=f(e,"user email").toLowerCase();let r=null;for(const t of n)if(U(t,o)){if(r!==null)throw new Error(y);r=t.uuid}return r}c(D,"findUserGroundRoot");function G(n,e){const o=D(n,e);if(o===null)throw new Error(y);return o}c(G,"userGroundRootId");function g(n){if(typeof n!="boolean")throw new Error("the host must say whether this computer's Amalgm folder is served by its drive");return n}c(g,"assertDrive");function O(n,e,o,r){if(n.place!==e.deliveryWatermark)throw new Error("placed user ground does not hold its graph's place on the line");if(r!==(n.onDemand===!0))throw new Error(r?"placed user ground is not marked as served on demand":"installed user ground is marked as served on demand, but no drive serves this computer's Amalgm folder");const t=(0,u.statedRecords)(I(n.records,o),e.snapshot.records),s=new Set(t.map(({uuid:l})=>l));for(const l of n.waiting??[])s.add(l);const{root:a,placed:i}=k(e.snapshot.records,o,r?{drive:r,standing:s}:{drive:r});if(!(0,u.sameRecords)(t,(0,u.withoutWaiting)(i,a,n.waiting??[])))throw new Error(r?"placed user-ground rows are not the runtime's own entries and the standing records exactly as the graph states them":"installed user-ground rows are not the home's active closure exactly as the graph states it")}c(O,"assertHomePlaced");async function v(n,e,o){const r=g(o.local.drive),t=E(await o.local.place(n,e),n,e.resourceId,o.sha256Hex);return O(t,e,n.userEmail,r),{head:S(e),local:t,mode:r?"placed":"materialized"}}c(v,"place");async function W(n,e,o){const r=E(await o.local.initialize(n),n,e,o.sha256Hex),t=(0,u.snapshotFromRecords)(r.records),s={checksum:o.sha256Hex((0,u.stableJson)(t)),snapshot:t};return await o.local.journalPublication(n,s),{ground:r,publication:s}}c(W,"capture");async function N(n,e,o,r,t){const s=await t.cloud.publishInitial({resourceId:e,...r}),a=p(s.current,e,t.sha256Hex);return s.created?(await t.local.join(n,0),{head:S(a),local:o,mode:"initialized"}):v(n,a,t)}c(N,"publish");function S(n){return{version:n.version,checksum:n.checksum}}c(S,"headOf");function C(n){const e=n;if(!e||e.active!==!0||!Number.isSafeInteger(e.roots)||Number(e.roots)<1)throw new Error("user-ground watcher coverage is not active");return{active:!0,roots:Number(e.roots)}}c(C,"assertWatch");async function F(n,e){const o=b(n);g(e.local.drive);const r=(0,u.privateEntityResourceId)(o.userId,e.sha256Hex),t=await e.local.inspect(o),s=t==null?null:R(t,o,r,e.sha256Hex);let a;if(s?.publication)a=await N(o,r,s,s.publication,e);else if(s?.head!==void 0)a=s.placing?await v(o,s.placing,e):{head:s.head,local:s,mode:s.pendingRecords===!0?"syncing":"current"};else{const m=await e.cloud.lookup(r);if(m==null){const{ground:H,publication:x}=await W(o,r,e);a=await N(o,r,H,x,e)}else a=await v(o,p(m,r,e.sha256Hex),e)}const{head:i,local:l,mode:h}=a,d=C(await e.local.watch(o));return{...o,localRoot:l.localRoot,sqlitePath:l.sqlitePath,cloudResourceId:r,cloudVersion:i.version,cloudHead:i.checksum,mode:h,watcherRoots:d.roots,watching:!0}}c(F,"convergeUserGround");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amalgm/live",
|
|
3
|
-
"version": "0.2.81-main.
|
|
3
|
+
"version": "0.2.81-main.46397184323",
|
|
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",
|
|
@@ -131,13 +131,13 @@
|
|
|
131
131
|
"esbuild@0.28.2": true
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"@amalgm/core": "0.5.1-main.
|
|
134
|
+
"@amalgm/core": "0.5.1-main.46384328664"
|
|
135
135
|
},
|
|
136
|
-
"gitHead": "
|
|
136
|
+
"gitHead": "fd7811acda35761816cd545fa52569f46eb14800",
|
|
137
137
|
"amalgmSource": {
|
|
138
138
|
"repository": "amalgm-inc/amalgm-live",
|
|
139
139
|
"branch": "main",
|
|
140
|
-
"commit": "
|
|
141
|
-
"occurrence": "
|
|
140
|
+
"commit": "fd7811acda35761816cd545fa52569f46eb14800",
|
|
141
|
+
"occurrence": "46397184323"
|
|
142
142
|
}
|
|
143
143
|
}
|