Skip to content

Commit 2e17b85

Browse files
committed
fix(channels): refresh ingress retry repair onto fixed main
2 parents 1b542ff + e2e5d1b commit 2e17b85

54 files changed

Lines changed: 2630 additions & 347 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1878,18 +1878,22 @@ public struct GatewaySuspendBlocker: Codable, Sendable {
18781878
public struct GatewaySuspendPrepareParams: Codable, Sendable {
18791879
public let requestid: String
18801880
public let terminalpolicy: AnyCodable?
1881+
public let drain: Bool?
18811882

18821883
public init(
18831884
requestid: String,
1884-
terminalpolicy: AnyCodable? = nil)
1885+
terminalpolicy: AnyCodable? = nil,
1886+
drain: Bool? = nil)
18851887
{
18861888
self.requestid = requestid
18871889
self.terminalpolicy = terminalpolicy
1890+
self.drain = drain
18881891
}
18891892

18901893
private enum CodingKeys: String, CodingKey {
18911894
case requestid = "requestId"
18921895
case terminalpolicy = "terminalPolicy"
1896+
case drain
18931897
}
18941898
}
18951899

@@ -1923,6 +1927,40 @@ public struct GatewaySuspendPrepareBusyResult: Codable, Sendable {
19231927
}
19241928
}
19251929

1930+
public struct GatewaySuspendPrepareDrainingResult: Codable, Sendable {
1931+
public let status: String
1932+
public let suspensionid: String
1933+
public let expiresatms: Int
1934+
public let retryafterms: Int
1935+
public let activecount: Int
1936+
public let blockers: [GatewaySuspendBlocker]
1937+
1938+
public init(
1939+
status: String,
1940+
suspensionid: String,
1941+
expiresatms: Int,
1942+
retryafterms: Int,
1943+
activecount: Int,
1944+
blockers: [GatewaySuspendBlocker])
1945+
{
1946+
self.status = status
1947+
self.suspensionid = suspensionid
1948+
self.expiresatms = expiresatms
1949+
self.retryafterms = retryafterms
1950+
self.activecount = activecount
1951+
self.blockers = blockers
1952+
}
1953+
1954+
private enum CodingKeys: String, CodingKey {
1955+
case status
1956+
case suspensionid = "suspensionId"
1957+
case expiresatms = "expiresAtMs"
1958+
case retryafterms = "retryAfterMs"
1959+
case activecount = "activeCount"
1960+
case blockers
1961+
}
1962+
}
1963+
19261964
public struct GatewaySuspendPrepareReadyResult: Codable, Sendable {
19271965
public let status: String
19281966
public let suspensionid: String
@@ -1977,6 +2015,36 @@ public struct GatewaySuspendStatusRunningResult: Codable, Sendable {
19772015
}
19782016
}
19792017

