Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(update): handle diverged checkout explicitly

rahmanef63a1a7e1c0e911
diff --git a/app/api/v1/sys/update/route.test.ts b/app/api/v1/sys/update/route.test.ts
index b5f44ecd..496f3bd1 100644
--- a/app/api/v1/sys/update/route.test.ts
+++ b/app/api/v1/sys/update/route.test.ts
@@ -37,6 +37,7 @@ const status = (over: Record<string, unknown> = {}) => ({
currentSubject: "feat: x",
buildSha: "abc1234",
pendingBuild: false,
+ ahead: 0,
behind: 2,
commits: [],
dirty: false,
diff --git a/frontend/slices/os-settings/components/update-section.tsx b/frontend/slices/os-settings/components/update-section.tsx
index 079920d4..7d8c298d 100644
--- a/frontend/slices/os-settings/components/update-section.tsx
+++ b/frontend/slices/os-settings/components/update-section.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useCallback, useEffect, useState } from "react";
+import { useCallback, useEffect, useRef, useState } from "react";
import { ArrowDownToLine, BookOpen, CheckCircle2, RefreshCw, Search } from "lucide-react";
import { Button } from "@/components/ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
@@ -34,6 +34,7 @@ export function UpdateSection() {
// "An update ran in THIS tab (or was running when we looked)" — the outcome below
// is derived from it rather than stored, so nothing has to be reset on the next run.
const [sawRunning, setSawRunning] = useState(false);
+ const postRunRemoteCheck = useRef(false);
const refresh = useCallback(async (check: boolean) => {
// A poll that lands while the service is restarting simply fails; keeping the
@@ -76,9 +77,16 @@ export function UpdateSection() {
return () => clearInterval(id);
}, [info?.running, refresh]);
+ useEffect(() => {
+ if (!sawRunning || info?.running !== false || postRunRemoteCheck.current) return;
+ postRunRemoteCheck.current = true;
+ void refresh(true);
+ }, [sawRunning, info?.running, refresh]);
+
const start = async (rebuildOnly: boolean) => {
setBusy(true);
setError(null);
+ postRunRemoteCheck.current = false;
setSawRunning(true);
try {
const res = await fetch("/api/v1/sys/update", {
@@ -97,7 +105,10 @@ export function UpdateSection() {
};
if (IS_DEMO || (!info && !checking)) return null;
+ const ahead = info?.ahead ?? 0;
const behind = info?.behind ?? 0;
+ const diverged = ahead > 0 && behind > 0;
+ const localAhead = ahead > 0 && behind === 0;
const running = info?.running ?? false;
const pending = (info?.pendingBuild ?? false) && behind === 0;
// Derived, never stored: an update we watched, that is no longer running, either
@@ -119,9 +130,13 @@ export function UpdateSection() {
<span className="font-medium text-foreground">
{checking
? "Checking for updates…"
- : behind > 0
- ? `${behind} update${behind > 1 ? "s" : ""} available`
- : pending
+ : diverged
+ ? "Update blocked — local and remote diverged"
+ : localAhead
+ ? `${ahead} local commit${ahead > 1 ? "s" : ""} not on origin/main`
+ : behind > 0
+ ? `${behind} update${behind > 1 ? "s" : ""} available`
+ : pending
? "A build is pending"
: "Up to date"}
</span>
@@ -138,6 +153,14 @@ export function UpdateSection() {
</p>
)}
{info?.currentSubject && <p className="line-clamp-2 text-[11px] text-muted-foreground">{info.currentSubject}</p>}
+ {diverged && (
+ <p className="text-[11px] text-destructive-text">
+ Local main is ahead {ahead} and behind {behind}. MSO will not overwrite either side automatically; reconcile or preserve the local commits first.
+ </p>
+ )}
+ {localAhead && (
+ <p className="text-[11px] text-amber-500">Local main has {ahead} unpushed commit{ahead > 1 ? "s" : ""}; normal update is blocked until it is pushed or reconciled.</p>
+ )}
{info && info.supported !== false && !info.remoteChecked && !running && (
<p className="text-[11px] text-amber-500">
Could not reach the remote — this is what was last fetched, not necessarily what is on main now.
@@ -169,7 +192,7 @@ export function UpdateSection() {
)}
</SettingsBlock>
- {behind > 0 && info?.supported !== false && (
+ {behind > 0 && ahead === 0 && info?.supported !== false && (
<SettingsActionRow
label={running ? "Updating…" : `Update to ${info?.commits[0]?.sha ?? "latest"} and restart`}
icon={<ArrowDownToLine />}
diff --git a/lib/host/self-update.test.ts b/lib/host/self-update.test.ts
index 76d6bc6f..a8d64b7e 100644
--- a/lib/host/self-update.test.ts
+++ b/lib/host/self-update.test.ts
@@ -16,6 +16,7 @@ const status = (over: Partial<Parameters<typeof blockingReason>[0]> = {}) => ({
currentSubject: "feat: something",
buildSha: "abc1234",
pendingBuild: false,
+ ahead: 0,
behind: 3,
commits: [],
dirty: false,
@@ -79,6 +80,12 @@ describe("blockingReason", () => {
expect(blockingReason(status(), false)).toBeNull();
});
+ it("refuses normal updates when local main is ahead or diverged, but still permits an explicit rebuild", () => {
+ expect(blockingReason(status({ ahead: 2, behind: 0 }), false)).toContain("ahead of origin/main");
+ expect(blockingReason(status({ ahead: 2, behind: 114 }), false)).toContain("diverged");
+ expect(blockingReason(status({ ahead: 2, behind: 114 }), true)).toBeNull();
+ });
+
it("refuses when there is nothing to pull, unless a rebuild was asked for", () => {
expect(blockingReason(status({ behind: 0 }), false)).toBe("already up to date");
expect(blockingReason(status({ behind: 0 }), true)).toBeNull();
diff --git a/lib/host/self-update.ts b/lib/host/self-update.ts
index 3752b720..da8b12dd 100644
--- a/lib/host/self-update.ts
+++ b/lib/host/self-update.ts
@@ -47,6 +47,7 @@ export interface UpdateStatus {
* pulled without rebuilding — the state the panel calls "a build is pending". */
buildSha: string;
pendingBuild: boolean;
+ ahead: number;
behind: number;
commits: UpdateCommit[];
/** Uncommitted work in the checkout: a `--ff-only` merge would fail, so refuse early. */
@@ -139,6 +140,11 @@ export function blockingReason(status: UpdateStatus, rebuildOnly: boolean): stri
if (status.dirty) {
return "the checkout has uncommitted changes — commit or stash them on the host first, a fast-forward would refuse anyway";
}
+ if (!rebuildOnly && status.ahead > 0) {
+ return status.behind > 0
+ ? `local main has diverged from origin/main (ahead ${status.ahead}, behind ${status.behind}); reconcile it before updating`
+ : `local main is ahead of origin/main by ${status.ahead} commit(s); push or reconcile it before updating`;
+ }
// `pendingBuild` is a reason to run even with nothing to pull: the checkout
// already holds code the running process was not built from, and refusing with
// "already up to date" would be describing the wrong thing as up to date.
@@ -179,6 +185,7 @@ export async function getUpdateStatus(fetchRemote = true): Promise<UpdateStatus>
currentSubject: "",
buildSha,
pendingBuild: false,
+ ahead: 0,
behind: 0,
commits: [],
dirty: false,
@@ -247,10 +254,13 @@ export async function getUpdateStatus(fetchRemote = true): Promise<UpdateStatus>
remoteChecked = fetched.code === 0;
}
- const [count, commitLog] = await Promise.all([
- git(["rev-list", "--count", "HEAD..origin/main"]),
+ const [relationCount, commitLog] = await Promise.all([
+ git(["rev-list", "--left-right", "--count", "HEAD...origin/main"]),
git(["log", "--format=%h%x1f%s%x1f%cs", `-${MAX_COMMITS}`, "HEAD..origin/main"]),
]);
+ const [aheadRaw = "0", behindRaw = "0"] = relationCount.stdout.trim().split(/\s+/);
+ const ahead = relationCount.code === 0 ? Number(aheadRaw) || 0 : 0;
+ const behind = relationCount.code === 0 ? Number(behindRaw) || 0 : 0;
const current = head.stdout.trim() || "unknown";
return {
@@ -261,7 +271,8 @@ export async function getUpdateStatus(fetchRemote = true): Promise<UpdateStatus>
// Only when we know both: a build with no sha baked in (an archive export, a
// dev server) must not be reported as permanently stale.
pendingBuild: Boolean(buildSha) && current !== "unknown" && buildSha !== current,
- behind: count.code === 0 ? Number(count.stdout.trim()) || 0 : 0,
+ ahead,
+ behind,
commits: commitLog.code === 0 ? parseCommits(commitLog.stdout) : [],
dirty: status.stdout.trim().length > 0,
running,