From 615d5e9a52c3d5a5d472744cec3dd6932eb696ac Mon Sep 17 00:00:00 2001 From: NianJiuZst <180004567+NianJiuZst@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:30:46 +0800 Subject: [PATCH] fix(cli,mcp): forward max_tokens across step, plan, and MCP sync-after actions (#755) Why: `bsk open --max-tokens ` and `bsk sync --max-tokens ` store `maxTokens` on the extension's cooperation baseline, but `bsk step`, `bsk plan`, and their MCP counterparts (`bsk_interact` with `sync_after`/`until_visible` and `bsk_plan`) previously exposed no `max_tokens` option and hardcoded `max_tokens: None` in `step::post_sync`. Any post-action sync on a changed or unchanged budgeted tab then failed `sameBudget` / `textDelta` (`baseline_reason: "token_budget_changed"`), discarded the held baseline, and forced an unbounded full-page re-capture. What: - Add `--max-tokens` to `bsk step` and `bsk plan`, threading `Option` through `step::post_sync` into `CooperateParams.max_tokens` for `Sync`/`Enable`. - Expose `max_tokens` on MCP `bsk_interact` (rejected unless `sync_after` or `until_visible` is set) and `bsk_plan` so CLI and MCP stay at parity. - Keep `None`/`undefined` explicit (do not silently inherit prior `maxTokens` in the extension), preserving callers' ability to switch back to unbounded captures and keeping `bsk recipe run` full-VOM resolution intact. - Update `crates/bsk-cli/skill/references/step-and-cache.md`, `docs/plan.md`, bilingual changelogs (`CHANGELOG.md`, `CHANGELOG.zh-CN.md`), and full-chain CLI/MCP E2E tests (`wave3a_lifecycle_envelope_and_cache`, `wave2_ssot_mcp_e2e`). --- CHANGELOG.md | 1 + CHANGELOG.zh-CN.md | 1 + .../skill/references/step-and-cache.md | 7 +- crates/bsk-cli/src/cli/mcp/tools.rs | 16 +- crates/bsk-cli/src/cli/plan.rs | 7 + crates/bsk-cli/src/cli/step.rs | 18 +- .../tests/integration/wave2_ssot_mcp_e2e.rs | 129 ++++++ .../wave3a_lifecycle_envelope_and_cache.rs | 392 ++++++++++++++++++ docs/plan.md | 5 +- 9 files changed, 566 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95691196b..6c2f13520 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ determines which versions can connect. ### Fixed +- Added `--max-tokens` (`max_tokens` in MCP) to `bsk step`, `bsk plan`, `bsk_interact` (with `sync_after` or `until_visible`), and `bsk_plan`, forwarding it to the post-action `tool.cooperate` sync instead of hardcoding `None` so `--max-tokens` baselines stay reusable across `delta` and `unchanged` syncs without forcing an unbounded page re-capture (#755). - Ignored internal `data-bsk-*` attributes and `` (`[data-bsk-overlay]`) ShadowHost mutations in `cooperation-capture.ts` so active recording and sensitive-field markers no longer advance `FrameState.structure`, excluded extension ShadowRoot UI controls (`HelpRequestOverlay`, `RecordOverlay`, `BorrowConfirmationOverlay`) across `record-capture.ts`, `record-hover-surface.ts`, and `cooperation-capture.ts` so overlay interactions and hover surfaces are never mis-recorded as webpage steps, and re-evaluated tab control ownership after awaiting `ensureOverlayInjected` so mid-injection tab detachments never flash stale `control` state. - VOM token estimation now handles unpaired UTF-16 surrogates without combining them with the following symbol or throwing a `RangeError`; valid emoji pairs retain their existing estimates. diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index cb236ee69..e3d6cd302 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -29,6 +29,7 @@ CLI 和扩展可以分别发布,能否连接由协议兼容性决定。更新 ### 修复 +- 为 `bsk step`、`bsk plan`、MCP `bsk_interact`(搭配 `sync_after` 或 `until_visible`)及 `bsk_plan` 补齐 `--max-tokens`(MCP 为 `max_tokens`)参数并透传至后置 `tool.cooperate` 同步请求,解决此前硬编码 `None` 导致带预算会话在后续动作触发 `token_budget_changed` 并回退为整页无上限重采的问题(#755)。 - 在 `cooperation-capture.ts` 中忽略扩展内部私有 `data-bsk-*` 属性与 ``(`[data-bsk-overlay]`)ShadowHost 变更以消除录制/敏感字段标记对 `FrameState.structure` 的自污染,在 `record-capture.ts`、`record-hover-surface.ts` 与 `cooperation-capture.ts` 中统一过滤扩展自身 ShadowRoot 浮层控件(`HelpRequestOverlay`、`RecordOverlay`、`BorrowConfirmationOverlay`)防止浮层内点击/输入或悬停面被误录为网页操作,并在 `ensureOverlayInjected` 异步注入完成后二次校验标签页受控归属以杜绝注入途中断开控制导致的陈旧浮层闪现。 - VOM Token 估算现在能处理未配对的 UTF-16 代理字符,不会再误合并后续符号或抛出 `RangeError`;有效 emoji 代理对的估算保持不变。 diff --git a/crates/bsk-cli/skill/references/step-and-cache.md b/crates/bsk-cli/skill/references/step-and-cache.md index 0f92e41c5..767259379 100644 --- a/crates/bsk-cli/skill/references/step-and-cache.md +++ b/crates/bsk-cli/skill/references/step-and-cache.md @@ -133,10 +133,9 @@ nothing is merged: apply the line patch yourself or sync again for a baseline. Anything the extension cannot express as such a patch returns a fresh `baseline` with a `baseline_reason`, for example `structure_changed`, `refs_replaced`, `frame_replaced`, `focus_changed`, `human_activity`, `text_log_gap`, `text_outside_observation`, -`text_ambiguous`, `token_budget_changed` or `token_budget_shift`. Keep the same -`--max-tokens` across syncs you want patched. Pages with child frames, and text inside -controls, headings, labels, form fields, table rows or list items, always take the -baseline path. +`text_ambiguous`, `token_budget_changed` or `token_budget_shift`. Pass the same +`--max-tokens` on `step` and `plan` as well as `sync` across calls you want patched. +Pages with child frames, and text inside controls, headings, labels, form fields, table rows or list items, always take the baseline path. For readonly date pickers, click to open the picker instead of filling it. If VOM exposes only a containing control, inspect that observed ref with `get-html --ref` diff --git a/crates/bsk-cli/src/cli/mcp/tools.rs b/crates/bsk-cli/src/cli/mcp/tools.rs index 6e001e31f..13601b09b 100644 --- a/crates/bsk-cli/src/cli/mcp/tools.rs +++ b/crates/bsk-cli/src/cli/mcp/tools.rs @@ -2133,6 +2133,8 @@ struct InteractArgs { wait_timeout_ms: Option, /// Policy for the post-action sync. Default auto. policy: Option, + /// Approximate token cap for the post-action sync observation (requires sync_after or until_visible). + max_tokens: Option, } enum Prepared { @@ -2191,9 +2193,11 @@ fn interact_prepare(args: InteractArgs, conn_scope: Option<&str>) -> Result) -> Result, /// Readiness budget for until_visible, 1-25000 ms. Default 5000. wait_timeout_ms: Option, + /// Approximate token cap for the post-plan sync observation. Keep the same budget across syncs, steps, and plans to preserve cache reuse. + max_tokens: Option, } /// Validate a plan completely before connecting to the daemon. @@ -2776,6 +2783,7 @@ fn plan_prepare( prepared.tab_id = args.tab_id; prepared.until_visible = args.until_visible; prepared.wait_timeout_ms = wait_timeout_ms; + prepared.max_tokens = args.max_tokens; Ok((prepared, ctx)) } @@ -4261,6 +4269,10 @@ mod tests { error(json!({"action":"click","session":"s","params":{"ref":"e1"},"policy":"target"})) .contains("sync_after") ); + assert!( + error(json!({"action":"click","session":"s","params":{"ref":"e1"},"max_tokens":150})) + .contains("sync_after") + ); let too_many = vec![json!({"target":"@e1","field":"exists","equals":true}); 17]; assert!( error(json!({"action":"click","session":"s","params":{"ref":"e1"},"expect":too_many})) diff --git a/crates/bsk-cli/src/cli/plan.rs b/crates/bsk-cli/src/cli/plan.rs index bcc274b27..42a12e109 100644 --- a/crates/bsk-cli/src/cli/plan.rs +++ b/crates/bsk-cli/src/cli/plan.rs @@ -64,6 +64,9 @@ pub struct PlanArgs { /// Readiness budget in milliseconds, independent of the steps' timeouts. #[arg(long, default_value_t = 5000, value_parser = clap::value_parser!(u32).range(1..=25000))] pub wait_timeout_ms: u32, + /// Token budget for the post-plan VOM observation. Keep the same budget across syncs, steps, and plans to preserve cache reuse. + #[arg(long = "max-tokens")] + pub max_tokens: Option, /// Save the unfiltered combined result, atomically, without overwriting. #[arg(long)] pub save_full: Option, @@ -99,6 +102,7 @@ pub(crate) struct Plan { pub(crate) steps: Vec, pub(crate) until_visible: Option, pub(crate) wait_timeout_ms: u32, + pub(crate) max_tokens: Option, pub(crate) budget: Duration, } @@ -143,6 +147,7 @@ impl Plan { steps, until_visible: None, wait_timeout_ms: DEFAULT_WAIT_MS as u32, + max_tokens: None, budget: BUDGET, }) } @@ -413,6 +418,7 @@ pub(crate) fn run_with_meta( final_session, cooperate::Policy::Auto, tab, + plan.max_tokens, &mut reply, &mut exit, &mut plain_call, @@ -444,6 +450,7 @@ fn prepare(args: &PlanArgs, flags: &GlobalFlags) -> anyhow::Result { plan.tab_id = args.tab_id; plan.until_visible = args.until_visible.clone(); plan.wait_timeout_ms = args.wait_timeout_ms; + plan.max_tokens = args.max_tokens; Ok(plan) } diff --git a/crates/bsk-cli/src/cli/step.rs b/crates/bsk-cli/src/cli/step.rs index cf0aaf492..08ed6f315 100644 --- a/crates/bsk-cli/src/cli/step.rs +++ b/crates/bsk-cli/src/cli/step.rs @@ -54,6 +54,9 @@ pub struct StepArgs { /// Readiness budget in milliseconds, independent of the action timeout. #[arg(long, default_value_t = 5000, value_parser = clap::value_parser!(u32).range(1..=25000))] pub wait_timeout_ms: u32, + /// Token budget for the post-action VOM observation. Keep the same budget across syncs, steps, and plans to preserve cache reuse. + #[arg(long = "max-tokens")] + pub max_tokens: Option, /// Save the unfiltered combined result, atomically, without overwriting. #[arg(long)] pub save_full: Option, @@ -256,6 +259,7 @@ pub(crate) fn post_sync( session: &str, policy: cooperate::Policy, tab: Option, + max_tokens: Option, result: &mut Value, exit: &mut u8, call: &mut impl FnMut(Method, Value, Duration) -> Result, @@ -270,7 +274,9 @@ pub(crate) fn post_sync( action: mode, tab_id: tab, timeout_ms: Some(1000), - max_tokens: None, + max_tokens: matches!(mode, CooperateAction::Sync | CooperateAction::Enable) + .then_some(max_tokens) + .flatten(), }) .expect("serializable sync"), TOOL_IPC_TIMEOUT, @@ -346,7 +352,15 @@ pub(crate) fn run( result["readiness"] = json!({"requested":false}); } let ready_ms = start.elapsed().as_millis() - action_ms; - post_sync(session, args.policy, tab, &mut result, &mut exit, &mut call); + post_sync( + session, + args.policy, + tab, + args.max_tokens, + &mut result, + &mut exit, + &mut call, + ); result["phase"] = if exit == 0 { "observed" } else { diff --git a/crates/bsk-cli/tests/integration/wave2_ssot_mcp_e2e.rs b/crates/bsk-cli/tests/integration/wave2_ssot_mcp_e2e.rs index b80fd8828..116548006 100644 --- a/crates/bsk-cli/tests/integration/wave2_ssot_mcp_e2e.rs +++ b/crates/bsk-cli/tests/integration/wave2_ssot_mcp_e2e.rs @@ -822,6 +822,19 @@ async fn wave2_full_chain_ssot_mcp_parity_and_counterexamples() { .iter() .find(|t| t["name"] == "bsk_interact") .unwrap(); + assert!( + interact_def["inputSchema"]["properties"]["max_tokens"]["description"] + .as_str() + .is_some_and(|s| s.contains("sync_after")), + "bsk_interact schema must document max_tokens" + ); + let plan_def = tools_arr.iter().find(|t| t["name"] == "bsk_plan").unwrap(); + assert!( + plan_def["inputSchema"]["properties"]["max_tokens"]["description"] + .as_str() + .is_some_and(|s| s.contains("token")), + "bsk_plan schema must document max_tokens" + ); assert!( interact_def["inputSchema"]["properties"]["expect"]["items"]["properties"]["target"] ["description"] @@ -1392,6 +1405,122 @@ async fn wave2_full_chain_ssot_mcp_parity_and_counterexamples() { .await; assert_eq!(snap_mcp_ok["tab_id"], 7); + // Issue #755: MCP bsk_interact (with sync_after/until_visible) and bsk_plan forward max_tokens, + // while bsk_interact rejects max_tokens without sync_after/until_visible before daemon contact. + let before_mcp_budget_err = ext_state.lock().unwrap().recorded.len(); + let interact_budget_err = mcp + .call_err( + "bsk_interact", + json!({ + "action": "click", + "scope": "agent-alpha", + "params": { "ref": "@e1" }, + "context": &next_ctx, + "max_tokens": 150 + }), + ) + .await; + assert_eq!(interact_budget_err["code"], "invalid_params"); + assert_eq!( + ext_state.lock().unwrap().recorded.len(), + before_mcp_budget_err, + "bsk_interact with max_tokens without sync_after must fail closed before WS dispatch" + ); + + let before_mcp_step_budget = ext_state.lock().unwrap().recorded.len(); + let interact_budget_ok = mcp + .call_ok( + "bsk_interact", + json!({ + "action": "click", + "scope": "agent-alpha", + "params": { "ref": "@e1" }, + "context": &next_ctx, + "sync_after": true, + "max_tokens": 150 + }), + ) + .await; + assert_eq!(interact_budget_ok["ok"], true); + let post_interact_ctx = interact_budget_ok["post"]["context_id"] + .as_str() + .expect("post context_id after budgeted bsk_interact") + .to_owned(); + { + let st = ext_state.lock().unwrap(); + let slice = &st.recorded[before_mcp_step_budget..]; + let click_req = slice + .iter() + .find(|r| r.method == Method::ToolClick) + .expect("tool.click sent"); + assert!( + click_req + .params + .as_ref() + .unwrap() + .get("max_tokens") + .is_none() + ); + let coop_req = slice + .iter() + .find(|r| r.method == Method::ToolCooperate) + .expect("tool.cooperate sent"); + assert_eq!(coop_req.params.as_ref().unwrap()["max_tokens"], 150); + } + + let before_mcp_until_budget = ext_state.lock().unwrap().recorded.len(); + let interact_until_ok = mcp + .call_ok( + "bsk_interact", + json!({ + "action": "click", + "scope": "agent-alpha", + "params": { "ref": "@e1" }, + "context": &post_interact_ctx, + "until_visible": "#saved", + "max_tokens": 150 + }), + ) + .await; + assert_eq!(interact_until_ok["ok"], true); + let post_until_ctx = interact_until_ok["post"]["context_id"] + .as_str() + .expect("post context_id after until_visible bsk_interact") + .to_owned(); + { + let st = ext_state.lock().unwrap(); + let slice = &st.recorded[before_mcp_until_budget..]; + let coop_req = slice + .iter() + .find(|r| r.method == Method::ToolCooperate) + .expect("tool.cooperate sent after until_visible bsk_interact"); + assert_eq!(coop_req.params.as_ref().unwrap()["max_tokens"], 150); + } + + let before_mcp_plan_budget = ext_state.lock().unwrap().recorded.len(); + let plan_budget_ok = mcp + .call_ok( + "bsk_plan", + json!({ + "scope": "agent-alpha", + "context": &post_until_ctx, + "steps": [{ "action": "click", "ref": "@e1" }], + "max_tokens": 150 + }), + ) + .await; + assert_eq!(plan_budget_ok["ok"], true); + assert_eq!(plan_budget_ok["completed"], 1); + { + let st = ext_state.lock().unwrap(); + let slice = &st.recorded[before_mcp_plan_budget..]; + let coop_req = slice + .iter() + .find(|r| r.method == Method::ToolCooperate) + .expect("tool.cooperate sent after bsk_plan"); + assert_eq!(coop_req.params.as_ref().unwrap()["max_tokens"], 150); + } + // Cleanly stop scoped sessions. let _ = mcp .call_ok("bsk_session", json!({ "action": "stop", "all": true })) diff --git a/crates/bsk-cli/tests/integration/wave3a_lifecycle_envelope_and_cache.rs b/crates/bsk-cli/tests/integration/wave3a_lifecycle_envelope_and_cache.rs index 6e6ee62a4..79bb9b376 100644 --- a/crates/bsk-cli/tests/integration/wave3a_lifecycle_envelope_and_cache.rs +++ b/crates/bsk-cli/tests/integration/wave3a_lifecycle_envelope_and_cache.rs @@ -3170,3 +3170,395 @@ async fn counterexample_f_bidirectional_ipc_envelope_rolling_upgrade_compat() { let _ = b4_srv.await; b4_daemon.shutdown().await; } + +/// Issue #755 full-chain regression: +/// `bsk step` and `bsk plan` forward `--max-tokens` to their post-action `tool.cooperate` +/// sync (keeping `baseline.maxTokens` intact for `delta` / `unchanged` reuse and updating +/// daemon `scopes` budget tracking), while omitting `--max-tokens` still requests unbounded syncs. +#[tokio::test] +async fn step_and_plan_forward_max_tokens_and_preserve_delta_budget() { + let step = Arc::new(Mutex::new(0u32)); + let step_ext = Arc::clone(&step); + + let ctx_1 = "11111111-1111-4111-8111-111111111755"; + let obs_1 = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaa751"; + let obs_2 = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaa752"; + let obs_3 = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaa753"; + let obs_4 = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaa754"; + let obs_5 = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaa755"; + + let env = IsolatedEnv::start(move |req| match &req.method { + Method::ToolNavigate => ResponseBody::Ok(json!({ + "tab_id": 7, + "url": "https://app.test/board", + "final_url": "https://app.test/board", + "reached": "load" + })), + Method::ToolScrollTo | Method::ToolClick => { + assert!( + req.params + .as_ref() + .and_then(|p| p.get("max_tokens")) + .is_none(), + "action RPC {:?} must not carry max_tokens: {:?}", + req.method, + req.params + ); + ResponseBody::Ok(json!({ "tab_id": 7, "used_ref": "e1" })) + } + Method::ToolCooperate => { + let mut seq = step_ext.lock().unwrap(); + *seq += 1; + let max_tokens = req + .params + .as_ref() + .and_then(|p| p.get("max_tokens")) + .and_then(Value::as_u64); + match *seq { + // 1: `bsk open --max-tokens 150` + 1 => { + assert_eq!(max_tokens, Some(150)); + ResponseBody::Ok(json!({ + "tab_id": 7, + "control_policy": "target", + "kind": "baseline", + "history_complete": true, + "context_id": ctx_1, + "observation_id": obs_1, + "changes": [], + "observation": { + "observation_id": obs_1, + "tab_id": 7, + "url": "https://app.test/board", + "title": "Board", + "text": "@vom 1\n@e1 heading \"Top\"\n@e2 button \"Count: 1\"", + "ref_count": 2, + "truncated": true, + "next_cursor": "cursor-150" + }, + "state": { "mode": "ready", "revision": 1 } + })) + } + // 2: `bsk step --max-tokens 150 --save-full step1.json` -> fresh 150-token baseline + 2 => { + assert_eq!(max_tokens, Some(150)); + ResponseBody::Ok(json!({ + "tab_id": 7, + "control_policy": "target", + "kind": "baseline", + "baseline_reason": "structure_changed", + "history_complete": true, + "context_id": ctx_1, + "observation_id": obs_1, + "changes": [], + "observation": { + "observation_id": obs_1, + "tab_id": 7, + "url": "https://app.test/board", + "title": "Board", + "text": "@vom 1\n@e1 heading \"Top\"\n@e2 button \"Count: 1\"", + "ref_count": 2, + "truncated": true, + "next_cursor": "cursor-150" + }, + "state": { "mode": "ready", "revision": 2 } + })) + } + // 3: `bsk step --max-tokens 150 --baseline step1.json --save-full step2.json` -> text delta + 3 => { + assert_eq!(max_tokens, Some(150)); + ResponseBody::Ok(json!({ + "tab_id": 7, + "control_policy": "target", + "kind": "delta", + "history_complete": true, + "previous_context_id": ctx_1, + "previous_observation_id": obs_1, + "context_id": ctx_1, + "observation_id": obs_2, + "observation_patch": { + "lines": [{ + "line": 3, + "previous": "@e2 button \"Count: 1\"", + "text": "@e2 button \"Count: 2\"" + }] + }, + "changes": [{ + "frame_id": 0, + "kind": "text", + "id": "t1", + "label": "Count", + "previous_value": "Count: 1", + "value": "Count: 2", + "lines": [3] + }], + "state": { "mode": "ready", "revision": 3 } + })) + } + // 4: follow-up `bsk sync --max-tokens 150` -> unchanged reuses step-patched cache + 4 => { + assert_eq!(max_tokens, Some(150)); + ResponseBody::Ok(json!({ + "tab_id": 7, + "control_policy": "target", + "kind": "unchanged", + "history_complete": true, + "previous_context_id": ctx_1, + "previous_observation_id": obs_2, + "context_id": ctx_1, + "observation_id": obs_2, + "changes": [], + "state": { "mode": "ready", "revision": 3 } + })) + } + // 5: `bsk plan --max-tokens 150` -> post-sync receives max_tokens: 150 + 5 => { + assert_eq!(max_tokens, Some(150)); + ResponseBody::Ok(json!({ + "tab_id": 7, + "control_policy": "target", + "kind": "baseline", + "baseline_reason": "structure_changed", + "history_complete": true, + "context_id": ctx_1, + "observation_id": obs_3, + "changes": [], + "observation": { + "observation_id": obs_3, + "tab_id": 7, + "url": "https://app.test/board", + "title": "Board", + "text": "@vom 1\n@e1 heading \"Top\"\n@e2 button \"Count: 3\"", + "ref_count": 2, + "truncated": true, + "next_cursor": "cursor-plan-150" + }, + "state": { "mode": "ready", "revision": 4 } + })) + } + // 6: `bsk step` without `--max-tokens` -> omits max_tokens (unbounded) + 6 => { + assert_eq!(max_tokens, None); + ResponseBody::Ok(json!({ + "tab_id": 7, + "control_policy": "target", + "kind": "baseline", + "baseline_reason": "token_budget_changed", + "history_complete": true, + "context_id": ctx_1, + "observation_id": obs_4, + "changes": [], + "observation": { + "observation_id": obs_4, + "tab_id": 7, + "url": "https://app.test/board", + "title": "Board", + "text": "@vom 1\n@e1 heading \"Top\"\n@e2 button \"Count: 3\"\n@e3 text \"Full\"", + "ref_count": 3, + "truncated": false + }, + "state": { "mode": "ready", "revision": 5 } + })) + } + // 7: `bsk plan` without `--max-tokens` -> omits max_tokens (unbounded) + 7 => { + assert_eq!(max_tokens, None); + ResponseBody::Ok(json!({ + "tab_id": 7, + "control_policy": "target", + "kind": "unchanged", + "history_complete": true, + "previous_context_id": ctx_1, + "previous_observation_id": obs_4, + "context_id": ctx_1, + "observation_id": obs_5, + "changes": [], + "state": { "mode": "ready", "revision": 5 } + })) + } + other => panic!("unexpected cooperate sequence {other}"), + } + } + other => panic!("unexpected method {other:?}"), + }) + .await; + + let tmp = tempfile::tempdir().unwrap(); + let step1_saved = tmp.path().join("step1.json"); + let step2_saved = tmp.path().join("step2.json"); + + let open = env + .run_bsk( + &[ + "open", + "https://app.test/board", + "--max-tokens", + "150", + "--json", + ], + &[], + ) + .await; + assert!( + open.status.success(), + "{}", + String::from_utf8_lossy(&open.stderr) + ); + let open_json: Value = serde_json::from_slice(&open.stdout).unwrap(); + assert_eq!(open_json["truncated"], true); + + let step1_out = env + .run_bsk( + &[ + "step", + "--action", + "click", + "--params", + r#"{"ref":"@e1"}"#, + "--max-tokens", + "150", + "--save-full", + step1_saved.to_str().unwrap(), + "--json", + ], + &[], + ) + .await; + assert!( + step1_out.status.success(), + "bsk step1 --max-tokens failed: stdout={} stderr={}", + String::from_utf8_lossy(&step1_out.stdout), + String::from_utf8_lossy(&step1_out.stderr) + ); + + let step2_out = env + .run_bsk( + &[ + "step", + "--action", + "scroll-to", + "--params", + r#"{"ref":"@e1"}"#, + "--max-tokens", + "150", + "--baseline", + step1_saved.to_str().unwrap(), + "--save-full", + step2_saved.to_str().unwrap(), + "--json", + ], + &[], + ) + .await; + assert!( + step2_out.status.success(), + "bsk step2 --max-tokens failed: stdout={} stderr={}", + String::from_utf8_lossy(&step2_out.stdout), + String::from_utf8_lossy(&step2_out.stderr) + ); + let step2_json: Value = serde_json::from_slice(&step2_out.stdout).unwrap(); + assert_eq!(step2_json["ok"], true); + assert_eq!(step2_json["post"]["kind"], "delta"); + assert_eq!( + step2_json["post"]["cache_reuse"]["reason"], + "exact_text_delta_predecessor" + ); + let merged: Value = serde_json::from_slice(&std::fs::read(&step2_saved).unwrap()).unwrap(); + assert_eq!(merged["post"]["observation_origin"]["kind"], "merged"); + assert!( + merged["post"]["observation"]["text"] + .as_str() + .unwrap() + .contains("@e2 button \"Count: 2\"") + ); + + // Daemon cache now holds the step-patched 150-token VOM under `last_vom_max_tokens = Some(150)`. + let sync_150 = env + .run_bsk(&["sync", "--max-tokens", "150", "--json"], &[]) + .await; + assert!(sync_150.status.success()); + let sync_150_json: Value = serde_json::from_slice(&sync_150.stdout).unwrap(); + assert_eq!(sync_150_json["unchanged"], true); + assert_eq!(sync_150_json["truncated"], true); + assert!( + sync_150_json["vom"] + .as_str() + .unwrap() + .contains("@e2 button \"Count: 2\""), + "expected step delta patch in cached VOM: {sync_150_json}" + ); + + let plan_out = env + .run_bsk( + &[ + "plan", + "--steps", + r#"[{"action":"scroll-to","ref":"@e1"},{"action":"click","ref":"@e2"}]"#, + "--max-tokens", + "150", + "--json", + ], + &[], + ) + .await; + assert!( + plan_out.status.success(), + "bsk plan --max-tokens failed: stdout={} stderr={}", + String::from_utf8_lossy(&plan_out.stdout), + String::from_utf8_lossy(&plan_out.stderr) + ); + let plan_json: Value = serde_json::from_slice(&plan_out.stdout).unwrap(); + assert_eq!(plan_json["ok"], true); + assert_eq!(plan_json["completed"], 2); + assert_eq!(plan_json["post"]["observation"]["truncated"], true); + + let step_unbounded = env + .run_bsk( + &[ + "step", + "--action", + "scroll-to", + "--params", + r#"{"ref":"@e1"}"#, + "--json", + ], + &[], + ) + .await; + assert!(step_unbounded.status.success()); + let step_unbounded_json: Value = serde_json::from_slice(&step_unbounded.stdout).unwrap(); + assert_eq!( + step_unbounded_json["post"]["observation"]["truncated"], + false + ); + + let plan_unbounded = env + .run_bsk( + &[ + "plan", + "--steps", + r#"[{"action":"click","ref":"@e2"}]"#, + "--json", + ], + &[], + ) + .await; + assert!(plan_unbounded.status.success()); + + let coop_reqs: Vec = env + .take_requests() + .into_iter() + .filter(|r| r.method == Method::ToolCooperate) + .map(|r| r.params.unwrap_or(Value::Null)) + .collect(); + assert_eq!(coop_reqs.len(), 7); + assert_eq!(coop_reqs[0]["max_tokens"], 150); + assert_eq!(coop_reqs[1]["max_tokens"], 150); + assert_eq!(coop_reqs[2]["max_tokens"], 150); + assert_eq!(coop_reqs[3]["max_tokens"], 150); + assert_eq!(coop_reqs[4]["max_tokens"], 150); + assert!(coop_reqs[5].get("max_tokens").is_none()); + assert!(coop_reqs[6].get("max_tokens").is_none()); + + env.shutdown().await; +} diff --git a/docs/plan.md b/docs/plan.md index acf4ab8d0..293449aae 100644 --- a/docs/plan.md +++ b/docs/plan.md @@ -37,8 +37,9 @@ A plan holds 1 to 20 steps. Use `context` and, for an untargeted first step, `observation` on the outer `bsk_plan` call. Steps use protocol names such as `ref` and `timeout_ms`; put `expect` on the -step it protects. `until_visible` and `wait_timeout_ms` work on `bsk_plan` as with -`bsk_interact`; `--save-full` and `--baseline` are CLI options on `bsk plan`. +step it protects. `until_visible`, `wait_timeout_ms`, and `max_tokens` (`--max-tokens` +on CLI) work on `bsk_plan` as with `bsk_interact`; `--save-full` and `--baseline` are +CLI options on `bsk plan`. ## Reply