From 90817cb2f8808441bc9aa58eedcd4079ae043e31 Mon Sep 17 00:00:00 2001 From: ypratama Date: Tue, 8 Sep 2026 17:25:11 +0700 Subject: [PATCH] Fix: Proxy NEVER overwrites custom label - two-step upsert approach --- proxy/collector.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/proxy/collector.js b/proxy/collector.js index 57b7a5b..e4f7c1e 100644 --- a/proxy/collector.js +++ b/proxy/collector.js @@ -34,6 +34,8 @@ async function collectAllAgents() { const totalAgents = agents.length; console.log(`[Collector] Processing ${totalAgents} unique agents globally`); + // Step 1: Upsert technical metadata only (NEVER touch label field during updates) + // This prevents ANY version of proxy from overwriting a custom label set by the UI. await Promise.allSettled(agents.map(a => AgentRegistry.findOneAndUpdate( { uuid: a.uuid }, @@ -41,18 +43,24 @@ async function collectAllAgents() { $set: { uuid: a.uuid, serial: a.serial || a.uuid, - site_uuid: 'global', // Store globally + site_uuid: 'global', provisioned: a.provisioned ?? true, activated: a.activated ?? false, last_seen_at: a.last_seen_at ?? null, }, - $setOnInsert: { - label: a.label, - } }, { upsert: true, new: true } ) )); + + // Step 2: Set default label ONLY for agents that have no label yet (brand new agents). + // We NEVER overwrite an existing label — even if it was set by an older proxy version. + await Promise.allSettled(agents.map(a => + AgentRegistry.updateOne( + { uuid: a.uuid, $or: [{ label: { $exists: false } }, { label: null }, { label: '' }] }, + { $set: { label: a.label } } + ) + )); console.log(`[Collector] ✓ ${agents.length} agents upserted into registry globally`); for (const agent of agents) {