@lensmcp/core 1.25.57 → 1.25.59
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/resource-store.d.ts +24 -2
- package/lib/resource-store.js +1 -1
- package/package.json +2 -2
package/lib/resource-store.d.ts
CHANGED
|
@@ -34,6 +34,16 @@ export interface ResourceStoreOptions {
|
|
|
34
34
|
* says which resource it was rebuilding (issues/closed/overseer-12). Begin a span; call what it returns at the end.
|
|
35
35
|
*/
|
|
36
36
|
work?: (what: string) => () => void;
|
|
37
|
+
/**
|
|
38
|
+
* Recompute a resource on its marks only while someone subscribes to it (false, the default: on every mark). A mark
|
|
39
|
+
* of one no one subscribes to only marks it stale; the next read that produces it recomputes it — revision bump and
|
|
40
|
+
* notification included, as a mark would have — and a first subscriber to a stale one gets its recompute at once.
|
|
41
|
+
* FoodGuard's dashboard, 2026-11-03: it subscribes to 25 resources, yet every mark of the others was produced and
|
|
42
|
+
* serialised for no one — catalog://sql, routes://lint, db://lint, the event-loop stalls, and kv://lint and
|
|
43
|
+
* queues://lint with a Redis census each — ~20 s of event-loop holds that day, ~35 s the day before (catalog://sql
|
|
44
|
+
* alone 22.7 s).
|
|
45
|
+
*/
|
|
46
|
+
lazy?: boolean;
|
|
37
47
|
}
|
|
38
48
|
/**
|
|
39
49
|
* The resource store. URIs are registered once; reading is cheap; the
|
|
@@ -52,6 +62,7 @@ export declare class ResourceStore {
|
|
|
52
62
|
/** What the last recompute of each URI took (produce + serialise), in ms. */
|
|
53
63
|
private readonly lastCost;
|
|
54
64
|
private readonly work;
|
|
65
|
+
private readonly lazy;
|
|
55
66
|
constructor(opts?: ResourceStoreOptions);
|
|
56
67
|
register(uri: string, produce: ResourceProducer): Unsubscribe;
|
|
57
68
|
read(uri: string): Promise<ResourceValue>;
|
|
@@ -59,20 +70,31 @@ export declare class ResourceStore {
|
|
|
59
70
|
* The resource as produced at most `maxAgeMs` ago — by its own recompute or a read — else produced now. For a reader
|
|
60
71
|
* that can take it that old and reads it as part of its own recompute: `agent://current-status` reads ~13 signal
|
|
61
72
|
* resources each time, and `read` produced each whole again (routes lint, the SQL lint…) — 2.6 GB of allocation a
|
|
62
|
-
* minute on FoodGuard's traffic (foodguard-72). The value is shared: never changed in place.
|
|
73
|
+
* minute on FoodGuard's traffic (foodguard-72). The value is shared: never changed in place. A stale resource (`lazy`)
|
|
74
|
+
* too is handed as produced within `maxAgeMs`, and recomputed past it.
|
|
63
75
|
*/
|
|
64
76
|
readRecent(uri: string, maxAgeMs: number): Promise<ResourceValue>;
|
|
77
|
+
/**
|
|
78
|
+
* What the resource produced last — by a recompute or a read — whatever its age; undefined before the first. Never
|
|
79
|
+
* produces it: for a reader that must not start the producer's work, and takes the last value it settled on —
|
|
80
|
+
* `agent://current-status` reading `kv://lint`, whose producer runs a Redis census. Shared: never changed in place.
|
|
81
|
+
*/
|
|
82
|
+
latest(uri: string): ResourceValue | undefined;
|
|
83
|
+
/** Marked since its last recompute, with no one subscribed (`lazy`): its revision waits for the next read. */
|
|
84
|
+
isStale(uri: string): boolean;
|
|
65
85
|
list(): string[];
|
|
66
86
|
/**
|
|
67
87
|
* Recompute the JSON, bump the revision counter if it differs, and
|
|
68
88
|
* notify subscribers. No-op if the URI is unknown or the JSON didn't
|
|
69
|
-
* change.
|
|
89
|
+
* change. In a `lazy` store, a resource no one subscribes to is only
|
|
90
|
+
* marked stale: its next read recomputes it.
|
|
70
91
|
*/
|
|
71
92
|
markDirty(uri: string): Promise<void>;
|
|
72
93
|
/** How long after its last recompute a URI may be recomputed again. */
|
|
73
94
|
private gapFor;
|
|
74
95
|
/** What the last recompute of a URI took, in ms (produce + serialise) — undefined before the first. */
|
|
75
96
|
cost(uri: string): number | undefined;
|
|
97
|
+
/** Produce it, and — when its JSON changed — bump its revision and notify its subscribers. Resolves to what it produced. */
|
|
76
98
|
private recompute;
|
|
77
99
|
revision(uri: string): number | undefined;
|
|
78
100
|
subscribe(uri: string, handler: ResourceUpdatedHandler): Unsubscribe;
|
package/lib/resource-store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var d=Object.defineProperty;var u=(o,t)=>d(o,"name",{value:t,configurable:!0});var w=Object.defineProperty,l=u((o,t)=>w(o,"name",{value:t,configurable:!0}),"c");export class ResourceStore{static{u(this,"ResourceStore")}static{l(this,"ResourceStore")}constructor(t={}){this.entries=new Map,this.subs=new Map,this.queued=new Map,this.lastRun=new Map,this.lastCost=new Map,this.coalesceMs=Math.max(0,t.coalesceMs??0),this.costFactor=Math.max(0,t.costFactor??0),this.maxGapMs=Math.max(0,t.maxGapMs??3e4),this.work=t.work,this.lazy=t.lazy===!0}register(t,e){if(this.entries.has(t))throw new Error(`Resource already registered: ${t}`);return this.entries.set(t,{uri:t,produce:e,revision:0}),()=>{this.entries.delete(t),this.subs.delete(t),this.lastRun.delete(t),this.lastCost.delete(t)}}async read(t){const e=this.entries.get(t);if(!e)throw new Error(`Unknown resource: ${t}`);if(e.stale)return this.recompute(t);const s=await e.produce();return e.produced={at:Date.now(),value:s},s}async readRecent(t,e){const s=this.entries.get(t);if(!s)throw new Error(`Unknown resource: ${t}`);return s.produced&&Date.now()-s.produced.at<=e?s.produced.value:this.read(t)}latest(t){const e=this.entries.get(t);if(!e)throw new Error(`Unknown resource: ${t}`);return e.produced?.value}isStale(t){return this.entries.get(t)?.stale===!0}list(){return[...this.entries.keys()]}markDirty(t){const e=this.entries.get(t);if(e&&this.lazy&&!this.subs.get(t)?.size)return e.stale=!0,Promise.resolve();if(this.coalesceMs===0&&this.costFactor===0)return this.recompute(t).then(()=>{});if(!e)return Promise.resolve();const s=this.queued.get(t);if(s)return s;const n=Math.max(0,(this.lastRun.get(t)??Number.NEGATIVE_INFINITY)+this.gapFor(t)-Date.now()),a=new Promise(r=>{const i=l(()=>{this.queued.delete(t),this.lastRun.set(t,Date.now()),this.recompute(t).then(()=>r(),()=>r())},"go");n===0?setImmediate(i):setTimeout(i,n).unref?.()});return this.queued.set(t,a),a}gapFor(t){const e=this.lastCost.get(t)??0;return Math.max(this.coalesceMs,Math.min(this.maxGapMs,this.costFactor*e))}cost(t){return this.lastCost.get(t)}async recompute(t){const e=this.entries.get(t);if(!e)return;const s=performance.now(),n=this.work?.(`recompute ${t}`),a=e.stale===!0;e.stale=!1;let r,i;try{r=await e.produce(),e.produced={at:Date.now(),value:r},i=JSON.stringify(r)}catch(c){throw a&&(e.stale=!0),c}finally{n?.()}if(this.lastCost.set(t,performance.now()-s),i===e.lastJson)return r;e.lastJson=i,e.revision+=1;const h=this.subs.get(t);if(h)for(const c of h)try{c(t,e.revision)}catch{}return r}revision(t){return this.entries.get(t)?.revision}subscribe(t,e){let s=this.subs.get(t);return s||(s=new Set,this.subs.set(t,s)),s.add(e),this.entries.get(t)?.stale&&this.markDirty(t).catch(()=>{}),()=>{s?.delete(e)}}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lensmcp/core",
|
|
3
|
-
"version": "1.25.
|
|
3
|
+
"version": "1.25.59",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -94,7 +94,7 @@
|
|
|
94
94
|
}
|
|
95
95
|
},
|
|
96
96
|
"dependencies": {
|
|
97
|
-
"@lensmcp/protocol-types": "1.25.
|
|
97
|
+
"@lensmcp/protocol-types": "1.25.59",
|
|
98
98
|
"tslib": "^2.3.0"
|
|
99
99
|
},
|
|
100
100
|
"license": "Apache-2.0",
|