@continuonai/rcan-ts 0.6.0 → 1.1.0

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.
@@ -2944,7 +2944,7 @@ var RobotURI = class _RobotURI {
2944
2944
  };
2945
2945
 
2946
2946
  // src/version.ts
2947
- var SPEC_VERSION = "1.6";
2947
+ var SPEC_VERSION = "2.1.0";
2948
2948
 
2949
2949
  // src/message.ts
2950
2950
  var RCANMessageError = class extends Error {
@@ -2980,6 +2980,10 @@ var RCANMessage = class _RCANMessage {
2980
2980
  transportEncoding;
2981
2981
  /** v1.6: GAP-18 multi-modal media chunks */
2982
2982
  mediaChunks;
2983
+ /** v2.1: SHA-256 of sender's firmware manifest */
2984
+ firmwareHash;
2985
+ /** v2.1: URI to sender's SBOM attestation endpoint */
2986
+ attestationRef;
2983
2987
  constructor(data) {
2984
2988
  if (!data.cmd || data.cmd.trim() === "") {
2985
2989
  throw new RCANMessageError("'cmd' is required");
@@ -3008,6 +3012,13 @@ var RCANMessage = class _RCANMessage {
3008
3012
  this.loa = data.loa;
3009
3013
  this.transportEncoding = data.transportEncoding;
3010
3014
  this.mediaChunks = data.mediaChunks;
3015
+ this.firmwareHash = data.firmwareHash;
3016
+ this.attestationRef = data.attestationRef;
3017
+ if (this.signature !== void 0 && this.signature["sig"] === "pending") {
3018
+ throw new RCANMessageError(
3019
+ "signature.sig:'pending' is not valid in RCAN v2.1. Sign the message before sending."
3020
+ );
3021
+ }
3011
3022
  if (this.confidence !== void 0) {
3012
3023
  if (this.confidence < 0 || this.confidence > 1) {
3013
3024
  throw new RCANMessageError(
@@ -3049,6 +3060,8 @@ var RCANMessage = class _RCANMessage {
3049
3060
  if (this.loa !== void 0) obj.loa = this.loa;
3050
3061
  if (this.transportEncoding !== void 0) obj.transportEncoding = this.transportEncoding;
3051
3062
  if (this.mediaChunks !== void 0) obj.mediaChunks = this.mediaChunks;
3063
+ if (this.firmwareHash !== void 0) obj.firmwareHash = this.firmwareHash;
3064
+ if (this.attestationRef !== void 0) obj.attestationRef = this.attestationRef;
3052
3065
  return obj;
3053
3066
  }
3054
3067
  /** Serialize to JSON string */
@@ -3091,7 +3104,9 @@ var RCANMessage = class _RCANMessage {
3091
3104
  readOnly: obj.readOnly,
3092
3105
  loa: obj.loa,
3093
3106
  transportEncoding: obj.transportEncoding,
3094
- mediaChunks: obj.mediaChunks
3107
+ mediaChunks: obj.mediaChunks,
3108
+ firmwareHash: obj.firmwareHash,
3109
+ attestationRef: obj.attestationRef
3095
3110
  });
3096
3111
  }
3097
3112
  };
package/dist/rcan.iife.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var RCAN=(()=>{var we=Object.defineProperty;var Gt=Object.getOwnPropertyDescriptor;var Wt=Object.getOwnPropertyNames;var zt=Object.prototype.hasOwnProperty;var ze=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var Qt=(n,e)=>{for(var t in e)we(n,t,{get:e[t],enumerable:!0})},Xt=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Wt(e))!zt.call(n,s)&&s!==t&&we(n,s,{get:()=>e[s],enumerable:!(r=Gt(e,s))||r.enumerable});return n};var Zt=n=>Xt(we({},"__esModule",{value:!0}),n);var vn={};Qt(vn,{AuditChain:()=>ee,AuditError:()=>Z,ClockDriftError:()=>B,CommitmentRecord:()=>$,ConfidenceGate:()=>Q,DEFAULT_LOA_POLICY:()=>Le,DataCategory:()=>Pe,FaultCode:()=>De,FederationSyncType:()=>je,GateError:()=>O,HiTLGate:()=>X,KeyStore:()=>he,LevelOfAssurance:()=>Ae,MediaEncoding:()=>Ve,MessageType:()=>C,NodeClient:()=>ue,OfflineModeManager:()=>_e,PRODUCTION_LOA_POLICY:()=>Ot,QoSAckTimeoutError:()=>le,QoSLevel:()=>Ie,QoSManager:()=>ge,RCANAddressError:()=>te,RCANConfigAuthorizationError:()=>ce,RCANDelegationChainError:()=>ae,RCANError:()=>R,RCANGateError:()=>re,RCANMessage:()=>l,RCANMessageError:()=>v,RCANNodeError:()=>L,RCANNodeNotFoundError:()=>k,RCANNodeSyncError:()=>y,RCANNodeTrustError:()=>j,RCANRegistryError:()=>D,RCANReplayAttackError:()=>ie,RCANSignatureError:()=>se,RCANValidationError:()=>ne,RCANVersionIncompatibleError:()=>oe,RCAN_VERSION:()=>bn,RegistryClient:()=>de,RegistryTier:()=>$e,ReplayCache:()=>pe,RevocationCache:()=>K,RobotURI:()=>U,RobotURIError:()=>M,SAFETY_MESSAGE_TYPE:()=>Me,SDK_VERSION:()=>Qe,SPEC_VERSION:()=>b,TransportEncoding:()=>He,TransportError:()=>x,TrustAnchorCache:()=>ve,VERSION:()=>An,addDelegationHop:()=>et,addMediaInline:()=>Ge,addMediaRef:()=>Ft,assertClockSynced:()=>yt,checkClockSync:()=>Ue,checkRevocation:()=>bt,decodeBleFrames:()=>$t,decodeCompact:()=>Ke,decodeMinimal:()=>Dt,encodeBleFrames:()=>Lt,encodeCompact:()=>Be,encodeMinimal:()=>Pt,extractLoaFromJwt:()=>be,fetchCanonicalSchema:()=>fe,isSafetyMessage:()=>gt,makeCloudRelayMessage:()=>Ze,makeConfigUpdate:()=>Rt,makeConsentDeny:()=>Se,makeConsentGrant:()=>Re,makeConsentRequest:()=>ye,makeEstopMessage:()=>ut,makeEstopWithQoS:()=>dt,makeFaultReport:()=>Nt,makeFederationSync:()=>Tt,makeKeyRotationMessage:()=>_t,makeResumeMessage:()=>lt,makeRevocationBroadcast:()=>vt,makeStopMessage:()=>ft,makeStreamChunk:()=>Kt,makeTrainingConsentDeny:()=>Et,makeTrainingConsentGrant:()=>Ct,makeTrainingConsentRequest:()=>xt,makeTrainingDataMessage:()=>Bt,makeTransparencyMessage:()=>pt,selectTransport:()=>jt,validateConfig:()=>it,validateConfigAgainstSchema:()=>at,validateConfigUpdate:()=>St,validateConsentMessage:()=>At,validateCrossRegistryCommand:()=>It,validateDelegationChain:()=>tt,validateLoaForScope:()=>kt,validateMediaChunks:()=>Ht,validateMessage:()=>ot,validateNodeAgainstSchema:()=>ct,validateReplay:()=>ht,validateSafetyMessage:()=>mt,validateTrainingDataMessage:()=>wt,validateURI:()=>st,validateVersionCompat:()=>Xe});var M=class extends Error{constructor(e){super(e),this.name="RobotURIError"}},U=class n{registry;manufacturer;model;version;deviceId;constructor(e){this.registry=e.registry,this.manufacturer=e.manufacturer,this.model=e.model,this.version=e.version,this.deviceId=e.deviceId}static parse(e){if(!e.startsWith("rcan://"))throw new M(`URI must start with 'rcan://' \u2014 got: ${e}`);let r=e.slice(7).split("/");if(r.length!==5)throw new M(`URI must have exactly 5 path segments (registry/manufacturer/model/version/device-id) \u2014 got ${r.length} in: ${e}`);let[s,o,i,a,c]=r;for(let[u,f]of[["registry",s],["manufacturer",o],["model",i],["version",a],["device-id",c]])if(!f||f.trim()==="")throw new M(`URI segment '${u}' must not be empty`);return new n({registry:s,manufacturer:o,model:i,version:a,deviceId:c})}static build(e){let t=e.registry??"registry.rcan.dev",{manufacturer:r,model:s,version:o,deviceId:i}=e;for(let[a,c]of[["manufacturer",r],["model",s],["version",o],["deviceId",i]])if(!c||c.trim()==="")throw new M(`'${a}' must not be empty`);return new n({registry:t,manufacturer:r,model:s,version:o,deviceId:i})}toString(){return`rcan://${this.registry}/${this.manufacturer}/${this.model}/${this.version}/${this.deviceId}`}get namespace(){return`${this.manufacturer}/${this.model}`}get registryUrl(){return`https://${this.registry}/registry/${this.manufacturer}/${this.model}/${this.version}/${this.deviceId}`}equals(e){return this.toString()===e.toString()}toJSON(){return{uri:this.toString(),registry:this.registry,manufacturer:this.manufacturer,model:this.model,version:this.version,deviceId:this.deviceId}}};var b="1.6",Qe="0.6.0";function Xe(n,e="1.6"){let t=o=>{let i=o.split("."),a=parseInt(i[0]??"0",10),c=parseInt(i[1]??"0",10);return[isNaN(a)?0:a,isNaN(c)?0:c]},[r]=t(n),[s]=t(e);return r===s}var C=(d=>(d[d.COMMAND=1]="COMMAND",d[d.RESPONSE=2]="RESPONSE",d[d.STATUS=3]="STATUS",d[d.HEARTBEAT=4]="HEARTBEAT",d[d.CONFIG=5]="CONFIG",d[d.SAFETY=6]="SAFETY",d[d.SENSOR_DATA=7]="SENSOR_DATA",d[d.AUDIT=8]="AUDIT",d[d.DISCOVER=9]="DISCOVER",d[d.TRAINING_DATA=10]="TRAINING_DATA",d[d.TRANSPARENCY=11]="TRANSPARENCY",d[d.FEDERATION_SYNC=12]="FEDERATION_SYNC",d[d.ALERT=13]="ALERT",d[d.TELEOP=14]="TELEOP",d[d.CHAT=15]="CHAT",d[d.ERROR=16]="ERROR",d[d.COMMAND_ACK=17]="COMMAND_ACK",d[d.COMMAND_COMMIT=18]="COMMAND_COMMIT",d[d.ROBOT_REVOCATION=19]="ROBOT_REVOCATION",d[d.CONSENT_REQUEST=20]="CONSENT_REQUEST",d[d.CONSENT_GRANT=21]="CONSENT_GRANT",d[d.CONSENT_DENY=22]="CONSENT_DENY",d[d.FLEET_COMMAND=23]="FLEET_COMMAND",d[d.SUBSCRIBE=24]="SUBSCRIBE",d[d.UNSUBSCRIBE=25]="UNSUBSCRIBE",d[d.FAULT_REPORT=26]="FAULT_REPORT",d[d.COMMAND_NACK=27]="COMMAND_NACK",d))(C||{}),v=class extends Error{constructor(e){super(e),this.name="RCANMessageError"}},l=class n{rcan;rcanVersion;cmd;target;params;confidence;modelIdentity;signature;timestamp;senderType;cloudProvider;keyId;delegationChain;groupId;qos;presenceVerified;proximityMeters;readOnly;loa;transportEncoding;mediaChunks;constructor(e){if(!e.cmd||e.cmd.trim()==="")throw new v("'cmd' is required");if(!e.target)throw new v("'target' is required");if(this.rcan=e.rcan??"1.6",this.rcanVersion=e.rcanVersion??"1.6",this.cmd=e.cmd,this.target=e.target instanceof U?e.target.toString():String(e.target),this.params=e.params??{},this.confidence=e.confidence,this.modelIdentity=e.modelIdentity??e.model_identity,this.signature=e.signature,this.timestamp=e.timestamp??new Date().toISOString(),this.senderType=e.senderType,this.cloudProvider=e.cloudProvider,this.keyId=e.keyId,this.delegationChain=e.delegationChain,this.groupId=e.groupId,this.qos=e.qos,this.presenceVerified=e.presenceVerified,this.proximityMeters=e.proximityMeters,this.readOnly=e.readOnly,this.loa=e.loa,this.transportEncoding=e.transportEncoding,this.mediaChunks=e.mediaChunks,this.confidence!==void 0&&(this.confidence<0||this.confidence>1))throw new v(`confidence must be in [0.0, 1.0] \u2014 got ${this.confidence}`)}get isSigned(){return this.signature!==void 0&&this.signature.sig!==""}get isAiDriven(){return this.confidence!==void 0}toJSON(){let e={rcan:this.rcan,rcanVersion:this.rcanVersion,cmd:this.cmd,target:this.target,timestamp:this.timestamp};return Object.keys(this.params).length>0&&(e.params=this.params),this.confidence!==void 0&&(e.confidence=this.confidence),this.modelIdentity&&(e.model_identity=this.modelIdentity),this.signature&&(e.signature=this.signature),this.senderType!==void 0&&(e.senderType=this.senderType),this.cloudProvider!==void 0&&(e.cloudProvider=this.cloudProvider),this.keyId!==void 0&&(e.keyId=this.keyId),this.delegationChain!==void 0&&(e.delegationChain=this.delegationChain),this.groupId!==void 0&&(e.groupId=this.groupId),this.qos!==void 0&&(e.qos=this.qos),this.presenceVerified!==void 0&&(e.presenceVerified=this.presenceVerified),this.proximityMeters!==void 0&&(e.proximityMeters=this.proximityMeters),this.readOnly!==void 0&&(e.readOnly=this.readOnly),this.loa!==void 0&&(e.loa=this.loa),this.transportEncoding!==void 0&&(e.transportEncoding=this.transportEncoding),this.mediaChunks!==void 0&&(e.mediaChunks=this.mediaChunks),e}toJSONString(e){return JSON.stringify(this.toJSON(),null,e)}static fromJSON(e){let t;if(typeof e=="string")try{t=JSON.parse(e)}catch{throw new v("Invalid JSON string")}else t=e;if(!t.cmd)throw new v("Missing required field: 'cmd'");if(!t.target)throw new v("Missing required field: 'target'");if(!t.rcan)throw new v("Missing required field: 'rcan'");return new n({rcan:t.rcan,rcanVersion:t.rcanVersion,cmd:t.cmd,target:t.target,params:t.params??{},confidence:t.confidence,modelIdentity:t.model_identity??t.modelIdentity,signature:t.signature,timestamp:t.timestamp,senderType:t.senderType,cloudProvider:t.cloudProvider,keyId:t.keyId,delegationChain:t.delegationChain,groupId:t.groupId,qos:t.qos,presenceVerified:t.presenceVerified,proximityMeters:t.proximityMeters,readOnly:t.readOnly,loa:t.loa,transportEncoding:t.transportEncoding,mediaChunks:t.mediaChunks})}};function Ze(n,e){let t=n.toJSON();return t.senderType="cloud_function",t.cloudProvider=e,new l(t)}function et(n,e){let t=n.delegationChain?[...n.delegationChain,e]:[e],r=n.toJSON();return r.delegationChain=t,new l(r)}function tt(n){if(n.length>4)return{valid:!1,reason:"DELEGATION_CHAIN_EXCEEDED: max depth is 4 hops"};for(let e=0;e<n.length;e++){let t=n[e];if(!t)return{valid:!1,reason:`hop ${e} is undefined`};if(!t.issuerRuri)return{valid:!1,reason:`hop ${e}: missing issuerRuri`};if(!t.humanSubject)return{valid:!1,reason:`hop ${e}: missing humanSubject`};if(!t.timestamp)return{valid:!1,reason:`hop ${e}: missing timestamp`};if(!t.scope)return{valid:!1,reason:`hop ${e}: missing scope`};if(!t.signature)return{valid:!1,reason:`hop ${e}: missing signature`}}return{valid:!0,reason:"ok"}}function z(){if(typeof globalThis.crypto<"u"&&typeof globalThis.crypto.randomUUID=="function")return globalThis.crypto.randomUUID();try{let{randomUUID:n}=ze("crypto");return n()}catch{return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,n=>{let e=Math.random()*16|0;return(n==="x"?e:e&3|8).toString(16)})}}function Oe(n,e){return typeof process<"u",tn(n,e)}function Ne(n){let e=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],t=1779033703,r=3144134277,s=1013904242,o=2773480762,i=1359893119,a=2600822924,c=528734635,u=1541459225,q=n.length*8,A=[...n];for(A.push(128);A.length%64!==56;)A.push(0);for(let h=7;h>=0;h--)A.push(q/Math.pow(2,h*8)&255);for(let h=0;h<A.length;h+=64){let S=[];for(let m=0;m<16;m++)S[m]=A[h+m*4]<<24|A[h+m*4+1]<<16|A[h+m*4+2]<<8|A[h+m*4+3];for(let m=16;m<64;m++){let Ee=E(S[m-15],7)^E(S[m-15],18)^S[m-15]>>>3,d=E(S[m-2],17)^E(S[m-2],19)^S[m-2]>>>10;S[m]=S[m-16]+Ee+S[m-7]+d>>>0}let[T,F,H,xe,I,G,W,Ce]=[t,r,s,o,i,a,c,u];for(let m=0;m<64;m++){let Ee=E(I,6)^E(I,11)^E(I,25),d=I&G^~I&W,We=Ce+Ee+d+e[m]+S[m]>>>0,Vt=E(T,2)^E(T,13)^E(T,22),Yt=T&F^T&H^F&H,Jt=Vt+Yt>>>0;[Ce,W,G,I,xe,H,F,T]=[W,G,I,xe+We>>>0,H,F,T,We+Jt>>>0]}t=t+T>>>0,r=r+F>>>0,s=s+H>>>0,o=o+xe>>>0,i=i+I>>>0,a=a+G>>>0,c=c+W>>>0,u=u+Ce>>>0}let p=new Uint8Array(32),N=new DataView(p.buffer);return[t,r,s,o,i,a,c,u].forEach((h,S)=>N.setUint32(S*4,h)),p}function E(n,e){return n>>>e|n<<32-e}function nt(n){if(typeof TextEncoder<"u")return new TextEncoder().encode(n);let e=new Uint8Array(n.length);for(let t=0;t<n.length;t++)e[t]=n.charCodeAt(t)&255;return e}function en(n){return Array.from(n).map(e=>e.toString(16).padStart(2,"0")).join("")}function tn(n,e){let r=nt(n);r.length>64&&(r=Ne(r));let s=new Uint8Array(64),o=new Uint8Array(64);for(let f=0;f<64;f++)s[f]=(r[f]??0)^54,o[f]=(r[f]??0)^92;let i=nt(e),a=new Uint8Array(64+i.length);a.set(s),a.set(i,64);let c=Ne(a),u=new Uint8Array(96);return u.set(o),u.set(c,64),en(Ne(u))}var O=class extends Error{constructor(e){super(e),this.name="GateError"}},Q=class{threshold;constructor(e=.8){if(e<0||e>1)throw new O(`threshold must be in [0.0, 1.0] \u2014 got ${e}`);this.threshold=e}allows(e){return e>=this.threshold}margin(e){return e-this.threshold}assert(e,t){if(!this.allows(e)){let r=t?` for action '${t}'`:"";throw new O(`Confidence ${e}${r} is below threshold ${this.threshold}`)}}},X=class{_pending=new Map;request(e,t={}){let r=z();return this._pending.set(r,{token:r,action:e,context:t,createdAt:new Date().toISOString(),status:"pending"}),r}approve(e){let t=this._pending.get(e);if(!t)throw new O(`Unknown token: ${e}`);t.status="approved"}deny(e,t){let r=this._pending.get(e);if(!r)throw new O(`Unknown token: ${e}`);r.status="denied",t&&(r.reason=t)}check(e){let t=this._pending.get(e);if(!t)throw new O(`Unknown token: ${e}`);return t.status}get pendingApprovals(){return Array.from(this._pending.values()).filter(e=>e.status==="pending")}getApproval(e){return this._pending.get(e)}clearResolved(){for(let[e,t]of this._pending.entries())t.status!=="pending"&&this._pending.delete(e)}};var Z=class extends Error{constructor(e){super(e),this.name="AuditError"}};function nn(n,e,t,r,s){let o=JSON.stringify({recordId:n,action:e,robotUri:t,timestamp:r,params:s},Object.keys({recordId:n,action:e,robotUri:t,timestamp:r,params:s}).sort());return Oe("rcan-content-hash",o)}function rt(n,e){let{hmac:t,...r}=e,s=JSON.stringify(r,Object.keys(r).sort());return Oe(n,s)}var $=class n{recordId;action;robotUri;confidence;modelIdentity;params;safetyApproved;timestamp;contentHash;previousHash;hmac;constructor(e){this.recordId=e.recordId,this.action=e.action,this.robotUri=e.robotUri,this.confidence=e.confidence,this.modelIdentity=e.modelIdentity,this.params=e.params,this.safetyApproved=e.safetyApproved,this.timestamp=e.timestamp,this.contentHash=e.contentHash,this.previousHash=e.previousHash,this.hmac=e.hmac}static create(e,t,r=null){let s=z(),o=new Date().toISOString(),i=e.params??{},a=e.robotUri??"",c=nn(s,e.action,a,o,i),u={recordId:s,action:e.action,robotUri:a,confidence:e.confidence,modelIdentity:e.modelIdentity,params:i,safetyApproved:e.safetyApproved??!0,timestamp:o,contentHash:c,previousHash:r,hmac:""};return u.hmac=rt(t,u),new n(u)}verify(e){return rt(e,this.toJSON())===this.hmac}toJSON(){return{recordId:this.recordId,action:this.action,robotUri:this.robotUri,confidence:this.confidence,modelIdentity:this.modelIdentity,params:this.params,safetyApproved:this.safetyApproved,timestamp:this.timestamp,contentHash:this.contentHash,previousHash:this.previousHash,hmac:this.hmac}}static fromJSON(e){return new n(e)}},ee=class n{_records=[];_secret;constructor(e){this._secret=e}get records(){return this._records}append(e){let r=this._records[this._records.length-1]?.contentHash??null,s=$.create(e,this._secret,r);return this._records.push(s),s}verifyAll(){let e=[],t=null;for(let r of this._records)r.verify(this._secret)||e.push(`HMAC invalid for record ${r.recordId.slice(0,8)}`),t!==null&&r.previousHash!==t&&e.push(`Chain broken at ${r.recordId.slice(0,8)}: expected prev=${t.slice(0,12)}`),t=r.contentHash;return{valid:e.length===0,count:this._records.length,errors:e}}toJSONL(){return this._records.map(e=>JSON.stringify(e.toJSON())).join(`
1
+ "use strict";var RCAN=(()=>{var qr=Object.create;var te=Object.defineProperty;var Fr=Object.getOwnPropertyDescriptor;var Hr=Object.getOwnPropertyNames;var Br=Object.getPrototypeOf,Vr=Object.prototype.hasOwnProperty;var re=(t=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(t,{get:(e,r)=>(typeof require<"u"?require:e)[r]}):t)(function(t){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});var Kr=(t,e)=>{for(var r in e)te(t,r,{get:e[r],enumerable:!0})},gt=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Hr(e))!Vr.call(t,s)&&s!==r&&te(t,s,{get:()=>e[s],enumerable:!(n=Fr(e,s))||n.enumerable});return t};var ht=(t,e,r)=>(r=t!=null?qr(Br(t)):{},gt(e||!t||!t.__esModule?te(r,"default",{value:t,enumerable:!0}):r,t)),Jr=t=>gt(te({},"__esModule",{value:!0}),t);var Sn={};Kr(Sn,{AUTHORITY_ERROR_CODES:()=>Mr,AuditChain:()=>ae,AuditError:()=>ie,COMPETITION_SCOPE_LEVEL:()=>it,CONTRIBUTE_SCOPE_LEVEL:()=>ot,ClockDriftError:()=>G,CommitmentRecord:()=>F,ConfidenceGate:()=>se,DEFAULT_LOA_POLICY:()=>zt,DataCategory:()=>Be,FIRMWARE_MANIFEST_PATH:()=>wr,FaultCode:()=>Ve,FederationSyncType:()=>Ye,FirmwareIntegrityError:()=>Ne,GateError:()=>N,HiTLGate:()=>oe,KeyStore:()=>Ae,LevelOfAssurance:()=>Yt,M2MAuthError:()=>S,M2M_TRUSTED_ISSUER:()=>ke,MediaEncoding:()=>tt,MessageType:()=>x,NodeClient:()=>ye,OfflineModeManager:()=>Te,PRODUCTION_LOA_POLICY:()=>Qt,QoSAckTimeoutError:()=>_e,QoSLevel:()=>qe,QoSManager:()=>Se,RCANAddressError:()=>ce,RCANConfigAuthorizationError:()=>ge,RCANDelegationChainError:()=>pe,RCANError:()=>_,RCANGateError:()=>ue,RCANMessage:()=>m,RCANMessageError:()=>C,RCANNodeError:()=>q,RCANNodeNotFoundError:()=>k,RCANNodeSyncError:()=>R,RCANNodeTrustError:()=>H,RCANRegistryError:()=>j,RCANReplayAttackError:()=>me,RCANSignatureError:()=>fe,RCANValidationError:()=>de,RCANVersionIncompatibleError:()=>le,RCAN_VERSION:()=>_n,ROLE_JWT_LEVEL:()=>B,RRF_REVOCATION_CACHE_TTL_MS:()=>dt,RRF_REVOCATION_URL:()=>ct,RegistryClient:()=>he,RegistryTier:()=>We,ReplayCache:()=>be,RevocationCache:()=>W,RobotURI:()=>P,RobotURIError:()=>U,Role:()=>D,SAFETY_MESSAGE_TYPE:()=>Fe,SCOPE_MIN_ROLE:()=>Ke,SDK_VERSION:()=>yt,SPEC_VERSION:()=>h,TransportEncoding:()=>Xe,TransportError:()=>w,TrustAnchorCache:()=>Oe,VERSION:()=>Rn,addDelegationHop:()=>St,addMediaInline:()=>st,addMediaRef:()=>fr,assertClockSynced:()=>$t,authorityAccessFromWire:()=>kr,authorityAccessToWire:()=>Nr,canonicalManifestJson:()=>vr,checkClockSync:()=>He,checkRevocation:()=>Ht,decodeBleFrames:()=>cr,decodeCompact:()=>et,decodeMinimal:()=>ir,encodeBleFrames:()=>ar,encodeCompact:()=>Ze,encodeMinimal:()=>or,extractIdentityFromJwt:()=>Zt,extractLoaFromJwt:()=>ve,extractRoleFromJwt:()=>Je,fetchCanonicalSchema:()=>Re,fetchRRFRevocations:()=>lt,isAuthorityRequestValid:()=>Ir,isM2mTrustedRevoked:()=>mt,isPreemptedBy:()=>_r,isSafetyMessage:()=>Mt,makeCloudRelayMessage:()=>_t,makeCompetitionEnter:()=>Sr,makeCompetitionScore:()=>Er,makeConfigUpdate:()=>Lt,makeConsentDeny:()=>xe,makeConsentGrant:()=>we,makeConsentRequest:()=>Ce,makeContributeCancel:()=>yr,makeContributeRequest:()=>gr,makeContributeResult:()=>hr,makeEstopMessage:()=>Nt,makeEstopWithQoS:()=>Ot,makeFaultReport:()=>Wt,makeFederationSync:()=>tr,makeKeyRotationMessage:()=>qt,makePersonalResearchResult:()=>Ar,makeResumeMessage:()=>It,makeRevocationBroadcast:()=>Bt,makeSeasonStanding:()=>br,makeStopMessage:()=>kt,makeStreamChunk:()=>pr,makeTrainingConsentDeny:()=>Jt,makeTrainingConsentGrant:()=>Kt,makeTrainingConsentRequest:()=>Vt,makeTrainingDataMessage:()=>mr,makeTransparencyMessage:()=>Pt,manifestFromWire:()=>Tr,manifestToWire:()=>xr,parseM2mPeerToken:()=>Pr,parseM2mTrustedToken:()=>ut,roleFromJwtLevel:()=>Y,selectTransport:()=>dr,validateAuthorityAccess:()=>at,validateCompetitionScope:()=>Cr,validateConfig:()=>xt,validateConfigAgainstSchema:()=>Tt,validateConfigUpdate:()=>jt,validateConsentMessage:()=>Ft,validateContributeScope:()=>Rr,validateCrossRegistryCommand:()=>rr,validateDelegationChain:()=>Et,validateLoaForScope:()=>er,validateManifest:()=>Or,validateMediaChunks:()=>lr,validateMessage:()=>wt,validateNodeAgainstSchema:()=>vt,validateReplay:()=>Dt,validateRoleForScope:()=>Ge,validateSafetyMessage:()=>Ut,validateTrainingDataMessage:()=>Gt,validateURI:()=>Ct,validateVersionCompat:()=>Rt,verifyM2mTrustedToken:()=>Dr,verifyM2mTrustedTokenClaims:()=>ft});var U=class extends Error{constructor(e){super(e),this.name="RobotURIError"}},P=class t{registry;manufacturer;model;version;deviceId;constructor(e){this.registry=e.registry,this.manufacturer=e.manufacturer,this.model=e.model,this.version=e.version,this.deviceId=e.deviceId}static parse(e){if(!e.startsWith("rcan://"))throw new U(`URI must start with 'rcan://' \u2014 got: ${e}`);let n=e.slice(7).split("/");if(n.length!==5)throw new U(`URI must have exactly 5 path segments (registry/manufacturer/model/version/device-id) \u2014 got ${n.length} in: ${e}`);let[s,o,i,a,d]=n;for(let[u,l]of[["registry",s],["manufacturer",o],["model",i],["version",a],["device-id",d]])if(!l||l.trim()==="")throw new U(`URI segment '${u}' must not be empty`);return new t({registry:s,manufacturer:o,model:i,version:a,deviceId:d})}static build(e){let r=e.registry??"registry.rcan.dev",{manufacturer:n,model:s,version:o,deviceId:i}=e;for(let[a,d]of[["manufacturer",n],["model",s],["version",o],["deviceId",i]])if(!d||d.trim()==="")throw new U(`'${a}' must not be empty`);return new t({registry:r,manufacturer:n,model:s,version:o,deviceId:i})}toString(){return`rcan://${this.registry}/${this.manufacturer}/${this.model}/${this.version}/${this.deviceId}`}get namespace(){return`${this.manufacturer}/${this.model}`}get registryUrl(){return`https://${this.registry}/registry/${this.manufacturer}/${this.model}/${this.version}/${this.deviceId}`}equals(e){return this.toString()===e.toString()}toJSON(){return{uri:this.toString(),registry:this.registry,manufacturer:this.manufacturer,model:this.model,version:this.version,deviceId:this.deviceId}}};var h="2.1.0",yt="1.1.0";function Rt(t,e=h){let r=o=>{let i=o.split("."),a=parseInt(i[0]??"0",10),d=parseInt(i[1]??"0",10);return[isNaN(a)?0:a,isNaN(d)?0:d]},[n]=r(t),[s]=r(e);return n===s}var x=(c=>(c[c.COMMAND=1]="COMMAND",c[c.RESPONSE=2]="RESPONSE",c[c.STATUS=3]="STATUS",c[c.HEARTBEAT=4]="HEARTBEAT",c[c.CONFIG=5]="CONFIG",c[c.SAFETY=6]="SAFETY",c[c.AUTH=7]="AUTH",c[c.ERROR=8]="ERROR",c[c.DISCOVER=9]="DISCOVER",c[c.PENDING_AUTH=10]="PENDING_AUTH",c[c.INVOKE=11]="INVOKE",c[c.INVOKE_RESULT=12]="INVOKE_RESULT",c[c.INVOKE_CANCEL=13]="INVOKE_CANCEL",c[c.REGISTRY_REGISTER=14]="REGISTRY_REGISTER",c[c.REGISTRY_RESOLVE=15]="REGISTRY_RESOLVE",c[c.TRANSPARENCY=16]="TRANSPARENCY",c[c.COMMAND_ACK=17]="COMMAND_ACK",c[c.COMMAND_NACK=18]="COMMAND_NACK",c[c.ROBOT_REVOCATION=19]="ROBOT_REVOCATION",c[c.CONSENT_REQUEST=20]="CONSENT_REQUEST",c[c.CONSENT_GRANT=21]="CONSENT_GRANT",c[c.CONSENT_DENY=22]="CONSENT_DENY",c[c.FLEET_COMMAND=23]="FLEET_COMMAND",c[c.SUBSCRIBE=24]="SUBSCRIBE",c[c.UNSUBSCRIBE=25]="UNSUBSCRIBE",c[c.FAULT_REPORT=26]="FAULT_REPORT",c[c.KEY_ROTATION=27]="KEY_ROTATION",c[c.COMMAND_COMMIT=28]="COMMAND_COMMIT",c[c.SENSOR_DATA=29]="SENSOR_DATA",c[c.TRAINING_CONSENT_REQUEST=30]="TRAINING_CONSENT_REQUEST",c[c.TRAINING_CONSENT_GRANT=31]="TRAINING_CONSENT_GRANT",c[c.TRAINING_CONSENT_DENY=32]="TRAINING_CONSENT_DENY",c[c.CONTRIBUTE_REQUEST=33]="CONTRIBUTE_REQUEST",c[c.CONTRIBUTE_RESULT=34]="CONTRIBUTE_RESULT",c[c.CONTRIBUTE_CANCEL=35]="CONTRIBUTE_CANCEL",c[c.TRAINING_DATA=36]="TRAINING_DATA",c[c.COMPETITION_ENTER=37]="COMPETITION_ENTER",c[c.COMPETITION_SCORE=38]="COMPETITION_SCORE",c[c.SEASON_STANDING=39]="SEASON_STANDING",c[c.PERSONAL_RESEARCH_RESULT=40]="PERSONAL_RESEARCH_RESULT",c[c.AUTHORITY_ACCESS=41]="AUTHORITY_ACCESS",c[c.AUTHORITY_RESPONSE=42]="AUTHORITY_RESPONSE",c[c.FIRMWARE_ATTESTATION=43]="FIRMWARE_ATTESTATION",c[c.SBOM_UPDATE=44]="SBOM_UPDATE",c))(x||{}),C=class extends Error{constructor(e){super(e),this.name="RCANMessageError"}},m=class t{rcan;rcanVersion;cmd;target;params;confidence;modelIdentity;signature;timestamp;senderType;cloudProvider;keyId;delegationChain;groupId;qos;presenceVerified;proximityMeters;readOnly;loa;transportEncoding;mediaChunks;firmwareHash;attestationRef;constructor(e){if(!e.cmd||e.cmd.trim()==="")throw new C("'cmd' is required");if(!e.target)throw new C("'target' is required");if(this.rcan=e.rcan??h,this.rcanVersion=e.rcanVersion??h,this.cmd=e.cmd,this.target=e.target instanceof P?e.target.toString():String(e.target),this.params=e.params??{},this.confidence=e.confidence,this.modelIdentity=e.modelIdentity??e.model_identity,this.signature=e.signature,this.timestamp=e.timestamp??new Date().toISOString(),this.senderType=e.senderType,this.cloudProvider=e.cloudProvider,this.keyId=e.keyId,this.delegationChain=e.delegationChain,this.groupId=e.groupId,this.qos=e.qos,this.presenceVerified=e.presenceVerified,this.proximityMeters=e.proximityMeters,this.readOnly=e.readOnly,this.loa=e.loa,this.transportEncoding=e.transportEncoding,this.mediaChunks=e.mediaChunks,this.firmwareHash=e.firmwareHash,this.attestationRef=e.attestationRef,this.signature!==void 0&&this.signature.sig==="pending")throw new C("signature.sig:'pending' is not valid in RCAN v2.1. Sign the message before sending.");if(this.confidence!==void 0&&(this.confidence<0||this.confidence>1))throw new C(`confidence must be in [0.0, 1.0] \u2014 got ${this.confidence}`)}get isSigned(){return this.signature!==void 0&&this.signature.sig!==""}get isAiDriven(){return this.confidence!==void 0}toJSON(){let e={rcan:this.rcan,rcanVersion:this.rcanVersion,cmd:this.cmd,target:this.target,timestamp:this.timestamp};return Object.keys(this.params).length>0&&(e.params=this.params),this.confidence!==void 0&&(e.confidence=this.confidence),this.modelIdentity&&(e.model_identity=this.modelIdentity),this.signature&&(e.signature=this.signature),this.senderType!==void 0&&(e.senderType=this.senderType),this.cloudProvider!==void 0&&(e.cloudProvider=this.cloudProvider),this.keyId!==void 0&&(e.keyId=this.keyId),this.delegationChain!==void 0&&(e.delegationChain=this.delegationChain),this.groupId!==void 0&&(e.groupId=this.groupId),this.qos!==void 0&&(e.qos=this.qos),this.presenceVerified!==void 0&&(e.presenceVerified=this.presenceVerified),this.proximityMeters!==void 0&&(e.proximityMeters=this.proximityMeters),this.readOnly!==void 0&&(e.readOnly=this.readOnly),this.loa!==void 0&&(e.loa=this.loa),this.transportEncoding!==void 0&&(e.transportEncoding=this.transportEncoding),this.mediaChunks!==void 0&&(e.mediaChunks=this.mediaChunks),this.firmwareHash!==void 0&&(e.firmwareHash=this.firmwareHash),this.attestationRef!==void 0&&(e.attestationRef=this.attestationRef),e}toJSONString(e){return JSON.stringify(this.toJSON(),null,e)}static fromJSON(e){let r;if(typeof e=="string")try{r=JSON.parse(e)}catch{throw new C("Invalid JSON string")}else r=e;if(!r.cmd)throw new C("Missing required field: 'cmd'");if(!r.target)throw new C("Missing required field: 'target'");if(!r.rcan)throw new C("Missing required field: 'rcan'");return new t({rcan:r.rcan,rcanVersion:r.rcanVersion,cmd:r.cmd,target:r.target,params:r.params??{},confidence:r.confidence,modelIdentity:r.model_identity??r.modelIdentity,signature:r.signature,timestamp:r.timestamp,senderType:r.senderType,cloudProvider:r.cloudProvider,keyId:r.keyId,delegationChain:r.delegationChain,groupId:r.groupId,qos:r.qos,presenceVerified:r.presenceVerified,proximityMeters:r.proximityMeters,readOnly:r.readOnly,loa:r.loa,transportEncoding:r.transportEncoding,mediaChunks:r.mediaChunks,firmwareHash:r.firmwareHash,attestationRef:r.attestationRef})}};function _t(t,e){let r=t.toJSON();return r.senderType="cloud_function",r.cloudProvider=e,new m(r)}function St(t,e){let r=t.delegationChain?[...t.delegationChain,e]:[e],n=t.toJSON();return n.delegationChain=r,new m(n)}function Et(t){if(t.length>4)return{valid:!1,reason:"DELEGATION_CHAIN_EXCEEDED: max depth is 4 hops"};for(let e=0;e<t.length;e++){let r=t[e];if(!r)return{valid:!1,reason:`hop ${e} is undefined`};if(!r.issuerRuri)return{valid:!1,reason:`hop ${e}: missing issuerRuri`};if(!r.humanSubject)return{valid:!1,reason:`hop ${e}: missing humanSubject`};if(!r.timestamp)return{valid:!1,reason:`hop ${e}: missing timestamp`};if(!r.scope)return{valid:!1,reason:`hop ${e}: missing scope`};if(!r.signature)return{valid:!1,reason:`hop ${e}: missing signature`}}return{valid:!0,reason:"ok"}}function ne(){if(typeof globalThis.crypto<"u"&&typeof globalThis.crypto.randomUUID=="function")return globalThis.crypto.randomUUID();try{let{randomUUID:t}=re("crypto");return t()}catch{return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,t=>{let e=Math.random()*16|0;return(t==="x"?e:e&3|8).toString(16)})}}function $e(t,e){return typeof process<"u",Wr(t,e)}function De(t){let e=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],r=1779033703,n=3144134277,s=1013904242,o=2773480762,i=1359893119,a=2600822924,d=528734635,u=1541459225,V=t.length*8,A=[...t];for(A.push(128);A.length%64!==56;)A.push(0);for(let y=7;y>=0;y--)A.push(V/Math.pow(2,y*8)&255);for(let y=0;y<A.length;y+=64){let E=[];for(let p=0;p<16;p++)E[p]=A[y+p*4]<<24|A[y+p*4+1]<<16|A[y+p*4+2]<<8|A[y+p*4+3];for(let p=16;p<64;p++){let Ue=T(E[p-15],7)^T(E[p-15],18)^E[p-15]>>>3,Pe=T(E[p-2],17)^T(E[p-2],19)^E[p-2]>>>10;E[p]=E[p-16]+Ue+E[p-7]+Pe>>>0}let[I,K,J,Ie,M,Z,ee,Me]=[r,n,s,o,i,a,d,u];for(let p=0;p<64;p++){let Ue=T(M,6)^T(M,11)^T(M,25),Pe=M&Z^~M&ee,pt=Me+Ue+Pe+e[p]+E[p]>>>0,$r=T(I,2)^T(I,13)^T(I,22),Lr=I&K^I&J^K&J,jr=$r+Lr>>>0;[Me,ee,Z,M,Ie,J,K,I]=[ee,Z,M,Ie+pt>>>0,J,K,I,pt+jr>>>0]}r=r+I>>>0,n=n+K>>>0,s=s+J>>>0,o=o+Ie>>>0,i=i+M>>>0,a=a+Z>>>0,d=d+ee>>>0,u=u+Me>>>0}let g=new Uint8Array(32),O=new DataView(g.buffer);return[r,n,s,o,i,a,d,u].forEach((y,E)=>O.setUint32(E*4,y)),g}function T(t,e){return t>>>e|t<<32-e}function bt(t){if(typeof TextEncoder<"u")return new TextEncoder().encode(t);let e=new Uint8Array(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r)&255;return e}function Gr(t){return Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join("")}function Wr(t,e){let n=bt(t);n.length>64&&(n=De(n));let s=new Uint8Array(64),o=new Uint8Array(64);for(let l=0;l<64;l++)s[l]=(n[l]??0)^54,o[l]=(n[l]??0)^92;let i=bt(e),a=new Uint8Array(64+i.length);a.set(s),a.set(i,64);let d=De(a),u=new Uint8Array(96);return u.set(o),u.set(d,64),Gr(De(u))}var N=class extends Error{constructor(e){super(e),this.name="GateError"}},se=class{threshold;constructor(e=.8){if(e<0||e>1)throw new N(`threshold must be in [0.0, 1.0] \u2014 got ${e}`);this.threshold=e}allows(e){return e>=this.threshold}margin(e){return e-this.threshold}assert(e,r){if(!this.allows(e)){let n=r?` for action '${r}'`:"";throw new N(`Confidence ${e}${n} is below threshold ${this.threshold}`)}}},oe=class{_pending=new Map;request(e,r={}){let n=ne();return this._pending.set(n,{token:n,action:e,context:r,createdAt:new Date().toISOString(),status:"pending"}),n}approve(e){let r=this._pending.get(e);if(!r)throw new N(`Unknown token: ${e}`);r.status="approved"}deny(e,r){let n=this._pending.get(e);if(!n)throw new N(`Unknown token: ${e}`);n.status="denied",r&&(n.reason=r)}check(e){let r=this._pending.get(e);if(!r)throw new N(`Unknown token: ${e}`);return r.status}get pendingApprovals(){return Array.from(this._pending.values()).filter(e=>e.status==="pending")}getApproval(e){return this._pending.get(e)}clearResolved(){for(let[e,r]of this._pending.entries())r.status!=="pending"&&this._pending.delete(e)}};var ie=class extends Error{constructor(e){super(e),this.name="AuditError"}};function Yr(t,e,r,n,s){let o=JSON.stringify({recordId:t,action:e,robotUri:r,timestamp:n,params:s},Object.keys({recordId:t,action:e,robotUri:r,timestamp:n,params:s}).sort());return $e("rcan-content-hash",o)}function At(t,e){let{hmac:r,...n}=e,s=JSON.stringify(n,Object.keys(n).sort());return $e(t,s)}var F=class t{recordId;action;robotUri;confidence;modelIdentity;params;safetyApproved;timestamp;contentHash;previousHash;hmac;constructor(e){this.recordId=e.recordId,this.action=e.action,this.robotUri=e.robotUri,this.confidence=e.confidence,this.modelIdentity=e.modelIdentity,this.params=e.params,this.safetyApproved=e.safetyApproved,this.timestamp=e.timestamp,this.contentHash=e.contentHash,this.previousHash=e.previousHash,this.hmac=e.hmac}static create(e,r,n=null){let s=ne(),o=new Date().toISOString(),i=e.params??{},a=e.robotUri??"",d=Yr(s,e.action,a,o,i),u={recordId:s,action:e.action,robotUri:a,confidence:e.confidence,modelIdentity:e.modelIdentity,params:i,safetyApproved:e.safetyApproved??!0,timestamp:o,contentHash:d,previousHash:n,hmac:""};return u.hmac=At(r,u),new t(u)}verify(e){return At(e,this.toJSON())===this.hmac}toJSON(){return{recordId:this.recordId,action:this.action,robotUri:this.robotUri,confidence:this.confidence,modelIdentity:this.modelIdentity,params:this.params,safetyApproved:this.safetyApproved,timestamp:this.timestamp,contentHash:this.contentHash,previousHash:this.previousHash,hmac:this.hmac}}static fromJSON(e){return new t(e)}},ae=class t{_records=[];_secret;constructor(e){this._secret=e}get records(){return this._records}append(e){let n=this._records[this._records.length-1]?.contentHash??null,s=F.create(e,this._secret,n);return this._records.push(s),s}verifyAll(){let e=[],r=null;for(let n of this._records)n.verify(this._secret)||e.push(`HMAC invalid for record ${n.recordId.slice(0,8)}`),r!==null&&n.previousHash!==r&&e.push(`Chain broken at ${n.recordId.slice(0,8)}: expected prev=${r.slice(0,12)}`),r=n.contentHash;return{valid:e.length===0,count:this._records.length,errors:e}}toJSONL(){return this._records.map(e=>JSON.stringify(e.toJSON())).join(`
2
2
  `)+`
3
- `}static fromJSONL(e,t){let r=new n(t),s=e.trim().split(`
4
- `).filter(o=>o.trim()!=="");for(let o of s){let i=JSON.parse(o);r._records.push($.fromJSON(i))}return r}};function ke(){return{ok:!0,issues:[],warnings:[],info:[]}}function w(n,e){n.ok=!1,n.issues.push(e)}function P(n,e){n.warnings.push(e)}function _(n,e){n.info.push(e)}function st(n){let e=ke();try{let t=U.parse(n);_(e,"\u2705 Valid RCAN URI"),_(e,` Registry: ${t.registry}`),_(e,` Manufacturer: ${t.manufacturer}`),_(e,` Model: ${t.model}`),_(e,` Version: ${t.version}`),_(e,` Device ID: ${t.deviceId}`)}catch(t){w(e,`Invalid RCAN URI: ${t instanceof Error?t.message:t}`)}return e}function ot(n){let e=ke(),t;if(typeof n=="string")try{t=JSON.parse(n)}catch{return w(e,"Invalid JSON string"),e}else if(typeof n=="object"&&n!==null)t=n;else return w(e,"Expected object or JSON string"),e;for(let r of["rcan","cmd","target"])(!(r in t)||!t[r])&&w(e,`Missing required field: '${r}'`);if(!e.ok)return e;try{let r=l.fromJSON(t);_(e,`\u2705 RCAN message valid (v${r.rcan})`),_(e,` cmd: ${r.cmd}`),_(e,` target: ${r.target}`),r.confidence!==void 0?_(e,` confidence: ${r.confidence}`):P(e,"No confidence score \u2014 add for RCAN \xA716 AI accountability"),r.isSigned?_(e,` signature: alg=${r.signature?.alg}, kid=${r.signature?.kid}`):P(e,"Message is unsigned (recommended for production)")}catch(r){w(e,`Message validation failed: ${r instanceof Error?r.message:r}`)}return e}function it(n){let e=ke(),t=n.metadata??{},r=n.agent??{},s=n.rcan_protocol??{};for(let i of["rcan_version","metadata","agent"])(!(i in n)||n[i]===void 0||n[i]===null)&&w(e,`Missing required key: '${i}'`);let o=n.rcan_version;if(o&&(/^\d+\.\d+$/.test(o)||w(e,`rcan_version '${o}' must match pattern N.N (e.g. '1.2')`)),t.manufacturer||w(e,"L1: metadata.manufacturer is required (\xA72)"),t.model||w(e,"L1: metadata.model is required (\xA72)"),!t.device_id&&!t.robot_name&&w(e,"L1: metadata.device_id (or robot_name) is required (\xA72)"),s.jwt_auth?.enabled||P(e,"L2: jwt_auth not enabled (required for L2 conformance, \xA78)"),(!r.confidence_gates||r.confidence_gates.length===0)&&P(e,"L2: confidence_gates not configured (\xA716)"),(!r.hitl_gates||r.hitl_gates.length===0)&&P(e,"L3: hitl_gates not configured (\xA716)"),r.commitment_chain?.enabled||P(e,"L3: commitment_chain not enabled (\xA716)"),t.rrn?_(e,`\u2705 RRN registered: ${t.rrn}`):P(e,"Robot not registered \u2014 visit rcan.dev/registry/register"),e.ok&&e.issues.length===0){let i=!e.warnings.some(f=>f.startsWith("L1")),a=i&&!e.warnings.some(f=>f.startsWith("L2")),u=a&&!e.warnings.some(f=>f.startsWith("L3"))?"L3":a?"L2":i?"L1":"FAIL";_(e,`\u2705 Config valid \u2014 conformance level: ${u}`)}return e}var R=class extends Error{constructor(e){super(e),this.name="RCANError",Object.setPrototypeOf(this,new.target.prototype)}},te=class extends R{constructor(e){super(e),this.name="RCANAddressError",Object.setPrototypeOf(this,new.target.prototype)}},ne=class extends R{constructor(e){super(e),this.name="RCANValidationError",Object.setPrototypeOf(this,new.target.prototype)}},re=class extends R{constructor(t,r,s,o){super(t);this.gateType=r;this.value=s;this.threshold=o;this.name="RCANGateError",Object.setPrototypeOf(this,new.target.prototype)}},se=class extends R{constructor(e){super(e),this.name="RCANSignatureError",Object.setPrototypeOf(this,new.target.prototype)}},D=class extends R{constructor(e){super(e),this.name="RCANRegistryError",Object.setPrototypeOf(this,new.target.prototype)}},L=class n extends R{constructor(t,r){super(t);this.nodeUrl=r;this.name="RCANNodeError",Object.setPrototypeOf(this,n.prototype)}},k=class n extends L{constructor(t,r){super(`RRN not found in federation: ${t}`,r);this.rrn=t;this.name="RCANNodeNotFoundError",Object.setPrototypeOf(this,n.prototype)}},y=class n extends L{constructor(t,r,s){super(t,r);this.cause=s;this.name="RCANNodeSyncError",Object.setPrototypeOf(this,n.prototype)}},j=class n extends L{reason;constructor(e,t){super(`Node trust verification failed: ${e}`,t),this.name="RCANNodeTrustError",this.reason=e,Object.setPrototypeOf(this,n.prototype)}},oe=class n extends R{constructor(e,t){super(`VERSION_INCOMPATIBLE: incoming=${e}, local=${t}`),this.name="RCANVersionIncompatibleError",Object.setPrototypeOf(this,n.prototype)}},ie=class n extends R{constructor(e){super(`REPLAY_DETECTED: ${e}`),this.name="RCANReplayAttackError",Object.setPrototypeOf(this,n.prototype)}},ae=class n extends R{constructor(e){super(`DELEGATION_CHAIN_ERROR: ${e}`),this.name="RCANDelegationChainError",Object.setPrototypeOf(this,n.prototype)}},ce=class n extends R{constructor(e){super(`CONFIG_AUTH_ERROR: ${e}`),this.name="RCANConfigAuthorizationError",Object.setPrototypeOf(this,n.prototype)}};var rn="https://rcan-spec.pages.dev",de=class{baseUrl;apiKey;timeout;constructor(e){this.baseUrl=(e?.baseUrl??rn).replace(/\/$/,""),this.apiKey=e?.apiKey,this.timeout=e?.timeout??1e4}async _fetch(e,t={}){let r=`${this.baseUrl}${e}`,s=new AbortController,o=setTimeout(()=>s.abort(),this.timeout);try{return await fetch(r,{...t,signal:s.signal,headers:{"Content-Type":"application/json",...t.headers??{}}})}finally{clearTimeout(o)}}_authHeaders(){if(!this.apiKey)throw new D("API key required for write operations. Pass apiKey to RegistryClient.");return{Authorization:`Bearer ${this.apiKey}`}}async _checkResponse(e){if(!e.ok){let t=`Registry API error: ${e.status}`;try{let r=await e.json();r?.error&&(t=r.error)}catch{}throw new D(t)}return await e.json()}async register(e){let t=await this._fetch("/api/v1/robots",{method:"POST",body:JSON.stringify(e)});return this._checkResponse(t)}async get(e){let t=await this._fetch(`/api/v1/robots/${encodeURIComponent(e)}`);return this._checkResponse(t)}async list(e){let t=new URLSearchParams;e?.limit!==void 0&&t.set("limit",String(e.limit)),e?.offset!==void 0&&t.set("offset",String(e.offset)),e?.tier&&t.set("tier",e.tier);let r=t.toString()?`?${t}`:"",s=await this._fetch(`/api/v1/robots${r}`);return this._checkResponse(s)}async patch(e,t){let r=await this._fetch(`/api/v1/robots/${encodeURIComponent(e)}`,{method:"PATCH",headers:this._authHeaders(),body:JSON.stringify(t)});return this._checkResponse(r)}async delete(e){let t=await this._fetch(`/api/v1/robots/${encodeURIComponent(e)}`,{method:"DELETE",headers:this._authHeaders()});t.ok||await this._checkResponse(t)}async search(e){let t=new URLSearchParams;e.q&&t.set("q",e.q),e.manufacturer&&t.set("manufacturer",e.manufacturer),e.model&&t.set("model",e.model),e.tier&&t.set("tier",e.tier);let r=t.toString()?`?${t}`:"",s=await this._fetch(`/api/v1/robots/search${r}`);if(!s.ok){let i=await this._fetch(`/api/v1/robots${r}`),a=await this._checkResponse(i);return"robots"in a?a.robots:"results"in a&&a.results?a.results:[]}let o=await s.json();return Array.isArray(o)?o:"results"in o&&o.results?o.results:"robots"in o?o.robots:[]}};var sn="https://rcan.dev",on="/.well-known/rcan-node.json",an=new Set(["root","authoritative","resolver","cache"]);function cn(n){let e=n.match(/^RRN-([A-Z0-9]{2,8})-(\d{8,16})$/);if(e)return{type:"delegated",prefix:e[1],serial:e[2]};let t=n.match(/^RRN-(\d{8,16})$/);return t?{type:"root",serial:t[1]}:null}var ue=class{rootUrl;timeoutMs;constructor(e=sn,t=1e4){this.rootUrl=e.replace(/\/$/,""),this.timeoutMs=t}async _fetch(e){let t=new AbortController,r=setTimeout(()=>t.abort(),this.timeoutMs);try{return await globalThis.fetch(e,{signal:t.signal})}catch(s){throw s instanceof Error&&s.name==="AbortError"?new y(`Request timed out: ${e}`,e,s):new y(`Network error fetching ${e}: ${s.message}`,e,s instanceof Error?s:void 0)}finally{clearTimeout(r)}}async getNodeManifest(e){let t=`${e.replace(/\/$/,"")}${on}`,r=await this._fetch(t);if(!r.ok)throw r.status===404?new k(t,e):new y(`Failed to fetch node manifest from ${e}: HTTP ${r.status}`,e);let s;try{s=await r.json()}catch(o){throw new y(`Invalid JSON in node manifest from ${e}`,e,o instanceof Error?o:void 0)}if(!this.verifyNode(s))throw new j("missing_pubkey",e);return s}async listNodes(e){let t=e?`?prefix=${encodeURIComponent(e)}`:"",r=`${this.rootUrl}/api/v1/nodes${t}`,s=await this._fetch(r);if(!s.ok)throw new y(`Failed to list nodes from ${r}: HTTP ${s.status}`,r);let o;try{o=await s.json()}catch(i){throw new y(`Invalid JSON in nodes list from ${r}`,r,i instanceof Error?i:void 0)}return Array.isArray(o)?o:o&&typeof o=="object"&&"nodes"in o?o.nodes:[]}async discover(e){let t=cn(e);if(!t)throw new k(e,this.rootUrl);if(t.type==="root")return this.getNodeManifest(this.rootUrl);let r=await this.listNodes(t.prefix);if(r.length===0)throw new k(e,this.rootUrl);return r[0]}async resolve(e){let t=`${this.rootUrl}/api/v1/resolve/${encodeURIComponent(e)}`,r;try{r=await this._fetch(t)}catch(a){throw a}if(r.ok)try{return await r.json()}catch(a){throw new y(`Invalid JSON in resolve response for ${e}`,this.rootUrl,a instanceof Error?a:void 0)}if(r.status!==404)throw new y(`Unexpected HTTP ${r.status} resolving ${e}`,this.rootUrl);let s=await this.discover(e),o=`${s.api_base.replace(/\/$/,"")}/robots/${encodeURIComponent(e)}`,i=await this._fetch(o);if(!i.ok)throw i.status===404?new k(e,s.api_base):new y(`HTTP ${i.status} from authoritative node for ${e}`,s.api_base);try{return await i.json()}catch(a){throw new y(`Invalid JSON in fallback resolve response for ${e}`,s.api_base,a instanceof Error?a:void 0)}}verifyNode(e){if(!e||typeof e!="object")return!1;let t=e;return!(typeof t.rcan_node_version!="string"||!t.rcan_node_version||typeof t.node_type!="string"||!an.has(t.node_type)||typeof t.operator!="string"||!t.operator||typeof t.namespace_prefix!="string"||!t.namespace_prefix||typeof t.public_key!="string"||!t.public_key.startsWith("ed25519:")||typeof t.api_base!="string"||!t.api_base.startsWith("https://"))}};var dn="https://rcan.dev/schemas",Te=new Map;async function fe(n){if(Te.has(n))return Te.get(n);try{let e=new AbortController,t=setTimeout(()=>e.abort(),5e3),r=await fetch(`${dn}/${n}`,{signal:e.signal});if(clearTimeout(t),!r.ok)return null;let s=await r.json();return Te.set(n,s),s}catch{return null}}async function at(n){let e=await fe("rcan-config.schema.json");if(!e)return{valid:!0,skipped:!0};let t=[];if(typeof n!="object"||n===null)return{valid:!1,errors:["Config must be an object"]};let r=n,s=e.required??[];for(let o of s)o in r||t.push(`Missing required field: ${o}`);return t.length===0?{valid:!0}:{valid:!1,errors:t}}async function ct(n){let e=await fe("rcan-node.schema.json");if(!e)return{valid:!0,skipped:!0};let t=[];if(typeof n!="object"||n===null)return{valid:!1,errors:["Manifest must be an object"]};let r=n,s=e.required??[];for(let o of s)o in r||t.push(`Missing required field: ${o}`);return t.length===0?{valid:!0}:{valid:!1,errors:t}}var Ie=(r=>(r[r.FIRE_AND_FORGET=0]="FIRE_AND_FORGET",r[r.ACKNOWLEDGED=1]="ACKNOWLEDGED",r[r.EXACTLY_ONCE=2]="EXACTLY_ONCE",r))(Ie||{}),le=class n extends Error{constructor(e){super(`ACK timeout for message ${e} \u2014 safety halt required`),this.name="QoSAckTimeoutError",Object.setPrototypeOf(this,n.prototype)}},ge=class{_send;_waitForAck;constructor(e,t){this._send=e,this._waitForAck=t}async sendWithQoS(e,t={}){let r=t.qos??0,s=t.maxRetries??3,o=t.initialBackoffMs??100,i=t.ackTimeoutMs??500;if(r===0)return await this._send(e),{delivered:!0,attempts:1,reason:"fire-and-forget"};let a=e.message_id??e.msg_id??"unknown",c=0,u=o;for(;c<=s;){if(await this._send(e),c++,await this._waitForAck(a,i))return{delivered:!0,attempts:c,reason:r===2?"exactly-once":"acknowledged"};if(c>s)break;await fn(u),u=Math.min(u*2,5e3)}return{delivered:!1,attempts:c,reason:`ACK not received after ${s} retries`}}};function dt(n,e){return{message_type:6,ruri:n,safety_event:"ESTOP",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:un(),qos:2}}function un(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let n=Array.from({length:16},()=>Math.floor(Math.random()*256));n[6]=n[6]&15|64,n[8]=n[8]&63|128;let e=n.map(t=>t.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}function fn(n){return new Promise(e=>setTimeout(e,n))}var Me=6;function me(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let n=Array.from({length:16},()=>Math.floor(Math.random()*256));n[6]=n[6]&15|64,n[8]=n[8]&63|128;let e=n.map(t=>t.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}function ut(n,e){return{message_type:6,ruri:n,safety_event:"ESTOP",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:me(),qos:2}}function ft(n,e){return{message_type:6,ruri:n,safety_event:"STOP",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:me()}}function lt(n,e){return{message_type:6,ruri:n,safety_event:"RESUME",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:me()}}function gt(n){return typeof n=="object"&&n!==null&&n.message_type===Me}function mt(n){let e=[];return n.message_type!==6&&e.push("message_type must be 6"),n.ruri||e.push("ruri is required"),["ESTOP","STOP","RESUME"].includes(n.safety_event??"")||e.push("safety_event must be ESTOP, STOP, or RESUME"),(!n.reason||n.reason.length===0)&&e.push("reason is required"),n.message_id||e.push("message_id is required"),(!n.timestamp_ms||n.timestamp_ms<=0)&&e.push("timestamp_ms must be positive"),e}function pt(n,e,t){return{message_type:11,ruri:n,disclosure:e,timestamp_ms:Date.now(),message_id:me(),delegation_chain:t}}var pe=class{windowSeconds;maxSize;_seen;constructor(e=30,t=1e4){this.windowSeconds=e,this.maxSize=t,this._seen=new Map}checkAndRecord(e,t,r=!1){let s=Date.now();this._evict(s);let o=r?Math.min(this.windowSeconds,10):this.windowSeconds,i=ln(t);if(i===null)return{allowed:!1,reason:`invalid timestamp format: ${t}`};let a=s-i,c=o*1e3;if(a>c)return{allowed:!1,reason:`message too old: age=${Math.round(a/1e3)}s > window=${o}s`};if(i>s+5e3)return{allowed:!1,reason:"message timestamp is in the future"};if(this._seen.has(e))return{allowed:!1,reason:`replay detected: msg_id ${e} already seen`};if(this._seen.size>=this.maxSize){let f=this._seen.keys().next().value;this._seen.delete(f)}let u=s+c;return this._seen.set(e,u),{allowed:!0,reason:"ok"}}_evict(e){for(let[t,r]of this._seen)r<=e&&this._seen.delete(t)}get size(){return this._seen.size}};function ht(n,e){let t=n,r=t.message_id??t.msg_id;if(!r)return{valid:!1,reason:"missing message_id / msg_id"};let s;if(typeof t.timestamp_ms=="number"?s=String(t.timestamp_ms/1e3):t.timestamp!==void 0&&(s=String(t.timestamp)),!s)return{valid:!1,reason:"missing timestamp"};let o=t.message_type===6||t.message_type===6,i=e.checkAndRecord(r,s,o);return{valid:i.allowed,reason:i.reason}}function ln(n){if(n.includes("T")||n.includes("-")){let t=new Date(n);if(!isNaN(t.getTime()))return t.getTime()}let e=parseFloat(n);return isNaN(e)?null:e>1e12?e:e*1e3}var B=class n extends Error{offsetSeconds;constructor(e,t){super(`Clock drift too large: offset=${e.toFixed(3)}s > max=${t}s`),this.name="ClockDriftError",this.offsetSeconds=e,Object.setPrototypeOf(this,n.prototype)}};async function Ue(n){let e=n??"https://worldtimeapi.org/api/ip";try{let t=Date.now(),r=await fetch(e,{method:"HEAD",signal:AbortSignal.timeout(3e3)}),s=Date.now(),o=r.headers.get("Date")??r.headers.get("date");if(!o)return{synchronized:!0,offsetSeconds:0,source:"assumed (no Date header)"};let i=new Date(o).getTime();if(isNaN(i))return{synchronized:!0,offsetSeconds:0,source:"assumed (unparseable Date header)"};let c=((t+s)/2-i)/1e3;return{synchronized:Math.abs(c)<=5,offsetSeconds:c,source:e}}catch{return{synchronized:!0,offsetSeconds:0,source:"assumed (network unavailable)"}}}async function yt(n=5){let e=await Ue();if(!e.synchronized||Math.abs(e.offsetSeconds)>n)throw new B(e.offsetSeconds,n)}async function gn(n){let e=JSON.stringify(n,Object.keys(n).sort());if(typeof crypto<"u"&&crypto.subtle){let r=new TextEncoder().encode(e),s=await crypto.subtle.digest("SHA-256",r);return Array.from(new Uint8Array(s)).map(o=>o.toString(16).padStart(2,"0")).join("")}let t=2166136261;for(let r=0;r<e.length;r++)t^=e.charCodeAt(r),t=t*16777619>>>0;return t.toString(16).padStart(8,"0")}async function Rt(n,e,t,r="rcan://local/config",s=!1){let o=await gn(n);return new l({rcan:"1.6",cmd:"CONFIG_UPDATE",target:r,params:{message_type:5,diff:n,rollback:t,scope:e,config_hash:o,safety_overrides:s}})}function St(n){let e=n.params;return!e.diff||typeof e.diff!="object"?{valid:!1,reason:"missing required field: params.diff"}:!e.config_hash||typeof e.config_hash!="string"?{valid:!1,reason:"missing required field: params.config_hash"}:"rollback"in e?e.safety_overrides===!0&&e.scope!=="creator"?{valid:!1,reason:"safety_overrides=true requires scope=creator (owner is insufficient)"}:{valid:!0,reason:"ok"}:{valid:!1,reason:"missing required field: params.rollback"}}function mn(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`}var he=class{_keys=[];addKey(e){this._keys.push(e)}getJWKS(){return{keys:[...this._keys]}}findKey(e){return this._keys.find(t=>t.kid===e)}isKeyValid(e,t){let r=this.findKey(e);if(!r)return!1;let s=(t??Date.now())/1e3;return!(r.revoked_at!==void 0&&r.revoked_at<=s||r.exp!==void 0&&r.exp<s)}expireKey(e,t){let r=this.findKey(e);r&&(r.exp=t??Math.floor(Date.now()/1e3))}revokeKey(e){let t=this.findKey(e);t&&(t.revoked_at=Math.floor(Date.now()/1e3))}validKeys(e){return this._keys.filter(t=>this.isKeyValid(t.kid,e))}};function _t(n,e,t=120,r="rcan://local/keys"){let s=mn().slice(0,8);return new l({rcan:"1.6",cmd:"KEY_ROTATION",target:r,params:{message_type:5,new_public_key:n,new_kid:s,old_kid:e,overlap_seconds:t,initiated_at:new Date().toISOString()},keyId:s})}function pn(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`}function ye(n){let e=n.requestId??pn();return new l({rcan:"1.6",cmd:"CONSENT_REQUEST",target:n.targetRuri,params:{message_type:20,requester_ruri:n.requesterRuri,requester_owner:n.requesterOwner,target_ruri:n.targetRuri,requested_scopes:n.requestedScopes,duration_hours:n.durationHours,justification:n.justification,request_id:e,consent_type:n.consentType??"cross_robot",data_categories:n.dataCategories??[]}})}function Re(n){let e=n.expiresAt??new Date(Date.now()+864e5).toISOString();return new l({rcan:"1.6",cmd:"CONSENT_GRANT",target:"rcan://local/consent",params:{message_type:21,request_id:n.requestId,granted_scopes:n.grantedScopes??[],expires_at:e,reason:n.reason??"approved"}})}function Se(n){return new l({rcan:"1.6",cmd:"CONSENT_DENY",target:"rcan://local/consent",params:{message_type:22,request_id:n.requestId,reason:n.reason??"denied"}})}function At(n){let e=n.cmd,t=n.params,r=t.message_type;return e==="CONSENT_REQUEST"?r!==20?{valid:!1,reason:"message_type must be CONSENT_REQUEST (20)"}:t.requester_ruri?t.target_ruri?!t.requested_scopes||!Array.isArray(t.requested_scopes)||t.requested_scopes.length===0?{valid:!1,reason:"requested_scopes must be a non-empty array"}:t.request_id?t.justification?{valid:!0,reason:"ok"}:{valid:!1,reason:"missing justification"}:{valid:!1,reason:"missing request_id"}:{valid:!1,reason:"missing target_ruri"}:{valid:!1,reason:"missing requester_ruri"}:e==="CONSENT_GRANT"?r!==21?{valid:!1,reason:"message_type must be CONSENT_GRANT (21)"}:t.request_id?t.expires_at?{valid:!0,reason:"ok"}:{valid:!1,reason:"missing expires_at"}:{valid:!1,reason:"missing request_id"}:e==="CONSENT_DENY"?r!==22?{valid:!1,reason:"message_type must be CONSENT_DENY (22)"}:t.request_id?{valid:!0,reason:"ok"}:{valid:!1,reason:"missing request_id"}:{valid:!1,reason:`unknown consent command: ${e}`}}var hn=3600*1e3,K=class{_cache=new Map;get(e,t){let r=this._cache.get(e);if(!r)return;let s=t??Date.now();if(r.cachedUntil!==void 0&&r.cachedUntil<s){this._cache.delete(e);return}return r}set(e,t){let r=t??Date.now();this._cache.set(e.rrn,{...e,cachedUntil:r+hn})}invalidate(e){this._cache.delete(e)}get size(){return this._cache.size}};async function bt(n,e,t){let r=t??new K,s=r.get(n);if(s)return s;let o=`${e.replace(/\/$/,"")}/api/v1/robots/${encodeURIComponent(n)}/revocation-status`;try{let i=await fetch(o,{signal:AbortSignal.timeout(5e3)});if(!i.ok){let u={rrn:n,status:"active",reason:`registry returned ${i.status}`};return r.set(u),u}let a=await i.json(),c={rrn:n,status:a.status??"active",revokedAt:a.revokedAt,reason:a.reason,authority:a.authority};return r.set(c),c}catch{return{rrn:n,status:"active",reason:"network unavailable"}}}function vt(n,e){return new l({rcan:"1.6",cmd:"ROBOT_REVOCATION",target:"rcan://broadcast/revocation",params:{message_type:19,rrn:n,reason:e,revoked_at:new Date().toISOString()}})}var Pe=(o=>(o.VIDEO="video",o.AUDIO="audio",o.LOCATION="location",o.BIOMETRIC="biometric",o.TELEMETRY="telemetry",o))(Pe||{});function xt(n){return ye({requesterRuri:n.requesterRuri,requesterOwner:n.requesterOwner,targetRuri:n.targetRuri,requestedScopes:["training_data"],durationHours:n.durationHours,justification:n.justification,requestId:n.requestId,consentType:"training_data",dataCategories:n.dataCategories})}function Ct(n){return Re(n)}function Et(n){return Se(n)}function wt(n){if(n.params.message_type!==10)return{valid:!1,reason:"not a TRAINING_DATA message"};let e=n.params.consent_token;return!e||typeof e!="string"||e.trim()===""?{valid:!1,reason:"TRAINING_DATA message missing consent_token (\xA717)"}:{valid:!0,reason:"ok"}}var _e=class{crossOwnerGraceS;keyTtlS;_cachedKeys=[];constructor(e=3600,t=86400){this.crossOwnerGraceS=e,this.keyTtlS=t}canAcceptCommand(e,t,r,s=!0,o=!1,i,a){if(e&&e.message_type===6&&e.safety_event==="ESTOP")return{allowed:!0,reason:"ESTOP always accepted (Protocol 66)"};if(!t)return{allowed:!0,reason:"online mode"};if(!r)return{allowed:!1,reason:"offline mode: cross-network commands blocked"};if(!s)return{allowed:!1,reason:"offline mode: only owner-role commands accepted from local network"};if(o&&i!==void 0){let u=((a??Date.now())-i)/1e3;if(u>this.crossOwnerGraceS)return{allowed:!1,reason:`offline mode: cross-owner grace period expired (${Math.round(u)}s > ${this.crossOwnerGraceS}s)`}}return{allowed:!0,reason:"offline mode: owner command on local network accepted"}}cacheKey(e,t){let r=t??Date.now();this._cachedKeys=this._cachedKeys.filter(s=>s.kid!==e.kid),this._cachedKeys.push({...e,cachedAtMs:r,ttlSeconds:this.keyTtlS})}getCachedKey(e,t){let r=t??Date.now(),s=this._cachedKeys.find(i=>i.kid===e);if(!s)return;if((r-s.cachedAtMs)/1e3>s.ttlSeconds){this._cachedKeys=this._cachedKeys.filter(i=>i.kid!==e);return}return s}getManifestFields(e,t){if(e===void 0)return{offline_mode:!1,offline_since_s:0};let r=t??Date.now();return{offline_mode:!0,offline_since_s:Math.round((r-e)/1e3)}}};var De=(p=>(p.SENSOR_PROXIMITY_FAILURE="SENSOR_PROXIMITY_FAILURE",p.SENSOR_CAMERA_FAILURE="SENSOR_CAMERA_FAILURE",p.SENSOR_IMU_FAILURE="SENSOR_IMU_FAILURE",p.MOTOR_OVERCURRENT="MOTOR_OVERCURRENT",p.MOTOR_OVERTEMP="MOTOR_OVERTEMP",p.MOTOR_STALL="MOTOR_STALL",p.BATTERY_CRITICAL="BATTERY_CRITICAL",p.BATTERY_LOW="BATTERY_LOW",p.NETWORK_TIMEOUT="NETWORK_TIMEOUT",p.NETWORK_REGISTRY_UNREACHABLE="NETWORK_REGISTRY_UNREACHABLE",p.SAFETY_ESTOP_STUCK="SAFETY_ESTOP_STUCK",p.SAFETY_WATCHDOG_TIMEOUT="SAFETY_WATCHDOG_TIMEOUT",p.UNKNOWN="UNKNOWN",p))(De||{});function Nt(n){return new l({rcan:"1.6",cmd:"FAULT_REPORT",target:n.target??"rcan://local/fault",params:{message_type:26,fault_code:n.faultCode,severity:n.severity,subsystem:n.subsystem,affects_safety:n.affectsSafety,safe_to_continue:n.safeToContinue,description:n.description??"",reported_at:new Date().toISOString()}})}var Ae=(r=>(r[r.ANONYMOUS=1]="ANONYMOUS",r[r.EMAIL_VERIFIED=2]="EMAIL_VERIFIED",r[r.HARDWARE_TOKEN=3]="HARDWARE_TOKEN",r))(Ae||{}),Le={minLoaDiscover:1,minLoaStatus:1,minLoaChat:1,minLoaControl:1,minLoaSafety:1},Ot={minLoaDiscover:1,minLoaStatus:1,minLoaChat:1,minLoaControl:2,minLoaSafety:3};function be(n){try{let e=n.split(".");if(e.length<2)return 1;let t=(e[1]??"").replace(/-/g,"+").replace(/_/g,"/"),r=t+"=".repeat((4-t.length%4)%4),s;typeof atob<"u"?s=atob(r):s=Buffer.from(r,"base64").toString("utf-8");let i=JSON.parse(s).loa;if(typeof i=="number"&&i>=1&&i<=3)return i}catch{}return 1}function yn(n,e){switch(n.toLowerCase()){case"discover":return e.minLoaDiscover;case"status":return e.minLoaStatus;case"chat":return e.minLoaChat;case"control":return e.minLoaControl;case"safety":return e.minLoaSafety;default:return null}}function kt(n,e,t=Le){let r=yn(e,t);return r===null?{valid:!0,reason:"unknown scope; allowed by default"}:n>=r?{valid:!0,reason:"ok"}:{valid:!1,reason:`LOA_INSUFFICIENT: scope=${e} requires LoA>=${r}, caller has LoA=${n}`}}var $e=(r=>(r.ROOT="root",r.AUTHORITATIVE="authoritative",r.COMMUNITY="community",r))($e||{}),je=(r=>(r.CONSENT="consent",r.REVOCATION="revocation",r.KEY="key",r))(je||{}),Rn=1440*60*1e3,ve=class{store=new Map;set(e){this.store.set(e.registryUrl,{identity:e,expiresAt:Date.now()+Rn})}lookup(e){let t=this.store.get(e);if(t){if(Date.now()>t.expiresAt){this.store.delete(e);return}return t.identity}}async discoverViaDns(e){let t=`_rcan-registry.${e}`,r;try{r=await ze("dns").promises.resolveTxt(t)}catch{return}for(let s of r){let o=s.join("");try{let i=JSON.parse(o);if(i.registryUrl&&i.tier&&i.publicKeyPem&&i.domain){let a={registryUrl:i.registryUrl,tier:i.tier,publicKeyPem:i.publicKeyPem,domain:i.domain,verifiedAt:new Date().toISOString()};return this.set(a),a}}catch{}}}async verifyRegistryJwt(e,t){let r=this.lookup(t);if(!r)throw new Error(`REGISTRY_UNKNOWN: ${t} is not in the trust cache`);let s;try{let i=(e.split(".")[1]??"").replace(/-/g,"+").replace(/_/g,"/"),a=i+"=".repeat((4-i.length%4)%4),c;typeof atob<"u"?c=atob(a):c=Buffer.from(a,"base64").toString("utf-8");let u=JSON.parse(c);s=typeof u.iss=="string"?u.iss:void 0}catch{throw new Error("REGISTRY_JWT_MALFORMED: cannot decode token payload")}if(s!==t)throw new Error(`REGISTRY_JWT_ISS_MISMATCH: expected iss=${t}, got iss=${s??"(none)"}`);return r}};function Sn(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let n=Array.from({length:16},()=>Math.floor(Math.random()*256));n[6]=(n[6]??0)&15|64,n[8]=(n[8]??0)&63|128;let e=n.map(t=>t.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}function Tt(n,e,t,r){return new l({rcan:"1.6",rcanVersion:"1.6",cmd:"federation_sync",target:e,params:{msg_type:12,msg_id:Sn(),source_registry:n,target_registry:e,sync_type:t,payload:r},timestamp:new Date().toISOString()})}async function It(n,e,t){let r=n.params?.msg_type;if(r===6||r===6||n.cmd==="estop"||n.cmd==="ESTOP")return{valid:!0,reason:"ESTOP always permitted (P66 invariant)"};let o=n.params?.source_registry??n.params?.from_registry;if(!o||o===e)return{valid:!0,reason:"local registry; no federation check needed"};if(!t.lookup(o))return{valid:!1,reason:`REGISTRY_UNKNOWN: ${o} is not in the local trust cache`};let a=1,c=n.params?.registry_jwt;return c?a=be(c):typeof n.loa=="number"&&(a=n.loa),a<2?{valid:!1,reason:`LOA_INSUFFICIENT: cross-registry commands require LoA>=2 (EMAIL_VERIFIED), got LoA=${a}`}:{valid:!0,reason:"cross-registry command accepted"}}var x=class n extends Error{constructor(e){super(e),this.name="TransportError",Object.setPrototypeOf(this,n.prototype)}},He=(s=>(s.HTTP="http",s.COMPACT="compact",s.MINIMAL="minimal",s.BLE="ble",s))(He||{}),qe={msg_type:"t",msg_id:"i",timestamp:"ts",from_rrn:"f",to_rrn:"to",scope:"s",payload:"p",signature:"sig"},Mt=Object.fromEntries(Object.entries(qe).map(([n,e])=>[e,n]));function Be(n){let e=n.toJSON(),t={};for(let[o,i]of Object.entries(e)){let a=qe[o];t[a??o]=i}if(t.p&&typeof t.p=="object"){let o=t.p,i={};for(let[a,c]of Object.entries(o)){let u=qe[a];i[u??a]=c}t.p=i}let r=JSON.stringify(t);return new TextEncoder().encode(r)}function Ke(n){let t=new TextDecoder().decode(n),r=JSON.parse(t),s={};for(let[o,i]of Object.entries(r)){let a=Mt[o];s[a??o]=i}if(s.payload&&typeof s.payload=="object"){let o=s.payload,i={};for(let[a,c]of Object.entries(o)){let u=Mt[a];i[u??a]=c}s.payload=i}return new l({rcan:s.rcan??"1.6",rcanVersion:s.rcanVersion,cmd:s.cmd,target:s.target,params:s.params??s.payload??{},timestamp:s.timestamp,confidence:s.confidence,signature:s.signature})}var Y=32,Fe=6;async function Ut(n){let e=new TextEncoder().encode(n),t=new ArrayBuffer(e.byteLength);new Uint8Array(t).set(e);let r=await crypto.subtle.digest("SHA-256",t);return new Uint8Array(r)}async function Pt(n){let e=n.params?.msg_type??0;if(e!==Fe)throw new x(`encodeMinimal only supports SAFETY (type 6) messages; got type=${e}`);let t=n.params?.from_rrn??n.target??"",r=n.params?.to_rrn??n.target??"",s=await Ut(t),o=await Ut(r),i=(n.signature?.sig??"").replace(/[^A-Za-z0-9+/=]/g,""),a;try{if(typeof atob<"u"){let N=atob(i.slice(0,16));a=new Uint8Array(N.length);for(let h=0;h<N.length;h++)a[h]=N.charCodeAt(h)}else a=Buffer.from(i.slice(0,16),"base64")}catch{a=new Uint8Array(8)}let u=(n.timestamp?Math.floor(new Date(n.timestamp).getTime()/1e3):Math.floor(Date.now()/1e3))>>>0,f=new Uint8Array(Y),q=new DataView(f.buffer);q.setUint16(0,Fe,!1),f.set(s.subarray(0,8),2),f.set(o.subarray(0,8),10),q.setUint32(18,u,!1);let A=new Uint8Array(8);A.set(a.subarray(0,Math.min(8,a.length))),f.set(A,22);let p=0;for(let N=0;N<30;N++)p^=f[N]??0;if(q.setUint16(30,p&65535,!1),f.length!==Y)throw new x(`encodeMinimal assertion failed: expected ${Y} bytes, got ${f.length}`);return f}function Dt(n){if(n.length!==Y)throw new x(`decodeMinimal: expected ${Y} bytes, got ${n.length}`);let e=new DataView(n.buffer,n.byteOffset,n.byteLength),t=e.getUint16(0,!1),r=n.subarray(2,10),s=n.subarray(10,18),o=e.getUint32(18,!1),i=n.subarray(22,30),a=new Date(o*1e3).toISOString(),c=u=>Array.from(u).map(f=>f.toString(16).padStart(2,"0")).join("");return{params:{msg_type:t,from_hash:c(r),to_hash:c(s),timestamp_s:o,sig_truncated:c(i)},timestamp:a}}var _n=251,V=3;function Lt(n,e=_n){let t=Be(n),r=e-V;if(r<=0)throw new x(`MTU ${e} is too small (need at least ${V+1})`);let s=Math.ceil(t.length/r),o=[];for(let i=0;i<s;i++){let a=t.subarray(i*r,(i+1)*r),c=new Uint8Array(V+a.length);c[0]=i,c[1]=s,c[2]=i===s-1?1:0,c.set(a,V),o.push(c)}return o}function $t(n){if(n.length===0)throw new x("decodeBleFrames: no frames provided");let e=[...n].sort((a,c)=>(a[0]??0)-(c[0]??0)),t=e[0]?.[1]??e.length;if(e.length!==t)throw new x(`decodeBleFrames: expected ${t} frames, got ${e.length}`);let r=e.map(a=>a.subarray(V)),s=r.reduce((a,c)=>a+c.length,0),o=new Uint8Array(s),i=0;for(let a of r)o.set(a,i),i+=a.length;return Ke(o)}function jt(n,e){let r=(e.params?.msg_type??0)===Fe,s=o=>n.includes(o);if(r){if(s("minimal"))return"minimal";if(s("ble"))return"ble";if(s("compact"))return"compact";if(s("http"))return"http"}else{if(s("http"))return"http";if(s("compact"))return"compact";if(s("ble"))return"ble"}throw new x(`No suitable transport available from: [${n.join(", ")}]`)}var Ve=(t=>(t.BASE64="base64",t.REF="ref",t))(Ve||{});function J(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let n=Array.from({length:16},()=>Math.floor(Math.random()*256));n[6]=(n[6]??0)&15|64,n[8]=(n[8]??0)&63|128;let e=n.map(t=>t.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}async function Ye(n){let e=new ArrayBuffer(n.byteLength);new Uint8Array(e).set(n);let t=await crypto.subtle.digest("SHA-256",e),r=new Uint8Array(t);return Array.from(r).map(s=>s.toString(16).padStart(2,"0")).join("")}function qt(n){if(typeof Buffer<"u")return Buffer.from(n).toString("base64");let e="";for(let t=0;t<n.length;t++)e+=String.fromCharCode(n[t]??0);return btoa(e)}function Je(n,e){let t={...n.toJSON(),...e};return l.fromJSON(t)}async function Ge(n,e,t){let r=await Ye(e),s=qt(e),o={chunkId:J(),mimeType:t,encoding:"base64",hashSha256:r,dataB64:s,sizeBytes:e.length},i=n.mediaChunks??[];return Je(n,{mediaChunks:[...i,o]})}function Ft(n,e,t,r,s){let o={chunkId:J(),mimeType:t,encoding:"ref",hashSha256:r,refUrl:e,sizeBytes:s},i=n.mediaChunks??[];return Je(n,{mediaChunks:[...i,o]})}async function Ht(n){let e=n.mediaChunks??[];if(e.length===0)return{valid:!0,reason:"no media chunks"};for(let t=0;t<e.length;t++){let r=e[t];if(!r.chunkId)return{valid:!1,reason:`chunk[${t}]: missing chunkId`};if(!r.mimeType)return{valid:!1,reason:`chunk[${t}]: missing mimeType`};if(!r.hashSha256)return{valid:!1,reason:`chunk[${t}]: missing hashSha256`};if(r.sizeBytes<0)return{valid:!1,reason:`chunk[${t}]: sizeBytes must be >= 0`};if(r.encoding==="base64"){if(!r.dataB64)return{valid:!1,reason:`chunk[${t}]: BASE64 encoding requires dataB64`};let s;try{if(typeof Buffer<"u")s=Buffer.from(r.dataB64,"base64");else{let i=atob(r.dataB64);s=new Uint8Array(i.length);for(let a=0;a<i.length;a++)s[a]=i.charCodeAt(a)}}catch{return{valid:!1,reason:`chunk[${t}]: failed to decode base64 data`}}let o=await Ye(s);if(o!==r.hashSha256)return{valid:!1,reason:`chunk[${t}]: SHA-256 mismatch (expected ${r.hashSha256}, got ${o})`}}else if(r.encoding==="ref"){if(!r.refUrl)return{valid:!1,reason:`chunk[${t}]: REF encoding requires refUrl`}}else return{valid:!1,reason:`chunk[${t}]: unknown encoding '${r.encoding}'`}}return{valid:!0,reason:"ok"}}async function Bt(n){let e=new l({rcan:"1.6",rcanVersion:"1.6",cmd:"training_data",target:"rcan://training/data",params:{msg_type:10,msg_id:J()},timestamp:new Date().toISOString()});for(let t of n)e=await Ge(e,t.data,t.mimeType);return e}async function Kt(n,e,t,r,s){let o=await Ye(e),i=qt(e),a={chunkId:J(),mimeType:t,encoding:"base64",hashSha256:o,dataB64:i,sizeBytes:e.length},c={streamId:n,chunkIndex:r,isFinal:s,chunk:a},u=new l({rcan:"1.6",rcanVersion:"1.6",cmd:"stream_chunk",target:"rcan://streaming/chunk",params:{msg_type:7,msg_id:J(),stream_chunk:c},timestamp:new Date().toISOString()});return u=Je(u,{mediaChunks:[a]}),u}var An="0.6.0",bn="1.6";return Zt(vn);})();
3
+ `}static fromJSONL(e,r){let n=new t(r),s=e.trim().split(`
4
+ `).filter(o=>o.trim()!=="");for(let o of s){let i=JSON.parse(o);n._records.push(F.fromJSON(i))}return n}};function Le(){return{ok:!0,issues:[],warnings:[],info:[]}}function v(t,e){t.ok=!1,t.issues.push(e)}function L(t,e){t.warnings.push(e)}function b(t,e){t.info.push(e)}function Ct(t){let e=Le();try{let r=P.parse(t);b(e,"\u2705 Valid RCAN URI"),b(e,` Registry: ${r.registry}`),b(e,` Manufacturer: ${r.manufacturer}`),b(e,` Model: ${r.model}`),b(e,` Version: ${r.version}`),b(e,` Device ID: ${r.deviceId}`)}catch(r){v(e,`Invalid RCAN URI: ${r instanceof Error?r.message:r}`)}return e}function wt(t){let e=Le(),r;if(typeof t=="string")try{r=JSON.parse(t)}catch{return v(e,"Invalid JSON string"),e}else if(typeof t=="object"&&t!==null)r=t;else return v(e,"Expected object or JSON string"),e;for(let n of["rcan","cmd","target"])(!(n in r)||!r[n])&&v(e,`Missing required field: '${n}'`);if(!e.ok)return e;try{let n=m.fromJSON(r);b(e,`\u2705 RCAN message valid (v${n.rcan})`),b(e,` cmd: ${n.cmd}`),b(e,` target: ${n.target}`),n.confidence!==void 0?b(e,` confidence: ${n.confidence}`):L(e,"No confidence score \u2014 add for RCAN \xA716 AI accountability"),n.isSigned?b(e,` signature: alg=${n.signature?.alg}, kid=${n.signature?.kid}`):L(e,"Message is unsigned (recommended for production)")}catch(n){v(e,`Message validation failed: ${n instanceof Error?n.message:n}`)}return e}function xt(t){let e=Le(),r=t.metadata??{},n=t.agent??{},s=t.rcan_protocol??{};for(let i of["rcan_version","metadata","agent"])(!(i in t)||t[i]===void 0||t[i]===null)&&v(e,`Missing required key: '${i}'`);let o=t.rcan_version;if(o&&(/^\d+\.\d+$/.test(o)||v(e,`rcan_version '${o}' must match pattern N.N (e.g. '1.2')`)),r.manufacturer||v(e,"L1: metadata.manufacturer is required (\xA72)"),r.model||v(e,"L1: metadata.model is required (\xA72)"),!r.device_id&&!r.robot_name&&v(e,"L1: metadata.device_id (or robot_name) is required (\xA72)"),s.jwt_auth?.enabled||L(e,"L2: jwt_auth not enabled (required for L2 conformance, \xA78)"),(!n.confidence_gates||n.confidence_gates.length===0)&&L(e,"L2: confidence_gates not configured (\xA716)"),(!n.hitl_gates||n.hitl_gates.length===0)&&L(e,"L3: hitl_gates not configured (\xA716)"),n.commitment_chain?.enabled||L(e,"L3: commitment_chain not enabled (\xA716)"),r.rrn?b(e,`\u2705 RRN registered: ${r.rrn}`):L(e,"Robot not registered \u2014 visit rcan.dev/registry/register"),e.ok&&e.issues.length===0){let i=!e.warnings.some(l=>l.startsWith("L1")),a=i&&!e.warnings.some(l=>l.startsWith("L2")),u=a&&!e.warnings.some(l=>l.startsWith("L3"))?"L3":a?"L2":i?"L1":"FAIL";b(e,`\u2705 Config valid \u2014 conformance level: ${u}`)}return e}var _=class extends Error{constructor(e){super(e),this.name="RCANError",Object.setPrototypeOf(this,new.target.prototype)}},ce=class extends _{constructor(e){super(e),this.name="RCANAddressError",Object.setPrototypeOf(this,new.target.prototype)}},de=class extends _{constructor(e){super(e),this.name="RCANValidationError",Object.setPrototypeOf(this,new.target.prototype)}},ue=class extends _{constructor(r,n,s,o){super(r);this.gateType=n;this.value=s;this.threshold=o;this.name="RCANGateError",Object.setPrototypeOf(this,new.target.prototype)}},fe=class extends _{constructor(e){super(e),this.name="RCANSignatureError",Object.setPrototypeOf(this,new.target.prototype)}},j=class extends _{constructor(e){super(e),this.name="RCANRegistryError",Object.setPrototypeOf(this,new.target.prototype)}},q=class t extends _{constructor(r,n){super(r);this.nodeUrl=n;this.name="RCANNodeError",Object.setPrototypeOf(this,t.prototype)}},k=class t extends q{constructor(r,n){super(`RRN not found in federation: ${r}`,n);this.rrn=r;this.name="RCANNodeNotFoundError",Object.setPrototypeOf(this,t.prototype)}},R=class t extends q{constructor(r,n,s){super(r,n);this.cause=s;this.name="RCANNodeSyncError",Object.setPrototypeOf(this,t.prototype)}},H=class t extends q{reason;constructor(e,r){super(`Node trust verification failed: ${e}`,r),this.name="RCANNodeTrustError",this.reason=e,Object.setPrototypeOf(this,t.prototype)}},le=class t extends _{constructor(e,r){super(`VERSION_INCOMPATIBLE: incoming=${e}, local=${r}`),this.name="RCANVersionIncompatibleError",Object.setPrototypeOf(this,t.prototype)}},me=class t extends _{constructor(e){super(`REPLAY_DETECTED: ${e}`),this.name="RCANReplayAttackError",Object.setPrototypeOf(this,t.prototype)}},pe=class t extends _{constructor(e){super(`DELEGATION_CHAIN_ERROR: ${e}`),this.name="RCANDelegationChainError",Object.setPrototypeOf(this,t.prototype)}},ge=class t extends _{constructor(e){super(`CONFIG_AUTH_ERROR: ${e}`),this.name="RCANConfigAuthorizationError",Object.setPrototypeOf(this,t.prototype)}};var zr="https://rcan-spec.pages.dev",he=class{baseUrl;apiKey;timeout;constructor(e){this.baseUrl=(e?.baseUrl??zr).replace(/\/$/,""),this.apiKey=e?.apiKey,this.timeout=e?.timeout??1e4}async _fetch(e,r={}){let n=`${this.baseUrl}${e}`,s=new AbortController,o=setTimeout(()=>s.abort(),this.timeout);try{return await fetch(n,{...r,signal:s.signal,headers:{"Content-Type":"application/json",...r.headers??{}}})}finally{clearTimeout(o)}}_authHeaders(){if(!this.apiKey)throw new j("API key required for write operations. Pass apiKey to RegistryClient.");return{Authorization:`Bearer ${this.apiKey}`}}async _checkResponse(e){if(!e.ok){let r=`Registry API error: ${e.status}`;try{let n=await e.json();n?.error&&(r=n.error)}catch{}throw new j(r)}return await e.json()}async register(e){let r=await this._fetch("/api/v1/robots",{method:"POST",body:JSON.stringify(e)});return this._checkResponse(r)}async get(e){let r=await this._fetch(`/api/v1/robots/${encodeURIComponent(e)}`);return this._checkResponse(r)}async list(e){let r=new URLSearchParams;e?.limit!==void 0&&r.set("limit",String(e.limit)),e?.offset!==void 0&&r.set("offset",String(e.offset)),e?.tier&&r.set("tier",e.tier);let n=r.toString()?`?${r}`:"",s=await this._fetch(`/api/v1/robots${n}`);return this._checkResponse(s)}async patch(e,r){let n=await this._fetch(`/api/v1/robots/${encodeURIComponent(e)}`,{method:"PATCH",headers:this._authHeaders(),body:JSON.stringify(r)});return this._checkResponse(n)}async delete(e){let r=await this._fetch(`/api/v1/robots/${encodeURIComponent(e)}`,{method:"DELETE",headers:this._authHeaders()});r.ok||await this._checkResponse(r)}async search(e){let r=new URLSearchParams;e.q&&r.set("q",e.q),e.manufacturer&&r.set("manufacturer",e.manufacturer),e.model&&r.set("model",e.model),e.tier&&r.set("tier",e.tier);let n=r.toString()?`?${r}`:"",s=await this._fetch(`/api/v1/robots/search${n}`);if(!s.ok){let i=await this._fetch(`/api/v1/robots${n}`),a=await this._checkResponse(i);return"robots"in a?a.robots:"results"in a&&a.results?a.results:[]}let o=await s.json();return Array.isArray(o)?o:"results"in o&&o.results?o.results:"robots"in o?o.robots:[]}};var Qr="https://rcan.dev",Xr="/.well-known/rcan-node.json",Zr=new Set(["root","authoritative","resolver","cache"]);function en(t){let e=t.match(/^RRN-([A-Z0-9]{2,8})-(\d{8,16})$/);if(e)return{type:"delegated",prefix:e[1],serial:e[2]};let r=t.match(/^RRN-(\d{8,16})$/);return r?{type:"root",serial:r[1]}:null}var ye=class{rootUrl;timeoutMs;constructor(e=Qr,r=1e4){this.rootUrl=e.replace(/\/$/,""),this.timeoutMs=r}async _fetch(e){let r=new AbortController,n=setTimeout(()=>r.abort(),this.timeoutMs);try{return await globalThis.fetch(e,{signal:r.signal})}catch(s){throw s instanceof Error&&s.name==="AbortError"?new R(`Request timed out: ${e}`,e,s):new R(`Network error fetching ${e}: ${s.message}`,e,s instanceof Error?s:void 0)}finally{clearTimeout(n)}}async getNodeManifest(e){let r=`${e.replace(/\/$/,"")}${Xr}`,n=await this._fetch(r);if(!n.ok)throw n.status===404?new k(r,e):new R(`Failed to fetch node manifest from ${e}: HTTP ${n.status}`,e);let s;try{s=await n.json()}catch(o){throw new R(`Invalid JSON in node manifest from ${e}`,e,o instanceof Error?o:void 0)}if(!this.verifyNode(s))throw new H("missing_pubkey",e);return s}async listNodes(e){let r=e?`?prefix=${encodeURIComponent(e)}`:"",n=`${this.rootUrl}/api/v1/nodes${r}`,s=await this._fetch(n);if(!s.ok)throw new R(`Failed to list nodes from ${n}: HTTP ${s.status}`,n);let o;try{o=await s.json()}catch(i){throw new R(`Invalid JSON in nodes list from ${n}`,n,i instanceof Error?i:void 0)}return Array.isArray(o)?o:o&&typeof o=="object"&&"nodes"in o?o.nodes:[]}async discover(e){let r=en(e);if(!r)throw new k(e,this.rootUrl);if(r.type==="root")return this.getNodeManifest(this.rootUrl);let n=await this.listNodes(r.prefix);if(n.length===0)throw new k(e,this.rootUrl);return n[0]}async resolve(e){let r=`${this.rootUrl}/api/v1/resolve/${encodeURIComponent(e)}`,n;try{n=await this._fetch(r)}catch(a){throw a}if(n.ok)try{return await n.json()}catch(a){throw new R(`Invalid JSON in resolve response for ${e}`,this.rootUrl,a instanceof Error?a:void 0)}if(n.status!==404)throw new R(`Unexpected HTTP ${n.status} resolving ${e}`,this.rootUrl);let s=await this.discover(e),o=`${s.api_base.replace(/\/$/,"")}/robots/${encodeURIComponent(e)}`,i=await this._fetch(o);if(!i.ok)throw i.status===404?new k(e,s.api_base):new R(`HTTP ${i.status} from authoritative node for ${e}`,s.api_base);try{return await i.json()}catch(a){throw new R(`Invalid JSON in fallback resolve response for ${e}`,s.api_base,a instanceof Error?a:void 0)}}verifyNode(e){if(!e||typeof e!="object")return!1;let r=e;return!(typeof r.rcan_node_version!="string"||!r.rcan_node_version||typeof r.node_type!="string"||!Zr.has(r.node_type)||typeof r.operator!="string"||!r.operator||typeof r.namespace_prefix!="string"||!r.namespace_prefix||typeof r.public_key!="string"||!r.public_key.startsWith("ed25519:")||typeof r.api_base!="string"||!r.api_base.startsWith("https://"))}};var tn="https://rcan.dev/schemas",je=new Map;async function Re(t){if(je.has(t))return je.get(t);try{let e=new AbortController,r=setTimeout(()=>e.abort(),5e3);r.unref?.();let n=await fetch(`${tn}/${t}`,{signal:e.signal});if(clearTimeout(r),!n.ok)return null;let s=await n.json();return je.set(t,s),s}catch{return null}}async function Tt(t){let e=await Re("rcan-config.schema.json");if(!e)return{valid:!0,skipped:!0};let r=[];if(typeof t!="object"||t===null)return{valid:!1,errors:["Config must be an object"]};let n=t,s=e.required??[];for(let o of s)o in n||r.push(`Missing required field: ${o}`);return r.length===0?{valid:!0}:{valid:!1,errors:r}}async function vt(t){let e=await Re("rcan-node.schema.json");if(!e)return{valid:!0,skipped:!0};let r=[];if(typeof t!="object"||t===null)return{valid:!1,errors:["Manifest must be an object"]};let n=t,s=e.required??[];for(let o of s)o in n||r.push(`Missing required field: ${o}`);return r.length===0?{valid:!0}:{valid:!1,errors:r}}var qe=(n=>(n[n.FIRE_AND_FORGET=0]="FIRE_AND_FORGET",n[n.ACKNOWLEDGED=1]="ACKNOWLEDGED",n[n.EXACTLY_ONCE=2]="EXACTLY_ONCE",n))(qe||{}),_e=class t extends Error{constructor(e){super(`ACK timeout for message ${e} \u2014 safety halt required`),this.name="QoSAckTimeoutError",Object.setPrototypeOf(this,t.prototype)}},Se=class{_send;_waitForAck;constructor(e,r){this._send=e,this._waitForAck=r}async sendWithQoS(e,r={}){let n=r.qos??0,s=r.maxRetries??3,o=r.initialBackoffMs??100,i=r.ackTimeoutMs??500;if(n===0)return await this._send(e),{delivered:!0,attempts:1,reason:"fire-and-forget"};let a=e.message_id??e.msg_id??"unknown",d=0,u=o;for(;d<=s;){if(await this._send(e),d++,await this._waitForAck(a,i))return{delivered:!0,attempts:d,reason:n===2?"exactly-once":"acknowledged"};if(d>s)break;await nn(u),u=Math.min(u*2,5e3)}return{delivered:!1,attempts:d,reason:`ACK not received after ${s} retries`}}};function Ot(t,e){return{message_type:6,ruri:t,safety_event:"ESTOP",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:rn(),qos:2}}function rn(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let t=Array.from({length:16},()=>Math.floor(Math.random()*256));t[6]=t[6]&15|64,t[8]=t[8]&63|128;let e=t.map(r=>r.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}function nn(t){return new Promise(e=>setTimeout(e,t))}var Fe=6;function Ee(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let t=Array.from({length:16},()=>Math.floor(Math.random()*256));t[6]=t[6]&15|64,t[8]=t[8]&63|128;let e=t.map(r=>r.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}function Nt(t,e){return{message_type:6,ruri:t,safety_event:"ESTOP",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:Ee(),qos:2}}function kt(t,e){return{message_type:6,ruri:t,safety_event:"STOP",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:Ee()}}function It(t,e){return{message_type:6,ruri:t,safety_event:"RESUME",reason:e.slice(0,512),timestamp_ms:Date.now(),message_id:Ee()}}function Mt(t){return typeof t=="object"&&t!==null&&t.message_type===Fe}function Ut(t){let e=[];return t.message_type!==6&&e.push("message_type must be 6"),t.ruri||e.push("ruri is required"),["ESTOP","STOP","RESUME"].includes(t.safety_event??"")||e.push("safety_event must be ESTOP, STOP, or RESUME"),(!t.reason||t.reason.length===0)&&e.push("reason is required"),t.message_id||e.push("message_id is required"),(!t.timestamp_ms||t.timestamp_ms<=0)&&e.push("timestamp_ms must be positive"),e}function Pt(t,e,r){return{message_type:11,ruri:t,disclosure:e,timestamp_ms:Date.now(),message_id:Ee(),delegation_chain:r}}var be=class{windowSeconds;maxSize;_seen;constructor(e=30,r=1e4){this.windowSeconds=e,this.maxSize=r,this._seen=new Map}checkAndRecord(e,r,n=!1){let s=Date.now();this._evict(s);let o=n?Math.min(this.windowSeconds,10):this.windowSeconds,i=sn(r);if(i===null)return{allowed:!1,reason:`invalid timestamp format: ${r}`};let a=s-i,d=o*1e3;if(a>d)return{allowed:!1,reason:`message too old: age=${Math.round(a/1e3)}s > window=${o}s`};if(i>s+5e3)return{allowed:!1,reason:"message timestamp is in the future"};if(this._seen.has(e))return{allowed:!1,reason:`replay detected: msg_id ${e} already seen`};if(this._seen.size>=this.maxSize){let l=this._seen.keys().next().value;this._seen.delete(l)}let u=s+d;return this._seen.set(e,u),{allowed:!0,reason:"ok"}}_evict(e){for(let[r,n]of this._seen)n<=e&&this._seen.delete(r)}get size(){return this._seen.size}};function Dt(t,e){let r=t,n=r.message_id??r.msg_id;if(!n)return{valid:!1,reason:"missing message_id / msg_id"};let s;if(typeof r.timestamp_ms=="number"?s=String(r.timestamp_ms/1e3):r.timestamp!==void 0&&(s=String(r.timestamp)),!s)return{valid:!1,reason:"missing timestamp"};let o=r.message_type===6||r.message_type===6,i=e.checkAndRecord(n,s,o);return{valid:i.allowed,reason:i.reason}}function sn(t){if(t.includes("T")||t.includes("-")){let r=new Date(t);if(!isNaN(r.getTime()))return r.getTime()}let e=parseFloat(t);return isNaN(e)?null:e>1e12?e:e*1e3}var G=class t extends Error{offsetSeconds;constructor(e,r){super(`Clock drift too large: offset=${e.toFixed(3)}s > max=${r}s`),this.name="ClockDriftError",this.offsetSeconds=e,Object.setPrototypeOf(this,t.prototype)}};async function He(t){let e=t??"https://worldtimeapi.org/api/ip";try{let r=Date.now(),n=await fetch(e,{method:"HEAD",signal:AbortSignal.timeout(3e3)}),s=Date.now(),o=n.headers.get("Date")??n.headers.get("date");if(!o)return{synchronized:!0,offsetSeconds:0,source:"assumed (no Date header)"};let i=new Date(o).getTime();if(isNaN(i))return{synchronized:!0,offsetSeconds:0,source:"assumed (unparseable Date header)"};let d=((r+s)/2-i)/1e3;return{synchronized:Math.abs(d)<=5,offsetSeconds:d,source:e}}catch{return{synchronized:!0,offsetSeconds:0,source:"assumed (network unavailable)"}}}async function $t(t=5){let e=await He();if(!e.synchronized||Math.abs(e.offsetSeconds)>t)throw new G(e.offsetSeconds,t)}async function on(t){let e=JSON.stringify(t,Object.keys(t).sort());if(typeof crypto<"u"&&crypto.subtle){let n=new TextEncoder().encode(e),s=await crypto.subtle.digest("SHA-256",n);return Array.from(new Uint8Array(s)).map(o=>o.toString(16).padStart(2,"0")).join("")}let r=2166136261;for(let n=0;n<e.length;n++)r^=e.charCodeAt(n),r=r*16777619>>>0;return r.toString(16).padStart(8,"0")}async function Lt(t,e,r,n="rcan://local/config",s=!1){let o=await on(t);return new m({rcan:h,cmd:"CONFIG_UPDATE",target:n,params:{message_type:5,diff:t,rollback:r,scope:e,config_hash:o,safety_overrides:s}})}function jt(t){let e=t.params;return!e.diff||typeof e.diff!="object"?{valid:!1,reason:"missing required field: params.diff"}:!e.config_hash||typeof e.config_hash!="string"?{valid:!1,reason:"missing required field: params.config_hash"}:"rollback"in e?e.safety_overrides===!0&&e.scope!=="creator"?{valid:!1,reason:"safety_overrides=true requires scope=creator (owner is insufficient)"}:{valid:!0,reason:"ok"}:{valid:!1,reason:"missing required field: params.rollback"}}function an(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`}var Ae=class{_keys=[];addKey(e){this._keys.push(e)}getJWKS(){return{keys:[...this._keys]}}findKey(e){return this._keys.find(r=>r.kid===e)}isKeyValid(e,r){let n=this.findKey(e);if(!n)return!1;let s=(r??Date.now())/1e3;return!(n.revoked_at!==void 0&&n.revoked_at<=s||n.exp!==void 0&&n.exp<s)}expireKey(e,r){let n=this.findKey(e);n&&(n.exp=r??Math.floor(Date.now()/1e3))}revokeKey(e){let r=this.findKey(e);r&&(r.revoked_at=Math.floor(Date.now()/1e3))}validKeys(e){return this._keys.filter(r=>this.isKeyValid(r.kid,e))}};function qt(t,e,r=120,n="rcan://local/keys"){let s=an().slice(0,8);return new m({rcan:h,cmd:"KEY_ROTATION",target:n,params:{message_type:5,new_public_key:t,new_kid:s,old_kid:e,overlap_seconds:r,initiated_at:new Date().toISOString()},keyId:s})}function cn(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`}function Ce(t){let e=t.requestId??cn();return new m({rcan:h,cmd:"CONSENT_REQUEST",target:t.targetRuri,params:{message_type:20,requester_ruri:t.requesterRuri,requester_owner:t.requesterOwner,target_ruri:t.targetRuri,requested_scopes:t.requestedScopes,duration_hours:t.durationHours,justification:t.justification,request_id:e,consent_type:t.consentType??"cross_robot",data_categories:t.dataCategories??[]}})}function we(t){let e=t.expiresAt??new Date(Date.now()+864e5).toISOString();return new m({rcan:h,cmd:"CONSENT_GRANT",target:"rcan://local/consent",params:{message_type:21,request_id:t.requestId,granted_scopes:t.grantedScopes??[],expires_at:e,reason:t.reason??"approved"}})}function xe(t){return new m({rcan:h,cmd:"CONSENT_DENY",target:"rcan://local/consent",params:{message_type:22,request_id:t.requestId,reason:t.reason??"denied"}})}function Ft(t){let e=t.cmd,r=t.params,n=r.message_type;return e==="CONSENT_REQUEST"?n!==20?{valid:!1,reason:"message_type must be CONSENT_REQUEST (20)"}:r.requester_ruri?r.target_ruri?!r.requested_scopes||!Array.isArray(r.requested_scopes)||r.requested_scopes.length===0?{valid:!1,reason:"requested_scopes must be a non-empty array"}:r.request_id?r.justification?{valid:!0,reason:"ok"}:{valid:!1,reason:"missing justification"}:{valid:!1,reason:"missing request_id"}:{valid:!1,reason:"missing target_ruri"}:{valid:!1,reason:"missing requester_ruri"}:e==="CONSENT_GRANT"?n!==21?{valid:!1,reason:"message_type must be CONSENT_GRANT (21)"}:r.request_id?r.expires_at?{valid:!0,reason:"ok"}:{valid:!1,reason:"missing expires_at"}:{valid:!1,reason:"missing request_id"}:e==="CONSENT_DENY"?n!==22?{valid:!1,reason:"message_type must be CONSENT_DENY (22)"}:r.request_id?{valid:!0,reason:"ok"}:{valid:!1,reason:"missing request_id"}:{valid:!1,reason:`unknown consent command: ${e}`}}var dn=3600*1e3,W=class{_cache=new Map;get(e,r){let n=this._cache.get(e);if(!n)return;let s=r??Date.now();if(n.cachedUntil!==void 0&&n.cachedUntil<s){this._cache.delete(e);return}return n}set(e,r){let n=r??Date.now();this._cache.set(e.rrn,{...e,cachedUntil:n+dn})}invalidate(e){this._cache.delete(e)}get size(){return this._cache.size}};async function Ht(t,e,r){let n=r??new W,s=n.get(t);if(s)return s;let o=`${e.replace(/\/$/,"")}/api/v1/robots/${encodeURIComponent(t)}/revocation-status`;try{let i=await fetch(o,{signal:AbortSignal.timeout(5e3)});if(!i.ok){let u={rrn:t,status:"active",reason:`registry returned ${i.status}`};return n.set(u),u}let a=await i.json(),d={rrn:t,status:a.status??"active",revokedAt:a.revokedAt,reason:a.reason,authority:a.authority};return n.set(d),d}catch{return{rrn:t,status:"active",reason:"network unavailable"}}}function Bt(t,e){return new m({rcan:h,cmd:"ROBOT_REVOCATION",target:"rcan://broadcast/revocation",params:{message_type:19,rrn:t,reason:e,revoked_at:new Date().toISOString()}})}var Be=(o=>(o.VIDEO="video",o.AUDIO="audio",o.LOCATION="location",o.BIOMETRIC="biometric",o.TELEMETRY="telemetry",o))(Be||{});function Vt(t){return Ce({requesterRuri:t.requesterRuri,requesterOwner:t.requesterOwner,targetRuri:t.targetRuri,requestedScopes:["training_data"],durationHours:t.durationHours,justification:t.justification,requestId:t.requestId,consentType:"training_data",dataCategories:t.dataCategories})}function Kt(t){return we(t)}function Jt(t){return xe(t)}function Gt(t){if(t.params.message_type!==36)return{valid:!1,reason:"not a TRAINING_DATA message"};let e=t.params.consent_token;return!e||typeof e!="string"||e.trim()===""?{valid:!1,reason:"TRAINING_DATA message missing consent_token (\xA717)"}:{valid:!0,reason:"ok"}}var Te=class{crossOwnerGraceS;keyTtlS;_cachedKeys=[];constructor(e=3600,r=86400){this.crossOwnerGraceS=e,this.keyTtlS=r}canAcceptCommand(e,r,n,s=!0,o=!1,i,a){if(e&&e.message_type===6&&e.safety_event==="ESTOP")return{allowed:!0,reason:"ESTOP always accepted (Protocol 66)"};if(!r)return{allowed:!0,reason:"online mode"};if(!n)return{allowed:!1,reason:"offline mode: cross-network commands blocked"};if(!s)return{allowed:!1,reason:"offline mode: only owner-role commands accepted from local network"};if(o&&i!==void 0){let u=((a??Date.now())-i)/1e3;if(u>this.crossOwnerGraceS)return{allowed:!1,reason:`offline mode: cross-owner grace period expired (${Math.round(u)}s > ${this.crossOwnerGraceS}s)`}}return{allowed:!0,reason:"offline mode: owner command on local network accepted"}}cacheKey(e,r){let n=r??Date.now();this._cachedKeys=this._cachedKeys.filter(s=>s.kid!==e.kid),this._cachedKeys.push({...e,cachedAtMs:n,ttlSeconds:this.keyTtlS})}getCachedKey(e,r){let n=r??Date.now(),s=this._cachedKeys.find(i=>i.kid===e);if(!s)return;if((n-s.cachedAtMs)/1e3>s.ttlSeconds){this._cachedKeys=this._cachedKeys.filter(i=>i.kid!==e);return}return s}getManifestFields(e,r){if(e===void 0)return{offline_mode:!1,offline_since_s:0};let n=r??Date.now();return{offline_mode:!0,offline_since_s:Math.round((n-e)/1e3)}}};var Ve=(g=>(g.SENSOR_PROXIMITY_FAILURE="SENSOR_PROXIMITY_FAILURE",g.SENSOR_CAMERA_FAILURE="SENSOR_CAMERA_FAILURE",g.SENSOR_IMU_FAILURE="SENSOR_IMU_FAILURE",g.MOTOR_OVERCURRENT="MOTOR_OVERCURRENT",g.MOTOR_OVERTEMP="MOTOR_OVERTEMP",g.MOTOR_STALL="MOTOR_STALL",g.BATTERY_CRITICAL="BATTERY_CRITICAL",g.BATTERY_LOW="BATTERY_LOW",g.NETWORK_TIMEOUT="NETWORK_TIMEOUT",g.NETWORK_REGISTRY_UNREACHABLE="NETWORK_REGISTRY_UNREACHABLE",g.SAFETY_ESTOP_STUCK="SAFETY_ESTOP_STUCK",g.SAFETY_WATCHDOG_TIMEOUT="SAFETY_WATCHDOG_TIMEOUT",g.UNKNOWN="UNKNOWN",g))(Ve||{});function Wt(t){return new m({rcan:h,cmd:"FAULT_REPORT",target:t.target??"rcan://local/fault",params:{message_type:26,fault_code:t.faultCode,severity:t.severity,subsystem:t.subsystem,affects_safety:t.affectsSafety,safe_to_continue:t.safeToContinue,description:t.description??"",reported_at:new Date().toISOString()}})}var D=(a=>(a[a.GUEST=1]="GUEST",a[a.OPERATOR=2]="OPERATOR",a[a.CONTRIBUTOR=3]="CONTRIBUTOR",a[a.ADMIN=4]="ADMIN",a[a.M2M_PEER=5]="M2M_PEER",a[a.CREATOR=6]="CREATOR",a[a.M2M_TRUSTED=7]="M2M_TRUSTED",a))(D||{}),Yt=D,B={1:1,2:2,3:2.5,4:3,5:4,6:5,7:6},un=new Map(Object.entries(B).map(([t,e])=>[e,Number(t)]));function Y(t){return un.get(t)}var Ke={status:1,discover:1,chat:1,observer:1,contribute:3,control:2,teleop:2,training:4,training_data:4,config:4,authority:4,admin:6,safety:6,estop:6,"fleet.trusted":7};var zt={minRoleForDiscover:1,minRoleForStatus:1,minRoleForChat:1,minRoleForControl:1,minRoleForSafety:1},Qt={minRoleForDiscover:1,minRoleForStatus:1,minRoleForChat:1,minRoleForControl:2,minRoleForSafety:6};function Xt(t){try{let e=t.split(".");if(e.length<2)return null;let r=(e[1]??"").replace(/-/g,"+").replace(/_/g,"/"),n=r+"=".repeat((4-r.length%4)%4);return JSON.parse(atob(n))}catch{return null}}function Je(t){let e=Xt(t);if(!e)return 1;let r=e.rcan_role;if(r!=null){let s=Y(Number(r));if(s!==void 0)return s}let n=e.loa;if(n!=null){let s=Y(Number(n));if(s!==void 0)return s}return 1}function ve(t){return Je(t)}function Zt(t){let e=Xt(t);if(!e)return{sub:"",role:1,jwtLevel:1,scopes:[]};let r=e.rcan_role,n=e.loa,s=r!==void 0?Number(r):n!==void 0?Number(n):1,o=Y(s)??1,i=Array.isArray(e.rcan_scopes)?e.rcan_scopes:Array.isArray(e.scopes)?e.scopes:[];return{sub:String(e.sub??""),role:o,jwtLevel:B[o],registryUrl:e.registry_url,scopes:i,verifiedAt:e.verified_at,peerRrn:e.peer_rrn,fleetRrns:Array.isArray(e.fleet_rrns)?e.fleet_rrns:void 0}}function Ge(t,e){let r=Ke[e.toLowerCase()];return r===void 0?t>=2?{ok:!0,reason:""}:{ok:!1,reason:`Unknown scope '${e}': applying OPERATOR minimum. Caller has ${D[t]}.`}:t>=r?{ok:!0,reason:""}:{ok:!1,reason:`Scope '${e}' requires ${D[r]} (JWT level ${B[r]}), but caller has ${D[t]} (JWT level ${B[t]})`}}function er(t,e){return Ge(t,e)}var We=(n=>(n.ROOT="root",n.AUTHORITATIVE="authoritative",n.COMMUNITY="community",n))(We||{}),Ye=(n=>(n.CONSENT="consent",n.REVOCATION="revocation",n.KEY="key",n))(Ye||{}),fn=1440*60*1e3,Oe=class{store=new Map;set(e){this.store.set(e.registryUrl,{identity:e,expiresAt:Date.now()+fn})}lookup(e){let r=this.store.get(e);if(r){if(Date.now()>r.expiresAt){this.store.delete(e);return}return r.identity}}async discoverViaDns(e){let r=`_rcan-registry.${e}`,n;try{n=await re("dns").promises.resolveTxt(r)}catch{return}for(let s of n){let o=s.join("");try{let i=JSON.parse(o);if(i.registryUrl&&i.tier&&i.publicKeyPem&&i.domain){let a={registryUrl:i.registryUrl,tier:i.tier,publicKeyPem:i.publicKeyPem,domain:i.domain,verifiedAt:new Date().toISOString()};return this.set(a),a}}catch{}}}async verifyRegistryJwt(e,r){let n=this.lookup(r);if(!n)throw new Error(`REGISTRY_UNKNOWN: ${r} is not in the trust cache`);let s;try{let i=(e.split(".")[1]??"").replace(/-/g,"+").replace(/_/g,"/"),a=i+"=".repeat((4-i.length%4)%4),d;typeof atob<"u"?d=atob(a):d=Buffer.from(a,"base64").toString("utf-8");let u=JSON.parse(d);s=typeof u.iss=="string"?u.iss:void 0}catch{throw new Error("REGISTRY_JWT_MALFORMED: cannot decode token payload")}if(s!==r)throw new Error(`REGISTRY_JWT_ISS_MISMATCH: expected iss=${r}, got iss=${s??"(none)"}`);return n}};function ln(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let t=Array.from({length:16},()=>Math.floor(Math.random()*256));t[6]=(t[6]??0)&15|64,t[8]=(t[8]??0)&63|128;let e=t.map(r=>r.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}function tr(t,e,r,n){return new m({rcan:"2.1.0",rcanVersion:"2.1.0",cmd:"federation_sync",target:e,params:{msg_type:23,msg_id:ln(),source_registry:t,target_registry:e,sync_type:r,payload:n},timestamp:new Date().toISOString()})}async function rr(t,e,r){let n=t.params?.msg_type;if(n===6||n===6||t.cmd==="estop"||t.cmd==="ESTOP")return{valid:!0,reason:"ESTOP always permitted (P66 invariant)"};let o=t.params?.source_registry??t.params?.from_registry;if(!o||o===e)return{valid:!0,reason:"local registry; no federation check needed"};if(!r.lookup(o))return{valid:!1,reason:`REGISTRY_UNKNOWN: ${o} is not in the local trust cache`};let a=1,d=t.params?.registry_jwt;return d?a=ve(d):typeof t.loa=="number"&&(a=t.loa),a<2?{valid:!1,reason:`LOA_INSUFFICIENT: cross-registry commands require LoA>=2 (OPERATOR), got role=${a}`}:{valid:!0,reason:"cross-registry command accepted"}}var w=class t extends Error{constructor(e){super(e),this.name="TransportError",Object.setPrototypeOf(this,t.prototype)}},Xe=(s=>(s.HTTP="http",s.COMPACT="compact",s.MINIMAL="minimal",s.BLE="ble",s))(Xe||{}),ze={msg_type:"t",msg_id:"i",timestamp:"ts",from_rrn:"f",to_rrn:"to",scope:"s",payload:"p",signature:"sig"},nr=Object.fromEntries(Object.entries(ze).map(([t,e])=>[e,t]));function Ze(t){let e=t.toJSON(),r={};for(let[o,i]of Object.entries(e)){let a=ze[o];r[a??o]=i}if(r.p&&typeof r.p=="object"){let o=r.p,i={};for(let[a,d]of Object.entries(o)){let u=ze[a];i[u??a]=d}r.p=i}let n=JSON.stringify(r);return new TextEncoder().encode(n)}function et(t){let r=new TextDecoder().decode(t),n=JSON.parse(r),s={};for(let[o,i]of Object.entries(n)){let a=nr[o];s[a??o]=i}if(s.payload&&typeof s.payload=="object"){let o=s.payload,i={};for(let[a,d]of Object.entries(o)){let u=nr[a];i[u??a]=d}s.payload=i}return new m({rcan:s.rcan??"1.6",rcanVersion:s.rcanVersion,cmd:s.cmd,target:s.target,params:s.params??s.payload??{},timestamp:s.timestamp,confidence:s.confidence,signature:s.signature})}var Q=32,Qe=6;async function sr(t){let e=new TextEncoder().encode(t),r=new ArrayBuffer(e.byteLength);new Uint8Array(r).set(e);let s=await(globalThis.crypto?.subtle??(await import("crypto")).webcrypto.subtle).digest("SHA-256",r);return new Uint8Array(s)}async function or(t){let e=t.params?.msg_type??0;if(e!==Qe)throw new w(`encodeMinimal only supports SAFETY (type 6) messages; got type=${e}`);let r=t.params?.from_rrn??t.target??"",n=t.params?.to_rrn??t.target??"",s=await sr(r),o=await sr(n),i=(t.signature?.sig??"").replace(/[^A-Za-z0-9+/=]/g,""),a;try{if(typeof atob<"u"){let O=atob(i.slice(0,16));a=new Uint8Array(O.length);for(let y=0;y<O.length;y++)a[y]=O.charCodeAt(y)}else a=Buffer.from(i.slice(0,16),"base64")}catch{a=new Uint8Array(8)}let u=(t.timestamp?Math.floor(new Date(t.timestamp).getTime()/1e3):Math.floor(Date.now()/1e3))>>>0,l=new Uint8Array(Q),V=new DataView(l.buffer);V.setUint16(0,Qe,!1),l.set(s.subarray(0,8),2),l.set(o.subarray(0,8),10),V.setUint32(18,u,!1);let A=new Uint8Array(8);A.set(a.subarray(0,Math.min(8,a.length))),l.set(A,22);let g=0;for(let O=0;O<30;O++)g^=l[O]??0;if(V.setUint16(30,g&65535,!1),l.length!==Q)throw new w(`encodeMinimal assertion failed: expected ${Q} bytes, got ${l.length}`);return l}function ir(t){if(t.length!==Q)throw new w(`decodeMinimal: expected ${Q} bytes, got ${t.length}`);let e=new DataView(t.buffer,t.byteOffset,t.byteLength),r=e.getUint16(0,!1),n=t.subarray(2,10),s=t.subarray(10,18),o=e.getUint32(18,!1),i=t.subarray(22,30),a=new Date(o*1e3).toISOString(),d=u=>Array.from(u).map(l=>l.toString(16).padStart(2,"0")).join("");return{params:{msg_type:r,from_hash:d(n),to_hash:d(s),timestamp_s:o,sig_truncated:d(i)},timestamp:a}}var mn=251,z=3;function ar(t,e=mn){let r=Ze(t),n=e-z;if(n<=0)throw new w(`MTU ${e} is too small (need at least ${z+1})`);let s=Math.ceil(r.length/n),o=[];for(let i=0;i<s;i++){let a=r.subarray(i*n,(i+1)*n),d=new Uint8Array(z+a.length);d[0]=i,d[1]=s,d[2]=i===s-1?1:0,d.set(a,z),o.push(d)}return o}function cr(t){if(t.length===0)throw new w("decodeBleFrames: no frames provided");let e=[...t].sort((a,d)=>(a[0]??0)-(d[0]??0)),r=e[0]?.[1]??e.length;if(e.length!==r)throw new w(`decodeBleFrames: expected ${r} frames, got ${e.length}`);let n=e.map(a=>a.subarray(z)),s=n.reduce((a,d)=>a+d.length,0),o=new Uint8Array(s),i=0;for(let a of n)o.set(a,i),i+=a.length;return et(o)}function dr(t,e){let n=(e.params?.msg_type??0)===Qe,s=o=>t.includes(o);if(n){if(s("minimal"))return"minimal";if(s("ble"))return"ble";if(s("compact"))return"compact";if(s("http"))return"http"}else{if(s("http"))return"http";if(s("compact"))return"compact";if(s("ble"))return"ble"}throw new w(`No suitable transport available from: [${t.join(", ")}]`)}var tt=(r=>(r.BASE64="base64",r.REF="ref",r))(tt||{});function X(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();let t=Array.from({length:16},()=>Math.floor(Math.random()*256));t[6]=(t[6]??0)&15|64,t[8]=(t[8]??0)&63|128;let e=t.map(r=>r.toString(16).padStart(2,"0"));return`${e.slice(0,4).join("")}-${e.slice(4,6).join("")}-${e.slice(6,8).join("")}-${e.slice(8,10).join("")}-${e.slice(10).join("")}`}async function rt(t){let e=globalThis.crypto?.subtle??(await import("crypto")).webcrypto.subtle,r=new ArrayBuffer(t.byteLength);new Uint8Array(r).set(t);let n=await e.digest("SHA-256",r),s=new Uint8Array(n);return Array.from(s).map(o=>o.toString(16).padStart(2,"0")).join("")}function ur(t){if(typeof Buffer<"u")return Buffer.from(t).toString("base64");let e="";for(let r=0;r<t.length;r++)e+=String.fromCharCode(t[r]??0);return btoa(e)}function nt(t,e){let r={...t.toJSON(),...e};return m.fromJSON(r)}async function st(t,e,r){let n=await rt(e),s=ur(e),o={chunkId:X(),mimeType:r,encoding:"base64",hashSha256:n,dataB64:s,sizeBytes:e.length},i=t.mediaChunks??[];return nt(t,{mediaChunks:[...i,o]})}function fr(t,e,r,n,s){let o={chunkId:X(),mimeType:r,encoding:"ref",hashSha256:n,refUrl:e,sizeBytes:s},i=t.mediaChunks??[];return nt(t,{mediaChunks:[...i,o]})}async function lr(t){let e=t.mediaChunks??[];if(e.length===0)return{valid:!0,reason:"no media chunks"};for(let r=0;r<e.length;r++){let n=e[r];if(!n.chunkId)return{valid:!1,reason:`chunk[${r}]: missing chunkId`};if(!n.mimeType)return{valid:!1,reason:`chunk[${r}]: missing mimeType`};if(!n.hashSha256)return{valid:!1,reason:`chunk[${r}]: missing hashSha256`};if(n.sizeBytes<0)return{valid:!1,reason:`chunk[${r}]: sizeBytes must be >= 0`};if(n.encoding==="base64"){if(!n.dataB64)return{valid:!1,reason:`chunk[${r}]: BASE64 encoding requires dataB64`};let s;try{if(typeof Buffer<"u")s=Buffer.from(n.dataB64,"base64");else{let i=atob(n.dataB64);s=new Uint8Array(i.length);for(let a=0;a<i.length;a++)s[a]=i.charCodeAt(a)}}catch{return{valid:!1,reason:`chunk[${r}]: failed to decode base64 data`}}let o=await rt(s);if(o!==n.hashSha256)return{valid:!1,reason:`chunk[${r}]: SHA-256 mismatch (expected ${n.hashSha256}, got ${o})`}}else if(n.encoding==="ref"){if(!n.refUrl)return{valid:!1,reason:`chunk[${r}]: REF encoding requires refUrl`}}else return{valid:!1,reason:`chunk[${r}]: unknown encoding '${n.encoding}'`}}return{valid:!0,reason:"ok"}}async function mr(t){let e=new m({rcan:"1.6",rcanVersion:"1.6",cmd:"training_data",target:"rcan://training/data",params:{msg_type:36,msg_id:X()},timestamp:new Date().toISOString()});for(let r of t)e=await st(e,r.data,r.mimeType);return e}async function pr(t,e,r,n,s){let o=await rt(e),i=ur(e),a={chunkId:X(),mimeType:r,encoding:"base64",hashSha256:o,dataB64:i,sizeBytes:e.length},d={streamId:t,chunkIndex:n,isFinal:s,chunk:a},u=new m({rcan:"1.6",rcanVersion:"1.6",cmd:"stream_chunk",target:"rcan://streaming/chunk",params:{msg_type:29,msg_id:X(),stream_chunk:d},timestamp:new Date().toISOString()});return u=nt(u,{mediaChunks:[a]}),u}var ot=2.5,pn=0;function gn(){return`cr-${Date.now()}-${++pn}`}function gr(t={}){return{type:33,request_id:t.request_id??gn(),project_id:t.project_id??"",project_name:t.project_name??"",work_unit_id:t.work_unit_id??"",resource_type:t.resource_type??"cpu",estimated_duration_s:t.estimated_duration_s??0,priority:t.priority??0,payload:t.payload??{},timestamp:t.timestamp??Date.now()/1e3}}function hr(t={}){let e={type:34,request_id:t.request_id??"",work_unit_id:t.work_unit_id??"",status:t.status??"completed",resource_type:t.resource_type??"cpu",duration_s:t.duration_s??0,compute_units:t.compute_units??0,result_payload:t.result_payload??{},timestamp:t.timestamp??Date.now()/1e3};return t.error_message!==void 0&&(e.error_message=t.error_message),e}function yr(t={}){return{type:35,request_id:t.request_id??"",work_unit_id:t.work_unit_id??"",reason:t.reason??"",timestamp:t.timestamp??Date.now()/1e3}}function Rr(t,e="request"){return e==="request"||e==="result"?t>=ot:e==="cancel"?t>=2:!1}function _r(t){return t>=3}var it=2,hn=0;function yn(){return`run-${Date.now()}-${++hn}`}function Sr(t={}){return{type:37,competition_id:t.competition_id??"",competition_format:t.competition_format??"sprint",hardware_tier:t.hardware_tier??"",model_id:t.model_id??"",robot_rrn:t.robot_rrn??"",entered_at:t.entered_at??Date.now()/1e3}}function Er(t={}){let e=t.score??0;if(e<0||e>1)throw new Error(`score must be in [0.0, 1.0], got ${e}`);return{type:38,competition_id:t.competition_id??"",candidate_id:t.candidate_id??"",score:e,hardware_tier:t.hardware_tier??"",verified:t.verified??!1,submitted_at:t.submitted_at??Date.now()/1e3}}function br(t={}){return{type:39,season_id:t.season_id??"",class_id:t.class_id??"",standings:t.standings??[],days_remaining:t.days_remaining??0,broadcast_at:t.broadcast_at??Date.now()/1e3}}function Ar(t={}){let e=t.score??0;if(e<0||e>1)throw new Error(`score must be in [0.0, 1.0], got ${e}`);return{type:40,run_id:t.run_id??yn(),run_type:t.run_type??"personal",candidate_id:t.candidate_id??"",score:e,hardware_tier:t.hardware_tier??"",model_id:t.model_id??"",owner_uid:t.owner_uid??"",metrics:t.metrics??{success_rate:0,p66_rate:0,token_efficiency:0,latency_score:0},submitted_to_community:t.submitted_to_community??!1,created_at:t.created_at??Date.now()/1e3}}function Cr(t){return t>=it}var wr="/.well-known/rcan-firmware-manifest.json";function xr(t){let e={rrn:t.rrn,firmware_version:t.firmwareVersion,build_hash:t.buildHash,components:t.components,signed_at:t.signedAt};return t.signature&&(e.signature=t.signature),e}function Tr(t){return{rrn:t.rrn,firmwareVersion:t.firmware_version,buildHash:t.build_hash,components:t.components??[],signedAt:t.signed_at??"",signature:t.signature}}function vr(t){let e={build_hash:t.buildHash,components:t.components.map(r=>({hash:r.hash,name:r.name,version:r.version})),firmware_version:t.firmwareVersion,rrn:t.rrn,signed_at:t.signedAt};return JSON.stringify(e)}var Ne=class extends Error{constructor(e){super(e),this.name="FirmwareIntegrityError"}};function Or(t){let e=[];t.rrn||e.push("rrn is required"),t.firmwareVersion||e.push("firmwareVersion is required"),t.buildHash||e.push("buildHash is required"),t.buildHash.startsWith("sha256:")||e.push("buildHash must start with 'sha256:'"),t.signedAt||e.push("signedAt is required"),t.signature||e.push("signature is required (manifest must be signed)");for(let[r,n]of t.components.entries())n.name||e.push(`components[${r}].name is required`),n.version||e.push(`components[${r}].version is required`),n.hash.startsWith("sha256:")||e.push(`components[${r}].hash must start with 'sha256:'`);return e}function Nr(t){return{request_id:t.requestId,authority_id:t.authorityId,requested_data:t.requestedData,justification:t.justification,expires_at:t.expiresAt}}function kr(t){return{requestId:t.request_id,authorityId:t.authority_id,requestedData:t.requested_data??[],justification:t.justification??"",expiresAt:t.expires_at??0}}function at(t){let e=[];return t.requestId||e.push("requestId is required"),t.authorityId||e.push("authorityId is required"),(!t.requestedData||t.requestedData.length===0)&&e.push("requestedData must include at least one category"),t.justification||e.push("justification is required"),(!t.expiresAt||t.expiresAt<=0)&&e.push("expiresAt must be a positive Unix timestamp"),t.expiresAt<Date.now()/1e3&&e.push("expiresAt is in the past \u2014 request has expired"),e}function Ir(t){return Date.now()/1e3<t.expiresAt&&at(t).length===0}var Mr={NOT_RECOGNIZED:"AUTHORITY_NOT_RECOGNIZED",REQUEST_EXPIRED:"AUTHORITY_REQUEST_EXPIRED",INVALID_TOKEN:"AUTHORITY_INVALID_TOKEN",RATE_LIMITED:"AUTHORITY_RATE_LIMITED"};var ct="https://api.rrf.rcan.dev/v2/revocations",ke="rrf.rcan.dev",dt=55e3;var S=class extends Error{constructor(e){super(e),this.name="M2MAuthError"}};function Ur(t){let e=t.split(".");if(e.length<2)throw new S("Invalid JWT structure");let r=(e[1]??"").replace(/-/g,"+").replace(/_/g,"/"),n=r+"=".repeat((4-r.length%4)%4);try{return JSON.parse(atob(n))}catch(s){throw new S(`JWT payload decode failed: ${String(s)}`)}}function Pr(t){let e=Ur(t),r=Number(e.exp??0);if(r>0&&Date.now()/1e3>r)throw new S(`M2M_PEER token expired (sub=${String(e.sub)})`);let n=String(e.peer_rrn??"");if(!n)throw new S("M2M_PEER token missing peer_rrn claim");return{sub:String(e.sub??""),peerRrn:n,scopes:Array.isArray(e.rcan_scopes)?e.rcan_scopes:Array.isArray(e.scopes)?e.scopes:[],exp:r,iss:String(e.iss??"")}}function ut(t){let e=Ur(t),r=String(e.iss??"");if(r!==ke)throw new S(`M2M_TRUSTED issuer must be '${ke}', got '${r}'`);let n=Array.isArray(e.rcan_scopes)?e.rcan_scopes:Array.isArray(e.scopes)?e.scopes:[];if(!n.includes("fleet.trusted"))throw new S("M2M_TRUSTED token missing required 'fleet.trusted' scope");let s=Number(e.exp??0);if(s>0&&Date.now()/1e3>s)throw new S(`M2M_TRUSTED token expired (sub=${String(e.sub)})`);let o=String(e.rrf_sig??"");if(!o)throw new S("M2M_TRUSTED token missing rrf_sig claim");let i=Array.isArray(e.fleet_rrns)?e.fleet_rrns:[];return{sub:String(e.sub??""),fleetRrns:i,scopes:n,exp:s,iss:r,rrfSig:o}}function ft(t,e){let r=ut(t);if(!r.fleetRrns.includes(e))throw new S(`M2M_TRUSTED token does not authorize commanding '${e}'. Authorized fleet: [${r.fleetRrns.join(", ")}]`);return r}var $=null;async function lt(t=ct){let e=Date.now();if($&&e-$.fetchedAt<dt)return $;try{let n=await(await fetch(t,{signal:AbortSignal.timeout?.(5e3)})).json();$={revokedOrchestrators:new Set(n.revoked_orchestrators??[]),revokedJtis:new Set(n.revoked_jtis??[]),fetchedAt:e}}catch{if($)return $;$={revokedOrchestrators:new Set,revokedJtis:new Set,fetchedAt:e}}return $}async function mt(t,e){let r=await lt();return!!(r.revokedOrchestrators.has(t.sub)||e&&r.revokedJtis.has(e))}async function Dr(t,e,r){let n=ft(t,e);if(!r?.skipRevocationCheck&&await mt(n))throw new S(`M2M_TRUSTED orchestrator '${n.sub}' is on the RRF revocation list`);return n}var Rn="0.6.0",_n="1.6";return Jr(Sn);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@continuonai/rcan-ts",
3
- "version": "0.6.0",
3
+ "version": "1.1.0",
4
4
  "description": "Official TypeScript SDK for the RCAN v1.6 robot communication protocol",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -66,4 +66,4 @@
66
66
  "publishConfig": {
67
67
  "access": "public"
68
68
  }
69
- }
69
+ }