dsh-agent-control-center 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +116 -0
- package/README.md +217 -83
- package/README.zh.md +159 -58
- package/client.js +7660 -2005
- package/cordis.patch.yml +5 -0
- package/docs/01-CONTRACT.md +3 -2
- package/docs/05-ACCEPTANCE-EVIDENCE.md +81 -0
- package/docs/06-UX-OPTIMIZATION.md +57 -0
- package/docs/07-GUIDE.md +327 -0
- package/docs/08-COMPATIBILITY.md +202 -0
- package/docs/09-ARCHITECTURE.md +333 -0
- package/docs/ROADMAP.md +448 -0
- package/index.js +67 -2
- package/lib/core/cache.js +277 -0
- package/lib/core/compat.js +877 -0
- package/lib/core/errors.js +14 -1
- package/lib/core/http.js +22 -0
- package/lib/core/index.js +53 -2
- package/lib/core/paths.js +6 -0
- package/lib/core/registry.js +21 -5
- package/lib/core/util.js +4 -1
- package/lib/modules/a-agents.js +104 -4
- package/lib/modules/b-tools.js +169 -26
- package/lib/modules/f-diagnostics.js +109 -17
- package/lib/modules/g-templates.js +18 -0
- package/lib/modules/i-recovery.js +84 -23
- package/lib/modules/j-instruction.js +624 -0
- package/lib/modules/k-export.js +540 -0
- package/package.json +1 -1
- package/scripts/release.mjs +81 -0
- package/scripts/verify.mjs +12 -0
- package/src/client/app.js +684 -118
- package/src/client/i18n/agents.js +96 -0
- package/src/client/i18n/common.js +36 -0
- package/src/client/i18n/diagnostics.js +52 -0
- package/src/client/i18n/export.js +174 -0
- package/src/client/i18n/instruction.js +236 -0
- package/src/client/i18n/meta.js +72 -0
- package/src/client/i18n/overview.js +38 -0
- package/src/client/i18n/recovery.js +66 -0
- package/src/client/i18n/sessions.js +64 -0
- package/src/client/i18n/templates.js +88 -0
- package/src/client/i18n/tools.js +96 -0
- package/src/client/i18n/usage.js +42 -0
- package/src/client/kernel.js +327 -40
- package/src/client/pages/agents.js +637 -354
- package/src/client/pages/diagnostics.js +526 -177
- package/src/client/pages/export.js +664 -0
- package/src/client/pages/instruction.js +648 -0
- package/src/client/pages/meta.js +553 -193
- package/src/client/pages/overview.js +255 -34
- package/src/client/pages/recovery.js +332 -131
- package/src/client/pages/sessions.js +470 -307
- package/src/client/pages/templates.js +691 -381
- package/src/client/pages/tools.js +559 -206
- package/src/client/pages/usage.js +235 -57
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read-through cache for expensive, read-only derivations.
|
|
3
|
+
*
|
|
4
|
+
* Guarantees:
|
|
5
|
+
* - an entry is fresh until `ttlMs` after it was stored and is evicted
|
|
6
|
+
* least-recently-used once `maxEntries` is exceeded;
|
|
7
|
+
* - concurrent `resolve()` calls for one key share a single factory promise, so
|
|
8
|
+
* overlapping page loads never run the same scan twice;
|
|
9
|
+
* - a rejected factory is never stored, so the next caller retries;
|
|
10
|
+
* - `resolve()` returns `{ value, cached, ageMs }` and never mutates the stored
|
|
11
|
+
* value, so callers can add response-only fields on a copy.
|
|
12
|
+
*
|
|
13
|
+
* A `ttlMs` of 0 disables storing (every call runs the factory); it is the
|
|
14
|
+
* deployment escape hatch for a cache that must never serve stale data.
|
|
15
|
+
*
|
|
16
|
+
* @module dsh-agent-control-center/core/cache
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Fallback entry lifetime for modules that do not pass one. */
|
|
20
|
+
export const DEFAULT_TTL_MS = 30_000
|
|
21
|
+
/** Fallback LRU bound. Keys are one per parameter combination, never unbounded. */
|
|
22
|
+
export const DEFAULT_MAX_ENTRIES = 32
|
|
23
|
+
/** Hard cap so a misconfiguration cannot pin memory for an hour. */
|
|
24
|
+
const MAX_TTL_MS = 3_600_000
|
|
25
|
+
/** Hard cap on the LRU bound. */
|
|
26
|
+
const MAX_ENTRIES_CAP = 1000
|
|
27
|
+
/** Delay before a start-up warm-up begins: activation and route mounting finish first. */
|
|
28
|
+
export const WARMUP_DELAY_MS = 200
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {unknown} value
|
|
32
|
+
* @param {number} fallback
|
|
33
|
+
* @param {number} min
|
|
34
|
+
* @param {number} max
|
|
35
|
+
* @returns {number}
|
|
36
|
+
*/
|
|
37
|
+
function clampInt(value, fallback, min, max) {
|
|
38
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return fallback
|
|
39
|
+
return Math.min(max, Math.max(min, Math.trunc(value)))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolve one module's cache lifetime from the plugin config.
|
|
44
|
+
*
|
|
45
|
+
* `config.cacheTtlMs` may be a plain number (every cached module) or an object
|
|
46
|
+
* keyed by module id (`{ tools: 30000, recovery: 60000 }`), which lets an
|
|
47
|
+
* operator tune the cached modules separately. 0 disables caching.
|
|
48
|
+
*
|
|
49
|
+
* @param {Record<string, any> | undefined} config plugin row config
|
|
50
|
+
* @param {string} moduleId module id the object form is keyed by
|
|
51
|
+
* @param {number} fallback module default when the config carries no value
|
|
52
|
+
* @returns {number} a value in [0, 3600000]
|
|
53
|
+
*/
|
|
54
|
+
export function resolveCacheTtlMs(config, moduleId, fallback) {
|
|
55
|
+
const raw = config === undefined || config === null ? undefined : config.cacheTtlMs
|
|
56
|
+
/** @type {unknown} */
|
|
57
|
+
let value
|
|
58
|
+
if (typeof raw === 'number') value = raw
|
|
59
|
+
else if (raw !== null && typeof raw === 'object' && typeof (/** @type {Record<string, unknown>} */ (raw))[moduleId] === 'number') {
|
|
60
|
+
value = /** @type {Record<string, unknown>} */ (raw)[moduleId]
|
|
61
|
+
}
|
|
62
|
+
if (typeof value !== 'number') return clampInt(fallback, DEFAULT_TTL_MS, 0, MAX_TTL_MS)
|
|
63
|
+
return clampInt(value, fallback, 0, MAX_TTL_MS)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Copy a resolved value into a response, adding only the two additive cache
|
|
68
|
+
* fields. The stored value is never mutated, so a cache hit cannot leak one
|
|
69
|
+
* caller's decoration into the next.
|
|
70
|
+
* @param {{ value: any, cached: boolean, ageMs: number }} resolved
|
|
71
|
+
* @returns {any}
|
|
72
|
+
*/
|
|
73
|
+
export function withCacheMeta(resolved) {
|
|
74
|
+
const value = resolved.value
|
|
75
|
+
const ageMs = Math.max(0, Math.trunc(typeof resolved.ageMs === 'number' ? resolved.ageMs : 0))
|
|
76
|
+
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
77
|
+
return { ...value, cached: resolved.cached === true, cacheAgeMs: ageMs }
|
|
78
|
+
}
|
|
79
|
+
return { value, cached: resolved.cached === true, cacheAgeMs: ageMs }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Run a start-up warm-up after activation without blocking it and without
|
|
84
|
+
* keeping the process alive. The task owns its error handling: a failed warm-up
|
|
85
|
+
* must log and leave the cache cold, never surface as an unhandled rejection.
|
|
86
|
+
* @param {() => void | Promise<void>} task
|
|
87
|
+
* @param {number} [delayMs] defaults to {@link WARMUP_DELAY_MS}; 0 runs next tick
|
|
88
|
+
* @returns {void}
|
|
89
|
+
*/
|
|
90
|
+
export function scheduleWarmup(task, delayMs = WARMUP_DELAY_MS) {
|
|
91
|
+
const timer = setTimeout(() => {
|
|
92
|
+
void Promise.resolve().then(task)
|
|
93
|
+
}, Math.max(0, Math.trunc(typeof delayMs === 'number' && Number.isFinite(delayMs) ? delayMs : WARMUP_DELAY_MS)))
|
|
94
|
+
if (timer !== null && typeof timer === 'object' && typeof timer.unref === 'function') timer.unref()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @typedef {{ value: any, storedAt: number, expiresAt: number }} CacheEntry
|
|
99
|
+
* @typedef {{ ttlMs?: number, maxEntries?: number, now?: () => number, label?: string }} CacheOptions
|
|
100
|
+
* @typedef {{ force?: boolean, ttlMs?: number, shouldCache?: (value: any) => boolean }} ResolveOptions
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Create one bounded cache instance.
|
|
105
|
+
* @param {CacheOptions} [options]
|
|
106
|
+
* @returns {{
|
|
107
|
+
* get: (key: string) => any,
|
|
108
|
+
* set: (key: string, value: any) => any,
|
|
109
|
+
* delete: (key: string) => boolean,
|
|
110
|
+
* clear: () => void,
|
|
111
|
+
* stats: () => Record<string, any>,
|
|
112
|
+
* resolve: (key: string, factory: () => Promise<any> | any, options?: ResolveOptions) => Promise<{ value: any, cached: boolean, ageMs: number }>,
|
|
113
|
+
* }}
|
|
114
|
+
*/
|
|
115
|
+
export function createCache(options = {}) {
|
|
116
|
+
const ttlMs = clampInt(options.ttlMs, DEFAULT_TTL_MS, 0, MAX_TTL_MS)
|
|
117
|
+
const maxEntries = clampInt(options.maxEntries, DEFAULT_MAX_ENTRIES, 1, MAX_ENTRIES_CAP)
|
|
118
|
+
const now = typeof options.now === 'function' ? options.now : Date.now
|
|
119
|
+
const label = typeof options.label === 'string' && options.label.length > 0 ? options.label : 'cache'
|
|
120
|
+
/** @type {Map<string, CacheEntry>} */
|
|
121
|
+
const entries = new Map()
|
|
122
|
+
/** @type {Map<string, Promise<{ value: any, cached: boolean, ageMs: number }>>} */
|
|
123
|
+
const inflight = new Map()
|
|
124
|
+
/** Per-key invalidation counter: a computation started before a delete must not repopulate. */
|
|
125
|
+
/** @type {Map<string, number>} */
|
|
126
|
+
const keyEpoch = new Map()
|
|
127
|
+
/** Bumped by clear(); invalidates every in-flight computation. */
|
|
128
|
+
let generation = 0
|
|
129
|
+
const counters = { hits: 0, misses: 0, coalesced: 0, factoryRuns: 0, errors: 0, evictions: 0 }
|
|
130
|
+
|
|
131
|
+
/** Drop expired entries (bounded scan: the map is small by construction). */
|
|
132
|
+
function pruneExpired() {
|
|
133
|
+
const at = now()
|
|
134
|
+
for (const [key, entry] of entries) if (entry.expiresAt <= at) entries.delete(key)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Fresh entry or undefined, moving a hit to the most-recently-used position.
|
|
139
|
+
* @param {string} key
|
|
140
|
+
* @returns {CacheEntry | undefined}
|
|
141
|
+
*/
|
|
142
|
+
function entryOf(key) {
|
|
143
|
+
const entry = entries.get(key)
|
|
144
|
+
if (entry === undefined) return undefined
|
|
145
|
+
if (entry.expiresAt <= now()) {
|
|
146
|
+
entries.delete(key)
|
|
147
|
+
return undefined
|
|
148
|
+
}
|
|
149
|
+
entries.delete(key)
|
|
150
|
+
entries.set(key, entry)
|
|
151
|
+
return entry
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** @param {string} key @returns {any} */
|
|
155
|
+
function get(key) {
|
|
156
|
+
const entry = entryOf(key)
|
|
157
|
+
if (entry === undefined) {
|
|
158
|
+
counters.misses += 1
|
|
159
|
+
return undefined
|
|
160
|
+
}
|
|
161
|
+
counters.hits += 1
|
|
162
|
+
return entry.value
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param {string} key
|
|
167
|
+
* @param {any} value
|
|
168
|
+
* @param {number} [entryTtlMs] per-entry lifetime override
|
|
169
|
+
* @returns {any} the stored value
|
|
170
|
+
*/
|
|
171
|
+
function set(key, value, entryTtlMs) {
|
|
172
|
+
const effectiveTtl = entryTtlMs === undefined ? ttlMs : clampInt(entryTtlMs, ttlMs, 0, MAX_TTL_MS)
|
|
173
|
+
if (effectiveTtl <= 0) return value
|
|
174
|
+
pruneExpired()
|
|
175
|
+
const storedAt = now()
|
|
176
|
+
entries.delete(key)
|
|
177
|
+
entries.set(key, { value, storedAt, expiresAt: storedAt + effectiveTtl })
|
|
178
|
+
while (entries.size > maxEntries) {
|
|
179
|
+
const oldest = entries.keys().next()
|
|
180
|
+
if (oldest.done === true) break
|
|
181
|
+
entries.delete(oldest.value)
|
|
182
|
+
counters.evictions += 1
|
|
183
|
+
}
|
|
184
|
+
return value
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** @param {string} key @returns {boolean} */
|
|
188
|
+
function deleteEntry(key) {
|
|
189
|
+
keyEpoch.set(key, (keyEpoch.get(key) ?? 0) + 1)
|
|
190
|
+
return entries.delete(key)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function clear() {
|
|
194
|
+
generation += 1
|
|
195
|
+
entries.clear()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** @returns {Record<string, any>} */
|
|
199
|
+
function stats() {
|
|
200
|
+
return {
|
|
201
|
+
label,
|
|
202
|
+
entries: entries.size,
|
|
203
|
+
hits: counters.hits,
|
|
204
|
+
misses: counters.misses,
|
|
205
|
+
coalesced: counters.coalesced,
|
|
206
|
+
factoryRuns: counters.factoryRuns,
|
|
207
|
+
errors: counters.errors,
|
|
208
|
+
evictions: counters.evictions,
|
|
209
|
+
inflight: inflight.size,
|
|
210
|
+
ttlMs,
|
|
211
|
+
maxEntries,
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Read through the cache, coalescing concurrent calls for the same key.
|
|
217
|
+
* @param {string} key
|
|
218
|
+
* @param {() => Promise<any> | any} factory produces the value on a miss
|
|
219
|
+
* @param {ResolveOptions} [resolveOptions]
|
|
220
|
+
* @returns {Promise<{ value: any, cached: boolean, ageMs: number }>}
|
|
221
|
+
*/
|
|
222
|
+
async function resolve(key, factory, resolveOptions = {}) {
|
|
223
|
+
const force = resolveOptions.force === true
|
|
224
|
+
if (!force) {
|
|
225
|
+
const entry = entryOf(key)
|
|
226
|
+
if (entry !== undefined) {
|
|
227
|
+
counters.hits += 1
|
|
228
|
+
return { value: entry.value, cached: true, ageMs: Math.max(0, now() - entry.storedAt) }
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const pending = inflight.get(key)
|
|
232
|
+
if (pending !== undefined) {
|
|
233
|
+
counters.misses += 1
|
|
234
|
+
counters.coalesced += 1
|
|
235
|
+
return pending
|
|
236
|
+
}
|
|
237
|
+
counters.misses += 1
|
|
238
|
+
counters.factoryRuns += 1
|
|
239
|
+
const startedGeneration = generation
|
|
240
|
+
const startedEpoch = keyEpoch.get(key) ?? 0
|
|
241
|
+
/** @type {{ resolve: (value: { value: any, cached: boolean, ageMs: number }) => void, reject: (error: unknown) => void }} */
|
|
242
|
+
let settle
|
|
243
|
+
/** @type {Promise<{ value: any, cached: boolean, ageMs: number }>} */
|
|
244
|
+
const shared = new Promise((resolve, reject) => {
|
|
245
|
+
settle = { resolve, reject }
|
|
246
|
+
})
|
|
247
|
+
// Register before scheduling the factory: a synchronously throwing factory
|
|
248
|
+
// must not leave a stale in-flight promise behind.
|
|
249
|
+
inflight.set(key, shared)
|
|
250
|
+
void Promise.resolve()
|
|
251
|
+
.then(() => factory())
|
|
252
|
+
.then((value) => {
|
|
253
|
+
let cacheable = true
|
|
254
|
+
if (typeof resolveOptions.shouldCache === 'function') {
|
|
255
|
+
try {
|
|
256
|
+
cacheable = resolveOptions.shouldCache(value) !== false
|
|
257
|
+
} catch {
|
|
258
|
+
// A predicate failure means "do not cache", never a lost result.
|
|
259
|
+
cacheable = false
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (cacheable && startedGeneration === generation && startedEpoch === (keyEpoch.get(key) ?? 0)) {
|
|
263
|
+
set(key, value, resolveOptions.ttlMs)
|
|
264
|
+
}
|
|
265
|
+
settle.resolve({ value, cached: false, ageMs: 0 })
|
|
266
|
+
}, (error) => {
|
|
267
|
+
counters.errors += 1
|
|
268
|
+
settle.reject(error)
|
|
269
|
+
})
|
|
270
|
+
.finally(() => {
|
|
271
|
+
if (inflight.get(key) === shared) inflight.delete(key)
|
|
272
|
+
})
|
|
273
|
+
return shared
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return { get, set, delete: deleteEntry, clear, stats, resolve }
|
|
277
|
+
}
|