Fix: Proxy NEVER overwrites custom label - two-step upsert approach

This commit is contained in:
ypratama committed 2026-09-08 17:25:11 +07:00
1 parent 0dcedf5d76
commit 90817cb2f8
1 file changed
+12 -4
+12 -4
View File
@@ -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) {