Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

fix(changes): Git removed under a repository view offers the install card

leen.zhou9ddcefe65bd7
diff --git a/packages/workspaces/src/brain/changes/git/changes-refs.ts b/packages/workspaces/src/brain/changes/git/changes-refs.ts
index 50023d8..ce6e492 100644
--- a/packages/workspaces/src/brain/changes/git/changes-refs.ts
+++ b/packages/workspaces/src/brain/changes/git/changes-refs.ts
@@ -5,7 +5,7 @@ import type { ChangesService } from "../changes-service.ts";
import { prepare } from "../write/changes-write.ts";
import { executeWrite } from "../write/changes-write-run.ts";
import { parseStatus } from "./changes-git.ts";
-import { probeRepo, read, repoOperation } from "./changes-repo.ts";
+import { notRepoError, probeRepo, read, repoOperation } from "./changes-repo.ts";
export async function listRefs(
service: ChangesService,
@@ -14,7 +14,7 @@ export async function listRefs(
) {
const scope = await service.scope(params, signal);
const probe = await probeRepo(service.reader, scope.root.path, service.platform, signal);
- if (probe.state !== "repo") throw changesError("changes_not_repo");
+ if (probe.state !== "repo") throw notRepoError(probe);
const result = await read(() =>
service.reader.run(
{ op: "refs", kind: params.kind, after: params.after },
@@ -53,7 +53,7 @@ export async function writeRef(
) {
const scope = await service.scope(params, signal);
const probe = await probeRepo(service.reader, scope.root.path, service.platform, signal);
- if (probe.state !== "repo") throw changesError("changes_not_repo");
+ if (probe.state !== "repo") throw notRepoError(probe);
if (service.platform === "win32" || !probe.info.layoutOk)
throw changesError("changes_ref_unavailable");
const prepared = await prepare(service, scope, signal);
diff --git a/packages/workspaces/src/brain/changes/git/changes-remotes.ts b/packages/workspaces/src/brain/changes/git/changes-remotes.ts
index 1cd3ec5..fa837eb 100644
--- a/packages/workspaces/src/brain/changes/git/changes-remotes.ts
+++ b/packages/workspaces/src/brain/changes/git/changes-remotes.ts
@@ -3,7 +3,7 @@ import type { MethodParams } from "@pibot/workspaces-contract/full";
import { type ChangesScope, changesError } from "../changes-scope.ts";
import type { ChangesService } from "../changes-service.ts";
import { parseStatus } from "./changes-git.ts";
-import { probeRepo, read } from "./changes-repo.ts";
+import { notRepoError, probeRepo, read } from "./changes-repo.ts";
export async function remoteSnapshot(
service: ChangesService,
@@ -11,7 +11,7 @@ export async function remoteSnapshot(
signal: AbortSignal,
) {
const probe = await probeRepo(service.reader, scope.root.path, service.platform, signal);
- if (probe.state !== "repo") throw changesError("changes_not_repo");
+ if (probe.state !== "repo") throw notRepoError(probe);
const run = (operation: Parameters<typeof service.reader.run>[0]) =>
read(() =>
service.reader.run(operation, { cwd: scope.root.path, signal, maxBytes: 256 * 1024 }),
diff --git a/packages/workspaces/src/brain/changes/git/changes-repo.ts b/packages/workspaces/src/brain/changes/git/changes-repo.ts
index d038336..dfd1ac1 100644
--- a/packages/workspaces/src/brain/changes/git/changes-repo.ts
+++ b/packages/workspaces/src/brain/changes/git/changes-repo.ts
@@ -25,6 +25,16 @@ export type RepoProbe =
| { state: "none"; reason: "not_repo" | "git_unavailable" };
type Platform = "darwin" | "linux" | "win32";
+/**
+ * The error for a write or read that needs a repository. Git missing is its own code, so the
+ * panel offers the install card instead of calling a known repository "not a repository".
+ */
+export function notRepoError(probe: Extract<RepoProbe, { state: "none" }>) {
+ return changesError(
+ probe.reason === "git_unavailable" ? "changes_git_unavailable" : "changes_not_repo",
+ );
+}
+
const emptyTrees: Record<string, string> = {
sha1: "4b825dc642cb6eb9a060e54bf8d69288fbee4904",
sha256: "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321",
diff --git a/packages/workspaces/src/brain/changes/review/changes-diff.ts b/packages/workspaces/src/brain/changes/review/changes-diff.ts
index 769da8b..3a9aa99 100644
--- a/packages/workspaces/src/brain/changes/review/changes-diff.ts
+++ b/packages/workspaces/src/brain/changes/review/changes-diff.ts
@@ -13,7 +13,7 @@ import {
parseNumstat,
parseStatus,
} from "../git/changes-git.ts";
-import { probeRepo, read } from "../git/changes-repo.ts";
+import { notRepoError, probeRepo, read } from "../git/changes-repo.ts";
import { turnPreviewable, visibleTurn } from "./changes-file.ts";
/** One file's diff text is capped; beyond it only the counts are returned. */
@@ -23,7 +23,7 @@ const TURN_COUNTED = 200;
export async function repo(service: ChangesService, scope: ChangesScope, signal: AbortSignal) {
const probe = await probeRepo(service.reader, scope.root.path, service.platform, signal);
- if (probe.state !== "repo") throw changesError("changes_not_repo");
+ if (probe.state !== "repo") throw notRepoError(probe);
return probe.info;
}
diff --git a/packages/workspaces/src/brain/changes/write/changes-write-run.ts b/packages/workspaces/src/brain/changes/write/changes-write-run.ts
index b8faf69..e231f6a 100644
--- a/packages/workspaces/src/brain/changes/write/changes-write-run.ts
+++ b/packages/workspaces/src/brain/changes/write/changes-write-run.ts
@@ -10,7 +10,7 @@ import {
} from "@pibot/execution";
import { type ChangesScope, changesError } from "../changes-scope.ts";
import type { ChangesService } from "../changes-service.ts";
-import { probeRepo } from "../git/changes-repo.ts";
+import { notRepoError, probeRepo } from "../git/changes-repo.ts";
export type WriteMode = "sandbox" | "direct";
@@ -39,7 +39,7 @@ async function run(
const root = scope.root.path;
const { deps } = service.local;
const probe = await probeRepo(service.reader, root, service.platform, signal);
- if (probe.state !== "repo") throw changesError("changes_not_repo");
+ if (probe.state !== "repo") throw notRepoError(probe);
if (probe.state === "repo" && !probe.info.layoutOk) {
const managed = service.managedLayout;
if (!managed || !(await managed.allows(scope, signal)))
@@ -99,7 +99,7 @@ export async function executeWrite(
signal: AbortSignal,
) {
const probe = await probeRepo(service.reader, scope.root.path, service.platform, signal);
- if (probe.state !== "repo") throw changesError("changes_not_repo");
+ if (probe.state !== "repo") throw notRepoError(probe);
if (probe.info.layoutOk) {
if (!service.userWriteLease) throw changesError("changes_busy");
return service.userWriteLease(scope, signal, (bound, assertAuthorized) =>
diff --git a/packages/workspaces/src/brain/changes/write/changes-write.ts b/packages/workspaces/src/brain/changes/write/changes-write.ts
index 12b1b4f..5dd5b12 100644
--- a/packages/workspaces/src/brain/changes/write/changes-write.ts
+++ b/packages/workspaces/src/brain/changes/write/changes-write.ts
@@ -7,7 +7,7 @@ import { resolveRunWorkplace } from "../../runtime.ts";
import { type ChangesScope, changesError, writeMode } from "../changes-scope.ts";
import type { ChangesService } from "../changes-service.ts";
import { parseDiff, parseStatus, type StatusEntry } from "../git/changes-git.ts";
-import { probeRepo, read, readIdentity, repoOperation } from "../git/changes-repo.ts";
+import { notRepoError, probeRepo, read, readIdentity, repoOperation } from "../git/changes-repo.ts";
import { executeWrite } from "./changes-write-run.ts";
/**
@@ -36,8 +36,12 @@ export function coAuthorTrailer(botName: string) {
export async function prepare(service: ChangesService, scope: ChangesScope, signal: AbortSignal) {
const root = scope.root;
if (root.role === "readonly") throw changesError("changes_readonly_root");
+ // Without Git the probe cannot recognise the repository either: asking first lets the panel
+ // offer the Git install card instead of calling a known repository "not a repository".
+ const program = await service.reader.program();
+ if (!program) throw changesError("changes_git_unavailable");
const probe = await probeRepo(service.reader, root.path, service.platform, signal);
- if (probe.state !== "repo") throw changesError("changes_not_repo");
+ if (probe.state !== "repo") throw notRepoError(probe);
// The repository's .git is above the folder, outside the write boundary.
if (probe.info.subdirectory) throw changesError("changes_repo_above_folder");
if (
@@ -64,8 +68,6 @@ export async function prepare(service: ChangesService, scope: ChangesScope, sign
)
throw changesError("changes_busy");
}
- const program = await service.reader.program();
- if (!program) throw changesError("changes_git_unavailable");
return { info: probe.info, program, mode };
}
diff --git a/packages/workspaces/src/ui/changes/history/GitRemoteActions.tsx b/packages/workspaces/src/ui/changes/history/GitRemoteActions.tsx
index 0e9574b..faff2a2 100644
--- a/packages/workspaces/src/ui/changes/history/GitRemoteActions.tsx
+++ b/packages/workspaces/src/ui/changes/history/GitRemoteActions.tsx
@@ -84,7 +84,7 @@ export function GitRemoteToolbar({
label={t(state.error)}
title={t(state.error)}
disabled={state.busy || state.loading}
- onClick={() => void state.reload()}
+ onClick={state.recheck}
>
<Icon name="warning" size={16} />
</IconButton>
@@ -105,11 +105,7 @@ export function GitRemoteToolbar({
{state.error && !state.dialog && (
<p className={styles.error} role="alert">
{t(state.error)}{" "}
- <Button
- variant="ghost"
- disabled={state.busy || state.loading}
- onClick={() => void state.reload()}
- >
+ <Button variant="ghost" disabled={state.busy || state.loading} onClick={state.recheck}>
{t("changes.remote.refresh")}
</Button>
</p>
diff --git a/packages/workspaces/src/ui/changes/history/use-git-remote.ts b/packages/workspaces/src/ui/changes/history/use-git-remote.ts
index 65a90a3..88eff5f 100644
--- a/packages/workspaces/src/ui/changes/history/use-git-remote.ts
+++ b/packages/workspaces/src/ui/changes/history/use-git-remote.ts
@@ -42,7 +42,8 @@ export function useGitRemote(target: ChangesTarget, status: ChangesStatus, refre
const key = JSON.stringify(target),
lifetime = useRef(new AbortController()),
lock = useRef(false),
- generation = useRef(0);
+ generation = useRef(0),
+ reprobed = useRef(false);
const [data, setData] = useState<RemoteSnapshot>(),
[loading, setLoading] = useState(true),
[busy, setBusy] = useState(false),
@@ -73,8 +74,17 @@ export function useGitRemote(target: ChangesTarget, status: ChangesStatus, refre
if (generation.current === current && !owner.signal.aborted) setData(value);
})
.catch((failure) => {
- if (!owner.signal.aborted && generation.current === current)
- setError(remoteErrorKey(failure));
+ if (!owner.signal.aborted && generation.current === current) {
+ const key = remoteErrorKey(failure);
+ setError(key);
+ // Git went away under an open repository view: the panel's own status re-probes and
+ // shows the install card instead of a stale branch.
+ // Once per view, so a status that still says "repo" cannot loop.
+ if (key === "changes.error.changes_git_unavailable" && !reprobed.current) {
+ reprobed.current = true;
+ refresh?.();
+ }
+ }
})
.finally(() => {
if (!owner.signal.aborted && generation.current === current) setLoading(false);
@@ -108,6 +118,11 @@ export function useGitRemote(target: ChangesTarget, status: ChangesStatus, refre
dialog,
setDialog,
reload,
+ /** 「刷新状态」 re-reads the panel too: a failed remote read can mean Git itself went away. */
+ recheck: () => {
+ refresh?.();
+ void reload();
+ },
open: async (action: RemoteAction) => {
const snapshot = await reload();
if (snapshot && !lifetime.current.signal.aborted)
diff --git a/packages/workspaces/src/ui/changes/use-changes-pane.ts b/packages/workspaces/src/ui/changes/use-changes-pane.ts
index be46842..fe81713 100644
--- a/packages/workspaces/src/ui/changes/use-changes-pane.ts
+++ b/packages/workspaces/src/ui/changes/use-changes-pane.ts
@@ -298,11 +298,17 @@ export function useChangesPane(options: ChangesPaneOptions) {
};
const commitNow = async (message: string, author?: GitIdentity) => {
const paths = files.filter((entry) => !excluded.has(entry.key)).map((entry) => entry.key);
- const result = await peer.call(
- "changes.commit",
- { ...target, paths, message, ...(author ? { author } : {}) },
- { timeoutMs: 200_000 },
- );
+ const result = await peer
+ .call(
+ "changes.commit",
+ { ...target, paths, message, ...(author ? { author } : {}) },
+ { timeoutMs: 200_000 },
+ )
+ .catch((error: unknown) => {
+ // Git removed under an open view: re-probe so the panel switches to the install card.
+ if (changesErrorKey(error) === "changes.error.changes_git_unavailable") source?.refresh();
+ throw error;
+ });
setCommitted({ oid: result.commit, subject: message.split("\n")[0] ?? "" });
setExcluded(new Set());
setSelected(undefined);