2018+
public struct GatewaySuspendStatusDrainingResult: Codable, Sendable {
2019+
public let status: String
2020+
public let expiresatms: Int
2021+
public let retryafterms: Int
2022+
public let activecount: Int
2023+
public let blockers: [GatewaySuspendBlocker]
2024+
2025+
public init(
2026+
status: String,
2027+
expiresatms: Int,
2028+
retryafterms: Int,
2029+
activecount: Int,
2030+
blockers: [GatewaySuspendBlocker])
2031+
{
2032+
self.status = status
2033+
self.expiresatms = expiresatms
2034+
self.retryafterms = retryafterms
2035+
self.activecount = activecount
2036+
self.blockers = blockers
2037+
}
2038+
2039+
private enum CodingKeys: String, CodingKey {
2040+
case status
2041+
case expiresatms = "expiresAtMs"
2042+
case retryafterms = "retryAfterMs"
2043+
case activecount = "activeCount"
2044+
case blockers
2045+
}
2046+
}
2047+
19802048
public struct GatewaySuspendStatusReadyResult: Codable, Sendable {
19812049
public let status: String
19822050
public let expiresatms: Int
@@ -20939,6 +21007,7 @@ public enum GatewayErrorDetails: Codable, Sendable {
2093921007

2094021008
public enum GatewaySuspendPrepareResult: Codable, Sendable {
2094121009
case busy(GatewaySuspendPrepareBusyResult)
21010+
case draining(GatewaySuspendPrepareDrainingResult)
2094221011
case ready(GatewaySuspendPrepareReadyResult)
2094321012

2094421013
private enum CodingKeys: String, CodingKey {
@@ -20950,6 +21019,7 @@ public enum GatewaySuspendPrepareResult: Codable, Sendable {
2095021019
let discriminator = try container.decode(String.self, forKey: .discriminator)
2095121020
switch discriminator {
2095221021
case "busy": self = try .busy(GatewaySuspendPrepareBusyResult(from: decoder))
21022+
case "draining": self = try .draining(GatewaySuspendPrepareDrainingResult(from: decoder))
2095321023
case "ready": self = try .ready(GatewaySuspendPrepareReadyResult(from: decoder))
2095421024
default:
2095521025
throw DecodingError.dataCorruptedError(
@@ -20963,13 +21033,15 @@ public enum GatewaySuspendPrepareResult: Codable, Sendable {
2096321033
public func encode(to encoder: Encoder) throws {
2096421034
switch self {
2096521035
case .busy(let value): try value.encode(to: encoder)
21036+
case .draining(let value): try value.encode(to: encoder)
2096621037
case .ready(let value): try value.encode(to: encoder)
2096721038
}
2096821039
}
2096921040
}
2097021041

2097121042
public enum GatewaySuspendStatusResult: Codable, Sendable {
2097221043
case running(GatewaySuspendStatusRunningResult)
21044+
case draining(GatewaySuspendStatusDrainingResult)
2097321045
case ready(GatewaySuspendStatusReadyResult)
2097421046

2097521047
private enum CodingKeys: String, CodingKey {
@@ -20981,6 +21053,7 @@ public enum GatewaySuspendStatusResult: Codable, Sendable {
2098121053
let discriminator = try container.decode(String.self, forKey: .discriminator)
2098221054
switch discriminator {
2098321055
case "running": self = try .running(GatewaySuspendStatusRunningResult(from: decoder))
21056+
case "draining": self = try .draining(GatewaySuspendStatusDrainingResult(from: decoder))
2098421057
case "ready": self = try .ready(GatewaySuspendStatusReadyResult(from: decoder))
2098521058
default:
2098621059
throw DecodingError.dataCorruptedError(
@@ -20994,6 +21067,7 @@ public enum GatewaySuspendStatusResult: Codable, Sendable {
2099421067
public func encode(to encoder: Encoder) throws {
2099521068
switch self {
2099621069
case .running(let value): try value.encode(to: encoder)
21070+
case .draining(let value): try value.encode(to: encoder)
2099721071
case .ready(let value): try value.encode(to: encoder)
2099821072
}
2099921073
}

‎docs/gateway/external-apps.md‎

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,38 +60,48 @@ host-neutral suspension handshake:
6060

6161
1. Stop admitting external ingress controlled by the host.
6262
2. Call `gateway.suspend.prepare` with a stable, unique `requestId`.
63-
3. If the response is `busy`, keep the process running and retry later.
64-
4. If it is `ready`, save the returned `suspensionId`, then freeze or snapshot
65-
the process before `expiresAtMs`.
63+
3. If the response is `busy`, keep the process running and retry later. To hold
64+
admission closed while already-admitted work finishes, request the optional
65+
preserve-only drain mode and poll `gateway.suspend.status` instead.
66+
4. If the response is `ready`, save the returned `suspensionId`, then freeze or
67+
snapshot the process before `expiresAtMs`.
6668
5. After thaw, or if suspension is abandoned, call `gateway.suspend.resume`
6769
with that `suspensionId` over the existing or a newly authenticated
6870
WebSocket. The CLI equivalents are `openclaw gateway suspend` and
6971
`openclaw gateway resume <suspensionId>`.
7072

71-
A prepared Gateway accepts authenticated WebSocket connects, but fences every
72-
method except `gateway.suspend.*` and one exact predecessor-bound restart. That
73-
exception requires a non-safe `gateway.restart.request` whose `target` matches
74-
the live Gateway lock; safe and untargeted restart requests remain fenced.
75-
Controllers may reconnect after thaw and call resume. The
73+
A draining or prepared Gateway accepts authenticated operator WebSocket
74+
connections, allowing a controller to reconnect and check, renew, or release
75+
its own lease. New node and worker connections remain fenced. A prepared
76+
Gateway fences every method except `gateway.suspend.*` and one exact
77+
predecessor-bound restart. That exception requires a non-safe
78+
`gateway.restart.request` whose `target` matches the live Gateway lock; safe and
79+
untargeted restart requests remain fenced. No restart exception is available
80+
while the Gateway is still draining. Controllers may reconnect after thaw and
81+
call resume. The
7682
[Admin HTTP RPC plugin](/plugins/admin-http-rpc) remains available for hosts
7783
that cannot speak WebSocket at all. If every control path is lost, the
7884
two-minute lease expiry reopens admission automatically.
7985

8086
The RPC contract is:
8187

8288
- `gateway.suspend.prepare` — `operator.admin`; params
83-
`{ "requestId": "stable-host-operation-id", "terminalPolicy": "preserve" }`
89+
`{ "requestId": "stable-host-operation-id", "terminalPolicy": "preserve", "drain": true }`
8490
- `gateway.suspend.status` — `operator.read`; params
8591
`{ "suspensionId": "id-from-prepare" }`
8692
- `gateway.suspend.resume` — `operator.admin`; params
8793
`{ "suspensionId": "id-from-prepare" }`
8894

89-
`terminalPolicy` is optional and accepts only `"preserve"` or `"terminate"`.
90-
Omitting it defaults to `"preserve"`, so open terminal sessions block normal
91-
host suspension. A caller preparing an update that will terminate the Gateway
92-
may explicitly use `"terminate"`; this ignores open process-local terminal
93-
sessions only. Terminal persistence activity and all other tracked work still
94-
block preparation.
95+
`terminalPolicy` and `drain` are optional. `terminalPolicy` accepts only
96+
`"preserve"` or `"terminate"` and defaults to `"preserve"`; `drain` defaults
97+
to `false`. With `drain: false` or no `drain` field, request handling and
98+
response shapes are unchanged: open terminal sessions block normal host
99+
suspension. A caller preparing an update that will terminate the Gateway may
100+
explicitly use `"terminate"`; this ignores open process-local terminal sessions
101+
only. Terminal persistence activity and all other tracked work still block
102+
preparation. Drain mode always preserves terminals: combining `drain: true`
103+
with `terminalPolicy: "terminate"` returns `INVALID_REQUEST` without acquiring
104+
a lease.
95105

96106
IDs are trimmed, must contain a non-whitespace character, and are limited to
97107
128 characters. A busy prepare result has `status: "busy"`, `reason`,
@@ -107,36 +117,91 @@ IDs are trimmed, must contain a non-whitespace character, and are limited to
107117
}
108118
```
109119

110-
Status returns `{"status":"running"}` or a ready result with `expiresAtMs`.
120+
If `drain: true` finds active work, preparation acquires a renewable lease,
121+
pauses new automatic cron scheduling, closes admission to unrelated new work,
122+
and returns:
123+
124+
```json
125+
{
126+
"status": "draining",
127+
"suspensionId": "2c3f...",
128+
"expiresAtMs": 1770000000000,
129+
"retryAfterMs": 20000,
130+
"activeCount": 2,
131+
"blockers": [
132+
{ "kind": "root-request", "count": 1, "message": "1 active request" },
133+
{ "kind": "terminal-session", "count": 1, "message": "1 open terminal session" }
134+
]
135+
}
136+
```
137+
138+
Already-admitted work and its owned completions continue naturally; unrelated
139+
new runs, sessions, scheduled jobs, and independent work stay rejected. Open
140+
terminal sessions and terminal-persistence work remain blockers until they
141+
settle naturally. Drain mode never terminates or detaches a terminal. A terminal
142+
that remains open indefinitely can therefore keep the lease draining until the
143+
controller resumes it or the lease expires.
144+
145+
Poll `gateway.suspend.status` with the returned `suspensionId`, honoring
146+
`retryAfterMs`. While blockers remain, status returns `status: "draining"`
147+
together with `expiresAtMs`, `retryAfterMs`, `activeCount`, and `blockers`.
148+
Each status call refreshes the active-work snapshot. Once every blocker has
149+
finished, the same lease transitions to `{"status":"ready","expiresAtMs":...}`.
150+
Status returns `{"status":"running"}` when no suspension is held; querying a
151+
different active lease returns a conflict without exposing its identifiers.
111152
Resume returns `{"ok":true,"status":"running","resumed":true}`; repeating it
112153
after a successful resume returns `resumed: false`.
113154

155+
The dedicated `openclaw gateway suspend` command retains its existing
156+
refuse-only behavior. Controllers can request drain mode through any Gateway
157+
client or the generic CLI RPC command:
158+
159+
```bash
160+
openclaw gateway call gateway.suspend.prepare \
161+
--params '{"requestId":"host-operation-1","terminalPolicy":"preserve","drain":true}' \
162+
--json
163+
openclaw gateway call gateway.suspend.status \
164+
--params '{"suspensionId":"<suspension-id>"}' \
165+
--json
166+
openclaw gateway resume '<suspension-id>'
167+
```
168+
114169
A competing request ID or transient scheduler-resume failure returns retryable
115170
`UNAVAILABLE` with `retryAfterMs`. During scheduler recovery, prepare, status,
116171
and resume all return that error, the Gateway remains not-ready and
117172
fail-closed, and the host must not freeze or snapshot it. OpenClaw retries the
118173
scheduler automatically and reopens admission only after recovery succeeds. A
119-
mismatched resume ID returns `INVALID_REQUEST`. Prepare shares the Gateway's
120-
control-plane write budget of three attempts per minute; honor the returned
121-
retry delay. WebSocket clients are bucketed by device and IP. Admin HTTP
174+
mismatched resume ID returns `INVALID_REQUEST`. Prepare is subject to the
175+
Gateway's control-plane write limit of 30 attempts per minute; honor the
176+
returned retry delay. WebSocket clients are bucketed by device and IP. Admin HTTP
122177
controllers are bucketed by resolved client IP, so controllers behind one
123178
proxy can share a budget.
124179

125-
Preparation is refuse-only: OpenClaw closes new root/session/command admission,
126-
pauses automatic cron ticks, and inspects work synchronously. If anything is
127-
active, it resumes the scheduler and reopens admission before returning
128-
`busy`; it does not interrupt or drain that work. A ready lease lasts two
129-
minutes. Repeating `prepare` with the same `requestId` renews it; expiry resumes
130-
the scheduler before reopening admission.
180+
Without `drain: true`, preparation remains refuse-only: OpenClaw closes new
181+
root/session/command admission, pauses automatic cron ticks, and inspects work
182+
synchronously. If anything is active, it resumes the scheduler and reopens
183+
admission before returning `busy`; it does not interrupt or drain that work.
184+
With `drain: true`, the same suspension owner instead keeps admission closed
185+
and cron scheduling paused until existing work settles. Already-owned cron
186+
completion and reconciliation continue.
187+
188+
Both draining and ready leases last two minutes. Repeat `prepare` before
189+
`expiresAtMs` with the same `requestId`, terminal policy, and drain mode to renew
190+
the same `suspensionId`; changing any of those values conflicts with the
191+
existing lease. Use `status` for routine polling and reserve `prepare` for
192+
renewal to avoid consuming the write budget. Explicit resume and lease expiry
193+
restore scheduling before reopening admission. Leases remain in memory and
194+
disappear if the Gateway process exits.
131195
Restart emission that becomes due during a ready lease waits until the lease
132196
resumes; an in-flight restart makes preparation return `busy`.
133197

134-
While ready, `/healthz` remains live and `/readyz` returns `503`. Local or
135-
authenticated readiness responses include `gateway-draining`; unauthenticated
136-
remote probes receive only `{ "ready": false }`. The HTTP health probe,
137-
suspension methods on existing WebSocket connections, and an already-enabled
138-
Admin HTTP RPC route remain available. Other RPCs return retryable
139-
`UNAVAILABLE`. Built-in HTTP user-work routes and ordinary plugin HTTP routes,
198+
While draining or ready, `/healthz` remains live and `/readyz` returns `503`.
199+
Local or authenticated readiness responses include `gateway-draining`;
200+
unauthenticated remote probes receive only `{ "ready": false }`. The HTTP health
201+
probe, suspension methods on authenticated operator WebSocket connections, and
202+
an already-enabled Admin HTTP RPC route remain available. Other unrelated RPCs
203+
return retryable `UNAVAILABLE`. Built-in HTTP user-work routes and ordinary
204+
plugin HTTP routes,
140205
including OpenAI-compatible APIs, tool/session operations, node watches, and
141206
configured hooks, return `503` with `error.code: "gateway_unavailable"`. New
142207
plugin-owned WebSocket upgrades also return `503`; this covers upgrade

0 commit comments

Comments
 (0)