huskies: merge 491_story_watcher_fires_on_crdt_state_transitions_instead_of_filesystem_events
This commit is contained in:
@@ -65,8 +65,9 @@ fn move_item<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(491): Wire up CRDT state transitions once the watcher story lands.
|
||||
// crate::db::crdt::crdt_write(story_id, target_dir, &target_path);
|
||||
// Write state through CRDT ops (and legacy shadow table) so subscribers
|
||||
// are notified of the stage transition without relying on the filesystem watcher.
|
||||
crate::db::shadow_write(story_id, target_dir, &target_path);
|
||||
|
||||
slog!("[lifecycle] Moved '{story_id}' from work/{src_dir}/ to work/{target_dir}/");
|
||||
Ok(Some(src_dir))
|
||||
|
||||
@@ -60,12 +60,23 @@ pub(super) fn is_story_blocked(project_root: &Path, stage_dir: &str, story_id: &
|
||||
/// Return `true` if the story has any `depends_on` entries that are not yet in
|
||||
/// `5_done` or `6_archived`.
|
||||
///
|
||||
/// Auto-assign calls this to hold back stories whose dependencies haven't landed.
|
||||
/// Reads dependency state from the CRDT document first. Falls back to the
|
||||
/// filesystem when the CRDT layer is not initialised.
|
||||
pub(super) fn has_unmet_dependencies(
|
||||
project_root: &Path,
|
||||
stage_dir: &str,
|
||||
story_id: &str,
|
||||
) -> bool {
|
||||
// Prefer CRDT-based check.
|
||||
let crdt_deps = crate::crdt_state::check_unmet_deps_crdt(story_id);
|
||||
if !crdt_deps.is_empty() {
|
||||
return true;
|
||||
}
|
||||
// If the CRDT had the item and returned empty deps, it means all are met.
|
||||
if crate::crdt_state::read_item(story_id).is_some() {
|
||||
return false;
|
||||
}
|
||||
// Fallback: filesystem check (CRDT not initialised or item not yet in CRDT).
|
||||
!crate::io::story_metadata::check_unmet_deps(project_root, stage_dir, story_id).is_empty()
|
||||
}
|
||||
|
||||
|
||||
+187
-4
@@ -1,9 +1,13 @@
|
||||
/// CRDT state layer for pipeline state, backed by SQLite.
|
||||
///
|
||||
/// Replaces the filesystem as the primary source of truth for pipeline item
|
||||
/// The CRDT document is the primary source of truth for pipeline item
|
||||
/// metadata (stage, name, agent, etc.). CRDT ops are persisted to SQLite so
|
||||
/// state survives restarts. The filesystem `.huskies/work/` directories are
|
||||
/// still updated as a secondary output for backwards compatibility.
|
||||
///
|
||||
/// Stage transitions detected by `write_item()` are broadcast as [`CrdtEvent`]s
|
||||
/// so subscribers (auto-assign, WebSocket, notifications) can react without
|
||||
/// polling the filesystem.
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
@@ -18,10 +22,34 @@ use serde_json::json;
|
||||
use sqlx::sqlite::SqliteConnectOptions;
|
||||
use sqlx::SqlitePool;
|
||||
use std::path::Path;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::{broadcast, mpsc};
|
||||
|
||||
use crate::slog;
|
||||
|
||||
// ── CRDT events ─────────────────────────────────────────────────────
|
||||
|
||||
/// An event emitted when a pipeline item's stage changes in the CRDT document.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CrdtEvent {
|
||||
/// Work item ID (e.g. `"42_story_my_feature"`).
|
||||
pub story_id: String,
|
||||
/// The stage the item was in before this transition, or `None` for new items.
|
||||
pub from_stage: Option<String>,
|
||||
/// The stage the item is now in.
|
||||
pub to_stage: String,
|
||||
/// Human-readable story name from the CRDT document.
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
/// Subscribe to CRDT state transition events.
|
||||
///
|
||||
/// Returns `None` if the CRDT layer has not been initialised yet.
|
||||
pub fn subscribe() -> Option<broadcast::Receiver<CrdtEvent>> {
|
||||
CRDT_EVENT_TX.get().map(|tx| tx.subscribe())
|
||||
}
|
||||
|
||||
static CRDT_EVENT_TX: OnceLock<broadcast::Sender<CrdtEvent>> = OnceLock::new();
|
||||
|
||||
// ── CRDT document types ──────────────────────────────────────────────
|
||||
|
||||
#[add_crdt_fields]
|
||||
@@ -156,6 +184,11 @@ pub async fn init(db_path: &Path) -> Result<(), sqlx::Error> {
|
||||
};
|
||||
|
||||
let _ = CRDT_STATE.set(Mutex::new(state));
|
||||
|
||||
// Initialise the CRDT event broadcast channel.
|
||||
let (event_tx, _) = broadcast::channel::<CrdtEvent>(256);
|
||||
let _ = CRDT_EVENT_TX.set(event_tx);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -214,6 +247,9 @@ where
|
||||
///
|
||||
/// If the item exists, updates its registers. If not, inserts a new item
|
||||
/// into the list. All ops are signed and persisted to SQLite.
|
||||
///
|
||||
/// When the stage changes (or a new item is created), a [`CrdtEvent`] is
|
||||
/// broadcast so subscribers can react to the transition.
|
||||
pub fn write_item(
|
||||
story_id: &str,
|
||||
stage: &str,
|
||||
@@ -231,9 +267,13 @@ pub fn write_item(
|
||||
};
|
||||
|
||||
if let Some(&idx) = state.index.get(story_id) {
|
||||
// Capture the old stage before updating so we can detect transitions.
|
||||
let old_stage = match state.crdt.doc.items[idx].stage.view() {
|
||||
JsonValue::String(s) => Some(s),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// Update existing item registers.
|
||||
// Each op is created, signed, applied, and persisted in a block so
|
||||
// borrows do not overlap between &mut crdt (set) and &keypair (sign).
|
||||
apply_and_persist(&mut state, |s| {
|
||||
s.crdt.doc.items[idx].stage.set(stage.to_string())
|
||||
});
|
||||
@@ -263,6 +303,22 @@ pub fn write_item(
|
||||
s.crdt.doc.items[idx].depends_on.set(d.to_string())
|
||||
});
|
||||
}
|
||||
|
||||
// Broadcast a CrdtEvent if the stage actually changed.
|
||||
let stage_changed = old_stage.as_deref() != Some(stage);
|
||||
if stage_changed {
|
||||
// Read the current name from the CRDT document for the event.
|
||||
let current_name = match state.crdt.doc.items[idx].name.view() {
|
||||
JsonValue::String(s) if !s.is_empty() => Some(s),
|
||||
_ => None,
|
||||
};
|
||||
emit_event(CrdtEvent {
|
||||
story_id: story_id.to_string(),
|
||||
from_stage: old_stage,
|
||||
to_stage: stage.to_string(),
|
||||
name: current_name,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Insert new item.
|
||||
let item_json: JsonValue = json!({
|
||||
@@ -282,6 +338,21 @@ pub fn write_item(
|
||||
|
||||
// Rebuild index after insertion (indices may shift).
|
||||
state.index = rebuild_index(&state.crdt);
|
||||
|
||||
// Broadcast a CrdtEvent for the new item.
|
||||
emit_event(CrdtEvent {
|
||||
story_id: story_id.to_string(),
|
||||
from_stage: None,
|
||||
to_stage: stage.to_string(),
|
||||
name: name.map(String::from),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Broadcast a CRDT event to all subscribers.
|
||||
fn emit_event(event: CrdtEvent) {
|
||||
if let Some(tx) = CRDT_EVENT_TX.get() {
|
||||
let _ = tx.send(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,6 +427,40 @@ fn extract_item_view(item: &PipelineItemCrdt) -> Option<PipelineItemView> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Check whether a dependency (by numeric ID prefix) is in `5_done` or `6_archived`
|
||||
/// according to CRDT state.
|
||||
///
|
||||
/// Returns `true` if the dependency is satisfied (item found in a done stage).
|
||||
pub fn dep_is_done_crdt(dep_number: u32) -> bool {
|
||||
let prefix = format!("{dep_number}_");
|
||||
if let Some(items) = read_all_items() {
|
||||
items.iter().any(|item| {
|
||||
item.story_id.starts_with(&prefix)
|
||||
&& matches!(item.stage.as_str(), "5_done" | "6_archived")
|
||||
})
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Check unmet dependencies for a story by reading its `depends_on` from the
|
||||
/// CRDT document and checking each dependency against CRDT state.
|
||||
///
|
||||
/// Returns the list of dependency numbers that are NOT in `5_done` or `6_archived`.
|
||||
pub fn check_unmet_deps_crdt(story_id: &str) -> Vec<u32> {
|
||||
let item = match read_item(story_id) {
|
||||
Some(i) => i,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
let deps = match item.depends_on {
|
||||
Some(d) => d,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
deps.into_iter()
|
||||
.filter(|&dep| !dep_is_done_crdt(dep))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Hex-encode a byte slice (no external dep needed).
|
||||
mod hex {
|
||||
pub fn encode(bytes: &[u8]) -> String {
|
||||
@@ -610,4 +715,82 @@ mod tests {
|
||||
assert_eq!(op.id(), deserialized.id());
|
||||
assert_eq!(op.inner.seq, deserialized.inner.seq);
|
||||
}
|
||||
|
||||
// ── CrdtEvent tests ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn crdt_event_has_expected_fields() {
|
||||
let evt = CrdtEvent {
|
||||
story_id: "42_story_foo".to_string(),
|
||||
from_stage: Some("1_backlog".to_string()),
|
||||
to_stage: "2_current".to_string(),
|
||||
name: Some("Foo Feature".to_string()),
|
||||
};
|
||||
assert_eq!(evt.story_id, "42_story_foo");
|
||||
assert_eq!(evt.from_stage.as_deref(), Some("1_backlog"));
|
||||
assert_eq!(evt.to_stage, "2_current");
|
||||
assert_eq!(evt.name.as_deref(), Some("Foo Feature"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crdt_event_clone_preserves_data() {
|
||||
let evt = CrdtEvent {
|
||||
story_id: "10_story_bar".to_string(),
|
||||
from_stage: None,
|
||||
to_stage: "1_backlog".to_string(),
|
||||
name: None,
|
||||
};
|
||||
let cloned = evt.clone();
|
||||
assert_eq!(cloned.story_id, "10_story_bar");
|
||||
assert!(cloned.from_stage.is_none());
|
||||
assert!(cloned.name.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_event_is_noop_when_channel_not_initialised() {
|
||||
// Before CRDT_EVENT_TX is set, emit_event should not panic.
|
||||
// This test verifies the guard clause works. In test binaries the
|
||||
// OnceLock may already be set by another test, so we just verify
|
||||
// the function doesn't panic regardless.
|
||||
emit_event(CrdtEvent {
|
||||
story_id: "99_story_noop".to_string(),
|
||||
from_stage: None,
|
||||
to_stage: "1_backlog".to_string(),
|
||||
name: None,
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crdt_event_broadcast_channel_round_trip() {
|
||||
let (tx, mut rx) = broadcast::channel::<CrdtEvent>(16);
|
||||
let evt = CrdtEvent {
|
||||
story_id: "70_story_broadcast".to_string(),
|
||||
from_stage: Some("1_backlog".to_string()),
|
||||
to_stage: "2_current".to_string(),
|
||||
name: Some("Broadcast Test".to_string()),
|
||||
};
|
||||
tx.send(evt).unwrap();
|
||||
|
||||
let received = rx.try_recv().unwrap();
|
||||
assert_eq!(received.story_id, "70_story_broadcast");
|
||||
assert_eq!(received.from_stage.as_deref(), Some("1_backlog"));
|
||||
assert_eq!(received.to_stage, "2_current");
|
||||
assert_eq!(received.name.as_deref(), Some("Broadcast Test"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dep_is_done_crdt_returns_false_when_no_crdt_state() {
|
||||
// When the global CRDT state is not initialised (or in a test environment),
|
||||
// dep_is_done_crdt should return false rather than panicking.
|
||||
// Note: in the test binary the global may or may not be initialised,
|
||||
// but the function should never panic either way.
|
||||
let _ = dep_is_done_crdt(9999);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_unmet_deps_crdt_returns_empty_when_item_not_found() {
|
||||
// Non-existent story should return empty deps.
|
||||
let result = check_unmet_deps_crdt("nonexistent_story");
|
||||
assert!(result.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,16 +215,12 @@ fn load_stage_items(
|
||||
) -> Result<Vec<UpcomingStory>, String> {
|
||||
let root = ctx.state.get_project_root()?;
|
||||
|
||||
// TODO(491): Merge CRDT layer items once the watcher story lands.
|
||||
|
||||
// Scan the filesystem for pipeline items.
|
||||
let dir = root.join(".huskies").join("work").join(stage_dir);
|
||||
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
|
||||
let seen: std::collections::HashSet<String> = std::collections::HashSet::new();
|
||||
let mut stories = Vec::new();
|
||||
|
||||
// TODO(491): Add CRDT items once the watcher story lands.
|
||||
|
||||
// Then, add filesystem items not in the CRDT (backwards compat).
|
||||
// Filesystem items (backwards compat fallback when CRDT is not initialised).
|
||||
if dir.exists() {
|
||||
for entry in fs::read_dir(&dir)
|
||||
.map_err(|e| format!("Failed to read {stage_dir} directory: {e}"))?
|
||||
|
||||
+37
-27
@@ -20,11 +20,9 @@
|
||||
//! the event so connected clients stay in sync.
|
||||
|
||||
use crate::config::{ProjectConfig, WatcherConfig};
|
||||
use crate::io::story_metadata::clear_front_matter_field;
|
||||
use crate::slog;
|
||||
use notify::{EventKind, RecommendedWatcher, RecursiveMode, Watcher, recommended_watcher};
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::mpsc;
|
||||
use std::time::{Duration, Instant, SystemTime};
|
||||
@@ -105,7 +103,10 @@ pub fn is_config_file(path: &Path, git_root: &Path) -> bool {
|
||||
}
|
||||
|
||||
/// Map a pipeline directory name to a (action, commit-message-prefix) pair.
|
||||
fn stage_metadata(stage: &str, item_id: &str) -> Option<(&'static str, String)> {
|
||||
///
|
||||
/// Used by the CRDT-to-watcher bridge (in `main.rs`) to derive the action and
|
||||
/// commit message for `WatcherEvent::WorkItem` events.
|
||||
pub fn stage_metadata(stage: &str, item_id: &str) -> Option<(&'static str, String)> {
|
||||
let (action, prefix) = match stage {
|
||||
"1_backlog" => ("create", format!("huskies: create {item_id}")),
|
||||
"2_current" => ("start", format!("huskies: start {item_id}")),
|
||||
@@ -124,6 +125,9 @@ fn stage_metadata(stage: &str, item_id: &str) -> Option<(&'static str, String)>
|
||||
/// Explicitly returns `None` for any path under `.huskies/worktrees/` so
|
||||
/// that code changes made by agents in their isolated worktrees are never
|
||||
/// auto-committed to master by the watcher.
|
||||
///
|
||||
/// Retained for tests; no longer called in production (CRDT drives events).
|
||||
#[cfg(test)]
|
||||
fn stage_for_path(path: &Path) -> Option<String> {
|
||||
// Reject any path that passes through the worktrees directory.
|
||||
if path.components().any(|c| c.as_os_str() == "worktrees") {
|
||||
@@ -149,6 +153,9 @@ fn stage_for_path(path: &Path) -> Option<String> {
|
||||
/// Uses `git add -A .huskies/work/` to catch both additions and deletions in
|
||||
/// a single commit. Returns `Ok(true)` if a commit was made, `Ok(false)` if
|
||||
/// there was nothing to commit, and `Err` for unexpected failures.
|
||||
///
|
||||
/// Retained for tests; no longer called in production (CRDT drives events).
|
||||
#[cfg(test)]
|
||||
fn git_add_work_and_commit(git_root: &Path, message: &str) -> Result<bool, String> {
|
||||
let work_rel = PathBuf::from(".huskies").join("work");
|
||||
|
||||
@@ -188,9 +195,15 @@ fn git_add_work_and_commit(git_root: &Path, message: &str) -> Result<bool, Strin
|
||||
/// Intermediate stages (current, qa, merge, done) are transient pipeline state
|
||||
/// that don't need to be committed — they're only relevant while the server is
|
||||
/// running and are broadcast to WebSocket clients for real-time UI updates.
|
||||
///
|
||||
/// Retained for tests; no longer called in production (CRDT drives events).
|
||||
#[cfg(test)]
|
||||
const COMMIT_WORTHY_STAGES: &[&str] = &["1_backlog", "5_done", "6_archived"];
|
||||
|
||||
/// Return `true` if changes in `stage` should be committed to git.
|
||||
///
|
||||
/// Retained for tests; no longer called in production (CRDT drives events).
|
||||
#[cfg(test)]
|
||||
fn should_commit_stage(stage: &str) -> bool {
|
||||
COMMIT_WORTHY_STAGES.contains(&stage)
|
||||
}
|
||||
@@ -203,11 +216,16 @@ fn should_commit_stage(stage: &str) -> bool {
|
||||
///
|
||||
/// Only terminal stages (`1_backlog` and `6_archived`) trigger git commits.
|
||||
/// All stages broadcast a [`WatcherEvent`] so the frontend stays in sync.
|
||||
///
|
||||
/// Retained for tests; no longer called in production (CRDT drives events).
|
||||
#[cfg(test)]
|
||||
fn flush_pending(
|
||||
pending: &HashMap<PathBuf, String>,
|
||||
pending: &std::collections::HashMap<PathBuf, String>,
|
||||
git_root: &Path,
|
||||
event_tx: &broadcast::Sender<WatcherEvent>,
|
||||
) {
|
||||
use crate::io::story_metadata::clear_front_matter_field;
|
||||
|
||||
// Separate into files that exist (additions) vs gone (deletions).
|
||||
let mut additions: Vec<(&PathBuf, &str)> = Vec::new();
|
||||
for (path, stage) in pending {
|
||||
@@ -392,10 +410,16 @@ fn sweep_done_to_archived(work_dir: &Path, git_root: &Path, done_retention: Dura
|
||||
|
||||
/// Start the filesystem watcher on a dedicated OS thread.
|
||||
///
|
||||
/// `work_dir` — absolute path to `.huskies/work/` (watched recursively).
|
||||
/// `git_root` — project root (passed to `git` commands as cwd, and used to
|
||||
/// derive the config file path `.huskies/project.toml`).
|
||||
/// `event_tx` — broadcast sender; each connected WebSocket client holds a receiver.
|
||||
/// Watches `.huskies/project.toml` and `.huskies/agents.toml` for config
|
||||
/// hot-reload, and periodically sweeps `5_done/` → `6_archived/`.
|
||||
///
|
||||
/// Work-item pipeline events (stage transitions) are no longer driven by
|
||||
/// filesystem events — they originate from CRDT state changes via
|
||||
/// [`crate::crdt_state::subscribe`].
|
||||
///
|
||||
/// `work_dir` — absolute path to `.huskies/work/` (used for sweep only).
|
||||
/// `git_root` — project root (passed to `git` commands and config loading).
|
||||
/// `event_tx` — broadcast sender for `ConfigChanged` events.
|
||||
/// `watcher_config` — initial sweep configuration loaded from `project.toml`.
|
||||
pub fn start_watcher(
|
||||
work_dir: PathBuf,
|
||||
@@ -416,12 +440,8 @@ pub fn start_watcher(
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = watcher.watch(&work_dir, RecursiveMode::Recursive) {
|
||||
slog!("[watcher] failed to watch {}: {e}", work_dir.display());
|
||||
return;
|
||||
}
|
||||
|
||||
// Also watch .huskies/project.toml and .huskies/agents.toml for hot-reload.
|
||||
// Watch config files for hot-reload. Work-item directories are NOT
|
||||
// watched — CRDT state transitions drive pipeline events now.
|
||||
let huskies = git_root.join(".huskies");
|
||||
for config_file in [huskies.join("project.toml"), huskies.join("agents.toml")] {
|
||||
if config_file.exists()
|
||||
@@ -434,7 +454,7 @@ pub fn start_watcher(
|
||||
}
|
||||
}
|
||||
|
||||
slog!("[watcher] watching {}", work_dir.display());
|
||||
slog!("[watcher] watching config files and running sweep timer");
|
||||
|
||||
const DEBOUNCE: Duration = Duration::from_millis(300);
|
||||
|
||||
@@ -447,8 +467,6 @@ pub fn start_watcher(
|
||||
watcher_config.done_retention_secs
|
||||
);
|
||||
|
||||
// Map path → stage for pending (uncommitted) work-item changes.
|
||||
let mut pending: HashMap<PathBuf, String> = HashMap::new();
|
||||
// Whether a config file change is pending in the current debounce window.
|
||||
let mut config_changed_pending = false;
|
||||
let mut deadline: Option<Instant> = None;
|
||||
@@ -466,9 +484,6 @@ pub fn start_watcher(
|
||||
|
||||
let flush = match notify_rx.recv_timeout(timeout) {
|
||||
Ok(Ok(event)) => {
|
||||
// Track creates, modifies, AND removes. Removes are needed so
|
||||
// that standalone deletions trigger a flush, and so that moves
|
||||
// (which fire Remove + Create) land in the same debounce window.
|
||||
let is_relevant_kind = matches!(
|
||||
event.kind,
|
||||
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
|
||||
@@ -480,10 +495,9 @@ pub fn start_watcher(
|
||||
slog!("[watcher] config change detected: {}", path.display());
|
||||
config_changed_pending = true;
|
||||
deadline = Some(Instant::now() + DEBOUNCE);
|
||||
} else if let Some(stage) = stage_for_path(&path) {
|
||||
pending.insert(path, stage);
|
||||
deadline = Some(Instant::now() + DEBOUNCE);
|
||||
}
|
||||
// Work-item file changes are intentionally ignored.
|
||||
// CRDT state transitions handle pipeline events.
|
||||
}
|
||||
}
|
||||
false
|
||||
@@ -501,10 +515,6 @@ pub fn start_watcher(
|
||||
};
|
||||
|
||||
if flush {
|
||||
if !pending.is_empty() {
|
||||
flush_pending(&pending, &git_root, &event_tx);
|
||||
pending.clear();
|
||||
}
|
||||
if config_changed_pending {
|
||||
slog!("[watcher] broadcasting agent_config_changed");
|
||||
let _ = event_tx.send(WatcherEvent::ConfigChanged);
|
||||
|
||||
+31
-18
@@ -302,25 +302,11 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise the CRDT state layer backed by SQLite.
|
||||
// Uses the same pipeline.db file — the crdt_ops table lives alongside
|
||||
// the legacy pipeline_items table.
|
||||
let crdt_db_path = app_state
|
||||
.project_root
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.map(|root| root.join(".huskies").join("pipeline.db"));
|
||||
// TODO(491): Initialise CRDT state layer once the watcher story lands.
|
||||
// if let Some(db_path) = crdt_db_path
|
||||
// && let Err(e) = db::crdt::init(&db_path).await
|
||||
// {
|
||||
// slog!("[crdt] Failed to initialise CRDT state layer: {e}");
|
||||
// }
|
||||
// (CRDT state layer is initialised above alongside the legacy pipeline.db.)
|
||||
|
||||
let workflow = Arc::new(std::sync::Mutex::new(WorkflowState::default()));
|
||||
|
||||
// Filesystem watcher: broadcast channel for work/ pipeline changes.
|
||||
// Event bus: broadcast channel for pipeline lifecycle events.
|
||||
// Created before AgentPool so the pool can emit AgentStateChanged events.
|
||||
let (watcher_tx, _) = broadcast::channel::<io::watcher::WatcherEvent>(1024);
|
||||
let agents = Arc::new(AgentPool::new(port, watcher_tx.clone()));
|
||||
@@ -329,6 +315,10 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
// When orphans are found, auto-assign is triggered to reassign free agents.
|
||||
let watchdog_root: Option<PathBuf> = app_state.project_root.lock().unwrap().clone();
|
||||
AgentPool::spawn_watchdog(Arc::clone(&agents), watchdog_root);
|
||||
|
||||
// Filesystem watcher: only watches config files (project.toml, agents.toml) and
|
||||
// handles the sweep of done→archived. Work-item pipeline events are now driven
|
||||
// by CRDT state transitions via crdt_state::subscribe().
|
||||
if let Some(ref root) = *app_state.project_root.lock().unwrap() {
|
||||
let work_dir = root.join(".huskies").join("work");
|
||||
if work_dir.is_dir() {
|
||||
@@ -339,8 +329,31 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
}
|
||||
}
|
||||
|
||||
// Bridge CRDT state-transition events to the watcher broadcast channel.
|
||||
// This replaces the filesystem watcher as the source of WorkItem events.
|
||||
{
|
||||
let crdt_watcher_tx = watcher_tx.clone();
|
||||
if let Some(mut crdt_rx) = crdt_state::subscribe() {
|
||||
tokio::spawn(async move {
|
||||
while let Ok(evt) = crdt_rx.recv().await {
|
||||
let (action, commit_msg) =
|
||||
io::watcher::stage_metadata(&evt.to_stage, &evt.story_id)
|
||||
.unwrap_or(("update", format!("huskies: update {}", evt.story_id)));
|
||||
let watcher_evt = io::watcher::WatcherEvent::WorkItem {
|
||||
stage: evt.to_stage,
|
||||
item_id: evt.story_id,
|
||||
action: action.to_string(),
|
||||
commit_msg,
|
||||
from_stage: evt.from_stage,
|
||||
};
|
||||
let _ = crdt_watcher_tx.send(watcher_evt);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to watcher events so that auto-assign triggers when a work item
|
||||
// file is moved into an active pipeline stage (2_current/, 3_qa/, 4_merge/).
|
||||
// enters an active pipeline stage (2_current/, 3_qa/, 4_merge/).
|
||||
{
|
||||
let watcher_auto_rx = watcher_tx.subscribe();
|
||||
let watcher_auto_agents = Arc::clone(&agents);
|
||||
@@ -353,7 +366,7 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
&& matches!(stage.as_str(), "2_current" | "3_qa" | "4_merge")
|
||||
{
|
||||
slog!(
|
||||
"[auto-assign] Watcher detected work item in {stage}/; \
|
||||
"[auto-assign] CRDT transition detected in {stage}/; \
|
||||
triggering auto-assign."
|
||||
);
|
||||
watcher_auto_agents.auto_assign_available_work(&root).await;
|
||||
|
||||
Reference in New Issue
Block a user