Skip to content

Commit f4e6252

Browse files
authored
fix(update): preserve upstream after pinned dev updates (#121328)
1 parent 2f97e8c commit f4e6252

20 files changed

Lines changed: 938 additions & 154 deletions

‎src/cli/update-cli.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { PluginInstallRecord } from "../config/types.plugins.js";
1414
import { GATEWAY_SERVICE_RUNTIME_PID_ENV } from "../daemon/constants.js";
1515
import type { ClawHubRiskAcknowledgementRequest } from "../infra/clawhub-install-trust.js";
1616
import { isBetaTag } from "../infra/update-channels.js";
17+
import { applyDevUpdateTargetEnv } from "../infra/update-dev-target.js";
1718
import {
1819
createDeferredConfiguredPluginRepairDoctorResult,
1920
UPDATE_POST_INSTALL_DOCTOR_ADVISORY_EXIT_CODE,
@@ -7449,6 +7450,87 @@ describe("update-cli", () => {
74497450
});
74507451
});
74517452

7453+
it.each([
7454+
{
7455+
name: "ref-only as detached",
7456+
env: { OPENCLAW_UPDATE_DEV_TARGET_REF: "frozen-sha" },
7457+
expected: { mode: "detached", ref: "frozen-sha" },
7458+
},
7459+
{
7460+
name: "versioned tracked target",
7461+
env: applyDevUpdateTargetEnv(
7462+
{},
7463+
{ mode: "tracked", upstreamRef: "origin/main", upstreamSha: "frozen-sha" },
7464+
),
7465+
expected: { mode: "tracked", upstreamRef: "origin/main", upstreamSha: "frozen-sha" },
7466+
},
7467+
])("maps the internal dev target environment $name", async ({ env, expected }) => {
7468+
await withEnvAsync(env, async () => {
7469+
await updateCommand({ channel: "dev", yes: true, restart: false });
7470+
});
7471+
7472+
expect(vi.mocked(runGatewayUpdate).mock.calls[0]?.[0]).toEqual(
7473+
expect.objectContaining({ devTarget: expected }),
7474+
);
7475+
});
7476+
7477+
it.each([
7478+
["malformed", "openclaw-dev-target:v1:not+base64url"],
7479+
["unknown version", "openclaw-dev-target:v2:hostile-ref"],
7480+
["unknown namespace", "other-dev-target:v1:hostile-ref"],
7481+
])("rejects a %s tracked dev target before update side effects", async (_name, value) => {
7482+
await withEnvAsync({ OPENCLAW_UPDATE_DEV_TARGET_REF: value }, async () => {
7483+
await updateCommand({ channel: "dev", yes: true, restart: false });
7484+
});
7485+
7486+
expect(defaultRuntime.error).toHaveBeenCalledWith(
7487+
"Invalid internal OPENCLAW_UPDATE_DEV_TARGET_REF contract; expected a plain Git ref or a supported tracked-target encoding.",
7488+
);
7489+
expect(defaultRuntime.error).toHaveBeenCalledTimes(1);
7490+
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
7491+
expectNoSideEffects(
7492+
cleanupStaleManagedServiceUpdateHandoffs,
7493+
runGatewayUpdate,
7494+
launchdUpdateCleanupMocks.disableCurrentOpenClawUpdateLaunchdJob,
7495+
);
7496+
});
7497+
7498+
it("rejects a malformed inferred dev target before running the update", async () => {
7499+
await withEnvAsync(
7500+
{ OPENCLAW_UPDATE_DEV_TARGET_REF: "openclaw-dev-target:v1:not+base64url" },
7501+
async () => {
7502+
await updateCommand({ yes: true, restart: false });
7503+
},
7504+
);
7505+
7506+
expect(defaultRuntime.error).toHaveBeenCalledWith(
7507+
"Invalid internal OPENCLAW_UPDATE_DEV_TARGET_REF contract; expected a plain Git ref or a supported tracked-target encoding.",
7508+
);
7509+
expect(defaultRuntime.error).toHaveBeenCalledTimes(1);
7510+
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
7511+
expect(runGatewayUpdate).not.toHaveBeenCalled();
7512+
expect(launchdUpdateCleanupMocks.disableCurrentOpenClawUpdateLaunchdJob).not.toHaveBeenCalled();
7513+
});
7514+
7515+
it("ignores a malformed dev target for a stable package update", async () => {
7516+
mockPackageInstallStatus(createCaseDir("openclaw-stable-update"));
7517+
mockCurrentProcessFreshDoctor();
7518+
7519+
await withEnvAsync(
7520+
{ OPENCLAW_UPDATE_DEV_TARGET_REF: "openclaw-dev-target:v1:not+base64url" },
7521+
async () => {
7522+
await updateCommand({ channel: "stable", yes: true, restart: false });
7523+
},
7524+
);
7525+
7526+
expect(defaultRuntime.error).not.toHaveBeenCalledWith(
7527+
expect.stringContaining("OPENCLAW_UPDATE_DEV_TARGET_REF"),
7528+
);
7529+
expect(defaultRuntime.exit).not.toHaveBeenCalledWith(1);
7530+
expect(packageInstallCommandCall()).toBeDefined();
7531+
expect(runGatewayUpdate).not.toHaveBeenCalled();
7532+
});
7533+
74527534
it("uses ~/openclaw as the default dev checkout directory", async () => {
74537535
const homedirSpy = vi.spyOn(os, "homedir").mockReturnValue("/tmp/oc-home");
74547536
try {

‎src/cli/update-cli/update-command-execution.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DevUpdateTarget } from "../../infra/update-dev-target.js";
12
import type { ResolvedGlobalInstallTarget } from "../../infra/update-global.js";
23
import type { UpdateRunResult } from "../../infra/update-runner.js";
34
import { defaultRuntime } from "../../runtime.js";
@@ -53,7 +54,7 @@ export async function executeMutableUpdate(params: {
5354
showProgress: boolean;
5455
opts: UpdateCommandOptions;
5556
shouldRestart: boolean;
56-
devTargetRef?: string;
57+
devTarget?: DevUpdateTarget;
5758
packageInstallSpec: string | null;
5859
packageInstallEnv?: NodeJS.ProcessEnv;
5960
packageInstallTarget?: ResolvedGlobalInstallTarget;
@@ -223,7 +224,7 @@ export async function executeMutableUpdate(params: {
223224
showProgress: params.showProgress,
224225
opts: params.opts,
225226
stop: params.stop,
226-
devTargetRef: params.devTargetRef,
227+
devTarget: params.devTarget,
227228
beforeGitMutation:
228229
params.updateInstallKind === "git"
229230
? createBeforeGitMutation({

‎src/cli/update-cli/update-command-git.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { UpdateChannel } from "../../infra/update-channels.js";
2+
import type { DevUpdateTarget } from "../../infra/update-dev-target.js";
23
import {
34
createGlobalInstallEnv,
45
globalInstallArgs,
@@ -127,7 +128,7 @@ export async function runGitUpdate(params: {
127128
showProgress: boolean;
128129
opts: UpdateCommandOptions;
129130
stop: () => void;
130-
devTargetRef?: string;
131+
devTarget?: DevUpdateTarget;
131132
beforeGitMutation?: BeforeGitMutation;
132133
allowGatewayServiceRepair: boolean;
133134
allowGatewayActivation: boolean;
@@ -167,7 +168,7 @@ export async function runGitUpdate(params: {
167168
progress: params.progress,
168169
channel: params.channel,
169170
tag: params.tag,
170-
devTargetRef: params.devTargetRef,
171+
devTarget: params.devTarget,
171172
deferConfiguredPluginInstallRepair: true,
172173
allowGatewayServiceRepair: params.allowGatewayServiceRepair,
173174
allowGatewayActivation: params.allowGatewayActivation,

‎src/cli/update-cli/update-command.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ import {
2727
resolveNpmChannelTag,
2828
} from "../../infra/update-check.js";
2929
import { readControlPlaneUpdateSentinelMeta } from "../../infra/update-control-plane-sentinel.js";
30+
import {
31+
parseDevUpdateTargetEnv,
32+
type DevUpdateTarget,
33+
UPDATE_DEV_TARGET_REF_ENV,
34+
} from "../../infra/update-dev-target.js";
3035
import {
3136
canResolveRegistryVersionForPackageTarget,
3237
createGlobalInstallEnv,
@@ -85,6 +90,18 @@ export { updateFinalizeCommand } from "./update-command-post-core.js";
8590
const CLI_NAME = resolveCliName();
8691
const DEFAULT_UPDATE_STEP_TIMEOUT_MS = 30 * 60_000;
8792

93+
function readDevUpdateTargetOrExit(): { ok: true; target?: DevUpdateTarget } | { ok: false } {
94+
const parsed = parseDevUpdateTargetEnv(process.env);
95+
if (parsed.status === "invalid") {
96+
defaultRuntime.error(
97+
`Invalid internal ${UPDATE_DEV_TARGET_REF_ENV} contract; expected a plain Git ref or a supported tracked-target encoding.`,
98+
);
99+
defaultRuntime.exit(1);
100+
return { ok: false };
101+
}
102+
return parsed.status === "valid" ? { ok: true, target: parsed.target } : { ok: true };
103+
}
104+
88105
async function withUpdateInProgressEnv<T>(run: () => Promise<T>): Promise<T> {
89106
const previousUpdateInProgress = process.env.OPENCLAW_UPDATE_IN_PROGRESS;
90107
process.env.OPENCLAW_UPDATE_IN_PROGRESS = "1";
@@ -134,6 +151,14 @@ async function updateCommandInternal(
134151
defaultRuntime.exit(1);
135152
return;
136153
}
154+
let devTarget: DevUpdateTarget | undefined;
155+
if (requestedChannel === "dev") {
156+
const resolvedDevTarget = readDevUpdateTargetOrExit();
157+
if (!resolvedDevTarget.ok) {
158+
return;
159+
}
160+
devTarget = resolvedDevTarget.target;
161+
}
137162

138163
if (!postCoreUpdateResume && opts.dryRun !== true && isGatewayExternallySupervised()) {
139164
defaultRuntime.error(formatExternalSupervisorUpdateRequired());
@@ -246,8 +271,13 @@ async function updateCommandInternal(
246271
currentVersion: VERSION,
247272
installKind: updateInstallKind,
248273
}).channel);
249-
const devTargetRef =
250-
channel === "dev" ? process.env.OPENCLAW_UPDATE_DEV_TARGET_REF?.trim() || undefined : undefined;
274+
if (channel === "dev" && requestedChannel !== "dev") {
275+
const resolvedDevTarget = readDevUpdateTargetOrExit();
276+
if (!resolvedDevTarget.ok) {
277+
return;
278+
}
279+
devTarget = resolvedDevTarget.target;
280+
}
251281

252282
const explicitTag = normalizeTag(opts.tag);
253283
if (channel === "extended-stable" && explicitTag) {
@@ -579,7 +609,7 @@ async function updateCommandInternal(
579609
showProgress,
580610
opts,
581611
shouldRestart,
582-
devTargetRef,
612+
devTarget,
583613
packageInstallSpec,
584614
packageInstallEnv,
585615
packageInstallTarget,

‎src/gateway/server-methods/update-run-campaign.test.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ type UpdateInstallSurface = Awaited<
3838
>;
3939
const resolveUpdateInstallSurfaceMock = vi.fn<() => Promise<UpdateInstallSurface>>();
4040
const detectRespawnSupervisorMock = vi.fn<() => RespawnSupervisor | null>();
41-
const startManagedServiceUpdateHandoffMock = vi.fn(async () => ({
41+
const startManagedServiceUpdateHandoffMock = vi.fn<
42+
typeof import("../../infra/update-managed-service-handoff.js").startManagedServiceUpdateHandoff
43+
>(async () => ({
4244
status: "started" as const,
4345
pid: 12345,
4446
command: "openclaw update --yes --timeout 1800",
@@ -323,7 +325,11 @@ describe("update.run campaign ownership", () => {
323325
expect(runGatewayUpdateMock).toHaveBeenCalledWith(
324326
expect.objectContaining({
325327
channel: "dev",
326-
devTargetRef: "frozen-upstream-sha",
328+
devTarget: {
329+
mode: "tracked",
330+
upstreamRef: "origin/main",
331+
upstreamSha: "frozen-upstream-sha",
332+
},
327333
}),
328334
);
329335
});
@@ -336,9 +342,11 @@ describe("update.run campaign ownership", () => {
336342

337343
expect(startManagedServiceUpdateHandoffMock).toHaveBeenCalledWith(
338344
expect.objectContaining({
339-
env: expect.objectContaining({
340-
OPENCLAW_UPDATE_DEV_TARGET_REF: "frozen-upstream-sha",
341-
}),
345+
devTarget: {
346+
mode: "tracked",
347+
upstreamRef: "origin/main",
348+
upstreamSha: "frozen-upstream-sha",
349+
},
342350
}),
343351
);
344352
});
@@ -350,7 +358,7 @@ describe("update.run campaign ownership", () => {
350358
await invokeUpdateRun();
351359

352360
expect(runGatewayUpdateMock).toHaveBeenCalledWith(
353-
expect.not.objectContaining({ devTargetRef: expect.anything() }),
361+
expect.not.objectContaining({ devTarget: expect.anything() }),
354362
);
355363
});
356364

@@ -385,7 +393,7 @@ describe("update.run campaign ownership", () => {
385393
expect(getCampaignStateMock).not.toHaveBeenCalled();
386394
expect(clearCampaignMock).not.toHaveBeenCalled();
387395
expect(runGatewayUpdateMock).toHaveBeenCalledWith(
388-
expect.not.objectContaining({ devTargetRef: expect.anything() }),
396+
expect.not.objectContaining({ devTarget: expect.anything() }),
389397
);
390398
});
391399

‎src/gateway/server-methods/update.ts‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "../../infra/update-channels.js";
3535
import { checkUpdateStatus } from "../../infra/update-check.js";
3636
import { CONTROL_PLANE_UPDATE_HANDOFF_STARTED_REASON } from "../../infra/update-control-plane-sentinel.js";
37+
import { devUpdateTargetFromGitCampaign } from "../../infra/update-dev-target.js";
3738
import { resolveUpdateInstallRoot } from "../../infra/update-install-root.js";
3839
import {
3940
buildManagedServiceHandoffUnavailableMessage,
@@ -254,9 +255,9 @@ export const updateHandlers: GatewayRequestHandlers = {
254255
}
255256
const adoptedCampaign = gatewayUpdateCampaign.adopt();
256257
const adoptedCampaignId = adoptedCampaign?.campaignId;
257-
const adoptedDevTargetRef =
258+
const adoptedDevTarget =
258259
adoptedCampaign?.target.kind === "git"
259-
? adoptedCampaign.target.upstreamSha.trim() || undefined
260+
? devUpdateTargetFromGitCampaign(adoptedCampaign.target)
260261
: undefined;
261262
const adoptedPackageTargetVersion =
262263
adoptedCampaign?.target.kind === "package"
@@ -409,14 +410,7 @@ export const updateHandlers: GatewayRequestHandlers = {
409410
restartDrainTimeoutMs: resolveGatewayRestartDeferralTimeoutMs(),
410411
...(handoffChannel ? { channel: handoffChannel } : {}),
411412
...(adoptedPackageTargetVersion ? { tag: adoptedPackageTargetVersion } : {}),
412-
...(adoptedDevTargetRef
413-
? {
414-
env: {
415-
...process.env,
416-
OPENCLAW_UPDATE_DEV_TARGET_REF: adoptedDevTargetRef,
417-
},
418-
}
419-
: {}),
413+
...(adoptedDevTarget ? { devTarget: adoptedDevTarget } : {}),
420414
restartDelayMs: managedRestartDelayMs,
421415
meta: sentinelMeta,
422416
handoffId,
@@ -529,7 +523,7 @@ export const updateHandlers: GatewayRequestHandlers = {
529523
? effectiveChannel
530524
: (configChannel ?? undefined),
531525
...(adoptedPackageTargetVersion ? { tag: adoptedPackageTargetVersion } : {}),
532-
...(adoptedDevTargetRef ? { devTargetRef: adoptedDevTargetRef } : {}),
526+
...(adoptedDevTarget ? { devTarget: adoptedDevTarget } : {}),
533527
allowGatewayServiceRepair: false,
534528
allowGatewayActivation: false,
535529
});

‎src/infra/restart-sentinel.test.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import {
5252
hasRestartSentinel,
5353
markUpdateRestartSentinelFailure,
5454
readRestartSentinel,
55-
readUpdateInstallReceipt,
55+
readSuccessfulGitUpdateReceipt,
5656
summarizeRestartSentinel,
5757
trimLogTail,
5858
writeRestartSentinel,
@@ -557,7 +557,11 @@ describe("restart sentinel", () => {
557557
mode: "git",
558558
root: installAlias,
559559
before: { sha: "aaaaaaaa" },
560-
after: { sha: "bbbbbbbb", version: "expected-version" },
560+
after: {
561+
sha: " bbbbbbbb ",
562+
upstreamRef: " origin/main ",
563+
version: "expected-version",
564+
},
561565
},
562566
});
563567

@@ -569,15 +573,11 @@ describe("restart sentinel", () => {
569573
);
570574
await clearRestartSentinel();
571575

572-
await expect(readUpdateInstallReceipt()).resolves.toMatchObject({
573-
kind: "update",
574-
status: "ok",
575-
ts,
576-
stats: {
577-
mode: "git",
578-
root: await fs.realpath(installRoot),
579-
after: { sha: "bbbbbbbb", version: "actual-version" },
580-
},
576+
await expect(readSuccessfulGitUpdateReceipt()).resolves.toEqual({
577+
root: await fs.realpath(installRoot),
578+
sha: "bbbbbbbb",
579+
upstreamRef: "origin/main",
580+
installedAtMs: ts,
581581
});
582582
});
583583
});
@@ -604,7 +604,7 @@ describe("restart sentinel", () => {
604604
process.cwd(),
605605
);
606606

607-
await expect(readUpdateInstallReceipt()).resolves.toBeNull();
607+
await expect(readSuccessfulGitUpdateReceipt()).resolves.toBeNull();
608608
});
609609
});
610610

@@ -634,7 +634,7 @@ describe("restart sentinel", () => {
634634
stats: { reason: "restart-revision-mismatch" },
635635
},
636636
});
637-
await expect(readUpdateInstallReceipt()).resolves.toBeNull();
637+
await expect(readSuccessfulGitUpdateReceipt()).resolves.toBeNull();
638638
});
639639
});
640640

@@ -670,7 +670,7 @@ describe("restart sentinel", () => {
670670
stats: { reason: "restart-root-mismatch" },
671671
},
672672
});
673-
await expect(readUpdateInstallReceipt()).resolves.toBeNull();
673+
await expect(readSuccessfulGitUpdateReceipt()).resolves.toBeNull();
674674
});
675675
});
676676
});

0 commit comments

Comments
 (0)