Skip to content

libtmux(feat[get_or_create]): Add window and session helpers - #920

Open
tony wants to merge 2 commits into
masterfrom
feat/916-get-or-create
Open

tony wants to merge 2 commits into
masterfrom
feat/916-get-or-create

Conversation

@tony

@tony tony commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Note

Sequenced in #925 (0.71.0) behind safe {Session,Window,Pane}.__exit__ teardown and the get-or-create / created vocabulary decision (#916, #804). Rebase onto v0.70.0 checks annotations with ty (uv run ty check) and Ruff ANN (#682) in place of mypy.

Summary

  • Add Session.get_or_create_window(name, ...): return the window with that name in the session, or create it.
  • Add Server.get_or_create_session(name, ...): return the session with that name, or create it, returning a concurrent creator's session when tmux refuses the duplicate.
  • No Window or Pane variant: panes have no name to look up.

Fixes #916. Builds on #917, whose name lookup get_or_create_window uses.

Design decisions

  • get_or_create_window calls new_window(select_existing=True) with the creation arguments and no index, so the lookup is Session(fix[new_window]): Look up reuse by name #917's exact-name match on every tmux. kill_existing and window_index are not accepted: -k and an index key on the window index, not the name.
  • get_or_create_session looks the name up exactly, creates it with new_session, and when creation is refused as a duplicate (libtmux's own check or tmux's duplicate session), re-reads and returns the session the other caller created. Any other creation error is raised, even if a session with the name appeared meanwhile.
  • Both return the object alone. Whether it was created or reused belongs to Create or reuse tmux resources with explicit cleanup ownership #804's result type; on the window path tmux prints nothing on reuse, so the answer is not available on every path.
  • The creation arguments apply only on creation; an existing window or session is returned as it is.

Test plan

  • Create-then-reuse for both helpers, and a Format window name
  • A concurrent creator's session is returned, through libtmux's pre-check and through tmux's duplicate session refusal
  • An unrelated creation error is raised even when the session appeared meanwhile; an invalid name raises BadSessionName
  • Without the helpers the tests fail; removing the re-read or treating every error as a duplicate each fails its test
  • Tested on tmux 3.3a and 3.8a
  • ruff check, ruff format --check, mypy, the full test suite and just build-docs (no warnings)
  • CI

tony added a commit that referenced this pull request Nov 23, 2026
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony tony mentioned this pull request Nov 23, 2026
43 of 62 tasks
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.22222% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.08%. Comparing base (efdbb56) to head (933bee7).

Files with missing lines Patch % Lines
src/libtmux/server.py 75.00% 3 Missing and 1 partial ⚠️
src/libtmux/session.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #920      +/-   ##
==========================================
+ Coverage   61.99%   62.08%   +0.09%     
==========================================
  Files          29       29              
  Lines        5091     5109      +18     
  Branches     1082     1085       +3     
==========================================
+ Hits         3156     3172      +16     
- Misses       1595     1597       +2     
  Partials      340      340              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tony
tony force-pushed the fix/914-select-existing-literal branch from 5d9e119 to 4588c58 Compare November 23, 2026 00:47
Base automatically changed from fix/914-select-existing-literal to master November 23, 2026 00:49
tony added a commit that referenced this pull request Nov 23, 2026
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony
tony force-pushed the feat/916-get-or-create branch from 2c03631 to 6e0f3e6 Compare November 23, 2026 00:59
tony added a commit that referenced this pull request Nov 23, 2026
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony
tony force-pushed the feat/916-get-or-create branch from 6e0f3e6 to a1f2035 Compare November 23, 2026 02:33
tony added a commit that referenced this pull request Nov 23, 2026
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony
tony force-pushed the feat/916-get-or-create branch from a1f2035 to f19f663 Compare November 23, 2026 23:35
@tony tony added area: lifecycle Server start, context managers, ownership, get-or-create, discovery, leases, program lifetime needs-decision The maintainer chooses before any code labels Nov 24, 2026
@tony tony added this to the 0.68.0 milestone Nov 24, 2026
@tony

tony commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Blocked on the naming decision recorded in #916: #831 and #771 add find_or_create_session() for the same idea. #925 also lists the reused-session hazard: Session.__exit__ would kill a session this helper reused inside a with block.

@tony tony added this to libtmux Nov 24, 2026
tony added a commit that referenced this pull request Nov 24, 2026
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony
tony force-pushed the feat/916-get-or-create branch from f19f663 to 839fb35 Compare November 24, 2026 01:44
@tony tony removed this from the 0.68.0 milestone Nov 24, 2026
tony added a commit that referenced this pull request Nov 25, 2026
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony
tony force-pushed the feat/916-get-or-create branch from 839fb35 to afdeeea Compare November 25, 2026 08:24
@tony tony moved this to In Progress in libtmux Nov 25, 2026
tony added a commit that referenced this pull request Nov 28, 2026
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony
tony force-pushed the feat/916-get-or-create branch from afdeeea to 383dd5c Compare November 28, 2026 12:49
@tony tony added this to the 0.71.0 milestone Nov 28, 2026
tony added 2 commits November 28, 2026 20:39
why: Idempotent setup asks "make this window or session unless it
exists". new_window(select_existing=True) names that with a tmux flag
that does not say what comes back, and sessions had no helper (#916).

what:
- Add Session.get_or_create_window, which calls new_window with
  select_existing=True and the creation arguments
- Add Server.get_or_create_session: exact lookup, new_session, and a
  re-read when the creation is refused as a duplicate, so a concurrent
  creator's session is returned
- Raise any other creation error, even if the session appeared
- No Window or Pane variant: panes have no name to look up
why: Users setting up windows and sessions idempotently should find the
new helpers in the changelog (#920).

what:
- Add a What's new entry for #920
@tony
tony force-pushed the feat/916-get-or-create branch from 383dd5c to 933bee7 Compare November 29, 2026 02:42

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: lifecycle Server start, context managers, ownership, get-or-create, discovery, leases, program lifetime enhancement needs-decision The maintainer chooses before any code

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Add get_or_create_window() and get_or_create_session() helpers

1 participant