Compare commits

...

8 Commits

Author SHA1 Message Date
Yeachan-Heo
00b557c8b7 Enforce machine-readable output across CLI surfaces 2026-04-05 17:35:36 +00:00
Yeachan-Heo
31163be347 style: cargo fmt 2026-04-05 16:56:48 +00:00
Yeachan-Heo
eb4d3b11ee merge fix/p2-19-subcommand-help-fallthrough 2026-04-05 16:54:59 +00:00
Yeachan-Heo
9bd7a78ca8 Merge branch 'fix/p2-18-context-window-preflight' 2026-04-05 16:54:45 +00:00
Yeachan-Heo
24d8f916c8 merge fix/p0-10-json-status 2026-04-05 16:54:38 +00:00
Yeachan-Heo
30883bddbd Keep doctor and local help paths shell-native
Promote doctor into a real top-level CLI action, reuse the same local report for resumed and REPL doctor invocations, and intercept doctor/status/sandbox help flags before prompt-mode dispatch. The parser change also closes the help fallthrough that previously wandered into runtime startup for local-info commands.

Constraint: Preserve prompt shorthand for normal multi-word text input while fixing exact local subcommand help paths
Rejected: Route \7⠋ 🦀 Thinking...8✘  Request failed
 through prompt/slash guidance | still shells out through the wrong surface and keeps health checks hidden
Rejected: Reuse the status report as doctor output | status does not explain auth/config health or expose a dedicated diagnostic summary
Confidence: high
Scope-risk: narrow
Directive: Keep doctor local-only unless an explicit network probe is intentionally added and separately tested
Tested: cargo build -p rusty-claude-cli; cargo test -p rusty-claude-cli; cargo run -p rusty-claude-cli -- doctor --help; CLAW_CONFIG_HOME=/tmp/tmp.7pm9SVzOPN ANTHROPIC_API_KEY= ANTHROPIC_AUTH_TOKEN= cargo run -p rusty-claude-cli -- doctor
Not-tested: direct /doctor outside the REPL remains interactive-only
2026-04-05 16:44:36 +00:00
Yeachan-Heo
1a2fa1581e Keep status JSON machine-readable for automation
The global --output-format json flag already reached prompt-mode responses, but
status and sandbox still bypassed that path and printed human-readable tables.
This change threads the selected output format through direct command aliases
and resumed slash-command execution so status queries emit valid structured
JSON instead of mixed prose.

It also adds end-to-end regression coverage for direct status/sandbox JSON
and resumed /status JSON so shell automation can rely on stable parsing.

Constraint: Global output formatting must stay compatible with existing text-mode reports
Rejected: Require callers to scrape text status tables | fragile and breaks automation
Confidence: high
Scope-risk: narrow
Directive: New direct commands that honor --output-format should thread the format through CliAction and resumed slash execution paths
Tested: cargo build -p rusty-claude-cli
Tested: cargo test -p rusty-claude-cli -- --nocapture
Tested: cargo test --workspace
Tested: cargo run -q -p rusty-claude-cli -- --output-format json status
Tested: cargo run -q -p rusty-claude-cli -- --output-format json sandbox
Not-tested: cargo clippy --workspace --all-targets -- -D warnings (fails in pre-existing runtime files unrelated to this change)
2026-04-05 16:41:02 +00:00
Yeachan-Heo
fa72cd665e Block oversized requests before providers hard-fail
The runtime already tracked rough token estimates for compaction, but provider-bound
requests still relied on naive model output limits and could be sent upstream even
when the selected model could not fit the estimated prompt plus requested output.

This adds a small model token/context registry in the API layer, estimates request
size from the serialized prompt payload, and fails locally with a dedicated
context-window error before Anthropic or xAI calls are made. Focused integration
coverage asserts the preflight fires before any HTTP request leaves the process.

Constraint: Keep the first pass minimal and reusable across both Anthropic and OpenAI-compatible providers
Rejected: Auto-compact-and-retry in the same patch | broader control-flow change than the requested minimal preflight
Confidence: medium
Scope-risk: narrow
Reversibility: clean
Directive: Expand the model registry before enabling preflight for additional providers or aliases
Tested: cargo build -p api -p tools -p rusty-claude-cli; cargo test -p api
Not-tested: End-to-end CLI auto-compaction or retry behavior after a local context_window_blocked failure
2026-04-05 16:39:58 +00:00
23 changed files with 1623 additions and 328 deletions

BIN
assets/sigrid-photo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

View File

@@ -0,0 +1,2 @@
{"created_at_ms":1775386832313,"session_id":"session-1775386832313-0","type":"session_meta","updated_at_ms":1775386832313,"version":1}
{"message":{"blocks":[{"text":"status --help","type":"text"}],"role":"user"},"type":"message"}

View File

@@ -0,0 +1,2 @@
{"created_at_ms":1775386842352,"session_id":"session-1775386842352-0","type":"session_meta","updated_at_ms":1775386842352,"version":1}
{"message":{"blocks":[{"text":"doctor --help","type":"text"}],"role":"user"},"type":"message"}

View File

@@ -0,0 +1,2 @@
{"created_at_ms":1775386852257,"session_id":"session-1775386852257-0","type":"session_meta","updated_at_ms":1775386852257,"version":1}
{"message":{"blocks":[{"text":"doctor --help","type":"text"}],"role":"user"},"type":"message"}

View File

@@ -0,0 +1,2 @@
{"created_at_ms":1775386853666,"session_id":"session-1775386853666-0","type":"session_meta","updated_at_ms":1775386853666,"version":1}
{"message":{"blocks":[{"text":"status --help","type":"text"}],"role":"user"},"type":"message"}

View File

