From aaf13e5588d6b2e13b63fbe62750fcd9940b259e Mon Sep 17 00:00:00 2001 From: taekchef Date: Tue, 14 Jul 2026 22:53:05 +0800 Subject: [PATCH 1/2] fix: preserve Pi startup cancellation --- Sources/WeiBeiCore/PiAgentRuntime.swift | 31 +++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Sources/WeiBeiCore/PiAgentRuntime.swift b/Sources/WeiBeiCore/PiAgentRuntime.swift index 635954d2f..ba3c51733 100644 --- a/Sources/WeiBeiCore/PiAgentRuntime.swift +++ b/Sources/WeiBeiCore/PiAgentRuntime.swift @@ -302,6 +302,8 @@ public actor PiAgentRuntime: StudyAgentRuntime { private var stderrTask: Task? private var pendingCommands: [String: PendingCommand] = [:] private var activeRun: ActiveRun? + private var startingRunID: UUID? + private var cancelledStartingRunIDs: Set = [] private var stderrBuffer = "" private var startupFailure: PiAgentRuntimeError? private var idleShutdownTask: Task? @@ -327,12 +329,21 @@ public actor PiAgentRuntime: StudyAgentRuntime { } public func respond(to request: StudyAgentRequest, progress: StudyAgentProgressHandler?) async throws -> StudyAgentReply { - guard activeRun == nil else { throw PiAgentRuntimeError.busy } + guard activeRun == nil, startingRunID == nil else { throw PiAgentRuntimeError.busy } + startingRunID = request.id + defer { + if startingRunID == request.id { + startingRunID = nil + } + cancelledStartingRunIDs.remove(request.id) + } idleShutdownTask?.cancel() idleShutdownTask = nil try await ensureProcess() + try requireStartingRun(request.id) _ = try await sendCommand(type: "new_session", timeoutSeconds: 3) + try requireStartingRun(request.id) let context = StudyAgentContextEnvelope(request: request) try writeContext(context) await progress?(.readingContext) @@ -359,8 +370,8 @@ public actor PiAgentRuntime: StudyAgentRuntime { allowedNoteSourceLabels: currentSourceLabels(in: context), progress: progress ) + startingRunID = nil refreshRunWatchdog() - do { _ = try await sendCommand( type: "prompt", @@ -378,8 +389,20 @@ public actor PiAgentRuntime: StudyAgentRuntime { } public func cancel() async { - guard let runID = activeRun?.id else { return } - await cancelRun(id: runID) + if let runID = activeRun?.id { + await cancelRun(id: runID) + return + } + guard let runID = startingRunID else { return } + cancelledStartingRunIDs.insert(runID) + startingRunID = nil + shutdownProcess(reason: PiAgentRuntimeError.cancelled) + } + + private func requireStartingRun(_ runID: UUID) throws { + guard startingRunID == runID, !cancelledStartingRunIDs.contains(runID) else { + throw PiAgentRuntimeError.cancelled + } } private func cancelRun(id runID: UUID) async { From d032feae178c06e488188a382a531784e637a931 Mon Sep 17 00:00:00 2001 From: taekchef Date: Tue, 14 Jul 2026 23:05:23 +0800 Subject: [PATCH 2/2] fix: extend Pi startup readiness window --- Sources/WeiBeiCore/PiAgentRuntime.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/WeiBeiCore/PiAgentRuntime.swift b/Sources/WeiBeiCore/PiAgentRuntime.swift index ba3c51733..184d60c77 100644 --- a/Sources/WeiBeiCore/PiAgentRuntime.swift +++ b/Sources/WeiBeiCore/PiAgentRuntime.swift @@ -262,6 +262,8 @@ public enum PiAgentDiagnosticSanitizer { } public actor PiAgentRuntime: StudyAgentRuntime { + private static let processReadinessTimeoutSeconds: UInt64 = 12 + private struct PendingCommand { var continuation: CheckedContinuation var timeoutTask: Task @@ -487,11 +489,17 @@ public actor PiAgentRuntime: StudyAgentRuntime { stderrTask = readStderr(errorPipe.fileHandleForReading) do { - let state = try await sendCommand(type: "get_state", timeoutSeconds: 3) + let state = try await sendCommand( + type: "get_state", + timeoutSeconds: Self.processReadinessTimeoutSeconds + ) guard state.dataJSON != nil else { throw PiAgentRuntimeError.protocolFailure("get_state returned no data") } - let commands = try await sendCommand(type: "get_commands", timeoutSeconds: 3) + let commands = try await sendCommand( + type: "get_commands", + timeoutSeconds: Self.processReadinessTimeoutSeconds + ) try verifyRequiredSkills(in: commands) if let startupFailure { throw startupFailure } } catch {