refactor: centralize presence routing and version precedence coverage (#19609)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 10d9df5263
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
Gustavo Madeira Santana
2026-02-18 00:02:51 -05:00
committed by GitHub
parent 5c69e625f5
commit 07fdceb5fd
12 changed files with 305 additions and 33 deletions

View File

@@ -1,8 +1,12 @@
import { randomUUID } from "node:crypto";
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { listSystemPresence, updateSystemPresence, upsertPresence } from "./system-presence.js";
describe("system-presence", () => {
afterEach(() => {
vi.useRealTimers();
});
it("dedupes entries across sources by case-insensitive instanceId key", () => {
const instanceIdUpper = `AaBb-${randomUUID()}`.toUpperCase();
const instanceIdLower = instanceIdUpper.toLowerCase();
@@ -56,4 +60,25 @@ describe("system-presence", () => {
expect(entry?.roles).toEqual(expect.arrayContaining(["operator", "node"]));
expect(entry?.scopes).toEqual(expect.arrayContaining(["operator.admin", "system.run"]));
});
it("prunes stale non-self entries after TTL", () => {
vi.useFakeTimers();
vi.setSystemTime(Date.now());
const deviceId = randomUUID();
upsertPresence(deviceId, {
deviceId,
host: "stale-host",
mode: "ui",
reason: "connect",
});
expect(listSystemPresence().some((entry) => entry.deviceId === deviceId)).toBe(true);
vi.advanceTimersByTime(5 * 60 * 1000 + 1);
const entries = listSystemPresence();
expect(entries.some((entry) => entry.deviceId === deviceId)).toBe(false);
expect(entries.some((entry) => entry.reason === "self")).toBe(true);
});
});

View File

@@ -1,6 +1,7 @@
import { spawnSync } from "node:child_process";
import os from "node:os";
import { pickPrimaryLanIPv4 } from "../gateway/net.js";
import { resolveRuntimeServiceVersion } from "../version.js";
export type SystemPresence = {
host?: string;
@@ -50,11 +51,7 @@ function resolvePrimaryIPv4(): string | undefined {
function initSelfPresence() {
const host = os.hostname();
const ip = resolvePrimaryIPv4() ?? undefined;
const version =
process.env.OPENCLAW_VERSION ??
process.env.OPENCLAW_SERVICE_VERSION ??
process.env.npm_package_version ??
"unknown";
const version = resolveRuntimeServiceVersion(process.env, "unknown");
const modelIdentifier = (() => {
const p = os.platform();
if (p === "darwin") {

View File

@@ -0,0 +1,60 @@
import { describe, expect, it, vi } from "vitest";
import { withEnvAsync } from "../test-utils/env.js";
async function withPresenceModule<T>(
env: Record<string, string | undefined>,
run: (module: typeof import("./system-presence.js")) => Promise<T> | T,
): Promise<T> {
return withEnvAsync(env, async () => {
vi.resetModules();
try {
const module = await import("./system-presence.js");
return await run(module);
} finally {
vi.resetModules();
}
});
}
describe("system-presence version fallback", () => {
it("uses OPENCLAW_SERVICE_VERSION when OPENCLAW_VERSION is not set", async () => {
await withPresenceModule(
{
OPENCLAW_SERVICE_VERSION: "2.4.6-service",
npm_package_version: "1.0.0-package",
},
({ listSystemPresence }) => {
const selfEntry = listSystemPresence().find((entry) => entry.reason === "self");
expect(selfEntry?.version).toBe("2.4.6-service");
},
);
});
it("prefers OPENCLAW_VERSION over OPENCLAW_SERVICE_VERSION", async () => {
await withPresenceModule(
{
OPENCLAW_VERSION: "9.9.9-cli",
OPENCLAW_SERVICE_VERSION: "2.4.6-service",
npm_package_version: "1.0.0-package",
},
({ listSystemPresence }) => {
const selfEntry = listSystemPresence().find((entry) => entry.reason === "self");
expect(selfEntry?.version).toBe("9.9.9-cli");
},
);
});
it("uses npm_package_version when OPENCLAW_VERSION and OPENCLAW_SERVICE_VERSION are blank", async () => {
await withPresenceModule(
{
OPENCLAW_VERSION: " ",
OPENCLAW_SERVICE_VERSION: "\t",
npm_package_version: "1.0.0-package",
},
({ listSystemPresence }) => {
const selfEntry = listSystemPresence().find((entry) => entry.reason === "self");
expect(selfEntry?.version).toBe("1.0.0-package");
},
);
});
});