Skip to content

Commit 126b549

Browse files
authored
feat: run and reclaim cloud worker sessions from Control UI (#108398)
* feat(gateway): finish cloud worker session reclaim * chore(gateway): refresh cloud worker artifacts * fix(gateway): preserve recovery environment narrowing * test(gateway): complete worker tunnel mock * fix(gateway): harden cloud worker recovery * fix(gateway): serialize forced worker teardown * fix(gateway): handle replaced workspace directories * fix(gateway): preserve forced destroy receiver * test(ui): cover cloud worker reclaim flow * fix(gateway): close final worker recovery races * fix(ci): align cloud worker generated surfaces * test(gateway): satisfy worker queue lint * fix(gateway): recheck remote workspace after lease fence * chore: move cloud worker release note to PR
1 parent b4b6afa commit 126b549

124 files changed

Lines changed: 6895 additions & 431 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
@@ -373,6 +373,7 @@ enum class GatewayMethod(
373373
ApprovalResolve("approval.resolve"),
374374
SessionsSearch("sessions.search"),
375375
SessionsDispatch("sessions.dispatch"),
376+
SessionsReclaim("sessions.reclaim"),
376377
ModelsProbe("models.probe"),
377378
MigrationsMemoryPlan("migrations.memory.plan"),
378379
MigrationsMemoryApply("migrations.memory.apply"),

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,15 +908,19 @@ public struct EnvironmentsCreateResult: Codable, Sendable {
908908

909909
public struct EnvironmentsDestroyParams: Codable, Sendable {
910910
public let environmentid: String
911+
public let force: Bool?
911912

912913
public init(
913-
environmentid: String)
914+
environmentid: String,
915+
force: Bool? = nil)
914916
{
915917
self.environmentid = environmentid
918+
self.force = force
916919
}
917920

918921
private enum CodingKeys: String, CodingKey {
919922
case environmentid = "environmentId"
923+
case force
920924
}
921925
}
922926

@@ -3952,6 +3956,50 @@ public struct SessionsDispatchResult: Codable, Sendable {
39523956
}
39533957
}
39543958

3959+
public struct SessionsReclaimParams: Codable, Sendable {
3960+
public let key: String
3961+
public let agentid: String?
3962+
3963+
public init(
3964+
key: String,
3965+
agentid: String? = nil)
3966+
{
3967+
self.key = key
3968+
self.agentid = agentid
3969+
}
3970+
3971+
private enum CodingKeys: String, CodingKey {
3972+
case key
3973+
case agentid = "agentId"
3974+
}
3975+
}
3976+
3977+
public struct SessionsReclaimResult: Codable, Sendable {
3978+
public let ok: Bool
3979+
public let key: String
3980+
public let sessionid: String
3981+
public let placement: ReclaimedSessionPlacement
3982+
3983+
public init(
3984+
ok: Bool,
3985+
key: String,
3986+
sessionid: String,
3987+
placement: ReclaimedSessionPlacement)
3988+
{
3989+
self.ok = ok
3990+
self.key = key
3991+
self.sessionid = sessionid
3992+
self.placement = placement
3993+
}
3994+
3995+
private enum CodingKeys: String, CodingKey {
3996+
case ok
3997+
case key
3998+
case sessionid = "sessionId"
3999+
case placement
4000+
}
4001+
}
4002+
39554003
public struct SessionsCompactionListParams: Codable, Sendable {
39564004
public let key: String
39574005
public let agentid: String?

‎docs/gateway/cloud-workers.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,21 @@ openclaw gateway call sessions.dispatch \
9696

9797
`sessions.dispatch` closes local turn admission, drains active work, provisions the lease, runs setup, bootstraps OpenClaw, syncs the workspace, and returns once the placement reaches `active` worker ownership. Budget several minutes for the first dispatch; leases and installs are cached where the provider supports it. After that, talk to the session as usual — turns route to the worker automatically.
9898

99-
Placement moves through a durable state machine (`local → requested → provisioning → syncing → starting → active`), so a Gateway restart mid-dispatch reconciles instead of leaking machines. Dispatch is one-way in v1: there is no pull-back RPC yet. A failed worker turn fails that turn and keeps the active placement available for a retry; lifecycle failures instead move the placement to an error or reclaimed state and preserve their diagnostic tail.
99+
Completed worker turns reconcile eligible, size-bounded workspace files back into the session's managed worktree before the turn claim is released. The terminal worker event creates a durable pending-result fence before it is acknowledged, so Gateway restart recovery pulls the remote workspace back before stale-turn cleanup can destroy its owner. Reconciliation authenticates the worker manifest and stops on local divergence instead of overwriting either side. Before changing files, the Gateway stores a bounded rollback journal in its SQLite state database; a retry recovers that journal after an interrupted Gateway process. Workspace results use Git file semantics: regular files, executable bits, symlinks, additions, changes, and deletions are retained, while empty directories and other directory modes are not. Remote commit objects are not retained; the resulting file changes remain in the managed worktree for normal review and commit.
100+
101+
When the work is complete and no turn is running, open the session menu and choose **Stop cloud worker…**. The Gateway performs one final workspace reconciliation before it destroys the environment. A placement already in `draining` or `reconciling` is finishing teardown; wait for its badge to become `reclaimed` before deleting the session.
102+
103+
For a broken or runaway attached worker, an operator can call `environments.destroy` with `{ "force": true }` as a last resort. Forced teardown durably marks the placement failed and abandons any unreconciled remote result before destroying the environment.
104+
105+
The equivalent administrative RPC is:
106+
107+
```bash
108+
openclaw gateway call sessions.reclaim \
109+
--timeout 600000 \
110+
--params '{"key":"agent:main:big-refactor"}'
111+
```
112+
113+
Placement moves through a durable state machine (`local → requested → provisioning → syncing → starting → active`), so a Gateway restart mid-dispatch reconciles instead of leaking machines. A failed model turn keeps the active placement available for a retry. If inbound workspace reconciliation fails, the worker also stays active so the operator can resolve the local conflict and retry without losing the remote result; lifecycle failures instead move the placement to an error or reclaimed state and preserve their diagnostic tail.
100114

101115
## Security model
102116

‎packages/gateway-protocol/src/index.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ import {
341341
SessionsDescribeParamsSchema,
342342
SessionsDispatchParamsSchema,
343343
SessionsDispatchResultSchema,
344+
SessionsReclaimParamsSchema,
345+
SessionsReclaimResultSchema,
344346
SessionGroupSchema,
345347
SessionsGroupsDeleteParamsSchema,
346348
SessionsGroupsListParamsSchema,
@@ -641,6 +643,8 @@ export const validateSessionsCreateParams = lazyCompile(SessionsCreateParamsSche
641643
export const validateSessionsSendParams = lazyCompile(SessionsSendParamsSchema);
642644
export const validateSessionsDispatchParams = lazyCompile(SessionsDispatchParamsSchema);
643645
export const validateSessionsDispatchResult = lazyCompile(SessionsDispatchResultSchema);
646+
export const validateSessionsReclaimParams = lazyCompile(SessionsReclaimParamsSchema);
647+
export const validateSessionsReclaimResult = lazyCompile(SessionsReclaimResultSchema);
644648
export const validateSessionsMessagesSubscribeParams = lazyCompile(
645649
SessionsMessagesSubscribeParamsSchema,
646650
);
@@ -993,6 +997,8 @@ export {
993997
SessionsCreateResultSchema,
994998
SessionsDispatchParamsSchema,
995999
SessionsDispatchResultSchema,
1000+
SessionsReclaimParamsSchema,
1001+
SessionsReclaimResultSchema,
9961002
SessionsSendParamsSchema,
9971003
SessionsAbortParamsSchema,
9981004
SessionsPatchParamsSchema,
@@ -1548,6 +1554,8 @@ export type {
15481554
SessionWorktreeInfo,
15491555
SessionsDispatchParams,
15501556
SessionsDispatchResult,
1557+
SessionsReclaimParams,
1558+
SessionsReclaimResult,
15511559
SessionsCreateResult,
15521560
SessionsPatchParams,
15531561
SessionsResetParams,

‎packages/gateway-protocol/src/schema/environments.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe("worker environment protocol schemas", () => {
6868
).toBe(false);
6969
expect(validateEnvironmentsDestroyParams({ environmentId: "" })).toBe(false);
7070
expect(validateEnvironmentsDestroyParams({ environmentId: "environment-1", force: true })).toBe(
71-
false,
71+
true,
7272
);
7373
});
7474

‎packages/gateway-protocol/src/schema/environments.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ export const EnvironmentsCreateParamsSchema = closedObject({
9393
export const EnvironmentsCreateResultSchema = createEnvironmentSummarySchema();
9494

9595
/** Destroys one durable worker environment by its gateway-owned id. */
96-
export const EnvironmentsDestroyParamsSchema = closedObject({ environmentId: NonEmptyString });
96+
export const EnvironmentsDestroyParamsSchema = closedObject({
97+
environmentId: NonEmptyString,
98+
force: Type.Optional(Type.Boolean()),
99+
});
97100

98101
/** Destroy result exposes the terminal worker lifecycle state. */
99102
export const EnvironmentsDestroyResultSchema = createEnvironmentSummarySchema();

‎packages/gateway-protocol/src/schema/session-placement.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
SessionPlacementStateSchema,
66
validateSessionsDispatchParams,
77
validateSessionsDispatchResult,
8+
validateSessionsReclaimParams,
9+
validateSessionsReclaimResult,
810
} from "../index.js";
911

1012
const placementStates = [
@@ -60,6 +62,15 @@ describe("session dispatch protocol schemas", () => {
6062
).toBe(false);
6163
});
6264

65+
it("accepts only a session selector for worker reclaim", () => {
66+
expect(validateSessionsReclaimParams({ key: "agent:main:dispatch", agentId: "main" })).toBe(
67+
true,
68+
);
69+
expect(validateSessionsReclaimParams({ key: "agent:main:dispatch", profileId: "dev" })).toBe(
70+
false,
71+
);
72+
});
73+
6374
it("keeps placement states closed", () => {
6475
for (const state of placementStates) {
6576
expect(Value.Check(SessionPlacementStateSchema, state)).toBe(true);
@@ -242,6 +253,25 @@ describe("session dispatch protocol schemas", () => {
242253
).toBe(false);
243254
});
244255

256+
it("accepts only reclaimed ownership in successful reclaim results", () => {
257+
expect(
258+
validateSessionsReclaimResult({
259+
ok: true,
260+
key: "agent:main:dispatch",
261+
sessionId: "session-1",
262+
placement: { state: "reclaimed", ...basePlacement, ...workerOwnedFields },
263+
}),
264+
).toBe(true);
265+
expect(
266+
validateSessionsReclaimResult({
267+
ok: true,
268+
key: "agent:main:dispatch",
269+
sessionId: "session-1",
270+
placement: { state: "active", ...basePlacement, ...workerOwnedFields },
271+
}),
272+
).toBe(false);
273+
});
274+
245275
it("rejects unknown placement fields", () => {
246276
expect(
247277
Value.Check(SessionPlacementSchema, {

‎packages/gateway-protocol/src/schema/session-placement.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ export const SessionsDispatchResultSchema = Type.Object(
175175
{ additionalProperties: false },
176176
);
177177

178+
/** Requests safe workspace reconciliation and teardown of an active cloud worker. */
179+
export const SessionsReclaimParamsSchema = Type.Object(
180+
{
181+
key: NonEmptyString,
182+
agentId: Type.Optional(NonEmptyString),
183+
},
184+
{ additionalProperties: false },
185+
);
186+
187+
/** Result returned once worker ownership has been destroyed and reclaimed. */
188+
export const SessionsReclaimResultSchema = Type.Object(
189+
{
190+
ok: Type.Literal(true),
191+
key: NonEmptyString,
192+
sessionId: NonEmptyString,
193+
placement: ReclaimedSessionPlacementSchema,
194+
},
195+
{ additionalProperties: false },
196+
);
197+
178198
export const SessionPlacementProtocolSchemas = {
179199
SessionPlacementState: SessionPlacementStateSchema,
180200
LocalSessionPlacement: LocalSessionPlacementSchema,
@@ -190,9 +210,13 @@ export const SessionPlacementProtocolSchemas = {
190210
SessionPlacement: SessionPlacementSchema,
191211
SessionsDispatchParams: SessionsDispatchParamsSchema,
192212
SessionsDispatchResult: SessionsDispatchResultSchema,
213+
SessionsReclaimParams: SessionsReclaimParamsSchema,
214+
SessionsReclaimResult: SessionsReclaimResultSchema,
193215
} as const;
194216

195217
export type SessionPlacementState = Static<typeof SessionPlacementStateSchema>;
196218
export type SessionPlacement = Static<typeof SessionPlacementSchema>;
197219
export type SessionsDispatchParams = Static<typeof SessionsDispatchParamsSchema>;
198220
export type SessionsDispatchResult = Static<typeof SessionsDispatchResultSchema>;
221+
export type SessionsReclaimParams = Static<typeof SessionsReclaimParamsSchema>;
222+
export type SessionsReclaimResult = Static<typeof SessionsReclaimResultSchema>;

‎src/gateway/method-scopes.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe("method scope resolution", () => {
5252
["config.schema.lookup", ["operator.read"]],
5353
["sessions.create", ["operator.write"]],
5454
["sessions.dispatch", ["operator.admin"]],
55+
["sessions.reclaim", ["operator.admin"]],
5556
["sessions.send", ["operator.write"]],
5657
["sessions.abort", ["operator.write"]],
5758
["tasks.cancel", ["operator.write"]],

‎src/gateway/methods/core-descriptors.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ const CORE_GATEWAY_METHOD_SPECS: readonly CoreGatewayMethodSpec[] = [
355355
startup: true,
356356
controlPlaneWrite: true,
357357
},
358+
{
359+
name: "sessions.reclaim",
360+
scope: "operator.admin",
361+
startup: true,
362+
controlPlaneWrite: true,
363+
},
358364
{ name: "models.probe", scope: "operator.admin" },
359365
// Memory migration reads host assistant state and writes agent workspaces.
360366
{ name: "migrations.memory.plan", scope: "operator.admin" },

0 commit comments

Comments
 (0)