diff --git a/gui/ollie9pclient.cpp b/gui/ollie9pclient.cpp index 9ead609..0efdbf9 100644 --- a/gui/ollie9pclient.cpp +++ b/gui/ollie9pclient.cpp @@ -48,40 +48,38 @@ Ollie9pClient::~Ollie9pClient() void Ollie9pClient::setActiveSessionId(const QString &id) { - if (m_activeSessionId == id) return; + if (m_activeSessionId == id) return; - stopStreams(); - m_activeSessionId = id; - m_agentId.clear(); - m_activeState = "idle"; + stopStreams(); + m_activeSessionId = id; + m_agentId.clear(); + m_activeState = "idle"; - if (id.isEmpty()) { - emit activeSessionIdChanged(); - emit activeStateChanged(); - return; - } + if (id.isEmpty()) { + emit activeSessionIdChanged(); + emit activeStateChanged(); + return; + } - // Discover agent ID BEFORE emitting signals - QStringList agents = getAgents(id); - if (!agents.isEmpty()) { - m_agentId = agents.first(); + // Discover agents. If exactly one, use it automatically (backward compat). + QStringList agents = getAgents(id); + if (agents.size() == 1) { + m_agentId = agents.first(); - // Read initial state immediately (state, not statewait) - QByteArray stateOut = run9p({"read", agentPath() + "/state"}); - QString stateStr = QString::fromUtf8(stateOut).trimmed(); - if (!stateStr.isEmpty()) - m_activeState = stateStr; + QByteArray stateOut = run9p({"read", agentPath() + "/state"}); + QString stateStr = QString::fromUtf8(stateOut).trimmed(); + if (!stateStr.isEmpty()) + m_activeState = stateStr; + } - emit activeSessionIdChanged(); - emit activeStateChanged(); + emit activeSessionIdChanged(); + emit activeAgentIdChanged(); + emit activeStateChanged(); - startChatStream(); - startStateStream(); - } else { - // Empty session — no agent yet - emit activeSessionIdChanged(); - emit activeStateChanged(); - } + if (!m_agentId.isEmpty()) { + startChatStream(); + startStateStream(); + } } void Ollie9pClient::refreshSessions() @@ -443,22 +441,22 @@ void Ollie9pClient::startChatStream() void Ollie9pClient::startStateStream() { - if (m_stateProc) return; + if (m_stateProc) return; - // Check if session still exists before starting - if (m_activeSessionId.isEmpty() || m_agentId.isEmpty()) return; + // Check if session still exists before starting + if (m_activeSessionId.isEmpty() || m_agentId.isEmpty()) return; - // Re-discover agent ID — directory may have been renamed - QStringList agents = getAgents(m_activeSessionId); - if (agents.isEmpty()) { - // Session or agent directory gone — don't start - return; - } - QString newId = agents.first(); - if (newId != m_agentId) { - m_agentId = newId; - emit activeAgentIdChanged(); - } + // Verify the current agent directory still exists + QStringList agents = getAgents(m_activeSessionId); + if (agents.isEmpty()) { + // Session or agent directory gone — don't start + return; + } + if (!agents.contains(m_agentId)) { + // Current agent no longer exists — fall back to first available + m_agentId = agents.first(); + emit activeAgentIdChanged(); + } m_stateProc = new QProcess(this); connect(m_stateProc, &QProcess::readyReadStandardOutput, this, &Ollie9pClient::onStateReadyRead);