This repository has been archived on 2026-09-16. You can view files and clone it, but cannot push or open issues or pull requests.
typst_reporter/diagnostics-normalize.typ

374 lines
24 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Pure-Typst normalizer for WinDiagnostics text transcripts.
#let get(dict, key, default) = if key in dict { dict.at(key) } else { default }
#let capture(text, pattern, default: "Unknown") = {
let found = text.match(regex(pattern))
if found == none { default } else { found.captures.at(0) }
}
#let number(value, default: 0.0) = {
let found = str(value).replace(",", ".").match(regex("-?\\d+(?:\\.\\d+)?"))
if found == none { default } else { float(found.text) }
}
#let is-leap-year(year) = calc.rem(year, 4) == 0 and (calc.rem(year, 100) != 0 or calc.rem(year, 400) == 0)
#let date-index(year, month, day) = {
let before-month = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
let completed-year = year - 1
365 * completed-year + int(calc.floor(completed-year / 4)) - int(calc.floor(completed-year / 100)) + int(calc.floor(completed-year / 400)) + before-month.at(month - 1) + day + if month > 2 and is-leap-year(year) { 1 } else { 0 }
}
#let report-day(value) = {
let found = str(value).match(regex("^(\\d{4})-(\\d{2})-(\\d{2})"))
if found == none { none } else { date-index(int(found.captures.at(0)), int(found.captures.at(1)), int(found.captures.at(2))) }
}
#let event-day(value, day-first: false) = {
let found = str(value).match(regex("^(\\d{1,2})/(\\d{1,2})/(\\d{4})"))
if found == none { none } else {
let year = int(found.captures.at(2))
let month = int(found.captures.at(if day-first { 1 } else { 0 }))
let day = int(found.captures.at(if day-first { 0 } else { 1 }))
if month < 1 or month > 12 or day < 1 or day > 31 { none } else { date-index(year, month, day) }
}
}
#let fade-weight(rule, event-time, reference-day, day-first: false) = {
if rule == none or not get(rule, "fading", false) or reference-day == none { (factor: 1.0, age_days: none) } else {
let observed-day = event-day(event-time, day-first: day-first)
if observed-day == none { (factor: 1.0, age_days: none) } else {
let age = calc.max(0, reference-day - observed-day)
let starts = get(rule, "fade_after_days", 7)
let reaches-half = calc.max(starts + 1, get(rule, "fade_to_half_days", 90))
let factor = if age <= starts { 1.0 } else { calc.max(0.5, 1.0 - 0.5 * (age - starts) / (reaches-half - starts)) }
(factor: factor, age_days: age)
}
}
}
#let parse-sections(text) = {
let headings = text.matches(regex("(?m)^--- (.+?) ---[ \\t]*$"))
let sections = (:)
for (index, heading) in headings.enumerate() {
let end = if index + 1 < headings.len() { headings.at(index + 1).start } else { text.len() }
sections.insert(heading.captures.at(0).trim(), text.slice(heading.end, end).trim())
}
sections
}
#let field(text, name, default: "Unknown") = capture(
text,
"(?m)^" + name + "[ \\t]*:[ \\t]*(.*?)[ \\t]*$",
default: default,
)
#let section(sections, name) = get(sections, name, "")
#let useful-lines(text) = text.split(regex("\\r?\\n")).filter(line => line.trim() != "")
#let table-count(text) = {
let lines = useful-lines(text).filter(line => {
let clean = line.trim()
let keep = clean.match(regex("^[ -]+$")) == none
keep = keep and not clean.ends-with(":")
keep = keep and not clean.starts-with("Name ")
keep = keep and not clean.starts-with("DisplayName ")
keep = keep and not clean.starts-with("DeviceID ")
keep = keep and not clean.starts-with("TaskName ")
keep = keep and not clean.starts-with("HotFixID ")
keep = keep and not clean.starts-with("ProcessName ")
keep = keep and not clean.starts-with("FriendlyName ")
keep = keep and not clean.starts-with("Model ")
keep
})
lines.len()
}
#let parse-events(sections) = {
let events = ()
for (name, body) in sections.pairs() {
if name.starts-with("RECENT ") and name.contains("ERRORS") {
let chunks = body.split(regex("(?m)^TimeCreated[ \\t]*:[ \\t]*"))
for chunk in chunks {
if chunk.contains("ProviderName") and chunk.contains("Id") {
let message-pos = chunk.position(regex("(?m)^Message[ \\t]*:[ \\t]*"))
let message = if message-pos == none {
"No message"
} else {
chunk.slice(message-pos).replace(regex("(?m)^Message[ \\t]*:[ \\t]*"), "").replace(regex("\\s+"), " ").trim()
}
events.push((
time: capture(chunk, "^([^\\n]+)", default: "Unknown"),
id: field(chunk, "Id", default: "?"),
provider: field(chunk, "ProviderName"),
message: message,
log: if name.contains("SYSTEM") { "System" } else { "Application" },
))
}
}
}
}
events
}
#let event-rule(rules, event) = {
get(rules, "event_rules", ()).find(
rule => get(rule, "provider", "") == event.provider and get(rule, "id", "") == event.id,
)
}
#let event-summary(event) = {
if event.provider == "MsiInstaller" {
let product = capture(event.message, "Product: (.*?) --", default: "Unknown product")
let error = capture(event.message, "Error (\\d+)", default: "unknown")
let description = capture(event.message, "-- Error \\d+\\. (.*?)(?:[ \t]+Action[ \t]|$)", default: "Windows Installer did not complete the transaction.")
product + " installation failed with MSI error " + error + ". " + description
} else if event.provider == "Application Error" and event.id == "1000" {
let application = capture(event.message, "(?:Faulting application name|Nom de l’application défaillante|Nom de l'application défaillante) *: ([^,]+)", default: "Unknown application")
let module = capture(event.message, "(?:Faulting module name|Nom du module défaillant) *: ([^,]+)", default: "unknown module")
let exception = capture(event.message, "Exception code: ([^ ]+)", default: "unknown code")
application + " terminated unexpectedly in " + module + " (exception " + exception + ")."
} else {
event.message
}
}
#let group-events(events, rules, reference-day: none, day-first: false) = {
let groups = ()
for event in events {
let index = groups.position(group => group.provider == event.provider and group.id == event.id)
if index == none {
let item = event
item.message = event-summary(event)
item.insert("count", 1)
item.insert("messages", (item.message,))
let rule = event-rule(rules, event)
let fading = fade-weight(rule, event.time, reference-day, day-first: day-first)
item.insert("severity", if rule == none { get(rules, "unknown_event_severity", "unanalysed") } else { get(rule, "severity", "unanalysed") })
item.insert("rationale", if rule == none { "FAIL TO ANALYSE — no matching rule in diagnostics-rules.yaml." } else { get(rule, "rationale", "") })
item.insert("remediation", if rule == none { "Add a reviewed provider/ID rule before attempting cleanup." } else { get(rule, "remediation", "") })
item.insert("resolution_steps", if rule == none { ("Review the raw event and add a rule to diagnostics-rules.yaml.", "Re-run the report after the rule has been reviewed.") } else { get(rule, "resolution_steps", ()) })
item.insert("reference_url", if rule == none { "" } else { get(rule, "reference_url", "") })
item.insert("reference_label", if rule == none { "" } else { get(rule, "reference_label", "Microsoft guidance") })
item.insert("confidence", if rule == none { 20 } else { get(rule, "confidence", 70) })
item.insert("weight_factor", fading.factor)
item.insert("age_days", fading.age_days)
item.insert("fading", rule != none and get(rule, "fading", false))
groups.push(item)
} else {
let item = groups.at(index)
item.count += 1
let summary = event-summary(event)
if not (summary in item.messages) {
item.messages.push(summary)
item.message = item.messages.join(" / ")
}
groups.at(index) = item
}
}
groups
}
// Correlate original records before provider/ID grouping loses timestamps.
#let event-second(value, day-first: false) = {
let day = event-day(value, day-first: day-first)
let clock = str(value).match(regex(" (\\d{1,2}):(\\d{2}):(\\d{2})"))
if day == none or clock == none { none } else {
day * 86400 + int(clock.captures.at(0)) * 3600 + int(clock.captures.at(1)) * 60 + int(clock.captures.at(2))
}
}
#let catalog-correlation(events, day-first: false) = {
let checksums = events.filter(event => event.provider == "ESENT" and event.id == "474" and lower(event.message).contains("catroot2") and event.message.contains("-1018"))
checksums.find(anchor => {
let time = event-second(anchor.time, day-first: day-first)
let nearby = events.filter(event => {
let other = event-second(event.time, day-first: day-first)
time != none and other != none and calc.abs(time - other) <= 120
})
(nearby.any(event => event.provider == "Application Error" and event.id == "1000" and lower(event.message).contains("cryptsvc") and lower(event.message).contains("esent.dll")) and
nearby.any(event => event.provider == "Microsoft-Windows-WindowsUpdateClient" and event.id == "20"))
})
}
#let normalize(source, rules: (:), source-name: "WinDiagnostics dump") = {
let text = source.replace(regex("\\r"), "")
let sections = parse-sections(text)
let os = section(sections, "OPERATING SYSTEM / COMPUTER / BIOS")
let cpu = section(sections, "CPU")
let uptime = section(sections, "UPTIME")
let defender = section(sections, "WINDOWS DEFENDER STATUS")
let battery = section(sections, "BATTERY")
let storage = section(sections, "STORAGE / FREE SPACE / HEALTH")
let generated = field(text, "Generated")
// Compare the local capture date with the ISO header; do not assume US dates.
let local-date = field(os, "LocalDateTime")
let day-first = event-day(local-date, day-first: true) == report-day(generated) and event-day(local-date) != report-day(generated)
let raw-events = parse-events(sections)
let events = group-events(raw-events, rules, reference-day: report-day(generated), day-first: day-first)
let findings = ()
let catalog-cluster = catalog-correlation(raw-events, day-first: day-first)
if catalog-cluster != none {
findings.push((severity: "critical", title: "Catalog corruption coincides with Cryptographic Services and update failures", detail: "Around " + catalog-cluster.time + ", CatRoot2 checksum failure (-1018), a CryptSvc crash in ESENT.dll, and Windows Update failure occur within two minutes of the checksum event. This supports a related failure sequence, but does not establish which hardware or software component caused the corruption. See the individual event evidence below.", confidence: 90, weight_factor: 0, resolution_steps: ("Back up important data and preserve event logs before resetting update components.", "Investigate storage and memory stability, including any ESENT memory warnings. Retest at stock CPU/RAM settings and review firmware guidance for the captured motherboard and CPU.", "Run Full diagnostics as administrator to obtain DISM and SFC results, then use those results to guide supported Windows component repair.", "Retry the failed update and verify Cryptographic Services remains stable with no new database checksum failures.")))
}
if field(text, "Administrator") == "False" {
findings.push((severity: "warning", title: "Diagnostic coverage is incomplete", detail: "The collector was not elevated. Skipped administrator-only checks, including DISM and SFC, are not evidence of healthy system files.", confidence: 100, resolution_steps: ("Run WinDiagnostics Full from an elevated PowerShell session.", "Review the new DISM, SFC, and other previously skipped results before closing the investigation.")))
}
let dism = section(sections, "WINDOWS COMPONENT STORE STATUS")
let sfc = section(sections, "SYSTEM FILE CHECK - VERIFY ONLY")
if lower(dism).contains("repairable") {
findings.push((severity: "warning", title: "Windows component store is repairable", detail: "DISM found component-store corruption, but classified the image as repairable with RestoreHealth rather than non-repairable.", confidence: 98, resolution_steps: ("Open an elevated terminal and run DISM.exe /Online /Cleanup-Image /RestoreHealth.", "After DISM succeeds, run sfc /scannow.", "Re-run diagnostics and confirm both checks are healthy."), reference_url: "https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/repair-a-windows-image?view=windows-11", reference_label: "Microsoft Windows image repair guidance"))
}
if lower(sfc).contains("integrity violations") {
findings.push((severity: "critical", title: "System-file integrity violations", detail: "SFC verification found integrity violations; review CBS.log and repair after DISM.", confidence: 98, resolution_steps: ("Repair the component store with DISM.exe /Online /Cleanup-Image /RestoreHealth.", "Run sfc /scannow after DISM completes.", "Re-run verification and confirm that no integrity violations remain."), reference_url: "https://support.microsoft.com/en-us/windows/experience/backup-recovery/using-system-file-checker-in-windows", reference_label: "Microsoft SFC and DISM guidance"))
}
for event in events {
if event.severity == "critical" or event.severity == "warning" or event.severity == "unanalysed" {
findings.push((severity: event.severity, title: event.provider + " · Event " + event.id, detail: event.message + " " + event.rationale, remediation: event.remediation, resolution_steps: event.resolution_steps, reference_url: event.reference_url, reference_label: event.reference_label, confidence: event.confidence, count: event.count, weight_factor: event.weight_factor, age_days: event.age_days, fading: event.fading))
}
}
let problems = section(sections, "DEVICES WITH PROBLEMS")
let disabled-displays = useful-lines(problems).filter(line => line.match(regex("^Error[ \t]+Display[ \t]+")) != none and line.contains("CM_PROB_DISABLED"))
for device in disabled-displays {
let name = capture(device, "^Error[ \t]+Display[ \t]+(.*?)[ \t]+PCI", default: "Display adapter")
findings.push((severity: "warning", title: name + " is disabled (Code 22)", detail: "The device reports CM_PROB_DISABLED. This records a disabled device, not proof of GPU hardware failure. The dump does not establish who or what disabled it.", confidence: 99, resolution_steps: ("Open Device Manager > Display adapters and inspect this adapter's status and Events tab.", "If it should be active, select Enable device and record any new status code.", "Restart and verify display acceleration and normal workload stability. If enabling fails or the device becomes disabled again, collect device events and review the installed vendor driver.", "Investigate concurrent IOMMU and memory/storage errors separately; this dump does not map the IOMMU device identifier to the GPU."), reference_url: "https://learn.microsoft.com/en-us/windows-hardware/drivers/install/cm-prob-disabled", reference_label: "Microsoft Code 22 guidance"))
}
let other-problems = useful-lines(problems).filter(line => line.starts-with("Error") and not (line in disabled-displays))
if problems != "" and not lower(problems).contains("no problem devices found") and (disabled-displays.len() == 0 or other-problems.len() > 0) {
findings.push((severity: "warning", title: "Devices report problems", detail: "One or more present devices did not report OK status.", confidence: 90, resolution_steps: ("Identify every reported device in Device Manager.", "Update, repair, or remove the associated driver or disconnected hardware.", "Re-run diagnostics and confirm that no present device reports a problem.")))
}
let disk-free = ()
for match in storage.matches(regex("(?m)^([A-Z]:)[ \\t]+.*?[ \\t](\\d+(?:[.,]\\d+)?)[ \\t]+(\\d+(?:[.,]\\d+)?)[ \\t]+(\\d+(?:[.,]\\d+)?)[ \\t]*$")) {
disk-free.push((drive: match.captures.at(0), size_gb: number(match.captures.at(1)), free_gb: number(match.captures.at(2)), free_percent: number(match.captures.at(3))))
}
let inventory-map = (
("Desktop programs", "INSTALLED PROGRAMS"),
("Store packages", "MICROSOFT STORE / APPX PACKAGES"),
("Automatic services", "AUTOMATIC SERVICES"),
("Signed drivers", "SIGNED DRIVERS"),
("Hotfixes", "WINDOWS UPDATES / HOTFIXES"),
("Scheduled tasks", "NON-MICROSOFT SCHEDULED TASKS"),
("Startup programs", "STARTUP PROGRAMS"),
("Listening endpoints", "ACTIVE TCP LISTENERS"),
)
let inventory = inventory-map.map(pair => (label: pair.at(0), count: table-count(section(sections, pair.at(1)))))
let excerpt-names = (
"OPERATING SYSTEM / COMPUTER / BIOS", "CPU", "MEMORY MODULES", "MOTHERBOARD",
"GPU / DISPLAY ADAPTERS", "STORAGE / FREE SPACE / HEALTH", "DEVICES WITH PROBLEMS",
"NETWORK ADAPTERS", "WINDOWS COMPONENT STORE STATUS", "SYSTEM FILE CHECK - VERIFY ONLY",
)
let excerpts = ()
for name in excerpt-names {
let value = section(sections, name)
if value != "" {
let level = "info"
if name == "OPERATING SYSTEM / COMPUTER / BIOS" {
value = value.replace(regex("\\n[ \\t]*\\n+"), "\\n")
}
if name == "CPU" {
value = "Name: " + field(value, "Name") + "\\nCores: " + field(value, "NumberOfCores", default: "?")
}
if name == "WINDOWS COMPONENT STORE STATUS" and not (lower(value).contains("repairable") or lower(value).contains("corrupt") or lower(value).contains("error")) {
value = ""
}
if name == "SYSTEM FILE CHECK - VERIFY ONLY" {
value = value.replace(regex("Verification \\d+% complete\\.[ \\t\\n]*"), "")
if lower(value).contains("integrity violations") { level = "critical" }
}
if value != "" { excerpts.push((title: name, text: value, severity: level)) }
}
}
let firewall = section(sections, "FIREWALL PROFILES")
let firewall-vanilla = firewall != "" and firewall.matches(regex("(?m)^(Domain|Private|Public)[ \\t]+True[ \\t]+NotConfigured[ \\t]+NotConfigured[ \\t]*$")).len() == 3
if firewall != "" and not firewall-vanilla {
excerpts.push((title: "FIREWALL PROFILES", text: firewall, severity: "warning"))
findings.push((severity: "warning", title: "Windows Firewall protection is incomplete", detail: "At least one standard firewall profile is disabled or differs from the expected enabled/default-policy state.", confidence: 95, resolution_steps: ("Confirm that organization policy does not intentionally disable the profile.", "Otherwise run Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled True in elevated PowerShell.", "Verify that all three profiles report Enabled=True, then re-run diagnostics."), reference_url: "https://learn.microsoft.com/en-us/windows/security/operating-system-security/network-security/windows-firewall/configure-with-command-line", reference_label: "Microsoft firewall command guidance"))
}
let defender-ok = defender != "" and (
field(defender, "AntivirusEnabled") == "True" and
field(defender, "AntispywareEnabled") == "True" and
field(defender, "RealTimeProtectionEnabled") == "True" and
field(defender, "BehaviorMonitorEnabled") == "True" and
field(defender, "IoavProtectionEnabled") == "True" and
field(defender, "NISEnabled") == "True"
)
if defender != "" and not defender-ok {
excerpts.push((title: "WINDOWS DEFENDER STATUS", text: defender, severity: "warning"))
}
let battery-charge = number(field(battery, "EstimatedChargeRemaining", default: "0"))
let battery-ok = battery == "" or (field(battery, "Status", default: "Unknown") == "OK" and battery-charge >= 20)
if battery != "" and not battery-ok {
excerpts.push((title: "BATTERY", text: battery, severity: if battery-charge < 10 { "critical" } else { "warning" }))
}
findings = findings.filter(item => item.severity == "critical") + findings.filter(item => item.severity == "warning") + findings.filter(item => item.severity == "unanalysed")
let coverage = ()
for (name, body) in sections.pairs() {
let included = excerpts.any(item => item.title == name)
let treatment = if included { "Detailed evidence" }
else if name.starts-with("RECENT ") and name.contains("ERRORS") { "Deduplicated findings" }
else if inventory-map.any(pair => pair.at(1) == name) { "Inventory count" }
else if name in ("IP CONFIGURATION", "IMPORTANT DIRECTORIES", "STARTUP FOLDERS", "DNS SERVERS") { "Reviewed; raw sensitive/noise-heavy values omitted" }
else if name in ("AUTOMATIC SERVICES NOT RUNNING", "POWER PLAN") { "Reviewed; routine snapshot omitted" }
else if name == "FIREWALL PROFILES" and firewall-vanilla { "Healthy vanilla configuration omitted" }
else if name == "WINDOWS DEFENDER STATUS" and defender-ok { "Healthy protection state omitted" }
else if name == "BATTERY" and battery-ok { "No actionable battery state omitted" }
else if name == "WINDOWS COMPONENT STORE STATUS" and not included { "Healthy result omitted" }
else { "Reviewed and summarized" }
let coverage-level = if name.starts-with("RECENT ") and name.contains("ERRORS") and events.any(event => event.severity == "critical") { "critical" }
else if included and excerpts.find(item => item.title == name).severity in ("critical", "warning") { excerpts.find(item => item.title == name).severity }
else if treatment.contains("omitted") or treatment.contains("Inventory") or treatment.contains("summarized") { "success" }
else { "info" }
coverage.push((section: name, use: treatment, lines: useful-lines(body).len(), severity: coverage-level))
}
let critical = findings.filter(item => item.severity == "critical").len()
let warnings = findings.filter(item => item.severity == "warning").len()
let unanalysed = findings.filter(item => item.severity == "unanalysed").len()
let penalty = 0.0
for finding in findings {
let base = if finding.severity == "critical" { 8 } else if finding.severity == "warning" { 2 } else if finding.severity == "unanalysed" { 1 } else { 0 }
penalty += base * get(finding, "weight_factor", 1.0)
}
let health-score = calc.round(calc.max(0, calc.min(100, 100 - penalty)))
let confidence-total = 0
for finding in findings { confidence-total += get(finding, "confidence", 50) }
let analysis-confidence = if findings.len() == 0 { 80 } else { calc.round(confidence-total / findings.len()) }
let uptime-days = field(uptime, "Days", default: none)
let uptime-hours = field(uptime, "Hours", default: none)
let uptime-minutes = field(uptime, "Minutes", default: none)
let uptime-value = if uptime-days == none or uptime-hours == none or uptime-minutes == none {
"Unanalysed"
} else {
uptime-days + "d " + uptime-hours + "h " + uptime-minutes + "m"
}
(
source: source-name,
mode: field(text, "Mode"),
generated: generated,
computer: field(text, "Computer"),
administrator: field(text, "Administrator"),
health: if critical > 0 { "Attention required" } else if warnings > 0 or unanalysed > 0 { "Review recommended" } else { "No actionable issue detected" },
health_score: health-score,
analysis_confidence: analysis-confidence,
system: (
os: field(os, "Caption"), version: field(os, "Version"), build: field(os, "BuildNumber"),
manufacturer: field(os, "Manufacturer"), model: field(os, "Model"), cpu: field(cpu, "Name"),
cores: field(cpu, "NumberOfCores", default: "?"), threads: field(cpu, "NumberOfLogicalProcessors", default: "?"),
ram_total_gb: number(field(os, "TotalMemoryGB", default: "0")), ram_free_gb: number(field(os, "FreeMemoryGB", default: "0")),
uptime: uptime-value,
),
security: (antivirus: field(defender, "AntivirusEnabled"), realtime: field(defender, "RealTimeProtectionEnabled"), signatures: field(defender, "AntivirusSignatureLastUpdated")),
battery: (charge: number(field(battery, "EstimatedChargeRemaining", default: "0")), status: field(battery, "Status", default: "Unavailable")),
disk_free: disk-free, inventory: inventory, findings: findings,
issue_counts: (critical: critical, warning: warnings, unanalysed: unanalysed), event_groups: events,
excerpts: excerpts, coverage: coverage,
)
}