@probnaya/057-cli 1.2.2 → 1.2.3

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.
@@ -44,6 +44,11 @@ CREATE INDEX IF NOT EXISTS events_source ON events(source_id, occurred_at);
44
44
  -- 057 took to start listening. With it, 0.13 s. An index, not a schema change: older builds open a
45
45
  -- store that has it, so user_version stays where it is.
46
46
  CREATE INDEX IF NOT EXISTS events_subject_time ON events(subject_type, subject_id, occurred_at);
47
+ -- Window hydration needs every declaration, including later annotations, but no observations.
48
+ -- The general subject index visits a subject's entire history just to reject non-began rows;
49
+ -- on a long-lived session this delayed the first Display frame by more than 20 seconds.
50
+ CREATE INDEX IF NOT EXISTS events_declarations
51
+ ON events(subject_type, subject_id, occurred_at, id) WHERE kind = 'began';
47
52
 
48
53
  CREATE TABLE IF NOT EXISTS claims (
49
54
  event_id TEXT NOT NULL,
@@ -111,7 +116,7 @@ data: ${l.wire}
111
116
  `)}async function d1(t){let e=0;const r=[];for await(const n of t){const s=Buffer.isBuffer(n)?n:Buffer.from(n);if(e+=s.length,e>l1)throw new Error("request body is too large");r.push(s)}return JSON.parse(Buffer.concat(r).toString("utf8"))}function f1(t){if(!t||typeof t!="object")throw new Error("pairing request must be an object");const e=t;for(const r of["coreId","grantId","secret","deviceId","deviceName","publicKey","signature"])if(typeof e[r]!="string"||e[r].length===0)throw new Error(`pairing request has no ${r}`);if(typeof e.protocol!="number")throw new Error("pairing request has no protocol version");if(e.deviceId.length>128||e.deviceName.length>128)throw new Error("Display identity is too long");return e}function p1(t){const e=t.socket;return e.authorized?e.getPeerCertificate().fingerprint256?.replaceAll(":","").toLowerCase()??null:null}async function h1(t){const e=await fetch(`${t}/api/landscape?window=live`,{headers:{"x-057-client":"display-delivery"},signal:AbortSignal.timeout(6e4)});if(!e.ok)throw new Error(`private Core returned ${e.status}`);const r=await e.json();if(r.contract!==hi||r.version!==qs||!r.landscape)throw new Error("private Core returned an incompatible landscape envelope");const n={contract:hi,version:qs,landscape:r.landscape};return{wire:JSON.stringify(n),envelope:n}}function m1(t,e,r){r1(t,async()=>{const n=await h1(e),s=Qn("sha256").update(n.wire).digest("base64url").slice(0,16);return{wire:n.wire,id:s}},n=>r.warn(`[057] Display landscape delivery failed: ${String(n)}`))}function y1(t,e){const r=t.socket.remoteAddress;return(r===xl||r==="::1"||r==="::ffff:127.0.0.1")&&t.headers.authorization===`Bearer ${e}`}async function g1(t){const e=await QE(t.storePath),r=new o1(Rv(t.storePath)),n=new c1(e),s=cb({key:e.serverPrivateKey,cert:`${e.serverCertificate.trimEnd()}
112
117
  ${e.caCertificate.trimStart()}`,ca:e.caCertificate,minVersion:"TLSv1.3",maxVersion:"TLSv1.3",requestCert:!0,rejectUnauthorized:!1},(l,d)=>{(async()=>{const m=new URL(l.url??"/","https://057-display.local");if(l.method==="POST"&&m.pathname==="/v1/pair"){try{const p=f1(await d1(l)),y=await n.redeem(p,r);Lt(d,201,{protocol:Zi,coreId:e.coreId,caPin:e.caPin,caCertificate:Buffer.from(new js(e.caCertificate).raw).toString("base64"),clientCertificate:y.certificateDer,expiresAt:y.expiresAt})}catch(p){t.log.warn(`[057] Display pairing refused: ${String(p)}`),Lt(d,403,{error:p instanceof Error?p.message:String(p)})}return}if(l.method==="GET"&&m.pathname==="/v1/landscape"){const p=p1(l);if(!p||!r.trusts(p)){Lt(d,401,{error:"trusted Display certificate required"});return}m1(d,t.loopbackUrl,t.log);return}Lt(d,404,{error:"not found"})})().catch(m=>{t.log.error(`[057] Display request failed: ${String(m)}`),d.headersSent?d.end():Lt(d,500,{error:"Display request failed"})})});s.requestTimeout=15e3,s.headersTimeout=15e3;const o=await cm(s,t.port??0,u1),i=Dn(32).toString("base64url"),a=pm((l,d)=>{(async()=>{if(!y1(l,i)){Lt(d,403,{error:"local Display control token required"});return}const m=new URL(l.url??"/","http://localhost");if(l.method==="POST"&&m.pathname==="/v1/grants"){Lt(d,201,n.create());return}if(l.method==="GET"&&m.pathname==="/v1/displays"){Lt(d,200,{displays:r.list()});return}if(l.method==="DELETE"&&m.pathname.startsWith("/v1/displays/")){const p=decodeURIComponent(m.pathname.slice(13));Lt(d,r.revoke(p)?200:404,{revoked:p});return}Lt(d,404,{error:"not found"})})()}),c=await cm(a,0,xl).catch(async l=>{throw await jc(s),l}),u={protocol:Zi,landscape:qs,coreId:e.coreId,caPin:e.caPin,port:o,controlUrl:`http://${xl}:${c}`,controlToken:i};return t.log.info(`[057] Display delivery ${e.coreId} listening securely on port ${o}`),{info:u,async close(){await Promise.all([jc(a),jc(s)])}}}const Pd=5057,ar="127.0.0.1",v1=["localhost","127.0.0.1","[::1]","::1"],b1=10,w1={".html":"text/html; charset=utf-8",".js":"text/javascript; charset=utf-8",".css":"text/css; charset=utf-8",".json":"application/json; charset=utf-8",".svg":"image/svg+xml",".woff2":"font/woff2",".png":"image/png",".ico":"image/vnd.microsoft.icon"};function um(t,e,r){let n;try{n=decodeURIComponent(e)}catch{return!1}if(n.includes("\0"))return!1;const s=yo(t,`.${Xv(n)}`);if(s!==t&&!s.startsWith(t+Lc)||!fe(s)||!Qi(s).isFile())return!1;const o=w1[Jv(s).toLowerCase()]??"application/octet-stream";return r.statusCode=200,r.setHeader("content-type",o),r.setHeader("x-content-type-options","nosniff"),ql(r),r.setHeader("cache-control",e.startsWith("/assets/")?"public, max-age=31536000, immutable":"no-store"),dm(s).pipe(r),!0}function A1(t,e,r,n){return new Promise((s,o)=>{let i=n,a=e;const c=u=>{if(u.code==="EADDRINUSE"&&i>1){i--,a++,t.listen(a,r);return}t.off("error",c),o(u)};t.on("error",c),t.once("listening",()=>{t.off("error",c);const u=t.address();s(typeof u=="object"&&u?u.port:a)}),t.listen(a,r)})}function S1(t,e){const r=String(t.headers["sec-fetch-dest"]??"");if(k1.has(r))return`refusing to be embedded as ${r}`;const n=t.headers["sec-fetch-site"];return typeof n=="string"&&n!==""&&n!=="same-origin"&&n!=="none"?`refusing a ${n} request for ${e}`:null}const k1=new Set(["iframe","frame","embed","object","fencedframe"]);async function Mv(t){const e=t.log??Iy,r=yo(t.root),n=t.host??ar,s=n===ar||n==="::1"||n==="localhost",o=t.runtime?.path??t.storePath??sa();if(!t.runtime){const y=await kS(o);if(y)throw new Gl(y)}const i=t.runtime??Cy({path:t.storePath,log:e,retention:t.retention,observe:t.observe}),a=import.meta.url.endsWith(".mjs")?wS(import.meta.url,{path:i.path,retention:i.retention,machineId:i.machine.id}):void 0,c=import.meta.url.endsWith(".mjs")?DS(import.meta.url,{path:i.path,machineId:i.machine.id}):void 0,u=qS({runtime:i,log:e,version:t.version,activity:a,landscape:c,allowedHosts:s?v1:null}),l=pm((y,v)=>{(async()=>{try{if(await u(y,v))return;if(y.method!=="GET"&&y.method!=="HEAD"){v.statusCode=405,v.setHeader("allow","GET, HEAD"),v.end();return}const g=new URL(y.url??"/","http://localhost").pathname,C=S1(y,g);if(C){e.warn(`[057] ${C}`),v.statusCode=403,v.setHeader("content-type","text/plain; charset=utf-8"),ql(v),v.end(`${C}
113
118
  `);return}if(um(r,g==="/"?"/index.html":g,v)||!g.startsWith("/assets/")&&um(r,"/index.html",v))return;v.statusCode=404,v.setHeader("content-type","text/plain; charset=utf-8"),v.end(`not found
114
- `)}catch(g){e.error(`[057] request failed: ${String(g)}`),v.headersSent||(v.statusCode=500),v.end()}})()}),d=await A1(l,t.port??Pd,n,b1).catch(y=>{throw i.stop(),y}),m=s?"localhost":n;let p;if(t.display)try{p=await g1({storePath:i.path,loopbackUrl:`http://${ar}:${d}`,log:e})}catch(y){e.error(`[057] authenticated Display delivery could not start: ${String(y)}`)}return xS(i.path,{pid:process.pid,url:`http://${s?ar:n}:${d}`,version:t.version??"dev",contract:Wl,store:i.path,startedAt:Date.now(),owner:t.owner??"cli",...p?{display:p.info}:{}}),{url:`http://${m}:${d}`,port:d,host:n,runtime:i,server:l,display:p?.info,close(){ES(i.path),a?.close(),c?.close();const y=new Promise(v=>l.close(()=>v()));return l.closeAllConnections?.(),Promise.all([y,p?.close()]).then(()=>i.stop())}}}function x1(t){return yo(Zv(new URL(".",t)),"..","dist")}function E1(t,e){switch(e.operation){case"summary":return{operation:"summary",result:t.summary(e.period)};case"availability":return{operation:"availability",result:t.availability(e.period)};case"compare":return{operation:"compare",result:t.compare(e.periodA,e.periodB)};case"breakdown":return{operation:"breakdown",result:t.breakdown(e.period,e.dimension)}}}const mo="1.2.2";function I1(t){const e={command:"serve",activityOperation:"summary",activityPrevious:!1,activityAscii:!1,open:!1,yes:!1,desktop:!1},r=[...t],n=r[0];if(n&&!n.startsWith("-"))if(n==="status"||n==="compact"||n==="reset")e.command=n,r.shift();else if(n==="activity"){e.command="activity",r.shift();const s=r[0];if(s&&!s.startsWith("-")){const o=j1(s);if(o)e.activityOperation=o.operation,e.activityDimension=o.dimension,e.activityItemFilter=o.itemFilter;else if(s==="today")e.activityOperation="summary";else if(Kw.includes(s))e.activityOperation=s;else return{...e,command:"help",error:`unknown activity operation: ${s}`};r.shift()}}else return{...e,command:"help",error:`unknown command: ${n}`};for(;r.length;){const s=r.shift(),[o,i]=s.includes("=")?C1(s,"="):[s,void 0],a=()=>i??r.shift();switch(o){case"-h":case"--help":e.command="help";break;case"-v":case"--version":e.command="version";break;case"--open":e.open=!0;break;case"--yes":case"-y":e.yes=!0;break;case"--desktop":e.desktop=!0;break;case"--last":{const c=a(),u=c?.match(/^(\d+)d$/),l=u?Number(u[1]):NaN;if(!Number.isInteger(l)||l<1)return{...e,command:"help",error:`--last needs a positive duration such as 7d, got ${c}`};e.activityLastDays=l;break}case"--previous":e.activityPrevious=!0;break;case"--by":{const c=a(),u=["model","executor","lifecycle","outcome","session","duration-band"];if(!c||!u.includes(c))return{...e,command:"help",error:`--by needs one of: ${u.join(", ")}`};e.activityDimension=c;break}case"--width":{const c=a(),u=Number(c);if(!Number.isInteger(u)||u<1)return{...e,command:"help",error:`--width needs a positive number of columns, got ${c}`};e.activityWidth=u;break}case"--color":{const c=a();if(c!=="always"&&c!=="never"&&c!=="auto")return{...e,command:"help",error:`--color needs always, never or auto, got ${c}`};e.activityColor=c;break}case"--ascii":e.activityAscii=!0;break;case"--port":{const c=a(),u=Number(c);if(!Number.isInteger(u)||u<0||u>65535)return{...e,command:"help",error:`--port needs a port number, got ${c}`};e.port=u;break}case"--host":{const c=a();if(!c)return{...e,command:"help",error:"--host needs a value"};e.host=c;break}case"--store":{const c=a();if(!c)return{...e,command:"help",error:"--store needs a path"};e.store=c;break}default:return{...e,command:"help",error:`unknown option: ${o}`}}}return e}function C1(t,e){const r=t.indexOf(e);return[t.slice(0,r),t.slice(r+1)]}const O1=`057 — a spatial interface for observing the shape of a computational workday
119
+ `)}catch(g){e.error(`[057] request failed: ${String(g)}`),v.headersSent||(v.statusCode=500),v.end()}})()}),d=await A1(l,t.port??Pd,n,b1).catch(y=>{throw i.stop(),y}),m=s?"localhost":n;let p;if(t.display)try{p=await g1({storePath:i.path,loopbackUrl:`http://${ar}:${d}`,log:e})}catch(y){e.error(`[057] authenticated Display delivery could not start: ${String(y)}`)}return xS(i.path,{pid:process.pid,url:`http://${s?ar:n}:${d}`,version:t.version??"dev",contract:Wl,store:i.path,startedAt:Date.now(),owner:t.owner??"cli",...p?{display:p.info}:{}}),{url:`http://${m}:${d}`,port:d,host:n,runtime:i,server:l,display:p?.info,close(){ES(i.path),a?.close(),c?.close();const y=new Promise(v=>l.close(()=>v()));return l.closeAllConnections?.(),Promise.all([y,p?.close()]).then(()=>i.stop())}}}function x1(t){return yo(Zv(new URL(".",t)),"..","dist")}function E1(t,e){switch(e.operation){case"summary":return{operation:"summary",result:t.summary(e.period)};case"availability":return{operation:"availability",result:t.availability(e.period)};case"compare":return{operation:"compare",result:t.compare(e.periodA,e.periodB)};case"breakdown":return{operation:"breakdown",result:t.breakdown(e.period,e.dimension)}}}const mo="1.2.3";function I1(t){const e={command:"serve",activityOperation:"summary",activityPrevious:!1,activityAscii:!1,open:!1,yes:!1,desktop:!1},r=[...t],n=r[0];if(n&&!n.startsWith("-"))if(n==="status"||n==="compact"||n==="reset")e.command=n,r.shift();else if(n==="activity"){e.command="activity",r.shift();const s=r[0];if(s&&!s.startsWith("-")){const o=j1(s);if(o)e.activityOperation=o.operation,e.activityDimension=o.dimension,e.activityItemFilter=o.itemFilter;else if(s==="today")e.activityOperation="summary";else if(Kw.includes(s))e.activityOperation=s;else return{...e,command:"help",error:`unknown activity operation: ${s}`};r.shift()}}else return{...e,command:"help",error:`unknown command: ${n}`};for(;r.length;){const s=r.shift(),[o,i]=s.includes("=")?C1(s,"="):[s,void 0],a=()=>i??r.shift();switch(o){case"-h":case"--help":e.command="help";break;case"-v":case"--version":e.command="version";break;case"--open":e.open=!0;break;case"--yes":case"-y":e.yes=!0;break;case"--desktop":e.desktop=!0;break;case"--last":{const c=a(),u=c?.match(/^(\d+)d$/),l=u?Number(u[1]):NaN;if(!Number.isInteger(l)||l<1)return{...e,command:"help",error:`--last needs a positive duration such as 7d, got ${c}`};e.activityLastDays=l;break}case"--previous":e.activityPrevious=!0;break;case"--by":{const c=a(),u=["model","executor","lifecycle","outcome","session","duration-band"];if(!c||!u.includes(c))return{...e,command:"help",error:`--by needs one of: ${u.join(", ")}`};e.activityDimension=c;break}case"--width":{const c=a(),u=Number(c);if(!Number.isInteger(u)||u<1)return{...e,command:"help",error:`--width needs a positive number of columns, got ${c}`};e.activityWidth=u;break}case"--color":{const c=a();if(c!=="always"&&c!=="never"&&c!=="auto")return{...e,command:"help",error:`--color needs always, never or auto, got ${c}`};e.activityColor=c;break}case"--ascii":e.activityAscii=!0;break;case"--port":{const c=a(),u=Number(c);if(!Number.isInteger(u)||u<0||u>65535)return{...e,command:"help",error:`--port needs a port number, got ${c}`};e.port=u;break}case"--host":{const c=a();if(!c)return{...e,command:"help",error:"--host needs a value"};e.host=c;break}case"--store":{const c=a();if(!c)return{...e,command:"help",error:"--store needs a path"};e.store=c;break}default:return{...e,command:"help",error:`unknown option: ${o}`}}}return e}function C1(t,e){const r=t.indexOf(e);return[t.slice(0,r),t.slice(r+1)]}const O1=`057 — a spatial interface for observing the shape of a computational workday
115
120
 
116
121
  057 observe this machine and serve the interface
117
122
  057 status where the store is, what it holds, what is observed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@probnaya/057-cli",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "A spatial interface for observing the shape of a computational workday. Local-first, accountless, offline.",
5
5
  "keywords": [
6
6
  "observability",