lycheedev 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/lycheedev.js +21 -6
- package/package.json +1 -1
- package/vendor/addon/Core/Bootstrap.lua +131 -131
- package/vendor/addon/Core/Clients/Mainline.lua +9 -9
- package/vendor/addon/Core/Clients/Mists.lua +9 -9
- package/vendor/addon/Core/Clients/Titan.lua +9 -9
- package/vendor/addon/Core/Compatibility.lua +43 -43
- package/vendor/addon/Core/Database.lua +859 -859
- package/vendor/addon/Core/Inspector.lua +647 -647
- package/vendor/addon/Core/Locale.lua +307 -307
- package/vendor/addon/Core/Locale_enUS.lua +318 -318
- package/vendor/addon/Core/Safety.lua +42 -42
- package/vendor/addon/Core/Serializer.lua +324 -324
- package/vendor/addon/Libs/AutomationQR.lua +1176 -1176
- package/vendor/addon/Lychee Dev_Mainline.toc +43 -43
- package/vendor/addon/Lychee Dev_Mists.toc +43 -43
- package/vendor/addon/Lychee Dev_Wrath.toc +43 -43
- package/vendor/addon/Modules/Automation/Controller.lua +757 -757
- package/vendor/addon/Modules/Automation/Report.lua +409 -409
- package/vendor/addon/Modules/Automation/auto/auto.lua +12 -12
- package/vendor/addon/Modules/Diagnostics.lua +238 -238
- package/vendor/addon/Modules/Events/Catalog.lua +184 -184
- package/vendor/addon/Modules/Events/CatalogData_Mainline.lua +1788 -1788
- package/vendor/addon/Modules/Events/CatalogData_Mists.lua +1489 -1489
- package/vendor/addon/Modules/Events/CatalogData_Titan.lua +1492 -1492
- package/vendor/addon/Modules/Events/Monitor.lua +181 -181
- package/vendor/addon/Modules/FunctionTrace.lua +85 -85
- package/vendor/addon/Modules/ObjectInspector.lua +329 -329
- package/vendor/addon/UI/AutomationOverlay.lua +183 -183
- package/vendor/addon/UI/Export.lua +140 -140
- package/vendor/addon/UI/Features.lua +890 -890
- package/vendor/addon/UI/MainWindow.lua +1145 -1145
- package/vendor/addon/UI/Pages/About.lua +223 -223
- package/vendor/addon/UI/Pages/Automation.lua +417 -417
- package/vendor/addon/UI/Pages/Diagnostics.lua +289 -289
- package/vendor/addon/UI/Pages/ExportRecords.lua +387 -387
- package/vendor/addon/UI/Pages/Object.lua +573 -573
- package/vendor/addon/UI/Pages/Trace.lua +165 -165
- package/vendor/skill/SKILL.md +94 -94
- package/vendor/skill/agents/openai.yaml +4 -4
- package/vendor/skill/references/automation.md +327 -327
- package/vendor/skill/references/runtime-investigations.md +97 -97
- package/vendor/skill/scripts/automation/__init__.py +1 -1
- package/vendor/skill/scripts/automation/registry.py +516 -516
- package/vendor/skill/scripts/automation/saved_variables.py +438 -438
- package/vendor/skill/scripts/automation/session.py +207 -207
- package/vendor/skill/scripts/automation/windows.py +573 -573
- package/vendor/skill/scripts/automation.py +865 -865
- package/vendor/skill/scripts/requirements.txt +13 -13
- package/vendor/skill/scripts/selftest_offline.py +981 -981
|
@@ -1,859 +1,859 @@
|
|
|
1
|
-
local ADDON_NAME, ns = ...
|
|
2
|
-
|
|
3
|
-
local SCHEMA_VERSION = 8
|
|
4
|
-
local MAX_HISTORY_BYTES = 16 * 1024 * 1024
|
|
5
|
-
local MAX_EXPORT_BYTES = 16 * 1024 * 1024
|
|
6
|
-
local MAX_EXPORT_RECORDS = 200
|
|
7
|
-
local MIN_HISTORY_ENTRY_BYTES = 16 * 1024
|
|
8
|
-
local MAX_CODE_BYTES = 12000
|
|
9
|
-
local MAX_RESULT_BYTES = 48000
|
|
10
|
-
local EXPORT_SCHEMA_VERSION = 2
|
|
11
|
-
local EVIDENCE_SCHEMA = "lychee.evidence.v1"
|
|
12
|
-
local AUTOMATION_INDEX_VERSION = 1
|
|
13
|
-
local MAX_AUTOMATION_RECORDS = 100
|
|
14
|
-
local MAX_AUTOMATION_INDEX_BYTES = 256 * 1024
|
|
15
|
-
local MAX_AUTOMATION_REPORT_BYTES = 1024 * 1024
|
|
16
|
-
local AUTOMATION_FAILURE_RESERVE_BYTES = 64 * 1024
|
|
17
|
-
local AUTOMATION_ACTIVE_STATUS = {
|
|
18
|
-
running = true,
|
|
19
|
-
finalizing = true,
|
|
20
|
-
}
|
|
21
|
-
local AUTOMATION_TERMINAL_STATUS = {
|
|
22
|
-
succeeded = true,
|
|
23
|
-
failed = true,
|
|
24
|
-
cancelled = true,
|
|
25
|
-
interrupted = true,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
local db
|
|
29
|
-
local historyBytes = 0
|
|
30
|
-
local exportBytes = 0
|
|
31
|
-
local pendingExports = {}
|
|
32
|
-
local pendingExportCount = 0
|
|
33
|
-
-- Reload protection is session-only: it must never survive a SavedVariables load.
|
|
34
|
-
local protectedExports = {}
|
|
35
|
-
local protectedExportCount = 0
|
|
36
|
-
local automationWritesBlocked = false
|
|
37
|
-
local ticketOwners = {}
|
|
38
|
-
|
|
39
|
-
local function TrimText(value, limit)
|
|
40
|
-
value = type(value) == "string" and value or tostring(value or "")
|
|
41
|
-
if #value <= limit then
|
|
42
|
-
return value
|
|
43
|
-
end
|
|
44
|
-
return value:sub(1, limit) .. "\n... <truncated>"
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
local function GetStoredTreeBytes(value, seen)
|
|
48
|
-
if type(value) == "string" then
|
|
49
|
-
return #value
|
|
50
|
-
elseif type(value) ~= "table" then
|
|
51
|
-
return 8
|
|
52
|
-
elseif seen[value] then
|
|
53
|
-
return 0
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
seen[value] = true
|
|
57
|
-
local bytes = 48
|
|
58
|
-
for key, child in pairs(value) do
|
|
59
|
-
bytes = bytes + GetStoredTreeBytes(key, seen) + GetStoredTreeBytes(child, seen)
|
|
60
|
-
if bytes >= MAX_HISTORY_BYTES then
|
|
61
|
-
break
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
return bytes
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
local function GetHistoryEntryBytes(entry)
|
|
68
|
-
local treeBytes = entry.tree and GetStoredTreeBytes(entry.tree, {}) or 0
|
|
69
|
-
local contentBytes = #(entry.code or "") + #(entry.result or "") + treeBytes + 128
|
|
70
|
-
return math.max(MIN_HISTORY_ENTRY_BYTES, contentBytes)
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
local function PruneHistoryToBudget()
|
|
74
|
-
while historyBytes > MAX_HISTORY_BYTES and #db.history > 0 do
|
|
75
|
-
local entry = db.history[#db.history]
|
|
76
|
-
historyBytes = math.max(0, historyBytes - GetHistoryEntryBytes(entry))
|
|
77
|
-
db.history[#db.history] = nil
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
local function GetExportEntryBytes(entry)
|
|
82
|
-
local payload = type(entry) == "table" and entry.payload or nil
|
|
83
|
-
return type(payload) == "table" and #(payload.content or "") or 0
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
local function RemoveExport(ticket)
|
|
87
|
-
local entry = db.exports.records[ticket]
|
|
88
|
-
if entry then
|
|
89
|
-
exportBytes = math.max(0, exportBytes - GetExportEntryBytes(entry))
|
|
90
|
-
db.exports.records[ticket] = nil
|
|
91
|
-
end
|
|
92
|
-
if pendingExports[ticket] then
|
|
93
|
-
pendingExports[ticket] = nil
|
|
94
|
-
pendingExportCount = math.max(0, pendingExportCount - 1)
|
|
95
|
-
end
|
|
96
|
-
if protectedExports[ticket] then
|
|
97
|
-
protectedExports[ticket] = nil
|
|
98
|
-
protectedExportCount = math.max(0, protectedExportCount - 1)
|
|
99
|
-
end
|
|
100
|
-
local executionId = ticketOwners[ticket]
|
|
101
|
-
if executionId then
|
|
102
|
-
ticketOwners[ticket] = nil
|
|
103
|
-
if db.automation and type(db.automation.records) == "table" then
|
|
104
|
-
local record = db.automation.records[executionId]
|
|
105
|
-
if record and record.ticket == ticket then
|
|
106
|
-
record.resultAvailable = false
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
local function FindPrunableIndex()
|
|
113
|
-
for index = #db.exports.order, 1, -1 do
|
|
114
|
-
if not protectedExports[db.exports.order[index]] then
|
|
115
|
-
return index
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
return nil
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
local function PruneExportsToBudget()
|
|
122
|
-
while (exportBytes > MAX_EXPORT_BYTES or #db.exports.order > MAX_EXPORT_RECORDS)
|
|
123
|
-
and #db.exports.order > 0 do
|
|
124
|
-
local index = FindPrunableIndex()
|
|
125
|
-
if not index then
|
|
126
|
-
break
|
|
127
|
-
end
|
|
128
|
-
local ticket = db.exports.order[index]
|
|
129
|
-
table.remove(db.exports.order, index)
|
|
130
|
-
RemoveExport(ticket)
|
|
131
|
-
end
|
|
132
|
-
db.exports.totalBytes = exportBytes
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
-- Precheck that a new record fits after pruning; protected records cannot be freed.
|
|
136
|
-
local function CanFitExport(entryBytes, reserve)
|
|
137
|
-
local budget = MAX_EXPORT_BYTES - math.max(0, reserve or 0)
|
|
138
|
-
if entryBytes > budget then
|
|
139
|
-
return false
|
|
140
|
-
end
|
|
141
|
-
local projectedBytes = exportBytes + entryBytes
|
|
142
|
-
local projectedCount = #db.exports.order + 1
|
|
143
|
-
if projectedBytes <= budget and projectedCount <= MAX_EXPORT_RECORDS then
|
|
144
|
-
return true
|
|
145
|
-
end
|
|
146
|
-
for index = #db.exports.order, 1, -1 do
|
|
147
|
-
local ticket = db.exports.order[index]
|
|
148
|
-
if not protectedExports[ticket] then
|
|
149
|
-
local entry = db.exports.records[ticket]
|
|
150
|
-
if entry then
|
|
151
|
-
projectedBytes = projectedBytes - GetExportEntryBytes(entry)
|
|
152
|
-
projectedCount = projectedCount - 1
|
|
153
|
-
if projectedBytes <= budget and projectedCount <= MAX_EXPORT_RECORDS then
|
|
154
|
-
return true
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
return false
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
local function CopyExportMetadata(value, depth)
|
|
163
|
-
local valueType = type(value)
|
|
164
|
-
if valueType == "string" then
|
|
165
|
-
return value:sub(1, 2048)
|
|
166
|
-
elseif valueType == "number" or valueType == "boolean" then
|
|
167
|
-
return value
|
|
168
|
-
elseif valueType ~= "table" or depth >= 2 then
|
|
169
|
-
return nil
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
local copied = {}
|
|
173
|
-
local count = 0
|
|
174
|
-
for key, child in pairs(value) do
|
|
175
|
-
if count >= 32 then
|
|
176
|
-
break
|
|
177
|
-
end
|
|
178
|
-
local keyType = type(key)
|
|
179
|
-
if keyType == "string" or keyType == "number" then
|
|
180
|
-
local safeChild = CopyExportMetadata(child, depth + 1)
|
|
181
|
-
if safeChild ~= nil then
|
|
182
|
-
copied[key] = safeChild
|
|
183
|
-
count = count + 1
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
end
|
|
187
|
-
return copied
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
local function CreateEvidenceRecord(ticket, kind, title, content, createdAt, metadata, client)
|
|
191
|
-
metadata = CopyExportMetadata(metadata, 0) or {}
|
|
192
|
-
client = type(client) == "table" and client or {}
|
|
193
|
-
return {
|
|
194
|
-
schema = EVIDENCE_SCHEMA,
|
|
195
|
-
ticket = ticket,
|
|
196
|
-
createdAt = tonumber(createdAt) or 0,
|
|
197
|
-
source = {
|
|
198
|
-
kind = type(kind) == "string" and kind or "unknown",
|
|
199
|
-
title = type(title) == "string" and title or "",
|
|
200
|
-
path = type(metadata.path) == "string" and metadata.path or nil,
|
|
201
|
-
},
|
|
202
|
-
payload = {
|
|
203
|
-
mediaType = "text/plain",
|
|
204
|
-
encoding = "utf-8",
|
|
205
|
-
content = content,
|
|
206
|
-
byteCount = #content,
|
|
207
|
-
},
|
|
208
|
-
environment = {
|
|
209
|
-
addonName = ADDON_NAME,
|
|
210
|
-
clientId = client.clientId,
|
|
211
|
-
version = client.version,
|
|
212
|
-
build = client.build,
|
|
213
|
-
buildDate = client.buildDate,
|
|
214
|
-
interface = client.interface,
|
|
215
|
-
locale = client.locale,
|
|
216
|
-
},
|
|
217
|
-
metadata = metadata,
|
|
218
|
-
}
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
local function NormalizeEvidenceRecord(ticket, entry)
|
|
222
|
-
if type(entry) ~= "table" then
|
|
223
|
-
return nil
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
local payload = entry.payload
|
|
227
|
-
if type(payload) == "table" and type(payload.content) == "string" then
|
|
228
|
-
entry.ticket = ticket
|
|
229
|
-
entry.schema = type(entry.schema) == "string" and entry.schema or EVIDENCE_SCHEMA
|
|
230
|
-
entry.createdAt = tonumber(entry.createdAt) or 0
|
|
231
|
-
entry.source = type(entry.source) == "table" and entry.source or {}
|
|
232
|
-
entry.source.kind = type(entry.source.kind) == "string" and entry.source.kind or "unknown"
|
|
233
|
-
entry.source.title = type(entry.source.title) == "string" and entry.source.title or ""
|
|
234
|
-
entry.metadata = CopyExportMetadata(entry.metadata, 0) or {}
|
|
235
|
-
if type(entry.source.path) ~= "string" then
|
|
236
|
-
entry.source.path = type(entry.metadata.path) == "string" and entry.metadata.path or nil
|
|
237
|
-
end
|
|
238
|
-
payload.mediaType = type(payload.mediaType) == "string" and payload.mediaType or "text/plain"
|
|
239
|
-
payload.encoding = type(payload.encoding) == "string" and payload.encoding or "utf-8"
|
|
240
|
-
payload.byteCount = #payload.content
|
|
241
|
-
entry.environment = type(entry.environment) == "table" and entry.environment or {}
|
|
242
|
-
entry.environment.addonName = type(entry.environment.addonName) == "string"
|
|
243
|
-
and entry.environment.addonName or ADDON_NAME
|
|
244
|
-
return entry
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
if type(entry.content) ~= "string" then
|
|
248
|
-
return nil
|
|
249
|
-
end
|
|
250
|
-
local client = type(entry.client) == "table" and entry.client or {}
|
|
251
|
-
local migrated = CreateEvidenceRecord(ticket, entry.kind, entry.title, entry.content,
|
|
252
|
-
entry.createdAt, entry.metadata, client)
|
|
253
|
-
for key, value in pairs(entry) do
|
|
254
|
-
if migrated[key] == nil and key ~= "content" and key ~= "kind"
|
|
255
|
-
and key ~= "title" and key ~= "client" and key ~= "byteCount" then
|
|
256
|
-
migrated[key] = value
|
|
257
|
-
end
|
|
258
|
-
end
|
|
259
|
-
return migrated
|
|
260
|
-
end
|
|
261
|
-
|
|
262
|
-
local function InitializeExports()
|
|
263
|
-
if type(db.exports) ~= "table" then
|
|
264
|
-
db.exports = {}
|
|
265
|
-
end
|
|
266
|
-
local exports = db.exports
|
|
267
|
-
exports.version = math.max(tonumber(exports.version) or 0, EXPORT_SCHEMA_VERSION)
|
|
268
|
-
exports.nextId = math.max(0, math.floor(tonumber(exports.nextId) or 0))
|
|
269
|
-
exports.records = type(exports.records) == "table" and exports.records or {}
|
|
270
|
-
exports.order = type(exports.order) == "table" and exports.order or {}
|
|
271
|
-
|
|
272
|
-
local validRecords = {}
|
|
273
|
-
exportBytes = 0
|
|
274
|
-
for ticket, entry in pairs(exports.records) do
|
|
275
|
-
local normalized = type(ticket) == "string" and NormalizeEvidenceRecord(ticket, entry) or nil
|
|
276
|
-
if normalized then
|
|
277
|
-
validRecords[ticket] = normalized
|
|
278
|
-
exportBytes = exportBytes + GetExportEntryBytes(normalized)
|
|
279
|
-
end
|
|
280
|
-
end
|
|
281
|
-
exports.records = validRecords
|
|
282
|
-
|
|
283
|
-
local ordered = {}
|
|
284
|
-
local included = {}
|
|
285
|
-
for index = 1, #exports.order do
|
|
286
|
-
local ticket = exports.order[index]
|
|
287
|
-
if validRecords[ticket] and not included[ticket] then
|
|
288
|
-
ordered[#ordered + 1] = ticket
|
|
289
|
-
included[ticket] = true
|
|
290
|
-
end
|
|
291
|
-
end
|
|
292
|
-
for ticket in pairs(validRecords) do
|
|
293
|
-
if not included[ticket] then
|
|
294
|
-
ordered[#ordered + 1] = ticket
|
|
295
|
-
end
|
|
296
|
-
end
|
|
297
|
-
table.sort(ordered, function(left, right)
|
|
298
|
-
return validRecords[left].createdAt > validRecords[right].createdAt
|
|
299
|
-
end)
|
|
300
|
-
exports.order = ordered
|
|
301
|
-
PruneExportsToBudget()
|
|
302
|
-
end
|
|
303
|
-
|
|
304
|
-
local function AllocateTicket(timestamp)
|
|
305
|
-
local ticket
|
|
306
|
-
repeat
|
|
307
|
-
db.exports.nextId = db.exports.nextId + 1
|
|
308
|
-
ticket = string.format("LYCHEE-%s-%04d", date("%Y%m%d-%H%M%S", timestamp), db.exports.nextId)
|
|
309
|
-
until not db.exports.records[ticket]
|
|
310
|
-
return ticket
|
|
311
|
-
end
|
|
312
|
-
local function GetAutomationIndexBytes(record)
|
|
313
|
-
local bytes = 96
|
|
314
|
-
local keys = { "taskId", "executionId", "revision", "kind", "status", "errorCode", "ticket" }
|
|
315
|
-
for index = 1, #keys do
|
|
316
|
-
local value = record[keys[index]]
|
|
317
|
-
if type(value) == "string" then
|
|
318
|
-
bytes = bytes + #value
|
|
319
|
-
end
|
|
320
|
-
end
|
|
321
|
-
return bytes
|
|
322
|
-
end
|
|
323
|
-
|
|
324
|
-
local function PruneAutomationIndex()
|
|
325
|
-
local automation = db.automation
|
|
326
|
-
if not automation then
|
|
327
|
-
return
|
|
328
|
-
end
|
|
329
|
-
local indexBytes = 0
|
|
330
|
-
for _, record in pairs(automation.records) do
|
|
331
|
-
indexBytes = indexBytes + GetAutomationIndexBytes(record)
|
|
332
|
-
end
|
|
333
|
-
local position = #automation.order
|
|
334
|
-
while position >= 1
|
|
335
|
-
and (#automation.order > MAX_AUTOMATION_RECORDS or indexBytes > MAX_AUTOMATION_INDEX_BYTES) do
|
|
336
|
-
local executionId = automation.order[position]
|
|
337
|
-
local record = automation.records[executionId]
|
|
338
|
-
local isProtected = record and record.ticket and protectedExports[record.ticket]
|
|
339
|
-
if record and AUTOMATION_TERMINAL_STATUS[record.status] and not isProtected then
|
|
340
|
-
indexBytes = math.max(0, indexBytes - GetAutomationIndexBytes(record))
|
|
341
|
-
automation.records[executionId] = nil
|
|
342
|
-
if record.ticket and ticketOwners[record.ticket] == executionId then
|
|
343
|
-
ticketOwners[record.ticket] = nil
|
|
344
|
-
end
|
|
345
|
-
table.remove(automation.order, position)
|
|
346
|
-
end
|
|
347
|
-
position = position - 1
|
|
348
|
-
end
|
|
349
|
-
end
|
|
350
|
-
|
|
351
|
-
local function InitializeAutomation()
|
|
352
|
-
if type(db.automation) ~= "table" then
|
|
353
|
-
db.automation = { version = AUTOMATION_INDEX_VERSION, nextLocalRequestId = 1 }
|
|
354
|
-
end
|
|
355
|
-
local automation = db.automation
|
|
356
|
-
automation.nextLocalRequestId = math.max(0, math.floor(tonumber(automation.nextLocalRequestId) or 0))
|
|
357
|
-
automation.records = type(automation.records) == "table" and automation.records or {}
|
|
358
|
-
automation.order = type(automation.order) == "table" and automation.order or {}
|
|
359
|
-
automationWritesBlocked = (tonumber(automation.version) or 0) > AUTOMATION_INDEX_VERSION
|
|
360
|
-
automation.version = math.max(AUTOMATION_INDEX_VERSION, tonumber(automation.version) or AUTOMATION_INDEX_VERSION)
|
|
361
|
-
|
|
362
|
-
ticketOwners = {}
|
|
363
|
-
local validRecords = {}
|
|
364
|
-
for executionId, record in pairs(automation.records) do
|
|
365
|
-
if type(record) == "table" and type(executionId) == "string" then
|
|
366
|
-
record.executionId = executionId
|
|
367
|
-
record.taskId = type(record.taskId) == "string" and record.taskId or executionId
|
|
368
|
-
record.kind = type(record.kind) == "string" and record.kind or "lua"
|
|
369
|
-
-- A non-terminal status loaded from disk belongs to a lost session.
|
|
370
|
-
record.status = AUTOMATION_TERMINAL_STATUS[record.status] and record.status or "interrupted"
|
|
371
|
-
record.startedAt = tonumber(record.startedAt) or 0
|
|
372
|
-
record.finishedAt = tonumber(record.finishedAt) or record.startedAt
|
|
373
|
-
record.revision = type(record.revision) == "string" and record.revision or nil
|
|
374
|
-
record.errorCode = type(record.errorCode) == "string" and record.errorCode or nil
|
|
375
|
-
record.ticket = type(record.ticket) == "string" and record.ticket or nil
|
|
376
|
-
record.resultAvailable = false
|
|
377
|
-
validRecords[executionId] = record
|
|
378
|
-
if record.ticket then
|
|
379
|
-
ticketOwners[record.ticket] = executionId
|
|
380
|
-
end
|
|
381
|
-
end
|
|
382
|
-
end
|
|
383
|
-
automation.records = validRecords
|
|
384
|
-
|
|
385
|
-
local ordered = {}
|
|
386
|
-
local included = {}
|
|
387
|
-
for index = 1, #automation.order do
|
|
388
|
-
local executionId = automation.order[index]
|
|
389
|
-
if validRecords[executionId] and not included[executionId] then
|
|
390
|
-
ordered[#ordered + 1] = executionId
|
|
391
|
-
included[executionId] = true
|
|
392
|
-
end
|
|
393
|
-
end
|
|
394
|
-
for executionId in pairs(validRecords) do
|
|
395
|
-
if not included[executionId] then
|
|
396
|
-
ordered[#ordered + 1] = executionId
|
|
397
|
-
end
|
|
398
|
-
end
|
|
399
|
-
automation.order = ordered
|
|
400
|
-
|
|
401
|
-
for _, executionId in ipairs(ordered) do
|
|
402
|
-
local record = validRecords[executionId]
|
|
403
|
-
if record.ticket and db.exports.records[record.ticket] then
|
|
404
|
-
record.resultAvailable = true
|
|
405
|
-
end
|
|
406
|
-
end
|
|
407
|
-
PruneAutomationIndex()
|
|
408
|
-
end
|
|
409
|
-
|
|
410
|
-
function ns.BeginAutomationExecution(executionId, info)
|
|
411
|
-
if not db or automationWritesBlocked or type(db.automation) ~= "table" then
|
|
412
|
-
return nil, ns.L.AUTO_DATABASE_UNSUPPORTED
|
|
413
|
-
end
|
|
414
|
-
if type(executionId) ~= "string" or executionId == ""
|
|
415
|
-
or db.automation.records[executionId] then
|
|
416
|
-
return nil, ns.L.AUTO_REQUEST_REPEATED
|
|
417
|
-
end
|
|
418
|
-
info = type(info) == "table" and info or {}
|
|
419
|
-
local record = {
|
|
420
|
-
taskId = type(info.taskId) == "string" and info.taskId or executionId,
|
|
421
|
-
executionId = executionId,
|
|
422
|
-
revision = type(info.revision) == "string" and info.revision or nil,
|
|
423
|
-
kind = type(info.kind) == "string" and info.kind or "lua",
|
|
424
|
-
status = "running",
|
|
425
|
-
startedAt = time(),
|
|
426
|
-
finishedAt = nil,
|
|
427
|
-
ticket = nil,
|
|
428
|
-
resultAvailable = false,
|
|
429
|
-
errorCode = nil,
|
|
430
|
-
}
|
|
431
|
-
db.automation.records[executionId] = record
|
|
432
|
-
table.insert(db.automation.order, 1, executionId)
|
|
433
|
-
PruneAutomationIndex()
|
|
434
|
-
return record
|
|
435
|
-
end
|
|
436
|
-
|
|
437
|
-
-- Atomic commit for one automation result: validates everything first, then
|
|
438
|
-
-- inserts the evidence record and updates the execution index in one step.
|
|
439
|
-
function ns.CommitAutomationResult(executionId, content, summary)
|
|
440
|
-
if not db then
|
|
441
|
-
return nil, ns.L.EXPORT_DATABASE_UNAVAILABLE
|
|
442
|
-
end
|
|
443
|
-
if automationWritesBlocked or type(db.automation) ~= "table" then
|
|
444
|
-
return nil, ns.L.AUTO_DATABASE_UNSUPPORTED
|
|
445
|
-
end
|
|
446
|
-
local record = type(executionId) == "string" and db.automation.records[executionId] or nil
|
|
447
|
-
if not record then
|
|
448
|
-
return nil, ns.L.AUTO_EXECUTION_UNKNOWN
|
|
449
|
-
end
|
|
450
|
-
if not AUTOMATION_ACTIVE_STATUS[record.status] then
|
|
451
|
-
return nil, ns.L.AUTO_REQUEST_REPEATED
|
|
452
|
-
end
|
|
453
|
-
summary = type(summary) == "table" and summary or {}
|
|
454
|
-
content = type(content) == "string" and content or ""
|
|
455
|
-
if content == "" then
|
|
456
|
-
return nil, ns.L.EXPORT_EMPTY
|
|
457
|
-
end
|
|
458
|
-
local status = summary.status
|
|
459
|
-
if not AUTOMATION_TERMINAL_STATUS[status] then
|
|
460
|
-
return nil, ns.L.AUTO_EXECUTION_UNKNOWN
|
|
461
|
-
end
|
|
462
|
-
local contentBytes = #content
|
|
463
|
-
if contentBytes > MAX_AUTOMATION_REPORT_BYTES then
|
|
464
|
-
return nil, ns.L.AUTO_REPORT_TOO_LARGE
|
|
465
|
-
end
|
|
466
|
-
|
|
467
|
-
local reserve = status == "failed" and 0 or AUTOMATION_FAILURE_RESERVE_BYTES
|
|
468
|
-
if not CanFitExport(contentBytes, reserve) then
|
|
469
|
-
return nil, ns.L.EXPORT_TOO_LARGE
|
|
470
|
-
end
|
|
471
|
-
|
|
472
|
-
-- Remember the active state so a failed commit can restore it exactly.
|
|
473
|
-
local previousStatus = record.status
|
|
474
|
-
local previousErrorCode = record.errorCode
|
|
475
|
-
local timestamp = time()
|
|
476
|
-
local ticket = AllocateTicket(timestamp)
|
|
477
|
-
local version, build, buildDate, interfaceVersion
|
|
478
|
-
if GetBuildInfo then
|
|
479
|
-
version, build, buildDate, interfaceVersion = GetBuildInfo()
|
|
480
|
-
end
|
|
481
|
-
local entry = CreateEvidenceRecord(ticket, summary.evidenceKind or "automation_result",
|
|
482
|
-
summary.title or record.taskId or executionId, content, timestamp, {
|
|
483
|
-
taskId = record.taskId,
|
|
484
|
-
executionId = executionId,
|
|
485
|
-
revision = record.revision,
|
|
486
|
-
resultSchema = summary.resultSchema,
|
|
487
|
-
status = status,
|
|
488
|
-
complete = summary.complete and true or false,
|
|
489
|
-
checksumAlgorithm = summary.checksumAlgorithm,
|
|
490
|
-
contentChecksum = summary.contentChecksum,
|
|
491
|
-
}, {
|
|
492
|
-
clientId = ns.Client and ns.Client.id or nil,
|
|
493
|
-
version = version,
|
|
494
|
-
build = build,
|
|
495
|
-
buildDate = buildDate,
|
|
496
|
-
interface = interfaceVersion,
|
|
497
|
-
locale = GetLocale and GetLocale() or nil,
|
|
498
|
-
})
|
|
499
|
-
entry.payload.mediaType = "application/json"
|
|
500
|
-
|
|
501
|
-
db.exports.records[ticket] = entry
|
|
502
|
-
table.insert(db.exports.order, 1, ticket)
|
|
503
|
-
exportBytes = exportBytes + contentBytes
|
|
504
|
-
pendingExports[ticket] = true
|
|
505
|
-
pendingExportCount = pendingExportCount + 1
|
|
506
|
-
protectedExports[ticket] = true
|
|
507
|
-
protectedExportCount = protectedExportCount + 1
|
|
508
|
-
ticketOwners[ticket] = executionId
|
|
509
|
-
|
|
510
|
-
record.status = status
|
|
511
|
-
record.finishedAt = timestamp
|
|
512
|
-
record.ticket = ticket
|
|
513
|
-
record.resultAvailable = true
|
|
514
|
-
record.errorCode = type(summary.errorCode) == "string" and summary.errorCode or nil
|
|
515
|
-
|
|
516
|
-
PruneExportsToBudget()
|
|
517
|
-
PruneAutomationIndex()
|
|
518
|
-
if not db.exports.records[ticket] or not db.automation.records[executionId] then
|
|
519
|
-
-- Roll the partial commit back: a failed commit must not leave a ticket
|
|
520
|
-
-- in the index without its evidence payload (spec 5.4 step 3). The
|
|
521
|
-
-- index record returns to its active state so the caller can finalize
|
|
522
|
-
-- it, and every mutation above is reversed exactly once.
|
|
523
|
-
db.exports.records[ticket] = nil
|
|
524
|
-
for index = #db.exports.order, 1, -1 do
|
|
525
|
-
if db.exports.order[index] == ticket then
|
|
526
|
-
table.remove(db.exports.order, index)
|
|
527
|
-
end
|
|
528
|
-
end
|
|
529
|
-
exportBytes = math.max(0, exportBytes - contentBytes)
|
|
530
|
-
db.exports.totalBytes = exportBytes
|
|
531
|
-
if pendingExports[ticket] then
|
|
532
|
-
pendingExports[ticket] = nil
|
|
533
|
-
pendingExportCount = math.max(0, pendingExportCount - 1)
|
|
534
|
-
end
|
|
535
|
-
if protectedExports[ticket] then
|
|
536
|
-
protectedExports[ticket] = nil
|
|
537
|
-
protectedExportCount = math.max(0, protectedExportCount - 1)
|
|
538
|
-
end
|
|
539
|
-
if ticketOwners[ticket] == executionId then
|
|
540
|
-
ticketOwners[ticket] = nil
|
|
541
|
-
end
|
|
542
|
-
if db.automation.records[executionId] ~= record then
|
|
543
|
-
db.automation.records[executionId] = record
|
|
544
|
-
table.insert(db.automation.order, 1, executionId)
|
|
545
|
-
end
|
|
546
|
-
record.status = previousStatus
|
|
547
|
-
record.finishedAt = nil
|
|
548
|
-
record.ticket = nil
|
|
549
|
-
record.resultAvailable = false
|
|
550
|
-
record.errorCode = previousErrorCode
|
|
551
|
-
return nil, ns.L.EXPORT_FAILED
|
|
552
|
-
end
|
|
553
|
-
return ticket, entry
|
|
554
|
-
end
|
|
555
|
-
|
|
556
|
-
-- Finalize an execution index record without a result ticket. Used only when
|
|
557
|
-
-- even a minimal failure report could not be committed; no notice may be shown.
|
|
558
|
-
function ns.FailAutomationExecution(executionId, status, errorCode)
|
|
559
|
-
if not db or type(db.automation) ~= "table" then
|
|
560
|
-
return false
|
|
561
|
-
end
|
|
562
|
-
local record = type(executionId) == "string" and db.automation.records[executionId] or nil
|
|
563
|
-
if not record or not AUTOMATION_ACTIVE_STATUS[record.status] then
|
|
564
|
-
return false
|
|
565
|
-
end
|
|
566
|
-
record.status = AUTOMATION_TERMINAL_STATUS[status] and status or "failed"
|
|
567
|
-
record.finishedAt = time()
|
|
568
|
-
record.ticket = nil
|
|
569
|
-
record.resultAvailable = false
|
|
570
|
-
record.errorCode = type(errorCode) == "string" and errorCode or nil
|
|
571
|
-
PruneAutomationIndex()
|
|
572
|
-
return true
|
|
573
|
-
end
|
|
574
|
-
|
|
575
|
-
-- Clear terminal, unprotected automation index summaries. Evidence records in
|
|
576
|
-
-- the shared export store are intentionally kept; protected executions stay.
|
|
577
|
-
function ns.ClearAutomationHistory()
|
|
578
|
-
if not db or type(db.automation) ~= "table" then
|
|
579
|
-
return 0
|
|
580
|
-
end
|
|
581
|
-
local removed = 0
|
|
582
|
-
for index = #db.automation.order, 1, -1 do
|
|
583
|
-
local executionId = db.automation.order[index]
|
|
584
|
-
local record = db.automation.records[executionId]
|
|
585
|
-
local isProtected = record and record.ticket and protectedExports[record.ticket]
|
|
586
|
-
if record and AUTOMATION_TERMINAL_STATUS[record.status] and not isProtected then
|
|
587
|
-
db.automation.records[executionId] = nil
|
|
588
|
-
if record.ticket and ticketOwners[record.ticket] == executionId then
|
|
589
|
-
ticketOwners[record.ticket] = nil
|
|
590
|
-
end
|
|
591
|
-
table.remove(db.automation.order, index)
|
|
592
|
-
removed = removed + 1
|
|
593
|
-
end
|
|
594
|
-
end
|
|
595
|
-
return removed
|
|
596
|
-
end
|
|
597
|
-
|
|
598
|
-
function ns.GetAutomationIndex()
|
|
599
|
-
return db and db.automation or nil
|
|
600
|
-
end
|
|
601
|
-
|
|
602
|
-
function ns.GetAutomationRecord(executionId)
|
|
603
|
-
return db and db.automation and db.automation.records[executionId] or nil
|
|
604
|
-
end
|
|
605
|
-
|
|
606
|
-
function ns.FindAutomationByTicket(ticket)
|
|
607
|
-
if not db or not db.automation or type(ticket) ~= "string" then
|
|
608
|
-
return nil
|
|
609
|
-
end
|
|
610
|
-
local executionId = ticketOwners[ticket]
|
|
611
|
-
if executionId and db.automation.records[executionId] then
|
|
612
|
-
return db.automation.records[executionId]
|
|
613
|
-
end
|
|
614
|
-
for _, candidateId in ipairs(db.automation.order) do
|
|
615
|
-
local record = db.automation.records[candidateId]
|
|
616
|
-
if record and record.ticket == ticket then
|
|
617
|
-
return record
|
|
618
|
-
end
|
|
619
|
-
end
|
|
620
|
-
return nil
|
|
621
|
-
end
|
|
622
|
-
|
|
623
|
-
function ns.AutomationWritesEnabled()
|
|
624
|
-
return db ~= nil and not automationWritesBlocked and type(db.automation) == "table"
|
|
625
|
-
end
|
|
626
|
-
|
|
627
|
-
-- One place that answers "where is this result in its life cycle?" so the page,
|
|
628
|
-
-- the notices and any future caller cannot disagree. The host's own report wins
|
|
629
|
-
-- over the in-memory flush marker, because a result can be acknowledged while
|
|
630
|
-
-- still protected in the session that produced it:
|
|
631
|
-
-- pending - committed, still waiting for the output-side reload
|
|
632
|
-
-- flushed - on disk, no read reported yet
|
|
633
|
-
-- received - the host reported a successful read
|
|
634
|
-
-- failed - the host reported a failed read
|
|
635
|
-
-- expired - the evidence itself is gone; only the index summary remains
|
|
636
|
-
function ns.AutomationResultState(record)
|
|
637
|
-
if type(record) ~= "table" or not record.ticket then
|
|
638
|
-
return "none"
|
|
639
|
-
end
|
|
640
|
-
if not ns.GetExport(record.ticket) then
|
|
641
|
-
return "expired"
|
|
642
|
-
end
|
|
643
|
-
if record.receivedStatus == "received" then
|
|
644
|
-
return "received"
|
|
645
|
-
end
|
|
646
|
-
if record.receivedStatus == "failed" then
|
|
647
|
-
return "failed"
|
|
648
|
-
end
|
|
649
|
-
if protectedExports[record.ticket] then
|
|
650
|
-
return "pending"
|
|
651
|
-
end
|
|
652
|
-
return "flushed"
|
|
653
|
-
end
|
|
654
|
-
|
|
655
|
-
function ns.NextAutomationLocalRequestId()
|
|
656
|
-
if not db or type(db.automation) ~= "table" or automationWritesBlocked then
|
|
657
|
-
return nil
|
|
658
|
-
end
|
|
659
|
-
db.automation.nextLocalRequestId = db.automation.nextLocalRequestId + 1
|
|
660
|
-
return db.automation.nextLocalRequestId
|
|
661
|
-
end
|
|
662
|
-
|
|
663
|
-
function ns.IsExportProtected(ticket)
|
|
664
|
-
return type(ticket) == "string" and protectedExports[ticket] == true
|
|
665
|
-
end
|
|
666
|
-
|
|
667
|
-
function ns.GetProtectedExportCount()
|
|
668
|
-
return protectedExportCount
|
|
669
|
-
end
|
|
670
|
-
|
|
671
|
-
function ns.InitializeDatabase()
|
|
672
|
-
if db then
|
|
673
|
-
return
|
|
674
|
-
end
|
|
675
|
-
|
|
676
|
-
if type(LycheeDevDB) ~= "table" then
|
|
677
|
-
if type(DumperDB) == "table" then
|
|
678
|
-
LycheeDevDB = DumperDB
|
|
679
|
-
else
|
|
680
|
-
LycheeDevDB = {}
|
|
681
|
-
end
|
|
682
|
-
end
|
|
683
|
-
DumperDB = nil
|
|
684
|
-
|
|
685
|
-
-- Reload protection never persists; a SavedVariables load starts a fresh session.
|
|
686
|
-
protectedExports = {}
|
|
687
|
-
protectedExportCount = 0
|
|
688
|
-
ticketOwners = {}
|
|
689
|
-
automationWritesBlocked = false
|
|
690
|
-
|
|
691
|
-
if type(LycheeDevDB.history) ~= "table" then
|
|
692
|
-
LycheeDevDB.history = {}
|
|
693
|
-
end
|
|
694
|
-
|
|
695
|
-
local storedTreeMigrationRequired = type(LycheeDevDB.schemaVersion) ~= "number"
|
|
696
|
-
or LycheeDevDB.schemaVersion < SCHEMA_VERSION
|
|
697
|
-
if storedTreeMigrationRequired then
|
|
698
|
-
LycheeDevDB.schemaVersion = SCHEMA_VERSION
|
|
699
|
-
end
|
|
700
|
-
db = LycheeDevDB
|
|
701
|
-
historyBytes = 0
|
|
702
|
-
|
|
703
|
-
for index = #db.history, 1, -1 do
|
|
704
|
-
local entry = db.history[index]
|
|
705
|
-
if type(entry) ~= "table" then
|
|
706
|
-
table.remove(db.history, index)
|
|
707
|
-
else
|
|
708
|
-
entry.code = TrimText(entry.code, MAX_CODE_BYTES)
|
|
709
|
-
entry.result = TrimText(entry.result, MAX_RESULT_BYTES)
|
|
710
|
-
entry.succeeded = entry.succeeded and true or false
|
|
711
|
-
if type(entry.timestamp) ~= "number" then
|
|
712
|
-
entry.timestamp = nil
|
|
713
|
-
end
|
|
714
|
-
if type(entry.tree) ~= "table" then
|
|
715
|
-
entry.tree = nil
|
|
716
|
-
if storedTreeMigrationRequired and entry.succeeded and ns.CreateStoredTreeFromSerialized then
|
|
717
|
-
entry.tree = ns.CreateStoredTreeFromSerialized(entry.result)
|
|
718
|
-
end
|
|
719
|
-
end
|
|
720
|
-
historyBytes = historyBytes + GetHistoryEntryBytes(entry)
|
|
721
|
-
end
|
|
722
|
-
end
|
|
723
|
-
|
|
724
|
-
PruneHistoryToBudget()
|
|
725
|
-
InitializeExports()
|
|
726
|
-
InitializeAutomation()
|
|
727
|
-
|
|
728
|
-
ns.db = db
|
|
729
|
-
end
|
|
730
|
-
|
|
731
|
-
function ns.GetHistory()
|
|
732
|
-
return db and db.history or nil
|
|
733
|
-
end
|
|
734
|
-
|
|
735
|
-
function ns.AddHistory(code, result, succeeded, tree)
|
|
736
|
-
if not db then
|
|
737
|
-
return nil
|
|
738
|
-
end
|
|
739
|
-
|
|
740
|
-
local entry = {
|
|
741
|
-
code = TrimText(code, MAX_CODE_BYTES),
|
|
742
|
-
result = TrimText(result, MAX_RESULT_BYTES),
|
|
743
|
-
succeeded = succeeded and true or false,
|
|
744
|
-
timestamp = time(),
|
|
745
|
-
tree = type(tree) == "table" and tree or nil,
|
|
746
|
-
}
|
|
747
|
-
table.insert(db.history, 1, entry)
|
|
748
|
-
|
|
749
|
-
historyBytes = historyBytes + GetHistoryEntryBytes(entry)
|
|
750
|
-
PruneHistoryToBudget()
|
|
751
|
-
return entry
|
|
752
|
-
end
|
|
753
|
-
|
|
754
|
-
function ns.ClearHistory()
|
|
755
|
-
if db then
|
|
756
|
-
wipe(db.history)
|
|
757
|
-
historyBytes = 0
|
|
758
|
-
end
|
|
759
|
-
end
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
function ns.AddExport(kind, title, content, metadata)
|
|
763
|
-
if not db then
|
|
764
|
-
return nil, ns.L.EXPORT_DATABASE_UNAVAILABLE
|
|
765
|
-
end
|
|
766
|
-
content = type(content) == "string" and content or tostring(content or "")
|
|
767
|
-
if content == "" then
|
|
768
|
-
return nil, ns.L.EXPORT_EMPTY
|
|
769
|
-
end
|
|
770
|
-
|
|
771
|
-
local timestamp = time()
|
|
772
|
-
local ticket = AllocateTicket(timestamp)
|
|
773
|
-
|
|
774
|
-
local version, build, buildDate, interfaceVersion
|
|
775
|
-
if GetBuildInfo then
|
|
776
|
-
version, build, buildDate, interfaceVersion = GetBuildInfo()
|
|
777
|
-
end
|
|
778
|
-
local entry = CreateEvidenceRecord(ticket, kind, title, content, timestamp, metadata, {
|
|
779
|
-
clientId = ns.Client and ns.Client.id or nil,
|
|
780
|
-
version = version,
|
|
781
|
-
build = build,
|
|
782
|
-
buildDate = buildDate,
|
|
783
|
-
interface = interfaceVersion,
|
|
784
|
-
locale = GetLocale and GetLocale() or nil,
|
|
785
|
-
})
|
|
786
|
-
local entryBytes = GetExportEntryBytes(entry)
|
|
787
|
-
if entryBytes > MAX_EXPORT_BYTES then
|
|
788
|
-
return nil, ns.L.EXPORT_TOO_LARGE
|
|
789
|
-
end
|
|
790
|
-
|
|
791
|
-
db.exports.records[ticket] = entry
|
|
792
|
-
table.insert(db.exports.order, 1, ticket)
|
|
793
|
-
exportBytes = exportBytes + entryBytes
|
|
794
|
-
pendingExports[ticket] = true
|
|
795
|
-
pendingExportCount = pendingExportCount + 1
|
|
796
|
-
PruneExportsToBudget()
|
|
797
|
-
return ticket, entry
|
|
798
|
-
end
|
|
799
|
-
|
|
800
|
-
function ns.GetExport(ticket)
|
|
801
|
-
return db and db.exports and db.exports.records[ticket] or nil
|
|
802
|
-
end
|
|
803
|
-
|
|
804
|
-
function ns.GetExports()
|
|
805
|
-
return db and db.exports or nil
|
|
806
|
-
end
|
|
807
|
-
|
|
808
|
-
function ns.GetExportStats()
|
|
809
|
-
if not db or not db.exports then
|
|
810
|
-
return 0, 0, MAX_EXPORT_BYTES
|
|
811
|
-
end
|
|
812
|
-
return #db.exports.order, exportBytes, MAX_EXPORT_BYTES
|
|
813
|
-
end
|
|
814
|
-
|
|
815
|
-
function ns.IsExportPending(ticket)
|
|
816
|
-
return type(ticket) == "string" and pendingExports[ticket] == true
|
|
817
|
-
end
|
|
818
|
-
|
|
819
|
-
function ns.GetPendingExportCount()
|
|
820
|
-
return pendingExportCount
|
|
821
|
-
end
|
|
822
|
-
|
|
823
|
-
function ns.DeleteExport(ticket)
|
|
824
|
-
if not db or not db.exports or type(ticket) ~= "string"
|
|
825
|
-
or not db.exports.records[ticket] then
|
|
826
|
-
return false
|
|
827
|
-
end
|
|
828
|
-
-- Results awaiting their reload disk write cannot be deleted.
|
|
829
|
-
if protectedExports[ticket] then
|
|
830
|
-
return false
|
|
831
|
-
end
|
|
832
|
-
|
|
833
|
-
RemoveExport(ticket)
|
|
834
|
-
for index = 1, #db.exports.order do
|
|
835
|
-
if db.exports.order[index] == ticket then
|
|
836
|
-
table.remove(db.exports.order, index)
|
|
837
|
-
break
|
|
838
|
-
end
|
|
839
|
-
end
|
|
840
|
-
db.exports.totalBytes = exportBytes
|
|
841
|
-
return true
|
|
842
|
-
end
|
|
843
|
-
|
|
844
|
-
function ns.ClearExports()
|
|
845
|
-
if not db or not db.exports then
|
|
846
|
-
return 0
|
|
847
|
-
end
|
|
848
|
-
local removed = 0
|
|
849
|
-
for index = #db.exports.order, 1, -1 do
|
|
850
|
-
local ticket = db.exports.order[index]
|
|
851
|
-
if not protectedExports[ticket] then
|
|
852
|
-
table.remove(db.exports.order, index)
|
|
853
|
-
RemoveExport(ticket)
|
|
854
|
-
removed = removed + 1
|
|
855
|
-
end
|
|
856
|
-
end
|
|
857
|
-
db.exports.totalBytes = exportBytes
|
|
858
|
-
return removed
|
|
859
|
-
end
|
|
1
|
+
local ADDON_NAME, ns = ...
|
|
2
|
+
|
|
3
|
+
local SCHEMA_VERSION = 8
|
|
4
|
+
local MAX_HISTORY_BYTES = 16 * 1024 * 1024
|
|
5
|
+
local MAX_EXPORT_BYTES = 16 * 1024 * 1024
|
|
6
|
+
local MAX_EXPORT_RECORDS = 200
|
|
7
|
+
local MIN_HISTORY_ENTRY_BYTES = 16 * 1024
|
|
8
|
+
local MAX_CODE_BYTES = 12000
|
|
9
|
+
local MAX_RESULT_BYTES = 48000
|
|
10
|
+
local EXPORT_SCHEMA_VERSION = 2
|
|
11
|
+
local EVIDENCE_SCHEMA = "lychee.evidence.v1"
|
|
12
|
+
local AUTOMATION_INDEX_VERSION = 1
|
|
13
|
+
local MAX_AUTOMATION_RECORDS = 100
|
|
14
|
+
local MAX_AUTOMATION_INDEX_BYTES = 256 * 1024
|
|
15
|
+
local MAX_AUTOMATION_REPORT_BYTES = 1024 * 1024
|
|
16
|
+
local AUTOMATION_FAILURE_RESERVE_BYTES = 64 * 1024
|
|
17
|
+
local AUTOMATION_ACTIVE_STATUS = {
|
|
18
|
+
running = true,
|
|
19
|
+
finalizing = true,
|
|
20
|
+
}
|
|
21
|
+
local AUTOMATION_TERMINAL_STATUS = {
|
|
22
|
+
succeeded = true,
|
|
23
|
+
failed = true,
|
|
24
|
+
cancelled = true,
|
|
25
|
+
interrupted = true,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
local db
|
|
29
|
+
local historyBytes = 0
|
|
30
|
+
local exportBytes = 0
|
|
31
|
+
local pendingExports = {}
|
|
32
|
+
local pendingExportCount = 0
|
|
33
|
+
-- Reload protection is session-only: it must never survive a SavedVariables load.
|
|
34
|
+
local protectedExports = {}
|
|
35
|
+
local protectedExportCount = 0
|
|
36
|
+
local automationWritesBlocked = false
|
|
37
|
+
local ticketOwners = {}
|
|
38
|
+
|
|
39
|
+
local function TrimText(value, limit)
|
|
40
|
+
value = type(value) == "string" and value or tostring(value or "")
|
|
41
|
+
if #value <= limit then
|
|
42
|
+
return value
|
|
43
|
+
end
|
|
44
|
+
return value:sub(1, limit) .. "\n... <truncated>"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
local function GetStoredTreeBytes(value, seen)
|
|
48
|
+
if type(value) == "string" then
|
|
49
|
+
return #value
|
|
50
|
+
elseif type(value) ~= "table" then
|
|
51
|
+
return 8
|
|
52
|
+
elseif seen[value] then
|
|
53
|
+
return 0
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
seen[value] = true
|
|
57
|
+
local bytes = 48
|
|
58
|
+
for key, child in pairs(value) do
|
|
59
|
+
bytes = bytes + GetStoredTreeBytes(key, seen) + GetStoredTreeBytes(child, seen)
|
|
60
|
+
if bytes >= MAX_HISTORY_BYTES then
|
|
61
|
+
break
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
return bytes
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
local function GetHistoryEntryBytes(entry)
|
|
68
|
+
local treeBytes = entry.tree and GetStoredTreeBytes(entry.tree, {}) or 0
|
|
69
|
+
local contentBytes = #(entry.code or "") + #(entry.result or "") + treeBytes + 128
|
|
70
|
+
return math.max(MIN_HISTORY_ENTRY_BYTES, contentBytes)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
local function PruneHistoryToBudget()
|
|
74
|
+
while historyBytes > MAX_HISTORY_BYTES and #db.history > 0 do
|
|
75
|
+
local entry = db.history[#db.history]
|
|
76
|
+
historyBytes = math.max(0, historyBytes - GetHistoryEntryBytes(entry))
|
|
77
|
+
db.history[#db.history] = nil
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
local function GetExportEntryBytes(entry)
|
|
82
|
+
local payload = type(entry) == "table" and entry.payload or nil
|
|
83
|
+
return type(payload) == "table" and #(payload.content or "") or 0
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
local function RemoveExport(ticket)
|
|
87
|
+
local entry = db.exports.records[ticket]
|
|
88
|
+
if entry then
|
|
89
|
+
exportBytes = math.max(0, exportBytes - GetExportEntryBytes(entry))
|
|
90
|
+
db.exports.records[ticket] = nil
|
|
91
|
+
end
|
|
92
|
+
if pendingExports[ticket] then
|
|
93
|
+
pendingExports[ticket] = nil
|
|
94
|
+
pendingExportCount = math.max(0, pendingExportCount - 1)
|
|
95
|
+
end
|
|
96
|
+
if protectedExports[ticket] then
|
|
97
|
+
protectedExports[ticket] = nil
|
|
98
|
+
protectedExportCount = math.max(0, protectedExportCount - 1)
|
|
99
|
+
end
|
|
100
|
+
local executionId = ticketOwners[ticket]
|
|
101
|
+
if executionId then
|
|
102
|
+
ticketOwners[ticket] = nil
|
|
103
|
+
if db.automation and type(db.automation.records) == "table" then
|
|
104
|
+
local record = db.automation.records[executionId]
|
|
105
|
+
if record and record.ticket == ticket then
|
|
106
|
+
record.resultAvailable = false
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
local function FindPrunableIndex()
|
|
113
|
+
for index = #db.exports.order, 1, -1 do
|
|
114
|
+
if not protectedExports[db.exports.order[index]] then
|
|
115
|
+
return index
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
return nil
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
local function PruneExportsToBudget()
|
|
122
|
+
while (exportBytes > MAX_EXPORT_BYTES or #db.exports.order > MAX_EXPORT_RECORDS)
|
|
123
|
+
and #db.exports.order > 0 do
|
|
124
|
+
local index = FindPrunableIndex()
|
|
125
|
+
if not index then
|
|
126
|
+
break
|
|
127
|
+
end
|
|
128
|
+
local ticket = db.exports.order[index]
|
|
129
|
+
table.remove(db.exports.order, index)
|
|
130
|
+
RemoveExport(ticket)
|
|
131
|
+
end
|
|
132
|
+
db.exports.totalBytes = exportBytes
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
-- Precheck that a new record fits after pruning; protected records cannot be freed.
|
|
136
|
+
local function CanFitExport(entryBytes, reserve)
|
|
137
|
+
local budget = MAX_EXPORT_BYTES - math.max(0, reserve or 0)
|
|
138
|
+
if entryBytes > budget then
|
|
139
|
+
return false
|
|
140
|
+
end
|
|
141
|
+
local projectedBytes = exportBytes + entryBytes
|
|
142
|
+
local projectedCount = #db.exports.order + 1
|
|
143
|
+
if projectedBytes <= budget and projectedCount <= MAX_EXPORT_RECORDS then
|
|
144
|
+
return true
|
|
145
|
+
end
|
|
146
|
+
for index = #db.exports.order, 1, -1 do
|
|
147
|
+
local ticket = db.exports.order[index]
|
|
148
|
+
if not protectedExports[ticket] then
|
|
149
|
+
local entry = db.exports.records[ticket]
|
|
150
|
+
if entry then
|
|
151
|
+
projectedBytes = projectedBytes - GetExportEntryBytes(entry)
|
|
152
|
+
projectedCount = projectedCount - 1
|
|
153
|
+
if projectedBytes <= budget and projectedCount <= MAX_EXPORT_RECORDS then
|
|
154
|
+
return true
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
return false
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
local function CopyExportMetadata(value, depth)
|
|
163
|
+
local valueType = type(value)
|
|
164
|
+
if valueType == "string" then
|
|
165
|
+
return value:sub(1, 2048)
|
|
166
|
+
elseif valueType == "number" or valueType == "boolean" then
|
|
167
|
+
return value
|
|
168
|
+
elseif valueType ~= "table" or depth >= 2 then
|
|
169
|
+
return nil
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
local copied = {}
|
|
173
|
+
local count = 0
|
|
174
|
+
for key, child in pairs(value) do
|
|
175
|
+
if count >= 32 then
|
|
176
|
+
break
|
|
177
|
+
end
|
|
178
|
+
local keyType = type(key)
|
|
179
|
+
if keyType == "string" or keyType == "number" then
|
|
180
|
+
local safeChild = CopyExportMetadata(child, depth + 1)
|
|
181
|
+
if safeChild ~= nil then
|
|
182
|
+
copied[key] = safeChild
|
|
183
|
+
count = count + 1
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
return copied
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
local function CreateEvidenceRecord(ticket, kind, title, content, createdAt, metadata, client)
|
|
191
|
+
metadata = CopyExportMetadata(metadata, 0) or {}
|
|
192
|
+
client = type(client) == "table" and client or {}
|
|
193
|
+
return {
|
|
194
|
+
schema = EVIDENCE_SCHEMA,
|
|
195
|
+
ticket = ticket,
|
|
196
|
+
createdAt = tonumber(createdAt) or 0,
|
|
197
|
+
source = {
|
|
198
|
+
kind = type(kind) == "string" and kind or "unknown",
|
|
199
|
+
title = type(title) == "string" and title or "",
|
|
200
|
+
path = type(metadata.path) == "string" and metadata.path or nil,
|
|
201
|
+
},
|
|
202
|
+
payload = {
|
|
203
|
+
mediaType = "text/plain",
|
|
204
|
+
encoding = "utf-8",
|
|
205
|
+
content = content,
|
|
206
|
+
byteCount = #content,
|
|
207
|
+
},
|
|
208
|
+
environment = {
|
|
209
|
+
addonName = ADDON_NAME,
|
|
210
|
+
clientId = client.clientId,
|
|
211
|
+
version = client.version,
|
|
212
|
+
build = client.build,
|
|
213
|
+
buildDate = client.buildDate,
|
|
214
|
+
interface = client.interface,
|
|
215
|
+
locale = client.locale,
|
|
216
|
+
},
|
|
217
|
+
metadata = metadata,
|
|
218
|
+
}
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
local function NormalizeEvidenceRecord(ticket, entry)
|
|
222
|
+
if type(entry) ~= "table" then
|
|
223
|
+
return nil
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
local payload = entry.payload
|
|
227
|
+
if type(payload) == "table" and type(payload.content) == "string" then
|
|
228
|
+
entry.ticket = ticket
|
|
229
|
+
entry.schema = type(entry.schema) == "string" and entry.schema or EVIDENCE_SCHEMA
|
|
230
|
+
entry.createdAt = tonumber(entry.createdAt) or 0
|
|
231
|
+
entry.source = type(entry.source) == "table" and entry.source or {}
|
|
232
|
+
entry.source.kind = type(entry.source.kind) == "string" and entry.source.kind or "unknown"
|
|
233
|
+
entry.source.title = type(entry.source.title) == "string" and entry.source.title or ""
|
|
234
|
+
entry.metadata = CopyExportMetadata(entry.metadata, 0) or {}
|
|
235
|
+
if type(entry.source.path) ~= "string" then
|
|
236
|
+
entry.source.path = type(entry.metadata.path) == "string" and entry.metadata.path or nil
|
|
237
|
+
end
|
|
238
|
+
payload.mediaType = type(payload.mediaType) == "string" and payload.mediaType or "text/plain"
|
|
239
|
+
payload.encoding = type(payload.encoding) == "string" and payload.encoding or "utf-8"
|
|
240
|
+
payload.byteCount = #payload.content
|
|
241
|
+
entry.environment = type(entry.environment) == "table" and entry.environment or {}
|
|
242
|
+
entry.environment.addonName = type(entry.environment.addonName) == "string"
|
|
243
|
+
and entry.environment.addonName or ADDON_NAME
|
|
244
|
+
return entry
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
if type(entry.content) ~= "string" then
|
|
248
|
+
return nil
|
|
249
|
+
end
|
|
250
|
+
local client = type(entry.client) == "table" and entry.client or {}
|
|
251
|
+
local migrated = CreateEvidenceRecord(ticket, entry.kind, entry.title, entry.content,
|
|
252
|
+
entry.createdAt, entry.metadata, client)
|
|
253
|
+
for key, value in pairs(entry) do
|
|
254
|
+
if migrated[key] == nil and key ~= "content" and key ~= "kind"
|
|
255
|
+
and key ~= "title" and key ~= "client" and key ~= "byteCount" then
|
|
256
|
+
migrated[key] = value
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
return migrated
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
local function InitializeExports()
|
|
263
|
+
if type(db.exports) ~= "table" then
|
|
264
|
+
db.exports = {}
|
|
265
|
+
end
|
|
266
|
+
local exports = db.exports
|
|
267
|
+
exports.version = math.max(tonumber(exports.version) or 0, EXPORT_SCHEMA_VERSION)
|
|
268
|
+
exports.nextId = math.max(0, math.floor(tonumber(exports.nextId) or 0))
|
|
269
|
+
exports.records = type(exports.records) == "table" and exports.records or {}
|
|
270
|
+
exports.order = type(exports.order) == "table" and exports.order or {}
|
|
271
|
+
|
|
272
|
+
local validRecords = {}
|
|
273
|
+
exportBytes = 0
|
|
274
|
+
for ticket, entry in pairs(exports.records) do
|
|
275
|
+
local normalized = type(ticket) == "string" and NormalizeEvidenceRecord(ticket, entry) or nil
|
|
276
|
+
if normalized then
|
|
277
|
+
validRecords[ticket] = normalized
|
|
278
|
+
exportBytes = exportBytes + GetExportEntryBytes(normalized)
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
exports.records = validRecords
|
|
282
|
+
|
|
283
|
+
local ordered = {}
|
|
284
|
+
local included = {}
|
|
285
|
+
for index = 1, #exports.order do
|
|
286
|
+
local ticket = exports.order[index]
|
|
287
|
+
if validRecords[ticket] and not included[ticket] then
|
|
288
|
+
ordered[#ordered + 1] = ticket
|
|
289
|
+
included[ticket] = true
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
for ticket in pairs(validRecords) do
|
|
293
|
+
if not included[ticket] then
|
|
294
|
+
ordered[#ordered + 1] = ticket
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
table.sort(ordered, function(left, right)
|
|
298
|
+
return validRecords[left].createdAt > validRecords[right].createdAt
|
|
299
|
+
end)
|
|
300
|
+
exports.order = ordered
|
|
301
|
+
PruneExportsToBudget()
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
local function AllocateTicket(timestamp)
|
|
305
|
+
local ticket
|
|
306
|
+
repeat
|
|
307
|
+
db.exports.nextId = db.exports.nextId + 1
|
|
308
|
+
ticket = string.format("LYCHEE-%s-%04d", date("%Y%m%d-%H%M%S", timestamp), db.exports.nextId)
|
|
309
|
+
until not db.exports.records[ticket]
|
|
310
|
+
return ticket
|
|
311
|
+
end
|
|
312
|
+
local function GetAutomationIndexBytes(record)
|
|
313
|
+
local bytes = 96
|
|
314
|
+
local keys = { "taskId", "executionId", "revision", "kind", "status", "errorCode", "ticket" }
|
|
315
|
+
for index = 1, #keys do
|
|
316
|
+
local value = record[keys[index]]
|
|
317
|
+
if type(value) == "string" then
|
|
318
|
+
bytes = bytes + #value
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
return bytes
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
local function PruneAutomationIndex()
|
|
325
|
+
local automation = db.automation
|
|
326
|
+
if not automation then
|
|
327
|
+
return
|
|
328
|
+
end
|
|
329
|
+
local indexBytes = 0
|
|
330
|
+
for _, record in pairs(automation.records) do
|
|
331
|
+
indexBytes = indexBytes + GetAutomationIndexBytes(record)
|
|
332
|
+
end
|
|
333
|
+
local position = #automation.order
|
|
334
|
+
while position >= 1
|
|
335
|
+
and (#automation.order > MAX_AUTOMATION_RECORDS or indexBytes > MAX_AUTOMATION_INDEX_BYTES) do
|
|
336
|
+
local executionId = automation.order[position]
|
|
337
|
+
local record = automation.records[executionId]
|
|
338
|
+
local isProtected = record and record.ticket and protectedExports[record.ticket]
|
|
339
|
+
if record and AUTOMATION_TERMINAL_STATUS[record.status] and not isProtected then
|
|
340
|
+
indexBytes = math.max(0, indexBytes - GetAutomationIndexBytes(record))
|
|
341
|
+
automation.records[executionId] = nil
|
|
342
|
+
if record.ticket and ticketOwners[record.ticket] == executionId then
|
|
343
|
+
ticketOwners[record.ticket] = nil
|
|
344
|
+
end
|
|
345
|
+
table.remove(automation.order, position)
|
|
346
|
+
end
|
|
347
|
+
position = position - 1
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
local function InitializeAutomation()
|
|
352
|
+
if type(db.automation) ~= "table" then
|
|
353
|
+
db.automation = { version = AUTOMATION_INDEX_VERSION, nextLocalRequestId = 1 }
|
|
354
|
+
end
|
|
355
|
+
local automation = db.automation
|
|
356
|
+
automation.nextLocalRequestId = math.max(0, math.floor(tonumber(automation.nextLocalRequestId) or 0))
|
|
357
|
+
automation.records = type(automation.records) == "table" and automation.records or {}
|
|
358
|
+
automation.order = type(automation.order) == "table" and automation.order or {}
|
|
359
|
+
automationWritesBlocked = (tonumber(automation.version) or 0) > AUTOMATION_INDEX_VERSION
|
|
360
|
+
automation.version = math.max(AUTOMATION_INDEX_VERSION, tonumber(automation.version) or AUTOMATION_INDEX_VERSION)
|
|
361
|
+
|
|
362
|
+
ticketOwners = {}
|
|
363
|
+
local validRecords = {}
|
|
364
|
+
for executionId, record in pairs(automation.records) do
|
|
365
|
+
if type(record) == "table" and type(executionId) == "string" then
|
|
366
|
+
record.executionId = executionId
|
|
367
|
+
record.taskId = type(record.taskId) == "string" and record.taskId or executionId
|
|
368
|
+
record.kind = type(record.kind) == "string" and record.kind or "lua"
|
|
369
|
+
-- A non-terminal status loaded from disk belongs to a lost session.
|
|
370
|
+
record.status = AUTOMATION_TERMINAL_STATUS[record.status] and record.status or "interrupted"
|
|
371
|
+
record.startedAt = tonumber(record.startedAt) or 0
|
|
372
|
+
record.finishedAt = tonumber(record.finishedAt) or record.startedAt
|
|
373
|
+
record.revision = type(record.revision) == "string" and record.revision or nil
|
|
374
|
+
record.errorCode = type(record.errorCode) == "string" and record.errorCode or nil
|
|
375
|
+
record.ticket = type(record.ticket) == "string" and record.ticket or nil
|
|
376
|
+
record.resultAvailable = false
|
|
377
|
+
validRecords[executionId] = record
|
|
378
|
+
if record.ticket then
|
|
379
|
+
ticketOwners[record.ticket] = executionId
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
automation.records = validRecords
|
|
384
|
+
|
|
385
|
+
local ordered = {}
|
|
386
|
+
local included = {}
|
|
387
|
+
for index = 1, #automation.order do
|
|
388
|
+
local executionId = automation.order[index]
|
|
389
|
+
if validRecords[executionId] and not included[executionId] then
|
|
390
|
+
ordered[#ordered + 1] = executionId
|
|
391
|
+
included[executionId] = true
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
for executionId in pairs(validRecords) do
|
|
395
|
+
if not included[executionId] then
|
|
396
|
+
ordered[#ordered + 1] = executionId
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
automation.order = ordered
|
|
400
|
+
|
|
401
|
+
for _, executionId in ipairs(ordered) do
|
|
402
|
+
local record = validRecords[executionId]
|
|
403
|
+
if record.ticket and db.exports.records[record.ticket] then
|
|
404
|
+
record.resultAvailable = true
|
|
405
|
+
end
|
|
406
|
+
end
|
|
407
|
+
PruneAutomationIndex()
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
function ns.BeginAutomationExecution(executionId, info)
|
|
411
|
+
if not db or automationWritesBlocked or type(db.automation) ~= "table" then
|
|
412
|
+
return nil, ns.L.AUTO_DATABASE_UNSUPPORTED
|
|
413
|
+
end
|
|
414
|
+
if type(executionId) ~= "string" or executionId == ""
|
|
415
|
+
or db.automation.records[executionId] then
|
|
416
|
+
return nil, ns.L.AUTO_REQUEST_REPEATED
|
|
417
|
+
end
|
|
418
|
+
info = type(info) == "table" and info or {}
|
|
419
|
+
local record = {
|
|
420
|
+
taskId = type(info.taskId) == "string" and info.taskId or executionId,
|
|
421
|
+
executionId = executionId,
|
|
422
|
+
revision = type(info.revision) == "string" and info.revision or nil,
|
|
423
|
+
kind = type(info.kind) == "string" and info.kind or "lua",
|
|
424
|
+
status = "running",
|
|
425
|
+
startedAt = time(),
|
|
426
|
+
finishedAt = nil,
|
|
427
|
+
ticket = nil,
|
|
428
|
+
resultAvailable = false,
|
|
429
|
+
errorCode = nil,
|
|
430
|
+
}
|
|
431
|
+
db.automation.records[executionId] = record
|
|
432
|
+
table.insert(db.automation.order, 1, executionId)
|
|
433
|
+
PruneAutomationIndex()
|
|
434
|
+
return record
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
-- Atomic commit for one automation result: validates everything first, then
|
|
438
|
+
-- inserts the evidence record and updates the execution index in one step.
|
|
439
|
+
function ns.CommitAutomationResult(executionId, content, summary)
|
|
440
|
+
if not db then
|
|
441
|
+
return nil, ns.L.EXPORT_DATABASE_UNAVAILABLE
|
|
442
|
+
end
|
|
443
|
+
if automationWritesBlocked or type(db.automation) ~= "table" then
|
|
444
|
+
return nil, ns.L.AUTO_DATABASE_UNSUPPORTED
|
|
445
|
+
end
|
|
446
|
+
local record = type(executionId) == "string" and db.automation.records[executionId] or nil
|
|
447
|
+
if not record then
|
|
448
|
+
return nil, ns.L.AUTO_EXECUTION_UNKNOWN
|
|
449
|
+
end
|
|
450
|
+
if not AUTOMATION_ACTIVE_STATUS[record.status] then
|
|
451
|
+
return nil, ns.L.AUTO_REQUEST_REPEATED
|
|
452
|
+
end
|
|
453
|
+
summary = type(summary) == "table" and summary or {}
|
|
454
|
+
content = type(content) == "string" and content or ""
|
|
455
|
+
if content == "" then
|
|
456
|
+
return nil, ns.L.EXPORT_EMPTY
|
|
457
|
+
end
|
|
458
|
+
local status = summary.status
|
|
459
|
+
if not AUTOMATION_TERMINAL_STATUS[status] then
|
|
460
|
+
return nil, ns.L.AUTO_EXECUTION_UNKNOWN
|
|
461
|
+
end
|
|
462
|
+
local contentBytes = #content
|
|
463
|
+
if contentBytes > MAX_AUTOMATION_REPORT_BYTES then
|
|
464
|
+
return nil, ns.L.AUTO_REPORT_TOO_LARGE
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
local reserve = status == "failed" and 0 or AUTOMATION_FAILURE_RESERVE_BYTES
|
|
468
|
+
if not CanFitExport(contentBytes, reserve) then
|
|
469
|
+
return nil, ns.L.EXPORT_TOO_LARGE
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
-- Remember the active state so a failed commit can restore it exactly.
|
|
473
|
+
local previousStatus = record.status
|
|
474
|
+
local previousErrorCode = record.errorCode
|
|
475
|
+
local timestamp = time()
|
|
476
|
+
local ticket = AllocateTicket(timestamp)
|
|
477
|
+
local version, build, buildDate, interfaceVersion
|
|
478
|
+
if GetBuildInfo then
|
|
479
|
+
version, build, buildDate, interfaceVersion = GetBuildInfo()
|
|
480
|
+
end
|
|
481
|
+
local entry = CreateEvidenceRecord(ticket, summary.evidenceKind or "automation_result",
|
|
482
|
+
summary.title or record.taskId or executionId, content, timestamp, {
|
|
483
|
+
taskId = record.taskId,
|
|
484
|
+
executionId = executionId,
|
|
485
|
+
revision = record.revision,
|
|
486
|
+
resultSchema = summary.resultSchema,
|
|
487
|
+
status = status,
|
|
488
|
+
complete = summary.complete and true or false,
|
|
489
|
+
checksumAlgorithm = summary.checksumAlgorithm,
|
|
490
|
+
contentChecksum = summary.contentChecksum,
|
|
491
|
+
}, {
|
|
492
|
+
clientId = ns.Client and ns.Client.id or nil,
|
|
493
|
+
version = version,
|
|
494
|
+
build = build,
|
|
495
|
+
buildDate = buildDate,
|
|
496
|
+
interface = interfaceVersion,
|
|
497
|
+
locale = GetLocale and GetLocale() or nil,
|
|
498
|
+
})
|
|
499
|
+
entry.payload.mediaType = "application/json"
|
|
500
|
+
|
|
501
|
+
db.exports.records[ticket] = entry
|
|
502
|
+
table.insert(db.exports.order, 1, ticket)
|
|
503
|
+
exportBytes = exportBytes + contentBytes
|
|
504
|
+
pendingExports[ticket] = true
|
|
505
|
+
pendingExportCount = pendingExportCount + 1
|
|
506
|
+
protectedExports[ticket] = true
|
|
507
|
+
protectedExportCount = protectedExportCount + 1
|
|
508
|
+
ticketOwners[ticket] = executionId
|
|
509
|
+
|
|
510
|
+
record.status = status
|
|
511
|
+
record.finishedAt = timestamp
|
|
512
|
+
record.ticket = ticket
|
|
513
|
+
record.resultAvailable = true
|
|
514
|
+
record.errorCode = type(summary.errorCode) == "string" and summary.errorCode or nil
|
|
515
|
+
|
|
516
|
+
PruneExportsToBudget()
|
|
517
|
+
PruneAutomationIndex()
|
|
518
|
+
if not db.exports.records[ticket] or not db.automation.records[executionId] then
|
|
519
|
+
-- Roll the partial commit back: a failed commit must not leave a ticket
|
|
520
|
+
-- in the index without its evidence payload (spec 5.4 step 3). The
|
|
521
|
+
-- index record returns to its active state so the caller can finalize
|
|
522
|
+
-- it, and every mutation above is reversed exactly once.
|
|
523
|
+
db.exports.records[ticket] = nil
|
|
524
|
+
for index = #db.exports.order, 1, -1 do
|
|
525
|
+
if db.exports.order[index] == ticket then
|
|
526
|
+
table.remove(db.exports.order, index)
|
|
527
|
+
end
|
|
528
|
+
end
|
|
529
|
+
exportBytes = math.max(0, exportBytes - contentBytes)
|
|
530
|
+
db.exports.totalBytes = exportBytes
|
|
531
|
+
if pendingExports[ticket] then
|
|
532
|
+
pendingExports[ticket] = nil
|
|
533
|
+
pendingExportCount = math.max(0, pendingExportCount - 1)
|
|
534
|
+
end
|
|
535
|
+
if protectedExports[ticket] then
|
|
536
|
+
protectedExports[ticket] = nil
|
|
537
|
+
protectedExportCount = math.max(0, protectedExportCount - 1)
|
|
538
|
+
end
|
|
539
|
+
if ticketOwners[ticket] == executionId then
|
|
540
|
+
ticketOwners[ticket] = nil
|
|
541
|
+
end
|
|
542
|
+
if db.automation.records[executionId] ~= record then
|
|
543
|
+
db.automation.records[executionId] = record
|
|
544
|
+
table.insert(db.automation.order, 1, executionId)
|
|
545
|
+
end
|
|
546
|
+
record.status = previousStatus
|
|
547
|
+
record.finishedAt = nil
|
|
548
|
+
record.ticket = nil
|
|
549
|
+
record.resultAvailable = false
|
|
550
|
+
record.errorCode = previousErrorCode
|
|
551
|
+
return nil, ns.L.EXPORT_FAILED
|
|
552
|
+
end
|
|
553
|
+
return ticket, entry
|
|
554
|
+
end
|
|
555
|
+
|
|
556
|
+
-- Finalize an execution index record without a result ticket. Used only when
|
|
557
|
+
-- even a minimal failure report could not be committed; no notice may be shown.
|
|
558
|
+
function ns.FailAutomationExecution(executionId, status, errorCode)
|
|
559
|
+
if not db or type(db.automation) ~= "table" then
|
|
560
|
+
return false
|
|
561
|
+
end
|
|
562
|
+
local record = type(executionId) == "string" and db.automation.records[executionId] or nil
|
|
563
|
+
if not record or not AUTOMATION_ACTIVE_STATUS[record.status] then
|
|
564
|
+
return false
|
|
565
|
+
end
|
|
566
|
+
record.status = AUTOMATION_TERMINAL_STATUS[status] and status or "failed"
|
|
567
|
+
record.finishedAt = time()
|
|
568
|
+
record.ticket = nil
|
|
569
|
+
record.resultAvailable = false
|
|
570
|
+
record.errorCode = type(errorCode) == "string" and errorCode or nil
|
|
571
|
+
PruneAutomationIndex()
|
|
572
|
+
return true
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
-- Clear terminal, unprotected automation index summaries. Evidence records in
|
|
576
|
+
-- the shared export store are intentionally kept; protected executions stay.
|
|
577
|
+
function ns.ClearAutomationHistory()
|
|
578
|
+
if not db or type(db.automation) ~= "table" then
|
|
579
|
+
return 0
|
|
580
|
+
end
|
|
581
|
+
local removed = 0
|
|
582
|
+
for index = #db.automation.order, 1, -1 do
|
|
583
|
+
local executionId = db.automation.order[index]
|
|
584
|
+
local record = db.automation.records[executionId]
|
|
585
|
+
local isProtected = record and record.ticket and protectedExports[record.ticket]
|
|
586
|
+
if record and AUTOMATION_TERMINAL_STATUS[record.status] and not isProtected then
|
|
587
|
+
db.automation.records[executionId] = nil
|
|
588
|
+
if record.ticket and ticketOwners[record.ticket] == executionId then
|
|
589
|
+
ticketOwners[record.ticket] = nil
|
|
590
|
+
end
|
|
591
|
+
table.remove(db.automation.order, index)
|
|
592
|
+
removed = removed + 1
|
|
593
|
+
end
|
|
594
|
+
end
|
|
595
|
+
return removed
|
|
596
|
+
end
|
|
597
|
+
|
|
598
|
+
function ns.GetAutomationIndex()
|
|
599
|
+
return db and db.automation or nil
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
function ns.GetAutomationRecord(executionId)
|
|
603
|
+
return db and db.automation and db.automation.records[executionId] or nil
|
|
604
|
+
end
|
|
605
|
+
|
|
606
|
+
function ns.FindAutomationByTicket(ticket)
|
|
607
|
+
if not db or not db.automation or type(ticket) ~= "string" then
|
|
608
|
+
return nil
|
|
609
|
+
end
|
|
610
|
+
local executionId = ticketOwners[ticket]
|
|
611
|
+
if executionId and db.automation.records[executionId] then
|
|
612
|
+
return db.automation.records[executionId]
|
|
613
|
+
end
|
|
614
|
+
for _, candidateId in ipairs(db.automation.order) do
|
|
615
|
+
local record = db.automation.records[candidateId]
|
|
616
|
+
if record and record.ticket == ticket then
|
|
617
|
+
return record
|
|
618
|
+
end
|
|
619
|
+
end
|
|
620
|
+
return nil
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
function ns.AutomationWritesEnabled()
|
|
624
|
+
return db ~= nil and not automationWritesBlocked and type(db.automation) == "table"
|
|
625
|
+
end
|
|
626
|
+
|
|
627
|
+
-- One place that answers "where is this result in its life cycle?" so the page,
|
|
628
|
+
-- the notices and any future caller cannot disagree. The host's own report wins
|
|
629
|
+
-- over the in-memory flush marker, because a result can be acknowledged while
|
|
630
|
+
-- still protected in the session that produced it:
|
|
631
|
+
-- pending - committed, still waiting for the output-side reload
|
|
632
|
+
-- flushed - on disk, no read reported yet
|
|
633
|
+
-- received - the host reported a successful read
|
|
634
|
+
-- failed - the host reported a failed read
|
|
635
|
+
-- expired - the evidence itself is gone; only the index summary remains
|
|
636
|
+
function ns.AutomationResultState(record)
|
|
637
|
+
if type(record) ~= "table" or not record.ticket then
|
|
638
|
+
return "none"
|
|
639
|
+
end
|
|
640
|
+
if not ns.GetExport(record.ticket) then
|
|
641
|
+
return "expired"
|
|
642
|
+
end
|
|
643
|
+
if record.receivedStatus == "received" then
|
|
644
|
+
return "received"
|
|
645
|
+
end
|
|
646
|
+
if record.receivedStatus == "failed" then
|
|
647
|
+
return "failed"
|
|
648
|
+
end
|
|
649
|
+
if protectedExports[record.ticket] then
|
|
650
|
+
return "pending"
|
|
651
|
+
end
|
|
652
|
+
return "flushed"
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
function ns.NextAutomationLocalRequestId()
|
|
656
|
+
if not db or type(db.automation) ~= "table" or automationWritesBlocked then
|
|
657
|
+
return nil
|
|
658
|
+
end
|
|
659
|
+
db.automation.nextLocalRequestId = db.automation.nextLocalRequestId + 1
|
|
660
|
+
return db.automation.nextLocalRequestId
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
function ns.IsExportProtected(ticket)
|
|
664
|
+
return type(ticket) == "string" and protectedExports[ticket] == true
|
|
665
|
+
end
|
|
666
|
+
|
|
667
|
+
function ns.GetProtectedExportCount()
|
|
668
|
+
return protectedExportCount
|
|
669
|
+
end
|
|
670
|
+
|
|
671
|
+
function ns.InitializeDatabase()
|
|
672
|
+
if db then
|
|
673
|
+
return
|
|
674
|
+
end
|
|
675
|
+
|
|
676
|
+
if type(LycheeDevDB) ~= "table" then
|
|
677
|
+
if type(DumperDB) == "table" then
|
|
678
|
+
LycheeDevDB = DumperDB
|
|
679
|
+
else
|
|
680
|
+
LycheeDevDB = {}
|
|
681
|
+
end
|
|
682
|
+
end
|
|
683
|
+
DumperDB = nil
|
|
684
|
+
|
|
685
|
+
-- Reload protection never persists; a SavedVariables load starts a fresh session.
|
|
686
|
+
protectedExports = {}
|
|
687
|
+
protectedExportCount = 0
|
|
688
|
+
ticketOwners = {}
|
|
689
|
+
automationWritesBlocked = false
|
|
690
|
+
|
|
691
|
+
if type(LycheeDevDB.history) ~= "table" then
|
|
692
|
+
LycheeDevDB.history = {}
|
|
693
|
+
end
|
|
694
|
+
|
|
695
|
+
local storedTreeMigrationRequired = type(LycheeDevDB.schemaVersion) ~= "number"
|
|
696
|
+
or LycheeDevDB.schemaVersion < SCHEMA_VERSION
|
|
697
|
+
if storedTreeMigrationRequired then
|
|
698
|
+
LycheeDevDB.schemaVersion = SCHEMA_VERSION
|
|
699
|
+
end
|
|
700
|
+
db = LycheeDevDB
|
|
701
|
+
historyBytes = 0
|
|
702
|
+
|
|
703
|
+
for index = #db.history, 1, -1 do
|
|
704
|
+
local entry = db.history[index]
|
|
705
|
+
if type(entry) ~= "table" then
|
|
706
|
+
table.remove(db.history, index)
|
|
707
|
+
else
|
|
708
|
+
entry.code = TrimText(entry.code, MAX_CODE_BYTES)
|
|
709
|
+
entry.result = TrimText(entry.result, MAX_RESULT_BYTES)
|
|
710
|
+
entry.succeeded = entry.succeeded and true or false
|
|
711
|
+
if type(entry.timestamp) ~= "number" then
|
|
712
|
+
entry.timestamp = nil
|
|
713
|
+
end
|
|
714
|
+
if type(entry.tree) ~= "table" then
|
|
715
|
+
entry.tree = nil
|
|
716
|
+
if storedTreeMigrationRequired and entry.succeeded and ns.CreateStoredTreeFromSerialized then
|
|
717
|
+
entry.tree = ns.CreateStoredTreeFromSerialized(entry.result)
|
|
718
|
+
end
|
|
719
|
+
end
|
|
720
|
+
historyBytes = historyBytes + GetHistoryEntryBytes(entry)
|
|
721
|
+
end
|
|
722
|
+
end
|
|
723
|
+
|
|
724
|
+
PruneHistoryToBudget()
|
|
725
|
+
InitializeExports()
|
|
726
|
+
InitializeAutomation()
|
|
727
|
+
|
|
728
|
+
ns.db = db
|
|
729
|
+
end
|
|
730
|
+
|
|
731
|
+
function ns.GetHistory()
|
|
732
|
+
return db and db.history or nil
|
|
733
|
+
end
|
|
734
|
+
|
|
735
|
+
function ns.AddHistory(code, result, succeeded, tree)
|
|
736
|
+
if not db then
|
|
737
|
+
return nil
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
local entry = {
|
|
741
|
+
code = TrimText(code, MAX_CODE_BYTES),
|
|
742
|
+
result = TrimText(result, MAX_RESULT_BYTES),
|
|
743
|
+
succeeded = succeeded and true or false,
|
|
744
|
+
timestamp = time(),
|
|
745
|
+
tree = type(tree) == "table" and tree or nil,
|
|
746
|
+
}
|
|
747
|
+
table.insert(db.history, 1, entry)
|
|
748
|
+
|
|
749
|
+
historyBytes = historyBytes + GetHistoryEntryBytes(entry)
|
|
750
|
+
PruneHistoryToBudget()
|
|
751
|
+
return entry
|
|
752
|
+
end
|
|
753
|
+
|
|
754
|
+
function ns.ClearHistory()
|
|
755
|
+
if db then
|
|
756
|
+
wipe(db.history)
|
|
757
|
+
historyBytes = 0
|
|
758
|
+
end
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
function ns.AddExport(kind, title, content, metadata)
|
|
763
|
+
if not db then
|
|
764
|
+
return nil, ns.L.EXPORT_DATABASE_UNAVAILABLE
|
|
765
|
+
end
|
|
766
|
+
content = type(content) == "string" and content or tostring(content or "")
|
|
767
|
+
if content == "" then
|
|
768
|
+
return nil, ns.L.EXPORT_EMPTY
|
|
769
|
+
end
|
|
770
|
+
|
|
771
|
+
local timestamp = time()
|
|
772
|
+
local ticket = AllocateTicket(timestamp)
|
|
773
|
+
|
|
774
|
+
local version, build, buildDate, interfaceVersion
|
|
775
|
+
if GetBuildInfo then
|
|
776
|
+
version, build, buildDate, interfaceVersion = GetBuildInfo()
|
|
777
|
+
end
|
|
778
|
+
local entry = CreateEvidenceRecord(ticket, kind, title, content, timestamp, metadata, {
|
|
779
|
+
clientId = ns.Client and ns.Client.id or nil,
|
|
780
|
+
version = version,
|
|
781
|
+
build = build,
|
|
782
|
+
buildDate = buildDate,
|
|
783
|
+
interface = interfaceVersion,
|
|
784
|
+
locale = GetLocale and GetLocale() or nil,
|
|
785
|
+
})
|
|
786
|
+
local entryBytes = GetExportEntryBytes(entry)
|
|
787
|
+
if entryBytes > MAX_EXPORT_BYTES then
|
|
788
|
+
return nil, ns.L.EXPORT_TOO_LARGE
|
|
789
|
+
end
|
|
790
|
+
|
|
791
|
+
db.exports.records[ticket] = entry
|
|
792
|
+
table.insert(db.exports.order, 1, ticket)
|
|
793
|
+
exportBytes = exportBytes + entryBytes
|
|
794
|
+
pendingExports[ticket] = true
|
|
795
|
+
pendingExportCount = pendingExportCount + 1
|
|
796
|
+
PruneExportsToBudget()
|
|
797
|
+
return ticket, entry
|
|
798
|
+
end
|
|
799
|
+
|
|
800
|
+
function ns.GetExport(ticket)
|
|
801
|
+
return db and db.exports and db.exports.records[ticket] or nil
|
|
802
|
+
end
|
|
803
|
+
|
|
804
|
+
function ns.GetExports()
|
|
805
|
+
return db and db.exports or nil
|
|
806
|
+
end
|
|
807
|
+
|
|
808
|
+
function ns.GetExportStats()
|
|
809
|
+
if not db or not db.exports then
|
|
810
|
+
return 0, 0, MAX_EXPORT_BYTES
|
|
811
|
+
end
|
|
812
|
+
return #db.exports.order, exportBytes, MAX_EXPORT_BYTES
|
|
813
|
+
end
|
|
814
|
+
|
|
815
|
+
function ns.IsExportPending(ticket)
|
|
816
|
+
return type(ticket) == "string" and pendingExports[ticket] == true
|
|
817
|
+
end
|
|
818
|
+
|
|
819
|
+
function ns.GetPendingExportCount()
|
|
820
|
+
return pendingExportCount
|
|
821
|
+
end
|
|
822
|
+
|
|
823
|
+
function ns.DeleteExport(ticket)
|
|
824
|
+
if not db or not db.exports or type(ticket) ~= "string"
|
|
825
|
+
or not db.exports.records[ticket] then
|
|
826
|
+
return false
|
|
827
|
+
end
|
|
828
|
+
-- Results awaiting their reload disk write cannot be deleted.
|
|
829
|
+
if protectedExports[ticket] then
|
|
830
|
+
return false
|
|
831
|
+
end
|
|
832
|
+
|
|
833
|
+
RemoveExport(ticket)
|
|
834
|
+
for index = 1, #db.exports.order do
|
|
835
|
+
if db.exports.order[index] == ticket then
|
|
836
|
+
table.remove(db.exports.order, index)
|
|
837
|
+
break
|
|
838
|
+
end
|
|
839
|
+
end
|
|
840
|
+
db.exports.totalBytes = exportBytes
|
|
841
|
+
return true
|
|
842
|
+
end
|
|
843
|
+
|
|
844
|
+
function ns.ClearExports()
|
|
845
|
+
if not db or not db.exports then
|
|
846
|
+
return 0
|
|
847
|
+
end
|
|
848
|
+
local removed = 0
|
|
849
|
+
for index = #db.exports.order, 1, -1 do
|
|
850
|
+
local ticket = db.exports.order[index]
|
|
851
|
+
if not protectedExports[ticket] then
|
|
852
|
+
table.remove(db.exports.order, index)
|
|
853
|
+
RemoveExport(ticket)
|
|
854
|
+
removed = removed + 1
|
|
855
|
+
end
|
|
856
|
+
end
|
|
857
|
+
db.exports.totalBytes = exportBytes
|
|
858
|
+
return removed
|
|
859
|
+
end
|