Skip to content

Commit c0e2f9b

Browse files
fix(agentos-server): fall back to _id lookup in /agents/resolve
Agents registered via the UI or /agents/register have no `agentId` field set — that field is only stamped by the telemetry projection when the SDK runs with agent_id=. The resolve endpoint was doing a strict { agentId } lookup, returning 404 for these agents even though the caller was passing the valid Mongo _id. Now falls back to an ObjectId _id lookup when the agentId field miss occurs, so callers can use the _id returned by /agents/register directly as their SMOKE_AGENT_ID / agent_id reference without a separate adoption run. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent dee8752 commit c0e2f9b

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

packages/agentos-server/src/routes/agents.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// chattable / scheduleable. Secrets stay in process.env.
1313

1414
import { Router, type Router as IRouter } from "express";
15+
import { ObjectId } from "mongodb";
1516
import { chatPinsColl, chatSessionsColl, messagesColl, registryColl, sessionsColl, type RegistryDoc } from "../mongo.js";
1617
import { listLiveSandboxes } from "../upstream.js";
1718
import { agentLogStore } from "../stores/agent-log-store.js";
@@ -120,7 +121,13 @@ agentsRouter.post("/agents/resolve", authorize("agents:read"), async (req, res,
120121
if (!agentId) {
121122
return res.status(400).json({ error: { code: "BAD_REQUEST", message: "`agentId` required" } });
122123
}
123-
const doc = await (await registryColl()).findOne({ agentId });
124+
const coll = await registryColl();
125+
let doc = await coll.findOne({ agentId });
126+
// Fall back to ObjectId (_id) lookup — lets callers use the Mongo _id
127+
// returned by /agents/register as the agent_id reference.
128+
if (!doc && ObjectId.isValid(agentId)) {
129+
doc = await coll.findOne({ _id: new ObjectId(agentId) });
130+
}
124131
if (!doc || !canRead(res.locals.principal, doc)) {
125132
return res.status(404).json({ error: { code: "NOT_FOUND", message: `no agent with agentId=${agentId}` } });
126133
}

0 commit comments

Comments
 (0)