import Foundation struct CheckSection: Identifiable, Equatable, Sendable { let id: Int let name: String let text: String let failed: Bool var isSummary = false static func split(_ log: StepLog, step: ThinStep) -> [Self] { let tail = log.boundedTail let failed = step.checks == "failed" || (step.exitCode.map { $0 != 0 } ?? false) let hosted = step.gate == "hosted CI" guard step.gate == ".amadan/ci.d" || hosted else { return [Self(id: 0, name: "", text: tail, failed: failed)] } let source = tail.components(separatedBy: "\n") // Older devices receive no hosted field. Only then can the header // supply the allowlist; a present empty field is authoritative. let headerNames = source.prefix { !$0.hasPrefix("=== ") }.first { $0.hasPrefix("Failed: ") } .map { String($0.dropFirst(8)).components(separatedBy: "; ") } ?? [] let names = Set((step.hosted.map { $0.failed ?? [] } ?? headerNames).map(hostedName)) var chunks: [(name: String, lines: [String])] = [] var name = "", lines: [String] = [] for line in source { let rawName = String(line.dropFirst(4)) let candidate = hosted ? hostedName(rawName) : rawName let basename = candidate.range(of: #"^[A-Za-z0-9_@+-][A-Za-z0-9._@+-]*$"#, options: .regularExpression) != nil && !["RUN", "PAUSE", "CONT", "NAME"].contains(candidate) let marker = line.hasPrefix("=== ") && (hosted ? names.contains(candidate) : basename) if marker { if !name.isEmpty || !lines.isEmpty { chunks.append((name, lines)) } name = candidate; lines = [line] } else { lines.append(line) } } if !name.isEmpty || !lines.isEmpty { chunks.append((name, lines)) } var sections = chunks.enumerated().map { index, chunk in Self(id: index, name: chunk.name, text: chunk.lines.joined(separator: "\n"), failed: failed && (hosted ? !chunk.name.isEmpty : index == chunks.count - 1), isSummary: hosted && chunk.name.isEmpty && chunk.lines.first?.hasPrefix("Hosted CI on ") == true) } // Local ci.d stops at the first failure. This is a hint: tool output // can imitate a basename marker. Grouping never removes any log line. if failed, !hosted, let last = sections.popLast() { sections.insert(last, at: 0) } return sections } // runner/thin/judge_hosted.go: strings.Fields, then clipName, in UTF-8 bytes. private static func hostedName(_ name: String) -> String { let collapsed = name.unicodeScalars.split(whereSeparator: { $0.properties.isWhitespace }).map(String.init).joined(separator: " ") var result = "" for scalar in collapsed.unicodeScalars { guard result.utf8.count + scalar.utf8.count <= 120 else { break } result.unicodeScalars.append(scalar) } return result } }