sentinelayer-cli 0.6.2 → 0.8.1
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/README.md +1009 -996
- package/bin/create-sentinelayer.js +5 -5
- package/bin/sentinelayer-cli.js +4 -4
- package/bin/sl.js +5 -5
- package/package.json +64 -63
- package/src/agents/ai-governance/index.js +12 -0
- package/src/agents/ai-governance/tools/base.js +171 -0
- package/src/agents/ai-governance/tools/eval-regression.js +47 -0
- package/src/agents/ai-governance/tools/hitl-audit.js +81 -0
- package/src/agents/ai-governance/tools/index.js +52 -0
- package/src/agents/ai-governance/tools/prompt-drift.js +42 -0
- package/src/agents/ai-governance/tools/provenance-check.js +69 -0
- package/src/agents/backend/index.js +12 -0
- package/src/agents/backend/tools/base.js +189 -0
- package/src/agents/backend/tools/circuit-breaker-check.js +123 -0
- package/src/agents/backend/tools/idempotency-audit.js +105 -0
- package/src/agents/backend/tools/index.js +87 -0
- package/src/agents/backend/tools/retry-audit.js +132 -0
- package/src/agents/backend/tools/timeout-audit.js +144 -0
- package/src/agents/code-quality/index.js +12 -0
- package/src/agents/code-quality/tools/base.js +159 -0
- package/src/agents/code-quality/tools/complexity-measure.js +197 -0
- package/src/agents/code-quality/tools/coupling-analysis.js +81 -0
- package/src/agents/code-quality/tools/cycle-detect.js +49 -0
- package/src/agents/code-quality/tools/dep-graph.js +196 -0
- package/src/agents/code-quality/tools/index.js +89 -0
- package/src/agents/data-layer/index.js +12 -0
- package/src/agents/data-layer/tools/base.js +181 -0
- package/src/agents/data-layer/tools/index-audit.js +165 -0
- package/src/agents/data-layer/tools/index.js +83 -0
- package/src/agents/data-layer/tools/migration-scan.js +135 -0
- package/src/agents/data-layer/tools/query-explain.js +120 -0
- package/src/agents/data-layer/tools/tenancy-scan.js +166 -0
- package/src/agents/documentation/index.js +12 -0
- package/src/agents/documentation/tools/api-diff.js +91 -0
- package/src/agents/documentation/tools/base.js +151 -0
- package/src/agents/documentation/tools/dead-link-check.js +58 -0
- package/src/agents/documentation/tools/docstring-coverage.js +78 -0
- package/src/agents/documentation/tools/index.js +52 -0
- package/src/agents/documentation/tools/readme-freshness.js +61 -0
- package/src/agents/envelope/fix-cycle.js +45 -0
- package/src/agents/envelope/index.js +31 -0
- package/src/agents/envelope/loop.js +150 -0
- package/src/agents/envelope/pulse.js +18 -0
- package/src/agents/envelope/stream.js +40 -0
- package/src/agents/infrastructure/index.js +12 -0
- package/src/agents/infrastructure/tools/base.js +171 -0
- package/src/agents/infrastructure/tools/checkov-run.js +32 -0
- package/src/agents/infrastructure/tools/drift-detect.js +59 -0
- package/src/agents/infrastructure/tools/iam-least-priv-check.js +78 -0
- package/src/agents/infrastructure/tools/index.js +52 -0
- package/src/agents/infrastructure/tools/tflint-run.js +31 -0
- package/src/agents/jules/config/definition.js +160 -160
- package/src/agents/jules/config/system-prompt.js +182 -182
- package/src/agents/jules/error-intake.js +51 -51
- package/src/agents/jules/fix-cycle.js +17 -17
- package/src/agents/jules/loop.js +460 -450
- package/src/agents/jules/pulse.js +10 -10
- package/src/agents/jules/stream.js +187 -186
- package/src/agents/jules/swarm/file-scanner.js +74 -74
- package/src/agents/jules/swarm/index.js +11 -11
- package/src/agents/jules/swarm/orchestrator.js +362 -362
- package/src/agents/jules/swarm/pattern-hunter.js +123 -123
- package/src/agents/jules/swarm/sub-agent.js +315 -309
- package/src/agents/jules/tools/aidenid-email.js +189 -189
- package/src/agents/jules/tools/auth-audit.js +1708 -1691
- package/src/agents/jules/tools/dispatch.js +340 -335
- package/src/agents/jules/tools/file-edit.js +2 -2
- package/src/agents/jules/tools/file-read.js +2 -2
- package/src/agents/jules/tools/frontend-analyze.js +570 -570
- package/src/agents/jules/tools/glob.js +2 -2
- package/src/agents/jules/tools/grep.js +2 -2
- package/src/agents/jules/tools/index.js +29 -29
- package/src/agents/jules/tools/path-guards.js +2 -2
- package/src/agents/jules/tools/runtime-audit.js +507 -507
- package/src/agents/jules/tools/shell.js +2 -2
- package/src/agents/jules/tools/url-policy.js +100 -100
- package/src/agents/mode.js +113 -0
- package/src/agents/observability/index.js +12 -0
- package/src/agents/observability/tools/alert-audit.js +39 -0
- package/src/agents/observability/tools/base.js +181 -0
- package/src/agents/observability/tools/dashboard-gap.js +42 -0
- package/src/agents/observability/tools/index.js +54 -0
- package/src/agents/observability/tools/log-schema-check.js +74 -0
- package/src/agents/observability/tools/span-coverage.js +74 -0
- package/src/agents/persona-visuals.js +102 -61
- package/src/agents/release/index.js +12 -0
- package/src/agents/release/tools/base.js +181 -0
- package/src/agents/release/tools/changelog-diff.js +86 -0
- package/src/agents/release/tools/feature-flag-audit.js +126 -0
- package/src/agents/release/tools/index.js +61 -0
- package/src/agents/release/tools/rollback-verify.js +129 -0
- package/src/agents/release/tools/semver-check.js +109 -0
- package/src/agents/reliability/index.js +12 -0
- package/src/agents/reliability/tools/backpressure-check.js +129 -0
- package/src/agents/reliability/tools/base.js +181 -0
- package/src/agents/reliability/tools/chaos-probe.js +109 -0
- package/src/agents/reliability/tools/graceful-degradation-check.js +114 -0
- package/src/agents/reliability/tools/health-check-audit.js +111 -0
- package/src/agents/reliability/tools/index.js +87 -0
- package/src/agents/run-persona.js +109 -0
- package/src/agents/security/index.js +12 -0
- package/src/agents/security/tools/authz-audit.js +134 -0
- package/src/agents/security/tools/base.js +190 -0
- package/src/agents/security/tools/crypto-review.js +175 -0
- package/src/agents/security/tools/index.js +97 -0
- package/src/agents/security/tools/sast-scan.js +175 -0
- package/src/agents/security/tools/secrets-scan.js +216 -0
- package/src/agents/shared-tools/dispatch-core.js +320 -315
- package/src/agents/shared-tools/file-edit.js +180 -180
- package/src/agents/shared-tools/file-read.js +100 -100
- package/src/agents/shared-tools/glob.js +168 -168
- package/src/agents/shared-tools/grep.js +228 -228
- package/src/agents/shared-tools/index.js +46 -46
- package/src/agents/shared-tools/path-guards.js +161 -161
- package/src/agents/shared-tools/shell.js +383 -383
- package/src/agents/supply-chain/index.js +12 -0
- package/src/agents/supply-chain/tools/attestation-check.js +42 -0
- package/src/agents/supply-chain/tools/base.js +151 -0
- package/src/agents/supply-chain/tools/index.js +52 -0
- package/src/agents/supply-chain/tools/lockfile-integrity.js +73 -0
- package/src/agents/supply-chain/tools/package-verify.js +56 -0
- package/src/agents/supply-chain/tools/sbom-diff.js +34 -0
- package/src/agents/testing/index.js +12 -0
- package/src/agents/testing/tools/base.js +202 -0
- package/src/agents/testing/tools/coverage-gap.js +144 -0
- package/src/agents/testing/tools/flake-detect.js +125 -0
- package/src/agents/testing/tools/index.js +85 -0
- package/src/agents/testing/tools/mutation-test.js +143 -0
- package/src/agents/testing/tools/snapshot-diff.js +103 -0
- package/src/ai/aidenid.js +1021 -1009
- package/src/ai/client.js +553 -553
- package/src/ai/domain-target-store.js +268 -268
- package/src/ai/identity-store.js +270 -270
- package/src/ai/proxy.js +137 -137
- package/src/ai/site-store.js +145 -145
- package/src/audit/agents/architecture.js +180 -180
- package/src/audit/agents/compliance.js +179 -179
- package/src/audit/agents/documentation.js +165 -165
- package/src/audit/agents/performance.js +145 -145
- package/src/audit/agents/security.js +215 -215
- package/src/audit/agents/testing.js +172 -172
- package/src/audit/orchestrator.js +557 -557
- package/src/audit/package.js +204 -204
- package/src/audit/registry.js +284 -284
- package/src/audit/replay.js +103 -103
- package/src/auth/gate.js +428 -371
- package/src/auth/http.js +681 -611
- package/src/auth/service.js +1106 -1106
- package/src/auth/session-store.js +813 -813
- package/src/cli.js +257 -252
- package/src/commands/ai/identity-lifecycle.js +1338 -1338
- package/src/commands/ai/provision-governance.js +1272 -1272
- package/src/commands/ai/shared.js +147 -147
- package/src/commands/ai.js +11 -11
- package/src/commands/apply.js +12 -12
- package/src/commands/audit.js +1171 -1166
- package/src/commands/auth.js +419 -419
- package/src/commands/chat.js +184 -191
- package/src/commands/config.js +184 -184
- package/src/commands/cost.js +311 -311
- package/src/commands/daemon/core.js +850 -850
- package/src/commands/daemon/extended.js +1048 -1048
- package/src/commands/daemon/shared.js +213 -213
- package/src/commands/daemon.js +11 -11
- package/src/commands/guide.js +174 -174
- package/src/commands/ingest.js +58 -58
- package/src/commands/init.js +55 -55
- package/src/commands/legacy-args.js +20 -10
- package/src/commands/mcp.js +461 -461
- package/src/commands/omargate.js +63 -29
- package/src/commands/persona.js +65 -20
- package/src/commands/plugin.js +260 -260
- package/src/commands/policy.js +132 -132
- package/src/commands/prompt.js +238 -238
- package/src/commands/review.js +704 -704
- package/src/commands/scan.js +865 -872
- package/src/commands/session.js +1238 -0
- package/src/commands/spec.js +771 -716
- package/src/commands/swarm.js +651 -651
- package/src/commands/telemetry.js +202 -202
- package/src/commands/watch.js +511 -511
- package/src/config/agent-dictionary.js +182 -182
- package/src/config/io.js +56 -56
- package/src/config/paths.js +18 -18
- package/src/config/schema.js +55 -55
- package/src/config/service.js +184 -184
- package/src/coord/events-log.js +141 -0
- package/src/coord/handshake.js +719 -0
- package/src/coord/index.js +35 -0
- package/src/coord/paths.js +84 -0
- package/src/coord/priority.js +62 -0
- package/src/coord/tarjan.js +157 -0
- package/src/cost/budget.js +235 -235
- package/src/cost/history.js +188 -188
- package/src/cost/tokenizer.js +160 -0
- package/src/cost/tracker.js +232 -171
- package/src/daemon/artifact-lineage.js +896 -534
- package/src/daemon/assignment-ledger.js +1083 -770
- package/src/daemon/ast-drift.js +496 -0
- package/src/daemon/ast-parser-layer.js +258 -258
- package/src/daemon/budget-governor.js +633 -633
- package/src/daemon/callgraph-overlay.js +646 -646
- package/src/daemon/error-worker.js +1209 -626
- package/src/daemon/fix-cycle.js +384 -377
- package/src/daemon/hybrid-mapper.js +929 -929
- package/src/daemon/ingest-refresh.js +79 -11
- package/src/daemon/jira-lifecycle.js +767 -632
- package/src/daemon/operator-control.js +657 -657
- package/src/daemon/pulse.js +327 -327
- package/src/daemon/reliability-lane.js +471 -471
- package/src/daemon/scope-engine.js +1068 -0
- package/src/daemon/watchdog.js +971 -971
- package/src/events/schema.js +190 -0
- package/src/guide/generator.js +316 -316
- package/src/ingest/engine.js +933 -918
- package/src/ingest/ownership.js +380 -0
- package/src/interactive/index.js +97 -97
- package/src/legacy-cli.js +3228 -2994
- package/src/mcp/registry.js +695 -695
- package/src/memory/blackboard.js +301 -301
- package/src/memory/retrieval.js +581 -581
- package/src/orchestrator/kai-chen.js +126 -0
- package/src/plugin/manifest.js +553 -553
- package/src/policy/packs.js +144 -144
- package/src/prompt/generator.js +136 -118
- package/src/review/ai-review.js +672 -679
- package/src/review/compliance-pack.js +389 -0
- package/src/review/investor-dd-config.js +54 -0
- package/src/review/investor-dd-file-loop.js +303 -0
- package/src/review/investor-dd-file-router.js +406 -0
- package/src/review/investor-dd-html-report.js +233 -0
- package/src/review/investor-dd-notification.js +120 -0
- package/src/review/investor-dd-orchestrator.js +405 -0
- package/src/review/investor-dd-persona-runner.js +275 -0
- package/src/review/live-validator.js +253 -0
- package/src/review/local-review.js +1351 -1305
- package/src/review/omargate-interactive.js +68 -68
- package/src/review/omargate-orchestrator.js +492 -300
- package/src/review/persona-prompts.js +484 -296
- package/src/review/reconciliation-rules.js +329 -0
- package/src/review/replay.js +235 -235
- package/src/review/report.js +664 -664
- package/src/review/reproducibility-chain.js +136 -0
- package/src/review/scan-modes.js +147 -42
- package/src/review/spec-binding.js +487 -487
- package/src/scaffold/generator.js +67 -67
- package/src/scaffold/templates.js +150 -150
- package/src/scan/generator.js +418 -418
- package/src/scan/gh-secrets.js +107 -107
- package/src/session/agent-registry.js +359 -0
- package/src/session/analytics.js +479 -0
- package/src/session/daemon.js +1396 -0
- package/src/session/file-locks.js +666 -0
- package/src/session/paths.js +37 -0
- package/src/session/recap.js +567 -0
- package/src/session/redact.js +82 -0
- package/src/session/runtime-bridge.js +762 -0
- package/src/session/scoring.js +406 -0
- package/src/session/setup-guides.js +304 -0
- package/src/session/store.js +704 -0
- package/src/session/stream.js +333 -0
- package/src/session/sync.js +753 -0
- package/src/session/tasks.js +1054 -0
- package/src/session/templates.js +188 -0
- package/src/spec/generator.js +619 -519
- package/src/spec/regenerate.js +237 -237
- package/src/spec/templates.js +91 -91
- package/src/swarm/dashboard.js +247 -247
- package/src/swarm/factory.js +363 -363
- package/src/swarm/pentest.js +934 -934
- package/src/swarm/registry.js +419 -419
- package/src/swarm/report.js +158 -158
- package/src/swarm/runtime.js +569 -576
- package/src/swarm/scenario-dsl.js +272 -272
- package/src/telemetry/ledger.js +302 -302
- package/src/telemetry/session-tracker.js +234 -234
- package/src/telemetry/sync.js +203 -203
- package/src/ui/command-hints.js +13 -13
- package/src/ui/markdown.js +220 -220
|
@@ -1,626 +1,1209 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import fsp from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
import { resolveOutputRoot } from "../config/service.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (!
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
{
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
const
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
const
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import fsp from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { resolveOutputRoot } from "../config/service.js";
|
|
6
|
+
import { createAgentEvent } from "../events/schema.js";
|
|
7
|
+
import { appendToStream } from "../session/stream.js";
|
|
8
|
+
|
|
9
|
+
const QUEUE_SCHEMA_VERSION = "1.0.0";
|
|
10
|
+
const STATE_SCHEMA_VERSION = "1.0.0";
|
|
11
|
+
const DEFAULT_MAX_EVENTS = 200;
|
|
12
|
+
const DEFAULT_DAEMON_POLL_MS = 5000;
|
|
13
|
+
const DEFAULT_SWEEP_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
14
|
+
const ERROR_DAEMON_AGENT_ID = "error-daemon";
|
|
15
|
+
const ACTIVE_DAEMONS = new Map();
|
|
16
|
+
|
|
17
|
+
const TERMINAL_WORK_ITEM_STATUSES = new Set(["DONE", "SQUASHED"]);
|
|
18
|
+
|
|
19
|
+
export const DEDUP_KEY_FIELDS = Object.freeze([
|
|
20
|
+
"service",
|
|
21
|
+
"endpoint",
|
|
22
|
+
"error_code",
|
|
23
|
+
"stack_fingerprint",
|
|
24
|
+
"commit_sha",
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
export const WAKE_MODES = Object.freeze({
|
|
28
|
+
REALTIME: "realtime",
|
|
29
|
+
SCHEDULED: "scheduled",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const WAKE_MODE_SET = new Set(Object.values(WAKE_MODES));
|
|
33
|
+
|
|
34
|
+
export const WORK_ITEM_STATUSES = Object.freeze([
|
|
35
|
+
"QUEUED",
|
|
36
|
+
"ASSIGNED",
|
|
37
|
+
"IN_PROGRESS",
|
|
38
|
+
"BLOCKED",
|
|
39
|
+
"DONE",
|
|
40
|
+
"SQUASHED",
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
const WORK_ITEM_STATUS_SET = new Set(WORK_ITEM_STATUSES);
|
|
44
|
+
|
|
45
|
+
const SEVERITY_RANK = new Map([
|
|
46
|
+
["UNKNOWN", 0],
|
|
47
|
+
["P3", 1],
|
|
48
|
+
["P2", 2],
|
|
49
|
+
["P1", 3],
|
|
50
|
+
["P0", 4],
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
function normalizeString(value) {
|
|
54
|
+
return String(value || "").trim();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function normalizeIsoTimestamp(value, fallbackIso = new Date().toISOString()) {
|
|
58
|
+
const normalized = normalizeString(value);
|
|
59
|
+
if (!normalized) {
|
|
60
|
+
return fallbackIso;
|
|
61
|
+
}
|
|
62
|
+
const epoch = Date.parse(normalized);
|
|
63
|
+
if (!Number.isFinite(epoch)) {
|
|
64
|
+
return fallbackIso;
|
|
65
|
+
}
|
|
66
|
+
return new Date(epoch).toISOString();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function normalizeNonNegativeInteger(value, fallbackValue = 0) {
|
|
70
|
+
if (value === undefined || value === null || normalizeString(value) === "") {
|
|
71
|
+
return fallbackValue;
|
|
72
|
+
}
|
|
73
|
+
const normalized = Number(value);
|
|
74
|
+
if (!Number.isFinite(normalized) || normalized < 0) {
|
|
75
|
+
throw new Error("Value must be a non-negative integer.");
|
|
76
|
+
}
|
|
77
|
+
return Math.floor(normalized);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function normalizePositiveInteger(value, fallbackValue = 1) {
|
|
81
|
+
if (value === undefined || value === null || normalizeString(value) === "") {
|
|
82
|
+
return fallbackValue;
|
|
83
|
+
}
|
|
84
|
+
const normalized = Number(value);
|
|
85
|
+
if (!Number.isFinite(normalized) || normalized <= 0) {
|
|
86
|
+
throw new Error("Value must be a positive integer.");
|
|
87
|
+
}
|
|
88
|
+
return Math.floor(normalized);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function stableTimestampForFile(date = new Date()) {
|
|
92
|
+
return date.toISOString().replace(/[:.]/g, "-");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function normalizeSeverity(value) {
|
|
96
|
+
const normalized = normalizeString(value).toUpperCase();
|
|
97
|
+
if (normalized === "P0" || normalized === "P1" || normalized === "P2" || normalized === "P3") {
|
|
98
|
+
return normalized;
|
|
99
|
+
}
|
|
100
|
+
return "UNKNOWN";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function normalizeMetadata(value) {
|
|
104
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
return { ...value };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function normalizeStackTrace(value) {
|
|
111
|
+
return String(value || "").replace(/\r\n/g, "\n").trim();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function normalizeWakeMode(value, fallbackValue = WAKE_MODES.REALTIME) {
|
|
115
|
+
const normalized = normalizeString(value).toLowerCase();
|
|
116
|
+
if (!normalized) {
|
|
117
|
+
return fallbackValue;
|
|
118
|
+
}
|
|
119
|
+
if (!WAKE_MODE_SET.has(normalized)) {
|
|
120
|
+
throw new Error(`wakeMode must be one of: ${Object.values(WAKE_MODES).join(", ")}.`);
|
|
121
|
+
}
|
|
122
|
+
return normalized;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function normalizeRequestIds(values) {
|
|
126
|
+
const input = Array.isArray(values) ? values : [];
|
|
127
|
+
const deduped = [];
|
|
128
|
+
const seen = new Set();
|
|
129
|
+
for (const value of input) {
|
|
130
|
+
const normalized = normalizeString(value);
|
|
131
|
+
if (!normalized || seen.has(normalized)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
seen.add(normalized);
|
|
135
|
+
deduped.push(normalized);
|
|
136
|
+
}
|
|
137
|
+
return deduped;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function mergeRequestIds(existing = [], next = []) {
|
|
141
|
+
return normalizeRequestIds([...normalizeRequestIds(existing), ...normalizeRequestIds(next)]);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function buildDedupKey(event = {}) {
|
|
145
|
+
const materialized = [
|
|
146
|
+
normalizeString(event.service).toLowerCase(),
|
|
147
|
+
normalizeString(event.endpoint).toLowerCase(),
|
|
148
|
+
normalizeString(event.errorCode || event.error_code).toLowerCase(),
|
|
149
|
+
normalizeString(event.stackFingerprint || event.stack_fingerprint).toLowerCase(),
|
|
150
|
+
normalizeString(event.commitSha || event.commit_sha).toLowerCase() || "none",
|
|
151
|
+
].join("|");
|
|
152
|
+
return materialized;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function buildDayKey(isoTimestamp = new Date().toISOString()) {
|
|
156
|
+
return normalizeIsoTimestamp(isoTimestamp, new Date().toISOString()).slice(0, 10);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function computeStackFingerprint(stackTrace = "") {
|
|
160
|
+
const normalized = normalizeStackTrace(stackTrace);
|
|
161
|
+
if (!normalized) {
|
|
162
|
+
return "none";
|
|
163
|
+
}
|
|
164
|
+
const reduced = normalized
|
|
165
|
+
.split("\n")
|
|
166
|
+
.map((line) => line.trim())
|
|
167
|
+
.filter(Boolean)
|
|
168
|
+
.slice(0, 6)
|
|
169
|
+
.join("|");
|
|
170
|
+
if (!reduced) {
|
|
171
|
+
return "none";
|
|
172
|
+
}
|
|
173
|
+
return createHash("sha256").update(reduced).digest("hex");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function computeFingerprint(event = {}) {
|
|
177
|
+
return createHash("sha256").update(buildDedupKey(event)).digest("hex");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function chooseHigherSeverity(left, right) {
|
|
181
|
+
const leftNormalized = normalizeSeverity(left);
|
|
182
|
+
const rightNormalized = normalizeSeverity(right);
|
|
183
|
+
const leftRank = SEVERITY_RANK.get(leftNormalized) ?? 0;
|
|
184
|
+
const rightRank = SEVERITY_RANK.get(rightNormalized) ?? 0;
|
|
185
|
+
return rightRank > leftRank ? rightNormalized : leftNormalized;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function toSeverityRank(value) {
|
|
189
|
+
return SEVERITY_RANK.get(normalizeSeverity(value)) ?? 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function createWorkItemId(nowIso = new Date().toISOString()) {
|
|
193
|
+
return `err-${stableTimestampForFile(new Date(nowIso))}-${randomUUID().slice(0, 8)}`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function normalizeWorkItemStatus(value) {
|
|
197
|
+
const normalized = normalizeString(value).toUpperCase();
|
|
198
|
+
if (WORK_ITEM_STATUS_SET.has(normalized)) {
|
|
199
|
+
return normalized;
|
|
200
|
+
}
|
|
201
|
+
return "QUEUED";
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isTerminalStatus(status = "") {
|
|
205
|
+
return TERMINAL_WORK_ITEM_STATUSES.has(normalizeWorkItemStatus(status));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function normalizeQueueItem(item = {}, fallbackNowIso = new Date().toISOString()) {
|
|
209
|
+
const createdAt = normalizeIsoTimestamp(item.createdAt, fallbackNowIso);
|
|
210
|
+
const service = normalizeString(item.service) || "unknown-service";
|
|
211
|
+
const endpoint = normalizeString(item.endpoint) || "unknown-endpoint";
|
|
212
|
+
const errorCode = normalizeString(item.errorCode || item.error_code) || "UNKNOWN_ERROR";
|
|
213
|
+
const stackFingerprint =
|
|
214
|
+
normalizeString(item.stackFingerprint || item.stack_fingerprint) || "none";
|
|
215
|
+
const commitSha = normalizeString(item.commitSha || item.commit_sha) || null;
|
|
216
|
+
const requestIds = normalizeRequestIds(item.requestIds || item.request_ids);
|
|
217
|
+
const dedupKey =
|
|
218
|
+
normalizeString(item.dedupKey || item.dedup_key) ||
|
|
219
|
+
buildDedupKey({
|
|
220
|
+
service,
|
|
221
|
+
endpoint,
|
|
222
|
+
errorCode,
|
|
223
|
+
stackFingerprint,
|
|
224
|
+
commitSha,
|
|
225
|
+
});
|
|
226
|
+
return {
|
|
227
|
+
workItemId: normalizeString(item.workItemId) || createWorkItemId(createdAt),
|
|
228
|
+
fingerprint: normalizeString(item.fingerprint),
|
|
229
|
+
source: normalizeString(item.source) || "admin_error_log",
|
|
230
|
+
service,
|
|
231
|
+
endpoint,
|
|
232
|
+
errorCode,
|
|
233
|
+
severity: normalizeSeverity(item.severity),
|
|
234
|
+
status: normalizeWorkItemStatus(item.status),
|
|
235
|
+
message: normalizeString(item.message),
|
|
236
|
+
stackFingerprint,
|
|
237
|
+
commitSha,
|
|
238
|
+
dedupKey,
|
|
239
|
+
firstSeenAt: normalizeIsoTimestamp(item.firstSeenAt, createdAt),
|
|
240
|
+
lastSeenAt: normalizeIsoTimestamp(item.lastSeenAt, createdAt),
|
|
241
|
+
latestEventId: normalizeString(item.latestEventId) || null,
|
|
242
|
+
occurrenceCount: Math.max(1, normalizeNonNegativeInteger(item.occurrenceCount, 1)),
|
|
243
|
+
requestIds,
|
|
244
|
+
createdAt,
|
|
245
|
+
updatedAt: normalizeIsoTimestamp(item.updatedAt, createdAt),
|
|
246
|
+
metadata: normalizeMetadata(item.metadata),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function getQueueInitialState(nowIso = new Date().toISOString()) {
|
|
251
|
+
return {
|
|
252
|
+
schemaVersion: QUEUE_SCHEMA_VERSION,
|
|
253
|
+
generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
|
|
254
|
+
items: [],
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function getWorkerStateInitial(nowIso = new Date().toISOString()) {
|
|
259
|
+
return {
|
|
260
|
+
schemaVersion: STATE_SCHEMA_VERSION,
|
|
261
|
+
updatedAt: normalizeIsoTimestamp(nowIso, nowIso),
|
|
262
|
+
lastRunAt: null,
|
|
263
|
+
streamOffset: 0,
|
|
264
|
+
runCount: 0,
|
|
265
|
+
totalProcessedEvents: 0,
|
|
266
|
+
totalQueuedItems: 0,
|
|
267
|
+
totalDedupeHits: 0,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async function loadQueueFile(queuePath, nowIso = new Date().toISOString()) {
|
|
272
|
+
try {
|
|
273
|
+
const raw = await fsp.readFile(queuePath, "utf-8");
|
|
274
|
+
const parsed = JSON.parse(raw);
|
|
275
|
+
const items = Array.isArray(parsed.items)
|
|
276
|
+
? parsed.items
|
|
277
|
+
.map((item) => normalizeQueueItem(item, nowIso))
|
|
278
|
+
.filter((item) => normalizeString(item.fingerprint))
|
|
279
|
+
: [];
|
|
280
|
+
return {
|
|
281
|
+
schemaVersion: normalizeString(parsed.schemaVersion) || QUEUE_SCHEMA_VERSION,
|
|
282
|
+
generatedAt: normalizeIsoTimestamp(parsed.generatedAt, nowIso),
|
|
283
|
+
items,
|
|
284
|
+
};
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (error && typeof error === "object" && error.code === "ENOENT") {
|
|
287
|
+
return getQueueInitialState(nowIso);
|
|
288
|
+
}
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async function writeQueueFile(queuePath, queue = {}, nowIso = new Date().toISOString()) {
|
|
294
|
+
const normalized = {
|
|
295
|
+
schemaVersion: QUEUE_SCHEMA_VERSION,
|
|
296
|
+
generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
|
|
297
|
+
items: Array.isArray(queue.items)
|
|
298
|
+
? queue.items
|
|
299
|
+
.map((item) => normalizeQueueItem(item, nowIso))
|
|
300
|
+
.filter((item) => normalizeString(item.fingerprint))
|
|
301
|
+
: [],
|
|
302
|
+
};
|
|
303
|
+
await fsp.mkdir(path.dirname(queuePath), { recursive: true });
|
|
304
|
+
await fsp.writeFile(queuePath, `${JSON.stringify(normalized, null, 2)}\n`, "utf-8");
|
|
305
|
+
return normalized;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async function loadWorkerStateFile(statePath, nowIso = new Date().toISOString()) {
|
|
309
|
+
try {
|
|
310
|
+
const raw = await fsp.readFile(statePath, "utf-8");
|
|
311
|
+
const parsed = JSON.parse(raw);
|
|
312
|
+
return {
|
|
313
|
+
schemaVersion: normalizeString(parsed.schemaVersion) || STATE_SCHEMA_VERSION,
|
|
314
|
+
updatedAt: normalizeIsoTimestamp(parsed.updatedAt, nowIso),
|
|
315
|
+
lastRunAt: parsed.lastRunAt ? normalizeIsoTimestamp(parsed.lastRunAt, nowIso) : null,
|
|
316
|
+
streamOffset: normalizeNonNegativeInteger(parsed.streamOffset, 0),
|
|
317
|
+
runCount: normalizeNonNegativeInteger(parsed.runCount, 0),
|
|
318
|
+
totalProcessedEvents: normalizeNonNegativeInteger(parsed.totalProcessedEvents, 0),
|
|
319
|
+
totalQueuedItems: normalizeNonNegativeInteger(parsed.totalQueuedItems, 0),
|
|
320
|
+
totalDedupeHits: normalizeNonNegativeInteger(parsed.totalDedupeHits, 0),
|
|
321
|
+
};
|
|
322
|
+
} catch (error) {
|
|
323
|
+
if (error && typeof error === "object" && error.code === "ENOENT") {
|
|
324
|
+
return getWorkerStateInitial(nowIso);
|
|
325
|
+
}
|
|
326
|
+
throw error;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function writeWorkerStateFile(statePath, state = {}, nowIso = new Date().toISOString()) {
|
|
331
|
+
const normalized = {
|
|
332
|
+
schemaVersion: STATE_SCHEMA_VERSION,
|
|
333
|
+
updatedAt: normalizeIsoTimestamp(nowIso, nowIso),
|
|
334
|
+
lastRunAt: state.lastRunAt ? normalizeIsoTimestamp(state.lastRunAt, nowIso) : null,
|
|
335
|
+
streamOffset: normalizeNonNegativeInteger(state.streamOffset, 0),
|
|
336
|
+
runCount: normalizeNonNegativeInteger(state.runCount, 0),
|
|
337
|
+
totalProcessedEvents: normalizeNonNegativeInteger(state.totalProcessedEvents, 0),
|
|
338
|
+
totalQueuedItems: normalizeNonNegativeInteger(state.totalQueuedItems, 0),
|
|
339
|
+
totalDedupeHits: normalizeNonNegativeInteger(state.totalDedupeHits, 0),
|
|
340
|
+
};
|
|
341
|
+
await fsp.mkdir(path.dirname(statePath), { recursive: true });
|
|
342
|
+
await fsp.writeFile(statePath, `${JSON.stringify(normalized, null, 2)}\n`, "utf-8");
|
|
343
|
+
return normalized;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async function loadStreamLines(streamPath) {
|
|
347
|
+
try {
|
|
348
|
+
const raw = await fsp.readFile(streamPath, "utf-8");
|
|
349
|
+
return raw
|
|
350
|
+
.split(/\r?\n/)
|
|
351
|
+
.map((line) => line.trim())
|
|
352
|
+
.filter(Boolean);
|
|
353
|
+
} catch (error) {
|
|
354
|
+
if (error && typeof error === "object" && error.code === "ENOENT") {
|
|
355
|
+
return [];
|
|
356
|
+
}
|
|
357
|
+
throw error;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function buildWorkItemFromEvent(event, nowIso) {
|
|
362
|
+
return normalizeQueueItem(
|
|
363
|
+
{
|
|
364
|
+
workItemId: createWorkItemId(nowIso),
|
|
365
|
+
fingerprint: event.fingerprint,
|
|
366
|
+
dedupKey: event.dedupKey,
|
|
367
|
+
source: event.source,
|
|
368
|
+
service: event.service,
|
|
369
|
+
endpoint: event.endpoint,
|
|
370
|
+
errorCode: event.errorCode,
|
|
371
|
+
severity: event.severity,
|
|
372
|
+
status: "QUEUED",
|
|
373
|
+
message: event.message,
|
|
374
|
+
stackFingerprint: event.stackFingerprint,
|
|
375
|
+
commitSha: event.commitSha,
|
|
376
|
+
firstSeenAt: event.occurredAt,
|
|
377
|
+
lastSeenAt: event.occurredAt,
|
|
378
|
+
latestEventId: event.eventId,
|
|
379
|
+
occurrenceCount: 1,
|
|
380
|
+
requestIds: event.requestId ? [event.requestId] : [],
|
|
381
|
+
createdAt: nowIso,
|
|
382
|
+
updatedAt: nowIso,
|
|
383
|
+
metadata: {
|
|
384
|
+
...event.metadata,
|
|
385
|
+
sourceEventId: event.eventId,
|
|
386
|
+
requestId: event.requestId || null,
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
nowIso
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function normalizeErrorEvent(event = {}, nowIso = new Date().toISOString()) {
|
|
394
|
+
const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
|
|
395
|
+
const stackTrace = normalizeStackTrace(event.stackTrace || event.stack || "");
|
|
396
|
+
const requestId =
|
|
397
|
+
normalizeString(event.requestId || event.request_id) ||
|
|
398
|
+
normalizeString(event?.metadata?.requestId || event?.metadata?.request_id) ||
|
|
399
|
+
null;
|
|
400
|
+
const normalized = {
|
|
401
|
+
eventId: normalizeString(event.eventId) || randomUUID(),
|
|
402
|
+
occurredAt: normalizeIsoTimestamp(event.occurredAt, normalizedNow),
|
|
403
|
+
recordedAt: normalizedNow,
|
|
404
|
+
source: normalizeString(event.source) || "admin_error_log",
|
|
405
|
+
service: normalizeString(event.service) || "unknown-service",
|
|
406
|
+
endpoint: normalizeString(event.endpoint) || "unknown-endpoint",
|
|
407
|
+
errorCode: normalizeString(event.errorCode) || "UNKNOWN_ERROR",
|
|
408
|
+
severity: normalizeSeverity(event.severity),
|
|
409
|
+
message: normalizeString(event.message) || "Unhandled runtime error",
|
|
410
|
+
stackTrace,
|
|
411
|
+
stackFingerprint: computeStackFingerprint(stackTrace),
|
|
412
|
+
commitSha: normalizeString(event.commitSha || event.commit_sha) || null,
|
|
413
|
+
requestId,
|
|
414
|
+
metadata: normalizeMetadata(event.metadata),
|
|
415
|
+
};
|
|
416
|
+
const dedupKey = buildDedupKey(normalized);
|
|
417
|
+
return {
|
|
418
|
+
...normalized,
|
|
419
|
+
dedupKey,
|
|
420
|
+
fingerprint: computeFingerprint(normalized),
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export async function resolveErrorDaemonStorage({
|
|
425
|
+
targetPath = ".",
|
|
426
|
+
outputDir = "",
|
|
427
|
+
env,
|
|
428
|
+
homeDir,
|
|
429
|
+
} = {}) {
|
|
430
|
+
const outputRoot = await resolveOutputRoot({
|
|
431
|
+
cwd: path.resolve(String(targetPath || ".")),
|
|
432
|
+
outputDirOverride: outputDir,
|
|
433
|
+
env,
|
|
434
|
+
homeDir,
|
|
435
|
+
});
|
|
436
|
+
const observabilityRoot = path.join(outputRoot, "observability");
|
|
437
|
+
const baseDir = path.join(observabilityRoot, "error-daemon");
|
|
438
|
+
return {
|
|
439
|
+
outputRoot,
|
|
440
|
+
observabilityRoot,
|
|
441
|
+
baseDir,
|
|
442
|
+
streamPath: path.join(baseDir, "admin-error-stream.ndjson"),
|
|
443
|
+
queuePath: path.join(baseDir, "queue.json"),
|
|
444
|
+
statePath: path.join(baseDir, "worker-state.json"),
|
|
445
|
+
intakeDir: path.join(baseDir, "intake"),
|
|
446
|
+
runsDir: path.join(baseDir, "runs"),
|
|
447
|
+
sweepsDir: path.join(baseDir, "sweeps"),
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export async function appendAdminErrorEvent({
|
|
452
|
+
targetPath = ".",
|
|
453
|
+
outputDir = "",
|
|
454
|
+
event = {},
|
|
455
|
+
env,
|
|
456
|
+
homeDir,
|
|
457
|
+
} = {}) {
|
|
458
|
+
const nowIso = new Date().toISOString();
|
|
459
|
+
const normalizedEvent = normalizeErrorEvent(event, nowIso);
|
|
460
|
+
const storage = await resolveErrorDaemonStorage({
|
|
461
|
+
targetPath,
|
|
462
|
+
outputDir,
|
|
463
|
+
env,
|
|
464
|
+
homeDir,
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
await fsp.mkdir(storage.baseDir, { recursive: true });
|
|
468
|
+
await fsp.appendFile(storage.streamPath, `${JSON.stringify(normalizedEvent)}\n`, "utf-8");
|
|
469
|
+
|
|
470
|
+
await fsp.mkdir(storage.intakeDir, { recursive: true });
|
|
471
|
+
const intakePath = path.join(
|
|
472
|
+
storage.intakeDir,
|
|
473
|
+
`intake-${stableTimestampForFile(new Date(normalizedEvent.recordedAt))}-${normalizedEvent.eventId.slice(0, 8)}.json`
|
|
474
|
+
);
|
|
475
|
+
await fsp.writeFile(
|
|
476
|
+
intakePath,
|
|
477
|
+
`${JSON.stringify(
|
|
478
|
+
{
|
|
479
|
+
schemaVersion: "1.0.0",
|
|
480
|
+
generatedAt: nowIso,
|
|
481
|
+
event: normalizedEvent,
|
|
482
|
+
},
|
|
483
|
+
null,
|
|
484
|
+
2
|
|
485
|
+
)}\n`,
|
|
486
|
+
"utf-8"
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
return {
|
|
490
|
+
outputRoot: storage.outputRoot,
|
|
491
|
+
streamPath: storage.streamPath,
|
|
492
|
+
intakePath,
|
|
493
|
+
event: normalizedEvent,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function resolveWorkItemIntakePath(storage, queueItem, nowIso = new Date().toISOString()) {
|
|
498
|
+
const dayKey = buildDayKey(queueItem.firstSeenAt || nowIso);
|
|
499
|
+
return path.join(storage.observabilityRoot, dayKey, queueItem.workItemId, "intake_event.json");
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
async function writeWorkItemIntakeArtifact(
|
|
503
|
+
storage,
|
|
504
|
+
queueItem,
|
|
505
|
+
{
|
|
506
|
+
nowIso = new Date().toISOString(),
|
|
507
|
+
} = {}
|
|
508
|
+
) {
|
|
509
|
+
const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
|
|
510
|
+
const intakePath = resolveWorkItemIntakePath(storage, queueItem, normalizedNow);
|
|
511
|
+
await fsp.mkdir(path.dirname(intakePath), { recursive: true });
|
|
512
|
+
const normalizedRequestIds = normalizeRequestIds(queueItem.requestIds || []);
|
|
513
|
+
const payload = {
|
|
514
|
+
schemaVersion: "1.0.0",
|
|
515
|
+
generated_at: normalizedNow,
|
|
516
|
+
work_item_id: queueItem.workItemId,
|
|
517
|
+
service: queueItem.service,
|
|
518
|
+
endpoint: queueItem.endpoint,
|
|
519
|
+
error_code: queueItem.errorCode,
|
|
520
|
+
stack_fingerprint: queueItem.stackFingerprint,
|
|
521
|
+
commit_sha: queueItem.commitSha,
|
|
522
|
+
first_seen_at: queueItem.firstSeenAt,
|
|
523
|
+
occurrence_count: Math.max(1, Number(queueItem.occurrenceCount || 1)),
|
|
524
|
+
last_seen_at: queueItem.lastSeenAt,
|
|
525
|
+
dedup_key:
|
|
526
|
+
normalizeString(queueItem.dedupKey) ||
|
|
527
|
+
buildDedupKey({
|
|
528
|
+
service: queueItem.service,
|
|
529
|
+
endpoint: queueItem.endpoint,
|
|
530
|
+
errorCode: queueItem.errorCode,
|
|
531
|
+
stackFingerprint: queueItem.stackFingerprint,
|
|
532
|
+
commitSha: queueItem.commitSha,
|
|
533
|
+
}),
|
|
534
|
+
request_ids: normalizedRequestIds,
|
|
535
|
+
fingerprint: queueItem.fingerprint,
|
|
536
|
+
severity: queueItem.severity,
|
|
537
|
+
status: queueItem.status,
|
|
538
|
+
message: queueItem.message,
|
|
539
|
+
};
|
|
540
|
+
await fsp.writeFile(intakePath, `${JSON.stringify(payload, null, 2)}\n`, "utf-8");
|
|
541
|
+
return {
|
|
542
|
+
intakePath,
|
|
543
|
+
payload,
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
async function emitDaemonSessionEvent(
|
|
548
|
+
sessionId,
|
|
549
|
+
payload = {},
|
|
550
|
+
{
|
|
551
|
+
event = "agent_intake",
|
|
552
|
+
targetPath = ".",
|
|
553
|
+
nowIso = new Date().toISOString(),
|
|
554
|
+
} = {}
|
|
555
|
+
) {
|
|
556
|
+
const normalizedSessionId = normalizeString(sessionId);
|
|
557
|
+
if (!normalizedSessionId) {
|
|
558
|
+
return null;
|
|
559
|
+
}
|
|
560
|
+
const normalizedPayload =
|
|
561
|
+
payload && typeof payload === "object" && !Array.isArray(payload) ? { ...payload } : {};
|
|
562
|
+
const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
|
|
563
|
+
const envelope = createAgentEvent({
|
|
564
|
+
event: normalizeString(event) || "agent_intake",
|
|
565
|
+
agentId: ERROR_DAEMON_AGENT_ID,
|
|
566
|
+
sessionId: normalizedSessionId,
|
|
567
|
+
ts: normalizedNow,
|
|
568
|
+
workItemId: normalizedPayload.workItemId || normalizedPayload.work_item_id || undefined,
|
|
569
|
+
payload: normalizedPayload,
|
|
570
|
+
});
|
|
571
|
+
await appendToStream(normalizedSessionId, envelope, {
|
|
572
|
+
targetPath: path.resolve(String(targetPath || ".")),
|
|
573
|
+
});
|
|
574
|
+
return envelope;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
export async function getErrorDaemonState({
|
|
578
|
+
targetPath = ".",
|
|
579
|
+
outputDir = "",
|
|
580
|
+
env,
|
|
581
|
+
homeDir,
|
|
582
|
+
} = {}) {
|
|
583
|
+
const storage = await resolveErrorDaemonStorage({
|
|
584
|
+
targetPath,
|
|
585
|
+
outputDir,
|
|
586
|
+
env,
|
|
587
|
+
homeDir,
|
|
588
|
+
});
|
|
589
|
+
const state = await loadWorkerStateFile(storage.statePath);
|
|
590
|
+
return {
|
|
591
|
+
...storage,
|
|
592
|
+
state,
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
export async function runErrorDaemonWorker({
|
|
597
|
+
targetPath = ".",
|
|
598
|
+
outputDir = "",
|
|
599
|
+
maxEvents = DEFAULT_MAX_EVENTS,
|
|
600
|
+
sessionId = "",
|
|
601
|
+
wakeMode = WAKE_MODES.REALTIME,
|
|
602
|
+
env,
|
|
603
|
+
homeDir,
|
|
604
|
+
nowIso = new Date().toISOString(),
|
|
605
|
+
} = {}) {
|
|
606
|
+
const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
|
|
607
|
+
const normalizedMaxEvents = normalizePositiveInteger(maxEvents, DEFAULT_MAX_EVENTS);
|
|
608
|
+
const normalizedWakeMode = normalizeWakeMode(wakeMode, WAKE_MODES.REALTIME);
|
|
609
|
+
const normalizedSessionId = normalizeString(sessionId);
|
|
610
|
+
const resolvedTargetPath = path.resolve(String(targetPath || "."));
|
|
611
|
+
const storage = await resolveErrorDaemonStorage({
|
|
612
|
+
targetPath: resolvedTargetPath,
|
|
613
|
+
outputDir,
|
|
614
|
+
env,
|
|
615
|
+
homeDir,
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
const [lines, queue, state] = await Promise.all([
|
|
619
|
+
loadStreamLines(storage.streamPath),
|
|
620
|
+
loadQueueFile(storage.queuePath, normalizedNow),
|
|
621
|
+
loadWorkerStateFile(storage.statePath, normalizedNow),
|
|
622
|
+
]);
|
|
623
|
+
|
|
624
|
+
const startOffset = Math.max(0, Math.min(state.streamOffset, lines.length));
|
|
625
|
+
const endOffset = Math.min(lines.length, startOffset + normalizedMaxEvents);
|
|
626
|
+
const openItemsByFingerprint = new Map();
|
|
627
|
+
for (const item of queue.items) {
|
|
628
|
+
const fingerprint = normalizeString(item.fingerprint);
|
|
629
|
+
if (!fingerprint || isTerminalStatus(item.status)) {
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
if (!openItemsByFingerprint.has(fingerprint)) {
|
|
633
|
+
openItemsByFingerprint.set(fingerprint, item);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
let processedCount = 0;
|
|
638
|
+
let queuedCount = 0;
|
|
639
|
+
let dedupedCount = 0;
|
|
640
|
+
let parseErrorCount = 0;
|
|
641
|
+
const intakeMutations = [];
|
|
642
|
+
for (let index = startOffset; index < endOffset; index += 1) {
|
|
643
|
+
const rawLine = lines[index];
|
|
644
|
+
if (!rawLine) {
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
let parsed = null;
|
|
649
|
+
try {
|
|
650
|
+
parsed = JSON.parse(rawLine);
|
|
651
|
+
} catch {
|
|
652
|
+
parseErrorCount += 1;
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
const event = normalizeErrorEvent(parsed, normalizedNow);
|
|
656
|
+
processedCount += 1;
|
|
657
|
+
const existing = openItemsByFingerprint.get(event.fingerprint);
|
|
658
|
+
if (existing) {
|
|
659
|
+
dedupedCount += 1;
|
|
660
|
+
existing.occurrenceCount = Math.max(1, Number(existing.occurrenceCount || 1)) + 1;
|
|
661
|
+
existing.lastSeenAt = event.occurredAt;
|
|
662
|
+
existing.updatedAt = normalizedNow;
|
|
663
|
+
existing.latestEventId = event.eventId;
|
|
664
|
+
existing.severity = chooseHigherSeverity(existing.severity, event.severity);
|
|
665
|
+
existing.dedupKey =
|
|
666
|
+
normalizeString(existing.dedupKey) ||
|
|
667
|
+
normalizeString(event.dedupKey) ||
|
|
668
|
+
buildDedupKey(event);
|
|
669
|
+
existing.requestIds = mergeRequestIds(existing.requestIds, event.requestId ? [event.requestId] : []);
|
|
670
|
+
if (event.message) {
|
|
671
|
+
existing.message = event.message;
|
|
672
|
+
}
|
|
673
|
+
existing.metadata = {
|
|
674
|
+
...normalizeMetadata(existing.metadata),
|
|
675
|
+
...normalizeMetadata(event.metadata),
|
|
676
|
+
sourceEventId: event.eventId,
|
|
677
|
+
requestId: event.requestId || null,
|
|
678
|
+
};
|
|
679
|
+
intakeMutations.push({
|
|
680
|
+
action: "dedup",
|
|
681
|
+
queueItem: existing,
|
|
682
|
+
sourceEvent: event,
|
|
683
|
+
});
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
const queued = buildWorkItemFromEvent(event, normalizedNow);
|
|
688
|
+
queue.items.push(queued);
|
|
689
|
+
openItemsByFingerprint.set(queued.fingerprint, queued);
|
|
690
|
+
queuedCount += 1;
|
|
691
|
+
intakeMutations.push({
|
|
692
|
+
action: "new",
|
|
693
|
+
queueItem: queued,
|
|
694
|
+
sourceEvent: event,
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
queue.generatedAt = normalizedNow;
|
|
699
|
+
const savedQueue = await writeQueueFile(storage.queuePath, queue, normalizedNow);
|
|
700
|
+
|
|
701
|
+
const savedState = await writeWorkerStateFile(
|
|
702
|
+
storage.statePath,
|
|
703
|
+
{
|
|
704
|
+
...state,
|
|
705
|
+
updatedAt: normalizedNow,
|
|
706
|
+
lastRunAt: normalizedNow,
|
|
707
|
+
streamOffset: endOffset,
|
|
708
|
+
runCount: state.runCount + 1,
|
|
709
|
+
totalProcessedEvents: state.totalProcessedEvents + processedCount,
|
|
710
|
+
totalQueuedItems: savedQueue.items.length,
|
|
711
|
+
totalDedupeHits: state.totalDedupeHits + dedupedCount,
|
|
712
|
+
},
|
|
713
|
+
normalizedNow
|
|
714
|
+
);
|
|
715
|
+
|
|
716
|
+
const intakeArtifacts = [];
|
|
717
|
+
const emittedEvents = [];
|
|
718
|
+
for (const mutation of intakeMutations) {
|
|
719
|
+
const written = await writeWorkItemIntakeArtifact(storage, mutation.queueItem, {
|
|
720
|
+
nowIso: normalizedNow,
|
|
721
|
+
});
|
|
722
|
+
intakeArtifacts.push({
|
|
723
|
+
action: mutation.action,
|
|
724
|
+
workItemId: mutation.queueItem.workItemId,
|
|
725
|
+
intakePath: written.intakePath,
|
|
726
|
+
dedupKey: written.payload.dedup_key,
|
|
727
|
+
occurrenceCount: written.payload.occurrence_count,
|
|
728
|
+
});
|
|
729
|
+
if (normalizedSessionId) {
|
|
730
|
+
const emitted = await emitDaemonSessionEvent(
|
|
731
|
+
normalizedSessionId,
|
|
732
|
+
{
|
|
733
|
+
action: mutation.action,
|
|
734
|
+
wakeMode: normalizedWakeMode,
|
|
735
|
+
source: mutation.sourceEvent.source,
|
|
736
|
+
eventId: mutation.sourceEvent.eventId,
|
|
737
|
+
workItemId: mutation.queueItem.workItemId,
|
|
738
|
+
service: mutation.queueItem.service,
|
|
739
|
+
endpoint: mutation.queueItem.endpoint,
|
|
740
|
+
errorCode: mutation.queueItem.errorCode,
|
|
741
|
+
severity: mutation.queueItem.severity,
|
|
742
|
+
stackFingerprint: mutation.queueItem.stackFingerprint,
|
|
743
|
+
commitSha: mutation.queueItem.commitSha,
|
|
744
|
+
dedupKey:
|
|
745
|
+
normalizeString(mutation.queueItem.dedupKey) ||
|
|
746
|
+
buildDedupKey(mutation.queueItem),
|
|
747
|
+
occurrenceCount: Math.max(1, Number(mutation.queueItem.occurrenceCount || 1)),
|
|
748
|
+
requestId: mutation.sourceEvent.requestId || null,
|
|
749
|
+
},
|
|
750
|
+
{
|
|
751
|
+
event: "agent_intake",
|
|
752
|
+
targetPath: resolvedTargetPath,
|
|
753
|
+
nowIso: normalizedNow,
|
|
754
|
+
}
|
|
755
|
+
);
|
|
756
|
+
if (emitted) {
|
|
757
|
+
emittedEvents.push(emitted);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
await fsp.mkdir(storage.runsDir, { recursive: true });
|
|
763
|
+
const runId = `error-daemon-run-${stableTimestampForFile(new Date(normalizedNow))}-${randomUUID().slice(0, 8)}`;
|
|
764
|
+
const runPath = path.join(storage.runsDir, `${runId}.json`);
|
|
765
|
+
await fsp.writeFile(
|
|
766
|
+
runPath,
|
|
767
|
+
`${JSON.stringify(
|
|
768
|
+
{
|
|
769
|
+
schemaVersion: "1.0.0",
|
|
770
|
+
generatedAt: normalizedNow,
|
|
771
|
+
runId,
|
|
772
|
+
wakeMode: normalizedWakeMode,
|
|
773
|
+
sessionId: normalizedSessionId || null,
|
|
774
|
+
streamPath: storage.streamPath,
|
|
775
|
+
queuePath: storage.queuePath,
|
|
776
|
+
statePath: storage.statePath,
|
|
777
|
+
startOffset,
|
|
778
|
+
endOffset,
|
|
779
|
+
streamLength: lines.length,
|
|
780
|
+
maxEvents: normalizedMaxEvents,
|
|
781
|
+
processedCount,
|
|
782
|
+
queuedCount,
|
|
783
|
+
dedupedCount,
|
|
784
|
+
parseErrorCount,
|
|
785
|
+
queueDepth: savedQueue.items.length,
|
|
786
|
+
intakeArtifactCount: intakeArtifacts.length,
|
|
787
|
+
emittedEventCount: emittedEvents.length,
|
|
788
|
+
},
|
|
789
|
+
null,
|
|
790
|
+
2
|
|
791
|
+
)}\n`,
|
|
792
|
+
"utf-8"
|
|
793
|
+
);
|
|
794
|
+
|
|
795
|
+
return {
|
|
796
|
+
...storage,
|
|
797
|
+
runId,
|
|
798
|
+
runPath,
|
|
799
|
+
wakeMode: normalizedWakeMode,
|
|
800
|
+
sessionId: normalizedSessionId || null,
|
|
801
|
+
maxEvents: normalizedMaxEvents,
|
|
802
|
+
startOffset,
|
|
803
|
+
endOffset,
|
|
804
|
+
streamLength: lines.length,
|
|
805
|
+
processedCount,
|
|
806
|
+
queuedCount,
|
|
807
|
+
dedupedCount,
|
|
808
|
+
parseErrorCount,
|
|
809
|
+
queueDepth: savedQueue.items.length,
|
|
810
|
+
intakeArtifacts,
|
|
811
|
+
emittedEvents,
|
|
812
|
+
queue: savedQueue,
|
|
813
|
+
state: savedState,
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
function buildDaemonInstanceKey({
|
|
818
|
+
targetPath = ".",
|
|
819
|
+
sessionId = "",
|
|
820
|
+
wakeMode = WAKE_MODES.REALTIME,
|
|
821
|
+
} = {}) {
|
|
822
|
+
const resolvedTargetPath = path.resolve(String(targetPath || "."));
|
|
823
|
+
const normalizedSessionId = normalizeString(sessionId) || "no-session";
|
|
824
|
+
const normalizedWakeMode = normalizeWakeMode(wakeMode, WAKE_MODES.REALTIME);
|
|
825
|
+
return `${resolvedTargetPath}::${normalizedSessionId}::${normalizedWakeMode}`;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function buildSeverityCounts(items = []) {
|
|
829
|
+
const counts = {
|
|
830
|
+
UNKNOWN: 0,
|
|
831
|
+
P3: 0,
|
|
832
|
+
P2: 0,
|
|
833
|
+
P1: 0,
|
|
834
|
+
P0: 0,
|
|
835
|
+
};
|
|
836
|
+
for (const item of items) {
|
|
837
|
+
const normalizedSeverity = normalizeSeverity(item?.severity);
|
|
838
|
+
counts[normalizedSeverity] = (counts[normalizedSeverity] || 0) + 1;
|
|
839
|
+
}
|
|
840
|
+
return counts;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
export async function scheduledErrorSweep({
|
|
844
|
+
targetPath = ".",
|
|
845
|
+
outputDir = "",
|
|
846
|
+
sessionId = "",
|
|
847
|
+
region = "global",
|
|
848
|
+
tz = "UTC",
|
|
849
|
+
env,
|
|
850
|
+
homeDir,
|
|
851
|
+
nowIso = new Date().toISOString(),
|
|
852
|
+
} = {}) {
|
|
853
|
+
const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
|
|
854
|
+
const resolvedTargetPath = path.resolve(String(targetPath || "."));
|
|
855
|
+
const normalizedSessionId = normalizeString(sessionId);
|
|
856
|
+
const normalizedRegion = normalizeString(region) || "global";
|
|
857
|
+
const normalizedTz = normalizeString(tz) || "UTC";
|
|
858
|
+
const storage = await resolveErrorDaemonStorage({
|
|
859
|
+
targetPath: resolvedTargetPath,
|
|
860
|
+
outputDir,
|
|
861
|
+
env,
|
|
862
|
+
homeDir,
|
|
863
|
+
});
|
|
864
|
+
const queue = await loadQueueFile(storage.queuePath, normalizedNow);
|
|
865
|
+
const activeItems = queue.items.filter((item) => !isTerminalStatus(item.status));
|
|
866
|
+
const severityCounts = buildSeverityCounts(activeItems);
|
|
867
|
+
const totalOccurrences = activeItems.reduce(
|
|
868
|
+
(sum, item) => sum + Math.max(1, Number(item.occurrenceCount || 1)),
|
|
869
|
+
0
|
|
870
|
+
);
|
|
871
|
+
const runId = `error-daemon-sweep-${stableTimestampForFile(new Date(normalizedNow))}-${randomUUID().slice(0, 8)}`;
|
|
872
|
+
|
|
873
|
+
const digest = {
|
|
874
|
+
schemaVersion: "1.0.0",
|
|
875
|
+
generatedAt: normalizedNow,
|
|
876
|
+
runId,
|
|
877
|
+
wakeMode: WAKE_MODES.SCHEDULED,
|
|
878
|
+
region: normalizedRegion,
|
|
879
|
+
tz: normalizedTz,
|
|
880
|
+
queueDepth: activeItems.length,
|
|
881
|
+
totalOccurrences,
|
|
882
|
+
severityCounts,
|
|
883
|
+
workItems: activeItems.map((item) => ({
|
|
884
|
+
workItemId: item.workItemId,
|
|
885
|
+
service: item.service,
|
|
886
|
+
endpoint: item.endpoint,
|
|
887
|
+
errorCode: item.errorCode,
|
|
888
|
+
severity: item.severity,
|
|
889
|
+
status: item.status,
|
|
890
|
+
occurrenceCount: item.occurrenceCount,
|
|
891
|
+
dedupKey: item.dedupKey,
|
|
892
|
+
lastSeenAt: item.lastSeenAt,
|
|
893
|
+
})),
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
await fsp.mkdir(storage.sweepsDir, { recursive: true });
|
|
897
|
+
const sweepPath = path.join(storage.sweepsDir, `${runId}.json`);
|
|
898
|
+
await fsp.writeFile(sweepPath, `${JSON.stringify(digest, null, 2)}\n`, "utf-8");
|
|
899
|
+
|
|
900
|
+
let sweepEvent = null;
|
|
901
|
+
if (normalizedSessionId) {
|
|
902
|
+
sweepEvent = await emitDaemonSessionEvent(
|
|
903
|
+
normalizedSessionId,
|
|
904
|
+
{
|
|
905
|
+
action: "scheduled_rollup",
|
|
906
|
+
wakeMode: WAKE_MODES.SCHEDULED,
|
|
907
|
+
region: normalizedRegion,
|
|
908
|
+
tz: normalizedTz,
|
|
909
|
+
queueDepth: activeItems.length,
|
|
910
|
+
totalOccurrences,
|
|
911
|
+
severityCounts,
|
|
912
|
+
runId,
|
|
913
|
+
},
|
|
914
|
+
{
|
|
915
|
+
event: "agent_intake",
|
|
916
|
+
targetPath: resolvedTargetPath,
|
|
917
|
+
nowIso: normalizedNow,
|
|
918
|
+
}
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
return {
|
|
923
|
+
...storage,
|
|
924
|
+
runId,
|
|
925
|
+
sweepPath,
|
|
926
|
+
wakeMode: WAKE_MODES.SCHEDULED,
|
|
927
|
+
region: normalizedRegion,
|
|
928
|
+
tz: normalizedTz,
|
|
929
|
+
queueDepth: activeItems.length,
|
|
930
|
+
totalOccurrences,
|
|
931
|
+
severityCounts,
|
|
932
|
+
digest,
|
|
933
|
+
event: sweepEvent,
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
export async function startErrorEventDaemon({
|
|
938
|
+
sessionId = "",
|
|
939
|
+
wakeMode = WAKE_MODES.REALTIME,
|
|
940
|
+
targetPath = ".",
|
|
941
|
+
outputDir = "",
|
|
942
|
+
pollMs = DEFAULT_DAEMON_POLL_MS,
|
|
943
|
+
sweepIntervalMs = DEFAULT_SWEEP_INTERVAL_MS,
|
|
944
|
+
maxEvents = DEFAULT_MAX_EVENTS,
|
|
945
|
+
autoStart = true,
|
|
946
|
+
region = "global",
|
|
947
|
+
tz = "UTC",
|
|
948
|
+
env,
|
|
949
|
+
homeDir,
|
|
950
|
+
} = {}) {
|
|
951
|
+
const normalizedWakeMode = normalizeWakeMode(wakeMode, WAKE_MODES.REALTIME);
|
|
952
|
+
const normalizedSessionId = normalizeString(sessionId);
|
|
953
|
+
const resolvedTargetPath = path.resolve(String(targetPath || "."));
|
|
954
|
+
const daemonKey = buildDaemonInstanceKey({
|
|
955
|
+
targetPath: resolvedTargetPath,
|
|
956
|
+
sessionId: normalizedSessionId,
|
|
957
|
+
wakeMode: normalizedWakeMode,
|
|
958
|
+
});
|
|
959
|
+
const existing = ACTIVE_DAEMONS.get(daemonKey);
|
|
960
|
+
if (existing) {
|
|
961
|
+
return existing.handle;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
const controller = new AbortController();
|
|
965
|
+
const startedAt = new Date().toISOString();
|
|
966
|
+
const normalizedPollMs = normalizePositiveInteger(pollMs, DEFAULT_DAEMON_POLL_MS);
|
|
967
|
+
const normalizedSweepIntervalMs = normalizePositiveInteger(
|
|
968
|
+
sweepIntervalMs,
|
|
969
|
+
DEFAULT_SWEEP_INTERVAL_MS
|
|
970
|
+
);
|
|
971
|
+
|
|
972
|
+
const daemonState = {
|
|
973
|
+
daemonKey,
|
|
974
|
+
sessionId: normalizedSessionId || null,
|
|
975
|
+
wakeMode: normalizedWakeMode,
|
|
976
|
+
targetPath: resolvedTargetPath,
|
|
977
|
+
startedAt,
|
|
978
|
+
running: true,
|
|
979
|
+
timer: null,
|
|
980
|
+
inFlight: false,
|
|
981
|
+
lastTickAt: null,
|
|
982
|
+
lastTickResult: null,
|
|
983
|
+
stopReason: null,
|
|
984
|
+
};
|
|
985
|
+
|
|
986
|
+
const runTick = async () => {
|
|
987
|
+
if (!daemonState.running || controller.signal.aborted || daemonState.inFlight) {
|
|
988
|
+
return daemonState.lastTickResult;
|
|
989
|
+
}
|
|
990
|
+
daemonState.inFlight = true;
|
|
991
|
+
try {
|
|
992
|
+
const result =
|
|
993
|
+
normalizedWakeMode === WAKE_MODES.SCHEDULED
|
|
994
|
+
? await scheduledErrorSweep({
|
|
995
|
+
targetPath: resolvedTargetPath,
|
|
996
|
+
outputDir,
|
|
997
|
+
sessionId: normalizedSessionId,
|
|
998
|
+
region,
|
|
999
|
+
tz,
|
|
1000
|
+
env,
|
|
1001
|
+
homeDir,
|
|
1002
|
+
})
|
|
1003
|
+
: await runErrorDaemonWorker({
|
|
1004
|
+
targetPath: resolvedTargetPath,
|
|
1005
|
+
outputDir,
|
|
1006
|
+
sessionId: normalizedSessionId,
|
|
1007
|
+
wakeMode: normalizedWakeMode,
|
|
1008
|
+
maxEvents,
|
|
1009
|
+
env,
|
|
1010
|
+
homeDir,
|
|
1011
|
+
});
|
|
1012
|
+
daemonState.lastTickAt = new Date().toISOString();
|
|
1013
|
+
daemonState.lastTickResult = result;
|
|
1014
|
+
return result;
|
|
1015
|
+
} finally {
|
|
1016
|
+
daemonState.inFlight = false;
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
|
|
1020
|
+
const stop = async (reason = "manual") => {
|
|
1021
|
+
if (!daemonState.running) {
|
|
1022
|
+
return {
|
|
1023
|
+
stopped: false,
|
|
1024
|
+
daemonKey,
|
|
1025
|
+
reason: daemonState.stopReason || normalizeString(reason) || "manual",
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
daemonState.running = false;
|
|
1029
|
+
daemonState.stopReason = normalizeString(reason) || "manual";
|
|
1030
|
+
controller.abort();
|
|
1031
|
+
if (daemonState.timer) {
|
|
1032
|
+
clearInterval(daemonState.timer);
|
|
1033
|
+
daemonState.timer = null;
|
|
1034
|
+
}
|
|
1035
|
+
ACTIVE_DAEMONS.delete(daemonKey);
|
|
1036
|
+
let event = null;
|
|
1037
|
+
if (normalizedSessionId) {
|
|
1038
|
+
event = await emitDaemonSessionEvent(
|
|
1039
|
+
normalizedSessionId,
|
|
1040
|
+
{
|
|
1041
|
+
target: ERROR_DAEMON_AGENT_ID,
|
|
1042
|
+
reason: daemonState.stopReason,
|
|
1043
|
+
wakeMode: normalizedWakeMode,
|
|
1044
|
+
},
|
|
1045
|
+
{
|
|
1046
|
+
event: "agent_killed",
|
|
1047
|
+
targetPath: resolvedTargetPath,
|
|
1048
|
+
nowIso: new Date().toISOString(),
|
|
1049
|
+
}
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
return {
|
|
1053
|
+
stopped: true,
|
|
1054
|
+
daemonKey,
|
|
1055
|
+
sessionId: normalizedSessionId || null,
|
|
1056
|
+
wakeMode: normalizedWakeMode,
|
|
1057
|
+
stoppedAt: new Date().toISOString(),
|
|
1058
|
+
reason: daemonState.stopReason,
|
|
1059
|
+
event,
|
|
1060
|
+
};
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
const handle = {
|
|
1064
|
+
daemonKey,
|
|
1065
|
+
sessionId: normalizedSessionId || null,
|
|
1066
|
+
wakeMode: normalizedWakeMode,
|
|
1067
|
+
targetPath: resolvedTargetPath,
|
|
1068
|
+
startedAt,
|
|
1069
|
+
signal: controller.signal,
|
|
1070
|
+
runTick,
|
|
1071
|
+
stop,
|
|
1072
|
+
isRunning: () => daemonState.running,
|
|
1073
|
+
getState: () => ({
|
|
1074
|
+
daemonKey,
|
|
1075
|
+
sessionId: normalizedSessionId || null,
|
|
1076
|
+
wakeMode: normalizedWakeMode,
|
|
1077
|
+
targetPath: resolvedTargetPath,
|
|
1078
|
+
startedAt,
|
|
1079
|
+
running: daemonState.running,
|
|
1080
|
+
lastTickAt: daemonState.lastTickAt,
|
|
1081
|
+
stopReason: daemonState.stopReason,
|
|
1082
|
+
}),
|
|
1083
|
+
};
|
|
1084
|
+
ACTIVE_DAEMONS.set(daemonKey, {
|
|
1085
|
+
...daemonState,
|
|
1086
|
+
handle,
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
if (autoStart) {
|
|
1090
|
+
await runTick();
|
|
1091
|
+
const intervalMs =
|
|
1092
|
+
normalizedWakeMode === WAKE_MODES.SCHEDULED ? normalizedSweepIntervalMs : normalizedPollMs;
|
|
1093
|
+
daemonState.timer = setInterval(() => {
|
|
1094
|
+
void runTick().catch(() => {});
|
|
1095
|
+
}, intervalMs);
|
|
1096
|
+
if (typeof daemonState.timer.unref === "function") {
|
|
1097
|
+
daemonState.timer.unref();
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
return handle;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
export async function stopErrorEventDaemon({
|
|
1105
|
+
targetPath = ".",
|
|
1106
|
+
sessionId = "",
|
|
1107
|
+
wakeMode = "",
|
|
1108
|
+
reason = "manual",
|
|
1109
|
+
} = {}) {
|
|
1110
|
+
const resolvedTargetPath = path.resolve(String(targetPath || "."));
|
|
1111
|
+
const normalizedSessionId = normalizeString(sessionId);
|
|
1112
|
+
const normalizedReason = normalizeString(reason) || "manual";
|
|
1113
|
+
const normalizedWakeMode = normalizeString(wakeMode)
|
|
1114
|
+
? normalizeWakeMode(wakeMode, WAKE_MODES.REALTIME)
|
|
1115
|
+
: "";
|
|
1116
|
+
|
|
1117
|
+
const matching = [];
|
|
1118
|
+
for (const [key, value] of ACTIVE_DAEMONS.entries()) {
|
|
1119
|
+
const sameTargetPath = value.targetPath === resolvedTargetPath;
|
|
1120
|
+
const sameSession =
|
|
1121
|
+
!normalizedSessionId || normalizeString(value.sessionId) === normalizedSessionId;
|
|
1122
|
+
const sameWakeMode = !normalizedWakeMode || value.wakeMode === normalizedWakeMode;
|
|
1123
|
+
if (sameTargetPath && sameSession && sameWakeMode) {
|
|
1124
|
+
matching.push({ key, value });
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
if (matching.length === 0) {
|
|
1129
|
+
return {
|
|
1130
|
+
stopped: false,
|
|
1131
|
+
count: 0,
|
|
1132
|
+
targetPath: resolvedTargetPath,
|
|
1133
|
+
sessionId: normalizedSessionId || null,
|
|
1134
|
+
wakeMode: normalizedWakeMode || null,
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
const stopped = [];
|
|
1139
|
+
for (const match of matching) {
|
|
1140
|
+
const result = await match.value.handle.stop(normalizedReason);
|
|
1141
|
+
stopped.push(result);
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
return {
|
|
1145
|
+
stopped: true,
|
|
1146
|
+
count: stopped.length,
|
|
1147
|
+
targetPath: resolvedTargetPath,
|
|
1148
|
+
sessionId: normalizedSessionId || null,
|
|
1149
|
+
wakeMode: normalizedWakeMode || null,
|
|
1150
|
+
daemons: stopped,
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
function parseStatusFilter(statuses = []) {
|
|
1155
|
+
if (!Array.isArray(statuses)) {
|
|
1156
|
+
return null;
|
|
1157
|
+
}
|
|
1158
|
+
const normalized = statuses
|
|
1159
|
+
.map((value) => normalizeString(value).toUpperCase())
|
|
1160
|
+
.filter(Boolean)
|
|
1161
|
+
.filter((value) => WORK_ITEM_STATUS_SET.has(value));
|
|
1162
|
+
if (normalized.length === 0) {
|
|
1163
|
+
return null;
|
|
1164
|
+
}
|
|
1165
|
+
return new Set(normalized);
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
export async function listErrorQueue({
|
|
1169
|
+
targetPath = ".",
|
|
1170
|
+
outputDir = "",
|
|
1171
|
+
statuses = [],
|
|
1172
|
+
limit = 50,
|
|
1173
|
+
env,
|
|
1174
|
+
homeDir,
|
|
1175
|
+
} = {}) {
|
|
1176
|
+
const normalizedLimit = normalizePositiveInteger(limit, 50);
|
|
1177
|
+
const storage = await resolveErrorDaemonStorage({
|
|
1178
|
+
targetPath,
|
|
1179
|
+
outputDir,
|
|
1180
|
+
env,
|
|
1181
|
+
homeDir,
|
|
1182
|
+
});
|
|
1183
|
+
const [queue, state] = await Promise.all([
|
|
1184
|
+
loadQueueFile(storage.queuePath),
|
|
1185
|
+
loadWorkerStateFile(storage.statePath),
|
|
1186
|
+
]);
|
|
1187
|
+
|
|
1188
|
+
const statusFilter = parseStatusFilter(statuses);
|
|
1189
|
+
const sorted = [...queue.items].sort((left, right) => {
|
|
1190
|
+
const severityDelta = toSeverityRank(right.severity) - toSeverityRank(left.severity);
|
|
1191
|
+
if (severityDelta !== 0) {
|
|
1192
|
+
return severityDelta;
|
|
1193
|
+
}
|
|
1194
|
+
const leftTs = Date.parse(String(left.lastSeenAt || left.firstSeenAt || "")) || 0;
|
|
1195
|
+
const rightTs = Date.parse(String(right.lastSeenAt || right.firstSeenAt || "")) || 0;
|
|
1196
|
+
return rightTs - leftTs;
|
|
1197
|
+
});
|
|
1198
|
+
const filtered = statusFilter
|
|
1199
|
+
? sorted.filter((item) => statusFilter.has(normalizeWorkItemStatus(item.status)))
|
|
1200
|
+
: sorted;
|
|
1201
|
+
|
|
1202
|
+
return {
|
|
1203
|
+
...storage,
|
|
1204
|
+
state,
|
|
1205
|
+
totalCount: sorted.length,
|
|
1206
|
+
filteredCount: filtered.length,
|
|
1207
|
+
items: filtered.slice(0, normalizedLimit),
|
|
1208
|
+
};
|
|
1209
|
+
}
|