Skip to content

feat(jmcp): commit check before commit, dry run, and commit confirmed - #34

Merged
nileshsimaria merged 2 commits into
Juniper:mainfrom
jgrizzuti:feat/commit-check-confirmed
Nov 2, 2026
Merged

nileshsimaria merged 2 commits into
Juniper:mainfrom
jgrizzuti:feat/commit-check-confirmed

Conversation

@jgrizzuti

Copy link
Copy Markdown
Contributor

Summary

load_and_commit_config currently locks, loads, diffs and runs a plain commit: there is no commit check, no dry run and no commit confirmed, so a bad change (e.g. one that cuts off management access) has no automatic rollback. The project article (docs/junos-mcp-server-article.html) describes commit(confirm=1), but the handler does not do this.

This PR adds those safety primitives:

  • Commit check before every commit in load_and_commit_config. If the check fails, nothing is committed, the candidate is rolled back, and the Junos error is returned (the result starts with Failed, so _is_error_content flags it as a tool error).
  • dry_run on load_and_commit_config: load, commit check, return the diff, roll back. Previously a standalone dry run was only possible through render_and_apply_j2_template.
  • confirm_timeout_mins on load_and_commit_config and render_and_apply_j2_template: commits with commit confirmed N (PyEZ commit(confirm=N)). Validated to Junos' range 1–65535 before any device is touched; booleans, strings and out-of-range values are rejected so a typo never silently becomes a non-reverting commit. Ignored when dry_run=true.
  • New confirm_commit tool: confirms a pending commit confirmed with a plain commit, cancelling the automatic rollback. It refuses when the candidate has uncommitted changes, so confirming can never commit someone else's edits.
  • load_and_commit_config now also declares its existing timeout argument in the schema.

Existing behaviour is unchanged when the new arguments are omitted, apart from the added commit check (a config that fails the check would have failed the commit anyway).

Cleanup on failure follows the existing pattern: confirm_commit closes the transport if unlock fails, so the pool evicts a possibly-locked session.

Testing

  • New tests/test_commit_confirmed.py (17 tests): timeout validation; commit check before a plain commit; failed check (exception and False) never commits; dry run rolls back; confirm passed to PyEZ for both tools; invalid timeouts rejected without touching the device; confirm_commit with clean and dirty candidates; unknown router.
  • tests/test_tool_registry.py updated for the new tool.
  • black --check . clean. Full suite passes except the two test_token_security tests that use os.fchmod, which fail only on Windows and fail identically on current main.
  • Live test on a vSRX through streamable-http:
    1. dry_run: true → commit check passed, diff returned, no new commit in show system commit.
    2. confirm_timeout_mins: 2, not confirmed → show system commit showed commit confirmed, rollback in 2mins / rollback pending; after 2 minutes the device rolled back automatically (by root via other) and the config diff against the pre-test rollback was empty.
    3. confirm_timeout_mins: 2 then confirm_commit → change still present after the 2-minute window.

🤖 Generated with Claude Code

- load_and_commit_config always runs a commit check before committing;
  a failed check rolls back the candidate and commits nothing.
- load_and_commit_config gains dry_run (check + diff + rollback).
- load_and_commit_config and render_and_apply_j2_template gain
  confirm_timeout_mins (commit confirmed N, validated to 1..65535).
- New confirm_commit tool confirms a pending commit confirmed; it refuses
  when the candidate holds uncommitted changes.
