Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
72fa49e
docs(plans): machine connection hardening epic
claude Nov 29, 2026
8c2b215
docs(machines): specify connection hardening (MACH-013 – MACH-019)
claude Nov 29, 2026
28d0407
feat(core): classify ssh failures by cause (MACH-018)
claude Nov 29, 2026
587c385
Windows: gate remote retries on machine state; classify ssh failures
claude Nov 29, 2026
3f4149c
feat(cli): pu doctor --machine, ssh connect options and classified er…
claude Nov 29, 2026
1b38cc4
feat(transport): multiplexed SSH relay, `pu daemon proxy --mux` (MACH…
claude Nov 29, 2026
b2ff663
docs: reliable machine connections guide and pu doctor (MACH-017 – MA…
claude Nov 29, 2026
95dd332
Merge branch 'worktree-agent-af91f6f8db487f3a5' into claude/cool-tesl…
claude Nov 29, 2026
5dc0bc5
feat(apple): harden machine connections (MACH-014 – MACH-018)
claude Nov 29, 2026
5d70ce4
Merge branch 'worktree-agent-ab43a12adbbc73bb5' into claude/cool-tesl…
claude Nov 29, 2026
34874b7
Windows: event-driven reconnects, jittered backoff and a connection log
claude Nov 29, 2026
8cbe398
Merge branch 'worktree-agent-a56ac00d116fb718e' into claude/cool-tesl…
claude Nov 29, 2026
666df03
feat(win-app): one multiplexed ssh.exe per machine (MACH-013, WINAPP-…
claude Nov 29, 2026
a455c9f
fix(machines): address review of connection hardening
claude Nov 29, 2026
c3b79ff
fix(transport): read buffered pongs before a ping deadline ends the m…
2witstudios Nov 30, 2026
5758220
fix(doctor): keep a ufw limit rule's restrictions in the suggested fix
2witstudios Nov 30, 2026
2605216
fix(win-app): keep a machine's gate open until disconnect's layout fl…
2witstudios Nov 30, 2026
91e0e14
fix(win-app): key a machine's mux session by its SSH host and drop st…
2witstudios Nov 30, 2026
3d3e003
fix(win-app): reset a remote grid stream's retries when it subscribed…
2witstudios Nov 30, 2026
98672db
test(win-e2e): let the fake ssh run the multiplexed relay
2witstudios Nov 30, 2026
be58bef
fix(doctor): compile the ufw script test only on unix, and format
2witstudios Nov 30, 2026
e553754
fix(doctor): suggest `ufw allow <port>` only when it matches the limi…
2witstudios Nov 30, 2026
11f21f0
fix(transport): give the reader grace periods before an expired ping …
2witstudios Nov 30, 2026
20a64a9
fix(win-app): check the slot's generation when a waiter takes a mux o…
2witstudios Nov 30, 2026
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Run `pu --help` for full usage.
| [Templates & Definitions](https://purepoint.dev/docs/guide/workflows/templates-and-definitions) | Prompts, agent defs, swarm defs |
| [Scheduling & Triggers](https://purepoint.dev/docs/guide/workflows/scheduling-and-triggers) | Schedules, triggers, git hooks |
| [Linux Server](https://purepoint.dev/docs/guide/linux-server) | Run the daemon on a headless Linux server |
| [Reliable Machine Connections](docs/guide/reliable-machine-connections.md) | Stable addresses, sshd and firewall settings, `pu doctor --machine` |
| [Windows](docs/guide/windows.md) | Install, daemon autostart, agents and troubleshooting on Windows |
| [Troubleshooting](https://purepoint.dev/docs/guide/troubleshooting) | Common issues and solutions |
| [Contributing](https://purepoint.dev/docs/contributing/overview) | Build, test, code style, architecture |
Expand Down
4 changes: 1 addition & 3 deletions apps/purepoint-macos/purepoint-macos/Models/ProjectKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ nonisolated struct DaemonEndpoint: Hashable, Sendable {
nonisolated(unsafe) private static var installedProvider: TransportProvider?
}

#if os(iOS)
nonisolated extension DaemonEndpoint {
/// The endpoint a socket path names: a machine's when it is `MachinePaths.socketPath` for
/// that machine, otherwise the (unreachable) local one.
/// that machine, otherwise the local one (unreachable on iOS).
init(socketPath: String) {
let parts = socketPath.split(separator: "/")
if parts.count >= 2, parts.last == "daemon.sock", let id = UUID(uuidString: String(parts[parts.count - 2])),
Expand All @@ -116,7 +115,6 @@ nonisolated extension DaemonEndpoint {
}
}
}
#endif

/// Filesystem locations shared with the CLI (`crates/pu-core/src/paths.rs`).
nonisolated enum MachinePaths {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ actor DaemonAttachSession {
/// when the agent's process exits. Callers must not blindly reattach: each
/// attach replays the agent's whole output buffer.
private(set) var didStreamEnd = false
/// Attaches the daemon has confirmed (`attach_ready`); each one resets the retry count.
private var successfulAttaches = 0
private var onFirstOutput: (() -> Void)?
/// Called on the main actor after each chunk of *live* output is fed to the terminal —
/// never for the replay of the daemon's buffer that every attach starts with.
Expand Down Expand Up @@ -51,13 +53,11 @@ actor DaemonAttachSession {
func start() async {
guard !stopped else { return }

let fastRetries = 5
let fastDelay: UInt64 = 100_000_000 // 100ms
let slowDelay: UInt64 = 2_000_000_000 // 2s
var retries = 0
let maxRetries = 20
var policy = AttachRetryPolicy()
let machineId = client.machineId

while !stopped {
attaching: while !stopped {
let attachedBefore = successfulAttaches
do {
try await runAttachLoop()
// Normal exit (agent completed) — don't reconnect
Expand All @@ -79,24 +79,46 @@ actor DaemonAttachSession {
// without spending retries while it is in the background.
if await AppActivity.shared.isInBackground {
await AppActivity.shared.untilForeground()
retries = 0
policy.reset()
continue
}
#endif
// Connection lost — attempt reconnect with backoff
retries += 1
print("[DaemonAttach \(agentId.prefix(8))] retry \(retries): \(error.localizedDescription)")
guard !stopped, retries <= maxRetries else { break }
let delay = retries <= fastRetries ? fastDelay : slowDelay
do {
try await Task.sleep(nanoseconds: delay)
} catch {
break // CancellationError — exit immediately
let decision = policy.recordFailure(
attached: successfulAttaches != attachedBefore,
machineIsDown: MachineConnectivity.shared.shouldWait(forMachine: machineId))
switch decision {
case .waitForMachine:
// The machine's connection is down: wait for it however long it takes, then
// reattach; the daemon replays the buffer, so nothing is lost (MACH-015).
print("[DaemonAttach \(agentId.prefix(8))] waiting for the machine: \(error.localizedDescription)")
guard let machineId, await waitForMachine(machineId) else { break attaching }
case .retry(let delay):
// Connection lost — attempt reconnect with backoff
print("[DaemonAttach \(agentId.prefix(8))] retry \(policy.retries): \(error.localizedDescription)")
guard !stopped else { break attaching }
do {
try await Task.sleep(for: delay)
} catch {
break attaching // CancellationError — exit immediately
}
case .giveUp:
break attaching
}
}
}
}

/// Wait until the machine reports connected, then a random 0–1 s so its panes do not all
/// reattach at once (MACH-014). False when stopped or cancelled meanwhile.
private func waitForMachine(_ machineId: UUID) async -> Bool {
while !stopped, !MachineConnectivity.shared.isConnected(machineId) {
do { try await Task.sleep(for: MachineConnectivity.pollInterval) } catch { return false }
}
guard !stopped else { return false }
do { try await Task.sleep(for: MachineConnectivity.resumeDelay()) } catch { return false }
return !stopped
}

/// Send terminal input to the daemon. Awaiting ensures actor serializes writes.
func sendInput(_ data: Data) async {
guard let conn = connection else { return }
Expand Down Expand Up @@ -184,6 +206,7 @@ actor DaemonAttachSession {
throw DaemonAttachError.unexpectedResponse
}
print("[DaemonAttach \(agentId.prefix(8))] attached successfully")
successfulAttaches += 1

let focused = await MainActor.run {
guard let tv else { return false }
Expand Down Expand Up @@ -337,6 +360,41 @@ actor DaemonAttachSession {
}
}

/// What an attach does after its connection failed (MACH-015). The retry count resets after
/// every successful attach, so a pane that keeps reattaching never runs out. A remote pane
/// whose machine is down waits for the machine instead of spending retries, however long the
/// outage lasts. A pane that cannot attach while its daemon is reachable still gives up after
/// `maxRetries`.
nonisolated struct AttachRetryPolicy: Sendable {
static let fastRetries = 5
static let maxRetries = 20
static let fastDelay: Duration = .milliseconds(100)
static let slowDelay: Duration = .seconds(2)

enum Decision: Equatable, Sendable {
case retry(after: Duration)
case waitForMachine
case giveUp
}

private(set) var retries = 0

/// `attached`: the failed run attached before it failed. `machineIsDown`: the pane is
/// remote and its machine does not report connected.
mutating func recordFailure(attached: Bool, machineIsDown: Bool) -> Decision {
if attached { retries = 0 }
if machineIsDown { return .waitForMachine }
retries += 1
guard retries <= Self.maxRetries else { return .giveUp }
return .retry(after: retries <= Self.fastRetries ? Self.fastDelay : Self.slowDelay)
}

/// Start counting again (the iPad app came back from the background).
mutating func reset() {
retries = 0
}
}

enum DaemonAttachError: Error, LocalizedError {
case attachFailed(String)
case unexpectedResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ nonisolated final class DaemonClient: @unchecked Sendable {
#endif
}

/// The machine this client's daemon runs on, nil for the local daemon.
var machineId: UUID? { DaemonEndpoint(socketPath: socketPath).machineId }

/// Send a single request and return the response.
/// Cancellation-aware: cancelling the task aborts a pending connect and closes the
/// stream, which completes any pending receive with an error instead of leaving the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ actor DaemonGridSubscription {
continue
}
#endif
// A remote machine that is down: only its supervisor reconnects. Resubscribe
// once it is back, without spending retries (MACH-014).
if let machineId = client.machineId, MachineConnectivity.shared.shouldWait(forMachine: machineId) {
guard await waitForMachine(machineId) else { break }
retries = 0
backoff = 500_000_000
continue
}
retries += 1
guard !stopped, retries <= maxRetries else { break }
do {
Expand Down Expand Up @@ -74,6 +82,17 @@ actor DaemonGridSubscription {

// MARK: - Private

/// Wait until the machine reports connected, then a random 0–1 s so its streams do not
/// all resubscribe at once (MACH-014). False when stopped or cancelled meanwhile.
private func waitForMachine(_ machineId: UUID) async -> Bool {
while !stopped, !MachineConnectivity.shared.isConnected(machineId) {
do { try await Task.sleep(for: MachineConnectivity.pollInterval) } catch { return false }
}
guard !stopped else { return false }
do { try await Task.sleep(for: MachineConnectivity.resumeDelay()) } catch { return false }
return !stopped
}

private func runSubscriptionLoop() async throws {
let reg = self.registry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ actor DaemonProjectsSubscription {
continue
}
#endif
// A remote machine that is down: only its supervisor reconnects. Resubscribe
// once it is back (MACH-014).
if let machineId = client.machineId, MachineConnectivity.shared.shouldWait(forMachine: machineId) {
guard await waitForMachine(machineId) else { return }
backoff = Self.initialBackoff
continue
}
}
guard !stopped else { return }
do { try await Task.sleep(nanoseconds: backoff) } catch { return }
Expand All @@ -47,6 +54,17 @@ actor DaemonProjectsSubscription {
connection = nil
}

/// Wait until the machine reports connected, then a random 0–1 s so its streams do not
/// all resubscribe at once (MACH-014). False when stopped or cancelled meanwhile.
private func waitForMachine(_ machineId: UUID) async -> Bool {
while !stopped, !MachineConnectivity.shared.isConnected(machineId) {
do { try await Task.sleep(for: MachineConnectivity.pollInterval) } catch { return false }
}
guard !stopped else { return false }
do { try await Task.sleep(for: MachineConnectivity.resumeDelay()) } catch { return false }
return !stopped
}

private func run(onChange: @escaping @MainActor ([String]) -> Void) async throws {
let (conn, reader) = try await client.connect()
// Stopped while connecting: nothing else would ever close this connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ actor DaemonStatusSubscription {
continue
}
#endif
// A remote machine that is down: only its supervisor reconnects. Resubscribe
// once it is back, instead of retrying or declaring its daemon lost (MACH-014).
if let machineId = client.machineId, MachineConnectivity.shared.shouldWait(forMachine: machineId) {
guard await waitForMachine(machineId) else { break }
retries = 0
backoff = 500_000_000
continue
}
retries += 1
guard !stopped, retries <= maxRetries else { break }
if subscribed || retries == Self.retriesBeforeDaemonLost, let onDaemonLost {
Expand All @@ -82,6 +90,17 @@ actor DaemonStatusSubscription {

// MARK: - Private

/// Wait until the machine reports connected, then a random 0–1 s so its streams do not
/// all resubscribe at once (MACH-014). False when stopped or cancelled meanwhile.
private func waitForMachine(_ machineId: UUID) async -> Bool {
while !stopped, !MachineConnectivity.shared.isConnected(machineId) {
do { try await Task.sleep(for: MachineConnectivity.pollInterval) } catch { return false }
}
guard !stopped else { return false }
do { try await Task.sleep(for: MachineConnectivity.resumeDelay()) } catch { return false }
return !stopped
}

private func runSubscriptionLoop(
onEvent: @escaping @MainActor ([WorktreeModel], [AgentModel]) -> Void
) async throws {
Expand Down
Loading
Loading