oa/ml/rl/environment/compiled.rs
1//! Compiled-environment contract (OA-CC-20).
2//!
3//! A compiled environment declares reset, step and observation programs that
4//! `Runtime::capture` records once and `CompiledRollout` replays across a fixed
5//! PPO horizon without per-step host recording. The design record is
6//! `oaCompiledEnvironment.md`.
7//!
8//! The seven-word rollout cursor shared by every replay program is:
9//!
10//! | Word | Role |
11//! |---:|---|
12//! | 0 | Step index, advanced on device |
13//! | 1 | Philox/action-sampling state, low |
14//! | 2 | Philox/action-sampling state, high |
15//! | 3 | Reset-seed state, low |
16//! | 4 | Reset-seed state, high |
17//! | 5 | Final-step flag for terminal handling |
18//! | 6 | Rollout append cursor |
19
20use crate::{Matrix, Result};
21
22use super::{Environment, EnvironmentTransition};
23
24/// Replayable reset/step/observation contract for one [`Environment`].
25///
26/// Implementations must keep every replay-visible piece of episode state in
27/// device-resident matrices: the captured plans bind those buffers once and
28/// replay the identical command sequence, so no host-computed value may
29/// differ across replays. Deterministic per-episode variation belongs in
30/// on-device counters (as CartPole's `episode_index` does) or in the cursor
31/// words above.
32pub trait CompiledEnvironment: Environment {
33 /// Whether the environment may record a replay plan at all.
34 ///
35 /// Trading uses this to reject capture while its tables are stale;
36 /// environments without externally supplied state return `true`.
37 fn replay_valid(&self) -> bool {
38 true
39 }
40
41 /// Whether committed device state lets a continuation collection skip the
42 /// reset plan and capture a resume plan instead.
43 fn replay_initialized(&self) -> bool;
44
45 /// Environment-owned matrices whose initialized-state tracking the
46 /// capture must mark: every buffer a replay program reads before writing.
47 /// Write-only transition outputs are excluded.
48 fn replay_slots(&self) -> Vec<&Matrix>;
49
50 /// Stable identity over every input the captured plans depend on: state
51 /// matrix value identities plus the complete recipe that changes command
52 /// output. The per-collection seed is excluded — it arrives through
53 /// cursor words 3 and 4.
54 fn replay_identity(&self) -> Vec<u64>;
55
56 /// Record the environment's reset program for plan capture.
57 ///
58 /// `only_done` resets just the lanes whose `done` flag is set. `cursor`
59 /// carries reset entropy in words 3 and 4 for environments that consume
60 /// it; environments whose reset derives deterministically from on-device
61 /// episode counters may ignore it.
62 ///
63 /// # Errors
64 ///
65 /// Returns the implementation's reset recording failures.
66 fn record_reset_replay(&mut self, only_done: bool, cursor: &Matrix) -> Result<()>;
67
68 /// Record the environment's step program for plan capture.
69 ///
70 /// `action` is the policy-selected discrete/continuous action buffer for
71 /// this step; `cursor` carries step entropy for environments that consume
72 /// it. The returned transition must reference device matrices the replay
73 /// appender can copy into cursor-addressed rollout storage.
74 ///
75 /// # Errors
76 ///
77 /// Returns the implementation's step recording failures.
78 fn record_step_replay(
79 &mut self,
80 action: &Matrix,
81 cursor: &Matrix,
82 ) -> Result<EnvironmentTransition>;
83
84 /// Rebind the observation storage the captured reset/step plans write.
85 ///
86 /// After plan capture the environment's observation must refer to the
87 /// storage bound into the plans. Environments whose observation is a
88 /// fixed state matrix (captured in place) implement this as a no-op.
89 fn replay_observation(&mut self, observation: Matrix);
90
91 /// Observe lane status after exact completion of a submitted chunk.
92 ///
93 /// Environments with a device status word reject failed lanes here; the
94 /// default accepts everything.
95 ///
96 /// # Errors
97 ///
98 /// Returns readback/completion failure or a rejected lane.
99 fn check_status(&self) -> Result<()> {
100 Ok(())
101 }
102}