Share one task owner, session namer, and Codex sign-in across `clai2` sessions in a process
Part 3 of the clai2 multi-session stack (#10468 → #10469 → this PR → #10485 → #10494 → #10495 → #10504 → #10541 → #10549). Based on #10469. No linked issue: requested by the CLAI maintainer.
Why. Several live sessions will run in one process (PR 5). They must share one delegation-task owner, one naming worker and one Codex sign-in, and each must see, wake on and cancel only its own work. This PR adds those shared services while production still runs one session, so behaviour is unchanged.
What changes
ClaiProcess(runtime/process.py) now holdstasks: TaskRouter,namer: SessionNamerandcodex_providers: dict[str, OpenAICodexProvider]next to the stores.ClaiProcess.open(store=None)builds them.TaskRouter(*, directory, step_store=None)(runtime/tasks.py) owns the one harnessDelegationTasks:async with router.opened(view=tasks) as owner: the first session opens the owner, the last closes it (cancelling what still runs, as before). A session leaving earlier cancels only the tasks it runs.- A task belongs to the session that started or last resumed it; a child task to its parent's session. Events, wake-ups and rows go to the session holding the task's conversation. Progress and partial output are kept for every task.
view(conversation_id) -> Tasks | None,async observe(update).
Tasks(*, console, conversation_id, router=None, ...)is a view over the router:records(),runs(record),running(),async deliver(update).background_runningandplugins_busynow userunning(), so they count only this session's tasks.SessionNamer(*, store, timeout=60)is shared. Callers pass a per-sessionNamingClient(*, generate, enabled=lambda: True)tosubmit(conversation_id, *, client),backfill(entries, *, client)andname(*, conversation_id, client). Titles go tonamer.listeners, each told in its own task so a busy session holds up no other naming.Sessions(..., namer=None)adds itsnamedlistener, which ignores other conversations and lets the newest name win.ModelResolver(..., codex_providers=...)andCodexAuth(console, *, ..., providers=None)share the Codex provider cache, so token refresh happens once per account, while each session still prompts on its own console.Interrupts(*, signals: bool = True): only a runtime withsignals=Trueinstalls and restores the SIGINT handler; others cancel throughcancel().create_shell(..., process=None, signals=True)stays backward compatible: without aprocess, a runtime gets its own._app.pyandcli/web.pystart the shared namer's worker.
Deviation from the plan: there is no shared ModelResolver/CodexAuth object, because each session resolves models with its own console and plugin providers. Only the Codex provider cache is shared. Deferred to PR 5: a runtime whose run ended stays in router.views and namer.listeners until the close path removes it.
Verification.
- New in-process tests (
tests/clai2/test_session_runtime.py): two runtimes over one process share the owner, namer and Codex providers; when A exits, B's running task and turn keep going and A's own tasks are cancelled; task events reach only the session holding the conversation; a child task belongs to its parent's session; the owner opens once for overlapping users;signal.getsignal(SIGINT)before, during and after overlappingInterrupts.runcalls ending in either order. - Naming tests: a session waiting for its terminal holds up no other conversation's naming; a failing listener is logged; the newer of two waiting names wins.
- pyright strict and ruff clean; 100% branch coverage of changed lines; related CLAI test files pass.
- Real run in tmux with isolated config:
/tasksopens,!sleep 20is interrupted by Ctrl-C, and Ctrl-C twice exits.clai2 --web -m testserves/api/state, andPOST /api/inputreturns 204.
What changes for existing users. Nothing. CLAI still runs one session per process.
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.