@@ -8,6 +8,13 @@ pub enum ApiError {
provider: &'static str, provider: &'static str,
env_vars: &'static [&'static str], env_vars: &'static [&'static str],
}, },
ContextWindowExceeded {
model: String,
estimated_input_tokens: u32,
requested_output_tokens: u32,
estimated_total_tokens: u32,
context_window_tokens: u32,
},
ExpiredOAuthToken, ExpiredOAuthToken,
Auth(String), Auth(String),
InvalidApiKeyEnv(VarError), InvalidApiKeyEnv(VarError),
@@ -48,6 +55,7 @@ impl ApiError {
Self::Api { retryable, .. } => *retryable, Self::Api { retryable, .. } => *retryable,
Self::RetriesExhausted { last_error, .. } => last_error.is_retryable(), Self::RetriesExhausted { last_error, .. } => last_error.is_retryable(),
Self::MissingCredentials { .. } Self::MissingCredentials { .. }
| Self::ContextWindowExceeded { .. }
| Self::ExpiredOAuthToken | Self::ExpiredOAuthToken
| Self::Auth(_) | Self::Auth(_)
| Self::InvalidApiKeyEnv(_) | Self::InvalidApiKeyEnv(_)
@@ -67,6 +75,16 @@ impl Display for ApiError {
"missing {provider} credentials; export {} before calling the {provider} API", "missing {provider} credentials; export {} before calling the {provider} API",
env_vars.join(" or ") env_vars.join(" or ")
), ),
Self::ContextWindowExceeded {
model,
estimated_input_tokens,
requested_output_tokens,
estimated_total_tokens,
context_window_tokens,
} => write!(
f,
"context_window_blocked for {model}: estimated input {estimated_input_tokens} + requested output {requested_output_tokens} = {estimated_total_tokens} tokens exceeds the {context_window_tokens}-token context window; compact the session or reduce request size before retrying"
),
Self::ExpiredOAuthToken => { Self::ExpiredOAuthToken => {
write!( write!(
f, f,

View File

@@ -14,7 +14,7 @@ use telemetry::{AnalyticsEvent, AnthropicRequestProfile, ClientIdentity, Session
use crate::error::ApiError; use crate::error::ApiError;
use crate::prompt_cache::{PromptCache, PromptCacheRecord, PromptCacheStats}; use crate::prompt_cache::{PromptCache, PromptCacheRecord, PromptCacheStats};
use super::{Provider, ProviderFuture}; use super::{preflight_message_request, Provider, ProviderFuture};
use crate::sse::SseParser; use crate::sse::SseParser;
use crate::types::{MessageDeltaEvent, MessageRequest, MessageResponse, StreamEvent, Usage}; use crate::types::{MessageDeltaEvent, MessageRequest, MessageResponse, StreamEvent, Usage};
@@ -294,6 +294,8 @@ impl AnthropicClient {
} }
} }
preflight_message_request(&request)?;
let response = self.send_with_retry(&request).await?; let response = self.send_with_retry(&request).await?;
let request_id = request_id_from_headers(response.headers()); let request_id = request_id_from_headers(response.headers());
let mut response = response let mut response = response
@@ -337,6 +339,7 @@ impl AnthropicClient {
&self, &self,
request: &MessageRequest, request: &MessageRequest,
) -> Result<MessageStream, ApiError> { ) -> Result<MessageStream, ApiError> {
preflight_message_request(request)?;
let response = self let response = self
.send_with_retry(&request.clone().with_streaming()) .send_with_retry(&request.clone().with_streaming())
.await?; .await?;

View File

@@ -1,6 +1,8 @@
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use serde::Serialize;
use crate::error::ApiError; use crate::error::ApiError;
use crate::types::{MessageRequest, MessageResponse}; use crate::types::{MessageRequest, MessageResponse};
@@ -40,6 +42,12 @@ pub struct ProviderMetadata {
pub default_base_url: &'static str, pub default_base_url: &'static str,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ModelTokenLimit {
pub max_output_tokens: u32,
pub context_window_tokens: u32,
}
const MODEL_REGISTRY: &[(&str, ProviderMetadata)] = &[ const MODEL_REGISTRY: &[(&str, ProviderMetadata)] = &[
( (
"opus", "opus",
@@ -182,17 +190,86 @@ pub fn detect_provider_kind(model: &str) -> ProviderKind {
#[must_use] #[must_use]
pub fn max_tokens_for_model(model: &str) -> u32 { pub fn max_tokens_for_model(model: &str) -> u32 {
model_token_limit(model).map_or_else(
|| {
let canonical = resolve_model_alias(model);
if canonical.contains("opus") {
32_000
} else {
64_000
}
},
|limit| limit.max_output_tokens,
)
}
#[must_use]
pub fn model_token_limit(model: &str) -> Option<ModelTokenLimit> {
let canonical = resolve_model_alias(model); let canonical = resolve_model_alias(model);
if canonical.contains("opus") { match canonical.as_str() {
32_000 "claude-opus-4-6" => Some(ModelTokenLimit {
} else { max_output_tokens: 32_000,
64_000 context_window_tokens: 200_000,
}),
"claude-sonnet-4-6" | "claude-haiku-4-5-20251213" => Some(ModelTokenLimit {
max_output_tokens: 64_000,
context_window_tokens: 200_000,
}),
"grok-3" | "grok-3-mini" => Some(ModelTokenLimit {
max_output_tokens: 64_000,
context_window_tokens: 131_072,
}),
_ => None,
} }
} }
pub fn preflight_message_request(request: &MessageRequest) -> Result<(), ApiError> {
let Some(limit) = model_token_limit(&request.model) else {
return Ok(());
};
let estimated_input_tokens = estimate_message_request_input_tokens(request);
let estimated_total_tokens = estimated_input_tokens.saturating_add(request.max_tokens);
if estimated_total_tokens > limit.context_window_tokens {
return Err(ApiError::ContextWindowExceeded {
model: resolve_model_alias(&request.model),
estimated_input_tokens,
requested_output_tokens: request.max_tokens,
estimated_total_tokens,
context_window_tokens: limit.context_window_tokens,
});
}
Ok(())
}
fn estimate_message_request_input_tokens(request: &MessageRequest) -> u32 {
let mut estimate = estimate_serialized_tokens(&request.messages);
estimate = estimate.saturating_add(estimate_serialized_tokens(&request.system));
estimate = estimate.saturating_add(estimate_serialized_tokens(&request.tools));
estimate = estimate.saturating_add(estimate_serialized_tokens(&request.tool_choice));
estimate
}
fn estimate_serialized_tokens<T: Serialize>(value: &T) -> u32 {
serde_json::to_vec(value)
.ok()
.map_or(0, |bytes| (bytes.len() / 4 + 1) as u32)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{detect_provider_kind, max_tokens_for_model, resolve_model_alias, ProviderKind}; use serde_json::json;
use crate::error::ApiError;
use crate::types::{
InputContentBlock, InputMessage, MessageRequest, ToolChoice, ToolDefinition,
};
use super::{
detect_provider_kind, max_tokens_for_model, model_token_limit, preflight_message_request,
resolve_model_alias, ProviderKind,
};
#[test] #[test]
fn resolves_grok_aliases() { fn resolves_grok_aliases() {
@@ -215,4 +292,86 @@ mod tests {
assert_eq!(max_tokens_for_model("opus"), 32_000); assert_eq!(max_tokens_for_model("opus"), 32_000);
assert_eq!(max_tokens_for_model("grok-3"), 64_000); assert_eq!(max_tokens_for_model("grok-3"), 64_000);
} }
#[test]
fn returns_context_window_metadata_for_supported_models() {
assert_eq!(
model_token_limit("claude-sonnet-4-6")
.expect("claude-sonnet-4-6 should be registered")
.context_window_tokens,
200_000
);
assert_eq!(
model_token_limit("grok-mini")
.expect("grok-mini should resolve to a registered model")
.context_window_tokens,
131_072
);
}
#[test]
fn preflight_blocks_requests_that_exceed_the_model_context_window() {
let request = MessageRequest {
model: "claude-sonnet-4-6".to_string(),
max_tokens: 64_000,
messages: vec![InputMessage {
role: "user".to_string(),
content: vec![InputContentBlock::Text {
text: "x".repeat(600_000),
}],
}],
system: Some("Keep the answer short.".to_string()),
tools: Some(vec![ToolDefinition {
name: "weather".to_string(),
description: Some("Fetches weather".to_string()),
input_schema: json!({
"type": "object",
"properties": { "city": { "type": "string" } },
}),
}]),
tool_choice: Some(ToolChoice::Auto),
stream: true,
};
let error = preflight_message_request(&request)
.expect_err("oversized request should be rejected before the provider call");
match error {
ApiError::ContextWindowExceeded {
model,
estimated_input_tokens,
requested_output_tokens,
estimated_total_tokens,
context_window_tokens,
} => {
assert_eq!(model, "claude-sonnet-4-6");
assert!(estimated_input_tokens > 136_000);
assert_eq!(requested_output_tokens, 64_000);
assert!(estimated_total_tokens > context_window_tokens);
assert_eq!(context_window_tokens, 200_000);
}
other => panic!("expected context-window preflight failure, got {other:?}"),
}
}
#[test]
fn preflight_skips_unknown_models() {
let request = MessageRequest {
model: "unknown-model".to_string(),
max_tokens: 64_000,
messages: vec![InputMessage {
role: "user".to_string(),
content: vec![InputContentBlock::Text {
text: "x".repeat(600_000),
}],
}],
system: None,
tools: None,
tool_choice: None,
stream: false,
};
preflight_message_request(&request)
.expect("models without context metadata should skip the guarded preflight");
}
} }

View File

@@ -12,7 +12,7 @@ use crate::types::{
ToolChoice, ToolDefinition, ToolResultContentBlock, Usage, ToolChoice, ToolDefinition, ToolResultContentBlock, Usage,
}; };
use super::{Provider, ProviderFuture}; use super::{preflight_message_request, Provider, ProviderFuture};
pub const DEFAULT_XAI_BASE_URL: &str = "https://api.x.ai/v1"; pub const DEFAULT_XAI_BASE_URL: &str = "https://api.x.ai/v1";
pub const DEFAULT_OPENAI_BASE_URL: &str = "https://api.openai.com/v1"; pub const DEFAULT_OPENAI_BASE_URL: &str = "https://api.openai.com/v1";
@@ -128,6 +128,7 @@ impl OpenAiCompatClient {
stream: false, stream: false,
..request.clone() ..request.clone()
}; };
preflight_message_request(&request)?;
let response = self.send_with_retry(&request).await?; let response = self.send_with_retry(&request).await?;
let request_id = request_id_from_headers(response.headers()); let request_id = request_id_from_headers(response.headers());
let payload = response.json::<ChatCompletionResponse>().await?; let payload = response.json::<ChatCompletionResponse>().await?;
@@ -142,6 +143,7 @@ impl OpenAiCompatClient {
&self, &self,
request: &MessageRequest, request: &MessageRequest,
) -> Result<MessageStream, ApiError> { ) -> Result<MessageStream, ApiError> {
preflight_message_request(request)?;
let response = self let response = self
.send_with_retry(&request.clone().with_streaming()) .send_with_retry(&request.clone().with_streaming())
.await?; .await?;

View File

@@ -103,6 +103,41 @@ async fn send_message_posts_json_and_parses_response() {
); );
} }
#[tokio::test]
async fn send_message_blocks_oversized_requests_before_the_http_call() {
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
let server = spawn_server(
state.clone(),
vec![http_response("200 OK", "application/json", "{}")],
)
.await;
let client = AnthropicClient::new("test-key").with_base_url(server.base_url());
let error = client
.send_message(&MessageRequest {
model: "claude-sonnet-4-6".to_string(),
max_tokens: 64_000,
messages: vec![InputMessage {
role: "user".to_string(),
content: vec![InputContentBlock::Text {
text: "x".repeat(600_000),
}],
}],
system: Some("Keep the answer short.".to_string()),
tools: None,
tool_choice: None,
stream: false,
})
.await
.expect_err("oversized request should fail local context-window preflight");
assert!(matches!(error, ApiError::ContextWindowExceeded { .. }));
assert!(
state.lock().await.is_empty(),
"preflight failure should avoid any upstream HTTP request"
);
}
#[tokio::test] #[tokio::test]
async fn send_message_applies_request_profile_and_records_telemetry() { async fn send_message_applies_request_profile_and_records_telemetry() {
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new())); let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));

View File

@@ -4,10 +4,10 @@ use std::sync::Arc;
use std::sync::{Mutex as StdMutex, OnceLock}; use std::sync::{Mutex as StdMutex, OnceLock};
use api::{ use api::{
ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent, ApiError, ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent,
InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest, OpenAiCompatClient, ContentBlockStopEvent, InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest,
OpenAiCompatConfig, OutputContentBlock, ProviderClient, StreamEvent, ToolChoice, OpenAiCompatClient, OpenAiCompatConfig, OutputContentBlock, ProviderClient, StreamEvent,
ToolDefinition, ToolChoice, ToolDefinition,
}; };
use serde_json::json; use serde_json::json;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
@@ -63,6 +63,42 @@ async fn send_message_uses_openai_compatible_endpoint_and_auth() {
assert_eq!(body["tools"][0]["type"], json!("function")); assert_eq!(body["tools"][0]["type"], json!("function"));
} }
#[tokio::test]
async fn send_message_blocks_oversized_xai_requests_before_the_http_call() {
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
let server = spawn_server(
state.clone(),
vec![http_response("200 OK", "application/json", "{}")],
)
.await;
let client = OpenAiCompatClient::new("xai-test-key", OpenAiCompatConfig::xai())
.with_base_url(server.base_url());
let error = client
.send_message(&MessageRequest {
model: "grok-3".to_string(),
max_tokens: 64_000,
messages: vec![InputMessage {
role: "user".to_string(),
content: vec![InputContentBlock::Text {
text: "x".repeat(300_000),
}],
}],
system: Some("Keep the answer short.".to_string()),
tools: None,
tool_choice: None,
stream: false,
})
.await
.expect_err("oversized request should fail local context-window preflight");
assert!(matches!(error, ApiError::ContextWindowExceeded { .. }));
assert!(
state.lock().await.is_empty(),
"preflight failure should avoid any upstream HTTP request"
);
}
#[tokio::test] #[tokio::test]
async fn send_message_accepts_full_chat_completions_endpoint_override() { async fn send_message_accepts_full_chat_completions_endpoint_override() {
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new())); let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));

