feat(agent): wire EffectLedger through mutating run_tool
Prepare/start/commit write and execute tools with idempotent dedupe, and refuse blind retry while ledger state is unknown or interrupted mid-flight (MA-T03). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
6bd90bb2d0
commit
11bb7ef277
5 files changed
+342
-17
No files matched your search
@@ -530,6 +530,74 @@ mod tests {
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ma_t03_unknown_then_reconcile_no_second_commit_path() {
|
||||
// Write "succeeds" locally only after reconcile; retry while unknown must not
|
||||
// look write_auto_resume_safe (MA-T03 watermark gate).
|
||||
let dir = scratch("mat03");
|
||||
let prev = std::env::current_dir().unwrap();
|
||||
std::env::set_current_dir(&dir).unwrap();
|
||||
effects_prepare(&json!({
|
||||
"operation_id": "op_kill",
|
||||
"idempotency_key": "run|s|write_probe|sha256-lite:1",
|
||||
"args_digest": "sha256-lite:1",
|
||||
"effect_class": "write",
|
||||
"ledger_dir": "led",
|
||||
"logical_run_id": "run_kill",
|
||||
}))
|
||||
.unwrap();
|
||||
effects_transition(&json!({
|
||||
"operation_id": "op_kill",
|
||||
"state": "started",
|
||||
"ledger_dir": "led",
|
||||
}))
|
||||
.unwrap();
|
||||
// Crash before commit:
|
||||
effects_transition(&json!({
|
||||
"operation_id": "op_kill",
|
||||
"state": "unknown",
|
||||
"ledger_dir": "led",
|
||||
}))
|
||||
.unwrap();
|
||||
let wm = effects_watermark(&json!({
|
||||
"ledger_dir": "led",
|
||||
"logical_run_id": "run_kill",
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(wm["write_auto_resume_safe"], false);
|
||||
|
||||
// Idempotent re-prepare returns same unknown record (no new op).
|
||||
let again = effects_prepare(&json!({
|
||||
"operation_id": "op_kill",
|
||||
"idempotency_key": "run|s|write_probe|sha256-lite:1",
|
||||
"args_digest": "sha256-lite:1",
|
||||
"effect_class": "write",
|
||||
"ledger_dir": "led",
|
||||
"logical_run_id": "run_kill",
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(again["deduped"], true);
|
||||
assert_eq!(again["record"]["state"], "unknown");
|
||||
|
||||
effects_reconcile(&json!({
|
||||
"operation_id": "op_kill",
|
||||
"outcome": "committed",
|
||||
"ledger_dir": "led",
|
||||
"result_ref": "effect:op_kill",
|
||||
}))
|
||||
.unwrap();
|
||||
let wm2 = effects_watermark(&json!({
|
||||
"ledger_dir": "led",
|
||||
"logical_run_id": "run_kill",
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(wm2["write_auto_resume_safe"], true);
|
||||
assert_eq!(wm2["pending_writes"], 0);
|
||||
|
||||
std::env::set_current_dir(prev).unwrap();
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn illegal_skip_started() {
|
||||
let dir = scratch("bad");
|
||||
|
||||
@@ -842,6 +842,16 @@ kb_ffi!(agent_tools_search, tools::tools_search, "agent_tools_search");
|
||||
kb_ffi!(agent_tools_describe, tools::tools_describe, "agent_tools_describe");
|
||||
kb_ffi!(agent_tools_prepare_call, tools::tools_prepare_call, "agent_tools_prepare_call");
|
||||
kb_ffi!(agent_tools_wrap_result, tools::tools_wrap_result, "agent_tools_wrap_result");
|
||||
kb_ffi!(
|
||||
agent_tools_args_digest,
|
||||
tools::tools_args_digest,
|
||||
"agent_tools_args_digest"
|
||||
);
|
||||
kb_ffi!(
|
||||
agent_tools_needs_ledger,
|
||||
tools::tools_needs_ledger,
|
||||
"agent_tools_needs_ledger"
|
||||
);
|
||||
kb_ffi!(
|
||||
agent_tools_register_eng_defaults,
|
||||
tools::tools_register_eng_defaults,
|
||||
@@ -961,6 +971,8 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
("agent_tools_describe", "id,tools", agent_tools_describe),
|
||||
("agent_tools_prepare_call", "tool_id,arguments,tools", agent_tools_prepare_call),
|
||||
("agent_tools_wrap_result", "ok,status,summary", agent_tools_wrap_result),
|
||||
("agent_tools_args_digest", "arguments", agent_tools_args_digest),
|
||||
("agent_tools_needs_ledger", "effect", agent_tools_needs_ledger),
|
||||
(
|
||||
"agent_tools_register_eng_defaults",
|
||||
"",
|
||||
|
||||
@@ -369,10 +369,10 @@ pub fn tools_prepare_call(args: &Value) -> Result<Value, String> {
|
||||
));
|
||||
}
|
||||
|
||||
let arguments = args
|
||||
.get("arguments")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| json!({}));
|
||||
let arguments = match args.get("arguments") {
|
||||
None | Some(Value::Null) => json!({}),
|
||||
Some(v) => v.clone(),
|
||||
};
|
||||
let schema = spec
|
||||
.get("input_schema")
|
||||
.cloned()
|
||||
@@ -396,6 +396,17 @@ pub fn tools_prepare_call(args: &Value) -> Result<Value, String> {
|
||||
.get("version")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("1");
|
||||
let effect = spec
|
||||
.get("effect")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("read");
|
||||
let digest = args_digest(&arguments);
|
||||
let logical_run = opt_text(args, "logical_run_id").unwrap_or("-");
|
||||
let step = opt_text(args, "step_id").unwrap_or("-");
|
||||
let idempotency_key = opt_text(args, "idempotency_key")
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| format!("{logical_run}|{step}|{tool_id}|{digest}"));
|
||||
let needs_ledger = matches!(effect, "write" | "execute" | "external_write");
|
||||
|
||||
let call = json!({
|
||||
"schema": "ToolCallV1",
|
||||
@@ -414,14 +425,56 @@ pub fn tools_prepare_call(args: &Value) -> Result<Value, String> {
|
||||
"call": call,
|
||||
"spec": {
|
||||
"id": tool_id,
|
||||
"effect": spec.get("effect").cloned().unwrap_or(json!("read")),
|
||||
"effect": effect,
|
||||
"timeout_ms": spec.get("timeout_ms").cloned().unwrap_or(json!(10000)),
|
||||
"max_result_bytes": spec.get("max_result_bytes").cloned().unwrap_or(json!(65536)),
|
||||
},
|
||||
"args_digest": digest,
|
||||
"idempotency_key": idempotency_key,
|
||||
"needs_ledger": needs_ledger,
|
||||
"summary": format!("prepared authorized call to `{tool_id}`"),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Stable digest of tool arguments for effect ledger idempotency (M-A5a).
|
||||
pub fn tools_args_digest(args: &Value) -> Result<Value, String> {
|
||||
let arguments = args
|
||||
.get("arguments")
|
||||
.cloned()
|
||||
.or_else(|| args.get("value").cloned())
|
||||
.unwrap_or_else(|| json!({}));
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"args_digest": args_digest(&arguments),
|
||||
}))
|
||||
}
|
||||
|
||||
fn args_digest(arguments: &Value) -> String {
|
||||
let text = serde_json::to_string(arguments).unwrap_or_else(|_| "{}".into());
|
||||
format!("sha256-lite:{:x}", fnv1a64(&text))
|
||||
}
|
||||
|
||||
fn fnv1a64(s: &str) -> u64 {
|
||||
let mut h: u64 = 0xcbf29ce484222325;
|
||||
for b in s.as_bytes() {
|
||||
h ^= u64::from(*b);
|
||||
h = h.wrapping_mul(0x100000001b3);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
/// True when effect_class requires EffectLedger before/after execution.
|
||||
pub fn tools_needs_ledger(args: &Value) -> Result<Value, String> {
|
||||
let effect = opt_text(args, "effect")
|
||||
.or_else(|| opt_text(args, "effect_class"))
|
||||
.unwrap_or("read");
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"needs_ledger": matches!(effect, "write" | "execute" | "external_write"),
|
||||
"effect": effect,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Wrap a successful / failed tool execution into ToolResultV1.
|
||||
pub fn tools_wrap_result(args: &Value) -> Result<Value, String> {
|
||||
let ok = args.get("ok").and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
@@ -689,6 +742,42 @@ mod tests {
|
||||
assert_eq!(good["ok"], true);
|
||||
assert_eq!(good["call"]["schema"], "ToolCallV1");
|
||||
assert_eq!(good["call"]["tool_id"], "tool_find");
|
||||
assert_eq!(good["needs_ledger"], false);
|
||||
assert!(good["args_digest"].as_str().unwrap().starts_with("sha256-lite:"));
|
||||
|
||||
let allow2 = json!(["tool_bind"]);
|
||||
let bind = tools_prepare_call(&json!({
|
||||
"tool_id": "tool_bind",
|
||||
"tools": allow2,
|
||||
"arguments": {"query": "x"},
|
||||
"call_id": "c2",
|
||||
"logical_run_id": "run1",
|
||||
"step_id": "s1"
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(bind["ok"], true);
|
||||
assert_eq!(bind["needs_ledger"], true);
|
||||
assert_eq!(bind["spec"]["effect"], "execute");
|
||||
assert!(bind["idempotency_key"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.starts_with("run1|s1|tool_bind|"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn needs_ledger_only_for_mutating_effects() {
|
||||
assert_eq!(
|
||||
tools_needs_ledger(&json!({"effect": "read"})).unwrap()["needs_ledger"],
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
tools_needs_ledger(&json!({"effect": "write"})).unwrap()["needs_ledger"],
|
||||
true
|
||||
);
|
||||
assert_eq!(
|
||||
tools_needs_ledger(&json!({"effect": "execute"})).unwrap()["needs_ledger"],
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user