Skip to content

fix: git branch read-only classification and the cache-invalidation diagnostic - #381

Merged
github-actions[bot] merged 2 commits into
mainfrom
fix/readonly-branch-flags-and-cache-diagnostic
Aug 17, 2026
Merged

github-actions[bot] merged 2 commits into
mainfrom
fix/readonly-branch-flags-and-cache-diagnostic

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

Two bugs raised as findings by earlier work on #379 and #380, investigated and fixed. Both were flagged as "not fixed, raising instead"; neither changes model-visible behavior.

1. git branch mutations classified as read-only

branch_is_readonly screened only the delete/rename/force family, so any other dash-leading option counted as a read. These all mutate repository state and all passed:

  • --set-upstream-to=origin/main
  • --unset-upstream
  • --edit-description
  • --set-upstream
  • -uorigin/main (attached short form — one word, so the bare-operand check cannot see it)

This was never exploitable, but the originally reported reason was wrong. The finding said the dedicated git tool "re-validates every option against its own allowlist". It does re-validate — but that allowlist explicitly permits -u and --set-upstream-to (builtins.rs:757-758), so it is not what stops these. What actually holds the line is that GitTool is SideEffectClass::Write, so authorize() HITL-gates the rewritten call by class. The backstop is real; the mechanism reported for it was not.

Labelling a mutation as a read one layer above a security boundary is still a bug, and a denylist is the wrong shape for the job. Inverted to an allowlist of known read-only options, so an option this module has never heard of is no longer assumed to be a read. Only the = spelling of valued options needs matching — the space spelling puts the value in a bare operand, which was already rejected.

Four tests replace the one that pinned the old behavior: the mutating flags above, unknown/future flags, the genuine reads still rewriting (including --sort=/--format=/--contains=), and a regression guard that the delete/rename/force family stays rejected.

2. CACHE PREFIX INVALIDATED never named a real mutation site

record_prompt_snapshot parsed the previous prompt with serde_json::from_slice to diff it against the current one. But last_prompt_wire holds the append-only encoding — tools\nsystem\nmsg0\nmsg1\n… — which is one JSON document per part, not a single document. That parse always failed once there was more than one part, fell back to json!({}), and left first_json_pointer comparing an empty object against the whole prompt. So first_difference only ever reported whichever key sorted first in the current wire.

There is a second layer to it: even had the parse succeeded, previous is the concatenated encoding while the current side is the wire object — different shapes, not comparable.

The obvious fix — retain the previous wire object to diff against — would undo the deep-copy removal from #380. Instead: common_prefix_len already reports where the two encodings diverge in bytes, so part_pointer_at counts the completed parts before that offset and names the part it lands in. No allocation, nothing retained, and it names the specific message rather than a top-level key. An offset sitting on a boundary names the part it opens, which is what a divergence point means.

This also removes the strip_cache_control deep copy that fed the old diff, so the DEBUG path no longer copies the prompt at all.

Six tests, including one that pins the root cause directly (the_snapshot_encoding_is_not_a_single_json_document), one that drives a real mutation end-to-end through common_prefix_len and asserts /messages/1, Codex-shaped instructions/input wires, boundary semantics, and totality on out-of-range offsets since this runs inside a diagnostic.

Verification

cargo fmt --all -- --check, cargo clippy --workspace --all-targets --locked -- -D warnings, and cargo test --workspace --locked --no-fail-fast all green on 19efceb. forge-governance 84 → 87, forge-model 66 → 71.

branch_is_readonly screened only the delete/rename/force family, so any
other dash-leading option classified as a read. --set-upstream-to=,
--unset-upstream, --edit-description and the attached -u<upstream> form
all mutate repository state and all passed.

This was never exploitable: the rewrite targets the dedicated git tool,
which is SideEffectClass::Write, so authorize still HITL-gates it. Note
the git tool's own argument policy explicitly permits -u and
--set-upstream-to, so it is not what stops them — the side-effect class
is. Labelling a mutation as a read one layer above a security boundary
is still wrong, and a denylist is the wrong shape for the job.

Inverted to an allowlist of known read-only options, so an option this
module has never heard of is no longer assumed to be a read. Only the =
spelling of valued options needs matching; the space spelling puts the
value in a bare operand, which is already rejected.
record_prompt_snapshot parsed the previous prompt with from_slice to
diff it against the current one. But last_prompt_wire holds the
append-only encoding -- tools\nsystem\nmsg0\nmsg1\n... -- which is one
JSON document per part, not a single document. That parse always failed
once there was more than one part, fell back to json!({}), and left
first_json_pointer comparing an empty object against the whole prompt.
The first_difference field in CACHE PREFIX INVALIDATED has therefore
never named a real mutation site.

Fixed without retaining the previous prompt: common_prefix_len already
reports where the two encodings diverge, so part_pointer_at counts the
completed parts before that offset and names the part it lands in. An
offset on a boundary names the part it opens, which is what a divergence
point means.

