Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix: metadata merge via json_patch prevents cli_version loss on upsert
Generc18590cf59cf
diff --git a/src/shared/src/db/queries/runtime.ts b/src/shared/src/db/queries/runtime.tsindex 03c1dac43..94de00b4a 100644--- a/src/shared/src/db/queries/runtime.ts+++ b/src/shared/src/db/queries/runtime.ts@@ -1,4 +1,4 @@-import { eq, and, asc } from "drizzle-orm";+import { eq, and, asc, sql } from "drizzle-orm";import { agentRuntime, agent, agentTaskQueue, machine } from "../schema";import type { Database } from "../index";@@ -14,6 +14,7 @@ export async function upsertAgentRuntime(}) {const now = new Date().toISOString();+ const metaJson = JSON.stringify(data.metadata ?? {});const rows = await db.insert(agentRuntime).values({@@ -33,7 +34,7 @@ export async function upsertAgentRuntime(set: {runtimeMode: data.runtimeMode,deviceInfo: data.deviceInfo,- metadata: data.metadata ?? null,+ metadata: sql`json_patch(coalesce(${agentRuntime.metadata}, '{}'), ${metaJson})`,updatedAt: now,},})diff --git a/src/web/src/app/(app)/w/[slug]/runtimes/page.tsx b/src/web/src/app/(app)/w/[slug]/runtimes/page.tsxindex 44e2aa753..78af3de0d 100644--- a/src/web/src/app/(app)/w/[slug]/runtimes/page.tsx+++ b/src/web/src/app/(app)/w/[slug]/runtimes/page.tsx@@ -410,7 +410,7 @@ export default function RuntimesPage() {<div className="flex items-center gap-1">{(() => {const isUpdating = !!machine.pendingUpdateVersion || effectiveUpdatingDaemons.has(daemonId);- const needsUpdate = latestCliVersion && machine.cliVersion && !semverGte(machine.cliVersion, latestCliVersion);+ const needsUpdate = latestCliVersion && (!machine.cliVersion || !semverGte(machine.cliVersion, latestCliVersion));if (isUpdating) {return (<Button variant="ghost" size="sm" disabled className="text-xs h-6 px-2">diff --git a/src/web/src/app/api/daemon/routes.test.ts b/src/web/src/app/api/daemon/routes.test.tsindex 00348f31f..734b4c326 100644--- a/src/web/src/app/api/daemon/routes.test.ts+++ b/src/web/src/app/api/daemon/routes.test.ts@@ -227,7 +227,7 @@ describe("daemon route body validation", () => {expect(upsertMock).toHaveBeenCalledWith({},expect.objectContaining({- metadata: expect.objectContaining({ version: "3.5" }),+ metadata: expect.objectContaining({ version: "3.5", cli_version: "0.5.1" }),}));});diff --git a/src/web/src/test/e2e/daemon-lifecycle.test.ts b/src/web/src/test/e2e/daemon-lifecycle.test.tsindex 10ca1b29e..b8ef76278 100644--- a/src/web/src/test/e2e/daemon-lifecycle.test.ts+++ b/src/web/src/test/e2e/daemon-lifecycle.test.ts@@ -64,6 +64,53 @@ describe("daemon lifecycle", () => {expect(data.runtimes[0].id).toBe(registeredRuntimeId)})+ it("register merges metadata (json_patch) instead of replacing", async () => {+ // First register sets cli_version+ const res1 = await tokenRequest("/api/daemon/register", seed.machineToken, {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ workspace_id: seed.workspaceId,+ daemon_id: daemonId,+ device_name: "e2e-machine",+ cli_version: "0.2.0",+ runtimes: [{ provider: "claude", runtime_mode: "local", version: "5.0" }],+ }),+ })+ expect(res1.status).toBe(200)++ // Verify cli_version is stored+ const rows1 = sqlQuery<{ metadata: string }>(+ `SELECT metadata FROM agent_runtime WHERE id = '${registeredRuntimeId}'`+ )+ const meta1 = JSON.parse(rows1[0]!.metadata)+ expect(meta1.cli_version).toBe("0.2.0")+ expect(meta1.version).toBe("5.0")++ // Second register without cli_version (simulates activate path)+ // We call register with empty cli_version — metadata should preserve cli_version+ const res2 = await tokenRequest("/api/daemon/register", seed.machineToken, {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ workspace_id: seed.workspaceId,+ daemon_id: daemonId,+ device_name: "e2e-machine",+ cli_version: "",+ runtimes: [{ provider: "claude", runtime_mode: "local", version: "5.1" }],+ }),+ })+ expect(res2.status).toBe(200)++ // cli_version should still be preserved via json_patch merge+ const rows2 = sqlQuery<{ metadata: string }>(+ `SELECT metadata FROM agent_runtime WHERE id = '${registeredRuntimeId}'`+ )+ const meta2 = JSON.parse(rows2[0]!.metadata)+ expect(meta2.cli_version).toBe("0.2.0")+ expect(meta2.version).toBe("5.1")+ })+it("POST /api/daemon/tasks/poll updates last_seen_at", async () => {const res = await tokenRequest("/api/daemon/tasks/poll", seed.machineToken, {method: "POST",