dsh-my-observability 0.1.3 → 0.1.4

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 CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  本文件记录 dsh-my-observability 的所有版本变更。格式遵循 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
4
4
 
5
+ ## [0.1.4] - 2026-09-03
6
+
7
+ ### 变更
8
+
9
+ - feat(observability): #R17 资源监控看板——写放大/资源超限提前发现
10
+
5
11
  ## [0.1.3] - 2026-09-03
6
12
 
7
13
  ### 变更
package/README.md CHANGED
@@ -26,6 +26,7 @@ Server 端只读观察 DSH 生命周期事件并记录审计日志:
26
26
  - **重启恢复**:持久化到 `$DSH_HOME/observability/audit.jsonl`(**增量追加** + 防抖批量 flush + 周期 compact 原子快照),重启后完整恢复;升级前旧格式 `audit.json` 自动迁移;
27
27
  - **防膨胀**:每会话最多 2000 条(FIFO 淘汰)、全局 20000 条(轮转淘汰);
28
28
  - **零写放大**:落盘只写新增事件(≈事件本体字节),不会因事件流持续而反复全量重写审计文件;
29
+ - **资源监控**:面板「资源」区块展示本进程 CPU/内存 + 审计文件大小与写入速率(15s 采样),写放大/超限自动告警(阈值见 `resource-budget-review`);
29
30
  - **只读观察**:waterfall 事件一律透传 `next()`,绝不改变工具/模型流程。
30
31
 
31
32
  ### 2. 轨迹回放面板(时间轴)
