trusty-code 0.8.2

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
//! Workstream domain model + flat JSON persistence + boot reconciliation
//! (DOC-48 Phase 1A, issue #3293).
//!
//! # Spec References
//!
//! - [`SPEC-WS-01~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-01~draft)
//! - [`SPEC-WS-02~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-02~draft)
//! - [`SPEC-WS-03~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-03~draft)
//! - [`SPEC-WS-05~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-05~draft)
//! - [`SPEC-WS-06~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-06~draft)
//! - [`SPEC-WS-07~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-07~draft) AC-7
//!
//! Why: DOC-39 §4B introduced **Workstream** as a durable named grouping of
//! sessions that must survive daemon restarts, but tcode's `SessionRegistry`
//! is in-memory only — a restart silently loses the workstream list (DOC-48
//! §1.1). This module is the foundation slice that fixes that: a serializable
//! domain object plus a single flat JSON store per project, written with an
//! atomic temp+rename so a crash mid-write never corrupts the file.
//!
//! **Disambiguation (DOC-48 §11):** [`trusty_agents_common::workstreams::Workstream`]
//! is a *different* type — the cross-tool engineering-lead ledger entry
//! (DOC-44). This module's [`Workstream`] is tcode's own native domain object.
//! Code that touches both MUST use qualified imports
//! (`trusty_agents_common::workstreams::Workstream` for the ledger,
//! `trusty_code::workstreams::Workstream` for tcode-native state) — never a
//! bare `use ...::Workstream` in a module that needs both.
//!
//! What: [`WorkstreamId`] (a UUID newtype), [`WorkstreamState`] (the computed
//! `active | idle | closed` enum — never persisted, see §2.2/§3.2),
//! [`Workstream`] (the domain record — see [`model`]); [`WorkstreamStore`]
//! (the flat-JSON, atomic-write, fingerprint-reload store plus boot
//! reconciliation — see [`store`]); path/filename derivation from the
//! daemon's [`crate::binding::ProjectBinding`] (see `path`); (issue #3294)
//! [`activation`] — the activation-lock exclusivity layer above the store
//! (`ActiveConflict`/force-switch semantics `WorkstreamStore::set_active`
//! deliberately left out) — [`protocol`], the full `workstream.*`
//! JSON-RPC surface (`create`/`get`/`list`/`close` from #3295 plus
//! `activate`/`deactivate` from #3294, built on [`activation`]); and (issue
//! #3297) [`sse`] — the workstream fan-out aggregation, built directly on
//! [`store::WorkstreamStore`] (bypassing the JSON-RPC router) and served as
//! the socket's `workstream.events` stream since #6637.
//!
//! **Scope note (Phase 1A):** #3293 built ONLY the domain model and
//! persistence layer (this module's `model`/`path`/`store` submodules);
//! #3294 added the activation-lock RPC semantics (`activation`, plus
//! `activate`/`deactivate` in [`protocol`]); #3295 added the rest of
//! `protocol`'s surface (`create`/`get`/`list`/`close`) and REST wrappers
//! (retired with the HTTP listener, #6637); #3297 adds [`sse`] —
//! the workstream aggregation — plus
//! `activation`'s new role as the sole publisher of
//! `crate::events::Event::WorkstreamActivationChanged`. The CLI (#3296) and
//! the `trusty-agents-common` extraction of [`sse`]'s aggregation layer
//! (Phase 1B, #3299) remain out of scope.
//!
//! Test: `cargo test -p trusty-code workstreams::` exercises every submodule
//! in place; see each submodule's own `Test:` line for specifics.
//!
//! [`Workstream`]: crate::workstreams::Workstream
//! [`WorkstreamId`]: crate::workstreams::WorkstreamId
//! [`WorkstreamState`]: crate::workstreams::WorkstreamState
//! [`model`]: crate::workstreams::model
//! [`WorkstreamStore`]: crate::workstreams::WorkstreamStore
//! [`store`]: crate::workstreams::store
//! [`activation`]: crate::workstreams::activation
//! [`protocol`]: crate::workstreams::protocol
//! [`sse`]: crate::workstreams::sse
//! [`store::WorkstreamStore`]: crate::workstreams::store::WorkstreamStore

pub mod activation;
/// (#6637) `workstream.events`: the socket transport's live fan-out, a bridge
/// onto the `sse::aggregate_live` combinator.
pub mod events_stream;
pub mod model;
mod path;
pub mod protocol;
pub mod sse;
pub mod store;

pub use activation::{ActivateOutcome, ActivationError, SharedWorkstreamStore};
pub use model::{Workstream, WorkstreamId, WorkstreamState};
pub use path::{default_data_dir, store_path};
pub use sse::WorkstreamEventEnvelope;
pub use store::{BindError, ReconcileOutcome, StoreError, WorkstreamStore};

/// Test-only helper: a fresh, in-memory-backed [`SharedWorkstreamStore`]
/// rooted at a throwaway tempdir (issue #3298).
///
/// Why: `session::protocol::register`/`task::protocol::register` both now
/// require a `SharedWorkstreamStore` — every test call site across the
/// crate that wires either `register` but does not itself care about
/// workstream binding needs a cheap, disposable store rather than resolving
/// the real `~/.trusty-code` directory. One helper here (rather than each
/// call site re-deriving its own) keeps that boilerplate in exactly one
/// place.
/// What: loads a fresh [`WorkstreamStore`] at a tempdir path and wraps it in
/// the same `Arc<tokio::sync::Mutex<_>>` shape production code uses. The
/// `TempDir` guard is intentionally dropped here — `WorkstreamStore::save`
/// re-creates its parent directory on every write (see that method's docs),
/// so a test that never asserts on persisted file contents (the common case)
/// is unaffected by the directory's removal.
#[cfg(test)]
pub(crate) async fn test_shared_store() -> SharedWorkstreamStore {
    let dir = tempfile::tempdir().expect("tempdir for test workstream store");
    let store = WorkstreamStore::load(dir.path().join("workstreams-test.json"))
        .await
        .expect("load fresh workstream store");
    std::sync::Arc::new(tokio::sync::Mutex::new(store))
}