diff --git a/gui/ollie9pclient.cpp b/gui/ollie9pclient.cpp index cb5f02e..1a013ce 100644 --- a/gui/ollie9pclient.cpp +++ b/gui/ollie9pclient.cpp @@ -268,31 +268,62 @@ bool Ollie9pClient::createSession(const QString &cwd, const QString &name, const { if (cwd.isEmpty()) return false; - QStringList args; - args << "cwd=" + cwd; - if (!name.isEmpty()) args << "name=" + name; - if (!backend.isEmpty()) args << "backend=" + backend; - if (!model.isEmpty()) args << "model=" + model; - if (!agent.isEmpty()) args << "agent=" + agent; - if (!remote.isEmpty()) args << "remote=" + remote; - if (!agentAlias.isEmpty()) args << "agent_alias=" + agentAlias; + // Step 1: Create the session (name only) + QString sessName = name; + { + QProcess proc; + QStringList args = {"-a", serverAddr(), "write", "session/new"}; + proc.start(ninepBin(), args); + proc.waitForStarted(3000); + QString input = "name=" + sessName; + proc.write(input.toUtf8()); + proc.closeWriteChannel(); + proc.waitForFinished(5000); + if (proc.exitCode() != 0) { + QString err = QString::fromUtf8(proc.readAllStandardError()).trimmed(); + if (err.isEmpty()) + err = QString::fromUtf8(proc.readAllStandardOutput()).trimmed(); + qDebug() << "createSession (step 1) failed:" << err; + return false; + } + // Read the returned session name (may be auto-generated) + QByteArray out = proc.readAllStandardOutput().trimmed(); + if (!out.isEmpty()) + sessName = QString::fromUtf8(out); + } - QProcess proc; - QStringList fullArgs = {"-a", serverAddr(), "write", "session/new"}; - proc.start(ninepBin(), fullArgs); - proc.waitForStarted(3000); + // Step 2: Create the agent within the session + { + QStringList agentArgs; + agentArgs << "cwd=" + cwd; + if (!backend.isEmpty()) agentArgs << "backend=" + backend; + if (!model.isEmpty()) agentArgs << "model=" + model; + if (!agent.isEmpty()) agentArgs << "agent=" + agent; + if (!remote.isEmpty()) agentArgs << "remote=" + remote; + if (!agentAlias.isEmpty()) agentArgs << "agent_alias=" + agentAlias; - QString input = args.join(" "); - proc.write(input.toUtf8()); - proc.closeWriteChannel(); - proc.waitForFinished(5000); + QProcess proc; + QStringList args = {"-a", serverAddr(), "write", "session/" + sessName + "/agent/new"}; + proc.start(ninepBin(), args); + proc.waitForStarted(3000); + proc.write(agentArgs.join(" ").toUtf8()); + proc.closeWriteChannel(); + proc.waitForFinished(5000); - if (proc.exitCode() != 0) { - QString err = QString::fromUtf8(proc.readAllStandardError()).trimmed(); - if (err.isEmpty()) - err = QString::fromUtf8(proc.readAllStandardOutput()).trimmed(); - qDebug() << "createSession failed:" << err; - return false; + if (proc.exitCode() != 0) { + QString err = QString::fromUtf8(proc.readAllStandardError()).trimmed(); + if (err.isEmpty()) + err = QString::fromUtf8(proc.readAllStandardOutput()).trimmed(); + qDebug() << "createSession (step 2) failed:" << err; + // Clean up the session + QProcess killProc; + killProc.start(ninepBin(), {"-a", serverAddr(), "write", "session/" + sessName + "/ctl"}); + killProc.waitForStarted(3000); + killProc.write("kill"); + killProc.closeWriteChannel(); + killProc.waitForFinished(3000); + return false; + } } return true;