lycheedev 1.0.0 → 1.0.2

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 (50) hide show
  1. package/bin/lycheedev.js +58 -8
  2. package/package.json +1 -1
  3. package/vendor/addon/Core/Bootstrap.lua +131 -131
  4. package/vendor/addon/Core/Clients/Mainline.lua +9 -9
  5. package/vendor/addon/Core/Clients/Mists.lua +9 -9
  6. package/vendor/addon/Core/Clients/Titan.lua +9 -9
  7. package/vendor/addon/Core/Compatibility.lua +43 -43
  8. package/vendor/addon/Core/Database.lua +859 -859
  9. package/vendor/addon/Core/Inspector.lua +647 -647
  10. package/vendor/addon/Core/Locale.lua +307 -307
  11. package/vendor/addon/Core/Locale_enUS.lua +318 -318
  12. package/vendor/addon/Core/Safety.lua +42 -42
  13. package/vendor/addon/Core/Serializer.lua +324 -324
  14. package/vendor/addon/Libs/AutomationQR.lua +1176 -1176
  15. package/vendor/addon/Lychee Dev_Mainline.toc +43 -43
  16. package/vendor/addon/Lychee Dev_Mists.toc +43 -43
  17. package/vendor/addon/Lychee Dev_Wrath.toc +43 -43
  18. package/vendor/addon/Modules/Automation/Controller.lua +757 -757
  19. package/vendor/addon/Modules/Automation/Report.lua +409 -409
  20. package/vendor/addon/Modules/Automation/auto/auto.lua +12 -12
  21. package/vendor/addon/Modules/Diagnostics.lua +238 -238
  22. package/vendor/addon/Modules/Events/Catalog.lua +184 -184
  23. package/vendor/addon/Modules/Events/CatalogData_Mainline.lua +1788 -1788
  24. package/vendor/addon/Modules/Events/CatalogData_Mists.lua +1489 -1489
  25. package/vendor/addon/Modules/Events/CatalogData_Titan.lua +1492 -1492
  26. package/vendor/addon/Modules/Events/Monitor.lua +181 -181
  27. package/vendor/addon/Modules/FunctionTrace.lua +85 -85
  28. package/vendor/addon/Modules/ObjectInspector.lua +329 -329
  29. package/vendor/addon/UI/AutomationOverlay.lua +183 -183
  30. package/vendor/addon/UI/Export.lua +140 -140
  31. package/vendor/addon/UI/Features.lua +890 -890
  32. package/vendor/addon/UI/MainWindow.lua +1145 -1145
  33. package/vendor/addon/UI/Pages/About.lua +223 -223
  34. package/vendor/addon/UI/Pages/Automation.lua +417 -417
  35. package/vendor/addon/UI/Pages/Diagnostics.lua +289 -289
  36. package/vendor/addon/UI/Pages/ExportRecords.lua +387 -387
  37. package/vendor/addon/UI/Pages/Object.lua +573 -573
  38. package/vendor/addon/UI/Pages/Trace.lua +165 -165
  39. package/vendor/skill/SKILL.md +94 -94
  40. package/vendor/skill/agents/openai.yaml +4 -4
  41. package/vendor/skill/references/automation.md +327 -327
  42. package/vendor/skill/references/runtime-investigations.md +97 -97
  43. package/vendor/skill/scripts/automation/__init__.py +1 -1
  44. package/vendor/skill/scripts/automation/registry.py +516 -516
  45. package/vendor/skill/scripts/automation/saved_variables.py +438 -438
  46. package/vendor/skill/scripts/automation/session.py +207 -207
  47. package/vendor/skill/scripts/automation/windows.py +573 -573
  48. package/vendor/skill/scripts/automation.py +865 -865
  49. package/vendor/skill/scripts/requirements.txt +13 -13
  50. package/vendor/skill/scripts/selftest_offline.py +981 -981
@@ -1,409 +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
- }
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
+ }