diff --git a/fs/session/create.go b/fs/session/create.go index 1c82555..3b86973 100644 --- a/fs/session/create.go +++ b/fs/session/create.go @@ -15,6 +15,7 @@ import ( ) // Create creates a new session from key=value args. Returns the session name. +// The session is NOT registered yet — CreateAgent must be called to finalize it. func Create(rs *rootState, args []string) (string, error) { name := "" for _, arg := range args { @@ -48,20 +49,13 @@ func Create(rs *rootState, args []string) (string, error) { return "", fmt.Errorf("session already exists: %s", name) } - sessionCtx, sessionCancel := context.WithCancel(rs.cfg.Ctx) - sess := NewSession(sessID, nil, sessionCtx, sessionCancel) - sess.SetName(name) - - rs.mu.Lock() - rs.sessions[name] = sess - rs.mu.Unlock() - - rs.cfg.Log.Info("new session %s (id=%s)", name, sessID) - broadcastEvents() + rs.cfg.Log.Info("reserved session name %s (id=%s)", name, sessID) return name, nil } -// CreateAgent creates and configures an agent within an existing session. +// CreateAgent creates and configures an agent within a new or existing session. +// If the session already exists (has a Core), it only replaces the agent. +// If the session doesn't exist yet, it creates the full session with agent. func CreateAgent(rs *rootState, sessName string, args []string) error { backendOverride := "" modelOverride := "" @@ -102,7 +96,11 @@ func CreateAgent(rs *rootState, sessName string, args []string) error { sess, ok := rs.sessions[sessName] rs.mu.RUnlock() if !ok { - return fmt.Errorf("session not found: %s", sessName) + // Session doesn't exist yet — create it later after Core is set + sessID := coresession.NewSessionID() + sessionCtx, sessionCancel := context.WithCancel(rs.cfg.Ctx) + sess = NewSession(sessID, nil, sessionCtx, sessionCancel) + sess.SetName(sessName) } if agentName == "" { @@ -257,6 +255,13 @@ func CreateAgent(rs *rootState, sessName string, args []string) error { sess.proc = proc sess.mu.Unlock() + // Register session in the map if not already there + rs.mu.Lock() + if _, exists := rs.sessions[sessName]; !exists { + rs.sessions[sessName] = sess + } + rs.mu.Unlock() + core.Agent().Submit(sess.SessionCtx, "/agent "+agentName) if agentAlias != "" { core.Agent().SetName(agentAlias)