Skip to content

Commit 48b0f4e

Browse files
authored
feat(gateway): session worktree targeting, branch listing, and server-side session groups (#103432)
* feat(gateway): session worktree targeting, branch listing, and group catalog - sessions.create accepts worktreeBaseRef/worktreeName (write scope) and execNode (admin); worktree binding persists on the session entry as worktree { id, branch, repoRoot } and projects onto session rows with execNode so UIs can show checkout/branch/node state - new worktrees.branches RPC lists local/remote branches (no fetch) for base-ref pickers; worktrees.remove now reports snapshotError - sessions.delete reports preserved dirty checkouts as worktreePreserved - gateway-owned session group catalog in the shared state DB with sessions.groups.list/put/rename/delete; rename/delete update member categories server-side without bumping recency; sessions.patch absorbs ad-hoc categories into the catalog - group session display names prefer the human chat title (subject or space #channel) over stored compact tokens Part of #103431 * fix(gateway): route group category updates through the session accessor and regen protocol models - bulk member-category rename/clear uses applySessionEntryReplacements instead of legacy updateSessionStore call sites (session accessor boundary guard) - regenerate Swift GatewayModels for the worktrees.branches schemas, snapshotError, and new sessions.create params Part of #103431 * fix(gateway): resolvable remote branch refs and workspace-scoped branch listing - worktrees.branches returns remote-only branches remote-qualified (origin/feature-a) so every advertised name works as a worktree base ref - write-scoped worktrees.branches callers are limited to configured agent workspaces; other host paths require operator.admin, matching the sessions.create cwd bar Part of #103431 * fix(gateway): guard worktree name reuse by owner and env-scope group transactions - managedWorktrees.create rejects a caller-chosen name whose live or restorable record belongs to a different owner, so write-scoped sessions.create cannot bind a session into another session's or a manual checkout - session group catalog writes run their SQLite transaction on the same env-scoped handle as their statements, keeping OPENCLAW_STATE_DIR overrides atomic and away from the default state DB Part of #103431
1 parent b2c1db4 commit 48b0f4e

35 files changed

Lines changed: 1546 additions & 24 deletions

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

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,18 +1242,22 @@ public struct WorktreesRemoveParams: Codable, Sendable {
12421242
public struct WorktreesRemoveResult: Codable, Sendable {
12431243
public let removed: Bool
12441244
public let snapshotref: String?
1245+
public let snapshoterror: String?
12451246

12461247
public init(
12471248
removed: Bool,
1248-
snapshotref: String? = nil)
1249+
snapshotref: String? = nil,
1250+
snapshoterror: String? = nil)
12491251
{
12501252
self.removed = removed
12511253
self.snapshotref = snapshotref
1254+
self.snapshoterror = snapshoterror
12521255
}
12531256

12541257
private enum CodingKeys: String, CodingKey {
12551258
case removed
12561259
case snapshotref = "snapshotRef"
1260+
case snapshoterror = "snapshotError"
12571261
}
12581262
}
12591263

@@ -1295,6 +1299,60 @@ public struct WorktreesGcResult: Codable, Sendable {
12951299
}
12961300
}
12971301

1302+
public struct WorktreeBranch: Codable, Sendable {
1303+
public let name: String
1304+
public let kind: AnyCodable
1305+
1306+
public init(
1307+
name: String,
1308+
kind: AnyCodable)
1309+
{
1310+
self.name = name
1311+
self.kind = kind
1312+
}
1313+
1314+
private enum CodingKeys: String, CodingKey {
1315+
case name
1316+
case kind
1317+
}
1318+
}
1319+
1320+
public struct WorktreesBranchesParams: Codable, Sendable {
1321+
public let reporoot: String
1322+
1323+
public init(
1324+
reporoot: String)
1325+
{
1326+
self.reporoot = reporoot
1327+
}
1328+
1329+
private enum CodingKeys: String, CodingKey {
1330+
case reporoot = "repoRoot"
1331+
}
1332+
}
1333+
1334+
public struct WorktreesBranchesResult: Codable, Sendable {
1335+
public let branches: [WorktreeBranch]
1336+
public let defaultbranch: String?
1337+
public let headbranch: String?
1338+
1339+
public init(
1340+
branches: [WorktreeBranch],
1341+
defaultbranch: String? = nil,
1342+
headbranch: String? = nil)
1343+
{
1344+
self.branches = branches
1345+
self.defaultbranch = defaultbranch
1346+
self.headbranch = headbranch
1347+
}
1348+
1349+
private enum CodingKeys: String, CodingKey {
1350+
case branches
1351+
case defaultbranch = "defaultBranch"
1352+
case headbranch = "headBranch"
1353+
}
1354+
}
1355+
12981356
public struct NodePairListParams: Codable, Sendable {}
12991357

13001358
public struct NodePairApproveParams: Codable, Sendable {
@@ -2528,6 +2586,9 @@ public struct SessionsCreateParams: Codable, Sendable {
25282586
public let task: String?
25292587
public let message: String?
25302588
public let worktree: Bool?
2589+
public let worktreebaseref: String?
2590+
public let worktreename: String?
2591+
public let execnode: String?
25312592
public let cwd: String?
25322593

25332594
public init(
@@ -2541,6 +2602,9 @@ public struct SessionsCreateParams: Codable, Sendable {
25412602
task: String? = nil,
25422603
message: String? = nil,
25432604
worktree: Bool? = nil,
2605+
worktreebaseref: String? = nil,
2606+
worktreename: String? = nil,
2607+
execnode: String? = nil,
25442608
cwd: String? = nil)
25452609
{
25462610
self.key = key
@@ -2553,6 +2617,9 @@ public struct SessionsCreateParams: Codable, Sendable {
25532617
self.task = task
25542618
self.message = message
25552619
self.worktree = worktree
2620+
self.worktreebaseref = worktreebaseref
2621+
self.worktreename = worktreename
2622+
self.execnode = execnode
25562623
self.cwd = cwd
25572624
}
25582625

@@ -2567,6 +2634,9 @@ public struct SessionsCreateParams: Codable, Sendable {
25672634
case task
25682635
case message
25692636
case worktree
2637+
case worktreebaseref = "worktreeBaseRef"
2638+
case worktreename = "worktreeName"
2639+
case execnode = "execNode"
25702640
case cwd
25712641
}
25722642
}
@@ -2943,6 +3013,108 @@ public struct SessionsDeleteParams: Codable, Sendable {
29433013
}
29443014
}
29453015

3016+
public struct SessionGroup: Codable, Sendable {
3017+
public let name: String
3018+
public let position: Int
3019+
3020+
public init(
3021+
name: String,
3022+
position: Int)
3023+
{
3024+
self.name = name
3025+
self.position = position
3026+
}
3027+
3028+
private enum CodingKeys: String, CodingKey {
3029+
case name
3030+
case position
3031+
}
3032+
}
3033+
3034+
public struct SessionsGroupsListParams: Codable, Sendable {}
3035+
3036+
public struct SessionsGroupsListResult: Codable, Sendable {
3037+
public let groups: [SessionGroup]
3038+
3039+
public init(
3040+
groups: [SessionGroup])
3041+
{
3042+
self.groups = groups
3043+
}
3044+
3045+
private enum CodingKeys: String, CodingKey {
3046+
case groups
3047+
}
3048+
}
3049+
3050+
public struct SessionsGroupsPutParams: Codable, Sendable {
3051+
public let names: [String]
3052+
3053+
public init(
3054+
names: [String])
3055+
{
3056+
self.names = names
3057+
}
3058+
3059+
private enum CodingKeys: String, CodingKey {
3060+
case names
3061+
}
3062+
}
3063+
3064+
public struct SessionsGroupsRenameParams: Codable, Sendable {
3065+
public let name: String
3066+
public let to: String
3067+
3068+
public init(
3069+
name: String,
3070+
to: String)
3071+
{
3072+
self.name = name
3073+
self.to = to
3074+
}
3075+
3076+
private enum CodingKeys: String, CodingKey {
3077+
case name
3078+
case to
3079+
}
3080+
}
3081+
3082+
public struct SessionsGroupsDeleteParams: Codable, Sendable {
3083+
public let name: String
3084+
3085+
public init(
3086+
name: String)
3087+
{
3088+
self.name = name
3089+
}
3090+
3091+
private enum CodingKeys: String, CodingKey {
3092+
case name
3093+
}
3094+
}
3095+
3096+
public struct SessionsGroupsMutationResult: Codable, Sendable {
3097+
public let ok: Bool
3098+
public let groups: [SessionGroup]
3099+
public let updatedsessions: Int?
3100+
3101+
public init(
3102+
ok: Bool,
3103+
groups: [SessionGroup],
3104+
updatedsessions: Int? = nil)
3105+
{
3106+
self.ok = ok
3107+
self.groups = groups
3108+
self.updatedsessions = updatedsessions
3109+
}
3110+
3111+
private enum CodingKeys: String, CodingKey {
3112+
case ok
3113+
case groups
3114+
case updatedsessions = "updatedSessions"
3115+
}
3116+
}
3117+
29463118
public struct SessionsCompactParams: Codable, Sendable {
29473119
public let key: String
29483120
public let agentid: String?

‎docs/concepts/managed-worktrees.md‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ The resulting managed worktree is owned by the session, and every agent run in t
5555

5656
`sessions.create` may include an absolute `cwd` together with `worktree: true` when a task targets a project other than the configured agent workspace. That explicit host path requires `operator.admin`; ordinary worktree chat creation remains `operator.write` and stays anchored to the configured workspace.
5757

58+
`sessions.create` also accepts `worktreeBaseRef` and `worktreeName` alongside `worktree: true` to pick the base ref and the worktree name (the branch becomes `openclaw/<name>`); both stay at `operator.write`. The created worktree is returned in the create result and persisted on the session row as `worktree: { id, branch, repoRoot }`, so session lists can show the checkout and branch. Deleting a session reports a preserved dirty checkout as `worktreePreserved` instead of silently leaving it behind.
59+
5860
## Snapshots, cleanup, and restore
5961

6062
Removal first creates a synthetic commit containing tracked and non-ignored untracked files, and pins it at `refs/openclaw/snapshots/<id>`. Gitignored files are excluded from the repository object database; files selected by `.worktreeinclude` are copied again during restore. If snapshot creation fails, removal stops. An explicit force delete can continue without a snapshot.
@@ -82,15 +84,16 @@ The Control UI **Worktrees** page under Settings provides the same list, delete,
8284

8385
## Gateway methods
8486

85-
| Method | Purpose |
86-
| ------------------- | --------------------------------------------- |
87-
| `worktrees.list` | List active and restorable worktree records. |
88-
| `worktrees.create` | Create or reuse a named managed worktree. |
89-
| `worktrees.remove` | Snapshot and remove a worktree. |
90-
| `worktrees.restore` | Restore a removed worktree from its snapshot. |
91-
| `worktrees.gc` | Run idle, orphan, and retention cleanup now. |
87+
| Method | Purpose |
88+
| -------------------- | ----------------------------------------------------------------------- |
89+
| `worktrees.list` | List active and restorable worktree records. |
90+
| `worktrees.branches` | List local and remote branches of a repository for base-ref pickers. |
91+
| `worktrees.create` | Create or reuse a named managed worktree. |
92+
| `worktrees.remove` | Snapshot and remove a worktree. Forced removals report `snapshotError`. |
93+
| `worktrees.restore` | Restore a removed worktree from its snapshot. |
94+
| `worktrees.gc` | Run idle, orphan, and retention cleanup now. |
9295

93-
`worktrees.list` requires `operator.read`. Mutating methods require `operator.admin`.
96+
`worktrees.list` requires `operator.read`, and the mutating methods require `operator.admin`. `worktrees.branches` needs `operator.write` for configured agent workspaces, while any other host path requires `operator.admin` (matching the `sessions.create` cwd bar). It reads existing refs only and never fetches, and remote-only branches come back remote-qualified (`origin/feature-a`) so every returned name resolves as a base ref.
9497

9598
## Workboard workspaces
9699

‎docs/gateway/protocol.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ methods. Treat this as feature discovery, not a full enumeration of
463463
- `sessions.preview` returns bounded transcript previews for specific session keys.
464464
- `sessions.describe` returns one gateway session row for an exact session key.
465465
- `sessions.resolve` resolves or canonicalizes a session target.
466-
- `sessions.create` creates a new session entry.
466+
- `sessions.create` creates a new session entry. `worktree: true` provisions a managed worktree; optional `worktreeBaseRef`/`worktreeName` select the base ref and branch name, and `execNode` (`operator.admin`) binds session exec to a node host. The created worktree is echoed in the result and persisted on the session row (`worktree: { id, branch, repoRoot }`).
467+
- `sessions.groups.list`, `sessions.groups.put`, `sessions.groups.rename`, and `sessions.groups.delete` manage the gateway-owned custom session group catalog (names + display order). Membership stays on each session's `category` field; rename and delete update member sessions server-side.
467468
- `sessions.send` sends a message into an existing session.
468469
- `sessions.steer` is the interrupt-and-steer variant for an active session.
469470
- `sessions.abort` aborts active work for a session. Pass `key` plus optional `runId`, or `runId` alone for active runs the gateway can resolve to a session.

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,20 @@ import {
442442
SessionsDeleteParamsSchema,
443443
type SessionsDescribeParams,
444444
SessionsDescribeParamsSchema,
445+
type SessionGroup,
446+
SessionGroupSchema,
447+
type SessionsGroupsDeleteParams,
448+
SessionsGroupsDeleteParamsSchema,
449+
type SessionsGroupsListParams,
450+
SessionsGroupsListParamsSchema,
451+
type SessionsGroupsListResult,
452+
SessionsGroupsListResultSchema,
453+
type SessionsGroupsMutationResult,
454+
SessionsGroupsMutationResultSchema,
455+
type SessionsGroupsPutParams,
456+
SessionsGroupsPutParamsSchema,
457+
type SessionsGroupsRenameParams,
458+
SessionsGroupsRenameParamsSchema,
445459
type SessionsFilesGetParams,
446460
SessionsFilesGetParamsSchema,
447461
type SessionsFilesGetResult,
@@ -642,6 +656,12 @@ import {
642656
WorktreesGcParamsSchema,
643657
type WorktreesGcResult,
644658
WorktreesGcResultSchema,
659+
type WorktreesBranchesParams,
660+
WorktreesBranchesParamsSchema,
661+
type WorktreeBranch,
662+
WorktreeBranchSchema,
663+
type WorktreesBranchesResult,
664+
WorktreesBranchesResultSchema,
645665
} from "./schema.js";
646666

647667
/** Normalized validation error shape exposed by every protocol validator. */
@@ -734,6 +754,9 @@ export const validateWorktreesRestoreParams = lazyCompile<WorktreesRestoreParams
734754
WorktreesRestoreParamsSchema,
735755
);
736756
export const validateWorktreesGcParams = lazyCompile<WorktreesGcParams>(WorktreesGcParamsSchema);
757+
export const validateWorktreesBranchesParams = lazyCompile<WorktreesBranchesParams>(
758+
WorktreesBranchesParamsSchema,
759+
);
737760
export const validateAgentsCreateParams = lazyCompile<AgentsCreateParams>(AgentsCreateParamsSchema);
738761
export const validateAgentsUpdateParams = lazyCompile<AgentsUpdateParams>(AgentsUpdateParamsSchema);
739762
export const validateAgentsDeleteParams = lazyCompile<AgentsDeleteParams>(AgentsDeleteParamsSchema);
@@ -854,6 +877,18 @@ export const validateSessionsResetParams =
854877
export const validateSessionsDeleteParams = lazyCompile<SessionsDeleteParams>(
855878
SessionsDeleteParamsSchema,
856879
);
880+
export const validateSessionsGroupsListParams = lazyCompile<SessionsGroupsListParams>(
881+
SessionsGroupsListParamsSchema,
882+
);
883+
export const validateSessionsGroupsPutParams = lazyCompile<SessionsGroupsPutParams>(
884+
SessionsGroupsPutParamsSchema,
885+
);
886+
export const validateSessionsGroupsRenameParams = lazyCompile<SessionsGroupsRenameParams>(
887+
SessionsGroupsRenameParamsSchema,
888+
);
889+
export const validateSessionsGroupsDeleteParams = lazyCompile<SessionsGroupsDeleteParams>(
890+
SessionsGroupsDeleteParamsSchema,
891+
);
857892
export const validateSessionsCompactParams = lazyCompile<SessionsCompactParams>(
858893
SessionsCompactParamsSchema,
859894
);
@@ -1309,6 +1344,13 @@ export {
13091344
SessionsPluginPatchParamsSchema,
13101345
SessionsResetParamsSchema,
13111346
SessionsDeleteParamsSchema,
1347+
SessionGroupSchema,
1348+
SessionsGroupsListParamsSchema,
1349+
SessionsGroupsListResultSchema,
1350+
SessionsGroupsPutParamsSchema,
1351+
SessionsGroupsRenameParamsSchema,
1352+
SessionsGroupsDeleteParamsSchema,
1353+
SessionsGroupsMutationResultSchema,
13121354
SessionsCompactParamsSchema,
13131355
SessionsUsageParamsSchema,
13141356
ArtifactSummarySchema,
@@ -1519,6 +1561,9 @@ export {
15191561
WorktreesRestoreParamsSchema,
15201562
WorktreesGcParamsSchema,
15211563
WorktreesGcResultSchema,
1564+
WorktreesBranchesParamsSchema,
1565+
WorktreeBranchSchema,
1566+
WorktreesBranchesResultSchema,
15221567
ProtocolSchemas,
15231568
MIN_CLIENT_PROTOCOL_VERSION,
15241569
MIN_NODE_PROTOCOL_VERSION,
@@ -1813,6 +1858,16 @@ export type {
18131858
WorktreesRestoreParams,
18141859
WorktreesGcParams,
18151860
WorktreesGcResult,
1861+
WorktreesBranchesParams,
1862+
WorktreeBranch,
1863+
WorktreesBranchesResult,
1864+
SessionGroup,
1865+
SessionsGroupsListParams,
1866+
SessionsGroupsListResult,
1867+
SessionsGroupsPutParams,
1868+
SessionsGroupsRenameParams,
1869+
SessionsGroupsDeleteParams,
1870+
SessionsGroupsMutationResult,
18161871
};
18171872
function uniqueStrings(values: string[]): string[] {
18181873
return [...new Set(values)];

0 commit comments

Comments
 (0)