Skip to content

fix(brain): normalise model-authored plan text at parse - #751

Merged
vladimirrott merged 1 commit into
lacs-project:mainfrom
yuee3:fix/json-summary-normalise
Nov 3, 2026
Merged

vladimirrott merged 1 commit into
lacs-project:mainfrom
yuee3:fix/json-summary-normalise

Conversation

@yuee3

@yuee3 yuee3 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #474.

What

parse_proposed_plan stored summary, explanation and every step summary exactly as the model wrote them, and the CLI's --json output emits plan.summary() and s.summary() as parsed (apps/sysknife-cli/src/runner.rs:2305 and :2328 — the maintainer's 21 Oct note names the same two call sites at their then-line numbers 2262/2270). 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.

This PR runs all three strings through the same normalisation parse_refusal already applies to its reason and suggestion — at the parse site, via a shared helper planning_tools::normalise_operator_text that both propose_plan.rs and refuse.rs now call.

Why parse-side

Plan is only constructed by parse_proposed_plan (the invariant is documented on Plan::new), so normalising there covers the --json output, and makes the terminal renderer's operator_safe scrub (#180) and the MCP boundary's sanitise_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.rs carried the invariant as a comment; propose_plan.rs never picked it up — two copies of "normalise model-authored text" is how this drifted. Both sites now call one named normalise_operator_text in planning_tools/mod.rs: a greppable name stating when a call site needs it, backed by crate::sanitize::normalise_free_text.

Other fields on Plan/PlanStep (scope's "any other field" question)

  • intent — the operator's own request, not model-authored.
  • step 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.rs PendingPrefChange::fact) and is normalised when written (write_prefs). I did notice the CLI's --json plan output prints c.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 — a summary carrying \x1b[8m, a bidi override \u{202e} and a zero-width joiner, with the same carriers in the explanation and step summary; asserts the parsed Plan carries 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 summary carrying 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::unix in sysknife-brain, CI on ubuntu-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:
test result: ok. 291 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 6.01s
  • UPDATE_TEST_BASELINE=1 scripts/test_baseline.sh (full cargo nextest run --workspace --locked):
     Summary [   8.987s] 2103 tests run: 2103 passed, 8 skipped

The published count in tests/evidence/workspace-tests.json moved 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 saw rate_limit::tests::calls_up_to_limit_all_succeed fail 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_limit mutates SYSKNIFE_MAX_RPM under ENV_LOCK, but other tests construct RateLimiter without taking that lock, so a constructor can read MAX_RPM=1 mid-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 dco check.

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]>
@yuee3
yuee3 requested a review from vladimirrott as a code owner November 3, 2026 16:09

@vladimirrott vladimirrott left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vladimirrott
vladimirrott merged commit b474091 into lacs-project:main Nov 3, 2026
15 checks passed
vladimirrott added a commit that referenced this pull request Nov 9, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The plan summary an operator approves is never sanitised, though the refusal beside it is

2 participants