Skip to content

Commit f59e945

Browse files
authored
feat(gateway): move sessions between Gateway and runners (#125036)
* chore: preserve session placement move work * feat(gateway): move sessions between runners * test(gateway): align recovery fixture with move barriers * fix(protocol): format literal-only Swift initializers * test(ui): prove session move terminal state * refactor(gateway): narrow session move target fence * fix(gateway): preserve pending result recovery ownership * test(workers): migrate reclaim lifecycle fixtures * refactor(gateway): keep move internals private * test(gateway): align split bootstrap fixture with moves * fix(gateway): recover session moves through exact intent * refactor(gateway): derive move placement subtypes locally
1 parent 2a43508 commit f59e945

81 files changed

Lines changed: 4176 additions & 438 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/android/app/src/main/java/ai/openclaw/app/gateway/GatewayProtocol.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ enum class GatewayMethod(
584584
PortalList("portal.list"),
585585
PortalOpen("portal.open"),
586586
PortalClose("portal.close"),
587+
SessionsMove("sessions.move"),
587588
}
588589

589590
enum class GatewayEvent(

‎apps/android/app/src/test/java/ai/openclaw/app/gateway/GatewayProtocolGeneratedTest.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ class GatewayProtocolGeneratedTest {
6363
assertTrue(events.size > 20)
6464
assertEquals(methods.size, methods.toSet().size)
6565
assertEquals(events.size, events.toSet().size)
66+
assertEquals("sessions.move", GatewayMethod.SessionsMove.rawValue)
6667
}
6768
}

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

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public enum SessionPlacementState: String, Codable, Sendable {
152152
case failed = "failed"
153153
}
154154

155+
public enum SessionMovePlacementState: String, Codable, Sendable {
156+
case local = "local"
157+
case active = "active"
158+
}
159+
155160
public enum SessionDiscussionState: String, Codable, Sendable {
156161
case none = "none"
157162
case available = "available"
@@ -7615,6 +7620,263 @@ public struct SessionsReclaimResult: Codable, Sendable {
76157620
}
76167621
}
76177622

7623+
public struct SessionMoveExpectedSource: Codable, Sendable {
7624+
public let generation: Int
7625+
public let environmentid: String
7626+
public let ownerepoch: Int
7627+
7628+
public init(
7629+
generation: Int,
7630+
environmentid: String,
7631+
ownerepoch: Int)
7632+
{
7633+
self.generation = generation
7634+
self.environmentid = environmentid
7635+
self.ownerepoch = ownerepoch
7636+
}
7637+
7638+
private enum CodingKeys: String, CodingKey {
7639+
case generation
7640+
case environmentid = "environmentId"
7641+
case ownerepoch = "ownerEpoch"
7642+
}
7643+
}
7644+
7645+
public struct SessionMoveGatewayTarget: Codable, Sendable {
7646+
public let kind: String
7647+
7648+
public init()
7649+
{
7650+
self.kind = "gateway"
7651+
}
7652+
7653+
private enum CodingKeys: String, CodingKey {
7654+
case kind
7655+
}
7656+
7657+
public init(from decoder: Decoder) throws {
7658+
let rawContainer = try decoder.container(keyedBy: GatewayAnyCodingKey.self)
7659+
let unexpectedKeys = rawContainer.allKeys
7660+
.map(\.stringValue)
7661+
.filter { !Set(["kind"]).contains($0) }
7662+
if !unexpectedKeys.isEmpty {
7663+
throw DecodingError.dataCorrupted(
7664+
.init(
7665+
codingPath: rawContainer.codingPath,
7666+
debugDescription: "Unexpected keys for SessionMoveGatewayTarget: \(unexpectedKeys.sorted().joined(separator: ", "))"
7667+
)
7668+
)
7669+
}
7670+
let container = try decoder.container(keyedBy: CodingKeys.self)
7671+
let decodedKind = try container.decode(String.self, forKey: .kind)
7672+
guard decodedKind == "gateway" else {
7673+
throw DecodingError.dataCorruptedError(
7674+
forKey: .kind,
7675+
in: container,
7676+
debugDescription: "Expected kind to equal gateway"
7677+
)
7678+
}
7679+
self.kind = "gateway"
7680+
}
7681+
7682+
public func encode(to encoder: Encoder) throws {
7683+
var container = encoder.container(keyedBy: CodingKeys.self)
7684+
try container.encode("gateway", forKey: .kind)
7685+
}
7686+
}
7687+
7688+
public struct SessionMoveProfileTarget: Codable, Sendable {
7689+
public let kind: String
7690+
public let profileid: String
7691+
7692+
public init(
7693+
profileid: String
7694+
)
7695+
{
7696+
self.kind = "profile"
7697+
self.profileid = profileid
7698+
}
7699+
7700+
private enum CodingKeys: String, CodingKey {
7701+
case kind
7702+
case profileid = "profileId"
7703+
}
7704+
7705+
public init(from decoder: Decoder) throws {
7706+
let rawContainer = try decoder.container(keyedBy: GatewayAnyCodingKey.self)
7707+
let unexpectedKeys = rawContainer.allKeys
7708+
.map(\.stringValue)
7709+
.filter { !Set(["kind", "profileId"]).contains($0) }
7710+
if !unexpectedKeys.isEmpty {
7711+
throw DecodingError.dataCorrupted(
7712+
.init(
7713+
codingPath: rawContainer.codingPath,
7714+
debugDescription: "Unexpected keys for SessionMoveProfileTarget: \(unexpectedKeys.sorted().joined(separator: ", "))"
7715+
)
7716+
)
7717+
}
7718+
let container = try decoder.container(keyedBy: CodingKeys.self)
7719+
let decodedKind = try container.decode(String.self, forKey: .kind)
7720+
guard decodedKind == "profile" else {
7721+
throw DecodingError.dataCorruptedError(
7722+
forKey: .kind,
7723+
in: container,
7724+
debugDescription: "Expected kind to equal profile"
7725+
)
7726+
}
7727+
self.kind = "profile"
7728+
self.profileid = try container.decode(String.self, forKey: .profileid)
7729+
}
7730+
7731+
public func encode(to encoder: Encoder) throws {
7732+
var container = encoder.container(keyedBy: CodingKeys.self)
7733+
try container.encode("profile", forKey: .kind)
7734+
try container.encode(profileid, forKey: .profileid)
7735+
}
7736+
}
7737+
7738+
public struct SessionMoveDeviceTarget: Codable, Sendable {
7739+
public let kind: String
7740+
public let deviceid: String
7741+
7742+
public init(
7743+
deviceid: String
7744+
)
7745+
{
7746+
self.kind = "device"
7747+
self.deviceid = deviceid
7748+
}
7749+
7750+
private enum CodingKeys: String, CodingKey {
7751+
case kind
7752+
case deviceid = "deviceId"
7753+
}
7754+
7755+
public init(from decoder: Decoder) throws {
7756+
let rawContainer = try decoder.container(keyedBy: GatewayAnyCodingKey.self)
7757+
let unexpectedKeys = rawContainer.allKeys
7758+
.map(\.stringValue)
7759+
.filter { !Set(["kind", "deviceId"]).contains($0) }
7760+
if !unexpectedKeys.isEmpty {
7761+
throw DecodingError.dataCorrupted(
7762+
.init(
7763+
codingPath: rawContainer.codingPath,
7764+
debugDescription: "Unexpected keys for SessionMoveDeviceTarget: \(unexpectedKeys.sorted().joined(separator: ", "))"
7765+
)
7766+
)
7767+
}
7768+
let container = try decoder.container(keyedBy: CodingKeys.self)
7769+
let decodedKind = try container.decode(String.self, forKey: .kind)
7770+
guard decodedKind == "device" else {
7771+
throw DecodingError.dataCorruptedError(
7772+
forKey: .kind,
7773+
in: container,
7774+
debugDescription: "Expected kind to equal device"
7775+
)
7776+
}
7777+
self.kind = "device"
7778+
self.deviceid = try container.decode(String.self, forKey: .deviceid)
7779+
}
7780+
7781+
public func encode(to encoder: Encoder) throws {
7782+
var container = encoder.container(keyedBy: CodingKeys.self)
7783+
try container.encode("device", forKey: .kind)
7784+
try container.encode(deviceid, forKey: .deviceid)
7785+
}
7786+
}
7787+
7788+
public struct SessionPlacementMove: Codable, Sendable {
7789+
public let target: SessionMoveTarget
7790+
public let updatedatms: Int
7791+
public let error: String?
7792+
7793+
public init(
7794+
target: SessionMoveTarget,
7795+
updatedatms: Int,
7796+
error: String? = nil)
7797+
{
7798+
self.target = target
7799+
self.updatedatms = updatedatms
7800+
self.error = error
7801+
}
7802+
7803+
private enum CodingKeys: String, CodingKey {
7804+
case target
7805+
case updatedatms = "updatedAtMs"
7806+
case error
7807+
}
7808+
}
7809+
7810+
public struct SessionsMoveParams: Codable, Sendable {
7811+
public let key: String
7812+
public let agentid: String?
7813+
public let expected: SessionMoveExpectedSource
7814+
public let target: SessionMoveTarget
7815+
7816+
public init(
7817+
key: String,
7818+
agentid: String? = nil,
7819+
expected: SessionMoveExpectedSource,
7820+
target: SessionMoveTarget)
7821+
{
7822+
self.key = key
7823+
self.agentid = agentid
7824+
self.expected = expected
7825+
self.target = target
7826+
}
7827+
7828+
private enum CodingKeys: String, CodingKey {
7829+
case key
7830+
case agentid = "agentId"
7831+
case expected
7832+
case target
7833+
}
7834+
}
7835+
7836+
public struct SessionMovePlacement: Codable, Sendable {
7837+
public let state: SessionMovePlacementState
7838+
public let generation: Int
7839+
7840+
public init(
7841+
state: SessionMovePlacementState,
7842+
generation: Int)
7843+
{
7844+
self.state = state
7845+
self.generation = generation
7846+
}
7847+
7848+
private enum CodingKeys: String, CodingKey {
7849+
case state
7850+
case generation
7851+
}
7852+
}
7853+
7854+
public struct SessionsMoveResult: Codable, Sendable {
7855+
public let ok: Bool
7856+
public let key: String
7857+
public let sessionid: String
7858+
public let placement: SessionMovePlacement
7859+
7860+
public init(
7861+
ok: Bool,
7862+
key: String,
7863+
sessionid: String,
7864+
placement: SessionMovePlacement)
7865+
{
7866+
self.ok = ok
7867+
self.key = key
7868+
self.sessionid = sessionid
7869+
self.placement = placement
7870+
}
7871+
7872+
private enum CodingKeys: String, CodingKey {
7873+
case ok
7874+
case key
7875+
case sessionid = "sessionId"
7876+
case placement
7877+
}
7878+
}
7879+
76187880
public struct SessionDiscussionInfo: Codable, Sendable {
76197881
public let state: SessionDiscussionState
76207882
public let embedurl: String?
@@ -20395,6 +20657,40 @@ public enum SessionsReclaimResultPlacement: Codable, Sendable {
2039520657
}
2039620658
}
2039720659

20660+
public enum SessionMoveTarget: Codable, Sendable {
20661+
case gateway(SessionMoveGatewayTarget)
20662+
case profile(SessionMoveProfileTarget)
20663+
case device(SessionMoveDeviceTarget)
20664+
20665+
private enum CodingKeys: String, CodingKey {
20666+
case discriminator = "kind"
20667+
}
20668+
20669+
public init(from decoder: Decoder) throws {
20670+
let container = try decoder.container(keyedBy: CodingKeys.self)
20671+
let discriminator = try container.decode(String.self, forKey: .discriminator)
20672+
switch discriminator {
20673+
case "gateway": self = try .gateway(SessionMoveGatewayTarget(from: decoder))
20674+
case "profile": self = try .profile(SessionMoveProfileTarget(from: decoder))
20675+
case "device": self = try .device(SessionMoveDeviceTarget(from: decoder))
20676+
default:
20677+
throw DecodingError.dataCorruptedError(
20678+
forKey: .discriminator,
20679+
in: container,
20680+
debugDescription: "Unknown SessionMoveTarget discriminator value"
20681+
)
20682+
}
20683+
}
20684+
20685+
public func encode(to encoder: Encoder) throws {
20686+
switch self {
20687+
case .gateway(let value): try value.encode(to: encoder)
20688+
case .profile(let value): try value.encode(to: encoder)
20689+
case .device(let value): try value.encode(to: encoder)
20690+
}
20691+
}
20692+
}
20693+
2039820694
public enum AuditActivityEventV1: Codable, Sendable {
2039920695
case agentRun(AuditActivityAgentRunV1)
2040020696
case toolAction(AuditActivityToolActionV1)

‎apps/shared/OpenClawKit/Tests/OpenClawKitTests/GatewayProtocolGeneratedModelsTests.swift‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,44 @@ struct GatewayProtocolGeneratedModelsTests {
7878
#expect(result.projects.isEmpty)
7979
#expect(result.observedprojects == nil)
8080
}
81+
82+
@Test
83+
func `session move models preserve exact source and closed targets`() throws {
84+
let params = try JSONDecoder().decode(
85+
SessionsMoveParams.self,
86+
from: Data(
87+
#"{"key":"agent:main:move","expected":{"generation":4,"environmentId":"environment-1","ownerEpoch":7},"target":{"kind":"profile","profileId":"development"}}"#.utf8))
88+
89+
#expect(params.expected.generation == 4)
90+
#expect(params.expected.environmentid == "environment-1")
91+
#expect(params.expected.ownerepoch == 7)
92+
guard case let .profile(profile) = params.target else {
93+
Issue.record("Expected the generated profile move target")
94+
return
95+
}
96+
#expect(profile.profileid == "development")
97+
98+
let gateway = try JSONDecoder().decode(
99+
SessionMoveTarget.self,
100+
from: Data(#"{"kind":"gateway"}"#.utf8))
101+
let device = try JSONDecoder().decode(
102+
SessionMoveTarget.self,
103+
from: Data(#"{"kind":"device","deviceId":"device-1"}"#.utf8))
104+
guard case .gateway = gateway, case let .device(deviceTarget) = device else {
105+
Issue.record("Expected generated gateway and device move targets")
106+
return
107+
}
108+
#expect(deviceTarget.deviceid == "device-1")
109+
110+
#expect(throws: DecodingError.self) {
111+
try JSONDecoder().decode(
112+
SessionMoveTarget.self,
113+
from: Data(#"{"kind":"other"}"#.utf8))
114+
}
115+
#expect(throws: DecodingError.self) {
116+
try JSONDecoder().decode(
117+
SessionMoveTarget.self,
118+
from: Data(#"{"kind":"gateway","profileId":"development"}"#.utf8))
119+
}
120+
}
81121
}

0 commit comments

Comments
 (0)