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.
Files changed (280) hide show
  1. package/README.md +1009 -996
  2. package/bin/create-sentinelayer.js +5 -5
  3. package/bin/sentinelayer-cli.js +4 -4
  4. package/bin/sl.js +5 -5
  5. package/package.json +64 -63
  6. package/src/agents/ai-governance/index.js +12 -0
  7. package/src/agents/ai-governance/tools/base.js +171 -0
  8. package/src/agents/ai-governance/tools/eval-regression.js +47 -0
  9. package/src/agents/ai-governance/tools/hitl-audit.js +81 -0
  10. package/src/agents/ai-governance/tools/index.js +52 -0
  11. package/src/agents/ai-governance/tools/prompt-drift.js +42 -0
  12. package/src/agents/ai-governance/tools/provenance-check.js +69 -0
  13. package/src/agents/backend/index.js +12 -0
  14. package/src/agents/backend/tools/base.js +189 -0
  15. package/src/agents/backend/tools/circuit-breaker-check.js +123 -0
  16. package/src/agents/backend/tools/idempotency-audit.js +105 -0
  17. package/src/agents/backend/tools/index.js +87 -0
  18. package/src/agents/backend/tools/retry-audit.js +132 -0
  19. package/src/agents/backend/tools/timeout-audit.js +144 -0
  20. package/src/agents/code-quality/index.js +12 -0
  21. package/src/agents/code-quality/tools/base.js +159 -0
  22. package/src/agents/code-quality/tools/complexity-measure.js +197 -0
  23. package/src/agents/code-quality/tools/coupling-analysis.js +81 -0
  24. package/src/agents/code-quality/tools/cycle-detect.js +49 -0
  25. package/src/agents/code-quality/tools/dep-graph.js +196 -0
  26. package/src/agents/code-quality/tools/index.js +89 -0
  27. package/src/agents/data-layer/index.js +12 -0
  28. package/src/agents/data-layer/tools/base.js +181 -0
  29. package/src/agents/data-layer/tools/index-audit.js +165 -0
  30. package/src/agents/data-layer/tools/index.js +83 -0
  31. package/src/agents/data-layer/tools/migration-scan.js +135 -0
  32. package/src/agents/data-layer/tools/query-explain.js +120 -0
  33. package/src/agents/data-layer/tools/tenancy-scan.js +166 -0
  34. package/src/agents/documentation/index.js +12 -0
  35. package/src/agents/documentation/tools/api-diff.js +91 -0
  36. package/src/agents/documentation/tools/base.js +151 -0
  37. package/src/agents/documentation/tools/dead-link-check.js +58 -0
  38. package/src/agents/documentation/tools/docstring-coverage.js +78 -0
  39. package/src/agents/documentation/tools/index.js +52 -0
  40. package/src/agents/documentation/tools/readme-freshness.js +61 -0
  41. package/src/agents/envelope/fix-cycle.js +45 -0
  42. package/src/agents/envelope/index.js +31 -0
  43. package/src/agents/envelope/loop.js +150 -0
  44. package/src/agents/envelope/pulse.js +18 -0
  45. package/src/agents/envelope/stream.js +40 -0
  46. package/src/agents/infrastructure/index.js +12 -0
  47. package/src/agents/infrastructure/tools/base.js +171 -0
  48. package/src/agents/infrastructure/tools/checkov-run.js +32 -0
  49. package/src/agents/infrastructure/tools/drift-detect.js +59 -0
  50. package/src/agents/infrastructure/tools/iam-least-priv-check.js +78 -0
  51. package/src/agents/infrastructure/tools/index.js +52 -0
  52. package/src/agents/infrastructure/tools/tflint-run.js +31 -0
  53. package/src/agents/jules/config/definition.js +160 -160
  54. package/src/agents/jules/config/system-prompt.js +182 -182
  55. package/src/agents/jules/error-intake.js +51 -51
  56. package/src/agents/jules/fix-cycle.js +17 -17
  57. package/src/agents/jules/loop.js +460 -450
  58. package/src/agents/jules/pulse.js +10 -10
  59. package/src/agents/jules/stream.js +187 -186
  60. package/src/agents/jules/swarm/file-scanner.js +74 -74
  61. package/src/agents/jules/swarm/index.js +11 -11
  62. package/src/agents/jules/swarm/orchestrator.js +362 -362
  63. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  64. package/src/agents/jules/swarm/sub-agent.js +315 -309
  65. package/src/agents/jules/tools/aidenid-email.js +189 -189
  66. package/src/agents/jules/tools/auth-audit.js +1708 -1691
  67. package/src/agents/jules/tools/dispatch.js +340 -335
  68. package/src/agents/jules/tools/file-edit.js +2 -2
  69. package/src/agents/jules/tools/file-read.js +2 -2
  70. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  71. package/src/agents/jules/tools/glob.js +2 -2
  72. package/src/agents/jules/tools/grep.js +2 -2
  73. package/src/agents/jules/tools/index.js +29 -29
  74. package/src/agents/jules/tools/path-guards.js +2 -2
  75. package/src/agents/jules/tools/runtime-audit.js +507 -507
  76. package/src/agents/jules/tools/shell.js +2 -2
  77. package/src/agents/jules/tools/url-policy.js +100 -100
  78. package/src/agents/mode.js +113 -0
  79. package/src/agents/observability/index.js +12 -0
  80. package/src/agents/observability/tools/alert-audit.js +39 -0
  81. package/src/agents/observability/tools/base.js +181 -0
  82. package/src/agents/observability/tools/dashboard-gap.js +42 -0
  83. package/src/agents/observability/tools/index.js +54 -0
  84. package/src/agents/observability/tools/log-schema-check.js +74 -0
  85. package/src/agents/observability/tools/span-coverage.js +74 -0
  86. package/src/agents/persona-visuals.js +102 -61
  87. package/src/agents/release/index.js +12 -0
  88. package/src/agents/release/tools/base.js +181 -0
  89. package/src/agents/release/tools/changelog-diff.js +86 -0
  90. package/src/agents/release/tools/feature-flag-audit.js +126 -0
  91. package/src/agents/release/tools/index.js +61 -0
  92. package/src/agents/release/tools/rollback-verify.js +129 -0
  93. package/src/agents/release/tools/semver-check.js +109 -0
  94. package/src/agents/reliability/index.js +12 -0
  95. package/src/agents/reliability/tools/backpressure-check.js +129 -0
  96. package/src/agents/reliability/tools/base.js +181 -0
  97. package/src/agents/reliability/tools/chaos-probe.js +109 -0
  98. package/src/agents/reliability/tools/graceful-degradation-check.js +114 -0
  99. package/src/agents/reliability/tools/health-check-audit.js +111 -0
  100. package/src/agents/reliability/tools/index.js +87 -0
  101. package/src/agents/run-persona.js +109 -0
  102. package/src/agents/security/index.js +12 -0
  103. package/src/agents/security/tools/authz-audit.js +134 -0
  104. package/src/agents/security/tools/base.js +190 -0
  105. package/src/agents/security/tools/crypto-review.js +175 -0
  106. package/src/agents/security/tools/index.js +97 -0
  107. package/src/agents/security/tools/sast-scan.js +175 -0
  108. package/src/agents/security/tools/secrets-scan.js +216 -0
  109. package/src/agents/shared-tools/dispatch-core.js +320 -315
  110. package/src/agents/shared-tools/file-edit.js +180 -180
  111. package/src/agents/shared-tools/file-read.js +100 -100
  112. package/src/agents/shared-tools/glob.js +168 -168
  113. package/src/agents/shared-tools/grep.js +228 -228
  114. package/src/agents/shared-tools/index.js +46 -46
  115. package/src/agents/shared-tools/path-guards.js +161 -161
  116. package/src/agents/shared-tools/shell.js +383 -383
  117. package/src/agents/supply-chain/index.js +12 -0
  118. package/src/agents/supply-chain/tools/attestation-check.js +42 -0
  119. package/src/agents/supply-chain/tools/base.js +151 -0
  120. package/src/agents/supply-chain/tools/index.js +52 -0
  121. package/src/agents/supply-chain/tools/lockfile-integrity.js +73 -0
  122. package/src/agents/supply-chain/tools/package-verify.js +56 -0
  123. package/src/agents/supply-chain/tools/sbom-diff.js +34 -0
  124. package/src/agents/testing/index.js +12 -0
  125. package/src/agents/testing/tools/base.js +202 -0
  126. package/src/agents/testing/tools/coverage-gap.js +144 -0
  127. package/src/agents/testing/tools/flake-detect.js +125 -0
  128. package/src/agents/testing/tools/index.js +85 -0
  129. package/src/agents/testing/tools/mutation-test.js +143 -0
  130. package/src/agents/testing/tools/snapshot-diff.js +103 -0
  131. package/src/ai/aidenid.js +1021 -1009
  132. package/src/ai/client.js +553 -553
  133. package/src/ai/domain-target-store.js +268 -268
  134. package/src/ai/identity-store.js +270 -270
  135. package/src/ai/proxy.js +137 -137
  136. package/src/ai/site-store.js +145 -145
  137. package/src/audit/agents/architecture.js +180 -180
  138. package/src/audit/agents/compliance.js +179 -179
  139. package/src/audit/agents/documentation.js +165 -165
  140. package/src/audit/agents/performance.js +145 -145
  141. package/src/audit/agents/security.js +215 -215
  142. package/src/audit/agents/testing.js +172 -172
  143. package/src/audit/orchestrator.js +557 -557
  144. package/src/audit/package.js +204 -204
  145. package/src/audit/registry.js +284 -284
  146. package/src/audit/replay.js +103 -103
  147. package/src/auth/gate.js +428 -371
  148. package/src/auth/http.js +681 -611
  149. package/src/auth/service.js +1106 -1106
  150. package/src/auth/session-store.js +813 -813
  151. package/src/cli.js +257 -252
  152. package/src/commands/ai/identity-lifecycle.js +1338 -1338
  153. package/src/commands/ai/provision-governance.js +1272 -1272
  154. package/src/commands/ai/shared.js +147 -147
  155. package/src/commands/ai.js +11 -11
  156. package/src/commands/apply.js +12 -12
  157. package/src/commands/audit.js +1171 -1166
  158. package/src/commands/auth.js +419 -419
  159. package/src/commands/chat.js +184 -191
  160. package/src/commands/config.js +184 -184
  161. package/src/commands/cost.js +311 -311
  162. package/src/commands/daemon/core.js +850 -850
  163. package/src/commands/daemon/extended.js +1048 -1048
  164. package/src/commands/daemon/shared.js +213 -213
  165. package/src/commands/daemon.js +11 -11
  166. package/src/commands/guide.js +174 -174
  167. package/src/commands/ingest.js +58 -58
  168. package/src/commands/init.js +55 -55
  169. package/src/commands/legacy-args.js +20 -10
  170. package/src/commands/mcp.js +461 -461
  171. package/src/commands/omargate.js +63 -29
  172. package/src/commands/persona.js +65 -20
  173. package/src/commands/plugin.js +260 -260
  174. package/src/commands/policy.js +132 -132
  175. package/src/commands/prompt.js +238 -238
  176. package/src/commands/review.js +704 -704
  177. package/src/commands/scan.js +865 -872
  178. package/src/commands/session.js +1238 -0
  179. package/src/commands/spec.js +771 -716
  180. package/src/commands/swarm.js +651 -651
  181. package/src/commands/telemetry.js +202 -202
  182. package/src/commands/watch.js +511 -511
  183. package/src/config/agent-dictionary.js +182 -182
  184. package/src/config/io.js +56 -56
  185. package/src/config/paths.js +18 -18
  186. package/src/config/schema.js +55 -55
  187. package/src/config/service.js +184 -184
  188. package/src/coord/events-log.js +141 -0
  189. package/src/coord/handshake.js +719 -0
  190. package/src/coord/index.js +35 -0
  191. package/src/coord/paths.js +84 -0
  192. package/src/coord/priority.js +62 -0
  193. package/src/coord/tarjan.js +157 -0
  194. package/src/cost/budget.js +235 -235
  195. package/src/cost/history.js +188 -188
  196. package/src/cost/tokenizer.js +160 -0
  197. package/src/cost/tracker.js +232 -171
  198. package/src/daemon/artifact-lineage.js +896 -534
  199. package/src/daemon/assignment-ledger.js +1083 -770
  200. package/src/daemon/ast-drift.js +496 -0
  201. package/src/daemon/ast-parser-layer.js +258 -258
  202. package/src/daemon/budget-governor.js +633 -633
  203. package/src/daemon/callgraph-overlay.js +646 -646
  204. package/src/daemon/error-worker.js +1209 -626
  205. package/src/daemon/fix-cycle.js +384 -377
  206. package/src/daemon/hybrid-mapper.js +929 -929
  207. package/src/daemon/ingest-refresh.js +79 -11
  208. package/src/daemon/jira-lifecycle.js +767 -632
  209. package/src/daemon/operator-control.js +657 -657
  210. package/src/daemon/pulse.js +327 -327
  211. package/src/daemon/reliability-lane.js +471 -471
  212. package/src/daemon/scope-engine.js +1068 -0
  213. package/src/daemon/watchdog.js +971 -971
  214. package/src/events/schema.js +190 -0
  215. package/src/guide/generator.js +316 -316
  216. package/src/ingest/engine.js +933 -918
  217. package/src/ingest/ownership.js +380 -0
  218. package/src/interactive/index.js +97 -97
  219. package/src/legacy-cli.js +3228 -2994
  220. package/src/mcp/registry.js +695 -695
  221. package/src/memory/blackboard.js +301 -301
  222. package/src/memory/retrieval.js +581 -581
  223. package/src/orchestrator/kai-chen.js +126 -0
  224. package/src/plugin/manifest.js +553 -553
  225. package/src/policy/packs.js +144 -144
  226. package/src/prompt/generator.js +136 -118
  227. package/src/review/ai-review.js +672 -679
  228. package/src/review/compliance-pack.js +389 -0
  229. package/src/review/investor-dd-config.js +54 -0
  230. package/src/review/investor-dd-file-loop.js +303 -0
  231. package/src/review/investor-dd-file-router.js +406 -0
  232. package/src/review/investor-dd-html-report.js +233 -0
  233. package/src/review/investor-dd-notification.js +120 -0
  234. package/src/review/investor-dd-orchestrator.js +405 -0
  235. package/src/review/investor-dd-persona-runner.js +275 -0
  236. package/src/review/live-validator.js +253 -0
  237. package/src/review/local-review.js +1351 -1305
  238. package/src/review/omargate-interactive.js +68 -68
  239. package/src/review/omargate-orchestrator.js +492 -300
  240. package/src/review/persona-prompts.js +484 -296
  241. package/src/review/reconciliation-rules.js +329 -0
  242. package/src/review/replay.js +235 -235
  243. package/src/review/report.js +664 -664
  244. package/src/review/reproducibility-chain.js +136 -0
  245. package/src/review/scan-modes.js +147 -42
  246. package/src/review/spec-binding.js +487 -487
  247. package/src/scaffold/generator.js +67 -67
  248. package/src/scaffold/templates.js +150 -150
  249. package/src/scan/generator.js +418 -418
  250. package/src/scan/gh-secrets.js +107 -107
  251. package/src/session/agent-registry.js +359 -0
  252. package/src/session/analytics.js +479 -0
  253. package/src/session/daemon.js +1396 -0
  254. package/src/session/file-locks.js +666 -0
  255. package/src/session/paths.js +37 -0
  256. package/src/session/recap.js +567 -0
  257. package/src/session/redact.js +82 -0
  258. package/src/session/runtime-bridge.js +762 -0
  259. package/src/session/scoring.js +406 -0
  260. package/src/session/setup-guides.js +304 -0
  261. package/src/session/store.js +704 -0
  262. package/src/session/stream.js +333 -0
  263. package/src/session/sync.js +753 -0
  264. package/src/session/tasks.js +1054 -0
  265. package/src/session/templates.js +188 -0
  266. package/src/spec/generator.js +619 -519
  267. package/src/spec/regenerate.js +237 -237
  268. package/src/spec/templates.js +91 -91
  269. package/src/swarm/dashboard.js +247 -247
  270. package/src/swarm/factory.js +363 -363
  271. package/src/swarm/pentest.js +934 -934
  272. package/src/swarm/registry.js +419 -419
  273. package/src/swarm/report.js +158 -158
  274. package/src/swarm/runtime.js +569 -576
  275. package/src/swarm/scenario-dsl.js +272 -272
  276. package/src/telemetry/ledger.js +302 -302
  277. package/src/telemetry/session-tracker.js +234 -234
  278. package/src/telemetry/sync.js +203 -203
  279. package/src/ui/command-hints.js +13 -13
  280. 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