This also drops the strip_cache_control deep copy that fed the old diff,
so the DEBUG path no longer copies the prompt at all.
@github-actions
github-actions Bot enabled auto-merge (squash) August 17, 2026 02:02
@github-actions
github-actions Bot merged commit 6aeb0fb into main Aug 17, 2026
7 checks passed
github-actions Bot pushed a commit that referenced this pull request Aug 17, 2026
…#383)

The comments added in #381 claimed the git branch misclassification was
never exploitable because GitTool is SideEffectClass::Write and authorize
still gates it. That is wrong, and it is wrong in the direction that
matters.

Approval is gated on tool identity, not side-effect class. hitl_tools
holds the shell-equivalent tools (bash, background_run, exec_command,
mcp:*) and apply_mode clears hitl_classes in every shipped mode, so
authorize returns Allow for any tool outside that set before pattern
rules are consulted. git is outside it, and GitTool's own argument policy
permits -u/--set-upstream-to. Nothing downstream catches a
misclassification.

So readonly.rs is the boundary, not a layer above one, and #381 closed an
approval bypass rather than fixing a cosmetic label: a mutating git
command classified as a read became an unprompted mutation.

Corrects the module header, the branch_is_readonly doc and the test
comment, and pins the mechanism with two tests so it cannot be misread
again: bash prompts while the rewritten git call is allowed outright, and
both shipped modes leave hitl_classes empty.
mohitranka added a commit that referenced this pull request Aug 21, 2026
…iagnostic (#381)

* fix(governance): allowlist read-only git branch options

branch_is_readonly screened only the delete/rename/force family, so any
other dash-leading option classified as a read. --set-upstream-to=,
--unset-upstream, --edit-description and the attached -u<upstream> form
all mutate repository state and all passed.

This was never exploitable: the rewrite targets the dedicated git tool,
which is SideEffectClass::Write, so authorize still HITL-gates it. Note
the git tool's own argument policy explicitly permits -u and
--set-upstream-to, so it is not what stops them — the side-effect class
is. Labelling a mutation as a read one layer above a security boundary
is still wrong, and a denylist is the wrong shape for the job.

Inverted to an allowlist of known read-only options, so an option this
module has never heard of is no longer assumed to be a read. Only the =
spelling of valued options needs matching; the space spelling puts the
value in a bare operand, which is already rejected.

* fix(core): make the cache-invalidation diagnostic name a real site

record_prompt_snapshot parsed the previous prompt with from_slice to
diff it against the current one. But last_prompt_wire holds the
append-only encoding -- tools\nsystem\nmsg0\nmsg1\n... -- which is one
JSON document per part, not a single document. That parse always failed
once there was more than one part, fell back to json!({}), and left
first_json_pointer comparing an empty object against the whole prompt.
The first_difference field in CACHE PREFIX INVALIDATED has therefore
never named a real mutation site.

Fixed without retaining the previous prompt: common_prefix_len already
reports where the two encodings diverge, so part_pointer_at counts the
completed parts before that offset and names the part it lands in. An
offset on a boundary names the part it opens, which is what a divergence
point means.

This also drops the strip_cache_control deep copy that fed the old diff,
so the DEBUG path no longer copies the prompt at all.
mohitranka added a commit that referenced this pull request Aug 21, 2026
…#383)

The comments added in #381 claimed the git branch misclassification was
never exploitable because GitTool is SideEffectClass::Write and authorize
still gates it. That is wrong, and it is wrong in the direction that
matters.

Approval is gated on tool identity, not side-effect class. hitl_tools
holds the shell-equivalent tools (bash, background_run, exec_command,
mcp:*) and apply_mode clears hitl_classes in every shipped mode, so
authorize returns Allow for any tool outside that set before pattern
rules are consulted. git is outside it, and GitTool's own argument policy
permits -u/--set-upstream-to. Nothing downstream catches a
misclassification.

So readonly.rs is the boundary, not a layer above one, and #381 closed an
approval bypass rather than fixing a cosmetic label: a mutating git
command classified as a read became an unprompted mutation.

Corrects the module header, the branch_is_readonly doc and the test
comment, and pins the mechanism with two tests so it cannot be misread
again: bash prompts while the rewritten git call is allowed outright, and
both shipped modes leave hitl_classes empty.
mohitranka added a commit that referenced this pull request Sep 11, 2026
…iagnostic (#381)

* fix(governance): allowlist read-only git branch options

branch_is_readonly screened only the delete/rename/force family, so any
other dash-leading option classified as a read. --set-upstream-to=,
--unset-upstream, --edit-description and the attached -u<upstream> form
all mutate repository state and all passed.

This was never exploitable: the rewrite targets the dedicated git tool,
which is SideEffectClass::Write, so authorize still HITL-gates it. Note
the git tool's own argument policy explicitly permits -u and
--set-upstream-to, so it is not what stops them — the side-effect class
is. Labelling a mutation as a read one layer above a security boundary
is still wrong, and a denylist is the wrong shape for the job.

Inverted to an allowlist of known read-only options, so an option this
module has never heard of is no longer assumed to be a read. Only the =
spelling of valued options needs matching; the space spelling puts the
value in a bare operand, which is already rejected.

* fix(core): make the cache-invalidation diagnostic name a real site

record_prompt_snapshot parsed the previous prompt with from_slice to
diff it against the current one. But last_prompt_wire holds the
append-only encoding -- tools\nsystem\nmsg0\nmsg1\n... -- which is one
JSON document per part, not a single document. That parse always failed
once there was more than one part, fell back to json!({}), and left
first_json_pointer comparing an empty object against the whole prompt.
The first_difference field in CACHE PREFIX INVALIDATED has therefore
never named a real mutation site.

Fixed without retaining the previous prompt: common_prefix_len already
reports where the two encodings diverge, so part_pointer_at counts the
completed parts before that offset and names the part it lands in. An
offset on a boundary names the part it opens, which is what a divergence
point means.

This also drops the strip_cache_control deep copy that fed the old diff,
so the DEBUG path no longer copies the prompt at all.
mohitranka added a commit that referenced this pull request Sep 11, 2026
…#383)

The comments added in #381 claimed the git branch misclassification was
never exploitable because GitTool is SideEffectClass::Write and authorize
still gates it. That is wrong, and it is wrong in the direction that
matters.

Approval is gated on tool identity, not side-effect class. hitl_tools
holds the shell-equivalent tools (bash, background_run, exec_command,
mcp:*) and apply_mode clears hitl_classes in every shipped mode, so
authorize returns Allow for any tool outside that set before pattern
rules are consulted. git is outside it, and GitTool's own argument policy
permits -u/--set-upstream-to. Nothing downstream catches a
misclassification.

So readonly.rs is the boundary, not a layer above one, and #381 closed an
approval bypass rather than fixing a cosmetic label: a mutating git
command classified as a read became an unprompted mutation.

Corrects the module header, the branch_is_readonly doc and the test
comment, and pins the mechanism with two tests so it cannot be misread
again: bash prompts while the rewritten git call is allowed outright, and
both shipped modes leave hitl_classes empty.
mohitranka added a commit that referenced this pull request Sep 11, 2026
…iagnostic (#381)

* fix(governance): allowlist read-only git branch options

branch_is_readonly screened only the delete/rename/force family, so any
other dash-leading option classified as a read. --set-upstream-to=,
--unset-upstream, --edit-description and the attached -u<upstream> form
all mutate repository state and all passed.

This was never exploitable: the rewrite targets the dedicated git tool,
which is SideEffectClass::Write, so authorize still HITL-gates it. Note
the git tool's own argument policy explicitly permits -u and
--set-upstream-to, so it is not what stops them — the side-effect class
is. Labelling a mutation as a read one layer above a security boundary
is still wrong, and a denylist is the wrong shape for the job.

Inverted to an allowlist of known read-only options, so an option this
module has never heard of is no longer assumed to be a read. Only the =
spelling of valued options needs matching; the space spelling puts the
value in a bare operand, which is already rejected.

* fix(core): make the cache-invalidation diagnostic name a real site

record_prompt_snapshot parsed the previous prompt with from_slice to
diff it against the current one. But last_prompt_wire holds the
append-only encoding -- tools\nsystem\nmsg0\nmsg1\n... -- which is one
JSON document per part, not a single document. That parse always failed
once there was more than one part, fell back to json!({}), and left
first_json_pointer comparing an empty object against the whole prompt.
The first_difference field in CACHE PREFIX INVALIDATED has therefore
never named a real mutation site.

Fixed without retaining the previous prompt: common_prefix_len already
reports where the two encodings diverge, so part_pointer_at counts the
completed parts before that offset and names the part it lands in. An
offset on a boundary names the part it opens, which is what a divergence
point means.

This also drops the strip_cache_control deep copy that fed the old diff,
so the DEBUG path no longer copies the prompt at all.
mohitranka added a commit that referenced this pull request Sep 11, 2026
…#383)

The comments added in #381 claimed the git branch misclassification was
never exploitable because GitTool is SideEffectClass::Write and authorize
still gates it. That is wrong, and it is wrong in the direction that
matters.

Approval is gated on tool identity, not side-effect class. hitl_tools
holds the shell-equivalent tools (bash, background_run, exec_command,
mcp:*) and apply_mode clears hitl_classes in every shipped mode, so
authorize returns Allow for any tool outside that set before pattern
rules are consulted. git is outside it, and GitTool's own argument policy
permits -u/--set-upstream-to. Nothing downstream catches a
misclassification.

So readonly.rs is the boundary, not a layer above one, and #381 closed an
approval bypass rather than fixing a cosmetic label: a mutating git
command classified as a read became an unprompted mutation.

Corrects the module header, the branch_is_readonly doc and the test
comment, and pins the mechanism with two tests so it cannot be misread
again: bash prompts while the rewritten git call is allowed outright, and
both shipped modes leave hitl_classes empty.
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.

1 participant