- Unit tests in tests/test_commit_confirmed.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fastrevmd-lab pushed a commit to mechubsec/fwskillsshare that referenced this pull request Oct 2, 2026
…it confirmed (#94)

Add junos-mcp-server with commit confirmed (jgrizzuti/junos-mcp-server fork,
proposed upstream as Juniper/junos-mcp-server#34) as a third server in both
capability mappings. It is identified by its confirm_commit tool; both skills
warn that re-sending load_and_commit_config does not confirm there.
srx-mnha-builder pushes stages with a 10-minute confirmed commit and lets an
unverified stage roll back.

Field-confirmed on vSRX 24.4R2.21, 2026-10-02.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@axlev

axlev commented Oct 4, 2026

Copy link
Copy Markdown

Glad someone did this - letting an agent commit to network device with no net has always made me uneasy, and the clean-candidate guard in confirm_commit is a nice touch. Out of curiosity: what pushed you to write it - an actual incident, or just that it felt wrong without one? I'm working on the layer above this (tracking the pending commit, confirming from a probe rather than a human), so the "why" interests me as much as the code.

@jgrizzuti

jgrizzuti commented Oct 5, 2026 via email •

Copy link
Copy Markdown
Contributor Author

@axlev

axlev commented Oct 5, 2026

Copy link
Copy Markdown

That explains the PR better than any incident would: you brought the Junos habit with you. On an HA pair, commit confirmed is the first thing most of us reach for, and it's telling that the agent tool didn't have it until you added it.

On the sandbox finding, I think two things, for what they're worth. Since it's a vendor repository, I'd probably send it to Juniper's security contact as well, so it reaches the right people and not only this thread. And if I were writing the fix, I'd look at jinja2's ImmutableSandboxedEnvironment and at passing only plain data into the template context, not Python objects; the sandbox narrows the paths, not exposing objects removes them. Happy to look at the change when you open it.

One question, since you're building the MNHA skill: is that aimed at real SRX pairs, or lab for now? I'm trying to understand how far people are letting agents apply changes today.

And welcome to collaborating on GitHub; this PR is a strong start :-)

@nileshsimaria nileshsimaria left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this — the feature is well scoped, follows the existing cleanup patterns, and the clean-candidate guard in confirm_commit is the right design. Tests and Black pass for me locally.

Requesting changes for one design issue: on Junos, commit check confirms a pending commit confirmed, so the new dry_run (and the always-on commit check) can silently cancel a rollback timer. Details inline, along with a few smaller items. A single "is a confirmed commit pending?" helper would fix the two main findings together.

Non-blocking follow-up: render_and_apply_j2_template with confirm_timeout_mins can start timers on several routers, but confirm_commit takes one router. router_names support there would be a natural next step.

Comment thread jmcp.py Outdated
# Validate before committing. PyEZ raises CommitError on a
# failed check; a False return is handled the same way.
try:
check_ok = config_util.commit_check()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Blocking: on Junos, a commit check issued inside the confirm window confirms a pending commit confirmed, the same as a plain commit does. That makes dry_run not side-effect-free:

  1. load_and_commit_config(confirm_timeout_mins=2, ...)
  2. load_and_commit_config(dry_run=true, ...) with any other change
  3. The first commit is now permanent, while the dry-run result says "Nothing was committed".

The same applies to a commit confirmed issued by a human on the CLI, and to the dry-run path in render_and_apply_j2_template (_dry_run_commit_check).

Could you verify this on your vSRX with the steps above, then check show system commit? If it reproduces, I'd suggest a small helper that queries junos_device.rpc.get_commit_information() and reports whether the latest commit is still an unconfirmed commit confirmed, called before commit_check() in both handlers. If one is pending, refuse with a message pointing to confirm_commit. Please also check how the pending state appears in the RPC reply when a commit comment is set.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reproduced and fixed. A pending-commit check now runs under the lock before loading in both handlers; dry run, check and commit are refused while one is pending.

Comment thread jmcp.py Outdated
Comment on lines +1868 to +1882
pending = config_util.diff()
if pending:
config_util.unlock()
return (
f"Failed to confirm commit on {router_name}: the "
"candidate configuration has uncommitted changes. "
"Nothing was committed; review them first:\n"
f"{pending}"
)
config_util.commit(comment=commit_comment, timeout=timeout)
config_util.unlock()
return (
f"Commit confirmed on {router_name}. The automatic "
"rollback is cancelled."
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Blocking: this only checks that the candidate is clean. It never checks that a confirmed commit is actually pending. If the window already expired and the device rolled back, the diff is empty, so this issues a no-op commit and returns "Commit confirmed ... The automatic rollback is cancelled." The caller believes the change is live when it has been reverted.

The helper from my comment on the commit check covers this too: if nothing is pending, return something like "No pending commit confirmed on {router_name}; nothing to confirm" (worded so _is_error_content flags it).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed. It now returns "Error: No pending commit confirmed on X; nothing to confirm…", which _is_error_content flags.

Comment thread jmcp.py
Comment on lines +1883 to +1893
except Exception as e:
try:
config_util.unlock()
except Exception:
# Same reasoning as _load_and_commit_sync: never hand
# a possibly-locked session back to the pool.
try:
junos_device.close()
except Exception:
pass
return f"Failed to confirm commit on {router_name}: {e}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If commit() raises RpcTimeoutError and the unlock() here succeeds, we return an error string and the session goes back to the pool. ConnectionPool.get_connection only evicts on RpcTimeoutError when the exception propagates out of the with block, so the pool keeps a session its own comment says is unreliable. Suggest closing the device explicitly in that case:

except Exception as e:
    try:
        config_util.unlock()
    except Exception:
        ...
    if isinstance(e, RpcTimeoutError):
        try:
            junos_device.close()
        except Exception:
            pass
    return f"Failed to confirm commit on {router_name}: {e}"

The message should also say the confirm may or may not have landed, and to check show system commit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. The session is closed on a timeout, and the message says the confirm may or may not have landed and to check show system commit.

Comment thread jmcp.py Outdated
Comment on lines +1781 to +1788
if confirm_timeout is None:
config_util.commit(comment=commit_comment, timeout=timeout)
else:
config_util.commit(
comment=commit_comment,
confirm=confirm_timeout,
timeout=timeout,
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: PyEZ's commit() does if confirm:, so confirm=None behaves exactly like omitting it. Both branches collapse to one call, here and in _apply_rendered_config_sync:

config_util.commit(
    comment=commit_comment, confirm=confirm_timeout, timeout=timeout
)

The tests asserting assertNotIn("confirm", kwargs) would become assertIsNone(kwargs.get("confirm")).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in both places. The tests now assert confirm is None.

Comment thread jmcp.py Outdated
if confirm_timeout is None:
config_util.commit(comment=commit_comment, timeout=timeout)
else:
config_util.commit(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If this confirmed commit times out (RpcTimeoutError), it may still have been applied on the device, with a rollback timer running that the caller doesn't know about. The generic "Failed to load/commit configuration" message hides that. When confirm_timeout is set and the failure is a timeout, please tell the caller to check show system commit and call confirm_commit if the commit landed. Same for the J2 path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done for both tools. The message says it may still have landed, and to check show system commit and call confirm_commit.

Comment thread jmcp.py Outdated
if confirm_timeout is None:
return ""
return (
f"\n\n⏳ Commit confirmed: {router_name} will automatically roll back in "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This note and the confirm_commit success message both start with "Commit confirmed" but mean opposite things: here the rollback is still armed, there it is cancelled. That is easy for an LLM client to misread. Suggest something like:

  • here: "⏳ Commit is pending confirmation: {router} will automatically roll back in N minute(s) unless confirm_commit is called for it."
  • in confirm_commit: "Pending commit on {router} confirmed; automatic rollback cancelled."

test_commit_check_runs_before_plain_commit asserts on the "Commit confirmed" string and would need updating.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to "Commit is pending confirmation…" and "Pending commit on X confirmed; automatic rollback cancelled."

Comment thread jmcp.py Outdated
except CommitError as ce:
check_ok = False
check_error = str(ce)
if not check_ok or dry_run:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: in PyEZ 2.7.4 commit_check() never returns False. It returns True, raises, or (in an ncclient error corner case) returns a truthy error dict, which not check_ok would treat as a pass. if check_ok is not True or dry_run: is more robust, and the comment above could drop the "a False return" wording.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now is not True in all three places, plus a test for the error-dict case.

Comment thread jmcp.py
Comment on lines +2281 to +2285
"description": (
"Commit with 'commit confirmed N': the device "
"automatically rolls back after N minutes unless "
"confirm_commit is called. Omit for a plain commit."
),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: the J2 tool's description says "Ignored when dry_run=true"; this one doesn't. Please add it here too. Once the pending-commit guard is in, the dry_run description should also say what happens when a confirmed commit is pending.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"Ignored when dry_run=true" added, and the pending-commit refusal described on both tools and on confirm_commit.

self.mock_config_cls.assert_not_called()


class ConfirmCommitTests(_DeviceTestCase):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few cases worth adding:

  • commit_check() raising a non-CommitError (RpcTimeoutError / RpcError): rollback, unlock, and error text.
  • confirm_commit when lock() fails, and when commit() raises (covers the unlock and close() eviction path).
  • Once the pending-commit guard exists: dry run refused while a confirmed commit is pending, and confirm_commit with nothing pending.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

All added: non-CommitError in the commit check, confirm_commit lock failure, commit errors (including unlock failure and timeout), the refusal while pending, and nothing pending.

@jgrizzuti

Copy link
Copy Markdown
Contributor Author

That explains the PR better than any incident would: you brought the Junos habit with you. On an HA pair, commit confirmed is the first thing most of us reach for, and it's telling that the agent tool didn't have it until you added it.

On the sandbox finding, I think two things, for what they're worth. Since it's a vendor repository, I'd probably send it to Juniper's security contact as well, so it reaches the right people and not only this thread. And if I were writing the fix, I'd look at jinja2's ImmutableSandboxedEnvironment and at passing only plain data into the template context, not Python objects; the sandbox narrows the paths, not exposing objects removes them. Happy to look at the change when you open it.

One question, since you're building the MNHA skill: is that aimed at real SRX pairs, or lab for now? I'm trying to understand how far people are letting agents apply changes today.

And welcome to collaborating on GitHub; this PR is a strong start :-)

I've been working on my demo lab with the JMCP, but I will present it to my colleagues, partners, and customers to start using it - I think the loop for operational automation starts with tested Skills, MCP Servers, and Human Review .

On Junos a `commit check` or `commit` inside the confirm window confirms a
pending `commit confirmed`, so dry_run and the always-on commit check could
silently cancel a rollback timer (reproduced on vSRX 24.4R2.21).

- Add _commit_confirmed_pending(): entry 0 of get-commit-information carries
  <commit-confirmed>rollback pending</commit-confirmed> while pending, with
  or without a commit comment; the <comment> text survives confirmation and
  is not used.
- load_and_commit_config and render_and_apply_j2_template check it under the
  lock before loading and refuse (dry run included) while one is pending.
- confirm_commit errors when nothing is pending instead of reporting a
  cancelled rollback after the device already rolled back.
- Warn that a timed-out commit confirmed may have landed; evict the pooled
  session after an RpcTimeoutError that is handled in place.
- Distinct messages for "pending confirmation" and "confirmed"; single
  commit() call; treat anything but True from commit_check() as failure;
  schema text for the refusal and dry_run/confirm interaction.
- Tests with fixtures from captured replies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jgrizzuti

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review, @nileshsimaria. You were right on the blocking issue.

Reproduced first on vSRX 24.4R2.21: I ran load_and_commit_config with confirm_timeout_mins: 3, then a dry_run with a different change inside the window. rollback pending disappeared with no new commit entry, the rollback never fired, and the change was still there after the window, while the dry run reported "Nothing was committed".

RPC reply, captured with get-commit-information:

  • A pending commit has <commit-confirmed>rollback pending</commit-confirmed> on entry 0, both with a commit comment (the comment goes in <log>) and without one.
  • After the commit is confirmed, even by a commit check, the <comment>commit confirmed, rollback in N mins</comment> text stays but the marker disappears. So the helper checks the marker, not the comment.
  • After an automatic rollback there's a new entry 0 by root/other.

Pushed in e3ad5a4:

  • _commit_confirmed_pending() runs under the config lock, before loading, in both handlers. Dry run, commit check and commit are all refused while a commit confirmed is pending.
  • confirm_commit returns an error when nothing is pending.
  • The timeout warnings and session eviction, the message wording, the single commit() call, is not True, the schema text, and the extra tests (fixtures built from the captured replies).

Live test with the fix, same vSRX:

  1. Commit confirmed.
  2. Dry run inside the window: refused, and rollback pending intact.
  3. confirm_commit: confirmed.
  4. confirm_commit again: "No pending commit confirmed".
  5. Dry run allowed again after confirming.

router_names for confirm_commit I'd suggest as a follow-up PR.

@jgrizzuti

Copy link
Copy Markdown
Contributor Author

Friendly ping: all review items are addressed in e3ad5a4 and live-tested on vSRX. Happy to make further changes.

@nileshsimaria

Copy link
Copy Markdown
Collaborator

Looks good

@nileshsimaria
nileshsimaria merged commit f6f06e1 into Juniper:main Nov 2, 2026
fastrevmd-lab added a commit to mechubsec/fwskillsshare that referenced this pull request Nov 15, 2026
…confirms a pending commit (#115)

* fix(srx-ips, srx-mnha-builder, srx-mnha-ipsec-builder): commit check confirms a pending commit

Juniper/junos-mcp-server#34 merged on 2026-11-02, so the "junos-mcp-server
with commit confirmed" column now points to Juniper's own main (no tagged
release yet) instead of a third-party fork.

On Junos a commit check or commit inside a confirm window confirms the
pending commit. All three skills now say to run only read-only commands on
a device while a commit confirmed is pending there; this matters most for
the IPsec builder's self-restoring uplink test. The merged server's refusal
and "No pending commit confirmed" messages are documented.

Lab-verified on vSRX 24.4R2.21, 2026-10-05.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* review: "nothing pending" means rolled back or already confirmed

Applies the maintainer's #115 review suggestions: a `No pending commit
confirmed` reply does not prove the change rolled back. A lost confirm
reply, another session, or a stray commit check also leaves it live with
nothing pending, so compare with the baseline before reporting either.
Also clarifies the traffic-generation snippet's early restore, and the
CHANGELOG entry's wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: fastrevmd-lab <fastrevmd@gmail.com>
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.

3 participants