Skip to main content

trusty_code/workstreams/
mod.rs

1//! Workstream domain model + flat JSON persistence + boot reconciliation
2//! (DOC-48 Phase 1A, issue #3293).
3//!
4//! # Spec References
5//!
6//! - [`SPEC-WS-01~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-01~draft)
7//! - [`SPEC-WS-02~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-02~draft)
8//! - [`SPEC-WS-03~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-03~draft)
9//! - [`SPEC-WS-05~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-05~draft)
10//! - [`SPEC-WS-06~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-06~draft)
11//! - [`SPEC-WS-07~draft`](docs/specs/DOC-48-tcode-workstreams.md#SPEC-WS-07~draft) AC-7
12//!
13//! Why: DOC-39 §4B introduced **Workstream** as a durable named grouping of
14//! sessions that must survive daemon restarts, but tcode's `SessionRegistry`
15//! is in-memory only — a restart silently loses the workstream list (DOC-48
16//! §1.1). This module is the foundation slice that fixes that: a serializable
17//! domain object plus a single flat JSON store per project, written with an
18//! atomic temp+rename so a crash mid-write never corrupts the file.
19//!
20//! **Disambiguation (DOC-48 §11):** [`trusty_agents_common::workstreams::Workstream`]
21//! is a *different* type — the cross-tool engineering-lead ledger entry
22//! (DOC-44). This module's [`Workstream`] is tcode's own native domain object.
23//! Code that touches both MUST use qualified imports
24//! (`trusty_agents_common::workstreams::Workstream` for the ledger,
25//! `trusty_code::workstreams::Workstream` for tcode-native state) — never a
26//! bare `use ...::Workstream` in a module that needs both.
27//!
28//! What: [`WorkstreamId`] (a UUID newtype), [`WorkstreamState`] (the computed
29//! `active | idle | closed` enum — never persisted, see §2.2/§3.2),
30//! [`Workstream`] (the domain record — see [`model`]); [`WorkstreamStore`]
31//! (the flat-JSON, atomic-write, fingerprint-reload store plus boot
32//! reconciliation — see [`store`]); path/filename derivation from the
33//! daemon's [`crate::binding::ProjectBinding`] (see `path`); (issue #3294)
34//! [`activation`] — the activation-lock exclusivity layer above the store
35//! (`ActiveConflict`/force-switch semantics `WorkstreamStore::set_active`
36//! deliberately left out) — [`protocol`], the full `workstream.*`
37//! JSON-RPC surface (`create`/`get`/`list`/`close` from #3295 plus
38//! `activate`/`deactivate` from #3294, built on [`activation`]); and (issue
39//! #3297) [`sse`] — the `GET /workstreams/{id}/events` fan-out aggregation
40//! route, built directly on [`store::WorkstreamStore`] (bypassing the
41//! JSON-RPC router, mirroring `crate::serve::http::session_events_sse`'s
42//! per-session precedent).
43//!
44//! **Scope note (Phase 1A):** #3293 built ONLY the domain model and
45//! persistence layer (this module's `model`/`path`/`store` submodules);
46//! #3294 added the activation-lock RPC semantics (`activation`, plus
47//! `activate`/`deactivate` in [`protocol`]); #3295 added the rest of
48//! `protocol`'s surface (`create`/`get`/`list`/`close`) and the REST wrappers
49//! (`crate::serve::rest::workstreams`); #3297 (this ticket) adds [`sse`] —
50//! the `GET /workstreams/{id}/events` aggregation route — plus
51//! `activation`'s new role as the sole publisher of
52//! `crate::events::Event::WorkstreamActivationChanged`. The CLI (#3296) and
53//! the `trusty-agents-common` extraction of [`sse`]'s aggregation layer
54//! (Phase 1B, #3299) remain out of scope.
55//!
56//! Test: `cargo test -p trusty-code workstreams::` exercises every submodule
57//! in place; see each submodule's own `Test:` line for specifics.
58//!
59//! [`Workstream`]: crate::workstreams::Workstream
60//! [`WorkstreamId`]: crate::workstreams::WorkstreamId
61//! [`WorkstreamState`]: crate::workstreams::WorkstreamState
62//! [`model`]: crate::workstreams::model
63//! [`WorkstreamStore`]: crate::workstreams::WorkstreamStore
64//! [`store`]: crate::workstreams::store
65//! [`activation`]: crate::workstreams::activation
66//! [`protocol`]: crate::workstreams::protocol
67//! [`sse`]: crate::workstreams::sse
68//! [`store::WorkstreamStore`]: crate::workstreams::store::WorkstreamStore
69
70pub mod activation;
71pub mod model;
72mod path;
73pub mod protocol;
74pub mod sse;
75pub mod store;
76
77pub use activation::{ActivateOutcome, ActivationError, SharedWorkstreamStore};
78pub use model::{Workstream, WorkstreamId, WorkstreamState};
79pub use path::{default_data_dir, store_path};
80pub use sse::WorkstreamEventEnvelope;
81pub use store::{BindError, ReconcileOutcome, StoreError, WorkstreamStore};
82
83/// Test-only helper: a fresh, in-memory-backed [`SharedWorkstreamStore`]
84/// rooted at a throwaway tempdir (issue #3298).
85///
86/// Why: `session::protocol::register`/`task::protocol::register` both now
87/// require a `SharedWorkstreamStore` — every test call site across the
88/// crate that wires either `register` but does not itself care about
89/// workstream binding needs a cheap, disposable store rather than resolving
90/// the real `~/.trusty-code` directory. One helper here (rather than each
91/// call site re-deriving its own) keeps that boilerplate in exactly one
92/// place.
93/// What: loads a fresh [`WorkstreamStore`] at a tempdir path and wraps it in
94/// the same `Arc<tokio::sync::Mutex<_>>` shape production code uses. The
95/// `TempDir` guard is intentionally dropped here — `WorkstreamStore::save`
96/// re-creates its parent directory on every write (see that method's docs),
97/// so a test that never asserts on persisted file contents (the common case)
98/// is unaffected by the directory's removal.
99#[cfg(test)]
100pub(crate) async fn test_shared_store() -> SharedWorkstreamStore {
101    let dir = tempfile::tempdir().expect("tempdir for test workstream store");
102    let store = WorkstreamStore::load(dir.path().join("workstreams-test.json"))
103        .await
104        .expect("load fresh workstream store");
105    std::sync::Arc::new(tokio::sync::Mutex::new(store))
106}