reportist/plugins/diagnostics/renderer/normalize.typ

846 lines
46 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 schema_version 1 and 2 JSON reports.
//
// The collector now emits structured JSON. This file intentionally does not
// reconstruct tables or parse presentation text. It only performs reporting
// work: classification, correlation, scoring, evidence selection and shaping
// the collector schema for diagnostics-model.typ.
// The legacy converter used snake_case for CIM fields; native JSON preserves case.
#let get(dict, key, default) = {
if type(dict) != dictionary { default } else {
let matched = if key in dict { key } else {
dict.keys().find(candidate => lower(candidate.replace("_", "")) == lower(key.replace("_", "")))
}
if matched == none or dict.at(matched) == none { default } else { dict.at(matched) }
}
}
#let value-text(value) = {
if type(value) == dictionary {
value.pairs().map(pair => pair.at(0) + ": " + value-text(pair.at(1))).join("; ")
} else if type(value) == array {
value.map(value-text).join("\n")
} else if value == none { "Unknown" }
else if value == true { "Yes" }
else if value == false { "No" }
else { str(value) }
}
#let enabled(value) = value == true or value == 1
#let legacy-incomplete(report, name) = {
let value = get(get(report, "sections", (:)), name, (:))
get(report, "legacy", none) != none and (
name == "" or name == "storage" or
get(value, "data", ()).any(item => get(item, "text", none) != none)
)
}
#let first(values, default: (:)) = {
if values == none or values.len() == 0 { default } else { values.at(0) }
}
#let section(report, name) = {
let sections = get(report, "sections", (:))
get(sections, name, (status: "missing", count: 0, data: (), error: none))
}
#let section-data(report, name) = get(section(report, name), "data", ()).filter(item => get(item, "text", none) == none)
#let section-first(report, name) = first(section-data(report, name))
#let section-count(report, name) = if legacy-incomplete(report, name) or get(section(report, name), "status", "missing") != "ok" { "Unavailable" } else { get(section(report, name), "count", "Unknown") }
#let section-status(report, name) = get(section(report, name), "status", "missing")
#let number(value, default: 0.0) = {
if value == none { default }
else if type(value) == int or type(value) == float { value }
else {
let found = str(value).replace(",", ".").match(regex("-?\\d+(?:\\.\\d+)?"))
if found == none { default } else { float(found.text) }
}
}
#let bool-text(value) = if value == true { "Yes" } else if value == false { "No" } else { str(value) }
#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 datetime-day(value) = {
let text = str(value)
let iso = text.match(regex("^(\\d{4})-(\\d{2})-(\\d{2})"))
if iso != none {
date-index(int(iso.captures.at(0)), int(iso.captures.at(1)), int(iso.captures.at(2)))
} else {
// Windows PowerShell 5.1 may serialize DateTime values as /Date(ms)/.
let ms = text.match(regex("^/Date\\((-?\\d+)"))
if ms == none { none } else {
date-index(1970, 1, 1) + int(calc.floor(float(ms.captures.at(0)) / 86400000))
}
}
}
#let datetime-second(value) = {
let text = str(value)
let day = datetime-day(text)
if day == none { none } else {
let iso-clock = text.match(regex("T(\\d{2}):(\\d{2}):(\\d{2})"))
if iso-clock != none {
let offset = text.match(regex("([+-])(\\d{2}):(\\d{2})$"))
let shift = if offset == none { 0 } else { (if offset.captures.at(0) == "-" { -1 } else { 1 }) * (int(offset.captures.at(1)) * 3600 + int(offset.captures.at(2)) * 60) }
(day - date-index(1970, 1, 1)) * 86400 + int(iso-clock.captures.at(0)) * 3600 + int(iso-clock.captures.at(1)) * 60 + int(iso-clock.captures.at(2)) - shift
} else {
let ms = text.match(regex("^/Date\\((-?\\d+)"))
if ms == none { none } else { int(calc.floor(float(ms.captures.at(0)) / 1000)) }
}
}
}
// Display absolute event times in UTC; retain raw values for correlation/fading.
#let display-time(value) = {
let raw = str(value)
let seconds = datetime-second(raw)
if seconds == none { raw } else {
let instant = datetime(year: 1970, month: 1, day: 1, hour: 0, minute: 0, second: 0) + duration(seconds: seconds)
instant.display("[year]-[month]-[day] [hour]:[minute]:[second]") + " UTC"
}
}
#let display-date(value) = {
let text = str(value)
let iso = text.match(regex("^(\\d{4}-\\d{2}-\\d{2})"))
if iso == none { text } else { iso.captures.at(0) }
}
#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 = datetime-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 event-rule(rules, event) = get(rules, "event_rules", ()).find(
rule => get(rule, "provider", "") == event.provider and str(get(rule, "id", "")) == event.id,
)
#let capture(text, pattern, default: "Unknown") = {
let found = str(text).match(regex(pattern))
if found == none { default } else { found.captures.at(0) }
}
#let event-summary(event) = {
if event.provider == "Microsoft-Windows-WER-SystemErrorReporting" and event.id == "1001" and lower(event.message).contains("0x00000133") {
"DPC_WATCHDOG_VIOLATION (0x133): Windows detected excessive time in a DPC/ISR or at elevated interrupt level. Analyze the saved dump to identify the responsible component. " + event.message
} else 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|faulting module name|Nom du module défaillant) *: ([^,]+)", default: "unknown module")
let exception = capture(event.message, "(?:Exception code|exception code): ([^ ]+)", default: "unknown code")
application + " terminated unexpectedly in " + module + " (exception " + exception + ")."
} else {
event.message
}
}
#let event-sections(report) = {
let names = get(report, "sections", (:)).keys()
(
(if "recent_system_events" in names { "recent_system_events" } else { "recent_system_errors" }, "System"),
(if "recent_application_events" in names { "recent_application_events" } else { "recent_application_errors" }, "Application"),
("code_integrity", "Code Integrity"),
)
}
#let windows-level(level) = if level == 1 { "Critical" } else if level == 2 { "Error" } else if level == 3 { "Warning" } else if level == 4 { "Information" } else if level == 5 { "Verbose" } else { "Unknown" }
#let raw-events(report) = {
let events = ()
for (section-name, log-name) in event-sections(report) {
for item in section-data(report, section-name) {
events.push((
time: str(get(item, "TimeCreated", "Unknown")),
id: str(get(item, "Id", "?")),
provider: str(get(item, "ProviderName", "Unknown")),
message: str(get(item, "Message", "No message")) + if get(report, "legacy", none) != none {
item.pairs().filter(pair => not (pair.at(0) in ("time_created", "id", "provider_name", "message"))).map(pair => " " + pair.at(0).replace("_", " ") + ": " + str(if pair.at(1) == none { "" } else { pair.at(1) })).join("")
} else { "" },
log: log-name,
source_section: section-name,
level: get(item, "Level", if section-name.ends-with("_errors") { 2 } else { none }),
level_display: get(item, "LevelDisplayName", "Unknown"),
))
}
}
events
}
#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 and get(group, "log", "") == get(event, "log", "") and get(group, "level", none) == get(event, "level", none))
if index == none {
let item = event
item.message = event-summary(event)
item.insert("count", 1)
item.insert("messages", (item.message,))
item.insert("occurrences", (event,))
let rule = event-rule(rules, event)
let fading = fade-weight(rule, event.time, reference-day)
item.insert("severity", if rule == none and get(event, "level", none) in (4, 5) { "info" } else if rule == none { get(rules, "unknown_event_severity", "unanalysed") } else { get(rule, "severity", "unanalysed") })
item.insert("rationale", if rule == none and get(event, "level", none) in (4, 5) { "Informational record retained without a reviewed interpretation; no fault inferred." } else if rule == none { "No reviewed interpretation is available for this event." } 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 config/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
item.occurrences.push(event)
let summary = event-summary(event)
if not (summary in item.messages) {
item.messages.push(summary)
item.message = item.messages.join(" / ")
}
// Keep the latest parseable event timestamp for display/fading context.
let old-time = datetime-second(item.time)
let new-time = datetime-second(event.time)
if new-time != none and (old-time == none or new-time > old-time) {
item.time = event.time
let fading = fade-weight(event-rule(rules, event), event.time, reference-day)
item.weight_factor = fading.factor
item.age_days = fading.age_days
}
groups.at(index) = item
}
}
groups
}
// Correlate original records before provider/ID grouping loses timestamps.
#let catalog-correlation(events) = {
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 = datetime-second(anchor.time)
let nearby = events.filter(event => {
let other = datetime-second(event.time)
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 command-output(report, name) = {
let item = section-first(report, name)
let output = get(item, "output", get(section(report, name), "legacy_raw", ()))
let text = if type(output) == array { output.map(value => str(value)).join("\n", default: "") } else { str(output) }
text.replace("\r", "\n").split("\n").filter(line => line.trim() != "" and not lower(line).contains("% complete")).join("\n", default: "")
}
#let object-lines(item, fields) = {
fields.map(pair => pair.at(0) + ": " + if pair.at(1) in ("LastWriteTime", "TimeGenerated", "DriverDate") { display-time(get(item, pair.at(1), "Unknown")) } else { value-text(get(item, pair.at(1), "Unknown")) }).join("\n", default: "")
}
#let watchlist-catalog = yaml("../config/diagnostics-watchlist.yaml")
#let watch-matches(report, matcher) = {
let name = matcher.section
// Never interpret incomplete collection as confirmed installation/enabling.
if section-status(report, name) != "ok" { () } else {
section-data(report, name).filter(item => {
let value = lower(value-text(get(item, matcher.field, ""))).trim()
if get(matcher, "basename", false) {
value = value.trim("\"").replace("\\", "/").split("/").last()
}
let identity = matcher.values.any(candidate => lower(candidate) == value)
let version-ok = get(matcher, "versions", ()).len() == 0 or get(matcher, "versions", ()).contains(value-text(get(item, get(matcher, "version_field", "DriverVersion"), "")))
let state-ok = not get(matcher, "enabled_only", false) or get(item, "State", none) in (2, "Enabled", "enabled")
identity and version-ok and state-ok
}).map(item => (
section: name,
identity: value-text(get(item, matcher.field, "Unknown")),
version: value-text(get(item, "DriverVersion", get(item, "DisplayVersion", none))),
state: value-text(get(item, "State", none)),
))
}
}
#let watchlist-findings(report, events, catalog: watchlist-catalog) = {
let matches = ()
for entry in catalog.entries {
let evidence = entry.match.map(matcher => watch-matches(report, matcher)).flatten().dedup()
let prerequisites = get(entry, "requires", ()).all(matcher => watch-matches(report, matcher).len() > 0)
let event-gate = get(entry, "events", none)
let signals = if event-gate == none { () } else { events.filter(e => e.provider == event-gate.provider and e.id in event-gate.ids) }
if evidence.len() > 0 and prerequisites and (event-gate == none or signals.len() >= event-gate.minimum) {
matches.push((
severity: "warning", title: entry.title, watch_id: entry.id,
detail: entry.detail + " Evidence: " + evidence.map(e => e.section + " / " + e.identity + " (version " + e.version + "; state " + e.state + ")").join("; ") + if signals.len() > 0 { ". Matching events: " + str(signals.len()) + "." } else { "." },
confidence: 85, weight_factor: get(entry, "weight_factor", 0),
resolution_steps: (entry.action,), reference_url: entry.reference_url,
reference_label: "Known-issue advisory", matched_records: evidence,
))
}
}
matches
}
// Reliability history can retain actionable records omitted from a bounded log.
// Keep only reviewed actionable provider/ID pairs and avoid counting the same
// source event twice. Same-second comparison allows collector timestamp rounding;
// normalized messages must also match so distinct events are not discarded.
#let reliability-events(report, raw, rules) = {
let extra = ()
for item in section-data(report, "reliability_records") {
let event = (provider: get(item, "SourceName", "Unknown"), id: value-text(get(item, "EventIdentifier", "?")), time: value-text(get(item, "TimeGenerated", "Unknown")), message: value-text(get(item, "Message", "No message")), log: "Reliability", source_section: "reliability_records", level: none, level_display: "Not supplied")
let rule = event-rule(rules, event)
let when = datetime-second(event.time)
let duplicate = (raw + extra).any(e => e.provider == event.provider and e.id == event.id and get(e, "message", "").replace(regex("\\s+"), " ").trim() == event.message.replace(regex("\\s+"), " ").trim() and (e.time == event.time or (when != none and datetime-second(e.time) == when)))
if rule != none and get(rule, "severity", "info") in ("warning", "critical") and not duplicate { extra.push(event) }
}
extra
}
#let normalize(report, rules: (:), source-name: "WinDiagnostics JSON") = {
let schema = get(report, "schema_version", 0)
if schema not in (1, 2) { panic("Unsupported WinDiagnostics schema_version: " + str(schema) + "; supported versions: 1 and 2") }
if type(report) != dictionary { panic("Malformed WinDiagnostics report: expected a JSON object") }
if type(get(report, "sections", (:))) != dictionary { panic("Malformed WinDiagnostics sections: expected an object") }
for (name, value) in get(report, "sections", (:)).pairs() {
if type(value) != dictionary or type(get(value, "data", ())) != array {
panic("Malformed WinDiagnostics section " + name + ": expected an envelope with optional data array")
}
}
let generated-raw = get(report, "generated_at", "Unknown")
let generated = if generated-raw == "Unknown" { "Unknown" } else { display-date(generated-raw) }
let reference-day = datetime-day(generated-raw)
let host = get(report, "host", (:))
let mode = str(get(report, "mode", "Unknown"))
let administrator = get(host, "administrator", false)
// Full/Mid reports use sections. Compact reports use summary.
let compact = lower(mode) == "compact"
let summary = get(report, "summary", (:))
// Use available check metadata regardless of the producer's version label.
// Older Compact captures contain summary values without check envelopes.
let compact-checks = compact and get(report, "sections", (:)).len() > 0
let os = if compact { get(summary, "operating_system", (:)) } else { section-first(report, "operating_system") }
let system = if compact { get(summary, "system", (:)) } else { section-first(report, "computer_system") }
let cpu = if compact { get(summary, "cpu", (:)) } else { section-first(report, "cpu") }
let uptime = if compact { get(summary, "uptime", (:)) } else { section-first(report, "uptime") }
let defender = if compact { get(summary, "defender", (:)) } else { section-first(report, "defender") }
let battery = if compact { (:) } else { section-first(report, "battery") }
let storage-root = if compact { get(summary, "storage", (:)) } else { section-first(report, "storage") }
let logical-volumes = if compact { get(storage-root, "logical_volumes", ()) } else { get(storage-root, "logical_volumes", ()) }
let raw = if compact { () } else { raw-events(report) }
let reliability = reliability-events(report, raw, rules)
let events = group-events(raw + reliability, rules, reference-day: reference-day)
let findings = ()
if get(report, "legacy", none) != none {
findings.push((severity: "warning", title: "Legacy conversion has incomplete evidence", detail: "This report was converted from formatted text. Table rows and event-message formatting may have been lost. Unrecoverable inventories and device/firewall states are unavailable; empty converted storage is not evidence that no disks exist. Legacy event dates retain their original format and receive no age discount.", confidence: 100, weight_factor: 0, resolution_steps: ("Collect a new native JSON report with WinDiagnostics Full to replace incomplete evidence.",)))
}
for warning in get(report, "warnings", ()) {
findings.push((severity: "warning", title: "Collector warning", detail: str(warning), confidence: 100, weight_factor: 0))
}
for (name, value) in get(report, "sections", (:)).pairs() {
if get(value, "status", "missing") not in ("ok", "missing") {
findings.push((severity: "warning", title: "Collector coverage: " + name, detail: get(value, "status", "") + ": " + str(get(value, "error", "No result collected")), confidence: 100, weight_factor: 0))
}
}
findings += watchlist-findings(report, raw)
let catalog-cluster = catalog-correlation(raw + reliability)
if catalog-cluster != none {
findings.push((
severity: "critical",
title: "Catalog corruption coincides with Cryptographic Services and update failures",
detail: "Around " + display-time(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.",
confidence: 90,
weight_factor: 0,
resolution_steps: (
"Back up important data and preserve event logs before resetting update components.",
"Investigate storage and memory stability. 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 not administrator {
findings.push((
severity: "warning",
title: "Diagnostic coverage is incomplete",
detail: "The collector was not elevated. Administrator-only checks, including DISM and SFC, may be skipped and 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 = if compact { "" } else { command-output(report, "component_store") }
let sfc = if compact { "" } else { command-output(report, "system_file_check") }
if lower(dism).contains("repairable") and not lower(dism).contains("non-repairable") {
findings.push((
severity: "warning",
title: "Windows component store is repairable",
detail: "DISM found component-store corruption and classified the image as repairable with RestoreHealth.",
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") and not lower(sfc).contains("did not find any 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 in ("critical", "warning", "unanalysed") {
findings.push((
severity: event.severity,
title: event.provider + " · Event " + event.id,
detail: event.rationale,
evidence: event.messages.first(),
event_key: event.provider + "/" + event.id,
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 problem-devices = if compact { () } else { section-data(report, "problem_devices").map(device => {
for (key, alias) in (("FriendlyName", "Name"), ("Class", "PNPClass"), ("InstanceId", "DeviceID")) {
if get(device, key, none) == none { device.insert(key, get(device, alias, "Unknown")) }
}
device
}) }
for device in problem-devices {
if (get(device, "ConfigManagerErrorCode", 0) == 22 or str(get(device, "Problem", "")).contains("CM_PROB_DISABLED")) {
findings.push((severity: "warning", title: str(get(device, "FriendlyName", get(device, "Name", "Device"))) + " is disabled (Code 22)", detail: "The device is disabled. This does not establish hardware failure or who disabled it.", confidence: 99, resolution_steps: ("Inspect the adapter status and Events tab in Device Manager.", "If it should be active, enable it, restart, and verify the device works under its normal workload.")))
}
}
if problem-devices.len() > 0 or (compact and (not compact-checks or section-status(report, "problem_devices") == "ok") and get(get(summary, "devices", (:)), "problem_count", 0) > 0) {
findings.push((
severity: "warning",
title: "Devices report problems",
detail: str(if compact { get(get(summary, "devices", (:)), "problem_count", 0) } else { problem-devices.len() }) + " present device(s) did not report a healthy state.",
confidence: 90,
resolution_steps: (
"Identify every reported device in Device Manager.",
"Update, repair, enable, or remove the associated driver or disconnected hardware as appropriate.",
"Re-run diagnostics and confirm that no present device reports a problem.",
),
))
}
let disk-free = logical-volumes.map(disk => (
drive: str(get(disk, "drive", get(disk, "DeviceID", "Disk"))),
size_gb: number(get(disk, "size_gb", 0)),
free_gb: number(get(disk, "free_gb", 0)),
free_percent: number(get(disk, "free_percent", 0)),
))
let low-disk = disk-free.filter(disk => disk.free_percent < 10)
if low-disk.len() > 0 {
findings.push((
severity: "warning",
title: "Low storage headroom",
detail: low-disk.map(disk => disk.drive + " has " + str(disk.free_percent) + "% free space").join("; ") + ".",
confidence: 100,
resolution_steps: (
"Review large or temporary files on the affected volume.",
"Preserve sufficient free space for Windows servicing, updates, paging, and application workloads.",
"Re-run diagnostics after cleanup and confirm storage headroom has recovered.",
),
))
}
let firewall = if compact { () } else { section-data(report, "firewall_profiles") }
let firewall-ok = firewall.len() == 0 or firewall.all(item => enabled(get(item, "Enabled", none)))
if firewall.len() > 0 and not firewall-ok {
findings.push((
severity: "critical",
title: "Windows Firewall protection is incomplete",
detail: "At least one captured firewall profile is disabled.",
confidence: 95,
resolution_steps: (
"Confirm that organization policy does not intentionally disable the profile.",
"Otherwise enable the required Domain, Private, and Public profiles in elevated PowerShell.",
"Verify the resulting profile state and 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 (
get(defender, "AntivirusEnabled", false) == true and
get(defender, "AntispywareEnabled", false) == true and
get(defender, "RealTimeProtectionEnabled", false) == true and
get(defender, "BehaviorMonitorEnabled", false) == true and
get(defender, "IoavProtectionEnabled", false) == true and
get(defender, "NISEnabled", false) == true
)
// Only explicit health/signing evidence creates a finding, never absent fields.
for (source, records) in (("storage.storage_health (Get-PhysicalDisk)", get(storage-root, "storage_health", ())), ("storage_disks (Get-Disk)", section-data(report, "storage_disks"))) {
let unhealthy = records.filter(item => lower(value-text(get(item, "HealthStatus", ""))) in ("warning", "unhealthy", "1", "2"))
if unhealthy.len() > 0 {
findings.push((severity: "warning", title: "Storage health requires review: " + source,
detail: unhealthy.map(item => value-text(get(item, "FriendlyName", "Unidentified disk")) + ": " + value-text(get(item, "HealthStatus", "Unknown"))).join("; ") + ". Source device sets are not joined by position.",
confidence: 90, resolution_steps: ("Preserve important data and identify each disk using its serial or device identifier. Review vendor diagnostics before attributing physical failure.",)))
}
}
let unsigned = section-data(report, "signed_drivers").filter(item => get(item, "IsSigned", none) == false)
if unsigned.len() > 0 {
findings.push((severity: "warning", title: "Drivers explicitly report unsigned status",
detail: str(unsigned.len()) + " records in signed_drivers report IsSigned=false. This is not proof of malware or crash causation. " + unsigned.slice(0, calc.min(3, unsigned.len())).map(item => value-text(get(item, "DeviceName", get(item, "InfName", "Unnamed device")))).join("; ") + if unsigned.len() > 3 { "; remaining identities are in source JSON." } else { "." },
confidence: 90, resolution_steps: ("Verify each driver package and signer with the OEM. Obtain a trusted signed replacement where appropriate; keep signing enforcement enabled.",)))
}
let battery-charge = number(get(battery, "EstimatedChargeRemaining", 0))
let battery-present = battery != (:)
let battery-ok = not battery-present or (str(get(battery, "Status", "Unknown")) == "OK" and battery-charge >= 20)
let inventory = if compact {
let software = get(summary, "software", (:))
let startup = get(summary, "startup", (:))
let services = get(summary, "services", (:))
(
(label: "Desktop programs", count: get(software, "desktop_program_count", 0)),
(label: "Store packages", count: get(software, "appx_package_count", 0)),
(label: "Startup commands", count: get(startup, "command_count", 0)),
(label: "Automatic services", count: get(services, "automatic_count", 0)),
)
} else {
(
(label: "Desktop programs", count: section-count(report, "installed_programs")),
(label: "Store packages", count: section-count(report, "appx_packages")),
(label: "Automatic services", count: section-count(report, "automatic_services")),
(label: "Signed drivers", count: section-count(report, "signed_drivers")),
(label: "Hotfixes", count: section-count(report, "hotfixes")),
(label: "Scheduled tasks", count: section-count(report, "non_microsoft_scheduled_tasks")),
(label: "Startup programs", count: section-count(report, "startup_programs")),
(label: "Listening endpoints", count: section-count(report, "active_tcp_listeners")),
)
}
if compact-checks {
for (index, name) in ("installed_programs", "appx_packages", "startup_programs", "automatic_services").enumerate() {
if section-status(report, name) != "ok" { inventory.at(index).count = "Unavailable" }
}
}
// Absent sections belong only in the final coverage recap.
let inventory-names = if compact { ("installed_programs", "appx_packages", "startup_programs", "automatic_services") } else { ("installed_programs", "appx_packages", "automatic_services", "signed_drivers", "hotfixes", "non_microsoft_scheduled_tasks", "startup_programs", "active_tcp_listeners") }
if not compact or compact-checks {
inventory = inventory.enumerate().filter(pair => section-status(report, inventory-names.at(pair.at(0))) != "missing").map(pair => pair.at(1))
}
let excerpts = ()
for (name, title, fields) in (
("bios", "BIOS", (("Manufacturer", "Manufacturer"), ("Version", "Name"))),
("motherboard", "MOTHERBOARD", (("Manufacturer", "Manufacturer"), ("Product", "Product"))),
("memory_modules", "MEMORY MODULES", (("Capacity GB", "capacity_gb"), ("Configured clock", "ConfiguredClockSpeed"), ("Part number", "PartNumber"))),
("gpus", "GPU / DISPLAY ADAPTERS", (("Name", "Name"), ("Driver", "DriverVersion"))),
) {
let records = section-data(report, name)
if records.len() > 0 {
excerpts.push((title: title, severity: "info", text: records.map(item => object-lines(item, fields)).join("\n\n")))
}
}
let evidence-sections = (
"signed_drivers", "system_drivers", "services", "storage_disks", "storage_pnp_devices",
"driver_store", "filter_drivers", "filter_instances", "upper_filters", "lower_filters",
"boot_configuration", "enabled_windows_features", "reliability_records", "minidumps",
)
let count-only-sections = ("system_drivers", "services", "storage_pnp_devices", "driver_store", "filter_instances", "upper_filters", "lower_filters", "boot_configuration")
let excerpt-fields = (
signed_drivers: (("Device", "DeviceName"), ("Driver", "DriverVersion"), ("INF", "InfName"), ("IsSigned", "IsSigned")),
storage_disks: (("Disk", "Number"), ("Name", "FriendlyName"), ("Bus", "BusType"), ("Health", "HealthStatus"), ("IsBoot", "IsBoot")),
minidumps: (("File", "Name"), ("Bytes", "Length"), ("Modified", "LastWriteTime")),
enabled_windows_features: (("Feature", "FeatureName"),),
reliability_records: (("Source", "SourceName"), ("Event", "EventIdentifier"), ("Product", "ProductName")),
)
for name in evidence-sections {
let records = section-data(report, name)
if name != "signed_drivers" and section-status(report, name) != "missing" { inventory.push((label: name.replace("_", " "), count: section-count(report, name))) }
let failed = section-status(report, name) != "ok"
if records.len() > 0 and (name not in count-only-sections or failed) {
// Reliability successes are inventory noise; actionable records feed events.
let candidates = if name == "reliability_records" {
records.filter(item => {
let rule = event-rule(rules, (provider: get(item, "SourceName", ""), id: value-text(get(item, "EventIdentifier", ""))))
rule != none and get(rule, "severity", "info") in ("warning", "critical")
})
} else if name == "signed_drivers" {
let relevant(item) = get(item, "IsSigned", none) == false or problem-devices.any(device => get(item, "DeviceID", none) != none and get(item, "DeviceID", none) == get(device, "InstanceId", none))
records.filter(relevant) + records.filter(item => not relevant(item))
} else { records }
let selected = candidates.slice(0, calc.min(3, candidates.len()))
if selected.len() > 0 {
let fields = get(excerpt-fields, name, ())
let text = selected.map(item => if fields.len() > 0 { object-lines(item, fields).replace("\n", " · ") } else { value-text(item) }).join("\n")
excerpts.push((title: upper(name.replace("_", " ")), severity: if failed { "warning" } else { "info" }, text: "Source: sections." + name + "; showing " + str(selected.len()) + " of " + str(records.len()) + " returned records. Full records remain in source JSON.\n" + text.slice(0, calc.min(1200, text.len())) + if text.len() > 1200 { "\n[Display shortened at 1200 characters; see source JSON.]" } else { "" }))
}
}
}
for name in ("component_store", "system_file_check") {
if section-status(report, name) != "ok" and section-data(report, name).len() > 0 {
excerpts.push((title: upper(name), severity: "warning", text: repr(section-data(report, name))))
}
}
if not compact and storage-root != (:) {
excerpts.push((title: "STORAGE SOURCES", severity: "info", text: get(storage-root, "physical_disks", ()).map(item => object-lines(item, (("Disk", "Model"), ("Interface", "InterfaceType"), ("Status", "Status"), ("Firmware", "FirmwareRevision"))).replace("\n", " · ")).join("\n")))
}
let storage-health = get(storage-root, "storage_health", ())
let unhealthy-storage = storage-health.filter(item => lower(str(get(item, "HealthStatus", "Unknown"))) != "healthy")
if unhealthy-storage.len() > 0 {
excerpts.push((
title: "STORAGE HEALTH",
severity: "warning",
text: unhealthy-storage.map(item => object-lines(item, (("Disk", "FriendlyName"), ("Health", "HealthStatus"), ("Operational", "OperationalStatus")))).join("\n\n"),
))
}
if problem-devices.len() > 0 {
excerpts.push((
title: "DEVICES WITH PROBLEMS",
severity: "warning",
text: problem-devices.map(item => object-lines(item, (("Status", "Status"), ("Class", "Class"), ("Device", "FriendlyName"), ("Instance", "InstanceId")))).join("\n\n"),
))
}
if dism != "" and (lower(dism).contains("repairable") or lower(dism).contains("corrupt") or lower(dism).contains("error")) {
excerpts.push((title: "WINDOWS COMPONENT STORE STATUS", text: dism, severity: "warning"))
}
if sfc != "" {
let sfc-level = if lower(sfc).contains("integrity violations") and not lower(sfc).contains("did not find any integrity violations") { "critical" } else { "info" }
excerpts.push((title: "SYSTEM FILE CHECK - VERIFY ONLY", text: sfc, severity: sfc-level))
}
if firewall.len() > 0 and not firewall-ok {
excerpts.push((
title: "FIREWALL PROFILES",
severity: "warning",
text: firewall.map(item => object-lines(item, (("Profile", "Name"), ("Enabled", "Enabled"), ("Inbound", "DefaultInboundAction"), ("Outbound", "DefaultOutboundAction")))).join("\n\n"),
))
}
if defender != (:) and not defender-ok {
excerpts.push((
title: "WINDOWS DEFENDER STATUS",
severity: "warning",
text: object-lines(defender, (
("Antivirus", "AntivirusEnabled"),
("Antispyware", "AntispywareEnabled"),
("Real-time", "RealTimeProtectionEnabled"),
("Behavior monitor", "BehaviorMonitorEnabled"),
("IOAV", "IoavProtectionEnabled"),
("NIS", "NISEnabled"),
)),
))
}
if battery-present and not battery-ok {
excerpts.push((
title: "BATTERY",
severity: if battery-charge < 10 { "critical" } else { "warning" },
text: object-lines(battery, (("Name", "Name"), ("Status", "Status"), ("Charge", "EstimatedChargeRemaining"), ("Battery status", "BatteryStatus"))),
))
}
let inventory-sections = (
"installed_programs", "appx_packages", "automatic_services", "signed_drivers",
"hotfixes", "non_microsoft_scheduled_tasks", "startup_programs", "active_tcp_listeners",
)
let sensitive-sections = ("ip_configuration", "important_directories", "startup_folders", "dns_servers")
let routine-sections = ("automatic_services_not_running", "power_plan", "top_processes_by_memory")
let detailed-sections = (
"storage", "problem_devices", "component_store", "system_file_check", "firewall_profiles", "defender", "battery",
)
let coverage = ()
if compact { coverage.push((section: "summary", use: "Compact summary; check individual collection states", records: 1, severity: "info")) }
for (name, value) in get(report, "sections", (:)).pairs() {
let status = get(value, "status", "missing")
let event-source = event-sections(report).any(pair => pair.at(0) == name)
let use = if status != "ok" { status + ": " + str(get(value, "error", "No verified result")) }
else if legacy-incomplete(report, name) { "Legacy conversion incomplete; recollect JSON" }
else if event-source { "Grouped by log, provider, ID and Windows level" }
else if name in count-only-sections { "Count and watchlist review; full records in source" }
else if name == "reliability_records" { "Reviewed failure events; successful updates omitted" }
else if name in evidence-sections { "Count and selected fields (up to 3 records)" }
else if compact { "Compact summary check completed" }
else if name in inventory-sections { "Inventory count" }
else if name in detailed-sections { "Structured health evidence" }
else { "Retained in source; no specific analysis" }
if event-source {
use += "; truncation: " + if get(value, "truncated", none) == true { "yes; records omitted" } else if get(value, "truncated", none) == false { "no" } else { "unknown" }
use += "; limit: " + str(get(value, "limit", "unknown"))
}
coverage.push((section: name, use: use, records: section-count(report, name), severity: if status not in ("ok", "missing") or legacy-incomplete(report, name) { "warning" } else { "info" }))
}
// Outer success does not imply that nested APIs were available.
for (name, records) in (("storage.storage_health", get(storage-root, "storage_health", ())), ("storage.reliability_counters", get(storage-root, "reliability_counters", ())), ("defender", (defender,))) {
for item in records {
if get(item, "status", "ok") != "ok" or get(item, "available", true) == false {
coverage.push((section: name, use: str(get(item, "status", "unavailable")) + ": " + str(get(item, "error", "Nested check unavailable")), records: "Unavailable", severity: "warning"))
}
}
}
let recap-sections = if compact { ("operating_system", "computer_system", "problem_devices") } else { ("operating_system", "computer_system", "storage", "problem_devices", "defender", "component_store", "system_file_check") + inventory-sections + evidence-sections + ("bios", "cpu", "memory_modules", "motherboard", "gpus", "code_integrity") + event-sections(report).slice(0, 2).map(pair => pair.at(0)) }
for name in recap-sections.dedup() {
if section-status(report, name) == "missing" and not coverage.any(item => item.section == name) and not (compact and not compact-checks) {
coverage.push((section: name, use: "Not supplied in this capture", records: "Unavailable", severity: "info"))
}
}
let incomplete = compact or get(report, "legacy", none) != none or coverage.any(item => item.severity == "warning") or event-sections(report).any(pair => get(section(report, pair.at(0)), "truncated", false) == true)
findings = findings.filter(item => item.severity == "critical") + findings.filter(item => item.severity == "warning") + findings.filter(item => item.severity == "unanalysed")
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-value = if uptime == (:) {
"Unanalysed"
} else {
str(get(uptime, "days", get(uptime, "Days", "?"))) + "d " + str(get(uptime, "hours", get(uptime, "Hours", "?"))) + "h " + str(get(uptime, "minutes", get(uptime, "Minutes", "?"))) + "m"
}
let os-name = str(get(os, "caption", get(os, "Caption", "Unknown")))
let os-version = str(get(os, "version", get(os, "Version", "Unknown")))
let os-build = str(get(os, "build_number", get(os, "BuildNumber", "Unknown")))
let manufacturer = str(get(system, "manufacturer", get(system, "Manufacturer", "Unknown")))
let model = str(get(system, "model", get(system, "Model", "Unknown")))
let cpu-name = str(get(cpu, "name", get(cpu, "Name", "Unknown")))
let cores = get(cpu, "cores", get(cpu, "NumberOfCores", "?"))
let threads = get(cpu, "logical_processors", get(cpu, "NumberOfLogicalProcessors", "?"))
let ram-total = if compact {
number(get(get(summary, "memory", (:)), "total_gb", 0))
} else {
number(get(system, "total_physical_memory_gb", 0))
}
let ram-free = if compact {
number(get(get(summary, "memory", (:)), "free_gb", 0))
} else {
number(get(os, "free_memory_gb", 0))
}
(
source: source-name,
schema_version: schema,
mode: mode,
generated: generated,
computer: str(get(host, "computer_name", "Unknown")),
administrator: bool-text(administrator),
health: if critical > 0 { "Attention required" } else if warnings > 0 or unanalysed > 0 { "Review recommended" } else if incomplete { "Insufficient evidence for a complete assessment" } else { "No actionable issue detected in collected evidence" },
health_score: health-score,
collection_incomplete: incomplete,
parameters: get(report, "parameters", (:)),
diagnostic_collection: get(report, "diagnostic_collection", (:)),
source_report: report,
raw_events: raw,
reliability_events: reliability,
watchlist: watchlist-catalog,
event_counts: (critical: raw.filter(e => e.level == 1).len(), error: raw.filter(e => e.level == 2).len(), warning: raw.filter(e => e.level == 3).len(), information: raw.filter(e => e.level in (4, 5)).len(), unknown: raw.filter(e => e.level == none).len()),
analysis_confidence: if incomplete { calc.min(50, analysis-confidence) } else { analysis-confidence },
system: (
os: os-name,
version: os-version,
build: os-build,
manufacturer: manufacturer,
model: model,
cpu: cpu-name,
cores: cores,
threads: threads,
ram_total_gb: ram-total,
ram_free_gb: ram-free,
uptime: uptime-value,
),
security: (
antivirus: bool-text(get(defender, "AntivirusEnabled", "Unavailable")),
realtime: bool-text(get(defender, "RealTimeProtectionEnabled", "Unavailable")),
signatures: display-time(get(defender, "AntivirusSignatureLastUpdated", "Unavailable")),
),
battery: (
charge: battery-charge,
status: str(get(battery, "Status", "Unavailable")),
),
disk_free: disk-free,
inventory: inventory,
findings: findings,
issue_counts: (critical: critical, warning: warnings, unanalysed: unanalysed),
event_groups: events.map(event => {
event.insert("display_time", display-time(event.time))
event
}),
excerpts: excerpts,
coverage: coverage,
)
}