Share one task owner, session namer, and Codex sign-in across `clai2` sessions in a process

Merged
MP
2mo ago
+667-167

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 holds tasks: TaskRouter, namer: SessionNamer and codex_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 harness DelegationTasks:
    • 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_running and plugins_busy now use running(), so they count only this session's tasks.
  • SessionNamer(*, store, timeout=60) is shared. Callers pass a per-session NamingClient(*, generate, enabled=lambda: True) to submit(conversation_id, *, client), backfill(entries, *, client) and name(*, conversation_id, client). Titles go to namer.listeners, each told in its own task so a busy session holds up no other naming. Sessions(..., namer=None) adds its named listener, which ignores other conversations and lets the newest name win.
  • ModelResolver(..., codex_providers=...) and CodexAuth(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 with signals=True installs and restores the SIGINT handler; others cancel through cancel().
  • create_shell(..., process=None, signals=True) stays backward compatible: without a process, a runtime gets its own.
  • _app.py and cli/web.py start 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 overlapping Interrupts.run calls 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: /tasks opens, !sleep 20 is interrupted by Ctrl-C, and Ctrl-C twice exits. clai2 --web -m test serves /api/state, and POST /api/input returns 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.