File size: 29,984 Bytes
afa0cbf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 | use std::collections::HashMap;
use std::time::Duration;
use anyhow::Result;
use app_test_support::TestAppServer;
use app_test_support::to_response;
use codex_app_server_protocol::CapabilityRootLocation;
use codex_app_server_protocol::GrantedPermissionProfile;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::PermissionGrantScope;
use codex_app_server_protocol::PermissionsRequestApprovalResponse;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::SelectedCapabilityRoot;
use codex_app_server_protocol::ServerRequest;
use codex_app_server_protocol::ThreadStartParams;
use codex_app_server_protocol::ThreadStartResponse;
use codex_app_server_protocol::TurnStartParams;
use codex_app_server_protocol::UserInput;
use codex_app_server_protocol::WarningNotification;
use codex_exec_server::CreateDirectoryOptions;
use codex_utils_path_uri::PathUri;
use core_test_support::responses;
use core_test_support::skip_if_remote;
use core_test_support::skip_if_target_windows;
use futures::StreamExt;
use futures::TryStreamExt;
use pretty_assertions::assert_eq;
use serde_json::json;
use tempfile::TempDir;
use tokio::time::timeout;
use super::analytics::mount_analytics_capture;
use super::analytics::wait_for_matching_analytics_event;
#[cfg(target_os = "macos")]
const READ_TIMEOUT: Duration = Duration::from_secs(60);
#[cfg(not(target_os = "macos"))]
const READ_TIMEOUT: Duration = Duration::from_secs(20);
const SKILL_NAME: &str = "demo-plugin:deploy";
const SKILL_MARKER: &str = "EXECUTOR_SKILL_BODY_MARKER";
const LOCAL_SKILL_MARKER: &str = "LOCAL_SKILL_BODY_MARKER";
const REFERENCE_MARKER: &str = "EXECUTOR_SKILL_REFERENCE_MARKER";
const DENIED_SKILL_NAME: &str = "demo-plugin:denied";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ExecutorSkillScenario {
VisibleWithBudgetWarning,
ExplicitOnly,
RestrictedPermittedReference,
RestrictedDeniedReference,
RestrictedVisible,
}
#[tokio::test]
async fn selected_executor_root_exposes_plugin_skill_and_forwards_budget_warning() -> Result<()> {
exercise_executor_skill(ExecutorSkillScenario::VisibleWithBudgetWarning).await
}
#[tokio::test]
async fn explicit_executor_skill_can_read_referenced_file() -> Result<()> {
exercise_executor_skill(ExecutorSkillScenario::ExplicitOnly).await
}
#[tokio::test]
async fn restricted_executor_skill_can_read_permitted_reference() -> Result<()> {
exercise_executor_skill(ExecutorSkillScenario::RestrictedPermittedReference).await
}
#[cfg(unix)]
#[tokio::test]
async fn restricted_executor_skill_rejects_reference_until_permission_approved() -> Result<()> {
exercise_executor_skill(ExecutorSkillScenario::RestrictedDeniedReference).await
}
#[tokio::test]
async fn restricted_executor_skill_is_listed_only_when_permitted() -> Result<()> {
exercise_executor_skill(ExecutorSkillScenario::RestrictedVisible).await
}
async fn exercise_executor_skill(scenario: ExecutorSkillScenario) -> Result<()> {
let restricted = matches!(
scenario,
ExecutorSkillScenario::RestrictedPermittedReference
| ExecutorSkillScenario::RestrictedDeniedReference
| ExecutorSkillScenario::RestrictedVisible
);
if restricted {
skip_if_target_windows!(
Ok(()),
"the unelevated Windows sandbox cannot enforce restricted filesystem reads"
);
}
if scenario == ExecutorSkillScenario::RestrictedDeniedReference {
skip_if_remote!(Ok(()), "the external symlink fixture is host-local");
}
let server = responses::start_mock_server().await;
let codex_home = TempDir::new()?;
let (sandbox_config, permission_profile) = if restricted {
(
"default_permissions = \"workspace\"",
"\n[permissions.workspace.filesystem.\":workspace_roots\"]\n\".\" = \"write\"\n\n[windows]\nsandbox = \"unelevated\"\n",
)
} else {
("sandbox_mode = \"read-only\"", "")
};
let (approval_policy, requested_permission_feature) =
if scenario == ExecutorSkillScenario::RestrictedDeniedReference {
(
"on-request",
"\n[features]\nrequest_permissions_tool = true\n",
)
} else {
("never", "")
};
let analytics_config = if scenario == ExecutorSkillScenario::ExplicitOnly {
format!("chatgpt_base_url = \"{}\"", server.uri())
} else {
String::new()
};
std::fs::write(
codex_home.path().join("config.toml"),
format!(
r#"
model = "mock-model"
{analytics_config}
approval_policy = "{approval_policy}"
{sandbox_config}
model_provider = "mock_provider"
[skills]
include_instructions = true
[model_providers.mock_provider]
name = "Mock provider for test"
base_url = "{}/v1"
wire_api = "responses"
request_max_retries = 0
stream_max_retries = 0
{permission_profile}
{requested_permission_feature}
"#,
server.uri()
),
)?;
if scenario == ExecutorSkillScenario::ExplicitOnly {
mount_analytics_capture(&server, codex_home.path()).await?;
}
let local_skill_dir = codex_home.path().join("skills/local-deploy");
std::fs::create_dir_all(&local_skill_dir)?;
std::fs::write(
local_skill_dir.join("SKILL.md"),
format!(
"---\nname: {SKILL_NAME}\ndescription: Colliding local skill.\n---\n\n# Local deploy\n\n{LOCAL_SKILL_MARKER}\n"
),
)?;
let mut app_server = TestAppServer::builder()
.with_codex_home(codex_home.path())
.build()
.await?;
let auto_env = app_server.auto_env()?;
let environment_id = auto_env.selection().environment_id.clone();
let plugin_dir = auto_env.selection().cwd.join("plugin")?;
let manifest_dir = plugin_dir.join(".codex-plugin")?;
let skill_dir = plugin_dir.join("skills/deploy")?;
let agents_dir = skill_dir.join("agents")?;
let reference_dir = skill_dir.join("references")?;
let file_system = auto_env.environment().get_filesystem();
for directory in [&manifest_dir, &agents_dir, &reference_dir] {
file_system
.create_directory(
directory,
CreateDirectoryOptions {
recursive: true,
follow_symlinks: true,
},
/*sandbox*/ None,
)
.await?;
}
let manifest_path = manifest_dir.join("plugin.json")?;
let skill_path = skill_dir.join("SKILL.md")?;
let openai_yaml_path = agents_dir.join("openai.yaml")?;
let reference_path = reference_dir.join("details.md")?;
let reference_size = match scenario {
ExecutorSkillScenario::VisibleWithBudgetWarning => 600 * 1024,
ExecutorSkillScenario::RestrictedPermittedReference
| ExecutorSkillScenario::RestrictedDeniedReference => 1024,
ExecutorSkillScenario::ExplicitOnly | ExecutorSkillScenario::RestrictedVisible => 40 * 1024,
};
let allow_implicit_invocation = matches!(
scenario,
ExecutorSkillScenario::VisibleWithBudgetWarning | ExecutorSkillScenario::RestrictedVisible
);
let reference_contents = format!("{REFERENCE_MARKER}\n{}", "x".repeat(reference_size));
tokio::try_join!(
file_system.write_file(
&manifest_path,
br#"{"name":"demo-plugin"}"#.to_vec(),
Default::default(), /*sandbox*/ None,
),
file_system.write_file(
&skill_path,
format!(
"---\nname: deploy\ndescription: Deploy through the executor.\n---\n\n# Deploy\n\n{SKILL_MARKER}\n\nRead references/details.md.\n"
)
.into_bytes(),
Default::default(), /*sandbox*/ None,
),
file_system.write_file(
&openai_yaml_path,
format!(
"policy:\n allow_implicit_invocation: {allow_implicit_invocation}\n"
)
.into_bytes(),
Default::default(), /*sandbox*/ None,
),
file_system.write_file(
&reference_path,
reference_contents.into_bytes(),
Default::default(), /*sandbox*/ None,
),
)?;
#[cfg(unix)]
if scenario == ExecutorSkillScenario::RestrictedDeniedReference {
let external_reference_dir = codex_home.path().join("external-reference");
std::fs::create_dir_all(&external_reference_dir)?;
let external_reference = external_reference_dir.join("details.md");
std::fs::write(
&external_reference,
format!(
"DENIED_REFERENCE_MARKER\n{REFERENCE_MARKER}\n{}",
"x".repeat(reference_size)
),
)?;
let reference_native_path = reference_path.to_abs_path()?;
std::fs::remove_file(reference_native_path.as_path())?;
std::os::unix::fs::symlink(external_reference, reference_native_path.as_path())?;
}
#[cfg(unix)]
if scenario == ExecutorSkillScenario::RestrictedVisible && !auto_env.environment().is_remote() {
let denied_skill_dir = codex_home.path().join("denied-skill");
std::fs::create_dir_all(&denied_skill_dir)?;
std::fs::write(
denied_skill_dir.join("SKILL.md"),
"---\nname: denied\ndescription: Skill outside the permitted workspace.\n---\n",
)?;
std::os::unix::fs::symlink(
denied_skill_dir,
plugin_dir.to_abs_path()?.join("skills/denied"),
)?;
}
if scenario == ExecutorSkillScenario::VisibleWithBudgetWarning {
futures::stream::iter(0..200)
.map(|index| {
let file_system = file_system.clone();
let plugin_dir = plugin_dir.clone();
async move {
let relative = format!("skills/skill-{index:03}");
let skill_dir = plugin_dir.join(&relative)?;
file_system
.create_directory(
&skill_dir,
CreateDirectoryOptions {
recursive: true,
follow_symlinks: true,
},
/*sandbox*/ None,
)
.await?;
file_system
.write_file(
&skill_dir.join("SKILL.md")?,
format!(
"---\nname: skill-{index:03}\ndescription: {}\n---\n",
"x".repeat(1_025)
)
.into_bytes(),
Default::default(),
/*sandbox*/ None,
)
.await?;
Ok::<(), anyhow::Error>(())
}
})
.buffer_unordered(16)
.try_collect::<Vec<_>>()
.await?;
}
let authority_id = "demo-plugin@1";
let locator = |path: &PathUri| {
format!(
"skill://{authority_id}/{}",
path.inferred_native_path_string()
.replace('\\', "/")
.trim_start_matches('/')
)
};
let package = locator(&skill_dir);
let main_package = if scenario == ExecutorSkillScenario::VisibleWithBudgetWarning {
"e0/skills/deploy".to_string()
} else {
package.clone()
};
let main_resource = locator(&skill_dir.join("SKILL.md")?);
let reference_resource = locator(&reference_dir.join("details.md")?);
let tool_response = |call_id: &str, tool: &str, arguments: serde_json::Value| {
responses::sse(vec![
responses::ev_response_created(&format!("resp-{call_id}")),
responses::ev_function_call_with_namespace(
call_id,
"skills",
tool,
&arguments.to_string(),
),
responses::ev_completed(&format!("resp-{call_id}")),
])
};
let mut model_responses = vec![
tool_response("list", "list", json!({"authority": {"kind": "executor"}})),
tool_response(
"main",
"read",
json!({
"package": main_package,
"resource": main_resource.clone(),
}),
),
tool_response(
"reference",
"read",
json!({
"package": package.clone(),
"authority": {
"kind": "executor",
"id": authority_id,
},
"resource": reference_resource.clone(),
}),
),
responses::sse(vec![
responses::ev_response_created("resp-done"),
responses::ev_assistant_message("msg-done", "Done"),
responses::ev_completed("resp-done"),
]),
];
if scenario == ExecutorSkillScenario::RestrictedDeniedReference {
let external_reference_dir = codex_home.path().join("external-reference");
model_responses.insert(
3,
responses::sse(vec![
responses::ev_response_created("resp-permissions"),
responses::ev_function_call(
"permissions",
"request_permissions",
&json!({
"reason": "Read the approved skill reference",
"permissions": {
"file_system": {"read": [external_reference_dir]}
}
})
.to_string(),
),
responses::ev_completed("resp-permissions"),
]),
);
model_responses.insert(
4,
tool_response(
"approved-reference",
"read",
json!({
"package": package.clone(),
"resource": reference_resource.clone(),
}),
),
);
}
let response_mock = responses::mount_sse_sequence(&server, model_responses).await;
timeout(READ_TIMEOUT, app_server.initialize()).await??;
let request_id = app_server
.send_thread_start_request_with_auto_env(ThreadStartParams {
model: Some("mock-model".to_string()),
config: matches!(
scenario,
ExecutorSkillScenario::RestrictedPermittedReference
| ExecutorSkillScenario::RestrictedDeniedReference
)
.then(|| HashMap::from([("tool_output_token_limit".to_string(), json!(250))])),
selected_capability_roots: Some(vec![SelectedCapabilityRoot {
id: "demo-plugin@1".to_string(),
location: CapabilityRootLocation::Environment {
environment_id,
path: plugin_dir,
},
}]),
..Default::default()
})
.await?;
let response: JSONRPCResponse = timeout(
READ_TIMEOUT,
app_server.read_stream_until_response_message(RequestId::Integer(request_id)),
)
.await??;
let ThreadStartResponse { thread, .. } = to_response(response)?;
let thread_id = thread.id;
let request_id = app_server
.send_turn_start_request(TurnStartParams {
thread_id: thread_id.clone(),
input: vec![UserInput::Text {
text: format!("Use ${SKILL_NAME}"),
text_elements: Vec::new(),
}],
..Default::default()
})
.await?;
timeout(
READ_TIMEOUT,
app_server.read_stream_until_response_message(RequestId::Integer(request_id)),
)
.await??;
if scenario == ExecutorSkillScenario::RestrictedDeniedReference {
let request =
timeout(READ_TIMEOUT, app_server.read_stream_until_request_message()).await??;
let ServerRequest::PermissionsRequestApproval { request_id, params } = request else {
panic!("expected a skill reference permissions request, got {request:?}");
};
app_server
.send_response(
request_id,
serde_json::to_value(PermissionsRequestApprovalResponse {
permissions: GrantedPermissionProfile {
network: None,
file_system: params.permissions.file_system,
},
scope: PermissionGrantScope::Turn,
strict_auto_review: None,
})?,
)
.await?;
}
if scenario == ExecutorSkillScenario::VisibleWithBudgetWarning {
let is_skills_budget_warning = |message: &str| {
message.starts_with("Exceeded skills context budget.")
|| message.starts_with(
"Skill descriptions were shortened to fit the skills context budget.",
)
};
let warning = timeout(READ_TIMEOUT, async {
loop {
let warning: WarningNotification = app_server.read_notification("warning").await?;
if is_skills_budget_warning(&warning.message) {
return Ok::<WarningNotification, anyhow::Error>(warning);
}
}
})
.await??;
assert_eq!(warning.thread_id, Some(thread_id.clone()));
assert!(is_skills_budget_warning(&warning.message));
}
timeout(
READ_TIMEOUT,
app_server.read_stream_until_notification_message("turn/completed"),
)
.await??;
if scenario == ExecutorSkillScenario::ExplicitOnly {
for invocation_type in ["explicit", "implicit"] {
let event = wait_for_matching_analytics_event(&server, READ_TIMEOUT, |event| {
event["event_type"] == "skill_invocation"
&& event["event_params"]["invoke_type"] == invocation_type
})
.await?;
assert_eq!(event["event_params"]["plugin_id"], authority_id);
assert_eq!(event["event_params"]["skill_scope"], "user");
}
}
let requests = response_mock.requests();
let request = &requests[0];
if scenario == ExecutorSkillScenario::VisibleWithBudgetWarning {
assert!(
request
.message_input_texts("developer")
.iter()
.any(|text| text.contains("executor package: e0/skills/deploy"))
);
}
assert!(
request
.message_input_texts("developer")
.iter()
.any(|text| text.contains(SKILL_NAME))
);
let skill_fragments = request
.message_input_texts("user")
.into_iter()
.filter(|text| text.starts_with("<skill>"))
.collect::<Vec<_>>();
assert_eq!(1, skill_fragments.len());
let skill_fragment = skill_fragments
.first()
.expect("executor skill instructions should be model-visible");
assert!(skill_fragment.contains(&format!("<name>{SKILL_NAME}</name>")));
assert!(skill_fragment.contains(SKILL_MARKER));
assert!(!skill_fragment.contains(LOCAL_SKILL_MARKER));
match scenario {
ExecutorSkillScenario::VisibleWithBudgetWarning
| ExecutorSkillScenario::RestrictedVisible => {
assert!(!skill_fragment.contains("<resource_access>"));
}
ExecutorSkillScenario::ExplicitOnly
| ExecutorSkillScenario::RestrictedPermittedReference
| ExecutorSkillScenario::RestrictedDeniedReference => {
let resource_access = skill_fragment
.split_once("<resource_access>")
.and_then(|(_, rest)| rest.split_once("</resource_access>"))
.map(|(metadata, _)| serde_json::from_str::<serde_json::Value>(metadata))
.transpose()?
.expect("explicit executor skill should include resource access metadata");
assert_eq!(
resource_access,
json!({
"authority": {"kind": "executor", "id": authority_id},
"package": package,
"main_resource": main_resource,
})
);
}
}
let list_output = serde_json::from_str::<serde_json::Value>(
&requests[1]
.function_call_output_text("list")
.expect("skills.list output"),
)?;
match scenario {
ExecutorSkillScenario::VisibleWithBudgetWarning
| ExecutorSkillScenario::RestrictedVisible => {
let deploy_skill = list_output["skills"]
.as_array()
.and_then(|skills| skills.iter().find(|skill| skill["name"] == SKILL_NAME))
.expect("skills.list should include the selected executor skill");
assert_eq!(
deploy_skill,
&json!({
"authority": {"kind": "executor", "id": authority_id},
"package": package,
"name": SKILL_NAME,
"description": "Deploy through the executor.",
"main_resource": main_resource,
})
);
assert!(list_output["skills"].as_array().is_none_or(|skills| {
skills
.iter()
.all(|skill| skill["name"] != DENIED_SKILL_NAME)
}));
if scenario == ExecutorSkillScenario::VisibleWithBudgetWarning {
assert!(list_output["next_cursor"].is_string());
} else {
assert!(list_output["next_cursor"].is_null());
}
}
ExecutorSkillScenario::ExplicitOnly
| ExecutorSkillScenario::RestrictedPermittedReference
| ExecutorSkillScenario::RestrictedDeniedReference => {
assert_eq!(list_output["skills"], json!([]));
}
}
let main_output = serde_json::from_str::<serde_json::Value>(
&requests[2]
.function_call_output_text("main")
.expect("main skill output"),
)?;
assert!(
main_output["contents"]
.as_str()
.is_some_and(|contents| contents.contains(SKILL_MARKER))
);
assert_eq!(
main_output["skill_root"],
json!(skill_dir.inferred_native_path_string())
);
let reference_output_text = requests[3]
.function_call_output_text("reference")
.expect("referenced skill file output");
if scenario == ExecutorSkillScenario::RestrictedDeniedReference {
assert!(reference_output_text.contains("failed to read skill resource"));
assert!(!reference_output_text.contains("DENIED_REFERENCE_MARKER"));
let approved_reference_output = requests[5]
.function_call_output_text("approved-reference")
.expect("approved skill reference output");
assert!(approved_reference_output.contains(REFERENCE_MARKER));
let approved_reference: serde_json::Value =
serde_json::from_str(&approved_reference_output)?;
let cursor = approved_reference["next_cursor"]
.as_str()
.expect("approved reference should paginate");
let expired = responses::mount_sse_sequence(
&server,
vec![
tool_response(
"expired-reference",
"read",
json!({
"package": package,
"resource": reference_resource,
"cursor": cursor,
}),
),
responses::sse(vec![responses::ev_completed("resp-expired-done")]),
],
)
.await;
timeout(
READ_TIMEOUT,
app_server.start_turn_and_wait_for_completion(TurnStartParams {
thread_id,
input: vec![UserInput::Text {
text: "Continue after the turn-scoped permission expired.".to_string(),
text_elements: Vec::new(),
}],
..Default::default()
}),
)
.await??;
assert_eq!(
expired.function_call_output_text("expired-reference"),
Some("failed to read skill resource".to_string())
);
return Ok(());
}
let mut reference_output = serde_json::from_str::<serde_json::Value>(&reference_output_text)?;
assert!(
reference_output["contents"]
.as_str()
.is_some_and(|contents| contents.contains(REFERENCE_MARKER))
);
assert_eq!(
reference_output["skill_root"],
json!(skill_dir.inferred_native_path_string())
);
assert!(reference_output["next_cursor"].is_string());
if scenario == ExecutorSkillScenario::RestrictedPermittedReference {
// 250 tokens gives both byte- and token-based policies a 1200-byte response budget.
assert!(reference_output_text.len() <= 1200);
let expected_contents = format!("{REFERENCE_MARKER}\n{}", "x".repeat(reference_size));
let original_cursor = reference_output["next_cursor"]
.as_str()
.expect("reference cursor")
.to_string();
let changed_contents = format!("CHANGED_REFERENCE\n{}", "y".repeat(reference_size));
file_system
.write_file(
&reference_path,
changed_contents.clone().into_bytes(),
Default::default(),
/*sandbox*/ None,
)
.await?;
let mut contents = reference_output["contents"]
.as_str()
.expect("reference contents")
.to_string();
while let Some(cursor) = reference_output["next_cursor"].as_str() {
let call_id = format!("reference-{}", contents.len());
let continuation = responses::mount_sse_sequence(
&server,
vec![
tool_response(
&call_id,
"read",
json!({
"package": package,
"resource": reference_resource,
"cursor": cursor,
}),
),
responses::sse(vec![responses::ev_completed(&format!(
"resp-{call_id}-done"
))]),
],
)
.await;
timeout(
READ_TIMEOUT,
app_server.start_turn_and_wait_for_completion(TurnStartParams {
thread_id: thread_id.clone(),
input: vec![UserInput::Text {
text: "Continue reading the reference.".to_string(),
text_elements: Vec::new(),
}],
..Default::default()
}),
)
.await??;
let output = continuation
.function_call_output_text(&call_id)
.expect("continued reference output");
assert!(output.len() <= 1200);
reference_output = serde_json::from_str(&output)?;
let page_contents = reference_output["contents"]
.as_str()
.expect("continued reference contents");
assert!(!page_contents.is_empty());
assert_eq!(
reference_output,
json!({
"resource": reference_resource,
"contents": page_contents,
"skill_root": skill_dir.inferred_native_path_string(),
"next_cursor": reference_output["next_cursor"].as_str(),
})
);
contents.push_str(page_contents);
assert!(expected_contents.starts_with(&contents));
}
assert_eq!(contents, expected_contents);
let restarted = responses::mount_sse_sequence(
&server,
vec![
tool_response(
"fresh-reference",
"read",
json!({"package": package, "resource": reference_resource}),
),
tool_response(
"evicted-reference",
"read",
json!({
"package": package,
"resource": reference_resource,
"cursor": original_cursor,
}),
),
responses::sse(vec![responses::ev_completed("resp-restarted-done")]),
],
)
.await;
timeout(
READ_TIMEOUT,
app_server.start_turn_and_wait_for_completion(TurnStartParams {
thread_id,
input: vec![UserInput::Text {
text: "Read the changed reference, then try its old cursor.".to_string(),
text_elements: Vec::new(),
}],
..Default::default()
}),
)
.await??;
let fresh_output = restarted
.function_call_output_text("fresh-reference")
.expect("fresh reference output");
assert!(fresh_output.len() <= 1200);
let fresh: serde_json::Value = serde_json::from_str(&fresh_output)?;
assert!(fresh["contents"].as_str().is_some_and(|page| {
page.starts_with("CHANGED_REFERENCE") && changed_contents.starts_with(page)
}));
assert!(fresh["next_cursor"].is_string());
assert_eq!(
restarted.function_call_output_text("evicted-reference"),
Some("skills.read cursor is stale; restart from the first page".to_string())
);
}
Ok(())
}
|