lycheedev 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/bin/lycheedev.js +257 -0
  2. package/package.json +42 -0
  3. package/src/addon.js +87 -0
  4. package/src/config.js +64 -0
  5. package/src/doctor.js +100 -0
  6. package/src/install.js +131 -0
  7. package/src/paths.js +93 -0
  8. package/src/python.js +107 -0
  9. package/src/skill.js +48 -0
  10. package/src/wow.js +110 -0
  11. package/vendor/addon/Core/Bootstrap.lua +131 -0
  12. package/vendor/addon/Core/Clients/Mainline.lua +9 -0
  13. package/vendor/addon/Core/Clients/Mists.lua +9 -0
  14. package/vendor/addon/Core/Clients/Titan.lua +9 -0
  15. package/vendor/addon/Core/Compatibility.lua +43 -0
  16. package/vendor/addon/Core/Database.lua +859 -0
  17. package/vendor/addon/Core/Inspector.lua +647 -0
  18. package/vendor/addon/Core/Locale.lua +307 -0
  19. package/vendor/addon/Core/Locale_enUS.lua +318 -0
  20. package/vendor/addon/Core/Safety.lua +42 -0
  21. package/vendor/addon/Core/Serializer.lua +324 -0
  22. package/vendor/addon/Libs/AutomationQR.lua +1176 -0
  23. package/vendor/addon/Lychee Dev_Mainline.toc +43 -0
  24. package/vendor/addon/Lychee Dev_Mists.toc +43 -0
  25. package/vendor/addon/Lychee Dev_Wrath.toc +43 -0
  26. package/vendor/addon/Media/GitHub.png +0 -0
  27. package/vendor/addon/Media/Logo.png +0 -0
  28. package/vendor/addon/Modules/Automation/Controller.lua +757 -0
  29. package/vendor/addon/Modules/Automation/Report.lua +409 -0
  30. package/vendor/addon/Modules/Automation/auto/auto.lua +12 -0
  31. package/vendor/addon/Modules/Diagnostics.lua +238 -0
  32. package/vendor/addon/Modules/Events/Catalog.lua +184 -0
  33. package/vendor/addon/Modules/Events/CatalogData_Mainline.lua +1788 -0
  34. package/vendor/addon/Modules/Events/CatalogData_Mists.lua +1489 -0
  35. package/vendor/addon/Modules/Events/CatalogData_Titan.lua +1492 -0
  36. package/vendor/addon/Modules/Events/Monitor.lua +181 -0
  37. package/vendor/addon/Modules/FunctionTrace.lua +85 -0
  38. package/vendor/addon/Modules/ObjectInspector.lua +329 -0
  39. package/vendor/addon/UI/AutomationOverlay.lua +183 -0
  40. package/vendor/addon/UI/Export.lua +140 -0
  41. package/vendor/addon/UI/Features.lua +890 -0
  42. package/vendor/addon/UI/MainWindow.lua +1145 -0
  43. package/vendor/addon/UI/Pages/About.lua +223 -0
  44. package/vendor/addon/UI/Pages/Automation.lua +417 -0
  45. package/vendor/addon/UI/Pages/Diagnostics.lua +289 -0
  46. package/vendor/addon/UI/Pages/ExportRecords.lua +387 -0
  47. package/vendor/addon/UI/Pages/Object.lua +573 -0
  48. package/vendor/addon/UI/Pages/Trace.lua +165 -0
  49. package/vendor/skill/SKILL.md +94 -0
  50. package/vendor/skill/agents/openai.yaml +4 -0
  51. package/vendor/skill/references/automation.md +327 -0
  52. package/vendor/skill/references/runtime-investigations.md +97 -0
  53. package/vendor/skill/scripts/automation/__init__.py +1 -0
  54. package/vendor/skill/scripts/automation/registry.py +516 -0
  55. package/vendor/skill/scripts/automation/saved_variables.py +438 -0
  56. package/vendor/skill/scripts/automation/session.py +207 -0
  57. package/vendor/skill/scripts/automation/windows.py +573 -0
  58. package/vendor/skill/scripts/automation.py +865 -0
  59. package/vendor/skill/scripts/requirements.txt +13 -0
  60. package/vendor/skill/scripts/selftest_offline.py +981 -0
