Repository navigation
fix(turn-runner): keep polling the turn while delivering progress #611
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
8 commits
Select commit
Hold shift + click to select a range
aac2644
test(turn_runner): add progress delivery tests
senamakel e09cbf2
test(turn_runner): register progress delivery test module
senamakel 7a642d7
refactor(turn-runner): deliver progress events without blocking the turn
senamakel 3dfa1c2
Merge commit 'b178a40c4' into fix/turn-runner-progress-futurelock
senamakel 2118e19
chore(vendor): bump tinyinference submodule
senamakel 9289033
chore(vendor): keep main's tinyinference pin after the merge
senamakel 50c05d0
test(turn_runner): import AgentProgress directly in progress delivery…
senamakel 1ef5cf6
test(turn_runner): reformat progress send call in test host
senamakel 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
119 changes: 119 additions & 0 deletions
119
crates/tinyagents-harness/src/turn_runner/progress_delivery_tests.rs
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,119 @@ | ||
| //! The runner keeps polling the turn while it hands a progress event to the | ||
| //! sink. | ||
| //! | ||
| //! A sink's delivery can depend on the turn moving: directly, or through a | ||
| //! resource the turn's future already holds or is queued for (a lock whose | ||
| //! permit was handed to a waiter inside the turn). If the runner awaited the | ||
| //! delivery without polling the turn, the two would wait on each other for | ||
| //! good, with every deadline inside the turn frozen with it. | ||
|
|
||
| use super::*; | ||
| use crate::progress::AgentProgress; | ||
|
|
||
| /// Sends one progress event, yields, then signals that it was polled again. | ||
| struct SignalsAfterProgress { | ||
| polled_again: Arc<tokio::sync::Notify>, | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl TurnHost for SignalsAfterProgress { | ||
| async fn checkout(&self, request: &TurnRequest) -> Result<TurnSession, String> { | ||
| Ok(TurnSession { | ||
| session_id: request.thread_id.clone(), | ||
| fingerprint: Some("stable".into()), | ||
| }) | ||
| } | ||
|
|
||
| async fn checkout_fork(&self, request: &TurnRequest) -> Result<TurnSession, String> { | ||
| self.checkout(request).await | ||
| } | ||
|
|
||
| async fn validate_fingerprint( | ||
| &self, | ||
| _request: &TurnRequest, | ||
| _session: &TurnSession, | ||
| ) -> Result<(), String> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn run_origin(&self, request: &TurnRequest) -> serde_json::Value { | ||
| serde_json::json!({"request": request.request_id}) | ||
| } | ||
|
|
||
| async fn run( | ||
| &self, | ||
| _request: &TurnRequest, | ||
| _session: &TurnSession, | ||
| _origin: &serde_json::Value, | ||
| _cancellation: crate::CancellationToken, | ||
| _queue: Arc<RunQueue<TurnRequest>>, | ||
| progress: tokio::sync::mpsc::Sender<TurnSignal>, | ||
| ) -> Result<TurnResult, String> { | ||
| progress | ||
| .send(TurnSignal::Progress(Box::new(AgentProgress::TurnStarted))) | ||
| .await | ||
| .map_err(|_| "progress channel closed".to_owned())?; | ||
| // Hand control back so the runner sees the event before the turn | ||
| // takes its next step. | ||
| tokio::task::yield_now().await; | ||
| self.polled_again.notify_one(); | ||
| Ok(TurnResult { | ||
| reply: "done".into(), | ||
| data: serde_json::Value::Null, | ||
| }) | ||
| } | ||
|
|
||
| async fn checkin(&self, _request: &TurnRequest) {} | ||
|
|
||
| async fn cancel_children(&self, _request: &TurnRequest) {} | ||
| } | ||
|
|
||
| /// Delivers a progress event only once the turn has taken its next step. | ||
| struct WaitsForTheTurn { | ||
| polled_again: Arc<tokio::sync::Notify>, | ||
| terminal: Mutex<Vec<TerminalEvent>>, | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl EventSink for WaitsForTheTurn { | ||
| async fn progress(&self, _request: &TurnRequest, _event: AgentProgress) { | ||
| self.polled_again.notified().await; | ||
| } | ||
|
|
||
| async fn terminal(&self, _request: &TurnRequest, event: TerminalEvent) { | ||
| self.terminal.lock().unwrap().push(event); | ||
| } | ||
|
|
||
| async fn collected(&self, _request: &TurnRequest, _items: Vec<TurnRequest>) {} | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn a_progress_delivery_that_waits_on_the_turn_does_not_freeze_it() { | ||
| let polled_again = Arc::new(tokio::sync::Notify::new()); | ||
| let events = Arc::new(WaitsForTheTurn { | ||
| polled_again: polled_again.clone(), | ||
| terminal: Mutex::default(), | ||
| }); | ||
| let runner = TurnRunner::new( | ||
| Arc::new(SignalsAfterProgress { polled_again }), | ||
| Arc::new(Ingress::default()), | ||
| Arc::new(Replies::default()), | ||
| events.clone(), | ||
| Arc::new(Failures), | ||
| Arc::new(Summary), | ||
| TurnRunnerConfig::default(), | ||
| ); | ||
|
|
||
| let outcome = tokio::time::timeout( | ||
| Duration::from_secs(10), | ||
| runner.run(request(QueueDisposition::Interrupt, false)), | ||
| ) | ||
| .await | ||
| .expect("the turn must keep running while its progress is delivered"); | ||
|
|
||
| assert!(outcome.is_ok(), "{outcome:?}"); | ||
| assert!(matches!( | ||
| events.terminal.lock().unwrap().as_slice(), | ||
| [TerminalEvent::Completed { .. }] | ||
| )); | ||
| } |
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
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.
If a pending progress sink holds a resource that
host.cancel_childrenalso needs, cancelling the turn or reaching its wall-clock deadline now deadlocks cleanup. This branch only borrows the future fromdelivering, so selecting cancellation or the deadline leaves the underlying delivery alive but unpolled while those handlers awaitcancel_children. Previously, the inlineselect!owned and dropped the delivery before entering its cancellation handler. Drop the in-flight delivery before awaiting cancellation/deadline cleanup, including the deadline check above the select.Useful? React with 👍 / 👎.