Skip to main content

resolve_validate_bind

Function resolve_validate_bind 

Source
pub async fn resolve_validate_bind<F>(
    store: &SharedWorkstreamStore,
    explicit: Option<&str>,
    method: &str,
    mint: F,
) -> Result<(String, Option<WorkstreamId>), RpcError>
where F: FnOnce() -> String,
Expand description

Resolve + VALIDATE the EFFECTIVE workstream target, then mint a session and persist its binding — all under one store lock (DOC-48 §4.1/§4.2, issue #3298; validate-before-mint ordering per PR #3354 code-critic HIGH findings 1 & 2).

Why: session::protocol::create and task::protocol::task_run (minting a NEW session) both need this exact resolution — an explicit workstream_id param (§4.1) takes precedence; otherwise the daemon’s currently active workstream is the ambient default target (§4.2); if neither applies, the session stays projectless (valid, never an error). Centralising it here (rather than duplicating in both protocol modules) is the same “one place, both callers” precedent crate::binding::ProjectBinding::resolve already set for project binding.

Why the mint closure (atomicity): SessionRegistry has no delete/rollback — a session, once minted, exists for the daemon lifetime. The first cut of this function took an ALREADY-minted session_id and could then fail (closed/unknown/malformed target), leaving an orphaned, caller-invisible session in the registry on every rejected bind — a retry-amplified memory-growth vector (code-critic HIGH, PR #3354). Now every caller-triggerable failure mode (malformed id, unknown id, closed workstream) is validated BEFORE mint() runs, so a rejected bind mints nothing. Holding the ONE store lock across resolve -> validate -> mint -> bind keeps the sequence TOCTOU-free: no concurrent workstream.close/deactivate can invalidate the target between its validation and the bind. What: explicit is the caller’s raw wire workstream_id string, if any; mint mints the session (infallible — SessionRegistry::create never fails) and returns its id, called EXACTLY ONCE, and only after validation passes. Returns (session_id, bound_workstream) — (id, None) when no explicit param was given and no workstream is active (projectless mint); (id, Some(ws)) after WorkstreamStore::bind_session persisted the append. Err(-32602 invalid_params) — explicit is not a valid UUID. Err(-32002 not_found) — explicit names no workstream. Err(-32003 invalid_argument) — the target (explicit OR ambient) is closed (§4.1: “new sessions may not bind to it”). All three fire before mint. The residual post-mint failure surface is bind_session’s save-to-disk I/O error (-32603 internal) — a daemon-side storage fault, not caller-triggerable; BindError::NotFound/Closed/ AlreadyBound are unreachable post-validation under the held lock and map to -32603 internal defensively. Test: protocol_tests::resolve_validate_bind_explicit_wins_over_active, protocol_tests::resolve_validate_bind_falls_back_to_active, protocol_tests::resolve_validate_bind_none_when_nothing_active, protocol_tests::resolve_validate_bind_rejects_closed_target_without_minting, protocol_tests::resolve_validate_bind_rejects_unknown_id_without_minting, protocol_tests::resolve_validate_bind_rejects_malformed_id_without_minting; the end-to-end no-phantom-session regression at both RPC surfaces in session::protocol::workstream_binding_tests::create_rejected_bind_leaves_no_phantom_session and task::protocol::tests::task_run_rejected_bind_leaves_no_phantom_session.