@@ -0,0 +1,409 @@
1
+ local ADDON_NAME, ns = ...
2
+
3
+ -- Automation report normalization and serialization.
4
+ -- This module owns the complete report body: strict JSON encoding, size
5
+ -- budgeting and the Adler-32 content checksum. It never touches QR transport.
6
+
7
+ local REPORT_SCHEMA = "lychee.automation.result.v1"
8
+ -- Keep in sync with MAX_AUTOMATION_REPORT_BYTES in Core/Database.lua.
9
+ local MAX_REPORT_CONTENT_BYTES = 1024 * 1024
10
+ local MAX_LIFECYCLE_LOG_ENTRIES = 128
11
+ local MAX_LIFECYCLE_LOG_BYTES = 16 * 1024
12
+ local MAX_ENCODE_DEPTH = 32
13
+
14
+ local BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
15
+
16
+ local CONTROL_ESCAPES = {
17
+ [0x08] = "\\b",
18
+ [0x09] = "\\t",
19
+ [0x0A] = "\\n",
20
+ [0x0C] = "\\f",
21
+ [0x0D] = "\\r",
22
+ }
23
+
24
+ -- --- checksums and encodings -------------------------------------------------
25
+
26
+ function ns.AutomationAdler32(text)
27
+ local a, b = 1, 0
28
+ local length = #text
29
+ local index = 1
30
+ -- 5552 is the largest n so that 255*n*(n+1)/2 + (n+1)*(65520) stays below 2^32.
31
+ while index <= length do
32
+ local chunkEnd = math.min(index + 5551, length)
33
+ for position = index, chunkEnd do
34
+ a = a + string.byte(text, position)
35
+ b = b + a
36
+ end
37
+ a = a % 65521
38
+ b = b % 65521
39
+ index = chunkEnd + 1
40
+ end
41
+ return string.format("%08x", b * 65536 + a)
42
+ end
43
+
44
+ local function EncodeBase64(text)
45
+ local parts = {}
46
+ local length = #text
47
+ local index = 1
48
+ while index <= length do
49
+ local b1 = string.byte(text, index)
50
+ local b2 = string.byte(text, index + 1)
51
+ local b3 = string.byte(text, index + 2)
52
+ local group = b1 * 65536 + (b2 or 0) * 256 + (b3 or 0)
53
+ local chars = {
54
+ BASE64_ALPHABET:sub(math.floor(group / 262144) % 64 + 1, math.floor(group / 262144) % 64 + 1),
55
+ BASE64_ALPHABET:sub(math.floor(group / 4096) % 64 + 1, math.floor(group / 4096) % 64 + 1),
56
+ b2 and BASE64_ALPHABET:sub(math.floor(group / 64) % 64 + 1, math.floor(group / 64) % 64 + 1) or "=",
57
+ b3 and BASE64_ALPHABET:sub(group % 64 + 1, group % 64 + 1) or "=",
58
+ }
59
+ parts[#parts + 1] = table.concat(chars)
60
+ index = index + 3
61
+ end
62
+ return table.concat(parts)
63
+ end
64
+
65
+ local function IsUtf8Continuation(byte)
66
+ return byte ~= nil and byte >= 0x80 and byte <= 0xBF
67
+ end
68
+
69
+ -- Strict UTF-8: every sequence must carry exactly as many continuation bytes
70
+ -- as its leading byte announces. The leading byte ranges already exclude
71
+ -- C0/C1 (overlong) and F5..FF (out of range); the second byte must then rule
72
+ -- out overlong forms (E0 80..9F, F0 80..8F), surrogates (ED A0..BF) and code
73
+ -- points above U+10FFFF (F4 90..BF).
74
+ local function ValidateUtf8(text)
75
+ local index = 1
76
+ local length = #text
77
+ while index <= length do
78
+ local byte = string.byte(text, index)
79
+ local count
80
+ if byte < 0x80 then
81
+ count = 1
82
+ elseif byte >= 0xC2 and byte <= 0xDF then
83
+ count = 2
84
+ elseif byte >= 0xE0 and byte <= 0xEF then
85
+ count = 3
86
+ elseif byte >= 0xF0 and byte <= 0xF4 then
87
+ count = 4
88
+ else
89
+ return false
90
+ end
91
+ if index + count - 1 > length then
92
+ return false
93
+ end
94
+ local second, third, fourth = string.byte(text, index + 1, index + count - 1)
95
+ if count == 2 then
96
+ if not IsUtf8Continuation(second) then
97
+ return false
98
+ end
99
+ elseif count == 3 then
100
+ if not IsUtf8Continuation(second) or not IsUtf8Continuation(third) then
101
+ return false
102
+ end
103
+ if byte == 0xE0 and second < 0xA0 then return false end
104
+ if byte == 0xED and second > 0x9F then return false end
105
+ elseif count == 4 then
106
+ if not IsUtf8Continuation(second) or not IsUtf8Continuation(third)
107
+ or not IsUtf8Continuation(fourth) then
108
+ return false
109
+ end
110
+ if byte == 0xF0 and second < 0x90 then return false end
111
+ if byte == 0xF4 and second > 0x8F then return false end
112
+ end
113
+ index = index + count
114
+ end
115
+ return true
116
+ end
117
+
118
+ -- --- strict JSON encoding ----------------------------------------------------
119
+
120
+ local EncodeState = {}
121
+ EncodeState.__index = EncodeState
122
+
123
+ function EncodeState:new()
124
+ return setmetatable({
125
+ parts = {},
126
+ bytes = 0,
127
+ incomplete = false,
128
+ incompleteReasons = {},
129
+ overflow = false,
130
+ }, self)
131
+ end
132
+
133
+ function EncodeState:MarkIncomplete(reason)
134
+ self.incomplete = true
135
+ if #self.incompleteReasons < 64 then
136
+ self.incompleteReasons[#self.incompleteReasons + 1] = reason
137
+ end
138
+ end
139
+
140
+ function EncodeState:Append(text)
141
+ if self.overflow then
142
+ return
143
+ end
144
+ if self.bytes + #text > MAX_REPORT_CONTENT_BYTES then
145
+ self.overflow = true
146
+ return
147
+ end
148
+ self.parts[#self.parts + 1] = text
149
+ self.bytes = self.bytes + #text
150
+ end
151
+
152
+ local function EncodeJSONString(state, text)
153
+ if not ValidateUtf8(text) then
154
+ -- Binary payloads travel as explicitly marked Base64, never transcoded.
155
+ state:Append('{"$base64":true,"data":"')
156
+ state:Append(EncodeBase64(text))
157
+ state:Append('"}')
158
+ state:MarkIncomplete("binary_string")
159
+ return
160
+ end
161
+
162
+ state:Append('"')
163
+ local index = 1
164
+ local length = #text
165
+ while index <= length do
166
+ local byte = string.byte(text, index)
167
+ local escape = CONTROL_ESCAPES[byte]
168
+ if escape then
169
+ state:Append(escape)
170
+ index = index + 1
171
+ elseif byte == 0x22 then
172
+ state:Append('\\"')
173
+ index = index + 1
174
+ elseif byte == 0x5C then
175
+ state:Append("\\\\")
176
+ index = index + 1
177
+ elseif byte < 0x20 then
178
+ state:Append(string.format("\\u%04x", byte))
179
+ index = index + 1
180
+ else
181
+ local start = index
182
+ repeat
183
+ index = index + 1
184
+ byte = index <= length and string.byte(text, index) or 0
185
+ until byte < 0x20 or byte == 0x22 or byte == 0x5C
186
+ state:Append(text:sub(start, index - 1))
187
+ end
188
+ if state.overflow then
189
+ return
190
+ end
191
+ end
192
+ state:Append('"')
193
+ end
194
+
195
+ local function EncodeJSONNumber(state, value)
196
+ if value ~= value or value == math.huge or value == -math.huge then
197
+ state:Append("null")
198
+ state:MarkIncomplete("non_finite_number")
199
+ return
200
+ end
201
+ state:Append(tostring(value))
202
+ end
203
+
204
+ local function IsPlainArray(value)
205
+ -- pairs() order is undefined, so array detection must not depend on it.
206
+ local maximum = 0
207
+ for key, _ in pairs(value) do
208
+ if type(key) ~= "number" or key % 1 ~= 0 or key < 1 then
209
+ return false
210
+ end
211
+ if key > maximum then
212
+ maximum = key
213
+ end
214
+ end
215
+ for index = 1, maximum do
216
+ if value[index] == nil then
217
+ return false
218
+ end
219
+ end
220
+ return true, maximum
221
+ end
222
+
223
+ local EncodeJSONValue
224
+
225
+ local function EncodeJSONTable(state, value, depth, seen)
226
+ if depth > MAX_ENCODE_DEPTH then
227
+ state:Append('"<max depth>"')
228
+ state:MarkIncomplete("max_depth")
229
+ return
230
+ end
231
+ if seen[value] then
232
+ state:Append('"<cycle>"')
233
+ state:MarkIncomplete("cycle")
234
+ return
235
+ end
236
+ seen[value] = true
237
+
238
+ local isArray, count = IsPlainArray(value)
239
+ if isArray and count > 0 then
240
+ state:Append("[")
241
+ for index = 1, count do
242
+ if index > 1 then
243
+ state:Append(",")
244
+ end
245
+ EncodeJSONValue(state, value[index], depth + 1, seen)
246
+ if state.overflow then
247
+ seen[value] = nil
248
+ return
249
+ end
250
+ end
251
+ state:Append("]")
252
+ else
253
+ state:Append("{")
254
+ local first = true
255
+ for key, child in pairs(value) do
256
+ if not first then
257
+ state:Append(",")
258
+ end
259
+ first = false
260
+ local keyType = type(key)
261
+ if keyType == "number" then
262
+ EncodeJSONString(state, tostring(key))
263
+ state:Append(":")
264
+ elseif keyType == "string" then
265
+ EncodeJSONString(state, key)
266
+ state:Append(":")
267
+ elseif keyType == "boolean" then
268
+ EncodeJSONString(state, tostring(key))
269
+ state:Append(":")
270
+ else
271
+ EncodeJSONString(state, "<unsupported key: " .. keyType .. ">")
272
+ state:Append(":")
273
+ state:MarkIncomplete("unsupported_key")
274
+ end
275
+ EncodeJSONValue(state, child, depth + 1, seen)
276
+ if state.overflow then
277
+ seen[value] = nil
278
+ return
279
+ end
280
+ end
281
+ state:Append("}")
282
+ end
283
+ seen[value] = nil
284
+ end
285
+
286
+ EncodeJSONValue = function(state, value, depth, seen)
287
+ local valueType = type(value)
288
+ if issecretvalue and issecretvalue(value) then
289
+ state:Append('"<secret>"')
290
+ state:MarkIncomplete("secret_value")
291
+ elseif valueType == "nil" then
292
+ state:Append("null")
293
+ elseif valueType == "boolean" then
294
+ state:Append(value and "true" or "false")
295
+ elseif valueType == "number" then
296
+ EncodeJSONNumber(state, value)
297
+ elseif valueType == "string" then
298
+ EncodeJSONString(state, value)
299
+ elseif valueType == "table" then
300
+ EncodeJSONTable(state, value, depth, seen)
301
+ else
302
+ state:Append('"' .. valueType .. '"')
303
+ state:MarkIncomplete("unsupported_type")
304
+ end
305
+ end
306
+
307
+ -- Encode one value as strict normalized JSON.
308
+ -- Returns json text (or nil on overflow), complete flag, reasons array.
309
+ local function EncodeJSON(value)
310
+ local state = EncodeState:new()
311
+ EncodeJSONValue(state, value, 0, {})
312
+ if state.overflow then
313
+ return nil, false, { "output_limit" }
314
+ end
315
+ return table.concat(state.parts), not state.incomplete, state.incompleteReasons
316
+ end
317
+
318
+ -- --- lifecycle log -----------------------------------------------------------
319
+
320
+ local function ClampLifecycleLog(log)
321
+ if type(log) ~= "table" then
322
+ return {}, false
323
+ end
324
+ local entries = {}
325
+ for index = 1, math.min(#log, MAX_LIFECYCLE_LOG_ENTRIES) do
326
+ entries[#entries + 1] = log[index]
327
+ end
328
+ local limited = #log > MAX_LIFECYCLE_LOG_ENTRIES
329
+ local bytes = 0
330
+ for index = 1, #entries do
331
+ local message = entries[index]
332
+ bytes = bytes + (type(message) == "table" and #(message.message or "") or #tostring(message)) + 24
333
+ end
334
+ while bytes > MAX_LIFECYCLE_LOG_BYTES and #entries > 1 do
335
+ local dropped = table.remove(entries, 1)
336
+ bytes = bytes - (#(type(dropped) == "table" and dropped.message or tostring(dropped)) + 24)
337
+ limited = true
338
+ end
339
+ return entries, limited
340
+ end
341
+
342
+ -- --- report building ---------------------------------------------------------
343
+
344
+ -- Build the complete automation report body.
345
+ -- Returns: content(string), metadata(table), errorCode(string|nil)
346
+ -- metadata: { complete = bool, contentChecksum = "xxxxxxxx" }
347
+ -- On overflow the caller receives nil and a stable errorCode.
348
+ function ns.AutomationReportBuild(spec)
349
+ spec = type(spec) == "table" and spec or {}
350
+
351
+ local log, logLimited = ClampLifecycleLog(spec.lifecycleLog)
352
+ local report = {
353
+ schema = REPORT_SCHEMA,
354
+ taskId = spec.taskId,
355
+ executionId = spec.executionId,
356
+ revision = spec.revision,
357
+ kind = spec.kind,
358
+ status = spec.status,
359
+ startedAt = spec.startedAt,
360
+ finishedAt = spec.finishedAt,
361
+ requestType = spec.requestType or "task",
362
+ source = spec.source,
363
+ params = spec.params,
364
+ stdout = spec.stdout,
365
+ outputTruncated = spec.outputTruncated and true or false,
366
+ returns = spec.returns,
367
+ error = spec.error,
368
+ lifecycleLog = log,
369
+ lifecycleLogLimited = logLimited,
370
+ bugSnapshot = spec.bugSnapshot,
371
+ environment = spec.environment,
372
+ complete = spec.complete and true or false,
373
+ incompleteReasons = spec.incompleteReasons,
374
+ }
375
+
376
+ local json, encodeComplete, reasons = EncodeJSON(report)
377
+ if not json then
378
+ return nil, nil, reasons and reasons[1] or "output_limit"
379
+ end
380
+ if spec.complete and not encodeComplete then
381
+ -- Encoding limits (cycles, secrets, unsupported types) make the report
382
+ -- incomplete; fold the encode reasons in and re-encode once.
383
+ local allReasons = {}
384
+ local specReasons = type(spec.incompleteReasons) == "table" and spec.incompleteReasons or {}
385
+ for index = 1, #specReasons do
386
+ allReasons[#allReasons + 1] = specReasons[index]
387
+ end
388
+ for index = 1, #reasons do
389
+ allReasons[#allReasons + 1] = "encode:" .. reasons[index]
390
+ end
391
+ report.incompleteReasons = allReasons
392
+ report.complete = false
393
+ json, encodeComplete, reasons = EncodeJSON(report)
394
+ if not json then
395
+ return nil, nil, "output_limit"
396
+ end
397
+ end
398
+
399
+ return json, {
400
+ complete = report.complete and true or false,
401
+ contentChecksum = ns.AutomationAdler32(json),
402
+ }, nil
403
+ end
404
+
405
+ ns.AutomationReport = {
406
+ Encode = EncodeJSON,
407
+ Adler32 = ns.AutomationAdler32,
408
+ MAX_REPORT_CONTENT_BYTES = MAX_REPORT_CONTENT_BYTES,
409
+ }
@@ -0,0 +1,12 @@
1
+ local ADDON_NAME, ns = ...
2
+
3
+ -- Automation task registry. This file contains generated data only; loading it
4
+ -- must never execute task source. Task blocks are written between the
5
+ -- BEGIN/END markers by the Lychee Dev skill tooling and are removed before
6
+ -- release packaging ships an empty registry.
7
+
8
+ ns.AutomationTaskDefinitions = ns.AutomationTaskDefinitions or {}
9
+
10
+ -- Task blocks use exactly this marker shape (task id may not contain the
11
+ -- marker text): "-- BEGIN LYCHEE DEV TASK <task-id>" ... "-- END LYCHEE DEV TASK <task-id>".
12
+ -- The skill tooling inserts and replaces them; releases ship this file empty.
@@ -0,0 +1,238 @@
1
+ local ADDON_NAME, ns = ...
2
+
3
+ local diagnostics = {}
4
+ local MAX_REPORT_BYTES = 48000
5
+ local SNAPSHOT_MAX_ERRORS = 100
6
+ local SNAPSHOT_TEXT_FIELDS = { "message", "stack", "locals", "source" }
7
+
8
+ -- Stable machine codes for snapshot failures; the caller reports them as data.
9
+ local SNAPSHOT_ERROR_COMBAT = "combat_blocked"
10
+ local SNAPSHOT_ERROR_COUNT = "invalid_count"
11
+ local SNAPSHOT_ERROR_PROVIDER = "provider_unavailable"
12
+ local SNAPSHOT_ERROR_PROVIDER_CALL = "provider_error"
13
+
14
+ local function IsSecret(value)
15
+ return issecretvalue and issecretvalue(value)
16
+ end
17
+
18
+ local function SafeText(value)
19
+ if value == nil then return "" end
20
+ if IsSecret(value) then return "<secret>" end
21
+ local text = type(value) == "string" and value or tostring(value)
22
+ if #text > MAX_REPORT_BYTES then
23
+ return text:sub(1, MAX_REPORT_BYTES) .. "\n... <truncated>"
24
+ end
25
+ return text
26
+ end
27
+
28
+ local function GetGrabber()
29
+ local grabber = _G.BugGrabber
30
+ if type(grabber) ~= "table" or type(grabber.GetDB) ~= "function" then
31
+ return nil
32
+ end
33
+ return grabber
34
+ end
35
+
36
+ local function GetSessionId(grabber)
37
+ if type(grabber.GetSessionId) ~= "function" then return -1 end
38
+ local succeeded, session = pcall(grabber.GetSessionId, grabber)
39
+ if not succeeded or IsSecret(session) then return -1 end
40
+ return tonumber(session) or -1
41
+ end
42
+
43
+ local function MatchesQuery(entry, query)
44
+ if query == "" then return true end
45
+ local searchable = table.concat({
46
+ SafeText(entry.message),
47
+ SafeText(entry.stack),
48
+ SafeText(entry.source),
49
+ }, "\n"):lower()
50
+ return searchable:find(query, 1, true) ~= nil
51
+ end
52
+
53
+ local function CollectErrors(scope, query)
54
+ local grabber = GetGrabber()
55
+ if not grabber then return {}, -1, false end
56
+ local succeeded, database = pcall(grabber.GetDB, grabber)
57
+ if not succeeded or type(database) ~= "table" then return {}, -1, false end
58
+
59
+ local session = GetSessionId(grabber)
60
+ query = SafeText(query):match("^%s*(.-)%s*$"):lower()
61
+ local errors = {}
62
+ for index = #database, 1, -1 do
63
+ local entry = database[index]
64
+ if type(entry) == "table"
65
+ and (scope == "all" or tonumber(entry.session) == session)
66
+ and MatchesQuery(entry, query) then
67
+ errors[#errors + 1] = entry
68
+ end
69
+ end
70
+ return errors, session, true
71
+ end
72
+
73
+ local function DetectSource(entry)
74
+ local source = SafeText(entry.source)
75
+ if source ~= "" then return source end
76
+ local combined = (SafeText(entry.message) .. "\n" .. SafeText(entry.stack)):gsub("\\", "/")
77
+ return combined:match("[Aa]dd[Oo]ns/([^/%s]+)") or ns.L.UNKNOWN_SOURCE
78
+ end
79
+
80
+ function diagnostics.GetErrors(scope, query)
81
+ if ns.IsCombatBlocked() then return false, nil, ns.L.COMBAT_BLOCKED end
82
+ local errors, session, available = CollectErrors(scope, query)
83
+ if not available then return false, nil, ns.L.BUGGRABBER_UNAVAILABLE end
84
+ return true, { errors = errors, session = session }
85
+ end
86
+
87
+ function diagnostics.FormatAgentReport(entry)
88
+ if type(entry) ~= "table" then return ns.L.SELECT_ERROR_DETAIL end
89
+ local version, build, buildDate = GetBuildInfo()
90
+ local timestamp = tonumber(entry.time) and date("%Y-%m-%d %H:%M:%S", entry.time) or ns.L.UNKNOWN_TIME
91
+ local sections = {
92
+ ns.L.AGENT_REPORT_TITLE,
93
+ "",
94
+ ns.L.REPORT_CONTEXT,
95
+ string.format(ns.L.REPORT_TIME, timestamp),
96
+ string.format(ns.L.REPORT_SOURCE, DetectSource(entry)),
97
+ string.format(ns.L.REPORT_COUNT, tonumber(entry.counter) or 1),
98
+ string.format(ns.L.REPORT_SESSION, tonumber(entry.session) or -1),
99
+ "",
100
+ ns.L.REPORT_ENVIRONMENT,
101
+ string.format(ns.L.REPORT_CLIENT, SafeText(version)),
102
+ string.format(ns.L.REPORT_BUILD, SafeText(build), SafeText(buildDate)),
103
+ string.format(ns.L.REPORT_LOCALE, SafeText(GetLocale())),
104
+ "",
105
+ ns.L.REPORT_ERROR,
106
+ SafeText(entry.message),
107
+ "",
108
+ ns.L.REPORT_STACK,
109
+ SafeText(entry.stack) ~= "" and SafeText(entry.stack) or ns.L.REPORT_NOT_AVAILABLE,
110
+ }
111
+ local locals = SafeText(entry.locals)
112
+ if locals ~= "" then
113
+ sections[#sections + 1] = ""
114
+ sections[#sections + 1] = ns.L.REPORT_LOCALS
115
+ sections[#sections + 1] = locals
116
+ end
117
+ return table.concat(sections, "\n")
118
+ end
119
+
120
+ function diagnostics.FormatError(entry)
121
+ return diagnostics.FormatAgentReport(entry)
122
+ end
123
+
124
+ function diagnostics.ResetErrors()
125
+ if ns.IsCombatBlocked() then return false, ns.L.COMBAT_BLOCKED end
126
+ local grabber = GetGrabber()
127
+ if not grabber or type(grabber.Reset) ~= "function" then
128
+ return false, ns.L.BUGGRABBER_UNAVAILABLE
129
+ end
130
+ local succeeded = pcall(grabber.Reset, grabber)
131
+ return succeeded, succeeded and ns.L.ERRORS_CLEARED or ns.L.ERRORS_CLEAR_FAILED
132
+ end
133
+
134
+ local function GetProviderVersion(grabber)
135
+ if type(grabber.version) == "string" and grabber.version ~= "" then
136
+ return grabber.version
137
+ end
138
+ local metadata = ns.Client and ns.Client.GetAddOnMetadata
139
+ if type(metadata) == "function" then
140
+ local succeeded, version = pcall(metadata, "!BugGrabber", "Version")
141
+ if succeeded and type(version) == "string" and not IsSecret(version) then
142
+ return version
143
+ end
144
+ end
145
+ return nil
146
+ end
147
+
148
+ -- Copy one error entry immediately so later BugGrabber updates cannot change it.
149
+ local function CopyErrorEntry(entry)
150
+ local missing = {}
151
+ local copy = {}
152
+ for index = 1, #SNAPSHOT_TEXT_FIELDS do
153
+ local field = SNAPSHOT_TEXT_FIELDS[index]
154
+ local value = entry[field]
155
+ if value == nil then
156
+ -- source is optional; message/stack/locals are reported missing when absent.
157
+ if field ~= "source" then
158
+ missing[#missing + 1] = field
159
+ end
160
+ elseif IsSecret(value) then
161
+ missing[#missing + 1] = field
162
+ elseif type(value) ~= "string" then
163
+ -- Never substitute tostring(table) for real content.
164
+ missing[#missing + 1] = field
165
+ else
166
+ copy[field] = value
167
+ end
168
+ end
169
+ local timeValue = tonumber(entry.time)
170
+ local sessionValue = tonumber(entry.session)
171
+ local counterValue = tonumber(entry.counter)
172
+ if not timeValue or IsSecret(entry.time) then missing[#missing + 1] = "time" end
173
+ if not sessionValue or IsSecret(entry.session) then missing[#missing + 1] = "session" end
174
+ if not counterValue or IsSecret(entry.counter) then missing[#missing + 1] = "counter" end
175
+ copy.time = timeValue
176
+ copy.session = sessionValue
177
+ copy.counter = counterValue
178
+ copy.missingFields = missing
179
+ return copy
180
+ end
181
+
182
+ -- Machine snapshot for automation exports; never calls FormatAgentReport and
183
+ -- never truncates fields silently. Returns nil plus a stable error code on failure.
184
+ function diagnostics.SnapshotRecentErrors(count, scope)
185
+ if ns.IsCombatBlocked() then
186
+ return nil, SNAPSHOT_ERROR_COMBAT
187
+ end
188
+ if type(count) ~= "number" or count ~= math.floor(count)
189
+ or count < 1 or count > SNAPSHOT_MAX_ERRORS then
190
+ return nil, SNAPSHOT_ERROR_COUNT
191
+ end
192
+ local grabber = GetGrabber()
193
+ if not grabber then
194
+ return nil, SNAPSHOT_ERROR_PROVIDER
195
+ end
196
+ local succeeded, database = pcall(grabber.GetDB, grabber)
197
+ if not succeeded or type(database) ~= "table" then
198
+ return nil, SNAPSHOT_ERROR_PROVIDER_CALL
199
+ end
200
+
201
+ local session = GetSessionId(grabber)
202
+ local allSessions = scope ~= "current_session"
203
+ local availableCount = 0
204
+ local errors = {}
205
+ local incompleteReasons = {}
206
+ for index = #database, 1, -1 do
207
+ local entry = database[index]
208
+ if type(entry) == "table" then
209
+ if allSessions or tonumber(entry.session) == session then
210
+ availableCount = availableCount + 1
211
+ if #errors < count then
212
+ local copy = CopyErrorEntry(entry)
213
+ errors[#errors + 1] = copy
214
+ for fieldIndex = 1, #copy.missingFields do
215
+ incompleteReasons[#incompleteReasons + 1]
216
+ = "error[" .. #errors .. "]." .. copy.missingFields[fieldIndex]
217
+ end
218
+ end
219
+ end
220
+ end
221
+ end
222
+
223
+ return {
224
+ scope = allSessions and "provider_storage" or "current_session",
225
+ requestedCount = count,
226
+ returnedCount = #errors,
227
+ availableCount = availableCount,
228
+ ordering = "provider_storage_reverse",
229
+ session = session,
230
+ providerVersion = GetProviderVersion(grabber),
231
+ capturedAt = time(),
232
+ complete = #incompleteReasons == 0,
233
+ incompleteReasons = incompleteReasons,
234
+ errors = errors,
235
+ }, nil
236
+ end
237
+
238
+ ns.Diagnostics = diagnostics