Repository navigation
Release Copilot idle hooks after native cancellation #2095
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
Open
khaliqgant
wants to merge
6
commits into
main
Choose a base branch
from
fix/copilot-stop-cancellation-1107
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b102be2
Release Copilot idle hooks after native cancellation
2457911
Preserve Copilot idle delivery and bounded cancellation reads
5361d75
Merge remote-tracking branch 'origin/main' into fix/copilot-stop-canc…
e63c24c
resolve conflicts
2a2b52a
Merge remote-tracking branch 'refs/remotes/origin/fix/copilot-stop-ca…
1135ea2
Merge remote-tracking branch 'origin/main' into fix/copilot-stop-canc…
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,142 @@ | ||
| //! Bounded native control records release a waiting stop hook after cancellation. | ||
| //! This never ingests history or claims a delivery. The native input timestamp | ||
| //! anchors this invocation, so an earlier abort cannot cancel a later wait. | ||
| use super::CopilotConfig; | ||
| use serde_json::{json, Value}; | ||
| use std::fs::{File, Metadata, OpenOptions}; | ||
| use std::io::{Read, Seek, SeekFrom}; | ||
| use std::path::PathBuf; | ||
|
|
||
| const LIMIT: u64 = 64 << 10; | ||
|
|
||
| fn path(config: &CopilotConfig, sid: &str) -> Option<PathBuf> { | ||
| crate::relay::store::is_uuid(sid).then(|| config.sessions().join(sid).join("events.jsonl")) | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn identity(metadata: &Metadata) -> Option<Value> { | ||
| use std::os::unix::fs::MetadataExt; | ||
| Some(json!([metadata.dev(), metadata.ino()])) | ||
| } | ||
|
|
||
| // Messaging is unavailable on Windows today. Keep the control reader portable; | ||
| // creation time is its file incarnation, as for the Codex rollout reader. | ||
| #[cfg(windows)] | ||
| fn identity(metadata: &Metadata) -> Option<Value> { | ||
| let created = metadata | ||
| .created() | ||
| .ok()? | ||
| .duration_since(std::time::UNIX_EPOCH) | ||
| .ok()?; | ||
| Some(json!([created.as_secs(), created.subsec_nanos()])) | ||
| } | ||
|
|
||
| pub(crate) fn snapshot(config: &CopilotConfig, sid: &str) -> Option<Value> { | ||
| let metadata = std::fs::symlink_metadata(path(config, sid)?).ok()?; | ||
| metadata.is_file().then_some(())?; | ||
| Some(json!({"identity":identity(&metadata)?,"length":metadata.len()})) | ||
| } | ||
|
|
||
| fn open(path: &std::path::Path) -> Option<File> { | ||
| std::fs::symlink_metadata(path) | ||
| .ok()? | ||
| .is_file() | ||
| .then_some(())?; | ||
| let mut options = OpenOptions::new(); | ||
| options.read(true); | ||
| #[cfg(unix)] | ||
| { | ||
| use std::os::unix::fs::OpenOptionsExt; | ||
| options.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK); | ||
| } | ||
| options.open(path).ok() | ||
| } | ||
|
|
||
| fn bytes(config: &CopilotConfig, sid: &str, stamp: &Value) -> Option<Vec<u8>> { | ||
| let path = path(config, sid)?; | ||
| let mut file = open(&path)?; | ||
| let metadata = file.metadata().ok()?; | ||
| if !metadata.is_file() | ||
| || identity(&metadata)? != stamp["identity"] | ||
| || metadata.len() < stamp["length"].as_u64()? | ||
| { | ||
| return None; | ||
| } | ||
| let bytes = read_tail_at(&mut file, metadata.len())?; | ||
| let after = std::fs::symlink_metadata(path).ok()?; | ||
| if !after.is_file() || identity(&after)? != stamp["identity"] || after.len() < metadata.len() { | ||
| return None; | ||
| } | ||
| Some(bytes) | ||
| } | ||
|
|
||
| fn read_tail_at(file: &mut File, length: u64) -> Option<Vec<u8>> { | ||
| let start = length.saturating_sub(LIMIT); | ||
| let trim = if start > 0 { | ||
| file.seek(SeekFrom::Start(start - 1)).ok()?; | ||
| let mut previous = [0]; | ||
| file.read_exact(&mut previous).ok()?; | ||
| previous[0] != b'\n' | ||
| } else { | ||
| false | ||
| }; | ||
| file.seek(SeekFrom::Start(start)).ok()?; | ||
| let mut bytes = Vec::new(); | ||
| // Appends after the metadata snapshot belong to the next poll. Reading | ||
| // past that bound must not turn an ordinary append into cancellation. | ||
| file.take(length - start).read_to_end(&mut bytes).ok()?; | ||
| if trim { | ||
| let first = bytes.iter().position(|b| *b == b'\n')?; | ||
| bytes.drain(..=first); | ||
| } | ||
| Some(bytes) | ||
| } | ||
|
|
||
| fn anchor(event: &Value, sid: &str, timestamp: u64) -> bool { | ||
| event["type"] == "hook.start" | ||
| && event["data"]["hookType"] == "agentStop" | ||
| && event["data"]["input"]["sessionId"] == sid | ||
| && event["data"]["input"]["timestamp"].as_u64() == Some(timestamp) | ||
| && event["data"]["input"]["stopReason"] == "end_turn" | ||
| } | ||
|
|
||
| fn scan(bytes: &[u8], sid: &str, timestamp: u64) -> Option<bool> { | ||
| let mut anchored = false; | ||
| // An incomplete last record can be an ordinary concurrent journal append. | ||
| for line in bytes | ||
| .split_inclusive(|b| *b == b'\n') | ||
| .filter(|l| l.ends_with(b"\n")) | ||
| { | ||
| let event: Value = match serde_json::from_slice::<Value>(line) { | ||
| Ok(value) if value.is_object() && value["type"].is_string() => value, | ||
| _ if anchored => return Some(true), | ||
| _ => continue, | ||
| }; | ||
| if !anchored { | ||
| anchored = anchor(&event, sid, timestamp); | ||
| continue; | ||
| } | ||
| if matches!( | ||
| event["type"].as_str(), | ||
| Some("abort" | "user.message" | "session.error") | ||
| ) || (event["type"] == "hook.start" && event["data"]["hookType"] == "agentStop") | ||
| { | ||
| return Some(true); | ||
| } | ||
| } | ||
| anchored.then_some(false) | ||
| } | ||
|
|
||
| /// Unverifiable/replaced/truncated journals release the waiter quietly. Returning | ||
| /// true is only a refusal to claim a message, never authority to send or read one. | ||
| pub(crate) fn cancelled(config: &CopilotConfig, sid: &str, payload: &Value) -> bool { | ||
| (|| { | ||
| let timestamp = payload["timestamp"].as_u64()?; | ||
| let bytes = bytes(config, sid, &payload["relayJournal"])?; | ||
| scan(&bytes, sid, timestamp) | ||
| })() | ||
| .unwrap_or(true) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; |
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.
CHANGELOG.mdrepeats the Copilot fix at lines 12 and 14, and the site-specific gateway fix at lines 13 and 16. This violates the repository guide's requirement for concise entries with one bullet per user-visible change. Remove one copy of each repeated entry before merging.Context Used: CLAUDE.md (source)
Prompt To Fix With AI