// 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).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) = { let found = str(value).match(regex("^(\\d{1,2})/(\\d{1,2})/(\\d{4})")) if found == none { none } else { date-index(int(found.captures.at(2)), int(found.captures.at(0)), int(found.captures.at(1))) } } #let fade-weight(rule, event-time, reference-day) = { 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) 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: ([^,]+)", default: "Unknown application") let module = capture(event.message, "Faulting module name: ([^,]+)", 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) = { 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) let rule = event-rule(rules, event) let fading = fade-weight(rule, event.time, reference-day) 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 groups.at(index) = item } } groups } #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") let events = group-events(parse-events(sections), rules, reference-day: report-day(generated)) let findings = () 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") if problems != "" and not lower(problems).contains("no problem devices found") { 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: float(match.captures.at(1)), free_gb: float(match.captures.at(2)), free_percent: float(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, ) }