diff --git a/src/prerna/reactor/agent/AgentRunner.java b/src/prerna/reactor/agent/AgentRunner.java index 4c785d2c04..4deb0f983d 100644 --- a/src/prerna/reactor/agent/AgentRunner.java +++ b/src/prerna/reactor/agent/AgentRunner.java @@ -278,6 +278,11 @@ private static AgentHarnessResult run(String roomId, String input, String engine AgentConfig agentConfig = AgentConfigLoader.load(room, filePath, modelId, params, agentParams, maxTurns, maxReflections, explicitWorkspaceId); + // A run that starts an empty room claims it for the agent, as CreateRoom would. + if (!resumeMode) { + persistWorkspaceOnEmptyRoom(room, insight, explicitWorkspaceId); + } + try { SkillStager.stage(filePath, agentConfig.getSkills()); } catch (Exception e) { @@ -410,6 +415,47 @@ private static final class WorkspaceOverlay { } } + /** + * Saves {@code workspaceId} on a room that has no messages yet, both as the + * {@code ROOM.WORKSPACE_ID} column and as {@code options.workspace}, so a room + * started by RunAgent matches one created with {@code CreateRoom(workspaceId)}. + * Rooms with messages keep their saved workspace; the run only overlays it. + * Skipped when the workspace is missing, disabled, or not viewable by the + * user. + */ + static void persistWorkspaceOnEmptyRoom(Room room, Insight insight, String workspaceId) { + if (workspaceId == null || workspaceId.trim().isEmpty()) { + return; + } + List messages = room.getMessages(); + if (messages != null && !messages.isEmpty()) { + return; + } + try { + Map ws = ModelInferenceLogsUtils.getWorkspaceEntry(workspaceId); + if (ws == null || !Boolean.TRUE.equals(ws.get("is_active")) || insight.getUser() == null + || !SecurityProjectUtils.userCanViewProject(insight.getUser(), workspaceId)) { + return; + } + + Map workspace = new HashMap<>(); + workspace.put("workspace_id", workspaceId); + if (ws.get("name") != null) { + workspace.put("name", String.valueOf(ws.get("name"))); + } + Map opts = new HashMap<>(room.getOptionsMap()); + opts.put("workspace", workspace); + + ModelInferenceLogsUtils.setRoomWorkspaceId(room.getId(), room.getUserId(), workspaceId); + ModelInferenceLogsUtils.setRoomOptions(room.getId(), room.getUserId(), opts); + room.setOptionsMap(opts); + logger.info("AgentRunner: saved workspace '{}' on empty room '{}'", workspaceId, room.getId()); + } catch (Exception e) { + logger.warn("AgentRunner: could not save workspace '{}' on room '{}': {}", workspaceId, room.getId(), + e.getMessage(), e); + } + } + /** * Applies an in-memory workspace override for one run when needed. */