@lensmcp/core 1.25.52 → 1.25.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ai-content.js +1 -1
- package/lib/answer-budget.js +2 -2
- package/lib/bus-rotation.js +1 -1
- package/lib/capture-off.js +1 -1
- package/lib/event-bus.js +1 -1
- package/lib/event-loop-history.js +1 -1
- package/lib/event-loop.js +1 -1
- package/lib/event-redactor.js +1 -1
- package/lib/fingerprint.js +1 -1
- package/lib/flow-categories.js +1 -1
- package/lib/flow-segments.d.ts +11 -0
- package/lib/flow-segments.js +1 -1
- package/lib/flow-shape.js +1 -1
- package/lib/flows-config.js +1 -1
- package/lib/graph-from-events.js +1 -1
- package/lib/graph-store.js +1 -1
- package/lib/headless-flow.js +1 -1
- package/lib/http-exchange.js +1 -1
- package/lib/http-shape.js +1 -1
- package/lib/key-pattern.js +1 -1
- package/lib/lens-dir.js +7 -7
- package/lib/lens-events.js +2 -2
- package/lib/patterns.js +1 -1
- package/lib/performance-window.js +2 -2
- package/lib/pg-errors.js +1 -1
- package/lib/redact.js +1 -1
- package/lib/redis-expiry.js +1 -1
- package/lib/reducer-registry.js +1 -1
- package/lib/replay-store.js +1 -1
- package/lib/resource-store.d.ts +6 -0
- package/lib/resource-store.js +1 -1
- package/lib/route-template.js +1 -1
- package/lib/service-hop.js +1 -1
- package/lib/settings.js +2 -2
- package/lib/sql-shape.js +3 -3
- package/lib/sql-values.js +1 -1
- package/lib/stores.js +2 -2
- package/lib/story.js +1 -1
- package/lib/thresholds.js +1 -1
- package/lib/ulid.js +1 -1
- package/package.json +2 -2
package/lib/ai-content.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var a=Object.defineProperty;var r=(t,i)=>a(t,"name",{value:i,configurable:!0});var s=Object.defineProperty,n=r((t,i)=>s(t,"name",{value:i,configurable:!0}),"r");import*as c from"node:fs";import{dirname as l,join as o}from"node:path";export const AI_CONTENT_DIR="ai";const d=/^ai_[0-9a-z]{8,40}$/;export const isAiCallId=n(t=>typeof t=="string"&&d.test(t),"isAiCallId");export function aiContentDir(t){return o(l(t),AI_CONTENT_DIR)}r(aiContentDir,"aiContentDir"),n(aiContentDir,"aiContentDir");export function aiContentFile(t,i){if(!isAiCallId(i))throw new Error("invalid ai call id");return o(aiContentDir(t),`${i}.json`)}r(aiContentFile,"aiContentFile"),n(aiContentFile,"aiContentFile");export function readAiContent(t,i){if(isAiCallId(i))try{const e=JSON.parse(c.readFileSync(aiContentFile(t,i),"utf8"));return e&&e.schemaVersion===1&&e.id===i?e:void 0}catch{return}}r(readAiContent,"readAiContent"),n(readAiContent,"readAiContent");
|
package/lib/answer-budget.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`,r);return o>=r/2&&(r=o),r>0&&(
|
|
1
|
+
"use strict";var L=Object.defineProperty;var m=(e,n)=>L(e,"name",{value:n,configurable:!0});var Y=Object.defineProperty,f=m((e,n)=>Y(e,"name",{value:n,configurable:!0}),"h");import{boundedClone as z}from"@lensmcp/protocol-types";export const DEFAULT_ANSWER_BUDGET_BYTES=24*1024,ANSWER_BUDGET_ENV="LENSMCP_ANSWER_BUDGET_BYTES",MIN_ANSWER_BUDGET_BYTES=2*1024,ANSWER_CUT_KEY="cutToFit",MAX_CUT_NOTE_BYTES=120,FITTED_STRING_CHARS=1500,REPEATED_STRING_MIN_CHARS=200;const C=600,V=32,H=new Set(["timestamp","startedAt","startTime","at","emittedAt","lastSeenAt","endedAt","endTime","t","ts","time"]),K=f(e=>typeof e=="number"&&e>=1e11&&e<=1e14,"isEpochMs"),J=.95;export function answerBudgetBytes(e=process.env){const n=e[ANSWER_BUDGET_ENV];if(n===void 0||n.trim()==="")return DEFAULT_ANSWER_BUDGET_BYTES;const r=Number(n);return!Number.isFinite(r)||r<0?DEFAULT_ANSWER_BUDGET_BYTES:r===0?Number.POSITIVE_INFINITY:Math.max(MIN_ANSWER_BUDGET_BYTES,Math.floor(r))}m(answerBudgetBytes,"answerBudgetBytes"),f(answerBudgetBytes,"answerBudgetBytes");export function utf8Bytes(e){let n=0;for(let r=0;r<e.length;r++){const o=e.charCodeAt(r);o<128?n+=1:o<2048?n+=2:o>=55296&&o<=56319&&r+1<e.length&&(e.charCodeAt(r+1)&64512)===56320?(n+=4,r+=1):n+=3}return n}m(utf8Bytes,"utf8Bytes"),f(utf8Bytes,"utf8Bytes");export function jsonBytes(e){let n;try{n=JSON.stringify(e)}catch{return Number.POSITIVE_INFINITY}return n===void 0?0:utf8Bytes(n)}m(jsonBytes,"jsonBytes"),f(jsonBytes,"jsonBytes");const R=f(e=>typeof e=="object"&&e!==null&&!Array.isArray(e),"isRecord");function O(e){if(!R(e))return!1;const n=Object.getPrototypeOf(e);return n===Object.prototype||n===null}m(O,"C"),f(O,"isPlain");export function newestEndOf(e){const n=e[0];if(e.length<2||!R(n))return;let r;for(const o of Object.keys(n)){if(typeof n[o]!="number"||!(H.has(o)||K(n[o])))continue;let s=0,i=0,u=!0,a=n[o];for(let y=1;y<e.length;y++){const x=e[y],d=R(x)?x[o]:void 0;if(typeof d!="number"){u=!1;break}d>a?s+=1:d<a&&(i+=1),a=d}const h=s+i;if(!u||h===0)continue;const l=Math.max(s,i)/h;l>=J&&(!r||l>r.agreement)&&(r={end:s>=i?"end":"start",agreement:l})}return r?.end}m(newestEndOf,"newestEndOf"),f(newestEndOf,"newestEndOf");export function newestFirst(e){return newestEndOf(e)==="end"?[...e].reverse():e.slice()}m(newestFirst,"newestFirst"),f(newestFirst,"newestFirst");export function shortenText(e,n){if(e.length<=n)return e;let r=Math.max(0,n);const o=e.lastIndexOf(`
|
|
2
|
+
`,r);return o>=r/2&&(r=o),r>0&&(e.charCodeAt(r-1)&64512)===55296&&(r-=1),`${e.slice(0,r)}\u2026[lensmcp truncated: ${e.length-r} more chars]`}m(shortenText,"shortenText"),f(shortenText,"shortenText");export function fitList(e,n,r={}){const o=e.length,s=e.reduce((c,w)=>c+jsonBytes(w)+1,2);if(s<=n)return{items:e.slice(),total:o,returned:o,kept:"all",shortenedStrings:0,repeatedStrings:0,bytes:s};const i=r.newest??newestEndOf(e),u=i??r.keep??"start",a=r.name??"items",h=r.maxStringChars??FITTED_STRING_CHARS,l=u==="start"?e:[...e].reverse(),y=String(Math.max(0,o-1)).length,x=new Map,d=[],b=[];let E=2,_=0,$=0;for(let c=0;c<l.length;c++){const w=new Map,T=[];let M=0;const A=f((t,p,g,G,W)=>{if(typeof t=="string"){if(t.length>=REPEATED_STRING_MIN_CHARS){const S=x.get(t)??w.get(t);if(S)return T.push({holder:G,key:W,owner:S.owner,path:S.path}),`\u2026[lensmcp: same text as ${a}[${"9".repeat(y)}]${S.path}]`;w.set(t,{owner:c,path:p})}return t.length>h?(M+=1,shortenText(t,h)):t}if(g>=V)return t;if(Array.isArray(t)){const S=[];return t.forEach((I,D)=>S.push(A(I,`${p}[${D}]`,g+1,S,D))),S}if(O(t)){const S={};for(const I of Object.keys(t))S[I]=A(t[I],`${p}.${I}`,g+1,S,I);return S}return t},"slim"),B=[];let N=A(l[c],"",0,B,0),v=jsonBytes(N)+1;if(b.length===0&&E+v>n)N=F(l[c],Math.max(0,n-E-1)),v=jsonBytes(N)+1,T.length=0,w.clear(),M=1;else if(E+v>n)break;b.push(N),E+=v,_+=M,$+=T.length;for(const[t,p]of w)x.set(t,p);for(const t of T)d.push(t.holder===B?{...t,holder:b,key:b.length-1}:t)}const k=b.length,j=f(c=>u==="start"?c:k-1-c,"indexOf");if(u==="end"){for(const c of d)c.holder===b&&(c.key=j(c.key));b.reverse()}for(const c of d)c.holder[c.key]=`\u2026[lensmcp: same text as ${a}[${j(c.owner)}]${c.path}]`;return{items:b,total:o,returned:k,kept:k===o?"all":i?"newest":u==="start"?"first":"last",shortenedStrings:_,repeatedStrings:$,bytes:jsonBytes(b)}}m(fitList,"fitList"),f(fitList,"fitList");function F(e,n){let r=Math.max(256,n),o=e;for(let s=0;s<6;s++){if(o=z(e,{rawByteLimit:r,maxStringBytes:Math.min(2e3,Math.max(128,Math.floor(r/4))),maxArrayItems:64}).value,jsonBytes(o)<=n)return o;r=Math.floor(r*.6)}return o}m(F,"D"),f(F,"clampTo");export function shareBudget(e,n){const r=e.map((i,u)=>({size:i,index:u})).sort((i,u)=>i.size-u.size),o=new Array(e.length).fill(0);let s=Math.max(0,n);return r.forEach(({size:i,index:u},a)=>{const h=Math.floor(s/(r.length-a));o[u]=Math.min(i,h),s-=o[u]}),o}m(shareBudget,"shareBudget"),f(shareBudget,"shareBudget");export function cutNote(e){return(e.lists?.length?"Cut to fit: each list in `lists` shows `returned` of `total`; every other count is of the whole.":"Cut to fit: large values were shortened; every count is of the whole.")+(e.more?" Rest: see `more`.":"")}m(cutNote,"cutNote"),f(cutNote,"cutNote");function P(e,n){const r={...e},o=new WeakSet([r]),s=[];let i=0;for(const u of n){const a=u.split("."),h=a[a.length-1];let l=r;for(const y of a.slice(0,-1)){const x=l[y];if(!O(x)){l=void 0;break}const d=o.has(x)?x:{...x};o.add(d),l[y]=d,l=d}!l||l[h]===void 0||l[h]===null||(s.push({keys:a,value:l[h]}),i+=jsonBytes(l[h]),l[h]=null)}return s.length?{body:r,parts:s,bytes:i}:void 0}m(P,"K"),f(P,"holdWhole");function U(e,n){for(const{keys:r,value:o}of n){let s=e;for(const i of r.slice(0,-1)){const u=s[i],a=O(u)?u:{};s[i]=a,s=a}s[r[r.length-1]]=o}return e}m(U,"z"),f(U,"putBack");export function fitAnswer(e,n,r={}){if(!Number.isFinite(n)||e===null||typeof e!="object")return{value:e};const o=jsonBytes(e);if(o<=n)return{value:e};const s=r.whole?.length&&O(e)?P(e,r.whole):void 0,i=s?s.body:e,u=s?jsonBytes(i):o,a=s?Math.max(MIN_ANSWER_BUDGET_BYTES,n-s.bytes):n;if(u<=a)return{value:e};const h=[];let l=0,y=0,x=!1,d;const b=f(()=>(d={budget:n,original:o,...h.length?{lists:h}:{},...l?{shortened:l}:{},...y?{repeated:y}:{},...x?{clamped:!0}:{},...s?{whole:s.parts.map(t=>t.keys.join("."))}:{},...r.more?{more:r.more}:{},note:cutNote({lists:h,more:r.more})},d),"describe"),E=f(t=>({value:s?U(t,s.parts):t,cut:d}),"done");if(Array.isArray(i)){const t=fitList(i,a);return t.kept!=="all"&&h.push({list:"items",total:t.total,returned:t.returned,kept:t.kept}),l=t.shortenedStrings,y=t.repeatedStrings,b(),E(t.items)}if(!O(i))return{value:e};const _=Object.keys(i),$=_.filter(t=>Array.isArray(i[t])&&i[t].length>0),k=$.map(t=>jsonBytes(i[t])),j=u-k.reduce((t,p)=>t+p,0),c=shareBudget(k,a-j-C),w={};$.forEach((t,p)=>{if(k[p]<=c[p])return;const g=fitList(i[t],c[p],{name:t,...r.newest?.[t]?{newest:r.newest[t]}:{}});w[t]=g.items,g.kept!=="all"&&h.push({list:t,total:g.total,returned:g.returned,kept:g.kept}),l+=g.shortenedStrings,y+=g.repeatedStrings});const T=f(t=>({[ANSWER_CUT_KEY]:b(),...t}),"assemble"),M={};for(const t of _)M[t]=t in w?w[t]:i[t];let A=T(M);if(jsonBytes(A)<=a)return E(A);x=!0;let B=Math.max(512,a-C);for(let t=0;t<6;t++){if(A=T(F(M,B)),jsonBytes(A)<=a)return E(A);B=Math.floor(B*.6)}const N={};let v=C;for(const t of _){const p=i[t];if(p!==null&&typeof p=="object")continue;const g=jsonBytes(p)+utf8Bytes(t)+4;if(v+g>a)break;N[t]=p,v+=g}return E(T(N))}m(fitAnswer,"fitAnswer"),f(fitAnswer,"fitAnswer");
|
package/lib/bus-rotation.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var l=Object.defineProperty;var i=(t,r)=>l(t,"name",{value:r,configurable:!0});var S=Object.defineProperty,u=i((t,r)=>S(t,"name",{value:r,configurable:!0}),"u");export const MAX_BUS_GENERATIONS=20,BUS_ROTATE_CHECK_BYTES=1048576;export function busCapsFromEnv(t=process.env){const r=Number(t.LENSMCP_EVENT_MAX_BYTES),e=Number(t.LENSMCP_EVENT_MAX_FILES);return{maxBytes:Number.isFinite(r)&&r>0?r:50*1024*1024,maxFiles:Number.isFinite(e)&&e>=1&&e<=20?Math.floor(e):5}}i(busCapsFromEnv,"busCapsFromEnv"),u(busCapsFromEnv,"busCapsFromEnv");export function rotateBusFile(t,r){const{fs:e,maxBytes:y,maxFiles:a}=r,n=`${t}.rotate.lock`,o=r.staleLockMs??3e4;let s;for(let c=0;c<2&&s===void 0;c++)try{s=e.openSync(n,"wx")}catch{try{if(Date.now()-e.statSync(n).mtimeMs<=o)return"busy";e.unlinkSync(n)}catch{}}if(s===void 0)return"busy";try{try{if(e.statSync(t).size<=y)return"not-needed"}catch{return"not-needed"}try{e.unlinkSync(`${t}.${a}`)}catch{}for(let c=a+1;c<=20;c++)try{e.unlinkSync(`${t}.${c}`)}catch{}for(let c=a-1;c>=1;c--)try{e.renameSync(`${t}.${c}`,`${t}.${c+1}`)}catch{}try{e.renameSync(t,`${t}.1`)}catch{}return"rotated"}finally{try{e.closeSync(s)}catch{}try{e.unlinkSync(n)}catch{}}}i(rotateBusFile,"rotateBusFile"),u(rotateBusFile,"rotateBusFile");export function createBusRotator(t,r){const{fs:e,maxBytes:y}=r,a=Math.max(0,Math.min(r.checkEveryBytes??1048576,y));let n=a,o=0;return s=>{if(n>=a||o+n>y)try{o=e.statSync(t).size,n=0,o>y&&rotateBusFile(t,r)==="rotated"&&(o=0)}catch{}n+=s}}i(createBusRotator,"createBusRotator"),u(createBusRotator,"createBusRotator");
|
package/lib/capture-off.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var d=Object.defineProperty;var s=(r,t)=>d(r,"name",{value:t,configurable:!0});var S=Object.defineProperty,a=s((r,t)=>S(r,"name",{value:t,configurable:!0}),"s");export const CAPTURE_FILE_NAME="capture.json",CAPTURE_OFF_EVERY_MS=2e3;export function readCaptureOff(r){return readCapture(r).off}s(readCaptureOff,"readCaptureOff"),a(readCaptureOff,"readCaptureOff");export function readCapture(r){const t=new Map;try{const e=JSON.parse(r);if(e.parts&&typeof e.parts=="object"&&!Array.isArray(e.parts))for(const[o,p]of Object.entries(e.parts))Array.isArray(p)&&t.set(o,new Set(p.filter(c=>typeof c=="string")));return{off:new Set(Array.isArray(e.off)?e.off.filter(o=>typeof o=="string"&&o.length>0):[]),parts:t}}catch{return{off:new Set,parts:t}}}s(readCapture,"readCapture"),a(readCapture,"readCapture");export function isRecordOff(r,t,e){return!r.size||typeof t!="string"?!1:r.has(t)||typeof e=="string"&&r.has(`${t}:${e}`)}s(isRecordOff,"isRecordOff"),a(isRecordOff,"isRecordOff");export function createCaptureOff(r,t,e={}){const o=e.everyMs??2e3,p=e.now??(()=>Date.now());let c=-1/0,u,y=new Set,h=new Map;const i=a(()=>{u=void 0,y=new Set,h=new Map},"none"),O=a(n=>{c=n;const f=r();if(!f)return i();let l;try{l=t.statSync(f).mtimeMs}catch{return i()}if(l!==u)try{({off:y,parts:h}=readCapture(t.readFileSync(f,"utf8"))),u=l}catch{i()}},"refresh"),A=a(()=>{const n=p();n-c>=o&&O(n)},"look"),w=a(((n,f)=>{try{return A(),isRecordOff(y,n,f)}catch{return!1}}),"ask");return w.part=(n,f)=>{try{return A(),!!n&&h.get(n)?.has(f)===!0}catch{return!1}},w.reset=()=>{c=-1/0,i()},w}s(createCaptureOff,"createCaptureOff"),a(createCaptureOff,"createCaptureOff");
|
package/lib/event-bus.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var o=Object.defineProperty;var n=(e,t)=>o(e,"name",{value:t,configurable:!0});var c=Object.defineProperty,h=n((e,t)=>c(e,"name",{value:t,configurable:!0}),"e");export class EventBus{static{n(this,"EventBus")}static{h(this,"EventBus")}constructor(t={}){this.subs=new Set,this.dropped=0,this.capacity=t.capacity??1e4,this.onDrop=t.onDrop,this.transform=t.transform}publish(t){if(this.subs.size>this.capacity){this.dropped++,this.onDrop?.(1);return}let s=t;if(this.transform)try{s=this.transform(t)??t}catch{s=t}for(const r of this.subs)if(!(r.filter&&!r.filter(s)))try{const i=r.handler(s);i&&typeof i.then=="function"&&i.catch(()=>{})}catch{}}subscribe(t,s){const r={handler:t,filter:s};return this.subs.add(r),()=>{this.subs.delete(r)}}droppedCount(){return this.dropped}subscriberCount(){return this.subs.size}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var d=Object.defineProperty;var m=(n,e)=>d(n,"name",{value:e,configurable:!0});var g=Object.defineProperty,p=m((n,e)=>g(n,"name",{value:e,configurable:!0}),"u");import{LOOP_STARTUP_MS as x,loopStartedAt as v}from"@lensmcp/protocol-types";export const LOOP_SAMPLE_MS=5e3,LOOP_ROLLUP_MS=3e5,LOOP_SAMPLE_HOURS=6;const O=720,h=p(n=>typeof n=="number"&&Number.isFinite(n)&&n>=0,"finite");export function parseLoopSamples(n){if(!Array.isArray(n))return[];const e=new Map;for(const a of n.slice(-O)){if(!Array.isArray(a)||a.length<5)continue;const[o,s,t,r,i]=a;!h(o)||o<=0||!h(s)||!h(t)||!h(r)||!h(i)||e.set(o,[o,s,t,r,Math.min(1,i)])}return[...e.values()].sort((a,o)=>a[0]-o[0])}m(parseLoopSamples,"parseLoopSamples"),p(parseLoopSamples,"parseLoopSamples");export const rollupOf=p(n=>({at:n[0],n:1,meanSum:n[1],p99:n[2],max:n[3],utilSum:n[4]}),"rollupOf");export function bucketRollups(n,e){const a=Math.max(1,Math.floor(e)),o=new Map;for(const t of n){if(!(t.n>0))continue;const r=Math.floor(t.at/a)*a,i=o.get(r);i?(i.n+=t.n,i.meanSum+=t.meanSum,i.utilSum+=t.utilSum,i.p99=Math.max(i.p99,t.p99),i.max=Math.max(i.max,t.max)):o.set(r,{...t,at:r})}const s=p((t,r)=>Number(t.toFixed(r)),"round");return[...o.values()].sort((t,r)=>t.at-r.at).map(t=>[t.at,s(t.meanSum/t.n,2),s(t.p99,2),s(t.max,2),s(t.utilSum/t.n,4)])}m(bucketRollups,"bucketRollups"),p(bucketRollups,"bucketRollups");const S=[5,10,15,30,60,120,300,600,900,1800,3600,7200,21600,43200,86400].map(n=>n*1e3);export function historyStep(n,e){const a=Math.max(e.minMs,e.requestedMs??0,n/(e.maxPoints??400));return S.find(o=>o>=a&&o%e.minMs===0)??S[S.length-1]}m(historyStep,"historyStep"),p(historyStep,"historyStep");export function historySource(n,e,a=LOOP_SAMPLE_HOURS){return n>=e-a*36e5-6e4?"samples":"rollups"}m(historySource,"historySource"),p(historySource,"historySource");export function bucketStalls(n,e){const a=Math.max(1,Math.floor(e)),o=new Map;for(const s of n){const t=Math.floor(s.startAt/a)*a,r=v(s.loopId),i=r!==void 0&&s.startAt<r+x,l=`${s.loopId}\0${t}\0${i}`,u=o.get(l);u?(u.count++,u.longestMs=Math.max(u.longestMs,s.durationMs),u.unresponsive||=s.unresponsive):o.set(l,{loopId:s.loopId,project:s.project,runtime:s.runtime,at:t,count:1,longestMs:s.durationMs,unresponsive:s.unresponsive,...i?{startup:!0}:{}})}return[...o.values()].sort((s,t)=>s.at-t.at||s.loopId.localeCompare(t.loopId)||+!!s.startup-+!!t.startup)}m(bucketStalls,"bucketStalls"),p(bucketStalls,"bucketStalls");export class LoopSampleRing{static{m(this,"LoopSampleRing")}static{p(this,"LoopSampleRing")}constructor(e=36e5,a=4e4){this.keepMs=e,this.maxSamples=a,this.rings=new Map,this.total=0,this.touches=0}add(e,a,o=Date.now()){const s=o-this.keepMs;let t=this.rings.get(e.id),r=0;for(const i of a){if(i[0]<s)continue;t||this.rings.set(e.id,t={loop:e,samples:[],touched:0});const l=t.samples,u=l[l.length-1];if(!u||i[0]>u[0])l.push(i);else{let c=0,f=l.length;for(;c<f;){const M=c+f>>1;l[M][0]<i[0]?c=M+1:f=M}if(l[c]?.[0]===i[0])continue;l.splice(c,0,i)}r++}return t&&r&&(t.loop=e,t.touched=++this.touches,this.total+=r),this.trim(s),r}read(e,a,o=Date.now()){this.trim(o-this.keepMs);const s=[];for(const t of this.rings.values()){const r=t.samples.filter(i=>i[0]>=e&&i[0]<a);r.length&&s.push({loop:t.loop,samples:r})}return s}get size(){return this.total}trim(e){for(const[a,o]of this.rings){let s=0;for(;s<o.samples.length&&o.samples[s][0]<e;)s++;s&&(o.samples.splice(0,s),this.total-=s),o.samples.length||this.rings.delete(a)}if(!(this.total<=this.maxSamples))for(const a of[...this.rings.values()].sort((o,s)=>o.touched-s.touched)){if(this.total<=this.maxSamples)break;this.total-=a.samples.length,this.rings.delete(a.loop.id)}}}
|
package/lib/event-loop.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var M=Object.defineProperty;var u=(n,e)=>M(n,"name",{value:e,configurable:!0});var k=Object.defineProperty,l=u((n,e)=>k(n,"name",{value:e,configurable:!0}),"u");import{LoopSampleRing as S,parseLoopSamples as L}from"./event-loop-history.js";const v=l(n=>n&&typeof n=="object"&&!Array.isArray(n)?n:{},"object"),o=l(n=>typeof n=="number"&&Number.isFinite(n)&&n>=0?n:void 0,"number"),s=l((n,e=250)=>typeof n=="string"&&n?n.slice(0,e):void 0,"text"),T=40;export function eventLoopIdentity(n){const e=v(n),t=s(e.id),i=s(e.project);if(!(!t||!i||e.runtime!=="node"&&e.runtime!=="browser"))return{id:t,project:i,runtime:e.runtime,...o(e.pid)!==void 0?{pid:o(e.pid)}:{},...o(e.threadId)!==void 0?{threadId:o(e.threadId)}:{},...s(e.tabId)?{tabId:s(e.tabId)}:{},...s(e.loadId)?{loadId:s(e.loadId)}:{}}}u(eventLoopIdentity,"eventLoopIdentity"),l(eventLoopIdentity,"eventLoopIdentity");export function eventLoopIncident(n){const e=v(n.raw);if(e.kind!=="event-loop-stall")return;const t=eventLoopIdentity(e.loop),i=o(e.startedAt),d=o(e.durationMs),r=e.mechanism;if(!t||i===void 0||d===void 0||!["callback","heartbeat","long-animation-frame","longtask"].includes(String(r)))return;const h=e.state==="unresponsive"?"unresponsive":e.state==="unknown"?"unknown":"recovered",p=[];for(const g of Array.isArray(e.scripts)?e.scripts.slice(0,8):[]){const c=v(g),f=s(c.url,512);f&&p.push({url:f,durationMs:o(c.durationMs)??0,startAt:o(c.startAt)??i,...s(c.functionName)?{functionName:s(c.functionName)}:{},...s(c.invoker)?{invoker:s(c.invoker)}:{}})}const m=r==="callback"?s(n.context?.flowId):void 0;return{id:s(e.incidentId)??n.id,loop:t,startAt:i,durationMs:d,observedAt:n.timestamp,...h==="recovered"?{endAt:i+d}:{},state:h,mechanism:r,severity:d>=1e3?"error":"warning",...m?{flowId:m}:{},...s(n.context?.requestId)?{requestId:s(n.context?.requestId)}:{},...s(e.callbackType)?{callbackType:s(e.callbackType)}:{},scripts:p,evidence:[],evidenceTruncated:e.scriptsTruncated===!0||(o(e.droppedIncidents)??0)>0||(o(e.floodSuppressed)??0)>0,explanation:r==="callback"?"Synchronous callback wall time, including any GC or process descheduling. Execution context identifies the flow; overlapping operations are not proven causes.":r==="heartbeat"?"The loop missed its heartbeat. The observer cannot identify executing code; overlapping work may have been delayed.":"Browser-measured main-thread work. Script timings identify contributing scripts where supported; overlapping flows are associated by time only."}}u(eventLoopIncident,"eventLoopIncident"),l(eventLoopIncident,"eventLoopIncident");export class EventLoopIndex{static{u(this,"EventLoopIndex")}static{l(this,"EventLoopIndex")}constructor(e=()=>{},t=()=>{}){this.onChange=e,this.onSamples=t,this.incidents=new Map,this.loops=new Map,this.recent=[],this.history=new S}ingest(e){const t=v(e.raw),i=s(t.kind)??e.source,d=eventLoopIdentity(t.loop);if(d){const a=this.loops.get(d.id);if(!a||e.timestamp>=a.lastSeen)for(this.loops.set(d.id,{loop:d,lastSeen:e.timestamp,state:t.state==="unresponsive"?"unresponsive":t.state==="unknown"?"unknown":"observed",coverage:s(t.coverage,500)??a?.coverage??(d.runtime==="node"?"Instrumented Node thread":"Browser main thread; supported PerformanceObserver entries"),...a?{meanMs:a.meanMs,maxMs:a.maxMs,p99Ms:a.p99Ms,utilization:a.utilization}:{},...o(t.lagMs)!==void 0?{meanMs:o(t.lagMs),maxMs:o(t.maxMs),p99Ms:o(t.p99Ms),utilization:o(t.utilization)}:{}});this.loops.size>200;)this.loops.delete(this.loops.keys().next().value);if(i==="event-loop-lag"){const I=L(t.samples);I.length&&(this.history.add(d,I),this.onSamples(d,I))}}const r=eventLoopIncident(e);if(r){const a=this.incidents.get(r.id);if(a&&a.observedAt>r.observedAt)return;a&&(r.evidence=a.evidence.slice(),r.evidenceTruncated||=a.evidenceTruncated);for(const I of this.recent)this.attach(r,I);for(this.incidents.set(r.id,r);this.incidents.size>200;)this.incidents.delete(this.incidents.keys().next().value);this.onChange(r);return}if(i.startsWith("event-loop-"))return;const h=s(t.loopId);if(!h)return;const p=v(t.span),m=v(t.request),g=v(t.render),c=o(t.durationMs)??o(p.durationMs)??o(m.durationMs)??o(g.actualDurationMs)??0,f=o(t.startedAt)??o(p.startTime)??o(m.startedAt)??e.timestamp-c,w=o(p.syncDurationMs),b=o(p.syncStartedAt),y=w!==void 0&&w>=50&&b!==void 0,x={loopId:h,eventId:e.id,title:(e.title??i).slice(0,250),kind:i,startAt:y?b:f,endAt:y?b+w:f+c,...e.context?.flowId?{flowId:e.context.flowId}:{},...e.context?.requestId?{requestId:e.context.requestId}:{},relation:y?"executing":"overlap"};this.recent.push(x),this.recent.length>2e3&&this.recent.splice(0,this.recent.length-2e3);for(const a of this.incidents.values())this.attach(a,x)&&this.onChange(a)}attach(e,t){if(t.loopId!==e.loop.id||t.endAt<e.startAt||t.startAt>(e.endAt??e.observedAt)||e.evidence.some(r=>r.eventId===t.eventId))return!1;if(e.evidence.length>=T){const r=!e.evidenceTruncated;return e.evidenceTruncated=!0,r}const{loopId:i,...d}=t;return e.evidence.push(d),!0}statuses(e=Date.now()){return[...this.loops.values()].map(t=>({...t,state:e-t.lastSeen>9e4?"unknown":t.state})).sort((t,i)=>i.lastSeen-t.lastSeen)}list(e){return[...this.incidents.values()].filter(t=>!e||t.flowId===e||t.evidence.some(i=>i.flowId===e)).sort((t,i)=>i.startAt-t.startAt)}}let A=l(()=>[],"readIncidents");export function setFlowLoopIncidents(n){A=n}u(setFlowLoopIncidents,"setFlowLoopIncidents"),l(setFlowLoopIncidents,"setFlowLoopIncidents");export function flowLoopIncidents(n){return A(n)}u(flowLoopIncidents,"flowLoopIncidents"),l(flowLoopIncidents,"flowLoopIncidents");
|
package/lib/event-redactor.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var u=Object.defineProperty;var o=(r,t)=>u(r,"name",{value:t,configurable:!0});var l=Object.defineProperty,i=o((r,t)=>l(r,"name",{value:t,configurable:!0}),"a");import{MAX_MESSAGE_CHARS as S,MAX_TITLE_CHARS as T,redactDeep as y,redactSqlParams as _,redactText as p,safeText as E,safeTitle as g}from"./redact.js";export const BUS_REDACT_MAX_DEPTH=12;export function createEventRedactor(r={}){const t=r.maxDepth??BUS_REDACT_MAX_DEPTH;let s=0,a=0;return{redact(e){s+=1;const c=e.title===void 0?void 0:g(p(e.title)),n=e.message===void 0?void 0:E(p(e.message),S),d=e.raw===void 0?void 0:v(y(e.raw,0,t)),A=c!==e.title,f=n!==e.message,m=d!==e.raw;return!A&&!f&&!m?e:(a+=1,{...e,...c!==void 0?{title:c}:{},...n!==void 0?{message:n}:{},...d!==void 0?{raw:d}:{}})},stats:i(()=>({inspected:s,redacted:a}),"stats"),reset:i(()=>{s=0,a=0},"reset")}}o(createEventRedactor,"createEventRedactor"),i(createEventRedactor,"createEventRedactor");function v(r){if(!r||typeof r!="object"||Array.isArray(r))return r;const t=r,s=t.query,a=t.params;if(typeof s!="string"||!Array.isArray(a))return r;const e=_(s,a);return e===a?r:{...t,params:e}}o(v,"T"),i(v,"redactSqlShape");export{T as MAX_TITLE_CHARS};
|
package/lib/fingerprint.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var a=Object.defineProperty;var r=(n,e)=>a(n,"name",{value:e,configurable:!0});var f=Object.defineProperty,i=r((n,e)=>f(n,"name",{value:e,configurable:!0}),"e");const l=2166136261,u=16777619;function o(n){let e=2166136261;for(let t=0;t<n.length;t++)e^=n.charCodeAt(t),e=Math.imul(e,16777619);return(e>>>0).toString(16).padStart(8,"0")}r(o,"i"),i(o,"fnv1a32");export function fingerprint(n){const e=[n.kind,n.identity,n.file??"",n.detail??""];return`${n.kind}:${o(e.join("|"))}`}r(fingerprint,"fingerprint"),i(fingerprint,"fingerprint");
|
package/lib/flow-categories.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var M=Object.defineProperty;var c=(e,t)=>M(e,"name",{value:t,configurable:!0});var T=Object.defineProperty,s=c((e,t)=>T(e,"name",{value:t,configurable:!0}),"s");export const CATEGORY_ORDER=["user-action","page-load","navigation","api","ai","background","test","system","other"];const p=new Map;export function globMatch(e,t){let o=p.get(e);return o||(o=new RegExp(`^${e.replace(/[.+^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*").replace(/\?/g,".")}$`),p.size>500&&p.clear(),p.set(e,o)),o.test(t)}c(globMatch,"globMatch"),s(globMatch,"globMatch");const F=s(e=>/^[A-Z]+ /.test(e),"hasMethod");function w(e,t){return t.some(o=>o.route?F(e)?globMatch(e,`${o.method??"GET"} ${o.route}`):globMatch(e,o.route):!1)}c(w,"$"),s(w,"routeMatches");function m(e,t){return!([e.flowId,e.originType,e.origin,e.route,e.project].filter(o=>o!==void 0).length===0||e.flowId!==void 0&&!globMatch(e.flowId,t.flowId)||e.originType!==void 0&&e.originType!==t.origin.type||e.origin!==void 0&&!(t.origin.nodeId!==void 0&&globMatch(e.origin,t.origin.nodeId))||e.route!==void 0&&!w(e.route,t.segments)||e.project!==void 0&&!t.segments.some(o=>o.projects.some(n=>globMatch(e.project,n))))}c(m,"b"),s(m,"ruleMatches");const y=s(e=>typeof e=="object"&&e!==null&&!Array.isArray(e),"isObj"),g=s(e=>typeof e=="string"&&e!==""?e:void 0,"str"),f=s(e=>y(e.raw)?g(e.raw.kind)??"":"","kindOf"),k=s((e,t)=>{const o=Number(e.timestamp)||0,n=Number(t.timestamp)||0;return o<n||o===n&&e.id<t.id},"earlier");function $(e){let t,o;for(const a of e)f(a)==="flow-origin"&&(!t||k(a,t))&&(t=a),g(a.context?.originType)&&(!o||k(a,o))&&(o=a);const n=t??o;if(!n)return{};const r=y(n.raw)?n.raw:{},i=g(n.context?.originType)??(t?g(n.title):void 0),u=g(r.originNodeId)??g(n.context?.originNodeId);return{...i?{type:i}:{},...u?{nodeId:u}:{}}}c($,"O"),s($,"originOf");const S=/^(verify|e2e|test|smoke|load)[-_:]/i,z=s((e,t)=>S.test(e)||t!==void 0&&/^(ui:)?(tools|scripts?|e2e)\//.test(t),"isTestFlow"),x={"page-load":"page-load","user-click":"user-action","form-submit":"user-action","input-change":"user-action","double-click":"user-action","context-menu":"user-action","key-press":"user-action","route-load":"navigation","route-change":"navigation",navigation:"navigation","ai-turn":"ai",request:"api",timer:"background",cron:"background",interval:"background",queue:"background",job:"background","websocket-message":"background","hmr-update":"system"},A=new Set(["page-load","user-action","navigation"]),E=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;export function templatePath(e){return e.replace(/[?#].*$/,"").split("/").map(t=>E.test(t)||/^[0-9a-f]{16,}$/i.test(t)||/^\d{2,}$/.test(t)?":id":t).join("/")}c(templatePath,"templatePath"),s(templatePath,"templatePath");function I(e){const t=/^ui:(?:.*\/)?([^/:]+?)(?:\.[a-z]+)?:\d+:([A-Za-z]+)$/.exec(e);return t?`${t[1]} \xB7 ${t[2]}`:e}c(I,"q"),s(I,"componentOf");export function scriptFamily(e){let t=e.replace(/[-_:][0-9a-f]{6,}(?:-[0-9a-f]+)*$/i,"");return t=t.replace(/[-_:](?=[a-z0-9]*\d)[a-z0-9]{6,}$/i,""),t=t.replace(/[-_:]\d+$/,""),t||e}c(scriptFamily,"scriptFamily"),s(scriptFamily,"scriptFamily");function j(e,t,o,n){const r=t.nodeId,i=r?/^(page|route|ambient):(.*)$/.exec(r):null;if(i)return`${i[1]}:${templatePath(i[2])}`;if(t.type&&x[t.type]==="user-action"&&r)return I(r);if(e==="test")return scriptFamily(o.flowId);const u=o.segments.find(l=>l.kind==="request"&&l.route);if(u?.route)return`${u.method??"GET"} ${templatePath(u.route)}`;const a=n.find(l=>f(l)==="ai-call");return a&&y(a.raw)?`ai \xB7 ${g(a.raw.model)??g(a.raw.provider)??"call"}`:r?templatePath(r):void 0}c(j,"R"),s(j,"subjectOf");const _=10,B=1e3,N=3,R=500,C=1e3;function q(e,t){const o=new Set,n=t.segments.filter(i=>i.kind==="request");t.segments.some(i=>i.counts.errors>0)&&o.add("has-error"),n.some(i=>i.durationMs>=B)&&o.add("slow"),n.length>=_&&o.add("fan-out"),new Set(t.segments.flatMap(i=>i.projects)).size>=3&&o.add("multi-service"),e.some(i=>f(i)==="queue-enqueued")&&o.add("queue"),e.some(i=>f(i)==="ai-call"||i.source==="ai")&&o.add("ai"),t.stats.bookkeeping>=R&&t.stats.bookkeeping*2>=t.stats.events&&o.add("storm"),t.stats.events>=C&&o.add("large");const r=new Map;for(const i of n)(!i.part||i.part.index===1)&&r.set(i.label,(r.get(i.label)??0)+1);return[...r.values()].some(i=>i>=N)&&o.add("repeated-requests"),[...o].sort()}c(q,"G"),s(q,"tagsOf");export function classifyFlow(e,t,o=[]){const n=$(e),r={flowId:t.flowId,origin:n,segments:t.segments},i=q(e,t),u=s((d,O,b)=>{const h=j(d,n,t,e);return{category:d,decidedBy:O,...b!==void 0?{ruleIndex:b}:{},tags:i,origin:n,...h?{subject:h}:{}}},"done"),a=o.findIndex(d=>d.category&&m(d,r));if(a!==-1)return u(o[a].category,"rule",a);if(z(t.flowId,n.nodeId))return u("test","test");const l=n.type?x[n.type]:void 0;if(l)return u(l,"origin");if(i.includes("ai"))return u("ai","shape");const v=t.segments.filter(d=>d.kind==="request");return v.length>0?u(v.every(d=>d.requestId?.startsWith("job:"))?"background":"api","shape"):t.segments.length>0&&t.segments.every(d=>d.kind==="ambient")?u("system","shape"):u("other","shape")}c(classifyFlow,"classifyFlow"),s(classifyFlow,"classifyFlow");export function categoryOfSegment(e,t,o=[]){const n=o.find(r=>r.category&&(r.route!==void 0||r.project!==void 0)&&r.flowId===void 0&&r.origin===void 0&&r.originType===void 0&&m(r,{flowId:e.flowId,origin:t.origin,segments:[e]}));return n?n.category:e.kind==="request"?e.requestId?.startsWith("job:")?"background":"api":e.kind==="ambient"?"system":e.kind==="origin"&&A.has(t.category)?t.category:"ui"}c(categoryOfSegment,"categoryOfSegment"),s(categoryOfSegment,"categoryOfSegment");export function groupFlows(e){return groupByCategory(e).map(t=>{const o=new Map;for(const r of t.items){const i=o.get(r.subject);i?i.push(r):o.set(r.subject,[r])}const n=[...o.entries()].sort((r,i)=>(r[0]===void 0?1:0)-(i[0]===void 0?1:0)||i[1].length-r[1].length||String(r[0]).localeCompare(String(i[0]))).map(([r,i])=>({subject:r,count:i.length,items:i}));return{...t,subjects:n}})}c(groupFlows,"groupFlows"),s(groupFlows,"groupFlows");export function groupByCategory(e){const t=new Map;for(const n of e){const r=t.get(n.category);r?r.push(n):t.set(n.category,[n])}const o=s(n=>{const r=CATEGORY_ORDER.indexOf(n);return r===-1?CATEGORY_ORDER.length:r},"rank");return[...t.entries()].sort((n,r)=>o(n[0])-o(r[0])||(n[0]<r[0]?-1:n[0]>r[0]?1:0)).map(([n,r])=>({category:n,count:r.length,items:r}))}c(groupByCategory,"groupByCategory"),s(groupByCategory,"groupByCategory");
|
package/lib/flow-segments.d.ts
CHANGED
|
@@ -107,6 +107,11 @@ export interface FlowSegment {
|
|
|
107
107
|
hot?: string[];
|
|
108
108
|
/** Set when a request or burst was cut by the size cap. */
|
|
109
109
|
part?: SegmentPart;
|
|
110
|
+
/**
|
|
111
|
+
* Event-loop stalls stamped with this request that began after its response had finished (foodguard-192): in
|
|
112
|
+
* `eventIds`, but not in `start`/`end`/`durationMs` — they delayed nothing the request did.
|
|
113
|
+
*/
|
|
114
|
+
stallsAfterResponse?: number;
|
|
110
115
|
worstSeverity: 'debug' | 'info' | 'warning' | 'error' | 'fatal';
|
|
111
116
|
/**
|
|
112
117
|
* Whether this request's SQL on the bus is all of it — on a unit that ran SQL, and on the first
|
|
@@ -137,6 +142,12 @@ export interface FlowSegmentation {
|
|
|
137
142
|
/** The flow's SQL completeness boundary — absent when no request in it ran SQL. */
|
|
138
143
|
sql?: FlowSqlAccounting;
|
|
139
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* {@link lateStallsOf} over a flow's events, request by request: event id → ms after its request's response. Every
|
|
147
|
+
* call of one request id is read as one request (a script that reuses its id), so a stall between two of them is not
|
|
148
|
+
* after the response. Empty when the flow holds no stall.
|
|
149
|
+
*/
|
|
150
|
+
export declare function stallsAfterResponse(events: readonly BaseEvent[]): Map<string, number>;
|
|
140
151
|
export declare function segmentFlow(input: readonly BaseEvent[], options?: SegmentOptions): FlowSegmentation;
|
|
141
152
|
/** Whether the SQL the bus holds for a request (or a flow) is all of it. */
|
|
142
153
|
export type SqlCompleteness = 'complete' | 'incomplete' | 'unverified';
|
package/lib/flow-segments.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Z=Object.defineProperty;var b=(e,t)=>Z(e,"name",{value:t,configurable:!0});var ee=Object.defineProperty,I=b((e,t)=>ee(e,"name",{value:t,configurable:!0}),"b"),te=Object.defineProperty,m=I((e,t)=>te(e,"name",{value:t,configurable:!0}),"h");export const DEFAULT_SEGMENT_OPTIONS={phaseGapMs:1500,burstGapMs:250,attachWindowMs:400,maxSegmentEvents:400,occurrenceSlackMs:10},FLOW_BOOKKEEPING_KINDS=new Set(["subscription-added","subscription-removed","hook-run","store-created","singleton-instance","visual-frame"]),SQL_ACCOUNT_KIND="db-request-account";const ne=new Set(["react","valtio","client-runtime","chrome","visual"]),S=m(e=>typeof e=="object"&&e!==null&&!Array.isArray(e),"isObj"),v=m(e=>typeof e=="string"&&e!==""?e:void 0,"str"),$=m(e=>typeof e=="number"&&Number.isFinite(e)?e:void 0,"num"),H=new WeakMap;function E(e){let t=H.get(e);return t||(t=W(e),H.set(e,t)),t}b(E,"F"),I(E,"U"),m(E,"itemOf");function W(e){const t=S(e.raw)?e.raw:{},s=S(t.request)?t.request:{},o=S(t.span)?t.span:{},i=e.context??{},n=v(t.kind)??"",l=n===SQL_ACCOUNT_KIND,g=(l?$(t.lastEndedAt):void 0)??$(e.timestamp)??0,q=$(s.startedAt)??$(o.startTime)??$(t.startedAt)??(l?$(t.firstStartedAt):void 0),M=$(s.endedAt)??$(o.endTime)??(q!==void 0&&$(t.durationMs)!==void 0?q+t.durationMs:void 0),w=Math.min(g,q??g),j=Math.max(g,M??g),a=S(t.render)?t.render:{},u=S(t.update)?t.update:{},y=n==="render"?v(a.componentName):n==="state-update"?v(u.path)?.split(".").slice(0,2).join("."):void 0,d=v(t.queue),c=v(t.jobId),f=String(e.source),r=v(t.project)??v(i.projectName),p=n==="fetch-start"||n==="fetch-done"||n==="gateway-request";return{event:e,id:e.id,ts:g,from:w,to:j,kind:n,source:f,requestId:v(i.requestId),parentRequestId:v(i.parentRequestId),queueKey:d&&c?`${d}#${c}`:void 0,browser:ne.has(f)||f==="memory"&&r===void 0,bookkeeping:FLOW_BOOKKEEPING_KINDS.has(n),project:r,subject:y,route:p?F(v(t.url)):n==="server-request"?v(s.route):void 0,method:p?v(t.method):n==="server-request"?v(s.method):void 0}}b(W,"G"),I(W,"L"),m(W,"readItem");function F(e){if(!e)return;const t=e.search(/[?#]/),s=t===-1?e:e.slice(0,t),o=/^[a-z][a-z0-9+.-]*:\/\/[^/]*(\/.*)?$/i.exec(s);return o?o[1]??"/":s}b(F,"T"),I(F,"B"),m(F,"pathOnly");const se=m(e=>e.split(".")[0],"rootOf"),J=m((e,t)=>e.reduce((s,o)=>Math.min(s,t(o)),1/0),"minOf"),V=m((e,t)=>e.reduce((s,o)=>Math.max(s,t(o)),-1/0),"maxOf"),T=m((e,t)=>e.ts-t.ts||(e.id<t.id?-1:e.id>t.id?1:0),"compare"),G=m(e=>e.requestId&&!e.requestId.startsWith("job:")?se(e.requestId):void 0,"directFamilyOf");function B(e){if(e.requestId?.startsWith("job:"))return e.requestId.slice(4);if(e.queueKey&&e.kind!=="queue-enqueued"&&!e.requestId)return e.queueKey}b(B,"U"),I(B,"D"),m(B,"jobKeyOf");function U(e,t){const s=e.filter(n=>!n.parentRequestId&&(n.kind==="server-request"||n.kind==="gateway-request"||n.kind==="fetch-start"||n.kind==="fetch-done")).sort((n,l)=>n.from-l.from||T(n,l));if(s.length<2)return[e];const o=[];for(const n of s){const l=o[o.length-1];l&&n.from<=l.to+t?l.to=Math.max(l.to,n.to):o.push({from:n.from,to:n.to})}if(o.length<2)return[e];const i=o.map(()=>[]);for(const n of e)i[t>=0?D(o,n.ts,t):_(o,n.ts,t)].push(n);return i.filter(n=>n.length>0)}b(U,"D"),I(U,"z"),m(U,"occurrencesOf");function _(e,t,s){const o=e.findIndex(n=>t>=n.from-s&&t<=n.to+s);if(o!==-1)return o;let i=0;for(let n=0;n<e.length;n+=1)e[n].from<=t&&(i=n);return i}b(_,"R"),I(_,"P"),m(_,"spanScan");function D(e,t,s){let o=0,i=e.length;for(;o<i;){const n=o+i>>1;e[n].to+s<t?o=n+1:i=n}if(o<e.length&&t>=e[o].from-s)return o;for(o=0,i=e.length;o<i;){const n=o+i>>1;e[n].from<=t?o=n+1:i=n}return Math.max(0,o-1)}b(D,"z"),I(D,"Q"),m(D,"spanAt");const X={debug:0,info:1,warning:2,error:3,fatal:4};function A(e,t){const s=[];let o=[];for(const i of e){const n=o[o.length-1];n&&i.ts-n.ts>=t&&(s.push(o),o=[]),o.push(i)}return o.length&&s.push(o),s}b(A,"A"),I(A,"R"),m(A,"runs");export function segmentFlow(e,t={}){const s={...DEFAULT_SEGMENT_OPTIONS};for(const[r,p]of Object.entries(t))typeof p=="number"&&Number.isFinite(p)&&(s[r]=p);const o=new Set,i=[];for(const r of e)o.has(r.id)||(o.add(r.id),i.push(E(r)));i.sort(T);const n=v(i[0]?.event.context?.flowId)??v(i[0]?.event.raw?.flowId)??"",l=new Map;for(const r of i)r.kind==="queue-enqueued"&&r.queueKey&&r.requestId&&!r.requestId.startsWith("job:")&&!l.has(r.queueKey)&&l.set(r.queueKey,r);const g=new Map,q=new Map,M=[];for(const r of i){const p=B(r),h=G(r);if(p!==void 0){const k=q.get(p);k?k.push(r):q.set(p,[r])}else if(h!==void 0){const k=g.get(h);k?k.push(r):g.set(h,[r])}else M.push(r)}const w=[],j=new Map;for(const[r,p]of g)U(p,s.occurrenceSlackMs).forEach((h,k)=>{const O={id:`${n}~r:${r}${k===0?"":`@${k+1}`}`,kind:"request",items:h,requestId:r};if(w.push(O),l.size)for(const x of h)x.kind==="queue-enqueued"&&j.set(x,O)});for(const[r,p]of q){const h=l.get(r),k=h?j.get(h):void 0;k?k.items.push(...p):w.push({id:`${n}~r:job:${r}`,kind:"request",items:p,requestId:`job:${r}`})}const a=i.filter(r=>r.kind==="fetch-done"&&G(r)!==void 0),u=new Map;for(const r of w)r.requestId&&u.set(r.requestId,[...u.get(r.requestId)??[],r]);const y=m((r,p)=>{const h=u.get(r);if(h)return h.find(k=>k.items.some(O=>O.kind==="fetch-done"&&O.ts===p))??h[h.length-1]},"owningUnit"),d=M.filter(r=>r.browser);for(const r of A(d,s.burstGapMs)){const p=r[0].ts,h=a.filter(x=>x.ts<=p&&p-x.ts<=s.attachWindowMs),k=[...new Set(h.map(x=>G(x)))];if(k.length===1){const x=y(k[0],h[h.length-1].ts);if(x){x.items.push(...r);continue}}const O=r.some(x=>x.kind==="flow-origin");w.push({id:`${n}~${O?"o":"u"}:${r[0].id}`,kind:O?"origin":"ui",items:r,...k.length>1?{after:k.sort()}:{}})}for(const r of A(M.filter(p=>!p.browser),s.phaseGapMs))w.push({id:`${n}~a:${r[0].id}`,kind:"ambient",items:r});const c=P(n,i.length,w,s),f=K(i);return f?{...c,sql:f}:c}b(segmentFlow,"segmentFlow"),I(segmentFlow,"segmentFlow"),m(segmentFlow,"segmentFlow");function P(e,t,s,o){for(const a of s)a.items.sort(T);const i=s.map(a=>({unit:a,from:J(a.items,u=>u.from),to:V(a.items,u=>u.to)}));i.sort((a,u)=>a.from-u.from||(a.unit.id<u.unit.id?-1:1));const n=new Map;let l=0,g=-1/0;for(const a of i)(l===0||a.from-g>=o.phaseGapMs)&&(l+=1),g=Math.max(g,a.to),n.set(a.unit,l);const q=[];for(const a of i){const u=R(a.unit.items,o.maxSegmentEvents),y=a.unit.kind==="request"?K(a.unit.items):void 0;u.forEach((d,c)=>{const f=z(e,a.unit,d,n.get(a.unit),u.length>1?{index:c+1,total:u.length}:void 0);q.push(y&&c===0?{...f,sql:y}:f)})}const M={};for(const a of q)for(const u of a.eventIds)M[u]=a.id;const w=[];for(const a of q){let u=w[a.phase-1];u||(u={index:a.phase,start:a.start,end:a.end,segmentIds:[]},w[a.phase-1]=u),u.start=Math.min(u.start,a.start),u.end=Math.max(u.end,a.end),u.segmentIds.push(a.id)}const j=q.reduce((a,u)=>a+u.counts.signal,0);return{flowId:e,segments:q,phases:w.filter(Boolean),assignment:M,stats:{events:t,signal:j,bookkeeping:t-j,segments:q.length,phases:w.filter(Boolean).length,largestSegment:q.reduce((a,u)=>Math.max(a,u.counts.total),0)}}}b(P,"L"),I(P,"V"),m(P,"assemble");function R(e,t){if(e.filter(n=>!n.bookkeeping).length<=t)return[e];const s=[];let o=[],i=0;for(const n of e)!n.bookkeeping&&i===t&&(s.push(o),o=[],i=0),o.push(n),n.bookkeeping||(i+=1);return o.length&&s.push(o),s}b(R,"P"),I(R,"X"),m(R,"chunk");function z(e,t,s,o,i){const n={total:s.length,signal:0,bookkeeping:0,errors:0,warnings:0},l=new Set;let g="debug";for(const u of s){u.bookkeeping?n.bookkeeping+=1:n.signal+=1;const y=String(u.event.severity);y==="error"||y==="fatal"?n.errors+=1:y==="warning"&&(n.warnings+=1),(X[y]??1)>(X[g]??0)&&(g=y),u.project&&l.add(u.project)}const q=t.kind==="ui"||t.kind==="origin"?C(s):[],M=J(s,u=>u.from),w=V(s,u=>u.to),j=L(t.items),a=t.id;return{id:i&&i.index>1?`${a}#${i.index}`:a,flowId:e,kind:t.kind,label:Q(t,s,j,i,q),phase:o,start:M,end:w,durationMs:w-M,eventIds:s.map(u=>u.id),counts:n,projects:[...l].sort(),...j.route?{route:j.route}:{},...j.method?{method:j.method}:{},...t.requestId?{requestId:t.requestId}:{},...t.after?{after:t.after}:{},...q.length?{hot:q}:{},...i?{part:i}:{},worstSeverity:g}}b(z,"Q"),I(z,"Y"),m(z,"toSegment");function L(e){const t=e.find(s=>s.kind==="fetch-start"&&s.route)??e.find(s=>s.kind==="server-request"&&!s.parentRequestId&&s.route)??e.find(s=>s.kind==="gateway-request"&&s.route)??e.find(s=>s.kind==="fetch-done"&&s.route);return{...t?.route?{route:t.route}:{},...t?.method?{method:t.method.toUpperCase()}:{}}}b(L,"H"),I(L,"H"),m(L,"routeOf");function C(e){const t=new Map;for(const s of e)s.subject&&!s.bookkeeping&&t.set(s.subject,(t.get(s.subject)??0)+1);return[...t.entries()].sort((s,o)=>o[1]-s[1]||(s[0]<o[0]?-1:1)).slice(0,3).map(([s,o])=>o>1?`${s} \xD7${o}`:s)}b(C,"J"),I(C,"J"),m(C,"hotOf");function Q(e,t,s,o,i){const n=o?` (part ${o.index}/${o.total})`:"";if(e.kind==="request"){if(s.route)return`${s.method?`${s.method} `:""}${s.route}${n}`;const l=t.find(g=>g.queueKey);return l?`job ${l.queueKey}${n}`:`request ${e.requestId??""}${n}`}return e.kind==="origin"?`${t.find(l=>l.kind==="flow-origin")?.event.title||"start"}${i.length?` \xB7 ${i.join(", ")}`:""}${n}`:e.kind==="ui"?`UI update${i.length?` \xB7 ${i.join(", ")}`:""}${n}`:`background${n}`}b(Q,"V"),I(Q,"Z"),m(Q,"labelOf");const Y=50;export function sqlAccountingOf(e){return N(e,!1)}b(sqlAccountingOf,"sqlAccountingOf"),I(sqlAccountingOf,"sqlAccountingOf"),m(sqlAccountingOf,"sqlAccountingOf");function K(e){return N(e.map(t=>t.event),!0)}b(K,"N"),I(K,"T"),m(K,"accountSqlOfItems");function N(e,t){const s=new Map,o=new Map,i=t?void 0:new Set;for(const d of e){if(i){if(i.has(d.id))continue;i.add(d.id)}const c=S(d.raw)?d.raw:{},f=v(c.kind),r=v(d.context?.requestId);if(!r)continue;const p=v(c.project)??v(d.context?.projectName);if(f==="db-query"&&d.source==="db"){const h=o.get(r)??{count:0,workMs:0,...p?{project:p}:{}};h.count+=1,h.workMs+=$(c.durationMs)??0,o.set(r,h)}else if(f===SQL_ACCOUNT_KIND){const h=v(c.accountId)??d.id,k=$(c.seq)??0,O=s.get(h);if(O&&O.seq>=k)continue;s.set(h,{requestId:r,...p?{project:p}:{},seq:k,basis:c.basis==="end"?"end":"settled",statements:$(c.statements)??0,failed:$(c.failed)??0,workMs:$(c.workMs)??0,recorded:$(c.recorded)??0,suppressed:$(c.suppressed)??0,unwritten:$(c.unwritten)??0})}}const n=new Map;for(const d of s.values()){const c=n.get(d.requestId);if(!c){n.set(d.requestId,{...d});continue}d.basis==="settled"&&(c.basis="settled"),c.statements+=d.statements,c.failed+=d.failed,c.workMs+=d.workMs,c.recorded+=d.recorded,c.suppressed+=d.suppressed,c.unwritten+=d.unwritten}const l=[...new Set([...o.keys(),...n.keys()])].sort();if(l.length===0)return;const g={statements:0,failed:0,workMs:0,recorded:0,suppressed:0,unwritten:0},q={statements:0,workMs:0},M=[];for(const d of l){const c=o.get(d),f=n.get(d),r=c?.count??0;q.statements+=r,q.workMs+=c?.workMs??0;const p=!f||r>f.statements?"unverified":r===f.statements?"complete":"incomplete";f&&p!=="unverified"&&(g.statements+=f.statements,g.failed+=f.failed,g.workMs+=f.workMs,g.recorded+=f.recorded,g.suppressed+=f.suppressed,g.unwritten+=f.unwritten);const h=c?.project??f?.project;M.push({requestId:d,...h?{project:h}:{},status:p,observed:r,observedWorkMs:c?.workMs??0,...f?{basis:f.basis,statements:f.statements,failed:f.failed,workMs:f.workMs,recorded:f.recorded,suppressed:f.suppressed,unwritten:f.unwritten}:{}})}const w=m(d=>M.filter(c=>c.status===d).length,"tally"),j=w("unverified"),a=w("incomplete"),u=m(d=>(d.statements??0)-d.observed,"missing"),y=M.filter(d=>d.status!=="complete").sort((d,c)=>(d.status==="incomplete"?0:1)-(c.status==="incomplete"?0:1)||u(c)-u(d)||(d.requestId<c.requestId?-1:d.requestId>c.requestId?1:0));return{status:j>0?"unverified":a>0?"incomplete":"complete",requests:M.length,complete:w("complete"),incomplete:a,unverified:j,settled:M.filter(d=>d.status!=="unverified"&&d.basis==="settled").length,observed:q,accounted:g,gaps:y.slice(0,Y),gapsTruncated:y.length>Y}}b(N,"E"),I(N,"W"),m(N,"accountSql");
|
|
1
|
+
"use strict";var te=Object.defineProperty;var w=(e,t)=>te(e,"name",{value:t,configurable:!0});var se=Object.defineProperty,p=w((e,t)=>se(e,"name",{value:t,configurable:!0}),"p");export const DEFAULT_SEGMENT_OPTIONS={phaseGapMs:1500,burstGapMs:250,attachWindowMs:400,maxSegmentEvents:400,occurrenceSlackMs:10},FLOW_BOOKKEEPING_KINDS=new Set(["subscription-added","subscription-removed","hook-run","store-created","singleton-instance","visual-frame"]),SQL_ACCOUNT_KIND="db-request-account";const ne=new Set(["react","valtio","client-runtime","chrome","visual"]),x=p(e=>typeof e=="object"&&e!==null&&!Array.isArray(e),"isObj"),b=p(e=>typeof e=="string"&&e!==""?e:void 0,"str"),M=p(e=>typeof e=="number"&&Number.isFinite(e)?e:void 0,"num"),T=new WeakMap;function A(e){let t=T.get(e);return t||(t=_(e),T.set(e,t)),t}w(A,"K"),p(A,"itemOf");function _(e){const t=x(e.raw)?e.raw:{},s=x(t.request)?t.request:{},o=x(t.span)?t.span:{},r=e.context??{},n=b(t.kind)??"",l=n===SQL_ACCOUNT_KIND,g=(l?M(t.lastEndedAt):void 0)??M(e.timestamp)??0,v=M(s.startedAt)??M(o.startTime)??M(t.startedAt)??(l?M(t.firstStartedAt):void 0),q=M(s.endedAt)??M(o.endTime)??(v!==void 0&&M(t.durationMs)!==void 0?v+t.durationMs:void 0),I=Math.min(g,v??g),$=Math.max(g,q??g),j=x(t.render)?t.render:{},a=x(t.update)?t.update:{},c=n==="render"?b(j.componentName):n==="state-update"?b(a.path)?.split(".").slice(0,2).join("."):void 0,d=b(t.queue),u=b(t.jobId),f=String(e.source),i=b(t.project)??b(r.projectName),m=n==="fetch-start"||n==="fetch-done"||n==="gateway-request";return{event:e,id:e.id,ts:g,from:I,to:$,kind:n,source:f,requestId:b(r.requestId),parentRequestId:b(r.parentRequestId),queueKey:d&&u?`${d}#${u}`:void 0,browser:ne.has(f)||f==="memory"&&i===void 0,bookkeeping:FLOW_BOOKKEEPING_KINDS.has(n),project:i,subject:c,route:m?B(b(t.url)):n==="server-request"?b(s.route):void 0,method:m?b(t.method):n==="server-request"?b(s.method):void 0}}w(_,"C"),p(_,"readItem");function B(e){if(!e)return;const t=e.search(/[?#]/),s=t===-1?e:e.slice(0,t),o=/^[a-z][a-z0-9+.-]*:\/\/[^/]*(\/.*)?$/i.exec(s);return o?o[1]??"/":s}w(B,"P"),p(B,"pathOnly");const oe=p(e=>e.split(".")[0],"rootOf"),U=p((e,t)=>e.reduce((s,o)=>Math.min(s,t(o)),1/0),"minOf"),D=p((e,t)=>e.reduce((s,o)=>Math.max(s,t(o)),-1/0),"maxOf"),K=p((e,t)=>e.ts-t.ts||(e.id<t.id?-1:e.id>t.id?1:0),"compare"),O=p(e=>e.requestId&&!e.requestId.startsWith("job:")?oe(e.requestId):void 0,"directFamilyOf");function z(e){if(e.requestId?.startsWith("job:"))return e.requestId.slice(4);if(e.queueKey&&e.kind!=="queue-enqueued"&&!e.requestId)return e.queueKey}w(z,"Q"),p(z,"jobKeyOf");function C(e,t){const s=e.filter(n=>!n.parentRequestId&&(n.kind==="server-request"||n.kind==="gateway-request"||n.kind==="fetch-start"||n.kind==="fetch-done")).sort((n,l)=>n.from-l.from||K(n,l));if(s.length<2)return[e];const o=[];for(const n of s){const l=o[o.length-1];l&&n.from<=l.to+t?l.to=Math.max(l.to,n.to):o.push({from:n.from,to:n.to})}if(o.length<2)return[e];const r=o.map(()=>[]);for(const n of e)r[t>=0?P(o,n.ts,t):L(o,n.ts,t)].push(n);return r.filter(n=>n.length>0)}w(C,"V"),p(C,"occurrencesOf");function L(e,t,s){const o=e.findIndex(n=>t>=n.from-s&&t<=n.to+s);if(o!==-1)return o;let r=0;for(let n=0;n<e.length;n+=1)e[n].from<=t&&(r=n);return r}w(L,"X"),p(L,"spanScan");function P(e,t,s){let o=0,r=e.length;for(;o<r;){const n=o+r>>1;e[n].to+s<t?o=n+1:r=n}if(o<e.length&&t>=e[o].from-s)return o;for(o=0,r=e.length;o<r;){const n=o+r>>1;e[n].from<=t?o=n+1:r=n}return Math.max(0,o-1)}w(P,"Y"),p(P,"spanAt");const re=new Set(["server-request","gateway-request","fetch-done","fetch-error","queue-process"]),N="event-loop-stall";function E(e){let t=-1/0,s=!1;for(const r of e)re.has(r.kind)?t=Math.max(t,r.to):r.kind===N&&(s=!0);if(!s||!Number.isFinite(t))return;let o;for(const r of e)r.kind===N&&r.from>=t&&(o??=new Map).set(r,Math.round(r.from-t));return o}w(E,"_"),p(E,"lateStallsOf");export function stallsAfterResponse(e){const t=new Map;if(!e.some(o=>x(o.raw)&&o.raw.kind===N))return t;const s=new Map;for(const o of e){const r=A(o),n=r.requestId?.startsWith("job:")?r.requestId:O(r);if(n===void 0)continue;const l=s.get(n);l?l.push(r):s.set(n,[r])}for(const o of s.values())for(const[r,n]of E(o)??[])t.set(r.id,n);return t}w(stallsAfterResponse,"stallsAfterResponse"),p(stallsAfterResponse,"stallsAfterResponse");function W(e,t){if(!t)return e;const s=e.filter(o=>!t.has(o));return s.length?s:e}w(W,"T"),p(W,"timedItems");const Q={debug:0,info:1,warning:2,error:3,fatal:4};function F(e,t){const s=[];let o=[];for(const r of e){const n=o[o.length-1];n&&r.ts-n.ts>=t&&(s.push(o),o=[]),o.push(r)}return o.length&&s.push(o),s}w(F,"F"),p(F,"runs");export function segmentFlow(e,t={}){const s={...DEFAULT_SEGMENT_OPTIONS};for(const[i,m]of Object.entries(t))typeof m=="number"&&Number.isFinite(m)&&(s[i]=m);const o=new Set,r=[];for(const i of e)o.has(i.id)||(o.add(i.id),r.push(A(i)));r.sort(K);const n=b(r[0]?.event.context?.flowId)??b(r[0]?.event.raw?.flowId)??"",l=new Map;for(const i of r)i.kind==="queue-enqueued"&&i.queueKey&&i.requestId&&!i.requestId.startsWith("job:")&&!l.has(i.queueKey)&&l.set(i.queueKey,i);const g=new Map,v=new Map,q=[];for(const i of r){const m=z(i),h=O(i);if(m!==void 0){const k=v.get(m);k?k.push(i):v.set(m,[i])}else if(h!==void 0){const k=g.get(h);k?k.push(i):g.set(h,[i])}else q.push(i)}const I=[],$=new Map;for(const[i,m]of g)C(m,s.occurrenceSlackMs).forEach((h,k)=>{const S={id:`${n}~r:${i}${k===0?"":`@${k+1}`}`,kind:"request",items:h,requestId:i};if(I.push(S),l.size)for(const y of h)y.kind==="queue-enqueued"&&$.set(y,S)});for(const[i,m]of v){const h=l.get(i),k=h?$.get(h):void 0;k?k.items.push(...m):I.push({id:`${n}~r:job:${i}`,kind:"request",items:m,requestId:`job:${i}`})}const j=r.filter(i=>i.kind==="fetch-done"&&O(i)!==void 0),a=new Map;for(const i of I)i.requestId&&a.set(i.requestId,[...a.get(i.requestId)??[],i]);const c=p((i,m)=>{const h=a.get(i);if(h)return h.find(k=>k.items.some(S=>S.kind==="fetch-done"&&S.ts===m))??h[h.length-1]},"owningUnit"),d=q.filter(i=>i.browser);for(const i of F(d,s.burstGapMs)){const m=i[0].ts,h=j.filter(y=>y.ts<=m&&m-y.ts<=s.attachWindowMs),k=[...new Set(h.map(y=>O(y)))];if(k.length===1){const y=c(k[0],h[h.length-1].ts);if(y){y.items.push(...i);continue}}const S=i.some(y=>y.kind==="flow-origin");I.push({id:`${n}~${S?"o":"u"}:${i[0].id}`,kind:S?"origin":"ui",items:i,...k.length>1?{after:k.sort()}:{}})}for(const i of F(q.filter(m=>!m.browser),s.phaseGapMs))I.push({id:`${n}~a:${i[0].id}`,kind:"ambient",items:i});const u=H(n,r.length,I,s),f=G(r);return f?{...u,sql:f}:u}w(segmentFlow,"segmentFlow"),p(segmentFlow,"segmentFlow");function H(e,t,s,o){for(const a of s)a.items.sort(K);const r=new Map;for(const a of s){const c=a.kind==="request"?E(a.items):void 0;c&&r.set(a,c)}const n=s.map(a=>{const c=W(a.items,r.get(a));return{unit:a,from:U(c,d=>d.from),to:D(c,d=>d.to)}});n.sort((a,c)=>a.from-c.from||(a.unit.id<c.unit.id?-1:1));const l=new Map;let g=0,v=-1/0;for(const a of n)(g===0||a.from-v>=o.phaseGapMs)&&(g+=1),v=Math.max(v,a.to),l.set(a.unit,g);const q=[];for(const a of n){const c=J(a.unit.items,o.maxSegmentEvents),d=a.unit.kind==="request"?G(a.unit.items):void 0;c.forEach((u,f)=>{const i=V(e,a.unit,u,l.get(a.unit),c.length>1?{index:f+1,total:c.length}:void 0,r.get(a.unit));q.push(d&&f===0?{...i,sql:d}:i)})}const I={};for(const a of q)for(const c of a.eventIds)I[c]=a.id;const $=[];for(const a of q){let c=$[a.phase-1];c||(c={index:a.phase,start:a.start,end:a.end,segmentIds:[]},$[a.phase-1]=c),c.start=Math.min(c.start,a.start),c.end=Math.max(c.end,a.end),c.segmentIds.push(a.id)}const j=q.reduce((a,c)=>a+c.counts.signal,0);return{flowId:e,segments:q,phases:$.filter(Boolean),assignment:I,stats:{events:t,signal:j,bookkeeping:t-j,segments:q.length,phases:$.filter(Boolean).length,largestSegment:q.reduce((a,c)=>Math.max(a,c.counts.total),0)}}}w(H,"J"),p(H,"assemble");function J(e,t){if(e.filter(n=>!n.bookkeeping).length<=t)return[e];const s=[];let o=[],r=0;for(const n of e)!n.bookkeeping&&r===t&&(s.push(o),o=[],r=0),o.push(n),n.bookkeeping||(r+=1);return o.length&&s.push(o),s}w(J,"Z"),p(J,"chunk");function V(e,t,s,o,r,n){const l={total:s.length,signal:0,bookkeeping:0,errors:0,warnings:0},g=new Set;let v="debug";for(const u of s){u.bookkeeping?l.bookkeeping+=1:l.signal+=1;const f=String(u.event.severity);f==="error"||f==="fatal"?l.errors+=1:f==="warning"&&(l.warnings+=1),(Q[f]??1)>(Q[v]??0)&&(v=f),u.project&&g.add(u.project)}const q=t.kind==="ui"||t.kind==="origin"?Y(s):[],I=W(s,n),$=U(I,u=>u.from),j=D(I,u=>u.to),a=n?s.filter(u=>n.has(u)).length:0,c=X(t.items),d=t.id;return{id:r&&r.index>1?`${d}#${r.index}`:d,flowId:e,kind:t.kind,label:Z(t,s,c,r,q),phase:o,start:$,end:j,durationMs:j-$,eventIds:s.map(u=>u.id),counts:l,projects:[...g].sort(),...c.route?{route:c.route}:{},...c.method?{method:c.method}:{},...t.requestId?{requestId:t.requestId}:{},...t.after?{after:t.after}:{},...q.length?{hot:q}:{},...r?{part:r}:{},...a?{stallsAfterResponse:a}:{},worstSeverity:v}}w(V,"ee"),p(V,"toSegment");function X(e){const t=e.find(s=>s.kind==="fetch-start"&&s.route)??e.find(s=>s.kind==="server-request"&&!s.parentRequestId&&s.route)??e.find(s=>s.kind==="gateway-request"&&s.route)??e.find(s=>s.kind==="fetch-done"&&s.route);return{...t?.route?{route:t.route}:{},...t?.method?{method:t.method.toUpperCase()}:{}}}w(X,"te"),p(X,"routeOf");function Y(e){const t=new Map;for(const s of e)s.subject&&!s.bookkeeping&&t.set(s.subject,(t.get(s.subject)??0)+1);return[...t.entries()].sort((s,o)=>o[1]-s[1]||(s[0]<o[0]?-1:1)).slice(0,3).map(([s,o])=>o>1?`${s} \xD7${o}`:s)}w(Y,"ne"),p(Y,"hotOf");function Z(e,t,s,o,r){const n=o?` (part ${o.index}/${o.total})`:"";if(e.kind==="request"){if(s.route)return`${s.method?`${s.method} `:""}${s.route}${n}`;const l=t.find(g=>g.queueKey);return l?`job ${l.queueKey}${n}`:`request ${e.requestId??""}${n}`}return e.kind==="origin"?`${t.find(l=>l.kind==="flow-origin")?.event.title||"start"}${r.length?` \xB7 ${r.join(", ")}`:""}${n}`:e.kind==="ui"?`UI update${r.length?` \xB7 ${r.join(", ")}`:""}${n}`:`background${n}`}w(Z,"se"),p(Z,"labelOf");const ee=50;export function sqlAccountingOf(e){return R(e,!1)}w(sqlAccountingOf,"sqlAccountingOf"),p(sqlAccountingOf,"sqlAccountingOf");function G(e){return R(e.map(t=>t.event),!0)}w(G,"L"),p(G,"accountSqlOfItems");function R(e,t){const s=new Map,o=new Map,r=t?void 0:new Set;for(const d of e){if(r){if(r.has(d.id))continue;r.add(d.id)}const u=x(d.raw)?d.raw:{},f=b(u.kind),i=b(d.context?.requestId);if(!i)continue;const m=b(u.project)??b(d.context?.projectName);if(f==="db-query"&&d.source==="db"){const h=o.get(i)??{count:0,workMs:0,...m?{project:m}:{}};h.count+=1,h.workMs+=M(u.durationMs)??0,o.set(i,h)}else if(f===SQL_ACCOUNT_KIND){const h=b(u.accountId)??d.id,k=M(u.seq)??0,S=s.get(h);if(S&&S.seq>=k)continue;s.set(h,{requestId:i,...m?{project:m}:{},seq:k,basis:u.basis==="end"?"end":"settled",statements:M(u.statements)??0,failed:M(u.failed)??0,workMs:M(u.workMs)??0,recorded:M(u.recorded)??0,suppressed:M(u.suppressed)??0,unwritten:M(u.unwritten)??0})}}const n=new Map;for(const d of s.values()){const u=n.get(d.requestId);if(!u){n.set(d.requestId,{...d});continue}d.basis==="settled"&&(u.basis="settled"),u.statements+=d.statements,u.failed+=d.failed,u.workMs+=d.workMs,u.recorded+=d.recorded,u.suppressed+=d.suppressed,u.unwritten+=d.unwritten}const l=[...new Set([...o.keys(),...n.keys()])].sort();if(l.length===0)return;const g={statements:0,failed:0,workMs:0,recorded:0,suppressed:0,unwritten:0},v={statements:0,workMs:0},q=[];for(const d of l){const u=o.get(d),f=n.get(d),i=u?.count??0;v.statements+=i,v.workMs+=u?.workMs??0;const m=!f||i>f.statements?"unverified":i===f.statements?"complete":"incomplete";f&&m!=="unverified"&&(g.statements+=f.statements,g.failed+=f.failed,g.workMs+=f.workMs,g.recorded+=f.recorded,g.suppressed+=f.suppressed,g.unwritten+=f.unwritten);const h=u?.project??f?.project;q.push({requestId:d,...h?{project:h}:{},status:m,observed:i,observedWorkMs:u?.workMs??0,...f?{basis:f.basis,statements:f.statements,failed:f.failed,workMs:f.workMs,recorded:f.recorded,suppressed:f.suppressed,unwritten:f.unwritten}:{}})}const I=p(d=>q.filter(u=>u.status===d).length,"tally"),$=I("unverified"),j=I("incomplete"),a=p(d=>(d.statements??0)-d.observed,"missing"),c=q.filter(d=>d.status!=="complete").sort((d,u)=>(d.status==="incomplete"?0:1)-(u.status==="incomplete"?0:1)||a(u)-a(d)||(d.requestId<u.requestId?-1:d.requestId>u.requestId?1:0));return{status:$>0?"unverified":j>0?"incomplete":"complete",requests:q.length,complete:I("complete"),incomplete:j,unverified:$,settled:q.filter(d=>d.status!=="unverified"&&d.basis==="settled").length,observed:v,accounted:g,gaps:c.slice(0,ee),gapsTruncated:c.length>ee}}w(R,"D"),p(R,"accountSql");
|
package/lib/flow-shape.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Bt=Object.defineProperty;var R=(i,f)=>Bt(i,"name",{value:f,configurable:!0});var St=Object.defineProperty,P=R((i,f)=>St(i,"name",{value:f,configurable:!0}),"R"),At=Object.defineProperty,w=P((i,f)=>At(i,"name",{value:f,configurable:!0}),"I");import{redactDeep as Rt,redactText as ut,redactUrlParams as Tt}from"./redact.js";import{templatisePath as wt}from"./route-template.js";import{SQL_ACCOUNT_KIND as Dt,sqlAccountingOf as Ct}from"./flow-segments.js";import{isServiceHopHost as kt}from"./service-hop.js";import{boundedClone as Pt}from"@lensmcp/protocol-types";export function areaOf(i){const{source:f,category:s,title:q,kind:l}=i;return f==="ai"?"ai":f==="perf"&&l==="event-loop-stall"?"services":l?.startsWith("memory")||l==="db-query"?"data":f==="gateway"||l==="gateway-request"?"gateway":l==="queue-process"||l==="queue-active"||l==="queue-completed"||l==="queue-failed"?"services":f==="db"||f==="redis"||f==="queue"||l==="redis-op"||l?.startsWith("queue-")||f==="fs"||l==="fs-ops"?"data":f==="exec"||l==="exec"||l==="egress"&&kt(i.host)?"services":f==="external"||l==="egress"?"network":f==="nestjs"||f==="nest"?/(?:Repository|Repo|Dao|Store|EntityManager|DataSource|QueryRunner|OrmClient)\./.test(q)?"data":"services":f==="valtio"?"state":s==="network"?"network":"browser"}R(areaOf,"areaOf"),P(areaOf,"areaOf"),w(areaOf,"areaOf");const qt=240,Ft=new Set(["systemPrompt","prompt","prompts","messages","completion","completions","input","output","draft","text","content","result","arguments","params"]),Nt=new Set(["tenantId","caller","userId","accountId","email","subject"]),Ot=8;function ct(i){const f=et(i,0);return Pt(Rt({redacted:!0,...f}),{rawByteLimit:16*1024}).value}R(ct,"vt"),P(ct,"Ct"),w(ct,"aiProjection");function et(i,f){if(typeof i=="string")return lt(i);if(i===null||typeof i!="object")return i;if(f>=Ot)return;if(Array.isArray(i))return i.slice(0,32).map(q=>et(q,f+1));const s={};for(const[q,l]of Object.entries(i))if(!Nt.has(q)){if(Ft.has(q)){typeof l=="string"?s[`${q}Chars`]=l.length:Array.isArray(l)?s[`${q}Count`]=l.length:l!=null&&(s[`${q}Present`]=!0);continue}s[q]=et(l,f+1)}return s}R(et,"rt"),P(et,"it"),w(et,"projectAi");function lt(i){return i.length<=qt?ut(i):`${ut(i.slice(0,qt))}\u2026`}R(lt,"kt"),P(lt,"Et"),w(lt,"clipText");export function httpFacts(i,f){const s=w(I=>typeof I=="string"&&I.length>0?I:void 0,"str"),q=typeof f.status=="number"?f.status:void 0,l=w(I=>Tt(I.split("?")[0]??I)||"/","pathOf"),F=w((I,y,j={})=>({...I?{method:I.toUpperCase()}:{},...y?{rawPath:y,route:wt(y)}:{},...q!==void 0?{status:q}:{},...j}),"facts");if(i==="gateway-request"){const I=s(f.url);return F(s(f.method),I?l(I):void 0,{...s(f.host)?{host:s(f.host)}:{},...s(f.caller)?{caller:s(f.caller)}:{}})}if(i==="egress"){const I=s(f.path),y=s(f.exchangeId);return F(s(f.method)??"GET",I?l(I):void 0,{...s(f.host)?{host:s(f.host)}:{},...y?{exchangeId:y}:{}})}if(i==="fetch-start"||i==="fetch-done"||i==="fetch-error"){const I=s(f.href)??s(f.url);let y,j;if(I)try{const p=new URL(I,"http://relative.invalid");y=l(p.pathname),p.hostname!=="relative.invalid"&&(j=p.hostname)}catch{}return F(s(f.method)??"GET",y,j?{host:j}:{})}if(i==="ai-call")return{...q!==void 0?{status:q}:{},...f.stream===!0?{streamed:!0}:{}};const g=f.request;if(g&&typeof g=="object"){const I=g,y=s(I.route),j=s(I.method);if(!y&&!j)return{};const p=typeof I.status=="number"?I.status:void 0,T=y?l(y):void 0;return{...j?{method:j.toUpperCase()}:{},...T?{rawPath:T,route:T.includes(":")?T:wt(T)}:{},...p!==void 0?{status:p}:{}}}return{}}R(httpFacts,"httpFacts"),P(httpFacts,"httpFacts"),w(httpFacts,"httpFacts");export function trimFlowEvent(i,f={}){const s=i.raw??{},q=s.render??{},l=s.span??{},F=s.request??{},g=w(d=>typeof d=="number"?d:void 0,"num"),I=s.loop??{},y=s.effect??{},j=g(s.durationMs)??g(l.durationMs)??g(F.durationMs)??g(I.durationMs)??g(q.actualDurationMs)??g(y.durationMs),p=typeof s.kind=="string"?s.kind:void 0,T=p==="fetch-done"||p==="fetch-error",_=g(q.commitTime),z=p==="render"&&_!==void 0&&Math.abs(_-i.timestamp)<6e4,J=T?i.timestamp:z?_:g(s.startedAt)??g(l.startTime)??g(F.startedAt)??g(I.startedAt)??(j!==void 0?i.timestamp-j:i.timestamp);let b;if(typeof s.query=="string"){const d=Array.isArray(s.params)?` [${s.params.join(", ")}]`:"",m=typeof s.result=="string"?` \u2192 ${s.result}`:"";b=s.query+d+m}else if(typeof s.command=="string"){const d=typeof s.value=="string"?` \u2192 ${s.value}`:s.hit===!1?" \u2192 (miss)":"";b=s.command+d}else if(p==="state-update"){const d=s.update??{};let m="";try{m=JSON.stringify(d.preview)}catch{}b=`${d.storeId??""}.${d.path??""} = ${m}`}else if(p==="fs-ops"&&Array.isArray(s.paths))b=s.paths.slice(0,8).map(d=>{const m=d??{},S=typeof m.n=="number"&&m.n>1?` \xD7${m.n}`:"",h=typeof m.bytes=="number"&&m.bytes>0?` (${m.bytes>=1024?`${(m.bytes/1024).toFixed(1)} KB`:`${m.bytes} B`})`:"";return`${String(m.op??"?")} ${String(m.path??"?")}${S}${h}`}).join(" \xB7 ");else if(p==="exec"&&typeof s.cmd=="string")b=`${s.cmd} \u2192 ${s.code??s.signal??s.error??"?"}`;else if(p==="queue-completed"&&typeof s.returned=="string")b=`returned ${s.returned}`;else if(p==="queue-enqueued")b=`${s.job??"job"}#${s.jobId??"?"} \u2192 ${s.queue??""}${typeof s.data=="string"?" "+s.data:""}`;else if(p==="log"){const d=F.error??{},m=typeof d.stack=="string"?d.stack:typeof s.text=="string"?s.text:void 0;m&&m!==i.title&&(b=m)}let U,W,Y,K;if(p==="span"){const d=l.outcome;if(U=d==="threw"||d==="returned"?d:l.status==="error"?"threw":l.status==="ok"?"returned":void 0,U==="threw"){const h=l.error??{},C=typeof h.name=="string"?h.name:void 0,G=typeof h.message=="string"?h.message:typeof i.message=="string"?i.message:void 0;Y=C,W=ut([C,G].filter(Boolean).join(": "))||void 0}const m=typeof l.args=="string"?l.args:void 0,S=typeof l.result=="string"?l.result:void 0;if(m!==void 0||S!==void 0){K=[...m!==void 0?["args"]:[],...S!==void 0?["result"]:[]];const h=w(C=>C.length>Mt?`${C.slice(0,Mt)}\u2026`:C,"short");b=[m!==void 0?`args ${h(m)}`:"",S!==void 0?`returned ${h(S)}`:""].filter(Boolean).join(" \u2192 ")}}let Q;if(p==="gateway-request")Q=`${s.host??""}${String(s.url??"").split("?")[0]}`;else if(p==="fetch-start"||p==="fetch-done"||p==="fetch-error")try{const d=new URL(String(s.href??s.url??""));Q=`${d.hostname}${d.pathname}`}catch{}let O;if(p==="gateway-request"&&Array.isArray(s.steps))O=s.steps.map(d=>{const m=[String(d.step)];return d.project&&m.push(String(d.project)),d.prefix&&m.push(String(d.prefix)),d.mode&&m.push(String(d.mode)+(d.ok===!1?" \u2717":d.ok===!0?" \u2713":"")),d.caller&&m.push("by "+String(d.caller)),d.pod&&m.push(String(d.pod)),typeof d.atMs=="number"&&m.push(`@${d.atMs}ms`),m.join(" ")}),b=O.map(d=>d.replace(/ @\d+ms$/,"")).join(" \u2192 ");else if(p==="egress")b=`${s.method??"GET"} ${s.host??""} \u2192 ${s.status??"?"}`;else if(i.source==="ai"){if(p==="ai-call"){const d=s.usage??{},m=d.promptTokens!==void 0||d.completionTokens!==void 0?` \xB7 ${g(d.promptTokens)??"?"}\u2192${g(d.completionTokens)??"?"} tok`:"",S=s.status!==void 0?` \xB7 ${String(s.status)}`:s.error?" \xB7 failed":"",h=g(s.toolCallCount)?` \xB7 ${g(s.toolCallCount)} tool call${g(s.toolCallCount)===1?"":"s"}`:"",C=g(s.durationMs)!==void 0?` \xB7 ${(g(s.durationMs)/1e3).toFixed(1)}s`:"";b=`${s.provider??""}/${s.model??"?"}${m}${S}${h}${C}${s.stream===!0?" \xB7 stream":""}`}else if(p==="ai-round"){const d=s.model??{},m=s.usage??{},S=Array.isArray(s.toolCalls)?s.toolCalls:[],h=`${g(m.promptTokens)??"?"}\u2192${g(m.completionTokens)??"?"} tok`,C=S.length?` \xB7 ${S.length} tool call${S.length===1?"":"s"}`:"",G=g(s.durationMs)!==void 0?` \xB7 ${(g(s.durationMs)/1e3).toFixed(1)}s`:"";b=`${d.provider??""}/${d.model??""} \xB7 ${h}${C}${G}`}else if(p==="ai-turn-start"){const d=Array.isArray(s.tools)?s.tools:[];b=`skill ${s.skill??""} \xB7 ${s.output??""}${d.length?" \xB7 tools: "+d.join(", "):""}`}else if(p==="ai-tool-call"){const d=s.call??{},m=s.result??{},S=g(s.durationMs)!==void 0?` \xB7 ${(g(s.durationMs)/1e3).toFixed(1)}s`:"";b=`${d.name??"tool"} ${m.error!=null?"\u2717 "+String(m.error):"\u2713"}${S}`}else if(p==="ai-turn-end"){const d=s.usage??{},m=g(s.rounds)!==void 0?` \xB7 ${g(s.rounds)} round${g(s.rounds)===1?"":"s"}`:"",S=g(s.durationMs)!==void 0?` \xB7 ${(g(s.durationMs)/1e3).toFixed(1)}s`:"";b=`${s.ok===!1?"FAILED":"ok"} \xB7 ${g(d.promptTokens)??"?"}+${g(d.completionTokens)??"?"} tok${m}${S}`}}let D;p==="db-query"&&typeof s.query=="string"?D=s.query:p==="redis-op"?D=`${s.op??""} ${String(s.key??"").replace(/\d+/g,"#")}`:p==="egress"?D=`${s.method??"GET"} ${s.host??""}`:p==="ai-call"?D=`ai ${s.provider??""} ${s.model??""} ${s.api??""}`:p==="queue-enqueued"?D=`enqueue ${s.queue??""}`:p==="span"&&typeof l.name=="string"?D=l.name:p==="memory-mutation"&&(D=i.title);const L=i.context??{},X=q.commitId??y.commitId??s.commitId,V=Array.isArray(q.why)?q.why.map(d=>typeof d?.type=="string"?d.type:void 0).filter(d=>!!d):[];return{id:i.id,t:i.timestamp,start:J,...D?{sig:D}:{},...Q?{urlKey:Q}:{},...httpFacts(p,s),...(p==="db-query"||p==="redis-op")&&typeof s.valuesId=="string"?{valuesId:s.valuesId}:{},...typeof s.queue=="string"?{queue:s.queue}:{},...s.jobId!=null?{jobId:String(s.jobId)}:{},source:i.source,category:i.category,severity:i.severity,title:U==="threw"&&typeof l.name=="string"?`${l.name} \u2014 threw${Y?` ${Y}`:""}${j!==void 0?` after ${j}ms`:""}`:i.title,kind:p,area:areaOf({source:i.source,category:i.category,title:i.title,kind:p,...typeof s.host=="string"?{host:s.host}:{}}),...p==="egress"&&kt(typeof s.host=="string"?s.host:void 0)?{hop:!0}:{},...p==="egress"&&typeof s.calleeRequestId=="string"?{calleeRequestId:s.calleeRequestId}:{},...U?{outcome:U}:{},...W?{error:W}:{},...K?{captured:K}:{},...typeof l.role=="string"?{role:l.role}:{},...typeof l.spanId=="string"?{spanId:l.spanId}:{},...typeof l.parentSpanId=="string"?{parentSpanId:l.parentSpanId}:{},...p==="log"&&typeof s.level=="string"?{level:s.level}:{},...p==="log"&&typeof s.logger=="string"?{logger:s.logger}:{},...p==="log"&&typeof s.stream=="string"?{stream:s.stream}:{},...typeof L.requestId=="string"?{requestId:L.requestId}:{},...typeof L.parentRequestId=="string"?{parentRequestId:L.parentRequestId}:{},...typeof X=="string"?{commitId:X}:{},...typeof q.componentName=="string"?{component:q.componentName}:{},...typeof q.phase=="string"?{phase:q.phase}:{},...V.length?{why:V}:{},...p==="effect-run"&&typeof y.hookLogicalId=="string"?{hook:y.hookLogicalId}:{},...p==="effect-run"&&y.cleanupRan===!0?{cleanup:!0}:{},...p==="render-commit-truncated"?{renderTotal:g(s.total),renderPublished:g(s.published),...g(s.walkStoppedAt)!==void 0?{renderWalkStoppedAt:g(s.walkStoppedAt)}:{}}:{},...typeof L.projectName=="string"?{project:L.projectName}:typeof s.project=="string"?{project:s.project}:{},...j!==void 0?{durationMs:j}:{},...T&&g(s.startedAt)!==void 0?{fetchStartedAt:g(s.startedAt)}:{},...i.source==="ai"?{ai:f.includeRaw?s:ct(s)}:{},...b?{detail:b}:{},...O?{steps:O}:{}}}R(trimFlowEvent,"trimFlowEvent"),P(trimFlowEvent,"trimFlowEvent"),w(trimFlowEvent,"trimFlowEvent");const Mt=300;export const OUTLIVED_PARENT_MIN_MS=250;const Et=new Set(["egress","db-query","redis-op","ai-call","span","gateway-request","server-request"]),pt=1;function ft(i){return i==="flow-origin"?0:i?.endsWith("-start")?1:i==="gateway-request"?2:i==="server-request"?3:i==="span"?4:i==="render"||i==="render-commit-truncated"?6:i==="effect-run"?7:5}R(ft,"Mt"),P(ft,"Nt"),w(ft,"kindRank");function H(i){return i.kind==="span"&&i.role==="controller"?3.5:i.kind==="queue-process"?3:ft(i.kind)}R(H,"J"),P(H,"Z"),w(H,"causalRank");export function buildFlowDetail(i,f={}){const s=Ct(i),q=i.slice().filter(t=>{const r=t.raw??{},e=typeof r.kind=="string"?r.kind:"";if(e==="subscription-added"||e==="subscription-removed"||e==="hook-run"||e==="store-created"||e==="singleton-instance"||e===Dt)return!1;const o=r.span;return!(typeof o?.name=="string"&&o.name.startsWith("nest.request "))}).map(t=>trimFlowEvent(t,f)),l=new Set;for(let t=q.length-1;t>=0;t--)l.has(q[t].id)?q.splice(t,1):l.add(q[t].id);const F=new Map;for(const t of q)(t.kind==="gateway-request"||t.kind==="queue-process")&&t.requestId&&!F.has(t.requestId)&&F.set(t.requestId,t.start);const g=w(t=>t.source!=="react"&&t.source!=="valtio"&&t.requestId&&F.has(t.requestId)?F.get(t.requestId):t.start,"anchorOf");q.sort((t,r)=>g(t)-g(r)||t.start-r.start||H(t)-H(r)||(r.durationMs??0)-(t.durationMs??0)||t.t-r.t);const I=w(t=>(t.kind==="span"||t.kind==="server-request"||t.kind==="gateway-request"||t.kind==="queue-process")&&(t.durationMs??0)>0,"spanLike"),y=q;for(const t of y)t.group=t.source==="react"||t.source==="valtio"||t.source==="client-runtime"||t.source==="visual"?"browser":"server";const j=new Map;for(const t of y)if((t.kind==="gateway-request"||t.kind==="queue-process")&&t.requestId&&t.project){const r=j.get(t.requestId)??[];r.push({project:t.project,start:t.start,end:t.start+(t.durationMs??0)}),j.set(t.requestId,r)}const p=w((t,r)=>{const e=j.get(t);if(e?.length)return e.every(o=>o.project===e[0].project)?e[0].project:e.filter(o=>o.start<=r+1&&o.end>=r-1).sort((o,n)=>n.start-o.start)[0]?.project},"projectOf");for(const t of y)if(!t.project&&t.source==="nestjs"&&t.requestId){const r=p(t.requestId,t.start);r&&(t.project=r)}const T=new Map;for(const t of y)t.spanId&&T.set(t.spanId,t);const _=w((t,r)=>{let e=t.parentSpanId?T.get(t.parentSpanId):void 0;for(let o=0;e&&o<64;o++){if(e===r)return!0;e=e.parentSpanId?T.get(e.parentSpanId):void 0}return!1},"inside"),z=new Map(y.map((t,r)=>[t,r])),J=new Map,b=w(t=>{let r=J.get(t.group);r||J.set(t.group,r=new Map);const e=t.requestId;let o=r.get(e);return o||r.set(e,o={spans:[],next:0,alive:[]}),o},"legOf");for(const t of y)I(t)&&Number.isFinite(t.start)&&b(t).spans.push(t);for(const t of J.values())for(const r of t.values())r.spans.sort((e,o)=>e.start-o.start||z.get(e)-z.get(o));const U=w(t=>Number.isFinite(t.start)?t.start:1/0,"startOf");for(const t of y.slice().sort((r,e)=>U(r)-U(e))){const r=t.parentSpanId?T.get(t.parentSpanId):void 0;if(r&&r!==t&&r.group===t.group&&r.requestId===t.requestId){t.parent=r.id;continue}if(!Number.isFinite(t.start))continue;const e=b(t);for(;e.next<e.spans.length&&e.spans[e.next].start<=t.start+pt;)e.alive.push(e.spans[e.next++]);e.alive=e.alive.filter(a=>a.start+(a.durationMs??0)>t.start);const o=e.alive.length>1?e.alive.slice().sort((a,$)=>z.get(a)-z.get($)):e.alive;let n;for(const a of o){if(a===t)continue;const $=a.project,k=t.project;if(a.kind!=="gateway-request"&&t.kind!=="gateway-request"&&$!==void 0&&k!==void 0&&$!==k)continue;const u=a.durationMs??0,c=t.durationMs??0,v=Math.abs(a.start-t.start)<=pt,M=H(a),A=H(t),x=a.start<=t.start;if(!(a.start+u>t.start&&(x||v&&M<A)&&(!v||(M!==A?M<A:x&&u>c))))continue;const E=n?H(n):-1,N=n?Math.abs(a.start-n.start)<=pt:!1,Z=n&&N&&M===E?_(a,n)?1:_(n,a)?-1:0:0;(!n||!N&&a.start>n.start||N&&M>E||Z>0||Z===0&&N&&M===E&&(a.durationMs??0)<(n.durationMs??0))&&(n=a)}n&&(t.parent=n.id)}for(const t of y)delete t.spanId,delete t.parentSpanId;{const t=new Set(y.map(a=>a.id)),r=new Set,e=new Map,o=[],n=w(a=>{const $=[a];for(;$.length;){const k=$.pop();o.push(k),r.add(k.id);const u=e.get(k.id);if(u){e.delete(k.id);for(let c=u.length-1;c>=0;c--)$.push(u[c])}}},"place");for(const a of y)if(a.parent&&a.parent!==a.id&&t.has(a.parent)&&!r.has(a.parent)){const $=e.get(a.parent)??[];$.push(a),e.set(a.parent,$)}else n(a);for(const a of y)r.has(a.id)||(o.push(a),r.add(a.id));for(let a=0;a<o.length;a++)y[a]=o[a]}const W=new Map;for(const t of y){if(!t.requestId)continue;const r=W.get(t.requestId);r?r.push(t):W.set(t.requestId,[t])}const Y=new Map(y.map(t=>[t.id,t])),K=new Map;for(const t of y)t.calleeRequestId&&!K.has(t.calleeRequestId)&&K.set(t.calleeRequestId,t);const Q=w((t,r)=>{const e=(W.get(t)??[]).filter(o=>I(o)&&o.start<=r&&o.start+(o.durationMs??0)>=r);if(e.length!==0)return e.sort((o,n)=>n.start-o.start||(o.durationMs??0)-(n.durationMs??0)),e[0]},"callerNodeFor");for(const t of y){if(!t.parentRequestId||t.kind!=="server-request"&&t.kind!=="gateway-request")continue;const r=t.parent?Y.get(t.parent):void 0;if(r&&r.requestId===t.requestId)continue;const e=t.requestId?K.get(t.requestId):void 0,o=e&&e.start<=t.start?e:Q(t.parentRequestId,t.start);o&&o!==t&&o.start<=t.start&&(t.parent=o.id)}const O=y,D=new Map;for(const t of O)t.kind==="gateway-request"&&t.requestId&&!D.has(t.requestId)&&D.set(t.requestId,t);for(const t of O)if(t.group==="server"&&!t.parent&&t.requestId&&t.kind!=="gateway-request"){const r=D.get(t.requestId);r&&r!==t&&r.start<=t.start&&(t.parent=r.id)}const L=new Set,X=new Set,V=[];for(const t of O){if(t.kind!=="gateway-request")continue;if(!t.requestId){V.push(t);continue}const r=W.get(t.requestId)??[];if(r.filter(n=>n.kind==="gateway-request").sort((n,a)=>n.start-a.start)[0]!==t)continue;const e=r.find(n=>n.kind==="fetch-start"),o=r.find(n=>n.kind==="fetch-done"||n.kind==="fetch-error");if(!e&&!o){V.push(t);continue}e&&(t.fetchStartId=e.id,L.add(e.id)),o&&(t.fetchDoneId=o.id,X.add(o.id))}const d=new Map;for(const t of O){if(!t.urlKey)continue;const r=d.get(t.urlKey)??{gws:[],starts:[],dones:[]};t.kind==="gateway-request"?V.includes(t)&&r.gws.push(t):t.kind==="fetch-start"?L.has(t.id)||r.starts.push(t):(t.kind==="fetch-done"||t.kind==="fetch-error")&&(X.has(t.id)||r.dones.push(t)),d.set(t.urlKey,r)}for(const{gws:t,starts:r,dones:e}of d.values()){t.sort((n,a)=>n.start-a.start),r.sort((n,a)=>n.start-a.start);const o=new Set;t.forEach((n,a)=>{const $=r[a];$&&(n.fetchStartId=$.id);const k=$?$.t:n.start;let u,c=1/0;for(const v of e){if(o.has(v.id))continue;const M=v.fetchStartedAt??v.start-(v.durationMs??0),A=Math.abs(M-k);A<c&&(c=A,u=v)}u&&(n.fetchDoneId=u.id,o.add(u.id))})}const m=new Map,S=new Map;for(const t of O)!t.queue||!t.jobId||(t.kind==="queue-enqueued"&&m.set(`${t.queue}#${t.jobId}`,t),t.kind==="queue-process"&&S.set(`${t.queue}#${t.jobId}`,t));for(const t of O){if(!t.queue||!t.jobId)continue;const r=`${t.queue}#${t.jobId}`;if(t.kind==="queue-process"&&!t.parent){const e=m.get(r);e&&e!==t&&e.start<=t.start&&(t.parent=e.id)}else if(t.kind==="queue-active"||t.kind==="queue-completed"||t.kind==="queue-failed"){const e=S.get(r);e&&e!==t&&e.start<=t.start&&(t.parent=e.id)}}{const t=new Map(y.map(r=>[r.id,r]));for(const r of y){if(!r.parent||!Et.has(r.kind??"")||r.durationMs===void 0)continue;const e=t.get(r.parent);if(!e||e.kind!=="span"||!((e.durationMs??0)>0))continue;const o=Math.round(r.start+r.durationMs-(e.start+e.durationMs));if(o<OUTLIVED_PARENT_MIN_MS)continue;r.outlivedParentMs=o;const n=e.leftRunning??{count:0,longestMs:0};e.leftRunning={count:n.count+1,longestMs:Math.max(n.longestMs,o)}}for(const r of y){const e=[];r.leftRunning&&e.push(`ended before ${r.leftRunning.count===1?"a call":`${r.leftRunning.count} calls`} it started had finished \u2014 ${r.leftRunning.count===1?"it":"the longest"} ran on ${r.leftRunning.longestMs}ms (a timeout that gave up, or work not awaited)`),r.outlivedParentMs!==void 0&&e.push(`ran on ${r.outlivedParentMs}ms after the span that started it had ended`),e.length>0&&(r.detail=[r.detail,...e].filter(Boolean).join(" \xB7 "))}}let h=y;{const t=new Map;for(const e of h)e.kind==="fetch-start"&&t.set(e.id,e);const r=new Map;for(const e of h){if(e.kind!=="fetch-done"&&e.kind!=="fetch-error")continue;const o=[...t.values()].find($=>$.requestId&&$.requestId===e.requestId)?.id,n=o?t.get(o):void 0;if(!n)continue;const a=e.fetchStartedAt??n.start;n.title=e.title,n.severity=e.severity,n.durationMs=e.durationMs??Math.max(0,e.t-a),n.kind="fetch",e.detail&&!n.detail&&(n.detail=e.detail),r.set(e.id,n.id)}if(r.size>0){h=h.filter(e=>!r.has(e.id));for(const e of h)for(e.fetchDoneId&&r.has(e.fetchDoneId)&&(e.fetchDoneId=r.get(e.fetchDoneId));e.parent&&r.has(e.parent);)e.parent=r.get(e.parent)}}const C=w(t=>!!t.commitId&&(t.kind==="render"||t.kind==="effect-run"||t.kind==="render-commit-truncated"),"commitMember"),G=w(t=>[...new Set(t)],"uniq"),ot=w(t=>G(t.map(r=>r.component).filter(r=>!!r)),"named"),It=w(t=>t.length<=2?t.join(" + "):`${t[0]} + ${t.length-1} more`,"nameList"),mt=w(t=>{const r=new Map;for(const e of t)e.component&&r.set(e.component,(r.get(e.component)??0)+1);return[...r].map(([e,o])=>o>1?`${e} \xD7${o}`:e).join(", ")},"tally"),bt=w(t=>{if(!t)return"effect";const r=/([^/\\]+?):(\d+)(?::\d+)?$/.exec(t);return r?`${r[1]}:${r[2]}`:t},"shortHook"),nt=w(t=>`${bt(t.hook)}${t.cleanup?" (cleanup)":""}`,"hookLabel"),gt=w(t=>{const r=Math.min(...t.map(o=>o.start)),e=Math.max(...t.map(o=>o.start+(o.durationMs??0)));return{start:r,durationMs:e-r}},"extent"),ht=w(t=>(delete t.component,delete t.why,delete t.phase,delete t.hook,delete t.cleanup,delete t.renderTotal,delete t.renderPublished,t),"asGroup");function at(t){const r=t[0],e=t.filter(B=>B.kind==="render"),o=t.filter(B=>B.kind==="effect-run"),n=t.find(B=>B.kind==="render-commit-truncated"),a=[...e].sort((B,tt)=>(tt.durationMs??0)-(B.durationMs??0)),$=a.filter(B=>(B.why??[]).some(tt=>tt!=="parent-render")),k=G($.flatMap(B=>(B.why??[]).filter(tt=>tt!=="parent-render"))),u=ot($),c=e.filter(B=>B.phase==="mount"),v=e.filter(B=>B.phase!=="mount"&&!$.includes(B)),M=a[0]?.component??"view",A=e.length>0&&c.length===e.length,x=k.length?k.join(" + "):void 0,E=A?"mount":"update",N=u.length?`view update \xB7 ${It(u)} \u2190 ${x}`:A?`view mount \xB7 ${M}`:`view update \xB7 ${M}`,Z=n?.renderTotal??e.length,jt=[c.length?`mounted ${mt(c)}`:"",v.length?`re-rendered ${mt(v)}`:"",!u.length&&!A&&e.length?"trigger not recorded \u2014 every captured render came from its parent":"",n?.renderWalkStoppedAt!==void 0?`at least ${Z} renders (the walk stopped at ${n.renderWalkStoppedAt} fibers), ${n.renderPublished??e.length} recorded`:n?`${Z} renders, ${n.renderPublished??e.length} recorded (per-commit cap)`:`${e.length} render${e.length===1?"":"s"}`,o.length?`${o.length} effect${o.length===1?"":"s"}: ${o.map(nt).join(", ")}`:""].filter(Boolean).join(" \xB7 "),vt=ot(e);return ht({...r,...gt(t),kind:"view-update",title:N,detail:jt,commitId:r.commitId,viewPhase:E,...x?{cause:x}:{},causeComponents:u,components:vt,mounted:ot(c),renderCount:Z,...n?.renderWalkStoppedAt!==void 0?{renderCountAtLeast:!0}:{},recordedRenders:e.length,effects:o.length,effectHooks:o.map(nt),memberIds:t.map(B=>B.id),sig:`commit:${E}:${x??"-"}:${u.join(",")}:${vt.slice().sort().join(",")}`,severity:t.some(B=>B.severity==="error")?"error":r.severity})}R(at,"ct"),P(at,"It"),w(at,"viewUpdateNode");function it(t){const r=t[0],e=t.map(nt);return ht({...r,...gt(t),kind:"commit-effects",title:t.length===1?`effect \xB7 ${e[0]}`:`effects \xB7 ${e[0]} + ${t.length-1} more`,detail:e.join(", "),commitId:r.commitId,effects:t.length,effectHooks:e,memberIds:t.map(o=>o.id),sig:`effects:${e.slice().sort().join(",")}`})}R(it,"lt"),P(it,"kt"),w(it,"effectsNode");const xt=w(t=>G(t.map(r=>r==="state-update"?"state update":r==="fetch-start"?"request":r==="flow-origin"?"interaction":"response")).join(" + "),"describeInputs");function dt(t,r){const e=t[0],o=t[t.length-1],n=t.length,a=e.detail,$=Math.min(...t.map(v=>v.start)),k=Math.max(...t.map(v=>v.start+(v.durationMs??0))),u=Math.round(t.reduce((v,M)=>v+(M.durationMs??0),0)/n*10)/10,c=Math.round((k-$)*10)/10;e.count=n,e.avgMs=u,e.wallMs=c,e.durationMs=k-$,e.items=t.slice(0,12).map(v=>({start:v.start-$,...v.durationMs!=null?{durationMs:v.durationMs}:{}})),e.memberIds=t.flatMap(v=>v.memberIds??[v.id]),e.title=`\xD7${n} ${e.title}`,t.some(v=>v.severity==="error")&&(e.severity="error"),e.kind==="view-update"?e.detail=[`${n} commits, ${r.length?`one per ${xt(r)}`:"back to back"}`,`avg ${u}ms \xB7 wall ${c}ms`,a??""].filter(Boolean).join(" \xB7 "):e.kind==="state-update"&&a&&o.detail?e.detail=`${n} updates in a row \xB7 wall ${c}ms \xB7 first ${a} \xB7 last ${o.detail}`:e.detail=[`${n} in a row \xB7 wall ${c}ms`,a??""].filter(Boolean).join(" \xB7 ")}R(dt,"pt"),P(dt,"qt"),w(dt,"collapseRepeat");const rt=[],st=new Map,$t=new Set;for(let t=0;t<h.length;t++){if($t.has(t))continue;const r=h[t];if(!C(r)){rt.push(r);continue}const e=[t];for(let a=t+1;a<h.length;a++){const $=h[a];if($.group==="browser"){if(!C($)||$.commitId!==r.commitId)break;e.push(a)}}for(const a of e)$t.add(a);const o=e.map(a=>h[a]),n=o.some(a=>a.kind!=="effect-run")?at(o):it(o);for(const a of o)a.id!==n.id&&st.set(a.id,n.id);rt.push(n)}if(st.size>0||rt.some(t=>t.kind==="view-update"||t.kind==="commit-effects")){h=rt;for(const t of h)for(;t.parent&&st.has(t.parent);)t.parent=st.get(t.parent)}{const t=new Map;for(const r of h)r.kind==="view-update"&&r.commitId&&!t.has(r.commitId)&&t.set(r.commitId,r);for(const r of h){if(r.kind!=="commit-effects"||r.parent||!r.commitId)continue;const e=t.get(r.commitId);e&&e.start<=r.start&&(r.parent=e.id)}}{const t=new Set(["state-update","fetch","fetch-start","fetch-done","fetch-error","flow-origin"]),r=h.map((u,c)=>u.group==="browser"?c:-1).filter(u=>u>=0),e=w(u=>`${u.kind}|${u.sig??u.title}`,"keyOf"),o=r.map(u=>e(h[u])),n=w((u,c,v)=>{for(let M=0;M<v;M++)if(o[u+M]!==o[c+M])return!1;return!0},"sameBlock"),a=w(u=>o[u].split("|")[0],"kindAt"),$=w(u=>{let c=0,v=1;for(let M=1;M<=4&&u+M<=o.length;M++){let A=1;for(;u+(A+1)*M<=o.length&&n(u,u+A*M,M);)A+=1;A>=3&&M*A>c*v&&(c=M,v=A)}return{k:c,r:v}},"bestBlockAt"),k=new Map;for(let u=0;u<o.length;){const{k:c,r:v}=$(u);if(c===0){u+=1;continue}const M=[...Array(c).keys()].find(x=>t.has(a(u+x)));if(M!==void 0&&M>0){const x=$(u+M);if(x.k===c&&x.r>=3&&x.r>=v-1){u+=M;continue}}const A=o.slice(u,u+c).map(x=>x.split("|")[0]).filter(x=>t.has(x));for(let x=0;x<c;x++){const E=[];for(let N=0;N<v;N++)E.push(h[r[u+x+N*c]]);dt(E,A);for(const N of E.slice(1))k.set(N.id,E[0].id)}u+=c*v}if(k.size>0){h=h.filter(u=>!k.has(u.id));for(const u of h)for(;u.parent&&k.has(u.parent);)u.parent=k.get(u.parent)}}{const t=new Map(h.map(r=>[r.id,r]));for(const r of h){const e=new Set([r.id]);let o=r;for(;o?.parent;){if(e.has(o.parent)){o.parent=void 0;break}e.add(o.parent),o=t.get(o.parent)}}}const yt=w(t=>{const r=new Map;for(const o of h){if(!o.sig||I(o)!==t||o.kind==="server-request"||o.kind==="gateway-request"||o.group==="browser")continue;const n=`${o.parent??"root"}|${o.kind}|${o.sig}`,a=r.get(n);a?a.push(o):r.set(n,[o])}const e=new Map;for(const o of r.values()){if(o.length<3)continue;o.sort((c,v)=>c.start-v.start);const n=o[0];let a=!1;for(let c=0;c+1<o.length;c++)if(o[c+1].start<o[c].start+(o[c].durationMs??0)){a=!0;break}const $=n.start,k=Math.max(...o.map(c=>c.start+(c.durationMs??0))),u=o.reduce((c,v)=>c+(v.durationMs??0),0);n.count=o.length,n.parallel=a,n.avgMs=Math.round(u/o.length*10)/10,n.wallMs=k-$,n.durationMs=k-$,n.title=`\xD7${o.length} ${n.title.replace(/\s*\((?:\d+ rows?, )?\d+ms\)$/,"")}`,n.detail=`${n.sig} \xD7${o.length} ${a?"\u2016 parallel":"\u21BB sequential loop"} \xB7 avg ${n.avgMs}ms \xB7 wall ${n.wallMs}ms`,n.items=o.slice(0,12).map(c=>({start:c.start-$,...c.durationMs!=null?{durationMs:c.durationMs}:{}})),n.memberIds=o.flatMap(c=>c.memberIds??[c.id]),o.some(c=>c.severity==="error")&&(n.severity="error");for(const c of o.slice(1))e.set(c.id,n.id)}if(e.size!==0){h=h.filter(o=>!e.has(o.id));for(const o of h)for(;o.parent&&e.has(o.parent);)o.parent=e.get(o.parent)}},"aggregatePass");yt(!0),yt(!1);for(const t of h){if(!I(t))continue;const r=t.durationMs??0;if(r<40)continue;const e=h.filter(k=>k.parent===t.id&&(k.durationMs??0)>0).map(k=>[k.start,k.start+(k.durationMs??0)]).sort((k,u)=>k[0]-u[0]);let o=0,n=-1,a=-1;for(const[k,u]of e)k>a?(a>n&&(o+=a-n),n=k,a=u):u>a&&(a=u);a>n&&(o+=a-n);const $=Math.max(0,r-o);($>=50||$/r>=.7&&r>=40)&&(t.selfMs=$,t.detail=`\u26A0 cpu-heavy: ${$}ms self-time of ${r}ms${t.detail?" \xB7 "+t.detail:""}`)}return{events:h,...s?{sql:s}:{}}}R(buildFlowDetail,"buildFlowDetail"),P(buildFlowDetail,"buildFlowDetail"),w(buildFlowDetail,"buildFlowDetail");
|
|
1
|
+
"use strict";var St=Object.defineProperty;var P=(i,g)=>St(i,"name",{value:g,configurable:!0});var Bt=Object.defineProperty,v=P((i,g)=>Bt(i,"name",{value:g,configurable:!0}),"y");import{redactDeep as At,redactText as at,redactUrlParams as Rt}from"./redact.js";import{templatisePath as $t}from"./route-template.js";import{SQL_ACCOUNT_KIND as Tt,sqlAccountingOf as Pt,stallsAfterResponse as Dt}from"./flow-segments.js";import{isServiceHopHost as yt}from"./service-hop.js";import{boundedClone as Ct}from"@lensmcp/protocol-types";export function areaOf(i){const{source:g,category:s,title:I,kind:l}=i;return g==="ai"?"ai":g==="perf"&&l==="event-loop-stall"?"services":l?.startsWith("memory")||l==="db-query"?"data":g==="gateway"||l==="gateway-request"?"gateway":l==="queue-process"||l==="queue-active"||l==="queue-completed"||l==="queue-failed"?"services":g==="db"||g==="redis"||g==="queue"||l==="redis-op"||l?.startsWith("queue-")||g==="fs"||l==="fs-ops"?"data":g==="exec"||l==="exec"||l==="egress"&&yt(i.host)?"services":g==="external"||l==="egress"?"network":g==="nestjs"||g==="nest"?/(?:Repository|Repo|Dao|Store|EntityManager|DataSource|QueryRunner|OrmClient)\./.test(I)?"data":"services":g==="valtio"?"state":s==="network"?"network":"browser"}P(areaOf,"areaOf"),v(areaOf,"areaOf");const vt=240,Nt=new Set(["systemPrompt","prompt","prompts","messages","completion","completions","input","output","draft","text","content","result","arguments","params"]),Ft=new Set(["tenantId","caller","userId","accountId","email","subject"]),Ot=8;function kt(i){const g=rt(i,0);return Ct(At({redacted:!0,...g}),{rawByteLimit:16*1024}).value}P(kt,"Ee"),v(kt,"aiProjection");function rt(i,g){if(typeof i=="string")return wt(i);if(i===null||typeof i!="object")return i;if(g>=Ot)return;if(Array.isArray(i))return i.slice(0,32).map(I=>rt(I,g+1));const s={};for(const[I,l]of Object.entries(i))if(!Ft.has(I)){if(Nt.has(I)){typeof l=="string"?s[`${I}Chars`]=l.length:Array.isArray(l)?s[`${I}Count`]=l.length:l!=null&&(s[`${I}Present`]=!0);continue}s[I]=rt(l,g+1)}return s}P(rt,"ae"),v(rt,"projectAi");function wt(i){return i.length<=vt?at(i):`${at(i.slice(0,vt))}\u2026`}P(wt,"Ne"),v(wt,"clipText");export function httpFacts(i,g){const s=v(q=>typeof q=="string"&&q.length>0?q:void 0,"str"),I=typeof g.status=="number"?g.status:void 0,l=v(q=>Rt(q.split("?")[0]??q)||"/","pathOf"),N=v((q,b,k={})=>({...q?{method:q.toUpperCase()}:{},...b?{rawPath:b,route:$t(b)}:{},...I!==void 0?{status:I}:{},...k}),"facts");if(i==="gateway-request"){const q=s(g.url);return N(s(g.method),q?l(q):void 0,{...s(g.host)?{host:s(g.host)}:{},...s(g.caller)?{caller:s(g.caller)}:{}})}if(i==="egress"){const q=s(g.path),b=s(g.exchangeId);return N(s(g.method)??"GET",q?l(q):void 0,{...s(g.host)?{host:s(g.host)}:{},...b?{exchangeId:b}:{}})}if(i==="fetch-start"||i==="fetch-done"||i==="fetch-error"){const q=s(g.href)??s(g.url);let b,k;if(q)try{const p=new URL(q,"http://relative.invalid");b=l(p.pathname),p.hostname!=="relative.invalid"&&(k=p.hostname)}catch{}return N(s(g.method)??"GET",b,k?{host:k}:{})}if(i==="ai-call")return{...I!==void 0?{status:I}:{},...g.stream===!0?{streamed:!0}:{}};const f=g.request;if(f&&typeof f=="object"){const q=f,b=s(q.route),k=s(q.method);if(!b&&!k)return{};const p=typeof q.status=="number"?q.status:void 0,F=b?l(b):void 0;return{...k?{method:k.toUpperCase()}:{},...F?{rawPath:F,route:F.includes(":")?F:$t(F)}:{},...p!==void 0?{status:p}:{}}}return{}}P(httpFacts,"httpFacts"),v(httpFacts,"httpFacts");export function trimFlowEvent(i,g={}){const s=i.raw??{},I=s.render??{},l=s.span??{},N=s.request??{},f=v(d=>typeof d=="number"?d:void 0,"num"),q=s.loop??{},b=s.effect??{},k=f(s.durationMs)??f(l.durationMs)??f(N.durationMs)??f(q.durationMs)??f(I.actualDurationMs)??f(b.durationMs),p=typeof s.kind=="string"?s.kind:void 0,F=p==="fetch-done"||p==="fetch-error",E=f(I.commitTime),X=p==="render"&&E!==void 0&&Math.abs(E-i.timestamp)<6e4,_=F?i.timestamp:X?E:f(s.startedAt)??f(l.startTime)??f(N.startedAt)??f(q.startedAt)??(k!==void 0?i.timestamp-k:i.timestamp);let x;if(typeof s.query=="string"){const d=Array.isArray(s.params)?` [${s.params.join(", ")}]`:"",m=typeof s.result=="string"?` \u2192 ${s.result}`:"";x=s.query+d+m}else if(typeof s.command=="string"){const d=typeof s.value=="string"?` \u2192 ${s.value}`:s.hit===!1?" \u2192 (miss)":"";x=s.command+d}else if(p==="state-update"){const d=s.update??{};let m="";try{m=JSON.stringify(d.preview)}catch{}x=`${d.storeId??""}.${d.path??""} = ${m}`}else if(p==="fs-ops"&&Array.isArray(s.paths))x=s.paths.slice(0,8).map(d=>{const m=d??{},B=typeof m.n=="number"&&m.n>1?` \xD7${m.n}`:"",T=typeof m.bytes=="number"&&m.bytes>0?` (${m.bytes>=1024?`${(m.bytes/1024).toFixed(1)} KB`:`${m.bytes} B`})`:"";return`${String(m.op??"?")} ${String(m.path??"?")}${B}${T}`}).join(" \xB7 ");else if(p==="exec"&&typeof s.cmd=="string")x=`${s.cmd} \u2192 ${s.code??s.signal??s.error??"?"}`;else if(p==="queue-completed"&&typeof s.returned=="string")x=`returned ${s.returned}`;else if(p==="queue-enqueued")x=`${s.job??"job"}#${s.jobId??"?"} \u2192 ${s.queue??""}${typeof s.data=="string"?" "+s.data:""}`;else if(p==="log"){const d=N.error??{},m=typeof d.stack=="string"?d.stack:typeof s.text=="string"?s.text:void 0;m&&m!==i.title&&(x=m)}let L,z,U,V;if(p==="span"){const d=l.outcome;if(L=d==="threw"||d==="returned"?d:l.status==="error"?"threw":l.status==="ok"?"returned":void 0,L==="threw"){const T=l.error??{},y=typeof T.name=="string"?T.name:void 0,G=typeof T.message=="string"?T.message:typeof i.message=="string"?i.message:void 0;U=y,z=at([y,G].filter(Boolean).join(": "))||void 0}const m=typeof l.args=="string"?l.args:void 0,B=typeof l.result=="string"?l.result:void 0;if(m!==void 0||B!==void 0){V=[...m!==void 0?["args"]:[],...B!==void 0?["result"]:[]];const T=v(y=>y.length>Mt?`${y.slice(0,Mt)}\u2026`:y,"short");x=[m!==void 0?`args ${T(m)}`:"",B!==void 0?`returned ${T(B)}`:""].filter(Boolean).join(" \u2192 ")}}let W;if(p==="gateway-request")W=`${s.host??""}${String(s.url??"").split("?")[0]}`;else if(p==="fetch-start"||p==="fetch-done"||p==="fetch-error")try{const d=new URL(String(s.href??s.url??""));W=`${d.hostname}${d.pathname}`}catch{}let K;if(p==="gateway-request"&&Array.isArray(s.steps))K=s.steps.map(d=>{const m=[String(d.step)];return d.project&&m.push(String(d.project)),d.prefix&&m.push(String(d.prefix)),d.mode&&m.push(String(d.mode)+(d.ok===!1?" \u2717":d.ok===!0?" \u2713":"")),d.caller&&m.push("by "+String(d.caller)),d.pod&&m.push(String(d.pod)),typeof d.atMs=="number"&&m.push(`@${d.atMs}ms`),m.join(" ")}),x=K.map(d=>d.replace(/ @\d+ms$/,"")).join(" \u2192 ");else if(p==="egress")x=`${s.method??"GET"} ${s.host??""} \u2192 ${s.status??"?"}`;else if(i.source==="ai"){if(p==="ai-call"){const d=s.usage??{},m=d.promptTokens!==void 0||d.completionTokens!==void 0?` \xB7 ${f(d.promptTokens)??"?"}\u2192${f(d.completionTokens)??"?"} tok`:"",B=s.status!==void 0?` \xB7 ${String(s.status)}`:s.error?" \xB7 failed":"",T=f(s.toolCallCount)?` \xB7 ${f(s.toolCallCount)} tool call${f(s.toolCallCount)===1?"":"s"}`:"",y=f(s.durationMs)!==void 0?` \xB7 ${(f(s.durationMs)/1e3).toFixed(1)}s`:"";x=`${s.provider??""}/${s.model??"?"}${m}${B}${T}${y}${s.stream===!0?" \xB7 stream":""}`}else if(p==="ai-round"){const d=s.model??{},m=s.usage??{},B=Array.isArray(s.toolCalls)?s.toolCalls:[],T=`${f(m.promptTokens)??"?"}\u2192${f(m.completionTokens)??"?"} tok`,y=B.length?` \xB7 ${B.length} tool call${B.length===1?"":"s"}`:"",G=f(s.durationMs)!==void 0?` \xB7 ${(f(s.durationMs)/1e3).toFixed(1)}s`:"";x=`${d.provider??""}/${d.model??""} \xB7 ${T}${y}${G}`}else if(p==="ai-turn-start"){const d=Array.isArray(s.tools)?s.tools:[];x=`skill ${s.skill??""} \xB7 ${s.output??""}${d.length?" \xB7 tools: "+d.join(", "):""}`}else if(p==="ai-tool-call"){const d=s.call??{},m=s.result??{},B=f(s.durationMs)!==void 0?` \xB7 ${(f(s.durationMs)/1e3).toFixed(1)}s`:"";x=`${d.name??"tool"} ${m.error!=null?"\u2717 "+String(m.error):"\u2713"}${B}`}else if(p==="ai-turn-end"){const d=s.usage??{},m=f(s.rounds)!==void 0?` \xB7 ${f(s.rounds)} round${f(s.rounds)===1?"":"s"}`:"",B=f(s.durationMs)!==void 0?` \xB7 ${(f(s.durationMs)/1e3).toFixed(1)}s`:"";x=`${s.ok===!1?"FAILED":"ok"} \xB7 ${f(d.promptTokens)??"?"}+${f(d.completionTokens)??"?"} tok${m}${B}`}}let R;p==="db-query"&&typeof s.query=="string"?R=s.query:p==="redis-op"?R=`${s.op??""} ${String(s.key??"").replace(/\d+/g,"#")}`:p==="egress"?R=`${s.method??"GET"} ${s.host??""}`:p==="ai-call"?R=`ai ${s.provider??""} ${s.model??""} ${s.api??""}`:p==="queue-enqueued"?R=`enqueue ${s.queue??""}`:p==="span"&&typeof l.name=="string"?R=l.name:p==="memory-mutation"&&(R=i.title);const O=i.context??{},Q=I.commitId??b.commitId??s.commitId,Y=Array.isArray(I.why)?I.why.map(d=>typeof d?.type=="string"?d.type:void 0).filter(d=>!!d):[];return{id:i.id,t:i.timestamp,start:_,...R?{sig:R}:{},...W?{urlKey:W}:{},...httpFacts(p,s),...(p==="db-query"||p==="redis-op")&&typeof s.valuesId=="string"?{valuesId:s.valuesId}:{},...typeof s.queue=="string"?{queue:s.queue}:{},...s.jobId!=null?{jobId:String(s.jobId)}:{},source:i.source,category:i.category,severity:i.severity,title:L==="threw"&&typeof l.name=="string"?`${l.name} \u2014 threw${U?` ${U}`:""}${k!==void 0?` after ${k}ms`:""}`:i.title,kind:p,area:areaOf({source:i.source,category:i.category,title:i.title,kind:p,...typeof s.host=="string"?{host:s.host}:{}}),...p==="egress"&&yt(typeof s.host=="string"?s.host:void 0)?{hop:!0}:{},...p==="egress"&&typeof s.calleeRequestId=="string"?{calleeRequestId:s.calleeRequestId}:{},...L?{outcome:L}:{},...z?{error:z}:{},...V?{captured:V}:{},...typeof l.role=="string"?{role:l.role}:{},...typeof l.spanId=="string"?{spanId:l.spanId}:{},...typeof l.parentSpanId=="string"?{parentSpanId:l.parentSpanId}:{},...p==="log"&&typeof s.level=="string"?{level:s.level}:{},...p==="log"&&typeof s.logger=="string"?{logger:s.logger}:{},...p==="log"&&typeof s.stream=="string"?{stream:s.stream}:{},...typeof O.requestId=="string"?{requestId:O.requestId}:{},...typeof O.parentRequestId=="string"?{parentRequestId:O.parentRequestId}:{},...typeof Q=="string"?{commitId:Q}:{},...typeof I.componentName=="string"?{component:I.componentName}:{},...typeof I.phase=="string"?{phase:I.phase}:{},...Y.length?{why:Y}:{},...p==="effect-run"&&typeof b.hookLogicalId=="string"?{hook:b.hookLogicalId}:{},...p==="effect-run"&&b.cleanupRan===!0?{cleanup:!0}:{},...p==="render-commit-truncated"?{renderTotal:f(s.total),renderPublished:f(s.published),...f(s.walkStoppedAt)!==void 0?{renderWalkStoppedAt:f(s.walkStoppedAt)}:{}}:{},...typeof O.projectName=="string"?{project:O.projectName}:typeof s.project=="string"?{project:s.project}:{},...k!==void 0?{durationMs:k}:{},...F&&f(s.startedAt)!==void 0?{fetchStartedAt:f(s.startedAt)}:{},...i.source==="ai"?{ai:g.includeRaw?s:kt(s)}:{},...x?{detail:x}:{},...K?{steps:K}:{}}}P(trimFlowEvent,"trimFlowEvent"),v(trimFlowEvent,"trimFlowEvent");const Mt=300;export const OUTLIVED_PARENT_MIN_MS=250;const Et=new Set(["egress","db-query","redis-op","ai-call","span","gateway-request","server-request"]),it=1;function qt(i){return i==="flow-origin"?0:i?.endsWith("-start")?1:i==="gateway-request"?2:i==="server-request"?3:i==="span"?4:i==="render"||i==="render-commit-truncated"?6:i==="effect-run"?7:5}P(qt,"_e"),v(qt,"kindRank");function J(i){return i.kind==="span"&&i.role==="controller"?3.5:i.kind==="queue-process"?3:qt(i.kind)}P(J,"X"),v(J,"causalRank");export function buildFlowDetail(i,g={}){const s=Pt(i),I=Dt(i),l=i.slice().filter(t=>{const r=t.raw??{},e=typeof r.kind=="string"?r.kind:"";if(e==="subscription-added"||e==="subscription-removed"||e==="hook-run"||e==="store-created"||e==="singleton-instance"||e===Tt)return!1;const o=r.span;return!(typeof o?.name=="string"&&o.name.startsWith("nest.request "))}).map(t=>trimFlowEvent(t,g)),N=new Set;for(let t=l.length-1;t>=0;t--)N.has(l[t].id)?l.splice(t,1):N.add(l[t].id);const f=new Map;for(const t of l)(t.kind==="gateway-request"||t.kind==="queue-process")&&t.requestId&&!f.has(t.requestId)&&f.set(t.requestId,t.start);const q=v(t=>t.source!=="react"&&t.source!=="valtio"&&t.requestId&&f.has(t.requestId)?f.get(t.requestId):t.start,"anchorOf");l.sort((t,r)=>q(t)-q(r)||t.start-r.start||J(t)-J(r)||(r.durationMs??0)-(t.durationMs??0)||t.t-r.t);const b=v(t=>(t.kind==="span"||t.kind==="server-request"||t.kind==="gateway-request"||t.kind==="queue-process")&&(t.durationMs??0)>0,"spanLike"),k=l;for(const t of k)t.group=t.source==="react"||t.source==="valtio"||t.source==="client-runtime"||t.source==="visual"?"browser":"server";const p=new Map;for(const t of k)if((t.kind==="gateway-request"||t.kind==="queue-process")&&t.requestId&&t.project){const r=p.get(t.requestId)??[];r.push({project:t.project,start:t.start,end:t.start+(t.durationMs??0)}),p.set(t.requestId,r)}const F=v((t,r)=>{const e=p.get(t);if(e?.length)return e.every(o=>o.project===e[0].project)?e[0].project:e.filter(o=>o.start<=r+1&&o.end>=r-1).sort((o,n)=>n.start-o.start)[0]?.project},"projectOf");for(const t of k)if(!t.project&&t.source==="nestjs"&&t.requestId){const r=F(t.requestId,t.start);r&&(t.project=r)}const E=new Map;for(const t of k)t.spanId&&E.set(t.spanId,t);const X=v((t,r)=>{let e=t.parentSpanId?E.get(t.parentSpanId):void 0;for(let o=0;e&&o<64;o++){if(e===r)return!0;e=e.parentSpanId?E.get(e.parentSpanId):void 0}return!1},"inside"),_=new Map(k.map((t,r)=>[t,r])),x=new Map,L=v(t=>{let r=x.get(t.group);r||x.set(t.group,r=new Map);const e=t.requestId;let o=r.get(e);return o||r.set(e,o={spans:[],next:0,alive:[]}),o},"legOf");for(const t of k)b(t)&&Number.isFinite(t.start)&&L(t).spans.push(t);for(const t of x.values())for(const r of t.values())r.spans.sort((e,o)=>e.start-o.start||_.get(e)-_.get(o));const z=v(t=>Number.isFinite(t.start)?t.start:1/0,"startOf");for(const t of k.slice().sort((r,e)=>z(r)-z(e))){const r=t.parentSpanId?E.get(t.parentSpanId):void 0;if(r&&r!==t&&r.group===t.group&&r.requestId===t.requestId){t.parent=r.id;continue}if(!Number.isFinite(t.start))continue;const e=L(t);for(;e.next<e.spans.length&&e.spans[e.next].start<=t.start+it;)e.alive.push(e.spans[e.next++]);e.alive=e.alive.filter(a=>a.start+(a.durationMs??0)>t.start);const o=e.alive.length>1?e.alive.slice().sort((a,h)=>_.get(a)-_.get(h)):e.alive;let n;for(const a of o){if(a===t)continue;const h=a.project,w=t.project;if(a.kind!=="gateway-request"&&t.kind!=="gateway-request"&&h!==void 0&&w!==void 0&&h!==w)continue;const u=a.durationMs??0,c=t.durationMs??0,$=Math.abs(a.start-t.start)<=it,M=J(a),A=J(t),j=a.start<=t.start;if(!(a.start+u>t.start&&(j||$&&M<A)&&(!$||(M!==A?M<A:j&&u>c))))continue;const C=n?J(n):-1,D=n?Math.abs(a.start-n.start)<=it:!1,H=n&&D&&M===C?X(a,n)?1:X(n,a)?-1:0:0;(!n||!D&&a.start>n.start||D&&M>C||H>0||H===0&&D&&M===C&&(a.durationMs??0)<(n.durationMs??0))&&(n=a)}n&&(t.parent=n.id)}for(const t of k)delete t.spanId,delete t.parentSpanId;{const t=new Set(k.map(a=>a.id)),r=new Set,e=new Map,o=[],n=v(a=>{const h=[a];for(;h.length;){const w=h.pop();o.push(w),r.add(w.id);const u=e.get(w.id);if(u){e.delete(w.id);for(let c=u.length-1;c>=0;c--)h.push(u[c])}}},"place");for(const a of k)if(a.parent&&a.parent!==a.id&&t.has(a.parent)&&!r.has(a.parent)){const h=e.get(a.parent)??[];h.push(a),e.set(a.parent,h)}else n(a);for(const a of k)r.has(a.id)||(o.push(a),r.add(a.id));for(let a=0;a<o.length;a++)k[a]=o[a]}const U=new Map;for(const t of k){if(!t.requestId)continue;const r=U.get(t.requestId);r?r.push(t):U.set(t.requestId,[t])}const V=new Map(k.map(t=>[t.id,t])),W=new Map;for(const t of k)t.calleeRequestId&&!W.has(t.calleeRequestId)&&W.set(t.calleeRequestId,t);const K=v((t,r)=>{const e=(U.get(t)??[]).filter(o=>b(o)&&o.start<=r&&o.start+(o.durationMs??0)>=r);if(e.length!==0)return e.sort((o,n)=>n.start-o.start||(o.durationMs??0)-(n.durationMs??0)),e[0]},"callerNodeFor");for(const t of k){if(!t.parentRequestId||t.kind!=="server-request"&&t.kind!=="gateway-request")continue;const r=t.parent?V.get(t.parent):void 0;if(r&&r.requestId===t.requestId)continue;const e=t.requestId?W.get(t.requestId):void 0,o=e&&e.start<=t.start?e:K(t.parentRequestId,t.start);o&&o!==t&&o.start<=t.start&&(t.parent=o.id)}const R=k,O=new Map;for(const t of R)t.kind==="gateway-request"&&t.requestId&&!O.has(t.requestId)&&O.set(t.requestId,t);for(const t of R)if(t.group==="server"&&!t.parent&&t.requestId&&t.kind!=="gateway-request"){const r=O.get(t.requestId);r&&r!==t&&r.start<=t.start&&(t.parent=r.id)}const Q=new Set,Y=new Set,d=[];for(const t of R){if(t.kind!=="gateway-request")continue;if(!t.requestId){d.push(t);continue}const r=U.get(t.requestId)??[];if(r.filter(n=>n.kind==="gateway-request").sort((n,a)=>n.start-a.start)[0]!==t)continue;const e=r.find(n=>n.kind==="fetch-start"),o=r.find(n=>n.kind==="fetch-done"||n.kind==="fetch-error");if(!e&&!o){d.push(t);continue}e&&(t.fetchStartId=e.id,Q.add(e.id)),o&&(t.fetchDoneId=o.id,Y.add(o.id))}const m=new Map;for(const t of R){if(!t.urlKey)continue;const r=m.get(t.urlKey)??{gws:[],starts:[],dones:[]};t.kind==="gateway-request"?d.includes(t)&&r.gws.push(t):t.kind==="fetch-start"?Q.has(t.id)||r.starts.push(t):(t.kind==="fetch-done"||t.kind==="fetch-error")&&(Y.has(t.id)||r.dones.push(t)),m.set(t.urlKey,r)}for(const{gws:t,starts:r,dones:e}of m.values()){t.sort((n,a)=>n.start-a.start),r.sort((n,a)=>n.start-a.start);const o=new Set;t.forEach((n,a)=>{const h=r[a];h&&(n.fetchStartId=h.id);const w=h?h.t:n.start;let u,c=1/0;for(const $ of e){if(o.has($.id))continue;const M=$.fetchStartedAt??$.start-($.durationMs??0),A=Math.abs(M-w);A<c&&(c=A,u=$)}u&&(n.fetchDoneId=u.id,o.add(u.id))})}const B=new Map,T=new Map;for(const t of R)!t.queue||!t.jobId||(t.kind==="queue-enqueued"&&B.set(`${t.queue}#${t.jobId}`,t),t.kind==="queue-process"&&T.set(`${t.queue}#${t.jobId}`,t));for(const t of R){if(!t.queue||!t.jobId)continue;const r=`${t.queue}#${t.jobId}`;if(t.kind==="queue-process"&&!t.parent){const e=B.get(r);e&&e!==t&&e.start<=t.start&&(t.parent=e.id)}else if(t.kind==="queue-active"||t.kind==="queue-completed"||t.kind==="queue-failed"){const e=T.get(r);e&&e!==t&&e.start<=t.start&&(t.parent=e.id)}}{const t=new Map(k.map(r=>[r.id,r]));for(const r of k){if(!r.parent||!Et.has(r.kind??"")||r.durationMs===void 0)continue;const e=t.get(r.parent);if(!e||e.kind!=="span"||!((e.durationMs??0)>0))continue;const o=Math.round(r.start+r.durationMs-(e.start+e.durationMs));if(o<OUTLIVED_PARENT_MIN_MS)continue;r.outlivedParentMs=o;const n=e.leftRunning??{count:0,longestMs:0};e.leftRunning={count:n.count+1,longestMs:Math.max(n.longestMs,o)}}for(const r of k){const e=[];r.leftRunning&&e.push(`ended before ${r.leftRunning.count===1?"a call":`${r.leftRunning.count} calls`} it started had finished \u2014 ${r.leftRunning.count===1?"it":"the longest"} ran on ${r.leftRunning.longestMs}ms (a timeout that gave up, or work not awaited)`),r.outlivedParentMs!==void 0&&e.push(`ran on ${r.outlivedParentMs}ms after the span that started it had ended`),e.length>0&&(r.detail=[r.detail,...e].filter(Boolean).join(" \xB7 "))}}if(I.size>0)for(const t of k){const r=I.get(t.id);r!==void 0&&(t.afterResponseMs=r,t.detail=[t.detail,`began ${r}ms after the request's response had finished \u2014 not part of the request's time`].filter(Boolean).join(" \xB7 "))}let y=k;{const t=new Map;for(const e of y)e.kind==="fetch-start"&&t.set(e.id,e);const r=new Map;for(const e of y){if(e.kind!=="fetch-done"&&e.kind!=="fetch-error")continue;const o=[...t.values()].find(h=>h.requestId&&h.requestId===e.requestId)?.id,n=o?t.get(o):void 0;if(!n)continue;const a=e.fetchStartedAt??n.start;n.title=e.title,n.severity=e.severity,n.durationMs=e.durationMs??Math.max(0,e.t-a),n.kind="fetch",e.detail&&!n.detail&&(n.detail=e.detail),r.set(e.id,n.id)}if(r.size>0){y=y.filter(e=>!r.has(e.id));for(const e of y)for(e.fetchDoneId&&r.has(e.fetchDoneId)&&(e.fetchDoneId=r.get(e.fetchDoneId));e.parent&&r.has(e.parent);)e.parent=r.get(e.parent)}}const G=v(t=>!!t.commitId&&(t.kind==="render"||t.kind==="effect-run"||t.kind==="render-commit-truncated"),"commitMember"),st=v(t=>[...new Set(t)],"uniq"),ot=v(t=>st(t.map(r=>r.component).filter(r=>!!r)),"named"),It=v(t=>t.length<=2?t.join(" + "):`${t[0]} + ${t.length-1} more`,"nameList"),dt=v(t=>{const r=new Map;for(const e of t)e.component&&r.set(e.component,(r.get(e.component)??0)+1);return[...r].map(([e,o])=>o>1?`${e} \xD7${o}`:e).join(", ")},"tally"),bt=v(t=>{if(!t)return"effect";const r=/([^/\\]+?):(\d+)(?::\d+)?$/.exec(t);return r?`${r[1]}:${r[2]}`:t},"shortHook"),nt=v(t=>`${bt(t.hook)}${t.cleanup?" (cleanup)":""}`,"hookLabel"),ut=v(t=>{const r=Math.min(...t.map(o=>o.start)),e=Math.max(...t.map(o=>o.start+(o.durationMs??0)));return{start:r,durationMs:e-r}},"extent"),ct=v(t=>(delete t.component,delete t.why,delete t.phase,delete t.hook,delete t.cleanup,delete t.renderTotal,delete t.renderPublished,t),"asGroup");function lt(t){const r=t[0],e=t.filter(S=>S.kind==="render"),o=t.filter(S=>S.kind==="effect-run"),n=t.find(S=>S.kind==="render-commit-truncated"),a=[...e].sort((S,Z)=>(Z.durationMs??0)-(S.durationMs??0)),h=a.filter(S=>(S.why??[]).some(Z=>Z!=="parent-render")),w=st(h.flatMap(S=>(S.why??[]).filter(Z=>Z!=="parent-render"))),u=ot(h),c=e.filter(S=>S.phase==="mount"),$=e.filter(S=>S.phase!=="mount"&&!h.includes(S)),M=a[0]?.component??"view",A=e.length>0&&c.length===e.length,j=w.length?w.join(" + "):void 0,C=A?"mount":"update",D=u.length?`view update \xB7 ${It(u)} \u2190 ${j}`:A?`view mount \xB7 ${M}`:`view update \xB7 ${M}`,H=n?.renderTotal??e.length,jt=[c.length?`mounted ${dt(c)}`:"",$.length?`re-rendered ${dt($)}`:"",!u.length&&!A&&e.length?"trigger not recorded \u2014 every captured render came from its parent":"",n?.renderWalkStoppedAt!==void 0?`at least ${H} renders (the walk stopped at ${n.renderWalkStoppedAt} fibers), ${n.renderPublished??e.length} recorded`:n?`${H} renders, ${n.renderPublished??e.length} recorded (per-commit cap)`:`${e.length} render${e.length===1?"":"s"}`,o.length?`${o.length} effect${o.length===1?"":"s"}: ${o.map(nt).join(", ")}`:""].filter(Boolean).join(" \xB7 "),ht=ot(e);return ct({...r,...ut(t),kind:"view-update",title:D,detail:jt,commitId:r.commitId,viewPhase:C,...j?{cause:j}:{},causeComponents:u,components:ht,mounted:ot(c),renderCount:H,...n?.renderWalkStoppedAt!==void 0?{renderCountAtLeast:!0}:{},recordedRenders:e.length,effects:o.length,effectHooks:o.map(nt),memberIds:t.map(S=>S.id),sig:`commit:${C}:${j??"-"}:${u.join(",")}:${ht.slice().sort().join(",")}`,severity:t.some(S=>S.severity==="error")?"error":r.severity})}P(lt,"Me"),v(lt,"viewUpdateNode");function pt(t){const r=t[0],e=t.map(nt);return ct({...r,...ut(t),kind:"commit-effects",title:t.length===1?`effect \xB7 ${e[0]}`:`effects \xB7 ${e[0]} + ${t.length-1} more`,detail:e.join(", "),commitId:r.commitId,effects:t.length,effectHooks:e,memberIds:t.map(o=>o.id),sig:`effects:${e.slice().sort().join(",")}`})}P(pt,"ke"),v(pt,"effectsNode");const xt=v(t=>st(t.map(r=>r==="state-update"?"state update":r==="fetch-start"?"request":r==="flow-origin"?"interaction":"response")).join(" + "),"describeInputs");function ft(t,r){const e=t[0],o=t[t.length-1],n=t.length,a=e.detail,h=Math.min(...t.map($=>$.start)),w=Math.max(...t.map($=>$.start+($.durationMs??0))),u=Math.round(t.reduce(($,M)=>$+(M.durationMs??0),0)/n*10)/10,c=Math.round((w-h)*10)/10;e.count=n,e.avgMs=u,e.wallMs=c,e.durationMs=w-h,e.items=t.slice(0,12).map($=>({start:$.start-h,...$.durationMs!=null?{durationMs:$.durationMs}:{}})),e.memberIds=t.flatMap($=>$.memberIds??[$.id]),e.title=`\xD7${n} ${e.title}`,t.some($=>$.severity==="error")&&(e.severity="error"),e.kind==="view-update"?e.detail=[`${n} commits, ${r.length?`one per ${xt(r)}`:"back to back"}`,`avg ${u}ms \xB7 wall ${c}ms`,a??""].filter(Boolean).join(" \xB7 "):e.kind==="state-update"&&a&&o.detail?e.detail=`${n} updates in a row \xB7 wall ${c}ms \xB7 first ${a} \xB7 last ${o.detail}`:e.detail=[`${n} in a row \xB7 wall ${c}ms`,a??""].filter(Boolean).join(" \xB7 ")}P(ft,"we"),v(ft,"collapseRepeat");const tt=[],et=new Map,mt=new Set;for(let t=0;t<y.length;t++){if(mt.has(t))continue;const r=y[t];if(!G(r)){tt.push(r);continue}const e=[t];for(let a=t+1;a<y.length;a++){const h=y[a];if(h.group==="browser"){if(!G(h)||h.commitId!==r.commitId)break;e.push(a)}}for(const a of e)mt.add(a);const o=e.map(a=>y[a]),n=o.some(a=>a.kind!=="effect-run")?lt(o):pt(o);for(const a of o)a.id!==n.id&&et.set(a.id,n.id);tt.push(n)}if(et.size>0||tt.some(t=>t.kind==="view-update"||t.kind==="commit-effects")){y=tt;for(const t of y)for(;t.parent&&et.has(t.parent);)t.parent=et.get(t.parent)}{const t=new Map;for(const r of y)r.kind==="view-update"&&r.commitId&&!t.has(r.commitId)&&t.set(r.commitId,r);for(const r of y){if(r.kind!=="commit-effects"||r.parent||!r.commitId)continue;const e=t.get(r.commitId);e&&e.start<=r.start&&(r.parent=e.id)}}{const t=new Set(["state-update","fetch","fetch-start","fetch-done","fetch-error","flow-origin"]),r=y.map((u,c)=>u.group==="browser"?c:-1).filter(u=>u>=0),e=v(u=>`${u.kind}|${u.sig??u.title}`,"keyOf"),o=r.map(u=>e(y[u])),n=v((u,c,$)=>{for(let M=0;M<$;M++)if(o[u+M]!==o[c+M])return!1;return!0},"sameBlock"),a=v(u=>o[u].split("|")[0],"kindAt"),h=v(u=>{let c=0,$=1;for(let M=1;M<=4&&u+M<=o.length;M++){let A=1;for(;u+(A+1)*M<=o.length&&n(u,u+A*M,M);)A+=1;A>=3&&M*A>c*$&&(c=M,$=A)}return{k:c,r:$}},"bestBlockAt"),w=new Map;for(let u=0;u<o.length;){const{k:c,r:$}=h(u);if(c===0){u+=1;continue}const M=[...Array(c).keys()].find(j=>t.has(a(u+j)));if(M!==void 0&&M>0){const j=h(u+M);if(j.k===c&&j.r>=3&&j.r>=$-1){u+=M;continue}}const A=o.slice(u,u+c).map(j=>j.split("|")[0]).filter(j=>t.has(j));for(let j=0;j<c;j++){const C=[];for(let D=0;D<$;D++)C.push(y[r[u+j+D*c]]);ft(C,A);for(const D of C.slice(1))w.set(D.id,C[0].id)}u+=c*$}if(w.size>0){y=y.filter(u=>!w.has(u.id));for(const u of y)for(;u.parent&&w.has(u.parent);)u.parent=w.get(u.parent)}}{const t=new Map(y.map(r=>[r.id,r]));for(const r of y){const e=new Set([r.id]);let o=r;for(;o?.parent;){if(e.has(o.parent)){o.parent=void 0;break}e.add(o.parent),o=t.get(o.parent)}}}const gt=v(t=>{const r=new Map;for(const o of y){if(!o.sig||b(o)!==t||o.kind==="server-request"||o.kind==="gateway-request"||o.group==="browser")continue;const n=`${o.parent??"root"}|${o.kind}|${o.sig}`,a=r.get(n);a?a.push(o):r.set(n,[o])}const e=new Map;for(const o of r.values()){if(o.length<3)continue;o.sort((c,$)=>c.start-$.start);const n=o[0];let a=!1;for(let c=0;c+1<o.length;c++)if(o[c+1].start<o[c].start+(o[c].durationMs??0)){a=!0;break}const h=n.start,w=Math.max(...o.map(c=>c.start+(c.durationMs??0))),u=o.reduce((c,$)=>c+($.durationMs??0),0);n.count=o.length,n.parallel=a,n.avgMs=Math.round(u/o.length*10)/10,n.wallMs=w-h,n.durationMs=w-h,n.title=`\xD7${o.length} ${n.title.replace(/\s*\((?:\d+ rows?, )?\d+ms\)$/,"")}`,n.detail=`${n.sig} \xD7${o.length} ${a?"\u2016 parallel":"\u21BB sequential loop"} \xB7 avg ${n.avgMs}ms \xB7 wall ${n.wallMs}ms`,n.items=o.slice(0,12).map(c=>({start:c.start-h,...c.durationMs!=null?{durationMs:c.durationMs}:{}})),n.memberIds=o.flatMap(c=>c.memberIds??[c.id]),o.some(c=>c.severity==="error")&&(n.severity="error");for(const c of o.slice(1))e.set(c.id,n.id)}if(e.size!==0){y=y.filter(o=>!e.has(o.id));for(const o of y)for(;o.parent&&e.has(o.parent);)o.parent=e.get(o.parent)}},"aggregatePass");gt(!0),gt(!1);for(const t of y){if(!b(t))continue;const r=t.durationMs??0;if(r<40)continue;const e=y.filter(w=>w.parent===t.id&&(w.durationMs??0)>0).map(w=>[w.start,w.start+(w.durationMs??0)]).sort((w,u)=>w[0]-u[0]);let o=0,n=-1,a=-1;for(const[w,u]of e)w>a?(a>n&&(o+=a-n),n=w,a=u):u>a&&(a=u);a>n&&(o+=a-n);const h=Math.max(0,r-o);(h>=50||h/r>=.7&&r>=40)&&(t.selfMs=h,t.detail=`\u26A0 cpu-heavy: ${h}ms self-time of ${r}ms${t.detail?" \xB7 "+t.detail:""}`)}return{events:y,...s?{sql:s}:{}}}P(buildFlowDetail,"buildFlowDetail"),v(buildFlowDetail,"buildFlowDetail");
|
package/lib/flows-config.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var b=Object.defineProperty;var c=(s,e)=>b(s,"name",{value:e,configurable:!0});var y=Object.defineProperty,l=c((s,e)=>y(s,"name",{value:e,configurable:!0}),"u");import{classifyFlow as $,categoryOfSegment as h}from"./flow-categories.js";import{DEFAULT_SEGMENT_OPTIONS as w,segmentFlow as j}from"./flow-segments.js";export const DEFAULT_FLOWS_CONFIG={categories:[],segments:{},problems:[]};const p=200,f=200,m=64,F=/^[A-Za-z0-9][A-Za-z0-9 ._:/-]*$/,O=["category","flowId","originType","origin","route","project"],A={phaseGapMs:[50,6e5],burstGapMs:[10,6e4],attachWindowMs:[0,6e4],maxSegmentEvents:[20,1e4],occurrenceSlackMs:[0,1e3]},g=l(s=>typeof s=="object"&&s!==null&&!Array.isArray(s),"isObj");function d(s,e){if(!g(s))return{problems:[`${e}: not an object`]};const o=[];for(const t of Object.keys(s))O.includes(t)||o.push(`${e}: unknown field "${t}" ignored`);const n=s.category;if(typeof n!="string"||n.trim()==="")return{problems:[...o,`${e}: "category" must be a non-empty string \u2014 rule dropped`]};const i=n.trim();if(i.length>m||!F.test(i))return{problems:[...o,`${e}: category "${i.slice(0,40)}" is not a usable name (letters, digits, space . _ : / -; up to ${m}) \u2014 rule dropped`]};const u={category:i};let a=0;for(const t of["flowId","originType","origin","route","project"]){const r=s[t];if(r!==void 0){if(typeof r!="string"||r===""||r.length>f)return{problems:[...o,`${e}: "${t}" must be a non-empty string of up to ${f} characters \u2014 rule dropped`]};u[t]=r,a+=1}}return a===0?{problems:[...o,`${e}: "${i}" has no match field (flowId, originType, origin, route or project) \u2014 rule dropped`]}:{rule:u,problems:o}}c(d,"w"),l(d,"parseRule");export function parseFlowsConfig(s){if(s==null)return DEFAULT_FLOWS_CONFIG;if(!g(s))return{...DEFAULT_FLOWS_CONFIG,problems:["lensmcp.flows must be an object \u2014 ignored"]};const e=[],o=[],n=s.categories;n!==void 0&&(Array.isArray(n)?(n.length>p&&e.push(`flows.categories has ${n.length} rules; only the first ${p} are used`),n.slice(0,p).forEach((a,t)=>{const r=d(a,`flows.categories[${t}]`);e.push(...r.problems),r.rule&&o.push(r.rule)})):e.push("flows.categories must be an array \u2014 ignored"));const i={},u=s.segments;if(u!==void 0)if(!g(u))e.push("flows.segments must be an object \u2014 ignored");else for(const[a,t]of Object.entries(u)){const r=A[a];if(!r){e.push(`flows.segments.${a}: unknown option ignored`);continue}if(typeof t!="number"||!Number.isFinite(t)||t<r[0]||t>r[1]){e.push(`flows.segments.${a}: ${JSON.stringify(t)} is outside ${r[0]}\u2013${r[1]}; using the default (${w[a]})`);continue}i[a]=t}return{categories:o,segments:i,problems:e}}c(parseFlowsConfig,"parseFlowsConfig"),l(parseFlowsConfig,"parseFlowsConfig");export function analyzeFlow(s,e=DEFAULT_FLOWS_CONFIG){const o=j(s,e.segments),n=$(s,o,e.categories),i={};for(const u of o.segments)i[u.id]=h(u,n,e.categories);return{segmentation:o,classification:n,segmentCategories:i}}c(analyzeFlow,"analyzeFlow"),l(analyzeFlow,"analyzeFlow");
|
package/lib/graph-from-events.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var g=Object.defineProperty;var l=(t,r)=>g(t,"name",{value:r,configurable:!0});var I=Object.defineProperty,p=l((t,r)=>I(t,"name",{value:r,configurable:!0}),"l");function m(t){const r=t.raw??null,s=r?.kind,d=t.context,a={id:t.id,parentTraceNodeId:d.causedByNodeId??d.originNodeId,lifecycle:"completed",context:d,startTime:t.timestamp,attributes:{...r??{}}};if(t.source==="react"&&s==="render"){const e=r;return{parentAxis:"render",node:{...a,type:"render",logicalId:e.render?.componentLogicalId,instanceId:e.render?.componentInstanceId,name:`render ${e.render?.componentName??"?"}`,durationMs:e.render?.actualDurationMs}}}if(t.source==="valtio"&&s==="state-update"){const e=r;return{parentAxis:"state",node:{...a,type:"state-update",logicalId:e.update?.storeId,name:`${e.update?.storeId??"?"}.${e.update?.path??"?"}`}}}if(t.source==="nestjs"&&s==="server-request"){const e=r;return{parentAxis:"backend",node:{...a,type:"server-request",name:`${e.request?.method??"?"} ${e.request?.route??"?"}`,durationMs:e.request?.durationMs}}}if(s==="db-query"){const e=r,c=[e.op,e.table].filter(Boolean).join(" ")||(e.name??"query");return{parentAxis:"backend",node:{...a,type:"db-query",logicalId:e.table?`db:${e.op??"query"}:${e.table}`:void 0,name:e.error?`${c} (failed)`:c,durationMs:e.durationMs}}}if(s==="loop")return{parentAxis:"performance",node:{...a,type:"loop",name:"loop",durationMs:r.loop?.durationMs}};if(s==="flow-origin"&&(t.source==="react"||t.source==="client-runtime")){const e=d.originNodeId??(typeof r?.originNodeId=="string"?r.originNodeId:void 0);return{parentAxis:"caused",node:{...a,parentTraceNodeId:d.causedByNodeId,type:"ui-event",logicalId:e,name:e?`${t.title} ${e}`:t.title}}}if(t.source==="client-runtime"&&s==="user-action"){const e=r;return{parentAxis:"caused",node:{...a,type:"ui-event",logicalId:e.origin?.nodeId,name:e.origin?.label??t.title}}}}l(m,"m"),p(m,"eventToNode");export function buildGraphFromEvents(t,r){const s=[...r].sort((o,n)=>o.timestamp-n.timestamp),d=[];for(const o of s){const n=m(o);n&&(t.addNode(n.node),d.push(n))}let a=0;const e=p((o,n,i,u)=>{const f={id:`${i}:${o}->${n}`,from:o,to:n,axis:i,type:u,timestamp:t.getNode(n)?.startTime??0};t.addEdge(f),a++},"addEdge");for(const o of d){const n=o.node.context.causedByNodeId;n&&t.getNode(n)&&e(n,o.node.id,o.parentAxis??"caused","caused-by")}const c=new Map;for(const o of d){const n=o.node.context.flowId;if(!n)continue;let i=c.get(n);i||(i=[],c.set(n,i)),i.push(o)}for(const o of c.values())for(let n=1;n<o.length;n++){const i=o[n-1].node,u=o[n].node;u.context.causedByNodeId!==i.id&&e(i.id,u.id,"caused","flow-sequence")}return{nodeCount:d.length,edgeCount:a}}l(buildGraphFromEvents,"buildGraphFromEvents"),p(buildGraphFromEvents,"buildGraphFromEvents");
|