Skip to main content

build_router

Function build_router 

Source
pub async fn build_router(
    binding: ProjectBinding,
) -> Result<(Router, Arc<SessionRegistry>, SharedWorkstreamStore)>
Expand description

Assemble the router (+ its session registry) with every method this build of tcode knows about, scoped to project.

Why: single place that lists which method groups are wired in, so a new ticket adding harness.describe touches exactly one line here plus its own register function. Both transports (run_stdio, run_http) call this so they can never drift into different method surfaces, and both need the SAME SessionRegistry instance — run_http hands it separately to the GET /sessions/{id}/events SSE route, which bypasses the Router entirely (see crate::serve::http module docs). task.run (#2056) is the first method that needs project for more than logging — it resolves agents_dir from it via crate::agents::locate_agents_dir (the same .claude/agents / .open-mpm/agents convention tcode run-task uses) and passes both down to spawned executions. fs.list_dir (UI Phase-1) is the one group that needs NO shared state — a directory listing is a pure function of the filesystem at call time — so it registers with neither sessions nor project. agents.*/skills.* (issue #3449) are the Foundry GUI’s catalog management surface — list/create/delete over the disk tier crate::agents::resolve_agent and crate::skills::discover_skill_metadata already recognise, plus the embedded/bundled roster each falls back to. Both register with immutable state resolved once here from binding (AgentsCatalogState/SkillsCatalogState — no Mutex, every operation is a stateless filesystem call at request time). What: builds an empty Router + a fresh SessionRegistry, calls methods::register, crate::session::protocol::register, crate::fs_browse::protocol::register, and crate::task::protocol::register on them, and returns both.

(DOC-48, issues #3294/#3295) also loads this project’s crate::workstreams::WorkstreamStore (~/.trusty-code/workstreams-....json, derived from binding per DOC-48 §3.1), runs its boot reconciliation (§3.3 — restores the persisted active pointer, or clears it if the workstream it named was deleted; never destructive), wraps it in a tokio::sync::Mutex (the store’s mutating methods take &mut self, and its file I/O is async — mirrors SessionRegistry’s Arc-shared-state convention, see crate::workstreams::activation::SharedWorkstreamStore’s docs for why tokio’s Mutex specifically), and registers crate::workstreams::protocol::register — all six workstream.* methods (create/get/list/close from #3295, activate/deactivate from #3294) sharing this one store handle — alongside every other method group. This is why build_router is now async and fallible — the prior synchronous, infallible signature had no way to load persisted state before the router starts answering requests, and a corrupt store file must fail daemon startup loudly rather than silently discard the user’s workstreams (see WorkstreamStore::load’s docs).

binding is the daemon’s project binding. Its None (projectless) state is what makes the shell’s entry screen implementable: previously build_router took a required PathBuf, so a daemon could not even START without a project, and task.run could not be called without one. agents_dir now resolves via ProjectBinding::agents_dir, which falls back to the user-level ~/.claude/agents when projectless rather than to the process CWD (which would silently bind a directory nobody chose). (issue #3297) Also returns the SharedWorkstreamStore itself (not just the Router it registered workstream.* against) — run_http needs the SAME handle to hand to crate::serve::http::run_http, which merges crate::workstreams::sse::routes (the GET /workstreams/{id}/events aggregation route) alongside the JSON-RPC-backed REST surface. This is why the return type grew a third element rather than staying a 2-tuple.

Test: run_stdio_router_recognises_proof_of_life_methods, build_router_wires_session_methods, build_router_wires_task_run, build_router_is_projectless_without_a_project, build_router_wires_fs_list_dir, build_router_wires_workstream_methods, build_router_wires_agents_and_skills_catalog_methods, build_router_reconciles_dangling_active_pointer_on_boot.