Repository navigation
fix(brain): normalise model-authored plan text at parse - #751
Conversation
The plan summary an operator approves, the explanation beside it and each step summary are model-authored text, and the CLI's --json output emitted them exactly as parsed: serde_json escapes control bytes but passes bidi overrides, zero-width characters and ANSI sequences through, so the text an operator is asked to approve could render as something other than what the step executes. Wire all three through the same normalisation parse_refusal already applies, at the parse site: Plan is only constructed by parse_proposed_plan, so this covers the --json output and makes the terminal renderer's operator_safe (lacs-project#180) and the MCP boundary's sanitise_result_json second layers rather than the only ones. The two parse sites share one helper, planning_tools::normalise_operator_text, instead of two copies of a convention that drifted apart once already; a greppable name beats a comment on one side. Field inventory for the scope's remaining question: intent is the operator's own input; step params is structured, execution-bound data (the same exemption the MCP boundary applies); pref_changes[].fact carries a documented render-side invariant and is normalised when written - the --json output prints it raw at runner.rs:2320, which looks like the same class of gap on the preference side but is a different surface and invariant, so it is flagged in the PR rather than changed here. Tests: hostile plan text (ANSI conceal, bidi override, zero-width joiner) asserts the parsed Plan carries none of them and nothing stays hidden; a benign plan asserts ordinary text survives unchanged; the refusal side gets the control-character lock the issue asked to see on both sides. The published test count in tests/evidence/workspace-tests.json was re-rendered with UPDATE_TEST_BASELINE=1 scripts/test_baseline.sh. Signed-off-by: yuee3 <[email protected]>
vladimirrott
left a comment
There was a problem hiding this comment.
Reviewed at 2d0ca252. Approving, and merging through my gate.
Your PR body answered every question #474 left open, field by field: why intent and params stay as they are, and why pref_changes[].fact belongs to a different invariant. Putting both parse sites behind normalise_operator_text removes the drift that caused this, where refuse.rs carried the rule as a comment and propose_plan.rs never picked it up. The exact-string assertions are the part I liked most: a conceal sequence that hid words would fail them, so the test checks that nothing stays hidden as well as that the bytes are gone.
I confirmed parse_proposed_plan is the only production caller of Plan::new; the other call sites in planner.rs and runner.rs sit in test modules. Then I ran cargo test -p sysknife-brain --lib --offline --locked planning_tools in a container (--network=none) on your head and on two mutants:
pristine running 50 tests ... 50 passed; 0 failed
parse site back to .to_string() plan_and_step_text_is_normalised_at_parse ... FAILED
summary: ANSI escape survived: "List services \u{1b}[8m and reboot \u{1b}[0m the host"
normalise_operator_text returns raw text plan_and_step_text_is_normalised_at_parse ... FAILED
refusal_text_carries_no_control_characters ... FAILED
pristine again 50 passed; 0 failed
The merge gate repeated the first mutant on its own and recorded observed: clean pass, mutated fail. The evidence count is right: main reads 2100 and you added three.
You were right to flag the preference side. runner.rs:2320 emits c.fact() raw in --json, while the terminal paths at render.rs:187 and runner.rs:2410 neutralise it. I'll file that as its own issue and offer it to you there, since you've already mapped it. The rate_limit env race you described I have not reproduced, so I'm leaving it for now; if you can make it fail under cargo test again, a separate issue with the command would help.
Since you run Ubuntu: if you use Linux for real work, I'd like to hear how SysKnife holds up there. npx sysknife-setup gets you started, and the read-only side (sysknife doctor, sysknife history, sysknife audit verify) changes nothing on the host.
Bump the fifteen package versions, the internal pins and the JSON manifests with scripts/bump_version.sh, and roll the changelog. The release carries #751 (plan text normalised at parse), #763 (RemoveMount keeps the fstab entry when the unmount fails), the rusqlite 0.41 bump and two CI and harness fixes. Claude-Session: https://claude.ai/code/session_01E8wjqGc1Yv6hYnMugenmqS Signed-off-by: Vladimir Rotariu <[email protected]> Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Fixes #474.
What
parse_proposed_planstoredsummary,explanationand every stepsummaryexactly as the model wrote them, and the CLI's--jsonoutput emitsplan.summary()ands.summary()as parsed (apps/sysknife-cli/src/runner.rs:2305and:2328— the maintainer's 21 Oct note names the same two call sites at their then-line numbers 2262/2270).serde_jsonescapes control bytes but passes bidi overrides, zero-width characters and ANSI sequences through, so the text an operator is asked to approve could render as something other than what the step executes.This PR runs all three strings through the same normalisation
parse_refusalalready applies to its reason and suggestion — at the parse site, via a shared helperplanning_tools::normalise_operator_textthat bothpropose_plan.rsandrefuse.rsnow call.Why parse-side
Planis only constructed byparse_proposed_plan(the invariant is documented onPlan::new), so normalising there covers the--jsonoutput, and makes the terminal renderer'soperator_safescrub (#180) and the MCP boundary'ssanitise_result_json(99bed84) second layers rather than the only ones — exactly the direction the issue's scope update points at ("the parse-side normalisation in Scope is still the right fix").The shared helper (scope asks this be decided in the PR)
refuse.rscarried the invariant as a comment;propose_plan.rsnever picked it up — two copies of "normalise model-authored text" is how this drifted. Both sites now call one namednormalise_operator_textinplanning_tools/mod.rs: a greppable name stating when a call site needs it, backed bycrate::sanitize::normalise_free_text.Other fields on
Plan/PlanStep(scope's "any other field" question)intent— the operator's own request, not model-authored.params— structured, execution-bound data; normalising it would change what executes. Same exemption the MCP boundary applies.pref_changes[].fact— model-authored, but it carries a documented render-side invariant ("Untrusted: render it neutralised",prefs.rsPendingPrefChange::fact) and is normalised when written (write_prefs). I did notice the CLI's--jsonplan output printsc.fact()raw (runner.rs:2320); with a render-side invariant that looks like the same class of gap on the preference side, but it is a different surface and invariant, so I left it out of this change and flag it here for the maintainer to decide.Tests
plan_and_step_text_is_normalised_at_parse— asummarycarrying\x1b[8m, a bidi override\u{202e}and a zero-width joiner, with the same carriers in the explanation and step summary; asserts the parsedPlancarries none of them and that nothing stays hidden (exact normalised strings asserted, so a summary cannot render as one request while carrying another).ordinary_plan_text_survives_parse_unchanged— a benign plan asserts normalisation is identity on ordinary text.refusal_text_carries_no_control_characters— the control-character test the issue asked to see added on the refusal side too (it had none).Written failing first (the plan-side test went red on
summarycarrying ANSI before the fix, green after; the refusal-side test passed before and after — it locks existing behaviour).Verification
Full rust-side CI reproduction on a Linux environment (WSL Ubuntu 26.04, rustc/cargo 1.99.1, Tauri system deps installed) — this repo targets Linux (
std::os::unixinsysknife-brain, CI onubuntu-latest), so a Windows host cannot build the workspace:cargo fmt --all -- --check— clean.cargo clippy --workspace --all-features --all-targets --locked -- -D warnings— clean.RUSTDOCFLAGS=-D warnings cargo doc --no-deps --workspace --locked— clean.cargo test -p sysknife-brain --lib --locked:UPDATE_TEST_BASELINE=1 scripts/test_baseline.sh(fullcargo nextest run --workspace --locked):The published count in
tests/evidence/workspace-tests.jsonmoved by the three added tests (2100 → 2103), re-rendered by the script on the green run.One honest environment note: under a plain
cargo test(single process, all suites in parallel threads, run from root in WSL) I twice sawrate_limit::tests::calls_up_to_limit_all_succeedfail with "call 1 should be within limit". It passes in isolation, and the full non-root nextest run above is green. Root cause looks like a pre-existing env-var race in the test suite:env_var_overrides_constructor_limitmutatesSYSKNIFE_MAX_RPMunderENV_LOCK, but other tests constructRateLimiterwithout taking that lock, so a constructor can readMAX_RPM=1mid-window; my three added tests merely shifted the scheduling. nextest's per-test process isolation (what CI runs) does not hit it. Not touched in this PR — flagging it here.DCO
Signed-off-by as required by the
dcocheck.