Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions crates/mcp-server/src/hook_handlers/post_tool_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ fn tool_call_succeeded(tool_response: &Value) -> bool {
has_response_evidence && !is_error && tool_response.get("error").is_none()
}

fn completes_session_init(normalized_tool_name: &str, tool_response: &Value) -> bool {
matches!(normalized_tool_name, "init" | "context") && tool_call_succeeded(tool_response)
}

fn contextstream_post_tool_nudge(
normalized_tool_name: &str,
action: &str,
Expand Down Expand Up @@ -791,14 +795,6 @@ fn parse_project_binding_from_config(

/// Handle the PostToolUse hook.
pub async fn handle() -> Result<()> {
if std::env::var("CONTEXTSTREAM_POSTWRITE_ENABLED")
.map(|v| v == "false")
.unwrap_or(false)
{
write_stdout_json(&HookOutput::empty())?;
return Ok(());
}

let input = read_stdin_json()?;
let tool_name = extract_tool_name(&input);
let tool_input = extract_tool_input(&input);
Expand All @@ -807,6 +803,31 @@ pub async fn handle() -> Result<()> {
let normalized_tool_name = normalize_contextstream_tool_name(&tool_name);
let cwd = extract_cwd(&input);

if is_contextstream_server_call(&mcp_server_name, &tool_name) {
// Record successful initialization before optional indexing/routing
// work, whose early returns must not lose this session's receipt.
if completes_session_init(&normalized_tool_name, &tool_response) {
super::prompt_state::clear_session_init_required(&cwd, &input);
}
if tool_call_succeeded(&tool_response)
&& (normalized_tool_name == "context"
|| (normalized_tool_name == "session"
&& contextstream_action(&tool_input) == "ground"))
{
super::prompt_state::clear_context_required(&cwd);
}
}

// Disabling file indexing must not disable initialization receipts while
// the corresponding PreToolUse gate remains enabled.
if std::env::var("CONTEXTSTREAM_POSTWRITE_ENABLED")
.map(|v| v == "false")
.unwrap_or(false)
{
write_stdout_json(&HookOutput::empty())?;
return Ok(());
}

if is_contextstream_server_call(&mcp_server_name, &tool_name) {
let action = contextstream_action(&tool_input);
// Observing a ContextStream read must never mutate local routing.
Expand Down Expand Up @@ -1275,6 +1296,27 @@ mod tests {
);
}

#[test]
fn session_init_requires_a_successful_init_or_context_result() {
let success =
serde_json::json!({"content": [{"type": "text", "text": "ready"}], "isError": false});
for tool in ["init", "context"] {
assert!(completes_session_init(tool, &success));
assert!(!completes_session_init(tool, &serde_json::json!({})));
assert!(!completes_session_init(
tool,
&serde_json::json!({"isError": true, "content": ["failed"]})
));
assert!(!completes_session_init(
tool,
&serde_json::json!({"error": "failed"})
));
}
for tool in ["search", "project", "session", "entity"] {
assert!(!completes_session_init(tool, &success));
}
}

#[test]
fn successful_handoff_create_reinforces_capsule_pairing_without_local_files() {
let message = contextstream_post_tool_nudge(
Expand Down
7 changes: 4 additions & 3 deletions crates/mcp-server/src/hook_handlers/pre_tool_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1879,9 +1879,10 @@ pub async fn handle() -> Result<()> {
// Prompt requirement: enforce context(...) before other MCP calls.
if supports_hard_first_call_enforcement(&editor) {
prompt_state::cleanup_stale(180);
if prompt_state::is_init_required(&cwd) {
if prompt_state::is_session_init_required(&cwd, &input) {
if clears_init_gate(is_contextstream_call, &normalized_contextstream_tool) {
prompt_state::clear_init_required(&cwd);
// Allow the initializing call, but record completion only in
// PostToolUse after its result confirms success.
emit_compliance(ComplianceEvent {
rule_key: compliance::RULE_INIT_REQUIRED,
rule_class: RuleClass::Procedural,
Expand All @@ -1890,7 +1891,7 @@ pub async fn handle() -> Result<()> {
severity: 1,
metadata: Some(serde_json::json!({
"tool": tool,
"reason": "init_gate_satisfied_by_init_or_context"
"reason": "initializing_call_allowed_pending_success"
})),
..Default::default()
});
Expand Down
Loading
Loading