package/lib/client.js CHANGED
@@ -42,6 +42,12 @@ function isZh() {
42
42
 
43
43
  const strings = {
44
44
  replayTitle: () => (isZh() ? '轨迹回放' : 'Trajectory'),
45
+ resourceTitle: () => (isZh() ? '资源监控' : 'Resources'),
46
+ resourceLoading: () => (isZh() ? '资源采样中…' : 'Sampling…'),
47
+ resourceFile: () => (isZh() ? '审计文件' : 'Audit file'),
48
+ resourceRate: () => (isZh() ? '写入速率' : 'Write rate'),
49
+ resourceCpu: () => (isZh() ? 'CPU' : 'CPU'),
50
+ resourceMem: () => (isZh() ? '内存' : 'Memory'),
45
51
  gitTitle: () => (isZh() ? 'Git 工具' : 'Git Tools'),
46
52
  allSessions: () => (isZh() ? '全部会话' : 'All sessions'),
47
53
  filterAll: () => (isZh() ? '全部' : 'All'),
@@ -986,6 +992,7 @@ function ReplayPanel(props) {
986
992
  return createElement(
987
993
  'div',
988
994
  { className: 'dsh-my-observability-panel' },
995
+ createElement(ResourcePanel, { resource: s.resource }),
989
996
  createElement(ReplayToolbar, {
990
997
  sessions: s.sessions,
991
998
  selected: s.selected,
@@ -1026,6 +1033,78 @@ function ReplayPanel(props) {
1026
1033
  )
1027
1034
  }
1028
1035
 
1036
+ // ── 资源监控区块(写放大/资源超限预警,见 lib/resource-monitor.js)──────
1037
+ // 依赖 replay.js 先拼接(apiJson)与 i18n.js(strings)。纯函数声明文本。
1038
+
1039
+ const RESOURCE_POLL_MS = 15000
1040
+
1041
+ function fmtResourceBytes(bytes) {
1042
+ if (!Number.isFinite(bytes)) return '-'
1043
+ return `${(bytes / 1048576).toFixed(1)} MB`
1044
+ }
1045
+
1046
+ /** 资源采样状态:可见时每 15s 轮询 /observability/api/resources。 */
1047
+ function useResourceState(visible) {
1048
+ const [resource, setResource] = useState(null)
1049
+ useEffect(() => {
1050
+ if (!visible) return undefined
1051
+ let alive = true
1052
+ const tick = () => {
1053
+ if (alive) apiJson('/observability/api/resources').then(setResource).catch(() => {})
1054
+ }
1055
+ tick()
1056
+ const timer = setInterval(tick, RESOURCE_POLL_MS)
1057
+ return () => {
1058
+ alive = false
1059
+ clearInterval(timer)
1060
+ }
1061
+ }, [visible])
1062
+ return resource
1063
+ }
1064
+
1065
+ function ResourceMetric({ label, value }) {
1066
+ return createElement(
1067
+ 'div',
1068
+ { className: 'dsh-my-observability-resource-metric' },
1069
+ createElement('span', { className: 'dsh-my-observability-resource-label' }, label),
1070
+ createElement('span', { className: 'dsh-my-observability-resource-value' }, value),
1071
+ )
1072
+ }
1073
+
1074
+ /** 资源面板:四指标 + 告警列表(write-rate/file-size level=error 红色,cpu/memory warn 黄色)。 */
1075
+ function ResourcePanel({ resource }) {
1076
+ if (resource === null || resource === undefined) {
1077
+ return createElement('div', { className: 'dsh-my-observability-resource' }, strings.resourceLoading())
1078
+ }
1079
+ const alerts = Array.isArray(resource.alerts) ? resource.alerts : []
1080
+ return createElement(
1081
+ 'div',
1082
+ { className: 'dsh-my-observability-resource' },
1083
+ createElement('div', { className: 'dsh-my-observability-resource-head' }, strings.resourceTitle()),
1084
+ createElement(
1085
+ 'div',
1086
+ { className: 'dsh-my-observability-resource-grid' },
1087
+ createElement(ResourceMetric, { label: strings.resourceFile(), value: fmtResourceBytes(resource.fileBytes) }),
1088
+ createElement(ResourceMetric, { label: strings.resourceRate(), value: `${fmtResourceBytes(resource.writeRateBytesPerHour)}/h` }),
1089
+ createElement(ResourceMetric, { label: strings.resourceCpu(), value: `${Math.round(resource.cpuPercent ?? 0)}%` }),
1090
+ createElement(ResourceMetric, { label: strings.resourceMem(), value: fmtResourceBytes(resource.memoryBytes) }),
1091
+ ),
1092
+ alerts.length > 0
1093
+ ? createElement(
1094
+ 'div',
1095
+ { className: 'dsh-my-observability-resource-alerts' },
1096
+ alerts.map((alert) =>
1097
+ createElement(
1098
+ 'div',
1099
+ { className: `dsh-my-observability-resource-alert dsh-my-observability-resource-alert-${alert.level}` },
1100
+ alert.message,
1101
+ ),
1102
+ ),
1103
+ )
1104
+ : null,
1105
+ )
1106
+ }
1107
+
1029
1108
  // ── 审计视图扩展:搜索 / 组合过滤 / 导出 / 统计 / 高亮(replay.js 拆出)────
1030
1109
  // 依赖 replay.js(REPLAY_POLL_MS 等常量与 loadReplayData/EventRow)与
1031
1110
  // audit-view.js 纯函数(applyAuditFilter 等)。始终以 function 声明提升。
@@ -1300,6 +1379,22 @@ function useReplayDataState(props) {
1300
1379
  const [loading, setLoading] = useState(true)
1301
1380
  const [error, setError] = useState('')
1302
1381
  const [reloadTick, setReloadTick] = useState(0)
1382
+ const [resource, setResource] = useState(null)
1383
+
1384
+ // 资源采样轮询(写放大/资源超限预警;可见时 15s 一次,隐藏暂停)
1385
+ useEffect(() => {
1386
+ if (!visible) return undefined
1387
+ let alive = true
1388
+ const tick = () => {
1389
+ if (alive) apiJson('/observability/api/resources').then(setResource).catch(() => {})
1390
+ }
1391
+ tick()
1392
+ const timer = setInterval(tick, RESOURCE_POLL_MS)
1393
+ return () => {
1394
+ alive = false
1395
+ clearInterval(timer)
1396
+ }
1397
+ }, [visible])
1303
1398
 
1304
1399
  useEffect(() => {
1305
1400
  if (!visible) return undefined
@@ -1324,6 +1419,7 @@ function useReplayDataState(props) {
1324
1419
 
1325
1420
  return {
1326
1421
  currentSession,
1422
+ resource,
1327
1423
  sessions,
1328
1424
  selected,
1329
1425
  events,
@@ -1372,6 +1468,7 @@ function useReplayState(props) {
1372
1468
  const onExport = (format) => void runExport(format, scope, filtered, criteria, data.setError)
1373
1469
 
1374
1470
  return {
1471
+ resource: data.resource,
1375
1472
  sessions: data.sessions,
1376
1473
  selected: data.selected,
1377
1474
  events: data.events,
@@ -1826,6 +1923,16 @@ const STYLES = `
1826
1923
  .dsh-my-observability-review-ok{font:var(--dsw-font-xxs-strong-12);color:var(--dsw-alias-state-success-primary)}
1827
1924
  .dsh-my-observability-ai{font:var(--dsw-font-xxs-12);color:var(--dsw-alias-label-secondary);line-height:1.5;
1828
1925
  border:1px dashed var(--dsw-alias-border-l2);border-radius:6px;padding:6px 8px}
1926
+ .dsh-my-observability-resource{border:1px solid var(--dsw-alias-border-l2);border-radius:8px;padding:8px;margin:0 0 8px}
1927
+ .dsh-my-observability-resource-head{font:var(--dsw-font-xxs-strong-12);color:var(--dsw-alias-label-primary);margin-bottom:6px}
1928
+ .dsh-my-observability-resource-grid{display:grid;grid-template-columns:1fr 1fr;gap:4px 10px}
1929
+ .dsh-my-observability-resource-metric{display:flex;justify-content:space-between;gap:8px;font:var(--dsw-font-xxs-12)}
1930
+ .dsh-my-observability-resource-label{color:var(--dsw-alias-label-secondary)}
1931
+ .dsh-my-observability-resource-value{color:var(--dsw-alias-label-primary);font-family:var(--dsw-font-mono-xxs)}
1932
+ .dsh-my-observability-resource-alerts{margin-top:6px;display:flex;flex-direction:column;gap:4px}
1933
+ .dsh-my-observability-resource-alert{font:var(--dsw-font-xxxs-11);border-radius:4px;padding:2px 6px}
1934
+ .dsh-my-observability-resource-alert-error{color:var(--dsw-alias-state-error-primary);background:color-mix(in srgb,var(--dsw-alias-state-error-primary) 12%,transparent)}
1935
+ .dsh-my-observability-resource-alert-warn{color:var(--dsw-alias-state-warn-primary);background:color-mix(in srgb,var(--dsw-alias-state-warn-primary) 12%,transparent)}
1829
1936
  `
1830
1937
 
1831
1938
  function injectStyles() {
package/lib/client.src.js CHANGED
@@ -34,6 +34,7 @@ window.__ModuleLoader__.load({
34
34
  /*__PART_ICONS__*/
35
35
  /*__PART_AUDIT_VIEW__*/
36
36
  /*__PART_REPLAY__*/
37
+ /*__PART_RESOURCE__*/
37
38
  /*__PART_REPLAY_EXT__*/
38
39
  /*__PART_GIT__*/
39
40
  /*__PART_STYLES__*/
package/lib/index.js CHANGED
@@ -22,6 +22,7 @@
22
22
  import { createStore } from './store.js'
23
23
  import { attachAuditListeners } from './audit.js'
24
24
  import { registerObservabilityRoutes } from './routes.js'
25
+ import { createResourceMonitor } from './resource-monitor.js'
25
26
 
26
27
  export const name = 'dsh-my-observability'
27
28
 
@@ -40,9 +41,19 @@ export function apply(ctx, config) {
40
41
  // ── 事件监听(只读观察;waterfall 一律透传 next())──────────────────
41
42
  attachAuditListeners(ctx, store.record)
42
43
 
43
- // ── 路由(查询 / git 工具 / diff 审查)──────────────────────────────
44
- registerObservabilityRoutes(ctx, store, options)
44
+ // ── 资源监控(15s 采样 CPU/内存/审计写入速率,阈值告警)─────────────
45
+ const monitor = createResourceMonitor(ctx, {
46
+ intervalMs: config?.resourceIntervalMs,
47
+ limits: config?.resourceLimits,
48
+ })
49
+ monitor.start()
45
50
 
46
- // ── 卸载冲刷:清防抖定时器 + 立即落盘 ───────────────────────────────
47
- ctx.effect(() => store.dispose, 'dsh-my-observability: persistence teardown')
51
+ // ── 路由(查询 / git 工具 / diff 审查 / 资源)───────────────────────
52
+ registerObservabilityRoutes(ctx, store, monitor, options)
53
+
54
+ // ── 卸载冲刷:清防抖定时器 + 立即落盘 + 停采样 ──────────────────────
55
+ ctx.effect(() => {
56
+ monitor.stop()
57
+ return store.dispose
58
+ }, 'dsh-my-observability: persistence teardown')
48
59
  }
package/lib/parts/i18n.js CHANGED
@@ -10,6 +10,12 @@ function isZh() {
10
10
 
11
11
  const strings = {
12
12
  replayTitle: () => (isZh() ? '轨迹回放' : 'Trajectory'),
13
+ resourceTitle: () => (isZh() ? '资源监控' : 'Resources'),
14
+ resourceLoading: () => (isZh() ? '资源采样中…' : 'Sampling…'),
15
+ resourceFile: () => (isZh() ? '审计文件' : 'Audit file'),
16
+ resourceRate: () => (isZh() ? '写入速率' : 'Write rate'),
17
+ resourceCpu: () => (isZh() ? 'CPU' : 'CPU'),
18
+ resourceMem: () => (isZh() ? '内存' : 'Memory'),
13
19
  gitTitle: () => (isZh() ? 'Git 工具' : 'Git Tools'),
14
20
  allSessions: () => (isZh() ? '全部会话' : 'All sessions'),
15
21
  filterAll: () => (isZh() ? '全部' : 'All'),
@@ -272,6 +272,22 @@ function useReplayDataState(props) {
272
272
  const [loading, setLoading] = useState(true)
273
273
  const [error, setError] = useState('')
274
274
  const [reloadTick, setReloadTick] = useState(0)
275
+ const [resource, setResource] = useState(null)
276
+
277
+ // 资源采样轮询(写放大/资源超限预警;可见时 15s 一次,隐藏暂停)
278
+ useEffect(() => {
279
+ if (!visible) return undefined
280
+ let alive = true
281
+ const tick = () => {
282
+ if (alive) apiJson('/observability/api/resources').then(setResource).catch(() => {})
283
+ }
284
+ tick()
285
+ const timer = setInterval(tick, RESOURCE_POLL_MS)
286
+ return () => {
287
+ alive = false
288
+ clearInterval(timer)
289
+ }
290
+ }, [visible])
275
291
 
276
292
  useEffect(() => {
277
293
  if (!visible) return undefined
@@ -296,6 +312,7 @@ function useReplayDataState(props) {
296
312
 
297
313
  return {
298
314
  currentSession,
315
+ resource,
299
316
  sessions,
300
317
  selected,
301
318
  events,
@@ -344,6 +361,7 @@ function useReplayState(props) {
344
361
  const onExport = (format) => void runExport(format, scope, filtered, criteria, data.setError)
345
362
 
346
363
  return {
364
+ resource: data.resource,
347
365
  sessions: data.sessions,
348
366
  selected: data.selected,
349
367
  events: data.events,
@@ -279,6 +279,7 @@ function ReplayPanel(props) {
279
279
  return createElement(
280
280
  'div',
281
281
  { className: 'dsh-my-observability-panel' },
282
+ createElement(ResourcePanel, { resource: s.resource }),
282
283
  createElement(ReplayToolbar, {
283
284
  sessions: s.sessions,
284
285
  selected: s.selected,
@@ -0,0 +1,71 @@
1
+ // ── 资源监控区块(写放大/资源超限预警,见 lib/resource-monitor.js)──────
2
+ // 依赖 replay.js 先拼接(apiJson)与 i18n.js(strings)。纯函数声明文本。
3
+
4
+ const RESOURCE_POLL_MS = 15000
5
+
6
+ function fmtResourceBytes(bytes) {
7
+ if (!Number.isFinite(bytes)) return '-'
8
+ return `${(bytes / 1048576).toFixed(1)} MB`
9
+ }
10
+
11
+ /** 资源采样状态:可见时每 15s 轮询 /observability/api/resources。 */
12
+ function useResourceState(visible) {
13
+ const [resource, setResource] = useState(null)
14
+ useEffect(() => {
15
+ if (!visible) return undefined
16
+ let alive = true
17
+ const tick = () => {
18
+ if (alive) apiJson('/observability/api/resources').then(setResource).catch(() => {})
19
+ }
20
+ tick()
21
+ const timer = setInterval(tick, RESOURCE_POLL_MS)
22
+ return () => {
23
+ alive = false
24
+ clearInterval(timer)
25
+ }
26
+ }, [visible])
27
+ return resource
28
+ }
29
+
30
+ function ResourceMetric({ label, value }) {
31
+ return createElement(
32
+ 'div',
33
+ { className: 'dsh-my-observability-resource-metric' },
34
+ createElement('span', { className: 'dsh-my-observability-resource-label' }, label),
35
+ createElement('span', { className: 'dsh-my-observability-resource-value' }, value),
36
+ )
37
+ }
38
+
39
+ /** 资源面板:四指标 + 告警列表(write-rate/file-size level=error 红色,cpu/memory warn 黄色)。 */
40
+ function ResourcePanel({ resource }) {
41
+ if (resource === null || resource === undefined) {
42
+ return createElement('div', { className: 'dsh-my-observability-resource' }, strings.resourceLoading())
43
+ }
44
+ const alerts = Array.isArray(resource.alerts) ? resource.alerts : []
45
+ return createElement(
46
+ 'div',
47
+ { className: 'dsh-my-observability-resource' },
48
+ createElement('div', { className: 'dsh-my-observability-resource-head' }, strings.resourceTitle()),
49
+ createElement(
50
+ 'div',
51
+ { className: 'dsh-my-observability-resource-grid' },
52
+ createElement(ResourceMetric, { label: strings.resourceFile(), value: fmtResourceBytes(resource.fileBytes) }),
53
+ createElement(ResourceMetric, { label: strings.resourceRate(), value: `${fmtResourceBytes(resource.writeRateBytesPerHour)}/h` }),
54
+ createElement(ResourceMetric, { label: strings.resourceCpu(), value: `${Math.round(resource.cpuPercent ?? 0)}%` }),
55
+ createElement(ResourceMetric, { label: strings.resourceMem(), value: fmtResourceBytes(resource.memoryBytes) }),
56
+ ),
57
+ alerts.length > 0
58
+ ? createElement(
59
+ 'div',
60
+ { className: 'dsh-my-observability-resource-alerts' },
61
+ alerts.map((alert) =>
62
+ createElement(
63
+ 'div',
64
+ { className: `dsh-my-observability-resource-alert dsh-my-observability-resource-alert-${alert.level}` },
65
+ alert.message,
66
+ ),
67
+ ),
68
+ )
69
+ : null,
70
+ )
71
+ }
@@ -123,6 +123,16 @@ const STYLES = `
123
123
  .dsh-my-observability-review-ok{font:var(--dsw-font-xxs-strong-12);color:var(--dsw-alias-state-success-primary)}
124
124
  .dsh-my-observability-ai{font:var(--dsw-font-xxs-12);color:var(--dsw-alias-label-secondary);line-height:1.5;
125
125
  border:1px dashed var(--dsw-alias-border-l2);border-radius:6px;padding:6px 8px}
126
+ .dsh-my-observability-resource{border:1px solid var(--dsw-alias-border-l2);border-radius:8px;padding:8px;margin:0 0 8px}
127
+ .dsh-my-observability-resource-head{font:var(--dsw-font-xxs-strong-12);color:var(--dsw-alias-label-primary);margin-bottom:6px}
128
+ .dsh-my-observability-resource-grid{display:grid;grid-template-columns:1fr 1fr;gap:4px 10px}
129
+ .dsh-my-observability-resource-metric{display:flex;justify-content:space-between;gap:8px;font:var(--dsw-font-xxs-12)}
130
+ .dsh-my-observability-resource-label{color:var(--dsw-alias-label-secondary)}
131
+ .dsh-my-observability-resource-value{color:var(--dsw-alias-label-primary);font-family:var(--dsw-font-mono-xxs)}
132
+ .dsh-my-observability-resource-alerts{margin-top:6px;display:flex;flex-direction:column;gap:4px}
133
+ .dsh-my-observability-resource-alert{font:var(--dsw-font-xxxs-11);border-radius:4px;padding:2px 6px}
134
+ .dsh-my-observability-resource-alert-error{color:var(--dsw-alias-state-error-primary);background:color-mix(in srgb,var(--dsw-alias-state-error-primary) 12%,transparent)}
135
+ .dsh-my-observability-resource-alert-warn{color:var(--dsw-alias-state-warn-primary);background:color-mix(in srgb,var(--dsw-alias-state-warn-primary) 12%,transparent)}
126
136
  `
127
137
 
128
138
  function injectStyles() {
@@ -0,0 +1,86 @@
1
+ /**
2
+ * dsh-my-observability — 资源采样监控。
3
+ *
4
+ * 每 intervalMs(默认 15s)采样本进程 CPU/内存与审计文件大小/写入速率,
5
+ * 保留最近 MAX_HISTORY 个样本(ring buffer),并做阈值评估(resource-rules)。
6
+ * 用途:让「写放大/资源超限」在运行期当小时可见(9/2 事故复盘结论:
7
+ * 15 小时 300GB 写入零监控是事故未被及时发现的主因)。
8
+ *
9
+ * 采样自身开销:15s 一次 process.cpuUsage/memoryUsage + fs.stat(<0.01% CPU、
10
+ * 零分配大对象),远低于「监控不能放大被监控对象」的护栏(resource-budget-review)。
11
+ */
12
+ import { statSync } from 'node:fs'
13
+ import { evaluateResourceAlerts, DEFAULT_LIMITS } from './resource-rules.js'
14
+ import { jsonlFile } from './store-persist.js'
15
+
16
+ const DEFAULT_INTERVAL_MS = 15000
17
+ const MAX_HISTORY = 60
18
+
19
+ /** 创建资源监控器:{ sample, start, stop }。options: intervalMs / limits。 */
20
+ export function createResourceMonitor(ctx, options = {}) {
21
+ const intervalMs = Number.isFinite(options.intervalMs) && options.intervalMs > 0 ? options.intervalMs : DEFAULT_INTERVAL_MS
22
+ const limits = { ...DEFAULT_LIMITS, ...(options.limits ?? {}) }
23
+ const state = {
24
+ timer: null,
25
+ file: jsonlFile(),
26
+ lastSample: null,
27
+ lastCpu: process.cpuUsage(),
28
+ history: [],
29
+ }
30
+ const monitor = {
31
+ sample: () => sample(state, limits),
32
+ start: () => startMonitor(state, intervalMs, monitor),
33
+ stop: () => stopMonitor(state),
34
+ }
35
+ return monitor
36
+ }
37
+
38
+ /** 采样一次:CPU 使用率(窗口内 user+sys)/RSS/审计文件字节/写入速率 + 告警。 */
39
+ function sample(state, limits) {
40
+ const now = Date.now()
41
+ const cpu = process.cpuUsage()
42
+ const cpuDelta = cpu.user - state.lastCpu.user + (cpu.system - state.lastCpu.system) // µs
43
+ state.lastCpu = cpu
44
+ const mem = process.memoryUsage()
45
+ const memoryBytes = mem.rss
46
+ let fileBytes = 0
47
+ try {
48
+ fileBytes = statSync(state.file).size
49
+ } catch {
50
+ // 审计文件尚未创建:字节为 0
51
+ }
52
+ const prev = state.lastSample
53
+ if (prev !== null) {
54
+ const deltaMs = Math.max(now - prev.time, 1)
55
+ // CPU 单核折算:cpuDelta(µs) / deltaMs(ms) / 1000 → 百分比(×100)
56
+ const cpuPercent = (cpuDelta / 1000 / deltaMs) * 100
57
+ const byteDelta = fileBytes - prev.fileBytes
58
+ const writeRateBytesPerHour = byteDelta > 0 ? (byteDelta / deltaMs) * 3600 * 1000 : 0
59
+ const sample = { time: now, cpuPercent, memoryBytes, fileBytes, writeRateBytesPerHour }
60
+ state.history.push(sample)
61
+ if (state.history.length > MAX_HISTORY) state.history.splice(0, state.history.length - MAX_HISTORY)
62
+ state.lastSample = sample
63
+ return { ...sample, history: [...state.history], alerts: evaluateResourceAlerts(sample, limits) }
64
+ }
65
+ state.lastSample = { time: now, fileBytes, memoryBytes, cpuPercent: 0, writeRateBytesPerHour: 0 }
66
+ return { ...state.lastSample, history: [...state.history], alerts: [] }
67
+ }
68
+
69
+ /** 启动周期采样(幂等)。 */
70
+ function startMonitor(state, intervalMs, monitor) {
71
+ if (state.timer === null) {
72
+ state.timer = setInterval(() => {
73
+ void monitor.sample()
74
+ }, intervalMs)
75
+ if (state.timer.unref) state.timer.unref()
76
+ }
77
+ return state.timer
78
+ }
79
+
80
+ /** 停止采样(幂等)。 */
81
+ function stopMonitor(state) {
82
+ if (state.timer !== null) {
83
+ clearInterval(state.timer)
84
+ state.timer = null
85
+ }
86
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * dsh-my-observability — 资源阈值判定(纯函数)。
3
+ *
4
+ * 阈值口径与 skill `resource-budget-review` 一致(设计/开发/运行三遍校验
5
+ * 的运行期部分):写放大/CPU/内存超限在采样数据上提前暴露,告警可查询。
6
+ * 纯函数便于单测与配置覆盖(config.resourceLimits 浅合并)。
7
+ */
8
+
9
+ /** 默认阈值(DSH 插件场景,来源 resource-budget-review 五维表)。 */
10
+ export const DEFAULT_LIMITS = {
11
+ /** 审计文件写入速率上限:50 MB/小时。 */
12
+ writeRateBytesPerHour: 50 * 1024 * 1024,
13
+ /** 审计文件大小上限:50 MB(循环 compact 后应远小于此)。 */
14
+ fileBytes: 50 * 1024 * 1024,
15
+ /** 本进程 CPU 均值上限:10%(单核折算,采样窗口内 user+sys / 时长)。 */
16
+ cpuPercent: 10,
17
+ /** 本进程 RSS 上限:500 MB。 */
18
+ memoryBytes: 500 * 1024 * 1024,
19
+ }
20
+
21
+ /** 采样数据 → 告警列表([{ rule, level, message, value, limit }])。 */
22
+ export function evaluateResourceAlerts(sample, limits = DEFAULT_LIMITS) {
23
+ const alerts = []
24
+ if (typeof sample.writeRateBytesPerHour === 'number' && sample.writeRateBytesPerHour > limits.writeRateBytesPerHour) {
25
+ alerts.push({
26
+ rule: 'write-rate',
27
+ level: 'error',
28
+ message: `审计写入速率 ${fmtMB(sample.writeRateBytesPerHour)}/h 超过上限 ${fmtMB(limits.writeRateBytesPerHour)}/h(写放大风险)`,
29
+ value: sample.writeRateBytesPerHour,
30
+ limit: limits.writeRateBytesPerHour,
31
+ })
32
+ }
33
+ if (typeof sample.fileBytes === 'number' && sample.fileBytes > limits.fileBytes) {
34
+ alerts.push({
35
+ rule: 'file-size',
36
+ level: 'error',
37
+ message: `审计文件 ${fmtMB(sample.fileBytes)} 超过上限 ${fmtMB(limits.fileBytes)}(请检查 compact/轮转)`,
38
+ value: sample.fileBytes,
39
+ limit: limits.fileBytes,
40
+ })
41
+ }
42
+ if (typeof sample.cpuPercent === 'number' && sample.cpuPercent > limits.cpuPercent) {
43
+ alerts.push({
44
+ rule: 'cpu',
45
+ level: 'warn',
46
+ message: `本进程 CPU ${Math.round(sample.cpuPercent)}% 超过上限 ${limits.cpuPercent}%`,
47
+ value: sample.cpuPercent,
48
+ limit: limits.cpuPercent,
49
+ })
50
+ }
51
+ if (typeof sample.memoryBytes === 'number' && sample.memoryBytes > limits.memoryBytes) {
52
+ alerts.push({
53
+ rule: 'memory',
54
+ level: 'warn',
55
+ message: `本进程内存 ${fmtMB(sample.memoryBytes)} 超过上限 ${fmtMB(limits.memoryBytes)}`,
56
+ value: sample.memoryBytes,
57
+ limit: limits.memoryBytes,
58
+ })
59
+ }
60
+ return alerts
61
+ }
62
+
63
+ function fmtMB(bytes) {
64
+ return `${(bytes / 1024 / 1024).toFixed(1)} MB`
65
+ }
package/lib/routes.js CHANGED
@@ -17,7 +17,7 @@ import { reviewRules } from './review.js'
17
17
  import { runAiReview } from './ai.js'
18
18
 
19
19
  /** 注册 /observability/api 路由(effect 持有 disposer)。 */
20
- export function registerObservabilityRoutes(ctx, store, options) {
20
+ export function registerObservabilityRoutes(ctx, store, monitor, options) {
21
21
  const webRuntime = ctx.get ? ctx.get('webRuntime') : undefined
22
22
  const trustedHosts =
23
23
  webRuntime !== undefined && webRuntime !== null && Array.isArray(webRuntime.trustedHosts)
@@ -30,14 +30,14 @@ export function registerObservabilityRoutes(ctx, store, options) {
30
30
  ctx.webServer.register({
31
31
  kind: 'prefix',
32
32
  path: '/observability/api',
33
- handler: apiHandler(ctx, fence, store, options),
33
+ handler: apiHandler(ctx, fence, store, monitor, options),
34
34
  }),
35
35
  'dsh-my-observability: /observability/api routes',
36
36
  )
37
37
  }
38
38
 
39
39
  /** 统一 handler:fence → 方法分派 → 404/错误兜底。 */
40
- function apiHandler(ctx, fence, store, options) {
40
+ function apiHandler(ctx, fence, store, monitor, options) {
41
41
  return async (request, response) => {
42
42
  if (!fence(request)) {
43
43
  writeJson(response, 403, { ok: false, error: { code: 'forbidden', message: 'forbidden' } })
@@ -47,7 +47,7 @@ function apiHandler(ctx, fence, store, options) {
47
47
  const pathname = url.pathname
48
48
  const method = pathname.startsWith('/observability/api/') ? pathname.slice('/observability/api/'.length) : undefined
49
49
  try {
50
- const handled = await dispatchMethod(method, request, response, url, ctx, store, options)
50
+ const handled = await dispatchMethod(method, request, response, url, ctx, store, monitor, options)
51
51
  if (!handled) {
52
52
  writeJson(response, 404, {
53
53
  ok: false,
@@ -66,11 +66,15 @@ function isMethod(method, request, name, verb) {
66
66
  }
67
67
 
68
68
  /** 按 method 分派到具体 handler;未识别返回 false(调用方回 404)。 */
69
- async function dispatchMethod(method, request, response, url, ctx, store, options) {
69
+ async function dispatchMethod(method, request, response, url, ctx, store, monitor, options) {
70
70
  if (isMethod(method, request, 'sessions', 'GET')) {
71
71
  writeJson(response, 200, { ok: true, value: store.sessions() })
72
72
  return true
73
73
  }
74
+ if (isMethod(method, request, 'resources', 'GET')) {
75
+ writeJson(response, 200, { ok: true, value: monitor.sample() })
76
+ return true
77
+ }
74
78
  if (isMethod(method, request, 'events', 'GET')) {
75
79
  writeJson(response, 200, {
76
80
  ok: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-my-observability",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "DSH 可观测性 + Git 工程工具插件:会话轨迹回放(时间轴)、事件审计(agent 行为记录)、结构化 Git 提交(Conventional Commits)、增量 diff 审查(提交前审查)。DSH web plugin: trajectory replay timeline, event audit log, structured git commits, pre-commit incremental diff review.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",