{"record":{"id":"6232936ba2c37eef","repo":"t8y2/dbx","slug":"agentsessionid-is-required-623293","errorCode":null,"errorMessage":"agentSessionId is required","messagePattern":"agentSessionId is required","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":2049,"sourceCode":"        }\n\n        private Session session(String sessionId) {\n            Session session = sessions.get(sessionId);\n            if (session == null) {\n                throw new IllegalStateException(\"Agent session not found: \" + sessionId);\n            }\n            return session;\n        }\n\n        private void closeAllSessions() {\n            for (String sessionId : sessions.keySet()) {\n                closeSession(sessionId);\n            }\n        }\n\n        private static String requiredSessionId(JsonObject params) {\n            if (!params.has(\"agentSessionId\") || params.get(\"agentSessionId\").getAsString().trim().isEmpty()) {\n                throw new IllegalArgumentException(\"agentSessionId is required\");\n            }\n            return params.get(\"agentSessionId\").getAsString();\n        }\n\n        private void writeResponse(String response) {\n            synchronized (outputLock) {\n                System.out.println(response);\n                System.out.flush();\n            }\n        }\n    }\n\n    private static final class Session {\n        private final MongoClient client;\n\n        private Session(MongoClient client) {\n            this.client = client;\n        }","sourceCodeStart":2031,"sourceCodeEnd":2067,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L2031-L2067","documentation":"MongoAgent's JSON-RPC handlers require an \"agentSessionId\" string parameter to route calls to the correct open session. requiredSessionId throws IllegalArgumentException when the parameter is missing, null, or blank/whitespace. This is an input-validation guard so session-scoped operations never run against an undefined session.","triggerScenarios":"Calling any session-scoped MongoAgent JSON-RPC method whose params object omits \"agentSessionId\", sets it to null, or passes an empty/whitespace-only string (e.g. after closeSession was called with a stale id and the client forgot to re-attach the new id).","commonSituations":"Client code caches session state and sends stale or empty ids after reconnecting; hand-written JSON-RPC requests in curl/tests omit the field; a serialization layer drops null fields so the key never reaches the agent; copying example payloads that use a different session field name.","solutions":["Add a non-empty \"agentSessionId\" string to the request params (use the id returned when the session was opened).","If the client drops null/empty fields during serialization, fix the serializer to send the value or re-open a session to obtain a valid id.","Wrap the JSON-RPC call in a try-catch for IllegalArgumentException and re-open a session before retrying."],"exampleFix":"// before\nclient.call(\"mongo.query\", Map.of(\"database\", \"shop\"));\n// after\nclient.call(\"mongo.query\", Map.of(\"agentSessionId\", sessionId, \"database\", \"shop\"));","handlingStrategy":"validation","validationCode":"if (params == null || params.get(\"agentSessionId\") == null || params.get(\"agentSessionId\").isJsonNull() || params.get(\"agentSessionId\").getAsString().trim().isEmpty()) {\n    throw new IllegalArgumentException(\"agentSessionId is required\");\n}","typeGuard":"boolean hasSessionId(com.google.gson.JsonObject p) {\n    return p != null && p.has(\"agentSessionId\")\n        && p.get(\"agentSessionId\").isJsonPrimitive()\n        && !p.get(\"agentSessionId\").getAsString().trim().isEmpty();\n}","tryCatchPattern":"try {\n    result = client.call(method, params);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"agentSessionId\")) {\n        sessionId = openSession(); // re-open and retry once with a valid id\n        params.addProperty(\"agentSessionId\", sessionId);\n        result = client.call(method, params);\n    } else throw e;\n}","preventionTips":["Always attach the id returned by the session-open call to every session-scoped request.","Never send raw Maps for RPC params; use a typed request builder that enforces agentSessionId.","Re-open a session after any reconnect instead of reusing a possibly-closed id."],"tags":["json-rpc","validation","mongodb","missing-parameter"],"backgroundTag":"missing-required-parameter","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}