View File

@@ -114,8 +114,12 @@ impl LaneEvent {
#[must_use] #[must_use]
pub fn finished(emitted_at: impl Into<String>, detail: Option<String>) -> Self { pub fn finished(emitted_at: impl Into<String>, detail: Option<String>) -> Self {
Self::new(LaneEventName::Finished, LaneEventStatus::Completed, emitted_at) Self::new(
.with_optional_detail(detail) LaneEventName::Finished,
LaneEventStatus::Completed,
emitted_at,
)
.with_optional_detail(detail)
} }
#[must_use] #[must_use]
@@ -161,19 +165,14 @@ impl LaneEvent {
mod tests { mod tests {
use serde_json::json; use serde_json::json;
use super::{ use super::{LaneEvent, LaneEventBlocker, LaneEventName, LaneEventStatus, LaneFailureClass};
LaneEvent, LaneEventBlocker, LaneEventName, LaneEventStatus, LaneFailureClass,
};
#[test] #[test]
fn canonical_lane_event_names_serialize_to_expected_wire_values() { fn canonical_lane_event_names_serialize_to_expected_wire_values() {
let cases = [ let cases = [
(LaneEventName::Started, "lane.started"), (LaneEventName::Started, "lane.started"),
(LaneEventName::Ready, "lane.ready"), (LaneEventName::Ready, "lane.ready"),
( (LaneEventName::PromptMisdelivery, "lane.prompt_misdelivery"),
LaneEventName::PromptMisdelivery,
"lane.prompt_misdelivery",
),
(LaneEventName::Blocked, "lane.blocked"), (LaneEventName::Blocked, "lane.blocked"),
(LaneEventName::Red, "lane.red"), (LaneEventName::Red, "lane.red"),
(LaneEventName::Green, "lane.green"), (LaneEventName::Green, "lane.green"),
@@ -193,7 +192,10 @@ mod tests {
]; ];
for (event, expected) in cases { for (event, expected) in cases {
assert_eq!(serde_json::to_value(event).expect("serialize event"), json!(expected)); assert_eq!(
serde_json::to_value(event).expect("serialize event"),
json!(expected)
);
} }
} }

View File

@@ -599,7 +599,10 @@ mod tests {
)); ));
match result { match result {
McpPhaseResult::Failure { phase: failed_phase, error } => { McpPhaseResult::Failure {
phase: failed_phase,
error,
} => {
assert_eq!(failed_phase, phase); assert_eq!(failed_phase, phase);
assert_eq!(error.phase, phase); assert_eq!(error.phase, phase);
assert_eq!( assert_eq!(

View File

@@ -360,8 +360,10 @@ impl McpServerManagerError {
} }
fn recoverable(&self) -> bool { fn recoverable(&self) -> bool {
!matches!(self.lifecycle_phase(), McpLifecyclePhase::InitializeHandshake) !matches!(
&& matches!(self, Self::Transport { .. } | Self::Timeout { .. }) self.lifecycle_phase(),
McpLifecyclePhase::InitializeHandshake
) && matches!(self, Self::Transport { .. } | Self::Timeout { .. })
} }
fn discovery_failure(&self, server_name: &str) -> McpDiscoveryFailure { fn discovery_failure(&self, server_name: &str) -> McpDiscoveryFailure {
@@ -417,10 +419,9 @@ impl McpServerManagerError {
("method".to_string(), (*method).to_string()), ("method".to_string(), (*method).to_string()),
("timeout_ms".to_string(), timeout_ms.to_string()), ("timeout_ms".to_string(), timeout_ms.to_string()),
]), ]),
Self::UnknownTool { qualified_name } => BTreeMap::from([( Self::UnknownTool { qualified_name } => {
"qualified_tool".to_string(), BTreeMap::from([("qualified_tool".to_string(), qualified_name.clone())])
qualified_name.clone(), }
)]),
Self::UnknownServer { server_name } => { Self::UnknownServer { server_name } => {
BTreeMap::from([("server".to_string(), server_name.clone())]) BTreeMap::from([("server".to_string(), server_name.clone())])
} }
@@ -1425,11 +1426,10 @@ mod tests {
use crate::mcp_client::McpClientBootstrap; use crate::mcp_client::McpClientBootstrap;
use super::{ use super::{
spawn_mcp_stdio_process, JsonRpcId, JsonRpcRequest, JsonRpcResponse, spawn_mcp_stdio_process, unsupported_server_failed_server, JsonRpcId, JsonRpcRequest,
McpInitializeClientInfo, McpInitializeParams, McpInitializeResult, McpInitializeServerInfo, JsonRpcResponse, McpInitializeClientInfo, McpInitializeParams, McpInitializeResult,
McpListToolsResult, McpReadResourceParams, McpReadResourceResult, McpServerManager, McpInitializeServerInfo, McpListToolsResult, McpReadResourceParams, McpReadResourceResult,
McpServerManagerError, McpStdioProcess, McpTool, McpToolCallParams, McpServerManager, McpServerManagerError, McpStdioProcess, McpTool, McpToolCallParams,
unsupported_server_failed_server,
}; };
use crate::McpLifecyclePhase; use crate::McpLifecyclePhase;
@@ -2698,7 +2698,10 @@ mod tests {
); );
assert!(!report.failed_servers[0].recoverable); assert!(!report.failed_servers[0].recoverable);
assert_eq!( assert_eq!(
report.failed_servers[0].context.get("method").map(String::as_str), report.failed_servers[0]
.context
.get("method")
.map(String::as_str),
Some("initialize") Some("initialize")
); );
assert!(report.failed_servers[0].error.contains("initialize")); assert!(report.failed_servers[0].error.contains("initialize"));

View File

@@ -4,7 +4,6 @@ use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH; use std::time::UNIX_EPOCH;
use crate::session::{Session, SessionError}; use crate::session::{Session, SessionError};
pub const PRIMARY_SESSION_EXTENSION: &str = "jsonl"; pub const PRIMARY_SESSION_EXTENSION: &str = "jsonl";

View File

@@ -66,11 +66,7 @@ pub fn validate_packet(packet: TaskPacket) -> Result<ValidatedPacket, TaskPacket
&packet.reporting_contract, &packet.reporting_contract,
&mut errors, &mut errors,
); );
validate_required( validate_required("escalation_policy", &packet.escalation_policy, &mut errors);
"escalation_policy",
&packet.escalation_policy,
&mut errors,
);
for (index, test) in packet.acceptance_tests.iter().enumerate() { for (index, test) in packet.acceptance_tests.iter().enumerate() {
if test.trim().is_empty() { if test.trim().is_empty() {
@@ -146,9 +142,9 @@ mod tests {
assert!(error assert!(error
.errors() .errors()
.contains(&"repo must not be empty".to_string())); .contains(&"repo must not be empty".to_string()));
assert!(error.errors().contains( assert!(error
&"acceptance_tests contains an empty value at index 1".to_string() .errors()
)); .contains(&"acceptance_tests contains an empty value at index 1".to_string()));
} }
#[test] #[test]

View File

@@ -76,11 +76,7 @@ impl TaskRegistry {
} }
pub fn create(&self, prompt: &str, description: Option<&str>) -> Task { pub fn create(&self, prompt: &str, description: Option<&str>) -> Task {
self.create_task( self.create_task(prompt.to_owned(), description.map(str::to_owned), None)
prompt.to_owned(),
description.map(str::to_owned),
None,
)
} }
pub fn create_from_packet( pub fn create_from_packet(

View File

@@ -257,7 +257,9 @@ impl WorkerRegistry {
let prompt_preview = prompt_preview(worker.last_prompt.as_deref().unwrap_or_default()); let prompt_preview = prompt_preview(worker.last_prompt.as_deref().unwrap_or_default());
let message = match observation.target { let message = match observation.target {
WorkerPromptTarget::Shell => { WorkerPromptTarget::Shell => {
format!("worker prompt landed in shell instead of coding agent: {prompt_preview}") format!(
"worker prompt landed in shell instead of coding agent: {prompt_preview}"
)
} }
WorkerPromptTarget::WrongTarget => format!( WorkerPromptTarget::WrongTarget => format!(
"worker prompt landed in the wrong target instead of {}: {}", "worker prompt landed in the wrong target instead of {}: {}",
@@ -312,7 +314,9 @@ impl WorkerRegistry {
worker.last_error = None; worker.last_error = None;
} }
if detect_ready_for_prompt(screen_text, &lowered) && worker.status != WorkerStatus::ReadyForPrompt { if detect_ready_for_prompt(screen_text, &lowered)
&& worker.status != WorkerStatus::ReadyForPrompt
{
worker.status = WorkerStatus::ReadyForPrompt; worker.status = WorkerStatus::ReadyForPrompt;
worker.prompt_in_flight = false; worker.prompt_in_flight = false;
if matches!( if matches!(
@@ -412,7 +416,10 @@ impl WorkerRegistry {
worker_id: worker.worker_id.clone(), worker_id: worker.worker_id.clone(),
status: worker.status, status: worker.status,
ready: worker.status == WorkerStatus::ReadyForPrompt, ready: worker.status == WorkerStatus::ReadyForPrompt,
blocked: matches!(worker.status, WorkerStatus::TrustRequired | WorkerStatus::Failed), blocked: matches!(
worker.status,
WorkerStatus::TrustRequired | WorkerStatus::Failed
),
replay_prompt_ready: worker.replay_prompt.is_some(), replay_prompt_ready: worker.replay_prompt.is_some(),
last_error: worker.last_error.clone(), last_error: worker.last_error.clone(),
}) })

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,449 @@
use std::fs;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::sync::atomic::{AtomicU64, Ordering};
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};
use mock_anthropic_service::{MockAnthropicService, SCENARIO_PREFIX};
use serde_json::{json, Value};
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
#[test]
fn help_emits_json_when_requested() {
let root = unique_temp_dir("help-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "help"], &envs);
assert_eq!(parsed["kind"], "help");
assert!(parsed["message"]
.as_str()
.expect("help message")
.contains("Usage:"));
}
#[test]
fn version_emits_json_when_requested() {
let root = unique_temp_dir("version-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "version"], &envs);
assert_eq!(parsed["kind"], "version");
assert_eq!(parsed["version"], env!("CARGO_PKG_VERSION"));
}
#[test]
fn status_emits_json_when_requested() {
let root = unique_temp_dir("status-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "status"], &envs);
assert_eq!(parsed["kind"], "status");
assert!(parsed["workspace"]["cwd"].as_str().is_some());
}
#[test]
fn sandbox_emits_json_when_requested() {
let root = unique_temp_dir("sandbox-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "sandbox"], &envs);
assert_eq!(parsed["kind"], "sandbox");
assert!(parsed["sandbox"].is_object());
}
#[test]
fn dump_manifests_emits_json_when_requested() {
let root = unique_temp_dir("dump-manifests-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let upstream = write_upstream_fixture(&root);
let mut envs = isolated_env(&root);
envs.push((
"CLAUDE_CODE_UPSTREAM".to_string(),
upstream.display().to_string(),
));
let parsed = assert_json_command(&root, &["--output-format", "json", "dump-manifests"], &envs);
assert_eq!(parsed["kind"], "dump-manifests");
assert_eq!(parsed["commands"], 1);
assert_eq!(parsed["tools"], 1);
}
#[test]
fn bootstrap_plan_emits_json_when_requested() {
let root = unique_temp_dir("bootstrap-plan-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "bootstrap-plan"], &envs);
assert_eq!(parsed["kind"], "bootstrap-plan");
assert!(parsed["phases"].as_array().expect("phases array").len() > 1);
}
#[test]
fn agents_emits_json_when_requested() {
let root = unique_temp_dir("agents-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "agents"], &envs);
assert_eq!(parsed["kind"], "agents");
assert!(!parsed["message"].as_str().expect("agents text").is_empty());
}
#[test]
fn mcp_emits_json_when_requested() {
let root = unique_temp_dir("mcp-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "mcp"], &envs);
assert_eq!(parsed["kind"], "mcp");
assert!(parsed["message"]
.as_str()
.expect("mcp text")
.contains("MCP"));
}
#[test]
fn skills_emits_json_when_requested() {
let root = unique_temp_dir("skills-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "skills"], &envs);
assert_eq!(parsed["kind"], "skills");
assert!(!parsed["message"].as_str().expect("skills text").is_empty());
}
#[test]
fn system_prompt_emits_json_when_requested() {
let root = unique_temp_dir("system-prompt-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "system-prompt"], &envs);
assert_eq!(parsed["kind"], "system-prompt");
assert!(parsed["message"]
.as_str()
.expect("system prompt text")
.contains("You are an interactive agent"));
}
#[test]
fn login_emits_json_when_requested() {
let root = unique_temp_dir("login-json");
let workspace = root.join("workspace");
fs::create_dir_all(&workspace).expect("workspace should exist");
let mut envs = isolated_env(&root);
let callback_port = reserve_port();
let token_port = reserve_port();
fs::create_dir_all(workspace.join(".claw")).expect("config dir should exist");
fs::write(
workspace.join(".claw").join("settings.json"),
json!({
"oauth": {
"clientId": "test-client",
"authorizeUrl": format!("http://127.0.0.1:{token_port}/authorize"),
"tokenUrl": format!("http://127.0.0.1:{token_port}/token"),
"callbackPort": callback_port,
"scopes": ["user:test"]
}
})
.to_string(),
)
.expect("oauth config should write");
let token_server = thread::spawn(move || {
let listener = TcpListener::bind(("127.0.0.1", token_port)).expect("token server bind");
let (mut stream, _) = listener.accept().expect("token request");
let mut request = [0_u8; 4096];
let _ = stream
.read(&mut request)
.expect("token request should read");
let body = json!({
"access_token": "test-access-token",
"refresh_token": "test-refresh-token",
"expires_at": 9_999_999_999_u64,
"scopes": ["user:test"]
})
.to_string();
let response = format!(
"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",
body.len(),
body
);
stream
.write_all(response.as_bytes())
.expect("token response should write");
});
let bin_dir = root.join("bin");
fs::create_dir_all(&bin_dir).expect("bin dir should exist");
let opener_path = bin_dir.join("xdg-open");
fs::write(
&opener_path,
format!(
"#!/usr/bin/env python3\nimport http.client\nimport sys\nimport urllib.parse\nurl = sys.argv[1]\nquery = urllib.parse.parse_qs(urllib.parse.urlparse(url).query)\nstate = query['state'][0]\nconn = http.client.HTTPConnection('127.0.0.1', {callback_port}, timeout=5)\nconn.request('GET', f\"/callback?code=test-code&state={{urllib.parse.quote(state)}}\")\nresp = conn.getresponse()\nresp.read()\nconn.close()\n"
),
)
.expect("xdg-open wrapper should write");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut permissions = fs::metadata(&opener_path)
.expect("wrapper metadata")
.permissions();
permissions.set_mode(0o755);
fs::set_permissions(&opener_path, permissions).expect("wrapper permissions");
}
let original_path = envs
.iter()
.find(|(key, _)| key == "PATH")
.map(|(_, value)| value.clone())
.unwrap_or_default();
for (key, value) in &mut envs {
if key == "PATH" {
*value = format!("{}:{original_path}", bin_dir.display());
}
}
let parsed = assert_json_command(&workspace, &["--output-format", "json", "login"], &envs);
token_server.join().expect("token server should finish");
assert_eq!(parsed["kind"], "login");
assert_eq!(parsed["callback_port"], callback_port);
}
#[test]
fn logout_emits_json_when_requested() {
let root = unique_temp_dir("logout-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&root, &["--output-format", "json", "logout"], &envs);
assert_eq!(parsed["kind"], "logout");
assert!(parsed["message"]
.as_str()
.expect("logout text")
.contains("cleared"));
}
#[test]
fn init_emits_json_when_requested() {
let root = unique_temp_dir("init-json");
let workspace = root.join("workspace");
fs::create_dir_all(&workspace).expect("workspace should exist");
let envs = isolated_env(&root);
let parsed = assert_json_command(&workspace, &["--output-format", "json", "init"], &envs);
assert_eq!(parsed["kind"], "init");
assert!(workspace.join("CLAUDE.md").exists());
}
#[test]
fn prompt_subcommand_emits_json_when_requested() {
let root = unique_temp_dir("prompt-subcommand-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let mut envs = isolated_env(&root);
let runtime = tokio::runtime::Runtime::new().expect("tokio runtime should build");
let server = runtime
.block_on(MockAnthropicService::spawn())
.expect("mock service should start");
envs.push(("ANTHROPIC_API_KEY".to_string(), "test-key".to_string()));
envs.push(("ANTHROPIC_BASE_URL".to_string(), server.base_url()));
let prompt = format!("{SCENARIO_PREFIX}streaming_text");
let args = vec![
"--model".to_string(),
"sonnet".to_string(),
"--permission-mode".to_string(),
"read-only".to_string(),
"--output-format".to_string(),
"json".to_string(),
"prompt".to_string(),
prompt,
];
let output = run_claw_with_env_owned(&root, &args, &envs);
let parsed = parse_json_stdout(&output);
assert_eq!(parsed["model"], "claude-sonnet-4-6");
assert!(parsed["message"]
.as_str()
.expect("assistant text")
.contains("streaming"));
}
#[test]
fn bare_prompt_mode_emits_json_when_requested() {
let root = unique_temp_dir("bare-prompt-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let mut envs = isolated_env(&root);
let runtime = tokio::runtime::Runtime::new().expect("tokio runtime should build");
let server = runtime
.block_on(MockAnthropicService::spawn())
.expect("mock service should start");
envs.push(("ANTHROPIC_API_KEY".to_string(), "test-key".to_string()));
envs.push(("ANTHROPIC_BASE_URL".to_string(), server.base_url()));
let prompt = format!("{SCENARIO_PREFIX}streaming_text");
let args = vec![
"--model".to_string(),
"sonnet".to_string(),
"--permission-mode".to_string(),
"read-only".to_string(),
"--output-format".to_string(),
"json".to_string(),
prompt,
];
let output = run_claw_with_env_owned(&root, &args, &envs);
let parsed = parse_json_stdout(&output);
assert_eq!(parsed["model"], "claude-sonnet-4-6");
assert!(parsed["message"]
.as_str()
.expect("assistant text")
.contains("streaming"));
}
#[test]
fn resume_restore_emits_json_when_requested() {
let root = unique_temp_dir("resume-json");
fs::create_dir_all(&root).expect("temp dir should exist");
let envs = isolated_env(&root);
let session_path = root.join("session.jsonl");
fs::write(
&session_path,
"{\"type\":\"session_meta\",\"version\":3,\"session_id\":\"resume-json\",\"created_at_ms\":0,\"updated_at_ms\":0}\n{\"type\":\"message\",\"message\":{\"role\":\"user\",\"blocks\":[{\"type\":\"text\",\"text\":\"hello\"}]}}\n",
)
.expect("session should write");
let args = vec![
"--output-format".to_string(),
"json".to_string(),
"--resume".to_string(),
session_path.display().to_string(),
];
let output = run_claw_with_env_owned(&root, &args, &envs);
let parsed = parse_json_stdout(&output);
assert_eq!(parsed["kind"], "resume");
assert_eq!(parsed["messages"], 1);
}
fn assert_json_command(current_dir: &Path, args: &[&str], envs: &[(String, String)]) -> Value {
let output = run_claw_with_env(current_dir, args, envs);
parse_json_stdout(&output)
}
fn parse_json_stdout(output: &Output) -> Value {
assert!(
output.status.success(),
"stdout:\n{}\n\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
serde_json::from_slice(&output.stdout).expect("stdout should be json")
}
fn run_claw_with_env(current_dir: &Path, args: &[&str], envs: &[(String, String)]) -> Output {
let owned_args = args
.iter()
.map(|value| (*value).to_string())
.collect::<Vec<_>>();
run_claw_with_env_owned(current_dir, &owned_args, envs)
}
fn run_claw_with_env_owned(
current_dir: &Path,
args: &[String],
envs: &[(String, String)],
) -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_claw"));
command.current_dir(current_dir).args(args).env_clear();
for (key, value) in envs {
command.env(key, value);
}
command.output().expect("claw should launch")
}
fn isolated_env(root: &Path) -> Vec<(String, String)> {
let config_home = root.join("config-home");
let home = root.join("home");
fs::create_dir_all(&config_home).expect("config home should exist");
fs::create_dir_all(&home).expect("home should exist");
vec![
(
"CLAW_CONFIG_HOME".to_string(),
config_home.display().to_string(),
),
("HOME".to_string(), home.display().to_string()),
(
"PATH".to_string(),
std::env::var("PATH").unwrap_or_else(|_| "/usr/bin:/bin".to_string()),
),
("NO_COLOR".to_string(), "1".to_string()),
]
}
fn write_upstream_fixture(root: &Path) -> PathBuf {
let upstream = root.join("claw-code");
let src = upstream.join("src");
let entrypoints = src.join("entrypoints");
fs::create_dir_all(&entrypoints).expect("upstream entrypoints dir should exist");
fs::write(
src.join("commands.ts"),
"import FooCommand from './commands/foo'\n",
)
.expect("commands fixture should write");
fs::write(
src.join("tools.ts"),
"import ReadTool from './tools/read'\n",
)
.expect("tools fixture should write");
fs::write(
entrypoints.join("cli.tsx"),
"if (args[0] === '--version') {}\nstartupProfiler()\n",
)
.expect("cli fixture should write");
upstream
}
fn reserve_port() -> u16 {
let listener = TcpListener::bind(("127.0.0.1", 0)).expect("ephemeral port should bind");
let port = listener.local_addr().expect("local addr").port();
drop(listener);
port
}
fn unique_temp_dir(label: &str) -> PathBuf {
let millis = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("clock should be after epoch")
.as_millis();
let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!(
"claw-output-format-{label}-{}-{millis}-{counter}",
std::process::id()
))
}

View File

@@ -7,6 +7,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use runtime::ContentBlock; use runtime::ContentBlock;
use runtime::Session; use runtime::Session;
use serde_json::Value;
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0); static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
@@ -221,6 +222,52 @@ fn resume_latest_restores_the_most_recent_managed_session() {
assert!(stdout.contains(newer_path.to_str().expect("utf8 path"))); assert!(stdout.contains(newer_path.to_str().expect("utf8 path")));
} }
#[test]
fn resumed_status_command_emits_structured_json_when_requested() {
// given
let temp_dir = unique_temp_dir("resume-status-json");
fs::create_dir_all(&temp_dir).expect("temp dir should exist");
let session_path = temp_dir.join("session.jsonl");
let mut session = Session::new();
session
.push_user_text("resume status json fixture")
.expect("session write should succeed");
session
.save_to_path(&session_path)
.expect("session should persist");
// when
let output = run_claw(
&temp_dir,
&[
"--output-format",
"json",
"--resume",
session_path.to_str().expect("utf8 path"),
"/status",
],
);
// then
assert!(
output.status.success(),
"stdout:\n{}\n\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8(output.stdout).expect("stdout should be utf8");
let parsed: Value =
serde_json::from_str(stdout.trim()).expect("resume status output should be json");
assert_eq!(parsed["kind"], "status");
assert_eq!(parsed["messages"], 1);
assert_eq!(
parsed["workspace"]["session"],
session_path.to_str().expect("utf8 path")
);
}
fn run_claw(current_dir: &Path, args: &[&str]) -> Output { fn run_claw(current_dir: &Path, args: &[&str]) -> Output {
run_claw_with_env(current_dir, args, &[]) run_claw_with_env(current_dir, args, &[])
} }

View File

@@ -16,7 +16,7 @@ use runtime::{
use crate::AgentOutput; use crate::AgentOutput;
/// Detects if a lane should be automatically marked as completed. /// Detects if a lane should be automatically marked as completed.
/// ///
/// Returns `Some(LaneContext)` with `completed = true` if all conditions met, /// Returns `Some(LaneContext)` with `completed = true` if all conditions met,
/// `None` if lane should remain active. /// `None` if lane should remain active.
#[allow(dead_code)] #[allow(dead_code)]
@@ -29,29 +29,29 @@ pub(crate) fn detect_lane_completion(
if output.error.is_some() { if output.error.is_some() {
return None; return None;
} }
// Must have finished status // Must have finished status
if !output.status.eq_ignore_ascii_case("completed") if !output.status.eq_ignore_ascii_case("completed")
&& !output.status.eq_ignore_ascii_case("finished") && !output.status.eq_ignore_ascii_case("finished")
{ {
return None; return None;
} }
// Must have no current blocker // Must have no current blocker
if output.current_blocker.is_some() { if output.current_blocker.is_some() {
return None; return None;
} }
// Must have green tests // Must have green tests
if !test_green { if !test_green {
return None; return None;
} }
// Must have pushed code // Must have pushed code
if !has_pushed { if !has_pushed {
return None; return None;
} }
// All conditions met — create completed context // All conditions met — create completed context
Some(LaneContext { Some(LaneContext {
lane_id: output.agent_id.clone(), lane_id: output.agent_id.clone(),
@@ -67,9 +67,7 @@ pub(crate) fn detect_lane_completion(
/// Evaluates policy actions for a completed lane. /// Evaluates policy actions for a completed lane.
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) fn evaluate_completed_lane( pub(crate) fn evaluate_completed_lane(context: &LaneContext) -> Vec<PolicyAction> {
context: &LaneContext,
) -> Vec<PolicyAction> {
let engine = PolicyEngine::new(vec![ let engine = PolicyEngine::new(vec![
PolicyRule::new( PolicyRule::new(
"closeout-completed-lane", "closeout-completed-lane",
@@ -87,7 +85,7 @@ pub(crate) fn evaluate_completed_lane(
5, 5,
), ),
]); ]);
evaluate(&engine, context) evaluate(&engine, context)
} }
@@ -114,53 +112,53 @@ mod tests {
error: None, error: None,
} }
} }
#[test] #[test]
fn detects_completion_when_all_conditions_met() { fn detects_completion_when_all_conditions_met() {
let output = test_output(); let output = test_output();
let result = detect_lane_completion(&output, true, true); let result = detect_lane_completion(&output, true, true);
assert!(result.is_some()); assert!(result.is_some());
let context = result.unwrap(); let context = result.unwrap();
assert!(context.completed); assert!(context.completed);
assert_eq!(context.green_level, 3); assert_eq!(context.green_level, 3);
assert_eq!(context.blocker, LaneBlocker::None); assert_eq!(context.blocker, LaneBlocker::None);
} }
#[test] #[test]
fn no_completion_when_error_present() { fn no_completion_when_error_present() {
let mut output = test_output(); let mut output = test_output();
output.error = Some("Build failed".to_string()); output.error = Some("Build failed".to_string());
let result = detect_lane_completion(&output, true, true); let result = detect_lane_completion(&output, true, true);
assert!(result.is_none()); assert!(result.is_none());
} }
#[test] #[test]
fn no_completion_when_not_finished() { fn no_completion_when_not_finished() {
let mut output = test_output(); let mut output = test_output();
output.status = "Running".to_string(); output.status = "Running".to_string();
let result = detect_lane_completion(&output, true, true); let result = detect_lane_completion(&output, true, true);
assert!(result.is_none()); assert!(result.is_none());
} }
#[test] #[test]
fn no_completion_when_tests_not_green() { fn no_completion_when_tests_not_green() {
let output = test_output(); let output = test_output();
let result = detect_lane_completion(&output, false, true); let result = detect_lane_completion(&output, false, true);
assert!(result.is_none()); assert!(result.is_none());
} }
#[test] #[test]
fn no_completion_when_not_pushed() { fn no_completion_when_not_pushed() {
let output = test_output(); let output = test_output();
let result = detect_lane_completion(&output, true, false); let result = detect_lane_completion(&output, true, false);
assert!(result.is_none()); assert!(result.is_none());
} }
#[test] #[test]
fn evaluate_triggers_closeout_for_completed_lane() { fn evaluate_triggers_closeout_for_completed_lane() {
let context = LaneContext { let context = LaneContext {
@@ -173,9 +171,9 @@ mod tests {
completed: true, completed: true,
reconciled: false, reconciled: false,
}; };
let actions = evaluate_completed_lane(&context); let actions = evaluate_completed_lane(&context);
assert!(actions.contains(&PolicyAction::CloseoutLane)); assert!(actions.contains(&PolicyAction::CloseoutLane));
assert!(actions.contains(&PolicyAction::CleanupSession)); assert!(actions.contains(&PolicyAction::CleanupSession));
} }

View File

@@ -17,7 +17,6 @@ use runtime::{
permission_enforcer::{EnforcementResult, PermissionEnforcer}, permission_enforcer::{EnforcementResult, PermissionEnforcer},
read_file, read_file,
summary_compression::compress_summary_text, summary_compression::compress_summary_text,
TaskPacket,
task_registry::TaskRegistry, task_registry::TaskRegistry,
team_cron_registry::{CronRegistry, TeamRegistry}, team_cron_registry::{CronRegistry, TeamRegistry},
worker_boot::{WorkerReadySnapshot, WorkerRegistry}, worker_boot::{WorkerReadySnapshot, WorkerRegistry},
@@ -25,7 +24,7 @@ use runtime::{
BranchFreshness, ContentBlock, ConversationMessage, ConversationRuntime, GrepSearchInput, BranchFreshness, ContentBlock, ConversationMessage, ConversationRuntime, GrepSearchInput,
LaneEvent, LaneEventBlocker, LaneEventName, LaneEventStatus, LaneFailureClass, LaneEvent, LaneEventBlocker, LaneEventName, LaneEventStatus, LaneFailureClass,
McpDegradedReport, MessageRole, PermissionMode, PermissionPolicy, PromptCacheEvent, McpDegradedReport, MessageRole, PermissionMode, PermissionPolicy, PromptCacheEvent,
RuntimeError, Session, ToolError, ToolExecutor, RuntimeError, Session, TaskPacket, ToolError, ToolExecutor,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value}; use serde_json::{json, Value};
@@ -1878,27 +1877,25 @@ fn branch_divergence_output(
dangerously_disable_sandbox: None, dangerously_disable_sandbox: None,
return_code_interpretation: Some("preflight_blocked:branch_divergence".to_string()), return_code_interpretation: Some("preflight_blocked:branch_divergence".to_string()),
no_output_expected: Some(false), no_output_expected: Some(false),
structured_content: Some(vec![ structured_content: Some(vec![serde_json::to_value(
serde_json::to_value( LaneEvent::new(
LaneEvent::new( LaneEventName::BranchStaleAgainstMain,
LaneEventName::BranchStaleAgainstMain, LaneEventStatus::Blocked,
LaneEventStatus::Blocked, iso8601_now(),
iso8601_now(),
)
.with_failure_class(LaneFailureClass::BranchDivergence)
.with_detail(stderr.clone())
.with_data(json!({
"branch": branch,
"mainRef": main_ref,
"commitsBehind": commits_behind,
"commitsAhead": commits_ahead,
"missingCommits": missing_fixes,
"blockedCommand": command,
"recommendedAction": format!("merge or rebase {main_ref} before workspace tests")
})),
) )
.expect("lane event should serialize"), .with_failure_class(LaneFailureClass::BranchDivergence)
]), .with_detail(stderr.clone())
.with_data(json!({
"branch": branch,
"mainRef": main_ref,
"commitsBehind": commits_behind,
"commitsAhead": commits_ahead,
"missingCommits": missing_fixes,
"blockedCommand": command,
"recommendedAction": format!("merge or rebase {main_ref} before workspace tests")
})),
)
.expect("lane event should serialize")]),
persisted_output_path: None, persisted_output_path: None,
persisted_output_size: None, persisted_output_size: None,
sandbox_status: None, sandbox_status: None,
@@ -3297,12 +3294,12 @@ fn persist_agent_terminal_state(
next_manifest.current_blocker = blocker.clone(); next_manifest.current_blocker = blocker.clone();
next_manifest.error = error; next_manifest.error = error;
if let Some(blocker) = blocker { if let Some(blocker) = blocker {
next_manifest.lane_events.push( next_manifest
LaneEvent::blocked(iso8601_now(), &blocker), .lane_events
); .push(LaneEvent::blocked(iso8601_now(), &blocker));
next_manifest.lane_events.push( next_manifest
LaneEvent::failed(iso8601_now(), &blocker), .lane_events
); .push(LaneEvent::failed(iso8601_now(), &blocker));
} else { } else {
next_manifest.current_blocker = None; next_manifest.current_blocker = None;
let compressed_detail = result let compressed_detail = result
@@ -4952,8 +4949,8 @@ mod tests {
agent_permission_policy, allowed_tools_for_subagent, classify_lane_failure, agent_permission_policy, allowed_tools_for_subagent, classify_lane_failure,
execute_agent_with_spawn, execute_tool, final_assistant_text, mvp_tool_specs, execute_agent_with_spawn, execute_tool, final_assistant_text, mvp_tool_specs,
permission_mode_from_plugin, persist_agent_terminal_state, push_output_block, permission_mode_from_plugin, persist_agent_terminal_state, push_output_block,
run_task_packet, AgentInput, AgentJob, GlobalToolRegistry, LaneEventName, run_task_packet, AgentInput, AgentJob, GlobalToolRegistry, LaneEventName, LaneFailureClass,
LaneFailureClass, SubagentToolExecutor, SubagentToolExecutor,
}; };
use api::OutputContentBlock; use api::OutputContentBlock;
use runtime::{ use runtime::{
@@ -5977,7 +5974,10 @@ mod tests {
"gateway routing rejected the request", "gateway routing rejected the request",
LaneFailureClass::GatewayRouting, LaneFailureClass::GatewayRouting,
), ),
("tool failed: denied tool execution from hook", LaneFailureClass::ToolRuntime), (
"tool failed: denied tool execution from hook",
LaneFailureClass::ToolRuntime,
),
("thread creation failed", LaneFailureClass::Infra), ("thread creation failed", LaneFailureClass::Infra),
]; ];
@@ -6000,11 +6000,17 @@ mod tests {
(LaneEventName::MergeReady, "lane.merge.ready"), (LaneEventName::MergeReady, "lane.merge.ready"),
(LaneEventName::Finished, "lane.finished"), (LaneEventName::Finished, "lane.finished"),
(LaneEventName::Failed, "lane.failed"), (LaneEventName::Failed, "lane.failed"),
(LaneEventName::BranchStaleAgainstMain, "branch.stale_against_main"), (
LaneEventName::BranchStaleAgainstMain,
"branch.stale_against_main",
),
]; ];
for (event, expected) in cases { for (event, expected) in cases {
assert_eq!(serde_json::to_value(event).expect("serialize lane event"), json!(expected)); assert_eq!(
serde_json::to_value(event).expect("serialize lane event"),
json!(expected)
);
} }
} }