Repository navigation
fix(a2/auto-dream): make LLM-stub path honest (parked, not WouldConsolidate) #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,21 @@ | ||
| //! # jcode-auto-dream | ||
| //! | ||
| //! Background memory consolidation with three-gate triggering. | ||
| //! Background memory consolidation gate mechanics (time + session + lock). | ||
| //! | ||
| //! autoDream runs a background consolidation process that merges, prunes, and | ||
| //! strengthens the memory graph when three independent gates all pass: | ||
| //! This crate provides the **gate evaluation** half of auto-dream. It does | ||
| //! **not** perform the consolidation LLM call itself. The crate is the | ||
| //! standalone, side-effect-free gate library; the intended consolidation call | ||
| //! lives in `jcode-app-core::ambient::auto_dream`, which uses a real | ||
| //! `jcode_provider_core::Provider` to invoke the model. | ||
| //! | ||
| //! **Production status:** `SessionGate::is_satisfied()` always returns false | ||
| //! outside tests. The app-core caller also uses `evaluate()`, so its provider | ||
| //! call is unreachable. Consolidation remains parked pending session-state wiring. | ||
| //! | ||
| //! ## Three gates | ||
| //! | ||
| //! [`DreamCoordinator`] evaluates three independent gates. All three must pass | ||
| //! before consolidation is allowed to run: | ||
| //! | ||
| //! 1. **Time gate** - at least 24 hours have elapsed since the last consolidation. | ||
| //! 2. **Session gate** - at least 5 sessions have occurred since the last | ||
|
|
@@ -13,8 +25,25 @@ | |
| //! | ||
| //! Additional subsystems: | ||
| //! - **Scan throttle** - limits how often memory scans fire (10-minute cooldown). | ||
| //! - **Consolidation prompt** - builds the LLM prompt that performs the actual | ||
| //! memory merge/deduplicate/strengthen pass. | ||
| //! - **Consolidation prompt** - builds the LLM prompt payload that *would* be | ||
| //! sent to the model if consolidation were wired up here. The real caller in | ||
| //! `jcode-app-core` already builds its own prompt via this builder. | ||
| //! | ||
| //! # Parking status (AUDIT-2026-10-16) | ||
| //! | ||
| //! The previous `maybe_dream()` entry point in this crate was a stub: it | ||
| //! logged `"gates satisfied, consolidation would run"` and did nothing. | ||
| //! As of the A-2 audit finding (2026-10-16), that stub has been retired. | ||
| //! The crate now only provides the gate library. The parked consolidation | ||
| //! path lives in `jcode-app-core/src/ambient/auto_dream.rs`, which uses | ||
| //! the real LLM provider. | ||
| //! | ||
| //! If you want to wire a new caller from this crate, see | ||
| //! [`DreamCoordinator::evaluate`] and the [`consolidation_prompt`] builder. | ||
| //! Do **not** reintroduce the old `WouldConsolidate` log line. | ||
| //! | ||
| //! Audit owner: jcode / A-2 (stubs-mocks, 2026-10-16). | ||
| //! Memory Graph Phase A design: see `docs/plans/MEMORY_GRAPH_PLAN.md`. | ||
| //! | ||
| //! # Module overview | ||
| //! | ||
|
|
@@ -46,12 +75,41 @@ pub const DEFAULT_SCAN_THROTTLE_MINUTES: u64 = 10; | |
| /// Outcome of evaluating all three gates. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum DreamTrigger { | ||
| /// All three gates passed -- consolidation should run. | ||
| /// All three gates passed -- consolidation is allowed to run. | ||
| Ready, | ||
| /// One or more gates have not yet been satisfied. | ||
| NotReady { reason: &'static str }, | ||
| } | ||
|
|
||
| /// Outcome of a [`maybe_dream`] evaluation. | ||
| /// | ||
| /// `maybe_dream` is the historical, never-wired-in stub entry point that was | ||
| /// flagged in the A-2 audit (2026-10-16). It used to log | ||
| /// `"gates satisfied, consolidation would run"` and return nothing. It now | ||
| /// returns an explicit outcome so callers (and tests) can detect the parked | ||
| /// state instead of being misled by a log line. | ||
| /// | ||
| /// **Parking status:** the consolidation call itself is intentionally **not** | ||
| /// implemented in this crate. The intended consolidation path lives in | ||
| /// `jcode-app-core::ambient::auto_dream::maybe_dream`, which uses a real | ||
| /// provider. See `docs/plans/MEMORY_GRAPH_PLAN.md` for the design context and | ||
| /// the AUDIT-2026-10-16 comment in `lib.rs` for the audit trail. | ||
|
Comment on lines
+94
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Assessment: 🟠 Prompt for AI Agent 🤖This is a comment left during a code review.
**Path:** crates/jcode-auto-dream/src/lib.rs
**Line:** 90:92
**Comment:**
*Docstring Mismatch: `jcode-app-core` cannot reach its provider call in production: `evaluate()` checks `SessionGate::is_satisfied()`, which returns `false` outside tests.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix |
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum DreamOutcome { | ||
| /// Gates did not all pass; consolidation was not attempted. | ||
| Skipped { reason: &'static str }, | ||
| /// Gates all passed but the consolidation LLM call is intentionally | ||
| /// **not** made by this crate. The previous variant | ||
| /// (`WouldConsolidate`) implied it would run; the renamed variant | ||
| /// honestly states that this crate does not perform the call. | ||
| /// | ||
| /// The ambient caller (`jcode-app-core::ambient::auto_dream::maybe_dream`) | ||
| /// contains provider-call code, but cannot reach it while the session gate | ||
| /// always returns false in production. It does not go through | ||
| /// this function. | ||
| GatesSatisfiedNoLlmCall, | ||
| } | ||
|
|
||
| /// Top-level evaluator that checks all three gates and decides whether | ||
| /// consolidation should run. | ||
| #[derive(Debug)] | ||
|
|
@@ -71,6 +129,23 @@ impl DreamCoordinator { | |
| } | ||
| } | ||
|
|
||
| /// Test-only constructor that pre-opens all gates. | ||
| /// | ||
| /// Required because `SessionGate::is_satisfied()` is itself a stub | ||
| /// (always returns `false` in production) and `ConsolidationLock` | ||
| /// already returns `true` from `is_available()`. Without this hook, | ||
| /// the "all gates open" branch of [`Self::run`] is unreachable in | ||
| /// tests, so the parking contract (variant is `GatesSatisfiedNoLlmCall` | ||
| /// and not `WouldConsolidate`) cannot be asserted from outside. | ||
| #[cfg(test)] | ||
| pub fn for_testing_all_gates_open(data_dir: impl Into<std::path::PathBuf>) -> Self { | ||
| Self { | ||
| time_gate_open: true, | ||
| session_gate: SessionGate::new(0), | ||
| lock: ConsolidationLock::new(data_dir.into()), | ||
| } | ||
| } | ||
|
|
||
| /// Evaluate all three gates. Returns `Ready` only if every gate passes. | ||
| pub fn evaluate(&self) -> DreamTrigger { | ||
| if !self.time_gate_open { | ||
|
|
@@ -92,27 +167,58 @@ impl DreamCoordinator { | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum DreamOutcome { | ||
| /// Gates passed but no action taken (not yet satisfied). | ||
| Skipped { reason: &'static str }, | ||
| /// Consolidation would run here (stub for now). | ||
| WouldConsolidate, | ||
| impl DreamCoordinator { | ||
| /// Convenience wrapper around [`Self::evaluate`] that returns a | ||
| /// [`DreamOutcome`]. | ||
| /// | ||
| /// **This is a stub entry point.** It does not perform the | ||
| /// consolidation LLM call. The parked consolidation path lives in | ||
| /// `jcode-app-core::ambient::auto_dream::maybe_dream` and uses a real | ||
| /// provider. | ||
| pub fn run(&self) -> DreamOutcome { | ||
| match self.evaluate() { | ||
| DreamTrigger::Ready => { | ||
| // AUDIT-2026-10-16: parked pending Memory Graph Phase A design. | ||
| // The previous log line ("gates satisfied, consolidation would run") | ||
| // was a silent failure -- no consolidation actually happened. The | ||
| // renamed variant `GatesSatisfiedNoLlmCall` makes the parked state | ||
| // explicit so callers/tests cannot be misled by an "everything is | ||
| // fine" log message. | ||
| // | ||
| // Audit owner: jcode / A-2 (stubs-mocks). | ||
| // Design context: docs/plans/MEMORY_GRAPH_PLAN.md. | ||
| tracing::warn!( | ||
| "auto-dream: gates satisfied but no LLM call is made by this \ | ||
| crate; the parked consolidation path is in \ | ||
| jcode-app-core::ambient::auto_dream::maybe_dream \ | ||
| (AUDIT-2026-10-16, parked pending Memory Graph Phase A)" | ||
| ); | ||
| DreamOutcome::GatesSatisfiedNoLlmCall | ||
| } | ||
| DreamTrigger::NotReady { reason } => { | ||
| tracing::debug!(reason, "auto-dream: gates not satisfied"); | ||
| DreamOutcome::Skipped { reason } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Convenience entry point called from the ambient runner. | ||
| /// Evaluates all three gates; if they pass, triggers memory consolidation. | ||
| pub fn maybe_dream(data_dir: impl Into<std::path::PathBuf>, _total_cycles: u64) { | ||
| /// Historical entry point preserved for API stability, **but unused by any | ||
| /// live caller**. | ||
| /// | ||
| /// As of AUDIT-2026-10-16 this function no longer logs a misleading | ||
| /// "consolidation would run" message. It builds a [`DreamCoordinator`], | ||
| /// runs the gates via [`DreamCoordinator::run`], and returns the resulting | ||
| /// [`DreamOutcome`]. When gates pass it returns | ||
| /// [`DreamOutcome::GatesSatisfiedNoLlmCall`] and emits a `warn`-level log | ||
| /// that explicitly states the parked status. | ||
| /// | ||
| /// Do not reintroduce silent behavior here. New callers should call | ||
| /// `jcode-app-core::ambient::auto_dream::maybe_dream` instead, which | ||
| /// contains the provider call, currently unreachable behind the session gate. | ||
| pub fn maybe_dream(data_dir: impl Into<std::path::PathBuf>, _total_cycles: u64) -> DreamOutcome { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: The function signature now returns Reply with |
||
| let coordinator = DreamCoordinator::new(data_dir); | ||
| match coordinator.evaluate() { | ||
| DreamTrigger::Ready => { | ||
| tracing::info!("auto-dream: gates satisfied, consolidation would run"); | ||
| // TODO: actually run consolidation prompt via LLM | ||
| } | ||
| DreamTrigger::NotReady { reason } => { | ||
| tracing::debug!(reason, "auto-dream: gates not satisfied"); | ||
| } | ||
| } | ||
| coordinator.run() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -125,4 +231,36 @@ mod tests { | |
| let result = coord.evaluate(); | ||
| assert_ne!(result, DreamTrigger::Ready); | ||
| } | ||
|
|
||
| #[test] | ||
| fn run_returns_skipped_when_gates_unsatisfied() { | ||
| // Time gate starts closed -> Skipped, never GatesSatisfiedNoLlmCall. | ||
| let coord = DreamCoordinator::new("/tmp/auto-dream-test-skipped"); | ||
| let outcome = coord.run(); | ||
| assert!( | ||
| matches!(outcome, DreamOutcome::Skipped { .. }), | ||
| "expected Skipped, got {:?}", | ||
| outcome | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn run_returns_gates_satisfied_no_llm_call_when_all_open() { | ||
| // Force all three gates open. The renamed variant name is the | ||
| // parking contract; this test makes sure we did not silently | ||
| // regress to `WouldConsolidate`. The session gate is satisfied | ||
| // via the test-only override hook because `SessionGate::is_satisfied` | ||
| // is itself a stub (always `false` in production) and the parking | ||
| // branch of `run` would otherwise be unreachable. | ||
| let mut coord = DreamCoordinator::for_testing_all_gates_open("/tmp/auto-dream-test-ready"); | ||
| coord.session_gate.force_satisfied_for_tests(); | ||
|
Comment on lines
+255
to
+256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '120,155p;225,265p' crates/jcode-auto-dream/src/lib.rs
sed -n '95,135p' crates/jcode-auto-dream/src/session_gate.rsRepository: KooshaPari/KCode Length of output: 4812 🏁 Script executed: nl -ba crates/jcode-auto-dream/src/lib.rs | sed -n '115,175p;238,265p'
nl -ba crates/jcode-auto-dream/src/session_gate.rs | sed -n '90,135p;180,215p'Repository: KooshaPari/KCode Length of output: 6829 Keep the force call and correct the constructor documentation.
♻️ Suggested fix- /// Test-only constructor that pre-opens all gates.
+ /// Test-only constructor that opens the time gate and sets the session
+ /// threshold to zero.
+ ///
+ /// `SessionGate::is_satisfied()` still returns `false` until the caller
+ /// invokes `session_gate.force_satisfied_for_tests()`.🤖 Prompt for AI Agents |
||
| let outcome = coord.run(); | ||
| assert_eq!( | ||
| outcome, | ||
| DreamOutcome::GatesSatisfiedNoLlmCall, | ||
| "AUDIT-2026-10-16: this crate must not silently perform the LLM call; \ | ||
| if this assertion fails, the parking contract has regressed" | ||
| ); | ||
| // And the old variant must NOT exist; this would not compile if it did. | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,18 @@ | |
| //! UUID-named `.jsonl` files with mtime after the last consolidation. | ||
|
|
||
| use std::path::Path; | ||
|
|
||
| #[cfg(test)] | ||
| use std::cell::Cell; | ||
|
|
||
| #[cfg(test)] | ||
| thread_local! { | ||
| /// Test-only override for `SessionGate::is_satisfied`. When any test | ||
| /// calls `force_satisfied_for_tests`, this flag is set on the calling | ||
| /// thread and `is_satisfied` returns `true` for the rest of the test. | ||
| /// It is automatically dropped when the test thread exits. | ||
| static SESSION_GATE_TEST_OVERRIDE: Cell<bool> = const { Cell::new(false) }; | ||
| } | ||
| use std::time::SystemTime; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
|
|
@@ -89,8 +101,31 @@ impl SessionGate { | |
| state.sessions_since >= self.threshold | ||
| } | ||
| pub fn is_satisfied(&self) -> bool { | ||
| #[cfg(test)] | ||
| { | ||
| if SESSION_GATE_TEST_OVERRIDE.with(Cell::get) { | ||
| return true; | ||
| } | ||
| } | ||
| false | ||
| } | ||
|
|
||
| /// Test-only escape hatch: force `is_satisfied` to return `true`. | ||
| /// | ||
| /// In production this method is compiled out and `is_satisfied` is | ||
| /// always `false` (the session gate logic itself is a stub pending the | ||
| /// Memory Graph Phase A design). The parking contract test in | ||
| /// `lib::tests` needs to drive the "all gates open" branch of | ||
| /// `DreamCoordinator::run`, which would otherwise be unreachable. | ||
| #[cfg(test)] | ||
| pub fn force_satisfied_for_tests(&mut self) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: Unused
|
||
| // No state to mutate: `is_satisfied` reads the test-only flag | ||
| // below, which we set here. | ||
| // (We keep the field layout minimal by using a thread-local | ||
| // override rather than a struct field; see the test override | ||
| // at the bottom of this module.) | ||
| SESSION_GATE_TEST_OVERRIDE.with(|cell| cell.set(true)); | ||
| } | ||
| pub fn record_session(&self, mut state: SessionGateState) -> SessionGateState { | ||
| state.sessions_since = state.sessions_since.saturating_add(1); | ||
| state.total_sessions = state.total_sessions.saturating_add(1); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
sed -n '1,55p;155,220p' crates/jcode-auto-dream/src/lib.rsRepository: KooshaPari/KCode
Length of output: 5469
🏁 Script executed:
Repository: KooshaPari/KCode
Length of output: 41074
Clarify that the old behavior was retired, not the API.
maybe_dream()remains public for API stability, andDreamCoordinator::run()remains public. Both returnDreamOutcome. When all gates pass, the parked path returnsDreamOutcome::GatesSatisfiedNoLlmCall.The gate-only description is accurate for LLM behavior, but the parking section should not imply that the retained entry point was removed.
📝 Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents