Skip to content

Commit 6b470b1

Browse files
authored
fix(ui): thread-changes button leads to a dead end when the workspace is not a git checkout (#112914)
* fix(ui): disable the thread-changes button when the workspace is not a git checkout * chore(protocol): regenerate Swift gateway models for sessions.files.list gitCheckout
1 parent a542f01 commit 6b470b1

7 files changed

Lines changed: 98 additions & 9 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6085,24 +6085,28 @@ public struct SessionsFilesListParams: Codable, Sendable {
60856085
public struct SessionsFilesListResult: Codable, Sendable {
60866086
public let sessionkey: String
60876087
public let root: String?
6088+
public let gitcheckout: Bool?
60886089
public let files: [SessionFileEntry]
60896090
public let browser: SessionFileBrowserResult?
60906091

60916092
public init(
60926093
sessionkey: String,
60936094
root: String? = nil,
6095+
gitcheckout: Bool? = nil,
60946096
files: [SessionFileEntry],
60956097
browser: SessionFileBrowserResult? = nil)
60966098
{
60976099
self.sessionkey = sessionkey
60986100
self.root = root
6101+
self.gitcheckout = gitcheckout
60996102
self.files = files
61006103
self.browser = browser
61016104
}
61026105

61036106
private enum CodingKeys: String, CodingKey {
61046107
case sessionkey = "sessionKey"
61056108
case root
6109+
case gitcheckout = "gitCheckout"
61066110
case files
61076111
case browser
61086112
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ export const SessionsFilesListParamsSchema = closedObject({
188188
export const SessionsFilesListResultSchema = closedObject({
189189
sessionKey: NonEmptyString,
190190
root: Type.Optional(NonEmptyString),
191+
/** Whether the session workspace directory is inside a git checkout; absent when the workspace root is unknown or the gateway predates the field. */
192+
gitCheckout: Type.Optional(Type.Boolean()),
191193
files: Type.Array(SessionFileEntrySchema),
192194
browser: Type.Optional(SessionFileBrowserResultSchema),
193195
});

‎src/gateway/server-methods/sessions-files.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { execFileSync } from "node:child_process";
12
// Session file method tests cover transcript-linked files plus the workspace browser.
23
import { createHash } from "node:crypto";
34
import fs from "node:fs";
@@ -163,6 +164,7 @@ describe("sessions.files RPC handlers", () => {
163164
);
164165

165166
expect(payload.root).toBe(workspaceRoot);
167+
expect(payload.gitCheckout).toBe(false);
166168
expect(payload.files.map((file: Record<string, unknown>) => [file.path, file.kind])).toEqual([
167169
["package.json", "modified"],
168170
["ui/chat.ts", "modified"],
@@ -180,6 +182,11 @@ describe("sessions.files RPC handlers", () => {
180182
["ui", "directory", "modified"],
181183
["package.json", "file", "modified"],
182184
]);
185+
execFileSync("git", ["-C", workspaceRoot, "init", "-q", "-b", "main"]);
186+
const gitPayload = expectOkPayload(
187+
await invokeSessionFilesHandler("sessions.files.list", { sessionKey: "agent:main:main" }),
188+
);
189+
expect(gitPayload.gitCheckout).toBe(true);
183190
});
184191

185192
it("reveals the same workspace root returned by sessions.files.list", async () => {

‎src/gateway/server-methods/sessions-files.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "../../../packages/gateway-protocol/src/index.js";
2020
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../../agents/agent-scope.js";
2121
import { resolveToCwd as resolveSessionToolPathToCwd } from "../../agents/sessions/tools/path-utils.js";
22+
import { runGit } from "../../agents/worktrees/git.js";
2223
import { FsSafeError } from "../../infra/fs-safe.js";
2324
import { normalizeAgentId, parseAgentSessionKey } from "../../routing/session-key.js";
2425
import { visitSessionMessagesAsync } from "../session-transcript-readers.js";
@@ -59,6 +60,7 @@ type TouchedFile = {
5960
type LoadedSessionFiles = {
6061
root?: string;
6162
fileRoot?: string;
63+
diffCwd?: string;
6264
files: TouchedFile[];
6365
};
6466

@@ -359,15 +361,21 @@ function loadSessionFileRoot(params: { sessionKey: string; agentId?: string }) {
359361
resolveDefaultAgentId(loaded.cfg),
360362
);
361363
const spawnedCwd = normalizePathValue(loaded.entry.spawnedCwd);
362-
const root =
363-
normalizePathValue(loaded.entry.spawnedWorkspaceDir) ??
364-
spawnedCwd ??
365-
normalizePathValue(resolveAgentWorkspaceDir(loaded.cfg, agentId));
364+
const spawnedWorkspaceDir = normalizePathValue(loaded.entry.spawnedWorkspaceDir);
365+
const configuredWorkspaceDir =
366+
spawnedCwd || spawnedWorkspaceDir
367+
? undefined
368+
: normalizePathValue(resolveAgentWorkspaceDir(loaded.cfg, agentId));
369+
// Keep this cwd precedence aligned with sessions.diff so the advertised
370+
// checkout state cannot disagree with the panel's fallback result.
371+
const diffCwd = spawnedCwd ?? spawnedWorkspaceDir ?? configuredWorkspaceDir;
372+
const root = spawnedWorkspaceDir ?? spawnedCwd ?? configuredWorkspaceDir;
366373
return {
367374
...loaded,
368375
agentId,
369376
root,
370377
fileRoot: resolveFileRoot({ root, spawnedCwd }),
378+
diffCwd,
371379
};
372380
}
373381

@@ -542,6 +550,7 @@ async function loadSessionFiles(params: {
542550
return {
543551
root: loaded.root,
544552
fileRoot: loaded.fileRoot,
553+
diffCwd: loaded.diffCwd,
545554
files: [...files.values()].toSorted((a, b) => {
546555
if (a.kind !== b.kind) {
547556
return a.kind === "modified" ? -1 : 1;
@@ -556,9 +565,23 @@ async function buildListResult(params: {
556565
agentId?: string;
557566
path?: string;
558567
search?: string;
559-
}): Promise<{ root?: string; files: SessionFileEntry[]; browser?: SessionFileBrowserResult }> {
568+
}): Promise<{
569+
root?: string;
570+
gitCheckout?: boolean;
571+
files: SessionFileEntry[];
572+
browser?: SessionFileBrowserResult;
573+
}> {
560574
const loaded = await loadSessionFiles(params);
561575
const root = loaded.root;
576+
let gitCheckout: boolean | undefined;
577+
if (loaded.diffCwd) {
578+
try {
579+
const result = await runGit(loaded.diffCwd, ["rev-parse", "--show-toplevel"]);
580+
gitCheckout = result.code === 0 && Boolean(result.stdout.trim());
581+
} catch {
582+
gitCheckout = false;
583+
}
584+
}
562585
const workspaceFiles = root
563586
? loaded.files.filter((file) =>
564587
Boolean(resolveTouchedFilePath({ root, fileRoot: loaded.fileRoot, filePath: file.path })),
@@ -576,6 +599,7 @@ async function buildListResult(params: {
576599
});
577600
return {
578601
...(root ? { root } : {}),
602+
...(gitCheckout === undefined ? {} : { gitCheckout }),
579603
files,
580604
...(browser ? { browser } : {}),
581605
};

‎ui/src/api/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ type SessionWorkspaceArtifactEntry = {
431431
export type SessionWorkspaceListResult = {
432432
sessionKey: string;
433433
root?: string;
434+
gitCheckout?: boolean;
434435
files: SessionWorkspaceFileEntry[];
435436
browser?: SessionWorkspaceBrowserResult;
436437
artifacts?: SessionWorkspaceArtifactEntry[];

‎ui/src/e2e/chat-session-diff.e2e.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,46 @@ describeControlUiE2e("session diff panel", () => {
163163
await expect.poll(() => modified.locator(".chat-diff").count()).toBe(1);
164164
});
165165

166-
it("shows the not-a-git-checkout notice", async () => {
166+
it("disables the diff toggle for a workspace outside a git checkout", async () => {
167+
const context = await newBrowserContext();
168+
const page = await context.newPage();
169+
await installMockGateway(page, {
170+
featureMethods: ["chat.metadata", "chat.startup", "sessions.diff"],
171+
methodResponses: {
172+
"sessions.files.list": {
173+
sessionKey: "main",
174+
root: "/tmp/plain-workspace",
175+
gitCheckout: false,
176+
files: [],
177+
browser: { path: "", entries: [] },
178+
},
179+
"sessions.diff": {
180+
sessionKey: "main",
181+
files: [],
182+
additions: 0,
183+
deletions: 0,
184+
unavailableReason: "not_git",
185+
},
186+
},
187+
});
188+
await page.goto(`${server.baseUrl}chat`);
189+
190+
const toggle = page.locator(".chat-session-diff-toggle").first();
191+
await expect.poll(() => toggle.isDisabled()).toBe(true);
192+
await expect.poll(() => toggle.getAttribute("aria-label")).toBe("Show thread changes");
193+
await expect
194+
.poll(() =>
195+
toggle.evaluate(
196+
(element) =>
197+
(element.closest("openclaw-tooltip") as (HTMLElement & { content?: string }) | null)
198+
?.content,
199+
),
200+
)
201+
.toBe("This thread's workspace is not a git checkout.");
202+
await expect.poll(() => page.locator(".session-diff").count()).toBe(0);
203+
});
204+
205+
it("keeps the panel fallback for gateways that omit checkout capability", async () => {
167206
const context = await newBrowserContext();
168207
const page = await context.newPage();
169208
await installMockGateway(page, {

‎ui/src/pages/chat/components/chat-session-workspace.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export type SessionWorkspaceProps = {
6262
onToggleBrowser?: () => void;
6363
/** Opens the session diff panel; absent when the gateway lacks sessions.diff. */
6464
onOpenDiff?: () => void;
65+
diffNotGit?: boolean;
6566
};
6667

6768
type SessionWorkspaceState = {
@@ -288,6 +289,7 @@ function loadWorkspace(
288289
current.list = {
289290
sessionKey,
290291
...(files?.root ? { root: files.root } : {}),
292+
...(typeof files?.gitCheckout === "boolean" ? { gitCheckout: files.gitCheckout } : {}),
291293
files: fileItems,
292294
...(files?.browser ? { browser: files.browser } : {}),
293295
artifacts: artifactItems,
@@ -651,7 +653,9 @@ export function createSessionWorkspaceProps(
651653
state.sessionWorkspaceDraftScope = options?.draftScope;
652654
const workspace = getWorkspaceState(state);
653655
if (
654-
!workspace.collapsed &&
656+
// The collapsed header still renders the diff action, so load its checkout
657+
// capability eagerly instead of waiting for the file rail to open.
658+
(!workspace.collapsed || isGatewayMethodAdvertised(state, "sessions.diff") === true) &&
655659
state.connected &&
656660
state.agentsList &&
657661
!workspace.loading &&
@@ -716,6 +720,7 @@ export function createSessionWorkspaceProps(
716720
window.dispatchEvent(new CustomEvent(BROWSER_PANEL_TOGGLE_EVENT, {}));
717721
}
718722
: undefined,
723+
diffNotGit: workspace.list?.gitCheckout === false,
719724
onOpenDiff:
720725
isGatewayMethodAdvertised(state, "sessions.diff") === true && state.client
721726
? () => state.handleOpenSidebar(buildSessionDiffSidebarContent(state))
@@ -822,12 +827,14 @@ export function renderSessionDiffToggle(
822827
return nothing;
823828
}
824829
const label = t("chat.sessionDiff.show");
830+
const tooltip = sessionWorkspace.diffNotGit ? t("chat.sessionDiff.notGit") : label;
825831
return html`
826-
<openclaw-tooltip .content=${label}>
832+
<openclaw-tooltip .content=${tooltip}>
827833
<button
828834
class="btn btn--ghost btn--icon chat-icon-btn chat-session-diff-toggle"
829835
type="button"
830836
aria-label=${label}
837+
?disabled=${sessionWorkspace.diffNotGit === true}
831838
@click=${sessionWorkspace.onOpenDiff}
832839
>
833840
${icons.gitBranch}
@@ -877,11 +884,16 @@ export function renderSessionWorkspaceRail(
877884
: nothing;
878885
const diffButton = sessionWorkspace.onOpenDiff
879886
? html`
880-
<openclaw-tooltip .content=${t("chat.sessionDiff.show")}>
887+
<openclaw-tooltip
888+
.content=${sessionWorkspace.diffNotGit
889+
? t("chat.sessionDiff.notGit")
890+
: t("chat.sessionDiff.show")}
891+
>
881892
<button
882893
type="button"
883894
class="chat-workspace-rail__terminal chat-session-diff-toggle"
884895
aria-label=${t("chat.sessionDiff.show")}
896+
?disabled=${sessionWorkspace.diffNotGit === true}
885897
@click=${sessionWorkspace.onOpenDiff}
886898
>
887899
${icons.gitBranch}

0 commit comments

Comments
 (0)