-
Notifications
You must be signed in to change notification settings - Fork 0
feat(surface,sdk,kernel): event triggers via webhook + inbox watcher (#301) #317
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
099cbe5
feat(surface,sdk,kernel): event triggers via webhook and inbox watche…
miyaontherelay 653c09f
fix(direct-run): distinguish import from body-run in pre-journal inva…
bff827f
fix(check-triggers): compose f.slack helper preflight for .flow.ts paths
ebfadb7
fix(sdk): guard against missing header in preflight paths reachable v…
4e0a02a
fix(kernel/journal): atomic schema+meta init to survive SIGKILL race
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| //! Local inbox ingress. Each `triggers/<name>.json` is a compiled RunSpec whose | ||
| //! event_type and executor equal <name>. Provision these files before ingress; | ||
| //! do not change a binding while its inbox is pending (dedupe is spec-scoped). | ||
| //! TODO https://github.com/AgentWorkforce/flows/issues/301: bind sealed bundles | ||
| //! through flows deploy; TS handler deployment and Cloud mounts are separate. | ||
| //! No author process stays alive between events. The daemon polls at 1 Hz. | ||
|
|
||
| use crate::engine::{EventSubmitOutcome, read_spec}; | ||
| use anyhow::{Context, Result, bail}; | ||
| use relayflowd_core::{Event, RunSpec}; | ||
| use std::{ | ||
| fs, | ||
| path::{Path, PathBuf}, | ||
| thread, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| const MAX_EVENT_BYTES: u64 = 1024 * 1024; | ||
|
|
||
| pub fn valid_name(name: &str) -> bool { | ||
| !name.is_empty() | ||
| && name.len() <= 128 | ||
| && name.as_bytes()[0].is_ascii_alphanumeric() | ||
| && name | ||
| .bytes() | ||
| .all(|c| c.is_ascii_alphanumeric() || c == b'_' || c == b'-') | ||
| } | ||
|
|
||
| /// The callback is the engine boundary; filesystem traversal never executes code. | ||
| /// Errors retain the offending file and do not starve other inboxes. | ||
| pub fn poll_once( | ||
| data_dir: &Path, | ||
| submit: &mut impl FnMut(RunSpec, Event) -> Result<EventSubmitOutcome>, | ||
| ) -> Result<Vec<String>> { | ||
| let inbox = data_dir.join("inbox"); | ||
| fs::create_dir_all(&inbox)?; | ||
| let mut errors = Vec::new(); | ||
| for directory in fs::read_dir(&inbox)? { | ||
| let directory = directory?; | ||
| if !directory.file_type()?.is_dir() { | ||
| continue; | ||
| } | ||
| let Some(name) = directory.file_name().to_str().map(str::to_owned) else { | ||
| continue; | ||
| }; | ||
| if !valid_name(&name) { | ||
| continue; | ||
| } | ||
| for file in fs::read_dir(directory.path())? { | ||
| let file = file?; | ||
| if !file.file_type()?.is_file() { | ||
| continue; | ||
| } | ||
| let filename = file.file_name(); | ||
| let Some(filename) = filename.to_str() else { | ||
| continue; | ||
| }; | ||
| let Some(id) = filename.strip_suffix(".json") else { | ||
| continue; | ||
| }; | ||
| if !valid_name(id) { | ||
| continue; | ||
| } | ||
| if let Err(error) = process_file(data_dir, &name, filename, &file.path(), submit) { | ||
| errors.push(format!("{}: {error:#}", file.path().display())); | ||
| } | ||
| } | ||
| } | ||
| Ok(errors) | ||
| } | ||
|
|
||
| fn process_file( | ||
| data_dir: &Path, | ||
| name: &str, | ||
| filename: &str, | ||
| path: &Path, | ||
| submit: &mut impl FnMut(RunSpec, Event) -> Result<EventSubmitOutcome>, | ||
| ) -> Result<()> { | ||
| if fs::metadata(path)?.len() > MAX_EVENT_BYTES { | ||
| bail!("event exceeds 1 MiB"); | ||
| } | ||
| let event = Event { | ||
| event_type: name.to_owned(), | ||
| payload: serde_json::from_slice(&fs::read(path)?).context("parse inbox event")?, | ||
| key: Some(filename.to_owned()), | ||
| }; | ||
| let binding = data_dir.join("triggers").join(format!("{name}.json")); | ||
| if !fs::symlink_metadata(&binding)?.is_file() { | ||
| bail!("trigger binding must be a regular file"); | ||
| } | ||
| let spec = read_spec(&binding)?; | ||
| spec.validate().context("invalid trigger spec")?; | ||
| if spec.triggers.is_empty() | ||
| || spec | ||
| .triggers | ||
| .iter() | ||
| .any(|trigger| trigger.executor != name || trigger.event_type.as_deref() != Some(name)) | ||
| { | ||
| bail!("trigger binding must declare executor and event_type {name:?}"); | ||
| } | ||
| let outcome = submit(spec, event)?; | ||
| // A nonmatch is a consumed filter rejection. A matched submission must be | ||
| // durable and driven (or resumed) before moving: crash-before-move retries. | ||
| if outcome.matched && outcome.run.is_none() { | ||
| bail!("matched event has no durable run receipt"); | ||
| } | ||
| let processed = data_dir.join("inbox-processed").join(name); | ||
| fs::create_dir_all(&processed)?; | ||
| if !fs::symlink_metadata(&processed)?.is_dir() { | ||
| bail!("processed inbox must be a directory"); | ||
| } | ||
| fs::rename(path, processed.join(filename)).context("archive inbox event")?; | ||
| fs::File::open(&processed)?.sync_all()?; | ||
| fs::File::open(path.parent().context("inbox parent")?)?.sync_all()?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) fn spawn_watcher( | ||
| data_dir: PathBuf, | ||
| mut submit: impl FnMut(RunSpec, Event) -> Result<EventSubmitOutcome> + Send + 'static, | ||
| ) { | ||
| thread::spawn(move || { | ||
| loop { | ||
| match poll_once(&data_dir, &mut submit) { | ||
| Ok(errors) => { | ||
| for error in errors { | ||
| eprintln!("relayflowd: inbox retained: {error}"); | ||
| } | ||
| } | ||
| Err(error) => eprintln!("relayflowd: inbox poll failed: {error:#}"), | ||
| } | ||
| // TODO https://github.com/AgentWorkforce/flows/issues/301: native watch | ||
| // notifications are a follow-up; this slice deliberately polls at 1 Hz. | ||
| thread::sleep(Duration::from_secs(1)); | ||
| } | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Event spawn skips workspace binding
Medium Severity
The new webhook submit path calls
preflight_placementbut neverbind_local_workspacesbeforedrive.run.startjournals those local workspace pins right after register so a later resume cannot inherit a different daemon cwd. Event-triggered runs with workspace-required deterministic steps lose that binding.Additional Locations (1)
kernel/relayflowd/src/engine/wake.rs#L212-L214Reviewed by Cursor Bugbot for commit ebfadb7. Configure here.