Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public enum GzipFile {
let output = try FileHandle(forWritingTo: destination)
defer { try? output.close() }
let inflater = try GzipInflater(budget: budget)
while let block = try input.read(upToCount: readSize), !block.isEmpty {
// Each read returns an autoreleased buffer, and this loop never suspends, so nothing drains them until it ends.
while let block = try autoreleasepool(invoking: { try input.read(upToCount: readSize) }), !block.isEmpty {
try Task.checkCancellation()
try block.withUnsafeBytes { raw in
try inflater.inflate(raw) { try output.write(contentsOf: $0) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum DownloadedBinary {

var hasher = SHA256()
do {
while let chunk = try handle.read(upToCount: hashChunkSize), !chunk.isEmpty {
// Each read returns an autoreleased buffer, and the caller's job may not drain its pool until the whole file is read.
while let chunk = try autoreleasepool(invoking: { try handle.read(upToCount: hashChunkSize) }), !chunk.isEmpty {
hasher.update(data: chunk)
}
} catch {
Expand All @@ -23,11 +24,7 @@ public enum DownloadedBinary {
return hasher.finalize().map { String(format: "%02x", $0) }.joined()
}

/// `ditto -xk` writes `com.apple.quarantine` onto every file it extracts, not just the
/// directory it extracts into, so clearing the bundle root alone leaves the Mach-O inside
/// `Contents/MacOS` still quarantined. Gatekeeper reads the flag on the file it is about
/// to map, and refuses a quarantined bundle with "library load disallowed by system
/// policy" even inside a process that has `com.apple.security.cs.disable-library-validation`.
// `ditto -xk` quarantines every extracted file, and Gatekeeper refuses to map a quarantined Mach-O even with library validation off.
public static func stripQuarantine(at url: URL) {
stripQuarantineFromSingleItem(at: url)

Expand Down
102 changes: 79 additions & 23 deletions apple/TablePro/Core/AI/CLI/AgentCLIProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Foundation
import os
import TableProProcess
import TableProUtilities

enum AgentCLIError: Error, LocalizedError {
case notInstalled(String)
Expand Down Expand Up @@ -40,6 +41,7 @@ struct AgentCLILaunch: Sendable {
}
}

// Never FileHandle.bytes: every AsyncBytes read shares one serial queue, so a quiet pipe stalls every other read.
struct AgentCLIProcess: Sendable {
private static let logger = Logger(subsystem: "com.TablePro", category: "AgentCLIProcess")
private static let terminationGracePeriod: Duration = .milliseconds(750)
Expand All @@ -56,20 +58,33 @@ struct AgentCLIProcess: Sendable {
let combined = Pipe()
process.standardOutput = combined
process.standardError = combined
// Read while the child runs: one that fills the pipe never exits.
let output = OSAllocatedUnfairLock(initialState: Data())
let reader = PipeReader(combined.fileHandleForReading)

return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
process.terminationHandler = { finished in
let data = (try? combined.fileHandleForReading.readToEnd() ?? Data()) ?? Data()
reader.stopAtEndOfFile()
let data = output.withLock { $0 }
continuation.resume(
returning: (finished.terminationStatus, String(data: data, encoding: .utf8) ?? "")
)
}
reader.start { chunk in
output.withLock { $0.append(chunk) }
}
do {
try process.run()
} catch {
process.terminationHandler = nil
reader.stop()
continuation.resume(throwing: AgentCLIError.launchFailed(error.localizedDescription))
return
}
// A cancel that landed before launch found nothing running to stop.
if Task.isCancelled {
Self.terminate(process, label: label)
}
}
} onCancel: {
Expand All @@ -92,37 +107,45 @@ struct AgentCLIProcess: Sendable {
process.standardError = stderr

let label = self.label
let stderrDrain = Task {
for try await line in stderr.fileHandleForReading.bytes.lines where !line.isEmpty {
Self.logger.error("\(label, privacy: .public) stderr: \(line, privacy: .private)")
let stdoutLines = OSAllocatedUnfairLock(initialState: LineSplitter())
let stderrLines = OSAllocatedUnfairLock(initialState: LineSplitter())
let stdoutReader = PipeReader(stdout.fileHandleForReading)
let stderrReader = PipeReader(stderr.fileHandleForReading)

continuation.onTermination = { _ in
Self.terminate(process, label: label)
stdoutReader.stop()
stderrReader.stop(drainingUpTo: DescriptorRead.pipeCapacity)
Self.logStderr(stderrLines.withLock { $0.finish() }, label: label)
}

stderrReader.start { chunk in
Self.logStderr(stderrLines.withLock { $0.append(chunk) }, label: label)
} atEnd: { _ in
Self.logStderr(stderrLines.withLock { $0.finish() }, label: label)
}
stdoutReader.start { chunk in
for line in stdoutLines.withLock({ $0.append(chunk) }) {
continuation.yield(line)
}
} atEnd: { failure in
for line in stdoutLines.withLock({ $0.finish() }) {
continuation.yield(line)
}
continuation.finish(throwing: failure)
}

do {
try process.run()
} catch {
stderrDrain.cancel()
continuation.finish(throwing: AgentCLIError.launchFailed(error.localizedDescription))
return
}

let reader = Task {
do {
for try await line in stdout.fileHandleForReading.bytes.lines {
if Task.isCancelled { break }
continuation.yield(line)
}
continuation.finish()
} catch {
continuation.finish(throwing: error)
}
}
}
}

continuation.onTermination = { _ in
reader.cancel()
stderrDrain.cancel()
Self.terminate(process, label: label)
}
private static func logStderr(_ lines: [String], label: String) {
for line in lines {
logger.error("\(label, privacy: .public) stderr: \(line, privacy: .private)")
}
}

Expand All @@ -147,3 +170,36 @@ struct AgentCLIProcess: Sendable {
}
}
}

extension AgentCLIProcess {
// LF alone ends a line: stream-json text can hold a raw U+2028, which FileHandle's `lines` also splits on.
struct LineSplitter: Sendable {
private var partial = Data()

mutating func append(_ chunk: Data) -> [String] {
var lines: [String] = []
var lineStart = chunk.startIndex
while let newline = chunk[lineStart...].firstIndex(of: 0x0A) {
partial.append(chunk[lineStart ..< newline])
if let line = Self.decode(partial) {
lines.append(line)
}
partial.removeAll(keepingCapacity: true)
lineStart = chunk.index(after: newline)
}
partial.append(chunk[lineStart...])
return lines
}

mutating func finish() -> [String] {
defer { partial = Data() }
return Self.decode(partial).map { [$0] } ?? []
}

private static func decode(_ bytes: Data) -> String? {
let line = bytes.last == 0x0D ? bytes.dropLast() : bytes
guard !line.isEmpty else { return nil }
return String(bytes: line, encoding: .utf8)
}
}
}
31 changes: 5 additions & 26 deletions apple/TablePro/Core/Diagnostics/TableLoadHistoryStore.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import Foundation
import os

/// Where a finished trace goes. The one requirement is that the producer is a synchronous
/// `@MainActor` method, so this cannot be `async`: the call has to return before any disk work
/// happens or the measurement pays for its own recording.
// Not async: the producer is a synchronous main-actor method that must return before any disk work, or the measurement pays for its own recording.
internal protocol TableLoadSummarySink: Sendable {
func record(_ record: TableLoadPerformanceRecord)
}

/// A bounded, device-local history of how long table loads took, kept so an intermittent slowdown
/// can be looked at after it happened rather than only while `log stream` is running.
///
/// Newline-delimited JSON rather than the single re-encoded array `ExecutionAuditLog` uses. Two
/// reasons, both about the caps this store has and that one does not: rewriting the whole array costs
/// the size of the store on every append, which at the ten thousand records allowed here is megabytes
/// written per table click; and a write torn by a crash takes the entire array with it, where a torn
/// final line costs one record. Nothing is held in memory between appends either, because ten
/// thousand decoded records is a lot of residency for a file the running app never reads.
// Newline-delimited JSON: the retention pass rewrites the file, and a write torn by a crash costs one line, not the file.
internal actor TableLoadHistoryStore: TableLoadSummarySink {
internal static let shared = TableLoadHistoryStore()

Expand All @@ -28,8 +18,7 @@ internal actor TableLoadHistoryStore: TableLoadSummarySink {
private static let newline = UInt8(ascii: "\n")
private static let logger = Logger(subsystem: "com.TablePro", category: "TableLoadHistory")

/// Fractional seconds, because a store that measures milliseconds and stamps whole seconds
/// cannot order a burst of navigation inside one, and the retention pass sorts on this field.
// Fractional seconds: the retention pass sorts on this field, and whole seconds cannot order a burst of navigation.
private static let timestamps = Date.ISO8601FormatStyle(includingFractionalSeconds: true)

private let fileURL: URL
Expand All @@ -42,8 +31,6 @@ internal actor TableLoadHistoryStore: TableLoadSummarySink {
self.fileURL = fileURL
}

/// Resolved through `AppStorageEnvironment` like every other store, so a UI test writes into its
/// own sandbox rather than the history of the person running it.
internal static func defaultFileURL() -> URL {
let directory = AppStorageEnvironment.shared.supportDirectory
try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
Expand All @@ -62,8 +49,7 @@ internal actor TableLoadHistoryStore: TableLoadSummarySink {
pruneIfNeeded()
}

/// The first touch in a process reconciles the file against the retention rules, so a history
/// left behind by a build that quit before pruning cannot grow past its caps.
// A history left by a build that quit before pruning must not grow past the caps.
private func loadIfNeeded() {
guard !loaded else { return }
loaded = true
Expand All @@ -77,14 +63,7 @@ internal actor TableLoadHistoryStore: TableLoadSummarySink {
prune()
}

/// An undecodable line is dropped rather than refused. A history whose tail was torn by a crash
/// has to keep working, because it is diagnostic data and refusing to read it would make the
/// store itself the outage. The count is logged rather than swallowed, so a schema change that
/// makes an older record unreadable shows up as a number instead of a file that quietly shrank.
///
/// The counts are set from the file as read before the rewrite, so a write that fails leaves the
/// caps reflecting what is still on disk. Left at zero they would read as under capacity and the
/// only retry would be the hourly one, which is an hour of appending with no bound enforced.
// A torn line is dropped and counted; the caps come from the file as read, so a failed rewrite leaves them true.
private func prune() {
lastPrunedAt = Date()

Expand Down
37 changes: 20 additions & 17 deletions apple/TablePro/Core/LSP/LSPTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ actor LSPTransport {
private var writer: LSPMessageWriter?
private var stdoutPipe: Pipe?
private var stderrPipe: Pipe?
private var stdoutReader: PipeReader?
private var stderrReader: PipeReader?
private var nextRequestID: Int = 1
private var pendingRequests: [Int: CheckedContinuation<Data, Error>] = [:]
Expand Down Expand Up @@ -128,9 +129,15 @@ actor LSPTransport {

try proc.run()

let handle = stdout.fileHandleForReading
// Not FileHandle.bytes: its reads share one queue, so an idle server would stall every other AsyncBytes read.
let (chunks, chunkSink) = AsyncStream.makeStream(of: Data.self)
let stdoutReader = PipeReader(stdout.fileHandleForReading)
stdoutReader.start { chunkSink.yield($0) } atEnd: { _ in
chunkSink.finish()
}
self.stdoutReader = stdoutReader
readerTask = Task { [weak self] in
await self?.runReadLoop(handle: handle)
await self?.runReadLoop(chunks)
}

Self.logger.info("LSP transport started: \(executablePath)")
Expand All @@ -148,6 +155,8 @@ actor LSPTransport {

writer?.close()
writer = nil
stdoutReader?.stop()
stdoutReader = nil
if let stdoutHandle = stdoutPipe?.fileHandleForReading {
try? stdoutHandle.close()
}
Expand Down Expand Up @@ -270,25 +279,19 @@ actor LSPTransport {
continuation.resume(throwing: error)
}

private func runReadLoop(handle: FileHandle) async {
private func runReadLoop(_ chunks: AsyncStream<Data>) async {
var buffer = Data()
do {
for try await byte in handle.bytes {
if Task.isCancelled { return }
buffer.append(byte)
while let (messageData, _) = Self.parseMessageFromBuffer(&buffer) {
dispatchMessage(messageData)
}
}
} catch {
if !Task.isCancelled {
Self.logger.debug("LSP read loop ended: \(error.localizedDescription)")
for await chunk in chunks {
// A stream ended by cancellation still hands over the chunks it buffered.
if Task.isCancelled { return }
buffer.append(chunk)
while let (messageData, _) = Self.parseMessageFromBuffer(&buffer) {
dispatchMessage(messageData)
}
}
}

/// Parse a single LSP message from the buffer.
/// Returns (messageBody, totalBytesConsumed) or nil if buffer is incomplete.
// Nil until the buffer holds a whole message; the Int is the bytes it consumed.
private static func parseMessageFromBuffer(_ buffer: inout Data) -> (Data, Int)? {
let separator: [UInt8] = [0x0D, 0x0A, 0x0D, 0x0A]
guard let separatorRange = buffer.range(of: Data(separator)) else {
Expand Down Expand Up @@ -363,7 +366,7 @@ actor LSPTransport {
if let handler = notificationHandlers[method] {
handler(data)
}
// Server-initiated request (has both id and method) — reply with handler result or null
// Server-initiated request (has both id and method): reply with the handler's result or null
if let id {
if let deferred = deferredRequestHandlers[method] {
deferred(data, id)
Expand Down
Loading
Loading