{"record":{"id":"2379c871fdbd1efc","repo":"chenhg5/cc-connect","slug":"tmux-create-window-q-in-session-q-w","errorCode":null,"errorMessage":"tmux: create window %q in session %q: %w","messagePattern":"tmux: create window %q in session %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agent/tmux/tmux.go","lineNumber":187,"sourceCode":"\twindowName, effectiveSessionID := a.windowForSession(sessionName, pane, workDir, sessionID)\n\tsessionID = effectiveSessionID\n\ttarget := sessionName + \":\" + windowName\n\n\tsessionExists := tmuxSessionExists(sessionName)\n\twindowExists := sessionExists && tmuxWindowExists(target)\n\n\tif !sessionExists {\n\t\tif !autoCreate {\n\t\t\treturn nil, fmt.Errorf(\"tmux: session %q does not exist and auto_create is disabled\", sessionName)\n\t\t}\n\t\tif err := createTmuxSession(sessionName, windowName, workDir, shell); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"tmux: create session %q: %w\", sessionName, err)\n\t\t}\n\t\tslog.Info(\"tmux: created session\", \"session\", sessionName, \"window\", windowName, \"work_dir\", workDir)\n\t} else if !windowExists && windowName != pane {\n\t\t// Session exists but this workspace window does not yet — create it.\n\t\tif err := createTmuxWindow(sessionName, windowName, workDir); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"tmux: create window %q in session %q: %w\", windowName, sessionName, err)\n\t\t}\n\t\tslog.Info(\"tmux: created window\", \"session\", sessionName, \"window\", windowName, \"work_dir\", workDir)\n\t}\n\n\tnewPane := !sessionExists || (!windowExists && windowName != pane)\n\tif newPane {\n\t\t// Always cd to the workspace directory so the shell (and any init_command)\n\t\t// starts in the right place, regardless of tmux's -c flag behaviour.\n\t\tif workDir != \"\" && workDir != \".\" {\n\t\t\tif err := sendKeys(target, \"cd \"+shellQuote(workDir)); err != nil {\n\t\t\t\tslog.Warn(\"tmux: cd failed\", \"work_dir\", workDir, \"err\", err)\n\t\t\t}\n\t\t}\n\t\tif initCmd != \"\" {\n\t\t\tif err := sendKeys(target, initCmd); err != nil {\n\t\t\t\tslog.Warn(\"tmux: init_command failed\", \"cmd\", initCmd, \"err\", err)\n\t\t\t} else {\n\t\t\t\tslog.Info(\"tmux: init_command sent\", \"cmd\", initCmd, \"target\", target)","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/agent/tmux/tmux.go#L169-L205","documentation":"When the tmux session exists but the workspace window does not (and windowName != pane), StartSession calls createTmuxWindow and wraps failures as 'tmux: create window %q in session %q: %w'. It is the session-level wrap around the raw `tmux new-window` exec error (errorIndex 351).","triggerScenarios":"StartSession against an existing tmux session with window_per_session style layout, where `tmux new-window -d` fails: bad -c workDir, malformed target, or the session dying between the has-session check and the call.","commonSituations":"Multiple workspaces sharing one tmux session with an invalid work_dir for a new workspace; session killed by another client in a race; window-name/target syntax issues from unusual characters.","solutions":["Check the inner tmux output for the concrete cause","Ensure work_dir for the workspace exists","Confirm the session still exists (tmux has-session -t <name>) and retry","Create the window manually: tmux new-window -d -t <session>: -n <name> -c <dir>"],"exampleFix":"// before\n# work_dir deleted -> new-window fails\n// tmux: create window \"feature-x\" in session \"cc\": exit status 1: can't set working directory\n\n// after\nmkdir -p /home/user/feature-x\n# retry StartSession","handlingStrategy":"retry","validationCode":"if !exec.Command(\"tmux\",\"has-session\",\"-t\",sessionName).Run() == nil { /* recreate session first */ }\nif _, err := os.Stat(workDir); err != nil { return err }","typeGuard":null,"tryCatchPattern":"sess, err := agent.StartSession(ctx, opts)\nif err != nil && strings.Contains(err.Error(), \"create window\") {\n    time.Sleep(200 * time.Millisecond)\n    sess, err = agent.StartSession(ctx, opts) // re-checks existence\n}","preventionTips":["Retry StartSession once on window-create failures (handles races)","Keep per-workspace work_dir valid and existing","Avoid killing tmux sessions externally while agents run","Use simple window names without target-syntax characters"],"tags":["go","tmux","exec","lifecycle"],"backgroundTag":"command-execution-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}