- const QUEUE_SCHEMA_VERSION = "1.0.0";
8
- const STATE_SCHEMA_VERSION = "1.0.0";
9
- const DEFAULT_MAX_EVENTS = 200;
10
-
11
- const TERMINAL_WORK_ITEM_STATUSES = new Set(["DONE", "SQUASHED"]);
12
-
13
- export const WORK_ITEM_STATUSES = Object.freeze([
14
- "QUEUED",
15
- "ASSIGNED",
16
- "IN_PROGRESS",
17
- "BLOCKED",
18
- "DONE",
19
- "SQUASHED",
20
- ]);
21
-
22
- const WORK_ITEM_STATUS_SET = new Set(WORK_ITEM_STATUSES);
23
-
24
- const SEVERITY_RANK = new Map([
25
- ["UNKNOWN", 0],
26
- ["P3", 1],
27
- ["P2", 2],
28
- ["P1", 3],
29
- ["P0", 4],
30
- ]);
31
-
32
- function normalizeString(value) {
33
- return String(value || "").trim();
34
- }
35
-
36
- function normalizeIsoTimestamp(value, fallbackIso = new Date().toISOString()) {
37
- const normalized = normalizeString(value);
38
- if (!normalized) {
39
- return fallbackIso;
40
- }
41
- const epoch = Date.parse(normalized);
42
- if (!Number.isFinite(epoch)) {
43
- return fallbackIso;
44
- }
45
- return new Date(epoch).toISOString();
46
- }
47
-
48
- function normalizeNonNegativeInteger(value, fallbackValue = 0) {
49
- if (value === undefined || value === null || normalizeString(value) === "") {
50
- return fallbackValue;
51
- }
52
- const normalized = Number(value);
53
- if (!Number.isFinite(normalized) || normalized < 0) {
54
- throw new Error("Value must be a non-negative integer.");
55
- }
56
- return Math.floor(normalized);
57
- }
58
-
59
- function normalizePositiveInteger(value, fallbackValue = 1) {
60
- if (value === undefined || value === null || normalizeString(value) === "") {
61
- return fallbackValue;
62
- }
63
- const normalized = Number(value);
64
- if (!Number.isFinite(normalized) || normalized <= 0) {
65
- throw new Error("Value must be a positive integer.");
66
- }
67
- return Math.floor(normalized);
68
- }
69
-
70
- function stableTimestampForFile(date = new Date()) {
71
- return date.toISOString().replace(/[:.]/g, "-");
72
- }
73
-
74
- function normalizeSeverity(value) {
75
- const normalized = normalizeString(value).toUpperCase();
76
- if (normalized === "P0" || normalized === "P1" || normalized === "P2" || normalized === "P3") {
77
- return normalized;
78
- }
79
- return "UNKNOWN";
80
- }
81
-
82
- function normalizeMetadata(value) {
83
- if (!value || typeof value !== "object" || Array.isArray(value)) {
84
- return {};
85
- }
86
- return { ...value };
87
- }
88
-
89
- function normalizeStackTrace(value) {
90
- return String(value || "").replace(/\r\n/g, "\n").trim();
91
- }
92
-
93
- function computeStackFingerprint(stackTrace = "") {
94
- const normalized = normalizeStackTrace(stackTrace);
95
- if (!normalized) {
96
- return "none";
97
- }
98
- const reduced = normalized
99
- .split("\n")
100
- .map((line) => line.trim())
101
- .filter(Boolean)
102
- .slice(0, 6)
103
- .join("|");
104
- if (!reduced) {
105
- return "none";
106
- }
107
- return createHash("sha256").update(reduced).digest("hex");
108
- }
109
-
110
- function computeFingerprint(event = {}) {
111
- const materialized = [
112
- normalizeString(event.service).toLowerCase(),
113
- normalizeString(event.endpoint).toLowerCase(),
114
- normalizeString(event.errorCode).toLowerCase(),
115
- normalizeString(event.stackFingerprint).toLowerCase(),
116
- normalizeString(event.commitSha).toLowerCase(),
117
- ].join("|");
118
- return createHash("sha256").update(materialized).digest("hex");
119
- }
120
-
121
- function chooseHigherSeverity(left, right) {
122
- const leftNormalized = normalizeSeverity(left);
123
- const rightNormalized = normalizeSeverity(right);
124
- const leftRank = SEVERITY_RANK.get(leftNormalized) ?? 0;
125
- const rightRank = SEVERITY_RANK.get(rightNormalized) ?? 0;
126
- return rightRank > leftRank ? rightNormalized : leftNormalized;
127
- }
128
-
129
- function toSeverityRank(value) {
130
- return SEVERITY_RANK.get(normalizeSeverity(value)) ?? 0;
131
- }
132
-
133
- function createWorkItemId(nowIso = new Date().toISOString()) {
134
- return `err-${stableTimestampForFile(new Date(nowIso))}-${randomUUID().slice(0, 8)}`;
135
- }
136
-
137
- function normalizeWorkItemStatus(value) {
138
- const normalized = normalizeString(value).toUpperCase();
139
- if (WORK_ITEM_STATUS_SET.has(normalized)) {
140
- return normalized;
141
- }
142
- return "QUEUED";
143
- }
144
-
145
- function isTerminalStatus(status = "") {
146
- return TERMINAL_WORK_ITEM_STATUSES.has(normalizeWorkItemStatus(status));
147
- }
148
-
149
- function normalizeQueueItem(item = {}, fallbackNowIso = new Date().toISOString()) {
150
- const createdAt = normalizeIsoTimestamp(item.createdAt, fallbackNowIso);
151
- return {
152
- workItemId: normalizeString(item.workItemId) || createWorkItemId(createdAt),
153
- fingerprint: normalizeString(item.fingerprint),
154
- source: normalizeString(item.source) || "admin_error_log",
155
- service: normalizeString(item.service) || "unknown-service",
156
- endpoint: normalizeString(item.endpoint) || "unknown-endpoint",
157
- errorCode: normalizeString(item.errorCode) || "UNKNOWN_ERROR",
158
- severity: normalizeSeverity(item.severity),
159
- status: normalizeWorkItemStatus(item.status),
160
- message: normalizeString(item.message),
161
- stackFingerprint: normalizeString(item.stackFingerprint) || "none",
162
- commitSha: normalizeString(item.commitSha) || null,
163
- firstSeenAt: normalizeIsoTimestamp(item.firstSeenAt, createdAt),
164
- lastSeenAt: normalizeIsoTimestamp(item.lastSeenAt, createdAt),
165
- latestEventId: normalizeString(item.latestEventId) || null,
166
- occurrenceCount: Math.max(1, normalizeNonNegativeInteger(item.occurrenceCount, 1)),
167
- createdAt,
168
- updatedAt: normalizeIsoTimestamp(item.updatedAt, createdAt),
169
- metadata: normalizeMetadata(item.metadata),
170
- };
171
- }
172
-
173
- function getQueueInitialState(nowIso = new Date().toISOString()) {
174
- return {
175
- schemaVersion: QUEUE_SCHEMA_VERSION,
176
- generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
177
- items: [],
178
- };
179
- }
180
-
181
- function getWorkerStateInitial(nowIso = new Date().toISOString()) {
182
- return {
183
- schemaVersion: STATE_SCHEMA_VERSION,
184
- updatedAt: normalizeIsoTimestamp(nowIso, nowIso),
185
- lastRunAt: null,
186
- streamOffset: 0,
187
- runCount: 0,
188
- totalProcessedEvents: 0,
189
- totalQueuedItems: 0,
190
- totalDedupeHits: 0,
191
- };
192
- }
193
-
194
- async function loadQueueFile(queuePath, nowIso = new Date().toISOString()) {
195
- try {
196
- const raw = await fsp.readFile(queuePath, "utf-8");
197
- const parsed = JSON.parse(raw);
198
- const items = Array.isArray(parsed.items)
199
- ? parsed.items
200
- .map((item) => normalizeQueueItem(item, nowIso))
201
- .filter((item) => normalizeString(item.fingerprint))
202
- : [];
203
- return {
204
- schemaVersion: normalizeString(parsed.schemaVersion) || QUEUE_SCHEMA_VERSION,
205
- generatedAt: normalizeIsoTimestamp(parsed.generatedAt, nowIso),
206
- items,
207
- };
208
- } catch (error) {
209
- if (error && typeof error === "object" && error.code === "ENOENT") {
210
- return getQueueInitialState(nowIso);
211
- }
212
- throw error;
213
- }
214
- }
215
-
216
- async function writeQueueFile(queuePath, queue = {}, nowIso = new Date().toISOString()) {
217
- const normalized = {
218
- schemaVersion: QUEUE_SCHEMA_VERSION,
219
- generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
220
- items: Array.isArray(queue.items)
221
- ? queue.items
222
- .map((item) => normalizeQueueItem(item, nowIso))
223
- .filter((item) => normalizeString(item.fingerprint))
224
- : [],
225
- };
226
- await fsp.mkdir(path.dirname(queuePath), { recursive: true });
227
- await fsp.writeFile(queuePath, `${JSON.stringify(normalized, null, 2)}\n`, "utf-8");
228
- return normalized;
229
- }
230
-
231
- async function loadWorkerStateFile(statePath, nowIso = new Date().toISOString()) {
232
- try {
233
- const raw = await fsp.readFile(statePath, "utf-8");
234
- const parsed = JSON.parse(raw);
235
- return {
236
- schemaVersion: normalizeString(parsed.schemaVersion) || STATE_SCHEMA_VERSION,
237
- updatedAt: normalizeIsoTimestamp(parsed.updatedAt, nowIso),
238
- lastRunAt: parsed.lastRunAt ? normalizeIsoTimestamp(parsed.lastRunAt, nowIso) : null,
239
- streamOffset: normalizeNonNegativeInteger(parsed.streamOffset, 0),
240
- runCount: normalizeNonNegativeInteger(parsed.runCount, 0),
241
- totalProcessedEvents: normalizeNonNegativeInteger(parsed.totalProcessedEvents, 0),
242
- totalQueuedItems: normalizeNonNegativeInteger(parsed.totalQueuedItems, 0),
243
- totalDedupeHits: normalizeNonNegativeInteger(parsed.totalDedupeHits, 0),
244
- };
245
- } catch (error) {
246
- if (error && typeof error === "object" && error.code === "ENOENT") {
247
- return getWorkerStateInitial(nowIso);
248
- }
249
- throw error;
250
- }
251
- }
252
-
253
- async function writeWorkerStateFile(statePath, state = {}, nowIso = new Date().toISOString()) {
254
- const normalized = {
255
- schemaVersion: STATE_SCHEMA_VERSION,
256
- updatedAt: normalizeIsoTimestamp(nowIso, nowIso),
257
- lastRunAt: state.lastRunAt ? normalizeIsoTimestamp(state.lastRunAt, nowIso) : null,
258
- streamOffset: normalizeNonNegativeInteger(state.streamOffset, 0),
259
- runCount: normalizeNonNegativeInteger(state.runCount, 0),
260
- totalProcessedEvents: normalizeNonNegativeInteger(state.totalProcessedEvents, 0),
261
- totalQueuedItems: normalizeNonNegativeInteger(state.totalQueuedItems, 0),
262
- totalDedupeHits: normalizeNonNegativeInteger(state.totalDedupeHits, 0),
263
- };
264
- await fsp.mkdir(path.dirname(statePath), { recursive: true });
265
- await fsp.writeFile(statePath, `${JSON.stringify(normalized, null, 2)}\n`, "utf-8");
266
- return normalized;
267
- }
268
-
269
- async function loadStreamLines(streamPath) {
270
- try {
271
- const raw = await fsp.readFile(streamPath, "utf-8");
272
- return raw
273
- .split(/\r?\n/)
274
- .map((line) => line.trim())
275
- .filter(Boolean);
276
- } catch (error) {
277
- if (error && typeof error === "object" && error.code === "ENOENT") {
278
- return [];
279
- }
280
- throw error;
281
- }
282
- }
283
-
284
- function buildWorkItemFromEvent(event, nowIso) {
285
- return normalizeQueueItem(
286
- {
287
- workItemId: createWorkItemId(nowIso),
288
- fingerprint: event.fingerprint,
289
- source: event.source,
290
- service: event.service,
291
- endpoint: event.endpoint,
292
- errorCode: event.errorCode,
293
- severity: event.severity,
294
- status: "QUEUED",
295
- message: event.message,
296
- stackFingerprint: event.stackFingerprint,
297
- commitSha: event.commitSha,
298
- firstSeenAt: event.occurredAt,
299
- lastSeenAt: event.occurredAt,
300
- latestEventId: event.eventId,
301
- occurrenceCount: 1,
302
- createdAt: nowIso,
303
- updatedAt: nowIso,
304
- metadata: {
305
- ...event.metadata,
306
- sourceEventId: event.eventId,
307
- },
308
- },
309
- nowIso
310
- );
311
- }
312
-
313
- export function normalizeErrorEvent(event = {}, nowIso = new Date().toISOString()) {
314
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
315
- const stackTrace = normalizeStackTrace(event.stackTrace || event.stack || "");
316
- const normalized = {
317
- eventId: normalizeString(event.eventId) || randomUUID(),
318
- occurredAt: normalizeIsoTimestamp(event.occurredAt, normalizedNow),
319
- recordedAt: normalizedNow,
320
- source: normalizeString(event.source) || "admin_error_log",
321
- service: normalizeString(event.service) || "unknown-service",
322
- endpoint: normalizeString(event.endpoint) || "unknown-endpoint",
323
- errorCode: normalizeString(event.errorCode) || "UNKNOWN_ERROR",
324
- severity: normalizeSeverity(event.severity),
325
- message: normalizeString(event.message) || "Unhandled runtime error",
326
- stackTrace,
327
- stackFingerprint: computeStackFingerprint(stackTrace),
328
- commitSha: normalizeString(event.commitSha) || null,
329
- metadata: normalizeMetadata(event.metadata),
330
- };
331
- return {
332
- ...normalized,
333
- fingerprint: computeFingerprint(normalized),
334
- };
335
- }
336
-
337
- export async function resolveErrorDaemonStorage({
338
- targetPath = ".",
339
- outputDir = "",
340
- env,
341
- homeDir,
342
- } = {}) {
343
- const outputRoot = await resolveOutputRoot({
344
- cwd: path.resolve(String(targetPath || ".")),
345
- outputDirOverride: outputDir,
346
- env,
347
- homeDir,
348
- });
349
- const baseDir = path.join(outputRoot, "observability", "error-daemon");
350
- return {
351
- outputRoot,
352
- baseDir,
353
- streamPath: path.join(baseDir, "admin-error-stream.ndjson"),
354
- queuePath: path.join(baseDir, "queue.json"),
355
- statePath: path.join(baseDir, "worker-state.json"),
356
- intakeDir: path.join(baseDir, "intake"),
357
- runsDir: path.join(baseDir, "runs"),
358
- };
359
- }
360
-
361
- export async function appendAdminErrorEvent({
362
- targetPath = ".",
363
- outputDir = "",
364
- event = {},
365
- env,
366
- homeDir,
367
- } = {}) {
368
- const nowIso = new Date().toISOString();
369
- const normalizedEvent = normalizeErrorEvent(event, nowIso);
370
- const storage = await resolveErrorDaemonStorage({
371
- targetPath,
372
- outputDir,
373
- env,
374
- homeDir,
375
- });
376
-
377
- await fsp.mkdir(storage.baseDir, { recursive: true });
378
- await fsp.appendFile(storage.streamPath, `${JSON.stringify(normalizedEvent)}\n`, "utf-8");
379
-
380
- await fsp.mkdir(storage.intakeDir, { recursive: true });
381
- const intakePath = path.join(
382
- storage.intakeDir,
383
- `intake-${stableTimestampForFile(new Date(normalizedEvent.recordedAt))}-${normalizedEvent.eventId.slice(0, 8)}.json`
384
- );
385
- await fsp.writeFile(
386
- intakePath,
387
- `${JSON.stringify(
388
- {
389
- schemaVersion: "1.0.0",
390
- generatedAt: nowIso,
391
- event: normalizedEvent,
392
- },
393
- null,
394
- 2
395
- )}\n`,
396
- "utf-8"
397
- );
398
-
399
- return {
400
- outputRoot: storage.outputRoot,
401
- streamPath: storage.streamPath,
402
- intakePath,
403
- event: normalizedEvent,
404
- };
405
- }
406
-
407
- export async function getErrorDaemonState({
408
- targetPath = ".",
409
- outputDir = "",
410
- env,
411
- homeDir,
412
- } = {}) {
413
- const storage = await resolveErrorDaemonStorage({
414
- targetPath,
415
- outputDir,
416
- env,
417
- homeDir,
418
- });
419
- const state = await loadWorkerStateFile(storage.statePath);
420
- return {
421
- ...storage,
422
- state,
423
- };
424
- }
425
-
426
- export async function runErrorDaemonWorker({
427
- targetPath = ".",
428
- outputDir = "",
429
- maxEvents = DEFAULT_MAX_EVENTS,
430
- env,
431
- homeDir,
432
- nowIso = new Date().toISOString(),
433
- } = {}) {
434
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
435
- const normalizedMaxEvents = normalizePositiveInteger(maxEvents, DEFAULT_MAX_EVENTS);
436
- const storage = await resolveErrorDaemonStorage({
437
- targetPath,
438
- outputDir,
439
- env,
440
- homeDir,
441
- });
442
-
443
- const [lines, queue, state] = await Promise.all([
444
- loadStreamLines(storage.streamPath),
445
- loadQueueFile(storage.queuePath, normalizedNow),
446
- loadWorkerStateFile(storage.statePath, normalizedNow),
447
- ]);
448
-
449
- const startOffset = Math.max(0, Math.min(state.streamOffset, lines.length));
450
- const endOffset = Math.min(lines.length, startOffset + normalizedMaxEvents);
451
- const openItemsByFingerprint = new Map();
452
- for (const item of queue.items) {
453
- const fingerprint = normalizeString(item.fingerprint);
454
- if (!fingerprint || isTerminalStatus(item.status)) {
455
- continue;
456
- }
457
- if (!openItemsByFingerprint.has(fingerprint)) {
458
- openItemsByFingerprint.set(fingerprint, item);
459
- }
460
- }
461
-
462
- let processedCount = 0;
463
- let queuedCount = 0;
464
- let dedupedCount = 0;
465
- let parseErrorCount = 0;
466
- for (let index = startOffset; index < endOffset; index += 1) {
467
- const rawLine = lines[index];
468
- if (!rawLine) {
469
- continue;
470
- }
471
-
472
- let parsed = null;
473
- try {
474
- parsed = JSON.parse(rawLine);
475
- } catch {
476
- parseErrorCount += 1;
477
- continue;
478
- }
479
- const event = normalizeErrorEvent(parsed, normalizedNow);
480
- processedCount += 1;
481
- const existing = openItemsByFingerprint.get(event.fingerprint);
482
- if (existing) {
483
- dedupedCount += 1;
484
- existing.occurrenceCount = Math.max(1, Number(existing.occurrenceCount || 1)) + 1;
485
- existing.lastSeenAt = event.occurredAt;
486
- existing.updatedAt = normalizedNow;
487
- existing.latestEventId = event.eventId;
488
- existing.severity = chooseHigherSeverity(existing.severity, event.severity);
489
- if (event.message) {
490
- existing.message = event.message;
491
- }
492
- existing.metadata = {
493
- ...normalizeMetadata(existing.metadata),
494
- ...normalizeMetadata(event.metadata),
495
- sourceEventId: event.eventId,
496
- };
497
- continue;
498
- }
499
-
500
- const queued = buildWorkItemFromEvent(event, normalizedNow);
501
- queue.items.push(queued);
502
- openItemsByFingerprint.set(queued.fingerprint, queued);
503
- queuedCount += 1;
504
- }
505
-
506
- queue.generatedAt = normalizedNow;
507
- const savedQueue = await writeQueueFile(storage.queuePath, queue, normalizedNow);
508
-
509
- const savedState = await writeWorkerStateFile(
510
- storage.statePath,
511
- {
512
- ...state,
513
- updatedAt: normalizedNow,
514
- lastRunAt: normalizedNow,
515
- streamOffset: endOffset,
516
- runCount: state.runCount + 1,
517
- totalProcessedEvents: state.totalProcessedEvents + processedCount,
518
- totalQueuedItems: savedQueue.items.length,
519
- totalDedupeHits: state.totalDedupeHits + dedupedCount,
520
- },
521
- normalizedNow
522
- );
523
-
524
- await fsp.mkdir(storage.runsDir, { recursive: true });
525
- const runId = `error-daemon-run-${stableTimestampForFile(new Date(normalizedNow))}-${randomUUID().slice(0, 8)}`;
526
- const runPath = path.join(storage.runsDir, `${runId}.json`);
527
- await fsp.writeFile(
528
- runPath,
529
- `${JSON.stringify(
530
- {
531
- schemaVersion: "1.0.0",
532
- generatedAt: normalizedNow,
533
- runId,
534
- streamPath: storage.streamPath,
535
- queuePath: storage.queuePath,
536
- statePath: storage.statePath,
537
- startOffset,
538
- endOffset,
539
- streamLength: lines.length,
540
- maxEvents: normalizedMaxEvents,
541
- processedCount,
542
- queuedCount,
543
- dedupedCount,
544
- parseErrorCount,
545
- queueDepth: savedQueue.items.length,
546
- },
547
- null,
548
- 2
549
- )}\n`,
550
- "utf-8"
551
- );
552
-
553
- return {
554
- ...storage,
555
- runId,
556
- runPath,
557
- maxEvents: normalizedMaxEvents,
558
- startOffset,
559
- endOffset,
560
- streamLength: lines.length,
561
- processedCount,
562
- queuedCount,
563
- dedupedCount,
564
- parseErrorCount,
565
- queueDepth: savedQueue.items.length,
566
- queue: savedQueue,
567
- state: savedState,
568
- };
569
- }
570
-
571
- function parseStatusFilter(statuses = []) {
572
- if (!Array.isArray(statuses)) {
573
- return null;
574
- }
575
- const normalized = statuses
576
- .map((value) => normalizeString(value).toUpperCase())
577
- .filter(Boolean)
578
- .filter((value) => WORK_ITEM_STATUS_SET.has(value));
579
- if (normalized.length === 0) {
580
- return null;
581
- }
582
- return new Set(normalized);
583
- }
584
-
585
- export async function listErrorQueue({
586
- targetPath = ".",
587
- outputDir = "",
588
- statuses = [],
589
- limit = 50,
590
- env,
591
- homeDir,
592
- } = {}) {
593
- const normalizedLimit = normalizePositiveInteger(limit, 50);
594
- const storage = await resolveErrorDaemonStorage({
595
- targetPath,
596
- outputDir,
597
- env,
598
- homeDir,
599
- });
600
- const [queue, state] = await Promise.all([
601
- loadQueueFile(storage.queuePath),
602
- loadWorkerStateFile(storage.statePath),
603
- ]);
604
-
605
- const statusFilter = parseStatusFilter(statuses);
606
- const sorted = [...queue.items].sort((left, right) => {
607
- const severityDelta = toSeverityRank(right.severity) - toSeverityRank(left.severity);
608
- if (severityDelta !== 0) {
609
- return severityDelta;
610
- }
611
- const leftTs = Date.parse(String(left.lastSeenAt || left.firstSeenAt || "")) || 0;
612
- const rightTs = Date.parse(String(right.lastSeenAt || right.firstSeenAt || "")) || 0;
613
- return rightTs - leftTs;
614
- });
615
- const filtered = statusFilter
616
- ? sorted.filter((item) => statusFilter.has(normalizeWorkItemStatus(item.status)))
617
- : sorted;
618
-
619
- return {
620
- ...storage,
621
- state,
622
- totalCount: sorted.length,
623
- filteredCount: filtered.length,
624
- items: filtered.slice(0, normalizedLimit),
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
+ }