返回 CodeWhale
mod.rs
根目录 / crates / engine / src / work_graph / mod.rs
1 //! Work Graph — the single authoritative work ledger for a session.
2 //!
3 //! One graph carries objectives, plan steps, operations, evidence, blockers,
4 //! and approvals. **Invariant: one graph writes every projection; projections
5 //! never write each other.** Plan and todo views, work-surface rows, and the
6 //! inspector all derive from [`WorkGraphSnapshot`] through pure functions.
7 //!
8 //! Why a graph instead of parallel trackers: flat status lists let an agent
9 //! mark work "done" by assertion, lose dependency structure, and cannot say
10 //! what evidence backed a completion. Here completion and verification are
11 //! distinct states, `Verified` is unreachable without a satisfying evidence
12 //! path (V4, fail-closed), dependencies are first-class edges (acyclic, V1),
13 //! and liveness truth stays with the owning subsystems — the graph records
14 //! observations, it never invents them.
15 //!
16 //! Session persistence, legacy import, UI projections, and liveness adapters
17 //! are wired: `session_manager` stores a snapshot, `tools/shell` and
18 //! `tools/tasks` observe operations, and the engine records owner liveness.
19
20 mod compat;
21 mod digest;
22 mod events;
23 mod ids;
24 mod liveness;
25 mod migration;
26 mod model;
27 mod reducer;
28 mod runtime;
29 mod validate;
30
31 #[cfg(test)]
32 mod tests;
33
34 pub use compat::{PlanProjection, TodoProjection, project_plan, project_todos};
35 pub use digest::{format_operation_digest, format_operation_digest_parts};
36 pub use events::{
37 ApprovalRef, CancelOutcome, ChangeCtx, ChangeReceipt, ObservationSummary, OperationObservation,
38 OwnerState, ProposedNodeUpdate, WorkGraphChange, WorkGraphProposal, WorkNodePatch,
39 };
40 pub use ids::{BindingId, ChangeId, ProposalId, WorkEdgeId, WorkNodeId};
41 pub use liveness::{
42 OperationIntent, OperationOwnerSnapshot, fleet_task_owner_snapshot, lane_owner_snapshot,
43 task_owner_snapshot,
44 };
45 pub use migration::import_legacy;
46 pub use model::{
47 ACTIVITY_CAP, AcceptanceRequirement, BoundedSet, BoundedVec, CompatPlanMetadata,
48 CompatProjectionState, CompatTodoBinding, EdgeKind, EvidenceKind, EvidenceKindTag, EvidenceRef,
49 EvidenceRefError, HISTORY_CAP, IdempotencyKey, NodeKind, NodeState, OperationBinding,
50 Provenance, SCHEMA_VERSION, SEEN_KEYS_CAP, Ts, WorkActivityEvent, WorkEdge, WorkGraphSnapshot,
51 WorkNode, external_identity_is_well_formed,
52 };
53 pub use model::{ENDED_OPERATION_CAP, constrained_effective_reasoning_for_route};
54 pub use reducer::apply;
55 pub use reducer::evictable_operations;
56 pub use runtime::{ACTIVE_OPERATION_SUMMARY_END, ACTIVE_OPERATION_SUMMARY_START};
57 pub use runtime::{SharedWorkRuntime, WorkRuntime, WorkRuntimeSnapshot, new_shared_work_runtime};
58 pub use validate::{ValidationCode, ValidationReport, Violation, validate};
59
60 /// Convenience wrapper owning the current snapshot. [`WorkGraph::apply`]
61 /// commits the reducer's result on success and leaves the snapshot untouched
62 /// on rejection — the fail-closed contract, packaged.
63 #[derive(Debug, Clone, PartialEq)]
64 pub struct WorkGraph {
65 snapshot: WorkGraphSnapshot,
66 }
67
68 impl WorkGraph {
69 #[must_use]
70 pub fn new() -> Self {
71 Self {
72 snapshot: WorkGraphSnapshot::new(),
73 }
74 }
75
76 #[must_use]
77 pub fn from_snapshot(snapshot: WorkGraphSnapshot) -> Self {
78 Self { snapshot }
79 }
80
81 #[must_use]
82 pub fn snapshot(&self) -> &WorkGraphSnapshot {
83 &self.snapshot
84 }
85
86 #[must_use]
87 pub fn into_snapshot(self) -> WorkGraphSnapshot {
88 self.snapshot
89 }
90
91 /// Apply one change through the reducer. On `Ok` the new snapshot is
92 /// committed; on `Err` the held snapshot is unchanged.
93 pub fn apply(
94 &mut self,
95 change: WorkGraphChange,
96 ctx: ChangeCtx,
97 ) -> Result<ChangeReceipt, ValidationReport> {
98 let (next, receipt) = reducer::apply(&self.snapshot, change, ctx)?;
99 self.snapshot = next;
100 Ok(receipt)
101 }
102 }
103
104 impl Default for WorkGraph {
105 fn default() -> Self {
106 Self::new()
107 }
108 }
109
109 lines RUST